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) << 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 // Check if we ever call Stop() twice. | |
|
scherkus (not reviewing)
2013/03/22 17:23:19
this comment isn't very useful (the DCHECK makes t
xhwang
2013/03/22 20:43:56
Done.
| |
| 99 DCHECK_NE(state_, STOPPED); | |
| 100 DCHECK(stop_cb_.is_null()); | |
| 101 | |
| 102 stop_cb_ = closure; | |
| 103 | |
| 104 // The stopping will continue after all of the following pending callbacks | |
| 105 // (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 message_loop_->PostTask(FROM_HERE, base::ResetAndReturn(&stop_cb_)); | |
| 127 } | |
| 128 | |
| 129 void VideoFrameStream::OnDecoderSelected( | |
| 130 scoped_ptr<VideoDecoderSelector> decoder_selector, | |
| 131 const scoped_refptr<VideoDecoder>& selected_decoder, | |
| 132 const scoped_refptr<DecryptingDemuxerStream>& decrypting_demuxer_stream) { | |
| 133 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 134 DCHECK_EQ(state_, UNINITIALIZED); | |
| 135 DCHECK(!init_cb_.is_null()); | |
| 136 | |
| 137 if (!selected_decoder) { | |
| 138 state_ = UNINITIALIZED; | |
| 139 base::ResetAndReturn(&init_cb_).Run(false, false); | |
| 140 } else { | |
| 141 decoder_ = selected_decoder; | |
| 142 decrypting_demuxer_stream_ = decrypting_demuxer_stream; | |
| 143 state_ = NORMAL; | |
| 144 base::ResetAndReturn(&init_cb_).Run(true, decoder_->HasAlpha()); | |
| 145 } | |
| 146 | |
| 147 // Stop() called during initialization. | |
| 148 if (!stop_cb_.is_null()) { | |
| 149 Stop(base::ResetAndReturn(&stop_cb_)); | |
| 150 return; | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 void VideoFrameStream::OnFrameRead(const VideoDecoder::Status status, | |
| 155 const scoped_refptr<VideoFrame>& frame) { | |
| 156 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 157 DCHECK_EQ(state_, NORMAL); | |
| 158 DCHECK(!read_cb_.is_null()); | |
| 159 | |
| 160 base::ResetAndReturn(&read_cb_).Run(status, frame); | |
| 161 } | |
| 162 | |
| 163 void VideoFrameStream::ResetDecoder() { | |
| 164 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 165 DCHECK_EQ(state_, NORMAL); | |
| 166 DCHECK(!reset_cb_.is_null()); | |
| 167 | |
| 168 decoder_->Reset(base::Bind(&VideoFrameStream::OnDecoderReset, weak_this_)); | |
| 169 } | |
| 170 | |
| 171 void VideoFrameStream::OnDecoderReset() { | |
| 172 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 173 DCHECK_EQ(state_, NORMAL); | |
| 174 // If Reset() was called during pending read, read callback should be fired | |
| 175 // before the reset callback is fired. | |
| 176 DCHECK(read_cb_.is_null()); | |
| 177 DCHECK(!reset_cb_.is_null()); | |
| 178 | |
| 179 base::ResetAndReturn(&reset_cb_).Run(); | |
| 180 } | |
| 181 | |
| 182 void VideoFrameStream::StopDecoder() { | |
| 183 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 184 DCHECK_EQ(state_, NORMAL); | |
| 185 DCHECK(!stop_cb_.is_null()); | |
| 186 | |
| 187 decoder_->Stop(base::Bind(&VideoFrameStream::OnDecoderStopped, weak_this_)); | |
| 188 } | |
| 189 | |
| 190 void VideoFrameStream::OnDecoderStopped() { | |
| 191 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 192 DCHECK_EQ(state_, NORMAL); | |
| 193 // If Stop() was called during pending read/reset, read/reset callback should | |
| 194 // be fired before the stop callback is fired. | |
| 195 DCHECK(read_cb_.is_null()); | |
| 196 DCHECK(reset_cb_.is_null()); | |
| 197 DCHECK(!stop_cb_.is_null()); | |
| 198 | |
| 199 state_ = STOPPED; | |
| 200 base::ResetAndReturn(&stop_cb_).Run(); | |
| 201 } | |
| 202 | |
| 203 } // namespace media | |
| OLD | NEW |