2013-06-08 08:24:29 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
|
|
|
#include "BluetoothA2dpManager.h"
|
|
|
|
|
2013-06-08 08:26:28 -07:00
|
|
|
#include "BluetoothCommon.h"
|
2013-06-08 08:24:29 -07:00
|
|
|
#include "BluetoothService.h"
|
|
|
|
#include "BluetoothSocket.h"
|
|
|
|
#include "BluetoothUtils.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsIAudioManager.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
2013-07-18 19:27:07 -07:00
|
|
|
StaticRefPtr<BluetoothA2dpManager> sBluetoothA2dpManager;
|
|
|
|
bool sInShutdown = false;
|
2013-06-08 08:24:29 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-06-19 18:20:02 -07:00
|
|
|
BluetoothA2dpManager::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
2013-06-08 08:24:29 -07:00
|
|
|
{
|
2013-07-18 19:27:07 -07:00
|
|
|
MOZ_ASSERT(sBluetoothA2dpManager);
|
2013-06-08 08:24:29 -07:00
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
2013-06-19 18:20:16 -07:00
|
|
|
HandleShutdown();
|
|
|
|
return NS_OK;
|
2013-06-08 08:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false, "BluetoothA2dpManager got unexpected topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothA2dpManager::BluetoothA2dpManager()
|
|
|
|
{
|
2013-07-29 02:32:34 -07:00
|
|
|
ResetA2dp();
|
|
|
|
ResetAvrcp();
|
2013-06-08 08:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothA2dpManager::Init()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-06-19 18:20:02 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE(obs, false);
|
|
|
|
if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) {
|
|
|
|
BT_WARNING("Failed to add shutdown observer!");
|
|
|
|
return false;
|
2013-06-08 08:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothA2dpManager::~BluetoothA2dpManager()
|
|
|
|
{
|
2013-06-19 18:20:02 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE_VOID(obs);
|
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID))) {
|
|
|
|
BT_WARNING("Failed to remove shutdown observer!");
|
|
|
|
}
|
2013-06-08 08:24:29 -07:00
|
|
|
}
|
|
|
|
|
2013-07-29 02:32:34 -07:00
|
|
|
void
|
|
|
|
BluetoothA2dpManager::ResetA2dp()
|
|
|
|
{
|
|
|
|
mA2dpConnected = false;
|
|
|
|
mPlaying = false;
|
|
|
|
mSinkState = SinkState::SINK_DISCONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::ResetAvrcp()
|
|
|
|
{
|
|
|
|
mAvrcpConnected = false;
|
2013-07-29 02:32:34 -07:00
|
|
|
mDuration = 0;
|
|
|
|
mMediaNumber = 0;
|
|
|
|
mTotalMediaCount = 0;
|
|
|
|
mPosition = 0;
|
|
|
|
mPlayStatus = ControlPlayStatus::PLAYSTATUS_UNKNOWN;
|
2013-07-29 02:32:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static BluetoothA2dpManager::SinkState
|
2013-06-08 08:25:41 -07:00
|
|
|
StatusStringToSinkState(const nsAString& aStatus)
|
|
|
|
{
|
2013-07-29 02:32:34 -07:00
|
|
|
BluetoothA2dpManager::SinkState state;
|
2013-06-08 08:25:41 -07:00
|
|
|
if (aStatus.EqualsLiteral("disconnected")) {
|
2013-07-29 02:32:34 -07:00
|
|
|
state = BluetoothA2dpManager::SinkState::SINK_DISCONNECTED;
|
2013-06-08 08:25:41 -07:00
|
|
|
} else if (aStatus.EqualsLiteral("connecting")) {
|
2013-07-29 02:32:34 -07:00
|
|
|
state = BluetoothA2dpManager::SinkState::SINK_CONNECTING;
|
2013-06-08 08:25:41 -07:00
|
|
|
} else if (aStatus.EqualsLiteral("connected")) {
|
2013-07-29 02:32:34 -07:00
|
|
|
state = BluetoothA2dpManager::SinkState::SINK_CONNECTED;
|
2013-06-08 08:25:41 -07:00
|
|
|
} else if (aStatus.EqualsLiteral("playing")) {
|
2013-07-29 02:32:34 -07:00
|
|
|
state = BluetoothA2dpManager::SinkState::SINK_PLAYING;
|
2013-06-08 08:25:41 -07:00
|
|
|
} else if (aStatus.EqualsLiteral("disconnecting")) {
|
2013-07-29 02:32:34 -07:00
|
|
|
state = BluetoothA2dpManager::SinkState::SINK_DISCONNECTING;
|
2013-06-08 08:25:41 -07:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "Unknown sink state");
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2013-06-08 08:24:29 -07:00
|
|
|
//static
|
|
|
|
BluetoothA2dpManager*
|
|
|
|
BluetoothA2dpManager::Get()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-07-18 19:27:07 -07:00
|
|
|
// If sBluetoothA2dpManager already exists, exit early
|
|
|
|
if (sBluetoothA2dpManager) {
|
|
|
|
return sBluetoothA2dpManager;
|
2013-06-08 08:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're in shutdown, don't create a new instance
|
2013-07-18 19:27:07 -07:00
|
|
|
if (sInShutdown) {
|
2013-06-08 08:24:29 -07:00
|
|
|
NS_WARNING("BluetoothA2dpManager can't be created during shutdown");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-18 19:27:07 -07:00
|
|
|
// Create a new instance, register, and return
|
2013-06-08 08:24:29 -07:00
|
|
|
BluetoothA2dpManager* manager = new BluetoothA2dpManager();
|
|
|
|
NS_ENSURE_TRUE(manager->Init(), nullptr);
|
|
|
|
|
2013-07-18 19:27:07 -07:00
|
|
|
sBluetoothA2dpManager = manager;
|
|
|
|
return sBluetoothA2dpManager;
|
2013-06-08 08:24:29 -07:00
|
|
|
}
|
|
|
|
|
2013-06-19 18:20:16 -07:00
|
|
|
void
|
2013-06-08 08:24:29 -07:00
|
|
|
BluetoothA2dpManager::HandleShutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-07-18 19:27:07 -07:00
|
|
|
sInShutdown = true;
|
2013-06-08 08:24:29 -07:00
|
|
|
Disconnect();
|
2013-07-18 19:27:07 -07:00
|
|
|
sBluetoothA2dpManager = nullptr;
|
2013-06-08 08:24:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothA2dpManager::Connect(const nsAString& aDeviceAddress)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!aDeviceAddress.IsEmpty());
|
|
|
|
|
2013-07-18 19:27:07 -07:00
|
|
|
if (sInShutdown) {
|
2013-06-08 08:24:29 -07:00
|
|
|
NS_WARNING("Connect called while in shutdown!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-29 02:32:34 -07:00
|
|
|
if (mA2dpConnected) {
|
2013-06-08 08:24:29 -07:00
|
|
|
NS_WARNING("BluetoothA2dpManager is connected");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDeviceAddress = aDeviceAddress;
|
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE(bs, false);
|
|
|
|
nsresult rv = bs->SendSinkMessage(aDeviceAddress,
|
|
|
|
NS_LITERAL_STRING("Connect"));
|
|
|
|
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::Disconnect()
|
|
|
|
{
|
2013-07-29 02:32:34 -07:00
|
|
|
if (!mA2dpConnected) {
|
2013-06-08 08:24:29 -07:00
|
|
|
NS_WARNING("BluetoothA2dpManager has been disconnected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-14 18:29:20 -07:00
|
|
|
MOZ_ASSERT(!mDeviceAddress.IsEmpty());
|
|
|
|
|
2013-06-08 08:24:29 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
|
|
bs->SendSinkMessage(mDeviceAddress, NS_LITERAL_STRING("Disconnect"));
|
|
|
|
}
|
2013-06-08 08:25:41 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::HandleSinkPropertyChanged(const BluetoothSignal& aSignal)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aSignal.value().type() == BluetoothValue::TArrayOfBluetoothNamedValue);
|
|
|
|
|
|
|
|
const InfallibleTArray<BluetoothNamedValue>& arr =
|
|
|
|
aSignal.value().get_ArrayOfBluetoothNamedValue();
|
|
|
|
MOZ_ASSERT(arr.Length() == 1);
|
|
|
|
|
|
|
|
const nsString& name = arr[0].name();
|
|
|
|
const BluetoothValue& value = arr[0].value();
|
|
|
|
if (name.EqualsLiteral("Connected")) {
|
|
|
|
// Indicates if a stream is setup to a A2DP sink on the remote device.
|
|
|
|
MOZ_ASSERT(value.type() == BluetoothValue::Tbool);
|
2013-07-29 02:32:34 -07:00
|
|
|
mA2dpConnected = value.get_bool();
|
2013-06-08 08:26:01 -07:00
|
|
|
NotifyStatusChanged();
|
2013-06-08 08:25:41 -07:00
|
|
|
} else if (name.EqualsLiteral("Playing")) {
|
|
|
|
// Indicates if a stream is active to a A2DP sink on the remote device.
|
|
|
|
MOZ_ASSERT(value.type() == BluetoothValue::Tbool);
|
|
|
|
mPlaying = value.get_bool();
|
|
|
|
} else if (name.EqualsLiteral("State")) {
|
|
|
|
MOZ_ASSERT(value.type() == BluetoothValue::TnsString);
|
|
|
|
HandleSinkStateChanged(StatusStringToSinkState(value.get_nsString()));
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Unknown sink property");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HandleSinkPropertyChanged update sink state in A2dp
|
|
|
|
*
|
|
|
|
* Possible values: "disconnected", "connecting", "connected", "playing"
|
|
|
|
*
|
|
|
|
* 1. "disconnected" -> "connecting"
|
|
|
|
* Either an incoming or outgoing connection attempt ongoing
|
|
|
|
* 2. "connecting" -> "disconnected"
|
|
|
|
* Connection attempt failed
|
|
|
|
* 3. "connecting" -> "connected"
|
|
|
|
* Successfully connected
|
|
|
|
* 4. "connected" -> "playing"
|
|
|
|
* Audio stream active
|
|
|
|
* 5. "playing" -> "connected"
|
|
|
|
* Audio stream suspended
|
|
|
|
* 6. "connected" -> "disconnected"
|
|
|
|
* "playing" -> "disconnected"
|
|
|
|
* Disconnected from the remote device
|
|
|
|
* 7. "disconnecting" -> "disconnected"
|
|
|
|
* Disconnected from local
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::HandleSinkStateChanged(SinkState aState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT_IF(aState == SinkState::SINK_CONNECTED,
|
|
|
|
mSinkState == SinkState::SINK_CONNECTING ||
|
|
|
|
mSinkState == SinkState::SINK_PLAYING);
|
|
|
|
MOZ_ASSERT_IF(aState == SinkState::SINK_PLAYING,
|
|
|
|
mSinkState == SinkState::SINK_CONNECTED);
|
|
|
|
|
|
|
|
if (aState == SinkState::SINK_DISCONNECTED) {
|
|
|
|
mDeviceAddress.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
mSinkState = aState;
|
|
|
|
}
|
2013-06-08 08:26:01 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::NotifyStatusChanged()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-07-30 02:34:56 -07:00
|
|
|
// Broadcast system message to Gaia
|
2013-06-19 18:20:16 -07:00
|
|
|
NS_NAMED_LITERAL_STRING(type, BLUETOOTH_A2DP_STATUS_CHANGED_ID);
|
2013-06-08 08:26:01 -07:00
|
|
|
InfallibleTArray<BluetoothNamedValue> parameters;
|
|
|
|
|
2013-07-29 02:32:34 -07:00
|
|
|
BluetoothValue v = mA2dpConnected;
|
2013-06-08 08:26:01 -07:00
|
|
|
parameters.AppendElement(
|
|
|
|
BluetoothNamedValue(NS_LITERAL_STRING("connected"), v));
|
|
|
|
|
|
|
|
v = mDeviceAddress;
|
|
|
|
parameters.AppendElement(
|
|
|
|
BluetoothNamedValue(NS_LITERAL_STRING("address"), v));
|
|
|
|
|
|
|
|
if (!BroadcastSystemMessage(type, parameters)) {
|
|
|
|
NS_WARNING("Failed to broadcast system message to settings");
|
|
|
|
}
|
|
|
|
|
2013-07-30 02:34:56 -07:00
|
|
|
// Notify Gecko observers
|
2013-06-08 08:26:01 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
NS_ENSURE_TRUE_VOID(obs);
|
|
|
|
|
|
|
|
if (NS_FAILED(obs->NotifyObservers(this,
|
2013-06-19 18:20:16 -07:00
|
|
|
BLUETOOTH_A2DP_STATUS_CHANGED_ID,
|
2013-07-30 02:34:56 -07:00
|
|
|
mDeviceAddress.get()))) {
|
2013-06-08 08:26:01 -07:00
|
|
|
NS_WARNING("Failed to notify bluetooth-a2dp-status-changed observsers!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::OnGetServiceChannel(const nsAString& aDeviceAddress,
|
|
|
|
const nsAString& aServiceUuid,
|
|
|
|
int aChannel)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::OnUpdateSdpRecords(const nsAString& aDeviceAddress)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::GetAddress(nsAString& aDeviceAddress)
|
|
|
|
{
|
|
|
|
aDeviceAddress = mDeviceAddress;
|
|
|
|
}
|
|
|
|
|
2013-07-29 02:32:34 -07:00
|
|
|
bool
|
|
|
|
BluetoothA2dpManager::IsConnected()
|
|
|
|
{
|
|
|
|
return mA2dpConnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::SetAvrcpConnected(bool aConnected)
|
|
|
|
{
|
|
|
|
mAvrcpConnected = aConnected;
|
|
|
|
if (!aConnected) {
|
|
|
|
ResetAvrcp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BluetoothA2dpManager::IsAvrcpConnected()
|
|
|
|
{
|
|
|
|
return mAvrcpConnected;
|
|
|
|
}
|
|
|
|
|
2013-07-29 02:32:34 -07:00
|
|
|
void
|
|
|
|
BluetoothA2dpManager::UpdateMetaData(const nsAString& aTitle,
|
|
|
|
const nsAString& aArtist,
|
|
|
|
const nsAString& aAlbum,
|
|
|
|
uint32_t aMediaNumber,
|
|
|
|
uint32_t aTotalMediaCount,
|
|
|
|
uint32_t aDuration)
|
|
|
|
{
|
|
|
|
mTitle.Assign(aTitle);
|
|
|
|
mArtist.Assign(aArtist);
|
|
|
|
mAlbum.Assign(aAlbum);
|
|
|
|
mMediaNumber = aMediaNumber;
|
|
|
|
mTotalMediaCount = aTotalMediaCount;
|
|
|
|
mDuration = aDuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::UpdatePlayStatus(uint32_t aDuration,
|
|
|
|
uint32_t aPosition,
|
|
|
|
ControlPlayStatus aPlayStatus)
|
|
|
|
{
|
|
|
|
mDuration = aDuration;
|
|
|
|
mPosition = aPosition;
|
|
|
|
mPlayStatus = aPlayStatus;
|
|
|
|
}
|
|
|
|
|
2013-07-29 02:32:34 -07:00
|
|
|
void
|
|
|
|
BluetoothA2dpManager::GetAlbum(nsAString& aAlbum)
|
|
|
|
{
|
|
|
|
aAlbum.Assign(mAlbum);
|
|
|
|
}
|
|
|
|
|
2013-07-29 02:32:34 -07:00
|
|
|
uint32_t
|
|
|
|
BluetoothA2dpManager::GetDuration()
|
|
|
|
{
|
|
|
|
return mDuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlPlayStatus
|
|
|
|
BluetoothA2dpManager::GetPlayStatus()
|
|
|
|
{
|
|
|
|
return mPlayStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
BluetoothA2dpManager::GetPosition()
|
|
|
|
{
|
|
|
|
return mPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
BluetoothA2dpManager::GetMediaNumber()
|
|
|
|
{
|
|
|
|
return mMediaNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothA2dpManager::GetTitle(nsAString& aTitle)
|
|
|
|
{
|
|
|
|
aTitle.Assign(mTitle);
|
|
|
|
}
|
|
|
|
|
2013-06-19 18:20:02 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(BluetoothA2dpManager, nsIObserver)
|
2013-06-08 08:26:01 -07:00
|
|
|
|