Skip to content

Commit

Permalink
openvidu-browser minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloFuente committed Mar 14, 2018
1 parent a116c2f commit c63e02b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
6 changes: 3 additions & 3 deletions openvidu-browser/ts/OpenVidu/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions openvidu-browser/ts/OpenViduInternal/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 0 additions & 4 deletions openvidu-browser/ts/OpenViduInternal/OpenViduInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class OpenViduInternal {
private rpcParams: any;
private callback: Callback<OpenViduInternal>;
private localStream: Stream;
private remoteStreams: Stream[] = [];
private secret: string;
private recorder: boolean = false;

Expand Down Expand Up @@ -148,9 +147,6 @@ export class OpenViduInternal {
return this.localStream;
}

getRemoteStreams() {
return this.remoteStreams;
}
/* NEW METHODS */

getWsUri() {
Expand Down
41 changes: 21 additions & 20 deletions openvidu-browser/ts/OpenViduInternal/SessionInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Publisher } from '../OpenVidu/Publisher';

import EventEmitter = require('wolfy87-eventemitter');

type ObjMap<T> = { [s: string]: T; }

const SECRET_PARAM = '?secret=';
const RECORDER_PARAM = '&recorder=';

Expand All @@ -28,7 +30,7 @@ export class SessionInternal {
private id: string;
private sessionId: string;
private ee = new EventEmitter();
private streams = {};
private remoteStreams: ObjMap<Stream> = {};
private participants = {};
private publishersSpeaking: Connection[] = [];
private connected = false;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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;

}
}
}
}

Expand All @@ -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);

}
Expand Down Expand Up @@ -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
Expand All @@ -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
}]);
Expand Down Expand Up @@ -618,8 +619,8 @@ export class SessionInternal {
}
}

getStreams() {
return this.streams;
getRemoteStreams() {
return this.remoteStreams;
}

addParticipantSpeaking(participantId) {
Expand Down

0 comments on commit c63e02b

Please sign in to comment.