Bug 1654430 - Replace 200ms mainthread sleep on inserting/removing USB/bluetooth cam/mic/speaker with WaitFor() promise. r=achronop

Differential Revision: https://phabricator.services.mozilla.com/D84458
This commit is contained in:
Jan-Ivar Bruaroey 2020-09-22 21:08:54 +00:00
Родитель d75995e17b
Коммит 962754db51
2 изменённых файлов: 37 добавлений и 5 удалений

Просмотреть файл

@ -2219,14 +2219,38 @@ void MediaManager::DeviceListChanged() {
// On some Windows machines, if we call EnumerateRawDevices immediately after
// receiving devicechange event, we would get an outdated devices list.
PR_Sleep(PR_MillisecondsToInterval(200));
if (mDeviceChangeTimer) {
mDeviceChangeTimer->Cancel();
} else {
mDeviceChangeTimer = MakeRefPtr<MediaTimer>();
}
RefPtr<MediaManager> self = this;
auto devices = MakeRefPtr<MediaDeviceSetRefCnt>();
EnumerateRawDevices(0, MediaSourceEnum::Camera, MediaSourceEnum::Microphone,
MediaSinkEnum::Speaker, DeviceEnumerationType::Normal,
DeviceEnumerationType::Normal, false, devices)
mDeviceChangeTimer->WaitFor(TimeDuration::FromMilliseconds(200), __func__)
->Then(
GetCurrentSerialEventTarget(), __func__,
[self = RefPtr<MediaManager>(this), this, devices](bool) {
[self, this, devices] {
if (!MediaManager::GetIfExists()) {
return MgrPromise::CreateAndReject(
MakeRefPtr<MediaMgrError>(MediaMgrError::Name::AbortError,
u"In shutdown"_ns),
__func__);
}
return EnumerateRawDevices(
0, MediaSourceEnum::Camera, MediaSourceEnum::Microphone,
MediaSinkEnum::Speaker, DeviceEnumerationType::Normal,
DeviceEnumerationType::Normal, false, devices);
},
[]() {
// Timer was canceled by us, or we're in shutdown.
return MgrPromise::CreateAndReject(
MakeRefPtr<MediaMgrError>(MediaMgrError::Name::AbortError),
__func__);
})
->Then(
GetCurrentSerialEventTarget(), __func__,
[self, this, devices](bool) {
if (!MediaManager::GetIfExists()) {
return;
}
@ -3618,6 +3642,12 @@ void MediaManager::Shutdown() {
#endif
}
if (mDeviceChangeTimer) {
mDeviceChangeTimer->Cancel();
// Drop ref to MediaTimer early to avoid blocking SharedThreadPool shutdown
mDeviceChangeTimer = nullptr;
}
{
// Close off any remaining active windows.

Просмотреть файл

@ -41,6 +41,7 @@ class nsIPrefBranch;
namespace mozilla {
class TaskQueue;
class MediaTimer;
namespace dom {
struct MediaStreamConstraints;
struct MediaTrackConstraints;
@ -343,6 +344,7 @@ class MediaManager final : public nsIMediaManagerService, public nsIObserver {
nsRefPtrHashtable<nsStringHashKey, GetUserMediaTask> mActiveCallbacks;
nsClassHashtable<nsUint64HashKey, nsTArray<nsString>> mCallIds;
nsTArray<RefPtr<dom::GetUserMediaRequest>> mPendingGUMRequest;
RefPtr<MediaTimer> mDeviceChangeTimer;
bool mCamerasMuted = false;
bool mMicrophonesMuted = false;