@@ -123,15 +123,14 @@ export default class Signaling extends events.EventEmitter {
123123        this . socket . send ( JSON . stringify ( data ) ) ; 
124124    } 
125125
126-     invite  =  ( peer_id ,  type )  =>  { 
126+     invite  =  ( peer_id ,  media )  =>  { 
127127        this . session_id  =  this . self_id  +  '-'  +  peer_id ; 
128-         let  message  =  { 
129-             type : 'invite' , 
130-             session_id : this . session_id , 
131-             to : peer_id , 
132-             media : type , 
133-         } 
134-         this . send ( message ) ; 
128+         this . getLocalStream ( media ) . then ( ( stream )  =>  { 
129+             this . local_stream  =  stream ; 
130+             this . createPeerConnection ( peer_id ,  media ,  true ,  stream ) ; 
131+             this . emit ( 'localstream' ,  stream ) ; 
132+             this . emit ( 'new_call' ,  this . self_id ,  this . session_id ) ; 
133+         } ) ; 
135134    } 
136135
137136    bye  =  ( )  =>  { 
@@ -143,41 +142,15 @@ export default class Signaling extends events.EventEmitter {
143142        this . send ( message ) ; 
144143    } 
145144
146-     onRinging  =  ( message )  =>  { 
147-         var  data  =  message . data ; 
148-         var  id  =  data . id ; 
149-         var  media  =  data . media ; 
150-         console . log ( "Remote peer ready: id "  +  id ) ; 
151-         this . emit ( 'ringing' ,  id ) ; 
152-         this . getLocalStream ( media ) . then ( ( stream )  =>  { 
153-             this . local_stream  =  stream ; 
154-             this . createPeerConnection ( id ,  true ,  stream ) ; 
155-             this . emit ( 'localstream' ,  stream ) ; 
156-         } ) ; 
157-     } 
158- 
159-     onInvite  =  ( message )  =>  { 
160-         var  data  =  message . data ; 
161-         var  from  =  data . from ; 
162-         console . log ( "data:"  +  data ) ; 
163-         var  media  =  data . media ; 
164-         this . session_id  =  data . session_id ; 
165-         this . emit ( 'invite' ,  from ,  this . session_id ) ; 
166-         this . getLocalStream ( media ) . then ( ( stream )  =>  { 
167-             this . local_stream  =  stream ; 
168-             this . emit ( 'localstream' ,  stream ) ; 
169-             var  pc  =  this . createPeerConnection ( from ,  false ,  stream ) ; 
170-         } ) ; 
171-     } 
172- 
173-     createOffer  =  ( pc ,  id )  =>  { 
145+     createOffer  =  ( pc ,  id ,  media )  =>  { 
174146        pc . createOffer ( ( desc )  =>  { 
175147            console . log ( 'createOffer: ' ,  desc . sdp ) ; 
176148            pc . setLocalDescription ( desc ,  ( )  =>  { 
177149                console . log ( 'setLocalDescription' ,  pc . localDescription ) ; 
178150                let  message  =  { 
179151                    type : 'offer' , 
180152                    to : id , 
153+                     media : media , 
181154                    description : pc . localDescription , 
182155                    session_id : this . session_id , 
183156                } 
@@ -186,7 +159,7 @@ export default class Signaling extends events.EventEmitter {
186159        } ,  this . logError ) ; 
187160    } 
188161
189-     createPeerConnection  =  ( id ,  isOffer ,  localstream )  =>  { 
162+     createPeerConnection  =  ( id ,  media ,   isOffer ,  localstream )  =>  { 
190163        var  pc  =  new  RTCPeerConnection ( configuration ) ; 
191164        this . peer_connections [ ""  +  id ]  =  pc ; 
192165        pc . onicecandidate  =  ( event )  =>  { 
@@ -229,7 +202,7 @@ export default class Signaling extends events.EventEmitter {
229202        pc . addStream ( localstream ) ; 
230203
231204        if  ( isOffer ) 
232-             this . createOffer ( pc ,  id ) ; 
205+             this . createOffer ( pc ,  id ,   media ) ; 
233206        return  pc ; 
234207    } 
235208
@@ -269,33 +242,36 @@ export default class Signaling extends events.EventEmitter {
269242    onOffer  =  ( message )  =>  { 
270243        var  data  =  message . data ; 
271244        var  from  =  data . from ; 
245+         console . log ( "data:"  +  data ) ; 
246+         var  media  =  data . media ; 
247+         this . session_id  =  data . session_id ; 
248+         this . emit ( 'new_call' ,  from ,  this . session_id ) ; 
272249
273-         console . log ( "data.from:"   +   data . from ) ; 
274- 
275-         var   pc   =   null ; 
276-         if   ( from   in   this . peer_connections )   { 
277-              pc   =   this . peer_connections [ from ] ; 
278-         } 
279-         if   ( pc   &&   data . description )   { 
280-             //console.log('on offer sdp', data); 
281-             pc . setRemoteDescription ( new   RTCSessionDescription ( data . description ) ,   ( )   =>   { 
282-                 if   ( pc . remoteDescription . type   ==   "offer" ) 
283-                     pc . createAnswer ( ( desc )   =>   { 
284-                         console . log ( 'createAnswer: ' ,   desc . description ) ; 
285-                         pc . setLocalDescription ( desc ,   ( )   =>   { 
286-                             console . log ( 'setLocalDescription' ,   pc . localDescription ) ; 
287-                             let   message   =   { 
288-                                 type :  'answer' , 
289-                                 to :  from , 
290-                                 description :  pc . localDescription , 
291-                                 session_id :  this . session_id , 
292-                             } 
293-                             this . send ( message ) ; 
250+         this . getLocalStream ( media ) . then ( ( stream )   =>   { 
251+              this . local_stream   =   stream ; 
252+              this . emit ( 'localstream' ,   stream ) ; 
253+              var   pc   =   this . createPeerConnection ( from ,   media ,   false ,   stream ) ; 
254+ 
255+              if   ( pc   &&   data . description )   { 
256+                  //console.log('on offer sdp',  data); 
257+                  pc . setRemoteDescription ( new   RTCSessionDescription ( data . description ) ,   ( )   =>   { 
258+                      if   ( pc . remoteDescription . type   ==   "offer" ) 
259+                          pc . createAnswer ( ( desc )   =>   { 
260+                              console . log ( 'createAnswer: ' ,   desc ) ; 
261+                              pc . setLocalDescription ( desc ,   ( )   =>   { 
262+                                  console . log ( 'setLocalDescription' ,   pc . localDescription ) ; 
263+                                  let   message   =   { 
264+                                      type :  'answer' , 
265+                                      to :  from , 
266+                                      description :  pc . localDescription , 
267+                                      session_id :  this . session_id , 
268+                                 } 
269+                                  this . send ( message ) ; 
270+                             } ,   this . logError ) ; 
294271                        } ,  this . logError ) ; 
295-                     } ,  this . logError ) ; 
296-             } ,  this . logError ) ; 
297-         } 
298- 
272+                 } ,  this . logError ) ; 
273+             } 
274+         } ) ; 
299275    } 
300276
301277    onAnswer  =  ( message )  =>  { 
@@ -305,7 +281,6 @@ export default class Signaling extends events.EventEmitter {
305281        if  ( from  in  this . peer_connections )  { 
306282            pc  =  this . peer_connections [ from ] ; 
307283        } 
308- 
309284        if  ( pc  &&  data . description )  { 
310285            //console.log('on answer sdp', data); 
311286            pc . setRemoteDescription ( new  RTCSessionDescription ( data . description ) ,  ( )  =>  { 
@@ -351,7 +326,7 @@ export default class Signaling extends events.EventEmitter {
351326        if  ( pc  !==  undefined )  { 
352327            pc . close ( ) ; 
353328            delete  peerConnections [ to ] ; 
354-             this . emit ( 'bye ' ,  to ,  this . session_id ) ; 
329+             this . emit ( 'call_end ' ,  to ,  this . session_id ) ; 
355330        } 
356331        if  ( this . local_stream  !=  null )  { 
357332            this . closeMediaStream ( this . local_stream ) ; 
@@ -364,11 +339,6 @@ export default class Signaling extends events.EventEmitter {
364339        console . log ( "logError" ,  error ) ; 
365340    } 
366341
367-     invitePeer  =  ( peer_id ,  type )  =>  { 
368-         this . invite ( peer_id ,  type ) ; 
369-         this . getLocalStream ( type ) ; 
370-     } 
371- 
372342    sendText ( )  { 
373343        var  text  =  "test send text..." ; //document.getElementById('textRoomInput').value; 
374344        if  ( text  ==  "" )  { 
0 commit comments