Move sandboxed_unpacker.{h,cc} from chrome/ to extensions/
[chromium-blink-merge.git] / media / filters / renderer_impl.h
blobf84b3bfe3bc1db9bad793baff9a52a2031131a91
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 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) 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 State state_;
125 // Task runner used to execute pipeline tasks.
126 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
128 DemuxerStreamProvider* demuxer_stream_provider_;
130 // Permanent callbacks to notify various renderer states/stats.
131 StatisticsCB statistics_cb_;
132 base::Closure ended_cb_;
133 PipelineStatusCB error_cb_;
134 BufferingStateCB buffering_state_cb_;
135 PaintCB paint_cb_;
137 // Temporary callback used for Initialize() and Flush().
138 PipelineStatusCB init_cb_;
139 base::Closure flush_cb_;
141 scoped_ptr<AudioRenderer> audio_renderer_;
142 scoped_ptr<VideoRenderer> video_renderer_;
144 // Renderer-provided time source used to control playback.
145 TimeSource* time_source_;
146 scoped_ptr<WallClockTimeSource> wall_clock_time_source_;
147 bool time_ticking_;
149 // The time to start playback from after starting/seeking has completed.
150 base::TimeDelta start_time_;
152 BufferingState audio_buffering_state_;
153 BufferingState video_buffering_state_;
155 // Whether we've received the audio/video ended events.
156 bool audio_ended_;
157 bool video_ended_;
159 CdmContext* cdm_context_;
161 // Callback registered by filters (decoder or demuxer) to be informed of a
162 // Decryptor.
163 // Note: We could have multiple filters registering this callback. One
164 // callback is okay because:
165 // 1, We always initialize filters in sequence.
166 // 2, Filter initialization will not finish until this callback is satisfied.
167 DecryptorReadyCB decryptor_ready_cb_;
169 bool underflow_disabled_for_testing_;
170 bool clockless_video_playback_enabled_for_testing_;
172 base::WeakPtr<RendererImpl> weak_this_;
173 base::WeakPtrFactory<RendererImpl> weak_factory_;
175 DISALLOW_COPY_AND_ASSIGN(RendererImpl);
178 } // namespace media
180 #endif // MEDIA_FILTERS_RENDERER_IMPL_H_