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