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 <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
16 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 16 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
17 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
19 #include "content/public/browser/media_device_id.h" | 19 #include "content/public/browser/media_device_id.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "extensions/browser/event_router.h" | 21 #include "extensions/browser/event_router.h" |
22 #include "extensions/browser/extension_registry.h" | 22 #include "extensions/browser/extension_registry.h" |
23 #include "extensions/common/error_utils.h" | 23 #include "extensions/common/error_utils.h" |
24 #include "extensions/common/permissions/permissions_data.h" | 24 #include "extensions/common/permissions/permissions_data.h" |
25 #include "media/audio/audio_device_description.h" | 25 #include "media/audio/audio_device_description.h" |
26 #include "media/audio/audio_output_controller.h" | |
27 #include "url/gurl.h" | 26 #include "url/gurl.h" |
28 #include "url/origin.h" | 27 #include "url/origin.h" |
29 | 28 |
30 namespace extensions { | 29 namespace extensions { |
31 | 30 |
32 using content::BrowserThread; | 31 using content::BrowserThread; |
33 using content::RenderProcessHost; | 32 using content::RenderProcessHost; |
34 using media::AudioDeviceDescriptions; | 33 using media::AudioDeviceDescriptions; |
35 using media::AudioManager; | 34 using media::AudioManager; |
36 | 35 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 BrowserThread::IO, FROM_HERE, | 131 BrowserThread::IO, FROM_HERE, |
133 base::BindOnce(&WebrtcAudioPrivateFunction::OnOutputDeviceDescriptions, | 132 base::BindOnce(&WebrtcAudioPrivateFunction::OnOutputDeviceDescriptions, |
134 this, base::Passed(&device_descriptions))); | 133 this, base::Passed(&device_descriptions))); |
135 } | 134 } |
136 | 135 |
137 void WebrtcAudioPrivateFunction::OnOutputDeviceDescriptions( | 136 void WebrtcAudioPrivateFunction::OnOutputDeviceDescriptions( |
138 std::unique_ptr<AudioDeviceDescriptions> device_descriptions) { | 137 std::unique_ptr<AudioDeviceDescriptions> device_descriptions) { |
139 NOTREACHED(); | 138 NOTREACHED(); |
140 } | 139 } |
141 | 140 |
142 bool WebrtcAudioPrivateFunction::GetControllerList(const RequestInfo& request) { | |
143 content::RenderProcessHost* rph = nullptr; | |
144 | |
145 // If |guest_process_id| is defined, directly use this id to find the | |
146 // corresponding RenderProcessHost. | |
147 if (request.guest_process_id.get()) { | |
148 rph = content::RenderProcessHost::FromID(*request.guest_process_id); | |
149 } else if (request.tab_id.get()) { | |
150 int tab_id = *request.tab_id; | |
151 content::WebContents* contents = NULL; | |
152 if (!ExtensionTabUtil::GetTabById(tab_id, GetProfile(), true, NULL, NULL, | |
153 &contents, NULL)) { | |
154 error_ = extensions::ErrorUtils::FormatErrorMessage( | |
155 extensions::tabs_constants::kTabNotFoundError, | |
156 base::IntToString(tab_id)); | |
157 return false; | |
158 } | |
159 rph = contents->GetRenderProcessHost(); | |
160 } else { | |
161 return false; | |
162 } | |
163 | |
164 if (!rph) | |
165 return false; | |
166 | |
167 rph->GetAudioOutputControllers( | |
168 base::Bind(&WebrtcAudioPrivateFunction::OnControllerList, this)); | |
169 return true; | |
170 } | |
171 | |
172 void WebrtcAudioPrivateFunction::OnControllerList( | |
173 const content::RenderProcessHost::AudioOutputControllerList& list) { | |
174 NOTREACHED(); | |
175 } | |
176 | |
177 void WebrtcAudioPrivateFunction::CalculateHMAC(const std::string& raw_id) { | 141 void WebrtcAudioPrivateFunction::CalculateHMAC(const std::string& raw_id) { |
178 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 142 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
179 BrowserThread::PostTask( | 143 BrowserThread::PostTask( |
180 BrowserThread::IO, FROM_HERE, | 144 BrowserThread::IO, FROM_HERE, |
181 base::BindOnce(&WebrtcAudioPrivateFunction::CalculateHMAC, this, | 145 base::BindOnce(&WebrtcAudioPrivateFunction::CalculateHMAC, this, |
182 raw_id)); | 146 raw_id)); |
183 return; | 147 return; |
184 } | 148 } |
185 | 149 |
186 std::string hmac = CalculateHMACImpl(raw_id); | 150 std::string hmac = CalculateHMACImpl(raw_id); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 BrowserThread::PostTask( | 255 BrowserThread::PostTask( |
292 BrowserThread::UI, FROM_HERE, | 256 BrowserThread::UI, FROM_HERE, |
293 base::BindOnce(&WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread, | 257 base::BindOnce(&WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread, |
294 this)); | 258 this)); |
295 } | 259 } |
296 | 260 |
297 void WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread() { | 261 void WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread() { |
298 SendResponse(true); | 262 SendResponse(true); |
299 } | 263 } |
300 | 264 |
301 bool WebrtcAudioPrivateGetActiveSinkFunction::RunAsync() { | |
302 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
303 InitDeviceIDSalt(); | |
304 | |
305 std::unique_ptr<wap::GetActiveSink::Params> params( | |
306 wap::GetActiveSink::Params::Create(*args_)); | |
307 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
308 | |
309 return GetControllerList(params->request); | |
310 } | |
311 | |
312 void WebrtcAudioPrivateGetActiveSinkFunction::OnControllerList( | |
313 const RenderProcessHost::AudioOutputControllerList& controllers) { | |
314 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
315 | |
316 if (controllers.empty()) { | |
317 // If there is no current audio stream for the rvh, we return an | |
318 // empty string as the sink ID. | |
319 DVLOG(2) << "chrome.webrtcAudioPrivate.getActiveSink: No controllers."; | |
320 results_.reset( | |
321 wap::GetActiveSink::Results::Create(std::string()).release()); | |
322 SendResponse(true); | |
323 } else { | |
324 DVLOG(2) << "chrome.webrtcAudioPrivate.getActiveSink: " | |
325 << controllers.size() << " controllers."; | |
326 // TODO(joi): Debug-only, DCHECK that all items have the same ID. | |
327 | |
328 // Send the raw ID through CalculateHMAC, and send the result in | |
329 // OnHMACCalculated. | |
330 (*controllers.begin())->GetOutputDeviceId( | |
331 base::Bind(&WebrtcAudioPrivateGetActiveSinkFunction::CalculateHMAC, | |
332 this)); | |
333 } | |
334 } | |
335 | |
336 void WebrtcAudioPrivateGetActiveSinkFunction::OnHMACCalculated( | |
337 const std::string& hmac_id) { | |
338 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
339 | |
340 std::string result = hmac_id; | |
341 if (result.empty()) { | |
342 DVLOG(2) << "Received empty ID, replacing with default ID."; | |
343 result = media::AudioDeviceDescription::kDefaultDeviceId; | |
344 } | |
345 results_ = wap::GetActiveSink::Results::Create(result); | |
346 SendResponse(true); | |
347 } | |
348 | |
349 WebrtcAudioPrivateSetActiveSinkFunction:: | |
350 WebrtcAudioPrivateSetActiveSinkFunction() | |
351 : num_remaining_sink_ids_(0) { | |
352 } | |
353 | |
354 WebrtcAudioPrivateSetActiveSinkFunction:: | |
355 ~WebrtcAudioPrivateSetActiveSinkFunction() { | |
356 } | |
357 | |
358 bool WebrtcAudioPrivateSetActiveSinkFunction::RunAsync() { | |
359 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
360 std::unique_ptr<wap::SetActiveSink::Params> params( | |
361 wap::SetActiveSink::Params::Create(*args_)); | |
362 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
363 | |
364 InitDeviceIDSalt(); | |
365 | |
366 if (params->request.guest_process_id.get()) { | |
367 request_info_.guest_process_id.reset( | |
368 new int(*params->request.guest_process_id)); | |
369 } else if (params->request.tab_id.get()) { | |
370 request_info_.tab_id.reset(new int(*params->request.tab_id)); | |
371 } else { | |
372 return false; | |
373 } | |
374 | |
375 sink_id_ = params->sink_id; | |
376 | |
377 return GetControllerList(request_info_); | |
378 } | |
379 | |
380 void WebrtcAudioPrivateSetActiveSinkFunction::OnControllerList( | |
381 const RenderProcessHost::AudioOutputControllerList& controllers) { | |
382 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
383 | |
384 std::string requested_process_type; | |
385 int requested_process_id; | |
386 if (request_info_.guest_process_id.get()) { | |
387 requested_process_type = "guestProcessId"; | |
388 requested_process_id = *request_info_.guest_process_id; | |
389 } else { | |
390 requested_process_type = "tabId"; | |
391 requested_process_id = *request_info_.tab_id; | |
392 } | |
393 | |
394 controllers_ = controllers; | |
395 num_remaining_sink_ids_ = controllers_.size(); | |
396 if (num_remaining_sink_ids_ == 0) { | |
397 error_ = extensions::ErrorUtils::FormatErrorMessage( | |
398 "No active stream for " + requested_process_type + " *", | |
399 base::IntToString(requested_process_id)); | |
400 SendResponse(false); | |
401 } else { | |
402 // We need to get the output device IDs, and calculate the HMAC | |
403 // for each, to find the raw ID for the ID provided to this API | |
404 // function call. | |
405 GetOutputDeviceDescriptions(); | |
406 } | |
407 } | |
408 | |
409 void WebrtcAudioPrivateSetActiveSinkFunction::OnOutputDeviceDescriptions( | |
410 std::unique_ptr<AudioDeviceDescriptions> device_descriptions) { | |
411 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
412 | |
413 std::string raw_sink_id; | |
414 if (sink_id_ == media::AudioDeviceDescription::kDefaultDeviceId) { | |
415 DVLOG(2) << "Received default ID, replacing with empty ID."; | |
416 raw_sink_id = ""; | |
417 } else { | |
418 for (AudioDeviceDescriptions::const_iterator it = | |
419 device_descriptions->begin(); | |
420 it != device_descriptions->end(); ++it) { | |
421 if (sink_id_ == CalculateHMACImpl(it->unique_id)) { | |
422 raw_sink_id = it->unique_id; | |
423 break; | |
424 } | |
425 } | |
426 | |
427 if (raw_sink_id.empty()) | |
428 DVLOG(2) << "Found no matching raw sink ID for HMAC " << sink_id_; | |
429 } | |
430 | |
431 RenderProcessHost::AudioOutputControllerList::const_iterator it = | |
432 controllers_.begin(); | |
433 for (; it != controllers_.end(); ++it) { | |
434 (*it)->SwitchOutputDevice(raw_sink_id, base::Bind( | |
435 &WebrtcAudioPrivateSetActiveSinkFunction::SwitchDone, this)); | |
436 } | |
437 } | |
438 | |
439 void WebrtcAudioPrivateSetActiveSinkFunction::SwitchDone() { | |
440 if (--num_remaining_sink_ids_ == 0) { | |
441 BrowserThread::PostTask( | |
442 BrowserThread::UI, FROM_HERE, | |
443 base::BindOnce(&WebrtcAudioPrivateSetActiveSinkFunction::DoneOnUIThread, | |
444 this)); | |
445 } | |
446 } | |
447 | |
448 void WebrtcAudioPrivateSetActiveSinkFunction::DoneOnUIThread() { | |
449 SendResponse(true); | |
450 } | |
451 | |
452 WebrtcAudioPrivateGetAssociatedSinkFunction:: | 265 WebrtcAudioPrivateGetAssociatedSinkFunction:: |
453 WebrtcAudioPrivateGetAssociatedSinkFunction() { | 266 WebrtcAudioPrivateGetAssociatedSinkFunction() { |
454 } | 267 } |
455 | 268 |
456 WebrtcAudioPrivateGetAssociatedSinkFunction:: | 269 WebrtcAudioPrivateGetAssociatedSinkFunction:: |
457 ~WebrtcAudioPrivateGetAssociatedSinkFunction() { | 270 ~WebrtcAudioPrivateGetAssociatedSinkFunction() { |
458 } | 271 } |
459 | 272 |
460 bool WebrtcAudioPrivateGetAssociatedSinkFunction::RunAsync() { | 273 bool WebrtcAudioPrivateGetAssociatedSinkFunction::RunAsync() { |
461 params_ = wap::GetAssociatedSink::Params::Create(*args_); | 274 params_ = wap::GetAssociatedSink::Params::Create(*args_); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 } | 373 } |
561 | 374 |
562 host->SetEchoCanceller3(*params->audio_experiments.enable_aec3); | 375 host->SetEchoCanceller3(*params->audio_experiments.enable_aec3); |
563 } | 376 } |
564 | 377 |
565 SendResponse(true); | 378 SendResponse(true); |
566 return true; | 379 return true; |
567 } | 380 } |
568 | 381 |
569 } // namespace extensions | 382 } // namespace extensions |
OLD | NEW |