OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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/decrypting_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/decoder_buffer.h" | |
12 #include "media/base/decryptor.h" | |
13 #include "media/base/demuxer_stream.h" | |
14 #include "media/base/pipeline.h" | |
15 #include "media/base/video_decoder_config.h" | |
16 #include "media/base/video_frame.h" | |
17 | |
18 namespace media { | |
19 | |
20 DecryptingVideoDecoder::DecryptingVideoDecoder( | |
21 const MessageLoopFactoryCB& message_loop_factory_cb, | |
22 Decryptor* decryptor) | |
23 : message_loop_factory_cb_(message_loop_factory_cb), | |
24 message_loop_(NULL), | |
scherkus (not reviewing)
2012/10/02 19:45:41
nit: remove
xhwang
2012/10/03 02:15:08
Done.
| |
25 state_(kUninitialized), | |
26 decryptor_(decryptor) { | |
27 } | |
28 | |
29 void DecryptingVideoDecoder::Initialize( | |
30 const scoped_refptr<DemuxerStream>& stream, | |
31 const PipelineStatusCB& status_cb, | |
32 const StatisticsCB& statistics_cb) { | |
33 DCHECK(!message_loop_); | |
34 message_loop_ = base::ResetAndReturn(&message_loop_factory_cb_).Run(); | |
35 message_loop_->PostTask(FROM_HERE, base::Bind( | |
36 &DecryptingVideoDecoder::DoInitialize, this, | |
37 stream, status_cb, statistics_cb)); | |
38 } | |
39 | |
40 void DecryptingVideoDecoder::Read(const ReadCB& read_cb) { | |
41 // Complete operation asynchronously on different stack of execution as per | |
42 // the API contract of VideoDecoder::Read() | |
43 message_loop_->PostTask(FROM_HERE, base::Bind( | |
44 &DecryptingVideoDecoder::DoRead, this, read_cb)); | |
45 } | |
46 | |
47 void DecryptingVideoDecoder::Reset(const base::Closure& closure) { | |
48 if (!message_loop_->BelongsToCurrentThread()) { | |
49 message_loop_->PostTask(FROM_HERE, base::Bind( | |
50 &DecryptingVideoDecoder::Reset, this, closure)); | |
51 return; | |
52 } | |
53 | |
54 DCHECK_NE(state_, kUninitialized); | |
55 DCHECK(stop_cb_.is_null()); // No Reset() during pending Stop(). | |
56 DCHECK(reset_cb_.is_null()); | |
57 reset_cb_ = closure; | |
58 | |
59 decryptor_->CancelDecryptAndDecodeVideo(); | |
60 | |
61 // Reset() cannot complete if the read callback is still pending. | |
62 // Defer the resetting process in this case. The |reset_cb_| will be fired | |
63 // after the read callback is fired (see DoDecryptAndDecodeBuffer() and | |
ddorwin
2012/10/02 06:10:41
nit: Might be cleaner to use a dash instead of par
xhwang
2012/10/03 02:15:08
Done.
| |
64 // DoDeliverFrame()). | |
65 if (!read_cb_.is_null()) | |
66 return; | |
67 | |
68 DoReset(); | |
69 } | |
70 | |
71 void DecryptingVideoDecoder::Stop(const base::Closure& closure) { | |
72 if (!message_loop_->BelongsToCurrentThread()) { | |
73 message_loop_->PostTask(FROM_HERE, base::Bind( | |
74 &DecryptingVideoDecoder::Stop, this, closure)); | |
75 return; | |
76 } | |
77 | |
78 DCHECK(stop_cb_.is_null()); | |
79 stop_cb_ = closure; | |
80 | |
81 decryptor_->StopVideoDecoder(); | |
82 | |
83 // Stop() cannot complete if the read callback is still pending. | |
84 // Defer the stopping process in this case. The |stop_cb_| will be fired after | |
85 // the read callback is fired (see DoDecryptAndDecodeBuffer() and | |
86 // DoDeliverFrame()). | |
scherkus (not reviewing)
2012/10/02 19:45:41
hmm.. is it worth mentioning that a pending reset_
xhwang
2012/10/03 02:15:08
Done. Also fixed a bug that stopping during initia
| |
87 if (!read_cb_.is_null()) | |
88 return; | |
89 | |
90 DoStop(); | |
91 } | |
92 | |
93 DecryptingVideoDecoder::~DecryptingVideoDecoder() { | |
94 DCHECK_EQ(state_, kUninitialized); | |
95 } | |
96 | |
97 void DecryptingVideoDecoder::DoInitialize( | |
98 const scoped_refptr<DemuxerStream>& stream, | |
99 const PipelineStatusCB& status_cb, | |
100 const StatisticsCB& statistics_cb) { | |
101 DCHECK(message_loop_->BelongsToCurrentThread()); | |
102 DCHECK(stream); | |
103 const VideoDecoderConfig& config = stream->video_decoder_config(); | |
104 if (!config.IsValidConfig()) { | |
105 DLOG(ERROR) << "Invalid video stream config: " | |
106 << config.AsHumanReadableString(); | |
107 status_cb.Run(PIPELINE_ERROR_DECODE); | |
108 return; | |
109 } | |
110 | |
111 // DecryptingVideoDecoder only accepts potentially encrypted stream. | |
112 if (!config.is_encrypted()) { | |
113 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED); | |
114 return; | |
115 } | |
116 | |
117 DCHECK(!demuxer_stream_); | |
118 demuxer_stream_ = stream; | |
119 statistics_cb_ = statistics_cb; | |
120 | |
121 DCHECK(decryptor_); | |
122 decryptor_->InitializeVideoDecoder( | |
scherkus (not reviewing)
2012/10/02 19:45:41
style nit: move "config, base::Bind(" here then dr
xhwang
2012/10/03 02:15:08
Done.
| |
123 config, | |
124 base::Bind(&DecryptingVideoDecoder::OnDecoderInitialized, this, | |
125 status_cb)); | |
126 } | |
127 | |
128 void DecryptingVideoDecoder::OnDecoderInitialized( | |
129 const PipelineStatusCB& status_cb, | |
130 bool success) { | |
131 if (!message_loop_->BelongsToCurrentThread()) { | |
132 message_loop_->PostTask(FROM_HERE, base::Bind( | |
133 &DecryptingVideoDecoder::OnDecoderInitialized, this, | |
134 status_cb, success)); | |
135 return; | |
136 } | |
137 | |
138 DCHECK(!status_cb.is_null()); | |
139 | |
140 if (!success) { | |
141 status_cb.Run(DECODER_ERROR_NOT_SUPPORTED); | |
142 return; | |
143 } | |
144 | |
145 // Success! | |
146 state_ = kNormal; | |
147 status_cb.Run(PIPELINE_OK); | |
148 } | |
149 | |
150 void DecryptingVideoDecoder::DoRead(const ReadCB& read_cb) { | |
151 DCHECK(message_loop_->BelongsToCurrentThread()); | |
152 DCHECK(!read_cb.is_null()); | |
153 CHECK_NE(state_, kUninitialized); | |
154 CHECK(read_cb_.is_null()) << "Overlapping decodes are not supported."; | |
155 | |
156 // Return empty frames if decoding has finished. | |
157 if (state_ == kDecodeFinished) { | |
158 read_cb.Run(kOk, VideoFrame::CreateEmptyFrame()); | |
159 return; | |
160 } | |
161 | |
162 read_cb_ = read_cb; | |
163 ReadFromDemuxerStream(); | |
164 } | |
165 | |
166 void DecryptingVideoDecoder::ReadFromDemuxerStream() { | |
167 DCHECK(message_loop_->BelongsToCurrentThread()); | |
168 DCHECK_EQ(state_, kNormal); | |
169 DCHECK(!read_cb_.is_null()); | |
170 | |
171 demuxer_stream_->Read( | |
172 base::Bind(&DecryptingVideoDecoder::DecryptAndDecodeBuffer, this)); | |
173 } | |
174 | |
175 void DecryptingVideoDecoder::DecryptAndDecodeBuffer( | |
176 DemuxerStream::Status status, | |
177 const scoped_refptr<DecoderBuffer>& buffer) { | |
178 // In theory, we don't need to force post the task here, because we do a | |
179 // force task post in DeliverFrame(). Therefore, even if | |
180 // demuxer_stream_->Read() execute the read callback on the same execution | |
181 // stack we are still fine. But it looks like a force post task makes the | |
182 // logic more understandable and manageable, so why not? | |
183 message_loop_->PostTask(FROM_HERE, base::Bind( | |
184 &DecryptingVideoDecoder::DoDecryptAndDecodeBuffer, this, | |
185 status, buffer)); | |
186 } | |
187 | |
188 void DecryptingVideoDecoder::DoDecryptAndDecodeBuffer( | |
189 DemuxerStream::Status status, | |
190 const scoped_refptr<DecoderBuffer>& buffer) { | |
191 DCHECK(message_loop_->BelongsToCurrentThread()); | |
192 DCHECK_EQ(state_, kNormal); | |
193 DCHECK(!read_cb_.is_null()); | |
194 DCHECK_EQ(!!buffer, status == DemuxerStream::kOk) << status; | |
scherkus (not reviewing)
2012/10/02 19:45:41
we decided against !! style -- just compare agains
xhwang
2012/10/03 02:15:08
Done.
| |
195 | |
196 if (!stop_cb_.is_null()) { | |
197 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
198 if (!reset_cb_.is_null()) | |
199 DoReset(); | |
200 | |
201 DoStop(); | |
202 return; | |
203 } | |
204 | |
205 if (!reset_cb_.is_null()) { | |
206 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
207 DoReset(); | |
208 return; | |
209 } | |
210 | |
211 if (status == DemuxerStream::kAborted) { | |
212 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
213 return; | |
214 } | |
215 | |
216 if (status == DemuxerStream::kConfigChanged) { | |
217 // TODO(xhwang): Add config change support. | |
218 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | |
219 return; | |
220 } | |
221 | |
222 DCHECK_EQ(status, DemuxerStream::kOk); | |
223 DCHECK(buffer); | |
scherkus (not reviewing)
2012/10/02 19:45:41
dcheck not useful
xhwang
2012/10/03 02:15:08
Done.
| |
224 | |
225 decryptor_->DecryptAndDecodeVideo( | |
226 buffer, base::Bind(&DecryptingVideoDecoder::DeliverFrame, this, | |
227 buffer->GetDataSize())); | |
228 } | |
229 | |
230 void DecryptingVideoDecoder::DeliverFrame( | |
231 int buffer_size, | |
232 Decryptor::Status status, | |
233 const scoped_refptr<VideoFrame>& frame) { | |
234 // We need to force task post here because the VideoDecodeCB can be executed | |
235 // synchronously in Reset()/Stop(). Instead of using more complicated logic in | |
236 // those function to fix it, why not force task post here to make everything | |
237 // simple and clear? | |
238 message_loop_->PostTask(FROM_HERE, base::Bind( | |
239 &DecryptingVideoDecoder::DoDeliverFrame, this, | |
240 buffer_size, status, frame)); | |
241 } | |
242 | |
243 void DecryptingVideoDecoder::DoDeliverFrame( | |
244 int buffer_size, | |
245 Decryptor::Status status, | |
246 const scoped_refptr<VideoFrame>& frame) { | |
247 DCHECK(message_loop_->BelongsToCurrentThread()); | |
248 DCHECK_EQ(state_, kNormal); | |
249 DCHECK(!read_cb_.is_null()); | |
250 | |
251 if (!stop_cb_.is_null()) { | |
252 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
253 if (!reset_cb_.is_null()) | |
254 DoReset(); | |
255 | |
256 DoStop(); | |
257 return; | |
258 } | |
259 | |
260 if (!reset_cb_.is_null()) { | |
261 base::ResetAndReturn(&read_cb_).Run(kOk, NULL); | |
262 DoReset(); | |
263 return; | |
264 } | |
265 | |
266 if (status == Decryptor::kNoKey || status == Decryptor::kError) { | |
267 DCHECK(!frame); | |
268 state_ = kDecodeFinished; | |
269 base::ResetAndReturn(&read_cb_).Run(kDecodeError, NULL); | |
270 return; | |
271 } | |
272 | |
273 // The buffer has been accepted by the decoder, let's report statistics. | |
274 if (buffer_size) { | |
275 PipelineStatistics statistics; | |
276 statistics.video_bytes_decoded = buffer_size; | |
277 statistics_cb_.Run(statistics); | |
278 } | |
279 | |
280 if (status == Decryptor::kNeedMoreData) { | |
281 DCHECK(!frame); | |
282 ReadFromDemuxerStream(); | |
283 return; | |
284 } | |
285 | |
286 DCHECK_EQ(status, Decryptor::kSuccess); | |
287 DCHECK(frame); | |
scherkus (not reviewing)
2012/10/02 19:45:41
dcheck not useful
xhwang
2012/10/03 02:15:08
Done.
| |
288 if (frame->IsEndOfStream()) | |
289 state_ = kDecodeFinished; | |
290 | |
291 base::ResetAndReturn(&read_cb_).Run(kOk, frame); | |
292 } | |
293 | |
294 void DecryptingVideoDecoder::DoReset() { | |
295 DCHECK(read_cb_.is_null()); | |
296 DCHECK_NE(state_, kUninitialized); | |
297 state_ = kNormal; | |
298 base::ResetAndReturn(&reset_cb_).Run(); | |
299 } | |
300 | |
301 void DecryptingVideoDecoder::DoStop() { | |
302 DCHECK(read_cb_.is_null()); | |
303 state_ = kUninitialized; | |
304 base::ResetAndReturn(&stop_cb_).Run(); | |
305 } | |
306 | |
307 } // namespace media | |
OLD | NEW |