forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_decode_accelerator_unittest.cc
1653 lines (1460 loc) · 62.1 KB
/
video_decode_accelerator_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// The bulk of this file is support code; sorry about that. Here's an overview
// to hopefully help readers of this code:
// - RenderingHelper is charged with interacting with X11/{EGL/GLES2,GLX/GL} or
// Win/EGL.
// - ClientState is an enum for the state of the decode client used by the test.
// - ClientStateNotification is a barrier abstraction that allows the test code
// to be written sequentially and wait for the decode client to see certain
// state transitions.
// - GLRenderingVDAClient is a VideoDecodeAccelerator::Client implementation
// - Finally actual TEST cases are at the bottom of this file, using the above
// infrastructure.
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include <map>
#include <memory>
#include <tuple>
#include <utility>
#include "base/at_exit.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/format_macros.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/md5.h"
#include "base/message_loop/message_loop.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringize_macros.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/launcher/unit_test_launcher.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/test_suite.h"
#include "base/threading/thread.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "gpu/command_buffer/service/gpu_preferences.h"
#include "gpu/config/gpu_driver_bug_workarounds.h"
#include "media/base/test_data_util.h"
#include "media/gpu/buildflags.h"
#include "media/gpu/fake_video_decode_accelerator.h"
#include "media/gpu/format_utils.h"
#include "media/gpu/gpu_video_decode_accelerator_factory.h"
#include "media/gpu/test/rendering_helper.h"
#include "media/gpu/test/video_accelerator_unittest_helpers.h"
#include "media/gpu/test/video_decode_accelerator_unittest_helpers.h"
#include "media/video/h264_parser.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gl/gl_image.h"
#if defined(OS_WIN)
#include "base/win/windows_version.h"
#include "media/gpu/windows/dxva_video_decode_accelerator_win.h"
#endif // defined(OS_WIN)
#if BUILDFLAG(USE_VAAPI)
#include "media/gpu/vaapi/vaapi_wrapper.h"
#endif // BUILDFLAG(USE_VAAPI)
#if defined(OS_CHROMEOS)
#include "ui/ozone/public/ozone_platform.h"
#endif // defined(OS_CHROMEOS)
namespace media {
namespace {
// Values optionally filled in from flags; see main() below.
// The syntax of multiple test videos is:
// test-video1;test-video2;test-video3
// where only the first video is required and other optional videos would be
// decoded by concurrent decoders.
// The syntax of each test-video is:
// filename:width:height:numframes:numfragments:minFPSwithRender:minFPSnoRender
// where only the first field is required. Value details:
// - |filename| must be an h264 Annex B (NAL) stream or an IVF VP8/9 stream.
// - |width| and |height| are in pixels.
// - |numframes| is the number of picture frames in the file.
// - |numfragments| NALU (h264) or frame (VP8/9) count in the stream.
// - |minFPSwithRender| and |minFPSnoRender| are minimum frames/second speeds
// expected to be achieved with and without rendering to the screen, resp.
// (the latter tests just decode speed).
// - |profile| is the VideoCodecProfile set during Initialization.
// An empty value for a numeric field means "ignore".
const base::FilePath::CharType* g_test_video_data =
// FILE_PATH_LITERAL("test-25fps.vp8:320:240:250:250:50:175:11");
FILE_PATH_LITERAL("test-25fps.h264:320:240:250:258:50:175:1");
// The file path of the test output log. This is used to communicate the test
// results to CrOS autotests. We can enable the log and specify the filename by
// the "--output_log" switch.
const base::FilePath::CharType* g_output_log = NULL;
// The value is set by the switch "--rendering_fps".
double g_rendering_fps = 60;
bool g_use_gl_renderer = true;
// The value is set by the switch "--num_play_throughs". The video will play
// the specified number of times. In different test cases, we have different
// values for |num_play_throughs|. This setting will override the value. A
// special value "0" means no override.
size_t g_num_play_throughs = 0;
// Fake decode
bool g_fake_decoder = 0;
// Test buffer import into VDA, providing buffers allocated by us, instead of
// requesting the VDA itself to allocate buffers.
bool g_test_import = false;
// This is the location of the test files. If empty, they're in the current
// working directory.
base::FilePath g_test_file_path;
// The location to output bad thumbnail image. If empty or invalid, fallback to
// the original location.
base::FilePath g_thumbnail_output_dir;
// Environment to store rendering thread.
media::test::VideoDecodeAcceleratorTestEnvironment* g_env;
constexpr size_t kMaxResetAfterFrameNum = 100;
constexpr size_t kMaxFramesToDelayReuse = 64;
const base::TimeDelta kReuseDelay = base::TimeDelta::FromSeconds(1);
// Simulate WebRTC and call VDA::Decode 30 times per second.
constexpr size_t kWebRtcDecodeCallsPerSecond = 30;
// Simulate an adjustment to a larger number of pictures to make sure the
// decoder supports an upwards adjustment.
constexpr size_t kExtraPictureBuffers = 2;
constexpr size_t kNoMidStreamReset = std::numeric_limits<size_t>::max();
const gfx::Size kThumbnailsPageSize(1600, 1200);
const gfx::Size kThumbnailSize(160, 120);
// We assert a minimal number of concurrent decoders we expect to succeed.
// Different platforms can support more concurrent decoders, so we don't assert
// failure above this.
constexpr size_t kMinSupportedNumConcurrentDecoders = 3;
// Magic constants for differentiating the reasons for NotifyResetDone being
// called.
enum ResetPoint {
// Reset() right after calling Flush() (before getting NotifyFlushDone()).
RESET_BEFORE_NOTIFY_FLUSH_DONE,
// Reset() just after calling Decode() with a fragment containing config info.
RESET_AFTER_FIRST_CONFIG_INFO,
// Reset() just after finishing Initialize().
START_OF_STREAM_RESET,
// Reset() after a specific number of Decode() are executed.
MID_STREAM_RESET,
// Reset() after NotifyFlushDone().
END_OF_STREAM_RESET,
// This is the state that Reset() by RESET_AFTER_FIRST_CONFIG_INFO
// is executed().
DONE_RESET_AFTER_FIRST_CONFIG_INFO,
};
// State of the GLRenderingVDAClient below. Order matters here as the test
// makes assumptions about it.
enum ClientState {
CS_CREATED = 0,
CS_DECODER_SET = 1,
CS_INITIALIZED = 2,
CS_FLUSHING = 3,
CS_FLUSHED = 4,
CS_RESETTING = 5,
CS_RESET = 6,
CS_ERROR = 7,
CS_DESTROYED = 8,
CS_MAX, // Must be last entry.
};
struct TestVideoFile {
explicit TestVideoFile(base::FilePath::StringType file_name)
: file_name(file_name),
width(0),
height(0),
num_frames(0),
num_fragments(0),
min_fps_render(0),
min_fps_no_render(0),
profile(VIDEO_CODEC_PROFILE_UNKNOWN),
reset_after_frame_num(std::numeric_limits<size_t>::max()) {}
base::FilePath::StringType file_name;
int width;
int height;
size_t num_frames;
size_t num_fragments;
double min_fps_render;
double min_fps_no_render;
VideoCodecProfile profile;
size_t reset_after_frame_num;
std::string data_str;
};
base::FilePath GetTestDataFile(const base::FilePath& input_file) {
if (input_file.IsAbsolute())
return input_file;
// input_file needs to be existed, otherwise base::MakeAbsoluteFilePath will
// return an empty base::FilePath.
base::FilePath abs_path =
base::MakeAbsoluteFilePath(g_test_file_path.Append(input_file));
LOG_IF(ERROR, abs_path.empty())
<< g_test_file_path.Append(input_file).value().c_str()
<< " is not an existing path.";
return abs_path;
}
// Client that can accept callbacks from a VideoDecodeAccelerator and is used by
// the TESTs below.
class GLRenderingVDAClient
: public VideoDecodeAccelerator::Client,
public base::SupportsWeakPtr<GLRenderingVDAClient> {
public:
// |window_id| the window_id of the client, which is used to identify the
// rendering area in the |rendering_helper_|.
// |num_in_flight_decodes| is the number of concurrent in-flight Decode()
// calls per decoder.
// |num_play_throughs| indicates how many times to play through the video.
// |reset_point| indicates the timing of executing Reset().
// |reset_after_frame_num| can be a frame number >=0 indicating a mid-stream
// Reset() should be done. This member argument is only meaningful and must
// not be less than 0 if |reset_point| == MID_STREAM_RESET.
// Unless |reset_point| == MID_STREAM_RESET, it must be kNoMidStreamReset.
// |delete_decoder_state| indicates when the underlying decoder should be
// Destroy()'d and deleted and can take values: N<0: delete after -N Decode()
// calls have been made, N>=0 means interpret as ClientState.
// Both |reset_after_frame_num| & |delete_decoder_state| apply only to the
// last play-through (governed by |num_play_throughs|).
// |frame_size| is the frame size of the video file.
// |profile| is video codec profile of the video file.
// |fake_decoder| indicates decoder_ would be fake_video_decode_accelerator.
// After |delay_reuse_after_frame_num| frame has been delivered, the client
// will start delaying the call to ReusePictureBuffer() for kReuseDelay.
// |decode_calls_per_second| is the number of VDA::Decode calls per second.
// If |decode_calls_per_second| > 0, |num_in_flight_decodes| must be 1.
// |render_as_thumbnails| indicates if the decoded picture will be rendered
// as thumbnails at the end of tests.
struct Config {
size_t window_id = 0;
size_t num_in_flight_decodes = 1;
size_t num_play_throughs = 1;
ResetPoint reset_point = END_OF_STREAM_RESET;
size_t reset_after_frame_num = kNoMidStreamReset;
// TODO(hiroh): Refactor as delete_decoder_state can be enum class.
// This can be set to not only ClientState, but also an integer in
// TearDownTiming test case.
int delete_decoder_state = CS_RESET;
gfx::Size frame_size;
VideoCodecProfile profile = VIDEO_CODEC_PROFILE_UNKNOWN;
bool fake_decoder = false;
size_t delay_reuse_after_frame_num = std::numeric_limits<size_t>::max();
size_t decode_calls_per_second = 0;
bool render_as_thumbnails = false;
};
// Doesn't take ownership of |rendering_helper| or |note|, which must outlive
// |*this|.
GLRenderingVDAClient(Config config,
std::string encoded_data,
RenderingHelper* rendering_helper,
ClientStateNotification<ClientState>* note);
~GLRenderingVDAClient() override;
void CreateAndStartDecoder();
// VideoDecodeAccelerator::Client implementation.
// The heart of the Client.
void ProvidePictureBuffers(uint32_t requested_num_of_buffers,
VideoPixelFormat format,
uint32_t textures_per_buffer,
const gfx::Size& dimensions,
uint32_t texture_target) override;
void DismissPictureBuffer(int32_t picture_buffer_id) override;
void PictureReady(const Picture& picture) override;
// Simple state changes.
void NotifyEndOfBitstreamBuffer(int32_t bitstream_buffer_id) override;
void NotifyFlushDone() override;
void NotifyResetDone() override;
void NotifyError(VideoDecodeAccelerator::Error error) override;
void OutputFrameDeliveryTimes(base::File* output);
// Simple getters for inspecting the state of the Client.
size_t num_done_bitstream_buffers() { return num_done_bitstream_buffers_; }
size_t num_skipped_fragments() {
return encoded_data_helper_->num_skipped_fragments();
}
size_t num_queued_fragments() { return num_queued_fragments_; }
size_t num_decoded_frames() { return num_decoded_frames_; }
double frames_per_second();
// Return the median of the decode time of all decoded frames.
base::TimeDelta decode_time_median();
bool decoder_deleted() { return !decoder_.get(); }
private:
typedef std::map<int32_t, scoped_refptr<media::test::TextureRef>>
TextureRefMap;
void SetState(ClientState new_state);
void FinishInitialization();
void ReturnPicture(int32_t picture_buffer_id);
// Delete the associated decoder helper.
void DeleteDecoder();
// Reset the associated decoder after flushing.
void ResetDecoderAfterFlush();
// Request decode of the next fragment in the encoded data.
void DecodeNextFragment();
const Config config_;
RenderingHelper* const rendering_helper_;
gfx::Size frame_size_;
size_t outstanding_decodes_;
int next_bitstream_buffer_id_;
ClientStateNotification<ClientState>* const note_;
std::unique_ptr<VideoDecodeAccelerator> decoder_;
base::WeakPtr<VideoDecodeAccelerator> weak_vda_;
std::unique_ptr<base::WeakPtrFactory<VideoDecodeAccelerator>>
weak_vda_ptr_factory_;
std::unique_ptr<GpuVideoDecodeAcceleratorFactory> vda_factory_;
size_t remaining_play_throughs_;
ResetPoint reset_point_;
ClientState state_;
size_t num_queued_fragments_;
size_t num_decoded_frames_;
size_t num_done_bitstream_buffers_;
base::TimeTicks initialize_done_ticks_;
GLenum texture_target_;
VideoPixelFormat pixel_format_;
std::vector<base::TimeTicks> frame_delivery_times_;
// A map from bitstream buffer id to the decode start time of the buffer.
std::map<int, base::TimeTicks> decode_start_time_;
// The decode time of all decoded frames.
std::vector<base::TimeDelta> decode_time_;
// A map of the textures that are currently active for the decoder, i.e.,
// have been created via AssignPictureBuffers() and not dismissed via
// DismissPictureBuffer(). The keys in the map are the IDs of the
// corresponding picture buffers, and the values are TextureRefs to the
// textures.
TextureRefMap active_textures_;
// A map of the textures that are still pending in the renderer.
// We check this to ensure all frames are rendered before entering the
// CS_RESET_State.
TextureRefMap pending_textures_;
int32_t next_picture_buffer_id_;
const std::unique_ptr<media::test::EncodedDataHelper> encoded_data_helper_;
base::WeakPtr<GLRenderingVDAClient> weak_this_;
base::WeakPtrFactory<GLRenderingVDAClient> weak_this_factory_;
DISALLOW_IMPLICIT_CONSTRUCTORS(GLRenderingVDAClient);
};
static bool DummyBindImage(uint32_t client_texture_id,
uint32_t texture_target,
const scoped_refptr<gl::GLImage>& image,
bool can_bind_to_sampler) {
return true;
}
GLRenderingVDAClient::GLRenderingVDAClient(
Config config,
std::string encoded_data,
RenderingHelper* rendering_helper,
ClientStateNotification<ClientState>* note)
: config_(std::move(config)),
rendering_helper_(rendering_helper),
frame_size_(config_.frame_size),
outstanding_decodes_(0),
next_bitstream_buffer_id_(0),
note_(note),
remaining_play_throughs_(config_.num_play_throughs),
reset_point_(config_.reset_point),
state_(CS_CREATED),
num_queued_fragments_(0),
num_decoded_frames_(0),
num_done_bitstream_buffers_(0),
texture_target_(0),
pixel_format_(PIXEL_FORMAT_UNKNOWN),
next_picture_buffer_id_(1),
encoded_data_helper_(std::make_unique<media::test::EncodedDataHelper>(
std::move(encoded_data),
config_.profile)),
weak_this_factory_(this) {
DCHECK_NE(config.profile, VIDEO_CODEC_PROFILE_UNKNOWN);
LOG_ASSERT(config_.num_in_flight_decodes > 0);
LOG_ASSERT(config_.num_play_throughs > 0);
// |num_in_flight_decodes_| is unsupported if |decode_calls_per_second_| > 0.
if (config_.decode_calls_per_second > 0)
LOG_ASSERT(1 == config_.num_in_flight_decodes);
weak_this_ = weak_this_factory_.GetWeakPtr();
if (config_.reset_point == MID_STREAM_RESET) {
EXPECT_NE(config_.reset_after_frame_num, kNoMidStreamReset)
<< "reset_ater_frame_num_ must not be kNoMidStreamReset "
<< "when reset_point = MID_STREAM_RESET";
} else {
EXPECT_EQ(config_.reset_after_frame_num, kNoMidStreamReset);
}
}
GLRenderingVDAClient::~GLRenderingVDAClient() {
DeleteDecoder(); // Clean up in case of expected error.
LOG_ASSERT(decoder_deleted());
SetState(CS_DESTROYED);
}
void GLRenderingVDAClient::CreateAndStartDecoder() {
LOG_ASSERT(decoder_deleted());
LOG_ASSERT(!decoder_.get());
VideoDecodeAccelerator::Config vda_config(config_.profile);
if (config_.fake_decoder) {
decoder_.reset(new FakeVideoDecodeAccelerator(
frame_size_, base::Bind([]() { return true; })));
LOG_ASSERT(decoder_->Initialize(vda_config, this));
} else {
if (!vda_factory_) {
if (g_use_gl_renderer) {
vda_factory_ = GpuVideoDecodeAcceleratorFactory::Create(
base::Bind(&RenderingHelper::GetGLContext,
base::Unretained(rendering_helper_)),
base::Bind([]() { return true; }), base::Bind(&DummyBindImage));
} else {
vda_factory_ = GpuVideoDecodeAcceleratorFactory::CreateWithNoGL();
}
LOG_ASSERT(vda_factory_);
}
if (g_test_import) {
vda_config.output_mode =
VideoDecodeAccelerator::Config::OutputMode::IMPORT;
}
gpu::GpuDriverBugWorkarounds workarounds;
gpu::GpuPreferences gpu_preferences;
decoder_ =
vda_factory_->CreateVDA(this, vda_config, workarounds, gpu_preferences);
}
LOG_ASSERT(decoder_) << "Failed creating a VDA";
decoder_->TryToSetupDecodeOnSeparateThread(
weak_this_, base::ThreadTaskRunnerHandle::Get());
weak_vda_ptr_factory_.reset(
new base::WeakPtrFactory<VideoDecodeAccelerator>(decoder_.get()));
weak_vda_ = weak_vda_ptr_factory_->GetWeakPtr();
SetState(CS_DECODER_SET);
FinishInitialization();
}
void GLRenderingVDAClient::ProvidePictureBuffers(
uint32_t requested_num_of_buffers,
VideoPixelFormat pixel_format,
uint32_t textures_per_buffer,
const gfx::Size& dimensions,
uint32_t texture_target) {
if (decoder_deleted())
return;
LOG_ASSERT(textures_per_buffer == 1u);
std::vector<PictureBuffer> buffers;
requested_num_of_buffers += static_cast<uint32_t>(kExtraPictureBuffers);
if (pixel_format == PIXEL_FORMAT_UNKNOWN)
pixel_format = PIXEL_FORMAT_ARGB;
LOG_ASSERT((pixel_format_ == PIXEL_FORMAT_UNKNOWN) ||
(pixel_format_ == pixel_format));
pixel_format_ = pixel_format;
frame_size_ = dimensions;
texture_target_ = texture_target;
for (uint32_t i = 0; i < requested_num_of_buffers; ++i) {
uint32_t texture_id;
base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
rendering_helper_->CreateTexture(texture_target_, &texture_id, dimensions,
&done);
done.Wait();
scoped_refptr<media::test::TextureRef> texture_ref;
base::Closure delete_texture_cb =
base::Bind(&RenderingHelper::DeleteTexture,
base::Unretained(rendering_helper_), texture_id);
if (g_test_import) {
texture_ref = media::test::TextureRef::CreatePreallocated(
texture_id, delete_texture_cb, pixel_format, dimensions);
} else {
texture_ref =
media::test::TextureRef::Create(texture_id, delete_texture_cb);
}
LOG_ASSERT(texture_ref);
int32_t picture_buffer_id = next_picture_buffer_id_++;
LOG_ASSERT(
active_textures_.insert(std::make_pair(picture_buffer_id, texture_ref))
.second);
PictureBuffer::TextureIds texture_ids(1, texture_id);
buffers.push_back(PictureBuffer(picture_buffer_id, dimensions,
PictureBuffer::TextureIds(), texture_ids,
texture_target, pixel_format));
}
decoder_->AssignPictureBuffers(buffers);
if (g_test_import) {
for (const auto& buffer : buffers) {
TextureRefMap::iterator texture_it = active_textures_.find(buffer.id());
ASSERT_NE(active_textures_.end(), texture_it);
const gfx::GpuMemoryBufferHandle& handle =
texture_it->second->ExportGpuMemoryBufferHandle();
LOG_ASSERT(!handle.is_null()) << "Failed producing GMB handle";
decoder_->ImportBufferForPicture(buffer.id(), pixel_format, handle);
}
}
}
void GLRenderingVDAClient::DismissPictureBuffer(int32_t picture_buffer_id) {
LOG_ASSERT(1U == active_textures_.erase(picture_buffer_id));
}
void GLRenderingVDAClient::PictureReady(const Picture& picture) {
// We shouldn't be getting pictures delivered after Reset has completed.
LOG_ASSERT(state_ < CS_RESET);
if (decoder_deleted())
return;
gfx::Rect visible_rect = picture.visible_rect();
if (!visible_rect.IsEmpty())
EXPECT_TRUE(gfx::Rect(frame_size_).Contains(visible_rect));
base::TimeTicks now = base::TimeTicks::Now();
frame_delivery_times_.push_back(now);
// Save the decode time of this picture.
std::map<int, base::TimeTicks>::iterator it =
decode_start_time_.find(picture.bitstream_buffer_id());
ASSERT_NE(decode_start_time_.end(), it);
decode_time_.push_back(now - it->second);
decode_start_time_.erase(it);
LOG_ASSERT(picture.bitstream_buffer_id() <= next_bitstream_buffer_id_);
++num_decoded_frames_;
// Mid-stream reset applies only to the last play-through per constructor
// comment.
if (remaining_play_throughs_ == 1 && reset_point_ == MID_STREAM_RESET &&
config_.reset_after_frame_num == num_decoded_frames_) {
decoder_->Reset();
// Re-start decoding from the beginning of the stream to avoid needing to
// know how to find I-frames and so on in this test.
encoded_data_helper_->Rewind();
}
TextureRefMap::iterator texture_it =
active_textures_.find(picture.picture_buffer_id());
ASSERT_NE(active_textures_.end(), texture_it);
scoped_refptr<VideoFrameTexture> video_frame = new VideoFrameTexture(
texture_target_, texture_it->second->texture_id(),
base::Bind(&GLRenderingVDAClient::ReturnPicture, AsWeakPtr(),
picture.picture_buffer_id()));
ASSERT_TRUE(pending_textures_.insert(*texture_it).second);
if (config_.render_as_thumbnails) {
rendering_helper_->RenderThumbnail(video_frame->texture_target(),
video_frame->texture_id());
} else {
rendering_helper_->QueueVideoFrame(config_.window_id, video_frame);
}
}
void GLRenderingVDAClient::ReturnPicture(int32_t picture_buffer_id) {
if (decoder_deleted())
return;
LOG_ASSERT(1U == pending_textures_.erase(picture_buffer_id));
if (pending_textures_.empty() && state_ == CS_RESETTING) {
SetState(CS_RESET);
DeleteDecoder();
return;
}
if (num_decoded_frames_ > config_.delay_reuse_after_frame_num) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::Bind(&VideoDecodeAccelerator::ReusePictureBuffer,
weak_vda_, picture_buffer_id),
kReuseDelay);
} else {
decoder_->ReusePictureBuffer(picture_buffer_id);
}
}
void GLRenderingVDAClient::ResetDecoderAfterFlush() {
DCHECK_GE(remaining_play_throughs_, 1u);
--remaining_play_throughs_;
// SetState(CS_RESETTING) should be called before decoder_->Reset(), because
// VDA can call NotifyFlushDone() from Reset().
// TODO(johnylin): call SetState() before all decoder Flush() and Reset().
SetState(CS_RESETTING);
// It is necessary to check decoder deleted here because it is possible to
// delete decoder in SetState() in some cases.
if (decoder_deleted())
return;
decoder_->Reset();
}
void GLRenderingVDAClient::NotifyEndOfBitstreamBuffer(
int32_t bitstream_buffer_id) {
if (decoder_deleted())
return;
// TODO(fischman): this test currently relies on this notification to make
// forward progress during a Reset(). But the VDA::Reset() API doesn't
// guarantee this, so stop relying on it (and remove the notifications from
// VaapiVideoDecodeAccelerator::FinishReset()).
LOG_ASSERT(outstanding_decodes_ != 0);
++num_done_bitstream_buffers_;
--outstanding_decodes_;
// Flush decoder after all BitstreamBuffers are processed.
if (encoded_data_helper_->ReachEndOfStream()) {
if (state_ != CS_FLUSHING) {
decoder_->Flush();
SetState(CS_FLUSHING);
if (reset_point_ == RESET_BEFORE_NOTIFY_FLUSH_DONE) {
SetState(CS_FLUSHED);
ResetDecoderAfterFlush();
}
}
} else if (config_.decode_calls_per_second == 0) {
DecodeNextFragment();
}
}
void GLRenderingVDAClient::NotifyFlushDone() {
if (decoder_deleted())
return;
if (reset_point_ == RESET_BEFORE_NOTIFY_FLUSH_DONE) {
// In ResetBeforeNotifyFlushDone case client is not necessary to wait for
// NotifyFlushDone(). But if client gets here, it should be always before
// NotifyResetDone().
ASSERT_EQ(state_, CS_RESETTING);
return;
}
SetState(CS_FLUSHED);
ResetDecoderAfterFlush();
}
void GLRenderingVDAClient::NotifyResetDone() {
if (decoder_deleted())
return;
switch (reset_point_) {
case DONE_RESET_AFTER_FIRST_CONFIG_INFO:
case MID_STREAM_RESET:
reset_point_ = END_OF_STREAM_RESET;
DecodeNextFragment();
return;
case START_OF_STREAM_RESET:
reset_point_ = END_OF_STREAM_RESET;
for (size_t i = 0; i < config_.num_in_flight_decodes; ++i)
DecodeNextFragment();
return;
case END_OF_STREAM_RESET:
case RESET_BEFORE_NOTIFY_FLUSH_DONE:
break;
case RESET_AFTER_FIRST_CONFIG_INFO:
NOTREACHED();
break;
}
if (remaining_play_throughs_) {
encoded_data_helper_->Rewind();
FinishInitialization();
return;
}
rendering_helper_->Flush(config_.window_id);
if (pending_textures_.empty()) {
SetState(CS_RESET);
DeleteDecoder();
}
}
void GLRenderingVDAClient::NotifyError(VideoDecodeAccelerator::Error error) {
SetState(CS_ERROR);
}
void GLRenderingVDAClient::OutputFrameDeliveryTimes(base::File* output) {
std::string s = base::StringPrintf("frame count: %" PRIuS "\n",
frame_delivery_times_.size());
output->WriteAtCurrentPos(s.data(), s.length());
base::TimeTicks t0 = initialize_done_ticks_;
for (size_t i = 0; i < frame_delivery_times_.size(); ++i) {
s = base::StringPrintf("frame %04" PRIuS ": %" PRId64 " us\n", i,
(frame_delivery_times_[i] - t0).InMicroseconds());
t0 = frame_delivery_times_[i];
output->WriteAtCurrentPos(s.data(), s.length());
}
}
void GLRenderingVDAClient::SetState(ClientState new_state) {
note_->Notify(new_state);
state_ = new_state;
if (!remaining_play_throughs_ && new_state == config_.delete_decoder_state) {
LOG_ASSERT(!decoder_deleted());
DeleteDecoder();
}
}
void GLRenderingVDAClient::FinishInitialization() {
SetState(CS_INITIALIZED);
initialize_done_ticks_ = base::TimeTicks::Now();
if (reset_point_ == START_OF_STREAM_RESET) {
decoder_->Reset();
return;
}
for (size_t i = 0; i < config_.num_in_flight_decodes; ++i)
DecodeNextFragment();
DCHECK_EQ(outstanding_decodes_, config_.num_in_flight_decodes);
}
void GLRenderingVDAClient::DeleteDecoder() {
if (decoder_deleted())
return;
weak_vda_ptr_factory_->InvalidateWeakPtrs();
decoder_.reset();
active_textures_.clear();
// Set state to CS_DESTROYED after decoder is deleted.
SetState(CS_DESTROYED);
}
void GLRenderingVDAClient::DecodeNextFragment() {
if (decoder_deleted())
return;
if (encoded_data_helper_->ReachEndOfStream())
return;
std::string next_fragment_bytes;
next_fragment_bytes = encoded_data_helper_->GetBytesForNextData();
size_t next_fragment_size = next_fragment_bytes.size();
if (next_fragment_size == 0)
return;
num_queued_fragments_++;
// Call Reset() just after Decode() if the fragment contains config info.
// This tests how the VDA behaves when it gets a reset request before it has
// a chance to ProvidePictureBuffers().
bool reset_here = false;
if (reset_point_ == RESET_AFTER_FIRST_CONFIG_INFO) {
reset_here = media::test::EncodedDataHelper::HasConfigInfo(
reinterpret_cast<const uint8_t*>(next_fragment_bytes.data()),
next_fragment_size, config_.profile);
// Set to DONE_RESET_AFTER_FIRST_CONFIG_INFO, to only Reset() for the first
// time.
if (reset_here)
reset_point_ = DONE_RESET_AFTER_FIRST_CONFIG_INFO;
}
// Populate the shared memory buffer w/ the fragment, duplicate its handle,
// and hand it off to the decoder.
base::SharedMemory shm;
LOG_ASSERT(shm.CreateAndMapAnonymous(next_fragment_size));
memcpy(shm.memory(), next_fragment_bytes.data(), next_fragment_size);
base::SharedMemoryHandle dup_handle = shm.handle().Duplicate();
LOG_ASSERT(dup_handle.IsValid());
// TODO(erikchen): This may leak the SharedMemoryHandle.
// https://crbug.com/640840.
BitstreamBuffer bitstream_buffer(next_bitstream_buffer_id_, dup_handle,
next_fragment_size);
decode_start_time_[next_bitstream_buffer_id_] = base::TimeTicks::Now();
// Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & 0x3FFFFFFF;
decoder_->Decode(bitstream_buffer);
++outstanding_decodes_;
if (!remaining_play_throughs_ &&
-config_.delete_decoder_state == next_bitstream_buffer_id_) {
DeleteDecoder();
}
if (reset_here) {
decoder_->Reset();
// Restart from the beginning to re-Decode() the SPS we just sent.
encoded_data_helper_->Rewind();
}
if (config_.decode_calls_per_second > 0) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&GLRenderingVDAClient::DecodeNextFragment, AsWeakPtr()),
base::TimeDelta::FromSeconds(1) / config_.decode_calls_per_second);
}
}
double GLRenderingVDAClient::frames_per_second() {
base::TimeDelta delta = frame_delivery_times_.back() - initialize_done_ticks_;
return num_decoded_frames_ / delta.InSecondsF();
}
base::TimeDelta GLRenderingVDAClient::decode_time_median() {
if (decode_time_.size() == 0)
return base::TimeDelta();
std::sort(decode_time_.begin(), decode_time_.end());
size_t index = decode_time_.size() / 2;
if (decode_time_.size() % 2 != 0)
return decode_time_[index];
return (decode_time_[index] + decode_time_[index - 1]) / 2;
}
class VideoDecodeAcceleratorTest : public ::testing::Test {
protected:
using TestFilesVector = std::vector<std::unique_ptr<TestVideoFile>>;
VideoDecodeAcceleratorTest();
void SetUp() override;
void TearDown() override;
// Parse |data| into its constituent parts, set the various output fields
// accordingly, and read in video stream. CHECK-fails on unexpected or
// missing required data. Unspecified optional fields are set to -1.
void ParseAndReadTestVideoData(base::FilePath::StringType data,
TestFilesVector* test_video_files);
// Update the parameters of |test_video_files| according to
// |num_concurrent_decoders| and |reset_point|. Ex: the expected number of
// frames should be adjusted if decoder is reset in the middle of the stream.
void UpdateTestVideoFileParams(size_t num_concurrent_decoders,
ResetPoint reset_point,
TestFilesVector* test_video_files);
void InitializeRenderingHelper(const RenderingHelperParams& helper_params);
void CreateAndStartDecoder(GLRenderingVDAClient* client,
ClientStateNotification<ClientState>* note);
// Wait until decode finishes and return the last state.
ClientState WaitUntilDecodeFinish(ClientStateNotification<ClientState>* note);
void WaitUntilIdle();
void OutputLogFile(const base::FilePath::CharType* log_path,
const std::string& content);
TestFilesVector test_video_files_;
RenderingHelper rendering_helper_;
protected:
// Must be static because this method may run after the destructor.
template <typename T>
static void Delete(T item) {
// |item| is cleared when the scope of this function is left.
}
using NotesVector =
std::vector<std::unique_ptr<ClientStateNotification<ClientState>>>;
using ClientsVector = std::vector<std::unique_ptr<GLRenderingVDAClient>>;
NotesVector notes_;
ClientsVector clients_;
private:
// Required for Thread to work. Not used otherwise.
base::ShadowingAtExitManager at_exit_manager_;
DISALLOW_COPY_AND_ASSIGN(VideoDecodeAcceleratorTest);
};
VideoDecodeAcceleratorTest::VideoDecodeAcceleratorTest() {}
void VideoDecodeAcceleratorTest::SetUp() {
ParseAndReadTestVideoData(g_test_video_data, &test_video_files_);
}
void VideoDecodeAcceleratorTest::TearDown() {
// |clients_| must be deleted first because |clients_| use |notes_|.
g_env->GetRenderingTaskRunner()->PostTask(
FROM_HERE, base::Bind(&Delete<ClientsVector>, base::Passed(&clients_)));
g_env->GetRenderingTaskRunner()->PostTask(
FROM_HERE, base::Bind(&Delete<NotesVector>, base::Passed(¬es_)));
WaitUntilIdle();
g_env->GetRenderingTaskRunner()->PostTask(
FROM_HERE,
base::Bind(&Delete<TestFilesVector>, base::Passed(&test_video_files_)));
base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
g_env->GetRenderingTaskRunner()->PostTask(
FROM_HERE, base::Bind(&RenderingHelper::UnInitialize,
base::Unretained(&rendering_helper_), &done));
done.Wait();
}
void VideoDecodeAcceleratorTest::ParseAndReadTestVideoData(
base::FilePath::StringType data,
TestFilesVector* test_video_files) {
std::vector<base::FilePath::StringType> entries =
base::SplitString(data, base::FilePath::StringType(1, ';'),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
LOG_ASSERT(entries.size() >= 1U) << data;
for (size_t index = 0; index < entries.size(); ++index) {
std::vector<base::FilePath::StringType> fields =
base::SplitString(entries[index], base::FilePath::StringType(1, ':'),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
LOG_ASSERT(fields.size() >= 1U) << entries[index];
LOG_ASSERT(fields.size() <= 8U) << entries[index];
std::unique_ptr<TestVideoFile> video_file =
std::make_unique<TestVideoFile>(fields[0]);
if (!fields[1].empty())
LOG_ASSERT(base::StringToInt(fields[1], &video_file->width));
if (!fields[2].empty())
LOG_ASSERT(base::StringToInt(fields[2], &video_file->height));
if (!fields[3].empty())
LOG_ASSERT(base::StringToSizeT(fields[3], &video_file->num_frames));
if (!fields[4].empty())
LOG_ASSERT(base::StringToSizeT(fields[4], &video_file->num_fragments));
if (!fields[5].empty()) {
std::string field(fields[5].begin(), fields[5].end());
LOG_ASSERT(base::StringToDouble(field, &video_file->min_fps_render));
}
if (!fields[6].empty()) {
std::string field(fields[5].begin(), fields[5].end());
LOG_ASSERT(base::StringToDouble(field, &video_file->min_fps_no_render));
}
// Default to H264 baseline if no profile provided.
int profile = static_cast<int>(H264PROFILE_BASELINE);
if (!fields[7].empty())
LOG_ASSERT(base::StringToInt(fields[7], &profile));
video_file->profile = static_cast<VideoCodecProfile>(profile);
// Read in the video data.
base::FilePath filepath(video_file->file_name);
LOG_ASSERT(base::ReadFileToString(GetTestDataFile(filepath),
&video_file->data_str))
<< "test_video_file: " << filepath.MaybeAsASCII();
test_video_files->push_back(std::move(video_file));
}
}
void VideoDecodeAcceleratorTest::UpdateTestVideoFileParams(
size_t num_concurrent_decoders,
ResetPoint reset_point,
TestFilesVector* test_video_files) {
for (size_t i = 0; i < test_video_files->size(); i++) {
TestVideoFile* video_file = (*test_video_files)[i].get();
if (reset_point == MID_STREAM_RESET) {
// Reset should not go beyond the last frame;
// reset in the middle of the stream for short videos.
video_file->reset_after_frame_num = kMaxResetAfterFrameNum;
if (video_file->num_frames <= video_file->reset_after_frame_num)
video_file->reset_after_frame_num = video_file->num_frames / 2;
video_file->num_frames += video_file->reset_after_frame_num;
} else {
video_file->reset_after_frame_num = kNoMidStreamReset;