-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VBLOCKS-2214 | Remove unnecessary enumerateDevices calls #199
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ import { InvalidArgumentError, NotSupportedError } from './errors'; | |
import Log from './log'; | ||
import OutputDeviceCollection from './outputdevicecollection'; | ||
import MediaDeviceInfoShim from './shims/mediadeviceinfo'; | ||
import getMediaDevicesInstance from './shims/mediadevices'; | ||
import { average, difference, isFirefox } from './util'; | ||
|
||
/** | ||
|
@@ -176,11 +175,11 @@ class AudioHelper extends EventEmitter { | |
}, options); | ||
|
||
this._getUserMedia = getUserMedia; | ||
this._mediaDevices = options.mediaDevices || getMediaDevicesInstance() as AudioHelper.MediaDevicesLike; | ||
this._mediaDevices = options.mediaDevices || navigator.mediaDevices; | ||
this._onActiveInputChanged = onActiveInputChanged; | ||
this._enumerateDevices = typeof options.enumerateDevices === 'function' | ||
? options.enumerateDevices | ||
: this._mediaDevices && this._mediaDevices.enumerateDevices; | ||
: this._mediaDevices && this._mediaDevices.enumerateDevices.bind(this._mediaDevices); | ||
|
||
const isAudioContextSupported: boolean = !!(options.AudioContext || options.audioContext); | ||
const isEnumerationSupported: boolean = !!this._enumerateDevices; | ||
|
@@ -308,10 +307,41 @@ class AudioHelper extends EventEmitter { | |
|
||
if (this._mediaDevices.removeEventListener) { | ||
this._mediaDevices.removeEventListener('devicechange', this._updateAvailableDevices); | ||
this._mediaDevices.removeEventListener('deviceinfochange', this._updateAvailableDevices); | ||
} | ||
} | ||
|
||
/** | ||
* Update the available input and output devices | ||
* @private | ||
*/ | ||
_updateAvailableDevices = (): Promise<void> => { | ||
if (!this._mediaDevices || !this._enumerateDevices) { | ||
return Promise.reject('Enumeration not supported'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to reject an error here, or just a string? Also, could we make this an async function and then use await and throw in the function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the original implementation and did not see any reason to change it. The only change that was required is to make it an internal-only public API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. Let's leave it as-is then. |
||
} | ||
|
||
return this._enumerateDevices().then((devices: MediaDeviceInfo[]) => { | ||
this._updateDevices(devices.filter((d: MediaDeviceInfo) => d.kind === 'audiooutput'), | ||
this.availableOutputDevices, | ||
this._removeLostOutput); | ||
|
||
this._updateDevices(devices.filter((d: MediaDeviceInfo) => d.kind === 'audioinput'), | ||
this.availableInputDevices, | ||
this._removeLostInput); | ||
|
||
const defaultDevice = this.availableOutputDevices.get('default') | ||
|| Array.from(this.availableOutputDevices.values())[0]; | ||
|
||
[this.speakerDevices, this.ringtoneDevices].forEach(outputDevices => { | ||
if (!outputDevices.get().size && this.availableOutputDevices.size && this.isOutputSelectionSupported) { | ||
outputDevices.set(defaultDevice.deviceId) | ||
.catch((reason) => { | ||
this._log.warn(`Unable to set audio output devices. ${reason}`); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Enable or disable the disconnect sound. | ||
* @param doEnable Passing `true` will enable the sound and `false` will disable the sound. | ||
|
@@ -426,7 +456,6 @@ class AudioHelper extends EventEmitter { | |
|
||
if (this._mediaDevices.addEventListener) { | ||
this._mediaDevices.addEventListener('devicechange', this._updateAvailableDevices); | ||
this._mediaDevices.addEventListener('deviceinfochange', this._updateAvailableDevices); | ||
} | ||
|
||
this._updateAvailableDevices().then(() => { | ||
|
@@ -542,37 +571,6 @@ class AudioHelper extends EventEmitter { | |
}); | ||
} | ||
|
||
/** | ||
* Update the available input and output devices | ||
*/ | ||
private _updateAvailableDevices = (): Promise<void> => { | ||
if (!this._mediaDevices || !this._enumerateDevices) { | ||
return Promise.reject('Enumeration not supported'); | ||
} | ||
|
||
return this._enumerateDevices().then((devices: MediaDeviceInfo[]) => { | ||
this._updateDevices(devices.filter((d: MediaDeviceInfo) => d.kind === 'audiooutput'), | ||
this.availableOutputDevices, | ||
this._removeLostOutput); | ||
|
||
this._updateDevices(devices.filter((d: MediaDeviceInfo) => d.kind === 'audioinput'), | ||
this.availableInputDevices, | ||
this._removeLostInput); | ||
|
||
const defaultDevice = this.availableOutputDevices.get('default') | ||
|| Array.from(this.availableOutputDevices.values())[0]; | ||
|
||
[this.speakerDevices, this.ringtoneDevices].forEach(outputDevices => { | ||
if (!outputDevices.get().size && this.availableOutputDevices.size && this.isOutputSelectionSupported) { | ||
outputDevices.set(defaultDevice.deviceId) | ||
.catch((reason) => { | ||
this._log.warn(`Unable to set audio output devices. ${reason}`); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Update a set of devices. | ||
* @param updatedDevices - An updated list of available Devices | ||
|
@@ -680,7 +678,6 @@ namespace AudioHelper { | |
* that were lost as a result of this deviceChange event. | ||
* @example `device.audio.on('deviceChange', lostActiveDevices => { })` | ||
* @event | ||
* @private | ||
*/ | ||
declare function deviceChangeEvent(lostActiveDevices: MediaDeviceInfo[]): void; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -669,6 +669,10 @@ class Call extends EventEmitter { | |
data: { audioConstraints }, | ||
}, this); | ||
|
||
if (this._options.onGetUserMedia) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's too verbose.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. That seems to be true with the JS SDK, but the RN SDK (which takes direct inspiration from the mobile SDKs) uses past tense like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just capturing our slack discussion here for future reference. This is more of a JS convention. For example, onClick, and not onClicked. onMouseMove, onTouchStart etc. There are exceptions of course. If we need to capture different stages, then we need to change. For example, connecting, connected, reconnecting, reconnected. |
||
this._options.onGetUserMedia(); | ||
} | ||
|
||
connect(); | ||
}, (error: Record<string, any>) => { | ||
let twilioError; | ||
|
@@ -1914,6 +1918,11 @@ namespace Call { | |
*/ | ||
offerSdp?: string | null; | ||
|
||
/** | ||
* Called after a successful getUserMedia call | ||
*/ | ||
onGetUserMedia?: () => void; | ||
|
||
/** | ||
* Whether this is a preflight call or not | ||
*/ | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this a browser compatibility thing we no longer need?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.