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

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

Issue 1347193004: Refactor DBusThreadManager to split away BT clients. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months 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
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> 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 "chromeos/dbus/dbus_thread_manager.h"
18 #include "dbus/message.h" 17 #include "dbus/message.h"
19 #include "device/bluetooth/bluetooth_adapter_chromeos.h" 18 #include "device/bluetooth/bluetooth_adapter_chromeos.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; 29 const int kInvalidFd = -1;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 volume_(BluetoothAudioSink::kInvalidVolume), 87 volume_(BluetoothAudioSink::kInvalidVolume),
88 read_mtu_(kInvalidReadMtu), 88 read_mtu_(kInvalidReadMtu),
89 write_mtu_(kInvalidWriteMtu), 89 write_mtu_(kInvalidWriteMtu),
90 read_has_failed_(false), 90 read_has_failed_(false),
91 adapter_(adapter), 91 adapter_(adapter),
92 weak_ptr_factory_(this) { 92 weak_ptr_factory_(this) {
93 VLOG(1) << "BluetoothAudioSinkChromeOS created"; 93 VLOG(1) << "BluetoothAudioSinkChromeOS created";
94 94
95 CHECK(adapter_.get()); 95 CHECK(adapter_.get());
96 CHECK(adapter_->IsPresent()); 96 CHECK(adapter_->IsPresent());
97 CHECK(DBusThreadManager::IsInitialized()); 97 CHECK(bluez::BluezDBusManager::IsInitialized());
98 98
99 adapter_->AddObserver(this); 99 adapter_->AddObserver(this);
100 100
101 BluetoothMediaClient* media = 101 bluez::BluetoothMediaClient* media =
102 DBusThreadManager::Get()->GetBluetoothMediaClient(); 102 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient();
103 CHECK(media); 103 CHECK(media);
104 media->AddObserver(this); 104 media->AddObserver(this);
105 105
106 BluetoothMediaTransportClient* transport = 106 bluez::BluetoothMediaTransportClient* transport =
107 DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); 107 bluez::BluezDBusManager::Get()->GetBluetoothMediaTransportClient();
108 CHECK(transport); 108 CHECK(transport);
109 transport->AddObserver(this); 109 transport->AddObserver(this);
110 110
111 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); 111 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED);
112 } 112 }
113 113
114 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() { 114 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() {
115 VLOG(1) << "BluetoothAudioSinkChromeOS destroyed"; 115 VLOG(1) << "BluetoothAudioSinkChromeOS destroyed";
116 116
117 DCHECK(adapter_.get()); 117 DCHECK(adapter_.get());
118 118
119 if (state_ != BluetoothAudioSink::STATE_INVALID && media_endpoint_.get()) { 119 if (state_ != BluetoothAudioSink::STATE_INVALID && media_endpoint_.get()) {
120 Unregister(base::Bind(&base::DoNothing), 120 Unregister(base::Bind(&base::DoNothing),
121 base::Bind(&UnregisterErrorCallback)); 121 base::Bind(&UnregisterErrorCallback));
122 } 122 }
123 123
124 adapter_->RemoveObserver(this); 124 adapter_->RemoveObserver(this);
125 125
126 BluetoothMediaClient* media = 126 bluez::BluetoothMediaClient* media =
127 DBusThreadManager::Get()->GetBluetoothMediaClient(); 127 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient();
128 CHECK(media); 128 CHECK(media);
129 media->RemoveObserver(this); 129 media->RemoveObserver(this);
130 130
131 BluetoothMediaTransportClient* transport = 131 bluez::BluetoothMediaTransportClient* transport =
132 DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); 132 bluez::BluezDBusManager::Get()->GetBluetoothMediaTransportClient();
133 CHECK(transport); 133 CHECK(transport);
134 transport->RemoveObserver(this); 134 transport->RemoveObserver(this);
135 } 135 }
136 136
137 void BluetoothAudioSinkChromeOS::Unregister( 137 void BluetoothAudioSinkChromeOS::Unregister(
138 const base::Closure& callback, 138 const base::Closure& callback,
139 const device::BluetoothAudioSink::ErrorCallback& error_callback) { 139 const device::BluetoothAudioSink::ErrorCallback& error_callback) {
140 VLOG(1) << "Unregister"; 140 VLOG(1) << "Unregister";
141 141
142 if (!DBusThreadManager::IsInitialized()) 142 if (!bluez::BluezDBusManager::IsInitialized())
143 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED); 143 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED);
144 144
145 BluetoothMediaClient* media = 145 bluez::BluetoothMediaClient* media =
146 DBusThreadManager::Get()->GetBluetoothMediaClient(); 146 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient();
147 CHECK(media); 147 CHECK(media);
148 148
149 media->UnregisterEndpoint( 149 media->UnregisterEndpoint(
150 media_path_, 150 media_path_,
151 endpoint_path_, 151 endpoint_path_,
152 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterSucceeded, 152 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterSucceeded,
153 weak_ptr_factory_.GetWeakPtr(), callback), 153 weak_ptr_factory_.GetWeakPtr(), callback),
154 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterFailed, 154 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterFailed,
155 weak_ptr_factory_.GetWeakPtr(), error_callback)); 155 weak_ptr_factory_.GetWeakPtr(), error_callback));
156 } 156 }
(...skipping 21 matching lines...) Expand all
178 void BluetoothAudioSinkChromeOS::Register( 178 void BluetoothAudioSinkChromeOS::Register(
179 const BluetoothAudioSink::Options& options, 179 const BluetoothAudioSink::Options& options,
180 const base::Closure& callback, 180 const base::Closure& callback,
181 const BluetoothAudioSink::ErrorCallback& error_callback) { 181 const BluetoothAudioSink::ErrorCallback& error_callback) {
182 VLOG(1) << "Register"; 182 VLOG(1) << "Register";
183 183
184 DCHECK(adapter_.get()); 184 DCHECK(adapter_.get());
185 DCHECK_EQ(state_, BluetoothAudioSink::STATE_DISCONNECTED); 185 DCHECK_EQ(state_, BluetoothAudioSink::STATE_DISCONNECTED);
186 186
187 // Gets system bus. 187 // Gets system bus.
188 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); 188 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus();
189 189
190 // Creates a Media Endpoint with newly-generated path. 190 // Creates a Media Endpoint with newly-generated path.
191 endpoint_path_ = GenerateEndpointPath(); 191 endpoint_path_ = GenerateEndpointPath();
192 media_endpoint_.reset( 192 media_endpoint_.reset(bluez::BluetoothMediaEndpointServiceProvider::Create(
193 BluetoothMediaEndpointServiceProvider::Create( 193 system_bus, endpoint_path_, this));
194 system_bus, endpoint_path_, this));
195 194
196 DCHECK(media_endpoint_.get()); 195 DCHECK(media_endpoint_.get());
197 196
198 // Creates endpoint properties with |options|. 197 // Creates endpoint properties with |options|.
199 options_ = options; 198 options_ = options;
200 chromeos::BluetoothMediaClient::EndpointProperties endpoint_properties; 199 bluez::BluetoothMediaClient::EndpointProperties endpoint_properties;
201 endpoint_properties.uuid = BluetoothMediaClient::kBluetoothAudioSinkUUID; 200 endpoint_properties.uuid =
201 bluez::BluetoothMediaClient::kBluetoothAudioSinkUUID;
202 endpoint_properties.codec = options_.codec; 202 endpoint_properties.codec = options_.codec;
203 endpoint_properties.capabilities = options_.capabilities; 203 endpoint_properties.capabilities = options_.capabilities;
204 204
205 media_path_ = static_cast<BluetoothAdapterChromeOS*>( 205 media_path_ = static_cast<BluetoothAdapterChromeOS*>(
206 adapter_.get())->object_path(); 206 adapter_.get())->object_path();
207 207
208 BluetoothMediaClient* media = 208 bluez::BluetoothMediaClient* media =
209 DBusThreadManager::Get()->GetBluetoothMediaClient(); 209 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient();
210 CHECK(media); 210 CHECK(media);
211 media->RegisterEndpoint( 211 media->RegisterEndpoint(
212 media_path_, 212 media_path_,
213 endpoint_path_, 213 endpoint_path_,
214 endpoint_properties, 214 endpoint_properties,
215 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterSucceeded, 215 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterSucceeded,
216 weak_ptr_factory_.GetWeakPtr(), callback), 216 weak_ptr_factory_.GetWeakPtr(), callback),
217 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterFailed, 217 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterFailed,
218 weak_ptr_factory_.GetWeakPtr(), error_callback)); 218 weak_ptr_factory_.GetWeakPtr(), error_callback));
219 } 219 }
220 220
221 BluetoothMediaEndpointServiceProvider* 221 bluez::BluetoothMediaEndpointServiceProvider*
222 BluetoothAudioSinkChromeOS::GetEndpointServiceProvider() { 222 BluetoothAudioSinkChromeOS::GetEndpointServiceProvider() {
223 return media_endpoint_.get(); 223 return media_endpoint_.get();
224 } 224 }
225 225
226 void BluetoothAudioSinkChromeOS::AdapterPresentChanged( 226 void BluetoothAudioSinkChromeOS::AdapterPresentChanged(
227 device::BluetoothAdapter* adapter, bool present) { 227 device::BluetoothAdapter* adapter, bool present) {
228 VLOG(1) << "AdapterPresentChanged: " << present; 228 VLOG(1) << "AdapterPresentChanged: " << present;
229 229
230 if (adapter != adapter_.get()) 230 if (adapter != adapter_.get())
231 return; 231 return;
232 232
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 273
274 void BluetoothAudioSinkChromeOS::MediaTransportPropertyChanged( 274 void BluetoothAudioSinkChromeOS::MediaTransportPropertyChanged(
275 const ObjectPath& object_path, 275 const ObjectPath& object_path,
276 const std::string& property_name) { 276 const std::string& property_name) {
277 if (object_path != transport_path_) 277 if (object_path != transport_path_)
278 return; 278 return;
279 279
280 VLOG(1) << "MediaTransportPropertyChanged: " << property_name; 280 VLOG(1) << "MediaTransportPropertyChanged: " << property_name;
281 281
282 // Retrieves the property set of the transport object with |object_path|. 282 // Retrieves the property set of the transport object with |object_path|.
283 BluetoothMediaTransportClient::Properties* properties = 283 bluez::BluetoothMediaTransportClient::Properties* properties =
284 DBusThreadManager::Get() 284 bluez::BluezDBusManager::Get()
285 ->GetBluetoothMediaTransportClient() 285 ->GetBluetoothMediaTransportClient()
286 ->GetProperties(object_path); 286 ->GetProperties(object_path);
287 287
288 // Dispatches a property changed event to the corresponding handler. 288 // Dispatches a property changed event to the corresponding handler.
289 if (property_name == properties->state.name()) { 289 if (property_name == properties->state.name()) {
290 if (properties->state.value() == 290 if (properties->state.value() ==
291 BluetoothMediaTransportClient::kStateIdle) { 291 bluez::BluetoothMediaTransportClient::kStateIdle) {
292 StateChanged(BluetoothAudioSink::STATE_IDLE); 292 StateChanged(BluetoothAudioSink::STATE_IDLE);
293 } else if (properties->state.value() == 293 } else if (properties->state.value() ==
294 BluetoothMediaTransportClient::kStatePending) { 294 bluez::BluetoothMediaTransportClient::kStatePending) {
295 StateChanged(BluetoothAudioSink::STATE_PENDING); 295 StateChanged(BluetoothAudioSink::STATE_PENDING);
296 } else if (properties->state.value() == 296 } else if (properties->state.value() ==
297 BluetoothMediaTransportClient::kStateActive) { 297 bluez::BluetoothMediaTransportClient::kStateActive) {
298 StateChanged(BluetoothAudioSink::STATE_ACTIVE); 298 StateChanged(BluetoothAudioSink::STATE_ACTIVE);
299 } 299 }
300 } else if (property_name == properties->volume.name()) { 300 } else if (property_name == properties->volume.name()) {
301 VolumeChanged(properties->volume.value()); 301 VolumeChanged(properties->volume.value());
302 } 302 }
303 } 303 }
304 304
305 void BluetoothAudioSinkChromeOS::SetConfiguration( 305 void BluetoothAudioSinkChromeOS::SetConfiguration(
306 const ObjectPath& transport_path, 306 const ObjectPath& transport_path,
307 const TransportProperties& properties) { 307 const TransportProperties& properties) {
308 VLOG(1) << "SetConfiguration"; 308 VLOG(1) << "SetConfiguration";
309 transport_path_ = transport_path; 309 transport_path_ = transport_path;
310 310
311 // The initial state for a connection should be "idle". 311 // The initial state for a connection should be "idle".
312 if (properties.state != BluetoothMediaTransportClient::kStateIdle) { 312 if (properties.state != bluez::BluetoothMediaTransportClient::kStateIdle) {
313 VLOG(1) << "SetConfiugration - unexpected state :" << properties.state; 313 VLOG(1) << "SetConfiugration - unexpected state :" << properties.state;
314 return; 314 return;
315 } 315 }
316 316
317 // Updates |volume_| if the volume level is provided in |properties|. 317 // Updates |volume_| if the volume level is provided in |properties|.
318 if (properties.volume.get()) { 318 if (properties.volume.get()) {
319 VolumeChanged(*properties.volume); 319 VolumeChanged(*properties.volume);
320 } 320 }
321 321
322 StateChanged(BluetoothAudioSink::STATE_IDLE); 322 StateChanged(BluetoothAudioSink::STATE_IDLE);
(...skipping 26 matching lines...) Expand all
349 349
350 void BluetoothAudioSinkChromeOS::OnFileCanWriteWithoutBlocking(int fd) { 350 void BluetoothAudioSinkChromeOS::OnFileCanWriteWithoutBlocking(int fd) {
351 // Do nothing for now. 351 // Do nothing for now.
352 } 352 }
353 353
354 void BluetoothAudioSinkChromeOS::AcquireFD() { 354 void BluetoothAudioSinkChromeOS::AcquireFD() {
355 VLOG(1) << "AcquireFD - transport path: " << transport_path_.value(); 355 VLOG(1) << "AcquireFD - transport path: " << transport_path_.value();
356 356
357 read_has_failed_ = false; 357 read_has_failed_ = false;
358 358
359 DBusThreadManager::Get()->GetBluetoothMediaTransportClient()->Acquire( 359 bluez::BluezDBusManager::Get()->GetBluetoothMediaTransportClient()->Acquire(
360 transport_path_, 360 transport_path_,
361 base::Bind(&BluetoothAudioSinkChromeOS::OnAcquireSucceeded, 361 base::Bind(&BluetoothAudioSinkChromeOS::OnAcquireSucceeded,
362 weak_ptr_factory_.GetWeakPtr()), 362 weak_ptr_factory_.GetWeakPtr()),
363 base::Bind(&BluetoothAudioSinkChromeOS::OnAcquireFailed, 363 base::Bind(&BluetoothAudioSinkChromeOS::OnAcquireFailed,
364 weak_ptr_factory_.GetWeakPtr())); 364 weak_ptr_factory_.GetWeakPtr()));
365 } 365 }
366 366
367 void BluetoothAudioSinkChromeOS::WatchFD() { 367 void BluetoothAudioSinkChromeOS::WatchFD() {
368 CHECK(file_.get() && file_->IsValid()); 368 CHECK(file_.get() && file_->IsValid());
369 369
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 } 566 }
567 567
568 void BluetoothAudioSinkChromeOS::ResetEndpoint() { 568 void BluetoothAudioSinkChromeOS::ResetEndpoint() {
569 VLOG(1) << "ResetEndpoint"; 569 VLOG(1) << "ResetEndpoint";
570 570
571 endpoint_path_ = ObjectPath(""); 571 endpoint_path_ = ObjectPath("");
572 media_endpoint_ = nullptr; 572 media_endpoint_ = nullptr;
573 } 573 }
574 574
575 } // namespace chromeos 575 } // namespace chromeos
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_audio_sink_chromeos.h ('k') | device/bluetooth/bluetooth_audio_sink_chromeos_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698