pipeline_controller.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #include "media/filters/pipeline_controller.h"
  5. #include "base/bind.h"
  6. #include "media/base/demuxer.h"
  7. namespace media {
  8. PipelineController::PipelineController(std::unique_ptr<Pipeline> pipeline,
  9. SeekedCB seeked_cb,
  10. SuspendedCB suspended_cb,
  11. BeforeResumeCB before_resume_cb,
  12. ResumedCB resumed_cb,
  13. PipelineStatusCB error_cb)
  14. : pipeline_(std::move(pipeline)),
  15. seeked_cb_(std::move(seeked_cb)),
  16. suspended_cb_(std::move(suspended_cb)),
  17. before_resume_cb_(std::move(before_resume_cb)),
  18. resumed_cb_(std::move(resumed_cb)),
  19. error_cb_(std::move(error_cb)) {
  20. DCHECK(pipeline_);
  21. DCHECK(seeked_cb_);
  22. DCHECK(suspended_cb_);
  23. DCHECK(before_resume_cb_);
  24. DCHECK(resumed_cb_);
  25. DCHECK(error_cb_);
  26. }
  27. PipelineController::~PipelineController() {
  28. DCHECK(thread_checker_.CalledOnValidThread());
  29. }
  30. void PipelineController::Start(Pipeline::StartType start_type,
  31. Demuxer* demuxer,
  32. Pipeline::Client* client,
  33. bool is_streaming,
  34. bool is_static) {
  35. DCHECK(thread_checker_.CalledOnValidThread());
  36. DCHECK_EQ(state_, State::STOPPED);
  37. DCHECK(demuxer);
  38. // Once the pipeline is started, we want to call the seeked callback but
  39. // without a time update.
  40. pending_startup_ = true;
  41. pending_seeked_cb_ = true;
  42. state_ = State::STARTING;
  43. demuxer_ = demuxer;
  44. is_streaming_ = is_streaming;
  45. is_static_ = is_static;
  46. pipeline_->Start(start_type, demuxer, client,
  47. base::BindOnce(&PipelineController::OnPipelineStatus,
  48. weak_factory_.GetWeakPtr(),
  49. start_type == Pipeline::StartType::kNormal
  50. ? State::PLAYING
  51. : State::PLAYING_OR_SUSPENDED));
  52. }
  53. void PipelineController::Seek(base::TimeDelta time, bool time_updated) {
  54. DCHECK(thread_checker_.CalledOnValidThread());
  55. // It would be slightly more clear to set this in Dispatch(), but we want to
  56. // be sure it gets updated even if the seek is elided.
  57. if (time_updated)
  58. pending_time_updated_ = true;
  59. pending_seeked_cb_ = true;
  60. pending_seek_except_start_ = true;
  61. // If we are already seeking to |time|, and the media is static, elide the
  62. // seek.
  63. if ((state_ == State::SEEKING || state_ == State::RESUMING) &&
  64. seek_time_ == time && is_static_) {
  65. pending_seek_ = false;
  66. return;
  67. }
  68. pending_seek_time_ = time;
  69. pending_seek_ = true;
  70. Dispatch();
  71. }
  72. // TODO(sandersd): It may be easier to use this interface if |suspended_cb_| is
  73. // executed when Suspend() is called while already suspended.
  74. void PipelineController::Suspend() {
  75. DCHECK(thread_checker_.CalledOnValidThread());
  76. pending_resume_ = false;
  77. if (state_ != State::SUSPENDING && state_ != State::SUSPENDED) {
  78. pending_suspend_ = true;
  79. Dispatch();
  80. }
  81. }
  82. void PipelineController::Resume() {
  83. DCHECK(thread_checker_.CalledOnValidThread());
  84. pending_suspend_ = false;
  85. // TODO(sandersd) fix resume during suspended start.
  86. if (state_ == State::SUSPENDING || state_ == State::SUSPENDED ||
  87. (state_ == State::SWITCHING_TRACKS &&
  88. previous_track_change_state_ == State::SUSPENDED)) {
  89. pending_resume_ = true;
  90. Dispatch();
  91. return;
  92. }
  93. }
  94. void PipelineController::OnDecoderStateLost() {
  95. DCHECK(thread_checker_.CalledOnValidThread());
  96. // Note: |time_updated| and |pending_seeked_cb_| are both false.
  97. pending_seek_except_start_ = true;
  98. // If we are already seeking or resuming, or if there's already a seek
  99. // pending,elide the seek. This is okay for decoder state lost since it just
  100. // needs one seek to recover (the decoder is reset and the next decode starts
  101. // from a key frame).
  102. //
  103. // Note on potential race condition: When the seek is elided, it's possible
  104. // that the decoder state loss happens before or after the previous seek
  105. // (decoder Reset()):
  106. // 1. Decoder state loss happens before Decoder::Reset() during the previous
  107. // seek. In this case we are fine since we just need a Reset().
  108. // 2. Decoder state loss happens after Decoder::Reset() during a previous
  109. // seek:
  110. // 2.1 If state loss happens before any Decode() we are still fine, since the
  111. // decoder is in a clean state.
  112. // 2.2 If state loss happens after a Decode(), then here we should not be in
  113. // the SEEKING state.
  114. if (state_ == State::SEEKING || state_ == State::RESUMING || pending_seek_)
  115. return;
  116. // Force a seek to the current time.
  117. pending_seek_time_ = pipeline_->GetMediaTime();
  118. pending_seek_ = true;
  119. Dispatch();
  120. }
  121. bool PipelineController::IsStable() {
  122. DCHECK(thread_checker_.CalledOnValidThread());
  123. return state_ == State::PLAYING;
  124. }
  125. bool PipelineController::IsPendingSeek() {
  126. DCHECK(thread_checker_.CalledOnValidThread());
  127. return pending_seek_except_start_;
  128. }
  129. bool PipelineController::IsSuspended() {
  130. DCHECK(thread_checker_.CalledOnValidThread());
  131. return (pending_suspend_ || state_ == State::SUSPENDING ||
  132. state_ == State::SUSPENDED) &&
  133. !pending_resume_;
  134. }
  135. bool PipelineController::IsPipelineSuspended() {
  136. DCHECK(thread_checker_.CalledOnValidThread());
  137. return state_ == State::SUSPENDED;
  138. }
  139. void PipelineController::OnPipelineStatus(State expected_state,
  140. PipelineStatus pipeline_status) {
  141. DCHECK(thread_checker_.CalledOnValidThread());
  142. if (pipeline_status != PIPELINE_OK) {
  143. error_cb_.Run(pipeline_status);
  144. return;
  145. }
  146. State old_state = state_;
  147. state_ = expected_state;
  148. // Resolve ambiguity of the current state if we may have suspended in startup.
  149. if (state_ == State::PLAYING_OR_SUSPENDED) {
  150. waiting_for_seek_ = false;
  151. state_ = pipeline_->IsSuspended() ? State::SUSPENDED : State::PLAYING;
  152. // It's possible for a Suspend() call to come in during startup. If we've
  153. // completed a suspended startup, we should clear that now.
  154. if (state_ == State::SUSPENDED)
  155. pending_suspend_ = false;
  156. }
  157. if (state_ == State::PLAYING) {
  158. // Start(), Seek(), or Resume() completed; we can be sure that
  159. // |demuxer_| got the seek it was waiting for.
  160. waiting_for_seek_ = false;
  161. // TODO(avayvod): Remove resumed callback after https://crbug.com/678374 is
  162. // properly fixed.
  163. if (old_state == State::RESUMING) {
  164. DCHECK(!pipeline_->IsSuspended());
  165. DCHECK(!pending_resume_);
  166. resumed_cb_.Run();
  167. }
  168. }
  169. if (state_ == State::SUSPENDED) {
  170. DCHECK(pipeline_->IsSuspended());
  171. DCHECK(!pending_suspend_);
  172. // Warning: possibly reentrant. The state may change inside this callback.
  173. // It must be safe to call Dispatch() twice in a row here.
  174. suspended_cb_.Run();
  175. }
  176. Dispatch();
  177. }
  178. // Note: Dispatch() may be called re-entrantly (by callbacks internally) or
  179. // twice in a row (by OnPipelineStatus()).
  180. void PipelineController::Dispatch() {
  181. DCHECK(thread_checker_.CalledOnValidThread());
  182. // Suspend/resume transitions take priority because seeks before a suspend
  183. // are wasted, and seeks after can be merged into the resume operation.
  184. if (pending_suspend_ && state_ == State::PLAYING) {
  185. pending_suspend_ = false;
  186. state_ = State::SUSPENDING;
  187. pipeline_->Suspend(base::BindOnce(&PipelineController::OnPipelineStatus,
  188. weak_factory_.GetWeakPtr(),
  189. State::SUSPENDED));
  190. return;
  191. }
  192. // In additional to the standard |pending_resume_| case, if we completed a
  193. // suspended startup, but a Seek() came in, we need to resume the pipeline to
  194. // complete the seek before calling |seeked_cb_|.
  195. if ((pending_resume_ || (pending_startup_ && pending_seek_)) &&
  196. state_ == State::SUSPENDED) {
  197. // If there is a pending seek, resume to that time instead...
  198. if (pending_seek_) {
  199. seek_time_ = pending_seek_time_;
  200. pending_seek_ = false;
  201. } else {
  202. seek_time_ = pipeline_->GetMediaTime();
  203. }
  204. // ...unless the media is streaming, in which case we resume at the start
  205. // because seeking doesn't work well.
  206. if (is_streaming_ && !seek_time_.is_zero()) {
  207. seek_time_ = base::TimeDelta();
  208. // In this case we want to make sure that the controls get updated
  209. // immediately, so we don't try to hide the seek.
  210. pending_time_updated_ = true;
  211. }
  212. // Tell |demuxer_| to expect our resume.
  213. DCHECK(!waiting_for_seek_);
  214. waiting_for_seek_ = true;
  215. demuxer_->StartWaitingForSeek(seek_time_);
  216. pending_resume_ = false;
  217. state_ = State::RESUMING;
  218. before_resume_cb_.Run();
  219. pipeline_->Resume(
  220. seek_time_, base::BindOnce(&PipelineController::OnPipelineStatus,
  221. weak_factory_.GetWeakPtr(), State::PLAYING));
  222. return;
  223. }
  224. // If we have pending operations, and a seek is ongoing, abort it.
  225. if ((pending_seek_ || pending_suspend_ || pending_audio_track_change_ ||
  226. pending_video_track_change_) &&
  227. waiting_for_seek_) {
  228. // If there is no pending seek, return the current seek to pending status.
  229. if (!pending_seek_) {
  230. pending_seek_time_ = seek_time_;
  231. pending_seek_ = true;
  232. }
  233. // CancelPendingSeek() may be reentrant, so update state first and return
  234. // immediately.
  235. waiting_for_seek_ = false;
  236. demuxer_->CancelPendingSeek(pending_seek_time_);
  237. return;
  238. }
  239. // We can only switch tracks if we are not in a transitioning state already.
  240. if ((pending_audio_track_change_ || pending_video_track_change_) &&
  241. (state_ == State::PLAYING || state_ == State::SUSPENDED)) {
  242. previous_track_change_state_ = state_;
  243. state_ = State::SWITCHING_TRACKS;
  244. // Attempt to do a track change _before_ attempting a seek operation,
  245. // otherwise the seek will apply to the old tracks instead of the new
  246. // one(s). Also attempt audio before video.
  247. if (pending_audio_track_change_) {
  248. pending_audio_track_change_ = false;
  249. pipeline_->OnEnabledAudioTracksChanged(
  250. pending_audio_track_change_ids_,
  251. base::BindOnce(&PipelineController::OnTrackChangeComplete,
  252. weak_factory_.GetWeakPtr()));
  253. return;
  254. }
  255. if (pending_video_track_change_) {
  256. pending_video_track_change_ = false;
  257. pipeline_->OnSelectedVideoTrackChanged(
  258. pending_video_track_change_id_,
  259. base::BindOnce(&PipelineController::OnTrackChangeComplete,
  260. weak_factory_.GetWeakPtr()));
  261. return;
  262. }
  263. }
  264. // Ordinary seeking.
  265. if (pending_seek_ && state_ == State::PLAYING) {
  266. seek_time_ = pending_seek_time_;
  267. // Tell |demuxer_| to expect our seek.
  268. DCHECK(!waiting_for_seek_);
  269. waiting_for_seek_ = true;
  270. demuxer_->StartWaitingForSeek(seek_time_);
  271. pending_seek_ = false;
  272. state_ = State::SEEKING;
  273. pipeline_->Seek(seek_time_,
  274. base::BindOnce(&PipelineController::OnPipelineStatus,
  275. weak_factory_.GetWeakPtr(), State::PLAYING));
  276. return;
  277. }
  278. // If |state_| is PLAYING and we didn't trigger an operation above then we
  279. // are in a stable state. If there is a seeked callback pending, emit it.
  280. //
  281. // We also need to emit it if we completed suspended startup.
  282. if (pending_seeked_cb_ &&
  283. (state_ == State::PLAYING ||
  284. (state_ == State::SUSPENDED && pending_startup_))) {
  285. // |seeked_cb_| may be reentrant, so update state first and return
  286. // immediately.
  287. pending_startup_ = false;
  288. pending_seeked_cb_ = false;
  289. pending_seek_except_start_ = false;
  290. bool was_pending_time_updated = pending_time_updated_;
  291. pending_time_updated_ = false;
  292. seeked_cb_.Run(was_pending_time_updated);
  293. return;
  294. }
  295. }
  296. void PipelineController::Stop() {
  297. if (state_ == State::STOPPED)
  298. return;
  299. demuxer_ = nullptr;
  300. waiting_for_seek_ = false;
  301. pending_seeked_cb_ = false;
  302. pending_seek_except_start_ = false;
  303. pending_time_updated_ = false;
  304. pending_seek_ = false;
  305. pending_suspend_ = false;
  306. pending_resume_ = false;
  307. pending_audio_track_change_ = false;
  308. pending_video_track_change_ = false;
  309. state_ = State::STOPPED;
  310. pipeline_->Stop();
  311. }
  312. bool PipelineController::IsPipelineRunning() const {
  313. return pipeline_->IsRunning();
  314. }
  315. double PipelineController::GetPlaybackRate() const {
  316. return pipeline_->GetPlaybackRate();
  317. }
  318. void PipelineController::SetPlaybackRate(double playback_rate) {
  319. pipeline_->SetPlaybackRate(playback_rate);
  320. }
  321. float PipelineController::GetVolume() const {
  322. return pipeline_->GetVolume();
  323. }
  324. void PipelineController::SetVolume(float volume) {
  325. pipeline_->SetVolume(volume);
  326. }
  327. void PipelineController::SetLatencyHint(
  328. absl::optional<base::TimeDelta> latency_hint) {
  329. DCHECK(!latency_hint || (*latency_hint >= base::TimeDelta()));
  330. pipeline_->SetLatencyHint(latency_hint);
  331. }
  332. void PipelineController::SetPreservesPitch(bool preserves_pitch) {
  333. pipeline_->SetPreservesPitch(preserves_pitch);
  334. }
  335. void PipelineController::SetWasPlayedWithUserActivation(
  336. bool was_played_with_user_activation) {
  337. pipeline_->SetWasPlayedWithUserActivation(was_played_with_user_activation);
  338. }
  339. base::TimeDelta PipelineController::GetMediaTime() const {
  340. return pipeline_->GetMediaTime();
  341. }
  342. Ranges<base::TimeDelta> PipelineController::GetBufferedTimeRanges() const {
  343. return pipeline_->GetBufferedTimeRanges();
  344. }
  345. base::TimeDelta PipelineController::GetMediaDuration() const {
  346. return pipeline_->GetMediaDuration();
  347. }
  348. bool PipelineController::DidLoadingProgress() {
  349. return pipeline_->DidLoadingProgress();
  350. }
  351. PipelineStatistics PipelineController::GetStatistics() const {
  352. return pipeline_->GetStatistics();
  353. }
  354. void PipelineController::SetCdm(CdmContext* cdm_context,
  355. CdmAttachedCB cdm_attached_cb) {
  356. pipeline_->SetCdm(cdm_context, std::move(cdm_attached_cb));
  357. }
  358. void PipelineController::OnEnabledAudioTracksChanged(
  359. const std::vector<MediaTrack::Id>& enabled_track_ids) {
  360. DCHECK(thread_checker_.CalledOnValidThread());
  361. pending_audio_track_change_ = true;
  362. pending_audio_track_change_ids_ = enabled_track_ids;
  363. Dispatch();
  364. }
  365. void PipelineController::OnSelectedVideoTrackChanged(
  366. absl::optional<MediaTrack::Id> selected_track_id) {
  367. DCHECK(thread_checker_.CalledOnValidThread());
  368. pending_video_track_change_ = true;
  369. pending_video_track_change_id_ = selected_track_id;
  370. Dispatch();
  371. }
  372. void PipelineController::OnExternalVideoFrameRequest() {
  373. DCHECK(thread_checker_.CalledOnValidThread());
  374. pipeline_->OnExternalVideoFrameRequest();
  375. }
  376. void PipelineController::FireOnTrackChangeCompleteForTesting(State set_to) {
  377. previous_track_change_state_ = set_to;
  378. OnTrackChangeComplete();
  379. }
  380. void PipelineController::OnTrackChangeComplete() {
  381. DCHECK(thread_checker_.CalledOnValidThread());
  382. if (state_ == State::SWITCHING_TRACKS)
  383. state_ = previous_track_change_state_;
  384. // Other track changed or seek/suspend/resume, etc may be waiting.
  385. Dispatch();
  386. }
  387. } // namespace media