Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / media / filters / renderer_impl.h
blob6a65bc008da37299cbacad32d431a066ef488d1c
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_FILTERS_RENDERER_IMPL_H_
6 #define MEDIA_FILTERS_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 base::Closure& 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) final;
53 void SetCdm(CdmContext* cdm_context,
54 const CdmAttachedCB& cdm_attached_cb) final;
55 void Flush(const base::Closure& flush_cb) final;
56 void StartPlayingFrom(base::TimeDelta time) final;
57 void SetPlaybackRate(float playback_rate) final;
58 void SetVolume(float volume) final;
59 base::TimeDelta GetMediaTime() final;
60 bool HasAudio() final;
61 bool HasVideo() final;
63 // Helper functions for testing purposes. Must be called before Initialize().
64 void DisableUnderflowForTesting();
65 void EnableClocklessVideoPlaybackForTesting();
67 private:
68 enum State {
69 STATE_UNINITIALIZED,
70 STATE_INITIALIZING,
71 STATE_FLUSHING,
72 STATE_PLAYING,
73 STATE_ERROR
76 base::TimeDelta GetMediaTimeForSyncingVideo();
78 // Requests that this object notifies when a decryptor is ready through the
79 // |decryptor_ready_cb| provided.
80 // If |decryptor_ready_cb| is null, the existing callback will be fired with
81 // nullptr immediately and reset.
82 void SetDecryptorReadyCallback(const DecryptorReadyCB& decryptor_ready_cb);
84 // Helper functions and callbacks for Initialize().
85 void InitializeAudioRenderer();
86 void OnAudioRendererInitializeDone(PipelineStatus status);
87 void InitializeVideoRenderer();
88 void OnVideoRendererInitializeDone(PipelineStatus status);
90 // Helper functions and callbacks for Flush().
91 void FlushAudioRenderer();
92 void OnAudioRendererFlushDone();
93 void FlushVideoRenderer();
94 void OnVideoRendererFlushDone();
96 // Callback executed by filters to update statistics.
97 void OnUpdateStatistics(const PipelineStatistics& stats);
99 // Collection of callback methods and helpers for tracking changes in
100 // buffering state and transition from paused/underflow states and playing
101 // states.
103 // While in the kPlaying state:
104 // - A waiting to non-waiting transition indicates preroll has completed
105 // and StartPlayback() should be called
106 // - A non-waiting to waiting transition indicates underflow has occurred
107 // and PausePlayback() should be called
108 void OnBufferingStateChanged(BufferingState* buffering_state,
109 BufferingState new_buffering_state);
110 bool WaitingForEnoughData() const;
111 void PausePlayback();
112 void StartPlayback();
114 // Callbacks executed when a renderer has ended.
115 void OnAudioRendererEnded();
116 void OnVideoRendererEnded();
117 bool PlaybackHasEnded() const;
118 void RunEndedCallbackIfNeeded();
120 // Callback executed when a runtime error happens.
121 void OnError(PipelineStatus error);
123 void FireAllPendingCallbacks();
125 State state_;
127 // Task runner used to execute pipeline tasks.
128 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
130 DemuxerStreamProvider* demuxer_stream_provider_;
132 // Permanent callbacks to notify various renderer states/stats.
133 StatisticsCB statistics_cb_;
134 base::Closure ended_cb_;
135 PipelineStatusCB error_cb_;
136 BufferingStateCB buffering_state_cb_;
137 PaintCB paint_cb_;
139 // Temporary callback used for Initialize() and Flush().
140 base::Closure 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_FILTERS_RENDERER_IMPL_H_