Bug fix: race condition of "streamCreated" event (triggered by "joinRoom" callback and "onParticipantPublished)

pull/30/merge
pabloFuente 2018-03-13 15:44:52 +01:00
parent 88d017dc18
commit df074b5d15
1 changed files with 22 additions and 18 deletions

View File

@ -273,14 +273,14 @@ export class SessionInternal {
this.openVidu.sendRequest('unsubscribeFromVideo', { this.openVidu.sendRequest('unsubscribeFromVideo', {
sender: stream.connection.connectionId sender: stream.connection.connectionId
}, },
(error, response) => { (error, response) => {
if (error) { if (error) {
console.error("Error unsubscribing from Subscriber", error); console.error("Error unsubscribing from Subscriber", error);
} else { } else {
console.info("Unsubscribed correctly from " + stream.connection.connectionId); console.info("Unsubscribed correctly from " + stream.connection.connectionId);
} }
stream.dispose(); stream.dispose();
}); });
} }
onParticipantPublished(response: ConnectionOptions) { onParticipantPublished(response: ConnectionOptions) {
@ -305,22 +305,26 @@ export class SessionInternal {
console.debug("Remote Connection found in connections list by its id [" + pid + "]"); console.debug("Remote Connection found in connections list by its id [" + pid + "]");
} }
this.participants[pid] = connection; this.participants[pid] = connection;
this.ee.emitEvent('participant-published', [{ connection }]);
let streams = connection.getStreams(); let streams = connection.getStreams();
for (let key in streams) { for (let key in streams) {
let stream = streams[key]; let stream = streams[key];
if (this.subscribeToStreams) { if (!this.streams[stream.streamId]) {
stream.subscribe(); // Avoid race condition between stream.subscribe() in "onParticipantPublished" and in "joinRoom" rpc callback
// This condition is false if openvidu-server sends "participantPublished" event to a subscriber participant that has
// already subscribed to certain stream in the callback of "joinRoom" method
if (this.subscribeToStreams) {
stream.subscribe();
}
this.ee.emitEvent('streamCreated', [{ stream }]);
// Adding the remote stream to the OpenVidu object
this.openVidu.getRemoteStreams().push(stream);
} }
this.ee.emitEvent('streamCreated', [{ stream }]);
// Adding the remote stream to the OpenVidu object
this.openVidu.getRemoteStreams().push(stream);
} }
} }
@ -584,7 +588,7 @@ export class SessionInternal {
return; return;
} else if (stream.connection !== this.localParticipant) { } else if (stream.connection !== this.localParticipant) {
console.error("The associated Connection object of this Publisher is not your local Connection." + console.error("The associated Connection object of this Publisher is not your local Connection." +
"Only moderators can force unpublish on remote Streams via 'forceUnpublish' method", stream); "Only moderators can force unpublish on remote Streams via 'forceUnpublish' method", stream);
return; return;
} else { } else {
stream.dispose(); stream.dispose();