b1fb59226668a32f193cab0b3021bc9ac5b04df7
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / video_encode_accelerator_unittest.cc
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 "base/at_exit.h"
6 #include "base/bind.h"
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/files/memory_mapped_file.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "base/process/process_handle.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/time/time.h"
16 #include "content/common/gpu/media/video_accelerator_unittest_helpers.h"
17 #include "media/base/bind_to_current_loop.h"
18 #include "media/base/bitstream_buffer.h"
19 #include "media/base/test_data_util.h"
20 #include "media/filters/h264_parser.h"
21 #include "media/video/video_encode_accelerator.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 #if defined(USE_X11)
25 #include "ui/gfx/x/x11_types.h"
26 #endif
27
28 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
29 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h"
30 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11)
31 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h"
32 #else
33 #error The VideoEncodeAcceleratorUnittest is not supported on this platform.
34 #endif
35
36 using media::VideoEncodeAccelerator;
37
38 namespace content {
39 namespace {
40
41 const media::VideoFrame::Format kInputFormat = media::VideoFrame::I420;
42
43 // Arbitrarily chosen to add some depth to the pipeline.
44 const unsigned int kNumOutputBuffers = 4;
45 const unsigned int kNumExtraInputFrames = 4;
46 // Maximum delay between requesting a keyframe and receiving one, in frames.
47 // Arbitrarily chosen as a reasonable requirement.
48 const unsigned int kMaxKeyframeDelay = 4;
49 // Value to use as max frame number for keyframe detection.
50 const unsigned int kMaxFrameNum =
51     std::numeric_limits<unsigned int>::max() - kMaxKeyframeDelay;
52 // Default initial bitrate.
53 const uint32 kDefaultBitrate = 2000000;
54 // Default ratio of requested_subsequent_bitrate to initial_bitrate
55 // (see test parameters below) if one is not provided.
56 const double kDefaultSubsequentBitrateRatio = 2.0;
57 // Default initial framerate.
58 const uint32 kDefaultFramerate = 30;
59 // Default ratio of requested_subsequent_framerate to initial_framerate
60 // (see test parameters below) if one is not provided.
61 const double kDefaultSubsequentFramerateRatio = 0.1;
62 // Tolerance factor for how encoded bitrate can differ from requested bitrate.
63 const double kBitrateTolerance = 0.1;
64 // Minimum required FPS throughput for the basic performance test.
65 const uint32 kMinPerfFPS = 30;
66 // Minimum (arbitrary) number of frames required to enforce bitrate requirements
67 // over. Streams shorter than this may be too short to realistically require
68 // an encoder to be able to converge to the requested bitrate over.
69 // The input stream will be looped as many times as needed in bitrate tests
70 // to reach at least this number of frames before calculating final bitrate.
71 const unsigned int kMinFramesForBitrateTests = 300;
72
73 // The syntax of multiple test streams is:
74 //  test-stream1;test-stream2;test-stream3
75 // The syntax of each test stream is:
76 // "in_filename:width:height:out_filename:requested_bitrate:requested_framerate
77 //  :requested_subsequent_bitrate:requested_subsequent_framerate"
78 // - |in_filename| must be an I420 (YUV planar) raw stream
79 //   (see http://www.fourcc.org/yuv.php#IYUV).
80 // - |width| and |height| are in pixels.
81 // - |profile| to encode into (values of media::VideoCodecProfile).
82 // - |out_filename| filename to save the encoded stream to (optional).
83 //   Output stream is saved for the simple encode test only.
84 // Further parameters are optional (need to provide preceding positional
85 // parameters if a specific subsequent parameter is required):
86 // - |requested_bitrate| requested bitrate in bits per second.
87 // - |requested_framerate| requested initial framerate.
88 // - |requested_subsequent_bitrate| bitrate to switch to in the middle of the
89 //                                  stream.
90 // - |requested_subsequent_framerate| framerate to switch to in the middle
91 //                                    of the stream.
92 //   Bitrate is only forced for tests that test bitrate.
93 const char* g_default_in_filename = "bear_320x192_40frames.yuv";
94 const char* g_default_in_parameters = ":320:192:1:out.h264:200000";
95 // Environment to store test stream data for all test cases.
96 class VideoEncodeAcceleratorTestEnvironment;
97 VideoEncodeAcceleratorTestEnvironment* g_env;
98
99 struct TestStream {
100   TestStream()
101       : num_frames(0),
102         aligned_buffer_size(0),
103         requested_bitrate(0),
104         requested_framerate(0),
105         requested_subsequent_bitrate(0),
106         requested_subsequent_framerate(0) {}
107   ~TestStream() {}
108
109   gfx::Size visible_size;
110   gfx::Size coded_size;
111   unsigned int num_frames;
112
113   // Original unaligned input file name provided as an argument to the test.
114   // And the file must be an I420 (YUV planar) raw stream.
115   std::string in_filename;
116
117   // A temporary file used to prepare aligned input buffers of |in_filename|.
118   // The file makes sure starting address of YUV planes are 64 byte-aligned.
119   base::FilePath aligned_in_file;
120
121   // The memory mapping of |aligned_in_file|
122   base::MemoryMappedFile mapped_aligned_in_file;
123
124   // Byte size of a frame of |aligned_in_file|.
125   size_t aligned_buffer_size;
126
127   // Byte size for each aligned plane of a frame
128   std::vector<size_t> aligned_plane_size;
129
130   std::string out_filename;
131   media::VideoCodecProfile requested_profile;
132   unsigned int requested_bitrate;
133   unsigned int requested_framerate;
134   unsigned int requested_subsequent_bitrate;
135   unsigned int requested_subsequent_framerate;
136 };
137
138 inline static size_t Align64Bytes(size_t value) {
139   return (value + 63) & ~63;
140 }
141
142 // Write |data| of |size| bytes at |offset| bytes into |file|.
143 static bool WriteFile(base::File* file,
144                       const off_t offset,
145                       const uint8* data,
146                       size_t size) {
147   size_t written_bytes = 0;
148   while (written_bytes < size) {
149     int bytes = file->Write(offset + written_bytes,
150                             reinterpret_cast<const char*>(data + written_bytes),
151                             size - written_bytes);
152     if (bytes <= 0)
153       return false;
154     written_bytes += bytes;
155   }
156   return true;
157 }
158
159 // ARM performs CPU cache management with CPU cache line granularity. We thus
160 // need to ensure our buffers are CPU cache line-aligned (64 byte-aligned).
161 // Otherwise newer kernels will refuse to accept them, and on older kernels
162 // we'll be treating ourselves to random corruption.
163 // Since we are just mapping and passing chunks of the input file directly to
164 // the VEA as input frames to avoid copying large chunks of raw data on each
165 // frame and thus affecting performance measurements, we have to prepare a
166 // temporary file with all planes aligned to 64-byte boundaries beforehand.
167 static void CreateAlignedInputStreamFile(const gfx::Size& coded_size,
168                                          TestStream* test_stream) {
169   // Test case may have many encoders and memory should be prepared once.
170   if (test_stream->coded_size == coded_size &&
171       test_stream->mapped_aligned_in_file.IsValid())
172     return;
173
174   // All encoders in multiple encoder test reuse the same test_stream, make
175   // sure they requested the same coded_size
176   ASSERT_TRUE(!test_stream->mapped_aligned_in_file.IsValid() ||
177               coded_size == test_stream->coded_size);
178   test_stream->coded_size = coded_size;
179
180   size_t num_planes = media::VideoFrame::NumPlanes(kInputFormat);
181   std::vector<size_t> padding_sizes(num_planes);
182   std::vector<size_t> coded_bpl(num_planes);
183   std::vector<size_t> visible_bpl(num_planes);
184   std::vector<size_t> visible_plane_rows(num_planes);
185
186   // Calculate padding in bytes to be added after each plane required to keep
187   // starting addresses of all planes at a 64 byte boudnary. This padding will
188   // be added after each plane when copying to the temporary file.
189   // At the same time we also need to take into account coded_size requested by
190   // the VEA; each row of visible_bpl bytes in the original file needs to be
191   // copied into a row of coded_bpl bytes in the aligned file.
192   for (size_t i = 0; i < num_planes; i++) {
193     size_t size =
194         media::VideoFrame::PlaneAllocationSize(kInputFormat, i, coded_size);
195     test_stream->aligned_plane_size.push_back(Align64Bytes(size));
196     test_stream->aligned_buffer_size += test_stream->aligned_plane_size.back();
197
198     coded_bpl[i] =
199         media::VideoFrame::RowBytes(i, kInputFormat, coded_size.width());
200     visible_bpl[i] = media::VideoFrame::RowBytes(
201         i, kInputFormat, test_stream->visible_size.width());
202     visible_plane_rows[i] = media::VideoFrame::Rows(
203         i, kInputFormat, test_stream->visible_size.height());
204     size_t padding_rows =
205         media::VideoFrame::Rows(i, kInputFormat, coded_size.height()) -
206         visible_plane_rows[i];
207     padding_sizes[i] = padding_rows * coded_bpl[i] + Align64Bytes(size) - size;
208   }
209
210   base::MemoryMappedFile src_file;
211   CHECK(src_file.Initialize(base::FilePath(test_stream->in_filename)));
212   CHECK(base::CreateTemporaryFile(&test_stream->aligned_in_file));
213
214   size_t visible_buffer_size = media::VideoFrame::AllocationSize(
215       kInputFormat, test_stream->visible_size);
216   CHECK_EQ(src_file.length() % visible_buffer_size, 0U)
217       << "Stream byte size is not a product of calculated frame byte size";
218
219   test_stream->num_frames = src_file.length() / visible_buffer_size;
220   uint32 flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
221                  base::File::FLAG_READ;
222
223   // Create a temporary file with coded_size length.
224   base::File dest_file(test_stream->aligned_in_file, flags);
225   CHECK_GT(test_stream->aligned_buffer_size, 0UL);
226   dest_file.SetLength(test_stream->aligned_buffer_size *
227                       test_stream->num_frames);
228
229   const uint8* src = src_file.data();
230   off_t dest_offset = 0;
231   for (size_t frame = 0; frame < test_stream->num_frames; frame++) {
232     for (size_t i = 0; i < num_planes; i++) {
233       // Assert that each plane of frame starts at 64 byte boundary.
234       ASSERT_EQ(dest_offset & 63, 0)
235           << "Planes of frame should be mapped at a 64 byte boundary";
236       for (size_t j = 0; j < visible_plane_rows[i]; j++) {
237         CHECK(WriteFile(&dest_file, dest_offset, src, visible_bpl[i]));
238         src += visible_bpl[i];
239         dest_offset += coded_bpl[i];
240       }
241       dest_offset += padding_sizes[i];
242     }
243   }
244   CHECK(test_stream->mapped_aligned_in_file.Initialize(dest_file.Pass()));
245   // Assert that memory mapped of file starts at 64 byte boundary. So each
246   // plane of frames also start at 64 byte boundary.
247   ASSERT_EQ(
248       reinterpret_cast<off_t>(test_stream->mapped_aligned_in_file.data()) & 63,
249       0)
250       << "File should be mapped at a 64 byte boundary";
251
252   CHECK_EQ(test_stream->mapped_aligned_in_file.length() %
253                test_stream->aligned_buffer_size,
254            0U)
255       << "Stream byte size is not a product of calculated frame byte size";
256   CHECK_GT(test_stream->num_frames, 0UL);
257   CHECK_LE(test_stream->num_frames, kMaxFrameNum);
258 }
259
260 // Parse |data| into its constituent parts, set the various output fields
261 // accordingly, read in video stream, and store them to |test_streams|.
262 static void ParseAndReadTestStreamData(const base::FilePath::StringType& data,
263                                        ScopedVector<TestStream>* test_streams) {
264   // Split the string to individual test stream data.
265   std::vector<base::FilePath::StringType> test_streams_data;
266   base::SplitString(data, ';', &test_streams_data);
267   CHECK_GE(test_streams_data.size(), 1U) << data;
268
269   // Parse each test stream data and read the input file.
270   for (size_t index = 0; index < test_streams_data.size(); ++index) {
271     std::vector<base::FilePath::StringType> fields;
272     base::SplitString(test_streams_data[index], ':', &fields);
273     CHECK_GE(fields.size(), 4U) << data;
274     CHECK_LE(fields.size(), 9U) << data;
275     TestStream* test_stream = new TestStream();
276
277     test_stream->in_filename = fields[0];
278     int width, height;
279     CHECK(base::StringToInt(fields[1], &width));
280     CHECK(base::StringToInt(fields[2], &height));
281     test_stream->visible_size = gfx::Size(width, height);
282     CHECK(!test_stream->visible_size.IsEmpty());
283     int profile;
284     CHECK(base::StringToInt(fields[3], &profile));
285     CHECK_GT(profile, media::VIDEO_CODEC_PROFILE_UNKNOWN);
286     CHECK_LE(profile, media::VIDEO_CODEC_PROFILE_MAX);
287     test_stream->requested_profile =
288         static_cast<media::VideoCodecProfile>(profile);
289
290     if (fields.size() >= 5 && !fields[4].empty())
291       test_stream->out_filename = fields[4];
292
293     if (fields.size() >= 6 && !fields[5].empty())
294       CHECK(base::StringToUint(fields[5], &test_stream->requested_bitrate));
295
296     if (fields.size() >= 7 && !fields[6].empty())
297       CHECK(base::StringToUint(fields[6], &test_stream->requested_framerate));
298
299     if (fields.size() >= 8 && !fields[7].empty()) {
300       CHECK(base::StringToUint(fields[7],
301                                &test_stream->requested_subsequent_bitrate));
302     }
303
304     if (fields.size() >= 9 && !fields[8].empty()) {
305       CHECK(base::StringToUint(fields[8],
306                                &test_stream->requested_subsequent_framerate));
307     }
308     test_streams->push_back(test_stream);
309   }
310 }
311
312 enum ClientState {
313   CS_CREATED,
314   CS_ENCODER_SET,
315   CS_INITIALIZED,
316   CS_ENCODING,
317   CS_FINISHED,
318   CS_ERROR,
319 };
320
321 // Performs basic, codec-specific sanity checks on the stream buffers passed
322 // to ProcessStreamBuffer(): whether we've seen keyframes before non-keyframes,
323 // correct sequences of H.264 NALUs (SPS before PPS and before slices), etc.
324 // Calls given FrameFoundCallback when a complete frame is found while
325 // processing.
326 class StreamValidator {
327  public:
328   // To be called when a complete frame is found while processing a stream
329   // buffer, passing true if the frame is a keyframe. Returns false if we
330   // are not interested in more frames and further processing should be aborted.
331   typedef base::Callback<bool(bool)> FrameFoundCallback;
332
333   virtual ~StreamValidator() {}
334
335   // Provide a StreamValidator instance for the given |profile|.
336   static scoped_ptr<StreamValidator> Create(media::VideoCodecProfile profile,
337                                             const FrameFoundCallback& frame_cb);
338
339   // Process and verify contents of a bitstream buffer.
340   virtual void ProcessStreamBuffer(const uint8* stream, size_t size) = 0;
341
342  protected:
343   explicit StreamValidator(const FrameFoundCallback& frame_cb)
344       : frame_cb_(frame_cb) {}
345
346   FrameFoundCallback frame_cb_;
347 };
348
349 class H264Validator : public StreamValidator {
350  public:
351   explicit H264Validator(const FrameFoundCallback& frame_cb)
352       : StreamValidator(frame_cb),
353         seen_sps_(false),
354         seen_pps_(false),
355         seen_idr_(false) {}
356
357   virtual void ProcessStreamBuffer(const uint8* stream, size_t size) override;
358
359  private:
360   // Set to true when encoder provides us with the corresponding NALU type.
361   bool seen_sps_;
362   bool seen_pps_;
363   bool seen_idr_;
364
365   media::H264Parser h264_parser_;
366 };
367
368 void H264Validator::ProcessStreamBuffer(const uint8* stream, size_t size) {
369   h264_parser_.SetStream(stream, size);
370
371   while (1) {
372     media::H264NALU nalu;
373     media::H264Parser::Result result;
374
375     result = h264_parser_.AdvanceToNextNALU(&nalu);
376     if (result == media::H264Parser::kEOStream)
377       break;
378
379     ASSERT_EQ(media::H264Parser::kOk, result);
380
381     bool keyframe = false;
382
383     switch (nalu.nal_unit_type) {
384       case media::H264NALU::kIDRSlice:
385         ASSERT_TRUE(seen_sps_);
386         ASSERT_TRUE(seen_pps_);
387         seen_idr_ = true;
388         keyframe = true;
389         // fallthrough
390       case media::H264NALU::kNonIDRSlice: {
391         ASSERT_TRUE(seen_idr_);
392         if (!frame_cb_.Run(keyframe))
393           return;
394         break;
395       }
396
397       case media::H264NALU::kSPS: {
398         int sps_id;
399         ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParseSPS(&sps_id));
400         seen_sps_ = true;
401         break;
402       }
403
404       case media::H264NALU::kPPS: {
405         ASSERT_TRUE(seen_sps_);
406         int pps_id;
407         ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParsePPS(&pps_id));
408         seen_pps_ = true;
409         break;
410       }
411
412       default:
413         break;
414     }
415   }
416 }
417
418 class VP8Validator : public StreamValidator {
419  public:
420   explicit VP8Validator(const FrameFoundCallback& frame_cb)
421       : StreamValidator(frame_cb),
422         seen_keyframe_(false) {}
423
424   virtual void ProcessStreamBuffer(const uint8* stream, size_t size) override;
425
426  private:
427   // Have we already got a keyframe in the stream?
428   bool seen_keyframe_;
429 };
430
431 void VP8Validator::ProcessStreamBuffer(const uint8* stream, size_t size) {
432   bool keyframe = !(stream[0] & 0x01);
433   if (keyframe)
434     seen_keyframe_ = true;
435
436   EXPECT_TRUE(seen_keyframe_);
437
438   frame_cb_.Run(keyframe);
439   // TODO(posciak): We could be getting more frames in the buffer, but there is
440   // no simple way to detect this. We'd need to parse the frames and go through
441   // partition numbers/sizes. For now assume one frame per buffer.
442 }
443
444 // static
445 scoped_ptr<StreamValidator> StreamValidator::Create(
446     media::VideoCodecProfile profile,
447     const FrameFoundCallback& frame_cb) {
448   scoped_ptr<StreamValidator> validator;
449
450   if (profile >= media::H264PROFILE_MIN &&
451       profile <= media::H264PROFILE_MAX) {
452     validator.reset(new H264Validator(frame_cb));
453   } else if (profile >= media::VP8PROFILE_MIN &&
454              profile <= media::VP8PROFILE_MAX) {
455     validator.reset(new VP8Validator(frame_cb));
456   } else {
457     LOG(FATAL) << "Unsupported profile: " << profile;
458   }
459
460   return validator.Pass();
461 }
462
463 class VEAClient : public VideoEncodeAccelerator::Client {
464  public:
465   VEAClient(TestStream* test_stream,
466             ClientStateNotification<ClientState>* note,
467             bool save_to_file,
468             unsigned int keyframe_period,
469             bool force_bitrate,
470             bool test_perf,
471             bool mid_stream_bitrate_switch,
472             bool mid_stream_framerate_switch);
473   virtual ~VEAClient();
474   void CreateEncoder();
475   void DestroyEncoder();
476
477   // Return the number of encoded frames per second.
478   double frames_per_second();
479
480   // VideoDecodeAccelerator::Client implementation.
481   virtual void RequireBitstreamBuffers(unsigned int input_count,
482                                        const gfx::Size& input_coded_size,
483                                        size_t output_buffer_size) override;
484   virtual void BitstreamBufferReady(int32 bitstream_buffer_id,
485                                     size_t payload_size,
486                                     bool key_frame) override;
487   virtual void NotifyError(VideoEncodeAccelerator::Error error) override;
488
489  private:
490   bool has_encoder() { return encoder_.get(); }
491
492   void SetState(ClientState new_state);
493
494   // Set current stream parameters to given |bitrate| at |framerate|.
495   void SetStreamParameters(unsigned int bitrate, unsigned int framerate);
496
497   // Called when encoder is done with a VideoFrame.
498   void InputNoLongerNeededCallback(int32 input_id);
499
500   // Ensure encoder has at least as many inputs as it asked for
501   // via RequireBitstreamBuffers().
502   void FeedEncoderWithInputs();
503
504   // Provide the encoder with a new output buffer.
505   void FeedEncoderWithOutput(base::SharedMemory* shm);
506
507   // Called on finding a complete frame (with |keyframe| set to true for
508   // keyframes) in the stream, to perform codec-independent, per-frame checks
509   // and accounting. Returns false once we have collected all frames we needed.
510   bool HandleEncodedFrame(bool keyframe);
511
512   // Verify that stream bitrate has been close to current_requested_bitrate_,
513   // assuming current_framerate_ since the last time VerifyStreamProperties()
514   // was called. Fail the test if |force_bitrate_| is true and the bitrate
515   // is not within kBitrateTolerance.
516   void VerifyStreamProperties();
517
518   // Test codec performance, failing the test if we are currently running
519   // the performance test.
520   void VerifyPerf();
521
522   // Prepare and return a frame wrapping the data at |position| bytes in
523   // the input stream, ready to be sent to encoder.
524   scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position);
525
526   // Update the parameters according to |mid_stream_bitrate_switch| and
527   // |mid_stream_framerate_switch|.
528   void UpdateTestStreamData(bool mid_stream_bitrate_switch,
529                             bool mid_stream_framerate_switch);
530
531   ClientState state_;
532   scoped_ptr<VideoEncodeAccelerator> encoder_;
533
534   TestStream* test_stream_;
535   // Used to notify another thread about the state. VEAClient does not own this.
536   ClientStateNotification<ClientState>* note_;
537
538   // Ids assigned to VideoFrames (start at 1 for easy comparison with
539   // num_encoded_frames_).
540   std::set<int32> inputs_at_client_;
541   int32 next_input_id_;
542
543   // Ids for output BitstreamBuffers.
544   typedef std::map<int32, base::SharedMemory*> IdToSHM;
545   ScopedVector<base::SharedMemory> output_shms_;
546   IdToSHM output_buffers_at_client_;
547   int32 next_output_buffer_id_;
548
549   // Current offset into input stream.
550   off_t pos_in_input_stream_;
551   gfx::Size input_coded_size_;
552   // Requested by encoder.
553   unsigned int num_required_input_buffers_;
554   size_t output_buffer_size_;
555
556   // Number of frames to encode. This may differ from the number of frames in
557   // stream if we need more frames for bitrate tests.
558   unsigned int num_frames_to_encode_;
559
560   // Number of encoded frames we've got from the encoder thus far.
561   unsigned int num_encoded_frames_;
562
563   // Frames since last bitrate verification.
564   unsigned int num_frames_since_last_check_;
565
566   // True if received a keyframe while processing current bitstream buffer.
567   bool seen_keyframe_in_this_buffer_;
568
569   // True if we are to save the encoded stream to a file.
570   bool save_to_file_;
571
572   // Request a keyframe every keyframe_period_ frames.
573   const unsigned int keyframe_period_;
574
575   // Frame number for which we requested a keyframe.
576   unsigned int keyframe_requested_at_;
577
578   // True if we are asking encoder for a particular bitrate.
579   bool force_bitrate_;
580
581   // Current requested bitrate.
582   unsigned int current_requested_bitrate_;
583
584   // Current expected framerate.
585   unsigned int current_framerate_;
586
587   // Byte size of the encoded stream (for bitrate calculation) since last
588   // time we checked bitrate.
589   size_t encoded_stream_size_since_last_check_;
590
591   // If true, verify performance at the end of the test.
592   bool test_perf_;
593
594   scoped_ptr<StreamValidator> validator_;
595
596   // The time when the encoding started.
597   base::TimeTicks encode_start_time_;
598
599   // The time when the last encoded frame is ready.
600   base::TimeTicks last_frame_ready_time_;
601
602   // All methods of this class should be run on the same thread.
603   base::ThreadChecker thread_checker_;
604
605   // Requested bitrate in bits per second.
606   unsigned int requested_bitrate_;
607
608   // Requested initial framerate.
609   unsigned int requested_framerate_;
610
611   // Bitrate to switch to in the middle of the stream.
612   unsigned int requested_subsequent_bitrate_;
613
614   // Framerate to switch to in the middle of the stream.
615   unsigned int requested_subsequent_framerate_;
616 };
617
618 VEAClient::VEAClient(TestStream* test_stream,
619                      ClientStateNotification<ClientState>* note,
620                      bool save_to_file,
621                      unsigned int keyframe_period,
622                      bool force_bitrate,
623                      bool test_perf,
624                      bool mid_stream_bitrate_switch,
625                      bool mid_stream_framerate_switch)
626     : state_(CS_CREATED),
627       test_stream_(test_stream),
628       note_(note),
629       next_input_id_(1),
630       next_output_buffer_id_(0),
631       pos_in_input_stream_(0),
632       num_required_input_buffers_(0),
633       output_buffer_size_(0),
634       num_frames_to_encode_(0),
635       num_encoded_frames_(0),
636       num_frames_since_last_check_(0),
637       seen_keyframe_in_this_buffer_(false),
638       save_to_file_(save_to_file),
639       keyframe_period_(keyframe_period),
640       keyframe_requested_at_(kMaxFrameNum),
641       force_bitrate_(force_bitrate),
642       current_requested_bitrate_(0),
643       current_framerate_(0),
644       encoded_stream_size_since_last_check_(0),
645       test_perf_(test_perf),
646       requested_bitrate_(0),
647       requested_framerate_(0),
648       requested_subsequent_bitrate_(0),
649       requested_subsequent_framerate_(0) {
650   if (keyframe_period_)
651     CHECK_LT(kMaxKeyframeDelay, keyframe_period_);
652
653   validator_ = StreamValidator::Create(
654       test_stream_->requested_profile,
655       base::Bind(&VEAClient::HandleEncodedFrame, base::Unretained(this)));
656
657   CHECK(validator_.get());
658
659   if (save_to_file_) {
660     CHECK(!test_stream_->out_filename.empty());
661     base::FilePath out_filename(test_stream_->out_filename);
662     // This creates or truncates out_filename.
663     // Without it, AppendToFile() will not work.
664     EXPECT_EQ(0, base::WriteFile(out_filename, NULL, 0));
665   }
666
667   // Initialize the parameters of the test streams.
668   UpdateTestStreamData(mid_stream_bitrate_switch, mid_stream_framerate_switch);
669
670   thread_checker_.DetachFromThread();
671 }
672
673 VEAClient::~VEAClient() { CHECK(!has_encoder()); }
674
675 void VEAClient::CreateEncoder() {
676   DCHECK(thread_checker_.CalledOnValidThread());
677   CHECK(!has_encoder());
678
679 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
680   scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder);
681   encoder_.reset(new V4L2VideoEncodeAccelerator(device.Pass()));
682 #elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11)
683   encoder_.reset(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay()));
684 #endif
685
686   SetState(CS_ENCODER_SET);
687
688   DVLOG(1) << "Profile: " << test_stream_->requested_profile
689            << ", initial bitrate: " << requested_bitrate_;
690   if (!encoder_->Initialize(kInputFormat,
691                             test_stream_->visible_size,
692                             test_stream_->requested_profile,
693                             requested_bitrate_,
694                             this)) {
695     LOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed";
696     SetState(CS_ERROR);
697     return;
698   }
699
700   SetStreamParameters(requested_bitrate_, requested_framerate_);
701   SetState(CS_INITIALIZED);
702 }
703
704 void VEAClient::DestroyEncoder() {
705   DCHECK(thread_checker_.CalledOnValidThread());
706   if (!has_encoder())
707     return;
708   encoder_.reset();
709 }
710
711 void VEAClient::UpdateTestStreamData(bool mid_stream_bitrate_switch,
712                                      bool mid_stream_framerate_switch) {
713   // Use defaults for bitrate/framerate if they are not provided.
714   if (test_stream_->requested_bitrate == 0)
715     requested_bitrate_ = kDefaultBitrate;
716   else
717     requested_bitrate_ = test_stream_->requested_bitrate;
718
719   if (test_stream_->requested_framerate == 0)
720     requested_framerate_ = kDefaultFramerate;
721   else
722     requested_framerate_ = test_stream_->requested_framerate;
723
724   // If bitrate/framerate switch is requested, use the subsequent values if
725   // provided, or, if not, calculate them from their initial values using
726   // the default ratios.
727   // Otherwise, if a switch is not requested, keep the initial values.
728   if (mid_stream_bitrate_switch) {
729     if (test_stream_->requested_subsequent_bitrate == 0)
730       requested_subsequent_bitrate_ =
731           requested_bitrate_ * kDefaultSubsequentBitrateRatio;
732     else
733       requested_subsequent_bitrate_ =
734           test_stream_->requested_subsequent_bitrate;
735   } else {
736     requested_subsequent_bitrate_ = requested_bitrate_;
737   }
738   if (requested_subsequent_bitrate_ == 0)
739     requested_subsequent_bitrate_ = 1;
740
741   if (mid_stream_framerate_switch) {
742     if (test_stream_->requested_subsequent_framerate == 0)
743       requested_subsequent_framerate_ =
744           requested_framerate_ * kDefaultSubsequentFramerateRatio;
745     else
746       requested_subsequent_framerate_ =
747           test_stream_->requested_subsequent_framerate;
748   } else {
749     requested_subsequent_framerate_ = requested_framerate_;
750   }
751   if (requested_subsequent_framerate_ == 0)
752     requested_subsequent_framerate_ = 1;
753 }
754
755 double VEAClient::frames_per_second() {
756   base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_;
757   return num_encoded_frames_ / duration.InSecondsF();
758 }
759
760 void VEAClient::RequireBitstreamBuffers(unsigned int input_count,
761                                         const gfx::Size& input_coded_size,
762                                         size_t output_size) {
763   DCHECK(thread_checker_.CalledOnValidThread());
764   ASSERT_EQ(state_, CS_INITIALIZED);
765   SetState(CS_ENCODING);
766
767   CreateAlignedInputStreamFile(input_coded_size, test_stream_);
768
769   // We may need to loop over the stream more than once if more frames than
770   // provided is required for bitrate tests.
771   if (force_bitrate_ && test_stream_->num_frames < kMinFramesForBitrateTests) {
772     DVLOG(1) << "Stream too short for bitrate test ("
773              << test_stream_->num_frames << " frames), will loop it to reach "
774              << kMinFramesForBitrateTests << " frames";
775     num_frames_to_encode_ = kMinFramesForBitrateTests;
776   } else {
777     num_frames_to_encode_ = test_stream_->num_frames;
778   }
779
780   input_coded_size_ = input_coded_size;
781   num_required_input_buffers_ = input_count;
782   ASSERT_GT(num_required_input_buffers_, 0UL);
783
784   output_buffer_size_ = output_size;
785   ASSERT_GT(output_buffer_size_, 0UL);
786
787   for (unsigned int i = 0; i < kNumOutputBuffers; ++i) {
788     base::SharedMemory* shm = new base::SharedMemory();
789     CHECK(shm->CreateAndMapAnonymous(output_buffer_size_));
790     output_shms_.push_back(shm);
791     FeedEncoderWithOutput(shm);
792   }
793
794   encode_start_time_ = base::TimeTicks::Now();
795   FeedEncoderWithInputs();
796 }
797
798 void VEAClient::BitstreamBufferReady(int32 bitstream_buffer_id,
799                                      size_t payload_size,
800                                      bool key_frame) {
801   DCHECK(thread_checker_.CalledOnValidThread());
802   ASSERT_LE(payload_size, output_buffer_size_);
803
804   IdToSHM::iterator it = output_buffers_at_client_.find(bitstream_buffer_id);
805   ASSERT_NE(it, output_buffers_at_client_.end());
806   base::SharedMemory* shm = it->second;
807   output_buffers_at_client_.erase(it);
808
809   if (state_ == CS_FINISHED)
810     return;
811
812   encoded_stream_size_since_last_check_ += payload_size;
813
814   const uint8* stream_ptr = static_cast<const uint8*>(shm->memory());
815   if (payload_size > 0)
816     validator_->ProcessStreamBuffer(stream_ptr, payload_size);
817
818   EXPECT_EQ(key_frame, seen_keyframe_in_this_buffer_);
819   seen_keyframe_in_this_buffer_ = false;
820
821   if (save_to_file_) {
822     int size = base::checked_cast<int>(payload_size);
823     EXPECT_TRUE(base::AppendToFile(
824                     base::FilePath::FromUTF8Unsafe(test_stream_->out_filename),
825                     static_cast<char*>(shm->memory()),
826                     size));
827   }
828
829   FeedEncoderWithOutput(shm);
830 }
831
832 void VEAClient::NotifyError(VideoEncodeAccelerator::Error error) {
833   DCHECK(thread_checker_.CalledOnValidThread());
834   SetState(CS_ERROR);
835 }
836
837 void VEAClient::SetState(ClientState new_state) {
838   DVLOG(4) << "Changing state " << state_ << "->" << new_state;
839   note_->Notify(new_state);
840   state_ = new_state;
841 }
842
843 void VEAClient::SetStreamParameters(unsigned int bitrate,
844                                     unsigned int framerate) {
845   current_requested_bitrate_ = bitrate;
846   current_framerate_ = framerate;
847   CHECK_GT(current_requested_bitrate_, 0UL);
848   CHECK_GT(current_framerate_, 0UL);
849   encoder_->RequestEncodingParametersChange(current_requested_bitrate_,
850                                             current_framerate_);
851   DVLOG(1) << "Switched parameters to " << current_requested_bitrate_
852            << " bps @ " << current_framerate_ << " FPS";
853 }
854
855 void VEAClient::InputNoLongerNeededCallback(int32 input_id) {
856   std::set<int32>::iterator it = inputs_at_client_.find(input_id);
857   ASSERT_NE(it, inputs_at_client_.end());
858   inputs_at_client_.erase(it);
859   FeedEncoderWithInputs();
860 }
861
862 scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) {
863   CHECK_LE(position + test_stream_->aligned_buffer_size,
864            test_stream_->mapped_aligned_in_file.length());
865
866   uint8* frame_data_y = const_cast<uint8*>(
867       test_stream_->mapped_aligned_in_file.data() + position);
868   uint8* frame_data_u = frame_data_y + test_stream_->aligned_plane_size[0];
869   uint8* frame_data_v = frame_data_u + test_stream_->aligned_plane_size[1];
870
871   CHECK_GT(current_framerate_, 0U);
872   scoped_refptr<media::VideoFrame> frame =
873       media::VideoFrame::WrapExternalYuvData(
874           kInputFormat,
875           input_coded_size_,
876           gfx::Rect(test_stream_->visible_size),
877           test_stream_->visible_size,
878           input_coded_size_.width(),
879           input_coded_size_.width() / 2,
880           input_coded_size_.width() / 2,
881           frame_data_y,
882           frame_data_u,
883           frame_data_v,
884           base::TimeDelta().FromMilliseconds(
885               next_input_id_ * base::Time::kMillisecondsPerSecond /
886               current_framerate_),
887           media::BindToCurrentLoop(
888               base::Bind(&VEAClient::InputNoLongerNeededCallback,
889                          base::Unretained(this),
890                          next_input_id_)));
891
892   CHECK(inputs_at_client_.insert(next_input_id_).second);
893   ++next_input_id_;
894
895   return frame;
896 }
897
898 void VEAClient::FeedEncoderWithInputs() {
899   if (!has_encoder())
900     return;
901
902   if (state_ != CS_ENCODING)
903     return;
904
905   while (inputs_at_client_.size() <
906          num_required_input_buffers_ + kNumExtraInputFrames) {
907     size_t bytes_left =
908         test_stream_->mapped_aligned_in_file.length() - pos_in_input_stream_;
909     if (bytes_left < test_stream_->aligned_buffer_size) {
910       DCHECK_EQ(bytes_left, 0UL);
911       // Rewind if at the end of stream and we are still encoding.
912       // This is to flush the encoder with additional frames from the beginning
913       // of the stream, or if the stream is shorter that the number of frames
914       // we require for bitrate tests.
915       pos_in_input_stream_ = 0;
916       continue;
917     }
918
919     bool force_keyframe = false;
920     if (keyframe_period_ && next_input_id_ % keyframe_period_ == 0) {
921       keyframe_requested_at_ = next_input_id_;
922       force_keyframe = true;
923     }
924
925     scoped_refptr<media::VideoFrame> video_frame =
926         PrepareInputFrame(pos_in_input_stream_);
927     pos_in_input_stream_ += test_stream_->aligned_buffer_size;
928
929     encoder_->Encode(video_frame, force_keyframe);
930   }
931 }
932
933 void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) {
934   if (!has_encoder())
935     return;
936
937   if (state_ != CS_ENCODING)
938     return;
939
940   base::SharedMemoryHandle dup_handle;
941   CHECK(shm->ShareToProcess(base::GetCurrentProcessHandle(), &dup_handle));
942
943   media::BitstreamBuffer bitstream_buffer(
944       next_output_buffer_id_++, dup_handle, output_buffer_size_);
945   CHECK(output_buffers_at_client_.insert(std::make_pair(bitstream_buffer.id(),
946                                                         shm)).second);
947   encoder_->UseOutputBitstreamBuffer(bitstream_buffer);
948 }
949
950 bool VEAClient::HandleEncodedFrame(bool keyframe) {
951   // This would be a bug in the test, which should not ignore false
952   // return value from this method.
953   CHECK_LE(num_encoded_frames_, num_frames_to_encode_);
954
955   ++num_encoded_frames_;
956   ++num_frames_since_last_check_;
957
958   last_frame_ready_time_ = base::TimeTicks::Now();
959   if (keyframe) {
960     // Got keyframe, reset keyframe detection regardless of whether we
961     // got a frame in time or not.
962     keyframe_requested_at_ = kMaxFrameNum;
963     seen_keyframe_in_this_buffer_ = true;
964   }
965
966   // Because the keyframe behavior requirements are loose, we give
967   // the encoder more freedom here. It could either deliver a keyframe
968   // immediately after we requested it, which could be for a frame number
969   // before the one we requested it for (if the keyframe request
970   // is asynchronous, i.e. not bound to any concrete frame, and because
971   // the pipeline can be deeper than one frame), at that frame, or after.
972   // So the only constraints we put here is that we get a keyframe not
973   // earlier than we requested one (in time), and not later than
974   // kMaxKeyframeDelay frames after the frame, for which we requested
975   // it, comes back encoded.
976   EXPECT_LE(num_encoded_frames_, keyframe_requested_at_ + kMaxKeyframeDelay);
977
978   if (num_encoded_frames_ == num_frames_to_encode_ / 2) {
979     VerifyStreamProperties();
980     if (requested_subsequent_bitrate_ != current_requested_bitrate_ ||
981         requested_subsequent_framerate_ != current_framerate_) {
982       SetStreamParameters(requested_subsequent_bitrate_,
983                           requested_subsequent_framerate_);
984     }
985   } else if (num_encoded_frames_ == num_frames_to_encode_) {
986     VerifyPerf();
987     VerifyStreamProperties();
988     SetState(CS_FINISHED);
989     return false;
990   }
991
992   return true;
993 }
994
995 void VEAClient::VerifyPerf() {
996   double measured_fps = frames_per_second();
997   LOG(INFO) << "Measured encoder FPS: " << measured_fps;
998   if (test_perf_)
999     EXPECT_GE(measured_fps, kMinPerfFPS);
1000 }
1001
1002 void VEAClient::VerifyStreamProperties() {
1003   CHECK_GT(num_frames_since_last_check_, 0UL);
1004   CHECK_GT(encoded_stream_size_since_last_check_, 0UL);
1005   unsigned int bitrate = encoded_stream_size_since_last_check_ * 8 *
1006                          current_framerate_ / num_frames_since_last_check_;
1007   DVLOG(1) << "Current chunk's bitrate: " << bitrate
1008            << " (expected: " << current_requested_bitrate_
1009            << " @ " << current_framerate_ << " FPS,"
1010            << " num frames in chunk: " << num_frames_since_last_check_;
1011
1012   num_frames_since_last_check_ = 0;
1013   encoded_stream_size_since_last_check_ = 0;
1014
1015   if (force_bitrate_) {
1016     EXPECT_NEAR(bitrate,
1017                 current_requested_bitrate_,
1018                 kBitrateTolerance * current_requested_bitrate_);
1019   }
1020 }
1021
1022 // Setup test stream data and delete temporary aligned files at the beginning
1023 // and end of unittest. We only need to setup once for all test cases.
1024 class VideoEncodeAcceleratorTestEnvironment : public ::testing::Environment {
1025  public:
1026   VideoEncodeAcceleratorTestEnvironment(
1027       scoped_ptr<base::FilePath::StringType> data) {
1028     test_stream_data_ = data.Pass();
1029   }
1030
1031   virtual void SetUp() {
1032     ParseAndReadTestStreamData(*test_stream_data_, &test_streams_);
1033   }
1034
1035   virtual void TearDown() {
1036     for (size_t i = 0; i < test_streams_.size(); i++) {
1037       base::DeleteFile(test_streams_[i]->aligned_in_file, false);
1038     }
1039   }
1040
1041   ScopedVector<TestStream> test_streams_;
1042
1043  private:
1044   scoped_ptr<base::FilePath::StringType> test_stream_data_;
1045 };
1046
1047 // Test parameters:
1048 // - Number of concurrent encoders.
1049 // - If true, save output to file (provided an output filename was supplied).
1050 // - Force a keyframe every n frames.
1051 // - Force bitrate; the actual required value is provided as a property
1052 //   of the input stream, because it depends on stream type/resolution/etc.
1053 // - If true, measure performance.
1054 // - If true, switch bitrate mid-stream.
1055 // - If true, switch framerate mid-stream.
1056 class VideoEncodeAcceleratorTest
1057     : public ::testing::TestWithParam<
1058           Tuple7<int, bool, int, bool, bool, bool, bool> > {};
1059
1060 TEST_P(VideoEncodeAcceleratorTest, TestSimpleEncode) {
1061   const size_t num_concurrent_encoders = GetParam().a;
1062   const bool save_to_file = GetParam().b;
1063   const unsigned int keyframe_period = GetParam().c;
1064   const bool force_bitrate = GetParam().d;
1065   const bool test_perf = GetParam().e;
1066   const bool mid_stream_bitrate_switch = GetParam().f;
1067   const bool mid_stream_framerate_switch = GetParam().g;
1068
1069   ScopedVector<ClientStateNotification<ClientState> > notes;
1070   ScopedVector<VEAClient> clients;
1071   base::Thread encoder_thread("EncoderThread");
1072   ASSERT_TRUE(encoder_thread.Start());
1073
1074   // Create all encoders.
1075   for (size_t i = 0; i < num_concurrent_encoders; i++) {
1076     size_t test_stream_index = i % g_env->test_streams_.size();
1077     // Disregard save_to_file if we didn't get an output filename.
1078     bool encoder_save_to_file =
1079         (save_to_file &&
1080          !g_env->test_streams_[test_stream_index]->out_filename.empty());
1081
1082     notes.push_back(new ClientStateNotification<ClientState>());
1083     clients.push_back(new VEAClient(g_env->test_streams_[test_stream_index],
1084                                     notes.back(),
1085                                     encoder_save_to_file,
1086                                     keyframe_period,
1087                                     force_bitrate,
1088                                     test_perf,
1089                                     mid_stream_bitrate_switch,
1090                                     mid_stream_framerate_switch));
1091
1092     encoder_thread.message_loop()->PostTask(
1093         FROM_HERE,
1094         base::Bind(&VEAClient::CreateEncoder,
1095                    base::Unretained(clients.back())));
1096   }
1097
1098   // All encoders must pass through states in this order.
1099   enum ClientState state_transitions[] = {CS_ENCODER_SET, CS_INITIALIZED,
1100                                           CS_ENCODING, CS_FINISHED};
1101
1102   // Wait for all encoders to go through all states and finish.
1103   // Do this by waiting for all encoders to advance to state n before checking
1104   // state n+1, to verify that they are able to operate concurrently.
1105   // It also simulates the real-world usage better, as the main thread, on which
1106   // encoders are created/destroyed, is a single GPU Process ChildThread.
1107   // Moreover, we can't have proper multithreading on X11, so this could cause
1108   // hard to debug issues there, if there were multiple "ChildThreads".
1109   for (size_t state_no = 0; state_no < arraysize(state_transitions); ++state_no)
1110     for (size_t i = 0; i < num_concurrent_encoders; i++)
1111       ASSERT_EQ(notes[i]->Wait(), state_transitions[state_no]);
1112
1113   for (size_t i = 0; i < num_concurrent_encoders; ++i) {
1114     encoder_thread.message_loop()->PostTask(
1115         FROM_HERE,
1116         base::Bind(&VEAClient::DestroyEncoder, base::Unretained(clients[i])));
1117   }
1118
1119   // This ensures all tasks have finished.
1120   encoder_thread.Stop();
1121 }
1122
1123 INSTANTIATE_TEST_CASE_P(
1124     SimpleEncode,
1125     VideoEncodeAcceleratorTest,
1126     ::testing::Values(MakeTuple(1, true, 0, false, false, false, false)));
1127
1128 INSTANTIATE_TEST_CASE_P(
1129     EncoderPerf,
1130     VideoEncodeAcceleratorTest,
1131     ::testing::Values(MakeTuple(1, false, 0, false, true, false, false)));
1132
1133 INSTANTIATE_TEST_CASE_P(
1134     ForceKeyframes,
1135     VideoEncodeAcceleratorTest,
1136     ::testing::Values(MakeTuple(1, false, 10, false, false, false, false)));
1137
1138 INSTANTIATE_TEST_CASE_P(
1139     ForceBitrate,
1140     VideoEncodeAcceleratorTest,
1141     ::testing::Values(MakeTuple(1, false, 0, true, false, false, false)));
1142
1143 INSTANTIATE_TEST_CASE_P(
1144     MidStreamParamSwitchBitrate,
1145     VideoEncodeAcceleratorTest,
1146     ::testing::Values(MakeTuple(1, false, 0, true, false, true, false)));
1147
1148 INSTANTIATE_TEST_CASE_P(
1149     MidStreamParamSwitchFPS,
1150     VideoEncodeAcceleratorTest,
1151     ::testing::Values(MakeTuple(1, false, 0, true, false, false, true)));
1152
1153 INSTANTIATE_TEST_CASE_P(
1154     MultipleEncoders,
1155     VideoEncodeAcceleratorTest,
1156     ::testing::Values(MakeTuple(3, false, 0, false, false, false, false),
1157                       MakeTuple(3, false, 0, true, false, false, true),
1158                       MakeTuple(3, false, 0, true, false, true, false)));
1159
1160 // TODO(posciak): more tests:
1161 // - async FeedEncoderWithOutput
1162 // - out-of-order return of outputs to encoder
1163 // - multiple encoders + decoders
1164 // - mid-stream encoder_->Destroy()
1165
1166 }  // namespace
1167 }  // namespace content
1168
1169 int main(int argc, char** argv) {
1170   testing::InitGoogleTest(&argc, argv);  // Removes gtest-specific args.
1171   base::CommandLine::Init(argc, argv);
1172
1173   base::ShadowingAtExitManager at_exit_manager;
1174   scoped_ptr<base::FilePath::StringType> test_stream_data(
1175       new base::FilePath::StringType(
1176           media::GetTestDataFilePath(content::g_default_in_filename).value() +
1177           content::g_default_in_parameters));
1178
1179   // Needed to enable DVLOG through --vmodule.
1180   logging::LoggingSettings settings;
1181   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
1182   CHECK(logging::InitLogging(settings));
1183
1184   const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
1185   DCHECK(cmd_line);
1186
1187   base::CommandLine::SwitchMap switches = cmd_line->GetSwitches();
1188   for (base::CommandLine::SwitchMap::const_iterator it = switches.begin();
1189        it != switches.end();
1190        ++it) {
1191     if (it->first == "test_stream_data") {
1192       test_stream_data->assign(it->second.c_str());
1193       continue;
1194     }
1195     if (it->first == "v" || it->first == "vmodule")
1196       continue;
1197     LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second;
1198   }
1199
1200   content::g_env =
1201       reinterpret_cast<content::VideoEncodeAcceleratorTestEnvironment*>(
1202           testing::AddGlobalTestEnvironment(
1203               new content::VideoEncodeAcceleratorTestEnvironment(
1204                   test_stream_data.Pass())));
1205
1206   return RUN_ALL_TESTS();
1207 }