| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/filters/pipeline_controller.h" | 5 #include "media/filters/pipeline_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "media/base/bind_to_current_loop.h" | |
| 10 #include "media/base/demuxer.h" | 8 #include "media/base/demuxer.h" |
| 11 | 9 |
| 12 namespace media { | 10 namespace media { |
| 13 | 11 |
| 14 PipelineController::PipelineController( | 12 PipelineController::PipelineController( |
| 15 Pipeline* pipeline, | 13 Pipeline* pipeline, |
| 16 const RendererFactoryCB& renderer_factory_cb, | 14 const RendererFactoryCB& renderer_factory_cb, |
| 17 const SeekedCB& seeked_cb, | 15 const SeekedCB& seeked_cb, |
| 18 const SuspendedCB& suspended_cb, | 16 const SuspendedCB& suspended_cb, |
| 19 const PipelineStatusCB& error_cb) | 17 const PipelineStatusCB& error_cb) |
| 20 : pipeline_(pipeline), | 18 : pipeline_(pipeline), |
| 21 renderer_factory_cb_(renderer_factory_cb), | 19 renderer_factory_cb_(renderer_factory_cb), |
| 22 seeked_cb_(seeked_cb), | 20 seeked_cb_(seeked_cb), |
| 23 suspended_cb_(suspended_cb), | 21 suspended_cb_(suspended_cb), |
| 24 error_cb_(error_cb), | 22 error_cb_(error_cb), |
| 25 weak_factory_(this) { | 23 weak_factory_(this) { |
| 26 DCHECK(pipeline_); | 24 DCHECK(pipeline_); |
| 27 DCHECK(!renderer_factory_cb_.is_null()); | 25 DCHECK(!renderer_factory_cb_.is_null()); |
| 28 DCHECK(!seeked_cb_.is_null()); | 26 DCHECK(!seeked_cb_.is_null()); |
| 29 DCHECK(!suspended_cb_.is_null()); | 27 DCHECK(!suspended_cb_.is_null()); |
| 30 DCHECK(!error_cb_.is_null()); | 28 DCHECK(!error_cb_.is_null()); |
| 31 } | 29 } |
| 32 | 30 |
| 33 PipelineController::~PipelineController() { | 31 PipelineController::~PipelineController() { |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); | 32 DCHECK(thread_checker_.CalledOnValidThread()); |
| 35 } | 33 } |
| 36 | 34 |
| 37 // TODO(sandersd): If there is a pending suspend, don't call pipeline_.Start() | 35 // TODO(sandersd): If there is a pending suspend, don't call pipeline_.Start() |
| 38 // until Resume(). | 36 // until Resume(). |
| 39 void PipelineController::Start( | 37 void PipelineController::Start(Demuxer* demuxer, |
| 40 Demuxer* demuxer, | 38 Pipeline::Client* client, |
| 41 bool is_streaming, | 39 bool is_streaming, |
| 42 bool is_static, | 40 bool is_static) { |
| 43 const base::Closure& ended_cb, | |
| 44 const PipelineMetadataCB& metadata_cb, | |
| 45 const BufferingStateCB& buffering_state_cb, | |
| 46 const base::Closure& duration_change_cb, | |
| 47 const AddTextTrackCB& add_text_track_cb, | |
| 48 const base::Closure& waiting_for_decryption_key_cb) { | |
| 49 DCHECK(thread_checker_.CalledOnValidThread()); | 41 DCHECK(thread_checker_.CalledOnValidThread()); |
| 50 DCHECK(state_ == State::CREATED); | 42 DCHECK(state_ == State::CREATED); |
| 51 DCHECK(demuxer); | 43 DCHECK(demuxer); |
| 52 | 44 |
| 53 // Once the pipeline is started, we want to call the seeked callback but | 45 // Once the pipeline is started, we want to call the seeked callback but |
| 54 // without a time update. | 46 // without a time update. |
| 55 pending_seeked_cb_ = true; | 47 pending_seeked_cb_ = true; |
| 56 state_ = State::STARTING; | 48 state_ = State::STARTING; |
| 57 | 49 |
| 58 demuxer_ = demuxer; | 50 demuxer_ = demuxer; |
| 59 is_streaming_ = is_streaming; | 51 is_streaming_ = is_streaming; |
| 60 is_static_ = is_static; | 52 is_static_ = is_static; |
| 61 pipeline_->Start( | 53 pipeline_->Start(demuxer, renderer_factory_cb_.Run(), client, |
| 62 demuxer, renderer_factory_cb_.Run(), ended_cb, | 54 base::Bind(&PipelineController::OnPipelineStatus, |
| 63 BindToCurrentLoop(error_cb_), | 55 weak_factory_.GetWeakPtr(), State::PLAYING)); |
| 64 BindToCurrentLoop(base::Bind(&PipelineController::OnPipelineStatus, | |
| 65 weak_factory_.GetWeakPtr(), State::PLAYING)), | |
| 66 metadata_cb, buffering_state_cb, duration_change_cb, add_text_track_cb, | |
| 67 waiting_for_decryption_key_cb); | |
| 68 } | 56 } |
| 69 | 57 |
| 70 void PipelineController::Seek(base::TimeDelta time, bool time_updated) { | 58 void PipelineController::Seek(base::TimeDelta time, bool time_updated) { |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); | 59 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 | 60 |
| 73 // It would be slightly more clear to set this in Dispatch(), but we want to | 61 // It would be slightly more clear to set this in Dispatch(), but we want to |
| 74 // be sure it gets updated even if the seek is elided. | 62 // be sure it gets updated even if the seek is elided. |
| 75 if (time_updated) | 63 if (time_updated) |
| 76 pending_time_updated_ = true; | 64 pending_time_updated_ = true; |
| 77 pending_seeked_cb_ = true; | 65 pending_seeked_cb_ = true; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 // Note: Dispatch() may be called re-entrantly (by callbacks internally) or | 139 // Note: Dispatch() may be called re-entrantly (by callbacks internally) or |
| 152 // twice in a row (by OnPipelineStatus()). | 140 // twice in a row (by OnPipelineStatus()). |
| 153 void PipelineController::Dispatch() { | 141 void PipelineController::Dispatch() { |
| 154 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 155 | 143 |
| 156 // Suspend/resume transitions take priority because seeks before a suspend | 144 // Suspend/resume transitions take priority because seeks before a suspend |
| 157 // are wasted, and seeks after can be merged into the resume operation. | 145 // are wasted, and seeks after can be merged into the resume operation. |
| 158 if (pending_suspend_ && state_ == State::PLAYING) { | 146 if (pending_suspend_ && state_ == State::PLAYING) { |
| 159 pending_suspend_ = false; | 147 pending_suspend_ = false; |
| 160 state_ = State::SUSPENDING; | 148 state_ = State::SUSPENDING; |
| 161 pipeline_->Suspend(BindToCurrentLoop( | 149 pipeline_->Suspend(base::Bind(&PipelineController::OnPipelineStatus, |
| 162 base::Bind(&PipelineController::OnPipelineStatus, | 150 weak_factory_.GetWeakPtr(), |
| 163 weak_factory_.GetWeakPtr(), State::SUSPENDED))); | 151 State::SUSPENDED)); |
| 164 return; | 152 return; |
| 165 } | 153 } |
| 166 | 154 |
| 167 if (pending_resume_ && state_ == State::SUSPENDED) { | 155 if (pending_resume_ && state_ == State::SUSPENDED) { |
| 168 // If there is a pending seek, resume to that time instead... | 156 // If there is a pending seek, resume to that time instead... |
| 169 if (pending_seek_) { | 157 if (pending_seek_) { |
| 170 seek_time_ = pending_seek_time_; | 158 seek_time_ = pending_seek_time_; |
| 171 pending_seek_ = false; | 159 pending_seek_ = false; |
| 172 } else { | 160 } else { |
| 173 seek_time_ = pipeline_->GetMediaTime(); | 161 seek_time_ = pipeline_->GetMediaTime(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 184 } | 172 } |
| 185 | 173 |
| 186 // Tell |demuxer_| to expect our resume. | 174 // Tell |demuxer_| to expect our resume. |
| 187 DCHECK(!waiting_for_seek_); | 175 DCHECK(!waiting_for_seek_); |
| 188 waiting_for_seek_ = true; | 176 waiting_for_seek_ = true; |
| 189 demuxer_->StartWaitingForSeek(seek_time_); | 177 demuxer_->StartWaitingForSeek(seek_time_); |
| 190 | 178 |
| 191 pending_resume_ = false; | 179 pending_resume_ = false; |
| 192 state_ = State::RESUMING; | 180 state_ = State::RESUMING; |
| 193 pipeline_->Resume(renderer_factory_cb_.Run(), seek_time_, | 181 pipeline_->Resume(renderer_factory_cb_.Run(), seek_time_, |
| 194 BindToCurrentLoop(base::Bind( | 182 base::Bind(&PipelineController::OnPipelineStatus, |
| 195 &PipelineController::OnPipelineStatus, | 183 weak_factory_.GetWeakPtr(), State::PLAYING)); |
| 196 weak_factory_.GetWeakPtr(), State::PLAYING))); | |
| 197 return; | 184 return; |
| 198 } | 185 } |
| 199 | 186 |
| 200 // If we have pending operations, and a seek is ongoing, abort it. | 187 // If we have pending operations, and a seek is ongoing, abort it. |
| 201 if ((pending_seek_ || pending_suspend_) && waiting_for_seek_) { | 188 if ((pending_seek_ || pending_suspend_) && waiting_for_seek_) { |
| 202 // If there is no pending seek, return the current seek to pending status. | 189 // If there is no pending seek, return the current seek to pending status. |
| 203 if (!pending_seek_) { | 190 if (!pending_seek_) { |
| 204 pending_seek_time_ = seek_time_; | 191 pending_seek_time_ = seek_time_; |
| 205 pending_seek_ = true; | 192 pending_seek_ = true; |
| 206 } | 193 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 217 seek_time_ = pending_seek_time_; | 204 seek_time_ = pending_seek_time_; |
| 218 | 205 |
| 219 // Tell |demuxer_| to expect our seek. | 206 // Tell |demuxer_| to expect our seek. |
| 220 DCHECK(!waiting_for_seek_); | 207 DCHECK(!waiting_for_seek_); |
| 221 waiting_for_seek_ = true; | 208 waiting_for_seek_ = true; |
| 222 demuxer_->StartWaitingForSeek(seek_time_); | 209 demuxer_->StartWaitingForSeek(seek_time_); |
| 223 | 210 |
| 224 pending_seek_ = false; | 211 pending_seek_ = false; |
| 225 state_ = State::SEEKING; | 212 state_ = State::SEEKING; |
| 226 pipeline_->Seek(seek_time_, | 213 pipeline_->Seek(seek_time_, |
| 227 BindToCurrentLoop(base::Bind( | 214 base::Bind(&PipelineController::OnPipelineStatus, |
| 228 &PipelineController::OnPipelineStatus, | 215 weak_factory_.GetWeakPtr(), State::PLAYING)); |
| 229 weak_factory_.GetWeakPtr(), State::PLAYING))); | |
| 230 return; | 216 return; |
| 231 } | 217 } |
| 232 | 218 |
| 233 // If |state_| is PLAYING and we didn't trigger an operation above then we | 219 // If |state_| is PLAYING and we didn't trigger an operation above then we |
| 234 // are in a stable state. If there is a seeked callback pending, emit it. | 220 // are in a stable state. If there is a seeked callback pending, emit it. |
| 235 if (state_ == State::PLAYING) { | 221 if (state_ == State::PLAYING) { |
| 236 if (pending_seeked_cb_) { | 222 if (pending_seeked_cb_) { |
| 237 // |seeked_cb_| may be reentrant, so update state first and return | 223 // |seeked_cb_| may be reentrant, so update state first and return |
| 238 // immediately. | 224 // immediately. |
| 239 pending_seeked_cb_ = false; | 225 pending_seeked_cb_ = false; |
| 240 bool was_pending_time_updated = pending_time_updated_; | 226 bool was_pending_time_updated = pending_time_updated_; |
| 241 pending_time_updated_ = false; | 227 pending_time_updated_ = false; |
| 242 seeked_cb_.Run(was_pending_time_updated); | 228 seeked_cb_.Run(was_pending_time_updated); |
| 243 return; | 229 return; |
| 244 } | 230 } |
| 245 } | 231 } |
| 246 } | 232 } |
| 247 | 233 |
| 248 } // namespace media | 234 } // namespace media |
| OLD | NEW |