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/time/time.h" |
| 11 #include "media/base/media_export.h" |
| 12 #include "media/base/pipeline.h" |
| 13 #include "media/base/renderer.h" |
| 14 |
| 15 namespace media { |
| 16 |
| 17 class ChunkDemuxer; |
| 18 class Demuxer; |
| 19 |
| 20 // PipelineController wraps a Pipeline to expose the one-at-a-time operations |
| 21 // (Seek(), Suspend(), and Resume()) with a simpler API. Internally it tracks |
| 22 // pending operations and dispatches them when possible. Duplicate requests |
| 23 // (such as seeking twice to the same time) may be elided. |
| 24 // |
| 25 // TODO(sandersd): |
| 26 // - Expose an operation that restarts via suspend+resume. |
| 27 // - Block invalid calls after an error occurs. |
| 28 class MEDIA_EXPORT PipelineController { |
| 29 public: |
| 30 enum class State { |
| 31 CREATED, |
| 32 STARTING, |
| 33 PLAYING, |
| 34 SEEKING, |
| 35 SUSPENDING, |
| 36 SUSPENDED, |
| 37 RESUMING, |
| 38 }; |
| 39 |
| 40 typedef base::Callback<scoped_ptr<Renderer>(void)> RendererFactoryCB; |
| 41 typedef base::Callback<void(bool time_updated)> SeekedCB; |
| 42 typedef base::Callback<void()> SuspendedCB; |
| 43 |
| 44 PipelineController(Pipeline* pipeline, |
| 45 const RendererFactoryCB& renderer_factory_cb, |
| 46 const SeekedCB& seeked_cb, |
| 47 const SuspendedCB& suspended_cb, |
| 48 const PipelineStatusCB& error_cb); |
| 49 ~PipelineController(); |
| 50 |
| 51 // Start |pipeline_|. If provided, |chunk_demuxer| will be stored and |
| 52 // StartWaitingForSeek()/CancelPendingSeek() will be issued to it as |
| 53 // necessary. Other parameters are just passed directly to |pipeline_|. |
| 54 void Start(ChunkDemuxer* chunk_demuxer, |
| 55 Demuxer* demuxer, |
| 56 const base::Closure& ended_cb, |
| 57 const PipelineMetadataCB& metadata_cb, |
| 58 const BufferingStateCB& buffering_state_cb, |
| 59 const base::Closure& duration_change_cb, |
| 60 const AddTextTrackCB& add_text_track_cb, |
| 61 const base::Closure& waiting_for_decryption_key_cb); |
| 62 |
| 63 void Seek(base::TimeDelta time, bool time_updated); |
| 64 void Suspend(); |
| 65 void Resume(); |
| 66 |
| 67 // Returns true if the current state is stable. This means that |state_| is |
| 68 // PLAYING and there are no pending operations. Requests are processed |
| 69 // immediately when the state is stable, otherwise they are queued. |
| 70 // (Exception: Resume() is processed immediately while suspended.) |
| 71 bool IsStable(); |
| 72 |
| 73 // Returns true if |pipeline_| is suspended. |
| 74 bool IsSuspended(); |
| 75 |
| 76 private: |
| 77 // Attempts to make progress from the current state to the target state. |
| 78 void Dispatch(); |
| 79 |
| 80 // PipelineStaus callback that also carries the target state. |
| 81 void OnPipelineStatus(State state, PipelineStatus pipeline_status); |
| 82 |
| 83 // The Pipeline we are managing state for. |
| 84 Pipeline* pipeline_ = nullptr; |
| 85 |
| 86 // Factory for Renderers, used for Start() and Resume(). |
| 87 RendererFactoryCB renderer_factory_cb_; |
| 88 |
| 89 // Called after seeks (which includes Start()) upon reaching a stable state. |
| 90 // Multiple seeks result in only one callback if no stable state occurs |
| 91 // between them. |
| 92 SeekedCB seeked_cb_; |
| 93 |
| 94 // Called immediately when |pipeline_| completes a suspend operation. |
| 95 SuspendedCB suspended_cb_; |
| 96 |
| 97 // Called immediately when any operation on |pipeline_| results in an error. |
| 98 PipelineStatusCB error_cb_; |
| 99 |
| 100 // State for handling StartWaitingForSeek()/CancelPendingSeek(). |
| 101 ChunkDemuxer* chunk_demuxer_ = nullptr; |
| 102 bool waiting_for_seek_ = false; |
| 103 |
| 104 // Tracks the current state of |pipeline_|. |
| 105 State state_ = State::CREATED; |
| 106 |
| 107 // Indicates that a seek has occurred. When set, a seeked callback will be |
| 108 // issued at the next stable state. |
| 109 bool pending_seeked_ = false; |
| 110 |
| 111 // Indicates that time has been changed by a seek, which will be reported at |
| 112 // the next seeked callback. |
| 113 bool pending_time_update_ = false; |
| 114 |
| 115 // The target time of the active seek; valid while SEEKING or RESUMING. |
| 116 base::TimeDelta seek_time_; |
| 117 |
| 118 // Target state which we will work to achieve. |pending_seek_time_| is only |
| 119 // valid when |pending_seek_| is true. |
| 120 bool pending_seek_ = false; |
| 121 base::TimeDelta pending_seek_time_; |
| 122 bool pending_suspend_ = false; |
| 123 bool pending_resume_ = false; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(PipelineController); |
| 126 }; |
| 127 |
| 128 } // namespace media |
| 129 |
| 130 #endif // MEDIA_FILTERS_PIPELINE_CONTROLLER_H_ |
OLD | NEW |