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.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
36using media::VideoEncodeAccelerator;
37
38namespace content {
39namespace {
40
41const media::VideoFrame::Format kInputFormat = media::VideoFrame::I420;
42
43// Arbitrarily chosen to add some depth to the pipeline.
44const unsigned int kNumOutputBuffers = 4;
45const unsigned int kNumExtraInputFrames = 4;
46// Maximum delay between requesting a keyframe and receiving one, in frames.
47// Arbitrarily chosen as a reasonable requirement.
48const unsigned int kMaxKeyframeDelay = 4;
49// Value to use as max frame number for keyframe detection.
50const unsigned int kMaxFrameNum =
51    std::numeric_limits<unsigned int>::max() - kMaxKeyframeDelay;
52// Default initial bitrate.
53const uint32 kDefaultBitrate = 2000000;
54// Default ratio of requested_subsequent_bitrate to initial_bitrate
55// (see test parameters below) if one is not provided.
56const double kDefaultSubsequentBitrateRatio = 2.0;
57// Default initial framerate.
58const uint32 kDefaultFramerate = 30;
59// Default ratio of requested_subsequent_framerate to initial_framerate
60// (see test parameters below) if one is not provided.
61const double kDefaultSubsequentFramerateRatio = 0.1;
62// Tolerance factor for how encoded bitrate can differ from requested bitrate.
63const double kBitrateTolerance = 0.1;
64// Minimum required FPS throughput for the basic performance test.
65const 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.
71const 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.
93const char* g_default_in_filename = "bear_320x192_40frames.yuv";
94const char* g_default_in_parameters = ":320:192:1:out.h264:200000";
95// Environment to store test stream data for all test cases.
96class VideoEncodeAcceleratorTestEnvironment;
97VideoEncodeAcceleratorTestEnvironment* g_env;
98
99struct 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
138inline 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|.
143static 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.
167static 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|.
262static 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
312enum 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.
326class 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
349class 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
368void 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        // fallthrough
389      case media::H264NALU::kNonIDRSlice: {
390        ASSERT_TRUE(seen_idr_);
391
392        media::H264SliceHeader shdr;
393        ASSERT_EQ(media::H264Parser::kOk,
394                  h264_parser_.ParseSliceHeader(nalu, &shdr));
395        keyframe = shdr.IsISlice() || shdr.IsSISlice();
396
397        if (!frame_cb_.Run(keyframe))
398          return;
399        break;
400      }
401
402      case media::H264NALU::kSPS: {
403        int sps_id;
404        ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParseSPS(&sps_id));
405        seen_sps_ = true;
406        break;
407      }
408
409      case media::H264NALU::kPPS: {
410        ASSERT_TRUE(seen_sps_);
411        int pps_id;
412        ASSERT_EQ(media::H264Parser::kOk, h264_parser_.ParsePPS(&pps_id));
413        seen_pps_ = true;
414        break;
415      }
416
417      default:
418        break;
419    }
420  }
421}
422
423class VP8Validator : public StreamValidator {
424 public:
425  explicit VP8Validator(const FrameFoundCallback& frame_cb)
426      : StreamValidator(frame_cb),
427        seen_keyframe_(false) {}
428
429  virtual void ProcessStreamBuffer(const uint8* stream, size_t size) OVERRIDE;
430
431 private:
432  // Have we already got a keyframe in the stream?
433  bool seen_keyframe_;
434};
435
436void VP8Validator::ProcessStreamBuffer(const uint8* stream, size_t size) {
437  bool keyframe = !(stream[0] & 0x01);
438  if (keyframe)
439    seen_keyframe_ = true;
440
441  EXPECT_TRUE(seen_keyframe_);
442
443  frame_cb_.Run(keyframe);
444  // TODO(posciak): We could be getting more frames in the buffer, but there is
445  // no simple way to detect this. We'd need to parse the frames and go through
446  // partition numbers/sizes. For now assume one frame per buffer.
447}
448
449// static
450scoped_ptr<StreamValidator> StreamValidator::Create(
451    media::VideoCodecProfile profile,
452    const FrameFoundCallback& frame_cb) {
453  scoped_ptr<StreamValidator> validator;
454
455  if (profile >= media::H264PROFILE_MIN &&
456      profile <= media::H264PROFILE_MAX) {
457    validator.reset(new H264Validator(frame_cb));
458  } else if (profile >= media::VP8PROFILE_MIN &&
459             profile <= media::VP8PROFILE_MAX) {
460    validator.reset(new VP8Validator(frame_cb));
461  } else {
462    LOG(FATAL) << "Unsupported profile: " << profile;
463  }
464
465  return validator.Pass();
466}
467
468class VEAClient : public VideoEncodeAccelerator::Client {
469 public:
470  VEAClient(TestStream* test_stream,
471            ClientStateNotification<ClientState>* note,
472            bool save_to_file,
473            unsigned int keyframe_period,
474            bool force_bitrate,
475            bool test_perf,
476            bool mid_stream_bitrate_switch,
477            bool mid_stream_framerate_switch);
478  virtual ~VEAClient();
479  void CreateEncoder();
480  void DestroyEncoder();
481
482  // Return the number of encoded frames per second.
483  double frames_per_second();
484
485  // VideoDecodeAccelerator::Client implementation.
486  virtual void RequireBitstreamBuffers(unsigned int input_count,
487                                       const gfx::Size& input_coded_size,
488                                       size_t output_buffer_size) OVERRIDE;
489  virtual void BitstreamBufferReady(int32 bitstream_buffer_id,
490                                    size_t payload_size,
491                                    bool key_frame) OVERRIDE;
492  virtual void NotifyError(VideoEncodeAccelerator::Error error) OVERRIDE;
493
494 private:
495  bool has_encoder() { return encoder_.get(); }
496
497  void SetState(ClientState new_state);
498
499  // Set current stream parameters to given |bitrate| at |framerate|.
500  void SetStreamParameters(unsigned int bitrate, unsigned int framerate);
501
502  // Called when encoder is done with a VideoFrame.
503  void InputNoLongerNeededCallback(int32 input_id);
504
505  // Ensure encoder has at least as many inputs as it asked for
506  // via RequireBitstreamBuffers().
507  void FeedEncoderWithInputs();
508
509  // Provide the encoder with a new output buffer.
510  void FeedEncoderWithOutput(base::SharedMemory* shm);
511
512  // Called on finding a complete frame (with |keyframe| set to true for
513  // keyframes) in the stream, to perform codec-independent, per-frame checks
514  // and accounting. Returns false once we have collected all frames we needed.
515  bool HandleEncodedFrame(bool keyframe);
516
517  // Verify that stream bitrate has been close to current_requested_bitrate_,
518  // assuming current_framerate_ since the last time VerifyStreamProperties()
519  // was called. Fail the test if |force_bitrate_| is true and the bitrate
520  // is not within kBitrateTolerance.
521  void VerifyStreamProperties();
522
523  // Test codec performance, failing the test if we are currently running
524  // the performance test.
525  void VerifyPerf();
526
527  // Prepare and return a frame wrapping the data at |position| bytes in
528  // the input stream, ready to be sent to encoder.
529  scoped_refptr<media::VideoFrame> PrepareInputFrame(off_t position);
530
531  // Update the parameters according to |mid_stream_bitrate_switch| and
532  // |mid_stream_framerate_switch|.
533  void UpdateTestStreamData(bool mid_stream_bitrate_switch,
534                            bool mid_stream_framerate_switch);
535
536  ClientState state_;
537  scoped_ptr<VideoEncodeAccelerator> encoder_;
538
539  TestStream* test_stream_;
540  // Used to notify another thread about the state. VEAClient does not own this.
541  ClientStateNotification<ClientState>* note_;
542
543  // Ids assigned to VideoFrames (start at 1 for easy comparison with
544  // num_encoded_frames_).
545  std::set<int32> inputs_at_client_;
546  int32 next_input_id_;
547
548  // Ids for output BitstreamBuffers.
549  typedef std::map<int32, base::SharedMemory*> IdToSHM;
550  ScopedVector<base::SharedMemory> output_shms_;
551  IdToSHM output_buffers_at_client_;
552  int32 next_output_buffer_id_;
553
554  // Current offset into input stream.
555  off_t pos_in_input_stream_;
556  gfx::Size input_coded_size_;
557  // Requested by encoder.
558  unsigned int num_required_input_buffers_;
559  size_t output_buffer_size_;
560
561  // Number of frames to encode. This may differ from the number of frames in
562  // stream if we need more frames for bitrate tests.
563  unsigned int num_frames_to_encode_;
564
565  // Number of encoded frames we've got from the encoder thus far.
566  unsigned int num_encoded_frames_;
567
568  // Frames since last bitrate verification.
569  unsigned int num_frames_since_last_check_;
570
571  // True if received a keyframe while processing current bitstream buffer.
572  bool seen_keyframe_in_this_buffer_;
573
574  // True if we are to save the encoded stream to a file.
575  bool save_to_file_;
576
577  // Request a keyframe every keyframe_period_ frames.
578  const unsigned int keyframe_period_;
579
580  // Frame number for which we requested a keyframe.
581  unsigned int keyframe_requested_at_;
582
583  // True if we are asking encoder for a particular bitrate.
584  bool force_bitrate_;
585
586  // Current requested bitrate.
587  unsigned int current_requested_bitrate_;
588
589  // Current expected framerate.
590  unsigned int current_framerate_;
591
592  // Byte size of the encoded stream (for bitrate calculation) since last
593  // time we checked bitrate.
594  size_t encoded_stream_size_since_last_check_;
595
596  // If true, verify performance at the end of the test.
597  bool test_perf_;
598
599  scoped_ptr<StreamValidator> validator_;
600
601  // The time when the encoding started.
602  base::TimeTicks encode_start_time_;
603
604  // The time when the last encoded frame is ready.
605  base::TimeTicks last_frame_ready_time_;
606
607  // All methods of this class should be run on the same thread.
608  base::ThreadChecker thread_checker_;
609
610  // Requested bitrate in bits per second.
611  unsigned int requested_bitrate_;
612
613  // Requested initial framerate.
614  unsigned int requested_framerate_;
615
616  // Bitrate to switch to in the middle of the stream.
617  unsigned int requested_subsequent_bitrate_;
618
619  // Framerate to switch to in the middle of the stream.
620  unsigned int requested_subsequent_framerate_;
621};
622
623VEAClient::VEAClient(TestStream* test_stream,
624                     ClientStateNotification<ClientState>* note,
625                     bool save_to_file,
626                     unsigned int keyframe_period,
627                     bool force_bitrate,
628                     bool test_perf,
629                     bool mid_stream_bitrate_switch,
630                     bool mid_stream_framerate_switch)
631    : state_(CS_CREATED),
632      test_stream_(test_stream),
633      note_(note),
634      next_input_id_(1),
635      next_output_buffer_id_(0),
636      pos_in_input_stream_(0),
637      num_required_input_buffers_(0),
638      output_buffer_size_(0),
639      num_frames_to_encode_(0),
640      num_encoded_frames_(0),
641      num_frames_since_last_check_(0),
642      seen_keyframe_in_this_buffer_(false),
643      save_to_file_(save_to_file),
644      keyframe_period_(keyframe_period),
645      keyframe_requested_at_(kMaxFrameNum),
646      force_bitrate_(force_bitrate),
647      current_requested_bitrate_(0),
648      current_framerate_(0),
649      encoded_stream_size_since_last_check_(0),
650      test_perf_(test_perf),
651      requested_bitrate_(0),
652      requested_framerate_(0),
653      requested_subsequent_bitrate_(0),
654      requested_subsequent_framerate_(0) {
655  if (keyframe_period_)
656    CHECK_LT(kMaxKeyframeDelay, keyframe_period_);
657
658  validator_ = StreamValidator::Create(
659      test_stream_->requested_profile,
660      base::Bind(&VEAClient::HandleEncodedFrame, base::Unretained(this)));
661
662  CHECK(validator_.get());
663
664  if (save_to_file_) {
665    CHECK(!test_stream_->out_filename.empty());
666    base::FilePath out_filename(test_stream_->out_filename);
667    // This creates or truncates out_filename.
668    // Without it, AppendToFile() will not work.
669    EXPECT_EQ(0, base::WriteFile(out_filename, NULL, 0));
670  }
671
672  // Initialize the parameters of the test streams.
673  UpdateTestStreamData(mid_stream_bitrate_switch, mid_stream_framerate_switch);
674
675  thread_checker_.DetachFromThread();
676}
677
678VEAClient::~VEAClient() { CHECK(!has_encoder()); }
679
680void VEAClient::CreateEncoder() {
681  DCHECK(thread_checker_.CalledOnValidThread());
682  CHECK(!has_encoder());
683
684#if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
685  scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder);
686  encoder_.reset(new V4L2VideoEncodeAccelerator(device.Pass()));
687#elif defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) && defined(USE_X11)
688  encoder_.reset(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay()));
689#endif
690
691  SetState(CS_ENCODER_SET);
692
693  DVLOG(1) << "Profile: " << test_stream_->requested_profile
694           << ", initial bitrate: " << requested_bitrate_;
695  if (!encoder_->Initialize(kInputFormat,
696                            test_stream_->visible_size,
697                            test_stream_->requested_profile,
698                            requested_bitrate_,
699                            this)) {
700    DLOG(ERROR) << "VideoEncodeAccelerator::Initialize() failed";
701    SetState(CS_ERROR);
702    return;
703  }
704
705  SetStreamParameters(requested_bitrate_, requested_framerate_);
706  SetState(CS_INITIALIZED);
707}
708
709void VEAClient::DestroyEncoder() {
710  DCHECK(thread_checker_.CalledOnValidThread());
711  if (!has_encoder())
712    return;
713  encoder_.reset();
714}
715
716void VEAClient::UpdateTestStreamData(bool mid_stream_bitrate_switch,
717                                     bool mid_stream_framerate_switch) {
718  // Use defaults for bitrate/framerate if they are not provided.
719  if (test_stream_->requested_bitrate == 0)
720    requested_bitrate_ = kDefaultBitrate;
721  else
722    requested_bitrate_ = test_stream_->requested_bitrate;
723
724  if (test_stream_->requested_framerate == 0)
725    requested_framerate_ = kDefaultFramerate;
726  else
727    requested_framerate_ = test_stream_->requested_framerate;
728
729  // If bitrate/framerate switch is requested, use the subsequent values if
730  // provided, or, if not, calculate them from their initial values using
731  // the default ratios.
732  // Otherwise, if a switch is not requested, keep the initial values.
733  if (mid_stream_bitrate_switch) {
734    if (test_stream_->requested_subsequent_bitrate == 0)
735      requested_subsequent_bitrate_ =
736          requested_bitrate_ * kDefaultSubsequentBitrateRatio;
737    else
738      requested_subsequent_bitrate_ =
739          test_stream_->requested_subsequent_bitrate;
740  } else {
741    requested_subsequent_bitrate_ = requested_bitrate_;
742  }
743  if (requested_subsequent_bitrate_ == 0)
744    requested_subsequent_bitrate_ = 1;
745
746  if (mid_stream_framerate_switch) {
747    if (test_stream_->requested_subsequent_framerate == 0)
748      requested_subsequent_framerate_ =
749          requested_framerate_ * kDefaultSubsequentFramerateRatio;
750    else
751      requested_subsequent_framerate_ =
752          test_stream_->requested_subsequent_framerate;
753  } else {
754    requested_subsequent_framerate_ = requested_framerate_;
755  }
756  if (requested_subsequent_framerate_ == 0)
757    requested_subsequent_framerate_ = 1;
758}
759
760double VEAClient::frames_per_second() {
761  base::TimeDelta duration = last_frame_ready_time_ - encode_start_time_;
762  return num_encoded_frames_ / duration.InSecondsF();
763}
764
765void VEAClient::RequireBitstreamBuffers(unsigned int input_count,
766                                        const gfx::Size& input_coded_size,
767                                        size_t output_size) {
768  DCHECK(thread_checker_.CalledOnValidThread());
769  ASSERT_EQ(state_, CS_INITIALIZED);
770  SetState(CS_ENCODING);
771
772  CreateAlignedInputStreamFile(input_coded_size, test_stream_);
773
774  // We may need to loop over the stream more than once if more frames than
775  // provided is required for bitrate tests.
776  if (force_bitrate_ && test_stream_->num_frames < kMinFramesForBitrateTests) {
777    DVLOG(1) << "Stream too short for bitrate test ("
778             << test_stream_->num_frames << " frames), will loop it to reach "
779             << kMinFramesForBitrateTests << " frames";
780    num_frames_to_encode_ = kMinFramesForBitrateTests;
781  } else {
782    num_frames_to_encode_ = test_stream_->num_frames;
783  }
784
785  input_coded_size_ = input_coded_size;
786  num_required_input_buffers_ = input_count;
787  ASSERT_GT(num_required_input_buffers_, 0UL);
788
789  output_buffer_size_ = output_size;
790  ASSERT_GT(output_buffer_size_, 0UL);
791
792  for (unsigned int i = 0; i < kNumOutputBuffers; ++i) {
793    base::SharedMemory* shm = new base::SharedMemory();
794    CHECK(shm->CreateAndMapAnonymous(output_buffer_size_));
795    output_shms_.push_back(shm);
796    FeedEncoderWithOutput(shm);
797  }
798
799  encode_start_time_ = base::TimeTicks::Now();
800  FeedEncoderWithInputs();
801}
802
803void VEAClient::BitstreamBufferReady(int32 bitstream_buffer_id,
804                                     size_t payload_size,
805                                     bool key_frame) {
806  DCHECK(thread_checker_.CalledOnValidThread());
807  ASSERT_LE(payload_size, output_buffer_size_);
808
809  IdToSHM::iterator it = output_buffers_at_client_.find(bitstream_buffer_id);
810  ASSERT_NE(it, output_buffers_at_client_.end());
811  base::SharedMemory* shm = it->second;
812  output_buffers_at_client_.erase(it);
813
814  if (state_ == CS_FINISHED)
815    return;
816
817  encoded_stream_size_since_last_check_ += payload_size;
818
819  const uint8* stream_ptr = static_cast<const uint8*>(shm->memory());
820  if (payload_size > 0)
821    validator_->ProcessStreamBuffer(stream_ptr, payload_size);
822
823  EXPECT_EQ(key_frame, seen_keyframe_in_this_buffer_);
824  seen_keyframe_in_this_buffer_ = false;
825
826  if (save_to_file_) {
827    int size = base::checked_cast<int>(payload_size);
828    EXPECT_EQ(base::AppendToFile(
829                  base::FilePath::FromUTF8Unsafe(test_stream_->out_filename),
830                  static_cast<char*>(shm->memory()),
831                  size),
832              size);
833  }
834
835  FeedEncoderWithOutput(shm);
836}
837
838void VEAClient::NotifyError(VideoEncodeAccelerator::Error error) {
839  DCHECK(thread_checker_.CalledOnValidThread());
840  SetState(CS_ERROR);
841}
842
843void VEAClient::SetState(ClientState new_state) {
844  DVLOG(4) << "Changing state " << state_ << "->" << new_state;
845  note_->Notify(new_state);
846  state_ = new_state;
847}
848
849void VEAClient::SetStreamParameters(unsigned int bitrate,
850                                    unsigned int framerate) {
851  current_requested_bitrate_ = bitrate;
852  current_framerate_ = framerate;
853  CHECK_GT(current_requested_bitrate_, 0UL);
854  CHECK_GT(current_framerate_, 0UL);
855  encoder_->RequestEncodingParametersChange(current_requested_bitrate_,
856                                            current_framerate_);
857  DVLOG(1) << "Switched parameters to " << current_requested_bitrate_
858           << " bps @ " << current_framerate_ << " FPS";
859}
860
861void VEAClient::InputNoLongerNeededCallback(int32 input_id) {
862  std::set<int32>::iterator it = inputs_at_client_.find(input_id);
863  ASSERT_NE(it, inputs_at_client_.end());
864  inputs_at_client_.erase(it);
865  FeedEncoderWithInputs();
866}
867
868scoped_refptr<media::VideoFrame> VEAClient::PrepareInputFrame(off_t position) {
869  CHECK_LE(position + test_stream_->aligned_buffer_size,
870           test_stream_->mapped_aligned_in_file.length());
871
872  uint8* frame_data_y = const_cast<uint8*>(
873      test_stream_->mapped_aligned_in_file.data() + position);
874  uint8* frame_data_u = frame_data_y + test_stream_->aligned_plane_size[0];
875  uint8* frame_data_v = frame_data_u + test_stream_->aligned_plane_size[1];
876
877  CHECK_GT(current_framerate_, 0U);
878  scoped_refptr<media::VideoFrame> frame =
879      media::VideoFrame::WrapExternalYuvData(
880          kInputFormat,
881          input_coded_size_,
882          gfx::Rect(test_stream_->visible_size),
883          test_stream_->visible_size,
884          input_coded_size_.width(),
885          input_coded_size_.width() / 2,
886          input_coded_size_.width() / 2,
887          frame_data_y,
888          frame_data_u,
889          frame_data_v,
890          base::TimeDelta().FromMilliseconds(
891              next_input_id_ * base::Time::kMillisecondsPerSecond /
892              current_framerate_),
893          media::BindToCurrentLoop(
894              base::Bind(&VEAClient::InputNoLongerNeededCallback,
895                         base::Unretained(this),
896                         next_input_id_)));
897
898  CHECK(inputs_at_client_.insert(next_input_id_).second);
899  ++next_input_id_;
900
901  return frame;
902}
903
904void VEAClient::FeedEncoderWithInputs() {
905  if (!has_encoder())
906    return;
907
908  if (state_ != CS_ENCODING)
909    return;
910
911  while (inputs_at_client_.size() <
912         num_required_input_buffers_ + kNumExtraInputFrames) {
913    size_t bytes_left =
914        test_stream_->mapped_aligned_in_file.length() - pos_in_input_stream_;
915    if (bytes_left < test_stream_->aligned_buffer_size) {
916      DCHECK_EQ(bytes_left, 0UL);
917      // Rewind if at the end of stream and we are still encoding.
918      // This is to flush the encoder with additional frames from the beginning
919      // of the stream, or if the stream is shorter that the number of frames
920      // we require for bitrate tests.
921      pos_in_input_stream_ = 0;
922      continue;
923    }
924
925    bool force_keyframe = false;
926    if (keyframe_period_ && next_input_id_ % keyframe_period_ == 0) {
927      keyframe_requested_at_ = next_input_id_;
928      force_keyframe = true;
929    }
930
931    scoped_refptr<media::VideoFrame> video_frame =
932        PrepareInputFrame(pos_in_input_stream_);
933    pos_in_input_stream_ += test_stream_->aligned_buffer_size;
934
935    encoder_->Encode(video_frame, force_keyframe);
936  }
937}
938
939void VEAClient::FeedEncoderWithOutput(base::SharedMemory* shm) {
940  if (!has_encoder())
941    return;
942
943  if (state_ != CS_ENCODING)
944    return;
945
946  base::SharedMemoryHandle dup_handle;
947  CHECK(shm->ShareToProcess(base::Process::Current().handle(), &dup_handle));
948
949  media::BitstreamBuffer bitstream_buffer(
950      next_output_buffer_id_++, dup_handle, output_buffer_size_);
951  CHECK(output_buffers_at_client_.insert(std::make_pair(bitstream_buffer.id(),
952                                                        shm)).second);
953  encoder_->UseOutputBitstreamBuffer(bitstream_buffer);
954}
955
956bool VEAClient::HandleEncodedFrame(bool keyframe) {
957  // This would be a bug in the test, which should not ignore false
958  // return value from this method.
959  CHECK_LE(num_encoded_frames_, num_frames_to_encode_);
960
961  ++num_encoded_frames_;
962  ++num_frames_since_last_check_;
963
964  last_frame_ready_time_ = base::TimeTicks::Now();
965  if (keyframe) {
966    // Got keyframe, reset keyframe detection regardless of whether we
967    // got a frame in time or not.
968    keyframe_requested_at_ = kMaxFrameNum;
969    seen_keyframe_in_this_buffer_ = true;
970  }
971
972  // Because the keyframe behavior requirements are loose, we give
973  // the encoder more freedom here. It could either deliver a keyframe
974  // immediately after we requested it, which could be for a frame number
975  // before the one we requested it for (if the keyframe request
976  // is asynchronous, i.e. not bound to any concrete frame, and because
977  // the pipeline can be deeper than one frame), at that frame, or after.
978  // So the only constraints we put here is that we get a keyframe not
979  // earlier than we requested one (in time), and not later than
980  // kMaxKeyframeDelay frames after the frame, for which we requested
981  // it, comes back encoded.
982  EXPECT_LE(num_encoded_frames_, keyframe_requested_at_ + kMaxKeyframeDelay);
983
984  if (num_encoded_frames_ == num_frames_to_encode_ / 2) {
985    VerifyStreamProperties();
986    if (requested_subsequent_bitrate_ != current_requested_bitrate_ ||
987        requested_subsequent_framerate_ != current_framerate_) {
988      SetStreamParameters(requested_subsequent_bitrate_,
989                          requested_subsequent_framerate_);
990    }
991  } else if (num_encoded_frames_ == num_frames_to_encode_) {
992    VerifyPerf();
993    VerifyStreamProperties();
994    SetState(CS_FINISHED);
995    return false;
996  }
997
998  return true;
999}
1000
1001void VEAClient::VerifyPerf() {
1002  double measured_fps = frames_per_second();
1003  LOG(INFO) << "Measured encoder FPS: " << measured_fps;
1004  if (test_perf_)
1005    EXPECT_GE(measured_fps, kMinPerfFPS);
1006}
1007
1008void VEAClient::VerifyStreamProperties() {
1009  CHECK_GT(num_frames_since_last_check_, 0UL);
1010  CHECK_GT(encoded_stream_size_since_last_check_, 0UL);
1011  unsigned int bitrate = encoded_stream_size_since_last_check_ * 8 *
1012                         current_framerate_ / num_frames_since_last_check_;
1013  DVLOG(1) << "Current chunk's bitrate: " << bitrate
1014           << " (expected: " << current_requested_bitrate_
1015           << " @ " << current_framerate_ << " FPS,"
1016           << " num frames in chunk: " << num_frames_since_last_check_;
1017
1018  num_frames_since_last_check_ = 0;
1019  encoded_stream_size_since_last_check_ = 0;
1020
1021  if (force_bitrate_) {
1022    EXPECT_NEAR(bitrate,
1023                current_requested_bitrate_,
1024                kBitrateTolerance * current_requested_bitrate_);
1025  }
1026}
1027
1028// Setup test stream data and delete temporary aligned files at the beginning
1029// and end of unittest. We only need to setup once for all test cases.
1030class VideoEncodeAcceleratorTestEnvironment : public ::testing::Environment {
1031 public:
1032  VideoEncodeAcceleratorTestEnvironment(
1033      scoped_ptr<base::FilePath::StringType> data) {
1034    test_stream_data_ = data.Pass();
1035  }
1036
1037  virtual void SetUp() {
1038    ParseAndReadTestStreamData(*test_stream_data_, &test_streams_);
1039  }
1040
1041  virtual void TearDown() {
1042    for (size_t i = 0; i < test_streams_.size(); i++) {
1043      base::DeleteFile(test_streams_[i]->aligned_in_file, false);
1044    }
1045  }
1046
1047  ScopedVector<TestStream> test_streams_;
1048
1049 private:
1050  scoped_ptr<base::FilePath::StringType> test_stream_data_;
1051};
1052
1053// Test parameters:
1054// - Number of concurrent encoders.
1055// - If true, save output to file (provided an output filename was supplied).
1056// - Force a keyframe every n frames.
1057// - Force bitrate; the actual required value is provided as a property
1058//   of the input stream, because it depends on stream type/resolution/etc.
1059// - If true, measure performance.
1060// - If true, switch bitrate mid-stream.
1061// - If true, switch framerate mid-stream.
1062class VideoEncodeAcceleratorTest
1063    : public ::testing::TestWithParam<
1064          Tuple7<int, bool, int, bool, bool, bool, bool> > {};
1065
1066TEST_P(VideoEncodeAcceleratorTest, TestSimpleEncode) {
1067  const size_t num_concurrent_encoders = GetParam().a;
1068  const bool save_to_file = GetParam().b;
1069  const unsigned int keyframe_period = GetParam().c;
1070  const bool force_bitrate = GetParam().d;
1071  const bool test_perf = GetParam().e;
1072  const bool mid_stream_bitrate_switch = GetParam().f;
1073  const bool mid_stream_framerate_switch = GetParam().g;
1074
1075  ScopedVector<ClientStateNotification<ClientState> > notes;
1076  ScopedVector<VEAClient> clients;
1077  base::Thread encoder_thread("EncoderThread");
1078  ASSERT_TRUE(encoder_thread.Start());
1079
1080  // Create all encoders.
1081  for (size_t i = 0; i < num_concurrent_encoders; i++) {
1082    size_t test_stream_index = i % g_env->test_streams_.size();
1083    // Disregard save_to_file if we didn't get an output filename.
1084    bool encoder_save_to_file =
1085        (save_to_file &&
1086         !g_env->test_streams_[test_stream_index]->out_filename.empty());
1087
1088    notes.push_back(new ClientStateNotification<ClientState>());
1089    clients.push_back(new VEAClient(g_env->test_streams_[test_stream_index],
1090                                    notes.back(),
1091                                    encoder_save_to_file,
1092                                    keyframe_period,
1093                                    force_bitrate,
1094                                    test_perf,
1095                                    mid_stream_bitrate_switch,
1096                                    mid_stream_framerate_switch));
1097
1098    encoder_thread.message_loop()->PostTask(
1099        FROM_HERE,
1100        base::Bind(&VEAClient::CreateEncoder,
1101                   base::Unretained(clients.back())));
1102  }
1103
1104  // All encoders must pass through states in this order.
1105  enum ClientState state_transitions[] = {CS_ENCODER_SET, CS_INITIALIZED,
1106                                          CS_ENCODING, CS_FINISHED};
1107
1108  // Wait for all encoders to go through all states and finish.
1109  // Do this by waiting for all encoders to advance to state n before checking
1110  // state n+1, to verify that they are able to operate concurrently.
1111  // It also simulates the real-world usage better, as the main thread, on which
1112  // encoders are created/destroyed, is a single GPU Process ChildThread.
1113  // Moreover, we can't have proper multithreading on X11, so this could cause
1114  // hard to debug issues there, if there were multiple "ChildThreads".
1115  for (size_t state_no = 0; state_no < arraysize(state_transitions); ++state_no)
1116    for (size_t i = 0; i < num_concurrent_encoders; i++)
1117      ASSERT_EQ(notes[i]->Wait(), state_transitions[state_no]);
1118
1119  for (size_t i = 0; i < num_concurrent_encoders; ++i) {
1120    encoder_thread.message_loop()->PostTask(
1121        FROM_HERE,
1122        base::Bind(&VEAClient::DestroyEncoder, base::Unretained(clients[i])));
1123  }
1124
1125  // This ensures all tasks have finished.
1126  encoder_thread.Stop();
1127}
1128
1129INSTANTIATE_TEST_CASE_P(
1130    SimpleEncode,
1131    VideoEncodeAcceleratorTest,
1132    ::testing::Values(MakeTuple(1, true, 0, false, false, false, false)));
1133
1134INSTANTIATE_TEST_CASE_P(
1135    EncoderPerf,
1136    VideoEncodeAcceleratorTest,
1137    ::testing::Values(MakeTuple(1, false, 0, false, true, false, false)));
1138
1139INSTANTIATE_TEST_CASE_P(
1140    ForceKeyframes,
1141    VideoEncodeAcceleratorTest,
1142    ::testing::Values(MakeTuple(1, false, 10, false, false, false, false)));
1143
1144INSTANTIATE_TEST_CASE_P(
1145    ForceBitrate,
1146    VideoEncodeAcceleratorTest,
1147    ::testing::Values(MakeTuple(1, false, 0, true, false, false, false)));
1148
1149INSTANTIATE_TEST_CASE_P(
1150    MidStreamParamSwitchBitrate,
1151    VideoEncodeAcceleratorTest,
1152    ::testing::Values(MakeTuple(1, false, 0, true, false, true, false)));
1153
1154INSTANTIATE_TEST_CASE_P(
1155    MidStreamParamSwitchFPS,
1156    VideoEncodeAcceleratorTest,
1157    ::testing::Values(MakeTuple(1, false, 0, true, false, false, true)));
1158
1159INSTANTIATE_TEST_CASE_P(
1160    MidStreamParamSwitchBitrateAndFPS,
1161    VideoEncodeAcceleratorTest,
1162    ::testing::Values(MakeTuple(1, false, 0, true, false, true, true)));
1163
1164INSTANTIATE_TEST_CASE_P(
1165    MultipleEncoders,
1166    VideoEncodeAcceleratorTest,
1167    ::testing::Values(MakeTuple(3, false, 0, false, false, false, false),
1168                      MakeTuple(3, false, 0, true, false, true, true)));
1169
1170// TODO(posciak): more tests:
1171// - async FeedEncoderWithOutput
1172// - out-of-order return of outputs to encoder
1173// - multiple encoders + decoders
1174// - mid-stream encoder_->Destroy()
1175
1176}  // namespace
1177}  // namespace content
1178
1179int main(int argc, char** argv) {
1180  testing::InitGoogleTest(&argc, argv);  // Removes gtest-specific args.
1181  base::CommandLine::Init(argc, argv);
1182
1183  base::ShadowingAtExitManager at_exit_manager;
1184  scoped_ptr<base::FilePath::StringType> test_stream_data(
1185      new base::FilePath::StringType(
1186          media::GetTestDataFilePath(content::g_default_in_filename).value() +
1187          content::g_default_in_parameters));
1188
1189  // Needed to enable DVLOG through --vmodule.
1190  logging::LoggingSettings settings;
1191  settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
1192  CHECK(logging::InitLogging(settings));
1193
1194  const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
1195  DCHECK(cmd_line);
1196
1197  base::CommandLine::SwitchMap switches = cmd_line->GetSwitches();
1198  for (base::CommandLine::SwitchMap::const_iterator it = switches.begin();
1199       it != switches.end();
1200       ++it) {
1201    if (it->first == "test_stream_data") {
1202      test_stream_data->assign(it->second.c_str());
1203      continue;
1204    }
1205    if (it->first == "v" || it->first == "vmodule")
1206      continue;
1207    LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second;
1208  }
1209
1210  content::g_env =
1211      reinterpret_cast<content::VideoEncodeAcceleratorTestEnvironment*>(
1212          testing::AddGlobalTestEnvironment(
1213              new content::VideoEncodeAcceleratorTestEnvironment(
1214                  test_stream_data.Pass())));
1215
1216  return RUN_ALL_TESTS();
1217}
1218