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