11use crate :: { config, tcp, websocket, ResultType } ;
2+ #[ cfg( feature = "webrtc" ) ]
3+ use crate :: webrtc;
24use sodiumoxide:: crypto:: secretbox:: Key ;
35use std:: net:: SocketAddr ;
46use tokio:: net:: TcpStream ;
57
68// support Websocket and tcp.
79pub enum Stream {
10+ #[ cfg( feature = "webrtc" ) ]
11+ WebRTC ( webrtc:: WebRTCStream ) ,
812 WebSocket ( websocket:: WsFramedStream ) ,
913 Tcp ( tcp:: FramedStream ) ,
1014}
@@ -13,6 +17,8 @@ impl Stream {
1317 #[ inline]
1418 pub fn set_send_timeout ( & mut self , ms : u64 ) {
1519 match self {
20+ #[ cfg( feature = "webrtc" ) ]
21+ Stream :: WebRTC ( s) => s. set_send_timeout ( ms) ,
1622 Stream :: WebSocket ( s) => s. set_send_timeout ( ms) ,
1723 Stream :: Tcp ( s) => s. set_send_timeout ( ms) ,
1824 }
@@ -21,6 +27,8 @@ impl Stream {
2127 #[ inline]
2228 pub fn set_raw ( & mut self ) {
2329 match self {
30+ #[ cfg( feature = "webrtc" ) ]
31+ Stream :: WebRTC ( s) => s. set_raw ( ) ,
2432 Stream :: WebSocket ( s) => s. set_raw ( ) ,
2533 Stream :: Tcp ( s) => s. set_raw ( ) ,
2634 }
@@ -29,6 +37,8 @@ impl Stream {
2937 #[ inline]
3038 pub async fn send_bytes ( & mut self , bytes : bytes:: Bytes ) -> ResultType < ( ) > {
3139 match self {
40+ #[ cfg( feature = "webrtc" ) ]
41+ Stream :: WebRTC ( s) => s. send_bytes ( bytes) . await ,
3242 Stream :: WebSocket ( s) => s. send_bytes ( bytes) . await ,
3343 Stream :: Tcp ( s) => s. send_bytes ( bytes) . await ,
3444 }
@@ -37,6 +47,8 @@ impl Stream {
3747 #[ inline]
3848 pub async fn send_raw ( & mut self , bytes : Vec < u8 > ) -> ResultType < ( ) > {
3949 match self {
50+ #[ cfg( feature = "webrtc" ) ]
51+ Stream :: WebRTC ( s) => s. send_raw ( bytes) . await ,
4052 Stream :: WebSocket ( s) => s. send_raw ( bytes) . await ,
4153 Stream :: Tcp ( s) => s. send_raw ( bytes) . await ,
4254 }
@@ -45,6 +57,8 @@ impl Stream {
4557 #[ inline]
4658 pub fn set_key ( & mut self , key : Key ) {
4759 match self {
60+ #[ cfg( feature = "webrtc" ) ]
61+ Stream :: WebRTC ( s) => s. set_key ( key) ,
4862 Stream :: WebSocket ( s) => s. set_key ( key) ,
4963 Stream :: Tcp ( s) => s. set_key ( key) ,
5064 }
@@ -53,6 +67,8 @@ impl Stream {
5367 #[ inline]
5468 pub fn is_secured ( & self ) -> bool {
5569 match self {
70+ #[ cfg( feature = "webrtc" ) ]
71+ Stream :: WebRTC ( s) => s. is_secured ( ) ,
5672 Stream :: WebSocket ( s) => s. is_secured ( ) ,
5773 Stream :: Tcp ( s) => s. is_secured ( ) ,
5874 }
@@ -64,6 +80,8 @@ impl Stream {
6480 timeout : u64 ,
6581 ) -> Option < Result < bytes:: BytesMut , std:: io:: Error > > {
6682 match self {
83+ #[ cfg( feature = "webrtc" ) ]
84+ Stream :: WebRTC ( s) => s. next_timeout ( timeout) . await ,
6785 Stream :: WebSocket ( s) => s. next_timeout ( timeout) . await ,
6886 Stream :: Tcp ( s) => s. next_timeout ( timeout) . await ,
6987 }
@@ -87,6 +105,8 @@ impl Stream {
87105 #[ inline]
88106 pub async fn send ( & mut self , msg : & impl protobuf:: Message ) -> ResultType < ( ) > {
89107 match self {
108+ #[ cfg( feature = "webrtc" ) ]
109+ Self :: WebRTC ( s) => s. send ( msg) . await ,
90110 Self :: WebSocket ( ws) => ws. send ( msg) . await ,
91111 Self :: Tcp ( tcp) => tcp. send ( msg) . await ,
92112 }
@@ -96,6 +116,8 @@ impl Stream {
96116 #[ inline]
97117 pub async fn next ( & mut self ) -> Option < Result < bytes:: BytesMut , std:: io:: Error > > {
98118 match self {
119+ #[ cfg( feature = "webrtc" ) ]
120+ Self :: WebRTC ( s) => s. next ( ) . await ,
99121 Self :: WebSocket ( ws) => ws. next ( ) . await ,
100122 Self :: Tcp ( tcp) => tcp. next ( ) . await ,
101123 }
@@ -104,6 +126,8 @@ impl Stream {
104126 #[ inline]
105127 pub fn local_addr ( & self ) -> SocketAddr {
106128 match self {
129+ #[ cfg( feature = "webrtc" ) ]
130+ Self :: WebRTC ( s) => s. local_addr ( ) ,
107131 Self :: WebSocket ( ws) => ws. local_addr ( ) ,
108132 Self :: Tcp ( tcp) => tcp. local_addr ( ) ,
109133 }
@@ -113,4 +137,13 @@ impl Stream {
113137 pub fn from ( stream : TcpStream , stream_addr : SocketAddr ) -> Self {
114138 Self :: Tcp ( tcp:: FramedStream :: from ( stream, stream_addr) )
115139 }
140+
141+ #[ inline]
142+ #[ cfg( feature = "webrtc" ) ]
143+ pub fn get_webrtc_stream ( & self ) -> Option < webrtc:: WebRTCStream > {
144+ match self {
145+ Self :: WebRTC ( s) => Some ( s. clone ( ) ) ,
146+ _ => None ,
147+ }
148+ }
116149}
0 commit comments