Migrate away from the PrefMetricsService-based device ID in PrefHashCalculator.
[chromium-blink-merge.git] / media / filters / fake_demuxer_stream.cc
blob941778c576e2445d85ff5d35c374b4228a17e508
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/filters/fake_demuxer_stream.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "media/base/bind_to_current_loop.h"
13 #include "media/base/decoder_buffer.h"
14 #include "media/base/test_helpers.h"
15 #include "media/base/video_frame.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/size.h"
19 namespace media {
21 const int kStartTimestampMs = 0;
22 const int kDurationMs = 30;
23 const int kStartWidth = 320;
24 const int kStartHeight = 240;
25 const int kWidthDelta = 4;
26 const int kHeightDelta = 3;
27 const uint8 kKeyId[] = { 0x00, 0x01, 0x02, 0x03 };
28 const uint8 kIv[] = {
29 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
33 FakeDemuxerStream::FakeDemuxerStream(int num_configs,
34 int num_buffers_in_one_config,
35 bool is_encrypted)
36 : task_runner_(base::MessageLoopProxy::current()),
37 num_configs_(num_configs),
38 num_buffers_in_one_config_(num_buffers_in_one_config),
39 config_changes_(num_configs > 1),
40 is_encrypted_(is_encrypted),
41 read_to_hold_(-1) {
42 DCHECK_GT(num_configs, 0);
43 DCHECK_GT(num_buffers_in_one_config, 0);
44 Initialize();
45 UpdateVideoDecoderConfig();
48 FakeDemuxerStream::~FakeDemuxerStream() {}
50 void FakeDemuxerStream::Initialize() {
51 DCHECK_EQ(-1, read_to_hold_);
52 num_configs_left_ = num_configs_;
53 num_buffers_left_in_current_config_ = num_buffers_in_one_config_;
54 num_buffers_returned_ = 0;
55 current_timestamp_ = base::TimeDelta::FromMilliseconds(kStartTimestampMs);
56 duration_ = base::TimeDelta::FromMilliseconds(kDurationMs);
57 splice_timestamp_ = kNoTimestamp();
58 next_coded_size_ = gfx::Size(kStartWidth, kStartHeight);
59 next_read_num_ = 0;
62 void FakeDemuxerStream::Read(const ReadCB& read_cb) {
63 DCHECK(task_runner_->BelongsToCurrentThread());
64 DCHECK(read_cb_.is_null());
66 read_cb_ = BindToCurrentLoop(read_cb);
68 if (read_to_hold_ == next_read_num_)
69 return;
71 DCHECK(read_to_hold_ == -1 || read_to_hold_ > next_read_num_);
72 DoRead();
75 AudioDecoderConfig FakeDemuxerStream::audio_decoder_config() {
76 DCHECK(task_runner_->BelongsToCurrentThread());
77 NOTREACHED();
78 return AudioDecoderConfig();
81 VideoDecoderConfig FakeDemuxerStream::video_decoder_config() {
82 DCHECK(task_runner_->BelongsToCurrentThread());
83 return video_decoder_config_;
86 // TODO(xhwang): Support audio if needed.
87 DemuxerStream::Type FakeDemuxerStream::type() {
88 DCHECK(task_runner_->BelongsToCurrentThread());
89 return VIDEO;
92 void FakeDemuxerStream::EnableBitstreamConverter() {
93 DCHECK(task_runner_->BelongsToCurrentThread());
96 bool FakeDemuxerStream::SupportsConfigChanges() {
97 return config_changes_;
100 VideoRotation FakeDemuxerStream::video_rotation() {
101 return VIDEO_ROTATION_0;
104 void FakeDemuxerStream::HoldNextRead() {
105 DCHECK(task_runner_->BelongsToCurrentThread());
106 read_to_hold_ = next_read_num_;
109 void FakeDemuxerStream::HoldNextConfigChangeRead() {
110 DCHECK(task_runner_->BelongsToCurrentThread());
111 // Set |read_to_hold_| to be the next config change read.
112 read_to_hold_ = next_read_num_ + num_buffers_in_one_config_ -
113 next_read_num_ % (num_buffers_in_one_config_ + 1);
116 void FakeDemuxerStream::SatisfyRead() {
117 DCHECK(task_runner_->BelongsToCurrentThread());
118 DCHECK_EQ(read_to_hold_, next_read_num_);
119 DCHECK(!read_cb_.is_null());
121 read_to_hold_ = -1;
122 DoRead();
125 void FakeDemuxerStream::SatisfyReadAndHoldNext() {
126 DCHECK(task_runner_->BelongsToCurrentThread());
127 DCHECK_EQ(read_to_hold_, next_read_num_);
128 DCHECK(!read_cb_.is_null());
130 ++read_to_hold_;
131 DoRead();
134 void FakeDemuxerStream::Reset() {
135 read_to_hold_ = -1;
137 if (!read_cb_.is_null())
138 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
141 void FakeDemuxerStream::SeekToStart() {
142 Reset();
143 Initialize();
146 void FakeDemuxerStream::UpdateVideoDecoderConfig() {
147 const gfx::Rect kVisibleRect(kStartWidth, kStartHeight);
148 video_decoder_config_.Initialize(
149 kCodecVP8, VIDEO_CODEC_PROFILE_UNKNOWN, VideoFrame::YV12,
150 next_coded_size_, kVisibleRect, next_coded_size_,
151 NULL, 0, is_encrypted_, false);
152 next_coded_size_.Enlarge(kWidthDelta, kHeightDelta);
155 void FakeDemuxerStream::DoRead() {
156 DCHECK(task_runner_->BelongsToCurrentThread());
157 DCHECK(!read_cb_.is_null());
159 next_read_num_++;
161 if (num_buffers_left_in_current_config_ == 0) {
162 // End of stream.
163 if (num_configs_left_ == 0) {
164 base::ResetAndReturn(&read_cb_).Run(kOk,
165 DecoderBuffer::CreateEOSBuffer());
166 return;
169 // Config change.
170 num_buffers_left_in_current_config_ = num_buffers_in_one_config_;
171 UpdateVideoDecoderConfig();
172 base::ResetAndReturn(&read_cb_).Run(kConfigChanged, NULL);
173 return;
176 scoped_refptr<DecoderBuffer> buffer = CreateFakeVideoBufferForTest(
177 video_decoder_config_, current_timestamp_, duration_);
179 // TODO(xhwang): Output out-of-order buffers if needed.
180 if (is_encrypted_) {
181 buffer->set_decrypt_config(scoped_ptr<DecryptConfig>(
182 new DecryptConfig(std::string(kKeyId, kKeyId + arraysize(kKeyId)),
183 std::string(kIv, kIv + arraysize(kIv)),
184 std::vector<SubsampleEntry>())));
186 buffer->set_timestamp(current_timestamp_);
187 buffer->set_duration(duration_);
188 buffer->set_splice_timestamp(splice_timestamp_);
189 current_timestamp_ += duration_;
191 num_buffers_left_in_current_config_--;
192 if (num_buffers_left_in_current_config_ == 0)
193 num_configs_left_--;
195 num_buffers_returned_++;
196 base::ResetAndReturn(&read_cb_).Run(kOk, buffer);
199 } // namespace media