From 5d13851360df3bf219660d4e5589617968e7b2fe Mon Sep 17 00:00:00 2001 From: Alex Chronopoulos Date: Wed, 11 Jul 2018 08:59:13 +0200 Subject: [PATCH] Bug 1152401 - Use MediaDevice in MediaEngine to allow enumeration of both sinks and sources. r=jib MozReview-Commit-ID: 6ntA5YUFSnN --HG-- extra : rebase_source : d90bc121bc8e4152d76429e2a49f4d4cc1cf439e --- dom/media/MediaManager.cpp | 53 ++++++++++--------------- dom/media/webrtc/MediaEngine.h | 2 +- dom/media/webrtc/MediaEngineDefault.cpp | 19 ++++++--- dom/media/webrtc/MediaEngineDefault.h | 2 +- dom/media/webrtc/MediaEngineWebRTC.cpp | 46 ++++++++++++--------- dom/media/webrtc/MediaEngineWebRTC.h | 6 +-- 6 files changed, 69 insertions(+), 59 deletions(-) diff --git a/dom/media/MediaManager.cpp b/dom/media/MediaManager.cpp index cc32a7841178..6213cd247140 100644 --- a/dom/media/MediaManager.cpp +++ b/dom/media/MediaManager.cpp @@ -1494,19 +1494,19 @@ private: // Source getter returning full list static void -GetSources(MediaEngine *aEngine, - uint64_t aWindowId, - MediaSourceEnum aSrcType, - nsTArray>& aResult, - const char* aMediaDeviceName = nullptr) +GetMediaDevices(MediaEngine *aEngine, + uint64_t aWindowId, + MediaSourceEnum aSrcType, + nsTArray>& aResult, + const char* aMediaDeviceName = nullptr) { MOZ_ASSERT(MediaManager::IsInMediaThread()); LOG(("%s: aEngine=%p, aWindowId=%" PRIu64 ", aSrcType=%" PRIu8 ", aMediaDeviceName=%s", __func__, aEngine, aWindowId, static_cast(aSrcType), aMediaDeviceName ? aMediaDeviceName : "null")); - nsTArray> sources; - aEngine->EnumerateDevices(aWindowId, aSrcType, &sources); + nsTArray> devices; + aEngine->EnumerateDevices(aWindowId, aSrcType, &devices); /* * We're allowing multiple tabs to access the same camera for parity @@ -1515,25 +1515,20 @@ GetSources(MediaEngine *aEngine, * to. */ if (aMediaDeviceName && *aMediaDeviceName) { - for (auto& source : sources) { - nsString deviceName = source->GetName(); - if (deviceName.EqualsASCII(aMediaDeviceName)) { - aResult.AppendElement(MakeRefPtr( - source, - source->GetName(), - NS_ConvertUTF8toUTF16(source->GetUUID()))); + for (auto& device : devices) { + if (device->mName.EqualsASCII(aMediaDeviceName)) { + aResult.AppendElement(device); LOG(("%s: found aMediaDeviceName=%s", __func__, aMediaDeviceName)); break; } } } else { - for (auto& source : sources) { - aResult.AppendElement(MakeRefPtr( - source, - source->GetName(), - NS_ConvertUTF8toUTF16(source->GetUUID()))); - LOG(("%s: appending device=%s", __func__, - NS_ConvertUTF16toUTF8(source->GetName()).get())); + aResult = devices; + if (MOZ_LOG_TEST(GetMediaManagerLog(), mozilla::LogLevel::Debug)) { + for (auto& device : devices) { + LOG(("%s: appending device=%s", __func__, + NS_ConvertUTF16toUTF8(device->mName).get())); + } } } } @@ -1951,21 +1946,17 @@ MediaManager::EnumerateRawDevices(uint64_t aWindowId, SourceSet videos; LOG(("EnumerateRawDevices Task: Getting video sources with %s backend", aVideoEnumType == DeviceEnumerationType::Fake ? "fake" : "real")); - GetSources(aVideoEnumType == DeviceEnumerationType::Fake ? fakeBackend : realBackend, - aWindowId, aVideoType, videos, videoLoopDev.get()); - for (auto& source : videos) { - result->AppendElement(source); - } + GetMediaDevices(aVideoEnumType == DeviceEnumerationType::Fake ? fakeBackend : realBackend, + aWindowId, aVideoType, videos, videoLoopDev.get()); + result->AppendElements(videos); } if (hasAudio) { SourceSet audios; LOG(("EnumerateRawDevices Task: Getting audio sources with %s backend", aVideoEnumType == DeviceEnumerationType::Fake ? "fake" : "real")); - GetSources(aAudioEnumType == DeviceEnumerationType::Fake ? fakeBackend : realBackend, - aWindowId, aAudioType, audios, audioLoopDev.get()); - for (auto& source : audios) { - result->AppendElement(source); - } + GetMediaDevices(aAudioEnumType == DeviceEnumerationType::Fake ? fakeBackend : realBackend, + aWindowId, aAudioType, audios, audioLoopDev.get()); + result->AppendElements(audios); } NS_DispatchToMainThread(NewRunnableFrom([id, result = std::move(result)]() mutable { MediaManager* mgr = MediaManager::GetIfExists(); diff --git a/dom/media/webrtc/MediaEngine.h b/dom/media/webrtc/MediaEngine.h index 8451422d4294..6b6c21f7ab91 100644 --- a/dom/media/webrtc/MediaEngine.h +++ b/dom/media/webrtc/MediaEngine.h @@ -51,7 +51,7 @@ public: */ virtual void EnumerateDevices(uint64_t aWindowId, dom::MediaSourceEnum, - nsTArray>*) = 0; + nsTArray>*) = 0; virtual void ReleaseResourcesForWindow(uint64_t aWindowId) = 0; virtual void Shutdown() = 0; diff --git a/dom/media/webrtc/MediaEngineDefault.cpp b/dom/media/webrtc/MediaEngineDefault.cpp index 898f7191e31a..5ed77b40d896 100644 --- a/dom/media/webrtc/MediaEngineDefault.cpp +++ b/dom/media/webrtc/MediaEngineDefault.cpp @@ -559,7 +559,7 @@ MediaEngineDefaultAudioSource::Pull(const RefPtr& aHandl void MediaEngineDefault::EnumerateDevices(uint64_t aWindowId, dom::MediaSourceEnum aMediaSource, - nsTArray>* aSources) + nsTArray>* aDevices) { AssertIsOnOwningThread(); @@ -575,7 +575,10 @@ MediaEngineDefault::EnumerateDevices(uint64_t aWindowId, devicesForThisWindow = mVSources.LookupOrAdd(aWindowId); auto newSource = MakeRefPtr(); devicesForThisWindow->AppendElement(newSource); - aSources->AppendElement(newSource); + aDevices->AppendElement(MakeRefPtr( + newSource, + newSource->GetName(), + NS_ConvertUTF8toUTF16(newSource->GetUUID()))); return; } case dom::MediaSourceEnum::Microphone: { @@ -583,15 +586,21 @@ MediaEngineDefault::EnumerateDevices(uint64_t aWindowId, devicesForThisWindow = mASources.LookupOrAdd(aWindowId); for (const RefPtr& source : *devicesForThisWindow) { if (source->IsAvailable()) { - aSources->AppendElement(source); + aDevices->AppendElement(MakeRefPtr( + source, + source->GetName(), + NS_ConvertUTF8toUTF16(source->GetUUID()))); } } - if (aSources->IsEmpty()) { + if (aDevices->IsEmpty()) { // All streams are currently busy, just make a new one. auto newSource = MakeRefPtr(); devicesForThisWindow->AppendElement(newSource); - aSources->AppendElement(newSource); + aDevices->AppendElement(MakeRefPtr( + newSource, + newSource->GetName(), + NS_ConvertUTF8toUTF16(newSource->GetUUID()))); } return; } diff --git a/dom/media/webrtc/MediaEngineDefault.h b/dom/media/webrtc/MediaEngineDefault.h index f0178f48cd66..24f12a1da2ef 100644 --- a/dom/media/webrtc/MediaEngineDefault.h +++ b/dom/media/webrtc/MediaEngineDefault.h @@ -189,7 +189,7 @@ public: void EnumerateDevices(uint64_t aWindowId, dom::MediaSourceEnum, - nsTArray>*) override; + nsTArray>*) override; void Shutdown() override; void ReleaseResourcesForWindow(uint64_t aWindowId) override; diff --git a/dom/media/webrtc/MediaEngineWebRTC.cpp b/dom/media/webrtc/MediaEngineWebRTC.cpp index ff72e86398eb..44a59effe28a 100644 --- a/dom/media/webrtc/MediaEngineWebRTC.cpp +++ b/dom/media/webrtc/MediaEngineWebRTC.cpp @@ -125,7 +125,7 @@ MediaEngineWebRTC::SetFakeDeviceChangeEvents() void MediaEngineWebRTC::EnumerateVideoDevices(uint64_t aWindowId, dom::MediaSourceEnum aMediaSource, - nsTArray >* aSources) + nsTArray >* aDevices) { mMutex.AssertCurrentThreadOwns(); @@ -228,23 +228,29 @@ MediaEngineWebRTC::EnumerateVideoDevices(uint64_t aWindowId, vSource->RequiresSharing()) { // We've already seen this shared device, just refresh and append. static_cast(vSource.get())->Refresh(i); - aSources->AppendElement(vSource.get()); } else { vSource = new MediaEngineRemoteVideoSource(i, capEngine, aMediaSource, scaryKind || scarySource); devicesForThisWindow->Put(uuid, vSource); - aSources->AppendElement(vSource); } + aDevices->AppendElement(MakeRefPtr( + vSource, + vSource->GetName(), + NS_ConvertUTF8toUTF16(vSource->GetUUID()))); } if (mHasTabVideoSource || dom::MediaSourceEnum::Browser == aMediaSource) { - aSources->AppendElement(new MediaEngineTabVideoSource()); + RefPtr tabVideoSource = new MediaEngineTabVideoSource(); + aDevices->AppendElement(MakeRefPtr( + tabVideoSource, + tabVideoSource->GetName(), + NS_ConvertUTF8toUTF16(tabVideoSource->GetUUID()))); } } void MediaEngineWebRTC::EnumerateMicrophoneDevices(uint64_t aWindowId, - nsTArray >* aSources) + nsTArray >* aDevices) { mMutex.AssertCurrentThreadOwns(); @@ -284,43 +290,47 @@ MediaEngineWebRTC::EnumerateMicrophoneDevices(uint64_t aWindowId, } - RefPtr aSource; + RefPtr micSource; NS_ConvertUTF8toUTF16 uuid(uniqueId); nsRefPtrHashtable* devicesForThisWindow = mAudioSources.LookupOrAdd(aWindowId); - if (devicesForThisWindow->Get(uuid, getter_AddRefs(aSource)) && - aSource->RequiresSharing()) { - // We've already seen this device, just append. - aSources->AppendElement(aSource.get()); - } else { - aSource = new MediaEngineWebRTCMicrophoneSource( + bool alreadySeenThisDeviceBefore = devicesForThisWindow->Get(uuid, getter_AddRefs(micSource)) && + micSource->RequiresSharing(); + if (!alreadySeenThisDeviceBefore) { + micSource = new MediaEngineWebRTCMicrophoneSource( new mozilla::AudioInputCubeb(i), i, deviceName, uniqueId, mDelayAgnostic, mExtendedFilter); - devicesForThisWindow->Put(uuid, aSource); - aSources->AppendElement(aSource); + devicesForThisWindow->Put(uuid, micSource); } + aDevices->AppendElement(MakeRefPtr( + micSource, + micSource->GetName(), + NS_ConvertUTF8toUTF16(micSource->GetUUID()))); } } void MediaEngineWebRTC::EnumerateDevices(uint64_t aWindowId, dom::MediaSourceEnum aMediaSource, - nsTArray >* aSources) + nsTArray >* aDevices) { // We spawn threads to handle gUM runnables, so we must protect the member vars MutexAutoLock lock(mMutex); if (MediaEngineSource::IsVideo(aMediaSource)) { - EnumerateVideoDevices(aWindowId, aMediaSource, aSources); + EnumerateVideoDevices(aWindowId, aMediaSource, aDevices); } else if (aMediaSource == dom::MediaSourceEnum::AudioCapture) { RefPtr audioCaptureSource = new MediaEngineWebRTCAudioCaptureSource(nullptr); - aSources->AppendElement(audioCaptureSource); + aDevices->AppendElement(MakeRefPtr( + audioCaptureSource, + audioCaptureSource->GetName(), + NS_ConvertUTF8toUTF16(audioCaptureSource->GetUUID()))); } else { MOZ_ASSERT(aMediaSource == dom::MediaSourceEnum::Microphone); - EnumerateMicrophoneDevices(aWindowId, aSources); + EnumerateMicrophoneDevices(aWindowId, aDevices); } } diff --git a/dom/media/webrtc/MediaEngineWebRTC.h b/dom/media/webrtc/MediaEngineWebRTC.h index ba0bf774a115..6165d02008f1 100644 --- a/dom/media/webrtc/MediaEngineWebRTC.h +++ b/dom/media/webrtc/MediaEngineWebRTC.h @@ -635,15 +635,15 @@ public: void EnumerateDevices(uint64_t aWindowId, dom::MediaSourceEnum, - nsTArray>*) override; + nsTArray>*) override; void ReleaseResourcesForWindow(uint64_t aWindowId) override; private: ~MediaEngineWebRTC() = default; void EnumerateVideoDevices(uint64_t aWindowId, dom::MediaSourceEnum, - nsTArray>*); + nsTArray>*); void EnumerateMicrophoneDevices(uint64_t aWindowId, - nsTArray>*); + nsTArray>*); nsCOMPtr mThread;