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/base/text_renderer.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback_helpers.h" | |
9 #include "base/logging.h" | |
10 #include "base/message_loop/message_loop_proxy.h" | |
11 #include "base/stl_util.h" | |
12 #include "media/base/bind_to_loop.h" | |
13 #include "media/base/decoder_buffer.h" | |
14 #include "media/base/demuxer.h" | |
15 #include "media/base/demuxer_stream.h" | |
16 #include "media/base/text_cue.h" | |
17 | |
18 namespace media { | |
19 | |
20 TextRenderer::TextRenderer( | |
21 const scoped_refptr<base::MessageLoopProxy>& message_loop, | |
22 const AddTextTrackCB& add_text_track_cb) | |
23 : message_loop_(message_loop), | |
24 weak_factory_(this), | |
25 add_text_track_cb_(add_text_track_cb), | |
26 state_(kUninitialized), | |
27 pending_read_count_(0), | |
28 pending_eos_count_(0) { | |
29 } | |
30 | |
31 TextRenderer::~TextRenderer() { | |
32 DCHECK(state_ == kUninitialized || | |
33 state_ == kStopped) << "state_ " << state_; | |
34 DCHECK_EQ(pending_read_count_, 0); | |
35 | |
36 STLDeleteValues(&text_track_map_); | |
37 } | |
38 | |
39 void TextRenderer::Initialize(const base::Closure& ended_cb) { | |
40 DCHECK(message_loop_->BelongsToCurrentThread()); | |
41 DCHECK(!ended_cb.is_null()); | |
42 DCHECK_EQ(kUninitialized, state_) << "state_ " << state_; | |
43 DCHECK(read_state_.empty()); | |
44 DCHECK_EQ(pending_read_count_, 0); | |
45 DCHECK_EQ(pending_eos_count_, 0); | |
46 DCHECK(ended_cb_.is_null()); | |
47 | |
48 weak_this_ = weak_factory_.GetWeakPtr(); | |
49 ended_cb_ = ended_cb; | |
50 state_ = kPaused; | |
51 } | |
52 | |
53 void TextRenderer::Play(const base::Closure& callback) { | |
54 DCHECK(message_loop_->BelongsToCurrentThread()); | |
55 DCHECK_EQ(state_, kPaused) << "state_ " << state_; | |
56 | |
57 for (ReadStateMap::iterator itr = read_state_.begin(); | |
58 itr != read_state_.end(); ++itr) { | |
59 if (itr->second == kReadPending) { | |
60 DCHECK_GT(pending_read_count_, 0); | |
61 continue; | |
62 } | |
63 | |
64 itr->second = kReadPending; | |
65 ++pending_read_count_; | |
66 | |
67 DemuxerStream* const stream = itr->first; | |
68 stream->Read(base::Bind(&TextRenderer::BufferReady, | |
69 weak_this_, | |
70 stream)); | |
71 } | |
72 | |
73 state_ = kPlaying; | |
74 callback.Run(); | |
75 } | |
76 | |
77 void TextRenderer::Pause(const base::Closure& callback) { | |
78 DCHECK(message_loop_->BelongsToCurrentThread()); | |
79 DCHECK(state_ == kPlaying || state_ == kEnded) << "state_ " << state_; | |
80 DCHECK_GE(pending_read_count_, 0); | |
81 pause_cb_ = callback; | |
82 | |
83 if (pending_read_count_ == 0) { | |
84 state_ = kPaused; | |
85 base::ResetAndReturn(&pause_cb_).Run(); | |
86 return; | |
87 } | |
88 | |
89 state_ = kPausePending; | |
90 } | |
91 | |
92 void TextRenderer::Flush(const base::Closure& callback) { | |
93 DCHECK(message_loop_->BelongsToCurrentThread()); | |
94 DCHECK_EQ(pending_read_count_, 0); | |
95 DCHECK(state_ == kPaused || state_ == kEnded) << "state_ " << state_; | |
96 | |
97 pending_eos_count_ = read_state_.size(); | |
98 | |
99 callback.Run(); | |
100 } | |
101 | |
102 void TextRenderer::Stop(const base::Closure& cb) { | |
103 DCHECK(message_loop_->BelongsToCurrentThread()); | |
104 DCHECK(!cb.is_null()); | |
105 DCHECK(state_ == kPlaying || | |
106 state_ == kPausePending || | |
107 state_ == kPaused || | |
108 state_ == kEnded) << "state_ " << state_; | |
109 DCHECK_GE(pending_read_count_, 0); | |
110 | |
111 stop_cb_ = cb; | |
112 | |
113 if (pending_read_count_ == 0) { | |
114 state_ = kStopped; | |
115 base::ResetAndReturn(&stop_cb_).Run(); | |
116 return; | |
117 } | |
118 | |
119 state_ = kStopPending; | |
120 } | |
121 | |
122 void TextRenderer::AddTextStream(DemuxerStream* text_stream, | |
123 TextKind kind, | |
124 const std::string& label, | |
125 const std::string& language) { | |
126 DCHECK(message_loop_->BelongsToCurrentThread()); | |
127 DCHECK(state_ != kUninitialized) << "state_ " << state_; | |
128 | |
129 if (state_ == kStopPending || state_ == kStopped) | |
130 return; | |
131 | |
132 media::AddTextTrackDoneCB done_cb = | |
133 media::BindToLoop(message_loop_, | |
134 base::Bind(&TextRenderer::OnAddTextTrackDone, | |
135 weak_this_, | |
136 text_stream)); | |
137 | |
138 add_text_track_cb_.Run(kind, label, language, done_cb); | |
139 } | |
140 | |
141 void TextRenderer::RemoveTextStream(DemuxerStream* text_stream) { | |
142 DCHECK(message_loop_->BelongsToCurrentThread()); | |
143 | |
144 ReadStateMap::iterator read_state_it = read_state_.find(text_stream); | |
145 | |
146 if (read_state_it != read_state_.end()) { | |
147 DCHECK_EQ(read_state_it->second, kReadIdle); | |
148 read_state_.erase(read_state_it); | |
149 } | |
150 | |
151 TextTrackMap::iterator text_track_it = text_track_map_.find(text_stream); | |
152 | |
153 if (text_track_it != text_track_map_.end()) { | |
154 media::TextTrack* text_track = text_track_it->second; | |
155 text_track_map_.erase(text_track_it); | |
156 delete text_track; | |
157 } | |
158 } | |
159 | |
160 bool TextRenderer::HasTracks() const { | |
161 DCHECK(message_loop_->BelongsToCurrentThread()); | |
162 return !text_track_map_.empty(); | |
163 } | |
164 | |
165 void TextRenderer::BufferReady( | |
166 DemuxerStream* stream, | |
167 DemuxerStream::Status status, | |
168 const scoped_refptr<DecoderBuffer>& input) { | |
169 DCHECK(message_loop_->BelongsToCurrentThread()); | |
170 DCHECK_NE(status, DemuxerStream::kConfigChanged); | |
171 | |
172 if (status == DemuxerStream::kAborted) { | |
173 DCHECK(!input.get()); | |
acolwell GONE FROM CHROMIUM
2013/10/21 20:10:40
nit: Do you really need the .get() here?
Matthew Heaney (Chromium)
2013/10/23 05:09:01
Done.
| |
174 DCHECK_GT(pending_read_count_, 0); | |
175 DCHECK_GT(pending_eos_count_, 0); | |
176 DCHECK_EQ(read_state_[stream], kReadPending); | |
177 | |
178 --pending_read_count_; | |
179 --pending_eos_count_; | |
acolwell GONE FROM CHROMIUM
2013/10/21 20:10:40
Why is the eos count being decremented here? An ab
Matthew Heaney (Chromium)
2013/10/23 05:09:01
Done.
| |
180 read_state_[stream] = kReadIdle; | |
181 return; | |
182 } | |
183 | |
184 if (input->end_of_stream()) { | |
185 CueReady(stream, NULL); | |
186 return; | |
187 } | |
188 | |
189 DCHECK_EQ(status, DemuxerStream::kOk); | |
190 DCHECK_GE(input->side_data_size(), 2); | |
191 | |
192 // The side data contains both the cue id and cue settings, | |
193 // each terminated with a NUL. | |
194 const char* id_ptr = reinterpret_cast<const char*>(input->side_data()); | |
195 size_t id_len = strlen(id_ptr); | |
196 std::string id(id_ptr, id_len); | |
197 | |
198 const char* settings_ptr = id_ptr + id_len + 1; | |
199 size_t settings_len = strlen(settings_ptr); | |
200 std::string settings(settings_ptr, settings_len); | |
201 | |
202 // The cue payload is stored in the data-part of the input buffer. | |
203 std::string text(input->data(), input->data() + input->data_size()); | |
204 | |
205 scoped_refptr<TextCue> text_cue( | |
206 new TextCue(input->timestamp(), | |
207 input->duration(), | |
208 id, | |
209 settings, | |
210 text)); | |
211 | |
212 CueReady(stream, text_cue); | |
213 } | |
214 | |
215 void TextRenderer::CueReady( | |
216 DemuxerStream* text_stream, | |
217 const scoped_refptr<TextCue>& text_cue) { | |
218 DCHECK(message_loop_->BelongsToCurrentThread()); | |
219 DCHECK(state_ != kUninitialized && | |
220 state_ != kStopped) << "state_ " << state_; | |
221 DCHECK_GT(pending_read_count_, 0); | |
222 DCHECK_GT(pending_eos_count_, 0); | |
223 DCHECK_EQ(read_state_[text_stream], kReadPending); | |
224 | |
225 --pending_read_count_; | |
226 read_state_[text_stream] = kReadIdle; | |
227 | |
228 switch (state_) { | |
229 case kPlaying: | |
230 case kPausePending: | |
231 if (text_cue) | |
232 break; | |
233 | |
234 // A NULL buffer means that we have reached EOS (or there's an error). | |
235 | |
236 --pending_eos_count_; | |
237 | |
238 if (pending_eos_count_ > 0) | |
239 return; | |
240 | |
241 if (state_ == kPausePending) | |
242 pause_cb_.Run(); | |
243 | |
244 state_ = kEnded; | |
245 ended_cb_.Run(); | |
246 | |
247 return; | |
248 | |
249 case kStopPending: | |
250 if (pending_read_count_ == 0) { | |
251 state_ = kStopped; | |
252 stop_cb_.Run(); | |
253 } | |
254 | |
255 return; | |
256 | |
257 case kPaused: | |
258 case kStopped: | |
259 case kUninitialized: | |
acolwell GONE FROM CHROMIUM
2013/10/21 20:10:40
Missing kEnded from this switch
Matthew Heaney (Chromium)
2013/10/23 05:09:01
Done.
| |
260 NOTREACHED(); | |
261 return; | |
262 } | |
263 | |
264 TextTrackMap::iterator it = text_track_map_.find(text_stream); | |
265 DCHECK(it != text_track_map_.end()); | |
266 | |
267 media::TextTrack* text_track = it->second; | |
268 DCHECK(text_track); | |
269 | |
270 base::TimeDelta start = text_cue->timestamp(); | |
271 base::TimeDelta end = start + text_cue->duration(); | |
272 | |
273 text_track->addWebVTTCue(start, end, | |
274 text_cue->id(), | |
acolwell GONE FROM CHROMIUM
2013/10/21 20:10:40
nit: Fix indent
Matthew Heaney (Chromium)
2013/10/23 05:09:01
Done.
| |
275 text_cue->text(), | |
276 text_cue->settings()); | |
277 | |
278 if (state_ == kPlaying) { | |
279 read_state_[text_stream] = kReadPending; | |
280 ++pending_read_count_; | |
281 text_stream->Read(base::Bind(&TextRenderer::BufferReady, | |
282 weak_this_, | |
283 text_stream)); | |
284 return; | |
285 } | |
286 | |
287 if (pending_read_count_ == 0) { | |
288 state_ = kPaused; | |
acolwell GONE FROM CHROMIUM
2013/10/21 20:10:40
DCHECK_EQ(state_, kPausePending) << " state_ << "
Matthew Heaney (Chromium)
2013/10/23 05:09:01
Done.
| |
289 pause_cb_.Run(); | |
290 } | |
291 } | |
292 | |
293 void TextRenderer::OnAddTextTrackDone(DemuxerStream* text_stream, | |
294 scoped_ptr<TextTrack> text_track) { | |
295 DCHECK(message_loop_->BelongsToCurrentThread()); | |
296 DCHECK(state_ != kUninitialized && | |
297 state_ != kStopped) << "state_ " << state_; | |
298 | |
299 text_track_map_[text_stream] = text_track.release(); | |
300 | |
301 ++pending_eos_count_; | |
302 | |
303 if (state_ != kPlaying) { | |
304 read_state_[text_stream] = kReadIdle; | |
305 } else { | |
306 read_state_[text_stream] = kReadPending; | |
307 ++pending_read_count_; | |
308 | |
309 text_stream->Read(base::Bind(&TextRenderer::BufferReady, | |
310 weak_this_, | |
311 text_stream)); | |
312 } | |
313 } | |
314 | |
315 } // namespace media | |
OLD | NEW |