Fix some potential after free errors on TestCompletionCallback
[chromium-blink-merge.git] / media / filters / fake_demuxer_stream.cc
blob1d5915c39013f912b3e9a7b1335a1bc0f36e1b0a
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.h"
12 #include "base/pickle.h"
13 #include "media/base/bind_to_loop.h"
14 #include "media/base/decoder_buffer.h"
15 #include "media/base/video_frame.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/size.h"
19 namespace media {
21 static const int kStartTimestampMs = 0;
22 static const int kDurationMs = 30;
23 static const int kStartWidth = 320;
24 static const int kStartHeight = 240;
25 static const int kWidthDelta = 4;
26 static const int kHeightDelta = 3;
27 static const char kFakeBufferHeader[] = "Fake Buffer";
29 FakeDemuxerStream::FakeDemuxerStream(int num_configs,
30 int num_buffers_in_one_config,
31 bool is_encrypted)
32 : message_loop_(base::MessageLoopProxy::current()),
33 num_configs_left_(num_configs),
34 num_buffers_in_one_config_(num_buffers_in_one_config),
35 is_encrypted_(is_encrypted),
36 num_buffers_left_in_current_config_(num_buffers_in_one_config),
37 num_buffers_returned_(0),
38 current_timestamp_(base::TimeDelta::FromMilliseconds(kStartTimestampMs)),
39 duration_(base::TimeDelta::FromMilliseconds(kDurationMs)),
40 next_coded_size_(kStartWidth, kStartHeight),
41 next_read_num_(0),
42 read_to_hold_(-1) {
43 DCHECK_GT(num_configs_left_, 0);
44 DCHECK_GT(num_buffers_in_one_config_, 0);
45 UpdateVideoDecoderConfig();
48 FakeDemuxerStream::~FakeDemuxerStream() {}
50 void FakeDemuxerStream::Read(const ReadCB& read_cb) {
51 DCHECK(message_loop_->BelongsToCurrentThread());
52 DCHECK(read_cb_.is_null());
54 read_cb_ = BindToCurrentLoop(read_cb);
56 if (read_to_hold_ == next_read_num_)
57 return;
59 DCHECK(read_to_hold_ == -1 || read_to_hold_ > next_read_num_);
60 DoRead();
63 AudioDecoderConfig FakeDemuxerStream::audio_decoder_config() {
64 DCHECK(message_loop_->BelongsToCurrentThread());
65 NOTREACHED();
66 return AudioDecoderConfig();
69 VideoDecoderConfig FakeDemuxerStream::video_decoder_config() {
70 DCHECK(message_loop_->BelongsToCurrentThread());
71 return video_decoder_config_;
74 // TODO(xhwang): Support audio if needed.
75 DemuxerStream::Type FakeDemuxerStream::type() {
76 DCHECK(message_loop_->BelongsToCurrentThread());
77 return VIDEO;
80 void FakeDemuxerStream::EnableBitstreamConverter() {
81 DCHECK(message_loop_->BelongsToCurrentThread());
84 void FakeDemuxerStream::HoldNextRead() {
85 DCHECK(message_loop_->BelongsToCurrentThread());
86 read_to_hold_ = next_read_num_;
89 void FakeDemuxerStream::HoldNextConfigChangeRead() {
90 DCHECK(message_loop_->BelongsToCurrentThread());
91 // Set |read_to_hold_| to be the next config change read.
92 read_to_hold_ = next_read_num_ + num_buffers_in_one_config_ -
93 next_read_num_ % (num_buffers_in_one_config_ + 1);
96 void FakeDemuxerStream::SatisfyRead() {
97 DCHECK(message_loop_->BelongsToCurrentThread());
98 DCHECK_EQ(read_to_hold_, next_read_num_);
99 DCHECK(!read_cb_.is_null());
101 read_to_hold_ = -1;
102 DoRead();
105 void FakeDemuxerStream::Reset() {
106 read_to_hold_ = -1;
108 if (!read_cb_.is_null())
109 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
112 void FakeDemuxerStream::UpdateVideoDecoderConfig() {
113 const gfx::Rect kVisibleRect(kStartWidth, kStartHeight);
114 video_decoder_config_.Initialize(
115 kCodecVP8, VIDEO_CODEC_PROFILE_UNKNOWN, VideoFrame::YV12,
116 next_coded_size_, kVisibleRect, next_coded_size_,
117 NULL, 0, is_encrypted_, false);
118 next_coded_size_.Enlarge(kWidthDelta, kHeightDelta);
121 void FakeDemuxerStream::DoRead() {
122 DCHECK(message_loop_->BelongsToCurrentThread());
123 DCHECK(!read_cb_.is_null());
125 next_read_num_++;
127 if (num_buffers_left_in_current_config_ == 0) {
128 // End of stream.
129 if (num_configs_left_ == 0) {
130 base::ResetAndReturn(&read_cb_).Run(kOk,
131 DecoderBuffer::CreateEOSBuffer());
132 return;
135 // Config change.
136 num_buffers_left_in_current_config_ = num_buffers_in_one_config_;
137 UpdateVideoDecoderConfig();
138 base::ResetAndReturn(&read_cb_).Run(kConfigChanged, NULL);
139 return;
142 // Prepare the next buffer.
143 Pickle pickle;
144 pickle.WriteString(kFakeBufferHeader);
145 pickle.WriteInt(video_decoder_config_.coded_size().width());
146 pickle.WriteInt(video_decoder_config_.coded_size().height());
147 pickle.WriteInt64(current_timestamp_.InMilliseconds());
149 scoped_refptr<DecoderBuffer> buffer = DecoderBuffer::CopyFrom(
150 static_cast<const uint8*>(pickle.data()), pickle.size());
152 // TODO(xhwang): Output out-of-order buffers if needed.
153 buffer->SetTimestamp(current_timestamp_);
154 buffer->SetDuration(duration_);
155 current_timestamp_ += duration_;
157 num_buffers_left_in_current_config_--;
158 if (num_buffers_left_in_current_config_ == 0)
159 num_configs_left_--;
161 num_buffers_returned_++;
162 base::ResetAndReturn(&read_cb_).Run(kOk, buffer);
165 } // namespace media