OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "media/filters/fake_video_decoder.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/location.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "media/base/bind_to_loop.h" |
| 12 #include "media/base/demuxer_stream.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 FakeVideoDecoder::FakeVideoDecoder(int decoding_delay) |
| 17 : message_loop_(base::MessageLoopProxy::current()), |
| 18 weak_factory_(this), |
| 19 decoding_delay_(decoding_delay), |
| 20 state_(UNINITIALIZED), |
| 21 demuxer_stream_(NULL) { |
| 22 DCHECK_GE(decoding_delay, 0); |
| 23 } |
| 24 |
| 25 FakeVideoDecoder::~FakeVideoDecoder() { |
| 26 DCHECK_EQ(state_, UNINITIALIZED); |
| 27 } |
| 28 |
| 29 void FakeVideoDecoder::Initialize(DemuxerStream* stream, |
| 30 const PipelineStatusCB& status_cb, |
| 31 const StatisticsCB& statistics_cb) { |
| 32 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 33 DCHECK(stream); |
| 34 DCHECK(stream->video_decoder_config().IsValidConfig()); |
| 35 DCHECK(read_cb_.IsNull()) << "No reinitialization during pending read."; |
| 36 DCHECK(reset_cb_.IsNull()) << "No reinitialization during pending reset."; |
| 37 |
| 38 weak_this_ = weak_factory_.GetWeakPtr(); |
| 39 |
| 40 demuxer_stream_ = stream; |
| 41 statistics_cb_ = statistics_cb; |
| 42 current_config_ = stream->video_decoder_config(); |
| 43 init_cb_.SetCallback(BindToCurrentLoop(status_cb)); |
| 44 |
| 45 if (!decoded_frames_.empty()) { |
| 46 DVLOG(1) << "Decoded frames dropped during reinitialization."; |
| 47 decoded_frames_.clear(); |
| 48 } |
| 49 |
| 50 state_ = NORMAL; |
| 51 init_cb_.RunOrHold(PIPELINE_OK); |
| 52 } |
| 53 |
| 54 void FakeVideoDecoder::Read(const ReadCB& read_cb) { |
| 55 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 56 DCHECK(read_cb_.IsNull()) << "Overlapping decodes are not supported."; |
| 57 DCHECK(reset_cb_.IsNull()); |
| 58 DCHECK_LE(decoded_frames_.size(), static_cast<size_t>(decoding_delay_)); |
| 59 |
| 60 read_cb_.SetCallback(BindToCurrentLoop(read_cb)); |
| 61 ReadFromDemuxerStream(); |
| 62 } |
| 63 |
| 64 void FakeVideoDecoder::Reset(const base::Closure& closure) { |
| 65 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 66 DCHECK(reset_cb_.IsNull()); |
| 67 reset_cb_.SetCallback(BindToCurrentLoop(closure)); |
| 68 |
| 69 // Defer the reset if a read is pending. |
| 70 if (!read_cb_.IsNull()) |
| 71 return; |
| 72 |
| 73 DoReset(); |
| 74 } |
| 75 |
| 76 void FakeVideoDecoder::Stop(const base::Closure& closure) { |
| 77 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 78 stop_cb_.SetCallback(BindToCurrentLoop(closure)); |
| 79 |
| 80 // Defer the reset if a read and/or a reset is pending. |
| 81 if (!read_cb_.IsNull() || !reset_cb_.IsNull()) |
| 82 return; |
| 83 |
| 84 DoStop(); |
| 85 } |
| 86 |
| 87 void FakeVideoDecoder::HoldNextInit() { |
| 88 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 89 init_cb_.HoldCallback(); |
| 90 } |
| 91 |
| 92 void FakeVideoDecoder::HoldNextRead() { |
| 93 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 94 read_cb_.HoldCallback(); |
| 95 } |
| 96 |
| 97 void FakeVideoDecoder::HoldNextReset() { |
| 98 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 99 reset_cb_.HoldCallback(); |
| 100 } |
| 101 |
| 102 void FakeVideoDecoder::HoldNextStop() { |
| 103 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 104 stop_cb_.HoldCallback(); |
| 105 } |
| 106 |
| 107 void FakeVideoDecoder::SatisfyInit() { |
| 108 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 109 DCHECK(read_cb_.IsNull()); |
| 110 DCHECK(reset_cb_.IsNull()); |
| 111 |
| 112 init_cb_.RunHeldCallback(); |
| 113 |
| 114 if (!stop_cb_.IsNull()) |
| 115 DoStop(); |
| 116 } |
| 117 |
| 118 void FakeVideoDecoder::SatisfyRead() { |
| 119 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 120 read_cb_.RunHeldCallback(); |
| 121 |
| 122 if (!reset_cb_.IsNull()) |
| 123 DoReset(); |
| 124 |
| 125 if (reset_cb_.IsNull() && !stop_cb_.IsNull()) |
| 126 DoStop(); |
| 127 } |
| 128 |
| 129 void FakeVideoDecoder::SatisfyReset() { |
| 130 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 131 DCHECK(read_cb_.IsNull()); |
| 132 reset_cb_.RunHeldCallback(); |
| 133 |
| 134 if (!stop_cb_.IsNull()) |
| 135 DoStop(); |
| 136 } |
| 137 |
| 138 void FakeVideoDecoder::SatisfyStop() { |
| 139 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 140 DCHECK(read_cb_.IsNull()); |
| 141 DCHECK(reset_cb_.IsNull()); |
| 142 stop_cb_.RunHeldCallback(); |
| 143 } |
| 144 |
| 145 void FakeVideoDecoder::ReadFromDemuxerStream() { |
| 146 DCHECK_EQ(state_, NORMAL); |
| 147 DCHECK(!read_cb_.IsNull()); |
| 148 demuxer_stream_->Read(base::Bind(&FakeVideoDecoder::BufferReady, weak_this_)); |
| 149 } |
| 150 |
| 151 void FakeVideoDecoder::BufferReady(DemuxerStream::Status status, |
| 152 const scoped_refptr<DecoderBuffer>& buffer) { |
| 153 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 154 DCHECK_EQ(state_, NORMAL); |
| 155 DCHECK(!read_cb_.IsNull()); |
| 156 DCHECK_EQ(status != DemuxerStream::kOk, !buffer) << status; |
| 157 |
| 158 if (!stop_cb_.IsNull()) { |
| 159 read_cb_.RunOrHold(kOk, scoped_refptr<VideoFrame>()); |
| 160 if (!reset_cb_.IsNull()) { |
| 161 DoReset(); |
| 162 } |
| 163 DoStop(); |
| 164 return; |
| 165 } |
| 166 |
| 167 if (status == DemuxerStream::kConfigChanged) { |
| 168 DCHECK(demuxer_stream_->video_decoder_config().IsValidConfig()); |
| 169 current_config_ = demuxer_stream_->video_decoder_config(); |
| 170 |
| 171 if (reset_cb_.IsNull()) { |
| 172 ReadFromDemuxerStream(); |
| 173 return; |
| 174 } |
| 175 } |
| 176 |
| 177 if (!reset_cb_.IsNull()) { |
| 178 read_cb_.RunOrHold(kOk, scoped_refptr<VideoFrame>()); |
| 179 DoReset(); |
| 180 return; |
| 181 } |
| 182 |
| 183 if (status == DemuxerStream::kAborted) { |
| 184 read_cb_.RunOrHold(kOk, scoped_refptr<VideoFrame>()); |
| 185 return; |
| 186 } |
| 187 |
| 188 DCHECK_EQ(status, DemuxerStream::kOk); |
| 189 |
| 190 // Make sure the decoder is always configured with the latest config. |
| 191 DCHECK(current_config_.Matches(demuxer_stream_->video_decoder_config())); |
| 192 |
| 193 if (buffer->IsEndOfStream() && decoded_frames_.empty()) { |
| 194 read_cb_.RunOrHold(kOk, VideoFrame::CreateEmptyFrame()); |
| 195 return; |
| 196 } |
| 197 |
| 198 if (!buffer->IsEndOfStream()) { |
| 199 scoped_refptr<VideoFrame> video_frame = VideoFrame::CreateColorFrame( |
| 200 current_config_.coded_size(), 0, 0, 0, buffer->GetTimestamp()); |
| 201 decoded_frames_.push_back(video_frame); |
| 202 |
| 203 if (decoded_frames_.size() <= static_cast<size_t>(decoding_delay_)) { |
| 204 ReadFromDemuxerStream(); |
| 205 return; |
| 206 } |
| 207 } |
| 208 |
| 209 scoped_refptr<VideoFrame> frame = decoded_frames_.front(); |
| 210 decoded_frames_.pop_front(); |
| 211 read_cb_.RunOrHold(kOk, frame); |
| 212 } |
| 213 |
| 214 void FakeVideoDecoder::DoReset() { |
| 215 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 216 DCHECK(read_cb_.IsNull()); |
| 217 DCHECK(!reset_cb_.IsNull()); |
| 218 |
| 219 decoded_frames_.clear(); |
| 220 reset_cb_.RunOrHold(); |
| 221 } |
| 222 |
| 223 void FakeVideoDecoder::DoStop() { |
| 224 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 225 DCHECK(read_cb_.IsNull()); |
| 226 DCHECK(reset_cb_.IsNull()); |
| 227 DCHECK(!stop_cb_.IsNull()); |
| 228 |
| 229 state_ = UNINITIALIZED; |
| 230 demuxer_stream_ = NULL; |
| 231 decoded_frames_.clear(); |
| 232 stop_cb_.RunOrHold(); |
| 233 } |
| 234 |
| 235 } // namespace media |
OLD | NEW |