OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #ifndef MEDIA_FILTERS_PIPELINE_CONTROLLER_H_ |
| 6 #define MEDIA_FILTERS_PIPELINE_CONTROLLER_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/threading/thread_checker.h" |
| 12 #include "base/time/time.h" |
| 13 #include "media/base/media_export.h" |
| 14 #include "media/base/pipeline.h" |
| 15 #include "media/base/renderer.h" |
| 16 |
| 17 namespace media { |
| 18 |
| 19 class ChunkDemuxer; |
| 20 class Demuxer; |
| 21 |
| 22 // PipelineController wraps a Pipeline to expose the one-at-a-time operations |
| 23 // (Seek(), Suspend(), and Resume()) with a simpler API. Internally it tracks |
| 24 // pending operations and dispatches them when possible. Duplicate requests |
| 25 // (such as seeking twice to the same time) may be elided. |
| 26 // |
| 27 // TODO(sandersd): |
| 28 // - Expose an operation that restarts via suspend+resume. |
| 29 // - Block invalid calls after an error occurs. |
| 30 class MEDIA_EXPORT PipelineController { |
| 31 public: |
| 32 enum class State { |
| 33 CREATED, |
| 34 STARTING, |
| 35 PLAYING, |
| 36 SEEKING, |
| 37 SUSPENDING, |
| 38 SUSPENDED, |
| 39 RESUMING, |
| 40 }; |
| 41 |
| 42 using RendererFactoryCB = base::Callback<scoped_ptr<Renderer>(void)>; |
| 43 using SeekedCB = base::Callback<void(bool time_updated)>; |
| 44 using SuspendedCB = base::Callback<void()>; |
| 45 using ResumedCB = base::Callback<void()>; |
| 46 |
| 47 // Construct a PipelineController wrapping |pipeline_|. |pipeline_| must |
| 48 // outlive the resulting PipelineController. The callbacks are: |
| 49 // - |renderer_factory_cb| is called by PipelineController to create new |
| 50 // renderers when starting and resuming. |
| 51 // - |seeked_cb| is called upon reaching a stable state if a seek occured. |
| 52 // - |suspended_cb| is called immediately after suspendeding. |
| 53 // - |resumed_cb| is called immediately after resuming. |
| 54 // - |error_cb| is called if any operation on |pipeline_| does not result |
| 55 // in PIPELINE_OK or its error callback is called. |
| 56 PipelineController(Pipeline* pipeline, |
| 57 const RendererFactoryCB& renderer_factory_cb, |
| 58 const SeekedCB& seeked_cb, |
| 59 const SuspendedCB& suspended_cb, |
| 60 const ResumedCB& resumed_cb, |
| 61 const PipelineStatusCB& error_cb); |
| 62 ~PipelineController(); |
| 63 |
| 64 // Start |pipeline_|. If provided, |chunk_demuxer| will be stored and |
| 65 // StartWaitingForSeek()/CancelPendingSeek() will be issued to it as |
| 66 // necessary. |
| 67 // |
| 68 // When |is_streaming| is true, Resume() will always start at the |
| 69 // beginning of the stream, rather than attempting to seek to the current |
| 70 // time. |
| 71 // |
| 72 // The other parameters are just passed directly to pipeline_.Start(). |
| 73 void Start(ChunkDemuxer* chunk_demuxer, |
| 74 Demuxer* demuxer, |
| 75 bool is_streaming, |
| 76 const base::Closure& ended_cb, |
| 77 const PipelineMetadataCB& metadata_cb, |
| 78 const BufferingStateCB& buffering_state_cb, |
| 79 const base::Closure& duration_change_cb, |
| 80 const AddTextTrackCB& add_text_track_cb, |
| 81 const base::Closure& waiting_for_decryption_key_cb); |
| 82 |
| 83 // Request a seek to |time|. If |time_updated| is true, then the eventual |
| 84 // |seeked_cb| callback will also have |time_updated| set to true; it |
| 85 // indicates that the seek was requested by Blink and a time update is |
| 86 // expected so that Blink can fire the seeked event. |
| 87 void Seek(base::TimeDelta time, bool time_updated); |
| 88 |
| 89 // Request that |pipeline_| be suspended. This is a no-op if |pipeline_| has |
| 90 // been suspended. |
| 91 void Suspend(); |
| 92 |
| 93 // Request that |pipeline_| be resumed. This is a no-op if |pipeline_| has not |
| 94 // been suspended. |
| 95 void Resume(); |
| 96 |
| 97 // Returns true if the current state is stable. This means that |state_| is |
| 98 // PLAYING and there are no pending operations. Requests are processed |
| 99 // immediately when the state is stable, otherwise they are queued. |
| 100 // |
| 101 // Exceptions to the above: |
| 102 // - Start() is processed immediately while in the CREATED state. |
| 103 // - Resume() is processed immediately while in the SUSPENDED state. |
| 104 bool IsStable(); |
| 105 |
| 106 // Returns true if |pipeline_| is suspended. |
| 107 bool IsSuspended(); |
| 108 |
| 109 private: |
| 110 // Attempts to make progress from the current state to the target state. |
| 111 void Dispatch(); |
| 112 |
| 113 // PipelineStaus callback that also carries the target state. |
| 114 void OnPipelineStatus(State state, PipelineStatus pipeline_status); |
| 115 |
| 116 // The Pipeline we are managing state for. |
| 117 Pipeline* pipeline_ = nullptr; |
| 118 |
| 119 // Factory for Renderers, used for Start() and Resume(). |
| 120 RendererFactoryCB renderer_factory_cb_; |
| 121 |
| 122 // Called after seeks (which includes Start()) upon reaching a stable state. |
| 123 // Multiple seeks result in only one callback if no stable state occurs |
| 124 // between them. |
| 125 SeekedCB seeked_cb_; |
| 126 |
| 127 // Called immediately when |pipeline_| completes a suspend operation. |
| 128 SuspendedCB suspended_cb_; |
| 129 |
| 130 // Called immediately when |pipeline_| completes a resume operation. |
| 131 ResumedCB resumed_cb_; |
| 132 |
| 133 // Called immediately when any operation on |pipeline_| results in an error. |
| 134 PipelineStatusCB error_cb_; |
| 135 |
| 136 // State for handling StartWaitingForSeek()/CancelPendingSeek(). |
| 137 ChunkDemuxer* chunk_demuxer_ = nullptr; |
| 138 bool waiting_for_seek_ = false; |
| 139 |
| 140 // When true, Resume() will start at time zero instead of seeking to the |
| 141 // current time. |
| 142 bool is_streaming_ = false; |
| 143 |
| 144 // Tracks the current state of |pipeline_|. |
| 145 State state_ = State::CREATED; |
| 146 |
| 147 // Indicates that a seek has occurred. When set, a seeked callback will be |
| 148 // issued at the next stable state. |
| 149 bool pending_seeked_cb_ = false; |
| 150 |
| 151 // Indicates that time has been changed by a seek, which will be reported at |
| 152 // the next seeked callback. |
| 153 bool pending_time_updated_ = false; |
| 154 |
| 155 // Indicates that the |pipeline_| was suspended, and therefore that a resumed |
| 156 // callback should be issued the next time we enter State::PLAYING. |
| 157 bool pending_resumed_cb_ = false; |
| 158 |
| 159 // The target time of the active seek; valid while SEEKING or RESUMING. |
| 160 base::TimeDelta seek_time_; |
| 161 |
| 162 // Target state which we will work to achieve. |pending_seek_time_| is only |
| 163 // valid when |pending_seek_| is true. |
| 164 bool pending_seek_ = false; |
| 165 base::TimeDelta pending_seek_time_; |
| 166 bool pending_suspend_ = false; |
| 167 bool pending_resume_ = false; |
| 168 |
| 169 base::ThreadChecker thread_checker_; |
| 170 base::WeakPtrFactory<PipelineController> weak_factory_; |
| 171 |
| 172 DISALLOW_COPY_AND_ASSIGN(PipelineController); |
| 173 }; |
| 174 |
| 175 } // namespace media |
| 176 |
| 177 #endif // MEDIA_FILTERS_PIPELINE_CONTROLLER_H_ |
OLD | NEW |