From 784704534af63bb604c87d4aa367813ce8ef7c54 Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Tue, 16 Apr 2019 15:42:42 +0000 Subject: [PATCH] Bug 1531833 - When the input is voice, activate the global communication mode. r=pehrsons Differential Revision: https://phabricator.services.mozilla.com/D21738 --HG-- extra : moz-landing-system : lando --- dom/media/CubebUtils.cpp | 22 +++++++++++++++++++++- dom/media/CubebUtils.h | 2 ++ dom/media/GraphDriver.cpp | 13 +++++++++++++ dom/media/webrtc/MediaEngineWebRTC.cpp | 5 +++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/dom/media/CubebUtils.cpp b/dom/media/CubebUtils.cpp index 885ed380fb58..2b46eedbc535 100644 --- a/dom/media/CubebUtils.cpp +++ b/dom/media/CubebUtils.cpp @@ -147,6 +147,14 @@ size_t sAudioIPCStackSize; StaticAutoPtr sBrandName; StaticAutoPtr sCubebBackendName; StaticAutoPtr sCubebOutputDeviceName; +#ifdef MOZ_WIDGET_ANDROID +// Counts the number of time a request for switching to global "communication +// mode" has been received. If this is > 0, global communication mode is to be +// enabled. If it is 0, the global communication mode is to be disabled. +// This allows to correctly track the global behaviour to adopt accross +// asynchronous GraphDriver changes, on Android. +int sInCommunicationCount = 0; +#endif const char kBrandBundleURL[] = "chrome://branding/locale/brand.properties"; @@ -311,7 +319,19 @@ void ForceSetCubebContext(cubeb* aCubebContext) { void SetInCommunication(bool aInCommunication) { #ifdef MOZ_WIDGET_ANDROID - java::GeckoAppShell::SetCommunicationAudioModeOn(aInCommunication); + StaticMutexAutoLock lock(sMutex); + if (aInCommunication) { + sInCommunicationCount++; + } else { + MOZ_ASSERT(sInCommunicationCount > 0); + sInCommunicationCount--; + } + + if (sInCommunicationCount == 1) { + java::GeckoAppShell::SetCommunicationAudioModeOn(true); + } else if (sInCommunicationCount == 0) { + java::GeckoAppShell::SetCommunicationAudioModeOn(false); + } #endif } diff --git a/dom/media/CubebUtils.h b/dom/media/CubebUtils.h index 976122c2362b..8e9dcc00c927 100644 --- a/dom/media/CubebUtils.h +++ b/dom/media/CubebUtils.h @@ -13,6 +13,8 @@ class AudioDeviceInfo; +MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(cubeb_stream_prefs) + namespace mozilla { namespace CubebUtils { diff --git a/dom/media/GraphDriver.cpp b/dom/media/GraphDriver.cpp index 3bcbf66335fe..c13049d66163 100644 --- a/dom/media/GraphDriver.cpp +++ b/dom/media/GraphDriver.cpp @@ -495,6 +495,13 @@ AudioCallbackDriver::AudioCallbackDriver(MediaStreamGraphImpl* aGraphImpl, audio::AudioNotificationReceiver::Register(this); } #endif + if (aAudioInputType == AudioInputType::Voice) { + LOG(LogLevel::Debug, ("VOICE.")); + mInputDevicePreference = CUBEB_DEVICE_PREF_VOICE; + CubebUtils::SetInCommunication(true); + } else { + mInputDevicePreference = CUBEB_DEVICE_PREF_ALL; + } } AudioCallbackDriver::~AudioCallbackDriver() { @@ -505,6 +512,9 @@ AudioCallbackDriver::~AudioCallbackDriver() { audio::AudioNotificationReceiver::Unregister(this); } #endif + if (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE) { + CubebUtils::SetInCommunication(false); + } } bool IsMacbookOrMacbookAir() { @@ -590,6 +600,9 @@ bool AudioCallbackDriver::Init() { output.channels = mOutputChannels; output.layout = CUBEB_LAYOUT_UNDEFINED; output.prefs = CubebUtils::GetDefaultStreamPrefs(); + if (mInputDevicePreference == CUBEB_DEVICE_PREF_VOICE) { + output.prefs |= static_cast(CUBEB_STREAM_PREF_VOICE); + } uint32_t latency_frames = CubebUtils::GetCubebMSGLatencyInFrames(&output); diff --git a/dom/media/webrtc/MediaEngineWebRTC.cpp b/dom/media/webrtc/MediaEngineWebRTC.cpp index 824057f69ff2..ab8bd85f250f 100644 --- a/dom/media/webrtc/MediaEngineWebRTC.cpp +++ b/dom/media/webrtc/MediaEngineWebRTC.cpp @@ -180,9 +180,14 @@ void MediaEngineWebRTC::EnumerateMicrophoneDevices( if (!foundPreferredDevice) { foundPreferredDevice = true; } else { + // This is possible on windows, there is a default communication + // device, and a default device: + // See https://bugzilla.mozilla.org/show_bug.cgi?id=1542739 +#ifndef XP_WIN MOZ_ASSERT(!foundPreferredDevice, "Found more than one preferred audio input device" "while enumerating"); +#endif } #endif aDevices->InsertElementAt(0, device);