pipeline_impl.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright (c) 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. #ifndef MEDIA_BASE_PIPELINE_IMPL_H_
  5. #define MEDIA_BASE_PIPELINE_IMPL_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/threading/thread_checker.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. #include "media/base/renderer_factory_selector.h"
  16. #include "third_party/abseil-cpp/absl/types/optional.h"
  17. namespace base {
  18. class SingleThreadTaskRunner;
  19. }
  20. namespace media {
  21. class MediaLog;
  22. // Callbacks used for Renderer creation. When the RendererType is nullopt, the
  23. // current base one will be created.
  24. using CreateRendererCB = base::RepeatingCallback<std::unique_ptr<Renderer>(
  25. absl::optional<RendererType>)>;
  26. using RendererCreatedCB = base::OnceCallback<void(std::unique_ptr<Renderer>)>;
  27. using AsyncCreateRendererCB =
  28. base::RepeatingCallback<void(absl::optional<RendererType>,
  29. RendererCreatedCB)>;
  30. // Pipeline runs the media pipeline. Filters are created and called on the
  31. // task runner injected into this object. Pipeline works like a state
  32. // machine to perform asynchronous initialization, pausing, seeking and playing.
  33. //
  34. // Here's a state diagram that describes the lifetime of this object.
  35. //
  36. // [ *Created ] [ Any State ]
  37. // | Start() | Stop()
  38. // V V
  39. // [ Starting ] [ Stopping ]
  40. // | |
  41. // V V
  42. // [ Playing ] <---------. [ Stopped ]
  43. // | | | Seek() |
  44. // | | V |
  45. // | | [ Seeking ] ---'
  46. // | | ^
  47. // | | *TrackChange() |
  48. // | V |
  49. // | [ Switching ] ----'
  50. // | ^
  51. // | Suspend() |
  52. // V |
  53. // [ Suspending ] |
  54. // | |
  55. // V |
  56. // [ Suspended ] |
  57. // | Resume() |
  58. // V |
  59. // [ Resuming ] ---------'
  60. //
  61. // Initialization is a series of state transitions from "Created" through each
  62. // filter initialization state. When all filter initialization states have
  63. // completed, we simulate a Seek() to the beginning of the media to give filters
  64. // a chance to preroll. From then on the normal Seek() transitions are carried
  65. // out and we start playing the media.
  66. //
  67. // If Stop() is ever called, this object will transition to "Stopped" state.
  68. // Pipeline::Stop() is never called from withing PipelineImpl. It's |client_|'s
  69. // responsibility to call stop when appropriate.
  70. //
  71. // TODO(sandersd): It should be possible to pass through Suspended when going
  72. // from InitDemuxer to InitRenderer, thereby eliminating the Resuming state.
  73. // Some annoying differences between the two paths need to be removed first.
  74. class MEDIA_EXPORT PipelineImpl : public Pipeline {
  75. public:
  76. // Constructs a pipeline that will execute media tasks on |media_task_runner|.
  77. // |create_renderer_cb|: to create renderers when starting and resuming.
  78. PipelineImpl(scoped_refptr<base::SingleThreadTaskRunner> media_task_runner,
  79. scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
  80. CreateRendererCB create_renderer_cb,
  81. MediaLog* media_log);
  82. PipelineImpl(const PipelineImpl&) = delete;
  83. PipelineImpl& operator=(const PipelineImpl&) = delete;
  84. ~PipelineImpl() override;
  85. // Pipeline implementation.
  86. void Start(StartType start_type,
  87. Demuxer* demuxer,
  88. Client* client,
  89. PipelineStatusCallback seek_cb) override;
  90. void Stop() override;
  91. void Seek(base::TimeDelta time, PipelineStatusCallback seek_cb) override;
  92. void Suspend(PipelineStatusCallback suspend_cb) override;
  93. void Resume(base::TimeDelta time, PipelineStatusCallback seek_cb) override;
  94. bool IsRunning() const override;
  95. bool IsSuspended() const override;
  96. double GetPlaybackRate() const override;
  97. void SetPlaybackRate(double playback_rate) override;
  98. float GetVolume() const override;
  99. void SetVolume(float volume) override;
  100. void SetLatencyHint(absl::optional<base::TimeDelta> latency_hint) override;
  101. void SetPreservesPitch(bool preserves_pitch) override;
  102. void SetWasPlayedWithUserActivation(
  103. bool was_played_with_user_activation) override;
  104. base::TimeDelta GetMediaTime() const override;
  105. Ranges<base::TimeDelta> GetBufferedTimeRanges() const override;
  106. base::TimeDelta GetMediaDuration() const override;
  107. bool DidLoadingProgress() override;
  108. PipelineStatistics GetStatistics() const override;
  109. void SetCdm(CdmContext* cdm_context, CdmAttachedCB cdm_attached_cb) override;
  110. // |enabled_track_ids| contains track ids of enabled audio tracks.
  111. void OnEnabledAudioTracksChanged(
  112. const std::vector<MediaTrack::Id>& enabled_track_ids,
  113. base::OnceClosure change_completed_cb) override;
  114. // |selected_track_id| is either empty, which means no video track is
  115. // selected, or contains the selected video track id.
  116. void OnSelectedVideoTrackChanged(
  117. absl::optional<MediaTrack::Id> selected_track_id,
  118. base::OnceClosure change_completed_cb) override;
  119. void OnExternalVideoFrameRequest() override;
  120. private:
  121. friend class MediaLog;
  122. class RendererWrapper;
  123. // Pipeline states, as described above.
  124. // TODO(alokp): Move this to RendererWrapper after removing the references
  125. // from MediaLog.
  126. enum State {
  127. kCreated,
  128. kStarting,
  129. kSeeking,
  130. kPlaying,
  131. kStopping,
  132. kStopped,
  133. kSuspending,
  134. kSuspended,
  135. kResuming,
  136. };
  137. static const char* GetStateString(State state);
  138. // Create a Renderer asynchronously. Must be called on the main task runner
  139. // and the callback will be called on the main task runner as well.
  140. void AsyncCreateRenderer(absl::optional<RendererType> renderer_type,
  141. RendererCreatedCB renderer_created_cb);
  142. // Notifications from RendererWrapper.
  143. void OnError(PipelineStatus error);
  144. void OnFallback(PipelineStatus fallback);
  145. void OnEnded();
  146. void OnMetadata(const PipelineMetadata& metadata);
  147. void OnBufferingStateChange(BufferingState state,
  148. BufferingStateChangeReason reason);
  149. void OnDurationChange(base::TimeDelta duration);
  150. void OnWaiting(WaitingReason reason);
  151. void OnAudioConfigChange(const AudioDecoderConfig& config);
  152. void OnVideoConfigChange(const VideoDecoderConfig& config);
  153. void OnVideoNaturalSizeChange(const gfx::Size& size);
  154. void OnVideoOpacityChange(bool opaque);
  155. void OnVideoAverageKeyframeDistanceUpdate();
  156. void OnAudioPipelineInfoChange(const AudioPipelineInfo& info);
  157. void OnVideoPipelineInfoChange(const VideoPipelineInfo& info);
  158. void OnRemotePlayStateChange(MediaStatus::State state);
  159. void OnVideoFrameRateChange(absl::optional<int> fps);
  160. // Task completion callbacks from RendererWrapper.
  161. void OnSeekDone(bool is_suspended);
  162. void OnSuspendDone();
  163. // Parameters passed in the constructor.
  164. const scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
  165. CreateRendererCB create_renderer_cb_;
  166. const raw_ptr<MediaLog> media_log_;
  167. // Pipeline client. Valid only while the pipeline is running.
  168. raw_ptr<Client> client_;
  169. // RendererWrapper instance that runs on the media thread.
  170. std::unique_ptr<RendererWrapper> renderer_wrapper_;
  171. // Temporary callback used for Start(), Seek(), and Resume().
  172. PipelineStatusCallback seek_cb_;
  173. // Temporary callback used for Suspend().
  174. PipelineStatusCallback suspend_cb_;
  175. // Current playback rate (>= 0.0). This value is set immediately via
  176. // SetPlaybackRate() and a task is dispatched on the task runner to notify
  177. // the filters.
  178. double playback_rate_;
  179. // Current volume level (from 0.0f to 1.0f). This value is set immediately
  180. // via SetVolume() and a task is dispatched on the task runner to notify the
  181. // filters.
  182. float volume_;
  183. // Current duration as reported by Demuxer.
  184. base::TimeDelta duration_;
  185. // Set by GetMediaTime(), used to prevent the current media time value as
  186. // reported to JavaScript from going backwards in time.
  187. mutable base::TimeDelta last_media_time_;
  188. // Set by Seek(), used in place of asking the renderer for current media time
  189. // while a seek is pending. Renderer's time cannot be trusted until the seek
  190. // has completed.
  191. base::TimeDelta seek_time_;
  192. // Cached suspension state for the RendererWrapper.
  193. bool is_suspended_;
  194. // 'external_video_frame_request_signaled_' tracks whether we've called
  195. // OnExternalVideoFrameRequest on the current renderer. Calls which may
  196. // create a new renderer in the RendererWrapper (Start, Resume, SetCdm) will
  197. // reset this member. There is no guarantee to the client that
  198. // OnExternalVideoFrameRequest will be called only once.
  199. bool external_video_frame_request_signaled_ = false;
  200. base::ThreadChecker thread_checker_;
  201. base::WeakPtrFactory<PipelineImpl> weak_factory_{this};
  202. };
  203. } // namespace media
  204. #endif // MEDIA_BASE_PIPELINE_IMPL_H_