From acdfef3e215b339370f3f519436a078542ec5703 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Tue, 10 Dec 2019 14:14:54 +0100 Subject: [PATCH] openvidu-browser: StreamManager#updatePublisherSpeakingEventsOptions --- openvidu-browser/src/OpenVidu/OpenVidu.ts | 4 ++- openvidu-browser/src/OpenVidu/Stream.ts | 8 +++-- .../src/OpenVidu/StreamManager.ts | 29 +++++++++++++++++++ .../Events/PublisherSpeakingEvent.ts | 2 -- .../Events/StreamManagerEvent.ts | 2 +- .../Public/OpenViduAdvancedConfiguration.ts | 6 ++-- 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/openvidu-browser/src/OpenVidu/OpenVidu.ts b/openvidu-browser/src/OpenVidu/OpenVidu.ts index d2cd570e..0d26510b 100644 --- a/openvidu-browser/src/OpenVidu/OpenVidu.ts +++ b/openvidu-browser/src/OpenVidu/OpenVidu.ts @@ -614,7 +614,9 @@ export class OpenVidu { * Set OpenVidu advanced configuration options. Currently `configuration` is an object with the following optional properties (see [[OpenViduAdvancedConfiguration]] for more details): * - `iceServers`: set custom STUN/TURN servers to be used by OpenVidu Browser * - `screenShareChromeExtension`: url to a custom screen share extension for Chrome to be used instead of the default one, based on ours [https://github.com/OpenVidu/openvidu-screen-sharing-chrome-extension](https://github.com/OpenVidu/openvidu-screen-sharing-chrome-extension) - * - `publisherSpeakingEventsOptions`: custom configuration for the [[PublisherSpeakingEvent]] feature + * - `publisherSpeakingEventsOptions`: custom configuration for the [[PublisherSpeakingEvent]] feature and the [StreamManagerEvent.streamAudioVolumeChange](/api/openvidu-browser/classes/streammanagerevent.html) feature + * + * Call this method to override previous values at any moment. */ setAdvancedConfiguration(configuration: OpenViduAdvancedConfiguration): void { this.advancedConfiguration = configuration; diff --git a/openvidu-browser/src/OpenVidu/Stream.ts b/openvidu-browser/src/OpenVidu/Stream.ts index 87930dd1..dd9c93df 100644 --- a/openvidu-browser/src/OpenVidu/Stream.ts +++ b/openvidu-browser/src/OpenVidu/Stream.ts @@ -188,6 +188,10 @@ export class Stream implements EventDispatcher { * @hidden */ volumeChangeEventEnabledOnce = false; + /** + * @hidden + */ + harkOptions; /** @@ -768,8 +772,8 @@ export class Stream implements EventDispatcher { private setSpeechEventIfNotExists(): boolean { if (!!this.mediaStream) { if (!this.speechEvent) { - const harkOptions = this.session.openvidu.advancedConfiguration.publisherSpeakingEventsOptions || {}; - harkOptions.interval = (typeof harkOptions.interval === 'number') ? harkOptions.interval : 50; + const harkOptions = !!this.harkOptions ? this.harkOptions : (this.session.openvidu.advancedConfiguration.publisherSpeakingEventsOptions || {}); + harkOptions.interval = (typeof harkOptions.interval === 'number') ? harkOptions.interval : 100; harkOptions.threshold = (typeof harkOptions.threshold === 'number') ? harkOptions.threshold : -50; this.speechEvent = hark(this.mediaStream, harkOptions); } diff --git a/openvidu-browser/src/OpenVidu/StreamManager.ts b/openvidu-browser/src/OpenVidu/StreamManager.ts index 618c3eab..66fc0294 100644 --- a/openvidu-browser/src/OpenVidu/StreamManager.ts +++ b/openvidu-browser/src/OpenVidu/StreamManager.ts @@ -343,6 +343,33 @@ export class StreamManager implements EventDispatcher { return video; } + /** + * Updates the current configuration for the [[PublisherSpeakingEvent]] feature and the [StreamManagerEvent.streamAudioVolumeChange](/api/openvidu-browser/classes/streammanagerevent.html) feature for this specific + * StreamManager audio stream, overriding the global options set with [[OpenVidu.setAdvancedConfiguration]]. This way you can customize the audio events options + * for each specific StreamManager and change them dynamically. + * + * @param publisherSpeakingEventsOptions New options to be applied to this StreamManager's audio stream. It is an object which includes the following optional properties: + * - `interval`: (number) how frequently the analyser polls the audio stream to check if speaking has started/stopped or audio volume has changed. Default **100** (ms) + * - `threshold`: (number) the volume at which _publisherStartSpeaking_, _publisherStopSpeaking_ events will be fired. Default **-50** (dB) + */ + updatePublisherSpeakingEventsOptions(publisherSpeakingEventsOptions): void { + const currentHarkOptions = !!this.stream.harkOptions ? this.stream.harkOptions : (this.stream.session.openvidu.advancedConfiguration.publisherSpeakingEventsOptions || {}); + const newInterval = (typeof publisherSpeakingEventsOptions.interval === 'number') ? + publisherSpeakingEventsOptions.interval : ((typeof currentHarkOptions.interval === 'number') ? currentHarkOptions.interval : 100); + const newThreshold = (typeof publisherSpeakingEventsOptions.threshold === 'number') ? + publisherSpeakingEventsOptions.threshold : ((typeof currentHarkOptions.threshold === 'number') ? currentHarkOptions.threshold : -50); + this.stream.harkOptions = { + interval: newInterval, + threshold: newThreshold + }; + if (!!this.stream.speechEvent) { + this.stream.speechEvent.setInterval(newInterval); + this.stream.speechEvent.setThreshold(newThreshold); + } + } + + /* Hidden methods */ + /** * @hidden */ @@ -458,6 +485,8 @@ export class StreamManager implements EventDispatcher { this.ee.emitEvent(type, eventArray); } + /* Private methods */ + private pushNewStreamManagerVideo(streamManagerVideo: StreamManagerVideo) { this.videos.push(streamManagerVideo); this.addPlayEventToFirstVideo(); diff --git a/openvidu-browser/src/OpenViduInternal/Events/PublisherSpeakingEvent.ts b/openvidu-browser/src/OpenViduInternal/Events/PublisherSpeakingEvent.ts index 29ca0b12..5da1427e 100644 --- a/openvidu-browser/src/OpenViduInternal/Events/PublisherSpeakingEvent.ts +++ b/openvidu-browser/src/OpenViduInternal/Events/PublisherSpeakingEvent.ts @@ -27,8 +27,6 @@ import { Session } from '../..'; * * More information: * - This events will only be triggered for **remote streams that have audio tracks** ([[Stream.hasAudio]] must be true) - * - Both events share the same lifecycle. That means that you can subscribe to only one of them if you want, but if you call `Session.off('publisherStopSpeaking')`, - * keep in mind that this will also internally remove any 'publisherStartSpeaking' event * - You can further configure how the events are dispatched by setting property `publisherSpeakingEventsOptions` in the call of [[OpenVidu.setAdvancedConfiguration]] */ export class PublisherSpeakingEvent extends Event { diff --git a/openvidu-browser/src/OpenViduInternal/Events/StreamManagerEvent.ts b/openvidu-browser/src/OpenViduInternal/Events/StreamManagerEvent.ts index a905cf58..5972959b 100644 --- a/openvidu-browser/src/OpenViduInternal/Events/StreamManagerEvent.ts +++ b/openvidu-browser/src/OpenViduInternal/Events/StreamManagerEvent.ts @@ -24,7 +24,7 @@ import { StreamManager } from '../../OpenVidu/StreamManager'; * and has begun to play). This event will be dispatched when these 3 conditions are met 1) The StreamManager has no video associated in the DOM 2) It is associated to one video 3) That video starts playing * - `streamAudioVolumeChange`: dispatched by [[StreamManager]] ([[Publisher]] and [[Subscriber]]) when the volume of its Stream's audio track * changes. Only applies if [[Stream.hasAudio]] is `true`. The frequency this event is fired with is defined by property `interval` of - * [[OpenViduAdvancedConfiguration.publisherSpeakingEventsOptions]] (default 50ms) + * [[OpenViduAdvancedConfiguration.publisherSpeakingEventsOptions]] (default 100ms) */ export class StreamManagerEvent extends Event { diff --git a/openvidu-browser/src/OpenViduInternal/Interfaces/Public/OpenViduAdvancedConfiguration.ts b/openvidu-browser/src/OpenViduInternal/Interfaces/Public/OpenViduAdvancedConfiguration.ts index f6eeee4d..fda2b965 100644 --- a/openvidu-browser/src/OpenViduInternal/Interfaces/Public/OpenViduAdvancedConfiguration.ts +++ b/openvidu-browser/src/OpenViduInternal/Interfaces/Public/OpenViduAdvancedConfiguration.ts @@ -33,9 +33,11 @@ export interface OpenViduAdvancedConfiguration { screenShareChromeExtension?: string; /** - * Custom configuration for the [[PublisherSpeakingEvent]] feature. It is an object which includes the following optional properties: - * - `interval`: (number) how frequently the analyser polls the audio stream to check if speaking has started or stopped. Default **50** (ms) + * Custom configuration for the [[PublisherSpeakingEvent]] feature and the [StreamManagerEvent.streamAudioVolumeChange](/api/openvidu-browser/classes/streammanagerevent.html) feature. It is an object which includes the following optional properties: + * - `interval`: (number) how frequently the analyser polls the audio stream to check if speaking has started/stopped or audio volume has changed. Default **100** (ms) * - `threshold`: (number) the volume at which _publisherStartSpeaking_ and _publisherStopSpeaking_ events will be fired. Default **-50** (dB) + * + * This sets the global default configuration that will affect all streams, but you can later customize these values for each specific stream by calling [[StreamManager.updatePublisherSpeakingEventsOptions]] */ publisherSpeakingEventsOptions?: any;