| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/media/media_devices_util.h" |
| 6 |
| 7 #include <utility> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_tokenizer.h" |
| 14 #include "content/browser/frame_host/render_frame_host_delegate.h" |
| 15 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/common/media_stream_request.h" |
| 18 #include "media/base/media_switches.h" |
| 19 |
| 20 namespace content { |
| 21 |
| 22 namespace { |
| 23 |
| 24 std::string GetDefaultMediaDeviceIDOnUIThread(MediaDeviceType device_type, |
| 25 int render_process_id, |
| 26 int render_frame_id) { |
| 27 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 28 RenderFrameHostImpl* frame_host = |
| 29 RenderFrameHostImpl::FromID(render_process_id, render_frame_id); |
| 30 if (!frame_host) |
| 31 return std::string(); |
| 32 |
| 33 RenderFrameHostDelegate* delegate = frame_host->delegate(); |
| 34 if (!delegate) |
| 35 return std::string(); |
| 36 |
| 37 MediaStreamType media_stream_type; |
| 38 switch (device_type) { |
| 39 case MEDIA_DEVICE_TYPE_AUDIO_INPUT: |
| 40 media_stream_type = MEDIA_DEVICE_AUDIO_CAPTURE; |
| 41 break; |
| 42 case MEDIA_DEVICE_TYPE_VIDEO_INPUT: |
| 43 media_stream_type = MEDIA_DEVICE_VIDEO_CAPTURE; |
| 44 break; |
| 45 default: |
| 46 return std::string(); |
| 47 } |
| 48 |
| 49 return delegate->GetDefaultMediaDeviceID(media_stream_type); |
| 50 } |
| 51 |
| 52 // This function is intended for testing purposes. |
| 53 std::string GetDefaultMediaDeviceIDFromCommandLine( |
| 54 MediaDeviceType device_type) { |
| 55 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 56 switches::kUseFakeDeviceForMediaStream)); |
| 57 const std::string option = |
| 58 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 59 switches::kUseFakeDeviceForMediaStream); |
| 60 // Optional comma delimited parameters to the command line can specify values |
| 61 // for the default device IDs. |
| 62 // Examples: "video-input-default-id=mycam, audio-input-default-id=mymic" |
| 63 base::StringTokenizer option_tokenizer(option, ", "); |
| 64 option_tokenizer.set_quote_chars("\""); |
| 65 |
| 66 while (option_tokenizer.GetNext()) { |
| 67 std::vector<std::string> param = |
| 68 base::SplitString(option_tokenizer.token(), "=", base::TRIM_WHITESPACE, |
| 69 base::SPLIT_WANT_NONEMPTY); |
| 70 if (param.size() != 2u) { |
| 71 DLOG(WARNING) << "Forgot a value '" << option << "'? Use name=value for " |
| 72 << switches::kUseFakeDeviceForMediaStream << "."; |
| 73 return std::string(); |
| 74 } |
| 75 |
| 76 if (device_type == MEDIA_DEVICE_TYPE_AUDIO_INPUT && |
| 77 param.front() == "audio-input-default-id") { |
| 78 return param.back(); |
| 79 } else if (device_type == MEDIA_DEVICE_TYPE_VIDEO_INPUT && |
| 80 param.front() == "video-input-default-id") { |
| 81 return param.back(); |
| 82 } |
| 83 } |
| 84 |
| 85 return std::string(); |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 void GetDefaultMediaDeviceID( |
| 91 MediaDeviceType device_type, |
| 92 int render_process_id, |
| 93 int render_frame_id, |
| 94 const base::Callback<void(const std::string&)>& callback) { |
| 95 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 96 switches::kUseFakeDeviceForMediaStream)) { |
| 97 callback.Run(GetDefaultMediaDeviceIDFromCommandLine(device_type)); |
| 98 return; |
| 99 } |
| 100 |
| 101 BrowserThread::PostTaskAndReplyWithResult( |
| 102 BrowserThread::UI, FROM_HERE, |
| 103 base::Bind(&GetDefaultMediaDeviceIDOnUIThread, device_type, |
| 104 render_process_id, render_frame_id), |
| 105 callback); |
| 106 } |
| 107 |
| 108 } // namespace content |
| OLD | NEW |