OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/bluez/bluetooth_audio_sink_bluez.h" | 5 #include "device/bluetooth/bluez/bluetooth_audio_sink_bluez.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <sstream> | 10 #include <sstream> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/debug/stack_trace.h" | 14 #include "base/debug/stack_trace.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "dbus/message.h" | 17 #include "dbus/message.h" |
18 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" | 18 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
19 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 19 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
20 | 20 |
21 using dbus::ObjectPath; | 21 using dbus::ObjectPath; |
22 using device::BluetoothAudioSink; | 22 using device::BluetoothAudioSink; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // TODO(mcchou): Add the constant to dbus/service_constants.h. | 26 // TODO(mcchou): Add the constant to dbus/service_constants.h. |
27 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink"; | 27 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink"; |
28 | 28 |
29 const int kInvalidFd = -1; | |
30 const uint16_t kInvalidReadMtu = 0; | 29 const uint16_t kInvalidReadMtu = 0; |
31 const uint16_t kInvalidWriteMtu = 0; | 30 const uint16_t kInvalidWriteMtu = 0; |
32 | 31 |
33 ObjectPath GenerateEndpointPath() { | 32 ObjectPath GenerateEndpointPath() { |
34 static unsigned int sequence_number = 0; | 33 static unsigned int sequence_number = 0; |
35 ++sequence_number; | 34 ++sequence_number; |
36 std::stringstream path; | 35 std::stringstream path; |
37 path << kBluetoothAudioSinkServicePath << "/endpoint" << sequence_number; | 36 path << kBluetoothAudioSinkServicePath << "/endpoint" << sequence_number; |
38 return ObjectPath(path.str()); | 37 return ObjectPath(path.str()); |
39 } | 38 } |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 void BluetoothAudioSinkBlueZ::OnUnregisterFailed( | 482 void BluetoothAudioSinkBlueZ::OnUnregisterFailed( |
484 const device::BluetoothAudioSink::ErrorCallback& error_callback, | 483 const device::BluetoothAudioSink::ErrorCallback& error_callback, |
485 const std::string& error_name, | 484 const std::string& error_name, |
486 const std::string& error_message) { | 485 const std::string& error_message) { |
487 VLOG(1) << "OnUnregisterFailed - error name: " << error_name | 486 VLOG(1) << "OnUnregisterFailed - error name: " << error_name |
488 << ", error message: " << error_message; | 487 << ", error message: " << error_message; |
489 | 488 |
490 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED); | 489 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED); |
491 } | 490 } |
492 | 491 |
493 void BluetoothAudioSinkBlueZ::OnAcquireSucceeded(dbus::FileDescriptor* fd, | 492 void BluetoothAudioSinkBlueZ::OnAcquireSucceeded(base::ScopedFD fd, |
494 const uint16_t read_mtu, | 493 const uint16_t read_mtu, |
495 const uint16_t write_mtu) { | 494 const uint16_t write_mtu) { |
496 CHECK(fd); | 495 CHECK(fd.is_valid()); |
497 fd->CheckValidity(); | |
498 CHECK(fd->is_valid() && fd->value() != kInvalidFd); | |
499 CHECK_GT(read_mtu, kInvalidReadMtu); | 496 CHECK_GT(read_mtu, kInvalidReadMtu); |
500 CHECK_GT(write_mtu, kInvalidWriteMtu); | 497 CHECK_GT(write_mtu, kInvalidWriteMtu); |
501 | 498 |
502 // Avoids unnecessary memory reallocation if read MTU doesn't change. | 499 // Avoids unnecessary memory reallocation if read MTU doesn't change. |
503 if (read_mtu != read_mtu_) { | 500 if (read_mtu != read_mtu_) { |
504 read_mtu_ = read_mtu; | 501 read_mtu_ = read_mtu; |
505 data_.reset(new char[read_mtu_]); | 502 data_.reset(new char[read_mtu_]); |
506 VLOG(1) << "OnAcquireSucceeded - allocate " << read_mtu_ | 503 VLOG(1) << "OnAcquireSucceeded - allocate " << read_mtu_ |
507 << " bytes of memory"; | 504 << " bytes of memory"; |
508 } | 505 } |
509 | 506 |
510 write_mtu_ = write_mtu; | 507 write_mtu_ = write_mtu; |
511 | 508 |
512 // Avoids closing the same file descriptor caused by reassignment. | 509 // Avoids closing the same file descriptor caused by reassignment. |
513 if (!file_.get() || file_->GetPlatformFile() != fd->value()) { | 510 if (!file_.get() || file_->GetPlatformFile() != fd.get()) { |
514 // Takes ownership of the file descriptor. | 511 // Takes ownership of the file descriptor. |
515 file_.reset(new base::File(fd->TakeValue())); | 512 file_.reset(new base::File(fd.release())); |
516 DCHECK(file_->IsValid()); | 513 DCHECK(file_->IsValid()); |
517 VLOG(1) << "OnAcquireSucceeded - update file"; | 514 VLOG(1) << "OnAcquireSucceeded - update file"; |
518 } | 515 } |
519 | 516 |
520 VLOG(1) << "OnAcquireSucceeded - file: " << file_->GetPlatformFile() | 517 VLOG(1) << "OnAcquireSucceeded - file: " << file_->GetPlatformFile() |
521 << ", read MTU: " << read_mtu_ << ", write MTU: " << write_mtu_; | 518 << ", read MTU: " << read_mtu_ << ", write MTU: " << write_mtu_; |
522 } | 519 } |
523 | 520 |
524 void BluetoothAudioSinkBlueZ::OnAcquireFailed( | 521 void BluetoothAudioSinkBlueZ::OnAcquireFailed( |
525 const std::string& error_name, | 522 const std::string& error_name, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 } | 558 } |
562 | 559 |
563 void BluetoothAudioSinkBlueZ::ResetEndpoint() { | 560 void BluetoothAudioSinkBlueZ::ResetEndpoint() { |
564 VLOG(1) << "ResetEndpoint"; | 561 VLOG(1) << "ResetEndpoint"; |
565 | 562 |
566 endpoint_path_ = ObjectPath(""); | 563 endpoint_path_ = ObjectPath(""); |
567 media_endpoint_ = nullptr; | 564 media_endpoint_ = nullptr; |
568 } | 565 } |
569 | 566 |
570 } // namespace bluez | 567 } // namespace bluez |
OLD | NEW |