OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <algorithm> | |
6 #include <cmath> | |
7 | |
8 #include "base/command_line.h" | |
9 #include "base/float_util.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "base/synchronization/lock.h" | |
13 #include "base/time/time.h" | |
14 #include "chrome/browser/extensions/extension_apitest.h" | |
15 #include "chrome/common/chrome_switches.h" | |
16 #include "content/public/common/content_switches.h" | |
17 #include "media/base/bind_to_current_loop.h" | |
18 #include "media/base/video_frame.h" | |
19 #include "media/cast/cast_config.h" | |
20 #include "media/cast/cast_environment.h" | |
21 #include "media/cast/test/utility/audio_utility.h" | |
22 #include "media/cast/test/utility/default_config.h" | |
23 #include "media/cast/test/utility/in_process_receiver.h" | |
24 #include "media/cast/test/utility/standalone_cast_environment.h" | |
25 #include "net/base/net_errors.h" | |
26 #include "net/base/net_util.h" | |
27 #include "net/base/rand_callback.h" | |
28 #include "net/udp/udp_socket.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | |
30 | |
31 namespace extensions { | |
32 | |
33 class CastStreamingApiTest : public ExtensionApiTest { | |
34 public: | |
35 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
36 ExtensionApiTest::SetUpCommandLine(command_line); | |
37 command_line->AppendSwitchASCII(switches::kWhitelistedExtensionID, | |
38 "ddchlicdkolnonkihahngkmmmjnjlkkf"); | |
39 } | |
40 }; | |
41 | |
42 // Test running the test extension for Cast Mirroring API. | |
43 IN_PROC_BROWSER_TEST_F(CastStreamingApiTest, Basics) { | |
44 ASSERT_TRUE(RunExtensionSubtest("cast_streaming", "basics.html")) << message_; | |
45 } | |
46 | |
47 IN_PROC_BROWSER_TEST_F(CastStreamingApiTest, BadLogging) { | |
48 ASSERT_TRUE(RunExtensionSubtest("cast_streaming", "bad_logging.html")) | |
49 << message_; | |
50 } | |
51 | |
52 namespace { | |
53 | |
54 // An in-process Cast receiver that examines the audio/video frames being | |
55 // received for expected colors and tones. Used in | |
56 // CastStreamingApiTest.EndToEnd, below. | |
57 class TestPatternReceiver : public media::cast::InProcessReceiver { | |
58 public: | |
59 explicit TestPatternReceiver( | |
60 const scoped_refptr<media::cast::CastEnvironment>& cast_environment, | |
61 const net::IPEndPoint& local_end_point) | |
62 : InProcessReceiver(cast_environment, | |
63 local_end_point, | |
64 net::IPEndPoint(), | |
65 media::cast::GetDefaultAudioReceiverConfig(), | |
66 media::cast::GetDefaultVideoReceiverConfig()), | |
67 target_tone_frequency_(0), | |
68 current_tone_frequency_(0.0f) { | |
69 memset(&target_color_, 0, sizeof(target_color_)); | |
70 memset(¤t_color_, 0, sizeof(current_color_)); | |
71 } | |
72 | |
73 // Blocks the caller until this receiver has seen both |yuv_color| and | |
74 // |tone_frequency| consistently for the given |duration|. | |
75 void WaitForColorAndTone(const uint8 yuv_color[3], | |
76 int tone_frequency, | |
77 base::TimeDelta duration) { | |
78 DCHECK(!cast_env()->CurrentlyOn(media::cast::CastEnvironment::MAIN)); | |
79 | |
80 LOG(INFO) << "Waiting for test pattern: color=yuv(" | |
81 << static_cast<int>(yuv_color[0]) << ", " | |
82 << static_cast<int>(yuv_color[1]) << ", " | |
83 << static_cast<int>(yuv_color[2]) | |
84 << "), tone_frequency=" << tone_frequency << " Hz"; | |
85 | |
86 // Reset target values and counters. | |
87 base::AutoLock auto_lock(lock_); | |
88 target_color_[0] = yuv_color[0]; | |
89 target_color_[1] = yuv_color[1]; | |
90 target_color_[2] = yuv_color[2]; | |
91 target_tone_frequency_ = tone_frequency; | |
92 first_time_near_target_color_ = base::TimeTicks(); | |
93 first_time_near_target_tone_ = base::TimeTicks(); | |
94 | |
95 // Wait until both the color and tone have matched, subject to a timeout. | |
96 while (true) { | |
97 base::RunLoop run_loop; | |
98 notify_callback_ = media::BindToCurrentLoop(run_loop.QuitClosure()); | |
99 { | |
100 base::AutoUnlock auto_unlock(lock_); | |
101 run_loop.Run(); | |
102 } | |
103 notify_callback_.Reset(); | |
104 | |
105 // TODO(miu): Check audio tone too, once audio is fixed in the library. | |
106 if (!first_time_near_target_color_.is_null() && | |
107 /*!first_time_near_target_tone_.is_null()*/ true) { | |
108 const base::TimeTicks now = cast_env()->Clock()->NowTicks(); | |
109 if ((now - first_time_near_target_color_) >= duration && | |
110 /*(now - first_time_near_target_tone_) >= duration*/ true) { | |
111 return; // Successfully matched for sufficient duration. | |
112 } | |
113 } | |
114 } | |
115 } | |
116 | |
117 private: | |
118 virtual ~TestPatternReceiver() {} | |
119 | |
120 // Invoked by InProcessReceiver for each received audio frame. | |
121 virtual void OnAudioFrame(scoped_ptr<media::cast::PcmAudioFrame> audio_frame, | |
122 const base::TimeTicks& playout_time) OVERRIDE { | |
123 DCHECK(cast_env()->CurrentlyOn(media::cast::CastEnvironment::MAIN)); | |
124 | |
125 if (audio_frame->samples.empty()) { | |
126 NOTREACHED() << "OnAudioFrame called with no samples?!?"; | |
127 return; | |
128 } | |
129 | |
130 // Assume the audio signal is a single sine wave (it can have some | |
131 // low-amplitude noise). Count zero crossings, and extrapolate the | |
132 // frequency of the sine wave in |audio_frame|. | |
133 const int crossings = media::cast::CountZeroCrossings(audio_frame->samples); | |
134 const float seconds_per_frame = audio_frame->samples.size() / | |
135 static_cast<float>(audio_frame->frequency); | |
136 const float frequency_in_frame = crossings / seconds_per_frame; | |
137 | |
138 const float kAveragingWeight = 0.1f; | |
139 UpdateExponentialMovingAverage( | |
140 kAveragingWeight, frequency_in_frame, ¤t_tone_frequency_); | |
141 VLOG(1) << "Current audio tone frequency: " << current_tone_frequency_; | |
142 | |
143 { | |
144 base::AutoLock auto_lock(lock_); | |
145 const float kTargetWindowHz = 20; | |
146 // Trigger the waiting thread while the current tone is within | |
147 // kTargetWindowHz of the target tone. | |
148 if (fabsf(current_tone_frequency_ - target_tone_frequency_) < | |
149 kTargetWindowHz) { | |
150 if (first_time_near_target_tone_.is_null()) | |
151 first_time_near_target_tone_ = cast_env()->Clock()->NowTicks(); | |
152 if (!notify_callback_.is_null()) | |
153 notify_callback_.Run(); | |
154 } else { | |
155 first_time_near_target_tone_ = base::TimeTicks(); | |
156 } | |
157 } | |
158 } | |
159 | |
160 virtual void OnVideoFrame(const scoped_refptr<media::VideoFrame>& video_frame, | |
161 const base::TimeTicks& render_time) OVERRIDE { | |
162 DCHECK(cast_env()->CurrentlyOn(media::cast::CastEnvironment::MAIN)); | |
163 | |
164 CHECK(video_frame->format() == media::VideoFrame::YV12 || | |
165 video_frame->format() == media::VideoFrame::I420 || | |
166 video_frame->format() == media::VideoFrame::YV12A); | |
167 | |
168 // Note: We take the median value of each plane because the test image will | |
169 // contain mostly a solid color plus some "cruft" which is the "Testing..." | |
170 // text in the upper-left corner of the video frame. In other words, we | |
171 // want to read "the most common color." | |
172 const int kPlanes[] = {media::VideoFrame::kYPlane, | |
173 media::VideoFrame::kUPlane, | |
174 media::VideoFrame::kVPlane}; | |
175 for (size_t i = 0; i < arraysize(kPlanes); ++i) { | |
176 current_color_[i] = | |
177 ComputeMedianIntensityInPlane(video_frame->row_bytes(kPlanes[i]), | |
178 video_frame->rows(kPlanes[i]), | |
179 video_frame->stride(kPlanes[i]), | |
180 video_frame->data(kPlanes[i])); | |
181 } | |
182 | |
183 VLOG(1) << "Current video color: yuv(" << current_color_[0] << ", " | |
184 << current_color_[1] << ", " << current_color_[2] << ')'; | |
185 | |
186 { | |
187 base::AutoLock auto_lock(lock_); | |
188 const float kTargetWindow = 10.0f; | |
189 // Trigger the waiting thread while all color channels are within | |
190 // kTargetWindow of the target. | |
191 if (fabsf(current_color_[0] - target_color_[0]) < kTargetWindow && | |
192 fabsf(current_color_[1] - target_color_[1]) < kTargetWindow && | |
193 fabsf(current_color_[2] - target_color_[2]) < kTargetWindow) { | |
194 if (first_time_near_target_color_.is_null()) | |
195 first_time_near_target_color_ = cast_env()->Clock()->NowTicks(); | |
196 if (!notify_callback_.is_null()) | |
197 notify_callback_.Run(); | |
198 } else { | |
199 first_time_near_target_color_ = base::TimeTicks(); | |
200 } | |
201 } | |
202 } | |
203 | |
204 static void UpdateExponentialMovingAverage(float weight, | |
205 float sample_value, | |
206 float* average) { | |
207 *average += weight * sample_value - weight * (*average); | |
208 CHECK(base::IsFinite(*average)); | |
209 } | |
210 | |
211 static uint8 ComputeMedianIntensityInPlane(int width, | |
212 int height, | |
213 int stride, | |
214 uint8* data) { | |
215 const int num_pixels = width * height; | |
216 if (num_pixels <= 0) | |
217 return 0; | |
218 // If necessary, re-pack the pixels such that the stride is equal to the | |
219 // width. | |
220 if (width < stride) { | |
221 for (int y = 1; y < height; ++y) { | |
222 uint8* const src = data + y * stride; | |
223 uint8* const dest = data + y * width; | |
224 memmove(dest, src, width); | |
225 } | |
226 } | |
227 const size_t middle_idx = num_pixels / 2; | |
228 std::nth_element(data, data + middle_idx, data + num_pixels); | |
229 return data[middle_idx]; | |
230 } | |
231 | |
232 base::Lock lock_; | |
233 base::Closure notify_callback_; | |
234 | |
235 float target_color_[3]; // Y, U, V | |
236 float target_tone_frequency_; | |
237 | |
238 float current_color_[3]; // Y, U, V | |
239 base::TimeTicks first_time_near_target_color_; | |
240 float current_tone_frequency_; | |
241 base::TimeTicks first_time_near_target_tone_; | |
242 | |
243 DISALLOW_COPY_AND_ASSIGN(TestPatternReceiver); | |
244 }; | |
245 | |
246 } // namespace | |
247 | |
248 class CastStreamingApiTestWithPixelOutput : public CastStreamingApiTest { | |
249 virtual void SetUp() OVERRIDE { | |
250 if (!UsingOSMesa()) | |
251 EnablePixelOutput(); | |
252 CastStreamingApiTest::SetUp(); | |
253 } | |
254 | |
255 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
256 command_line->AppendSwitchASCII(switches::kWindowSize, "128,128"); | |
257 CastStreamingApiTest::SetUpCommandLine(command_line); | |
258 } | |
259 }; | |
260 | |
261 // Tests the Cast streaming API and its basic functionality end-to-end. An | |
262 // extension subtest is run to generate test content, capture that content, and | |
263 // use the API to send it out. At the same time, this test launches an | |
264 // in-process Cast receiver, listening on a localhost UDP socket, to receive the | |
265 // content and check whether it matches expectations. | |
266 // | |
267 // Note: This test is disabled until outstanding bugs are fixed and the | |
268 // media/cast library has achieved sufficient stability. | |
269 // http://crbug.com/349599 | |
270 IN_PROC_BROWSER_TEST_F(CastStreamingApiTestWithPixelOutput, DISABLED_EndToEnd) { | |
271 // This test is too slow to succeed with OSMesa on the bots. | |
272 if (UsingOSMesa()) { | |
273 LOG(WARNING) << "Skipping this test since OSMesa is too slow on the bots. " | |
hubbe
2014/03/06 19:54:43
It's still too slow, even with a window size of 12
miu
2014/03/07 22:40:29
Fixed resolution issues, now that I know about the
| |
274 "Try --enable-gpu."; | |
275 return; | |
276 } | |
277 | |
278 // Determine a unused UDP port for the in-process receiver to listen on. | |
279 // Method: Bind a UDP socket on port 0, and then check which port the | |
280 // operating system assigned to it. | |
281 net::IPAddressNumber localhost; | |
282 localhost.push_back(127); | |
283 localhost.push_back(0); | |
284 localhost.push_back(0); | |
285 localhost.push_back(1); | |
286 scoped_ptr<net::UDPSocket> receive_socket( | |
287 new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND, | |
288 net::RandIntCallback(), | |
289 NULL, | |
290 net::NetLog::Source())); | |
291 receive_socket->AllowAddressReuse(); | |
292 ASSERT_EQ(net::OK, receive_socket->Bind(net::IPEndPoint(localhost, 0))); | |
293 net::IPEndPoint receiver_end_point; | |
294 ASSERT_EQ(net::OK, receive_socket->GetLocalAddress(&receiver_end_point)); | |
295 receive_socket.reset(); | |
296 | |
297 // Start the in-process receiver that examines audio/video for the expected | |
298 // test patterns. | |
299 const scoped_refptr<media::cast::StandaloneCastEnvironment> cast_environment( | |
300 new media::cast::StandaloneCastEnvironment( | |
301 media::cast::CastLoggingConfig())); | |
302 const scoped_refptr<TestPatternReceiver> receiver( | |
303 new TestPatternReceiver(cast_environment, receiver_end_point)); | |
304 receiver->Start(); | |
305 | |
306 // Launch the page that: 1) renders the source content; 2) uses the | |
307 // chrome.tabCapture and chrome.cast.streaming APIs to capture its content and | |
308 // stream using Cast; and 3) calls chrome.test.succeed() once it is | |
309 // operational. | |
310 const std::string page_url = base::StringPrintf( | |
311 "end_to_end_sender.html?port=%d", receiver_end_point.port()); | |
312 ASSERT_TRUE(RunExtensionSubtest("cast_streaming", page_url)) << message_; | |
313 | |
314 // Examine the Cast receiver for expected audio/video test patterns. The | |
315 // colors and tones specified here must match those in end_to_end_sender.js. | |
316 const uint8 kRedInYUV[3] = {82, 90, 240}; // rgb(255, 0, 0) | |
317 const uint8 kGreenInYUV[3] = {145, 54, 34}; // rgb(0, 255, 0) | |
318 const uint8 kBlueInYUV[3] = {41, 240, 110}; // rgb(0, 0, 255) | |
319 const base::TimeDelta kOneHalfSecond = base::TimeDelta::FromMilliseconds(500); | |
320 receiver->WaitForColorAndTone(kRedInYUV, 200 /* Hz */, kOneHalfSecond); | |
321 receiver->WaitForColorAndTone(kGreenInYUV, 500 /* Hz */, kOneHalfSecond); | |
322 receiver->WaitForColorAndTone(kBlueInYUV, 1800 /* Hz */, kOneHalfSecond); | |
323 | |
324 // TODO(miu): Uncomment once GetWeakPtr() NULL crash in PacedSender is fixed | |
325 // (see http://crbug.com/349786): | |
326 // receiver->StopSoon(); | |
327 cast_environment->Shutdown(); | |
328 } | |
329 | |
330 } // namespace extensions | |
OLD | NEW |