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