Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: media/base/text_renderer.cc

Issue 23702007: Render inband text tracks in the media pipeline (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: incorporate aaron's comments (10/12) Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 state_ = kPlaying;
58 callback.Run();
acolwell GONE FROM CHROMIUM 2013/10/14 20:42:24 It is safer to run the callback at the end of the
Matthew Heaney (Chromium) 2013/10/17 05:46:44 Done.
Matthew Heaney (Chromium) 2013/10/17 05:46:44 Done.
59
60 for (ReadStateMap::iterator itr = read_state_.begin();
61 itr != read_state_.end(); ++itr) {
62 if (itr->second == kReadPending) {
63 DCHECK_GT(pending_read_count_, 0);
64 continue;
65 }
66
67 itr->second = kReadPending;
68 ++pending_read_count_;
69
70 DemuxerStream* const stream = itr->first;
71 stream->Read(base::Bind(&TextRenderer::BufferReady,
72 weak_this_,
73 stream));
74 }
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_ == kPaused ||
acolwell GONE FROM CHROMIUM 2013/10/14 20:42:24 I'm pretty sure you need to add kPausePending to t
Matthew Heaney (Chromium) 2013/10/17 05:46:44 Done.
107 state_ == kEnded) << "state_ " << state_;
108 DCHECK_GE(pending_read_count_, 0);
109
110 stop_cb_ = cb;
111
112 if (pending_read_count_ == 0) {
113 state_ = kStopped;
114 base::ResetAndReturn(&stop_cb_).Run();
115 return;
116 }
117
118 state_ = kStopPending;
119 }
120
121 void TextRenderer::AddTextStream(DemuxerStream* text_stream,
122 TextKind kind,
123 const std::string& label,
124 const std::string& language) {
125 DCHECK(message_loop_->BelongsToCurrentThread());
126 DCHECK(state_ != kUninitialized &&
127 state_ != kStopped) << "state_ " << state_;
128
129 media::AddTextTrackDoneCB done_cb =
130 media::BindToLoop(message_loop_,
131 base::Bind(&TextRenderer::OnAddTextTrackDone,
132 weak_this_,
133 text_stream));
134
135 add_text_track_cb_.Run(kind, label, language, done_cb);
136 }
137
138 void TextRenderer::OnAddTextTrackDone(DemuxerStream* text_stream,
139 scoped_ptr<TextTrack> text_track) {
140 DCHECK(message_loop_->BelongsToCurrentThread());
141 DCHECK(state_ != kUninitialized &&
142 state_ != kStopped) << "state_ " << state_;
acolwell GONE FROM CHROMIUM 2013/10/14 20:42:24 I believe it might be possible for state_ to be in
Matthew Heaney (Chromium) 2013/10/17 05:46:44 Done.
143
144 text_track_map_[text_stream] = text_track.release();
145
146 ++pending_eos_count_;
147
148 if (state_ != kPlaying) {
149 read_state_[text_stream] = kReadIdle;
150 } else {
151 read_state_[text_stream] = kReadPending;
152 ++pending_read_count_;
153
154 text_stream->Read(base::Bind(&TextRenderer::BufferReady,
155 weak_this_,
156 text_stream));
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
acolwell GONE FROM CHROMIUM 2013/10/14 20:42:24 nit: DCHECK_NE(status, DemuxerStream::kConfigChang
Matthew Heaney (Chromium) 2013/10/17 05:46:44 Done.
171 if (status == DemuxerStream::kAborted) {
172 DCHECK(!input.get());
173 CueReady(stream, NULL);
acolwell GONE FROM CHROMIUM 2013/10/14 20:42:24 I think there is a bug here. Aborted reads should
Matthew Heaney (Chromium) 2013/10/17 05:46:44 Done.
174 return;
175 }
176
177 if (input->end_of_stream()) {
178 CueReady(stream, NULL);
179 return;
180 }
181
182 DCHECK_EQ(status, DemuxerStream::kOk);
183 DCHECK(input.get());
acolwell GONE FROM CHROMIUM 2013/10/14 20:42:24 nit: Remove since you would have NPE above it this
Matthew Heaney (Chromium) 2013/10/17 05:46:44 Done.
184 DCHECK_GE(input->side_data_size(), 2);
185
186 // The side data contains both the cue id and cue settings,
187 // each terminated with a NUL.
188 const char* id_ptr = reinterpret_cast<const char*>(input->side_data());
189 size_t id_len = strlen(id_ptr);
190 std::string id(id_ptr, id_len);
191
192 const char* settings_ptr = id_ptr + id_len + 1;
193 size_t settings_len = strlen(settings_ptr);
194 std::string settings(settings_ptr, settings_len);
195
196 // The cue payload is stored in the data-part of the input buffer.
197 std::string text(input->data(), input->data() + input->data_size());
198
199 scoped_refptr<TextCue> text_cue(
200 new TextCue(input->timestamp(),
201 input->duration(),
202 id,
203 settings,
204 text));
205
206 CueReady(stream, text_cue);
207 }
208
209 void TextRenderer::CueReady(
210 DemuxerStream* text_stream,
211 const scoped_refptr<TextCue>& text_cue) {
212 DCHECK(message_loop_->BelongsToCurrentThread());
213 DCHECK(state_ != kUninitialized &&
214 state_ != kStopped) << "state_ " << state_;
215 DCHECK_GT(pending_read_count_, 0);
216 DCHECK_GT(pending_eos_count_, 0);
217 DCHECK_EQ(read_state_[text_stream], kReadPending);
218
219 --pending_read_count_;
220 read_state_[text_stream] = kReadIdle;
221
222 switch (state_) {
223 case kPlaying:
224 case kPausePending:
225 if (text_cue)
226 break;
227
228 // A NULL buffer means that we have reached EOS (or there's an error).
229
230 --pending_eos_count_;
231
232 if (pending_eos_count_ > 0)
233 return;
234
235 if (state_ == kPausePending)
236 pause_cb_.Run();
237
238 state_ = kEnded;
239 ended_cb_.Run();
240
241 return;
242
243 case kStopPending:
244 if (pending_read_count_ == 0) {
245 state_ = kStopped;
246 stop_cb_.Run();
247 }
248
249 return;
250
251 case kPaused:
252 case kStopped:
253 case kUninitialized:
254 NOTREACHED();
255 return;
256 }
257
258 TextTrackMap::iterator it = text_track_map_.find(text_stream);
259 DCHECK(it != text_track_map_.end());
260
261 media::TextTrack* text_track = it->second;
262 DCHECK_NE(text_track, static_cast<media::TextTrack*>(NULL));
acolwell GONE FROM CHROMIUM 2013/10/14 20:42:24 nit: Use DCHECK(text_track) instead.
Matthew Heaney (Chromium) 2013/10/17 05:46:44 Done.
263
264 base::TimeDelta start = text_cue->timestamp();
265 base::TimeDelta end = start + text_cue->duration();
266
267 text_track->addWebVTTCue(start, end,
268 text_cue->id(),
269 text_cue->text(),
270 text_cue->settings());
271
272 if (state_ == kPlaying) {
273 read_state_[text_stream] = kReadPending;
274 ++pending_read_count_;
275 text_stream->Read(base::Bind(&TextRenderer::BufferReady,
276 weak_this_,
277 text_stream));
acolwell GONE FROM CHROMIUM 2013/10/14 20:42:24 nit: return early & drop else
Matthew Heaney (Chromium) 2013/10/17 05:46:44 Done.
278 } else if (pending_read_count_ == 0) {
279 state_ = kPaused;
280 pause_cb_.Run();
281 }
282 }
283
284 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698