@@ -19,6 +19,8 @@ const webrtcPeerController = {
1919 newRequestWebRTC : RequestWebRTC ,
2020 currentReqRes : ReqRes
2121 ) => {
22+ // const enableAudio =
23+ // store.getState().newRequest.newRequestWebRTC.enableAudio ?? false;
2224 const enableAudio = newRequestWebRTC . enableAudio ?? false ; //if null set to false
2325
2426 let servers = {
@@ -35,7 +37,7 @@ const webrtcPeerController = {
3537 // request access to user camera
3638 video : true , // request access to video
3739 audio : enableAudio , //if enable audio is true request access to audio //not if false
38- } ) ; // from
40+ } ) ;
3941
4042 if ( document . getElementById ( 'localstream' ) ) {
4143 ( < HTMLVideoElement > document . getElementById ( 'localstream' ) ) . srcObject =
@@ -62,44 +64,90 @@ const webrtcPeerController = {
6264 enableAudio,
6365 } )
6466 ) ;
65- }
66- if ( newRequestWebRTC . webRTCDataChannel === 'Audio' ) {
67+ peerConnection . onicecandidate = async (
68+ event : RTCPeerConnectionIceEvent
69+ ) : Promise < void > => {
70+ if (
71+ event . candidate &&
72+ peerConnection . localDescription ! . type === 'offer'
73+ ) {
74+ appDispatch (
75+ newRequestWebRTCOfferSet (
76+ JSON . stringify ( peerConnection . localDescription )
77+ )
78+ ) ;
79+ } else if (
80+ event . candidate &&
81+ peerConnection . localDescription ! . type === 'answer'
82+ ) {
83+ appDispatch (
84+ newRequestWebRTCAnswerSet (
85+ JSON . stringify ( peerConnection . localDescription )
86+ )
87+ ) ;
88+ }
89+ } ;
90+ } else if ( newRequestWebRTC . webRTCDataChannel === 'Audio' ) {
6791 let localStream = await navigator . mediaDevices . getUserMedia ( {
68- // accesses user audio
69- video : false ,
70- audio : true ,
71- } ) ;
92+ // request access to user camera
93+ video : false , // request access to video
94+ audio : true , //if enable audio is true request access to audio //not if false
95+ } ) ; // from
96+
97+ if ( document . getElementById ( 'localstream' ) ) {
98+ ( < HTMLVideoElement > document . getElementById ( 'localstream' ) ) . srcObject =
99+ localStream ;
100+ }
72101
73- console . log ( 'Local Audio Tracks Captured:' , localStream . getTracks ( ) ) ;
74102 localStream . getTracks ( ) . forEach ( ( track ) => {
75103 // iterates over tracks in local stream
76- console . log ( `adding track: ${ track . kind } ` ) ;
77- peerConnection . addTrack ( track , localStream ) ; // adds them to the peer connection using add track
78- } ) ;
104+ peerConnection . addTrack ( track , localStream ) ;
105+ } ) ; // adds them to the peer connection using add track
79106
80- let remoteStream = new MediaStream ( ) ; // initialize new media stream object
81- //to hold audio tracks from remote peer
107+ let remoteStream = new MediaStream ( ) ; //initialize new media stream object
82108 peerConnection . ontrack = async ( event ) => {
83- // sets up an event handler fro on track event of peer connection object
84- console . log ( `recieved remote track: ${ event . track . kind } ` ) ;
109+ // adds them to the peer connection using add track
85110 event . streams [ 0 ] . getTracks ( ) . forEach ( ( track ) => {
86- // iterates over each track recieved to the remote stream object// returns array of tracks
111+ // sets up an event handler for on track event of peer connection object
112+ // returns array of tracks
87113 remoteStream . addTrack ( track ) ;
88114 } ) ;
89115 } ;
90116
91117 appDispatch (
92118 // update redux state
93119 newRequestWebRTCSet ( {
94- // new state object is created to update redux state
95- ...newRequestWebRTC , //copy properties from existing state object
120+ //new state object is created to update redux object
121+ ...newRequestWebRTC , // copy properties from existing object
96122 webRTCpeerConnection : peerConnection ,
97123 webRTCLocalStream : localStream ,
98- webRTCRemoteStream : remoteStream , //remote stream overwritten
124+ webRTCRemoteStream : remoteStream , // remote stream overwritten
99125 } )
100126 ) ;
101- }
102- if ( newRequestWebRTC . webRTCDataChannel === 'Text' ) {
127+ peerConnection . onicecandidate = async (
128+ event : RTCPeerConnectionIceEvent
129+ ) : Promise < void > => {
130+ if (
131+ event . candidate &&
132+ peerConnection . localDescription ! . type === 'offer'
133+ ) {
134+ appDispatch (
135+ newRequestWebRTCOfferSet (
136+ JSON . stringify ( peerConnection . localDescription )
137+ )
138+ ) ;
139+ } else if (
140+ event . candidate &&
141+ peerConnection . localDescription ! . type === 'answer'
142+ ) {
143+ appDispatch (
144+ newRequestWebRTCAnswerSet (
145+ JSON . stringify ( peerConnection . localDescription )
146+ )
147+ ) ;
148+ }
149+ } ;
150+ } else if ( newRequestWebRTC . webRTCDataChannel === 'Text' ) {
103151 // const { request, response } = currentReqRes as {
104152 // request: RequestWebRTCText;
105153 // response: ResponseWebRTCText;
0 commit comments