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
This commit is contained in:
Alex Chronopoulos 2018-07-11 08:59:13 +02:00
Родитель 96fb94d4ab
Коммит 5d13851360
6 изменённых файлов: 69 добавлений и 59 удалений

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

@ -1494,19 +1494,19 @@ private:
// Source getter returning full list
static void
GetSources(MediaEngine *aEngine,
uint64_t aWindowId,
MediaSourceEnum aSrcType,
nsTArray<RefPtr<MediaDevice>>& aResult,
const char* aMediaDeviceName = nullptr)
GetMediaDevices(MediaEngine *aEngine,
uint64_t aWindowId,
MediaSourceEnum aSrcType,
nsTArray<RefPtr<MediaDevice>>& aResult,
const char* aMediaDeviceName = nullptr)
{
MOZ_ASSERT(MediaManager::IsInMediaThread());
LOG(("%s: aEngine=%p, aWindowId=%" PRIu64 ", aSrcType=%" PRIu8 ", aMediaDeviceName=%s",
__func__, aEngine, aWindowId, static_cast<uint8_t>(aSrcType),
aMediaDeviceName ? aMediaDeviceName : "null"));
nsTArray<RefPtr<MediaEngineSource>> sources;
aEngine->EnumerateDevices(aWindowId, aSrcType, &sources);
nsTArray<RefPtr<MediaDevice>> 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<MediaDevice>(
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<MediaDevice>(
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();

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

@ -51,7 +51,7 @@ public:
*/
virtual void EnumerateDevices(uint64_t aWindowId,
dom::MediaSourceEnum,
nsTArray<RefPtr<MediaEngineSource>>*) = 0;
nsTArray<RefPtr<MediaDevice>>*) = 0;
virtual void ReleaseResourcesForWindow(uint64_t aWindowId) = 0;
virtual void Shutdown() = 0;

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

@ -559,7 +559,7 @@ MediaEngineDefaultAudioSource::Pull(const RefPtr<const AllocationHandle>& aHandl
void
MediaEngineDefault::EnumerateDevices(uint64_t aWindowId,
dom::MediaSourceEnum aMediaSource,
nsTArray<RefPtr<MediaEngineSource>>* aSources)
nsTArray<RefPtr<MediaDevice>>* aDevices)
{
AssertIsOnOwningThread();
@ -575,7 +575,10 @@ MediaEngineDefault::EnumerateDevices(uint64_t aWindowId,
devicesForThisWindow = mVSources.LookupOrAdd(aWindowId);
auto newSource = MakeRefPtr<MediaEngineDefaultVideoSource>();
devicesForThisWindow->AppendElement(newSource);
aSources->AppendElement(newSource);
aDevices->AppendElement(MakeRefPtr<MediaDevice>(
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<MediaEngineDefaultAudioSource>& source : *devicesForThisWindow) {
if (source->IsAvailable()) {
aSources->AppendElement(source);
aDevices->AppendElement(MakeRefPtr<MediaDevice>(
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<MediaEngineDefaultAudioSource>();
devicesForThisWindow->AppendElement(newSource);
aSources->AppendElement(newSource);
aDevices->AppendElement(MakeRefPtr<MediaDevice>(
newSource,
newSource->GetName(),
NS_ConvertUTF8toUTF16(newSource->GetUUID())));
}
return;
}

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

@ -189,7 +189,7 @@ public:
void EnumerateDevices(uint64_t aWindowId,
dom::MediaSourceEnum,
nsTArray<RefPtr<MediaEngineSource>>*) override;
nsTArray<RefPtr<MediaDevice>>*) override;
void Shutdown() override;
void ReleaseResourcesForWindow(uint64_t aWindowId) override;

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

@ -125,7 +125,7 @@ MediaEngineWebRTC::SetFakeDeviceChangeEvents()
void
MediaEngineWebRTC::EnumerateVideoDevices(uint64_t aWindowId,
dom::MediaSourceEnum aMediaSource,
nsTArray<RefPtr<MediaEngineSource> >* aSources)
nsTArray<RefPtr<MediaDevice> >* 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<MediaEngineRemoteVideoSource*>(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<MediaDevice>(
vSource,
vSource->GetName(),
NS_ConvertUTF8toUTF16(vSource->GetUUID())));
}
if (mHasTabVideoSource || dom::MediaSourceEnum::Browser == aMediaSource) {
aSources->AppendElement(new MediaEngineTabVideoSource());
RefPtr<MediaEngineSource> tabVideoSource = new MediaEngineTabVideoSource();
aDevices->AppendElement(MakeRefPtr<MediaDevice>(
tabVideoSource,
tabVideoSource->GetName(),
NS_ConvertUTF8toUTF16(tabVideoSource->GetUUID())));
}
}
void
MediaEngineWebRTC::EnumerateMicrophoneDevices(uint64_t aWindowId,
nsTArray<RefPtr<MediaEngineSource> >* aSources)
nsTArray<RefPtr<MediaDevice> >* aDevices)
{
mMutex.AssertCurrentThreadOwns();
@ -284,43 +290,47 @@ MediaEngineWebRTC::EnumerateMicrophoneDevices(uint64_t aWindowId,
}
RefPtr<MediaEngineSource> aSource;
RefPtr<MediaEngineSource> micSource;
NS_ConvertUTF8toUTF16 uuid(uniqueId);
nsRefPtrHashtable<nsStringHashKey, MediaEngineSource>*
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<MediaDevice>(
micSource,
micSource->GetName(),
NS_ConvertUTF8toUTF16(micSource->GetUUID())));
}
}
void
MediaEngineWebRTC::EnumerateDevices(uint64_t aWindowId,
dom::MediaSourceEnum aMediaSource,
nsTArray<RefPtr<MediaEngineSource> >* aSources)
nsTArray<RefPtr<MediaDevice> >* 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<MediaEngineWebRTCAudioCaptureSource> audioCaptureSource =
new MediaEngineWebRTCAudioCaptureSource(nullptr);
aSources->AppendElement(audioCaptureSource);
aDevices->AppendElement(MakeRefPtr<MediaDevice>(
audioCaptureSource,
audioCaptureSource->GetName(),
NS_ConvertUTF8toUTF16(audioCaptureSource->GetUUID())));
} else {
MOZ_ASSERT(aMediaSource == dom::MediaSourceEnum::Microphone);
EnumerateMicrophoneDevices(aWindowId, aSources);
EnumerateMicrophoneDevices(aWindowId, aDevices);
}
}

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

@ -635,15 +635,15 @@ public:
void EnumerateDevices(uint64_t aWindowId,
dom::MediaSourceEnum,
nsTArray<RefPtr<MediaEngineSource>>*) override;
nsTArray<RefPtr<MediaDevice>>*) override;
void ReleaseResourcesForWindow(uint64_t aWindowId) override;
private:
~MediaEngineWebRTC() = default;
void EnumerateVideoDevices(uint64_t aWindowId,
dom::MediaSourceEnum,
nsTArray<RefPtr<MediaEngineSource>>*);
nsTArray<RefPtr<MediaDevice>>*);
void EnumerateMicrophoneDevices(uint64_t aWindowId,
nsTArray<RefPtr<MediaEngineSource>>*);
nsTArray<RefPtr<MediaDevice>>*);
nsCOMPtr<nsIThread> mThread;