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/video_frame_stream.h" |
| 6 |
| 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_proxy.h" |
| 12 #include "media/base/bind_to_loop.h" |
| 13 #include "media/base/demuxer_stream.h" |
| 14 #include "media/base/video_decoder_config.h" |
| 15 #include "media/filters/decrypting_demuxer_stream.h" |
| 16 #include "media/filters/video_decoder_selector.h" |
| 17 |
| 18 namespace media { |
| 19 |
| 20 VideoFrameStream::VideoFrameStream( |
| 21 const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| 22 const SetDecryptorReadyCB& set_decryptor_ready_cb) |
| 23 : message_loop_(message_loop), |
| 24 weak_factory_(this), |
| 25 state_(UNINITIALIZED), |
| 26 set_decryptor_ready_cb_(set_decryptor_ready_cb) { |
| 27 } |
| 28 |
| 29 VideoFrameStream::~VideoFrameStream() { |
| 30 DCHECK(state_ == UNINITIALIZED || state_ == STOPPED) << state_; |
| 31 } |
| 32 |
| 33 void VideoFrameStream::Initialize(const scoped_refptr<DemuxerStream>& stream, |
| 34 const VideoDecoderList& decoders, |
| 35 const StatisticsCB& statistics_cb, |
| 36 const InitCB& init_cb) { |
| 37 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 38 DCHECK_EQ(state_, UNINITIALIZED); |
| 39 |
| 40 weak_this_ = weak_factory_.GetWeakPtr(); |
| 41 |
| 42 DCHECK(init_cb_.is_null()); |
| 43 DCHECK(!init_cb.is_null()); |
| 44 init_cb_ = init_cb; |
| 45 |
| 46 scoped_ptr<VideoDecoderSelector> decoder_selector( |
| 47 new VideoDecoderSelector(message_loop_, |
| 48 decoders, |
| 49 set_decryptor_ready_cb_)); |
| 50 |
| 51 // To avoid calling |decoder_selector| methods and passing ownership of |
| 52 // |decoder_selector| in the same line. |
| 53 VideoDecoderSelector* decoder_selector_ptr = decoder_selector.get(); |
| 54 |
| 55 decoder_selector_ptr->SelectVideoDecoder( |
| 56 stream, |
| 57 statistics_cb, |
| 58 base::Bind(&VideoFrameStream::OnDecoderSelected, weak_this_, |
| 59 base::Passed(&decoder_selector))); |
| 60 } |
| 61 |
| 62 void VideoFrameStream::ReadFrame(const VideoDecoder::ReadCB& read_cb) { |
| 63 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 64 DCHECK_EQ(state_, NORMAL); |
| 65 // No two reads in the flight at any time. |
| 66 DCHECK(read_cb_.is_null()); |
| 67 // No read during resetting or stopping process. |
| 68 DCHECK(reset_cb_.is_null()); |
| 69 DCHECK(stop_cb_.is_null()); |
| 70 |
| 71 read_cb_ = read_cb; |
| 72 |
| 73 decoder_->Read(base::Bind(&VideoFrameStream::OnFrameRead, weak_this_)); |
| 74 } |
| 75 |
| 76 void VideoFrameStream::Reset(const base::Closure& closure) { |
| 77 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 78 DCHECK_EQ(state_, NORMAL); |
| 79 DCHECK(reset_cb_.is_null()); |
| 80 DCHECK(stop_cb_.is_null()); |
| 81 |
| 82 reset_cb_ = closure; |
| 83 |
| 84 // We may or may not have pending read, but we'll start to reset everything |
| 85 // regardless. |
| 86 |
| 87 if (decrypting_demuxer_stream_) { |
| 88 decrypting_demuxer_stream_->Reset(base::Bind( |
| 89 &VideoFrameStream::ResetDecoder, weak_this_)); |
| 90 return; |
| 91 } |
| 92 |
| 93 ResetDecoder(); |
| 94 } |
| 95 |
| 96 void VideoFrameStream::Stop(const base::Closure& closure) { |
| 97 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 98 DCHECK_NE(state_, STOPPED); |
| 99 DCHECK(stop_cb_.is_null()); |
| 100 |
| 101 stop_cb_ = closure; |
| 102 |
| 103 // The stopping will continue after all of the following pending callbacks |
| 104 // (if they are not null) are satisfied. |
| 105 // TODO(xhwang): Now we cannot stop the initialization process through |
| 106 // VideoDecoderSelector. Fix this. See: http://crbug.com/222054 |
| 107 if (!init_cb_.is_null()) |
| 108 return; |
| 109 |
| 110 // We may or may not have pending read and/or pending reset, but we'll start |
| 111 // to stop everything regardless. |
| 112 |
| 113 if (decrypting_demuxer_stream_) { |
| 114 decrypting_demuxer_stream_->Reset(base::Bind( |
| 115 &VideoFrameStream::StopDecoder, weak_this_)); |
| 116 return; |
| 117 } |
| 118 |
| 119 if (decoder_) { |
| 120 StopDecoder(); |
| 121 return; |
| 122 } |
| 123 |
| 124 state_ = STOPPED; |
| 125 message_loop_->PostTask(FROM_HERE, base::ResetAndReturn(&stop_cb_)); |
| 126 } |
| 127 |
| 128 bool VideoFrameStream::HasOutputFrameAvailable() const { |
| 129 return decoder_->HasOutputFrameAvailable(); |
| 130 } |
| 131 |
| 132 void VideoFrameStream::OnDecoderSelected( |
| 133 scoped_ptr<VideoDecoderSelector> decoder_selector, |
| 134 const scoped_refptr<VideoDecoder>& selected_decoder, |
| 135 const scoped_refptr<DecryptingDemuxerStream>& decrypting_demuxer_stream) { |
| 136 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 137 DCHECK_EQ(state_, UNINITIALIZED); |
| 138 DCHECK(!init_cb_.is_null()); |
| 139 |
| 140 if (!selected_decoder) { |
| 141 state_ = UNINITIALIZED; |
| 142 base::ResetAndReturn(&init_cb_).Run(false, false); |
| 143 } else { |
| 144 decoder_ = selected_decoder; |
| 145 decrypting_demuxer_stream_ = decrypting_demuxer_stream; |
| 146 state_ = NORMAL; |
| 147 base::ResetAndReturn(&init_cb_).Run(true, decoder_->HasAlpha()); |
| 148 } |
| 149 |
| 150 // Stop() called during initialization. |
| 151 if (!stop_cb_.is_null()) { |
| 152 Stop(base::ResetAndReturn(&stop_cb_)); |
| 153 return; |
| 154 } |
| 155 } |
| 156 |
| 157 void VideoFrameStream::OnFrameRead(const VideoDecoder::Status status, |
| 158 const scoped_refptr<VideoFrame>& frame) { |
| 159 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 160 DCHECK_EQ(state_, NORMAL); |
| 161 DCHECK(!read_cb_.is_null()); |
| 162 |
| 163 base::ResetAndReturn(&read_cb_).Run(status, frame); |
| 164 } |
| 165 |
| 166 void VideoFrameStream::ResetDecoder() { |
| 167 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 168 DCHECK_EQ(state_, NORMAL); |
| 169 DCHECK(!reset_cb_.is_null()); |
| 170 |
| 171 decoder_->Reset(base::Bind(&VideoFrameStream::OnDecoderReset, weak_this_)); |
| 172 } |
| 173 |
| 174 void VideoFrameStream::OnDecoderReset() { |
| 175 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 176 DCHECK_EQ(state_, NORMAL); |
| 177 // If Reset() was called during pending read, read callback should be fired |
| 178 // before the reset callback is fired. |
| 179 DCHECK(read_cb_.is_null()); |
| 180 DCHECK(!reset_cb_.is_null()); |
| 181 |
| 182 base::ResetAndReturn(&reset_cb_).Run(); |
| 183 } |
| 184 |
| 185 void VideoFrameStream::StopDecoder() { |
| 186 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 187 DCHECK_EQ(state_, NORMAL); |
| 188 DCHECK(!stop_cb_.is_null()); |
| 189 |
| 190 decoder_->Stop(base::Bind(&VideoFrameStream::OnDecoderStopped, weak_this_)); |
| 191 } |
| 192 |
| 193 void VideoFrameStream::OnDecoderStopped() { |
| 194 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 195 DCHECK_EQ(state_, NORMAL); |
| 196 // If Stop() was called during pending read/reset, read/reset callback should |
| 197 // be fired before the stop callback is fired. |
| 198 DCHECK(read_cb_.is_null()); |
| 199 DCHECK(reset_cb_.is_null()); |
| 200 DCHECK(!stop_cb_.is_null()); |
| 201 |
| 202 state_ = STOPPED; |
| 203 base::ResetAndReturn(&stop_cb_).Run(); |
| 204 } |
| 205 |
| 206 } // namespace media |
OLD | NEW |