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

Side by Side Diff: chrome/browser/extensions/api/cast_streaming/cast_streaming_apitest.cc

Issue 184813009: Cast Streaming API end-to-end browser_test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove ref-counting of InProcessReceiver and always destroy it on the MAIN thread. Created 6 years, 9 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 | Annotate | Revision Log
OLDNEW
(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(&current_color_, 0, sizeof(current_color_));
71 }
72
73 virtual ~TestPatternReceiver() {}
74
75 // Blocks the caller until this receiver has seen both |yuv_color| and
76 // |tone_frequency| consistently for the given |duration|.
77 void WaitForColorAndTone(const uint8 yuv_color[3],
78 int tone_frequency,
79 base::TimeDelta duration) {
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 base::RunLoop run_loop;
87 cast_env()->PostTask(
88 media::cast::CastEnvironment::MAIN,
89 FROM_HERE,
90 base::Bind(&TestPatternReceiver::NotifyOnceMatched,
91 base::Unretained(this),
92 yuv_color,
93 tone_frequency,
94 duration,
95 media::BindToCurrentLoop(run_loop.QuitClosure())));
96 run_loop.Run();
97 }
98
99 private:
100 // Resets tracking data and sets the match duration and callback.
101 void NotifyOnceMatched(const uint8 yuv_color[3],
102 int tone_frequency,
103 base::TimeDelta match_duration,
104 const base::Closure& matched_callback) {
105 DCHECK(cast_env()->CurrentlyOn(media::cast::CastEnvironment::MAIN));
106
107 match_duration_ = match_duration;
108 matched_callback_ = matched_callback;
109 target_color_[0] = yuv_color[0];
110 target_color_[1] = yuv_color[1];
111 target_color_[2] = yuv_color[2];
112 target_tone_frequency_ = tone_frequency;
113 first_time_near_target_color_ = base::TimeTicks();
114 first_time_near_target_tone_ = base::TimeTicks();
115 }
116
117 // Runs |matched_callback_| once both color and tone have been matched for the
118 // required |match_duration_|.
119 void NotifyIfMatched() {
120 DCHECK(cast_env()->CurrentlyOn(media::cast::CastEnvironment::MAIN));
121
122 // TODO(miu): Check audio tone too, once audio is fixed in the library.
hubbe 2014/03/07 22:48:50 bug?
miu 2014/03/08 02:20:49 Done.
123 if (first_time_near_target_color_.is_null() ||
124 /*first_time_near_target_tone_.is_null()*/ false)
125 return;
126 const base::TimeTicks now = cast_env()->Clock()->NowTicks();
127 if ((now - first_time_near_target_color_) >= match_duration_ &&
128 /*(now - first_time_near_target_tone_) >= match_duration_*/ true) {
129 matched_callback_.Run();
130 }
131 }
132
133 // Invoked by InProcessReceiver for each received audio frame.
134 virtual void OnAudioFrame(scoped_ptr<media::cast::PcmAudioFrame> audio_frame,
135 const base::TimeTicks& playout_time) OVERRIDE {
136 DCHECK(cast_env()->CurrentlyOn(media::cast::CastEnvironment::MAIN));
137
138 if (audio_frame->samples.empty()) {
139 NOTREACHED() << "OnAudioFrame called with no samples?!?";
140 return;
141 }
142
143 // Assume the audio signal is a single sine wave (it can have some
144 // low-amplitude noise). Count zero crossings, and extrapolate the
145 // frequency of the sine wave in |audio_frame|.
146 const int crossings = media::cast::CountZeroCrossings(audio_frame->samples);
147 const float seconds_per_frame = audio_frame->samples.size() /
148 static_cast<float>(audio_frame->frequency);
149 const float frequency_in_frame = crossings / seconds_per_frame;
150
151 const float kAveragingWeight = 0.1f;
152 UpdateExponentialMovingAverage(
153 kAveragingWeight, frequency_in_frame, &current_tone_frequency_);
154 VLOG(1) << "Current audio tone frequency: " << current_tone_frequency_;
155
156 const float kTargetWindowHz = 20;
157 // Update the time at which the current tone started falling within
158 // kTargetWindowHz of the target tone.
159 if (fabsf(current_tone_frequency_ - target_tone_frequency_) <
160 kTargetWindowHz) {
161 if (first_time_near_target_tone_.is_null())
162 first_time_near_target_tone_ = cast_env()->Clock()->NowTicks();
163 NotifyIfMatched();
164 } else {
165 first_time_near_target_tone_ = base::TimeTicks();
166 }
167 }
168
169 virtual void OnVideoFrame(const scoped_refptr<media::VideoFrame>& video_frame,
170 const base::TimeTicks& render_time) OVERRIDE {
171 DCHECK(cast_env()->CurrentlyOn(media::cast::CastEnvironment::MAIN));
172
173 CHECK(video_frame->format() == media::VideoFrame::YV12 ||
174 video_frame->format() == media::VideoFrame::I420 ||
175 video_frame->format() == media::VideoFrame::YV12A);
176
177 // Note: We take the median value of each plane because the test image will
178 // contain mostly a solid color plus some "cruft" which is the "Testing..."
179 // text in the upper-left corner of the video frame. In other words, we
180 // want to read "the most common color."
181 const int kPlanes[] = {media::VideoFrame::kYPlane,
182 media::VideoFrame::kUPlane,
183 media::VideoFrame::kVPlane};
184 for (size_t i = 0; i < arraysize(kPlanes); ++i) {
185 current_color_[i] =
186 ComputeMedianIntensityInPlane(video_frame->row_bytes(kPlanes[i]),
187 video_frame->rows(kPlanes[i]),
188 video_frame->stride(kPlanes[i]),
189 video_frame->data(kPlanes[i]));
190 }
191
192 VLOG(1) << "Current video color: yuv(" << current_color_[0] << ", "
193 << current_color_[1] << ", " << current_color_[2] << ')';
194
195 const float kTargetWindow = 10.0f;
196 // Update the time at which all color channels started falling within
197 // kTargetWindow of the target.
198 if (fabsf(current_color_[0] - target_color_[0]) < kTargetWindow &&
199 fabsf(current_color_[1] - target_color_[1]) < kTargetWindow &&
200 fabsf(current_color_[2] - target_color_[2]) < kTargetWindow) {
201 if (first_time_near_target_color_.is_null())
202 first_time_near_target_color_ = cast_env()->Clock()->NowTicks();
203 NotifyIfMatched();
204 } else {
205 first_time_near_target_color_ = base::TimeTicks();
206 }
207 }
208
209 static void UpdateExponentialMovingAverage(float weight,
210 float sample_value,
211 float* average) {
212 *average = weight * sample_value + (1.0f - weight) * (*average);
213 CHECK(base::IsFinite(*average));
214 }
215
216 static uint8 ComputeMedianIntensityInPlane(int width,
217 int height,
218 int stride,
219 uint8* data) {
220 const int num_pixels = width * height;
221 if (num_pixels <= 0)
222 return 0;
223 // If necessary, re-pack the pixels such that the stride is equal to the
224 // width.
225 if (width < stride) {
226 for (int y = 1; y < height; ++y) {
227 uint8* const src = data + y * stride;
228 uint8* const dest = data + y * width;
229 memmove(dest, src, width);
230 }
231 }
232 const size_t middle_idx = num_pixels / 2;
233 std::nth_element(data, data + middle_idx, data + num_pixels);
234 return data[middle_idx];
235 }
236
237 base::TimeDelta match_duration_;
238 base::Closure matched_callback_;
239
240 float target_color_[3]; // Y, U, V
241 float target_tone_frequency_;
242
243 float current_color_[3]; // Y, U, V
244 base::TimeTicks first_time_near_target_color_;
245 float current_tone_frequency_;
246 base::TimeTicks first_time_near_target_tone_;
247
248 DISALLOW_COPY_AND_ASSIGN(TestPatternReceiver);
249 };
250
251 } // namespace
252
253 class CastStreamingApiTestWithPixelOutput : public CastStreamingApiTest {
254 virtual void SetUp() OVERRIDE {
255 EnablePixelOutput();
256 CastStreamingApiTest::SetUp();
257 }
258
259 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
260 command_line->AppendSwitchASCII(switches::kWindowSize, "128,128");
261 CastStreamingApiTest::SetUpCommandLine(command_line);
262 }
263 };
264
265 // Tests the Cast streaming API and its basic functionality end-to-end. An
266 // extension subtest is run to generate test content, capture that content, and
267 // use the API to send it out. At the same time, this test launches an
268 // in-process Cast receiver, listening on a localhost UDP socket, to receive the
269 // content and check whether it matches expectations.
270 //
271 // Note: This test is disabled until outstanding bugs are fixed and the
272 // media/cast library has achieved sufficient stability.
273 // http://crbug.com/349599
274 IN_PROC_BROWSER_TEST_F(CastStreamingApiTestWithPixelOutput, DISABLED_EndToEnd) {
275 // Determine a unused UDP port for the in-process receiver to listen on.
276 // Method: Bind a UDP socket on port 0, and then check which port the
277 // operating system assigned to it.
278 net::IPAddressNumber localhost;
279 localhost.push_back(127);
280 localhost.push_back(0);
281 localhost.push_back(0);
282 localhost.push_back(1);
283 scoped_ptr<net::UDPSocket> receive_socket(
284 new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND,
285 net::RandIntCallback(),
286 NULL,
287 net::NetLog::Source()));
288 receive_socket->AllowAddressReuse();
289 ASSERT_EQ(net::OK, receive_socket->Bind(net::IPEndPoint(localhost, 0)));
290 net::IPEndPoint receiver_end_point;
291 ASSERT_EQ(net::OK, receive_socket->GetLocalAddress(&receiver_end_point));
292 receive_socket.reset();
293
294 // Start the in-process receiver that examines audio/video for the expected
295 // test patterns.
296 const scoped_refptr<media::cast::StandaloneCastEnvironment> cast_environment(
297 new media::cast::StandaloneCastEnvironment(
298 media::cast::CastLoggingConfig()));
299 TestPatternReceiver* const receiver =
300 new TestPatternReceiver(cast_environment, receiver_end_point);
301 receiver->Start();
302
303 // Launch the page that: 1) renders the source content; 2) uses the
304 // chrome.tabCapture and chrome.cast.streaming APIs to capture its content and
305 // stream using Cast; and 3) calls chrome.test.succeed() once it is
306 // operational.
307 const std::string page_url = base::StringPrintf(
308 "end_to_end_sender.html?port=%d", receiver_end_point.port());
309 ASSERT_TRUE(RunExtensionSubtest("cast_streaming", page_url)) << message_;
310
311 // Examine the Cast receiver for expected audio/video test patterns. The
312 // colors and tones specified here must match those in end_to_end_sender.js.
313 const uint8 kRedInYUV[3] = {82, 90, 240}; // rgb(255, 0, 0)
314 const uint8 kGreenInYUV[3] = {145, 54, 34}; // rgb(0, 255, 0)
315 const uint8 kBlueInYUV[3] = {41, 240, 110}; // rgb(0, 0, 255)
316 const base::TimeDelta kOneHalfSecond = base::TimeDelta::FromMilliseconds(500);
317 receiver->WaitForColorAndTone(kRedInYUV, 200 /* Hz */, kOneHalfSecond);
318 receiver->WaitForColorAndTone(kGreenInYUV, 500 /* Hz */, kOneHalfSecond);
319 receiver->WaitForColorAndTone(kBlueInYUV, 1800 /* Hz */, kOneHalfSecond);
320
321 // TODO(miu): Uncomment once GetWeakPtr() NULL crash in PacedSender is fixed
322 // (see http://crbug.com/349786):
323 // receiver->DestroySoon();
324 cast_environment->Shutdown();
325 }
326
327 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698