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