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..f2921a1d228466819d87bde6c522dc31f00a8e35 |
--- /dev/null |
+++ b/media/base/text_renderer.cc |
@@ -0,0 +1,201 @@ |
+// 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 "media/base/demuxer.h" |
+#include "media/base/demuxer_stream.h" |
+#include "media/base/text_cue.h" |
+#include "media/base/text_decoder.h" |
+ |
+namespace media { |
+ |
+TextRenderer::TextRenderer( |
+ const scoped_refptr<base::MessageLoopProxy>& message_loop, |
+ scoped_ptr<TextDecoder> decoder, |
+ const AddTextStreamCB& add_text_stream_cb, |
+ const CueReadyCB& cue_ready_cb) |
+ : message_loop_(message_loop), |
+ weak_factory_(this), |
+ decoder_(decoder.Pass()), |
+ add_text_stream_cb_(add_text_stream_cb), |
+ cue_ready_cb_(cue_ready_cb), |
+ state_(kUninitialized), |
+ pending_read_count_(0), |
+ pending_eos_count_(0) { |
+} |
+ |
+TextRenderer::~TextRenderer() { |
+ DCHECK(state_ == kUninitialized || state_ == kStopped); |
+ DCHECK_EQ(pending_read_count_, 0); |
+} |
+ |
+void TextRenderer::Initialize(const base::Closure& ended_cb) { |
+ DCHECK(message_loop_->BelongsToCurrentThread()); |
+ DCHECK(!ended_cb.is_null()); |
+ DCHECK_EQ(kUninitialized, 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; |
+ decoder_->Initialize(); |
+} |
+ |
+void TextRenderer::Play(const base::Closure& callback) { |
+ DCHECK(message_loop_->BelongsToCurrentThread()); |
+ DCHECK(state_ == kPaused); |
+ |
+ state_ = kPlaying; |
+ callback.Run(); |
+ |
+ for (ReadStateMap::iterator itr = read_state_.begin(); |
+ itr != read_state_.end(); ++itr) { |
+ ReadStateMap::value_type& val = *itr; |
+ |
+ if (val.second == kReadPending) { |
+ DCHECK_GT(pending_read_count_, 0); |
+ continue; |
+ } |
+ |
+ val.second = kReadPending; |
+ ++pending_read_count_; |
+ decoder_->Read(val.first, |
+ base::Bind(&TextRenderer::CueReady, weak_this_)); |
+ } |
+} |
+ |
+void TextRenderer::Pause(const base::Closure& callback) { |
+ DCHECK(message_loop_->BelongsToCurrentThread()); |
+ DCHECK(state_ == kPlaying || state_ == kEnded); |
+ 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); |
+ |
+ 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 || state_ == kEnded); |
+ |
+ 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); |
+ add_text_stream_cb_.Run(text_stream, kind, label, language); |
+ ++pending_eos_count_; |
+ |
+ if (state_ != kPlaying) { |
+ read_state_[text_stream] = kReadIdle; |
+ } else { |
+ read_state_[text_stream] = kReadPending; |
+ ++pending_read_count_; |
+ |
+ decoder_->Read(text_stream, |
+ base::Bind(&TextRenderer::CueReady, weak_this_)); |
+ } |
+} |
+ |
+void TextRenderer::CueReady( |
+ DemuxerStream* text_stream, |
+ const scoped_refptr<TextCue>& text_cue) { |
+ DCHECK(message_loop_->BelongsToCurrentThread()); |
+ DCHECK_NE(state_, kUninitialized); |
+ DCHECK_NE(state_, kStopped); |
+ 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: |
+ if (text_cue) |
+ break; |
+ |
+ // A NULL buffer means that we have reached EOS (or there's an error). |
+ |
+ --pending_eos_count_; |
+ |
+ if (pending_read_count_ > 0) |
+ return; |
+ |
+ if (pending_eos_count_ <= 0) { |
+ state_ = kEnded; |
+ ended_cb_.Run(); |
+ } |
+ |
+ return; |
+ |
+ case kPausePending: |
+ if (pending_read_count_ <= 0) { |
+ state_ = kPaused; |
+ pause_cb_.Run(); |
+ } |
+ |
+ return; |
+ |
+ case kStopPending: |
+ if (pending_read_count_ <= 0) { |
+ state_ = kStopped; |
+ stop_cb_.Run(); |
+ } |
+ |
+ return; |
+ |
+ case kPaused: |
+ case kStopped: |
+ case kUninitialized: |
+ default: |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ if (!text_cue->text().empty()) |
+ cue_ready_cb_.Run(text_stream, text_cue); |
+ |
+ read_state_[text_stream] = kReadPending; |
+ ++pending_read_count_; |
+ decoder_->Read(text_stream, |
+ base::Bind(&TextRenderer::CueReady, weak_this_)); |
+} |
+ |
+} // namespace media |