Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(503)

Side by Side Diff: media/filters/pipeline_controller.cc

Issue 1641423002: Re-land extract state management from WebMediaPlayerImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add thread checker. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "media/filters/pipeline_controller.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "media/base/bind_to_current_loop.h"
10 #include "media/filters/chunk_demuxer.h"
11
12 namespace media {
13
14 PipelineController::PipelineController(
15 Pipeline* pipeline,
16 const RendererFactoryCB& renderer_factory_cb,
17 const SeekedCB& seeked_cb,
18 const SuspendedCB& suspended_cb,
19 const ResumedCB& resumed_cb,
20 const PipelineStatusCB& error_cb)
21 : pipeline_(pipeline),
22 renderer_factory_cb_(renderer_factory_cb),
23 seeked_cb_(seeked_cb),
24 suspended_cb_(suspended_cb),
25 resumed_cb_(resumed_cb),
26 error_cb_(error_cb),
27 weak_factory_(this) {
28 DCHECK(pipeline_);
29 DCHECK(!renderer_factory_cb_.is_null());
30 DCHECK(!seeked_cb_.is_null());
31 DCHECK(!suspended_cb_.is_null());
32 DCHECK(!resumed_cb_.is_null());
33 DCHECK(!error_cb_.is_null());
34 }
35
36 PipelineController::~PipelineController() {
37 DCHECK(thread_checker_.CalledOnValidThread());
38 }
39
40 // TODO(sandersd): Move ChunkDemuxer API to Demuxer so that Pipeline can
41 // implement all of this.
42 // TODO(sandersd): If there is a pending suspend, don't call pipeline_.Start()
43 // until Resume().
44 void PipelineController::Start(
45 ChunkDemuxer* chunk_demuxer,
46 Demuxer* demuxer,
47 bool is_streaming,
48 const base::Closure& ended_cb,
49 const PipelineMetadataCB& metadata_cb,
50 const BufferingStateCB& buffering_state_cb,
51 const base::Closure& duration_change_cb,
52 const AddTextTrackCB& add_text_track_cb,
53 const base::Closure& waiting_for_decryption_key_cb) {
54 DCHECK(thread_checker_.CalledOnValidThread());
55 DCHECK(state_ == State::CREATED);
56
57 if (chunk_demuxer)
58 DCHECK_EQ(demuxer, chunk_demuxer);
59
60 // Once the pipeline is started, we want to call the seeked callback but
61 // without a time update.
62 pending_seeked_cb_ = true;
63 state_ = State::STARTING;
64
65 chunk_demuxer_ = chunk_demuxer;
66 is_streaming_ = is_streaming;
67 pipeline_->Start(
68 demuxer, renderer_factory_cb_.Run(), ended_cb, error_cb_,
69 BindToCurrentLoop(base::Bind(&PipelineController::OnPipelineStatus,
70 weak_factory_.GetWeakPtr(), State::PLAYING)),
71 metadata_cb, buffering_state_cb, duration_change_cb, add_text_track_cb,
72 waiting_for_decryption_key_cb);
73 }
74
75 void PipelineController::Seek(base::TimeDelta time, bool time_updated) {
76 DCHECK(thread_checker_.CalledOnValidThread());
77
78 // It would be slightly more clear to set this in Dispatch(), but we want to
79 // be sure it gets updated even if the seek is elided.
80 if (time_updated)
81 pending_time_updated_ = true;
82 pending_seeked_cb_ = true;
83
84 // If we are already seeking to |time|, just clear any pending seek. This does
85 // not apply to MSE because the underlying buffer could have been changed
86 // between the seek calls.
87 // TODO(sandersd): The underlying buffer could also have changed for
88 // File objects, but WMPI is also broken in that case (because it caches).
89 if ((state_ == State::SEEKING || state_ == State::RESUMING) &&
90 seek_time_ == time && !chunk_demuxer_) {
91 pending_seek_ = false;
92 return;
93 }
94
95 pending_seek_time_ = time;
96 pending_seek_ = true;
97 Dispatch();
98 }
99
100 // TODO(sandersd): It may be easier to use this interface if |suspended_cb_| is
101 // executed when Suspend() is called while already suspended.
102 void PipelineController::Suspend() {
103 DCHECK(thread_checker_.CalledOnValidThread());
104 pending_resume_ = false;
105 if (state_ != State::SUSPENDING && state_ != State::SUSPENDED) {
106 pending_suspend_ = true;
107 Dispatch();
108 }
109 }
110
111 void PipelineController::Resume() {
112 DCHECK(thread_checker_.CalledOnValidThread());
113 pending_suspend_ = false;
114 if (state_ == State::SUSPENDING || state_ == State::SUSPENDED) {
115 pending_resume_ = true;
116 Dispatch();
117 }
118 }
119
120 bool PipelineController::IsStable() {
121 DCHECK(thread_checker_.CalledOnValidThread());
122 return (state_ == State::PLAYING);
123 }
124
125 bool PipelineController::IsSuspended() {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 return (state_ == State::SUSPENDED);
128 }
129
130 void PipelineController::OnPipelineStatus(State state,
131 PipelineStatus pipeline_status) {
132 DCHECK(thread_checker_.CalledOnValidThread());
133
134 if (pipeline_status != PIPELINE_OK) {
135 error_cb_.Run(pipeline_status);
136 return;
137 }
138
139 state_ = state;
140
141 if (state == State::PLAYING) {
142 // Start(), Seek(), or Resume() completed; we can be sure that
143 // |chunk_demuxer_| got the seek it was waiting for.
144 waiting_for_seek_ = false;
145 if (pending_resumed_cb_) {
146 pending_resumed_cb_ = false;
147
148 // Warning: possibly reentrant. The state may change inside this callback.
149 // It must be safe to call Dispatch() twice in a row here.
150 resumed_cb_.Run();
151 }
152 } else if (state == State::SUSPENDED) {
153 pending_resumed_cb_ = true;
154
155 // Warning: possibly reentrant. The state may change inside this callback.
156 // It must be safe to call Dispatch() twice in a row here.
157 suspended_cb_.Run();
158 }
159
160 Dispatch();
161 }
162
163 // Note: Dispatch() may be called re-entrantly (by callbacks internally) or
164 // twice in a row (by OnPipelineStatus()).
165 void PipelineController::Dispatch() {
166 DCHECK(thread_checker_.CalledOnValidThread());
167
168 // Suspend/resume transitions take priority because seeks before a suspend
169 // are wasted, and seeks after can be merged into the resume operation.
170 if (pending_suspend_ && state_ == State::PLAYING) {
171 pending_suspend_ = false;
172 state_ = State::SUSPENDING;
173 pipeline_->Suspend(BindToCurrentLoop(
174 base::Bind(&PipelineController::OnPipelineStatus,
175 weak_factory_.GetWeakPtr(), State::SUSPENDED)));
176 return;
177 }
178
179 if (pending_resume_ && state_ == State::SUSPENDED) {
180 // If there is a pending seek, resume to that time instead.
181 if (pending_seek_) {
182 seek_time_ = pending_seek_time_;
183 pending_seek_ = false;
184 } else {
185 seek_time_ = pipeline_->GetMediaTime();
186 }
187
188 // Unless the media is streaming, in which case we resume at the start
189 // because seeking doesn't work well.
190 if (is_streaming_ && !seek_time_.is_zero()) {
191 seek_time_ = base::TimeDelta();
192
193 // In this case we want to make sure that the controls get updated
194 // immediately, so we don't try to hide the seek.
195 pending_time_updated_ = true;
196 }
197
198 // Tell |chunk_demuxer_| to expect our resume.
199 if (chunk_demuxer_) {
200 DCHECK(!waiting_for_seek_);
201 chunk_demuxer_->StartWaitingForSeek(seek_time_);
202 waiting_for_seek_ = true;
203 }
204
205 pending_resume_ = false;
206 state_ = State::RESUMING;
207 pipeline_->Resume(renderer_factory_cb_.Run(), seek_time_,
208 BindToCurrentLoop(base::Bind(
209 &PipelineController::OnPipelineStatus,
210 weak_factory_.GetWeakPtr(), State::PLAYING)));
211 return;
212 }
213
214 // |chunk_demuxer_| supports aborting seeks. Make use of that when we have
215 // other pending operations.
216 if ((pending_seek_ || pending_suspend_) && waiting_for_seek_) {
217 CHECK(chunk_demuxer_);
218
219 // If there is no pending seek, return the current seek to pending status.
220 if (!pending_seek_) {
221 pending_seek_time_ = seek_time_;
222 pending_seek_ = true;
223 }
224
225 // CancelPendingSeek() may be reentrant, so update state first and return
226 // immediately.
227 waiting_for_seek_ = false;
228 chunk_demuxer_->CancelPendingSeek(pending_seek_time_);
229 return;
230 }
231
232 // Ordinary seeking.
233 if (pending_seek_ && state_ == State::PLAYING) {
234 seek_time_ = pending_seek_time_;
235
236 // Tell |chunk_demuxer_| to expect our seek.
237 if (chunk_demuxer_) {
238 DCHECK(!waiting_for_seek_);
239 waiting_for_seek_ = true;
240 chunk_demuxer_->StartWaitingForSeek(seek_time_);
241 }
242
243 pending_seek_ = false;
244 state_ = State::SEEKING;
245 pipeline_->Seek(seek_time_,
246 BindToCurrentLoop(base::Bind(
247 &PipelineController::OnPipelineStatus,
248 weak_factory_.GetWeakPtr(), State::PLAYING)));
249 return;
250 }
251
252 // If |state_| is PLAYING and we didn't trigger an operation above then we
253 // are in a stable state. If there is a seeked callback pending, emit it.
254 if (state_ == State::PLAYING) {
255 if (pending_seeked_cb_) {
256 // |seeked_cb_| may be reentrant, so update state first and return
257 // immediately.
258 pending_seeked_cb_ = false;
259 bool was_pending_time_updated = pending_time_updated_;
260 pending_time_updated_ = false;
261 seeked_cb_.Run(was_pending_time_updated);
262 return;
263 }
264 }
265 }
266
267 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698