c0e3a71d1f87aed079a43941bb72428f3bea6e5f
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / video_decode_accelerator_unittest.cc
1 // Copyright (c) 2012 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 // The bulk of this file is support code; sorry about that.  Here's an overview
6 // to hopefully help readers of this code:
7 // - RenderingHelper is charged with interacting with X11/{EGL/GLES2,GLX/GL} or
8 //   Win/EGL.
9 // - ClientState is an enum for the state of the decode client used by the test.
10 // - ClientStateNotification is a barrier abstraction that allows the test code
11 //   to be written sequentially and wait for the decode client to see certain
12 //   state transitions.
13 // - GLRenderingVDAClient is a VideoDecodeAccelerator::Client implementation
14 // - Finally actual TEST cases are at the bottom of this file, using the above
15 //   infrastructure.
16
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <algorithm>
21 #include <deque>
22 #include <map>
23
24 // Include gtest.h out of order because <X11/X.h> #define's Bool & None, which
25 // gtest uses as struct names (inside a namespace).  This means that
26 // #include'ing gtest after anything that pulls in X.h fails to compile.
27 // This is http://code.google.com/p/googletest/issues/detail?id=371
28 #include "testing/gtest/include/gtest/gtest.h"
29
30 #include "base/at_exit.h"
31 #include "base/bind.h"
32 #include "base/command_line.h"
33 #include "base/file_util.h"
34 #include "base/format_macros.h"
35 #include "base/md5.h"
36 #include "base/message_loop/message_loop_proxy.h"
37 #include "base/platform_file.h"
38 #include "base/process/process.h"
39 #include "base/stl_util.h"
40 #include "base/strings/string_number_conversions.h"
41 #include "base/strings/string_split.h"
42 #include "base/strings/stringize_macros.h"
43 #include "base/strings/stringprintf.h"
44 #include "base/strings/utf_string_conversions.h"
45 #include "base/synchronization/condition_variable.h"
46 #include "base/synchronization/lock.h"
47 #include "base/synchronization/waitable_event.h"
48 #include "base/threading/thread.h"
49 #include "content/common/gpu/media/rendering_helper.h"
50 #include "content/common/gpu/media/video_accelerator_unittest_helpers.h"
51 #include "content/public/common/content_switches.h"
52 #include "ui/gfx/codec/png_codec.h"
53
54 #if defined(OS_WIN)
55 #include "content/common/gpu/media/dxva_video_decode_accelerator.h"
56 #elif defined(OS_CHROMEOS) || defined(OS_LINUX)
57 #if defined(ARCH_CPU_ARMEL)
58 #include "content/common/gpu/media/v4l2_video_decode_accelerator.h"
59 #include "content/common/gpu/media/v4l2_video_device.h"
60 #elif defined(ARCH_CPU_X86_FAMILY)
61 #include "content/common/gpu/media/vaapi_video_decode_accelerator.h"
62 #include "content/common/gpu/media/vaapi_wrapper.h"
63 #if defined(USE_X11)
64 #include "ui/gl/gl_implementation.h"
65 #endif  // USE_X11
66 #endif  // ARCH_CPU_ARMEL
67 #else
68 #error The VideoAccelerator tests are not supported on this platform.
69 #endif  // OS_WIN
70
71 using media::VideoDecodeAccelerator;
72
73 namespace content {
74 namespace {
75
76 // Values optionally filled in from flags; see main() below.
77 // The syntax of multiple test videos is:
78 //  test-video1;test-video2;test-video3
79 // where only the first video is required and other optional videos would be
80 // decoded by concurrent decoders.
81 // The syntax of each test-video is:
82 //  filename:width:height:numframes:numfragments:minFPSwithRender:minFPSnoRender
83 // where only the first field is required.  Value details:
84 // - |filename| must be an h264 Annex B (NAL) stream or an IVF VP8 stream.
85 // - |width| and |height| are in pixels.
86 // - |numframes| is the number of picture frames in the file.
87 // - |numfragments| NALU (h264) or frame (VP8) count in the stream.
88 // - |minFPSwithRender| and |minFPSnoRender| are minimum frames/second speeds
89 //   expected to be achieved with and without rendering to the screen, resp.
90 //   (the latter tests just decode speed).
91 // - |profile| is the media::VideoCodecProfile set during Initialization.
92 // An empty value for a numeric field means "ignore".
93 const base::FilePath::CharType* g_test_video_data =
94     // FILE_PATH_LITERAL("test-25fps.vp8:320:240:250:250:50:175:11");
95     FILE_PATH_LITERAL("test-25fps.h264:320:240:250:258:50:175:1");
96
97 // The file path of the test output log. This is used to communicate the test
98 // results to CrOS autotests. We can enable the log and specify the filename by
99 // the "--output_log" switch.
100 const base::FilePath::CharType* g_output_log = NULL;
101
102 // The value is set by the switch "--rendering_fps".
103 double g_rendering_fps = 0;
104
105 // Disable rendering, the value is set by the switch "--disable_rendering".
106 bool g_disable_rendering = false;
107
108 // Magic constants for differentiating the reasons for NotifyResetDone being
109 // called.
110 enum ResetPoint {
111   START_OF_STREAM_RESET = -3,
112   MID_STREAM_RESET = -2,
113   END_OF_STREAM_RESET = -1
114 };
115
116 const int kMaxResetAfterFrameNum = 100;
117 const int kMaxFramesToDelayReuse = 64;
118 const base::TimeDelta kReuseDelay = base::TimeDelta::FromSeconds(1);
119 // Simulate WebRTC and call VDA::Decode 30 times per second.
120 const int kWebRtcDecodeCallsPerSecond = 30;
121
122 struct TestVideoFile {
123   explicit TestVideoFile(base::FilePath::StringType file_name)
124       : file_name(file_name),
125         width(-1),
126         height(-1),
127         num_frames(-1),
128         num_fragments(-1),
129         min_fps_render(-1),
130         min_fps_no_render(-1),
131         profile(-1),
132         reset_after_frame_num(END_OF_STREAM_RESET) {
133   }
134
135   base::FilePath::StringType file_name;
136   int width;
137   int height;
138   int num_frames;
139   int num_fragments;
140   int min_fps_render;
141   int min_fps_no_render;
142   int profile;
143   int reset_after_frame_num;
144   std::string data_str;
145 };
146
147 // Presumed minimal display size.
148 // We subtract one pixel from the width because some ARM chromebooks do not
149 // support two fullscreen app running at the same time. See crbug.com/270064.
150 const gfx::Size kThumbnailsDisplaySize(1366 - 1, 768);
151 const gfx::Size kThumbnailsPageSize(1600, 1200);
152 const gfx::Size kThumbnailSize(160, 120);
153 const int kMD5StringLength = 32;
154
155 // Read in golden MD5s for the thumbnailed rendering of this video
156 void ReadGoldenThumbnailMD5s(const TestVideoFile* video_file,
157                              std::vector<std::string>* md5_strings) {
158   base::FilePath filepath(video_file->file_name);
159   filepath = filepath.AddExtension(FILE_PATH_LITERAL(".md5"));
160   std::string all_md5s;
161   base::ReadFileToString(filepath, &all_md5s);
162   base::SplitString(all_md5s, '\n', md5_strings);
163   // Check these are legitimate MD5s.
164   for (std::vector<std::string>::iterator md5_string = md5_strings->begin();
165       md5_string != md5_strings->end(); ++md5_string) {
166       // Ignore the empty string added by SplitString
167       if (!md5_string->length())
168         continue;
169       // Ignore comments
170       if (md5_string->at(0) == '#')
171         continue;
172
173       CHECK_EQ(static_cast<int>(md5_string->length()),
174                kMD5StringLength) << *md5_string;
175       bool hex_only = std::count_if(md5_string->begin(),
176                                     md5_string->end(), isxdigit) ==
177                                     kMD5StringLength;
178       CHECK(hex_only) << *md5_string;
179   }
180   CHECK_GE(md5_strings->size(), 1U) << all_md5s;
181 }
182
183 // State of the GLRenderingVDAClient below.  Order matters here as the test
184 // makes assumptions about it.
185 enum ClientState {
186   CS_CREATED = 0,
187   CS_DECODER_SET = 1,
188   CS_INITIALIZED = 2,
189   CS_FLUSHING = 3,
190   CS_FLUSHED = 4,
191   CS_RESETTING = 5,
192   CS_RESET = 6,
193   CS_ERROR = 7,
194   CS_DESTROYED = 8,
195   CS_MAX,  // Must be last entry.
196 };
197
198 // A wrapper client that throttles the PictureReady callbacks to a given rate.
199 // It may drops or queues frame to deliver them on time.
200 class ThrottlingVDAClient : public VideoDecodeAccelerator::Client,
201                             public base::SupportsWeakPtr<ThrottlingVDAClient> {
202  public:
203   // Callback invoked whan the picture is dropped and should be reused for
204   // the decoder again.
205   typedef base::Callback<void(int32 picture_buffer_id)> ReusePictureCB;
206
207   ThrottlingVDAClient(VideoDecodeAccelerator::Client* client,
208                       double fps,
209                       ReusePictureCB reuse_picture_cb);
210   virtual ~ThrottlingVDAClient();
211
212   // VideoDecodeAccelerator::Client implementation
213   virtual void ProvidePictureBuffers(uint32 requested_num_of_buffers,
214                                      const gfx::Size& dimensions,
215                                      uint32 texture_target) OVERRIDE;
216   virtual void DismissPictureBuffer(int32 picture_buffer_id) OVERRIDE;
217   virtual void PictureReady(const media::Picture& picture) OVERRIDE;
218   virtual void NotifyInitializeDone() OVERRIDE;
219   virtual void NotifyEndOfBitstreamBuffer(int32 bitstream_buffer_id) OVERRIDE;
220   virtual void NotifyFlushDone() OVERRIDE;
221   virtual void NotifyResetDone() OVERRIDE;
222   virtual void NotifyError(VideoDecodeAccelerator::Error error) OVERRIDE;
223
224   int num_decoded_frames() { return num_decoded_frames_; }
225
226  private:
227
228   void CallClientPictureReady(int version);
229
230   VideoDecodeAccelerator::Client* client_;
231   ReusePictureCB reuse_picture_cb_;
232   base::TimeTicks next_frame_delivered_time_;
233   base::TimeDelta frame_duration_;
234
235   int num_decoded_frames_;
236   int stream_version_;
237   std::deque<media::Picture> pending_pictures_;
238
239   DISALLOW_IMPLICIT_CONSTRUCTORS(ThrottlingVDAClient);
240 };
241
242 ThrottlingVDAClient::ThrottlingVDAClient(VideoDecodeAccelerator::Client* client,
243                                          double fps,
244                                          ReusePictureCB reuse_picture_cb)
245     : client_(client),
246       reuse_picture_cb_(reuse_picture_cb),
247       num_decoded_frames_(0),
248       stream_version_(0) {
249   CHECK(client_);
250   CHECK_GT(fps, 0);
251   frame_duration_ = base::TimeDelta::FromSeconds(1) / fps;
252 }
253
254 ThrottlingVDAClient::~ThrottlingVDAClient() {}
255
256 void ThrottlingVDAClient::ProvidePictureBuffers(uint32 requested_num_of_buffers,
257                                                 const gfx::Size& dimensions,
258                                                 uint32 texture_target) {
259   client_->ProvidePictureBuffers(
260       requested_num_of_buffers, dimensions, texture_target);
261 }
262
263 void ThrottlingVDAClient::DismissPictureBuffer(int32 picture_buffer_id) {
264   client_->DismissPictureBuffer(picture_buffer_id);
265 }
266
267 void ThrottlingVDAClient::PictureReady(const media::Picture& picture) {
268   ++num_decoded_frames_;
269
270   if (pending_pictures_.empty()) {
271     base::TimeDelta delay =
272         next_frame_delivered_time_.is_null()
273             ? base::TimeDelta()
274             : next_frame_delivered_time_ - base::TimeTicks::Now();
275     base::MessageLoop::current()->PostDelayedTask(
276         FROM_HERE,
277         base::Bind(&ThrottlingVDAClient::CallClientPictureReady,
278                    AsWeakPtr(),
279                    stream_version_),
280         delay);
281   }
282   pending_pictures_.push_back(picture);
283 }
284
285 void ThrottlingVDAClient::CallClientPictureReady(int version) {
286   // Just return if we have reset the decoder
287   if (version != stream_version_)
288     return;
289
290   base::TimeTicks now = base::TimeTicks::Now();
291
292   if (next_frame_delivered_time_.is_null())
293     next_frame_delivered_time_ = now;
294
295   if (next_frame_delivered_time_ + frame_duration_ < now) {
296     // Too late, drop the frame
297     reuse_picture_cb_.Run(pending_pictures_.front().picture_buffer_id());
298   } else {
299     client_->PictureReady(pending_pictures_.front());
300   }
301
302   pending_pictures_.pop_front();
303   next_frame_delivered_time_ += frame_duration_;
304   if (!pending_pictures_.empty()) {
305     base::MessageLoop::current()->PostDelayedTask(
306         FROM_HERE,
307         base::Bind(&ThrottlingVDAClient::CallClientPictureReady,
308                    AsWeakPtr(),
309                    stream_version_),
310         next_frame_delivered_time_ - base::TimeTicks::Now());
311   }
312 }
313
314 void ThrottlingVDAClient::NotifyInitializeDone() {
315   client_->NotifyInitializeDone();
316 }
317
318 void ThrottlingVDAClient::NotifyEndOfBitstreamBuffer(
319     int32 bitstream_buffer_id) {
320   client_->NotifyEndOfBitstreamBuffer(bitstream_buffer_id);
321 }
322
323 void ThrottlingVDAClient::NotifyFlushDone() {
324   if (!pending_pictures_.empty()) {
325     base::MessageLoop::current()->PostDelayedTask(
326         FROM_HERE,
327         base::Bind(&ThrottlingVDAClient::NotifyFlushDone,
328                    base::Unretained(this)),
329         next_frame_delivered_time_ - base::TimeTicks::Now());
330     return;
331   }
332   client_->NotifyFlushDone();
333 }
334
335 void ThrottlingVDAClient::NotifyResetDone() {
336   ++stream_version_;
337   while (!pending_pictures_.empty()) {
338     reuse_picture_cb_.Run(pending_pictures_.front().picture_buffer_id());
339     pending_pictures_.pop_front();
340   }
341   next_frame_delivered_time_ = base::TimeTicks();
342   client_->NotifyResetDone();
343 }
344
345 void ThrottlingVDAClient::NotifyError(VideoDecodeAccelerator::Error error) {
346   client_->NotifyError(error);
347 }
348
349 // Client that can accept callbacks from a VideoDecodeAccelerator and is used by
350 // the TESTs below.
351 class GLRenderingVDAClient
352     : public VideoDecodeAccelerator::Client,
353       public base::SupportsWeakPtr<GLRenderingVDAClient> {
354  public:
355   // Doesn't take ownership of |rendering_helper| or |note|, which must outlive
356   // |*this|.
357   // |num_play_throughs| indicates how many times to play through the video.
358   // |reset_after_frame_num| can be a frame number >=0 indicating a mid-stream
359   // Reset() should be done after that frame number is delivered, or
360   // END_OF_STREAM_RESET to indicate no mid-stream Reset().
361   // |delete_decoder_state| indicates when the underlying decoder should be
362   // Destroy()'d and deleted and can take values: N<0: delete after -N Decode()
363   // calls have been made, N>=0 means interpret as ClientState.
364   // Both |reset_after_frame_num| & |delete_decoder_state| apply only to the
365   // last play-through (governed by |num_play_throughs|).
366   // |rendering_fps| indicates the target rendering fps. 0 means no target fps
367   // and it would render as fast as possible.
368   // |suppress_rendering| indicates GL rendering is suppressed or not.
369   // After |delay_reuse_after_frame_num| frame has been delivered, the client
370   // will start delaying the call to ReusePictureBuffer() for kReuseDelay.
371   // |decode_calls_per_second| is the number of VDA::Decode calls per second.
372   // If |decode_calls_per_second| > 0, |num_in_flight_decodes| must be 1.
373   GLRenderingVDAClient(RenderingHelper* rendering_helper,
374                        int rendering_window_id,
375                        ClientStateNotification<ClientState>* note,
376                        const std::string& encoded_data,
377                        int num_in_flight_decodes,
378                        int num_play_throughs,
379                        int reset_after_frame_num,
380                        int delete_decoder_state,
381                        int frame_width,
382                        int frame_height,
383                        int profile,
384                        double rendering_fps,
385                        bool suppress_rendering,
386                        int delay_reuse_after_frame_num,
387                        int decode_calls_per_second);
388   virtual ~GLRenderingVDAClient();
389   void CreateAndStartDecoder();
390
391   // VideoDecodeAccelerator::Client implementation.
392   // The heart of the Client.
393   virtual void ProvidePictureBuffers(uint32 requested_num_of_buffers,
394                                      const gfx::Size& dimensions,
395                                      uint32 texture_target) OVERRIDE;
396   virtual void DismissPictureBuffer(int32 picture_buffer_id) OVERRIDE;
397   virtual void PictureReady(const media::Picture& picture) OVERRIDE;
398   // Simple state changes.
399   virtual void NotifyInitializeDone() OVERRIDE;
400   virtual void NotifyEndOfBitstreamBuffer(int32 bitstream_buffer_id) OVERRIDE;
401   virtual void NotifyFlushDone() OVERRIDE;
402   virtual void NotifyResetDone() OVERRIDE;
403   virtual void NotifyError(VideoDecodeAccelerator::Error error) OVERRIDE;
404
405   void OutputFrameDeliveryTimes(base::PlatformFile output);
406
407   void NotifyFrameDropped(int32 picture_buffer_id);
408
409   // Simple getters for inspecting the state of the Client.
410   int num_done_bitstream_buffers() { return num_done_bitstream_buffers_; }
411   int num_skipped_fragments() { return num_skipped_fragments_; }
412   int num_queued_fragments() { return num_queued_fragments_; }
413   int num_decoded_frames();
414   double frames_per_second();
415   // Return the median of the decode time in milliseconds.
416   int decode_time_median();
417   bool decoder_deleted() { return !decoder_.get(); }
418
419  private:
420   typedef std::map<int, media::PictureBuffer*> PictureBufferById;
421
422   void SetState(ClientState new_state);
423
424   // Delete the associated decoder helper.
425   void DeleteDecoder();
426
427   // Compute & return the first encoded bytes (including a start frame) to send
428   // to the decoder, starting at |start_pos| and returning one fragment. Skips
429   // to the first decodable position.
430   std::string GetBytesForFirstFragment(size_t start_pos, size_t* end_pos);
431   // Compute & return the encoded bytes of next fragment to send to the decoder
432   // (based on |start_pos|).
433   std::string GetBytesForNextFragment(size_t start_pos, size_t* end_pos);
434   // Helpers for GetBytesForNextFragment above.
435   void GetBytesForNextNALU(size_t start_pos, size_t* end_pos);  // For h.264.
436   std::string GetBytesForNextFrame(
437       size_t start_pos, size_t* end_pos);  // For VP8.
438
439   // Request decode of the next fragment in the encoded data.
440   void DecodeNextFragment();
441
442   RenderingHelper* rendering_helper_;
443   int rendering_window_id_;
444   std::string encoded_data_;
445   const int num_in_flight_decodes_;
446   int outstanding_decodes_;
447   size_t encoded_data_next_pos_to_decode_;
448   int next_bitstream_buffer_id_;
449   ClientStateNotification<ClientState>* note_;
450   scoped_ptr<VideoDecodeAccelerator> decoder_;
451   std::set<int> outstanding_texture_ids_;
452   int remaining_play_throughs_;
453   int reset_after_frame_num_;
454   int delete_decoder_state_;
455   ClientState state_;
456   int num_skipped_fragments_;
457   int num_queued_fragments_;
458   int num_decoded_frames_;
459   int num_done_bitstream_buffers_;
460   PictureBufferById picture_buffers_by_id_;
461   base::TimeTicks initialize_done_ticks_;
462   int profile_;
463   GLenum texture_target_;
464   bool suppress_rendering_;
465   std::vector<base::TimeTicks> frame_delivery_times_;
466   int delay_reuse_after_frame_num_;
467   scoped_ptr<ThrottlingVDAClient> throttling_client_;
468   // A map from bitstream buffer id to the decode start time of the buffer.
469   std::map<int, base::TimeTicks> decode_start_time_;
470   // The decode time of all decoded frames.
471   std::vector<base::TimeDelta> decode_time_;
472   // The number of VDA::Decode calls per second. This is to simulate webrtc.
473   int decode_calls_per_second_;
474
475   DISALLOW_IMPLICIT_CONSTRUCTORS(GLRenderingVDAClient);
476 };
477
478 GLRenderingVDAClient::GLRenderingVDAClient(
479     RenderingHelper* rendering_helper,
480     int rendering_window_id,
481     ClientStateNotification<ClientState>* note,
482     const std::string& encoded_data,
483     int num_in_flight_decodes,
484     int num_play_throughs,
485     int reset_after_frame_num,
486     int delete_decoder_state,
487     int frame_width,
488     int frame_height,
489     int profile,
490     double rendering_fps,
491     bool suppress_rendering,
492     int delay_reuse_after_frame_num,
493     int decode_calls_per_second)
494     : rendering_helper_(rendering_helper),
495       rendering_window_id_(rendering_window_id),
496       encoded_data_(encoded_data),
497       num_in_flight_decodes_(num_in_flight_decodes),
498       outstanding_decodes_(0),
499       encoded_data_next_pos_to_decode_(0),
500       next_bitstream_buffer_id_(0),
501       note_(note),
502       remaining_play_throughs_(num_play_throughs),
503       reset_after_frame_num_(reset_after_frame_num),
504       delete_decoder_state_(delete_decoder_state),
505       state_(CS_CREATED),
506       num_skipped_fragments_(0),
507       num_queued_fragments_(0),
508       num_decoded_frames_(0),
509       num_done_bitstream_buffers_(0),
510       profile_(profile),
511       texture_target_(0),
512       suppress_rendering_(suppress_rendering),
513       delay_reuse_after_frame_num_(delay_reuse_after_frame_num),
514       decode_calls_per_second_(decode_calls_per_second) {
515   CHECK_GT(num_in_flight_decodes, 0);
516   CHECK_GT(num_play_throughs, 0);
517   CHECK_GE(rendering_fps, 0);
518   // |num_in_flight_decodes_| is unsupported if |decode_calls_per_second_| > 0.
519   if (decode_calls_per_second_ > 0)
520     CHECK_EQ(1, num_in_flight_decodes_);
521   if (rendering_fps > 0)
522     throttling_client_.reset(new ThrottlingVDAClient(
523         this,
524         rendering_fps,
525         base::Bind(&GLRenderingVDAClient::NotifyFrameDropped,
526                    base::Unretained(this))));
527 }
528
529 GLRenderingVDAClient::~GLRenderingVDAClient() {
530   DeleteDecoder();  // Clean up in case of expected error.
531   CHECK(decoder_deleted());
532   STLDeleteValues(&picture_buffers_by_id_);
533   SetState(CS_DESTROYED);
534 }
535
536 static bool DoNothingReturnTrue() { return true; }
537
538 void GLRenderingVDAClient::CreateAndStartDecoder() {
539   CHECK(decoder_deleted());
540   CHECK(!decoder_.get());
541
542   VideoDecodeAccelerator::Client* client = this;
543   base::WeakPtr<VideoDecodeAccelerator::Client> weak_client = AsWeakPtr();
544   if (throttling_client_) {
545     client = throttling_client_.get();
546     weak_client = throttling_client_->AsWeakPtr();
547   }
548 #if defined(OS_WIN)
549   decoder_.reset(
550       new DXVAVideoDecodeAccelerator(client, base::Bind(&DoNothingReturnTrue)));
551 #elif defined(OS_CHROMEOS) || defined(OS_LINUX)
552 #if defined(ARCH_CPU_ARMEL)
553
554   scoped_ptr<V4L2Device> device = V4L2Device::Create();
555   if (!device.get()) {
556     NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
557     return;
558   }
559   decoder_.reset(new V4L2VideoDecodeAccelerator(
560       static_cast<EGLDisplay>(rendering_helper_->GetGLDisplay()),
561       client,
562       weak_client,
563       base::Bind(&DoNothingReturnTrue),
564       device.Pass(),
565       base::MessageLoopProxy::current()));
566 #elif defined(ARCH_CPU_X86_FAMILY)
567   CHECK_EQ(gfx::kGLImplementationDesktopGL, gfx::GetGLImplementation())
568       << "Hardware video decode does not work with OSMesa";
569   decoder_.reset(new VaapiVideoDecodeAccelerator(
570       static_cast<Display*>(rendering_helper_->GetGLDisplay()),
571       client,
572       base::Bind(&DoNothingReturnTrue)));
573 #endif  // ARCH_CPU_ARMEL
574 #endif  // OS_WIN
575   CHECK(decoder_.get());
576   SetState(CS_DECODER_SET);
577   if (decoder_deleted())
578     return;
579
580   // Configure the decoder.
581   media::VideoCodecProfile profile = media::H264PROFILE_BASELINE;
582   if (profile_ != -1)
583     profile = static_cast<media::VideoCodecProfile>(profile_);
584   CHECK(decoder_->Initialize(profile));
585 }
586
587 void GLRenderingVDAClient::ProvidePictureBuffers(
588     uint32 requested_num_of_buffers,
589     const gfx::Size& dimensions,
590     uint32 texture_target) {
591   if (decoder_deleted())
592     return;
593   std::vector<media::PictureBuffer> buffers;
594
595   texture_target_ = texture_target;
596   for (uint32 i = 0; i < requested_num_of_buffers; ++i) {
597     uint32 id = picture_buffers_by_id_.size();
598     uint32 texture_id;
599     base::WaitableEvent done(false, false);
600     rendering_helper_->CreateTexture(
601         rendering_window_id_, texture_target_, &texture_id, &done);
602     done.Wait();
603     CHECK(outstanding_texture_ids_.insert(texture_id).second);
604     media::PictureBuffer* buffer =
605         new media::PictureBuffer(id, dimensions, texture_id);
606     CHECK(picture_buffers_by_id_.insert(std::make_pair(id, buffer)).second);
607     buffers.push_back(*buffer);
608   }
609   decoder_->AssignPictureBuffers(buffers);
610 }
611
612 void GLRenderingVDAClient::DismissPictureBuffer(int32 picture_buffer_id) {
613   PictureBufferById::iterator it =
614       picture_buffers_by_id_.find(picture_buffer_id);
615   CHECK(it != picture_buffers_by_id_.end());
616   CHECK_EQ(outstanding_texture_ids_.erase(it->second->texture_id()), 1U);
617   rendering_helper_->DeleteTexture(it->second->texture_id());
618   delete it->second;
619   picture_buffers_by_id_.erase(it);
620 }
621
622 void GLRenderingVDAClient::PictureReady(const media::Picture& picture) {
623   // We shouldn't be getting pictures delivered after Reset has completed.
624   CHECK_LT(state_, CS_RESET);
625
626   if (decoder_deleted())
627     return;
628
629   base::TimeTicks now = base::TimeTicks::Now();
630   frame_delivery_times_.push_back(now);
631   // Save the decode time of this picture.
632   std::map<int, base::TimeTicks>::iterator it =
633       decode_start_time_.find(picture.bitstream_buffer_id());
634   ASSERT_NE(decode_start_time_.end(), it);
635   decode_time_.push_back(now - it->second);
636   decode_start_time_.erase(it);
637
638   CHECK_LE(picture.bitstream_buffer_id(), next_bitstream_buffer_id_);
639   ++num_decoded_frames_;
640
641   // Mid-stream reset applies only to the last play-through per constructor
642   // comment.
643   if (remaining_play_throughs_ == 1 &&
644       reset_after_frame_num_ == num_decoded_frames()) {
645     reset_after_frame_num_ = MID_STREAM_RESET;
646     decoder_->Reset();
647     // Re-start decoding from the beginning of the stream to avoid needing to
648     // know how to find I-frames and so on in this test.
649     encoded_data_next_pos_to_decode_ = 0;
650   }
651
652   media::PictureBuffer* picture_buffer =
653       picture_buffers_by_id_[picture.picture_buffer_id()];
654   CHECK(picture_buffer);
655   if (!suppress_rendering_) {
656     rendering_helper_->RenderTexture(texture_target_,
657                                      picture_buffer->texture_id());
658   }
659
660   if (num_decoded_frames() > delay_reuse_after_frame_num_) {
661     base::MessageLoop::current()->PostDelayedTask(
662         FROM_HERE,
663         base::Bind(&VideoDecodeAccelerator::ReusePictureBuffer,
664                    decoder_->AsWeakPtr(),
665                    picture.picture_buffer_id()),
666         kReuseDelay);
667   } else {
668     decoder_->ReusePictureBuffer(picture.picture_buffer_id());
669   }
670 }
671
672 void GLRenderingVDAClient::NotifyInitializeDone() {
673   SetState(CS_INITIALIZED);
674   initialize_done_ticks_ = base::TimeTicks::Now();
675
676   if (reset_after_frame_num_ == START_OF_STREAM_RESET) {
677     decoder_->Reset();
678     return;
679   }
680
681   for (int i = 0; i < num_in_flight_decodes_; ++i)
682     DecodeNextFragment();
683   DCHECK_EQ(outstanding_decodes_, num_in_flight_decodes_);
684 }
685
686 void GLRenderingVDAClient::NotifyEndOfBitstreamBuffer(
687     int32 bitstream_buffer_id) {
688   // TODO(fischman): this test currently relies on this notification to make
689   // forward progress during a Reset().  But the VDA::Reset() API doesn't
690   // guarantee this, so stop relying on it (and remove the notifications from
691   // VaapiVideoDecodeAccelerator::FinishReset()).
692   ++num_done_bitstream_buffers_;
693   --outstanding_decodes_;
694   if (decode_calls_per_second_ == 0)
695     DecodeNextFragment();
696 }
697
698 void GLRenderingVDAClient::NotifyFlushDone() {
699   if (decoder_deleted())
700     return;
701   SetState(CS_FLUSHED);
702   --remaining_play_throughs_;
703   DCHECK_GE(remaining_play_throughs_, 0);
704   if (decoder_deleted())
705     return;
706   decoder_->Reset();
707   SetState(CS_RESETTING);
708 }
709
710 void GLRenderingVDAClient::NotifyResetDone() {
711   if (decoder_deleted())
712     return;
713
714   if (reset_after_frame_num_ == MID_STREAM_RESET) {
715     reset_after_frame_num_ = END_OF_STREAM_RESET;
716     DecodeNextFragment();
717     return;
718   } else if (reset_after_frame_num_ == START_OF_STREAM_RESET) {
719     reset_after_frame_num_ = END_OF_STREAM_RESET;
720     for (int i = 0; i < num_in_flight_decodes_; ++i)
721       DecodeNextFragment();
722     return;
723   }
724
725   if (remaining_play_throughs_) {
726     encoded_data_next_pos_to_decode_ = 0;
727     NotifyInitializeDone();
728     return;
729   }
730
731   SetState(CS_RESET);
732   if (!decoder_deleted())
733     DeleteDecoder();
734 }
735
736 void GLRenderingVDAClient::NotifyError(VideoDecodeAccelerator::Error error) {
737   SetState(CS_ERROR);
738 }
739
740 void GLRenderingVDAClient::OutputFrameDeliveryTimes(base::PlatformFile output) {
741   std::string s = base::StringPrintf("frame count: %" PRIuS "\n",
742                                      frame_delivery_times_.size());
743   base::WritePlatformFileAtCurrentPos(output, s.data(), s.length());
744   base::TimeTicks t0 = initialize_done_ticks_;
745   for (size_t i = 0; i < frame_delivery_times_.size(); ++i) {
746     s = base::StringPrintf("frame %04" PRIuS ": %" PRId64 " us\n",
747                            i,
748                            (frame_delivery_times_[i] - t0).InMicroseconds());
749     t0 = frame_delivery_times_[i];
750     base::WritePlatformFileAtCurrentPos(output, s.data(), s.length());
751   }
752 }
753
754 void GLRenderingVDAClient::NotifyFrameDropped(int32 picture_buffer_id) {
755   decoder_->ReusePictureBuffer(picture_buffer_id);
756 }
757
758 static bool LookingAtNAL(const std::string& encoded, size_t pos) {
759   return encoded[pos] == 0 && encoded[pos + 1] == 0 &&
760       encoded[pos + 2] == 0 && encoded[pos + 3] == 1;
761 }
762
763 void GLRenderingVDAClient::SetState(ClientState new_state) {
764   note_->Notify(new_state);
765   state_ = new_state;
766   if (!remaining_play_throughs_ && new_state == delete_decoder_state_) {
767     CHECK(!decoder_deleted());
768     DeleteDecoder();
769   }
770 }
771
772 void GLRenderingVDAClient::DeleteDecoder() {
773   if (decoder_deleted())
774     return;
775   decoder_.release()->Destroy();
776   STLClearObject(&encoded_data_);
777   for (std::set<int>::iterator it = outstanding_texture_ids_.begin();
778        it != outstanding_texture_ids_.end(); ++it) {
779     rendering_helper_->DeleteTexture(*it);
780   }
781   outstanding_texture_ids_.clear();
782   // Cascade through the rest of the states to simplify test code below.
783   for (int i = state_ + 1; i < CS_MAX; ++i)
784     SetState(static_cast<ClientState>(i));
785 }
786
787 std::string GLRenderingVDAClient::GetBytesForFirstFragment(
788     size_t start_pos, size_t* end_pos) {
789   if (profile_ < media::H264PROFILE_MAX) {
790     *end_pos = start_pos;
791     while (*end_pos + 4 < encoded_data_.size()) {
792       if ((encoded_data_[*end_pos + 4] & 0x1f) == 0x7) // SPS start frame
793         return GetBytesForNextFragment(*end_pos, end_pos);
794       GetBytesForNextNALU(*end_pos, end_pos);
795       num_skipped_fragments_++;
796     }
797     *end_pos = start_pos;
798     return std::string();
799   }
800   DCHECK_LE(profile_, media::VP8PROFILE_MAX);
801   return GetBytesForNextFragment(start_pos, end_pos);
802 }
803
804 std::string GLRenderingVDAClient::GetBytesForNextFragment(
805     size_t start_pos, size_t* end_pos) {
806   if (profile_ < media::H264PROFILE_MAX) {
807     *end_pos = start_pos;
808     GetBytesForNextNALU(*end_pos, end_pos);
809     if (start_pos != *end_pos) {
810       num_queued_fragments_++;
811     }
812     return encoded_data_.substr(start_pos, *end_pos - start_pos);
813   }
814   DCHECK_LE(profile_, media::VP8PROFILE_MAX);
815   return GetBytesForNextFrame(start_pos, end_pos);
816 }
817
818 void GLRenderingVDAClient::GetBytesForNextNALU(
819     size_t start_pos, size_t* end_pos) {
820   *end_pos = start_pos;
821   if (*end_pos + 4 > encoded_data_.size())
822     return;
823   CHECK(LookingAtNAL(encoded_data_, start_pos));
824   *end_pos += 4;
825   while (*end_pos + 4 <= encoded_data_.size() &&
826          !LookingAtNAL(encoded_data_, *end_pos)) {
827     ++*end_pos;
828   }
829   if (*end_pos + 3 >= encoded_data_.size())
830     *end_pos = encoded_data_.size();
831 }
832
833 std::string GLRenderingVDAClient::GetBytesForNextFrame(
834     size_t start_pos, size_t* end_pos) {
835   // Helpful description: http://wiki.multimedia.cx/index.php?title=IVF
836   std::string bytes;
837   if (start_pos == 0)
838     start_pos = 32;  // Skip IVF header.
839   *end_pos = start_pos;
840   uint32 frame_size = *reinterpret_cast<uint32*>(&encoded_data_[*end_pos]);
841   *end_pos += 12;  // Skip frame header.
842   bytes.append(encoded_data_.substr(*end_pos, frame_size));
843   *end_pos += frame_size;
844   num_queued_fragments_++;
845   return bytes;
846 }
847
848 void GLRenderingVDAClient::DecodeNextFragment() {
849   if (decoder_deleted())
850     return;
851   if (encoded_data_next_pos_to_decode_ == encoded_data_.size()) {
852     if (outstanding_decodes_ == 0) {
853       decoder_->Flush();
854       SetState(CS_FLUSHING);
855     }
856     return;
857   }
858   size_t end_pos;
859   std::string next_fragment_bytes;
860   if (encoded_data_next_pos_to_decode_ == 0) {
861     next_fragment_bytes = GetBytesForFirstFragment(0, &end_pos);
862   } else {
863     next_fragment_bytes =
864         GetBytesForNextFragment(encoded_data_next_pos_to_decode_, &end_pos);
865   }
866   size_t next_fragment_size = next_fragment_bytes.size();
867
868   // Populate the shared memory buffer w/ the fragment, duplicate its handle,
869   // and hand it off to the decoder.
870   base::SharedMemory shm;
871   CHECK(shm.CreateAndMapAnonymous(next_fragment_size));
872   memcpy(shm.memory(), next_fragment_bytes.data(), next_fragment_size);
873   base::SharedMemoryHandle dup_handle;
874   CHECK(shm.ShareToProcess(base::Process::Current().handle(), &dup_handle));
875   media::BitstreamBuffer bitstream_buffer(
876       next_bitstream_buffer_id_, dup_handle, next_fragment_size);
877   decode_start_time_[next_bitstream_buffer_id_] = base::TimeTicks::Now();
878   // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
879   next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF;
880   decoder_->Decode(bitstream_buffer);
881   ++outstanding_decodes_;
882   encoded_data_next_pos_to_decode_ = end_pos;
883
884   if (!remaining_play_throughs_ &&
885       -delete_decoder_state_ == next_bitstream_buffer_id_) {
886     DeleteDecoder();
887   }
888
889   if (decode_calls_per_second_ > 0) {
890     base::MessageLoop::current()->PostDelayedTask(
891         FROM_HERE,
892         base::Bind(&GLRenderingVDAClient::DecodeNextFragment, AsWeakPtr()),
893         base::TimeDelta::FromSeconds(1) / decode_calls_per_second_);
894   }
895 }
896
897 int GLRenderingVDAClient::num_decoded_frames() {
898   return throttling_client_ ? throttling_client_->num_decoded_frames()
899                             : num_decoded_frames_;
900 }
901
902 double GLRenderingVDAClient::frames_per_second() {
903   base::TimeDelta delta = frame_delivery_times_.back() - initialize_done_ticks_;
904   if (delta.InSecondsF() == 0)
905     return 0;
906   return num_decoded_frames() / delta.InSecondsF();
907 }
908
909 int GLRenderingVDAClient::decode_time_median() {
910   if (decode_time_.size() == 0)
911     return 0;
912   std::sort(decode_time_.begin(), decode_time_.end());
913   int index = decode_time_.size() / 2;
914   if (decode_time_.size() % 2 != 0)
915     return decode_time_[index].InMilliseconds();
916
917   return (decode_time_[index] + decode_time_[index - 1]).InMilliseconds() / 2;
918 }
919
920 class VideoDecodeAcceleratorTest : public ::testing::Test {
921  protected:
922   VideoDecodeAcceleratorTest();
923   virtual void SetUp();
924   virtual void TearDown();
925
926   // Parse |data| into its constituent parts, set the various output fields
927   // accordingly, and read in video stream. CHECK-fails on unexpected or
928   // missing required data. Unspecified optional fields are set to -1.
929   void ParseAndReadTestVideoData(base::FilePath::StringType data,
930                                  std::vector<TestVideoFile*>* test_video_files);
931
932   // Update the parameters of |test_video_files| according to
933   // |num_concurrent_decoders| and |reset_point|. Ex: the expected number of
934   // frames should be adjusted if decoder is reset in the middle of the stream.
935   void UpdateTestVideoFileParams(
936       size_t num_concurrent_decoders,
937       int reset_point,
938       std::vector<TestVideoFile*>* test_video_files);
939
940   void InitializeRenderingHelper(const RenderingHelperParams& helper_params);
941   void CreateAndStartDecoder(GLRenderingVDAClient* client,
942                              ClientStateNotification<ClientState>* note);
943   void WaitUntilDecodeFinish(ClientStateNotification<ClientState>* note);
944   void WaitUntilIdle();
945   void OutputLogFile(const base::FilePath::CharType* log_path,
946                      const std::string& content);
947
948   std::vector<TestVideoFile*> test_video_files_;
949   RenderingHelper rendering_helper_;
950   scoped_refptr<base::MessageLoopProxy> rendering_loop_proxy_;
951
952  private:
953   base::Thread rendering_thread_;
954   // Required for Thread to work.  Not used otherwise.
955   base::ShadowingAtExitManager at_exit_manager_;
956
957   DISALLOW_COPY_AND_ASSIGN(VideoDecodeAcceleratorTest);
958 };
959
960 VideoDecodeAcceleratorTest::VideoDecodeAcceleratorTest()
961     : rendering_thread_("GLRenderingVDAClientThread") {}
962
963 void VideoDecodeAcceleratorTest::SetUp() {
964   ParseAndReadTestVideoData(g_test_video_data, &test_video_files_);
965
966   // Initialize the rendering thread.
967   base::Thread::Options options;
968   options.message_loop_type = base::MessageLoop::TYPE_DEFAULT;
969 #if defined(OS_WIN)
970   // For windows the decoding thread initializes the media foundation decoder
971   // which uses COM. We need the thread to be a UI thread.
972   options.message_loop_type = base::MessageLoop::TYPE_UI;
973 #endif  // OS_WIN
974
975   rendering_thread_.StartWithOptions(options);
976   rendering_loop_proxy_ = rendering_thread_.message_loop_proxy();
977 }
978
979 void VideoDecodeAcceleratorTest::TearDown() {
980   rendering_loop_proxy_->PostTask(
981       FROM_HERE,
982       base::Bind(&STLDeleteElements<std::vector<TestVideoFile*> >,
983                  &test_video_files_));
984
985   base::WaitableEvent done(false, false);
986   rendering_loop_proxy_->PostTask(
987       FROM_HERE,
988       base::Bind(&RenderingHelper::UnInitialize,
989                  base::Unretained(&rendering_helper_),
990                  &done));
991   done.Wait();
992
993   rendering_thread_.Stop();
994 }
995
996 void VideoDecodeAcceleratorTest::ParseAndReadTestVideoData(
997     base::FilePath::StringType data,
998     std::vector<TestVideoFile*>* test_video_files) {
999   std::vector<base::FilePath::StringType> entries;
1000   base::SplitString(data, ';', &entries);
1001   CHECK_GE(entries.size(), 1U) << data;
1002   for (size_t index = 0; index < entries.size(); ++index) {
1003     std::vector<base::FilePath::StringType> fields;
1004     base::SplitString(entries[index], ':', &fields);
1005     CHECK_GE(fields.size(), 1U) << entries[index];
1006     CHECK_LE(fields.size(), 8U) << entries[index];
1007     TestVideoFile* video_file = new TestVideoFile(fields[0]);
1008     if (!fields[1].empty())
1009       CHECK(base::StringToInt(fields[1], &video_file->width));
1010     if (!fields[2].empty())
1011       CHECK(base::StringToInt(fields[2], &video_file->height));
1012     if (!fields[3].empty())
1013       CHECK(base::StringToInt(fields[3], &video_file->num_frames));
1014     if (!fields[4].empty())
1015       CHECK(base::StringToInt(fields[4], &video_file->num_fragments));
1016     if (!fields[5].empty())
1017       CHECK(base::StringToInt(fields[5], &video_file->min_fps_render));
1018     if (!fields[6].empty())
1019       CHECK(base::StringToInt(fields[6], &video_file->min_fps_no_render));
1020     if (!fields[7].empty())
1021       CHECK(base::StringToInt(fields[7], &video_file->profile));
1022
1023     // Read in the video data.
1024     base::FilePath filepath(video_file->file_name);
1025     CHECK(base::ReadFileToString(filepath, &video_file->data_str))
1026         << "test_video_file: " << filepath.MaybeAsASCII();
1027
1028     test_video_files->push_back(video_file);
1029   }
1030 }
1031
1032 void VideoDecodeAcceleratorTest::UpdateTestVideoFileParams(
1033     size_t num_concurrent_decoders,
1034     int reset_point,
1035     std::vector<TestVideoFile*>* test_video_files) {
1036   for (size_t i = 0; i < test_video_files->size(); i++) {
1037     TestVideoFile* video_file = (*test_video_files)[i];
1038     if (video_file->num_frames > 0 && reset_point == MID_STREAM_RESET) {
1039       // Reset should not go beyond the last frame; reset after the first
1040       // frame for short videos.
1041       video_file->reset_after_frame_num = kMaxResetAfterFrameNum;
1042       if (video_file->num_frames <= kMaxResetAfterFrameNum)
1043         video_file->reset_after_frame_num = 1;
1044       video_file->num_frames += video_file->reset_after_frame_num;
1045     }
1046     if (video_file->min_fps_render != -1)
1047       video_file->min_fps_render /= num_concurrent_decoders;
1048     if (video_file->min_fps_no_render != -1)
1049       video_file->min_fps_no_render /= num_concurrent_decoders;
1050   }
1051 }
1052
1053 void VideoDecodeAcceleratorTest::InitializeRenderingHelper(
1054     const RenderingHelperParams& helper_params) {
1055   base::WaitableEvent done(false, false);
1056   rendering_loop_proxy_->PostTask(
1057       FROM_HERE,
1058       base::Bind(&RenderingHelper::Initialize,
1059                  base::Unretained(&rendering_helper_),
1060                  helper_params,
1061                  &done));
1062   done.Wait();
1063 }
1064
1065 void VideoDecodeAcceleratorTest::CreateAndStartDecoder(
1066     GLRenderingVDAClient* client,
1067     ClientStateNotification<ClientState>* note) {
1068   rendering_loop_proxy_->PostTask(
1069       FROM_HERE,
1070       base::Bind(&GLRenderingVDAClient::CreateAndStartDecoder,
1071                  base::Unretained(client)));
1072   ASSERT_EQ(note->Wait(), CS_DECODER_SET);
1073 }
1074
1075 void VideoDecodeAcceleratorTest::WaitUntilDecodeFinish(
1076     ClientStateNotification<ClientState>* note) {
1077   for (int i = 0; i < CS_MAX; i++) {
1078     if (note->Wait() == CS_DESTROYED)
1079       break;
1080   }
1081 }
1082
1083 void VideoDecodeAcceleratorTest::WaitUntilIdle() {
1084   base::WaitableEvent done(false, false);
1085   rendering_loop_proxy_->PostTask(
1086       FROM_HERE,
1087       base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
1088   done.Wait();
1089 }
1090
1091 void VideoDecodeAcceleratorTest::OutputLogFile(
1092     const base::FilePath::CharType* log_path,
1093     const std::string& content) {
1094   base::PlatformFile file = base::CreatePlatformFile(
1095       base::FilePath(log_path),
1096       base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
1097       NULL,
1098       NULL);
1099   base::WritePlatformFileAtCurrentPos(file, content.data(), content.length());
1100   base::ClosePlatformFile(file);
1101 }
1102
1103 // Test parameters:
1104 // - Number of concurrent decoders.
1105 // - Number of concurrent in-flight Decode() calls per decoder.
1106 // - Number of play-throughs.
1107 // - reset_after_frame_num: see GLRenderingVDAClient ctor.
1108 // - delete_decoder_phase: see GLRenderingVDAClient ctor.
1109 // - whether to test slow rendering by delaying ReusePictureBuffer().
1110 // - whether the video frames are rendered as thumbnails.
1111 class VideoDecodeAcceleratorParamTest
1112     : public VideoDecodeAcceleratorTest,
1113       public ::testing::WithParamInterface<
1114         Tuple7<int, int, int, ResetPoint, ClientState, bool, bool> > {
1115 };
1116
1117 // Helper so that gtest failures emit a more readable version of the tuple than
1118 // its byte representation.
1119 ::std::ostream& operator<<(
1120     ::std::ostream& os,
1121     const Tuple7<int, int, int, ResetPoint, ClientState, bool, bool>& t) {
1122   return os << t.a << ", " << t.b << ", " << t.c << ", " << t.d << ", " << t.e
1123             << ", " << t.f << ", " << t.g;
1124 }
1125
1126 // Wait for |note| to report a state and if it's not |expected_state| then
1127 // assert |client| has deleted its decoder.
1128 static void AssertWaitForStateOrDeleted(
1129     ClientStateNotification<ClientState>* note,
1130     GLRenderingVDAClient* client,
1131     ClientState expected_state) {
1132   ClientState state = note->Wait();
1133   if (state == expected_state) return;
1134   ASSERT_TRUE(client->decoder_deleted())
1135       << "Decoder not deleted but Wait() returned " << state
1136       << ", instead of " << expected_state;
1137 }
1138
1139 // We assert a minimal number of concurrent decoders we expect to succeed.
1140 // Different platforms can support more concurrent decoders, so we don't assert
1141 // failure above this.
1142 enum { kMinSupportedNumConcurrentDecoders = 3 };
1143
1144 // Test the most straightforward case possible: data is decoded from a single
1145 // chunk and rendered to the screen.
1146 TEST_P(VideoDecodeAcceleratorParamTest, TestSimpleDecode) {
1147   const size_t num_concurrent_decoders = GetParam().a;
1148   const size_t num_in_flight_decodes = GetParam().b;
1149   const int num_play_throughs = GetParam().c;
1150   const int reset_point = GetParam().d;
1151   const int delete_decoder_state = GetParam().e;
1152   bool test_reuse_delay = GetParam().f;
1153   const bool render_as_thumbnails = GetParam().g;
1154
1155   UpdateTestVideoFileParams(
1156       num_concurrent_decoders, reset_point, &test_video_files_);
1157
1158   // Suppress GL rendering for all tests when the "--disable_rendering" is set.
1159   const bool suppress_rendering = g_disable_rendering;
1160
1161   std::vector<ClientStateNotification<ClientState>*>
1162       notes(num_concurrent_decoders, NULL);
1163   std::vector<GLRenderingVDAClient*> clients(num_concurrent_decoders, NULL);
1164
1165   RenderingHelperParams helper_params;
1166   helper_params.num_windows = num_concurrent_decoders;
1167   helper_params.render_as_thumbnails = render_as_thumbnails;
1168   if (render_as_thumbnails) {
1169     // Only one decoder is supported with thumbnail rendering
1170     CHECK_EQ(num_concurrent_decoders, 1U);
1171     gfx::Size frame_size(test_video_files_[0]->width,
1172                          test_video_files_[0]->height);
1173     helper_params.frame_dimensions.push_back(frame_size);
1174     helper_params.window_dimensions.push_back(kThumbnailsDisplaySize);
1175     helper_params.thumbnails_page_size = kThumbnailsPageSize;
1176     helper_params.thumbnail_size = kThumbnailSize;
1177   } else {
1178     for (size_t index = 0; index < test_video_files_.size(); ++index) {
1179       gfx::Size frame_size(test_video_files_[index]->width,
1180                            test_video_files_[index]->height);
1181       helper_params.frame_dimensions.push_back(frame_size);
1182       helper_params.window_dimensions.push_back(frame_size);
1183     }
1184   }
1185   InitializeRenderingHelper(helper_params);
1186
1187   // First kick off all the decoders.
1188   for (size_t index = 0; index < num_concurrent_decoders; ++index) {
1189     TestVideoFile* video_file =
1190         test_video_files_[index % test_video_files_.size()];
1191     ClientStateNotification<ClientState>* note =
1192         new ClientStateNotification<ClientState>();
1193     notes[index] = note;
1194
1195     int delay_after_frame_num = std::numeric_limits<int>::max();
1196     if (test_reuse_delay &&
1197         kMaxFramesToDelayReuse * 2 < video_file->num_frames) {
1198       delay_after_frame_num = video_file->num_frames - kMaxFramesToDelayReuse;
1199     }
1200
1201     GLRenderingVDAClient* client =
1202         new GLRenderingVDAClient(&rendering_helper_,
1203                                  index,
1204                                  note,
1205                                  video_file->data_str,
1206                                  num_in_flight_decodes,
1207                                  num_play_throughs,
1208                                  video_file->reset_after_frame_num,
1209                                  delete_decoder_state,
1210                                  video_file->width,
1211                                  video_file->height,
1212                                  video_file->profile,
1213                                  g_rendering_fps,
1214                                  suppress_rendering,
1215                                  delay_after_frame_num,
1216                                  0);
1217     clients[index] = client;
1218
1219     CreateAndStartDecoder(client, note);
1220   }
1221   // Then wait for all the decodes to finish.
1222   // Only check performance & correctness later if we play through only once.
1223   bool skip_performance_and_correctness_checks = num_play_throughs > 1;
1224   for (size_t i = 0; i < num_concurrent_decoders; ++i) {
1225     ClientStateNotification<ClientState>* note = notes[i];
1226     ClientState state = note->Wait();
1227     if (state != CS_INITIALIZED) {
1228       skip_performance_and_correctness_checks = true;
1229       // We expect initialization to fail only when more than the supported
1230       // number of decoders is instantiated.  Assert here that something else
1231       // didn't trigger failure.
1232       ASSERT_GT(num_concurrent_decoders,
1233                 static_cast<size_t>(kMinSupportedNumConcurrentDecoders));
1234       continue;
1235     }
1236     ASSERT_EQ(state, CS_INITIALIZED);
1237     for (int n = 0; n < num_play_throughs; ++n) {
1238       // For play-throughs other than the first, we expect initialization to
1239       // succeed unconditionally.
1240       if (n > 0) {
1241         ASSERT_NO_FATAL_FAILURE(
1242             AssertWaitForStateOrDeleted(note, clients[i], CS_INITIALIZED));
1243       }
1244       // InitializeDone kicks off decoding inside the client, so we just need to
1245       // wait for Flush.
1246       ASSERT_NO_FATAL_FAILURE(
1247           AssertWaitForStateOrDeleted(note, clients[i], CS_FLUSHING));
1248       ASSERT_NO_FATAL_FAILURE(
1249           AssertWaitForStateOrDeleted(note, clients[i], CS_FLUSHED));
1250       // FlushDone requests Reset().
1251       ASSERT_NO_FATAL_FAILURE(
1252           AssertWaitForStateOrDeleted(note, clients[i], CS_RESETTING));
1253     }
1254     ASSERT_NO_FATAL_FAILURE(
1255         AssertWaitForStateOrDeleted(note, clients[i], CS_RESET));
1256     // ResetDone requests Destroy().
1257     ASSERT_NO_FATAL_FAILURE(
1258         AssertWaitForStateOrDeleted(note, clients[i], CS_DESTROYED));
1259   }
1260   // Finally assert that decoding went as expected.
1261   for (size_t i = 0; i < num_concurrent_decoders &&
1262            !skip_performance_and_correctness_checks; ++i) {
1263     // We can only make performance/correctness assertions if the decoder was
1264     // allowed to finish.
1265     if (delete_decoder_state < CS_FLUSHED)
1266       continue;
1267     GLRenderingVDAClient* client = clients[i];
1268     TestVideoFile* video_file = test_video_files_[i % test_video_files_.size()];
1269     if (video_file->num_frames > 0) {
1270       // Expect the decoded frames may be more than the video frames as frames
1271       // could still be returned until resetting done.
1272       if (video_file->reset_after_frame_num > 0)
1273         EXPECT_GE(client->num_decoded_frames(), video_file->num_frames);
1274       else
1275         EXPECT_EQ(client->num_decoded_frames(), video_file->num_frames);
1276     }
1277     if (reset_point == END_OF_STREAM_RESET) {
1278       EXPECT_EQ(video_file->num_fragments, client->num_skipped_fragments() +
1279                 client->num_queued_fragments());
1280       EXPECT_EQ(client->num_done_bitstream_buffers(),
1281                 client->num_queued_fragments());
1282     }
1283     VLOG(0) << "Decoder " << i << " fps: " << client->frames_per_second();
1284     if (!render_as_thumbnails) {
1285       int min_fps = suppress_rendering ?
1286           video_file->min_fps_no_render : video_file->min_fps_render;
1287       if (min_fps > 0 && !test_reuse_delay)
1288         EXPECT_GT(client->frames_per_second(), min_fps);
1289     }
1290   }
1291
1292   if (render_as_thumbnails) {
1293     std::vector<unsigned char> rgb;
1294     bool alpha_solid;
1295     base::WaitableEvent done(false, false);
1296     rendering_loop_proxy_->PostTask(
1297       FROM_HERE,
1298       base::Bind(&RenderingHelper::GetThumbnailsAsRGB,
1299                  base::Unretained(&rendering_helper_),
1300                  &rgb, &alpha_solid, &done));
1301     done.Wait();
1302
1303     std::vector<std::string> golden_md5s;
1304     std::string md5_string = base::MD5String(
1305         base::StringPiece(reinterpret_cast<char*>(&rgb[0]), rgb.size()));
1306     ReadGoldenThumbnailMD5s(test_video_files_[0], &golden_md5s);
1307     std::vector<std::string>::iterator match =
1308         find(golden_md5s.begin(), golden_md5s.end(), md5_string);
1309     if (match == golden_md5s.end()) {
1310       // Convert raw RGB into PNG for export.
1311       std::vector<unsigned char> png;
1312       gfx::PNGCodec::Encode(&rgb[0],
1313                             gfx::PNGCodec::FORMAT_RGB,
1314                             kThumbnailsPageSize,
1315                             kThumbnailsPageSize.width() * 3,
1316                             true,
1317                             std::vector<gfx::PNGCodec::Comment>(),
1318                             &png);
1319
1320       LOG(ERROR) << "Unknown thumbnails MD5: " << md5_string;
1321
1322       base::FilePath filepath(test_video_files_[0]->file_name);
1323       filepath = filepath.AddExtension(FILE_PATH_LITERAL(".bad_thumbnails"));
1324       filepath = filepath.AddExtension(FILE_PATH_LITERAL(".png"));
1325       int num_bytes = file_util::WriteFile(filepath,
1326                                            reinterpret_cast<char*>(&png[0]),
1327                                            png.size());
1328       ASSERT_EQ(num_bytes, static_cast<int>(png.size()));
1329     }
1330     ASSERT_NE(match, golden_md5s.end());
1331     EXPECT_EQ(alpha_solid, true) << "RGBA frame had incorrect alpha";
1332   }
1333
1334   // Output the frame delivery time to file
1335   // We can only make performance/correctness assertions if the decoder was
1336   // allowed to finish.
1337   if (g_output_log != NULL && delete_decoder_state >= CS_FLUSHED) {
1338     base::PlatformFile output_file = base::CreatePlatformFile(
1339         base::FilePath(g_output_log),
1340         base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
1341         NULL,
1342         NULL);
1343     for (size_t i = 0; i < num_concurrent_decoders; ++i) {
1344       clients[i]->OutputFrameDeliveryTimes(output_file);
1345     }
1346     base::ClosePlatformFile(output_file);
1347   }
1348
1349   rendering_loop_proxy_->PostTask(
1350       FROM_HERE,
1351       base::Bind(&STLDeleteElements<std::vector<GLRenderingVDAClient*> >,
1352                  &clients));
1353   rendering_loop_proxy_->PostTask(
1354       FROM_HERE,
1355       base::Bind(&STLDeleteElements<
1356                       std::vector<ClientStateNotification<ClientState>*> >,
1357                  &notes));
1358   WaitUntilIdle();
1359 };
1360
1361 // Test that replay after EOS works fine.
1362 INSTANTIATE_TEST_CASE_P(
1363     ReplayAfterEOS, VideoDecodeAcceleratorParamTest,
1364     ::testing::Values(
1365         MakeTuple(1, 1, 4, END_OF_STREAM_RESET, CS_RESET, false, false)));
1366
1367 // This hangs on Exynos, preventing further testing and wasting test machine
1368 // time.
1369 // TODO(ihf): Enable again once http://crbug.com/269754 is fixed.
1370 #if defined(ARCH_CPU_X86_FAMILY)
1371 // Test that Reset() before the first Decode() works fine.
1372 INSTANTIATE_TEST_CASE_P(
1373     ResetBeforeDecode, VideoDecodeAcceleratorParamTest,
1374     ::testing::Values(
1375         MakeTuple(1, 1, 1, START_OF_STREAM_RESET, CS_RESET, false, false)));
1376 #endif  // ARCH_CPU_X86_FAMILY
1377
1378 // Test that Reset() mid-stream works fine and doesn't affect decoding even when
1379 // Decode() calls are made during the reset.
1380 INSTANTIATE_TEST_CASE_P(
1381     MidStreamReset, VideoDecodeAcceleratorParamTest,
1382     ::testing::Values(
1383         MakeTuple(1, 1, 1, MID_STREAM_RESET, CS_RESET, false, false)));
1384
1385 INSTANTIATE_TEST_CASE_P(
1386     SlowRendering, VideoDecodeAcceleratorParamTest,
1387     ::testing::Values(
1388         MakeTuple(1, 1, 1, END_OF_STREAM_RESET, CS_RESET, true, false)));
1389
1390 // Test that Destroy() mid-stream works fine (primarily this is testing that no
1391 // crashes occur).
1392 INSTANTIATE_TEST_CASE_P(
1393     TearDownTiming, VideoDecodeAcceleratorParamTest,
1394     ::testing::Values(
1395         MakeTuple(1, 1, 1, END_OF_STREAM_RESET, CS_DECODER_SET, false, false),
1396         MakeTuple(1, 1, 1, END_OF_STREAM_RESET, CS_INITIALIZED, false, false),
1397         MakeTuple(1, 1, 1, END_OF_STREAM_RESET, CS_FLUSHING, false, false),
1398         MakeTuple(1, 1, 1, END_OF_STREAM_RESET, CS_FLUSHED, false, false),
1399         MakeTuple(1, 1, 1, END_OF_STREAM_RESET, CS_RESETTING, false, false),
1400         MakeTuple(1, 1, 1, END_OF_STREAM_RESET, CS_RESET, false, false),
1401         MakeTuple(1, 1, 1, END_OF_STREAM_RESET,
1402                   static_cast<ClientState>(-1), false, false),
1403         MakeTuple(1, 1, 1, END_OF_STREAM_RESET,
1404                   static_cast<ClientState>(-10), false, false),
1405         MakeTuple(1, 1, 1, END_OF_STREAM_RESET,
1406                   static_cast<ClientState>(-100), false, false)));
1407
1408 // Test that decoding various variation works with multiple in-flight decodes.
1409 INSTANTIATE_TEST_CASE_P(
1410     DecodeVariations, VideoDecodeAcceleratorParamTest,
1411     ::testing::Values(
1412         MakeTuple(1, 1, 1, END_OF_STREAM_RESET, CS_RESET, false, false),
1413         MakeTuple(1, 10, 1, END_OF_STREAM_RESET, CS_RESET, false, false),
1414         // Tests queuing.
1415         MakeTuple(1, 15, 1, END_OF_STREAM_RESET, CS_RESET, false, false)));
1416
1417 // Find out how many concurrent decoders can go before we exhaust system
1418 // resources.
1419 INSTANTIATE_TEST_CASE_P(
1420     ResourceExhaustion, VideoDecodeAcceleratorParamTest,
1421     ::testing::Values(
1422         // +0 hack below to promote enum to int.
1423         MakeTuple(kMinSupportedNumConcurrentDecoders + 0, 1, 1,
1424                   END_OF_STREAM_RESET, CS_RESET, false, false),
1425         MakeTuple(kMinSupportedNumConcurrentDecoders + 1, 1, 1,
1426                   END_OF_STREAM_RESET, CS_RESET, false, false)));
1427
1428 // Thumbnailing test
1429 INSTANTIATE_TEST_CASE_P(
1430     Thumbnail, VideoDecodeAcceleratorParamTest,
1431     ::testing::Values(
1432         MakeTuple(1, 1, 1, END_OF_STREAM_RESET, CS_RESET, false, true)));
1433
1434 // Measure the median of the decode time when VDA::Decode is called 30 times per
1435 // second.
1436 TEST_F(VideoDecodeAcceleratorTest, TestDecodeTimeMedian) {
1437   RenderingHelperParams helper_params;
1438   helper_params.num_windows = 1;
1439   helper_params.render_as_thumbnails = false;
1440   gfx::Size frame_size(test_video_files_[0]->width,
1441                        test_video_files_[0]->height);
1442   helper_params.frame_dimensions.push_back(frame_size);
1443   helper_params.window_dimensions.push_back(frame_size);
1444   InitializeRenderingHelper(helper_params);
1445
1446   ClientStateNotification<ClientState>* note =
1447       new ClientStateNotification<ClientState>();
1448   GLRenderingVDAClient* client =
1449       new GLRenderingVDAClient(&rendering_helper_,
1450                                0,
1451                                note,
1452                                test_video_files_[0]->data_str,
1453                                1,
1454                                1,
1455                                test_video_files_[0]->reset_after_frame_num,
1456                                CS_RESET,
1457                                test_video_files_[0]->width,
1458                                test_video_files_[0]->height,
1459                                test_video_files_[0]->profile,
1460                                g_rendering_fps,
1461                                true,
1462                                std::numeric_limits<int>::max(),
1463                                kWebRtcDecodeCallsPerSecond);
1464   CreateAndStartDecoder(client, note);
1465   WaitUntilDecodeFinish(note);
1466
1467   int decode_time_median = client->decode_time_median();
1468   std::string output_string =
1469       base::StringPrintf("Decode time median: %d ms", decode_time_median);
1470   VLOG(0) << output_string;
1471   ASSERT_GT(decode_time_median, 0);
1472
1473   if (g_output_log != NULL)
1474     OutputLogFile(g_output_log, output_string);
1475
1476   rendering_loop_proxy_->DeleteSoon(FROM_HERE, client);
1477   rendering_loop_proxy_->DeleteSoon(FROM_HERE, note);
1478   WaitUntilIdle();
1479 };
1480
1481 // TODO(fischman, vrk): add more tests!  In particular:
1482 // - Test life-cycle: Seek/Stop/Pause/Play for a single decoder.
1483 // - Test alternate configurations
1484 // - Test failure conditions.
1485 // - Test frame size changes mid-stream
1486
1487 }  // namespace
1488 }  // namespace content
1489
1490 int main(int argc, char **argv) {
1491   testing::InitGoogleTest(&argc, argv);  // Removes gtest-specific args.
1492   CommandLine::Init(argc, argv);
1493
1494   // Needed to enable DVLOG through --vmodule.
1495   logging::LoggingSettings settings;
1496   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
1497   settings.dcheck_state =
1498       logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS;
1499   CHECK(logging::InitLogging(settings));
1500
1501   CommandLine* cmd_line = CommandLine::ForCurrentProcess();
1502   DCHECK(cmd_line);
1503
1504   CommandLine::SwitchMap switches = cmd_line->GetSwitches();
1505   for (CommandLine::SwitchMap::const_iterator it = switches.begin();
1506        it != switches.end(); ++it) {
1507     if (it->first == "test_video_data") {
1508       content::g_test_video_data = it->second.c_str();
1509       continue;
1510     }
1511     // TODO(wuchengli): remove frame_deliver_log after CrOS test get updated.
1512     // See http://crosreview.com/175426.
1513     if (it->first == "frame_delivery_log" || it->first == "output_log") {
1514       content::g_output_log = it->second.c_str();
1515       continue;
1516     }
1517     if (it->first == "rendering_fps") {
1518       // On Windows, CommandLine::StringType is wstring. We need to convert
1519       // it to std::string first
1520       std::string input(it->second.begin(), it->second.end());
1521       CHECK(base::StringToDouble(input, &content::g_rendering_fps));
1522       continue;
1523     }
1524     if (it->first == "disable_rendering") {
1525       content::g_disable_rendering = true;
1526       continue;
1527     }
1528     if (it->first == "v" || it->first == "vmodule")
1529       continue;
1530     LOG(FATAL) << "Unexpected switch: " << it->first << ":" << it->second;
1531   }
1532
1533   base::ShadowingAtExitManager at_exit_manager;
1534
1535   return RUN_ALL_TESTS();
1536 }