 Chromium Code Reviews
 Chromium Code Reviews Issue 23702007:
  Render inband text tracks in the media pipeline  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@master
    
  
    Issue 23702007:
  Render inband text tracks in the media pipeline  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@master| 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/demuxer.h" | |
| 14 #include "media/base/demuxer_stream.h" | |
| 15 #include "media/base/text_cue.h" | |
| 16 #include "media/base/text_decoder.h" | |
| 17 | |
| 18 namespace media { | |
| 19 | |
| 20 TextRenderer::TextRenderer( | |
| 21 const scoped_refptr<base::MessageLoopProxy>& message_loop, | |
| 22 scoped_ptr<TextDecoder> decoder, | |
| 23 const AddTextTrackCB2& add_text_track_cb) | |
| 24 : message_loop_(message_loop), | |
| 25 weak_factory_(this), | |
| 26 decoder_(decoder.Pass()), | |
| 27 add_text_track_cb_(add_text_track_cb), | |
| 28 state_(kUninitialized), | |
| 29 pending_read_count_(0), | |
| 30 pending_eos_count_(0) { | |
| 31 } | |
| 32 | |
| 33 TextRenderer::~TextRenderer() { | |
| 34 DCHECK(state_ == kUninitialized || state_ == kStopped); | |
| 35 DCHECK_EQ(pending_read_count_, 0); | |
| 36 | |
| 37 STLDeleteValues(&text_track_map_); | |
| 38 } | |
| 39 | |
| 40 void TextRenderer::Initialize(const base::Closure& ended_cb) { | |
| 41 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 42 DCHECK(!ended_cb.is_null()); | |
| 43 DCHECK_EQ(kUninitialized, state_); | |
| 44 DCHECK(read_state_.empty()); | |
| 45 DCHECK_EQ(pending_read_count_, 0); | |
| 46 DCHECK_EQ(pending_eos_count_, 0); | |
| 47 DCHECK(ended_cb_.is_null()); | |
| 48 | |
| 49 weak_this_ = weak_factory_.GetWeakPtr(); | |
| 50 ended_cb_ = ended_cb; | |
| 51 state_ = kPaused; | |
| 52 decoder_->Initialize(); | |
| 53 } | |
| 54 | |
| 55 void TextRenderer::Play(const base::Closure& callback) { | |
| 56 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 57 DCHECK(state_ == kPaused); | |
| 58 | |
| 59 state_ = kPlaying; | |
| 60 callback.Run(); | |
| 61 | |
| 62 for (ReadStateMap::iterator itr = read_state_.begin(); | |
| 63 itr != read_state_.end(); ++itr) { | |
| 64 ReadStateMap::value_type& val = *itr; | |
| 65 | |
| 66 if (val.second == kReadPending) { | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
nit: Just use itr-> here and below. Val isn't real
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 67 DCHECK_GT(pending_read_count_, 0); | |
| 68 continue; | |
| 69 } | |
| 70 | |
| 71 val.second = kReadPending; | |
| 72 ++pending_read_count_; | |
| 73 decoder_->Read(val.first, | |
| 74 base::Bind(&TextRenderer::CueReady, weak_this_)); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 void TextRenderer::Pause(const base::Closure& callback) { | |
| 79 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 80 DCHECK(state_ == kPlaying || state_ == kEnded); | |
| 81 pause_cb_ = callback; | |
| 82 | |
| 83 if (pending_read_count_ <= 0) { | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
nit: Add DCHECK_GE(pending_read_count_, 0) above a
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 84 state_ = kPaused; | |
| 85 base::ResetAndReturn(&pause_cb_).Run(); | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 state_ = kPausePending; | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
nit: Consider using a ChangeState() helper method
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Ack.
 | |
| 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); | |
| 96 | |
| 97 pending_eos_count_ = read_state_.size(); | |
| 98 | |
| 99 callback.Run(); | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
You likely need to wait for any pending reads to c
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
No reads are pending during the kPaused or kEnded
 | |
| 100 } | |
| 101 | |
| 102 void TextRenderer::Stop(const base::Closure& cb) { | |
| 103 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 104 DCHECK(!cb.is_null()); | |
| 105 DCHECK(state_ == kPlaying || state_ == kPaused || state_ == kEnded); | |
| 106 | |
| 107 stop_cb_ = cb; | |
| 108 | |
| 109 if (pending_read_count_ <= 0) { | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
nit: s/<=/==/ since a negative value is a sign of
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 110 state_ = kStopped; | |
| 111 base::ResetAndReturn(&stop_cb_).Run(); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 state_ = kStopPending; | |
| 116 } | |
| 117 | |
| 118 void TextRenderer::AddTextStream(DemuxerStream* text_stream, | |
| 119 TextKind kind, | |
| 120 const std::string& label, | |
| 121 const std::string& language) { | |
| 122 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 123 DCHECK(state_ != kUninitialized && state_ != kStopped); | |
| 124 | |
| 125 media::AddTextTrackDoneCB done_cb = | |
| 126 media::BindToLoop(message_loop_, | |
| 127 base::Bind(&TextRenderer::OnAddTextTrackDone, | |
| 128 weak_this_)); | |
| 129 | |
| 130 // TODO(matthewjheaney): curry-away the text_stream arg, if possible. | |
| 131 add_text_track_cb_.Run(text_stream, kind, label, language, done_cb); | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
Yes. You should include text_stream in the Bind ab
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 132 } | |
| 133 | |
| 134 void TextRenderer::OnAddTextTrackDone(DemuxerStream* text_stream, | |
| 135 scoped_ptr<TextTrack> text_track) { | |
| 136 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 137 DCHECK(state_ != kUninitialized && state_ != kStopped); | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
nit: Add << "state_ " << state so that it is easy
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 138 | |
| 139 text_track_map_[text_stream] = text_track.release(); | |
| 140 | |
| 141 ++pending_eos_count_; | |
| 142 | |
| 143 if (state_ != kPlaying) { | |
| 144 read_state_[text_stream] = kReadIdle; | |
| 145 } else { | |
| 146 read_state_[text_stream] = kReadPending; | |
| 147 ++pending_read_count_; | |
| 148 | |
| 149 decoder_->Read(text_stream, | |
| 150 base::Bind(&TextRenderer::CueReady, weak_this_)); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 bool TextRenderer::HasTracks() const { | |
| 155 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 156 return (!text_track_map_.empty()); | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
nit: remove unnecessary ()
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 157 } | |
| 158 | |
| 159 void TextRenderer::CueReady( | |
| 160 DemuxerStream* text_stream, | |
| 161 const scoped_refptr<TextCue>& text_cue) { | |
| 162 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 163 DCHECK_NE(state_, kUninitialized); | |
| 164 DCHECK_NE(state_, kStopped); | |
| 165 DCHECK_GT(pending_read_count_, 0); | |
| 166 DCHECK_GT(pending_eos_count_, 0); | |
| 167 DCHECK_EQ(read_state_[text_stream], kReadPending); | |
| 168 | |
| 169 --pending_read_count_; | |
| 170 read_state_[text_stream] = kReadIdle; | |
| 171 | |
| 172 switch (state_) { | |
| 173 case kPlaying: | |
| 174 if (text_cue) | |
| 175 break; | |
| 176 | |
| 177 // A NULL buffer means that we have reached EOS (or there's an error). | |
| 178 | |
| 179 --pending_eos_count_; | |
| 180 | |
| 181 if (pending_read_count_ > 0) | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
Why do you need to worry about the pending_read_co
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 182 return; | |
| 183 | |
| 184 if (pending_eos_count_ <= 0) { | |
| 185 state_ = kEnded; | |
| 186 ended_cb_.Run(); | |
| 187 } | |
| 188 | |
| 189 return; | |
| 190 | |
| 191 case kPausePending: | |
| 192 if (pending_read_count_ <= 0) { | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
Do we really want to drop cues received while tryi
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 193 state_ = kPaused; | |
| 194 pause_cb_.Run(); | |
| 195 } | |
| 196 | |
| 197 return; | |
| 198 | |
| 199 case kStopPending: | |
| 200 if (pending_read_count_ <= 0) { | |
| 201 state_ = kStopped; | |
| 202 stop_cb_.Run(); | |
| 203 } | |
| 204 | |
| 205 return; | |
| 206 | |
| 207 case kPaused: | |
| 208 case kStopped: | |
| 209 case kUninitialized: | |
| 210 default: | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
nit: remove default so the compiler will warn us o
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 211 NOTREACHED(); | |
| 212 return; | |
| 213 } | |
| 214 | |
| 215 if (!text_cue->text().empty()) { | |
| 
acolwell GONE FROM CHROMIUM
2013/10/08 15:45:24
Why is a cue w/ empty text being skipped? Either t
 
Matthew Heaney (Chromium)
2013/10/13 05:30:17
Done.
 | |
| 216 TextTrackMap::iterator it = text_track_map_.find(text_stream); | |
| 217 DCHECK(it != text_track_map_.end()); | |
| 218 | |
| 219 media::TextTrack* text_track = it->second; | |
| 220 DCHECK_NE(text_track, static_cast<media::TextTrack*>(NULL)); | |
| 221 | |
| 222 base::TimeDelta start = text_cue->timestamp(); | |
| 223 base::TimeDelta end = start + text_cue->duration(); | |
| 224 | |
| 225 text_track->addWebVTTCue(start, end, | |
| 226 text_cue->id(), | |
| 227 text_cue->text(), | |
| 228 text_cue->settings()); | |
| 229 } | |
| 230 | |
| 231 read_state_[text_stream] = kReadPending; | |
| 232 ++pending_read_count_; | |
| 233 decoder_->Read(text_stream, | |
| 234 base::Bind(&TextRenderer::CueReady, weak_this_)); | |
| 235 } | |
| 236 | |
| 237 } // namespace media | |
| OLD | NEW |