@@ -43,7 +43,6 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
4343 this . _pendingCandidates = [ ] ;
4444 this . _subscribePromises = new Map ( ) ; // internalId => { resolve, reject }
4545 this . _publishPromises = new Map ( ) ; // internalId => { resolve, reject }
46- this . _subscribedStreams = new Map ( ) ; // intenalId => RemoteStream
4746 this . _publications = new Map ( ) ; // PublicationId => Publication
4847 this . _subscriptions = new Map ( ) ; // SubscriptionId => Subscription
4948 this . _publishTransceivers = new Map ( ) ; // internalId => { id, transceivers: [Transceiver] }
@@ -52,15 +51,15 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
5251 // Timer for PeerConnection disconnected. Will stop connection after timer.
5352 this . _disconnectTimer = null ;
5453 this . _ended = false ;
55- this . _stopped = false ;
5654 // Channel ID assigned by conference
57- this . _id = undefined ;
55+ this . _transportId = undefined ;
5856 // Used to create internal ID for publication/subscription
5957 this . _internalCount = 0 ;
6058 this . _sdpPromise = Promise . resolve ( ) ;
6159 this . _sdpResolverMap = new Map ( ) ; // internalId => {finish, resolve, reject}
6260 this . _sdpResolvers = [ ] ; // [{finish, resolve, reject}]
6361 this . _sdpResolveNum = 0 ;
62+ this . _remoteMediaStream = new Map ( ) ; // Key is subscription ID, value is MediaStream.
6463 }
6564
6665 /**
@@ -300,11 +299,11 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
300299 return this . _signaling . sendSignalingMessage ( 'publish' , {
301300 media : { tracks : trackOptions } ,
302301 attributes : stream . attributes ,
303- transport : { id : this . _id , type : 'webrtc' } ,
302+ transport : { id : this . _transportId , type : 'webrtc' } ,
304303 } ) . catch ( ( e ) => {
305304 // Send SDP even when failed to get Answer.
306305 this . _signaling . sendSignalingMessage ( 'soac' , {
307- id : this . _id ,
306+ id : this . _transportId ,
308307 signaling : localDesc ,
309308 } ) ;
310309 throw e ;
@@ -320,10 +319,10 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
320319 this . _publishTransceivers . get ( internalId ) . id = publicationId ;
321320 this . _reverseIdMap . set ( publicationId , internalId ) ;
322321
323- if ( this . _id && this . _id !== data . transportId ) {
322+ if ( this . _transportId && this . _transportId !== data . transportId ) {
324323 Logger . warning ( 'Server returns conflict ID: ' + data . transportId ) ;
325324 }
326- this . _id = data . transportId ;
325+ this . _transportId = data . transportId ;
327326
328327 // Modify local SDP before sending
329328 if ( options ) {
@@ -335,7 +334,7 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
335334 } ) ;
336335 }
337336 this . _signaling . sendSignalingMessage ( 'soac' , {
338- id : this . _id ,
337+ id : this . _transportId ,
339338 signaling : localDesc ,
340339 } ) ;
341340 } ) . catch ( ( e ) => {
@@ -483,7 +482,6 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
483482 offerOptions . offerToReceiveVideo = ! ! options . video ;
484483 }
485484 this . _subscribeTransceivers . set ( internalId , { transceivers} ) ;
486- this . _subscribedStreams . set ( internalId , stream ) ;
487485
488486 let localDesc ;
489487 this . _pc . createOffer ( offerOptions ) . then ( ( desc ) => {
@@ -510,11 +508,11 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
510508 } ) ;
511509 return this . _signaling . sendSignalingMessage ( 'subscribe' , {
512510 media : { tracks : trackOptions } ,
513- transport : { id : this . _id , type : 'webrtc' } ,
511+ transport : { id : this . _transportId , type : 'webrtc' } ,
514512 } ) . catch ( ( e ) => {
515513 // Send SDP even when failed to get Answer.
516514 this . _signaling . sendSignalingMessage ( 'soac' , {
517- id : this . _id ,
515+ id : this . _transportId ,
518516 signaling : localDesc ,
519517 } ) ;
520518 throw e ;
@@ -529,10 +527,10 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
529527
530528 this . _subscribeTransceivers . get ( internalId ) . id = subscriptionId ;
531529 this . _reverseIdMap . set ( subscriptionId , internalId ) ;
532- if ( this . _id && this . _id !== data . transportId ) {
530+ if ( this . _transportId && this . _transportId !== data . transportId ) {
533531 Logger . warning ( 'Server returns conflict ID: ' + data . transportId ) ;
534532 }
535- this . _id = data . transportId ;
533+ this . _transportId = data . transportId ;
536534
537535 // Modify local SDP before sending
538536 if ( options ) {
@@ -542,7 +540,7 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
542540 } ) ;
543541 }
544542 this . _signaling . sendSignalingMessage ( 'soac' , {
545- id : this . _id ,
543+ id : this . _transportId ,
546544 signaling : localDesc ,
547545 } ) ;
548546 } ) . catch ( ( e ) => {
@@ -674,11 +672,6 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
674672 new ConferenceError ( 'Failed to subscribe' ) ) ;
675673 }
676674 }
677- // Clear media stream
678- if ( this . _subscribedStreams . has ( internalId ) ) {
679- this . _subscribedStreams . get ( internalId ) . mediaStream = null ;
680- this . _subscribedStreams . delete ( internalId ) ;
681- }
682675 if ( this . _sdpResolverMap . has ( internalId ) ) {
683676 const resolver = this . _sdpResolverMap . get ( internalId ) ;
684677 if ( ! resolver . finish ) {
@@ -732,31 +725,24 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
732725
733726 _onRemoteStreamAdded ( event ) {
734727 Logger . debug ( 'Remote stream added.' ) ;
735- let find = false ;
736728 for ( const [ internalId , sub ] of this . _subscribeTransceivers ) {
737- const subscriptionId = sub . id ;
738729 if ( sub . transceivers . find ( ( t ) => t . transceiver === event . transceiver ) ) {
739- find = true ;
740- const subscribedStream = this . _subscribedStreams . get ( internalId ) ;
741- if ( ! subscribedStream . mediaStream ) {
742- this . _subscribedStreams . get ( internalId ) . mediaStream =
743- event . streams [ 0 ] ;
744- // Resolve subscription if ready handler has been called
745- const subscription = this . _subscriptions . get ( subscriptionId ) ;
746- if ( subscription ) {
730+ if ( this . _subscriptions . has ( sub . id ) ) {
731+ const subscription = this . _subscriptions . get ( sub . id ) ;
732+ subscription . stream = event . streams [ 0 ] ;
733+ if ( this . _subscribePromises . has ( internalId ) ) {
747734 this . _subscribePromises . get ( internalId ) . resolve ( subscription ) ;
735+ this . _subscribePromises . delete ( internalId ) ;
748736 }
749737 } else {
750- // Add track to the existing stream
751- subscribedStream . mediaStream . addTrack ( event . track ) ;
738+ this . _remoteMediaStream . set ( sub . id , event . streams [ 0 ] ) ;
752739 }
740+ return ;
753741 }
754742 }
755- if ( ! find ) {
756- // This is not expected path. However, this is going to happen on Safari
757- // because it does not support setting direction of transceiver.
758- Logger . warning ( 'Received remote stream without subscription.' ) ;
759- }
743+ // This is not expected path. However, this is going to happen on Safari
744+ // because it does not support setting direction of transceiver.
745+ Logger . warning ( 'Received remote stream without subscription.' ) ;
760746 }
761747
762748 _onLocalIceCandidate ( event ) {
@@ -840,7 +826,7 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
840826
841827 _sendCandidate ( candidate ) {
842828 this . _signaling . sendSignalingMessage ( 'soac' , {
843- id : this . _id ,
829+ id : this . _transportId ,
844830 signaling : {
845831 type : 'candidate' ,
846832 candidate : {
@@ -887,25 +873,21 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
887873 }
888874
889875 _readyHandler ( sessionId ) {
876+ Logger . debug ( 'Received ready of ' + sessionId ) ;
890877 const internalId = this . _reverseIdMap . get ( sessionId ) ;
891878 if ( this . _subscribePromises . has ( internalId ) ) {
892- const subscription = new Subscription ( sessionId , ( ) => {
879+ const mediaStream = this . _remoteMediaStream . get ( sessionId ) ;
880+ const subscription = new Subscription ( sessionId , mediaStream , ( ) => {
893881 this . _unsubscribe ( internalId ) ;
894882 } , ( ) => this . _getStats ( ) ,
895883 ( trackKind ) => this . _muteOrUnmute ( sessionId , true , false , trackKind ) ,
896884 ( trackKind ) => this . _muteOrUnmute ( sessionId , false , false , trackKind ) ,
897885 ( options ) => this . _applyOptions ( sessionId , options ) ) ;
898886 this . _subscriptions . set ( sessionId , subscription ) ;
899- // Fire subscription's ended event when associated stream is ended.
900- this . _subscribedStreams . get ( internalId ) . addEventListener ( 'ended' , ( ) => {
901- if ( this . _subscriptions . has ( sessionId ) ) {
902- this . _subscriptions . get ( sessionId ) . dispatchEvent (
903- 'ended' , new OwtEvent ( 'ended' ) ) ;
904- }
905- } ) ;
906- // Resolve subscription if mediaStream is ready
907- if ( this . _subscribedStreams . get ( internalId ) . mediaStream ) {
887+ // Resolve subscription if mediaStream is ready.
888+ if ( this . _subscriptions . get ( sessionId ) . stream ) {
908889 this . _subscribePromises . get ( internalId ) . resolve ( subscription ) ;
890+ this . _subscribePromises . delete ( internalId ) ;
909891 }
910892 } else if ( this . _publishPromises . has ( internalId ) ) {
911893 const publication = new Publication ( sessionId , ( ) => {
@@ -1056,21 +1038,23 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
10561038 return sdp ;
10571039 }
10581040
1059- // Handle stream event sent from MCU. Some stream events should be publication
1060- // event or subscription event. It will be handled here.
1041+ // Handle stream event sent from MCU. Some stream update events sent from
1042+ // server, more specifically audio.status and video.status events should be
1043+ // publication event or subscription events. They don't change MediaStream's
1044+ // status. See
1045+ // https://github.com/open-webrtc-toolkit/owt-server/blob/master/doc/Client-Portal%20Protocol.md#339-participant-is-notified-on-streams-update-in-room
1046+ // for more information.
10611047 _onStreamEvent ( message ) {
10621048 const eventTargets = [ ] ;
10631049 if ( this . _publications . has ( message . id ) ) {
10641050 eventTargets . push ( this . _publications . get ( message . id ) ) ;
10651051 }
1066- for ( const [ internalId , subscribedStream ] of this . _subscribedStreams ) {
1067- if ( message . id === subscribedStream . id ) {
1068- const subscriptionId = this . _subscribeTransceivers . get ( internalId ) . id ;
1069- eventTargets . push ( this . _subscriptions . get ( subscriptionId ) ) ;
1070- break ;
1052+ for ( const subscription of this . _subscriptions ) {
1053+ if ( message . id === subscription . _audioTrackId ||
1054+ message . id === subscription . _videoTrackId ) {
1055+ eventTargets . push ( subscription ) ;
10711056 }
10721057 }
1073-
10741058 if ( ! eventTargets . length ) {
10751059 return ;
10761060 }
@@ -1100,9 +1084,9 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
11001084 // Only check the first one.
11011085 const param = obj [ 0 ] ;
11021086 return ! ! (
1103- param . codecPayloadType || param . dtx || param . active || param . ptime ||
1104- param . maxFramerate || param . scaleResolutionDownBy || param . rid ||
1105- param . scalabilityMode ) ;
1087+ param . codecPayloadType || param . dtx || param . active || param . ptime ||
1088+ param . maxFramerate || param . scaleResolutionDownBy || param . rid ||
1089+ param . scalabilityMode ) ;
11061090 }
11071091
11081092 _isOwtEncodingParameters ( obj ) {
0 commit comments