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..11c45675b7ccd5f99d2afa7b57453cede39c7afb |
| --- /dev/null |
| +++ b/media/base/text_renderer.cc |
| @@ -0,0 +1,319 @@ |
| +// 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); |
| + |
| + 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.
|
| + itr != text_track_state_map_.end(); ++itr) { |
| + TextTrackState& state = itr->second; |
| + delete state.text_track; |
| + } |
| +} |
| + |
| +void TextRenderer::Initialize(const base::Closure& ended_cb) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(!ended_cb.is_null()); |
| + DCHECK_EQ(kUninitialized, state_) << "state_ " << state_; |
| + DCHECK(text_track_state_map_.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_; |
| + |
| + for (TextTrackStateMap::iterator itr = text_track_state_map_.begin(); |
| + itr != text_track_state_map_.end(); ++itr) { |
| + TextTrackState& state = itr->second; |
| + if (state.read_state == kReadPending) { |
| + DCHECK_GT(pending_read_count_, 0); |
| + continue; |
| + } |
| + |
| + state.read_state = kReadPending; |
| + ++pending_read_count_; |
| + |
| + DemuxerStream* const stream = itr->first; |
| + stream->Read(base::Bind(&TextRenderer::BufferReady, |
| + weak_this_, |
| + stream)); |
| + } |
| + |
| + state_ = kPlaying; |
| + callback.Run(); |
| +} |
| + |
| +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_ = text_track_state_map_.size(); |
| + |
| + callback.Run(); |
| +} |
| + |
| +void TextRenderer::Stop(const base::Closure& cb) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(!cb.is_null()); |
| + DCHECK(state_ == kPlaying || |
| + state_ == kPausePending || |
| + state_ == kPaused || |
| + 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, |
| + const TextTrackConfig& config) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(state_ != kUninitialized) << "state_ " << state_; |
| + |
| + 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.
|
| + return; |
| + |
| + media::AddTextTrackDoneCB done_cb = |
| + media::BindToLoop(message_loop_, |
| + base::Bind(&TextRenderer::OnAddTextTrackDone, |
| + weak_this_, |
| + text_stream)); |
| + |
| + add_text_track_cb_.Run(config, done_cb); |
| +} |
| + |
| +void TextRenderer::RemoveTextStream(DemuxerStream* text_stream) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + |
| + TextTrackStateMap::iterator itr = text_track_state_map_.find(text_stream); |
| + |
| + if (itr != text_track_state_map_.end()) { |
| + TextTrackState& state = itr->second; |
| + DCHECK_EQ(state.read_state, kReadIdle); |
| + delete state.text_track; |
| + text_track_state_map_.erase(itr); |
| + } |
| +} |
| + |
| +bool TextRenderer::HasTracks() const { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + return !text_track_state_map_.empty(); |
| +} |
| + |
| +void TextRenderer::BufferReady( |
| + DemuxerStream* stream, |
| + DemuxerStream::Status status, |
| + const scoped_refptr<DecoderBuffer>& input) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK_NE(status, DemuxerStream::kConfigChanged); |
| + |
| + if (status == DemuxerStream::kAborted) { |
| + DCHECK(!input); |
| + DCHECK_GT(pending_read_count_, 0); |
| + DCHECK_GT(pending_eos_count_, 0); |
| + |
| + TextTrackStateMap::iterator itr = text_track_state_map_.find(stream); |
| + DCHECK(itr != text_track_state_map_.end()); |
| + |
| + TextTrackState& state = itr->second; |
| + DCHECK_EQ(state.read_state, kReadPending); |
| + |
| + --pending_read_count_; |
| + state.read_state = kReadIdle; |
| + return; |
| + } |
| + |
| + if (input->end_of_stream()) { |
| + CueReady(stream, NULL); |
| + return; |
| + } |
| + |
| + DCHECK_EQ(status, DemuxerStream::kOk); |
| + 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); |
| + |
| + TextTrackStateMap::iterator itr = text_track_state_map_.find(text_stream); |
| + DCHECK(itr != text_track_state_map_.end()); |
| + |
| + TextTrackState& state = itr->second; |
| + DCHECK_EQ(state.read_state, kReadPending); |
| + DCHECK(state.text_track); |
| + |
| + --pending_read_count_; |
| + state.read_state = 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: |
| + case kEnded: |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + base::TimeDelta start = text_cue->timestamp(); |
| + base::TimeDelta end = start + text_cue->duration(); |
| + |
| + state.text_track->addWebVTTCue(start, end, |
| + text_cue->id(), |
| + text_cue->text(), |
| + text_cue->settings()); |
| + |
| + if (state_ == kPlaying) { |
| + state.read_state = kReadPending; |
| + ++pending_read_count_; |
| + text_stream->Read(base::Bind(&TextRenderer::BufferReady, |
| + weak_this_, |
| + text_stream)); |
| + return; |
| + } |
| + |
| + if (pending_read_count_ == 0) { |
| + DCHECK_EQ(state_, kPausePending) << "state_ " << state_; |
| + state_ = kPaused; |
| + pause_cb_.Run(); |
| + } |
| +} |
| + |
| +void TextRenderer::OnAddTextTrackDone(DemuxerStream* text_stream, |
| + scoped_ptr<TextTrack> text_track) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + DCHECK(state_ != kUninitialized && |
| + state_ != kStopped) << "state_ " << state_; |
| + |
| + TextTrackState& state = text_track_state_map_[text_stream]; |
| + state.text_track = text_track.release(); |
| + |
| + ++pending_eos_count_; |
| + |
| + if (state_ != kPlaying) { |
| + state.read_state = kReadIdle; |
| + } else { |
| + state.read_state = kReadPending; |
| + ++pending_read_count_; |
| + |
| + text_stream->Read(base::Bind(&TextRenderer::BufferReady, |
| + weak_this_, |
| + text_stream)); |
| + } |
| +} |
| + |
| +} // namespace media |