openvidu-browser: typed Session event handlers

pull/635/head
pabloFuente 2021-06-23 13:02:05 +02:00
parent 5b5696d0ac
commit 1b22fd5c3a
2 changed files with 96 additions and 99 deletions

View File

@ -761,16 +761,15 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onParticipantJoined(response: RemoteConnectionOptions): void { onParticipantJoined(event: RemoteConnectionOptions): void {
// Connection shouldn't exist // Connection shouldn't exist
this.getConnection(response.id, '') this.getConnection(event.id, '')
.then(connection => { .then(connection => {
logger.warn('Connection ' + response.id + ' already exists in connections list'); logger.warn('Connection ' + connection.connectionId + ' already exists in connections list');
}) })
.catch(openViduError => { .catch(openViduError => {
const connection = new Connection(this, response); const connection = new Connection(this, event);
this.remoteConnections.set(response.id, connection); this.remoteConnections.set(event.id, connection);
this.ee.emitEvent('connectionCreated', [new ConnectionEvent(false, this, 'connectionCreated', connection, '')]); this.ee.emitEvent('connectionCreated', [new ConnectionEvent(false, this, 'connectionCreated', connection, '')]);
}); });
} }
@ -778,21 +777,21 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onParticipantLeft(msg): void { onParticipantLeft(event: { connectionId: string, reason: string }): void {
if (this.remoteConnections.size > 0) { if (this.remoteConnections.size > 0) {
this.getRemoteConnection(msg.connectionId, 'onParticipantLeft').then(connection => { this.getRemoteConnection(event.connectionId, 'onParticipantLeft').then(connection => {
if (!!connection.stream) { if (!!connection.stream) {
const stream = connection.stream; const stream = connection.stream;
const streamEvent = new StreamEvent(true, this, 'streamDestroyed', stream, msg.reason); const streamEvent = new StreamEvent(true, this, 'streamDestroyed', stream, event.reason);
this.ee.emitEvent('streamDestroyed', [streamEvent]); this.ee.emitEvent('streamDestroyed', [streamEvent]);
streamEvent.callDefaultBehavior(); streamEvent.callDefaultBehavior();
this.remoteStreamsCreated.delete(stream.streamId); this.remoteStreamsCreated.delete(stream.streamId);
} }
this.remoteConnections.delete(connection.connectionId); this.remoteConnections.delete(connection.connectionId);
this.ee.emitEvent('connectionDestroyed', [new ConnectionEvent(false, this, 'connectionDestroyed', connection, msg.reason)]); this.ee.emitEvent('connectionDestroyed', [new ConnectionEvent(false, this, 'connectionDestroyed', connection, event.reason)]);
}) })
.catch(openViduError => { .catch(openViduError => {
logger.error(openViduError); logger.error(openViduError);
@ -803,7 +802,7 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onParticipantPublished(response: RemoteConnectionOptions): void { onParticipantPublished(event: RemoteConnectionOptions): void {
const afterConnectionFound = (connection) => { const afterConnectionFound = (connection) => {
@ -823,19 +822,19 @@ export class Session extends EventDispatcher {
// Get the existing Connection created on 'onParticipantJoined' for // Get the existing Connection created on 'onParticipantJoined' for
// existing participants or create a new one for new participants // existing participants or create a new one for new participants
let connection: Connection; let connection: Connection;
this.getRemoteConnection(response.id, 'onParticipantPublished') this.getRemoteConnection(event.id, 'onParticipantPublished')
.then(con => { .then(con => {
// Update existing Connection // Update existing Connection
connection = con; connection = con;
response.metadata = con.data; event.metadata = con.data;
connection.remoteOptions = response; connection.remoteOptions = event;
connection.initRemoteStreams(response.streams); connection.initRemoteStreams(event.streams);
afterConnectionFound(connection); afterConnectionFound(connection);
}) })
.catch(openViduError => { .catch(openViduError => {
// Create new Connection // Create new Connection
connection = new Connection(this, response); connection = new Connection(this, event);
afterConnectionFound(connection); afterConnectionFound(connection);
}); });
} }
@ -843,16 +842,16 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onParticipantUnpublished(msg): void { onParticipantUnpublished(event: { connectionId: string, reason: string }): void {
if (msg.connectionId === this.connection.connectionId) { if (event.connectionId === this.connection.connectionId) {
// Your stream has been forcedly unpublished from the session // Your stream has been forcedly unpublished from the session
this.stopPublisherStream(msg.reason); this.stopPublisherStream(event.reason);
} else { } else {
this.getRemoteConnection(msg.connectionId, 'onParticipantUnpublished') this.getRemoteConnection(event.connectionId, 'onParticipantUnpublished')
.then(connection => { .then(connection => {
const streamEvent = new StreamEvent(true, this, 'streamDestroyed', connection.stream!, msg.reason); const streamEvent = new StreamEvent(true, this, 'streamDestroyed', connection.stream!, event.reason);
this.ee.emitEvent('streamDestroyed', [streamEvent]); this.ee.emitEvent('streamDestroyed', [streamEvent]);
streamEvent.callDefaultBehavior(); streamEvent.callDefaultBehavior();
@ -871,11 +870,11 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onParticipantEvicted(msg): void { onParticipantEvicted(event: { connectionId: string, reason: string }): void {
if (msg.connectionId === this.connection.connectionId) { if (event.connectionId === this.connection.connectionId) {
// You have been evicted from the session // You have been evicted from the session
if (!!this.sessionId && !this.connection.disposed) { if (!!this.sessionId && !this.connection.disposed) {
this.leave(true, msg.reason); this.leave(true, event.reason);
} }
} }
} }
@ -883,21 +882,21 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onNewMessage(msg): void { onNewMessage(event: { type?: string, data?: string, from?: string }): void {
logger.info('New signal: ' + JSON.stringify(msg)); logger.info('New signal: ' + JSON.stringify(event));
const strippedType: string = !!msg.type ? msg.type.replace(/^(signal:)/, '') : undefined; const strippedType = !!event.type ? event.type.replace(/^(signal:)/, '') : undefined;
if (!!msg.from) { if (!!event.from) {
// Signal sent by other client // Signal sent by other client
this.getConnection(msg.from, "Connection '" + msg.from + "' unknown when 'onNewMessage'. Existing remote connections: " this.getConnection(event.from, "Connection '" + event.from + "' unknown when 'onNewMessage'. Existing remote connections: "
+ JSON.stringify(this.remoteConnections.keys()) + '. Existing local connection: ' + this.connection.connectionId) + JSON.stringify(this.remoteConnections.keys()) + '. Existing local connection: ' + this.connection.connectionId)
.then(connection => { .then(connection => {
this.ee.emitEvent('signal', [new SignalEvent(this, strippedType, msg.data, connection)]); this.ee.emitEvent('signal', [new SignalEvent(this, strippedType, event.data, connection)]);
if (msg.type !== 'signal') { if (!!event.type && event.type !== 'signal') {
this.ee.emitEvent(msg.type, [new SignalEvent(this, strippedType, msg.data, connection)]); this.ee.emitEvent(event.type, [new SignalEvent(this, strippedType, event.data, connection)]);
} }
}) })
.catch(openViduError => { .catch(openViduError => {
@ -905,9 +904,9 @@ export class Session extends EventDispatcher {
}); });
} else { } else {
// Signal sent by server // Signal sent by server
this.ee.emitEvent('signal', [new SignalEvent(this, strippedType, msg.data, undefined)]); this.ee.emitEvent('signal', [new SignalEvent(this, strippedType, event.data, undefined)]);
if (msg.type !== 'signal') { if (!!event.type && event.type !== 'signal') {
this.ee.emitEvent(msg.type, [new SignalEvent(this, strippedType, msg.data, undefined)]); this.ee.emitEvent(event.type, [new SignalEvent(this, strippedType, event.data, undefined)]);
} }
} }
} }
@ -915,57 +914,57 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onStreamPropertyChanged(msg): void { onStreamPropertyChanged(event: { connectionId: string, streamId: string, property: string, newValue: any, reason: string }): void {
const callback = (connection: Connection) => { const callback = (connection: Connection) => {
if (!!connection.stream && connection.stream.streamId === msg.streamId) { if (!!connection.stream && connection.stream.streamId === event.streamId) {
const stream = connection.stream; const stream = connection.stream;
let oldValue; let oldValue;
switch (msg.property) { switch (event.property) {
case 'audioActive': case 'audioActive':
oldValue = stream.audioActive; oldValue = stream.audioActive;
msg.newValue = msg.newValue === 'true'; event.newValue = event.newValue === 'true';
stream.audioActive = msg.newValue; stream.audioActive = event.newValue;
break; break;
case 'videoActive': case 'videoActive':
oldValue = stream.videoActive; oldValue = stream.videoActive;
msg.newValue = msg.newValue === 'true'; event.newValue = event.newValue === 'true';
stream.videoActive = msg.newValue; stream.videoActive = event.newValue;
break; break;
case 'videoDimensions': case 'videoDimensions':
oldValue = stream.videoDimensions; oldValue = stream.videoDimensions;
msg.newValue = JSON.parse(JSON.parse(msg.newValue)); event.newValue = JSON.parse(JSON.parse(event.newValue));
stream.videoDimensions = msg.newValue; stream.videoDimensions = event.newValue;
break; break;
case 'filter': case 'filter':
oldValue = stream.filter; oldValue = stream.filter;
msg.newValue = (Object.keys(msg.newValue).length > 0) ? msg.newValue : undefined; event.newValue = (Object.keys(event.newValue).length > 0) ? event.newValue : undefined;
if (msg.newValue !== undefined) { if (event.newValue !== undefined) {
stream.filter = new Filter(msg.newValue.type, msg.newValue.options); stream.filter = new Filter(event.newValue.type, event.newValue.options);
stream.filter.stream = stream; stream.filter.stream = stream;
if (msg.newValue.lastExecMethod) { if (event.newValue.lastExecMethod) {
stream.filter.lastExecMethod = msg.newValue.lastExecMethod; stream.filter.lastExecMethod = event.newValue.lastExecMethod;
} }
} else { } else {
delete stream.filter; delete stream.filter;
} }
msg.newValue = stream.filter; event.newValue = stream.filter;
break; break;
} }
this.ee.emitEvent('streamPropertyChanged', [new StreamPropertyChangedEvent(this, stream, msg.property, msg.newValue, oldValue, msg.reason)]); this.ee.emitEvent('streamPropertyChanged', [new StreamPropertyChangedEvent(this, stream, event.property, event.newValue, oldValue, event.reason)]);
if (!!stream.streamManager) { if (!!stream.streamManager) {
stream.streamManager.emitEvent('streamPropertyChanged', [new StreamPropertyChangedEvent(stream.streamManager, stream, msg.property, msg.newValue, oldValue, msg.reason)]); stream.streamManager.emitEvent('streamPropertyChanged', [new StreamPropertyChangedEvent(stream.streamManager, stream, event.property, event.newValue, oldValue, event.reason)]);
} }
} else { } else {
logger.error("No stream with streamId '" + msg.streamId + "' found for connection '" + msg.connectionId + "' on 'streamPropertyChanged' event"); logger.error("No stream with streamId '" + event.streamId + "' found for connection '" + event.connectionId + "' on 'streamPropertyChanged' event");
} }
}; };
if (msg.connectionId === this.connection.connectionId) { if (event.connectionId === this.connection.connectionId) {
// Your stream has been forcedly changed (filter feature) // Your stream has been forcedly changed (filter feature)
callback(this.connection); callback(this.connection);
} else { } else {
this.getRemoteConnection(msg.connectionId, 'onStreamPropertyChanged') this.getRemoteConnection(event.connectionId, 'onStreamPropertyChanged')
.then(connection => { .then(connection => {
callback(connection); callback(connection);
}) })
@ -978,34 +977,34 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onConnectionPropertyChanged(msg): void { onConnectionPropertyChanged(event: { property: string, newValue: any }): void {
let oldValue; let oldValue;
switch (msg.property) { switch (event.property) {
case 'role': case 'role':
oldValue = this.connection.role.slice(); oldValue = this.connection.role.slice();
this.connection.role = msg.newValue; this.connection.role = event.newValue;
this.connection.localOptions!.role = msg.newValue; this.connection.localOptions!.role = event.newValue;
break; break;
case 'record': case 'record':
oldValue = this.connection.record; oldValue = this.connection.record;
msg.newValue = msg.newValue === 'true'; event.newValue = event.newValue === 'true';
this.connection.record = msg.newValue; this.connection.record = event.newValue;
this.connection.localOptions!.record = msg.newValue; this.connection.localOptions!.record = event.newValue;
break; break;
} }
this.ee.emitEvent('connectionPropertyChanged', [new ConnectionPropertyChangedEvent(this, this.connection, msg.property, msg.newValue, oldValue)]); this.ee.emitEvent('connectionPropertyChanged', [new ConnectionPropertyChangedEvent(this, this.connection, event.property, event.newValue, oldValue)]);
} }
/** /**
* @hidden * @hidden
*/ */
onNetworkQualityLevelChangedChanged(msg): void { onNetworkQualityLevelChangedChanged(event: { connectionId: string, newValue: number, oldValue: number }): void {
if (msg.connectionId === this.connection.connectionId) { if (event.connectionId === this.connection.connectionId) {
this.ee.emitEvent('networkQualityLevelChanged', [new NetworkQualityLevelChangedEvent(this, msg.newValue, msg.oldValue, this.connection)]); this.ee.emitEvent('networkQualityLevelChanged', [new NetworkQualityLevelChangedEvent(this, event.newValue, event.oldValue, this.connection)]);
} else { } else {
this.getConnection(msg.connectionId, 'Connection not found for connectionId ' + msg.connectionId) this.getConnection(event.connectionId, 'Connection not found for connectionId ' + event.connectionId)
.then((connection: Connection) => { .then((connection: Connection) => {
this.ee.emitEvent('networkQualityLevelChanged', [new NetworkQualityLevelChangedEvent(this, msg.newValue, msg.oldValue, connection)]); this.ee.emitEvent('networkQualityLevelChanged', [new NetworkQualityLevelChangedEvent(this, event.newValue, event.oldValue, connection)]);
}) })
.catch(openViduError => { .catch(openViduError => {
logger.error(openViduError); logger.error(openViduError);
@ -1016,31 +1015,31 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
recvIceCandidate(msg): void { recvIceCandidate(event: { senderConnectionId: string, endpointName: string, sdpMLineIndex: number, sdpMid: string, candidate: string }): void {
const candidate: RTCIceCandidate = { const candidate: RTCIceCandidate = {
candidate: msg.candidate, candidate: event.candidate,
component: msg.component, sdpMid: event.sdpMid,
foundation: msg.foundation, sdpMLineIndex: event.sdpMLineIndex,
port: msg.port, component: null,
priority: msg.priority, foundation: null,
protocol: msg.protocol, port: null,
relatedAddress: msg.relatedAddress, priority: null,
relatedPort: msg.relatedPort, protocol: null,
sdpMid: msg.sdpMid, relatedAddress: null,
sdpMLineIndex: msg.sdpMLineIndex, relatedPort: null,
tcpType: msg.tcpType, tcpType: null,
usernameFragment: msg.usernameFragment, usernameFragment: null,
type: msg.type, type: null,
toJSON: () => { toJSON: () => {
return { candidate: msg.candidate }; return { candidate: event.candidate };
} }
}; };
this.getConnection(msg.senderConnectionId, 'Connection not found for connectionId ' + msg.senderConnectionId + ' owning endpoint ' + msg.endpointName + '. Ice candidate will be ignored: ' + candidate) this.getConnection(event.senderConnectionId, 'Connection not found for connectionId ' + event.senderConnectionId + ' owning endpoint ' + event.endpointName + '. Ice candidate will be ignored: ' + candidate)
.then(connection => { .then(connection => {
const stream: Stream = connection.stream!; const stream: Stream = connection.stream!;
stream.getWebRtcPeer().addIceCandidate(candidate).catch(error => { stream.getWebRtcPeer().addIceCandidate(candidate).catch(error => {
logger.error('Error adding candidate for ' + stream!.streamId logger.error('Error adding candidate for ' + stream!.streamId
+ ' stream of endpoint ' + msg.endpointName + ': ' + error); + ' stream of endpoint ' + event.endpointName + ': ' + error);
}); });
}) })
.catch(openViduError => { .catch(openViduError => {
@ -1085,44 +1084,42 @@ export class Session extends EventDispatcher {
/** /**
* @hidden * @hidden
*/ */
onMediaError(params): void { onMediaError(event: { error: string }): void {
logger.error('Media error: ' + JSON.stringify(params)); logger.error('Media error: ' + JSON.stringify(event));
const err = params.error; const err = event.error;
if (err) { if (err) {
this.ee.emitEvent('error-media', [{ this.ee.emitEvent('error-media', [{
error: err error: err
}]); }]);
} else { } else {
logger.warn('Received undefined media error. Params:', params); logger.warn('Received undefined media error:', event);
} }
} }
/** /**
* @hidden * @hidden
*/ */
onRecordingStarted(response): void { onRecordingStarted(event: { id: string, name: string }): void {
this.ee.emitEvent('recordingStarted', [new RecordingEvent(this, 'recordingStarted', response.id, response.name)]); this.ee.emitEvent('recordingStarted', [new RecordingEvent(this, 'recordingStarted', event.id, event.name)]);
} }
/** /**
* @hidden * @hidden
*/ */
onRecordingStopped(response): void { onRecordingStopped(event: { id: string, name: string, reason: string }): void {
this.ee.emitEvent('recordingStopped', [new RecordingEvent(this, 'recordingStopped', response.id, response.name, response.reason)]); this.ee.emitEvent('recordingStopped', [new RecordingEvent(this, 'recordingStopped', event.id, event.name, event.reason)]);
} }
/** /**
* @hidden * @hidden
* response = {connectionId: string, streamId: string, type: string, data: Object}
*/ */
onFilterEventDispatched(response): void { onFilterEventDispatched(event: { connectionId: string, streamId: string, filterType: string, eventType: string, data: string }): void {
const connectionId: string = response.connectionId; const connectionId: string = event.connectionId;
const streamId: string = response.streamId;
this.getConnection(connectionId, 'No connection found for connectionId ' + connectionId) this.getConnection(connectionId, 'No connection found for connectionId ' + connectionId)
.then(connection => { .then(connection => {
logger.info('Filter event dispatched'); logger.info('Filter event dispatched');
const stream: Stream = connection.stream!; const stream: Stream = connection.stream!;
stream.filter!.handlers.get(response.eventType)?.call(this, new FilterEvent(stream.filter!, response.eventType, response.data)); stream.filter!.handlers.get(event.eventType)?.call(this, new FilterEvent(stream.filter!, event.eventType, event.data));
}); });
} }

View File

@ -51,7 +51,7 @@ export class SignalEvent extends Event {
/** /**
* @hidden * @hidden
*/ */
constructor(target: Session, type: string, data?: string, from?: Connection) { constructor(target: Session, type?: string, data?: string, from?: Connection) {
super(false, target, 'signal'); super(false, target, 'signal');
if (!!type) { if (!!type) {
this.type = 'signal:' + type; this.type = 'signal:' + type;