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 <sstream> | 7 #include <sstream> |
| 8 #include <vector> |
8 | 9 |
| 10 #include "base/debug/stack_trace.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "dbus/message.h" |
11 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 14 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
12 | 15 |
13 namespace { | 16 namespace { |
14 | 17 |
15 // TODO(mcchou): Add the constant to dbus/service_constants.h. | 18 // TODO(mcchou): Add the constant to dbus/service_constants.h. |
16 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink"; | 19 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink"; |
17 | 20 |
18 dbus::ObjectPath GenerateEndpointPath() { | 21 dbus::ObjectPath GenerateEndpointPath() { |
19 static unsigned int sequence_number = 0; | 22 static unsigned int sequence_number = 0; |
20 ++sequence_number; | 23 ++sequence_number; |
(...skipping 10 matching lines...) Expand all Loading... |
31 scoped_refptr<device::BluetoothAdapter> adapter) | 34 scoped_refptr<device::BluetoothAdapter> adapter) |
32 : state_(device::BluetoothAudioSink::STATE_INVALID), | 35 : state_(device::BluetoothAudioSink::STATE_INVALID), |
33 volume_(0), | 36 volume_(0), |
34 read_mtu_(0), | 37 read_mtu_(0), |
35 write_mtu_(0), | 38 write_mtu_(0), |
36 adapter_(adapter), | 39 adapter_(adapter), |
37 weak_ptr_factory_(this) { | 40 weak_ptr_factory_(this) { |
38 DCHECK(adapter_.get()); | 41 DCHECK(adapter_.get()); |
39 DCHECK(adapter_->IsPresent()); | 42 DCHECK(adapter_->IsPresent()); |
40 | 43 |
| 44 adapter_->AddObserver(this); |
| 45 |
| 46 chromeos::BluetoothMediaClient* media = |
| 47 DBusThreadManager::Get()->GetBluetoothMediaClient(); |
| 48 DCHECK(media); |
| 49 media->AddObserver(this); |
| 50 |
| 51 chromeos::BluetoothMediaTransportClient *transport = |
| 52 chromeos::DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); |
| 53 DCHECK(transport); |
| 54 transport->AddObserver(this); |
| 55 |
41 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | 56 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); |
42 adapter_->AddObserver(this); | |
43 } | 57 } |
44 | 58 |
45 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() { | 59 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() { |
46 DCHECK(adapter_.get()); | 60 DCHECK(adapter_.get()); |
47 adapter_->RemoveObserver(this); | 61 adapter_->RemoveObserver(this); |
| 62 |
| 63 chromeos::BluetoothMediaClient* media = |
| 64 DBusThreadManager::Get()->GetBluetoothMediaClient(); |
| 65 DCHECK(media); |
| 66 media->RemoveObserver(this); |
| 67 |
| 68 chromeos::BluetoothMediaTransportClient *transport = |
| 69 chromeos::DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); |
| 70 DCHECK(transport); |
| 71 transport->RemoveObserver(this); |
| 72 |
48 // TODO(mcchou): BUG=441581 | 73 // TODO(mcchou): BUG=441581 |
49 // Unregister() should be called while media and media endpoint are still | 74 // Unregister() should be called while media and media endpoint are still |
50 // valid. | 75 // valid. |
51 } | 76 } |
52 | 77 |
| 78 void BluetoothAudioSinkChromeOS::Unregister( |
| 79 const base::Closure& callback, |
| 80 const device::BluetoothAudioSink::ErrorCallback& error_callback) { |
| 81 // TODO(mcchou): BUG=441581 |
| 82 // Call UnregisterEndpoint on the media object with |media_path_| and clean |
| 83 // |observers_| and transport_path_ and reset state_ and volume. |
| 84 // Check whether the media endpoint is registered before invoking |
| 85 // UnregisterEndpoint. |
| 86 } |
| 87 |
53 void BluetoothAudioSinkChromeOS::AddObserver( | 88 void BluetoothAudioSinkChromeOS::AddObserver( |
54 device::BluetoothAudioSink::Observer* observer) { | 89 device::BluetoothAudioSink::Observer* observer) { |
55 DCHECK(observer); | 90 DCHECK(observer); |
56 observers_.AddObserver(observer); | 91 observers_.AddObserver(observer); |
57 } | 92 } |
58 | 93 |
59 void BluetoothAudioSinkChromeOS::RemoveObserver( | 94 void BluetoothAudioSinkChromeOS::RemoveObserver( |
60 device::BluetoothAudioSink::Observer* observer) { | 95 device::BluetoothAudioSink::Observer* observer) { |
61 DCHECK(observer); | 96 DCHECK(observer); |
62 observers_.RemoveObserver(observer); | 97 observers_.RemoveObserver(observer); |
(...skipping 16 matching lines...) Expand all Loading... |
79 if (adapter->IsPresent()) { | 114 if (adapter->IsPresent()) { |
80 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | 115 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); |
81 } else { | 116 } else { |
82 StateChanged(device::BluetoothAudioSink::STATE_INVALID); | 117 StateChanged(device::BluetoothAudioSink::STATE_INVALID); |
83 } | 118 } |
84 } | 119 } |
85 | 120 |
86 void BluetoothAudioSinkChromeOS::MediaRemoved( | 121 void BluetoothAudioSinkChromeOS::MediaRemoved( |
87 const dbus::ObjectPath& object_path) { | 122 const dbus::ObjectPath& object_path) { |
88 // TODO(mcchou): BUG=441581 | 123 // TODO(mcchou): BUG=441581 |
89 // Check if |object_path| equals to |media_path_|. If true, change the state | 124 // Changes |state_| to STATE_INVALID or STATE_DISCONNECTED? |
90 // of the audio sink, call StateChanged and reset the audio sink. | 125 if (object_path == media_path_) { |
| 126 StateChanged(device::BluetoothAudioSink::STATE_INVALID); |
| 127 } |
91 } | 128 } |
92 | 129 |
93 void BluetoothAudioSinkChromeOS::MediaTransportRemoved( | 130 void BluetoothAudioSinkChromeOS::MediaTransportRemoved( |
94 const dbus::ObjectPath& object_path) { | 131 const dbus::ObjectPath& object_path) { |
95 // TODO(mcchou): BUG=441581 | |
96 // Check if |object_path| equals to |transport_path_|. If true, change the | |
97 // state of the audio sink, call StateChanged and reset the audio sink. | |
98 // Whenever powered of |adapter_| turns false while present stays true, media | 132 // Whenever powered of |adapter_| turns false while present stays true, media |
99 // transport object should be removed accordingly, and the state should be | 133 // transport object should be removed accordingly, and the state should be |
100 // changed to STATE_DISCONNECTED. | 134 // changed to STATE_DISCONNECTED. |
| 135 if (object_path == transport_path_) { |
| 136 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); |
| 137 } |
101 } | 138 } |
102 | 139 |
103 void BluetoothAudioSinkChromeOS::MediaTransportPropertyChanged( | 140 void BluetoothAudioSinkChromeOS::MediaTransportPropertyChanged( |
104 const dbus::ObjectPath& object_path, | 141 const dbus::ObjectPath& object_path, |
105 const std::string& property_name) { | 142 const std::string& property_name) { |
106 // TODO(mcchou): BUG=441581 | 143 if (object_path != transport_path_) |
107 // Call StateChanged and VolumeChanged accordingly if there is any change on | 144 return; |
108 // state/volume. | 145 |
| 146 // Retrieves the property set of the transport object with |object_path|. |
| 147 chromeos::BluetoothMediaTransportClient::Properties* properties = |
| 148 DBusThreadManager::Get() |
| 149 ->GetBluetoothMediaTransportClient() |
| 150 ->GetProperties(object_path); |
| 151 |
| 152 // Dispatches a property changed event to the corresponding handler. |
| 153 if (property_name == properties->state.name()) { |
| 154 if (properties->state.value() == |
| 155 BluetoothMediaTransportClient::kStateIdle) { |
| 156 StateChanged(device::BluetoothAudioSink::STATE_IDLE); |
| 157 } else if (properties->state.value() == |
| 158 BluetoothMediaTransportClient::kStatePending) { |
| 159 StateChanged(device::BluetoothAudioSink::STATE_PENDING); |
| 160 } else if (properties->state.value() == |
| 161 BluetoothMediaTransportClient::kStateActive) { |
| 162 StateChanged(device::BluetoothAudioSink::STATE_ACTIVE); |
| 163 } |
| 164 } else if (property_name == properties->volume.name()) { |
| 165 VolumeChanged(properties->volume.value()); |
| 166 } else { |
| 167 VLOG(1) << "Bluetooth audio sink: transport property " << property_name |
| 168 << " changed"; |
| 169 } |
109 } | 170 } |
110 | 171 |
111 void BluetoothAudioSinkChromeOS::SetConfiguration( | 172 void BluetoothAudioSinkChromeOS::SetConfiguration( |
112 const dbus::ObjectPath& transport_path, | 173 const dbus::ObjectPath& transport_path, |
113 const dbus::MessageReader& properties) { | 174 const TransportProperties& properties) { |
114 // TODO(mcchou): BUG=441581 | 175 VLOG(1) << "Bluetooth audio sink: SetConfiguration called"; |
115 // Update |transport_path_| and store properties if needed. | 176 transport_path_ = transport_path; |
| 177 |
| 178 // The initial state for a connection should be "idle". |
| 179 if (properties.state != BluetoothMediaTransportClient::kStateIdle) { |
| 180 VLOG(1) << "Bluetooth Audio Sink: unexpected state " << properties.state; |
| 181 return; |
| 182 } |
| 183 |
| 184 // Updates |volume_| if the volume level is provided in |properties|. |
| 185 if (properties.volume.get() != nullptr) |
| 186 VolumeChanged(*properties.volume); |
| 187 |
| 188 StateChanged(device::BluetoothAudioSink::STATE_IDLE); |
116 } | 189 } |
117 | 190 |
118 void BluetoothAudioSinkChromeOS::SelectConfiguration( | 191 void BluetoothAudioSinkChromeOS::SelectConfiguration( |
119 const std::vector<uint8_t>& capabilities, | 192 const std::vector<uint8_t>& capabilities, |
120 const SelectConfigurationCallback& callback) { | 193 const SelectConfigurationCallback& callback) { |
121 // TODO(mcchou): BUG=441581 | |
122 // Use SelectConfigurationCallback to return the agreed capabilities. | |
123 VLOG(1) << "Bluetooth audio sink: SelectConfiguration called"; | 194 VLOG(1) << "Bluetooth audio sink: SelectConfiguration called"; |
124 callback.Run(options_.capabilities); | 195 callback.Run(options_.capabilities); |
125 } | 196 } |
126 | 197 |
127 void BluetoothAudioSinkChromeOS::ClearConfiguration( | 198 void BluetoothAudioSinkChromeOS::ClearConfiguration( |
128 const dbus::ObjectPath& transport_path) { | 199 const dbus::ObjectPath& transport_path) { |
129 // TODO(mcchou): BUG=441581 | 200 VLOG(1) << "Bluetooth audio sink: ClearConfiguration called"; |
130 // Reset the configuration to the default one and close IOBuffer. | 201 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); |
131 } | 202 } |
132 | 203 |
133 void BluetoothAudioSinkChromeOS::Release() { | 204 void BluetoothAudioSinkChromeOS::Released() { |
134 VLOG(1) << "Bluetooth audio sink: Release called"; | 205 VLOG(1) << "Bluetooth audio sink: Released called"; |
135 StateChanged(device::BluetoothAudioSink::STATE_INVALID); | 206 StateChanged(device::BluetoothAudioSink::STATE_INVALID); |
136 } | 207 } |
137 | 208 |
138 void BluetoothAudioSinkChromeOS::Register( | 209 void BluetoothAudioSinkChromeOS::Register( |
139 const device::BluetoothAudioSink::Options& options, | 210 const device::BluetoothAudioSink::Options& options, |
140 const base::Closure& callback, | 211 const base::Closure& callback, |
141 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 212 const device::BluetoothAudioSink::ErrorCallback& error_callback) { |
142 // TODO(mcchou): BUG=441581 | |
143 // Get Media object, initiate an Media Endpoint with options, and return the | |
144 // audio sink via callback. Add the audio sink as observer of both Media and | |
145 // Media Transport. | |
146 DCHECK(adapter_.get()); | 213 DCHECK(adapter_.get()); |
147 DCHECK_EQ(state_, device::BluetoothAudioSink::STATE_DISCONNECTED); | 214 DCHECK_EQ(state_, device::BluetoothAudioSink::STATE_DISCONNECTED); |
148 | 215 |
149 // Gets system bus. | 216 // Gets system bus. |
150 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | 217 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); |
151 | 218 |
152 // Creates a Media Endpoint with newly-generated path. | 219 // Creates a Media Endpoint with newly-generated path. |
153 endpoint_path_ = GenerateEndpointPath(); | 220 endpoint_path_ = GenerateEndpointPath(); |
154 media_endpoint_.reset( | 221 media_endpoint_.reset( |
155 BluetoothMediaEndpointServiceProvider::Create( | 222 BluetoothMediaEndpointServiceProvider::Create( |
156 system_bus, endpoint_path_, this)); | 223 system_bus, endpoint_path_, this)); |
157 | 224 |
158 DCHECK(media_endpoint_.get()); | 225 DCHECK(media_endpoint_.get()); |
159 | 226 |
160 // Creates endpoint properties with |options|. | 227 // Creates endpoint properties with |options|. |
161 options_ = options; | 228 options_ = options; |
162 chromeos::BluetoothMediaClient::EndpointProperties endpoint_properties; | 229 chromeos::BluetoothMediaClient::EndpointProperties endpoint_properties; |
163 endpoint_properties.uuid = BluetoothMediaClient::kBluetoothAudioSinkUUID; | 230 endpoint_properties.uuid = BluetoothMediaClient::kBluetoothAudioSinkUUID; |
164 endpoint_properties.codec = options_.codec; | 231 endpoint_properties.codec = options_.codec; |
165 endpoint_properties.capabilities = options_.capabilities; | 232 endpoint_properties.capabilities = options_.capabilities; |
166 | 233 |
167 // Gets Media object exported by D-Bus and registers the endpoint. | 234 media_path_ = static_cast<BluetoothAdapterChromeOS*>( |
| 235 adapter_.get())->object_path(); |
| 236 |
168 chromeos::BluetoothMediaClient* media = | 237 chromeos::BluetoothMediaClient* media = |
169 DBusThreadManager::Get()->GetBluetoothMediaClient(); | 238 DBusThreadManager::Get()->GetBluetoothMediaClient(); |
170 BluetoothAdapterChromeOS* adapter_chromeos = | 239 DCHECK(media); |
171 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()); | |
172 media->AddObserver(this); | |
173 media_path_ = adapter_chromeos->object_path(); | |
174 media->RegisterEndpoint( | 240 media->RegisterEndpoint( |
175 media_path_, | 241 media_path_, |
176 endpoint_path_, | 242 endpoint_path_, |
177 endpoint_properties, | 243 endpoint_properties, |
178 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterSucceeded, | 244 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterSucceeded, |
179 weak_ptr_factory_.GetWeakPtr(), callback), | 245 weak_ptr_factory_.GetWeakPtr(), callback), |
180 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterFailed, | 246 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterFailed, |
181 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 247 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
182 } | 248 } |
183 | 249 |
184 void BluetoothAudioSinkChromeOS::Unregister( | 250 BluetoothMediaEndpointServiceProvider* |
185 const base::Closure& callback, | 251 BluetoothAudioSinkChromeOS::GetEndpointServiceProvider() { |
186 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 252 return media_endpoint_.get(); |
187 // TODO(mcchou): BUG=441581 | |
188 // Call UnregisterEndpoint on the media object with |media_path_| and clean | |
189 // |observers_| and transport_path_ and reset state_ and volume. | |
190 // Check whether the media endpoint is registered before invoking | |
191 // UnregisterEndpoint. | |
192 } | 253 } |
193 | 254 |
194 void BluetoothAudioSinkChromeOS::StateChanged( | 255 void BluetoothAudioSinkChromeOS::StateChanged( |
195 device::BluetoothAudioSink::State state) { | 256 device::BluetoothAudioSink::State state) { |
196 if (state == state_) | 257 if (state == state_) |
197 return; | 258 return; |
198 | 259 |
199 VLOG(1) << "Bluetooth audio sink state changed: " << state; | 260 VLOG(1) << "Bluetooth audio sink state changed: " << state; |
200 | 261 |
201 switch (state) { | 262 switch (state) { |
202 case device::BluetoothAudioSink::STATE_INVALID: { | 263 case device::BluetoothAudioSink::STATE_INVALID: { |
203 // TODO(mcchou): BUG=441581 | 264 // TODO(mcchou): BUG=441581 |
204 // Clean media, media transport and media endpoint. | 265 ResetMedia(); |
| 266 ResetTransport(); |
| 267 ResetEndpoint(); |
205 break; | 268 break; |
206 } | 269 } |
207 case device::BluetoothAudioSink::STATE_DISCONNECTED: { | 270 case device::BluetoothAudioSink::STATE_DISCONNECTED: { |
208 // TODO(mcchou): BUG=441581 | 271 // TODO(mcchou): BUG=441581 |
209 // Clean media transport. | 272 // Clean media transport and remove the audio sink from its observer list. |
| 273 ResetTransport(); |
210 break; | 274 break; |
211 } | 275 } |
212 case device::BluetoothAudioSink::STATE_IDLE: { | 276 case device::BluetoothAudioSink::STATE_IDLE: { |
213 // TODO(mcchou): BUG=441581 | 277 // TODO(mcchou): BUG=441581 |
214 // Triggered by MediaTransportPropertyChanged. Stop watching on file | 278 // Triggered by MediaTransportPropertyChanged and SetConfiguration. |
215 // descriptor if there is one. | 279 // Stop watching on file descriptor if there is one. |
216 break; | 280 break; |
217 } | 281 } |
218 case device::BluetoothAudioSink::STATE_PENDING: { | 282 case device::BluetoothAudioSink::STATE_PENDING: { |
219 // TODO(mcchou): BUG=441581 | 283 // TODO(mcchou): BUG=441581 |
220 // Call BluetoothMediaTransportClient::Acquire() to get fd and mtus. | 284 // Call BluetoothMediaTransportClient::Acquire() to get fd and mtus. |
221 break; | 285 break; |
222 } | 286 } |
223 case device::BluetoothAudioSink::STATE_ACTIVE: { | 287 case device::BluetoothAudioSink::STATE_ACTIVE: { |
224 // TODO(mcchou): BUG=441581 | 288 // TODO(mcchou): BUG=441581 |
225 // Read from fd and call DataAvailable. | 289 // Read from fd and call DataAvailable. |
(...skipping 25 matching lines...) Expand all Loading... |
251 callback.Run(); | 315 callback.Run(); |
252 } | 316 } |
253 | 317 |
254 void BluetoothAudioSinkChromeOS::OnRegisterFailed( | 318 void BluetoothAudioSinkChromeOS::OnRegisterFailed( |
255 const BluetoothAudioSink::ErrorCallback& error_callback, | 319 const BluetoothAudioSink::ErrorCallback& error_callback, |
256 const std::string& error_name, | 320 const std::string& error_name, |
257 const std::string& error_message) { | 321 const std::string& error_message) { |
258 DCHECK(media_endpoint_.get()); | 322 DCHECK(media_endpoint_.get()); |
259 VLOG(1) << "Bluetooth audio sink: " << error_name << ": " << error_message; | 323 VLOG(1) << "Bluetooth audio sink: " << error_name << ": " << error_message; |
260 | 324 |
261 endpoint_path_ = dbus::ObjectPath(""); | 325 ResetEndpoint(); |
262 media_endpoint_ = nullptr; | |
263 error_callback.Run(device::BluetoothAudioSink::ERROR_NOT_REGISTERED); | 326 error_callback.Run(device::BluetoothAudioSink::ERROR_NOT_REGISTERED); |
264 } | 327 } |
265 | 328 |
266 void BluetoothAudioSinkChromeOS::ReadFromFD() { | 329 void BluetoothAudioSinkChromeOS::ReadFromFD() { |
267 DCHECK_GE(fd_.value(), 0); | 330 DCHECK_GE(fd_.value(), 0); |
268 | 331 |
269 // TODO(mcchou): BUG=441581 | 332 // TODO(mcchou): BUG=441581 |
270 // Read from file descriptor using watcher and create a buffer to contain the | 333 // Read from file descriptor using watcher and create a buffer to contain the |
271 // data. Notify |Observers_| while there is audio data available. | 334 // data. Notify |Observers_| while there is audio data available. |
272 } | 335 } |
273 | 336 |
| 337 void BluetoothAudioSinkChromeOS::ResetMedia() { |
| 338 media_path_ = dbus::ObjectPath(""); |
| 339 } |
| 340 |
| 341 void BluetoothAudioSinkChromeOS::ResetTransport() { |
| 342 transport_path_ = dbus::ObjectPath(""); |
| 343 volume_ = 0; |
| 344 read_mtu_ = 0; |
| 345 write_mtu_ = 0; |
| 346 fd_.PutValue(-1); |
| 347 } |
| 348 |
| 349 void BluetoothAudioSinkChromeOS::ResetEndpoint() { |
| 350 endpoint_path_ = dbus::ObjectPath(""); |
| 351 media_endpoint_ = nullptr; |
| 352 } |
| 353 |
274 } // namespace chromeos | 354 } // namespace chromeos |
OLD | NEW |