tools/gn: fix rebase_path example
[chromium-blink-merge.git] / media / renderers / renderer_impl.h
blobbefa95c18f6a48050b8d403ff9cf3d7e47687948
1 // Copyright 2014 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.
5 #ifndef MEDIA_RENDERERS_RENDERER_IMPL_H_
6 #define MEDIA_RENDERERS_RENDERER_IMPL_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/synchronization/lock.h"
12 #include "base/time/clock.h"
13 #include "base/time/default_tick_clock.h"
14 #include "base/time/time.h"
15 #include "media/base/buffering_state.h"
16 #include "media/base/decryptor.h"
17 #include "media/base/media_export.h"
18 #include "media/base/pipeline_status.h"
19 #include "media/base/renderer.h"
21 namespace base {
22 class SingleThreadTaskRunner;
25 namespace media {
27 class AudioRenderer;
28 class DemuxerStreamProvider;
29 class TimeSource;
30 class VideoRenderer;
31 class WallClockTimeSource;
33 class MEDIA_EXPORT RendererImpl : public Renderer {
34 public:
35 // Renders audio/video streams using |audio_renderer| and |video_renderer|
36 // provided. All methods except for GetMediaTime() run on the |task_runner|.
37 // GetMediaTime() runs on the render main thread because it's part of JS sync
38 // API.
39 RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
40 scoped_ptr<AudioRenderer> audio_renderer,
41 scoped_ptr<VideoRenderer> video_renderer);
43 ~RendererImpl() final;
45 // Renderer implementation.
46 void Initialize(DemuxerStreamProvider* demuxer_stream_provider,
47 const PipelineStatusCB& init_cb,
48 const StatisticsCB& statistics_cb,
49 const BufferingStateCB& buffering_state_cb,
50 const PaintCB& paint_cb,
51 const base::Closure& ended_cb,
52 const PipelineStatusCB& error_cb,
53 const base::Closure& waiting_for_decryption_key_cb) final;
54 void SetCdm(CdmContext* cdm_context,
55 const CdmAttachedCB& cdm_attached_cb) final;
56 void Flush(const base::Closure& flush_cb) final;
57 void StartPlayingFrom(base::TimeDelta time) final;
58 void SetPlaybackRate(float playback_rate) final;
59 void SetVolume(float volume) final;
60 base::TimeDelta GetMediaTime() final;
61 bool HasAudio() final;
62 bool HasVideo() final;
64 // Helper functions for testing purposes. Must be called before Initialize().
65 void DisableUnderflowForTesting();
66 void EnableClocklessVideoPlaybackForTesting();
68 private:
69 enum State {
70 STATE_UNINITIALIZED,
71 STATE_INITIALIZING,
72 STATE_FLUSHING,
73 STATE_PLAYING,
74 STATE_ERROR
77 base::TimeDelta GetMediaTimeForSyncingVideo();
79 // Requests that this object notifies when a decryptor is ready through the
80 // |decryptor_ready_cb| provided.
81 // If |decryptor_ready_cb| is null, the existing callback will be fired with
82 // nullptr immediately and reset.
83 void SetDecryptorReadyCallback(const DecryptorReadyCB& decryptor_ready_cb);
85 // Helper functions and callbacks for Initialize().
86 void InitializeAudioRenderer();
87 void OnAudioRendererInitializeDone(PipelineStatus status);
88 void InitializeVideoRenderer();
89 void OnVideoRendererInitializeDone(PipelineStatus status);
91 // Helper functions and callbacks for Flush().
92 void FlushAudioRenderer();
93 void OnAudioRendererFlushDone();
94 void FlushVideoRenderer();
95 void OnVideoRendererFlushDone();
97 // Callback executed by filters to update statistics.
98 void OnUpdateStatistics(const PipelineStatistics& stats);
100 // Collection of callback methods and helpers for tracking changes in
101 // buffering state and transition from paused/underflow states and playing
102 // states.
104 // While in the kPlaying state:
105 // - A waiting to non-waiting transition indicates preroll has completed
106 // and StartPlayback() should be called
107 // - A non-waiting to waiting transition indicates underflow has occurred
108 // and PausePlayback() should be called
109 void OnBufferingStateChanged(BufferingState* buffering_state,
110 BufferingState new_buffering_state);
111 bool WaitingForEnoughData() const;
112 void PausePlayback();
113 void StartPlayback();
115 // Callbacks executed when a renderer has ended.
116 void OnAudioRendererEnded();
117 void OnVideoRendererEnded();
118 bool PlaybackHasEnded() const;
119 void RunEndedCallbackIfNeeded();
121 // Callback executed when a runtime error happens.
122 void OnError(PipelineStatus error);
124 State state_;
126 // Task runner used to execute pipeline tasks.
127 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
129 DemuxerStreamProvider* demuxer_stream_provider_;
131 // Permanent callbacks to notify various renderer states/stats.
132 StatisticsCB statistics_cb_;
133 base::Closure ended_cb_;
134 PipelineStatusCB error_cb_;
135 BufferingStateCB buffering_state_cb_;
136 PaintCB paint_cb_;
137 base::Closure waiting_for_decryption_key_cb_;
139 // Temporary callback used for Initialize() and Flush().
140 PipelineStatusCB init_cb_;
141 base::Closure flush_cb_;
143 scoped_ptr<AudioRenderer> audio_renderer_;
144 scoped_ptr<VideoRenderer> video_renderer_;
146 // Renderer-provided time source used to control playback.
147 TimeSource* time_source_;
148 scoped_ptr<WallClockTimeSource> wall_clock_time_source_;
149 bool time_ticking_;
151 // The time to start playback from after starting/seeking has completed.
152 base::TimeDelta start_time_;
154 BufferingState audio_buffering_state_;
155 BufferingState video_buffering_state_;
157 // Whether we've received the audio/video ended events.
158 bool audio_ended_;
159 bool video_ended_;
161 CdmContext* cdm_context_;
163 // Callback registered by filters (decoder or demuxer) to be informed of a
164 // Decryptor.
165 // Note: We could have multiple filters registering this callback. One
166 // callback is okay because:
167 // 1, We always initialize filters in sequence.
168 // 2, Filter initialization will not finish until this callback is satisfied.
169 DecryptorReadyCB decryptor_ready_cb_;
171 bool underflow_disabled_for_testing_;
172 bool clockless_video_playback_enabled_for_testing_;
174 base::WeakPtr<RendererImpl> weak_this_;
175 base::WeakPtrFactory<RendererImpl> weak_factory_;
177 DISALLOW_COPY_AND_ASSIGN(RendererImpl);
180 } // namespace media
182 #endif // MEDIA_RENDERERS_RENDERER_IMPL_H_