Skip to content

Commit

Permalink
Use ontrack and onremovetrack instead of onaddstream and onremovestre…
Browse files Browse the repository at this point in the history
…am (#1823)
  • Loading branch information
lodoyun authored Jul 20, 2022
1 parent d7935de commit 5c82bec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
20 changes: 13 additions & 7 deletions erizo_controller/erizoClient/src/ErizoConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,20 @@ class ErizoConnection extends EventEmitterConst {
// PeerConnection Events
if (this.stack.peerConnection) {
this.peerConnection = this.stack.peerConnection; // For backwards compatibility
this.stack.peerConnection.onaddstream = (evt) => {
this.emit(ConnectionEvent({ type: 'add-stream', stream: evt.stream }));
this.stack.peerConnection.ontrack = (trackEvt) => {
if (trackEvt.streams.length !== 1) {
log.warning('More that one mediaStream associated with track!');
}
const stream = trackEvt.streams[0];
stream.onremovetrack = () => {
if (stream.getTracks().length === 0) {
this.emit(ConnectionEvent({ type: 'remove-stream', stream }));
this.streamRemovedListener(stream.id);
}
};
this.emit(ConnectionEvent({ type: 'add-stream', stream }));
};

this.stack.peerConnection.onremovestream = (evt) => {
this.emit(ConnectionEvent({ type: 'remove-stream', stream: evt.stream }));
this.streamRemovedListener(evt.stream.id);
};

this.stack.peerConnection.oniceconnectionstatechange = () => {
const state = this.stack.peerConnection.iceConnectionState;
Expand Down Expand Up @@ -144,7 +150,7 @@ class ErizoConnection extends EventEmitterConst {
removeStream(stream) {
const streamId = stream.getID();
if (!this.streamsMap.has(streamId)) {
log.debug(`message: Cannot remove stream not in map, ${this.toLog()}, ${stream.toLog()}`);
log.warning(`message: Cannot remove stream not in map, ${this.toLog()}, ${stream.toLog()}`);
return;
}
this.streamsMap.remove(streamId);
Expand Down
14 changes: 8 additions & 6 deletions erizo_controller/erizoClient/src/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,19 @@ const Room = (altIo, altConnectionHelpers, altConnectionManager, specInput) => {
}
};

const dispatchStreamSubscribed = (streamInput, evt) => {
const maybeDispatchStreamSubscribed = (streamInput, evt) => {
const stream = streamInput;
// Draw on html
log.info(`message: Stream subscribed, ${stream.toLog()}, ${toLog()}`);
stream.stream = evt.stream;
if (!that.p2p) {
stream.pc.addStream(stream);
}
stream.state = 'subscribed';
const evt2 = StreamEvent({ type: 'stream-subscribed', stream });
that.dispatchEvent(evt2);
if (stream.state !== 'subscribed') {
stream.state = 'subscribed';
const evt2 = StreamEvent({ type: 'stream-subscribed', stream });
that.dispatchEvent(evt2);
}
};

const maybeDispatchStreamUnsubscribed = (streamInput) => {
Expand Down Expand Up @@ -169,7 +171,7 @@ const Room = (altIo, altConnectionHelpers, altConnectionManager, specInput) => {
const connection = that.erizoConnectionManager.getOrBuildErizoConnection(connectionOptions);
stream.addPC(connection, false, connectionOptions);
connection.on('connection-failed', that.dispatchEvent.bind(this));
stream.on('added', dispatchStreamSubscribed.bind(null, stream));
stream.on('added', maybeDispatchStreamSubscribed.bind(null, stream));
stream.on('icestatechanged', (evt) => {
log.debug(`message: icestatechanged, ${stream.toLog()}, iceConnectionState: ${evt.msg.state}, ${toLog()}`);
if (evt.msg.state === 'failed') {
Expand Down Expand Up @@ -266,7 +268,7 @@ const Room = (altIo, altConnectionHelpers, altConnectionManager, specInput) => {
stream.addPC(connection, false, connectionOpts);
connection.on('connection-failed', that.dispatchEvent.bind(this));

stream.on('added', dispatchStreamSubscribed.bind(null, stream));
stream.on('added', maybeDispatchStreamSubscribed.bind(null, stream));
stream.on('icestatechanged', (evt) => {
log.debug(`message: icestatechanged, ${stream.toLog()}, iceConnectionState: ${evt.msg.state}, ${toLog()}`);
if (evt.msg.state === 'failed') {
Expand Down

0 comments on commit 5c82bec

Please sign in to comment.