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/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 } | |
| 29 | |
| 30 TextRenderer::~TextRenderer() { | |
| 31 DCHECK(state_ == kUninitialized || | |
| 32 state_ == kStopped) << "state_ " << state_; | |
| 33 DCHECK_EQ(pending_read_count_, 0); | |
| 34 STLDeleteValues(&text_track_state_map_); | |
| 35 } | |
| 36 | |
| 37 void TextRenderer::Initialize(const base::Closure& ended_cb) { | |
| 38 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 39 DCHECK(!ended_cb.is_null()); | |
| 40 DCHECK_EQ(kUninitialized, state_) << "state_ " << state_; | |
| 41 DCHECK(text_track_state_map_.empty()); | |
| 42 DCHECK_EQ(pending_read_count_, 0); | |
| 43 DCHECK(pending_eos_set_.empty()); | |
| 44 DCHECK(ended_cb_.is_null()); | |
| 45 | |
| 46 weak_this_ = weak_factory_.GetWeakPtr(); | |
| 47 ended_cb_ = ended_cb; | |
| 48 state_ = kPaused; | |
| 49 } | |
| 50 | |
| 51 void TextRenderer::Play(const base::Closure& callback) { | |
| 52 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 53 DCHECK_EQ(state_, kPaused) << "state_ " << state_; | |
| 54 | |
| 55 for (TextTrackStateMap::iterator itr = text_track_state_map_.begin(); | |
| 56 itr != text_track_state_map_.end(); ++itr) { | |
| 57 TextTrackState* state = itr->second; | |
| 58 if (state->read_state == TextTrackState::kReadPending) { | |
| 59 DCHECK_GT(pending_read_count_, 0); | |
| 60 continue; | |
| 61 } | |
| 62 | |
| 63 Read(state, itr->first); | |
| 64 } | |
| 65 | |
| 66 state_ = kPlaying; | |
| 67 callback.Run(); | |
| 68 } | |
| 69 | |
| 70 void TextRenderer::Pause(const base::Closure& callback) { | |
| 71 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 72 DCHECK(state_ == kPlaying || state_ == kEnded) << "state_ " << state_; | |
| 73 DCHECK_GE(pending_read_count_, 0); | |
| 74 pause_cb_ = callback; | |
| 75 | |
| 76 if (pending_read_count_ == 0) { | |
| 77 state_ = kPaused; | |
| 78 base::ResetAndReturn(&pause_cb_).Run(); | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 state_ = kPausePending; | |
| 83 } | |
| 84 | |
| 85 void TextRenderer::Flush(const base::Closure& callback) { | |
| 86 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 87 DCHECK_EQ(pending_read_count_, 0); | |
| 88 DCHECK(state_ == kPaused || state_ == kEnded) << "state_ " << state_; | |
| 89 | |
| 90 pending_eos_set_.clear(); | |
| 91 for (TextTrackStateMap::iterator itr = text_track_state_map_.begin(); | |
| 92 itr != text_track_state_map_.end(); ++itr) { | |
| 93 pending_eos_set_.insert(itr->first); | |
| 94 } | |
| 95 | |
| 96 state_ = kPaused; | |
|
acolwell GONE FROM CHROMIUM
2013/11/10 19:48:08
Why is this transition needed now? I believe that
Matthew Heaney (Chromium)
2013/11/13 05:44:49
OK, that's fine. I'll strengthen the precondition
| |
| 97 callback.Run(); | |
| 98 } | |
| 99 | |
| 100 void TextRenderer::Stop(const base::Closure& cb) { | |
| 101 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 102 DCHECK(!cb.is_null()); | |
| 103 DCHECK(state_ == kPlaying || | |
| 104 state_ == kPausePending || | |
| 105 state_ == kPaused || | |
| 106 state_ == kEnded) << "state_ " << state_; | |
| 107 DCHECK_GE(pending_read_count_, 0); | |
| 108 | |
| 109 stop_cb_ = cb; | |
| 110 | |
| 111 if (pending_read_count_ == 0) { | |
| 112 state_ = kStopped; | |
| 113 base::ResetAndReturn(&stop_cb_).Run(); | |
| 114 return; | |
| 115 } | |
| 116 | |
| 117 state_ = kStopPending; | |
| 118 } | |
| 119 | |
| 120 void TextRenderer::AddTextStream(DemuxerStream* text_stream, | |
| 121 const TextTrackConfig& config) { | |
| 122 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 123 DCHECK(state_ != kUninitialized) << "state_ " << state_; | |
| 124 DCHECK_NE(state_, kStopPending); | |
| 125 DCHECK_NE(state_, kStopped); | |
| 126 DCHECK(text_track_state_map_.find(text_stream) == | |
| 127 text_track_state_map_.end()); | |
| 128 DCHECK(pending_eos_set_.find(text_stream) == | |
| 129 pending_eos_set_.end()); | |
| 130 | |
| 131 media::AddTextTrackDoneCB done_cb = | |
| 132 media::BindToLoop(message_loop_, | |
| 133 base::Bind(&TextRenderer::OnAddTextTrackDone, | |
| 134 weak_this_, | |
| 135 text_stream)); | |
| 136 | |
| 137 add_text_track_cb_.Run(config, done_cb); | |
| 138 } | |
| 139 | |
| 140 void TextRenderer::RemoveTextStream(DemuxerStream* text_stream) { | |
| 141 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 142 | |
| 143 TextTrackStateMap::iterator itr = text_track_state_map_.find(text_stream); | |
| 144 DCHECK(itr != text_track_state_map_.end()); | |
| 145 | |
| 146 TextTrackState* state = itr->second; | |
| 147 DCHECK_EQ(state->read_state, TextTrackState::kReadIdle); | |
| 148 delete state; | |
| 149 text_track_state_map_.erase(itr); | |
| 150 | |
| 151 pending_eos_set_.erase(text_stream); | |
| 152 } | |
| 153 | |
| 154 bool TextRenderer::HasTracks() const { | |
| 155 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 156 return !text_track_state_map_.empty(); | |
| 157 } | |
| 158 | |
| 159 void TextRenderer::BufferReady( | |
| 160 DemuxerStream* stream, | |
| 161 DemuxerStream::Status status, | |
| 162 const scoped_refptr<DecoderBuffer>& input) { | |
| 163 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 164 DCHECK_NE(status, DemuxerStream::kConfigChanged); | |
| 165 | |
| 166 if (status == DemuxerStream::kAborted) { | |
| 167 DCHECK(!input); | |
| 168 DCHECK_GT(pending_read_count_, 0); | |
| 169 DCHECK(pending_eos_set_.find(stream) != pending_eos_set_.end()); | |
| 170 | |
| 171 TextTrackStateMap::iterator itr = text_track_state_map_.find(stream); | |
| 172 DCHECK(itr != text_track_state_map_.end()); | |
| 173 | |
| 174 TextTrackState* state = itr->second; | |
| 175 DCHECK_EQ(state->read_state, TextTrackState::kReadPending); | |
| 176 | |
| 177 --pending_read_count_; | |
| 178 state->read_state = TextTrackState::kReadIdle; | |
| 179 | |
| 180 switch (state_) { | |
| 181 case kPlaying: | |
| 182 return; | |
| 183 | |
| 184 case kPausePending: | |
| 185 if (pending_read_count_ == 0) { | |
| 186 state_ = kPaused; | |
| 187 base::ResetAndReturn(&pause_cb_).Run(); | |
| 188 } | |
| 189 | |
| 190 return; | |
| 191 | |
| 192 case kStopPending: | |
| 193 if (pending_read_count_ == 0) { | |
| 194 state_ = kStopped; | |
| 195 stop_cb_.Run(); | |
| 196 } | |
| 197 | |
| 198 return; | |
| 199 | |
| 200 case kPaused: | |
| 201 case kStopped: | |
| 202 case kUninitialized: | |
| 203 case kEnded: | |
| 204 NOTREACHED(); | |
| 205 return; | |
| 206 } | |
| 207 | |
| 208 NOTREACHED(); | |
| 209 return; | |
| 210 } | |
| 211 | |
| 212 if (input->end_of_stream()) { | |
| 213 CueReady(stream, NULL); | |
| 214 return; | |
| 215 } | |
| 216 | |
| 217 DCHECK_EQ(status, DemuxerStream::kOk); | |
| 218 DCHECK_GE(input->side_data_size(), 2); | |
| 219 | |
| 220 // The side data contains both the cue id and cue settings, | |
| 221 // each terminated with a NUL. | |
| 222 const char* id_ptr = reinterpret_cast<const char*>(input->side_data()); | |
| 223 size_t id_len = strlen(id_ptr); | |
| 224 std::string id(id_ptr, id_len); | |
| 225 | |
| 226 const char* settings_ptr = id_ptr + id_len + 1; | |
| 227 size_t settings_len = strlen(settings_ptr); | |
| 228 std::string settings(settings_ptr, settings_len); | |
| 229 | |
| 230 // The cue payload is stored in the data-part of the input buffer. | |
| 231 std::string text(input->data(), input->data() + input->data_size()); | |
| 232 | |
| 233 scoped_refptr<TextCue> text_cue( | |
| 234 new TextCue(input->timestamp(), | |
| 235 input->duration(), | |
| 236 id, | |
| 237 settings, | |
| 238 text)); | |
| 239 | |
| 240 CueReady(stream, text_cue); | |
| 241 } | |
| 242 | |
| 243 void TextRenderer::CueReady( | |
| 244 DemuxerStream* text_stream, | |
| 245 const scoped_refptr<TextCue>& text_cue) { | |
| 246 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 247 DCHECK(state_ != kUninitialized && | |
| 248 state_ != kStopped) << "state_ " << state_; | |
| 249 DCHECK_GT(pending_read_count_, 0); | |
| 250 DCHECK(pending_eos_set_.find(text_stream) != pending_eos_set_.end()); | |
| 251 | |
| 252 TextTrackStateMap::iterator itr = text_track_state_map_.find(text_stream); | |
| 253 DCHECK(itr != text_track_state_map_.end()); | |
| 254 | |
| 255 TextTrackState* state = itr->second; | |
| 256 DCHECK_EQ(state->read_state, TextTrackState::kReadPending); | |
| 257 DCHECK(state->text_track); | |
| 258 | |
| 259 --pending_read_count_; | |
| 260 state->read_state = TextTrackState::kReadIdle; | |
| 261 | |
| 262 switch (state_) { | |
| 263 case kPlaying: { | |
| 264 if (text_cue) | |
| 265 break; | |
| 266 | |
| 267 const size_t count = pending_eos_set_.erase(text_stream); | |
| 268 DCHECK_EQ(count, 1U); | |
| 269 | |
| 270 if (pending_eos_set_.empty()) { | |
| 271 DCHECK_EQ(pending_read_count_, 0); | |
| 272 state_ = kEnded; | |
| 273 ended_cb_.Run(); | |
| 274 return; | |
| 275 } | |
| 276 | |
| 277 DCHECK_GT(pending_read_count_, 0); | |
| 278 return; | |
| 279 } | |
| 280 case kPausePending: { | |
| 281 if (text_cue) | |
| 282 break; | |
| 283 | |
| 284 const size_t count = pending_eos_set_.erase(text_stream); | |
| 285 DCHECK_EQ(count, 1U); | |
| 286 | |
| 287 if (pending_read_count_ > 0) { | |
| 288 DCHECK(!pending_eos_set_.empty()); | |
| 289 return; | |
| 290 } | |
| 291 | |
| 292 state_ = kPaused; | |
| 293 base::ResetAndReturn(&pause_cb_).Run(); | |
| 294 | |
| 295 if (pending_eos_set_.empty()) { | |
|
acolwell GONE FROM CHROMIUM
2013/11/10 19:48:08
I don't think we should call ended_cb_.Run() here
Matthew Heaney (Chromium)
2013/11/13 05:44:49
Done.
| |
| 296 state_ = kEnded; | |
| 297 ended_cb_.Run(); | |
| 298 } | |
| 299 | |
| 300 return; | |
| 301 } | |
| 302 case kStopPending: | |
| 303 if (pending_read_count_ == 0) { | |
| 304 state_ = kStopped; | |
| 305 stop_cb_.Run(); | |
| 306 } | |
| 307 | |
| 308 return; | |
| 309 | |
| 310 case kPaused: | |
| 311 case kStopped: | |
| 312 case kUninitialized: | |
| 313 case kEnded: | |
| 314 NOTREACHED(); | |
| 315 return; | |
| 316 } | |
| 317 | |
| 318 base::TimeDelta start = text_cue->timestamp(); | |
| 319 base::TimeDelta end = start + text_cue->duration(); | |
| 320 | |
| 321 state->text_track->addWebVTTCue(start, end, | |
| 322 text_cue->id(), | |
| 323 text_cue->text(), | |
| 324 text_cue->settings()); | |
| 325 | |
| 326 if (state_ == kPlaying) { | |
| 327 Read(state, text_stream); | |
| 328 return; | |
| 329 } | |
| 330 | |
| 331 if (pending_read_count_ == 0) { | |
| 332 DCHECK_EQ(state_, kPausePending) << "state_ " << state_; | |
| 333 state_ = kPaused; | |
| 334 base::ResetAndReturn(&pause_cb_).Run(); | |
| 335 } | |
| 336 } | |
| 337 | |
| 338 void TextRenderer::OnAddTextTrackDone(DemuxerStream* text_stream, | |
| 339 scoped_ptr<TextTrack> text_track) { | |
| 340 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 341 DCHECK(state_ != kUninitialized && | |
| 342 state_ != kStopped && | |
| 343 state_ != kStopPending) << "state_ " << state_; | |
| 344 | |
| 345 scoped_ptr<TextTrackState> state(new TextTrackState(text_track.Pass())); | |
| 346 | |
| 347 typedef std::pair<TextTrackStateMap::iterator, bool> InsertResult; | |
| 348 TextTrackState* const null_state = static_cast<TextTrackState*>(NULL); | |
| 349 const TextTrackStateMap::value_type value(text_stream, null_state); | |
| 350 InsertResult result = text_track_state_map_.insert(value); | |
|
acolwell GONE FROM CHROMIUM
2013/11/10 19:48:08
wow. This does not make the code easier to read. H
Matthew Heaney (Chromium)
2013/11/13 05:44:49
I wanted a check there, but the compiler needed so
| |
| 351 DCHECK(result.second); | |
| 352 | |
| 353 result.first->second = state.release(); | |
| 354 pending_eos_set_.insert(text_stream); | |
| 355 | |
| 356 if (state_ == kPlaying) | |
| 357 Read(result.first->second, text_stream); | |
| 358 } | |
| 359 | |
| 360 void TextRenderer::Read( | |
| 361 TextTrackState* state, | |
| 362 DemuxerStream* text_stream) { | |
| 363 state->read_state = TextTrackState::kReadPending; | |
|
acolwell GONE FROM CHROMIUM
2013/11/10 19:48:08
nit: Add a DCHECK_NE(state->read_state, TextTrackS
Matthew Heaney (Chromium)
2013/11/13 05:44:49
Done.
| |
| 364 ++pending_read_count_; | |
| 365 | |
| 366 text_stream->Read(base::Bind(&TextRenderer::BufferReady, | |
| 367 weak_this_, | |
| 368 text_stream)); | |
| 369 } | |
| 370 | |
| 371 TextRenderer::TextTrackState::TextTrackState(scoped_ptr<TextTrack> tt) | |
| 372 : read_state(kReadIdle), | |
| 373 text_track(tt.Pass()) { | |
| 374 } | |
| 375 | |
| 376 } // namespace media | |
| OLD | NEW |