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 #include <vector> |
9 | 9 |
10 #include "base/debug/stack_trace.h" | 10 #include "base/debug/stack_trace.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
13 #include "dbus/message.h" | 13 #include "dbus/message.h" |
14 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 14 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
15 | 15 |
16 using dbus::ObjectPath; | |
17 using device::BluetoothAudioSink; | |
18 | |
16 namespace { | 19 namespace { |
17 | 20 |
18 // TODO(mcchou): Add the constant to dbus/service_constants.h. | 21 // TODO(mcchou): Add the constant to dbus/service_constants.h. |
19 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink"; | 22 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink"; |
20 | 23 |
21 dbus::ObjectPath GenerateEndpointPath() { | 24 ObjectPath GenerateEndpointPath() { |
22 static unsigned int sequence_number = 0; | 25 static unsigned int sequence_number = 0; |
23 ++sequence_number; | 26 ++sequence_number; |
24 std::stringstream path; | 27 std::stringstream path; |
25 path << kBluetoothAudioSinkServicePath << "/endpoint" << sequence_number; | 28 path << kBluetoothAudioSinkServicePath << "/endpoint" << sequence_number; |
26 return dbus::ObjectPath(path.str()); | 29 return ObjectPath(path.str()); |
27 } | 30 } |
28 | 31 |
29 } // namespace | 32 } // namespace |
30 | 33 |
31 namespace chromeos { | 34 namespace chromeos { |
32 | 35 |
33 BluetoothAudioSinkChromeOS::BluetoothAudioSinkChromeOS( | 36 BluetoothAudioSinkChromeOS::BluetoothAudioSinkChromeOS( |
34 scoped_refptr<device::BluetoothAdapter> adapter) | 37 scoped_refptr<device::BluetoothAdapter> adapter) |
35 : state_(device::BluetoothAudioSink::STATE_INVALID), | 38 : state_(BluetoothAudioSink::STATE_INVALID), |
36 volume_(0), | 39 read_mtu_(nullptr), |
37 read_mtu_(0), | 40 write_mtu_(nullptr), |
38 write_mtu_(0), | |
39 adapter_(adapter), | 41 adapter_(adapter), |
40 weak_ptr_factory_(this) { | 42 weak_ptr_factory_(this) { |
41 DCHECK(adapter_.get()); | 43 DCHECK(adapter_.get()); |
42 DCHECK(adapter_->IsPresent()); | 44 DCHECK(adapter_->IsPresent()); |
43 | 45 |
44 adapter_->AddObserver(this); | 46 adapter_->AddObserver(this); |
45 | 47 |
46 chromeos::BluetoothMediaClient* media = | 48 chromeos::BluetoothMediaClient* media = |
47 DBusThreadManager::Get()->GetBluetoothMediaClient(); | 49 DBusThreadManager::Get()->GetBluetoothMediaClient(); |
48 DCHECK(media); | 50 DCHECK(media); |
49 media->AddObserver(this); | 51 media->AddObserver(this); |
50 | 52 |
51 chromeos::BluetoothMediaTransportClient *transport = | 53 chromeos::BluetoothMediaTransportClient *transport = |
52 chromeos::DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); | 54 chromeos::DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); |
53 DCHECK(transport); | 55 DCHECK(transport); |
54 transport->AddObserver(this); | 56 transport->AddObserver(this); |
55 | 57 |
56 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | 58 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); |
57 } | 59 } |
58 | 60 |
59 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() { | 61 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() { |
60 DCHECK(adapter_.get()); | 62 DCHECK(adapter_.get()); |
63 | |
64 if (state_ != BluetoothAudioSink::STATE_INVALID && media_endpoint_.get()) { | |
65 Unregister(base::Bind(&base::DoNothing), | |
66 base::Bind(&BluetoothAudioSinkChromeOS::UnregisterErrorCallback, | |
67 base::Unretained(this))); | |
armansito
2015/02/23 22:07:06
You wouldn't need to pass an unretained pointer if
Miao
2015/02/24 01:12:13
Done.
| |
68 } | |
69 | |
61 adapter_->RemoveObserver(this); | 70 adapter_->RemoveObserver(this); |
62 | 71 |
63 chromeos::BluetoothMediaClient* media = | 72 chromeos::BluetoothMediaClient* media = |
64 DBusThreadManager::Get()->GetBluetoothMediaClient(); | 73 DBusThreadManager::Get()->GetBluetoothMediaClient(); |
65 DCHECK(media); | 74 DCHECK(media); |
66 media->RemoveObserver(this); | 75 media->RemoveObserver(this); |
67 | 76 |
68 chromeos::BluetoothMediaTransportClient *transport = | 77 chromeos::BluetoothMediaTransportClient *transport = |
69 chromeos::DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); | 78 chromeos::DBusThreadManager::Get()->GetBluetoothMediaTransportClient(); |
70 DCHECK(transport); | 79 DCHECK(transport); |
71 transport->RemoveObserver(this); | 80 transport->RemoveObserver(this); |
72 | |
73 // TODO(mcchou): BUG=441581 | |
74 // Unregister() should be called while media and media endpoint are still | |
75 // valid. | |
76 } | 81 } |
77 | 82 |
78 void BluetoothAudioSinkChromeOS::Unregister( | 83 void BluetoothAudioSinkChromeOS::Unregister( |
79 const base::Closure& callback, | 84 const base::Closure& callback, |
80 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 85 const device::BluetoothAudioSink::ErrorCallback& error_callback) { |
81 // TODO(mcchou): BUG=441581 | 86 // TODO(mcchou): Guarantee that DBusThreadManager outlives |
82 // Call UnregisterEndpoint on the media object with |media_path_| and clean | 87 // BluetoothAudioSinkChromeOS. Otherwise the code below will crash. |
83 // |observers_| and transport_path_ and reset state_ and volume. | 88 // Gets Media object exported by D-Bus and unregisters the endpoint. |
armansito
2015/02/23 22:07:06
nit: probably file a bug for this and refer to the
Miao
2015/02/24 01:12:13
Done.
| |
84 // Check whether the media endpoint is registered before invoking | 89 chromeos::BluetoothMediaClient* media = |
85 // UnregisterEndpoint. | 90 DBusThreadManager::Get()->GetBluetoothMediaClient(); |
91 DCHECK(media); | |
92 | |
93 media->UnregisterEndpoint( | |
94 media_path_, | |
95 endpoint_path_, | |
96 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterSucceeded, | |
97 weak_ptr_factory_.GetWeakPtr(), callback), | |
98 base::Bind(&BluetoothAudioSinkChromeOS::OnUnregisterFailed, | |
99 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
86 } | 100 } |
87 | 101 |
88 void BluetoothAudioSinkChromeOS::AddObserver( | 102 void BluetoothAudioSinkChromeOS::AddObserver( |
89 device::BluetoothAudioSink::Observer* observer) { | 103 BluetoothAudioSink::Observer* observer) { |
90 DCHECK(observer); | 104 DCHECK(observer); |
91 observers_.AddObserver(observer); | 105 observers_.AddObserver(observer); |
92 } | 106 } |
93 | 107 |
94 void BluetoothAudioSinkChromeOS::RemoveObserver( | 108 void BluetoothAudioSinkChromeOS::RemoveObserver( |
95 device::BluetoothAudioSink::Observer* observer) { | 109 BluetoothAudioSink::Observer* observer) { |
96 DCHECK(observer); | 110 DCHECK(observer); |
97 observers_.RemoveObserver(observer); | 111 observers_.RemoveObserver(observer); |
98 } | 112 } |
99 | 113 |
100 device::BluetoothAudioSink::State BluetoothAudioSinkChromeOS::GetState() const { | 114 BluetoothAudioSink::State BluetoothAudioSinkChromeOS::GetState() const { |
101 return state_; | 115 return state_; |
102 } | 116 } |
103 | 117 |
104 uint16_t BluetoothAudioSinkChromeOS::GetVolume() const { | 118 uint16_t BluetoothAudioSinkChromeOS::GetVolume() const { |
105 return volume_; | 119 return volume_.get() ? *volume_ : 0; |
armansito
2015/02/23 22:07:06
Document at the API definition that GetVolume() re
Miao
2015/02/24 01:12:13
Changed |volume_| back to uint16_t. Possible value
| |
106 } | 120 } |
107 | 121 |
108 void BluetoothAudioSinkChromeOS::AdapterPresentChanged( | 122 void BluetoothAudioSinkChromeOS::AdapterPresentChanged( |
109 device::BluetoothAdapter* adapter, | 123 device::BluetoothAdapter* adapter, bool present) { |
110 bool present) { | |
111 VLOG(1) << "Bluetooth audio sink: Bluetooth adapter present changed: " | 124 VLOG(1) << "Bluetooth audio sink: Bluetooth adapter present changed: " |
112 << present; | 125 << present; |
113 | 126 |
114 if (adapter->IsPresent()) { | 127 if (adapter->IsPresent()) { |
115 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | 128 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED); |
116 } else { | 129 } else { |
117 StateChanged(device::BluetoothAudioSink::STATE_INVALID); | 130 adapter_->RemoveObserver(this); |
131 StateChanged(BluetoothAudioSink::STATE_INVALID); | |
118 } | 132 } |
119 } | 133 } |
120 | 134 |
121 void BluetoothAudioSinkChromeOS::MediaRemoved( | 135 void BluetoothAudioSinkChromeOS::AdapterPoweredChanged( |
122 const dbus::ObjectPath& object_path) { | 136 device::BluetoothAdapter* adapter, bool powered) { |
123 // TODO(mcchou): BUG=441581 | 137 VLOG(1) << "Bluetooth audio sink: Bluetooth adapter powered changed: " |
124 // Changes |state_| to STATE_INVALID or STATE_DISCONNECTED? | 138 << powered; |
139 | |
140 // Regardless of the new powered state, |state_| goes to STATE_DISCONNECTED. | |
141 // If false, the transport is closed, but the endpoint is still valid for use. | |
142 // If true, the previous transport has been deprecated, so the |state_| has to | |
armansito
2015/02/23 22:07:06
"deprecated" is not the correct word here, maybe "
Miao
2015/02/24 01:12:13
Done.
| |
143 // be disconnected before SetConfigruation is called. | |
144 if (state_ != BluetoothAudioSink::STATE_INVALID) | |
145 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED); | |
146 } | |
147 | |
148 void BluetoothAudioSinkChromeOS::MediaRemoved(const ObjectPath& object_path) { | |
125 if (object_path == media_path_) { | 149 if (object_path == media_path_) { |
126 StateChanged(device::BluetoothAudioSink::STATE_INVALID); | 150 StateChanged(BluetoothAudioSink::STATE_INVALID); |
127 } | 151 } |
128 } | 152 } |
129 | 153 |
130 void BluetoothAudioSinkChromeOS::MediaTransportRemoved( | 154 void BluetoothAudioSinkChromeOS::MediaTransportRemoved( |
131 const dbus::ObjectPath& object_path) { | 155 const ObjectPath& object_path) { |
132 // Whenever powered of |adapter_| turns false while present stays true, media | 156 // Whenever powered of |adapter_| turns false while present stays true, media |
133 // transport object should be removed accordingly, and the state should be | 157 // transport object should be removed accordingly, and the state should be |
134 // changed to STATE_DISCONNECTED. | 158 // changed to STATE_DISCONNECTED. |
135 if (object_path == transport_path_) { | 159 if (object_path == transport_path_) { |
136 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | 160 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED); |
137 } | 161 } |
138 } | 162 } |
139 | 163 |
140 void BluetoothAudioSinkChromeOS::MediaTransportPropertyChanged( | 164 void BluetoothAudioSinkChromeOS::MediaTransportPropertyChanged( |
141 const dbus::ObjectPath& object_path, | 165 const ObjectPath& object_path, |
142 const std::string& property_name) { | 166 const std::string& property_name) { |
143 if (object_path != transport_path_) | 167 if (object_path != transport_path_) |
144 return; | 168 return; |
145 | 169 |
146 // Retrieves the property set of the transport object with |object_path|. | 170 // Retrieves the property set of the transport object with |object_path|. |
147 chromeos::BluetoothMediaTransportClient::Properties* properties = | 171 chromeos::BluetoothMediaTransportClient::Properties* properties = |
148 DBusThreadManager::Get() | 172 DBusThreadManager::Get() |
149 ->GetBluetoothMediaTransportClient() | 173 ->GetBluetoothMediaTransportClient() |
150 ->GetProperties(object_path); | 174 ->GetProperties(object_path); |
151 | 175 |
152 // Dispatches a property changed event to the corresponding handler. | 176 // Dispatches a property changed event to the corresponding handler. |
153 if (property_name == properties->state.name()) { | 177 if (property_name == properties->state.name()) { |
154 if (properties->state.value() == | 178 if (properties->state.value() == |
155 BluetoothMediaTransportClient::kStateIdle) { | 179 BluetoothMediaTransportClient::kStateIdle) { |
156 StateChanged(device::BluetoothAudioSink::STATE_IDLE); | 180 StateChanged(BluetoothAudioSink::STATE_IDLE); |
157 } else if (properties->state.value() == | 181 } else if (properties->state.value() == |
158 BluetoothMediaTransportClient::kStatePending) { | 182 BluetoothMediaTransportClient::kStatePending) { |
159 StateChanged(device::BluetoothAudioSink::STATE_PENDING); | 183 StateChanged(BluetoothAudioSink::STATE_PENDING); |
160 } else if (properties->state.value() == | 184 } else if (properties->state.value() == |
161 BluetoothMediaTransportClient::kStateActive) { | 185 BluetoothMediaTransportClient::kStateActive) { |
162 StateChanged(device::BluetoothAudioSink::STATE_ACTIVE); | 186 StateChanged(BluetoothAudioSink::STATE_ACTIVE); |
163 } | 187 } |
164 } else if (property_name == properties->volume.name()) { | 188 } else if (property_name == properties->volume.name()) { |
165 VolumeChanged(properties->volume.value()); | 189 VolumeChanged(properties->volume.value()); |
166 } else { | 190 } else { |
167 VLOG(1) << "Bluetooth audio sink: transport property " << property_name | 191 VLOG(1) << "Bluetooth audio sink: transport property " << property_name |
168 << " changed"; | 192 << " changed"; |
169 } | 193 } |
170 } | 194 } |
171 | 195 |
172 void BluetoothAudioSinkChromeOS::SetConfiguration( | 196 void BluetoothAudioSinkChromeOS::SetConfiguration( |
173 const dbus::ObjectPath& transport_path, | 197 const ObjectPath& transport_path, |
174 const TransportProperties& properties) { | 198 const TransportProperties& properties) { |
175 VLOG(1) << "Bluetooth audio sink: SetConfiguration called"; | 199 VLOG(1) << "Bluetooth audio sink: SetConfiguration called"; |
176 transport_path_ = transport_path; | 200 transport_path_ = transport_path; |
177 | 201 |
178 // The initial state for a connection should be "idle". | 202 // The initial state for a connection should be "idle". |
179 if (properties.state != BluetoothMediaTransportClient::kStateIdle) { | 203 if (properties.state != BluetoothMediaTransportClient::kStateIdle) { |
180 VLOG(1) << "Bluetooth Audio Sink: unexpected state " << properties.state; | 204 VLOG(1) << "Bluetooth Audio Sink: unexpected state " << properties.state; |
181 return; | 205 return; |
182 } | 206 } |
183 | 207 |
184 // Updates |volume_| if the volume level is provided in |properties|. | 208 // Updates |volume_| if the volume level is provided in |properties|. |
185 if (properties.volume.get() != nullptr) | 209 if (properties.volume.get()) { |
186 VolumeChanged(*properties.volume); | 210 VolumeChanged(*properties.volume); |
211 } | |
187 | 212 |
188 StateChanged(device::BluetoothAudioSink::STATE_IDLE); | 213 StateChanged(BluetoothAudioSink::STATE_IDLE); |
189 } | 214 } |
190 | 215 |
191 void BluetoothAudioSinkChromeOS::SelectConfiguration( | 216 void BluetoothAudioSinkChromeOS::SelectConfiguration( |
192 const std::vector<uint8_t>& capabilities, | 217 const std::vector<uint8_t>& capabilities, |
193 const SelectConfigurationCallback& callback) { | 218 const SelectConfigurationCallback& callback) { |
194 VLOG(1) << "Bluetooth audio sink: SelectConfiguration called"; | 219 VLOG(1) << "Bluetooth audio sink: SelectConfiguration called"; |
195 callback.Run(options_.capabilities); | 220 callback.Run(options_.capabilities); |
196 } | 221 } |
197 | 222 |
198 void BluetoothAudioSinkChromeOS::ClearConfiguration( | 223 void BluetoothAudioSinkChromeOS::ClearConfiguration( |
199 const dbus::ObjectPath& transport_path) { | 224 const ObjectPath& transport_path) { |
225 if (transport_path != transport_path_) | |
226 return; | |
200 VLOG(1) << "Bluetooth audio sink: ClearConfiguration called"; | 227 VLOG(1) << "Bluetooth audio sink: ClearConfiguration called"; |
201 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | 228 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED); |
202 } | 229 } |
203 | 230 |
204 void BluetoothAudioSinkChromeOS::Released() { | 231 void BluetoothAudioSinkChromeOS::Released() { |
205 VLOG(1) << "Bluetooth audio sink: Released called"; | 232 VLOG(1) << "Bluetooth audio sink: Released called"; |
206 StateChanged(device::BluetoothAudioSink::STATE_INVALID); | 233 StateChanged(BluetoothAudioSink::STATE_INVALID); |
207 } | 234 } |
208 | 235 |
209 void BluetoothAudioSinkChromeOS::Register( | 236 void BluetoothAudioSinkChromeOS::Register( |
210 const device::BluetoothAudioSink::Options& options, | 237 const BluetoothAudioSink::Options& options, |
211 const base::Closure& callback, | 238 const base::Closure& callback, |
212 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 239 const BluetoothAudioSink::ErrorCallback& error_callback) { |
213 DCHECK(adapter_.get()); | 240 DCHECK(adapter_.get()); |
214 DCHECK_EQ(state_, device::BluetoothAudioSink::STATE_DISCONNECTED); | 241 DCHECK_EQ(state_, BluetoothAudioSink::STATE_DISCONNECTED); |
215 | 242 |
216 // Gets system bus. | 243 // Gets system bus. |
217 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | 244 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); |
218 | 245 |
219 // Creates a Media Endpoint with newly-generated path. | 246 // Creates a Media Endpoint with newly-generated path. |
220 endpoint_path_ = GenerateEndpointPath(); | 247 endpoint_path_ = GenerateEndpointPath(); |
221 media_endpoint_.reset( | 248 media_endpoint_.reset( |
222 BluetoothMediaEndpointServiceProvider::Create( | 249 BluetoothMediaEndpointServiceProvider::Create( |
223 system_bus, endpoint_path_, this)); | 250 system_bus, endpoint_path_, this)); |
224 | 251 |
(...skipping 21 matching lines...) Expand all Loading... | |
246 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterFailed, | 273 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterFailed, |
247 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 274 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
248 } | 275 } |
249 | 276 |
250 BluetoothMediaEndpointServiceProvider* | 277 BluetoothMediaEndpointServiceProvider* |
251 BluetoothAudioSinkChromeOS::GetEndpointServiceProvider() { | 278 BluetoothAudioSinkChromeOS::GetEndpointServiceProvider() { |
252 return media_endpoint_.get(); | 279 return media_endpoint_.get(); |
253 } | 280 } |
254 | 281 |
255 void BluetoothAudioSinkChromeOS::StateChanged( | 282 void BluetoothAudioSinkChromeOS::StateChanged( |
256 device::BluetoothAudioSink::State state) { | 283 BluetoothAudioSink::State state) { |
257 if (state == state_) | 284 if (state == state_) |
258 return; | 285 return; |
259 | 286 |
260 VLOG(1) << "Bluetooth audio sink state changed: " << state; | 287 VLOG(1) << "Bluetooth audio sink state changed: " << state; |
261 | |
262 switch (state) { | 288 switch (state) { |
263 case device::BluetoothAudioSink::STATE_INVALID: { | 289 case BluetoothAudioSink::STATE_INVALID: |
264 // TODO(mcchou): BUG=441581 | |
265 ResetMedia(); | 290 ResetMedia(); |
266 ResetTransport(); | |
267 ResetEndpoint(); | 291 ResetEndpoint(); |
268 break; | 292 case BluetoothAudioSink::STATE_DISCONNECTED: |
269 } | |
270 case device::BluetoothAudioSink::STATE_DISCONNECTED: { | |
271 // TODO(mcchou): BUG=441581 | |
272 // Clean media transport and remove the audio sink from its observer list. | |
273 ResetTransport(); | 293 ResetTransport(); |
274 break; | 294 break; |
275 } | 295 case BluetoothAudioSink::STATE_IDLE: |
276 case device::BluetoothAudioSink::STATE_IDLE: { | |
277 // TODO(mcchou): BUG=441581 | 296 // TODO(mcchou): BUG=441581 |
278 // Triggered by MediaTransportPropertyChanged and SetConfiguration. | 297 // Triggered by MediaTransportPropertyChanged and SetConfiguration. |
279 // Stop watching on file descriptor if there is one. | 298 // Stop watching on file descriptor if there is one. |
280 break; | 299 break; |
281 } | 300 case BluetoothAudioSink::STATE_PENDING: |
282 case device::BluetoothAudioSink::STATE_PENDING: { | |
283 // TODO(mcchou): BUG=441581 | 301 // TODO(mcchou): BUG=441581 |
284 // Call BluetoothMediaTransportClient::Acquire() to get fd and mtus. | 302 // Call BluetoothMediaTransportClient::Acquire() to get fd and mtus. |
285 break; | 303 break; |
286 } | 304 case BluetoothAudioSink::STATE_ACTIVE: |
287 case device::BluetoothAudioSink::STATE_ACTIVE: { | |
288 // TODO(mcchou): BUG=441581 | 305 // TODO(mcchou): BUG=441581 |
289 // Read from fd and call DataAvailable. | 306 // Read from fd and call DataAvailable. |
290 ReadFromFD(); | 307 ReadFromFD(); |
291 break; | 308 break; |
292 } | |
293 default: | 309 default: |
294 break; | 310 break; |
295 } | 311 } |
296 | 312 |
297 state_ = state; | 313 state_ = state; |
298 FOR_EACH_OBSERVER(device::BluetoothAudioSink::Observer, observers_, | 314 FOR_EACH_OBSERVER(BluetoothAudioSink::Observer, observers_, |
299 BluetoothAudioSinkStateChanged(this, state_)); | 315 BluetoothAudioSinkStateChanged(this, state_)); |
300 } | 316 } |
301 | 317 |
302 void BluetoothAudioSinkChromeOS::VolumeChanged(uint16_t volume) { | 318 void BluetoothAudioSinkChromeOS::VolumeChanged(uint16_t volume) { |
303 DCHECK_NE(volume, volume_); | 319 if (volume_.get() && volume == *volume_) |
armansito
2015/02/23 22:07:06
You could probably check "if (GetVolume() == volum
Miao
2015/02/24 01:12:13
Done.
| |
320 return; | |
304 VLOG(1) << "Bluetooth audio sink volume changed: " << volume; | 321 VLOG(1) << "Bluetooth audio sink volume changed: " << volume; |
305 volume_ = volume; | 322 volume_.reset(new uint16_t(volume)); |
armansito
2015/02/23 22:07:06
You should only reset here if volume_ is NULL, to
Miao
2015/02/24 01:12:13
Done.
| |
306 FOR_EACH_OBSERVER(device::BluetoothAudioSink::Observer, observers_, | 323 FOR_EACH_OBSERVER(BluetoothAudioSink::Observer, observers_, |
307 BluetoothAudioSinkVolumeChanged(this, volume_)); | 324 BluetoothAudioSinkVolumeChanged(this, *volume_)); |
308 } | 325 } |
309 | 326 |
310 void BluetoothAudioSinkChromeOS::OnRegisterSucceeded( | 327 void BluetoothAudioSinkChromeOS::OnRegisterSucceeded( |
311 const base::Closure& callback) { | 328 const base::Closure& callback) { |
329 DCHECK(media_endpoint_.get()); | |
312 VLOG(1) << "Bluetooth audio sink registerd"; | 330 VLOG(1) << "Bluetooth audio sink registerd"; |
313 | 331 |
314 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | 332 StateChanged(BluetoothAudioSink::STATE_DISCONNECTED); |
315 callback.Run(); | 333 callback.Run(); |
316 } | 334 } |
317 | 335 |
318 void BluetoothAudioSinkChromeOS::OnRegisterFailed( | 336 void BluetoothAudioSinkChromeOS::OnRegisterFailed( |
319 const BluetoothAudioSink::ErrorCallback& error_callback, | 337 const BluetoothAudioSink::ErrorCallback& error_callback, |
320 const std::string& error_name, | 338 const std::string& error_name, |
321 const std::string& error_message) { | 339 const std::string& error_message) { |
322 DCHECK(media_endpoint_.get()); | |
323 VLOG(1) << "Bluetooth audio sink: " << error_name << ": " << error_message; | 340 VLOG(1) << "Bluetooth audio sink: " << error_name << ": " << error_message; |
324 | 341 |
325 ResetEndpoint(); | 342 ResetEndpoint(); |
326 error_callback.Run(device::BluetoothAudioSink::ERROR_NOT_REGISTERED); | 343 error_callback.Run(BluetoothAudioSink::ERROR_NOT_REGISTERED); |
344 } | |
345 | |
346 void BluetoothAudioSinkChromeOS::OnUnregisterSucceeded( | |
347 const base::Closure& callback) { | |
348 VLOG(1) << "Bluetooth audio sink unregisterd"; | |
349 | |
350 // Once the state becomes STATE_INVALID, media, media transport and media | |
351 // endpoint will be reset. | |
352 StateChanged(BluetoothAudioSink::STATE_INVALID); | |
353 callback.Run(); | |
354 } | |
355 | |
356 void BluetoothAudioSinkChromeOS::OnUnregisterFailed( | |
357 const device::BluetoothAudioSink::ErrorCallback& error_callback, | |
358 const std::string& error_name, | |
359 const std::string& error_message) { | |
360 VLOG(1) << "Bluetooth audio sink: " << error_name << ": " << error_message; | |
361 error_callback.Run(BluetoothAudioSink::ERROR_NOT_UNREGISTERED); | |
362 } | |
363 | |
364 void BluetoothAudioSinkChromeOS::UnregisterErrorCallback( | |
365 device::BluetoothAudioSink::ErrorCode error_code) { | |
366 VLOG(1) << "Bluetooth audio sink: Error code: " << error_code; | |
327 } | 367 } |
328 | 368 |
329 void BluetoothAudioSinkChromeOS::ReadFromFD() { | 369 void BluetoothAudioSinkChromeOS::ReadFromFD() { |
330 DCHECK_GE(fd_.value(), 0); | 370 DCHECK_GE(fd_.value(), 0); |
331 | 371 |
332 // TODO(mcchou): BUG=441581 | 372 // TODO(mcchou): BUG=441581 |
333 // Read from file descriptor using watcher and create a buffer to contain the | 373 // Read from file descriptor using watcher and create a buffer to contain the |
334 // data. Notify |Observers_| while there is audio data available. | 374 // data. Notify |Observers_| while there is audio data available. |
335 } | 375 } |
336 | 376 |
337 void BluetoothAudioSinkChromeOS::ResetMedia() { | 377 void BluetoothAudioSinkChromeOS::ResetMedia() { |
338 media_path_ = dbus::ObjectPath(""); | 378 media_path_ = dbus::ObjectPath(""); |
339 } | 379 } |
340 | 380 |
341 void BluetoothAudioSinkChromeOS::ResetTransport() { | 381 void BluetoothAudioSinkChromeOS::ResetTransport() { |
382 if (transport_path_.value() == "") | |
383 return; | |
342 transport_path_ = dbus::ObjectPath(""); | 384 transport_path_ = dbus::ObjectPath(""); |
343 volume_ = 0; | 385 VolumeChanged(0); |
386 volume_.reset(); | |
344 read_mtu_ = 0; | 387 read_mtu_ = 0; |
345 write_mtu_ = 0; | 388 write_mtu_ = 0; |
346 fd_.PutValue(-1); | 389 fd_.PutValue(-1); |
347 } | 390 } |
348 | 391 |
349 void BluetoothAudioSinkChromeOS::ResetEndpoint() { | 392 void BluetoothAudioSinkChromeOS::ResetEndpoint() { |
350 endpoint_path_ = dbus::ObjectPath(""); | 393 endpoint_path_ = ObjectPath(""); |
351 media_endpoint_ = nullptr; | 394 media_endpoint_ = nullptr; |
352 } | 395 } |
353 | 396 |
354 } // namespace chromeos | 397 } // namespace chromeos |
OLD | NEW |