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