Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(489)

Side by Side Diff: chrome/browser/extensions/api/webrtc_audio_private/webrtc_audio_private_browsertest.cc

Issue 2791203002: Switching WebRtc private API from AudioManager to AudioSystem (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/extensions/api/webrtc_audio_private/webrtc_audio_private_api.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
(...skipping 17 matching lines...) Expand all
28 #include "chrome/common/features.h" 28 #include "chrome/common/features.h"
29 #include "chrome/test/base/in_process_browser_test.h" 29 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/ui_test_utils.h" 30 #include "chrome/test/base/ui_test_utils.h"
31 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/media_device_id.h" 32 #include "content/public/browser/media_device_id.h"
33 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
34 #include "content/public/test/browser_test_utils.h" 34 #include "content/public/test/browser_test_utils.h"
35 #include "extensions/common/permissions/permission_set.h" 35 #include "extensions/common/permissions/permission_set.h"
36 #include "extensions/common/permissions/permissions_data.h" 36 #include "extensions/common/permissions/permissions_data.h"
37 #include "media/audio/audio_device_description.h" 37 #include "media/audio/audio_device_description.h"
38 #include "media/audio/audio_manager.h" 38 #include "media/audio/audio_system.h"
39 #include "net/test/embedded_test_server/embedded_test_server.h" 39 #include "net/test/embedded_test_server/embedded_test_server.h"
40 #include "testing/gtest/include/gtest/gtest.h" 40 #include "testing/gtest/include/gtest/gtest.h"
41 41
42 #if defined(OS_WIN) 42 #if defined(OS_WIN)
43 #include "base/win/windows_version.h" 43 #include "base/win/windows_version.h"
44 #endif 44 #endif
45 45
46 using base::JSONWriter; 46 using base::JSONWriter;
47 using content::RenderProcessHost; 47 using content::RenderProcessHost;
48 using content::WebContents; 48 using content::WebContents;
49 using media::AudioDeviceDescriptions; 49 using media::AudioDeviceDescriptions;
50 using media::AudioManager;
51 50
52 namespace extensions { 51 namespace extensions {
53 52
54 using extension_function_test_utils::RunFunctionAndReturnError; 53 using extension_function_test_utils::RunFunctionAndReturnError;
55 using extension_function_test_utils::RunFunctionAndReturnSingleResult; 54 using extension_function_test_utils::RunFunctionAndReturnSingleResult;
56 55
56 namespace {
57
58 // Synchronously (from the calling thread's point of view) runs the
59 // given enumeration function on the device thread. On return,
60 // |device_descriptions| has been filled with the device descriptions
61 // resulting from that call.
62 void GetAudioDeviceDescriptions(bool for_input,
63 AudioDeviceDescriptions* device_descriptions) {
64 base::RunLoop run_loop;
65 media::AudioSystem::Get()->GetDeviceDescriptions(
66 base::Bind(
67 [](base::Closure finished_callback, AudioDeviceDescriptions* result,
68 AudioDeviceDescriptions received) {
69 *result = std::move(received);
70 finished_callback.Run();
71 },
72 base::Passed(run_loop.QuitClosure()), device_descriptions),
73 for_input);
74 run_loop.Run();
75 }
76
77 } // namespace
78
57 class AudioWaitingExtensionTest : public ExtensionApiTest { 79 class AudioWaitingExtensionTest : public ExtensionApiTest {
58 protected: 80 protected:
59 void WaitUntilAudioIsPlaying(WebContents* tab) { 81 void WaitUntilAudioIsPlaying(WebContents* tab) {
60 // Wait for audio to start playing. 82 // Wait for audio to start playing.
61 bool audio_playing = false; 83 bool audio_playing = false;
62 for (size_t remaining_tries = 50; remaining_tries > 0; --remaining_tries) { 84 for (size_t remaining_tries = 50; remaining_tries > 0; --remaining_tries) {
63 audio_playing = tab->WasRecentlyAudible(); 85 audio_playing = tab->WasRecentlyAudible();
64 base::RunLoop().RunUntilIdle(); 86 base::RunLoop().RunUntilIdle();
65 if (audio_playing) 87 if (audio_playing)
66 break; 88 break;
67
68 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); 89 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
69 } 90 }
70
71 if (!audio_playing) 91 if (!audio_playing)
72 FAIL() << "Audio did not start playing within ~5 seconds."; 92 FAIL() << "Audio did not start playing within ~5 seconds.";
73 } 93 }
74 }; 94 };
75 95
76 class WebrtcAudioPrivateTest : public AudioWaitingExtensionTest { 96 class WebrtcAudioPrivateTest : public AudioWaitingExtensionTest {
77 public: 97 public:
78 WebrtcAudioPrivateTest() 98 WebrtcAudioPrivateTest() {}
79 : enumeration_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
80 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
81 99
82 void SetUpOnMainThread() override { 100 void SetUpOnMainThread() override {
83 AudioWaitingExtensionTest::SetUpOnMainThread(); 101 AudioWaitingExtensionTest::SetUpOnMainThread();
84 // Needs to happen after chrome's schemes are added. 102 // Needs to happen after chrome's schemes are added.
85 source_url_ = GURL("chrome-extension://fakeid012345678/fakepage.html"); 103 source_url_ = GURL("chrome-extension://fakeid012345678/fakepage.html");
86 } 104 }
87 105
88 protected: 106 protected:
89 void AppendTabIdToRequestInfo(base::ListValue* params, int tab_id) { 107 void AppendTabIdToRequestInfo(base::ListValue* params, int tab_id) {
90 std::unique_ptr<base::DictionaryValue> request_info( 108 std::unique_ptr<base::DictionaryValue> request_info(
91 new base::DictionaryValue()); 109 new base::DictionaryValue());
92 request_info->SetInteger("tabId", tab_id); 110 request_info->SetInteger("tabId", tab_id);
93 params->Append(std::move(request_info)); 111 params->Append(std::move(request_info));
94 } 112 }
95 113
96 std::unique_ptr<base::Value> InvokeGetSinks(base::ListValue** sink_list) { 114 std::unique_ptr<base::Value> InvokeGetSinks(base::ListValue** sink_list) {
97 scoped_refptr<WebrtcAudioPrivateGetSinksFunction> function = 115 scoped_refptr<WebrtcAudioPrivateGetSinksFunction> function =
98 new WebrtcAudioPrivateGetSinksFunction(); 116 new WebrtcAudioPrivateGetSinksFunction();
99 function->set_source_url(source_url_); 117 function->set_source_url(source_url_);
100 118
101 std::unique_ptr<base::Value> result( 119 std::unique_ptr<base::Value> result(
102 RunFunctionAndReturnSingleResult(function.get(), "[]", browser())); 120 RunFunctionAndReturnSingleResult(function.get(), "[]", browser()));
103 result->GetAsList(sink_list); 121 result->GetAsList(sink_list);
104 return result; 122 return result;
105 } 123 }
106 124
107 // Synchronously (from the calling thread's point of view) runs the
108 // given enumeration function on the device thread. On return,
109 // |device_descriptions| has been filled with the device descriptions
110 // resulting from that call.
111 void GetAudioDeviceDescriptions(
112 void (AudioManager::*EnumerationFunc)(AudioDeviceDescriptions*),
113 AudioDeviceDescriptions* device_descriptions) {
114 AudioManager* audio_manager = AudioManager::Get();
115
116 if (!audio_manager->GetTaskRunner()->BelongsToCurrentThread()) {
117 audio_manager->GetTaskRunner()->PostTask(
118 FROM_HERE,
119 base::BindOnce(&WebrtcAudioPrivateTest::GetAudioDeviceDescriptions,
120 base::Unretained(this), EnumerationFunc,
121 device_descriptions));
122 enumeration_event_.Wait();
123 } else {
124 (audio_manager->*EnumerationFunc)(device_descriptions);
125 enumeration_event_.Signal();
126 }
127 }
128
129 // Synchronously (from the calling thread's point of view) retrieve the
130 // device id in the |origin| on the IO thread. On return,
131 // |id_in_origin| contains the id |raw_device_id| is known by in
132 // the origin.
133 void GetIDInOrigin(content::ResourceContext* resource_context,
134 GURL origin,
135 const std::string& raw_device_id,
136 std::string* id_in_origin) {
137 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
138 content::BrowserThread::PostTask(
139 content::BrowserThread::UI, FROM_HERE,
140 base::BindOnce(&WebrtcAudioPrivateTest::GetIDInOrigin,
141 base::Unretained(this), resource_context, origin,
142 raw_device_id, id_in_origin));
143 enumeration_event_.Wait();
144 } else {
145 *id_in_origin = content::GetHMACForMediaDeviceID(
146 resource_context->GetMediaDeviceIDSalt(), url::Origin(origin),
147 raw_device_id);
148 enumeration_event_.Signal();
149 }
150 }
151
152 // Event used to signal completion of enumeration.
153 base::WaitableEvent enumeration_event_;
154
155 GURL source_url_; 125 GURL source_url_;
156 }; 126 };
157 127
158 #if !defined(OS_MACOSX) 128 #if !defined(OS_MACOSX)
159 // http://crbug.com/334579 129 // http://crbug.com/334579
160 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetSinks) { 130 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetSinks) {
161 AudioDeviceDescriptions devices; 131 AudioDeviceDescriptions devices;
162 GetAudioDeviceDescriptions(&AudioManager::GetAudioOutputDeviceDescriptions, 132 GetAudioDeviceDescriptions(false, &devices);
163 &devices);
164 133
165 base::ListValue* sink_list = NULL; 134 base::ListValue* sink_list = NULL;
166 std::unique_ptr<base::Value> result = InvokeGetSinks(&sink_list); 135 std::unique_ptr<base::Value> result = InvokeGetSinks(&sink_list);
167 136
168 std::string result_string; 137 std::string result_string;
169 JSONWriter::Write(*result, &result_string); 138 JSONWriter::Write(*result, &result_string);
170 VLOG(2) << result_string; 139 VLOG(2) << result_string;
171 140
172 EXPECT_EQ(devices.size(), sink_list->GetSize()); 141 EXPECT_EQ(devices.size(), sink_list->GetSize());
173 142
174 // Iterate through both lists in lockstep and compare. The order 143 // Iterate through both lists in lockstep and compare. The order
175 // should be identical. 144 // should be identical.
176 size_t ix = 0; 145 size_t ix = 0;
177 AudioDeviceDescriptions::const_iterator it = devices.begin(); 146 AudioDeviceDescriptions::const_iterator it = devices.begin();
178 for (; ix < sink_list->GetSize() && it != devices.end(); 147 for (; ix < sink_list->GetSize() && it != devices.end();
179 ++ix, ++it) { 148 ++ix, ++it) {
180 base::DictionaryValue* dict = NULL; 149 base::DictionaryValue* dict = NULL;
181 sink_list->GetDictionary(ix, &dict); 150 sink_list->GetDictionary(ix, &dict);
182 std::string sink_id; 151 std::string sink_id;
183 dict->GetString("sinkId", &sink_id); 152 dict->GetString("sinkId", &sink_id);
184 153
185 std::string expected_id; 154 std::string expected_id =
186 if (media::AudioDeviceDescription::IsDefaultDevice(it->unique_id)) { 155 media::AudioDeviceDescription::IsDefaultDevice(it->unique_id)
187 expected_id = media::AudioDeviceDescription::kDefaultDeviceId; 156 ? media::AudioDeviceDescription::kDefaultDeviceId
188 } else { 157 : content::GetHMACForMediaDeviceID(
189 GetIDInOrigin(profile()->GetResourceContext(), 158 profile()->GetResourceContext()->GetMediaDeviceIDSalt(),
190 source_url_.GetOrigin(), 159 url::Origin(source_url_.GetOrigin()), it->unique_id);
191 it->unique_id,
192 &expected_id);
193 }
194 160
195 EXPECT_EQ(expected_id, sink_id); 161 EXPECT_EQ(expected_id, sink_id);
196 std::string sink_label; 162 std::string sink_label;
197 dict->GetString("sinkLabel", &sink_label); 163 dict->GetString("sinkLabel", &sink_label);
198 EXPECT_EQ(it->device_name, sink_label); 164 EXPECT_EQ(it->device_name, sink_label);
199 165
200 // TODO(joi): Verify the contents of these once we start actually 166 // TODO(joi): Verify the contents of these once we start actually
201 // filling them in. 167 // filling them in.
202 EXPECT_TRUE(dict->HasKey("isDefault")); 168 EXPECT_TRUE(dict->HasKey("isDefault"));
203 EXPECT_TRUE(dict->HasKey("isReady")); 169 EXPECT_TRUE(dict->HasKey("isReady"));
204 EXPECT_TRUE(dict->HasKey("sampleRate")); 170 EXPECT_TRUE(dict->HasKey("sampleRate"));
205 } 171 }
206 } 172 }
207 #endif // OS_MACOSX 173 #endif // OS_MACOSX
208 174
209 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetAssociatedSink) { 175 IN_PROC_BROWSER_TEST_F(WebrtcAudioPrivateTest, GetAssociatedSink) {
210 // Get the list of input devices. We can cheat in the unit test and 176 // Get the list of input devices. We can cheat in the unit test and
211 // run this on the main thread since nobody else will be running at 177 // run this on the main thread since nobody else will be running at
212 // the same time. 178 // the same time.
213 AudioDeviceDescriptions devices; 179 AudioDeviceDescriptions devices;
214 GetAudioDeviceDescriptions(&AudioManager::GetAudioInputDeviceDescriptions, 180 GetAudioDeviceDescriptions(true, &devices);
215 &devices);
216 181
217 // Try to get an associated sink for each source. 182 // Try to get an associated sink for each source.
218 for (const auto& device : devices) { 183 for (const auto& device : devices) {
219 scoped_refptr<WebrtcAudioPrivateGetAssociatedSinkFunction> function = 184 scoped_refptr<WebrtcAudioPrivateGetAssociatedSinkFunction> function =
220 new WebrtcAudioPrivateGetAssociatedSinkFunction(); 185 new WebrtcAudioPrivateGetAssociatedSinkFunction();
221 function->set_source_url(source_url_); 186 function->set_source_url(source_url_);
222 187
223 std::string raw_device_id = device.unique_id; 188 std::string raw_device_id = device.unique_id;
224 VLOG(2) << "Trying to find associated sink for device " << raw_device_id; 189 VLOG(2) << "Trying to find associated sink for device " << raw_device_id;
225 std::string source_id_in_origin;
226 GURL origin(GURL("http://www.google.com/").GetOrigin()); 190 GURL origin(GURL("http://www.google.com/").GetOrigin());
227 GetIDInOrigin(profile()->GetResourceContext(), 191 std::string source_id_in_origin = content::GetHMACForMediaDeviceID(
228 origin, 192 profile()->GetResourceContext()->GetMediaDeviceIDSalt(),
229 raw_device_id, 193 url::Origin(origin), raw_device_id);
230 &source_id_in_origin);
231 194
232 base::ListValue parameters; 195 base::ListValue parameters;
233 parameters.AppendString(origin.spec()); 196 parameters.AppendString(origin.spec());
234 parameters.AppendString(source_id_in_origin); 197 parameters.AppendString(source_id_in_origin);
235 std::string parameter_string; 198 std::string parameter_string;
236 JSONWriter::Write(parameters, &parameter_string); 199 JSONWriter::Write(parameters, &parameter_string);
237 200
238 std::unique_ptr<base::Value> result(RunFunctionAndReturnSingleResult( 201 std::unique_ptr<base::Value> result(RunFunctionAndReturnSingleResult(
239 function.get(), parameter_string, browser())); 202 function.get(), parameter_string, browser()));
240 std::string result_string; 203 std::string result_string;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("failure")); 266 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("failure"));
304 base::string16 result = title_watcher.WaitAndGetTitle(); 267 base::string16 result = title_watcher.WaitAndGetTitle();
305 EXPECT_EQ(base::ASCIIToUTF16("success"), result); 268 EXPECT_EQ(base::ASCIIToUTF16("success"), result);
306 269
307 g_browser_process->webrtc_log_uploader()->OverrideUploadWithBufferForTesting( 270 g_browser_process->webrtc_log_uploader()->OverrideUploadWithBufferForTesting(
308 NULL); 271 NULL);
309 } 272 }
310 #endif // BUILDFLAG(ENABLE_HANGOUT_SERVICES_EXTENSION) 273 #endif // BUILDFLAG(ENABLE_HANGOUT_SERVICES_EXTENSION)
311 274
312 } // namespace extensions 275 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/webrtc_audio_private/webrtc_audio_private_api.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698