Index: media/blink/pipeline_state.cc |
diff --git a/media/blink/pipeline_state.cc b/media/blink/pipeline_state.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1a0d5c937010a06789cfe7fcb33c86ef4f3db10d |
--- /dev/null |
+++ b/media/blink/pipeline_state.cc |
@@ -0,0 +1,202 @@ |
+// Copyright 2015 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/blink/pipeline_state.h" |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "build/build_config.h" |
+#include "media/filters/chunk_demuxer.h" |
+ |
+namespace media { |
+ |
+PipelineState::PipelineState(Pipeline* pipeline, |
+ const RendererFactoryCB& renderer_factory_cb, |
+ const SeekedCB& seeked_cb, |
+ const SuspendedCB& suspended_cb, |
+ const PipelineStatusCB& error_cb) |
+ : pipeline_(pipeline), |
+ renderer_factory_cb_(renderer_factory_cb), |
+ seeked_cb_(seeked_cb), |
+ suspended_cb_(suspended_cb), |
+ error_cb_(error_cb) { |
+ DCHECK(pipeline_); |
+ DCHECK(!renderer_factory_cb_.is_null()); |
+ DCHECK(!seeked_cb_.is_null()); |
+ DCHECK(!suspended_cb_.is_null()); |
+ DCHECK(!error_cb_.is_null()); |
+} |
+ |
+PipelineState::~PipelineState() {} |
+ |
+// TODO(sandersd): Move ChunkDemuxer API to Demuxer so that Pipeline can |
+// implement all of this. |
+void PipelineState::Start(ChunkDemuxer* chunk_demuxer, |
+ Demuxer* demuxer, |
+ const base::Closure& ended_cb, |
+ const PipelineMetadataCB& metadata_cb, |
+ const BufferingStateCB& buffering_state_cb, |
+ const base::Closure& duration_change_cb, |
+ const AddTextTrackCB& add_text_track_cb, |
+ const base::Closure& waiting_for_decryption_key_cb) { |
+ chunk_demuxer_ = chunk_demuxer; |
+ pipeline_->Start(demuxer, renderer_factory_cb_.Run(), ended_cb, error_cb_, |
+ base::Bind(&PipelineState::OnPipelineStatus, |
+ base::Unretained(this), State::PLAYING), |
+ metadata_cb, buffering_state_cb, duration_change_cb, |
+ add_text_track_cb, waiting_for_decryption_key_cb); |
+} |
+ |
+void PipelineState::Seek(base::TimeDelta time, bool time_updated) { |
+ // It would be slightly more clear to set this in Dispatch(), but we want to |
+ // be sure it gets updated even if the seek is elided. |
+ // TODO(sandersd): Is it even possible to have an elided seek during Start()? |
sandersd (OOO until July 31)
2016/02/01 23:19:26
FYI: I removed this TODO, because it turns out tha
|
+ // During this time playback should be paused, and so WMPI would handle the |
+ // elision itself. |
+ if (time_updated) |
+ pending_time_update_ = true; |
+ pending_seeked_ = true; |
+ |
+ // If we are already seeking to |time|, just clear any pending seek. This does |
+ // not apply to MSE because the underlying buffer could have been changed |
wolenetz
2016/01/29 21:42:30
nit: as you mentioned yesterday, this could also a
sandersd (OOO until July 31)
2016/02/01 23:19:26
I added a comment about that for now.
|
+ // between the seek calls. |
+ if ((state_ == State::SEEKING || state_ == State::RESUMING) && |
+ seek_time_ == time && !chunk_demuxer_) { |
+ pending_seek_ = false; |
+ return; |
+ } |
+ |
+ pending_seek_time_ = time; |
+ pending_seek_ = true; |
+ Dispatch(); |
+} |
+ |
+void PipelineState::Suspend() { |
+ pending_resume_ = false; |
+ if (state_ != State::SUSPENDING && state_ != State::SUSPENDED) { |
+ pending_suspend_ = true; |
+ Dispatch(); |
+ } |
+} |
+ |
+void PipelineState::Resume() { |
+ pending_suspend_ = false; |
+ if (state_ == State::SUSPENDING || state_ == State::SUSPENDED) { |
+ pending_resume_ = true; |
+ Dispatch(); |
+ } |
+} |
+ |
+bool PipelineState::IsPlaying() { |
+ return (state_ == State::PLAYING); |
+} |
+ |
+bool PipelineState::IsSuspended() { |
+ return (state_ == State::SUSPENDED || state_ == State::RESUMING); |
+} |
+ |
+void PipelineState::OnPipelineStatus(State state, |
+ PipelineStatus pipeline_status) { |
+ if (pipeline_status != PIPELINE_OK) { |
+ error_cb_.Run(pipeline_status); |
+ return; |
+ } |
+ |
+ state_ = state; |
+ |
+ // Start(), Seek(), or Resume() completed; we can be sure that |
+ // |chunk_demuxer_| got the seek it was waiting for. |
+ if (state == State::PLAYING) |
+ waiting_for_seek_ = false; |
+ |
+ // Sadly we need to signal this state change via a possibly reentrant |
+ // callback. Keep in mind that the state may change inside the callback! |
+ // (In particular, it must be safe to call Dispatch() twice in a row here.) |
+ if (state == State::SUSPENDED) |
+ suspended_cb_.Run(); |
+ |
+ Dispatch(); |
+} |
+ |
+// Note: Dispatch() may be called twice in a row. (See OnPipelineStatus().) |
wolenetz
2016/01/29 21:42:30
nit: Saying it must support re-entrancy by (at lea
sandersd (OOO until July 31)
2016/02/01 23:19:26
Done.
|
+void PipelineState::Dispatch() { |
+ // Suspend/resume transitions take priority because seeks before a suspend |
+ // are wasted, an seeks after can be merged into the resume operation. |
+ if (pending_suspend_ && state_ == State::PLAYING) { |
+ pending_suspend_ = false; |
+ state_ = State::SUSPENDING; |
+ pipeline_->Suspend(base::Bind(&PipelineState::OnPipelineStatus, |
wolenetz
2016/01/29 21:42:30
In the case of an already-in-progress pipeline_->S
sandersd (OOO until July 31)
2016/02/01 23:19:26
While I tested this manually, and it seemed to wor
|
+ base::Unretained(this), State::SUSPENDED)); |
+ return; |
+ } |
+ |
+ if (pending_resume_ && state_ == State::SUSPENDED) { |
+ // If there is a pending seek, resume to that time instead. |
+ if (pending_seek_) { |
+ seek_time_ = pending_seek_time_; |
+ pending_seek_ = false; |
+ } else { |
+ seek_time_ = pipeline_->GetMediaTime(); |
+ } |
+ |
+ // Tell |chunk_demuxer_| to expect our resume. |
+ if (chunk_demuxer_) { |
+ DCHECK(!waiting_for_seek_); |
+ chunk_demuxer_->StartWaitingForSeek(seek_time_); |
+ waiting_for_seek_ = true; |
+ } |
+ |
+ pending_resume_ = false; |
+ state_ = State::RESUMING; |
+ pipeline_->Resume(renderer_factory_cb_.Run(), seek_time_, |
+ base::Bind(&PipelineState::OnPipelineStatus, |
+ base::Unretained(this), State::PLAYING)); |
+ return; |
+ } |
+ |
+ // |chunk_demuxer_| supports aborting seeks. Make use of that when we have |
+ // iother pending operations. |
+ if ((pending_seek_ || pending_suspend_) && waiting_for_seek_) { |
+ CHECK(chunk_demuxer_); |
+ // CancelPendingSeek() may be reentrant, so update state first and return |
+ // immediately. |
+ waiting_for_seek_ = false; |
+ chunk_demuxer_->CancelPendingSeek(pending_seek_time_); |
+ return; |
+ } |
+ |
+ if (pending_seek_ && state_ == State::PLAYING) { |
+ seek_time_ = pending_seek_time_; |
+ |
+ // Tell |chunk_demuxer_| to expect our seek. |
+ if (chunk_demuxer_) { |
+ DCHECK(!waiting_for_seek_); |
+ waiting_for_seek_ = true; |
+ chunk_demuxer_->StartWaitingForSeek(seek_time_); |
+ } |
+ |
+ pending_seek_ = false; |
+ state_ = State::SEEKING; |
+ pipeline_->Seek(seek_time_, |
+ base::Bind(&PipelineState::OnPipelineStatus, |
+ base::Unretained(this), State::PLAYING)); |
+ return; |
+ } |
+ |
+ // If |state_| is PLAYING and we didn't trigger an operation above then we |
+ // are in a stable state. If there is a seeked callback pending, emit it. |
+ if (state_ == State::PLAYING) { |
+ if (pending_seeked_) { |
+ // |seeked_cb_| may be reentrant, so update state first and return |
+ // immediately. |
+ pending_seeked_ = false; |
+ bool was_pending_time_update = pending_time_update_; |
+ pending_time_update_ = false; |
+ seeked_cb_.Run(was_pending_time_update); |
+ return; |
+ } |
+ } |
+} |
+ |
+} // namespace media |