OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/webrtc_audio_private/webrtc_audio_privat
e_api.h" | 5 #include "chrome/browser/extensions/api/webrtc_audio_private/webrtc_audio_privat
e_api.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/message_loop/message_loop_proxy.h" | |
9 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
10 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/extensions/extension_tab_util.h" | 12 #include "chrome/browser/extensions/extension_tab_util.h" |
14 #include "content/public/browser/media_device_id.h" | 13 #include "content/public/browser/media_device_id.h" |
15 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
16 #include "extensions/browser/event_router.h" | 15 #include "extensions/browser/event_router.h" |
17 #include "extensions/common/error_utils.h" | 16 #include "extensions/common/error_utils.h" |
18 #include "media/audio/audio_manager_base.h" | 17 #include "media/audio/audio_manager_base.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 scoped_ptr<Event> event( | 91 scoped_ptr<Event> event( |
93 new Event(kEventName, make_scoped_ptr(new base::ListValue()).Pass())); | 92 new Event(kEventName, make_scoped_ptr(new base::ListValue()).Pass())); |
94 router->DispatchEventToExtension(extension_id, event.Pass()); | 93 router->DispatchEventToExtension(extension_id, event.Pass()); |
95 } | 94 } |
96 } | 95 } |
97 } | 96 } |
98 | 97 |
99 bool WebrtcAudioPrivateGetSinksFunction::RunImpl() { | 98 bool WebrtcAudioPrivateGetSinksFunction::RunImpl() { |
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
101 | 100 |
102 AudioManager::Get()->GetMessageLoop()->PostTaskAndReply( | 101 AudioManager::Get()->GetTaskRunner()->PostTaskAndReply( |
103 FROM_HERE, | 102 FROM_HERE, |
104 base::Bind(&WebrtcAudioPrivateGetSinksFunction::DoQuery, this), | 103 base::Bind(&WebrtcAudioPrivateGetSinksFunction::DoQuery, this), |
105 base::Bind(&WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread, this)); | 104 base::Bind(&WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread, this)); |
106 return true; | 105 return true; |
107 } | 106 } |
108 | 107 |
109 void WebrtcAudioPrivateGetSinksFunction::DoQuery() { | 108 void WebrtcAudioPrivateGetSinksFunction::DoQuery() { |
110 DCHECK(AudioManager::Get()->GetMessageLoop()->BelongsToCurrentThread()); | 109 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
111 | 110 |
112 AudioDeviceNames device_names; | 111 AudioDeviceNames device_names; |
113 AudioManager::Get()->GetAudioOutputDeviceNames(&device_names); | 112 AudioManager::Get()->GetAudioOutputDeviceNames(&device_names); |
114 | 113 |
115 std::vector<linked_ptr<wap::SinkInfo> > results; | 114 std::vector<linked_ptr<wap::SinkInfo> > results; |
116 for (AudioDeviceNames::const_iterator it = device_names.begin(); | 115 for (AudioDeviceNames::const_iterator it = device_names.begin(); |
117 it != device_names.end(); | 116 it != device_names.end(); |
118 ++it) { | 117 ++it) { |
119 linked_ptr<wap::SinkInfo> info(new wap::SinkInfo); | 118 linked_ptr<wap::SinkInfo> info(new wap::SinkInfo); |
120 info->sink_id = it->unique_id; | 119 info->sink_id = it->unique_id; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 if (result.empty()) { | 190 if (result.empty()) { |
192 DVLOG(2) << "Received empty ID, replacing with default ID."; | 191 DVLOG(2) << "Received empty ID, replacing with default ID."; |
193 result = media::AudioManagerBase::kDefaultDeviceId; | 192 result = media::AudioManagerBase::kDefaultDeviceId; |
194 } | 193 } |
195 results_.reset(wap::GetActiveSink::Results::Create(result).release()); | 194 results_.reset(wap::GetActiveSink::Results::Create(result).release()); |
196 SendResponse(true); | 195 SendResponse(true); |
197 } | 196 } |
198 | 197 |
199 WebrtcAudioPrivateSetActiveSinkFunction:: | 198 WebrtcAudioPrivateSetActiveSinkFunction:: |
200 WebrtcAudioPrivateSetActiveSinkFunction() | 199 WebrtcAudioPrivateSetActiveSinkFunction() |
201 : message_loop_(base::MessageLoopProxy::current()), | 200 : task_runner_(base::MessageLoopProxy::current()), |
202 tab_id_(0), | 201 tab_id_(0), |
203 num_remaining_sink_ids_(0) { | 202 num_remaining_sink_ids_(0) { |
204 } | 203 } |
205 | 204 |
206 WebrtcAudioPrivateSetActiveSinkFunction:: | 205 WebrtcAudioPrivateSetActiveSinkFunction:: |
207 ~WebrtcAudioPrivateSetActiveSinkFunction() { | 206 ~WebrtcAudioPrivateSetActiveSinkFunction() { |
208 } | 207 } |
209 | 208 |
210 bool WebrtcAudioPrivateSetActiveSinkFunction::RunImpl() { | 209 bool WebrtcAudioPrivateSetActiveSinkFunction::RunImpl() { |
211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 25 matching lines...) Expand all Loading... |
237 controllers.begin(); | 236 controllers.begin(); |
238 for (; it != controllers.end(); ++it) { | 237 for (; it != controllers.end(); ++it) { |
239 (*it)->SwitchOutputDevice(sink_id_, base::Bind( | 238 (*it)->SwitchOutputDevice(sink_id_, base::Bind( |
240 &WebrtcAudioPrivateSetActiveSinkFunction::SwitchDone, this)); | 239 &WebrtcAudioPrivateSetActiveSinkFunction::SwitchDone, this)); |
241 } | 240 } |
242 } | 241 } |
243 } | 242 } |
244 | 243 |
245 void WebrtcAudioPrivateSetActiveSinkFunction::SwitchDone() { | 244 void WebrtcAudioPrivateSetActiveSinkFunction::SwitchDone() { |
246 if (--num_remaining_sink_ids_ == 0) { | 245 if (--num_remaining_sink_ids_ == 0) { |
247 message_loop_->PostTask( | 246 task_runner_->PostTask( |
248 FROM_HERE, | 247 FROM_HERE, |
249 base::Bind(&WebrtcAudioPrivateSetActiveSinkFunction::DoneOnUIThread, | 248 base::Bind(&WebrtcAudioPrivateSetActiveSinkFunction::DoneOnUIThread, |
250 this)); | 249 this)); |
251 } | 250 } |
252 } | 251 } |
253 | 252 |
254 void WebrtcAudioPrivateSetActiveSinkFunction::DoneOnUIThread() { | 253 void WebrtcAudioPrivateSetActiveSinkFunction::DoneOnUIThread() { |
255 SendResponse(true); | 254 SendResponse(true); |
256 } | 255 } |
257 | 256 |
258 WebrtcAudioPrivateGetAssociatedSinkFunction:: | 257 WebrtcAudioPrivateGetAssociatedSinkFunction:: |
259 WebrtcAudioPrivateGetAssociatedSinkFunction() { | 258 WebrtcAudioPrivateGetAssociatedSinkFunction() { |
260 } | 259 } |
261 | 260 |
262 WebrtcAudioPrivateGetAssociatedSinkFunction:: | 261 WebrtcAudioPrivateGetAssociatedSinkFunction:: |
263 ~WebrtcAudioPrivateGetAssociatedSinkFunction() { | 262 ~WebrtcAudioPrivateGetAssociatedSinkFunction() { |
264 } | 263 } |
265 | 264 |
266 bool WebrtcAudioPrivateGetAssociatedSinkFunction::RunImpl() { | 265 bool WebrtcAudioPrivateGetAssociatedSinkFunction::RunImpl() { |
267 params_ = wap::GetAssociatedSink::Params::Create(*args_); | 266 params_ = wap::GetAssociatedSink::Params::Create(*args_); |
268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
269 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 268 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
270 | 269 |
271 AudioManager::Get()->GetMessageLoop()->PostTaskAndReply( | 270 AudioManager::Get()->GetTaskRunner()->PostTaskAndReply( |
272 FROM_HERE, | 271 FROM_HERE, |
273 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: | 272 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: |
274 GetDevicesOnDeviceThread, this), | 273 GetDevicesOnDeviceThread, this), |
275 base::Bind( | 274 base::Bind( |
276 &WebrtcAudioPrivateGetAssociatedSinkFunction::OnGetDevicesDone, | 275 &WebrtcAudioPrivateGetAssociatedSinkFunction::OnGetDevicesDone, |
277 this)); | 276 this)); |
278 | 277 |
279 return true; | 278 return true; |
280 } | 279 } |
281 | 280 |
282 void WebrtcAudioPrivateGetAssociatedSinkFunction::GetDevicesOnDeviceThread() { | 281 void WebrtcAudioPrivateGetAssociatedSinkFunction::GetDevicesOnDeviceThread() { |
283 DCHECK(AudioManager::Get()->GetMessageLoop()->BelongsToCurrentThread()); | 282 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
284 AudioManager::Get()->GetAudioInputDeviceNames(&source_devices_); | 283 AudioManager::Get()->GetAudioInputDeviceNames(&source_devices_); |
285 } | 284 } |
286 | 285 |
287 void WebrtcAudioPrivateGetAssociatedSinkFunction::OnGetDevicesDone() { | 286 void WebrtcAudioPrivateGetAssociatedSinkFunction::OnGetDevicesDone() { |
288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
289 BrowserThread::PostTaskAndReplyWithResult( | 288 BrowserThread::PostTaskAndReplyWithResult( |
290 BrowserThread::IO, | 289 BrowserThread::IO, |
291 FROM_HERE, | 290 FROM_HERE, |
292 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: | 291 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: |
293 GetRawSourceIDOnIOThread, | 292 GetRawSourceIDOnIOThread, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 } | 324 } |
326 } | 325 } |
327 | 326 |
328 return raw_source_id; | 327 return raw_source_id; |
329 } | 328 } |
330 | 329 |
331 void WebrtcAudioPrivateGetAssociatedSinkFunction::OnGetRawSourceIDDone( | 330 void WebrtcAudioPrivateGetAssociatedSinkFunction::OnGetRawSourceIDDone( |
332 const std::string& raw_source_id) { | 331 const std::string& raw_source_id) { |
333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
334 base::PostTaskAndReplyWithResult( | 333 base::PostTaskAndReplyWithResult( |
335 AudioManager::Get()->GetMessageLoop(), | 334 AudioManager::Get()->GetTaskRunner(), |
336 FROM_HERE, | 335 FROM_HERE, |
337 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: | 336 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: |
338 GetAssociatedSinkOnDeviceThread, | 337 GetAssociatedSinkOnDeviceThread, |
339 this, | 338 this, |
340 raw_source_id), | 339 raw_source_id), |
341 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: | 340 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: |
342 OnGetAssociatedSinkDone, | 341 OnGetAssociatedSinkDone, |
343 this)); | 342 this)); |
344 } | 343 } |
345 | 344 |
346 std::string | 345 std::string |
347 WebrtcAudioPrivateGetAssociatedSinkFunction::GetAssociatedSinkOnDeviceThread( | 346 WebrtcAudioPrivateGetAssociatedSinkFunction::GetAssociatedSinkOnDeviceThread( |
348 const std::string& raw_source_id) { | 347 const std::string& raw_source_id) { |
349 DCHECK(AudioManager::Get()->GetMessageLoop()->BelongsToCurrentThread()); | 348 DCHECK(AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
350 | 349 |
351 // We return an empty string if there is no associated output device. | 350 // We return an empty string if there is no associated output device. |
352 std::string result; | 351 std::string result; |
353 if (!raw_source_id.empty()) { | 352 if (!raw_source_id.empty()) { |
354 result = AudioManager::Get()->GetAssociatedOutputDeviceID(raw_source_id); | 353 result = AudioManager::Get()->GetAssociatedOutputDeviceID(raw_source_id); |
355 } | 354 } |
356 | 355 |
357 return result; | 356 return result; |
358 } | 357 } |
359 | 358 |
360 void WebrtcAudioPrivateGetAssociatedSinkFunction::OnGetAssociatedSinkDone( | 359 void WebrtcAudioPrivateGetAssociatedSinkFunction::OnGetAssociatedSinkDone( |
361 const std::string& associated_sink_id) { | 360 const std::string& associated_sink_id) { |
362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
363 | 362 |
364 results_.reset( | 363 results_.reset( |
365 wap::GetAssociatedSink::Results::Create(associated_sink_id).release()); | 364 wap::GetAssociatedSink::Results::Create(associated_sink_id).release()); |
366 SendResponse(true); | 365 SendResponse(true); |
367 } | 366 } |
368 | 367 |
369 } // namespace extensions | 368 } // namespace extensions |
OLD | NEW |