From c63e02bd668b63dc8abc2c192925027f9ae41b2d Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Wed, 14 Mar 2018 10:19:17 +0100 Subject: [PATCH] openvidu-browser minor refactoring --- openvidu-browser/ts/OpenVidu/Session.ts | 6 +-- .../ts/OpenViduInternal/Connection.ts | 4 +- .../ts/OpenViduInternal/OpenViduInternal.ts | 4 -- .../ts/OpenViduInternal/SessionInternal.ts | 41 ++++++++++--------- 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/openvidu-browser/ts/OpenVidu/Session.ts b/openvidu-browser/ts/OpenVidu/Session.ts index f59417a7c5..c92b9691d8 100644 --- a/openvidu-browser/ts/OpenVidu/Session.ts +++ b/openvidu-browser/ts/OpenVidu/Session.ts @@ -26,10 +26,10 @@ export class Session { // Listens to the deactivation of the default behaviour upon the disconnection of a Session this.session.addEventListener('session-disconnected-default', () => { let s: Stream; - for (s of this.openVidu.openVidu.getRemoteStreams()) { - s.removeVideo(); + for (let streamId in this.session.getRemoteStreams()) { + this.session.getRemoteStreams()[streamId].removeVideo(); } - if (this.connection) { + if (this.connection && (Object.keys(this.connection.getStreams()).length > 0)) { for (let streamId in this.connection.getStreams()) { this.connection.getStreams()[streamId].removeVideo(); } diff --git a/openvidu-browser/ts/OpenViduInternal/Connection.ts b/openvidu-browser/ts/OpenViduInternal/Connection.ts index c5e0dc7969..9b4732f8d6 100644 --- a/openvidu-browser/ts/OpenViduInternal/Connection.ts +++ b/openvidu-browser/ts/OpenViduInternal/Connection.ts @@ -38,12 +38,12 @@ export class Connection { addStream( stream: Stream ) { stream.connection = this; this.streams[stream.streamId] = stream; - this.room.getStreams()[stream.streamId] = stream; + //this.room.getStreams()[stream.streamId] = stream; } removeStream( key: string ) { delete this.streams[key]; - delete this.room.getStreams()[key]; + //delete this.room.getStreams()[key]; delete this.inboundStreamsOpts; } diff --git a/openvidu-browser/ts/OpenViduInternal/OpenViduInternal.ts b/openvidu-browser/ts/OpenViduInternal/OpenViduInternal.ts index a582fa6119..c677cb3336 100644 --- a/openvidu-browser/ts/OpenViduInternal/OpenViduInternal.ts +++ b/openvidu-browser/ts/OpenViduInternal/OpenViduInternal.ts @@ -30,7 +30,6 @@ export class OpenViduInternal { private rpcParams: any; private callback: Callback; private localStream: Stream; - private remoteStreams: Stream[] = []; private secret: string; private recorder: boolean = false; @@ -148,9 +147,6 @@ export class OpenViduInternal { return this.localStream; } - getRemoteStreams() { - return this.remoteStreams; - } /* NEW METHODS */ getWsUri() { diff --git a/openvidu-browser/ts/OpenViduInternal/SessionInternal.ts b/openvidu-browser/ts/OpenViduInternal/SessionInternal.ts index 5ca8840248..3a499f380e 100644 --- a/openvidu-browser/ts/OpenViduInternal/SessionInternal.ts +++ b/openvidu-browser/ts/OpenViduInternal/SessionInternal.ts @@ -5,6 +5,8 @@ import { Publisher } from '../OpenVidu/Publisher'; import EventEmitter = require('wolfy87-eventemitter'); +type ObjMap = { [s: string]: T; } + const SECRET_PARAM = '?secret='; const RECORDER_PARAM = '&recorder='; @@ -28,7 +30,7 @@ export class SessionInternal { private id: string; private sessionId: string; private ee = new EventEmitter(); - private streams = {}; + private remoteStreams: ObjMap = {}; private participants = {}; private publishersSpeaking: Connection[] = []; private connected = false; @@ -129,7 +131,7 @@ export class SessionInternal { if (this.localParticipant) { if (Object.keys(this.localParticipant.getStreams()).some(streamId => - this.streams[streamId].isDataChannelEnabled())) { + this.remoteStreams[streamId].isDataChannelEnabled())) { joinParams.dataChannels = true; } } @@ -191,8 +193,8 @@ export class SessionInternal { for (let stream of roomEvent.streams) { this.ee.emitEvent('streamCreated', [{ stream }]); - // Adding the remote stream to the OpenVidu object - this.openVidu.getRemoteStreams().push(stream); + // Store the remote stream + this.remoteStreams[stream.streamId] = stream; } callback(undefined); @@ -311,7 +313,7 @@ export class SessionInternal { for (let key in streams) { let stream = streams[key]; - if (!this.streams[stream.streamId]) { + if (!this.remoteStreams[stream.streamId]) { // 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 @@ -321,10 +323,10 @@ export class SessionInternal { } this.ee.emitEvent('streamCreated', [{ stream }]); - // Adding the remote stream to the OpenVidu object - this.openVidu.getRemoteStreams().push(stream); + // Store the remote stream + this.remoteStreams[stream.streamId] = stream; - } + } } } @@ -343,14 +345,12 @@ export class SessionInternal { stream: streams[key] }]); - // Deleting the removed stream from the OpenVidu object - let index = this.openVidu.getRemoteStreams().indexOf(streams[key]); - let stream = this.openVidu.getRemoteStreams()[index]; - + // Deleting the remote stream + let streamId: string = streams[key].streamId; + let stream: Stream = this.remoteStreams[streamId]; stream.dispose(); - this.openVidu.getRemoteStreams().splice(index, 1); - delete this.streams[stream.streamId]; + delete this.remoteStreams[stream.streamId]; connection.removeStream(stream.streamId); } @@ -391,7 +391,6 @@ export class SessionInternal { let connection: Connection = this.participants[msg.name]; if (connection !== undefined) { - delete this.participants[msg.name]; this.ee.emitEvent('participant-left', [{ connection: connection @@ -407,13 +406,15 @@ export class SessionInternal { stream: streams[key] }]); - // Deleting the removed stream from the OpenVidu object - let index = this.openVidu.getRemoteStreams().indexOf(streams[key]); - this.openVidu.getRemoteStreams().splice(index, 1); + // Deleting the remote stream + let streamId: string = streams[key].streamId; + delete this.remoteStreams[streamId]; } connection.dispose(); + delete this.participants[msg.name]; + this.ee.emitEvent('connectionDestroyed', [{ connection: connection }]); @@ -618,8 +619,8 @@ export class SessionInternal { } } - getStreams() { - return this.streams; + getRemoteStreams() { + return this.remoteStreams; } addParticipantSpeaking(participantId) {