From fc93566e04c23049cc0ae379114149938292c055 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Tue, 15 Nov 2022 15:28:55 +0100 Subject: [PATCH] openvidu-browser: add Types for Events --- openvidu-browser/src/OpenVidu/OpenVidu.ts | 21 +++++----- openvidu-browser/src/OpenVidu/Publisher.ts | 3 +- openvidu-browser/src/OpenVidu/Session.ts | 17 ++++---- .../Events/ConnectionEvent.ts | 7 ++-- .../OpenViduInternal/Events/RecordingEvent.ts | 7 ++-- .../Events/SessionDisconnectedEvent.ts | 5 ++- .../Events/SpeechToTextEvent.ts | 5 ++- .../OpenViduInternal/Events/StreamEvent.ts | 5 ++- .../Events/StreamPropertyChangedEvent.ts | 11 ++--- .../OpenViduInternal/Events/Types/Types.ts | 42 +++++++++++++++++++ openvidu-browser/src/index.ts | 2 + 11 files changed, 89 insertions(+), 36 deletions(-) create mode 100644 openvidu-browser/src/OpenViduInternal/Events/Types/Types.ts diff --git a/openvidu-browser/src/OpenVidu/OpenVidu.ts b/openvidu-browser/src/OpenVidu/OpenVidu.ts index ddd81e1f..ed0b77a0 100644 --- a/openvidu-browser/src/OpenVidu/OpenVidu.ts +++ b/openvidu-browser/src/OpenVidu/OpenVidu.ts @@ -29,6 +29,7 @@ import { OpenViduError, OpenViduErrorName } from '../OpenViduInternal/Enums/Open import { VideoInsertMode } from '../OpenViduInternal/Enums/VideoInsertMode'; import { OpenViduLogger } from '../OpenViduInternal/Logger/OpenViduLogger'; import { PlatformUtils } from '../OpenViduInternal/Utils/Platform'; +import { StreamPropertyChangedEventReason, ChangedPropertyType } from '../OpenViduInternal/Events/Types/Types'; import * as screenSharingAuto from '../OpenViduInternal/ScreenSharing/Screen-Capturing-Auto'; import * as screenSharing from '../OpenViduInternal/ScreenSharing/Screen-Capturing'; @@ -219,8 +220,8 @@ export class OpenVidu { typeof MediaStreamTrack !== 'undefined' && properties.videoSource instanceof MediaStreamTrack ? undefined : typeof properties.frameRate !== 'undefined' - ? properties.frameRate - : undefined, + ? properties.frameRate + : undefined, insertMode: typeof properties.insertMode !== 'undefined' ? typeof properties.insertMode === 'string' @@ -234,8 +235,8 @@ export class OpenVidu { typeof MediaStreamTrack !== 'undefined' && properties.videoSource instanceof MediaStreamTrack ? undefined : typeof properties.resolution !== 'undefined' - ? properties.resolution - : '640x480', + ? properties.resolution + : '640x480', videoSource: typeof properties.videoSource !== 'undefined' ? properties.videoSource : undefined, videoSimulcast: properties.videoSimulcast, filter: properties.filter @@ -642,7 +643,7 @@ export class OpenVidu { /** * @hidden */ - sendNewVideoDimensionsIfRequired(publisher: Publisher, reason: string, WAIT_INTERVAL: number, MAX_ATTEMPTS: number) { + sendNewVideoDimensionsIfRequired(publisher: Publisher, reason: StreamPropertyChangedEventReason, WAIT_INTERVAL: number, MAX_ATTEMPTS: number) { let attempts = 0; const oldWidth = publisher?.stream?.videoDimensions?.width || 0; const oldHeight = publisher?.stream?.videoDimensions?.height || 0; @@ -666,7 +667,7 @@ export class OpenVidu { */ sendVideoDimensionsChangedEvent( publisher: Publisher, - reason: string, + reason: StreamPropertyChangedEventReason, oldWidth: number, oldHeight: number, newWidth: number, @@ -717,7 +718,7 @@ export class OpenVidu { /** * @hidden */ - sendTrackChangedEvent(publisher: Publisher, oldLabel: string, newLabel: string, propertyType: string) { + sendTrackChangedEvent(publisher: Publisher, oldLabel: string, newLabel: string, propertyType: ChangedPropertyType) { const oldValue = { label: oldLabel }; const newValue = { label: newLabel }; const reason = 'trackReplaced'; @@ -1028,9 +1029,9 @@ export class OpenVidu { const error = new OpenViduError( OpenViduErrorName.SCREEN_SHARING_NOT_SUPPORTED, 'You can only screen share in desktop Chrome, Firefox, Opera, Safari (>=13.0), Edge (>= 80) or Electron. Detected client: ' + - platform.getName() + - ' ' + - platform.getVersion() + platform.getName() + + ' ' + + platform.getVersion() ); logger.error(error); return reject(error); diff --git a/openvidu-browser/src/OpenVidu/Publisher.ts b/openvidu-browser/src/OpenVidu/Publisher.ts index c4bec311..4bdbaade 100644 --- a/openvidu-browser/src/OpenVidu/Publisher.ts +++ b/openvidu-browser/src/OpenVidu/Publisher.ts @@ -28,6 +28,7 @@ import { VideoInsertMode } from '../OpenViduInternal/Enums/VideoInsertMode'; import { OpenViduLogger } from '../OpenViduInternal/Logger/OpenViduLogger'; import { PlatformUtils } from '../OpenViduInternal/Utils/Platform'; import { TypeOfVideo } from '../OpenViduInternal/Enums/TypeOfVideo'; +import { StreamEventReason } from '../OpenViduInternal/Events/Types/Types'; /** * @hidden @@ -92,7 +93,7 @@ export class Publisher extends StreamManager { this.properties = properties; this.openvidu = openvidu; - this.stream.ee.on('local-stream-destroyed', (reason: string) => { + this.stream.ee.on('local-stream-destroyed', (reason: StreamEventReason) => { this.stream.isLocalStreamPublished = false; const streamEvent = new StreamEvent(true, this, 'streamDestroyed', this.stream, reason); this.emitEvent('streamDestroyed', [streamEvent]); diff --git a/openvidu-browser/src/OpenVidu/Session.ts b/openvidu-browser/src/OpenVidu/Session.ts index 58397650..301d7edb 100644 --- a/openvidu-browser/src/OpenVidu/Session.ts +++ b/openvidu-browser/src/OpenVidu/Session.ts @@ -44,6 +44,7 @@ import { OpenViduError, OpenViduErrorName } from '../OpenViduInternal/Enums/Open import { VideoInsertMode } from '../OpenViduInternal/Enums/VideoInsertMode'; import { OpenViduLogger } from '../OpenViduInternal/Logger/OpenViduLogger'; import { PlatformUtils } from '../OpenViduInternal/Utils/Platform'; +import { StreamPropertyChangedEventReason, ChangedPropertyType, RecordingEventReason, ConnectionEventReason, StreamEventReason } from '../OpenViduInternal/Events/Types/Types'; /** * @hidden */ @@ -833,7 +834,7 @@ export class Session extends EventDispatcher { /** * @hidden */ - onParticipantLeft(event: { connectionId: string; reason: string }): void { + onParticipantLeft(event: { connectionId: string; reason: ConnectionEventReason }): void { this.getRemoteConnection(event.connectionId, 'onParticipantLeft') .then((connection) => { if (!!connection.stream) { @@ -897,7 +898,7 @@ export class Session extends EventDispatcher { /** * @hidden */ - onParticipantUnpublished(event: { connectionId: string; reason: string }): void { + onParticipantUnpublished(event: { connectionId: string; reason: StreamEventReason }): void { if (event.connectionId === this.connection.connectionId) { // Your stream has been forcedly unpublished from the session this.stopPublisherStream(event.reason); @@ -925,7 +926,7 @@ export class Session extends EventDispatcher { /** * @hidden */ - onParticipantEvicted(event: { connectionId: string; reason: string }): void { + onParticipantEvicted(event: { connectionId: string; reason: ConnectionEventReason }): void { if (event.connectionId === this.connection.connectionId) { // You have been evicted from the session if (!!this.sessionId && !this.connection.disposed) { @@ -975,7 +976,7 @@ export class Session extends EventDispatcher { /** * @hidden */ - onStreamPropertyChanged(event: { connectionId: string; streamId: string; property: string; newValue: any; reason: string }): void { + onStreamPropertyChanged(event: { connectionId: string; streamId: string; property: ChangedPropertyType; newValue: any; reason: StreamPropertyChangedEventReason }): void { const callback = (connection: Connection) => { if (!!connection.stream && connection.stream.streamId === event.streamId) { const stream = connection.stream; @@ -1159,7 +1160,7 @@ export class Session extends EventDispatcher { /** * @hidden */ - onLostConnection(reason: string): void { + onLostConnection(reason: ConnectionEventReason): void { logger.warn('Lost connection in Session ' + this.sessionId); if (!!this.sessionId && !!this.connection && !this.connection.disposed) { this.leave(true, reason); @@ -1202,7 +1203,7 @@ export class Session extends EventDispatcher { /** * @hidden */ - onRecordingStopped(event: { id: string; name: string; reason: string }): void { + onRecordingStopped(event: { id: string; name: string; reason: RecordingEventReason }): void { this.ee.emitEvent('recordingStopped', [new RecordingEvent(this, 'recordingStopped', event.id, event.name, event.reason)]); } @@ -1354,7 +1355,7 @@ export class Session extends EventDispatcher { /** * @hidden */ - leave(forced: boolean, reason: string): void { + leave(forced: boolean, reason: ConnectionEventReason): void { forced = !!forced; logger.info('Leaving Session (forced=' + forced + ')'); this.stopVideoDataIntervals(); @@ -1636,7 +1637,7 @@ export class Session extends EventDispatcher { }); } - private stopPublisherStream(reason: string) { + private stopPublisherStream(reason: StreamEventReason) { if (!!this.connection.stream) { // Dispose Publisher's local stream this.connection.stream.disposeWebRtcPeer(); diff --git a/openvidu-browser/src/OpenViduInternal/Events/ConnectionEvent.ts b/openvidu-browser/src/OpenViduInternal/Events/ConnectionEvent.ts index 5a94b8d7..522bb8bf 100644 --- a/openvidu-browser/src/OpenViduInternal/Events/ConnectionEvent.ts +++ b/openvidu-browser/src/OpenViduInternal/Events/ConnectionEvent.ts @@ -18,6 +18,7 @@ import { Event } from './Event'; import { Connection } from '../../OpenVidu/Connection'; import { Session } from '../../OpenVidu/Session'; +import { ConnectionEventReason } from './Types/Types'; /** * Triggered by: @@ -41,12 +42,12 @@ export class ConnectionEvent extends Event { * * For `connectionCreated` event an empty string */ - reason: string; + reason: ConnectionEventReason; /** * @hidden */ - constructor(cancelable: boolean, target: Session, type: string, connection: Connection, reason: string) { + constructor(cancelable: boolean, target: Session, type: string, connection: Connection, reason: ConnectionEventReason) { super(cancelable, target, type); this.connection = connection; this.reason = reason; @@ -56,5 +57,5 @@ export class ConnectionEvent extends Event { * @hidden */ // tslint:disable-next-line:no-empty - callDefaultBehavior() {} + callDefaultBehavior() { } } diff --git a/openvidu-browser/src/OpenViduInternal/Events/RecordingEvent.ts b/openvidu-browser/src/OpenViduInternal/Events/RecordingEvent.ts index 2f205708..d981c807 100644 --- a/openvidu-browser/src/OpenViduInternal/Events/RecordingEvent.ts +++ b/openvidu-browser/src/OpenViduInternal/Events/RecordingEvent.ts @@ -17,6 +17,7 @@ import { Event } from './Event'; import { Session } from '../../OpenVidu/Session'; +import { RecordingEventReason } from './Types/Types'; /** * Triggered by: @@ -48,12 +49,12 @@ export class RecordingEvent extends Event { * * For 'recordingStarted' empty string */ - reason?: string; + reason?: RecordingEventReason; /** * @hidden */ - constructor(target: Session, type: string, id: string, name: string, reason?: string) { + constructor(target: Session, type: string, id: string, name: string, reason?: RecordingEventReason) { super(false, target, type); this.id = id; if (name !== id) { @@ -66,5 +67,5 @@ export class RecordingEvent extends Event { * @hidden */ // tslint:disable-next-line:no-empty - callDefaultBehavior() {} + callDefaultBehavior() { } } diff --git a/openvidu-browser/src/OpenViduInternal/Events/SessionDisconnectedEvent.ts b/openvidu-browser/src/OpenViduInternal/Events/SessionDisconnectedEvent.ts index fb663374..7d44f197 100644 --- a/openvidu-browser/src/OpenViduInternal/Events/SessionDisconnectedEvent.ts +++ b/openvidu-browser/src/OpenViduInternal/Events/SessionDisconnectedEvent.ts @@ -18,6 +18,7 @@ import { Event } from './Event'; import { Session } from '../../OpenVidu/Session'; import { OpenViduLogger } from '../Logger/OpenViduLogger'; +import { ConnectionEventReason } from './Types/Types'; /** * @hidden @@ -40,12 +41,12 @@ export class SessionDisconnectedEvent extends Event { * - "nodeCrashed": a node has crashed in the server side. You can use this reason to ask your application's backend to reconnect * to a new session to replace the crashed one */ - reason: string; + reason: ConnectionEventReason; /** * @hidden */ - constructor(target: Session, reason: string) { + constructor(target: Session, reason: ConnectionEventReason) { super(true, target, 'sessionDisconnected'); this.reason = reason; } diff --git a/openvidu-browser/src/OpenViduInternal/Events/SpeechToTextEvent.ts b/openvidu-browser/src/OpenViduInternal/Events/SpeechToTextEvent.ts index 443ad548..fb82aae9 100644 --- a/openvidu-browser/src/OpenViduInternal/Events/SpeechToTextEvent.ts +++ b/openvidu-browser/src/OpenViduInternal/Events/SpeechToTextEvent.ts @@ -18,6 +18,7 @@ import { Event } from './Event'; import { Connection } from '../../OpenVidu/Connection'; import { Session } from '../../OpenVidu/Session'; +import { SpeechToTextEventReason } from './Types/Types'; /** * Triggered by [[SessionEventMap.speechToTextMessage]] @@ -38,7 +39,7 @@ export class SpeechToTextEvent extends Event { /** * All speech-to-text events are generated */ - reason: 'recognizing' | 'recognized'; + reason: SpeechToTextEventReason; /** * The original event from the speech to text engine. This can vary depending on the engine @@ -54,7 +55,7 @@ export class SpeechToTextEvent extends Event { /** * @hidden */ - constructor(target: Session, connection: Connection, text: string, reason: 'recognizing' | 'recognized', raw: string, lang: string) { + constructor(target: Session, connection: Connection, text: string, reason: SpeechToTextEventReason, raw: string, lang: string) { super(false, target, 'speechToTextMessage'); this.connection = connection; this.text = text; diff --git a/openvidu-browser/src/OpenViduInternal/Events/StreamEvent.ts b/openvidu-browser/src/OpenViduInternal/Events/StreamEvent.ts index 9de3040c..7f447f4b 100644 --- a/openvidu-browser/src/OpenViduInternal/Events/StreamEvent.ts +++ b/openvidu-browser/src/OpenViduInternal/Events/StreamEvent.ts @@ -20,6 +20,7 @@ import { Publisher } from '../../OpenVidu/Publisher'; import { Session } from '../../OpenVidu/Session'; import { Stream } from '../../OpenVidu/Stream'; import { OpenViduLogger } from '../Logger/OpenViduLogger'; +import { StreamEventReason } from './Types/Types'; /** * @hidden @@ -51,12 +52,12 @@ export class StreamEvent extends Event { * * For 'streamCreated' empty string */ - reason: string; + reason: StreamEventReason; /** * @hidden */ - constructor(cancelable: boolean, target: Session | Publisher, type: string, stream: Stream, reason: string) { + constructor(cancelable: boolean, target: Session | Publisher, type: string, stream: Stream, reason: StreamEventReason) { super(cancelable, target, type); this.stream = stream; this.reason = reason; diff --git a/openvidu-browser/src/OpenViduInternal/Events/StreamPropertyChangedEvent.ts b/openvidu-browser/src/OpenViduInternal/Events/StreamPropertyChangedEvent.ts index cc72227f..2df546db 100644 --- a/openvidu-browser/src/OpenViduInternal/Events/StreamPropertyChangedEvent.ts +++ b/openvidu-browser/src/OpenViduInternal/Events/StreamPropertyChangedEvent.ts @@ -19,6 +19,7 @@ import { Event } from './Event'; import { Session } from '../../OpenVidu/Session'; import { Stream } from '../../OpenVidu/Stream'; import { StreamManager } from '../../OpenVidu/StreamManager'; +import { StreamPropertyChangedEventReason, ChangedPropertyType } from './Types/Types'; /** * Triggered by `streamPropertyChanged` (available for [Session](/en/stable/api/openvidu-browser/interfaces/SessionEventMap.html#streamPropertyChanged) and [StreamManager](/en/stable/api/openvidu-browser/interfaces/StreamManagerEventMap.html#streamPropertyChanged) objects) @@ -32,7 +33,7 @@ export class StreamPropertyChangedEvent extends Event { /** * The property of the stream that changed. This value is either `"videoActive"`, `"audioActive"`, `"videoTrack"`, `"audioTrack"`, `"videoDimensions"` or `"filter"` */ - changedProperty: string; + changedProperty: ChangedPropertyType; /** * Cause of the change on the stream's property: @@ -43,7 +44,7 @@ export class StreamPropertyChangedEvent extends Event { * - For `videoDimensions`: `"deviceRotated"`, `"screenResized"` or `"trackReplaced"` * - For `filter`: `"applyFilter"`, `"execFilterMethod"` or `"removeFilter"` */ - reason: string; + reason: StreamPropertyChangedEventReason; /** * New value of the property (after change, current value) @@ -61,10 +62,10 @@ export class StreamPropertyChangedEvent extends Event { constructor( target: Session | StreamManager, stream: Stream, - changedProperty: string, + changedProperty: ChangedPropertyType, newValue: Object, oldValue: Object, - reason: string + reason: StreamPropertyChangedEventReason ) { super(false, target, 'streamPropertyChanged'); this.stream = stream; @@ -78,5 +79,5 @@ export class StreamPropertyChangedEvent extends Event { * @hidden */ // tslint:disable-next-line:no-empty - callDefaultBehavior() {} + callDefaultBehavior() { } } diff --git a/openvidu-browser/src/OpenViduInternal/Events/Types/Types.ts b/openvidu-browser/src/OpenViduInternal/Events/Types/Types.ts new file mode 100644 index 00000000..30f0314f --- /dev/null +++ b/openvidu-browser/src/OpenViduInternal/Events/Types/Types.ts @@ -0,0 +1,42 @@ +export type ChangedPropertyType = + 'videoActive' | + 'audioActive' | + 'videoTrack' | + 'audioTrack' | + 'videoDimensions' | + 'filter'; + +export type StreamPropertyChangedEventReason = + 'publishVideo' | + 'publishAudio' | + 'trackReplaced' | + 'deviceRotated' | + 'screenResized' | + 'applyFilter' | + 'execFilterMethod' | + 'removeFilter'; + +export type ConnectionEventReason = + 'disconnect' | + 'forceDisconnectByUser' | + 'forceDisconnectByServer' | + 'sessionClosedByServer' | + 'networkDisconnect' | + 'nodeCrashed' | + ''; + +export type StreamEventReason = + ConnectionEventReason | + 'unpublish' | + 'forceUnpublishByUser' | + 'forceUnpublishByServer'; + +export type RecordingEventReason = + 'recordingStoppedByServer' | + 'sessionClosedByServer' | + 'automaticStop' | + 'nodeCrashed'; + +export type SpeechToTextEventReason = + 'recognizing' | + 'recognized'; \ No newline at end of file diff --git a/openvidu-browser/src/index.ts b/openvidu-browser/src/index.ts index a9f538fc..e4fb3588 100644 --- a/openvidu-browser/src/index.ts +++ b/openvidu-browser/src/index.ts @@ -45,5 +45,7 @@ export { SessionEventMap } from './OpenViduInternal/Events/EventMap/SessionEvent export { StreamManagerEventMap } from './OpenViduInternal/Events/EventMap/StreamManagerEventMap'; export { PublisherEventMap } from './OpenViduInternal/Events/EventMap/PublisherEventMap'; +export * from './OpenViduInternal/Events/Types/Types'; + // Disable jsnlog when library is loaded JL.setOptions({ enabled: false });