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 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "chromeos/dbus/dbus_thread_manager.h" | |
11 | |
12 namespace { | |
13 | |
14 // TODO(mcchou): Add the constant to dbus/service_constants.h. | |
15 const char kBluetoothAudioSinkServicePath[] = "/org/chromium/AudioSink"; | |
16 const char kBluetoothAudioSinkUUID[] = "0000110b-0000-1000-8000-00805f9b34fb"; | |
17 | |
18 dbus::ObjectPath GenerateEndpointPath() { | |
19 static unsigned int sequence_number = 0; | |
20 ++sequence_number; | |
21 std::stringstream path; | |
22 path << kBluetoothAudioSinkServicePath << "/endpoint" << sequence_number; | |
23 return dbus::ObjectPath(path.str()); | |
24 } | |
25 | |
26 } // namespace | |
10 | 27 |
11 namespace chromeos { | 28 namespace chromeos { |
12 | 29 |
13 BluetoothAudioSinkChromeOS::BluetoothAudioSinkChromeOS( | 30 BluetoothAudioSinkChromeOS::BluetoothAudioSinkChromeOS( |
14 BluetoothAdapterChromeOS* adapter) | 31 BluetoothAdapterChromeOS* adapter) |
15 : state_(device::BluetoothAudioSink::STATE_INVALID), | 32 : state_(device::BluetoothAudioSink::STATE_INVALID), |
16 present_(false), | 33 present_(false), |
17 powered_(false), | 34 powered_(false), |
18 volume_(0), | 35 volume_(0), |
19 read_mtu_(0), | 36 read_mtu_(0), |
20 write_mtu_(0), | 37 write_mtu_(0), |
21 adapter_(adapter), | 38 adapter_(adapter), |
22 weak_ptr_factory_(this) { | 39 weak_ptr_factory_(this) { |
23 DCHECK(adapter_); | 40 DCHECK(adapter_); |
24 | 41 |
25 present_ = adapter_->IsPresent(); | 42 present_ = adapter_->IsPresent(); |
26 powered_ = adapter_->IsPowered(); | 43 powered_ = adapter_->IsPowered(); |
27 if (present_ && powered_) | 44 if (present_ && powered_) |
28 state_ = device::BluetoothAudioSink::STATE_DISCONNECTED; | 45 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); |
29 adapter_->AddObserver(this); | 46 adapter_->AddObserver(this); |
30 } | 47 } |
31 | 48 |
32 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() { | 49 BluetoothAudioSinkChromeOS::~BluetoothAudioSinkChromeOS() { |
33 DCHECK(adapter_); | 50 DCHECK(adapter_); |
34 adapter_->RemoveObserver(this); | 51 adapter_->RemoveObserver(this); |
35 } | 52 } |
36 | 53 |
37 void BluetoothAudioSinkChromeOS::AddObserver( | 54 void BluetoothAudioSinkChromeOS::AddObserver( |
38 device::BluetoothAudioSink::Observer* observer) { | 55 device::BluetoothAudioSink::Observer* observer) { |
(...skipping 11 matching lines...) Expand all Loading... | |
50 return state_; | 67 return state_; |
51 } | 68 } |
52 | 69 |
53 uint16_t BluetoothAudioSinkChromeOS::GetVolume() const { | 70 uint16_t BluetoothAudioSinkChromeOS::GetVolume() const { |
54 return volume_; | 71 return volume_; |
55 } | 72 } |
56 | 73 |
57 void BluetoothAudioSinkChromeOS::AdapterPresentChanged( | 74 void BluetoothAudioSinkChromeOS::AdapterPresentChanged( |
58 device::BluetoothAdapter* adapter, | 75 device::BluetoothAdapter* adapter, |
59 bool present) { | 76 bool present) { |
60 // TODO(mcchou): BUG=441581 | 77 if (present == present_) return; |
armansito
2015/01/27 04:38:06
nit: Put return in its own line.
armansito
2015/01/27 04:38:06
This check isn't really necessary, since the adapt
Miao
2015/01/28 02:05:01
Removed.
| |
61 // If |persent| is true, change state to |STATE_DISCONNECTED| and call | 78 VLOG(1) << "Bluetooth audio sink: Bluetooth adapter present changed: " |
62 // StateChanged(). Otherwise, change state to |STATE_INVALID| and call | 79 << present; |
63 // StateChanged. | 80 present_ = present; |
armansito
2015/01/27 04:38:06
I don't think that |present_| and |powered_| are n
Miao
2015/01/28 02:05:01
Removed.
| |
81 if (present_ && adapter->IsPowered()) { | |
82 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | |
83 } else { | |
armansito
2015/01/27 04:38:06
This means that the state will become INVALID if |
Miao
2015/01/28 02:05:01
Will remove AdapterPoweredChanged and let MediaTra
| |
84 StateChanged(device::BluetoothAudioSink::STATE_INVALID); | |
85 } | |
64 } | 86 } |
65 | 87 |
66 void BluetoothAudioSinkChromeOS::AdapterPoweredChanged( | 88 void BluetoothAudioSinkChromeOS::AdapterPoweredChanged( |
67 device::BluetoothAdapter* adapter, | 89 device::BluetoothAdapter* adapter, |
68 bool powered) { | 90 bool powered) { |
69 // TODO(mcchou): BUG=441581 | 91 if (powered == powered_) return; |
armansito
2015/01/27 04:38:05
Same as above.
Miao
2015/01/28 02:05:01
Removed.
| |
70 // If |powered| is true, change state to |STATE_DISCONNECTED| and call | 92 VLOG(1) << "Bluetooth audio sink: Bluetooth adapter powered changed: " |
71 // StateChanged(). Otherwise, change state to |STATE_INVALID| and call | 93 << powered; |
72 // StateChanged. | 94 powered_ = powered; |
95 if (powered_ && adapter->IsPresent()) { | |
96 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | |
97 } else { | |
98 StateChanged(device::BluetoothAudioSink::STATE_INVALID); | |
armansito
2015/01/27 04:38:05
Same logic as above. Besides this code is essentia
Miao
2015/01/28 02:05:01
Removed.
| |
99 } | |
73 } | 100 } |
74 | 101 |
75 void BluetoothAudioSinkChromeOS::MediaRemoved( | 102 void BluetoothAudioSinkChromeOS::MediaRemoved( |
76 const dbus::ObjectPath& object_path) { | 103 const dbus::ObjectPath& object_path) { |
77 // TODO(mcchou): BUG=441581 | 104 // TODO(mcchou): BUG=441581 |
78 // Check if |object_path| equals to |media_path_|. If true, change the state | 105 // Check if |object_path| equals to |media_path_|. If true, change the state |
79 // of the audio sink, call StateChanged and reset the audio sink. | 106 // of the audio sink, call StateChanged and reset the audio sink. |
80 } | 107 } |
81 | 108 |
82 void BluetoothAudioSinkChromeOS::MediaTransportRemoved( | 109 void BluetoothAudioSinkChromeOS::MediaTransportRemoved( |
(...skipping 16 matching lines...) Expand all Loading... | |
99 const dbus::MessageReader& properties) { | 126 const dbus::MessageReader& properties) { |
100 // TODO(mcchou): BUG=441581 | 127 // TODO(mcchou): BUG=441581 |
101 // Update |transport_path_| and store properties if needed. | 128 // Update |transport_path_| and store properties if needed. |
102 } | 129 } |
103 | 130 |
104 void BluetoothAudioSinkChromeOS::SelectConfiguration( | 131 void BluetoothAudioSinkChromeOS::SelectConfiguration( |
105 const std::vector<uint8_t>& capabilities, | 132 const std::vector<uint8_t>& capabilities, |
106 const SelectConfigurationCallback& callback) { | 133 const SelectConfigurationCallback& callback) { |
107 // TODO(mcchou): BUG=441581 | 134 // TODO(mcchou): BUG=441581 |
108 // Use SelectConfigurationCallback to return the agreed capabilities. | 135 // Use SelectConfigurationCallback to return the agreed capabilities. |
136 VLOG(1) << "Bluetooth audio sink: SelectConfiguration called"; | |
137 callback.Run(options_.capabilities); | |
109 } | 138 } |
110 | 139 |
111 void BluetoothAudioSinkChromeOS::ClearConfiguration( | 140 void BluetoothAudioSinkChromeOS::ClearConfiguration( |
112 const dbus::ObjectPath& transport_path) { | 141 const dbus::ObjectPath& transport_path) { |
113 // TODO(mcchou): BUG=441581 | 142 // TODO(mcchou): BUG=441581 |
114 // Reset the configuration to the default one and close IOBuffer. | 143 // Reset the configuration to the default one and close IOBuffer. |
115 } | 144 } |
116 | 145 |
117 void BluetoothAudioSinkChromeOS::Release() { | 146 void BluetoothAudioSinkChromeOS::Release() { |
118 // TODO(mcchou): BUG=441581 | 147 // TODO(mcchou): BUG=441581 |
119 // Let the audio sink does the clean-up and do nothing here. | 148 // Let the audio sink does the clean-up and do nothing here. |
120 } | 149 } |
121 | 150 |
122 void BluetoothAudioSinkChromeOS::Register( | 151 void BluetoothAudioSinkChromeOS::Register( |
123 const device::BluetoothAudioSink::Options& options, | 152 const device::BluetoothAudioSink::Options& options, |
124 const base::Closure& callback, | 153 const base::Closure& callback, |
125 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 154 const device::BluetoothAudioSink::ErrorCallback& error_callback) { |
126 // TODO(mcchou): BUG=441581 | 155 // TODO(mcchou): BUG=441581 |
127 // Get Media object, initiate an Media Endpoint with options, and return the | 156 // Get Media object, initiate an Media Endpoint with options, and return the |
128 // audio sink via callback. Add the audio sink as observer of both Media and | 157 // audio sink via callback. Add the audio sink as observer of both Media and |
129 // Media Transport. | 158 // Media Transport. |
159 DCHECK(adapter_); | |
160 DCHECK_EQ(state_, device::BluetoothAudioSink::STATE_DISCONNECTED); | |
161 | |
162 // Gets system bus. | |
163 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | |
164 | |
165 // Creates a Media Endpoint with newly-generated path. | |
166 dbus::ObjectPath endpoint_path_ = GenerateEndpointPath(); | |
armansito
2015/01/27 04:38:05
This code shouldn't compile, since you're redeclar
Miao
2015/01/28 02:05:01
The compilation will pass, since it will be a loca
| |
167 media_endpoint_.reset( | |
168 BluetoothMediaEndpointServiceProvider::Create( | |
169 system_bus, endpoint_path_, this)); | |
170 | |
171 DCHECK(media_endpoint_.get()); | |
172 | |
173 // Creates endpoint properties with |options|. | |
174 options_ = options; | |
175 chromeos::BluetoothMediaClient::EndpointProperties endpoint_properties; | |
176 endpoint_properties.uuid = std::string(kBluetoothAudioSinkUUID); | |
177 endpoint_properties.codec = options_.codec; | |
178 endpoint_properties.capabilities = options_.capabilities; | |
179 | |
180 // Gets Media object exported by D-Bus and registers the endpoint. | |
181 chromeos::BluetoothMediaClient* media = | |
182 DBusThreadManager::Get()->GetBluetoothMediaClient(); | |
183 media->AddObserver(this); | |
184 media_path_ = adapter_->object_path(); | |
185 media->RegisterEndpoint( | |
186 media_path_, | |
187 endpoint_path_, | |
188 endpoint_properties, | |
189 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterSucceeded, | |
190 weak_ptr_factory_.GetWeakPtr(), callback), | |
191 base::Bind(&BluetoothAudioSinkChromeOS::OnRegisterFailed, | |
192 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
130 } | 193 } |
131 | 194 |
132 void BluetoothAudioSinkChromeOS::Unregister( | 195 void BluetoothAudioSinkChromeOS::Unregister( |
133 const base::Closure& callback, | 196 const base::Closure& callback, |
134 const device::BluetoothAudioSink::ErrorCallback& error_callback) { | 197 const device::BluetoothAudioSink::ErrorCallback& error_callback) { |
135 // TODO(mcchou): BUG=441581 | 198 // TODO(mcchou): BUG=441581 |
136 // Clean |observers_| and |transport_path_| and reset |state_| and |volume_|. | 199 // Call UnregisterEndpoint on the media object with |media_path_| and clean |
200 // |observers_| and transport_path_ and reset state_ and volume. | |
137 } | 201 } |
138 | 202 |
139 void BluetoothAudioSinkChromeOS::StateChanged( | 203 void BluetoothAudioSinkChromeOS::StateChanged( |
140 device::BluetoothAudioSink::State state) { | 204 device::BluetoothAudioSink::State state) { |
141 DCHECK_NE(state, state_); | 205 if (state == state_) return; |
armansito
2015/01/27 04:38:06
nit: return; in its own line.
Miao
2015/01/28 02:05:01
Done.
| |
142 VLOG(1) << "Bluetooth audio sink state changed: " << state; | 206 VLOG(1) << "Bluetooth audio sink state changed: " << state; |
207 switch (state) { | |
208 case device::BluetoothAudioSink::STATE_INVALID: { | |
209 // TODO(mcchou): BUG=441581 | |
210 // Clean media, media transport and media endpoint. | |
211 break; | |
212 } | |
213 case device::BluetoothAudioSink::STATE_DISCONNECTED: { | |
214 // TODO(mcchou): BUG=441581 | |
215 // Clean media transport. | |
216 break; | |
217 } | |
218 case device::BluetoothAudioSink::STATE_IDLE: { | |
219 // TODO(mcchou): BUG=441581 | |
220 // Triggered by MediaTransportPropertyChanged. Stop watching on file | |
221 // descriptor if there is one. | |
222 break; | |
223 } | |
224 case device::BluetoothAudioSink::STATE_PENDING: { | |
225 // TODO(mcchou): BUG=441581 | |
226 // Call BluetoothMediaTransportClient::Acquire() to get fd and mtus. | |
227 break; | |
228 } | |
229 case device::BluetoothAudioSink::STATE_ACTIVE: { | |
230 // TODO(mcchou): BUG=441581 | |
231 // Read from fd and call DataAvailable. | |
232 ReadFromFD(); | |
233 break; | |
234 } | |
235 default: { | |
armansito
2015/01/27 04:38:05
No need for empty scope here.
Miao
2015/01/28 02:05:01
Done.
| |
236 } | |
237 } | |
238 | |
143 state_ = state; | 239 state_ = state; |
144 FOR_EACH_OBSERVER(device::BluetoothAudioSink::Observer, observers_, | 240 FOR_EACH_OBSERVER(device::BluetoothAudioSink::Observer, observers_, |
145 BluetoothAudioSinkStateChanged(this, state_)); | 241 BluetoothAudioSinkStateChanged(this, state_)); |
146 } | 242 } |
147 | 243 |
148 void BluetoothAudioSinkChromeOS::VolumeChanged(uint16_t volume) { | 244 void BluetoothAudioSinkChromeOS::VolumeChanged(uint16_t volume) { |
149 DCHECK_NE(volume, volume_); | 245 DCHECK_NE(volume, volume_); |
150 VLOG(1) << "Bluetooth audio sink volume changed: " << volume; | 246 VLOG(1) << "Bluetooth audio sink volume changed: " << volume; |
151 volume_ = volume; | 247 volume_ = volume; |
152 FOR_EACH_OBSERVER(device::BluetoothAudioSink::Observer, observers_, | 248 FOR_EACH_OBSERVER(device::BluetoothAudioSink::Observer, observers_, |
153 BluetoothAudioSinkVolumeChanged(this, volume_)); | 249 BluetoothAudioSinkVolumeChanged(this, volume_)); |
154 } | 250 } |
155 | 251 |
252 void BluetoothAudioSinkChromeOS::OnRegisterSucceeded( | |
253 const base::Closure& callback) { | |
254 VLOG(1) << "Bluetooth audio sink registerd"; | |
255 | |
256 StateChanged(device::BluetoothAudioSink::STATE_DISCONNECTED); | |
257 callback.Run(); | |
258 } | |
259 | |
260 void BluetoothAudioSinkChromeOS::OnRegisterFailed( | |
261 const BluetoothAudioSink::ErrorCallback& error_back, | |
262 const std::string& error_name, | |
263 const std::string& error_message) { | |
264 DCHECK(media_endpoint_.get()); | |
265 VLOG(1) << "Bluetooth audio sink: " << error_name << ": " << error_message; | |
266 | |
267 endpoint_path_ = dbus::ObjectPath(""); | |
268 media_endpoint_ = nullptr; | |
269 error_back.Run(device::BluetoothAudioSink::ERROR_NOT_REGISTERED); | |
270 } | |
271 | |
156 void BluetoothAudioSinkChromeOS::ReadFromFD() { | 272 void BluetoothAudioSinkChromeOS::ReadFromFD() { |
157 DCHECK_GE(fd_.value(), 0); | 273 DCHECK_GE(fd_.value(), 0); |
158 | 274 |
159 // TODO(mcchou): BUG=441581 | 275 // TODO(mcchou): BUG=441581 |
160 // Read from file descriptor using watcher and create a buffer to contain the | 276 // Read from file descriptor using watcher and create a buffer to contain the |
161 // data. Notify |Observers_| while there is audio data available. | 277 // data. Notify |Observers_| while there is audio data available. |
162 } | 278 } |
163 | 279 |
164 } // namespace chromeos | 280 } // namespace chromeos |
OLD | NEW |