Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Side by Side Diff: device/bluetooth/bluetooth_audio_sink_chromeos.cc

Issue 1415573014: Reland "Add Linux support for the Bluetooth API" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: build fix. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h"
6
7 #include <unistd.h>
8
9 #include <algorithm>
10 #include <sstream>
11 #include <string>
12 #include <vector>
13
14 #include "base/debug/stack_trace.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "dbus/message.h"
18 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
19 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
20
21 using dbus::ObjectPath;
22 using device::BluetoothAudioSink;
23
24 namespace {
25
26 // TODO(mcchou): Add the constant to dbus/service_constants.h.
27 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink";
28
29 const int kInvalidFd = -1;
30 const uint16_t kInvalidReadMtu = 0;
31 const uint16_t kInvalidWriteMtu = 0;
32
33 ObjectPath GenerateEndpointPath() {
34 static unsigned int sequence_number = 0;
35 ++sequence_number;
36 std::stringstream path;
37 path << kBluetoothAudioSinkServicePath << "/endpoint" << sequence_number;
38 return ObjectPath(path.str());
39 }
40
41 std::string StateToString(const BluetoothAudioSink::State& state) {
42 switch (state) {
43 case BluetoothAudioSink::STATE_INVALID:
44 return "invalid";
45 case BluetoothAudioSink::STATE_DISCONNECTED:
46 return "disconnected";
47 case BluetoothAudioSink::STATE_IDLE:
48 return "idle";
49 case BluetoothAudioSink::STATE_PENDING:
50 return "pending";
51 case BluetoothAudioSink::STATE_ACTIVE:
52 return "active";
53 default:
54 return "unknown";
55 }
56 }
57
58 std::string ErrorCodeToString(const BluetoothAudioSink::ErrorCode& error_code) {
59 switch (error_code) {
60 case BluetoothAudioSink::ERROR_UNSUPPORTED_PLATFORM:
61 return "unsupported platform";
62 case BluetoothAudioSink::ERROR_INVALID_ADAPTER:
63 return "invalid adapter";
64 case BluetoothAudioSink::ERROR_NOT_REGISTERED:
65 return "not registered";
66 case BluetoothAudioSink::ERROR_NOT_UNREGISTERED:
67 return "not unregistered";
68 default:
69 return "unknown";
70 }
71 }
72
73 // A dummy error callback for calling Unregister() in destructor.
74 void UnregisterErrorCallback(
75 device::BluetoothAudioSink::ErrorCode error_code) {
76 VLOG(1) << "UnregisterErrorCallback - " << ErrorCodeToString(error_code)
77 << "(" << error_code << ")";
78 }
79
80 } // namespace
81
82 namespace chromeos {
83
84 BluetoothAudioSinkChromeOS::BluetoothAudioSinkChromeOS(
85 scoped_refptr<device::BluetoothAdapter> adapter)
86 : state_(BluetoothAudioSink::STATE_INVALID),
87 volume_(BluetoothAudioSink::kInvalidVolume),
88 read_mtu_(kInvalidReadMtu),
89 write_mtu_(kInvalidWriteMtu),
90 read_has_failed_(false),
91 adapter_(adapter),
92 weak_ptr_factory_(this) {
93 VLOG(1) << "BluetoothAudioSinkChromeOS created";
94
95 CHECK(adapter_.get());
96 CHECK(adapter_->IsPresent());
97 CHECK(bluez::BluezDBusManager::IsInitialized());
98
99 adapter_->AddObserver(this);
100
101 bluez::BluetoothMediaClient* media =
102 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient();
103 CHECK(media);
104 media->AddObserver(this);
105
106 bluez::BluetoothMediaTransportClient* transport =
107 bluez::BluezDBusManager::Get()->GetBluetoothMediaTransportClient();
108 CHECK(transport);
109 transport->AddObserver(this);
110
111 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED);
112 }
113
114 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() {
115 VLOG(1) << "BluetoothAudioSinkChromeOS destroyed";
116
117 DCHECK(adapter_.get());
118
119 if (state_ != BluetoothAudioSink::STATE_INVALID && media_endpoint_.get()) {
120 Unregister(base::Bind(&base::DoNothing),
121 base::Bind(&UnregisterErrorCallback));
122 }
123
124 adapter_->RemoveObserver(this);
125
126 bluez::BluetoothMediaClient* media =
127 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient();
128 CHECK(media);
129 media->RemoveObserver(this);
130
131 bluez::BluetoothMediaTransportClient* transport =
132 bluez::BluezDBusManager::Get()->GetBluetoothMediaTransportClient();
133 CHECK(transport);
134 transport->RemoveObserver(this);
135 }
136
137 void BluetoothAudioSinkChromeOS::Unregister(
138 const base::Closure& callback,
139 const device::BluetoothAudioSink::ErrorCallback& error_callback) {
140 VLOG(1) << "Unregister";
141
142 if (!bluez::BluezDBusManager::IsInitialized())
143 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED);
144
145 bluez::BluetoothMediaClient* media =
146 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient();
147 CHECK(media);
148
149 media->UnregisterEndpoint(
150 media_path_,
151 endpoint_path_,
152 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterSucceeded,
153 weak_ptr_factory_.GetWeakPtr(), callback),
154 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterFailed,
155 weak_ptr_factory_.GetWeakPtr(), error_callback));
156 }
157
158 void BluetoothAudioSinkChromeOS::AddObserver(
159 BluetoothAudioSink::Observer* observer) {
160 CHECK(observer);
161 observers_.AddObserver(observer);
162 }
163
164 void BluetoothAudioSinkChromeOS::RemoveObserver(
165 BluetoothAudioSink::Observer* observer) {
166 CHECK(observer);
167 observers_.RemoveObserver(observer);
168 }
169
170 BluetoothAudioSink::State BluetoothAudioSinkChromeOS::GetState() const {
171 return state_;
172 }
173
174 uint16_t BluetoothAudioSinkChromeOS::GetVolume() const {
175 return volume_;
176 }
177
178 void BluetoothAudioSinkChromeOS::Register(
179 const BluetoothAudioSink::Options& options,
180 const base::Closure& callback,
181 const BluetoothAudioSink::ErrorCallback& error_callback) {
182 VLOG(1) << "Register";
183
184 DCHECK(adapter_.get());
185 DCHECK_EQ(state_, BluetoothAudioSink::STATE_DISCONNECTED);
186
187 // Gets system bus.
188 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus();
189
190 // Creates a Media Endpoint with newly-generated path.
191 endpoint_path_ = GenerateEndpointPath();
192 media_endpoint_.reset(bluez::BluetoothMediaEndpointServiceProvider::Create(
193 system_bus, endpoint_path_, this));
194
195 DCHECK(media_endpoint_.get());
196
197 // Creates endpoint properties with |options|.
198 options_ = options;
199 bluez::BluetoothMediaClient::EndpointProperties endpoint_properties;
200 endpoint_properties.uuid =
201 bluez::BluetoothMediaClient::kBluetoothAudioSinkUUID;
202 endpoint_properties.codec = options_.codec;
203 endpoint_properties.capabilities = options_.capabilities;
204
205 media_path_ = static_cast<BluetoothAdapterChromeOS*>(
206 adapter_.get())->object_path();
207
208 bluez::BluetoothMediaClient* media =
209 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient();
210 CHECK(media);
211 media->RegisterEndpoint(
212 media_path_,
213 endpoint_path_,
214 endpoint_properties,
215 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterSucceeded,
216 weak_ptr_factory_.GetWeakPtr(), callback),
217 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterFailed,
218 weak_ptr_factory_.GetWeakPtr(), error_callback));
219 }
220
221 bluez::BluetoothMediaEndpointServiceProvider*
222 BluetoothAudioSinkChromeOS::GetEndpointServiceProvider() {
223 return media_endpoint_.get();
224 }
225
226 void BluetoothAudioSinkChromeOS::AdapterPresentChanged(
227 device::BluetoothAdapter* adapter, bool present) {
228 VLOG(1) << "AdapterPresentChanged: " << present;
229
230 if (adapter != adapter_.get())
231 return;
232
233 if (adapter->IsPresent()) {
234 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED);
235 } else {
236 adapter_->RemoveObserver(this);
237 StateChanged(BluetoothAudioSink::STATE_INVALID);
238 }
239 }
240
241 void BluetoothAudioSinkChromeOS::AdapterPoweredChanged(
242 device::BluetoothAdapter* adapter, bool powered) {
243 VLOG(1) << "AdapterPoweredChanged: " << powered;
244
245 if (adapter != adapter_.get())
246 return;
247
248 // Regardless of the new powered state, |state_| goes to STATE_DISCONNECTED.
249 // If false, the transport is closed, but the endpoint is still valid for use.
250 // If true, the previous transport has been torn down, so the |state_| has to
251 // be disconnected before SetConfigruation is called.
252 if (state_ != BluetoothAudioSink::STATE_INVALID)
253 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED);
254 }
255
256 void BluetoothAudioSinkChromeOS::MediaRemoved(const ObjectPath& object_path) {
257 if (object_path == media_path_) {
258 VLOG(1) << "MediaRemoved: " << object_path.value();
259 StateChanged(BluetoothAudioSink::STATE_INVALID);
260 }
261 }
262
263 void BluetoothAudioSinkChromeOS::MediaTransportRemoved(
264 const ObjectPath& object_path) {
265 // Whenever powered of |adapter_| turns false while present stays true, media
266 // transport object should be removed accordingly, and the state should be
267 // changed to STATE_DISCONNECTED.
268 if (object_path == transport_path_) {
269 VLOG(1) << "MediaTransportRemoved: " << object_path.value();
270 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED);
271 }
272 }
273
274 void BluetoothAudioSinkChromeOS::MediaTransportPropertyChanged(
275 const ObjectPath& object_path,
276 const std::string& property_name) {
277 if (object_path != transport_path_)
278 return;
279
280 VLOG(1) << "MediaTransportPropertyChanged: " << property_name;
281
282 // Retrieves the property set of the transport object with |object_path|.
283 bluez::BluetoothMediaTransportClient::Properties* properties =
284 bluez::BluezDBusManager::Get()
285 ->GetBluetoothMediaTransportClient()
286 ->GetProperties(object_path);
287
288 // Dispatches a property changed event to the corresponding handler.
289 if (property_name == properties->state.name()) {
290 if (properties->state.value() ==
291 bluez::BluetoothMediaTransportClient::kStateIdle) {
292 StateChanged(BluetoothAudioSink::STATE_IDLE);
293 } else if (properties->state.value() ==
294 bluez::BluetoothMediaTransportClient::kStatePending) {
295 StateChanged(BluetoothAudioSink::STATE_PENDING);
296 } else if (properties->state.value() ==
297 bluez::BluetoothMediaTransportClient::kStateActive) {
298 StateChanged(BluetoothAudioSink::STATE_ACTIVE);
299 }
300 } else if (property_name == properties->volume.name()) {
301 VolumeChanged(properties->volume.value());
302 }
303 }
304
305 void BluetoothAudioSinkChromeOS::SetConfiguration(
306 const ObjectPath& transport_path,
307 const TransportProperties& properties) {
308 VLOG(1) << "SetConfiguration";
309 transport_path_ = transport_path;
310
311 // The initial state for a connection should be "idle".
312 if (properties.state != bluez::BluetoothMediaTransportClient::kStateIdle) {
313 VLOG(1) << "SetConfiugration - unexpected state :" << properties.state;
314 return;
315 }
316
317 // Updates |volume_| if the volume level is provided in |properties|.
318 if (properties.volume.get()) {
319 VolumeChanged(*properties.volume);
320 }
321
322 StateChanged(BluetoothAudioSink::STATE_IDLE);
323 }
324
325 void BluetoothAudioSinkChromeOS::SelectConfiguration(
326 const std::vector<uint8_t>& capabilities,
327 const SelectConfigurationCallback& callback) {
328 VLOG(1) << "SelectConfiguration";
329 callback.Run(options_.capabilities);
330 }
331
332 void BluetoothAudioSinkChromeOS::ClearConfiguration(
333 const ObjectPath& transport_path) {
334 if (transport_path != transport_path_)
335 return;
336
337 VLOG(1) << "ClearConfiguration";
338 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED);
339 }
340
341 void BluetoothAudioSinkChromeOS::Released() {
342 VLOG(1) << "Released";
343 StateChanged(BluetoothAudioSink::STATE_INVALID);
344 }
345
346 void BluetoothAudioSinkChromeOS::OnFileCanReadWithoutBlocking(int fd) {
347 ReadFromFile();
348 }
349
350 void BluetoothAudioSinkChromeOS::OnFileCanWriteWithoutBlocking(int fd) {
351 // Do nothing for now.
352 }
353
354 void BluetoothAudioSinkChromeOS::AcquireFD() {
355 VLOG(1) << "AcquireFD - transport path: " << transport_path_.value();
356
357 read_has_failed_ = false;
358
359 bluez::BluezDBusManager::Get()->GetBluetoothMediaTransportClient()->Acquire(
360 transport_path_,
361 base::Bind(&BluetoothAudioSinkChromeOS::OnAcquireSucceeded,
362 weak_ptr_factory_.GetWeakPtr()),
363 base::Bind(&BluetoothAudioSinkChromeOS::OnAcquireFailed,
364 weak_ptr_factory_.GetWeakPtr()));
365 }
366
367 void BluetoothAudioSinkChromeOS::WatchFD() {
368 CHECK(file_.get() && file_->IsValid());
369
370 VLOG(1) << "WatchFD - file: " << file_->GetPlatformFile()
371 << ", file validity: " << file_->IsValid();
372
373 base::MessageLoopForIO::current()->WatchFileDescriptor(
374 file_->GetPlatformFile(), true, base::MessageLoopForIO::WATCH_READ,
375 &fd_read_watcher_, this);
376 }
377
378 void BluetoothAudioSinkChromeOS::StopWatchingFD() {
379 if (!file_.get()) {
380 VLOG(1) << "StopWatchingFD - skip";
381 return;
382 }
383
384 bool stopped = fd_read_watcher_.StopWatchingFileDescriptor();
385 VLOG(1) << "StopWatchingFD - watch stopped: " << stopped;
386 CHECK(stopped);
387
388 read_mtu_ = kInvalidReadMtu;
389 write_mtu_ = kInvalidWriteMtu;
390 file_.reset(); // This will close the file descriptor.
391 }
392
393 void BluetoothAudioSinkChromeOS::ReadFromFile() {
394 DCHECK(file_.get() && file_->IsValid());
395 DCHECK(data_.get());
396
397 int size = file_->ReadAtCurrentPosNoBestEffort(data_.get(), read_mtu_);
398
399 if (size == -1) {
400 // To reduce the number of logs, log only once for multiple failures.
401 if (!read_has_failed_) {
402 VLOG(1) << "ReadFromFile - failed";
403 read_has_failed_ = true;
404 }
405 return;
406 }
407
408 VLOG(1) << "ReadFromFile - read " << size << " bytes";
409 FOR_EACH_OBSERVER(
410 BluetoothAudioSink::Observer, observers_,
411 BluetoothAudioSinkDataAvailable(this, data_.get(), size, read_mtu_));
412 }
413
414 void BluetoothAudioSinkChromeOS::StateChanged(
415 BluetoothAudioSink::State state) {
416 if (state == state_)
417 return;
418
419 VLOG(1) << "StateChanged - state: " << StateToString(state);
420
421 switch (state) {
422 case BluetoothAudioSink::STATE_INVALID:
423 ResetMedia();
424 ResetEndpoint();
425 case BluetoothAudioSink::STATE_DISCONNECTED:
426 ResetTransport();
427 break;
428 case BluetoothAudioSink::STATE_IDLE:
429 StopWatchingFD();
430 break;
431 case BluetoothAudioSink::STATE_PENDING:
432 AcquireFD();
433 break;
434 case BluetoothAudioSink::STATE_ACTIVE:
435 WatchFD();
436 break;
437 default:
438 break;
439 }
440
441 state_ = state;
442 FOR_EACH_OBSERVER(BluetoothAudioSink::Observer, observers_,
443 BluetoothAudioSinkStateChanged(this, state_));
444 }
445
446 void BluetoothAudioSinkChromeOS::VolumeChanged(uint16_t volume) {
447 if (volume == volume_)
448 return;
449
450 VLOG(1) << "VolumeChanged: " << volume;
451
452 volume_ = std::min(volume, BluetoothAudioSink::kInvalidVolume);
453 FOR_EACH_OBSERVER(BluetoothAudioSink::Observer, observers_,
454 BluetoothAudioSinkVolumeChanged(this, volume_));
455 }
456
457 void BluetoothAudioSinkChromeOS::OnRegisterSucceeded(
458 const base::Closure& callback) {
459 DCHECK(media_endpoint_.get());
460 VLOG(1) << "OnRegisterSucceeded";
461
462 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED);
463 callback.Run();
464 }
465
466 void BluetoothAudioSinkChromeOS::OnRegisterFailed(
467 const BluetoothAudioSink::ErrorCallback& error_callback,
468 const std::string& error_name,
469 const std::string& error_message) {
470 VLOG(1) << "OnRegisterFailed - error name: " << error_name
471 << ", error message: " << error_message;
472
473 ResetEndpoint();
474 error_callback.Run(BluetoothAudioSink::ERROR_NOT_REGISTERED);
475 }
476
477 void BluetoothAudioSinkChromeOS::OnUnregisterSucceeded(
478 const base::Closure& callback) {
479 VLOG(1) << "Unregistered - endpoint: " << endpoint_path_.value();
480
481 // Once the state becomes STATE_INVALID, media, media transport and media
482 // endpoint will be reset.
483 StateChanged(BluetoothAudioSink::STATE_INVALID);
484 callback.Run();
485 }
486
487 void BluetoothAudioSinkChromeOS::OnUnregisterFailed(
488 const device::BluetoothAudioSink::ErrorCallback& error_callback,
489 const std::string& error_name,
490 const std::string& error_message) {
491 VLOG(1) << "OnUnregisterFailed - error name: " << error_name
492 << ", error message: " << error_message;
493
494 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED);
495 }
496
497 void BluetoothAudioSinkChromeOS::OnAcquireSucceeded(
498 dbus::FileDescriptor* fd,
499 const uint16_t read_mtu,
500 const uint16_t write_mtu) {
501 CHECK(fd);
502 fd->CheckValidity();
503 CHECK(fd->is_valid() && fd->value() != kInvalidFd);
504 CHECK_GT(read_mtu, kInvalidReadMtu);
505 CHECK_GT(write_mtu, kInvalidWriteMtu);
506
507 // Avoids unnecessary memory reallocation if read MTU doesn't change.
508 if (read_mtu != read_mtu_) {
509 read_mtu_ = read_mtu;
510 data_.reset(new char[read_mtu_]);
511 VLOG(1) << "OnAcquireSucceeded - allocate " << read_mtu_
512 << " bytes of memory";
513 }
514
515 write_mtu_ = write_mtu;
516
517 // Avoids closing the same file descriptor caused by reassignment.
518 if (!file_.get() || file_->GetPlatformFile() != fd->value()) {
519 // Takes ownership of the file descriptor.
520 file_.reset(new base::File(fd->TakeValue()));
521 DCHECK(file_->IsValid());
522 VLOG(1) << "OnAcquireSucceeded - update file";
523 }
524
525 VLOG(1) << "OnAcquireSucceeded - file: " << file_->GetPlatformFile()
526 << ", read MTU: " << read_mtu_ << ", write MTU: " << write_mtu_;
527 }
528
529 void BluetoothAudioSinkChromeOS::OnAcquireFailed(
530 const std::string& error_name,
531 const std::string& error_message) {
532 VLOG(1) << "OnAcquireFailed - error name: " << error_name
533 << ", error message: " << error_message;
534 }
535
536 void BluetoothAudioSinkChromeOS::OnReleaseFDSucceeded() {
537 VLOG(1) << "OnReleaseFDSucceeded";
538 }
539
540 void BluetoothAudioSinkChromeOS::OnReleaseFDFailed(
541 const std::string& error_name,
542 const std::string& error_message) {
543 VLOG(1) << "OnReleaseFDFailed - error name: " << error_name
544 << ", error message: " << error_message;
545 }
546
547 void BluetoothAudioSinkChromeOS::ResetMedia() {
548 VLOG(1) << "ResetMedia";
549
550 media_path_ = dbus::ObjectPath("");
551 }
552
553 void BluetoothAudioSinkChromeOS::ResetTransport() {
554 if (!transport_path_.IsValid()) {
555 VLOG(1) << "ResetTransport - skip";
556 return;
557 }
558
559 VLOG(1) << "ResetTransport - clean-up";
560
561 VolumeChanged(BluetoothAudioSink::kInvalidVolume);
562 transport_path_ = dbus::ObjectPath("");
563 read_mtu_ = kInvalidReadMtu;
564 write_mtu_ = kInvalidWriteMtu;
565 file_.reset();
566 }
567
568 void BluetoothAudioSinkChromeOS::ResetEndpoint() {
569 VLOG(1) << "ResetEndpoint";
570
571 endpoint_path_ = ObjectPath("");
572 media_endpoint_ = nullptr;
573 }
574
575 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698