dom/bluetooth/bluez/BluetoothA2dpManager.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "base/basictypes.h"
michael@0 8
michael@0 9 #include "BluetoothA2dpManager.h"
michael@0 10
michael@0 11 #include "BluetoothCommon.h"
michael@0 12 #include "BluetoothService.h"
michael@0 13 #include "BluetoothSocket.h"
michael@0 14 #include "BluetoothUtils.h"
michael@0 15
michael@0 16 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
michael@0 17 #include "mozilla/Services.h"
michael@0 18 #include "mozilla/StaticPtr.h"
michael@0 19 #include "nsIObserverService.h"
michael@0 20 #include "MainThreadUtils.h"
michael@0 21
michael@0 22
michael@0 23 using namespace mozilla;
michael@0 24 USING_BLUETOOTH_NAMESPACE
michael@0 25
michael@0 26 namespace {
michael@0 27 StaticRefPtr<BluetoothA2dpManager> sBluetoothA2dpManager;
michael@0 28 bool sInShutdown = false;
michael@0 29 } // anonymous namespace
michael@0 30
michael@0 31 NS_IMETHODIMP
michael@0 32 BluetoothA2dpManager::Observe(nsISupports* aSubject,
michael@0 33 const char* aTopic,
michael@0 34 const char16_t* aData)
michael@0 35 {
michael@0 36 MOZ_ASSERT(sBluetoothA2dpManager);
michael@0 37
michael@0 38 if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
michael@0 39 HandleShutdown();
michael@0 40 return NS_OK;
michael@0 41 }
michael@0 42
michael@0 43 MOZ_ASSERT(false, "BluetoothA2dpManager got unexpected topic!");
michael@0 44 return NS_ERROR_UNEXPECTED;
michael@0 45 }
michael@0 46
michael@0 47 BluetoothA2dpManager::BluetoothA2dpManager()
michael@0 48 {
michael@0 49 Reset();
michael@0 50 }
michael@0 51
michael@0 52 void
michael@0 53 BluetoothA2dpManager::Reset()
michael@0 54 {
michael@0 55 ResetA2dp();
michael@0 56 ResetAvrcp();
michael@0 57 }
michael@0 58
michael@0 59 bool
michael@0 60 BluetoothA2dpManager::Init()
michael@0 61 {
michael@0 62 MOZ_ASSERT(NS_IsMainThread());
michael@0 63
michael@0 64 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
michael@0 65 NS_ENSURE_TRUE(obs, false);
michael@0 66 if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) {
michael@0 67 BT_WARNING("Failed to add shutdown observer!");
michael@0 68 return false;
michael@0 69 }
michael@0 70
michael@0 71 return true;
michael@0 72 }
michael@0 73
michael@0 74 BluetoothA2dpManager::~BluetoothA2dpManager()
michael@0 75 {
michael@0 76 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
michael@0 77 NS_ENSURE_TRUE_VOID(obs);
michael@0 78 if (NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID))) {
michael@0 79 BT_WARNING("Failed to remove shutdown observer!");
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 void
michael@0 84 BluetoothA2dpManager::ResetA2dp()
michael@0 85 {
michael@0 86 mA2dpConnected = false;
michael@0 87 mSinkState = SinkState::SINK_DISCONNECTED;
michael@0 88 mController = nullptr;
michael@0 89 }
michael@0 90
michael@0 91 void
michael@0 92 BluetoothA2dpManager::ResetAvrcp()
michael@0 93 {
michael@0 94 mAvrcpConnected = false;
michael@0 95 mDuration = 0;
michael@0 96 mMediaNumber = 0;
michael@0 97 mTotalMediaCount = 0;
michael@0 98 mPosition = 0;
michael@0 99 mPlayStatus = ControlPlayStatus::PLAYSTATUS_UNKNOWN;
michael@0 100 }
michael@0 101
michael@0 102 static BluetoothA2dpManager::SinkState
michael@0 103 StatusStringToSinkState(const nsAString& aStatus)
michael@0 104 {
michael@0 105 BluetoothA2dpManager::SinkState state =
michael@0 106 BluetoothA2dpManager::SinkState::SINK_UNKNOWN;
michael@0 107 if (aStatus.EqualsLiteral("disconnected")) {
michael@0 108 state = BluetoothA2dpManager::SinkState::SINK_DISCONNECTED;
michael@0 109 } else if (aStatus.EqualsLiteral("connecting")) {
michael@0 110 state = BluetoothA2dpManager::SinkState::SINK_CONNECTING;
michael@0 111 } else if (aStatus.EqualsLiteral("connected")) {
michael@0 112 state = BluetoothA2dpManager::SinkState::SINK_CONNECTED;
michael@0 113 } else if (aStatus.EqualsLiteral("playing")) {
michael@0 114 state = BluetoothA2dpManager::SinkState::SINK_PLAYING;
michael@0 115 } else {
michael@0 116 BT_WARNING("Unknown sink state");
michael@0 117 }
michael@0 118 return state;
michael@0 119 }
michael@0 120
michael@0 121 //static
michael@0 122 BluetoothA2dpManager*
michael@0 123 BluetoothA2dpManager::Get()
michael@0 124 {
michael@0 125 MOZ_ASSERT(NS_IsMainThread());
michael@0 126
michael@0 127 // If sBluetoothA2dpManager already exists, exit early
michael@0 128 if (sBluetoothA2dpManager) {
michael@0 129 return sBluetoothA2dpManager;
michael@0 130 }
michael@0 131
michael@0 132 // If we're in shutdown, don't create a new instance
michael@0 133 NS_ENSURE_FALSE(sInShutdown, nullptr);
michael@0 134
michael@0 135 // Create a new instance, register, and return
michael@0 136 BluetoothA2dpManager* manager = new BluetoothA2dpManager();
michael@0 137 NS_ENSURE_TRUE(manager->Init(), nullptr);
michael@0 138
michael@0 139 sBluetoothA2dpManager = manager;
michael@0 140 return sBluetoothA2dpManager;
michael@0 141 }
michael@0 142
michael@0 143 void
michael@0 144 BluetoothA2dpManager::HandleShutdown()
michael@0 145 {
michael@0 146 MOZ_ASSERT(NS_IsMainThread());
michael@0 147 sInShutdown = true;
michael@0 148 Disconnect(nullptr);
michael@0 149 sBluetoothA2dpManager = nullptr;
michael@0 150 }
michael@0 151
michael@0 152 void
michael@0 153 BluetoothA2dpManager::Connect(const nsAString& aDeviceAddress,
michael@0 154 BluetoothProfileController* aController)
michael@0 155 {
michael@0 156 MOZ_ASSERT(NS_IsMainThread());
michael@0 157 MOZ_ASSERT(!aDeviceAddress.IsEmpty());
michael@0 158 MOZ_ASSERT(aController && !mController);
michael@0 159
michael@0 160 BluetoothService* bs = BluetoothService::Get();
michael@0 161 if (!bs || sInShutdown) {
michael@0 162 aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
michael@0 163 return;
michael@0 164 }
michael@0 165
michael@0 166 if (mA2dpConnected) {
michael@0 167 aController->NotifyCompletion(NS_LITERAL_STRING(ERR_ALREADY_CONNECTED));
michael@0 168 return;
michael@0 169 }
michael@0 170
michael@0 171 mDeviceAddress = aDeviceAddress;
michael@0 172 mController = aController;
michael@0 173
michael@0 174 if (NS_FAILED(bs->SendSinkMessage(aDeviceAddress,
michael@0 175 NS_LITERAL_STRING("Connect")))) {
michael@0 176 aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
michael@0 177 return;
michael@0 178 }
michael@0 179 }
michael@0 180
michael@0 181 void
michael@0 182 BluetoothA2dpManager::Disconnect(BluetoothProfileController* aController)
michael@0 183 {
michael@0 184 BluetoothService* bs = BluetoothService::Get();
michael@0 185 if (!bs) {
michael@0 186 if (aController) {
michael@0 187 aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
michael@0 188 }
michael@0 189 return;
michael@0 190 }
michael@0 191
michael@0 192 if (!mA2dpConnected) {
michael@0 193 if (aController) {
michael@0 194 aController->NotifyCompletion(NS_LITERAL_STRING(ERR_ALREADY_DISCONNECTED));
michael@0 195 }
michael@0 196 return;
michael@0 197 }
michael@0 198
michael@0 199 MOZ_ASSERT(!mDeviceAddress.IsEmpty());
michael@0 200 MOZ_ASSERT(!mController);
michael@0 201
michael@0 202 mController = aController;
michael@0 203
michael@0 204 if (NS_FAILED(bs->SendSinkMessage(mDeviceAddress,
michael@0 205 NS_LITERAL_STRING("Disconnect")))) {
michael@0 206 aController->NotifyCompletion(NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
michael@0 207 return;
michael@0 208 }
michael@0 209 }
michael@0 210
michael@0 211 void
michael@0 212 BluetoothA2dpManager::OnConnect(const nsAString& aErrorStr)
michael@0 213 {
michael@0 214 MOZ_ASSERT(NS_IsMainThread());
michael@0 215
michael@0 216 /**
michael@0 217 * On the one hand, notify the controller that we've done for outbound
michael@0 218 * connections. On the other hand, we do nothing for inbound connections.
michael@0 219 */
michael@0 220 NS_ENSURE_TRUE_VOID(mController);
michael@0 221
michael@0 222 nsRefPtr<BluetoothProfileController> controller = mController.forget();
michael@0 223 controller->NotifyCompletion(aErrorStr);
michael@0 224 }
michael@0 225
michael@0 226 void
michael@0 227 BluetoothA2dpManager::OnDisconnect(const nsAString& aErrorStr)
michael@0 228 {
michael@0 229 MOZ_ASSERT(NS_IsMainThread());
michael@0 230
michael@0 231 /**
michael@0 232 * On the one hand, notify the controller that we've done for outbound
michael@0 233 * connections. On the other hand, we do nothing for inbound connections.
michael@0 234 */
michael@0 235 NS_ENSURE_TRUE_VOID(mController);
michael@0 236
michael@0 237 nsRefPtr<BluetoothProfileController> controller = mController.forget();
michael@0 238 controller->NotifyCompletion(aErrorStr);
michael@0 239
michael@0 240 Reset();
michael@0 241 }
michael@0 242
michael@0 243 /* HandleSinkPropertyChanged update sink state in A2dp
michael@0 244 *
michael@0 245 * Possible values: "disconnected", "connecting", "connected", "playing"
michael@0 246 *
michael@0 247 * 1. "disconnected" -> "connecting"
michael@0 248 * Either an incoming or outgoing connection attempt ongoing
michael@0 249 * 2. "connecting" -> "disconnected"
michael@0 250 * Connection attempt failed
michael@0 251 * 3. "connecting" -> "connected"
michael@0 252 * Successfully connected
michael@0 253 * 4. "connected" -> "playing"
michael@0 254 * Audio stream active
michael@0 255 * 5. "playing" -> "connected"
michael@0 256 * Audio stream suspended
michael@0 257 * 6. "connected" -> "disconnected"
michael@0 258 * "playing" -> "disconnected"
michael@0 259 * Disconnected from local or the remote device
michael@0 260 */
michael@0 261 void
michael@0 262 BluetoothA2dpManager::HandleSinkPropertyChanged(const BluetoothSignal& aSignal)
michael@0 263 {
michael@0 264 MOZ_ASSERT(NS_IsMainThread());
michael@0 265 MOZ_ASSERT(aSignal.value().type() == BluetoothValue::TArrayOfBluetoothNamedValue);
michael@0 266
michael@0 267 const nsString& address = aSignal.path();
michael@0 268 /**
michael@0 269 * Update sink property only if
michael@0 270 * - mDeviceAddress is empty (A2dp is disconnected), or
michael@0 271 * - this property change is from the connected sink.
michael@0 272 */
michael@0 273 NS_ENSURE_TRUE_VOID(mDeviceAddress.IsEmpty() ||
michael@0 274 mDeviceAddress.Equals(address));
michael@0 275
michael@0 276 const InfallibleTArray<BluetoothNamedValue>& arr =
michael@0 277 aSignal.value().get_ArrayOfBluetoothNamedValue();
michael@0 278 MOZ_ASSERT(arr.Length() == 1);
michael@0 279
michael@0 280 /**
michael@0 281 * There are three properties:
michael@0 282 * - "State": a string
michael@0 283 * - "Connected": a boolean value
michael@0 284 * - "Playing": a boolean value
michael@0 285 *
michael@0 286 * Note that only "State" is handled in this function.
michael@0 287 */
michael@0 288
michael@0 289 const nsString& name = arr[0].name();
michael@0 290 NS_ENSURE_TRUE_VOID(name.EqualsLiteral("State"));
michael@0 291
michael@0 292 const BluetoothValue& value = arr[0].value();
michael@0 293 MOZ_ASSERT(value.type() == BluetoothValue::TnsString);
michael@0 294 SinkState newState = StatusStringToSinkState(value.get_nsString());
michael@0 295 NS_ENSURE_TRUE_VOID((newState != SinkState::SINK_UNKNOWN) &&
michael@0 296 (newState != mSinkState));
michael@0 297
michael@0 298 /**
michael@0 299 * Reject 'connected' state change if bluetooth is already disabled.
michael@0 300 * Sink state would be reset to 'disconnected' when bluetooth is disabled.
michael@0 301 *
michael@0 302 * See bug 984284 for more information about the edge case.
michael@0 303 */
michael@0 304 NS_ENSURE_FALSE_VOID(newState == SinkState::SINK_CONNECTED &&
michael@0 305 mSinkState == SinkState::SINK_DISCONNECTED);
michael@0 306
michael@0 307 SinkState prevState = mSinkState;
michael@0 308 mSinkState = newState;
michael@0 309
michael@0 310 switch(mSinkState) {
michael@0 311 case SinkState::SINK_CONNECTING:
michael@0 312 // case 1: Either an incoming or outgoing connection attempt ongoing
michael@0 313 MOZ_ASSERT(prevState == SinkState::SINK_DISCONNECTED);
michael@0 314 break;
michael@0 315 case SinkState::SINK_PLAYING:
michael@0 316 // case 4: Audio stream active
michael@0 317 MOZ_ASSERT(prevState == SinkState::SINK_CONNECTED);
michael@0 318 break;
michael@0 319 case SinkState::SINK_CONNECTED:
michael@0 320 // case 5: Audio stream suspended
michael@0 321 if (prevState == SinkState::SINK_PLAYING) {
michael@0 322 break;
michael@0 323 }
michael@0 324
michael@0 325 // case 3: Successfully connected
michael@0 326 MOZ_ASSERT(prevState == SinkState::SINK_CONNECTING);
michael@0 327
michael@0 328 mA2dpConnected = true;
michael@0 329 mDeviceAddress = address;
michael@0 330 NotifyConnectionStatusChanged();
michael@0 331
michael@0 332 OnConnect(EmptyString());
michael@0 333 break;
michael@0 334 case SinkState::SINK_DISCONNECTED:
michael@0 335 // case 2: Connection attempt failed
michael@0 336 if (prevState == SinkState::SINK_CONNECTING) {
michael@0 337 OnConnect(NS_LITERAL_STRING(ERR_CONNECTION_FAILED));
michael@0 338 break;
michael@0 339 }
michael@0 340
michael@0 341 // case 6: Disconnected from the remote device
michael@0 342 MOZ_ASSERT(prevState == SinkState::SINK_CONNECTED ||
michael@0 343 prevState == SinkState::SINK_PLAYING);
michael@0 344
michael@0 345 mA2dpConnected = false;
michael@0 346 NotifyConnectionStatusChanged();
michael@0 347 mDeviceAddress.Truncate();
michael@0 348 OnDisconnect(EmptyString());
michael@0 349 break;
michael@0 350 default:
michael@0 351 break;
michael@0 352 }
michael@0 353 }
michael@0 354
michael@0 355 void
michael@0 356 BluetoothA2dpManager::NotifyConnectionStatusChanged()
michael@0 357 {
michael@0 358 MOZ_ASSERT(NS_IsMainThread());
michael@0 359
michael@0 360 // Notify Gecko observers
michael@0 361 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
michael@0 362 NS_ENSURE_TRUE_VOID(obs);
michael@0 363
michael@0 364 if (NS_FAILED(obs->NotifyObservers(this,
michael@0 365 BLUETOOTH_A2DP_STATUS_CHANGED_ID,
michael@0 366 mDeviceAddress.get()))) {
michael@0 367 BT_WARNING("Failed to notify bluetooth-a2dp-status-changed observsers!");
michael@0 368 }
michael@0 369
michael@0 370 // Dispatch an event of status change
michael@0 371 DispatchStatusChangedEvent(
michael@0 372 NS_LITERAL_STRING(A2DP_STATUS_CHANGED_ID), mDeviceAddress, mA2dpConnected);
michael@0 373 }
michael@0 374
michael@0 375 void
michael@0 376 BluetoothA2dpManager::OnGetServiceChannel(const nsAString& aDeviceAddress,
michael@0 377 const nsAString& aServiceUuid,
michael@0 378 int aChannel)
michael@0 379 {
michael@0 380 }
michael@0 381
michael@0 382 void
michael@0 383 BluetoothA2dpManager::OnUpdateSdpRecords(const nsAString& aDeviceAddress)
michael@0 384 {
michael@0 385 }
michael@0 386
michael@0 387 void
michael@0 388 BluetoothA2dpManager::GetAddress(nsAString& aDeviceAddress)
michael@0 389 {
michael@0 390 aDeviceAddress = mDeviceAddress;
michael@0 391 }
michael@0 392
michael@0 393 bool
michael@0 394 BluetoothA2dpManager::IsConnected()
michael@0 395 {
michael@0 396 return mA2dpConnected;
michael@0 397 }
michael@0 398
michael@0 399 void
michael@0 400 BluetoothA2dpManager::SetAvrcpConnected(bool aConnected)
michael@0 401 {
michael@0 402 mAvrcpConnected = aConnected;
michael@0 403 if (!aConnected) {
michael@0 404 ResetAvrcp();
michael@0 405 }
michael@0 406 }
michael@0 407
michael@0 408 bool
michael@0 409 BluetoothA2dpManager::IsAvrcpConnected()
michael@0 410 {
michael@0 411 return mAvrcpConnected;
michael@0 412 }
michael@0 413
michael@0 414 void
michael@0 415 BluetoothA2dpManager::UpdateMetaData(const nsAString& aTitle,
michael@0 416 const nsAString& aArtist,
michael@0 417 const nsAString& aAlbum,
michael@0 418 uint64_t aMediaNumber,
michael@0 419 uint64_t aTotalMediaCount,
michael@0 420 uint32_t aDuration)
michael@0 421 {
michael@0 422 mTitle.Assign(aTitle);
michael@0 423 mArtist.Assign(aArtist);
michael@0 424 mAlbum.Assign(aAlbum);
michael@0 425 mMediaNumber = aMediaNumber;
michael@0 426 mTotalMediaCount = aTotalMediaCount;
michael@0 427 mDuration = aDuration;
michael@0 428 }
michael@0 429
michael@0 430 void
michael@0 431 BluetoothA2dpManager::UpdatePlayStatus(uint32_t aDuration,
michael@0 432 uint32_t aPosition,
michael@0 433 ControlPlayStatus aPlayStatus)
michael@0 434 {
michael@0 435 mDuration = aDuration;
michael@0 436 mPosition = aPosition;
michael@0 437 mPlayStatus = aPlayStatus;
michael@0 438 }
michael@0 439
michael@0 440 void
michael@0 441 BluetoothA2dpManager::GetAlbum(nsAString& aAlbum)
michael@0 442 {
michael@0 443 aAlbum.Assign(mAlbum);
michael@0 444 }
michael@0 445
michael@0 446 uint32_t
michael@0 447 BluetoothA2dpManager::GetDuration()
michael@0 448 {
michael@0 449 return mDuration;
michael@0 450 }
michael@0 451
michael@0 452 ControlPlayStatus
michael@0 453 BluetoothA2dpManager::GetPlayStatus()
michael@0 454 {
michael@0 455 return mPlayStatus;
michael@0 456 }
michael@0 457
michael@0 458 uint32_t
michael@0 459 BluetoothA2dpManager::GetPosition()
michael@0 460 {
michael@0 461 return mPosition;
michael@0 462 }
michael@0 463
michael@0 464 uint64_t
michael@0 465 BluetoothA2dpManager::GetMediaNumber()
michael@0 466 {
michael@0 467 return mMediaNumber;
michael@0 468 }
michael@0 469
michael@0 470 void
michael@0 471 BluetoothA2dpManager::GetTitle(nsAString& aTitle)
michael@0 472 {
michael@0 473 aTitle.Assign(mTitle);
michael@0 474 }
michael@0 475
michael@0 476 NS_IMPL_ISUPPORTS(BluetoothA2dpManager, nsIObserver)
michael@0 477

mercurial