mirror of https://github.com/OpenVidu/openvidu.git
openvidu-browser: PublisherSpeaking and StreamVolumeChange events refactoring
parent
c387145894
commit
78ffeca860
|
@ -100,7 +100,19 @@ export class Session implements EventDispatcher {
|
||||||
/**
|
/**
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
speakingEventsEnabled = false;
|
startSpeakingEventsEnabled = false;
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
startSpeakingEventsEnabledOnce = false;
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
stopSpeakingEventsEnabled = false;
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
stopSpeakingEventsEnabledOnce = false;
|
||||||
|
|
||||||
private ee = new EventEmitter();
|
private ee = new EventEmitter();
|
||||||
|
|
||||||
|
@ -569,13 +581,23 @@ export class Session implements EventDispatcher {
|
||||||
handler(event);
|
handler(event);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (type === 'publisherStartSpeaking' || type === 'publisherStopSpeaking') {
|
if (type === 'publisherStartSpeaking') {
|
||||||
this.speakingEventsEnabled = true;
|
this.startSpeakingEventsEnabled = true;
|
||||||
// If there are already available remote streams, enable hark 'speaking' event in all of them
|
// If there are already available remote streams, enable hark 'speaking' event in all of them
|
||||||
for (const connectionId in this.remoteConnections) {
|
for (const connectionId in this.remoteConnections) {
|
||||||
const str = this.remoteConnections[connectionId].stream;
|
const str = this.remoteConnections[connectionId].stream;
|
||||||
if (!!str && str.hasAudio) {
|
if (!!str && str.hasAudio) {
|
||||||
str.enableSpeakingEvents();
|
str.enableStartSpeakingEvent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (type === 'publisherStopSpeaking') {
|
||||||
|
this.stopSpeakingEventsEnabled = true;
|
||||||
|
// If there are already available remote streams, enable hark 'stopped_speaking' event in all of them
|
||||||
|
for (const connectionId in this.remoteConnections) {
|
||||||
|
const str = this.remoteConnections[connectionId].stream;
|
||||||
|
if (!!str && str.hasAudio) {
|
||||||
|
str.enableStopSpeakingEvent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -591,20 +613,30 @@ export class Session implements EventDispatcher {
|
||||||
|
|
||||||
this.ee.once(type, event => {
|
this.ee.once(type, event => {
|
||||||
if (event) {
|
if (event) {
|
||||||
console.info("Event '" + type + "' triggered by 'Session'", event);
|
console.info("Event '" + type + "' triggered once by 'Session'", event);
|
||||||
} else {
|
} else {
|
||||||
console.info("Event '" + type + "' triggered by 'Session'");
|
console.info("Event '" + type + "' triggered once by 'Session'");
|
||||||
}
|
}
|
||||||
handler(event);
|
handler(event);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (type === 'publisherStartSpeaking' || type === 'publisherStopSpeaking') {
|
if (type === 'publisherStartSpeaking') {
|
||||||
this.speakingEventsEnabled = true;
|
this.startSpeakingEventsEnabledOnce = true;
|
||||||
// If there are already available remote streams, enable hark in all of them
|
// If there are already available remote streams, enable hark 'speaking' event in all of them once
|
||||||
for (const connectionId in this.remoteConnections) {
|
for (const connectionId in this.remoteConnections) {
|
||||||
const str = this.remoteConnections[connectionId].stream;
|
const str = this.remoteConnections[connectionId].stream;
|
||||||
if (!!str && str.hasAudio) {
|
if (!!str && str.hasAudio) {
|
||||||
str.enableOnceSpeakingEvents();
|
str.enableOnceStartSpeakingEvent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (type === 'publisherStopSpeaking') {
|
||||||
|
this.stopSpeakingEventsEnabledOnce = true;
|
||||||
|
// If there are already available remote streams, enable hark 'stopped_speaking' event in all of them once
|
||||||
|
for (const connectionId in this.remoteConnections) {
|
||||||
|
const str = this.remoteConnections[connectionId].stream;
|
||||||
|
if (!!str && str.hasAudio) {
|
||||||
|
str.enableOnceStopSpeakingEvent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -617,21 +649,35 @@ export class Session implements EventDispatcher {
|
||||||
* See [[EventDispatcher.off]]
|
* See [[EventDispatcher.off]]
|
||||||
*/
|
*/
|
||||||
off(type: string, handler?: (event: SessionDisconnectedEvent | SignalEvent | StreamEvent | ConnectionEvent | PublisherSpeakingEvent | RecordingEvent) => void): Session {
|
off(type: string, handler?: (event: SessionDisconnectedEvent | SignalEvent | StreamEvent | ConnectionEvent | PublisherSpeakingEvent | RecordingEvent) => void): Session {
|
||||||
|
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
this.ee.removeAllListeners(type);
|
this.ee.removeAllListeners(type);
|
||||||
} else {
|
} else {
|
||||||
this.ee.off(type, handler);
|
this.ee.off(type, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'publisherStartSpeaking' || type === 'publisherStopSpeaking') {
|
if (type === 'publisherStartSpeaking') {
|
||||||
this.speakingEventsEnabled = false;
|
let remainingStartSpeakingListeners = this.ee.getListeners(type).length;
|
||||||
|
if (remainingStartSpeakingListeners === 0) {
|
||||||
|
this.startSpeakingEventsEnabled = false;
|
||||||
// If there are already available remote streams, disable hark in all of them
|
// If there are already available remote streams, disable hark in all of them
|
||||||
for (const connectionId in this.remoteConnections) {
|
for (const connectionId in this.remoteConnections) {
|
||||||
const str = this.remoteConnections[connectionId].stream;
|
const str = this.remoteConnections[connectionId].stream;
|
||||||
if (!!str) {
|
if (!!str) {
|
||||||
str.disableSpeakingEvents();
|
str.disableStartSpeakingEvent(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (type === 'publisherStopSpeaking') {
|
||||||
|
let remainingStopSpeakingListeners = this.ee.getListeners(type).length;
|
||||||
|
if (remainingStopSpeakingListeners === 0) {
|
||||||
|
this.stopSpeakingEventsEnabled = false;
|
||||||
|
// If there are already available remote streams, disable hark in all of them
|
||||||
|
for (const connectionId in this.remoteConnections) {
|
||||||
|
const str = this.remoteConnections[connectionId].stream;
|
||||||
|
if (!!str) {
|
||||||
|
str.disableStopSpeakingEvent(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -168,14 +168,26 @@ export class Stream implements EventDispatcher {
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
publisherStartSpeakingEventEnabled = false;
|
publisherStartSpeakingEventEnabled = false;
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
publisherStartSpeakingEventEnabledOnce = false;
|
||||||
/**
|
/**
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
publisherStopSpeakingEventEnabled = false;
|
publisherStopSpeakingEventEnabled = false;
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
publisherStopSpeakingEventEnabledOnce = false;
|
||||||
/**
|
/**
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
volumeChangeEventEnabled = false;
|
volumeChangeEventEnabled = false;
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
volumeChangeEventEnabledOnce = false;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -529,30 +541,13 @@ export class Stream implements EventDispatcher {
|
||||||
/**
|
/**
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
setSpeechEventIfNotExists(): void {
|
enableStartSpeakingEvent(): void {
|
||||||
if (!this.speechEvent) {
|
|
||||||
const harkOptions = this.session.openvidu.advancedConfiguration.publisherSpeakingEventsOptions || {};
|
|
||||||
harkOptions.interval = (typeof harkOptions.interval === 'number') ? harkOptions.interval : 50;
|
|
||||||
harkOptions.threshold = (typeof harkOptions.threshold === 'number') ? harkOptions.threshold : -50;
|
|
||||||
this.speechEvent = hark(this.mediaStream, harkOptions);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @hidden
|
|
||||||
*/
|
|
||||||
enableSpeakingEvents(): void {
|
|
||||||
this.setSpeechEventIfNotExists();
|
this.setSpeechEventIfNotExists();
|
||||||
if (!this.publisherStartSpeakingEventEnabled) {
|
if (!this.publisherStartSpeakingEventEnabled) {
|
||||||
this.publisherStartSpeakingEventEnabled = true;
|
this.publisherStartSpeakingEventEnabled = true;
|
||||||
this.speechEvent.on('speaking', () => {
|
this.speechEvent.on('speaking', () => {
|
||||||
this.session.emitEvent('publisherStartSpeaking', [new PublisherSpeakingEvent(this.session, 'publisherStartSpeaking', this.connection, this.streamId)]);
|
this.session.emitEvent('publisherStartSpeaking', [new PublisherSpeakingEvent(this.session, 'publisherStartSpeaking', this.connection, this.streamId)]);
|
||||||
});
|
this.publisherStartSpeakingEventEnabledOnce = false; // Disable 'once' version if 'on' version was triggered
|
||||||
}
|
|
||||||
if (!this.publisherStopSpeakingEventEnabled) {
|
|
||||||
this.publisherStopSpeakingEventEnabled = true;
|
|
||||||
this.speechEvent.on('stopped_speaking', () => {
|
|
||||||
this.session.emitEvent('publisherStopSpeaking', [new PublisherSpeakingEvent(this.session, 'publisherStopSpeaking', this.connection, this.streamId)]);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -560,20 +555,16 @@ export class Stream implements EventDispatcher {
|
||||||
/**
|
/**
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
enableOnceSpeakingEvents(): void {
|
enableOnceStartSpeakingEvent(): void {
|
||||||
this.setSpeechEventIfNotExists();
|
this.setSpeechEventIfNotExists();
|
||||||
if (!this.publisherStartSpeakingEventEnabled) {
|
if (!this.publisherStartSpeakingEventEnabledOnce) {
|
||||||
this.publisherStartSpeakingEventEnabled = true;
|
this.publisherStartSpeakingEventEnabledOnce = true;
|
||||||
this.speechEvent.once('speaking', () => {
|
this.speechEvent.once('speaking', () => {
|
||||||
|
if (this.publisherStartSpeakingEventEnabledOnce) {
|
||||||
|
// If the listener has been disabled in the meantime (for example by the 'on' version) do not trigger the event
|
||||||
this.session.emitEvent('publisherStartSpeaking', [new PublisherSpeakingEvent(this.session, 'publisherStartSpeaking', this.connection, this.streamId)]);
|
this.session.emitEvent('publisherStartSpeaking', [new PublisherSpeakingEvent(this.session, 'publisherStartSpeaking', this.connection, this.streamId)]);
|
||||||
this.disableSpeakingEvents();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
if (!this.publisherStopSpeakingEventEnabled) {
|
this.disableStartSpeakingEvent(true);
|
||||||
this.publisherStopSpeakingEventEnabled = true;
|
|
||||||
this.speechEvent.once('stopped_speaking', () => {
|
|
||||||
this.session.emitEvent('publisherStopSpeaking', [new PublisherSpeakingEvent(this.session, 'publisherStopSpeaking', this.connection, this.streamId)]);
|
|
||||||
this.disableSpeakingEvents();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -581,27 +572,99 @@ export class Stream implements EventDispatcher {
|
||||||
/**
|
/**
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
disableSpeakingEvents(): void {
|
disableStartSpeakingEvent(disabledByOnce: boolean): void {
|
||||||
if (!!this.speechEvent) {
|
if (!!this.speechEvent) {
|
||||||
if (this.volumeChangeEventEnabled) {
|
this.publisherStartSpeakingEventEnabledOnce = false;
|
||||||
// 'streamAudioVolumeChange' event is enabled. Cannot stop the hark process
|
if (disabledByOnce) {
|
||||||
this.speechEvent.off('speaking');
|
if (this.publisherStartSpeakingEventEnabled) {
|
||||||
this.speechEvent.off('stopped_speaking');
|
// The 'on' version of this same event is enabled too. Do not remove the hark listener
|
||||||
|
return;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
this.publisherStartSpeakingEventEnabled = false;
|
||||||
|
}
|
||||||
|
// Shutting down the hark event
|
||||||
|
if (this.volumeChangeEventEnabled ||
|
||||||
|
this.volumeChangeEventEnabledOnce ||
|
||||||
|
this.publisherStopSpeakingEventEnabled ||
|
||||||
|
this.publisherStopSpeakingEventEnabledOnce) {
|
||||||
|
// Some other hark event is enabled. Cannot stop the hark process, just remove the specific listener
|
||||||
|
this.speechEvent.off('speaking');
|
||||||
|
} else {
|
||||||
|
// No other hark event is enabled. We can get entirely rid of it
|
||||||
this.speechEvent.stop();
|
this.speechEvent.stop();
|
||||||
delete this.speechEvent;
|
delete this.speechEvent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.publisherStartSpeakingEventEnabled = false;
|
|
||||||
this.publisherStopSpeakingEventEnabled = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
enableVolumeChangeEvent(): void {
|
enableStopSpeakingEvent(): void {
|
||||||
this.setSpeechEventIfNotExists();
|
this.setSpeechEventIfNotExists();
|
||||||
if (!this.volumeChangeEventEnabled) {
|
if (!this.publisherStopSpeakingEventEnabled) {
|
||||||
|
this.publisherStopSpeakingEventEnabled = true;
|
||||||
|
this.speechEvent.on('stopped_speaking', () => {
|
||||||
|
this.session.emitEvent('publisherStopSpeaking', [new PublisherSpeakingEvent(this.session, 'publisherStopSpeaking', this.connection, this.streamId)]);
|
||||||
|
this.publisherStopSpeakingEventEnabledOnce = false; // Disable 'once' version if 'on' version was triggered
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
enableOnceStopSpeakingEvent(): void {
|
||||||
|
this.setSpeechEventIfNotExists();
|
||||||
|
if (!this.publisherStopSpeakingEventEnabledOnce) {
|
||||||
|
this.publisherStopSpeakingEventEnabledOnce = true;
|
||||||
|
this.speechEvent.once('stopped_speaking', () => {
|
||||||
|
if (this.publisherStopSpeakingEventEnabledOnce) {
|
||||||
|
// If the listener has been disabled in the meantime (for example by the 'on' version) do not trigger the event
|
||||||
|
this.session.emitEvent('publisherStopSpeaking', [new PublisherSpeakingEvent(this.session, 'publisherStopSpeaking', this.connection, this.streamId)]);
|
||||||
|
}
|
||||||
|
this.disableStopSpeakingEvent(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
disableStopSpeakingEvent(disabledByOnce: boolean): void {
|
||||||
|
if (!!this.speechEvent) {
|
||||||
|
this.publisherStopSpeakingEventEnabledOnce = false;
|
||||||
|
if (disabledByOnce) {
|
||||||
|
if (this.publisherStopSpeakingEventEnabled) {
|
||||||
|
// We are cancelling the 'once' listener for this event, but the 'on' version
|
||||||
|
// of this same event is enabled too. Do not remove the hark listener
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.publisherStopSpeakingEventEnabled = false;
|
||||||
|
}
|
||||||
|
// Shutting down the hark event
|
||||||
|
if (this.volumeChangeEventEnabled ||
|
||||||
|
this.volumeChangeEventEnabledOnce ||
|
||||||
|
this.publisherStartSpeakingEventEnabled ||
|
||||||
|
this.publisherStartSpeakingEventEnabledOnce) {
|
||||||
|
// Some other hark event is enabled. Cannot stop the hark process, just remove the specific listener
|
||||||
|
this.speechEvent.off('stopped_speaking');
|
||||||
|
} else {
|
||||||
|
// No other hark event is enabled. We can get entirely rid of it
|
||||||
|
this.speechEvent.stop();
|
||||||
|
delete this.speechEvent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @hidden
|
||||||
|
*/
|
||||||
|
enableVolumeChangeEvent(force: boolean): void {
|
||||||
|
if (this.setSpeechEventIfNotExists()) {
|
||||||
|
if (!this.volumeChangeEventEnabled || force) {
|
||||||
this.volumeChangeEventEnabled = true;
|
this.volumeChangeEventEnabled = true;
|
||||||
this.speechEvent.on('volume_change', harkEvent => {
|
this.speechEvent.on('volume_change', harkEvent => {
|
||||||
const oldValue = this.speechEvent.oldVolumeValue;
|
const oldValue = this.speechEvent.oldVolumeValue;
|
||||||
|
@ -610,39 +673,61 @@ export class Stream implements EventDispatcher {
|
||||||
this.streamManager.emitEvent('streamAudioVolumeChange', [new StreamManagerEvent(this.streamManager, 'streamAudioVolumeChange', value)]);
|
this.streamManager.emitEvent('streamAudioVolumeChange', [new StreamManagerEvent(this.streamManager, 'streamAudioVolumeChange', value)]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// This way whenever the MediaStream object is available, the event listener will be automatically added
|
||||||
|
this.volumeChangeEventEnabled = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
enableOnceVolumeChangeEvent(): void {
|
enableOnceVolumeChangeEvent(force: boolean): void {
|
||||||
this.setSpeechEventIfNotExists();
|
if (this.setSpeechEventIfNotExists()) {
|
||||||
if (!this.volumeChangeEventEnabled) {
|
if (!this.volumeChangeEventEnabledOnce || force) {
|
||||||
this.volumeChangeEventEnabled = true;
|
this.volumeChangeEventEnabledOnce = true;
|
||||||
this.speechEvent.once('volume_change', harkEvent => {
|
this.speechEvent.once('volume_change', harkEvent => {
|
||||||
const oldValue = this.speechEvent.oldVolumeValue;
|
const oldValue = this.speechEvent.oldVolumeValue;
|
||||||
const value = { newValue: harkEvent, oldValue };
|
const value = { newValue: harkEvent, oldValue };
|
||||||
this.speechEvent.oldVolumeValue = harkEvent;
|
this.speechEvent.oldVolumeValue = harkEvent;
|
||||||
this.disableVolumeChangeEvent();
|
this.disableVolumeChangeEvent(true);
|
||||||
this.streamManager.emitEvent('streamAudioVolumeChange', [new StreamManagerEvent(this.streamManager, 'streamAudioVolumeChange', value)]);
|
this.streamManager.emitEvent('streamAudioVolumeChange', [new StreamManagerEvent(this.streamManager, 'streamAudioVolumeChange', value)]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// This way whenever the MediaStream object is available, the event listener will be automatically added
|
||||||
|
this.volumeChangeEventEnabledOnce = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @hidden
|
* @hidden
|
||||||
*/
|
*/
|
||||||
disableVolumeChangeEvent(): void {
|
disableVolumeChangeEvent(disabledByOnce: boolean): void {
|
||||||
if (!!this.speechEvent) {
|
if (!!this.speechEvent) {
|
||||||
if (this.session.speakingEventsEnabled) {
|
this.volumeChangeEventEnabledOnce = false;
|
||||||
// 'publisherStartSpeaking' and/or publisherStopSpeaking` events are enabled. Cannot stop the hark process
|
if (disabledByOnce) {
|
||||||
|
if (this.volumeChangeEventEnabled) {
|
||||||
|
// We are cancelling the 'once' listener for this event, but the 'on' version
|
||||||
|
// of this same event is enabled too. Do not remove the hark listener
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.volumeChangeEventEnabled = false;
|
||||||
|
}
|
||||||
|
// Shutting down the hark event
|
||||||
|
if (this.publisherStartSpeakingEventEnabled ||
|
||||||
|
this.publisherStartSpeakingEventEnabledOnce ||
|
||||||
|
this.publisherStopSpeakingEventEnabled ||
|
||||||
|
this.publisherStopSpeakingEventEnabledOnce) {
|
||||||
|
// Some other hark event is enabled. Cannot stop the hark process, just remove the specific listener
|
||||||
this.speechEvent.off('volume_change');
|
this.speechEvent.off('volume_change');
|
||||||
} else {
|
} else {
|
||||||
|
// No other hark event is enabled. We can get entirely rid of it
|
||||||
this.speechEvent.stop();
|
this.speechEvent.stop();
|
||||||
delete this.speechEvent;
|
delete this.speechEvent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.volumeChangeEventEnabled = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -680,9 +765,24 @@ export class Stream implements EventDispatcher {
|
||||||
|
|
||||||
/* Private methods */
|
/* Private methods */
|
||||||
|
|
||||||
|
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;
|
||||||
|
harkOptions.threshold = (typeof harkOptions.threshold === 'number') ? harkOptions.threshold : -50;
|
||||||
|
this.speechEvent = hark(this.mediaStream, harkOptions);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private initWebRtcPeerSend(): Promise<any> {
|
private initWebRtcPeerSend(): Promise<any> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
this.initHarkEvents(); // Init hark events for the local stream
|
||||||
|
|
||||||
const userMediaConstraints = {
|
const userMediaConstraints = {
|
||||||
audio: this.isSendAudio(),
|
audio: this.isSendAudio(),
|
||||||
video: this.isSendVideo()
|
video: this.isSendVideo()
|
||||||
|
@ -849,8 +949,34 @@ export class Stream implements EventDispatcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.updateMediaStreamInVideos();
|
this.updateMediaStreamInVideos();
|
||||||
if (!this.displayMyRemote() && !!this.mediaStream.getAudioTracks()[0] && this.session.speakingEventsEnabled) {
|
this.initHarkEvents(); // Init hark events for the remote stream
|
||||||
this.enableSpeakingEvents();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private initHarkEvents(): void {
|
||||||
|
if (!!this.mediaStream.getAudioTracks()[0]) {
|
||||||
|
// Hark events can only be set if audio track is available
|
||||||
|
if (this.streamManager.remote) {
|
||||||
|
// publisherStartSpeaking/publisherStopSpeaking is only defined for remote streams
|
||||||
|
if (this.session.startSpeakingEventsEnabled) {
|
||||||
|
this.enableStartSpeakingEvent();
|
||||||
|
}
|
||||||
|
if (this.session.startSpeakingEventsEnabledOnce) {
|
||||||
|
this.enableOnceStartSpeakingEvent();
|
||||||
|
}
|
||||||
|
if (this.session.stopSpeakingEventsEnabled) {
|
||||||
|
this.enableStopSpeakingEvent();
|
||||||
|
}
|
||||||
|
if (this.session.stopSpeakingEventsEnabledOnce) {
|
||||||
|
this.enableOnceStopSpeakingEvent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// streamAudioVolumeChange event is defined for both Publishers and Subscribers
|
||||||
|
if (this.volumeChangeEventEnabled) {
|
||||||
|
this.enableVolumeChangeEvent(true);
|
||||||
|
}
|
||||||
|
if (this.volumeChangeEventEnabledOnce) {
|
||||||
|
this.enableOnceVolumeChangeEvent(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,7 +167,7 @@ export class StreamManager implements EventDispatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (type === 'streamAudioVolumeChange' && this.stream.hasAudio) {
|
if (type === 'streamAudioVolumeChange' && this.stream.hasAudio) {
|
||||||
this.stream.enableVolumeChangeEvent();
|
this.stream.enableVolumeChangeEvent(false);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -178,9 +178,9 @@ export class StreamManager implements EventDispatcher {
|
||||||
once(type: string, handler: (event: Event) => void): StreamManager {
|
once(type: string, handler: (event: Event) => void): StreamManager {
|
||||||
this.ee.once(type, event => {
|
this.ee.once(type, event => {
|
||||||
if (event) {
|
if (event) {
|
||||||
console.info("Event '" + type + "' triggered once", event);
|
console.info("Event '" + type + "' triggered once by '" + (this.remote ? 'Subscriber' : 'Publisher') + "'", event);
|
||||||
} else {
|
} else {
|
||||||
console.info("Event '" + type + "' triggered once");
|
console.info("Event '" + type + "' triggered once by '" + (this.remote ? 'Subscriber' : 'Publisher') + "'");
|
||||||
}
|
}
|
||||||
handler(event);
|
handler(event);
|
||||||
});
|
});
|
||||||
|
@ -200,7 +200,7 @@ export class StreamManager implements EventDispatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (type === 'streamAudioVolumeChange' && this.stream.hasAudio) {
|
if (type === 'streamAudioVolumeChange' && this.stream.hasAudio) {
|
||||||
this.stream.enableOnceVolumeChangeEvent();
|
this.stream.enableOnceVolumeChangeEvent(false);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -216,7 +216,10 @@ export class StreamManager implements EventDispatcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'streamAudioVolumeChange') {
|
if (type === 'streamAudioVolumeChange') {
|
||||||
this.stream.disableVolumeChangeEvent();
|
let remainingVolumeEventListeners = this.ee.getListeners(type).length;
|
||||||
|
if (remainingVolumeEventListeners === 0) {
|
||||||
|
this.stream.disableVolumeChangeEvent(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
|
Loading…
Reference in New Issue