| 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/bluetooth_audio_sink_chromeos.h" | 5 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h" |
| 6 | 6 |
| 7 #include <unistd.h> |
| 8 |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 8 #include <sstream> | 10 #include <sstream> |
| 9 #include <string> | 11 #include <string> |
| 10 #include <vector> | 12 #include <vector> |
| 11 | 13 |
| 12 #include "base/debug/stack_trace.h" | 14 #include "base/debug/stack_trace.h" |
| 15 #include "base/files/file_util.h" |
| 13 #include "base/logging.h" | 16 #include "base/logging.h" |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" | 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 15 #include "dbus/message.h" | 18 #include "dbus/message.h" |
| 16 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 19 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| 17 | 20 |
| 18 using dbus::ObjectPath; | 21 using dbus::ObjectPath; |
| 19 using device::BluetoothAudioSink; | 22 using device::BluetoothAudioSink; |
| 20 | 23 |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 23 // TODO(mcchou): Add the constant to dbus/service_constants.h. | 26 // TODO(mcchou): Add the constant to dbus/service_constants.h. |
| 24 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink"; | 27 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink"; |
| 25 | 28 |
| 29 const int kInvalidFd = -1; |
| 30 const uint16_t kInvalidReadMtu = 0; |
| 31 const uint16_t kInvalidWriteMtu = 0; |
| 32 |
| 26 ObjectPath GenerateEndpointPath() { | 33 ObjectPath GenerateEndpointPath() { |
| 27 static unsigned int sequence_number = 0; | 34 static unsigned int sequence_number = 0; |
| 28 ++sequence_number; | 35 ++sequence_number; |
| 29 std::stringstream path; | 36 std::stringstream path; |
| 30 path << kBluetoothAudioSinkServicePath << "/endpoint" << sequence_number; | 37 path << kBluetoothAudioSinkServicePath << "/endpoint" << sequence_number; |
| 31 return ObjectPath(path.str()); | 38 return ObjectPath(path.str()); |
| 32 } | 39 } |
| 33 | 40 |
| 34 std::string StateToString(const BluetoothAudioSink::State& state) { | 41 std::string StateToString(const BluetoothAudioSink::State& state) { |
| 35 switch (state) { | 42 switch (state) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 } | 78 } |
| 72 | 79 |
| 73 } // namespace | 80 } // namespace |
| 74 | 81 |
| 75 namespace chromeos { | 82 namespace chromeos { |
| 76 | 83 |
| 77 BluetoothAudioSinkChromeOS::BluetoothAudioSinkChromeOS( | 84 BluetoothAudioSinkChromeOS::BluetoothAudioSinkChromeOS( |
| 78 scoped_refptr<device::BluetoothAdapter> adapter) | 85 scoped_refptr<device::BluetoothAdapter> adapter) |
| 79 : state_(BluetoothAudioSink::STATE_INVALID), | 86 : state_(BluetoothAudioSink::STATE_INVALID), |
| 80 volume_(BluetoothAudioSink::kInvalidVolume), | 87 volume_(BluetoothAudioSink::kInvalidVolume), |
| 81 read_mtu_(nullptr), | 88 read_mtu_(kInvalidReadMtu), |
| 82 write_mtu_(nullptr), | 89 write_mtu_(kInvalidWriteMtu), |
| 90 read_has_failed_(false), |
| 83 adapter_(adapter), | 91 adapter_(adapter), |
| 84 weak_ptr_factory_(this) { | 92 weak_ptr_factory_(this) { |
| 85 VLOG(1) << "BluetoothAudioSinkChromeOS created"; | 93 VLOG(1) << "BluetoothAudioSinkChromeOS created"; |
| 86 | 94 |
| 87 DCHECK(adapter_.get()); | 95 CHECK(adapter_.get()); |
| 88 DCHECK(adapter_->IsPresent()); | 96 CHECK(adapter_->IsPresent()); |
| 97 CHECK(DBusThreadManager::IsInitialized()); |
| 89 | 98 |
| 90 adapter_->AddObserver(this); | 99 adapter_->AddObserver(this); |
| 91 | 100 |
| 92 chromeos::BluetoothMediaClient* media = | 101 BluetoothMediaClient* media = |
| 93 DBusThreadManager::Get()->GetBluetoothMediaClient(); | 102 DBusThreadManager::Get()->GetBluetoothMediaClient(); |
| 94 DCHECK(media); | 103 CHECK(media); |
| 95 media->AddObserver(this); | 104 media->AddObserver(this); |
| 96 | 105 |
| 97 chromeos::BluetoothMediaTransportClient *transport = | 106 BluetoothMediaTransportClient* transport = |
| 98 chromeos::DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); | 107 DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); |
| 99 DCHECK(transport); | 108 CHECK(transport); |
| 100 transport->AddObserver(this); | 109 transport->AddObserver(this); |
| 101 | 110 |
| 102 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | 111 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); |
| 103 } | 112 } |
| 104 | 113 |
| 105 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() { | 114 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() { |
| 106 VLOG(1) << "BluetoothAudioSinkChromeOS destroyed"; | 115 VLOG(1) << "BluetoothAudioSinkChromeOS destroyed"; |
| 107 | 116 |
| 108 DCHECK(adapter_.get()); | 117 DCHECK(adapter_.get()); |
| 109 | 118 |
| 110 if (state_ != BluetoothAudioSink::STATE_INVALID && media_endpoint_.get()) { | 119 if (state_ != BluetoothAudioSink::STATE_INVALID && media_endpoint_.get()) { |
| 111 Unregister(base::Bind(&base::DoNothing), | 120 Unregister(base::Bind(&base::DoNothing), |
| 112 base::Bind(&UnregisterErrorCallback)); | 121 base::Bind(&UnregisterErrorCallback)); |
| 113 } | 122 } |
| 114 | 123 |
| 115 adapter_->RemoveObserver(this); | 124 adapter_->RemoveObserver(this); |
| 116 | 125 |
| 117 chromeos::BluetoothMediaClient* media = | 126 BluetoothMediaClient* media = |
| 118 DBusThreadManager::Get()->GetBluetoothMediaClient(); | 127 DBusThreadManager::Get()->GetBluetoothMediaClient(); |
| 119 DCHECK(media); | 128 CHECK(media); |
| 120 media->RemoveObserver(this); | 129 media->RemoveObserver(this); |
| 121 | 130 |
| 122 chromeos::BluetoothMediaTransportClient *transport = | 131 BluetoothMediaTransportClient* transport = |
| 123 chromeos::DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); | 132 DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); |
| 124 DCHECK(transport); | 133 CHECK(transport); |
| 125 transport->RemoveObserver(this); | 134 transport->RemoveObserver(this); |
| 126 } | 135 } |
| 127 | 136 |
| 128 void BluetoothAudioSinkChromeOS::Unregister( | 137 void BluetoothAudioSinkChromeOS::Unregister( |
| 129 const base::Closure& callback, | 138 const base::Closure& callback, |
| 130 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 139 const device::BluetoothAudioSink::ErrorCallback& error_callback) { |
| 131 VLOG(1) << "Unregister"; | 140 VLOG(1) << "Unregister"; |
| 132 | 141 |
| 133 if (!DBusThreadManager::IsInitialized()) | 142 if (!DBusThreadManager::IsInitialized()) |
| 134 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED); | 143 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED); |
| 135 | 144 |
| 136 chromeos::BluetoothMediaClient* media = | 145 BluetoothMediaClient* media = |
| 137 DBusThreadManager::Get()->GetBluetoothMediaClient(); | 146 DBusThreadManager::Get()->GetBluetoothMediaClient(); |
| 138 DCHECK(media); | 147 CHECK(media); |
| 139 | 148 |
| 140 media->UnregisterEndpoint( | 149 media->UnregisterEndpoint( |
| 141 media_path_, | 150 media_path_, |
| 142 endpoint_path_, | 151 endpoint_path_, |
| 143 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterSucceeded, | 152 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterSucceeded, |
| 144 weak_ptr_factory_.GetWeakPtr(), callback), | 153 weak_ptr_factory_.GetWeakPtr(), callback), |
| 145 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterFailed, | 154 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterFailed, |
| 146 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 155 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 147 } | 156 } |
| 148 | 157 |
| 149 void BluetoothAudioSinkChromeOS::AddObserver( | 158 void BluetoothAudioSinkChromeOS::AddObserver( |
| 150 BluetoothAudioSink::Observer* observer) { | 159 BluetoothAudioSink::Observer* observer) { |
| 151 DCHECK(observer); | 160 CHECK(observer); |
| 152 observers_.AddObserver(observer); | 161 observers_.AddObserver(observer); |
| 153 } | 162 } |
| 154 | 163 |
| 155 void BluetoothAudioSinkChromeOS::RemoveObserver( | 164 void BluetoothAudioSinkChromeOS::RemoveObserver( |
| 156 BluetoothAudioSink::Observer* observer) { | 165 BluetoothAudioSink::Observer* observer) { |
| 157 DCHECK(observer); | 166 CHECK(observer); |
| 158 observers_.RemoveObserver(observer); | 167 observers_.RemoveObserver(observer); |
| 159 } | 168 } |
| 160 | 169 |
| 161 BluetoothAudioSink::State BluetoothAudioSinkChromeOS::GetState() const { | 170 BluetoothAudioSink::State BluetoothAudioSinkChromeOS::GetState() const { |
| 162 return state_; | 171 return state_; |
| 163 } | 172 } |
| 164 | 173 |
| 165 uint16_t BluetoothAudioSinkChromeOS::GetVolume() const { | 174 uint16_t BluetoothAudioSinkChromeOS::GetVolume() const { |
| 166 return volume_; | 175 return volume_; |
| 167 } | 176 } |
| 168 | 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 = DBusThreadManager::Get()->GetSystemBus(); |
| 189 |
| 190 // Creates a Media Endpoint with newly-generated path. |
| 191 endpoint_path_ = GenerateEndpointPath(); |
| 192 media_endpoint_.reset( |
| 193 BluetoothMediaEndpointServiceProvider::Create( |
| 194 system_bus, endpoint_path_, this)); |
| 195 |
| 196 DCHECK(media_endpoint_.get()); |
| 197 |
| 198 // Creates endpoint properties with |options|. |
| 199 options_ = options; |
| 200 chromeos::BluetoothMediaClient::EndpointProperties endpoint_properties; |
| 201 endpoint_properties.uuid = 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 BluetoothMediaClient* media = |
| 209 DBusThreadManager::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 BluetoothMediaEndpointServiceProvider* |
| 222 BluetoothAudioSinkChromeOS::GetEndpointServiceProvider() { |
| 223 return media_endpoint_.get(); |
| 224 } |
| 225 |
| 169 void BluetoothAudioSinkChromeOS::AdapterPresentChanged( | 226 void BluetoothAudioSinkChromeOS::AdapterPresentChanged( |
| 170 device::BluetoothAdapter* adapter, bool present) { | 227 device::BluetoothAdapter* adapter, bool present) { |
| 171 VLOG(1) << "AdapterPresentChanged: " << present; | 228 VLOG(1) << "AdapterPresentChanged: " << present; |
| 172 | 229 |
| 173 if (adapter->IsPresent()) { | 230 if (adapter->IsPresent()) { |
| 174 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED); | 231 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED); |
| 175 } else { | 232 } else { |
| 176 adapter_->RemoveObserver(this); | 233 adapter_->RemoveObserver(this); |
| 177 StateChanged(BluetoothAudioSink::STATE_INVALID); | 234 StateChanged(BluetoothAudioSink::STATE_INVALID); |
| 178 } | 235 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 267 |
| 211 void BluetoothAudioSinkChromeOS::MediaTransportPropertyChanged( | 268 void BluetoothAudioSinkChromeOS::MediaTransportPropertyChanged( |
| 212 const ObjectPath& object_path, | 269 const ObjectPath& object_path, |
| 213 const std::string& property_name) { | 270 const std::string& property_name) { |
| 214 if (object_path != transport_path_) | 271 if (object_path != transport_path_) |
| 215 return; | 272 return; |
| 216 | 273 |
| 217 VLOG(1) << "MediaTransportPropertyChanged: " << property_name; | 274 VLOG(1) << "MediaTransportPropertyChanged: " << property_name; |
| 218 | 275 |
| 219 // Retrieves the property set of the transport object with |object_path|. | 276 // Retrieves the property set of the transport object with |object_path|. |
| 220 chromeos::BluetoothMediaTransportClient::Properties* properties = | 277 BluetoothMediaTransportClient::Properties* properties = |
| 221 DBusThreadManager::Get() | 278 DBusThreadManager::Get() |
| 222 ->GetBluetoothMediaTransportClient() | 279 ->GetBluetoothMediaTransportClient() |
| 223 ->GetProperties(object_path); | 280 ->GetProperties(object_path); |
| 224 | 281 |
| 225 // Dispatches a property changed event to the corresponding handler. | 282 // Dispatches a property changed event to the corresponding handler. |
| 226 if (property_name == properties->state.name()) { | 283 if (property_name == properties->state.name()) { |
| 227 if (properties->state.value() == | 284 if (properties->state.value() == |
| 228 BluetoothMediaTransportClient::kStateIdle) { | 285 BluetoothMediaTransportClient::kStateIdle) { |
| 229 StateChanged(BluetoothAudioSink::STATE_IDLE); | 286 StateChanged(BluetoothAudioSink::STATE_IDLE); |
| 230 } else if (properties->state.value() == | 287 } else if (properties->state.value() == |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 const std::vector<uint8_t>& capabilities, | 320 const std::vector<uint8_t>& capabilities, |
| 264 const SelectConfigurationCallback& callback) { | 321 const SelectConfigurationCallback& callback) { |
| 265 VLOG(1) << "SelectConfiguration"; | 322 VLOG(1) << "SelectConfiguration"; |
| 266 callback.Run(options_.capabilities); | 323 callback.Run(options_.capabilities); |
| 267 } | 324 } |
| 268 | 325 |
| 269 void BluetoothAudioSinkChromeOS::ClearConfiguration( | 326 void BluetoothAudioSinkChromeOS::ClearConfiguration( |
| 270 const ObjectPath& transport_path) { | 327 const ObjectPath& transport_path) { |
| 271 if (transport_path != transport_path_) | 328 if (transport_path != transport_path_) |
| 272 return; | 329 return; |
| 330 |
| 273 VLOG(1) << "ClearConfiguration"; | 331 VLOG(1) << "ClearConfiguration"; |
| 274 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED); | 332 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED); |
| 275 } | 333 } |
| 276 | 334 |
| 277 void BluetoothAudioSinkChromeOS::Released() { | 335 void BluetoothAudioSinkChromeOS::Released() { |
| 278 VLOG(1) << "Released"; | 336 VLOG(1) << "Released"; |
| 279 StateChanged(BluetoothAudioSink::STATE_INVALID); | 337 StateChanged(BluetoothAudioSink::STATE_INVALID); |
| 280 } | 338 } |
| 281 | 339 |
| 282 void BluetoothAudioSinkChromeOS::Register( | 340 void BluetoothAudioSinkChromeOS::OnFileCanReadWithoutBlocking(int fd) { |
| 283 const BluetoothAudioSink::Options& options, | 341 ReadFromFile(); |
| 284 const base::Closure& callback, | |
| 285 const BluetoothAudioSink::ErrorCallback& error_callback) { | |
| 286 VLOG(1) << "Register"; | |
| 287 | |
| 288 DCHECK(adapter_.get()); | |
| 289 DCHECK_EQ(state_, BluetoothAudioSink::STATE_DISCONNECTED); | |
| 290 | |
| 291 // Gets system bus. | |
| 292 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | |
| 293 | |
| 294 // Creates a Media Endpoint with newly-generated path. | |
| 295 endpoint_path_ = GenerateEndpointPath(); | |
| 296 media_endpoint_.reset( | |
| 297 BluetoothMediaEndpointServiceProvider::Create( | |
| 298 system_bus, endpoint_path_, this)); | |
| 299 | |
| 300 DCHECK(media_endpoint_.get()); | |
| 301 | |
| 302 // Creates endpoint properties with |options|. | |
| 303 options_ = options; | |
| 304 chromeos::BluetoothMediaClient::EndpointProperties endpoint_properties; | |
| 305 endpoint_properties.uuid = BluetoothMediaClient::kBluetoothAudioSinkUUID; | |
| 306 endpoint_properties.codec = options_.codec; | |
| 307 endpoint_properties.capabilities = options_.capabilities; | |
| 308 | |
| 309 media_path_ = static_cast<BluetoothAdapterChromeOS*>( | |
| 310 adapter_.get())->object_path(); | |
| 311 | |
| 312 chromeos::BluetoothMediaClient* media = | |
| 313 DBusThreadManager::Get()->GetBluetoothMediaClient(); | |
| 314 DCHECK(media); | |
| 315 media->RegisterEndpoint( | |
| 316 media_path_, | |
| 317 endpoint_path_, | |
| 318 endpoint_properties, | |
| 319 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterSucceeded, | |
| 320 weak_ptr_factory_.GetWeakPtr(), callback), | |
| 321 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterFailed, | |
| 322 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 323 } | 342 } |
| 324 | 343 |
| 325 BluetoothMediaEndpointServiceProvider* | 344 void BluetoothAudioSinkChromeOS::OnFileCanWriteWithoutBlocking(int fd) { |
| 326 BluetoothAudioSinkChromeOS::GetEndpointServiceProvider() { | 345 // Do nothing for now. |
| 327 return media_endpoint_.get(); | 346 } |
| 347 |
| 348 void BluetoothAudioSinkChromeOS::AcquireFD() { |
| 349 VLOG(1) << "AcquireFD - transport path: " << transport_path_.value(); |
| 350 |
| 351 read_has_failed_ = false; |
| 352 |
| 353 DBusThreadManager::Get()->GetBluetoothMediaTransportClient()->Acquire( |
| 354 transport_path_, |
| 355 base::Bind(&BluetoothAudioSinkChromeOS::OnAcquireSucceeded, |
| 356 weak_ptr_factory_.GetWeakPtr()), |
| 357 base::Bind(&BluetoothAudioSinkChromeOS::OnAcquireFailed, |
| 358 weak_ptr_factory_.GetWeakPtr())); |
| 359 } |
| 360 |
| 361 void BluetoothAudioSinkChromeOS::WatchFD() { |
| 362 CHECK(file_.get() && file_->IsValid()); |
| 363 |
| 364 VLOG(1) << "WatchFD - file: " << file_->GetPlatformFile() |
| 365 << ", file validity: " << file_->IsValid(); |
| 366 |
| 367 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 368 file_->GetPlatformFile(), true, base::MessageLoopForIO::WATCH_READ, |
| 369 &fd_read_watcher_, this); |
| 370 } |
| 371 |
| 372 void BluetoothAudioSinkChromeOS::StopWatchingFD() { |
| 373 if (!file_.get()) { |
| 374 VLOG(1) << "StopWatchingFD - skip"; |
| 375 return; |
| 376 } |
| 377 |
| 378 bool stopped = fd_read_watcher_.StopWatchingFileDescriptor(); |
| 379 VLOG(1) << "StopWatchingFD - watch stopped: " << stopped; |
| 380 CHECK(stopped); |
| 381 |
| 382 read_mtu_ = kInvalidReadMtu; |
| 383 write_mtu_ = kInvalidWriteMtu; |
| 384 file_.reset(); // This will close the file descriptor. |
| 385 } |
| 386 |
| 387 void BluetoothAudioSinkChromeOS::ReadFromFile() { |
| 388 DCHECK(file_.get() && file_->IsValid()); |
| 389 DCHECK(data_.get()); |
| 390 |
| 391 int size = file_->ReadAtCurrentPosNoBestEffort(data_.get(), read_mtu_); |
| 392 |
| 393 if (size == -1) { |
| 394 // To reduce the number of logs, log only once for multiple failures. |
| 395 if (!read_has_failed_) { |
| 396 VLOG(1) << "ReadFromFile - failed"; |
| 397 read_has_failed_ = true; |
| 398 } |
| 399 return; |
| 400 } |
| 401 |
| 402 VLOG(1) << "ReadFromFile - read " << size << " bytes"; |
| 403 FOR_EACH_OBSERVER(BluetoothAudioSink::Observer, observers_, |
| 404 BluetoothAudioSinkDataAvailable(this, data_.get(), size)); |
| 328 } | 405 } |
| 329 | 406 |
| 330 void BluetoothAudioSinkChromeOS::StateChanged( | 407 void BluetoothAudioSinkChromeOS::StateChanged( |
| 331 BluetoothAudioSink::State state) { | 408 BluetoothAudioSink::State state) { |
| 332 if (state == state_) | 409 if (state == state_) |
| 333 return; | 410 return; |
| 334 | 411 |
| 335 VLOG(1) << "StateChnaged: " << StateToString(state); | 412 VLOG(1) << "StateChanged - state: " << StateToString(state); |
| 336 | 413 |
| 337 switch (state) { | 414 switch (state) { |
| 338 case BluetoothAudioSink::STATE_INVALID: | 415 case BluetoothAudioSink::STATE_INVALID: |
| 339 ResetMedia(); | 416 ResetMedia(); |
| 340 ResetEndpoint(); | 417 ResetEndpoint(); |
| 341 case BluetoothAudioSink::STATE_DISCONNECTED: | 418 case BluetoothAudioSink::STATE_DISCONNECTED: |
| 342 ResetTransport(); | 419 ResetTransport(); |
| 343 break; | 420 break; |
| 344 case BluetoothAudioSink::STATE_IDLE: | 421 case BluetoothAudioSink::STATE_IDLE: |
| 345 // TODO(mcchou): BUG=441581 | 422 StopWatchingFD(); |
| 346 // Triggered by MediaTransportPropertyChanged and SetConfiguration. | |
| 347 // Stop watching on file descriptor if there is one. | |
| 348 break; | 423 break; |
| 349 case BluetoothAudioSink::STATE_PENDING: | 424 case BluetoothAudioSink::STATE_PENDING: |
| 350 // TODO(mcchou): BUG=441581 | 425 AcquireFD(); |
| 351 // Call BluetoothMediaTransportClient::Acquire() to get fd and mtus. | |
| 352 break; | 426 break; |
| 353 case BluetoothAudioSink::STATE_ACTIVE: | 427 case BluetoothAudioSink::STATE_ACTIVE: |
| 354 // TODO(mcchou): BUG=441581 | 428 WatchFD(); |
| 355 // Read from fd and call DataAvailable. | |
| 356 ReadFromFD(); | |
| 357 break; | 429 break; |
| 358 default: | 430 default: |
| 359 break; | 431 break; |
| 360 } | 432 } |
| 361 | 433 |
| 362 state_ = state; | 434 state_ = state; |
| 363 FOR_EACH_OBSERVER(BluetoothAudioSink::Observer, observers_, | 435 FOR_EACH_OBSERVER(BluetoothAudioSink::Observer, observers_, |
| 364 BluetoothAudioSinkStateChanged(this, state_)); | 436 BluetoothAudioSinkStateChanged(this, state_)); |
| 365 } | 437 } |
| 366 | 438 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 390 const std::string& error_message) { | 462 const std::string& error_message) { |
| 391 VLOG(1) << "OnRegisterFailed - error name: " << error_name | 463 VLOG(1) << "OnRegisterFailed - error name: " << error_name |
| 392 << ", error message: " << error_message; | 464 << ", error message: " << error_message; |
| 393 | 465 |
| 394 ResetEndpoint(); | 466 ResetEndpoint(); |
| 395 error_callback.Run(BluetoothAudioSink::ERROR_NOT_REGISTERED); | 467 error_callback.Run(BluetoothAudioSink::ERROR_NOT_REGISTERED); |
| 396 } | 468 } |
| 397 | 469 |
| 398 void BluetoothAudioSinkChromeOS::OnUnregisterSucceeded( | 470 void BluetoothAudioSinkChromeOS::OnUnregisterSucceeded( |
| 399 const base::Closure& callback) { | 471 const base::Closure& callback) { |
| 400 VLOG(1) << "Unregisterd"; | 472 VLOG(1) << "Unregistered - endpoint: " << endpoint_path_.value(); |
| 401 | 473 |
| 402 // Once the state becomes STATE_INVALID, media, media transport and media | 474 // Once the state becomes STATE_INVALID, media, media transport and media |
| 403 // endpoint will be reset. | 475 // endpoint will be reset. |
| 404 StateChanged(BluetoothAudioSink::STATE_INVALID); | 476 StateChanged(BluetoothAudioSink::STATE_INVALID); |
| 405 callback.Run(); | 477 callback.Run(); |
| 406 } | 478 } |
| 407 | 479 |
| 408 void BluetoothAudioSinkChromeOS::OnUnregisterFailed( | 480 void BluetoothAudioSinkChromeOS::OnUnregisterFailed( |
| 409 const device::BluetoothAudioSink::ErrorCallback& error_callback, | 481 const device::BluetoothAudioSink::ErrorCallback& error_callback, |
| 410 const std::string& error_name, | 482 const std::string& error_name, |
| 411 const std::string& error_message) { | 483 const std::string& error_message) { |
| 412 VLOG(1) << "OnUnregisterFailed - error name: " << error_name | 484 VLOG(1) << "OnUnregisterFailed - error name: " << error_name |
| 413 << ", error message: " << error_message; | 485 << ", error message: " << error_message; |
| 414 | 486 |
| 415 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED); | 487 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED); |
| 416 } | 488 } |
| 417 | 489 |
| 418 void BluetoothAudioSinkChromeOS::ReadFromFD() { | 490 void BluetoothAudioSinkChromeOS::OnAcquireSucceeded( |
| 419 DCHECK_GE(fd_.value(), 0); | 491 dbus::FileDescriptor* fd, |
| 492 const uint16_t read_mtu, |
| 493 const uint16_t write_mtu) { |
| 494 CHECK(fd); |
| 495 fd->CheckValidity(); |
| 496 CHECK(fd->is_valid() && fd->value() != kInvalidFd); |
| 497 CHECK_GT(read_mtu, kInvalidReadMtu); |
| 498 CHECK_GT(write_mtu, kInvalidWriteMtu); |
| 420 | 499 |
| 421 // TODO(mcchou): BUG=441581 | 500 // Avoids unnecessary memory reallocation if read MTU doesn't change. |
| 422 // Read from file descriptor using watcher and create a buffer to contain the | 501 if (read_mtu != read_mtu_) { |
| 423 // data. Notify |Observers_| while there is audio data available. | 502 read_mtu_ = read_mtu; |
| 503 data_.reset(new char[read_mtu_]); |
| 504 VLOG(1) << "OnAcquireSucceeded - allocate " << read_mtu_ |
| 505 << " bytes of memory"; |
| 506 } |
| 507 |
| 508 write_mtu_ = write_mtu; |
| 509 |
| 510 // Avoids closing the same file descriptor caused by reassignment. |
| 511 if (!file_.get() || file_->GetPlatformFile() != fd->value()) { |
| 512 // Takes ownership of the file descriptor. |
| 513 file_.reset(new base::File(fd->TakeValue())); |
| 514 DCHECK(file_->IsValid()); |
| 515 VLOG(1) << "OnAcquireSucceeded - update file"; |
| 516 } |
| 517 |
| 518 VLOG(1) << "OnAcquireSucceeded - file: " << file_->GetPlatformFile() |
| 519 << ", read MTU: " << read_mtu_ << ", write MTU: " << write_mtu_; |
| 520 } |
| 521 |
| 522 void BluetoothAudioSinkChromeOS::OnAcquireFailed( |
| 523 const std::string& error_name, |
| 524 const std::string& error_message) { |
| 525 VLOG(1) << "OnAcquireFailed - error name: " << error_name |
| 526 << ", error message: " << error_message; |
| 527 } |
| 528 |
| 529 void BluetoothAudioSinkChromeOS::OnReleaseFDSucceeded() { |
| 530 VLOG(1) << "OnReleaseFDSucceeded"; |
| 531 } |
| 532 |
| 533 void BluetoothAudioSinkChromeOS::OnReleaseFDFailed( |
| 534 const std::string& error_name, |
| 535 const std::string& error_message) { |
| 536 VLOG(1) << "OnReleaseFDFailed - error name: " << error_name |
| 537 << ", error message: " << error_message; |
| 424 } | 538 } |
| 425 | 539 |
| 426 void BluetoothAudioSinkChromeOS::ResetMedia() { | 540 void BluetoothAudioSinkChromeOS::ResetMedia() { |
| 427 VLOG(1) << "ResetMedia"; | 541 VLOG(1) << "ResetMedia"; |
| 428 | 542 |
| 429 media_path_ = dbus::ObjectPath(""); | 543 media_path_ = dbus::ObjectPath(""); |
| 430 } | 544 } |
| 431 | 545 |
| 432 void BluetoothAudioSinkChromeOS::ResetTransport() { | 546 void BluetoothAudioSinkChromeOS::ResetTransport() { |
| 433 VLOG(1) << "ResetTransport"; | 547 if (!transport_path_.IsValid()) { |
| 548 VLOG(1) << "ResetTransport - skip"; |
| 549 return; |
| 550 } |
| 434 | 551 |
| 435 if (transport_path_.value() == "") | 552 VLOG(1) << "ResetTransport - clean-up"; |
| 436 return; | 553 |
| 554 VolumeChanged(BluetoothAudioSink::kInvalidVolume); |
| 437 transport_path_ = dbus::ObjectPath(""); | 555 transport_path_ = dbus::ObjectPath(""); |
| 438 VolumeChanged(BluetoothAudioSink::kInvalidVolume); | 556 read_mtu_ = kInvalidReadMtu; |
| 439 read_mtu_ = 0; | 557 write_mtu_ = kInvalidWriteMtu; |
| 440 write_mtu_ = 0; | 558 file_.reset(); |
| 441 fd_.PutValue(-1); | |
| 442 } | 559 } |
| 443 | 560 |
| 444 void BluetoothAudioSinkChromeOS::ResetEndpoint() { | 561 void BluetoothAudioSinkChromeOS::ResetEndpoint() { |
| 445 VLOG(1) << "ResetEndpoint"; | 562 VLOG(1) << "ResetEndpoint"; |
| 446 | 563 |
| 447 endpoint_path_ = ObjectPath(""); | 564 endpoint_path_ = ObjectPath(""); |
| 448 media_endpoint_ = nullptr; | 565 media_endpoint_ = nullptr; |
| 449 } | 566 } |
| 450 | 567 |
| 451 } // namespace chromeos | 568 } // namespace chromeos |
| OLD | NEW |