2012-07-20 02:10:44 -07:00
|
|
|
/* Copyright 2012 Mozilla Foundation and Mozilla contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2011-12-12 10:22:26 -08:00
|
|
|
|
2012-11-02 02:36:46 -07:00
|
|
|
#include <android/log.h>
|
2013-11-26 23:46:27 -08:00
|
|
|
#include <cutils/properties.h>
|
2014-11-13 21:12:00 -08:00
|
|
|
#include <binder/IServiceManager.h>
|
2012-04-22 11:14:36 -07:00
|
|
|
|
2013-06-08 08:26:28 -07:00
|
|
|
#include "AudioChannelService.h"
|
2011-12-12 10:22:26 -08:00
|
|
|
#include "AudioManager.h"
|
2013-06-08 08:26:28 -07:00
|
|
|
|
2012-09-06 13:51:15 -07:00
|
|
|
#include "nsIObserverService.h"
|
2013-12-03 22:57:40 -08:00
|
|
|
#ifdef MOZ_B2G_RIL
|
2013-11-26 23:46:27 -08:00
|
|
|
#include "nsIRadioInterfaceLayer.h"
|
2013-12-03 22:57:40 -08:00
|
|
|
#endif
|
2013-08-22 20:29:11 -07:00
|
|
|
#include "nsISettingsService.h"
|
2013-06-08 08:26:28 -07:00
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
|
|
|
|
#include "mozilla/Hal.h"
|
2012-09-06 13:51:15 -07:00
|
|
|
#include "mozilla/Services.h"
|
2014-05-27 23:19:22 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2014-08-14 18:47:15 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-07-16 22:08:11 -07:00
|
|
|
#include "base/message_loop.h"
|
2013-06-08 08:26:28 -07:00
|
|
|
|
|
|
|
#include "BluetoothCommon.h"
|
2014-01-12 21:58:43 -08:00
|
|
|
#include "BluetoothHfpManagerBase.h"
|
2011-12-12 10:22:26 -08:00
|
|
|
|
2013-07-17 22:22:36 -07:00
|
|
|
#include "nsJSUtils.h"
|
2013-08-27 21:14:57 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2013-09-10 13:56:05 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2014-05-27 23:19:22 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2014-09-16 10:15:16 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/SettingChangeNotificationBinding.h"
|
2013-07-17 22:22:36 -07:00
|
|
|
|
2012-03-14 15:43:26 -07:00
|
|
|
using namespace mozilla::dom::gonk;
|
2011-12-12 10:22:26 -08:00
|
|
|
using namespace android;
|
2012-04-22 11:14:36 -07:00
|
|
|
using namespace mozilla::hal;
|
|
|
|
using namespace mozilla;
|
2013-06-08 08:26:28 -07:00
|
|
|
using namespace mozilla::dom::bluetooth;
|
2012-04-22 11:14:36 -07:00
|
|
|
|
2015-01-08 13:52:19 -08:00
|
|
|
#undef LOG
|
2012-11-02 02:36:46 -07:00
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "AudioManager" , ## args)
|
2011-12-12 10:22:26 -08:00
|
|
|
|
2014-05-29 00:20:42 -07:00
|
|
|
#define HEADPHONES_STATUS_HEADSET MOZ_UTF16("headset")
|
|
|
|
#define HEADPHONES_STATUS_HEADPHONE MOZ_UTF16("headphone")
|
|
|
|
#define HEADPHONES_STATUS_OFF MOZ_UTF16("off")
|
|
|
|
#define HEADPHONES_STATUS_UNKNOWN MOZ_UTF16("unknown")
|
|
|
|
#define HEADPHONES_STATUS_CHANGED "headphones-status-changed"
|
|
|
|
#define MOZ_SETTINGS_CHANGE_ID "mozsettings-changed"
|
|
|
|
#define AUDIO_CHANNEL_PROCESS_CHANGED "audio-channel-process-changed"
|
2014-11-13 21:12:00 -08:00
|
|
|
#define AUDIO_POLICY_SERVICE_NAME "media.audio_policy"
|
2012-09-06 13:51:15 -07:00
|
|
|
|
2013-05-15 21:00:02 -07:00
|
|
|
static void BinderDeadCallback(status_t aErr);
|
|
|
|
static void InternalSetAudioRoutes(SwitchState aState);
|
2012-11-02 02:36:46 -07:00
|
|
|
// Refer AudioService.java from Android
|
|
|
|
static int sMaxStreamVolumeTbl[AUDIO_STREAM_CNT] = {
|
2012-12-04 02:21:43 -08:00
|
|
|
5, // voice call
|
2012-11-28 18:14:11 -08:00
|
|
|
15, // system
|
2012-12-04 02:21:43 -08:00
|
|
|
15, // ring
|
2012-11-02 02:36:46 -07:00
|
|
|
15, // music
|
2012-12-04 02:21:43 -08:00
|
|
|
15, // alarm
|
|
|
|
15, // notification
|
2012-11-02 02:36:46 -07:00
|
|
|
15, // BT SCO
|
2012-12-04 02:21:43 -08:00
|
|
|
15, // enforced audible
|
2012-11-02 02:36:46 -07:00
|
|
|
15, // DTMF
|
|
|
|
15, // TTS
|
2012-11-28 18:14:11 -08:00
|
|
|
15, // FM
|
2012-11-02 02:36:46 -07:00
|
|
|
};
|
2012-09-25 04:03:50 -07:00
|
|
|
// A bitwise variable for recording what kind of headset is attached.
|
2013-05-23 03:28:20 -07:00
|
|
|
static int sHeadsetState;
|
2015-02-25 00:20:00 -08:00
|
|
|
#if defined(MOZ_B2G_BT) || ANDROID_VERSION >= 17
|
2014-10-07 19:49:59 -07:00
|
|
|
static bool sBluetoothA2dpEnabled;
|
2015-02-25 00:20:00 -08:00
|
|
|
#endif
|
2013-06-08 08:26:28 -07:00
|
|
|
static const int kBtSampleRate = 8000;
|
2013-07-16 22:08:11 -07:00
|
|
|
static bool sSwitchDone = true;
|
2015-02-25 00:20:00 -08:00
|
|
|
#ifdef MOZ_B2G_BT
|
2014-07-25 00:19:26 -07:00
|
|
|
static bool sA2dpSwitchDone = true;
|
2015-02-25 00:20:00 -08:00
|
|
|
#endif
|
2012-09-25 04:03:50 -07:00
|
|
|
|
2013-07-17 22:22:36 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace gonk {
|
2013-05-15 21:00:02 -07:00
|
|
|
class RecoverTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RecoverTask() {}
|
|
|
|
NS_IMETHODIMP Run() {
|
2013-07-17 22:22:36 -07:00
|
|
|
nsCOMPtr<nsIAudioManager> amService = do_GetService(NS_AUDIOMANAGER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(amService, NS_OK);
|
|
|
|
AudioManager *am = static_cast<AudioManager *>(amService.get());
|
2014-11-13 21:12:00 -08:00
|
|
|
|
|
|
|
int attempt;
|
|
|
|
for (attempt = 0; attempt < 50; attempt++) {
|
|
|
|
if (defaultServiceManager()->checkService(String16(AUDIO_POLICY_SERVICE_NAME)) != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("AudioPolicyService is dead! attempt=%d", attempt);
|
|
|
|
usleep(1000 * 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(attempt < 50);
|
|
|
|
|
2013-07-17 22:22:36 -07:00
|
|
|
for (int loop = 0; loop < AUDIO_STREAM_CNT; loop++) {
|
|
|
|
AudioSystem::initStreamVolume(static_cast<audio_stream_type_t>(loop), 0,
|
|
|
|
sMaxStreamVolumeTbl[loop]);
|
|
|
|
int32_t index;
|
|
|
|
am->GetStreamVolumeIndex(loop, &index);
|
|
|
|
am->SetStreamVolumeIndex(loop, index);
|
2013-05-15 21:00:02 -07:00
|
|
|
}
|
2013-07-17 22:22:36 -07:00
|
|
|
|
2013-05-15 21:00:02 -07:00
|
|
|
if (sHeadsetState & AUDIO_DEVICE_OUT_WIRED_HEADSET)
|
|
|
|
InternalSetAudioRoutes(SWITCH_STATE_HEADSET);
|
|
|
|
else if (sHeadsetState & AUDIO_DEVICE_OUT_WIRED_HEADPHONE)
|
|
|
|
InternalSetAudioRoutes(SWITCH_STATE_HEADPHONE);
|
|
|
|
else
|
|
|
|
InternalSetAudioRoutes(SWITCH_STATE_OFF);
|
|
|
|
|
|
|
|
int32_t phoneState = nsIAudioManager::PHONE_STATE_INVALID;
|
|
|
|
am->GetPhoneState(&phoneState);
|
2013-05-20 00:34:42 -07:00
|
|
|
#if ANDROID_VERSION < 17
|
2013-05-15 21:00:02 -07:00
|
|
|
AudioSystem::setPhoneState(phoneState);
|
2013-05-20 00:34:42 -07:00
|
|
|
#else
|
|
|
|
AudioSystem::setPhoneState(static_cast<audio_mode_t>(phoneState));
|
|
|
|
#endif
|
2013-05-15 21:00:02 -07:00
|
|
|
|
|
|
|
AudioSystem::get_audio_flinger();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
2013-08-22 20:29:11 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class AudioChannelVolInitCallback final : public nsISettingsServiceCallback
|
2013-08-22 20:29:11 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
AudioChannelVolInitCallback() {}
|
|
|
|
|
2014-01-09 09:39:36 -08:00
|
|
|
NS_IMETHOD Handle(const nsAString& aName, JS::Handle<JS::Value> aResult)
|
2013-08-22 20:29:11 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAudioManager> audioManager =
|
|
|
|
do_GetService(NS_AUDIOMANAGER_CONTRACTID);
|
2014-04-27 19:47:02 -07:00
|
|
|
NS_ENSURE_TRUE(aResult.isInt32(), NS_OK);
|
2013-08-22 20:29:11 -07:00
|
|
|
|
2014-04-27 19:55:08 -07:00
|
|
|
int32_t volIndex = aResult.toInt32();
|
2013-08-22 20:29:11 -07:00
|
|
|
if (aName.EqualsLiteral("audio.volume.content")) {
|
2014-04-10 10:39:20 -07:00
|
|
|
audioManager->SetAudioChannelVolume((int32_t)AudioChannel::Content,
|
|
|
|
volIndex);
|
2013-08-22 20:29:11 -07:00
|
|
|
} else if (aName.EqualsLiteral("audio.volume.notification")) {
|
2014-04-10 10:39:20 -07:00
|
|
|
audioManager->SetAudioChannelVolume((int32_t)AudioChannel::Notification,
|
2013-08-22 20:29:11 -07:00
|
|
|
volIndex);
|
|
|
|
} else if (aName.EqualsLiteral("audio.volume.alarm")) {
|
2014-04-10 10:39:20 -07:00
|
|
|
audioManager->SetAudioChannelVolume((int32_t)AudioChannel::Alarm,
|
|
|
|
volIndex);
|
2013-08-22 20:29:11 -07:00
|
|
|
} else if (aName.EqualsLiteral("audio.volume.telephony")) {
|
2014-04-10 10:39:20 -07:00
|
|
|
audioManager->SetAudioChannelVolume((int32_t)AudioChannel::Telephony,
|
|
|
|
volIndex);
|
2013-08-22 20:29:11 -07:00
|
|
|
} else if (aName.EqualsLiteral("audio.volume.bt_sco")) {
|
|
|
|
static_cast<AudioManager *>(audioManager.get())->SetStreamVolumeIndex(
|
|
|
|
AUDIO_STREAM_BLUETOOTH_SCO, volIndex);
|
|
|
|
} else {
|
2014-04-20 00:36:40 -07:00
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected audio channel for initializing "
|
2013-10-14 03:45:35 -07:00
|
|
|
"volume control");
|
2013-08-22 20:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD HandleError(const nsAString& aName)
|
|
|
|
{
|
|
|
|
LOG("AudioChannelVolInitCallback::HandleError: %s\n",
|
|
|
|
NS_ConvertUTF16toUTF8(aName).get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-10 05:44:02 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
~AudioChannelVolInitCallback() {}
|
2013-08-22 20:29:11 -07:00
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(AudioChannelVolInitCallback, nsISettingsServiceCallback)
|
2013-07-17 22:22:36 -07:00
|
|
|
} /* namespace gonk */
|
|
|
|
} /* namespace dom */
|
|
|
|
} /* namespace mozilla */
|
2013-05-15 21:00:02 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
BinderDeadCallback(status_t aErr)
|
|
|
|
{
|
|
|
|
if (aErr == DEAD_OBJECT) {
|
|
|
|
NS_DispatchToMainThread(new RecoverTask());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 10:51:35 -07:00
|
|
|
static bool
|
2012-11-14 19:00:34 -08:00
|
|
|
IsDeviceOn(audio_devices_t device)
|
2012-09-19 10:51:35 -07:00
|
|
|
{
|
|
|
|
if (static_cast<
|
|
|
|
audio_policy_dev_state_t (*) (audio_devices_t, const char *)
|
2012-11-14 19:00:34 -08:00
|
|
|
>(AudioSystem::getDeviceConnectionState))
|
|
|
|
return AudioSystem::getDeviceConnectionState(device, "") ==
|
|
|
|
AUDIO_POLICY_DEVICE_STATE_AVAILABLE;
|
|
|
|
|
|
|
|
return false;
|
2012-09-19 10:51:35 -07:00
|
|
|
}
|
|
|
|
|
2013-07-16 22:08:11 -07:00
|
|
|
static void ProcessDelayedAudioRoute(SwitchState aState)
|
|
|
|
{
|
|
|
|
if (sSwitchDone)
|
|
|
|
return;
|
|
|
|
InternalSetAudioRoutes(aState);
|
|
|
|
sSwitchDone = true;
|
|
|
|
}
|
|
|
|
|
2015-02-25 00:20:00 -08:00
|
|
|
#ifdef MOZ_B2G_BT
|
2014-10-02 02:22:44 -07:00
|
|
|
static void ProcessDelayedA2dpRoute(audio_policy_dev_state_t aState, const nsCString aAddress)
|
2014-07-25 00:19:26 -07:00
|
|
|
{
|
|
|
|
if (sA2dpSwitchDone)
|
|
|
|
return;
|
|
|
|
AudioSystem::setDeviceConnectionState(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,
|
2014-10-02 02:22:44 -07:00
|
|
|
aState, aAddress.get());
|
2014-07-25 00:19:26 -07:00
|
|
|
String8 cmd("bluetooth_enabled=false");
|
|
|
|
AudioSystem::setParameters(0, cmd);
|
|
|
|
cmd.setTo("A2dpSuspended=true");
|
|
|
|
AudioSystem::setParameters(0, cmd);
|
|
|
|
sA2dpSwitchDone = true;
|
|
|
|
}
|
2015-02-25 00:20:00 -08:00
|
|
|
#endif
|
2014-07-25 00:19:26 -07:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(AudioManager, nsIAudioManager, nsIObserver)
|
2011-12-12 10:22:26 -08:00
|
|
|
|
2012-04-22 11:14:36 -07:00
|
|
|
static void
|
2012-09-25 04:03:50 -07:00
|
|
|
InternalSetAudioRoutesICS(SwitchState aState)
|
|
|
|
{
|
|
|
|
if (aState == SWITCH_STATE_HEADSET) {
|
|
|
|
AudioSystem::setDeviceConnectionState(AUDIO_DEVICE_OUT_WIRED_HEADSET,
|
|
|
|
AUDIO_POLICY_DEVICE_STATE_AVAILABLE, "");
|
|
|
|
sHeadsetState |= AUDIO_DEVICE_OUT_WIRED_HEADSET;
|
|
|
|
} else if (aState == SWITCH_STATE_HEADPHONE) {
|
|
|
|
AudioSystem::setDeviceConnectionState(AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
|
|
|
|
AUDIO_POLICY_DEVICE_STATE_AVAILABLE, "");
|
|
|
|
sHeadsetState |= AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
|
|
|
|
} else if (aState == SWITCH_STATE_OFF) {
|
2014-11-12 19:52:17 -08:00
|
|
|
if (sHeadsetState & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
|
|
|
|
AudioSystem::setDeviceConnectionState(AUDIO_DEVICE_OUT_WIRED_HEADSET,
|
|
|
|
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, "");
|
|
|
|
}
|
|
|
|
if (sHeadsetState & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
|
|
|
|
AudioSystem::setDeviceConnectionState(AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
|
|
|
|
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, "");
|
|
|
|
}
|
2012-09-25 04:03:50 -07:00
|
|
|
sHeadsetState = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InternalSetAudioRoutes(SwitchState aState)
|
|
|
|
{
|
|
|
|
if (static_cast<
|
|
|
|
status_t (*)(audio_devices_t, audio_policy_dev_state_t, const char*)
|
|
|
|
>(AudioSystem::setDeviceConnectionState)) {
|
|
|
|
InternalSetAudioRoutesICS(aState);
|
2013-07-17 22:23:10 -07:00
|
|
|
} else {
|
|
|
|
NS_NOTREACHED("Doesn't support audio routing on GB version");
|
2012-04-22 11:14:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 23:58:58 -07:00
|
|
|
void
|
|
|
|
AudioManager::HandleBluetoothStatusChanged(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const nsCString aAddress)
|
|
|
|
{
|
2013-07-30 19:44:48 -07:00
|
|
|
#ifdef MOZ_B2G_BT
|
2013-07-29 23:58:58 -07:00
|
|
|
bool status;
|
|
|
|
if (!strcmp(aTopic, BLUETOOTH_SCO_STATUS_CHANGED_ID)) {
|
2014-01-12 21:58:43 -08:00
|
|
|
BluetoothHfpManagerBase* hfp =
|
|
|
|
static_cast<BluetoothHfpManagerBase*>(aSubject);
|
2013-07-29 23:58:58 -07:00
|
|
|
status = hfp->IsScoConnected();
|
|
|
|
} else {
|
|
|
|
BluetoothProfileManagerBase* profile =
|
|
|
|
static_cast<BluetoothProfileManagerBase*>(aSubject);
|
|
|
|
status = profile->IsConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
audio_policy_dev_state_t audioState = status ?
|
|
|
|
AUDIO_POLICY_DEVICE_STATE_AVAILABLE :
|
|
|
|
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, BLUETOOTH_SCO_STATUS_CHANGED_ID)) {
|
|
|
|
if (audioState == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) {
|
|
|
|
String8 cmd;
|
|
|
|
cmd.appendFormat("bt_samplerate=%d", kBtSampleRate);
|
|
|
|
AudioSystem::setParameters(0, cmd);
|
|
|
|
SetForceForUse(nsIAudioManager::USE_COMMUNICATION, nsIAudioManager::FORCE_BT_SCO);
|
|
|
|
} else {
|
|
|
|
int32_t force;
|
|
|
|
GetForceForUse(nsIAudioManager::USE_COMMUNICATION, &force);
|
|
|
|
if (force == nsIAudioManager::FORCE_BT_SCO)
|
|
|
|
SetForceForUse(nsIAudioManager::USE_COMMUNICATION, nsIAudioManager::FORCE_NONE);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(aTopic, BLUETOOTH_A2DP_STATUS_CHANGED_ID)) {
|
2014-07-25 00:19:26 -07:00
|
|
|
if (audioState == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE && sA2dpSwitchDone) {
|
|
|
|
MessageLoop::current()->PostDelayedTask(
|
2014-10-02 02:22:44 -07:00
|
|
|
FROM_HERE, NewRunnableFunction(&ProcessDelayedA2dpRoute, audioState, aAddress), 1000);
|
2014-07-25 00:19:26 -07:00
|
|
|
sA2dpSwitchDone = false;
|
|
|
|
} else {
|
|
|
|
AudioSystem::setDeviceConnectionState(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,
|
|
|
|
audioState, aAddress.get());
|
2013-07-29 23:58:58 -07:00
|
|
|
String8 cmd("bluetooth_enabled=true");
|
|
|
|
AudioSystem::setParameters(0, cmd);
|
|
|
|
cmd.setTo("A2dpSuspended=false");
|
|
|
|
AudioSystem::setParameters(0, cmd);
|
2014-07-25 00:19:26 -07:00
|
|
|
sA2dpSwitchDone = true;
|
2014-10-07 19:49:59 -07:00
|
|
|
#if ANDROID_VERSION >= 17
|
|
|
|
if (AudioSystem::getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_NO_BT_A2DP) {
|
|
|
|
SetForceForUse(AUDIO_POLICY_FORCE_FOR_MEDIA, AUDIO_POLICY_FORCE_NONE);
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-29 23:58:58 -07:00
|
|
|
}
|
2014-10-07 19:49:59 -07:00
|
|
|
sBluetoothA2dpEnabled = audioState == AUDIO_POLICY_DEVICE_STATE_AVAILABLE;
|
2013-07-29 23:58:58 -07:00
|
|
|
} else if (!strcmp(aTopic, BLUETOOTH_HFP_STATUS_CHANGED_ID)) {
|
|
|
|
AudioSystem::setDeviceConnectionState(AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
|
|
|
|
audioState, aAddress.get());
|
|
|
|
AudioSystem::setDeviceConnectionState(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,
|
|
|
|
audioState, aAddress.get());
|
|
|
|
}
|
2013-07-30 19:44:48 -07:00
|
|
|
#endif
|
2013-07-29 23:58:58 -07:00
|
|
|
}
|
|
|
|
|
2014-05-29 00:20:42 -07:00
|
|
|
void
|
|
|
|
AudioManager::HandleAudioChannelProcessChanged()
|
|
|
|
{
|
|
|
|
// Note: If the user answers a VoIP call (e.g. WebRTC calls) during the
|
|
|
|
// telephony call (GSM/CDMA calls) the audio manager won't set the
|
|
|
|
// PHONE_STATE_IN_COMMUNICATION audio state. Once the telephony call finishes
|
|
|
|
// the RIL plumbing sets the PHONE_STATE_NORMAL audio state. This seems to be
|
|
|
|
// an issue for the VoIP call but it is not. Once the RIL plumbing sets the
|
|
|
|
// the PHONE_STATE_NORMAL audio state the AudioManager::mPhoneAudioAgent
|
|
|
|
// member will call the StopPlaying() method causing that this function will
|
|
|
|
// be called again and therefore the audio manager sets the
|
|
|
|
// PHONE_STATE_IN_COMMUNICATION audio state.
|
|
|
|
|
|
|
|
if ((mPhoneState == PHONE_STATE_IN_CALL) ||
|
|
|
|
(mPhoneState == PHONE_STATE_RINGTONE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:10:16 -07:00
|
|
|
AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService();
|
|
|
|
MOZ_ASSERT(service);
|
2014-05-29 00:20:42 -07:00
|
|
|
|
|
|
|
bool telephonyChannelIsActive = service->TelephonyChannelIsActive();
|
|
|
|
telephonyChannelIsActive ? SetPhoneState(PHONE_STATE_IN_COMMUNICATION) :
|
|
|
|
SetPhoneState(PHONE_STATE_NORMAL);
|
|
|
|
}
|
|
|
|
|
2012-09-29 02:39:05 -07:00
|
|
|
nsresult
|
|
|
|
AudioManager::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* aData)
|
2012-09-29 02:39:05 -07:00
|
|
|
{
|
2013-06-19 18:20:16 -07:00
|
|
|
if ((strcmp(aTopic, BLUETOOTH_SCO_STATUS_CHANGED_ID) == 0) ||
|
2013-07-29 23:58:58 -07:00
|
|
|
(strcmp(aTopic, BLUETOOTH_HFP_STATUS_CHANGED_ID) == 0) ||
|
2013-06-19 18:20:16 -07:00
|
|
|
(strcmp(aTopic, BLUETOOTH_A2DP_STATUS_CHANGED_ID) == 0)) {
|
2013-07-29 23:58:58 -07:00
|
|
|
nsCString address = NS_ConvertUTF16toUTF8(nsDependentString(aData));
|
|
|
|
if (address.IsEmpty()) {
|
|
|
|
NS_WARNING(nsPrintfCString("Invalid address of %s", aTopic).get());
|
2013-06-08 08:26:28 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-07-29 23:58:58 -07:00
|
|
|
HandleBluetoothStatusChanged(aSubject, aTopic, address);
|
|
|
|
return NS_OK;
|
2013-07-26 02:00:38 -07:00
|
|
|
}
|
2013-07-29 23:58:58 -07:00
|
|
|
|
2014-05-29 00:20:42 -07:00
|
|
|
else if (!strcmp(aTopic, AUDIO_CHANNEL_PROCESS_CHANGED)) {
|
|
|
|
HandleAudioChannelProcessChanged();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-17 22:22:36 -07:00
|
|
|
// To process the volume control on each audio channel according to
|
|
|
|
// change of settings
|
2013-08-22 20:29:11 -07:00
|
|
|
else if (!strcmp(aTopic, MOZ_SETTINGS_CHANGE_ID)) {
|
2014-12-29 02:56:00 -08:00
|
|
|
RootedDictionary<dom::SettingChangeNotification> setting(nsContentUtils::RootingCxForThread());
|
|
|
|
if (!WrappedJSToDictionary(aSubject, setting)) {
|
2013-07-17 22:22:36 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-09-16 10:15:16 -07:00
|
|
|
if (!setting.mKey.EqualsASCII("audio.volume.bt_sco")) {
|
2013-07-17 22:22:36 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-09-16 10:15:16 -07:00
|
|
|
if (!setting.mValue.isNumber()) {
|
2013-07-17 22:22:36 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-16 10:15:16 -07:00
|
|
|
int32_t index = setting.mValue.toNumber();
|
2013-07-17 22:22:36 -07:00
|
|
|
SetStreamVolumeIndex(AUDIO_STREAM_BLUETOOTH_SCO, index);
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-09-29 02:39:05 -07:00
|
|
|
}
|
2013-06-08 08:26:28 -07:00
|
|
|
|
2013-07-17 22:22:36 -07:00
|
|
|
NS_WARNING("Unexpected topic in AudioManager");
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-29 02:39:05 -07:00
|
|
|
}
|
|
|
|
|
2012-09-06 13:51:15 -07:00
|
|
|
static void
|
|
|
|
NotifyHeadphonesStatus(SwitchState aState)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
2012-11-26 19:13:19 -08:00
|
|
|
if (aState == SWITCH_STATE_HEADSET) {
|
|
|
|
obs->NotifyObservers(nullptr, HEADPHONES_STATUS_CHANGED, HEADPHONES_STATUS_HEADSET);
|
|
|
|
} else if (aState == SWITCH_STATE_HEADPHONE) {
|
|
|
|
obs->NotifyObservers(nullptr, HEADPHONES_STATUS_CHANGED, HEADPHONES_STATUS_HEADPHONE);
|
2012-09-06 13:51:15 -07:00
|
|
|
} else if (aState == SWITCH_STATE_OFF) {
|
|
|
|
obs->NotifyObservers(nullptr, HEADPHONES_STATUS_CHANGED, HEADPHONES_STATUS_OFF);
|
|
|
|
} else {
|
|
|
|
obs->NotifyObservers(nullptr, HEADPHONES_STATUS_CHANGED, HEADPHONES_STATUS_UNKNOWN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-22 11:14:36 -07:00
|
|
|
class HeadphoneSwitchObserver : public SwitchObserver
|
|
|
|
{
|
|
|
|
public:
|
2014-10-07 19:49:59 -07:00
|
|
|
HeadphoneSwitchObserver(AudioManager* aAudioManager)
|
|
|
|
: mAudioManager(aAudioManager) { }
|
2012-04-22 11:14:36 -07:00
|
|
|
void Notify(const SwitchEvent& aEvent) {
|
2012-09-06 13:51:15 -07:00
|
|
|
NotifyHeadphonesStatus(aEvent.status());
|
2013-07-16 22:08:11 -07:00
|
|
|
// When user pulled out the headset, a delay of routing here can avoid the leakage of audio from speaker.
|
|
|
|
if (aEvent.status() == SWITCH_STATE_OFF && sSwitchDone) {
|
|
|
|
MessageLoop::current()->PostDelayedTask(
|
|
|
|
FROM_HERE, NewRunnableFunction(&ProcessDelayedAudioRoute, SWITCH_STATE_OFF), 1000);
|
|
|
|
sSwitchDone = false;
|
|
|
|
} else if (aEvent.status() != SWITCH_STATE_OFF) {
|
|
|
|
InternalSetAudioRoutes(aEvent.status());
|
|
|
|
sSwitchDone = true;
|
|
|
|
}
|
2014-10-07 19:49:59 -07:00
|
|
|
// Handle the coexistence of a2dp / headset device, latest one wins.
|
|
|
|
#if ANDROID_VERSION >= 17
|
|
|
|
int32_t forceUse = 0;
|
|
|
|
mAudioManager->GetForceForUse(AUDIO_POLICY_FORCE_FOR_MEDIA, &forceUse);
|
|
|
|
if (aEvent.status() != SWITCH_STATE_OFF && sBluetoothA2dpEnabled) {
|
|
|
|
mAudioManager->SetForceForUse(AUDIO_POLICY_FORCE_FOR_MEDIA, AUDIO_POLICY_FORCE_NO_BT_A2DP);
|
|
|
|
} else if (forceUse == AUDIO_POLICY_FORCE_NO_BT_A2DP) {
|
|
|
|
mAudioManager->SetForceForUse(AUDIO_POLICY_FORCE_FOR_MEDIA, AUDIO_POLICY_FORCE_NONE);
|
|
|
|
}
|
|
|
|
#endif
|
2012-04-22 11:14:36 -07:00
|
|
|
}
|
2014-10-07 19:49:59 -07:00
|
|
|
private:
|
|
|
|
AudioManager* mAudioManager;
|
2012-04-22 11:14:36 -07:00
|
|
|
};
|
|
|
|
|
2013-12-03 22:57:40 -08:00
|
|
|
AudioManager::AudioManager()
|
|
|
|
: mPhoneState(PHONE_STATE_CURRENT)
|
2014-10-07 19:49:59 -07:00
|
|
|
, mObserver(new HeadphoneSwitchObserver(this))
|
2013-12-03 22:57:40 -08:00
|
|
|
#ifdef MOZ_B2G_RIL
|
|
|
|
, mMuteCallToRIL(false)
|
|
|
|
#endif
|
2012-04-22 11:14:36 -07:00
|
|
|
{
|
|
|
|
RegisterSwitchObserver(SWITCH_HEADPHONES, mObserver);
|
2012-09-25 04:03:50 -07:00
|
|
|
|
2012-04-22 11:14:36 -07:00
|
|
|
InternalSetAudioRoutes(GetCurrentSwitchState(SWITCH_HEADPHONES));
|
2012-09-27 20:06:01 -07:00
|
|
|
NotifyHeadphonesStatus(GetCurrentSwitchState(SWITCH_HEADPHONES));
|
2012-09-29 02:39:05 -07:00
|
|
|
|
2012-11-02 02:36:46 -07:00
|
|
|
for (int loop = 0; loop < AUDIO_STREAM_CNT; loop++) {
|
|
|
|
AudioSystem::initStreamVolume(static_cast<audio_stream_type_t>(loop), 0,
|
|
|
|
sMaxStreamVolumeTbl[loop]);
|
2013-05-15 21:00:02 -07:00
|
|
|
mCurrentStreamVolumeTbl[loop] = sMaxStreamVolumeTbl[loop];
|
2012-11-02 02:36:46 -07:00
|
|
|
}
|
2013-01-08 19:09:37 -08:00
|
|
|
// Force publicnotification to output at maximal volume
|
2013-07-17 22:22:36 -07:00
|
|
|
SetStreamVolumeIndex(AUDIO_STREAM_ENFORCED_AUDIBLE,
|
|
|
|
sMaxStreamVolumeTbl[AUDIO_STREAM_ENFORCED_AUDIBLE]);
|
2013-08-22 20:29:11 -07:00
|
|
|
|
|
|
|
// Get the initial volume index from settings DB during boot up.
|
|
|
|
nsCOMPtr<nsISettingsService> settingsService =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
NS_ENSURE_TRUE_VOID(settingsService);
|
|
|
|
nsCOMPtr<nsISettingsServiceLock> lock;
|
2014-02-07 03:19:58 -08:00
|
|
|
nsresult rv = settingsService->CreateLock(nullptr, getter_AddRefs(lock));
|
2013-08-22 20:29:11 -07:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
nsCOMPtr<nsISettingsServiceCallback> callback = new AudioChannelVolInitCallback();
|
|
|
|
NS_ENSURE_TRUE_VOID(callback);
|
|
|
|
lock->Get("audio.volume.content", callback);
|
|
|
|
lock->Get("audio.volume.notification", callback);
|
|
|
|
lock->Get("audio.volume.alarm", callback);
|
|
|
|
lock->Get("audio.volume.telephony", callback);
|
|
|
|
lock->Get("audio.volume.bt_sco", callback);
|
|
|
|
|
2013-07-17 19:21:24 -07:00
|
|
|
// Gecko only control stream volume not master so set to default value
|
|
|
|
// directly.
|
|
|
|
AudioSystem::setMasterVolume(1.0);
|
2013-05-15 21:00:02 -07:00
|
|
|
AudioSystem::setErrorCallback(BinderDeadCallback);
|
2013-08-22 20:29:11 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE_VOID(obs);
|
|
|
|
if (NS_FAILED(obs->AddObserver(this, BLUETOOTH_SCO_STATUS_CHANGED_ID, false))) {
|
|
|
|
NS_WARNING("Failed to add bluetooth sco status changed observer!");
|
|
|
|
}
|
|
|
|
if (NS_FAILED(obs->AddObserver(this, BLUETOOTH_A2DP_STATUS_CHANGED_ID, false))) {
|
|
|
|
NS_WARNING("Failed to add bluetooth a2dp status changed observer!");
|
|
|
|
}
|
|
|
|
if (NS_FAILED(obs->AddObserver(this, BLUETOOTH_HFP_STATUS_CHANGED_ID, false))) {
|
|
|
|
NS_WARNING("Failed to add bluetooth hfp status changed observer!");
|
|
|
|
}
|
|
|
|
if (NS_FAILED(obs->AddObserver(this, MOZ_SETTINGS_CHANGE_ID, false))) {
|
|
|
|
NS_WARNING("Failed to add mozsettings-changed observer!");
|
|
|
|
}
|
2014-05-29 00:20:42 -07:00
|
|
|
if (NS_FAILED(obs->AddObserver(this, AUDIO_CHANNEL_PROCESS_CHANGED, false))) {
|
|
|
|
NS_WARNING("Failed to add audio-channel-process-changed observer!");
|
|
|
|
}
|
2013-11-26 23:46:27 -08:00
|
|
|
|
2013-12-03 22:57:40 -08:00
|
|
|
#ifdef MOZ_B2G_RIL
|
2013-11-26 23:46:27 -08:00
|
|
|
char value[PROPERTY_VALUE_MAX];
|
|
|
|
property_get("ro.moz.mute.call.to_ril", value, "false");
|
|
|
|
if (!strcmp(value, "true")) {
|
|
|
|
mMuteCallToRIL = true;
|
|
|
|
}
|
2013-12-03 22:57:40 -08:00
|
|
|
#endif
|
2012-04-22 11:14:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioManager::~AudioManager() {
|
|
|
|
UnregisterSwitchObserver(SWITCH_HEADPHONES, mObserver);
|
2012-09-29 02:39:05 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2013-06-19 18:20:16 -07:00
|
|
|
NS_ENSURE_TRUE_VOID(obs);
|
2013-06-20 22:37:21 -07:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, BLUETOOTH_SCO_STATUS_CHANGED_ID))) {
|
|
|
|
NS_WARNING("Failed to remove bluetooth sco status changed observer!");
|
|
|
|
}
|
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, BLUETOOTH_A2DP_STATUS_CHANGED_ID))) {
|
|
|
|
NS_WARNING("Failed to remove bluetooth a2dp status changed observer!");
|
2012-09-29 02:39:05 -07:00
|
|
|
}
|
2013-07-29 23:58:58 -07:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, BLUETOOTH_HFP_STATUS_CHANGED_ID))) {
|
|
|
|
NS_WARNING("Failed to remove bluetooth hfp status changed observer!");
|
|
|
|
}
|
2013-08-22 20:29:11 -07:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, MOZ_SETTINGS_CHANGE_ID))) {
|
|
|
|
NS_WARNING("Failed to remove mozsettings-changed observer!");
|
|
|
|
}
|
2014-05-29 00:20:42 -07:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, AUDIO_CHANNEL_PROCESS_CHANGED))) {
|
|
|
|
NS_WARNING("Failed to remove audio-channel-process-changed!");
|
|
|
|
}
|
2012-04-22 11:14:36 -07:00
|
|
|
}
|
|
|
|
|
2014-05-27 23:19:22 -07:00
|
|
|
static StaticRefPtr<AudioManager> sAudioManager;
|
|
|
|
|
|
|
|
already_AddRefed<AudioManager>
|
|
|
|
AudioManager::GetInstance()
|
|
|
|
{
|
|
|
|
// Avoid createing AudioManager from content process.
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
MOZ_CRASH("Non-chrome processes should not get here.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid createing multiple AudioManager instance inside main process.
|
|
|
|
if (!sAudioManager) {
|
|
|
|
sAudioManager = new AudioManager();
|
|
|
|
ClearOnShutdown(&sAudioManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<AudioManager> audioMgr = sAudioManager.get();
|
|
|
|
return audioMgr.forget();
|
|
|
|
}
|
|
|
|
|
2011-12-12 10:22:26 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioManager::GetMicrophoneMuted(bool* aMicrophoneMuted)
|
|
|
|
{
|
2013-12-03 22:57:40 -08:00
|
|
|
#ifdef MOZ_B2G_RIL
|
2013-11-26 23:46:27 -08:00
|
|
|
if (mMuteCallToRIL) {
|
|
|
|
// Simply return cached mIsMicMuted if mute call go via RIL.
|
|
|
|
*aMicrophoneMuted = mIsMicMuted;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-12-03 22:57:40 -08:00
|
|
|
#endif
|
2013-11-26 23:46:27 -08:00
|
|
|
|
2011-12-12 10:22:26 -08:00
|
|
|
if (AudioSystem::isMicrophoneMuted(aMicrophoneMuted)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioManager::SetMicrophoneMuted(bool aMicrophoneMuted)
|
|
|
|
{
|
2013-11-26 23:46:27 -08:00
|
|
|
if (!AudioSystem::muteMicrophone(aMicrophoneMuted)) {
|
2013-12-03 22:57:40 -08:00
|
|
|
#ifdef MOZ_B2G_RIL
|
2013-11-26 23:46:27 -08:00
|
|
|
if (mMuteCallToRIL) {
|
|
|
|
// Extra mute request to RIL for specific platform.
|
|
|
|
nsCOMPtr<nsIRadioInterfaceLayer> ril = do_GetService("@mozilla.org/ril;1");
|
|
|
|
NS_ENSURE_TRUE(ril, NS_ERROR_FAILURE);
|
|
|
|
ril->SetMicrophoneMuted(aMicrophoneMuted);
|
|
|
|
mIsMicMuted = aMicrophoneMuted;
|
|
|
|
}
|
2013-12-03 22:57:40 -08:00
|
|
|
#endif
|
2013-11-26 23:46:27 -08:00
|
|
|
return NS_OK;
|
2011-12-12 10:22:26 -08:00
|
|
|
}
|
2013-11-26 23:46:27 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-12-12 10:22:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
AudioManager::GetPhoneState(int32_t* aState)
|
2011-12-12 10:22:26 -08:00
|
|
|
{
|
|
|
|
*aState = mPhoneState;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
AudioManager::SetPhoneState(int32_t aState)
|
2011-12-12 10:22:26 -08:00
|
|
|
{
|
2012-12-12 17:45:10 -08:00
|
|
|
if (mPhoneState == aState) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-09-13 01:51:48 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
nsString state;
|
|
|
|
state.AppendInt(aState);
|
|
|
|
obs->NotifyObservers(nullptr, "phone-state-changed", state.get());
|
|
|
|
}
|
|
|
|
|
2013-05-20 00:34:42 -07:00
|
|
|
#if ANDROID_VERSION < 17
|
2011-12-12 10:22:26 -08:00
|
|
|
if (AudioSystem::setPhoneState(aState)) {
|
2013-05-20 00:34:42 -07:00
|
|
|
#else
|
|
|
|
if (AudioSystem::setPhoneState(static_cast<audio_mode_t>(aState))) {
|
|
|
|
#endif
|
2011-12-12 10:22:26 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-05-14 21:13:06 -07:00
|
|
|
|
|
|
|
mPhoneState = aState;
|
2012-12-06 02:11:06 -08:00
|
|
|
|
2013-02-03 19:23:06 -08:00
|
|
|
if (mPhoneAudioAgent) {
|
|
|
|
mPhoneAudioAgent->StopPlaying();
|
|
|
|
mPhoneAudioAgent = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aState == PHONE_STATE_IN_CALL || aState == PHONE_STATE_RINGTONE) {
|
|
|
|
mPhoneAudioAgent = do_CreateInstance("@mozilla.org/audiochannelagent;1");
|
|
|
|
MOZ_ASSERT(mPhoneAudioAgent);
|
|
|
|
if (aState == PHONE_STATE_IN_CALL) {
|
2012-12-12 17:45:10 -08:00
|
|
|
// Telephony doesn't be paused by any other channels.
|
2014-04-10 10:39:20 -07:00
|
|
|
mPhoneAudioAgent->Init(nullptr, (int32_t)AudioChannel::Telephony, nullptr);
|
2013-02-03 19:23:06 -08:00
|
|
|
} else {
|
2014-04-10 10:39:20 -07:00
|
|
|
mPhoneAudioAgent->Init(nullptr, (int32_t)AudioChannel::Ringer, nullptr);
|
2012-12-12 17:45:10 -08:00
|
|
|
}
|
2013-02-03 19:23:06 -08:00
|
|
|
|
|
|
|
// Telephony can always play.
|
2013-09-02 02:45:44 -07:00
|
|
|
int32_t canPlay;
|
2013-02-03 19:23:06 -08:00
|
|
|
mPhoneAudioAgent->StartPlaying(&canPlay);
|
2012-12-06 02:11:06 -08:00
|
|
|
}
|
2012-12-12 17:45:10 -08:00
|
|
|
|
2011-12-12 10:22:26 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
AudioManager::SetForceForUse(int32_t aUsage, int32_t aForce)
|
2011-12-12 10:22:26 -08:00
|
|
|
{
|
2012-03-26 17:39:33 -07:00
|
|
|
if (static_cast<
|
|
|
|
status_t (*)(audio_policy_force_use_t, audio_policy_forced_cfg_t)
|
|
|
|
>(AudioSystem::setForceUse)) {
|
|
|
|
// Dynamically resolved the ICS signature.
|
2013-07-17 22:23:10 -07:00
|
|
|
status_t status = AudioSystem::setForceUse(
|
|
|
|
(audio_policy_force_use_t)aUsage,
|
|
|
|
(audio_policy_forced_cfg_t)aForce);
|
|
|
|
return status ? NS_ERROR_FAILURE : NS_OK;
|
2011-12-12 10:22:26 -08:00
|
|
|
}
|
2012-03-26 17:39:33 -07:00
|
|
|
|
2013-07-17 22:23:10 -07:00
|
|
|
NS_NOTREACHED("Doesn't support force routing on GB version");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-12-12 10:22:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
AudioManager::GetForceForUse(int32_t aUsage, int32_t* aForce) {
|
2012-03-26 17:39:33 -07:00
|
|
|
if (static_cast<
|
2013-07-17 22:23:10 -07:00
|
|
|
audio_policy_forced_cfg_t (*)(audio_policy_force_use_t)
|
2012-03-26 17:39:33 -07:00
|
|
|
>(AudioSystem::getForceUse)) {
|
|
|
|
// Dynamically resolved the ICS signature.
|
|
|
|
*aForce = AudioSystem::getForceUse((audio_policy_force_use_t)aUsage);
|
2013-07-17 22:23:10 -07:00
|
|
|
return NS_OK;
|
2012-03-26 17:39:33 -07:00
|
|
|
}
|
2013-07-17 22:23:10 -07:00
|
|
|
|
|
|
|
NS_NOTREACHED("Doesn't support force routing on GB version");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-12-12 10:22:26 -08:00
|
|
|
}
|
2012-04-22 11:14:36 -07:00
|
|
|
|
2012-09-19 10:51:35 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioManager::GetFmRadioAudioEnabled(bool *aFmRadioAudioEnabled)
|
|
|
|
{
|
2012-11-14 19:00:34 -08:00
|
|
|
*aFmRadioAudioEnabled = IsDeviceOn(AUDIO_DEVICE_OUT_FM);
|
2012-09-19 10:51:35 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioManager::SetFmRadioAudioEnabled(bool aFmRadioAudioEnabled)
|
|
|
|
{
|
2014-11-24 16:48:49 -08:00
|
|
|
AudioSystem::setDeviceConnectionState(AUDIO_DEVICE_OUT_FM,
|
|
|
|
aFmRadioAudioEnabled ? AUDIO_POLICY_DEVICE_STATE_AVAILABLE :
|
|
|
|
AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, "");
|
|
|
|
InternalSetAudioRoutes(GetCurrentSwitchState(SWITCH_HEADPHONES));
|
|
|
|
// sync volume with music after powering on fm radio
|
|
|
|
if (aFmRadioAudioEnabled) {
|
|
|
|
int32_t volIndex = mCurrentStreamVolumeTbl[AUDIO_STREAM_MUSIC];
|
|
|
|
SetStreamVolumeIndex(AUDIO_STREAM_FM, volIndex);
|
|
|
|
mCurrentStreamVolumeTbl[AUDIO_STREAM_FM] = volIndex;
|
2012-04-30 11:10:48 -07:00
|
|
|
}
|
2014-11-24 16:48:49 -08:00
|
|
|
return NS_OK;
|
2012-04-22 11:14:36 -07:00
|
|
|
}
|
2012-11-02 02:36:46 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-07-17 22:22:36 -07:00
|
|
|
AudioManager::SetAudioChannelVolume(int32_t aChannel, int32_t aIndex) {
|
2013-11-21 23:20:25 -08:00
|
|
|
nsresult status;
|
2013-07-17 22:22:36 -07:00
|
|
|
|
2014-04-10 10:39:20 -07:00
|
|
|
switch (static_cast<AudioChannel>(aChannel)) {
|
|
|
|
case AudioChannel::Content:
|
2013-07-17 22:22:36 -07:00
|
|
|
// sync FMRadio's volume with content channel.
|
|
|
|
if (IsDeviceOn(AUDIO_DEVICE_OUT_FM)) {
|
2013-11-21 23:20:25 -08:00
|
|
|
status = SetStreamVolumeIndex(AUDIO_STREAM_FM, aIndex);
|
|
|
|
NS_ENSURE_SUCCESS(status, status);
|
2013-07-17 22:22:36 -07:00
|
|
|
}
|
2013-11-21 23:20:25 -08:00
|
|
|
status = SetStreamVolumeIndex(AUDIO_STREAM_MUSIC, aIndex);
|
|
|
|
NS_ENSURE_SUCCESS(status, status);
|
|
|
|
status = SetStreamVolumeIndex(AUDIO_STREAM_SYSTEM, aIndex);
|
2013-07-17 22:22:36 -07:00
|
|
|
break;
|
2014-04-10 10:39:20 -07:00
|
|
|
case AudioChannel::Notification:
|
2013-07-17 22:22:36 -07:00
|
|
|
status = SetStreamVolumeIndex(AUDIO_STREAM_NOTIFICATION, aIndex);
|
2013-11-21 23:20:25 -08:00
|
|
|
NS_ENSURE_SUCCESS(status, status);
|
|
|
|
status = SetStreamVolumeIndex(AUDIO_STREAM_RING, aIndex);
|
2013-07-17 22:22:36 -07:00
|
|
|
break;
|
2014-04-10 10:39:20 -07:00
|
|
|
case AudioChannel::Alarm:
|
2013-07-17 22:22:36 -07:00
|
|
|
status = SetStreamVolumeIndex(AUDIO_STREAM_ALARM, aIndex);
|
|
|
|
break;
|
2014-04-10 10:39:20 -07:00
|
|
|
case AudioChannel::Telephony:
|
2013-07-17 22:22:36 -07:00
|
|
|
status = SetStreamVolumeIndex(AUDIO_STREAM_VOICE_CALL, aIndex);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2012-11-28 18:14:11 -08:00
|
|
|
}
|
2012-12-11 02:26:01 -08:00
|
|
|
|
2013-11-21 23:20:25 -08:00
|
|
|
return status;
|
2012-11-02 02:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-07-17 22:22:36 -07:00
|
|
|
AudioManager::GetAudioChannelVolume(int32_t aChannel, int32_t* aIndex) {
|
|
|
|
if (!aIndex) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2014-04-10 10:39:20 -07:00
|
|
|
switch (static_cast<AudioChannel>(aChannel)) {
|
|
|
|
case AudioChannel::Content:
|
2013-07-17 22:22:36 -07:00
|
|
|
MOZ_ASSERT(mCurrentStreamVolumeTbl[AUDIO_STREAM_MUSIC] ==
|
|
|
|
mCurrentStreamVolumeTbl[AUDIO_STREAM_SYSTEM]);
|
|
|
|
*aIndex = mCurrentStreamVolumeTbl[AUDIO_STREAM_MUSIC];
|
|
|
|
break;
|
2014-04-10 10:39:20 -07:00
|
|
|
case AudioChannel::Notification:
|
2013-07-17 22:22:36 -07:00
|
|
|
MOZ_ASSERT(mCurrentStreamVolumeTbl[AUDIO_STREAM_NOTIFICATION] ==
|
|
|
|
mCurrentStreamVolumeTbl[AUDIO_STREAM_RING]);
|
|
|
|
*aIndex = mCurrentStreamVolumeTbl[AUDIO_STREAM_NOTIFICATION];
|
|
|
|
break;
|
2014-04-10 10:39:20 -07:00
|
|
|
case AudioChannel::Alarm:
|
2013-07-17 22:22:36 -07:00
|
|
|
*aIndex = mCurrentStreamVolumeTbl[AUDIO_STREAM_ALARM];
|
|
|
|
break;
|
2014-04-10 10:39:20 -07:00
|
|
|
case AudioChannel::Telephony:
|
2013-07-17 22:22:36 -07:00
|
|
|
*aIndex = mCurrentStreamVolumeTbl[AUDIO_STREAM_VOICE_CALL];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2013-05-15 21:00:02 -07:00
|
|
|
return NS_OK;
|
2012-11-02 02:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-07-17 22:22:36 -07:00
|
|
|
AudioManager::GetMaxAudioChannelVolume(int32_t aChannel, int32_t* aMaxIndex) {
|
|
|
|
if (!aMaxIndex) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t stream;
|
2014-04-10 10:39:20 -07:00
|
|
|
switch (static_cast<AudioChannel>(aChannel)) {
|
|
|
|
case AudioChannel::Content:
|
2013-07-17 22:22:36 -07:00
|
|
|
MOZ_ASSERT(sMaxStreamVolumeTbl[AUDIO_STREAM_MUSIC] ==
|
|
|
|
sMaxStreamVolumeTbl[AUDIO_STREAM_SYSTEM]);
|
|
|
|
stream = AUDIO_STREAM_MUSIC;
|
|
|
|
break;
|
2014-04-10 10:39:20 -07:00
|
|
|
case AudioChannel::Notification:
|
2013-07-17 22:22:36 -07:00
|
|
|
MOZ_ASSERT(sMaxStreamVolumeTbl[AUDIO_STREAM_NOTIFICATION] ==
|
|
|
|
sMaxStreamVolumeTbl[AUDIO_STREAM_RING]);
|
|
|
|
stream = AUDIO_STREAM_NOTIFICATION;
|
|
|
|
break;
|
2014-04-10 10:39:20 -07:00
|
|
|
case AudioChannel::Alarm:
|
2013-07-17 22:22:36 -07:00
|
|
|
stream = AUDIO_STREAM_ALARM;
|
|
|
|
break;
|
2014-04-10 10:39:20 -07:00
|
|
|
case AudioChannel::Telephony:
|
2013-07-17 22:22:36 -07:00
|
|
|
stream = AUDIO_STREAM_VOICE_CALL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aMaxIndex = sMaxStreamVolumeTbl[stream];
|
|
|
|
return NS_OK;
|
2012-11-02 02:36:46 -07:00
|
|
|
}
|
|
|
|
|
2013-11-21 23:20:25 -08:00
|
|
|
nsresult
|
2013-07-17 22:22:36 -07:00
|
|
|
AudioManager::SetStreamVolumeIndex(int32_t aStream, int32_t aIndex) {
|
|
|
|
if (aIndex < 0 || aIndex > sMaxStreamVolumeTbl[aStream]) {
|
2013-11-21 23:20:25 -08:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2013-07-17 22:22:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentStreamVolumeTbl[aStream] = aIndex;
|
2013-11-21 23:20:25 -08:00
|
|
|
status_t status;
|
2013-07-17 22:22:36 -07:00
|
|
|
#if ANDROID_VERSION < 17
|
2013-11-21 23:20:25 -08:00
|
|
|
status = AudioSystem::setStreamVolumeIndex(
|
|
|
|
static_cast<audio_stream_type_t>(aStream),
|
|
|
|
aIndex);
|
|
|
|
return status ? NS_ERROR_FAILURE : NS_OK;
|
2013-07-17 22:22:36 -07:00
|
|
|
#else
|
|
|
|
int device = 0;
|
|
|
|
|
|
|
|
if (aStream == AUDIO_STREAM_BLUETOOTH_SCO) {
|
|
|
|
device = AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
|
|
|
|
} else if (aStream == AUDIO_STREAM_FM) {
|
|
|
|
device = AUDIO_DEVICE_OUT_FM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device != 0) {
|
2013-11-21 23:20:25 -08:00
|
|
|
status = AudioSystem::setStreamVolumeIndex(
|
|
|
|
static_cast<audio_stream_type_t>(aStream),
|
|
|
|
aIndex,
|
|
|
|
device);
|
|
|
|
return status ? NS_ERROR_FAILURE : NS_OK;
|
2013-07-17 22:22:36 -07:00
|
|
|
}
|
|
|
|
|
2013-11-21 23:20:25 -08:00
|
|
|
status = AudioSystem::setStreamVolumeIndex(
|
|
|
|
static_cast<audio_stream_type_t>(aStream),
|
|
|
|
aIndex,
|
|
|
|
AUDIO_DEVICE_OUT_BLUETOOTH_A2DP);
|
|
|
|
status += AudioSystem::setStreamVolumeIndex(
|
|
|
|
static_cast<audio_stream_type_t>(aStream),
|
|
|
|
aIndex,
|
|
|
|
AUDIO_DEVICE_OUT_SPEAKER);
|
2013-07-17 22:22:36 -07:00
|
|
|
status += AudioSystem::setStreamVolumeIndex(
|
|
|
|
static_cast<audio_stream_type_t>(aStream),
|
|
|
|
aIndex,
|
|
|
|
AUDIO_DEVICE_OUT_WIRED_HEADSET);
|
|
|
|
status += AudioSystem::setStreamVolumeIndex(
|
|
|
|
static_cast<audio_stream_type_t>(aStream),
|
|
|
|
aIndex,
|
|
|
|
AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
|
|
|
|
status += AudioSystem::setStreamVolumeIndex(
|
|
|
|
static_cast<audio_stream_type_t>(aStream),
|
|
|
|
aIndex,
|
|
|
|
AUDIO_DEVICE_OUT_EARPIECE);
|
2014-10-15 21:40:09 -07:00
|
|
|
status += AudioSystem::setStreamVolumeIndex(
|
|
|
|
static_cast<audio_stream_type_t>(aStream),
|
|
|
|
aIndex,
|
|
|
|
AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET);
|
|
|
|
|
2013-11-21 23:20:25 -08:00
|
|
|
return status ? NS_ERROR_FAILURE : NS_OK;
|
2013-07-17 22:22:36 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-21 23:20:25 -08:00
|
|
|
nsresult
|
2013-07-17 22:22:36 -07:00
|
|
|
AudioManager::GetStreamVolumeIndex(int32_t aStream, int32_t *aIndex) {
|
|
|
|
if (!aIndex) {
|
2013-11-21 23:20:25 -08:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2013-07-17 22:22:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aStream <= AUDIO_STREAM_DEFAULT || aStream >= AUDIO_STREAM_MAX) {
|
2013-11-21 23:20:25 -08:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2013-07-17 22:22:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*aIndex = mCurrentStreamVolumeTbl[aStream];
|
|
|
|
|
2013-11-21 23:20:25 -08:00
|
|
|
return NS_OK;
|
2013-07-17 22:22:36 -07:00
|
|
|
}
|