1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/base/fake_demuxer_stream.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "media/base/bind_to_current_loop.h"
14 #include "media/base/decoder_buffer.h"
15 #include "media/base/test_helpers.h"
16 #include "media/base/timestamp_constants.h"
17 #include "media/base/video_frame.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/size.h"
23 const int kStartTimestampMs
= 0;
24 const int kDurationMs
= 30;
25 const int kStartWidth
= 320;
26 const int kStartHeight
= 240;
27 const int kWidthDelta
= 4;
28 const int kHeightDelta
= 3;
29 const uint8 kKeyId
[] = { 0x00, 0x01, 0x02, 0x03 };
31 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
35 FakeDemuxerStream::FakeDemuxerStream(int num_configs
,
36 int num_buffers_in_one_config
,
38 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
39 num_configs_(num_configs
),
40 num_buffers_in_one_config_(num_buffers_in_one_config
),
41 config_changes_(num_configs
> 1),
42 is_encrypted_(is_encrypted
),
44 DCHECK_GT(num_configs
, 0);
45 DCHECK_GT(num_buffers_in_one_config
, 0);
47 UpdateVideoDecoderConfig();
50 FakeDemuxerStream::~FakeDemuxerStream() {}
52 void FakeDemuxerStream::Initialize() {
53 DCHECK_EQ(-1, read_to_hold_
);
54 num_configs_left_
= num_configs_
;
55 num_buffers_left_in_current_config_
= num_buffers_in_one_config_
;
56 num_buffers_returned_
= 0;
57 current_timestamp_
= base::TimeDelta::FromMilliseconds(kStartTimestampMs
);
58 duration_
= base::TimeDelta::FromMilliseconds(kDurationMs
);
59 splice_timestamp_
= kNoTimestamp();
60 next_coded_size_
= gfx::Size(kStartWidth
, kStartHeight
);
64 void FakeDemuxerStream::Read(const ReadCB
& read_cb
) {
65 DCHECK(task_runner_
->BelongsToCurrentThread());
66 DCHECK(read_cb_
.is_null());
68 read_cb_
= BindToCurrentLoop(read_cb
);
70 if (read_to_hold_
== next_read_num_
)
73 DCHECK(read_to_hold_
== -1 || read_to_hold_
> next_read_num_
);
77 AudioDecoderConfig
FakeDemuxerStream::audio_decoder_config() {
78 DCHECK(task_runner_
->BelongsToCurrentThread());
80 return AudioDecoderConfig();
83 VideoDecoderConfig
FakeDemuxerStream::video_decoder_config() {
84 DCHECK(task_runner_
->BelongsToCurrentThread());
85 return video_decoder_config_
;
88 // TODO(xhwang): Support audio if needed.
89 DemuxerStream::Type
FakeDemuxerStream::type() const {
90 DCHECK(task_runner_
->BelongsToCurrentThread());
94 bool FakeDemuxerStream::SupportsConfigChanges() {
95 return config_changes_
;
98 VideoRotation
FakeDemuxerStream::video_rotation() {
99 return VIDEO_ROTATION_0
;
102 void FakeDemuxerStream::HoldNextRead() {
103 DCHECK(task_runner_
->BelongsToCurrentThread());
104 read_to_hold_
= next_read_num_
;
107 void FakeDemuxerStream::HoldNextConfigChangeRead() {
108 DCHECK(task_runner_
->BelongsToCurrentThread());
109 // Set |read_to_hold_| to be the next config change read.
110 read_to_hold_
= next_read_num_
+ num_buffers_in_one_config_
-
111 next_read_num_
% (num_buffers_in_one_config_
+ 1);
114 void FakeDemuxerStream::SatisfyRead() {
115 DCHECK(task_runner_
->BelongsToCurrentThread());
116 DCHECK_EQ(read_to_hold_
, next_read_num_
);
117 DCHECK(!read_cb_
.is_null());
123 void FakeDemuxerStream::SatisfyReadAndHoldNext() {
124 DCHECK(task_runner_
->BelongsToCurrentThread());
125 DCHECK_EQ(read_to_hold_
, next_read_num_
);
126 DCHECK(!read_cb_
.is_null());
132 void FakeDemuxerStream::Reset() {
135 if (!read_cb_
.is_null())
136 base::ResetAndReturn(&read_cb_
).Run(kAborted
, NULL
);
139 void FakeDemuxerStream::SeekToStart() {
144 void FakeDemuxerStream::UpdateVideoDecoderConfig() {
145 const gfx::Rect
kVisibleRect(kStartWidth
, kStartHeight
);
146 video_decoder_config_
.Initialize(kCodecVP8
, VIDEO_CODEC_PROFILE_UNKNOWN
,
147 PIXEL_FORMAT_YV12
, COLOR_SPACE_UNSPECIFIED
,
148 next_coded_size_
, kVisibleRect
,
149 next_coded_size_
, NULL
, 0, is_encrypted_
);
150 next_coded_size_
.Enlarge(kWidthDelta
, kHeightDelta
);
153 void FakeDemuxerStream::DoRead() {
154 DCHECK(task_runner_
->BelongsToCurrentThread());
155 DCHECK(!read_cb_
.is_null());
159 if (num_buffers_left_in_current_config_
== 0) {
161 if (num_configs_left_
== 0) {
162 base::ResetAndReturn(&read_cb_
).Run(kOk
,
163 DecoderBuffer::CreateEOSBuffer());
168 num_buffers_left_in_current_config_
= num_buffers_in_one_config_
;
169 UpdateVideoDecoderConfig();
170 base::ResetAndReturn(&read_cb_
).Run(kConfigChanged
, NULL
);
174 scoped_refptr
<DecoderBuffer
> buffer
= CreateFakeVideoBufferForTest(
175 video_decoder_config_
, current_timestamp_
, duration_
);
177 // TODO(xhwang): Output out-of-order buffers if needed.
179 buffer
->set_decrypt_config(scoped_ptr
<DecryptConfig
>(
180 new DecryptConfig(std::string(kKeyId
, kKeyId
+ arraysize(kKeyId
)),
181 std::string(kIv
, kIv
+ arraysize(kIv
)),
182 std::vector
<SubsampleEntry
>())));
184 buffer
->set_timestamp(current_timestamp_
);
185 buffer
->set_duration(duration_
);
186 buffer
->set_splice_timestamp(splice_timestamp_
);
187 current_timestamp_
+= duration_
;
189 num_buffers_left_in_current_config_
--;
190 if (num_buffers_left_in_current_config_
== 0)
193 num_buffers_returned_
++;
194 base::ResetAndReturn(&read_cb_
).Run(kOk
, buffer
);
197 FakeDemuxerStreamProvider::FakeDemuxerStreamProvider(
198 int num_video_configs
,
199 int num_video_buffers_in_one_config
,
200 bool is_video_encrypted
)
201 : fake_video_stream_(num_video_configs
,
202 num_video_buffers_in_one_config
,
203 is_video_encrypted
) {
206 FakeDemuxerStreamProvider::~FakeDemuxerStreamProvider() {
209 DemuxerStream
* FakeDemuxerStreamProvider::GetStream(DemuxerStream::Type type
) {
210 if (type
== DemuxerStream::Type::AUDIO
)
212 return &fake_video_stream_
;