Chromium Code Reviews| 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); | |
|
scherkus (not reviewing)
2013/03/22 01:02:40
emit state
xhwang
2013/03/22 07:23:28
Done.
| |
| 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 DCHECK(decoder_) << "Reset() without successfully initialized a decoder."; | |
|
scherkus (not reviewing)
2013/03/22 01:02:40
not needed -- we'll crash
xhwang
2013/03/22 07:23:28
Done.
| |
| 94 ResetDecoder(); | |
| 95 } | |
| 96 | |
| 97 void VideoFrameStream::Stop(const base::Closure& closure) { | |
| 98 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 99 // TODO(xhwang): This is necessary. But it would be interesting to see if we | |
| 100 // ever hit. | |
|
scherkus (not reviewing)
2013/03/22 01:02:40
not sure I understand this TODO
xhwang
2013/03/22 07:23:28
Updated.
| |
| 101 DCHECK_NE(state_, STOPPED); | |
| 102 DCHECK(stop_cb_.is_null()); | |
| 103 | |
| 104 stop_cb_ = closure; | |
| 105 | |
| 106 // The stopping process will be continued after all of the following pending | |
|
scherkus (not reviewing)
2013/03/22 01:02:40
s/stopping process will be continued/stopping will
xhwang
2013/03/22 07:23:28
Done.
| |
| 107 // callbacks (if they are not null) are satisfied. | |
| 108 // TODO(xhwang): Now we cannot stop the initialization process through | |
| 109 // VideoDecoderSelector. Fix this. See: http://crbug.com/222054 | |
| 110 if (!init_cb_.is_null()) | |
| 111 return; | |
| 112 | |
| 113 // We may or may not have pending read and/or pending reset, but we'll start | |
| 114 // to stop everything regardless. | |
| 115 | |
| 116 if (decrypting_demuxer_stream_) { | |
| 117 decrypting_demuxer_stream_->Reset(base::Bind( | |
| 118 &VideoFrameStream::StopDecoder, weak_this_)); | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 if (decoder_) { | |
| 123 StopDecoder(); | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 state_ = STOPPED; | |
| 128 // BindToCurrentLoop() force posts the callback. | |
| 129 BindToCurrentLoop(closure).Run(); | |
|
scherkus (not reviewing)
2013/03/22 01:02:40
what do you think about using "message_loop_->Post
xhwang
2013/03/22 07:23:28
Done.
| |
| 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(state_ == NORMAL); | |
|
scherkus (not reviewing)
2013/03/22 01:02:40
EQ
xhwang
2013/03/22 07:23:28
Done.
| |
| 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 |