Chromium Code Reviews| Index: media/base/text_renderer.cc | 
| diff --git a/media/base/text_renderer.cc b/media/base/text_renderer.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..b218c4e96ae1b3ff79844107b41e9220576f4c38 | 
| --- /dev/null | 
| +++ b/media/base/text_renderer.cc | 
| @@ -0,0 +1,284 @@ | 
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "media/base/text_renderer.h" | 
| + | 
| +#include "base/bind.h" | 
| +#include "base/callback_helpers.h" | 
| +#include "base/logging.h" | 
| +#include "base/message_loop/message_loop_proxy.h" | 
| +#include "base/stl_util.h" | 
| +#include "media/base/bind_to_loop.h" | 
| +#include "media/base/decoder_buffer.h" | 
| +#include "media/base/demuxer.h" | 
| +#include "media/base/demuxer_stream.h" | 
| +#include "media/base/text_cue.h" | 
| + | 
| +namespace media { | 
| + | 
| +TextRenderer::TextRenderer( | 
| + const scoped_refptr<base::MessageLoopProxy>& message_loop, | 
| + const AddTextTrackCB& add_text_track_cb) | 
| + : message_loop_(message_loop), | 
| + weak_factory_(this), | 
| + add_text_track_cb_(add_text_track_cb), | 
| + state_(kUninitialized), | 
| + pending_read_count_(0), | 
| + pending_eos_count_(0) { | 
| +} | 
| + | 
| +TextRenderer::~TextRenderer() { | 
| + DCHECK(state_ == kUninitialized || | 
| + state_ == kStopped) << "state_ " << state_; | 
| + DCHECK_EQ(pending_read_count_, 0); | 
| + | 
| + STLDeleteValues(&text_track_map_); | 
| +} | 
| + | 
| +void TextRenderer::Initialize(const base::Closure& ended_cb) { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + DCHECK(!ended_cb.is_null()); | 
| + DCHECK_EQ(kUninitialized, state_) << "state_ " << state_; | 
| + DCHECK(read_state_.empty()); | 
| + DCHECK_EQ(pending_read_count_, 0); | 
| + DCHECK_EQ(pending_eos_count_, 0); | 
| + DCHECK(ended_cb_.is_null()); | 
| + | 
| + weak_this_ = weak_factory_.GetWeakPtr(); | 
| + ended_cb_ = ended_cb; | 
| + state_ = kPaused; | 
| +} | 
| + | 
| +void TextRenderer::Play(const base::Closure& callback) { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + DCHECK_EQ(state_, kPaused) << "state_ " << state_; | 
| + | 
| + state_ = kPlaying; | 
| + 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.
 
 | 
| + | 
| + for (ReadStateMap::iterator itr = read_state_.begin(); | 
| + itr != read_state_.end(); ++itr) { | 
| + if (itr->second == kReadPending) { | 
| + DCHECK_GT(pending_read_count_, 0); | 
| + continue; | 
| + } | 
| + | 
| + itr->second = kReadPending; | 
| + ++pending_read_count_; | 
| + | 
| + DemuxerStream* const stream = itr->first; | 
| + stream->Read(base::Bind(&TextRenderer::BufferReady, | 
| + weak_this_, | 
| + stream)); | 
| + } | 
| +} | 
| + | 
| +void TextRenderer::Pause(const base::Closure& callback) { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + DCHECK(state_ == kPlaying || state_ == kEnded) << "state_ " << state_; | 
| + DCHECK_GE(pending_read_count_, 0); | 
| + pause_cb_ = callback; | 
| + | 
| + if (pending_read_count_ == 0) { | 
| + state_ = kPaused; | 
| + base::ResetAndReturn(&pause_cb_).Run(); | 
| + return; | 
| + } | 
| + | 
| + state_ = kPausePending; | 
| +} | 
| + | 
| +void TextRenderer::Flush(const base::Closure& callback) { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + DCHECK_EQ(pending_read_count_, 0); | 
| + DCHECK(state_ == kPaused || state_ == kEnded) << "state_ " << state_; | 
| + | 
| + pending_eos_count_ = read_state_.size(); | 
| + | 
| + callback.Run(); | 
| +} | 
| + | 
| +void TextRenderer::Stop(const base::Closure& cb) { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + DCHECK(!cb.is_null()); | 
| + DCHECK(state_ == kPlaying || | 
| + 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.
 
 | 
| + state_ == kEnded) << "state_ " << state_; | 
| + DCHECK_GE(pending_read_count_, 0); | 
| + | 
| + stop_cb_ = cb; | 
| + | 
| + if (pending_read_count_ == 0) { | 
| + state_ = kStopped; | 
| + base::ResetAndReturn(&stop_cb_).Run(); | 
| + return; | 
| + } | 
| + | 
| + state_ = kStopPending; | 
| +} | 
| + | 
| +void TextRenderer::AddTextStream(DemuxerStream* text_stream, | 
| + TextKind kind, | 
| + const std::string& label, | 
| + const std::string& language) { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + DCHECK(state_ != kUninitialized && | 
| + state_ != kStopped) << "state_ " << state_; | 
| + | 
| + media::AddTextTrackDoneCB done_cb = | 
| + media::BindToLoop(message_loop_, | 
| + base::Bind(&TextRenderer::OnAddTextTrackDone, | 
| + weak_this_, | 
| + text_stream)); | 
| + | 
| + add_text_track_cb_.Run(kind, label, language, done_cb); | 
| +} | 
| + | 
| +void TextRenderer::OnAddTextTrackDone(DemuxerStream* text_stream, | 
| + scoped_ptr<TextTrack> text_track) { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + DCHECK(state_ != kUninitialized && | 
| + 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.
 
 | 
| + | 
| + text_track_map_[text_stream] = text_track.release(); | 
| + | 
| + ++pending_eos_count_; | 
| + | 
| + if (state_ != kPlaying) { | 
| + read_state_[text_stream] = kReadIdle; | 
| + } else { | 
| + read_state_[text_stream] = kReadPending; | 
| + ++pending_read_count_; | 
| + | 
| + text_stream->Read(base::Bind(&TextRenderer::BufferReady, | 
| + weak_this_, | 
| + text_stream)); | 
| + } | 
| +} | 
| + | 
| +bool TextRenderer::HasTracks() const { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + return !text_track_map_.empty(); | 
| +} | 
| + | 
| +void TextRenderer::BufferReady( | 
| + DemuxerStream* stream, | 
| + DemuxerStream::Status status, | 
| + const scoped_refptr<DecoderBuffer>& input) { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + | 
| 
 
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.
 
 | 
| + if (status == DemuxerStream::kAborted) { | 
| + DCHECK(!input.get()); | 
| + 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.
 
 | 
| + return; | 
| + } | 
| + | 
| + if (input->end_of_stream()) { | 
| + CueReady(stream, NULL); | 
| + return; | 
| + } | 
| + | 
| + DCHECK_EQ(status, DemuxerStream::kOk); | 
| + 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.
 
 | 
| + DCHECK_GE(input->side_data_size(), 2); | 
| + | 
| + // The side data contains both the cue id and cue settings, | 
| + // each terminated with a NUL. | 
| + const char* id_ptr = reinterpret_cast<const char*>(input->side_data()); | 
| + size_t id_len = strlen(id_ptr); | 
| + std::string id(id_ptr, id_len); | 
| + | 
| + const char* settings_ptr = id_ptr + id_len + 1; | 
| + size_t settings_len = strlen(settings_ptr); | 
| + std::string settings(settings_ptr, settings_len); | 
| + | 
| + // The cue payload is stored in the data-part of the input buffer. | 
| + std::string text(input->data(), input->data() + input->data_size()); | 
| + | 
| + scoped_refptr<TextCue> text_cue( | 
| + new TextCue(input->timestamp(), | 
| + input->duration(), | 
| + id, | 
| + settings, | 
| + text)); | 
| + | 
| + CueReady(stream, text_cue); | 
| +} | 
| + | 
| +void TextRenderer::CueReady( | 
| + DemuxerStream* text_stream, | 
| + const scoped_refptr<TextCue>& text_cue) { | 
| + DCHECK(message_loop_->BelongsToCurrentThread()); | 
| + DCHECK(state_ != kUninitialized && | 
| + state_ != kStopped) << "state_ " << state_; | 
| + DCHECK_GT(pending_read_count_, 0); | 
| + DCHECK_GT(pending_eos_count_, 0); | 
| + DCHECK_EQ(read_state_[text_stream], kReadPending); | 
| + | 
| + --pending_read_count_; | 
| + read_state_[text_stream] = kReadIdle; | 
| + | 
| + switch (state_) { | 
| + case kPlaying: | 
| + case kPausePending: | 
| + if (text_cue) | 
| + break; | 
| + | 
| + // A NULL buffer means that we have reached EOS (or there's an error). | 
| + | 
| + --pending_eos_count_; | 
| + | 
| + if (pending_eos_count_ > 0) | 
| + return; | 
| + | 
| + if (state_ == kPausePending) | 
| + pause_cb_.Run(); | 
| + | 
| + state_ = kEnded; | 
| + ended_cb_.Run(); | 
| + | 
| + return; | 
| + | 
| + case kStopPending: | 
| + if (pending_read_count_ == 0) { | 
| + state_ = kStopped; | 
| + stop_cb_.Run(); | 
| + } | 
| + | 
| + return; | 
| + | 
| + case kPaused: | 
| + case kStopped: | 
| + case kUninitialized: | 
| + NOTREACHED(); | 
| + return; | 
| + } | 
| + | 
| + TextTrackMap::iterator it = text_track_map_.find(text_stream); | 
| + DCHECK(it != text_track_map_.end()); | 
| + | 
| + media::TextTrack* text_track = it->second; | 
| + 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.
 
 | 
| + | 
| + base::TimeDelta start = text_cue->timestamp(); | 
| + base::TimeDelta end = start + text_cue->duration(); | 
| + | 
| + text_track->addWebVTTCue(start, end, | 
| + text_cue->id(), | 
| + text_cue->text(), | 
| + text_cue->settings()); | 
| + | 
| + if (state_ == kPlaying) { | 
| + read_state_[text_stream] = kReadPending; | 
| + ++pending_read_count_; | 
| + text_stream->Read(base::Bind(&TextRenderer::BufferReady, | 
| + weak_this_, | 
| + 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.
 
 | 
| + } else if (pending_read_count_ == 0) { | 
| + state_ = kPaused; | 
| + pause_cb_.Run(); | 
| + } | 
| +} | 
| + | 
| +} // namespace media |