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 #include "media/filters/pipeline_controller.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "build/build_config.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 PipelineStatusCB& error_cb) |
| 20 : pipeline_(pipeline), |
| 21 renderer_factory_cb_(renderer_factory_cb), |
| 22 seeked_cb_(seeked_cb), |
| 23 suspended_cb_(suspended_cb), |
| 24 error_cb_(error_cb) { |
| 25 DCHECK(pipeline_); |
| 26 DCHECK(!renderer_factory_cb_.is_null()); |
| 27 DCHECK(!seeked_cb_.is_null()); |
| 28 DCHECK(!suspended_cb_.is_null()); |
| 29 DCHECK(!error_cb_.is_null()); |
| 30 } |
| 31 |
| 32 PipelineController::~PipelineController() {} |
| 33 |
| 34 // TODO(sandersd): Move ChunkDemuxer API to Demuxer so that Pipeline can |
| 35 // implement all of this. |
| 36 // TODO(sandersd): If there is a pending suspend, don't call pipeline_.Start() |
| 37 // until Resume(). |
| 38 void PipelineController::Start( |
| 39 ChunkDemuxer* chunk_demuxer, |
| 40 Demuxer* demuxer, |
| 41 const base::Closure& ended_cb, |
| 42 const PipelineMetadataCB& metadata_cb, |
| 43 const BufferingStateCB& buffering_state_cb, |
| 44 const base::Closure& duration_change_cb, |
| 45 const AddTextTrackCB& add_text_track_cb, |
| 46 const base::Closure& waiting_for_decryption_key_cb) { |
| 47 DCHECK(state_ == State::CREATED); |
| 48 |
| 49 // Once the pipeline is started, we want to call the seeked callback but |
| 50 // without a time update. |
| 51 pending_seeked_ = true; |
| 52 state_ = State::STARTING; |
| 53 |
| 54 chunk_demuxer_ = chunk_demuxer; |
| 55 pipeline_->Start(demuxer, renderer_factory_cb_.Run(), ended_cb, error_cb_, |
| 56 base::Bind(&PipelineController::OnPipelineStatus, |
| 57 base::Unretained(this), State::PLAYING), |
| 58 metadata_cb, buffering_state_cb, duration_change_cb, |
| 59 add_text_track_cb, waiting_for_decryption_key_cb); |
| 60 } |
| 61 |
| 62 void PipelineController::Seek(base::TimeDelta time, bool time_updated) { |
| 63 // It would be slightly more clear to set this in Dispatch(), but we want to |
| 64 // be sure it gets updated even if the seek is elided. |
| 65 if (time_updated) |
| 66 pending_time_update_ = true; |
| 67 pending_seeked_ = true; |
| 68 |
| 69 // If we are already seeking to |time|, just clear any pending seek. This does |
| 70 // not apply to MSE because the underlying buffer could have been changed |
| 71 // between the seek calls. |
| 72 // TODO(sandersd): The underlying buffer could also have changed for |
| 73 // File objects, but WMPI is also broken in that case (because it caches). |
| 74 if ((state_ == State::SEEKING || state_ == State::RESUMING) && |
| 75 seek_time_ == time && !chunk_demuxer_) { |
| 76 pending_seek_ = false; |
| 77 return; |
| 78 } |
| 79 |
| 80 pending_seek_time_ = time; |
| 81 pending_seek_ = true; |
| 82 Dispatch(); |
| 83 } |
| 84 |
| 85 // TODO(sandersd): It may be easier to use this interface if |suspended_cb_| is |
| 86 // executed when Suspend() is called while already suspended. |
| 87 void PipelineController::Suspend() { |
| 88 pending_resume_ = false; |
| 89 if (state_ != State::SUSPENDING && state_ != State::SUSPENDED) { |
| 90 pending_suspend_ = true; |
| 91 Dispatch(); |
| 92 } |
| 93 } |
| 94 |
| 95 void PipelineController::Resume() { |
| 96 pending_suspend_ = false; |
| 97 if (state_ == State::SUSPENDING || state_ == State::SUSPENDED) { |
| 98 pending_resume_ = true; |
| 99 Dispatch(); |
| 100 } |
| 101 } |
| 102 |
| 103 bool PipelineController::IsStable() { |
| 104 return (state_ == State::PLAYING); |
| 105 } |
| 106 |
| 107 bool PipelineController::IsSuspended() { |
| 108 return (state_ == State::SUSPENDED); |
| 109 } |
| 110 |
| 111 void PipelineController::OnPipelineStatus(State state, |
| 112 PipelineStatus pipeline_status) { |
| 113 if (pipeline_status != PIPELINE_OK) { |
| 114 error_cb_.Run(pipeline_status); |
| 115 return; |
| 116 } |
| 117 |
| 118 state_ = state; |
| 119 |
| 120 // Start(), Seek(), or Resume() completed; we can be sure that |
| 121 // |chunk_demuxer_| got the seek it was waiting for. |
| 122 if (state == State::PLAYING) |
| 123 waiting_for_seek_ = false; |
| 124 |
| 125 // Sadly we need to signal this state change via a possibly reentrant |
| 126 // callback. Keep in mind that the state may change inside the callback! |
| 127 // (In particular, it must be safe to call Dispatch() twice in a row here.) |
| 128 if (state == State::SUSPENDED) |
| 129 suspended_cb_.Run(); |
| 130 |
| 131 Dispatch(); |
| 132 } |
| 133 |
| 134 // Note: Dispatch() may be called re-entrantly (by callbacks internally) or |
| 135 // twice in a row (by OnPipelineStatus()). |
| 136 void PipelineController::Dispatch() { |
| 137 // Suspend/resume transitions take priority because seeks before a suspend |
| 138 // are wasted, and seeks after can be merged into the resume operation. |
| 139 if (pending_suspend_ && state_ == State::PLAYING) { |
| 140 pending_suspend_ = false; |
| 141 state_ = State::SUSPENDING; |
| 142 pipeline_->Suspend(base::Bind(&PipelineController::OnPipelineStatus, |
| 143 base::Unretained(this), State::SUSPENDED)); |
| 144 return; |
| 145 } |
| 146 |
| 147 if (pending_resume_ && state_ == State::SUSPENDED) { |
| 148 // If there is a pending seek, resume to that time instead. |
| 149 if (pending_seek_) { |
| 150 seek_time_ = pending_seek_time_; |
| 151 pending_seek_ = false; |
| 152 } else { |
| 153 seek_time_ = pipeline_->GetMediaTime(); |
| 154 } |
| 155 |
| 156 // Tell |chunk_demuxer_| to expect our resume. |
| 157 if (chunk_demuxer_) { |
| 158 DCHECK(!waiting_for_seek_); |
| 159 chunk_demuxer_->StartWaitingForSeek(seek_time_); |
| 160 waiting_for_seek_ = true; |
| 161 } |
| 162 |
| 163 pending_resume_ = false; |
| 164 state_ = State::RESUMING; |
| 165 pipeline_->Resume(renderer_factory_cb_.Run(), seek_time_, |
| 166 base::Bind(&PipelineController::OnPipelineStatus, |
| 167 base::Unretained(this), State::PLAYING)); |
| 168 return; |
| 169 } |
| 170 |
| 171 // |chunk_demuxer_| supports aborting seeks. Make use of that when we have |
| 172 // other pending operations. |
| 173 if ((pending_seek_ || pending_suspend_) && waiting_for_seek_) { |
| 174 CHECK(chunk_demuxer_); |
| 175 |
| 176 // If there is no pending seek, return the current seek to pending status. |
| 177 if (!pending_seek_) { |
| 178 pending_seek_time_ = seek_time_; |
| 179 pending_seek_ = true; |
| 180 } |
| 181 |
| 182 // CancelPendingSeek() may be reentrant, so update state first and return |
| 183 // immediately. |
| 184 waiting_for_seek_ = false; |
| 185 chunk_demuxer_->CancelPendingSeek(pending_seek_time_); |
| 186 return; |
| 187 } |
| 188 |
| 189 // Ordinary seeking. |
| 190 if (pending_seek_ && state_ == State::PLAYING) { |
| 191 seek_time_ = pending_seek_time_; |
| 192 |
| 193 // Tell |chunk_demuxer_| to expect our seek. |
| 194 if (chunk_demuxer_) { |
| 195 DCHECK(!waiting_for_seek_); |
| 196 waiting_for_seek_ = true; |
| 197 chunk_demuxer_->StartWaitingForSeek(seek_time_); |
| 198 } |
| 199 |
| 200 pending_seek_ = false; |
| 201 state_ = State::SEEKING; |
| 202 pipeline_->Seek(seek_time_, |
| 203 base::Bind(&PipelineController::OnPipelineStatus, |
| 204 base::Unretained(this), State::PLAYING)); |
| 205 return; |
| 206 } |
| 207 |
| 208 // If |state_| is PLAYING and we didn't trigger an operation above then we |
| 209 // are in a stable state. If there is a seeked callback pending, emit it. |
| 210 if (state_ == State::PLAYING) { |
| 211 if (pending_seeked_) { |
| 212 // |seeked_cb_| may be reentrant, so update state first and return |
| 213 // immediately. |
| 214 pending_seeked_ = false; |
| 215 bool was_pending_time_update = pending_time_update_; |
| 216 pending_time_update_ = false; |
| 217 seeked_cb_.Run(was_pending_time_update); |
| 218 return; |
| 219 } |
| 220 } |
| 221 } |
| 222 |
| 223 } // namespace media |
OLD | NEW |