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

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

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

Powered by Google App Engine
This is Rietveld 408576698