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