[go: nahoru, domu]

blob: e0374fb5a334393aeb647de998fc8376087d1361 [file] [log] [blame]
xhwangbe9da702014-08-23 21:44:551// 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.
4
servolkf54f5c8f2015-02-24 20:32:395#ifndef MEDIA_RENDERERS_RENDERER_IMPL_H_
6#define MEDIA_RENDERERS_RENDERER_IMPL_H_
xhwangbe9da702014-08-23 21:44:557
dalecurtisc11e7bb2015-04-03 05:07:088#include "base/cancelable_callback.h"
xhwangbe9da702014-08-23 21:44:559#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "base/synchronization/lock.h"
13#include "base/time/clock.h"
14#include "base/time/default_tick_clock.h"
15#include "base/time/time.h"
16#include "media/base/buffering_state.h"
xhwang97de4202014-11-25 08:44:0117#include "media/base/decryptor.h"
xhwangbe9da702014-08-23 21:44:5518#include "media/base/media_export.h"
19#include "media/base/pipeline_status.h"
20#include "media/base/renderer.h"
21
22namespace base {
23class SingleThreadTaskRunner;
24}
25
26namespace media {
27
28class AudioRenderer;
xhwang1c5668e2014-09-10 02:42:0429class DemuxerStreamProvider;
xhwangbe9da702014-08-23 21:44:5530class TimeSource;
31class VideoRenderer;
scherkusece89452014-09-09 23:13:3832class WallClockTimeSource;
xhwangbe9da702014-08-23 21:44:5533
34class MEDIA_EXPORT RendererImpl : public Renderer {
35 public:
xhwangabd95fd2014-10-03 07:10:0136 // Renders audio/video streams using |audio_renderer| and |video_renderer|
37 // provided. All methods except for GetMediaTime() run on the |task_runner|.
38 // GetMediaTime() runs on the render main thread because it's part of JS sync
39 // API.
xhwangbe9da702014-08-23 21:44:5540 RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
xhwangbe9da702014-08-23 21:44:5541 scoped_ptr<AudioRenderer> audio_renderer,
42 scoped_ptr<VideoRenderer> video_renderer);
43
xhwang97de4202014-11-25 08:44:0144 ~RendererImpl() final;
xhwangbe9da702014-08-23 21:44:5545
46 // Renderer implementation.
dchengc2456542014-10-21 12:23:2747 void Initialize(DemuxerStreamProvider* demuxer_stream_provider,
dalecurtis47046112015-01-23 22:58:2048 const PipelineStatusCB& init_cb,
dchengc2456542014-10-21 12:23:2749 const StatisticsCB& statistics_cb,
xhwang94c0bd32014-11-13 20:49:3150 const BufferingStateCB& buffering_state_cb,
51 const PaintCB& paint_cb,
dchengc2456542014-10-21 12:23:2752 const base::Closure& ended_cb,
jrummell74fc4f942015-03-02 22:48:2753 const PipelineStatusCB& error_cb,
54 const base::Closure& waiting_for_decryption_key_cb) final;
xhwang97de4202014-11-25 08:44:0155 void SetCdm(CdmContext* cdm_context,
56 const CdmAttachedCB& cdm_attached_cb) final;
57 void Flush(const base::Closure& flush_cb) final;
58 void StartPlayingFrom(base::TimeDelta time) final;
59 void SetPlaybackRate(float playback_rate) final;
60 void SetVolume(float volume) final;
61 base::TimeDelta GetMediaTime() final;
62 bool HasAudio() final;
63 bool HasVideo() final;
xhwangbe9da702014-08-23 21:44:5564
65 // Helper functions for testing purposes. Must be called before Initialize().
66 void DisableUnderflowForTesting();
scherkusece89452014-09-09 23:13:3867 void EnableClocklessVideoPlaybackForTesting();
dalecurtisc11e7bb2015-04-03 05:07:0868 void set_time_source_for_testing(TimeSource* time_source) {
69 time_source_ = time_source;
70 }
71 void set_video_underflow_threshold_for_testing(base::TimeDelta threshold) {
72 video_underflow_threshold_ = threshold;
73 }
xhwangbe9da702014-08-23 21:44:5574
75 private:
76 enum State {
77 STATE_UNINITIALIZED,
78 STATE_INITIALIZING,
79 STATE_FLUSHING,
80 STATE_PLAYING,
81 STATE_ERROR
82 };
83
dalecurtis57550422015-03-26 20:31:2284 base::TimeTicks GetWallClockTime(base::TimeDelta time);
xhwang97de4202014-11-25 08:44:0185
86 // Requests that this object notifies when a decryptor is ready through the
87 // |decryptor_ready_cb| provided.
88 // If |decryptor_ready_cb| is null, the existing callback will be fired with
89 // nullptr immediately and reset.
90 void SetDecryptorReadyCallback(const DecryptorReadyCB& decryptor_ready_cb);
xhwangbe9da702014-08-23 21:44:5591
92 // Helper functions and callbacks for Initialize().
93 void InitializeAudioRenderer();
94 void OnAudioRendererInitializeDone(PipelineStatus status);
95 void InitializeVideoRenderer();
96 void OnVideoRendererInitializeDone(PipelineStatus status);
97
98 // Helper functions and callbacks for Flush().
99 void FlushAudioRenderer();
100 void OnAudioRendererFlushDone();
101 void FlushVideoRenderer();
102 void OnVideoRendererFlushDone();
xhwangbe9da702014-08-23 21:44:55103
104 // Callback executed by filters to update statistics.
105 void OnUpdateStatistics(const PipelineStatistics& stats);
106
107 // Collection of callback methods and helpers for tracking changes in
108 // buffering state and transition from paused/underflow states and playing
109 // states.
110 //
111 // While in the kPlaying state:
112 // - A waiting to non-waiting transition indicates preroll has completed
113 // and StartPlayback() should be called
114 // - A non-waiting to waiting transition indicates underflow has occurred
115 // and PausePlayback() should be called
116 void OnBufferingStateChanged(BufferingState* buffering_state,
117 BufferingState new_buffering_state);
118 bool WaitingForEnoughData() const;
119 void PausePlayback();
120 void StartPlayback();
121
xhwangbe9da702014-08-23 21:44:55122 // Callbacks executed when a renderer has ended.
123 void OnAudioRendererEnded();
124 void OnVideoRendererEnded();
scherkusece89452014-09-09 23:13:38125 bool PlaybackHasEnded() const;
xhwangbe9da702014-08-23 21:44:55126 void RunEndedCallbackIfNeeded();
127
128 // Callback executed when a runtime error happens.
129 void OnError(PipelineStatus error);
130
xhwangbe9da702014-08-23 21:44:55131 State state_;
132
133 // Task runner used to execute pipeline tasks.
134 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
135
xhwang1c5668e2014-09-10 02:42:04136 DemuxerStreamProvider* demuxer_stream_provider_;
xhwangbe9da702014-08-23 21:44:55137
138 // Permanent callbacks to notify various renderer states/stats.
139 StatisticsCB statistics_cb_;
140 base::Closure ended_cb_;
141 PipelineStatusCB error_cb_;
142 BufferingStateCB buffering_state_cb_;
xhwang94c0bd32014-11-13 20:49:31143 PaintCB paint_cb_;
jrummell74fc4f942015-03-02 22:48:27144 base::Closure waiting_for_decryption_key_cb_;
xhwangbe9da702014-08-23 21:44:55145
146 // Temporary callback used for Initialize() and Flush().
dalecurtis47046112015-01-23 22:58:20147 PipelineStatusCB init_cb_;
xhwangbe9da702014-08-23 21:44:55148 base::Closure flush_cb_;
149
150 scoped_ptr<AudioRenderer> audio_renderer_;
151 scoped_ptr<VideoRenderer> video_renderer_;
152
153 // Renderer-provided time source used to control playback.
154 TimeSource* time_source_;
scherkusece89452014-09-09 23:13:38155 scoped_ptr<WallClockTimeSource> wall_clock_time_source_;
156 bool time_ticking_;
xhwangbe9da702014-08-23 21:44:55157
158 // The time to start playback from after starting/seeking has completed.
159 base::TimeDelta start_time_;
160
161 BufferingState audio_buffering_state_;
162 BufferingState video_buffering_state_;
163
164 // Whether we've received the audio/video ended events.
165 bool audio_ended_;
166 bool video_ended_;
167
xhwang97de4202014-11-25 08:44:01168 CdmContext* cdm_context_;
169
170 // Callback registered by filters (decoder or demuxer) to be informed of a
171 // Decryptor.
172 // Note: We could have multiple filters registering this callback. One
173 // callback is okay because:
174 // 1, We always initialize filters in sequence.
175 // 2, Filter initialization will not finish until this callback is satisfied.
176 DecryptorReadyCB decryptor_ready_cb_;
177
xhwangbe9da702014-08-23 21:44:55178 bool underflow_disabled_for_testing_;
scherkusece89452014-09-09 23:13:38179 bool clockless_video_playback_enabled_for_testing_;
xhwangbe9da702014-08-23 21:44:55180
dalecurtisc11e7bb2015-04-03 05:07:08181 // Used to defer underflow for video when audio is present.
182 base::CancelableClosure deferred_underflow_cb_;
183
184 // The amount of time to wait before declaring underflow if the video renderer
185 // runs out of data but the audio renderer still has enough.
186 base::TimeDelta video_underflow_threshold_;
187
xhwangbe9da702014-08-23 21:44:55188 base::WeakPtr<RendererImpl> weak_this_;
dmichael284c5e92014-12-09 20:02:47189 base::WeakPtrFactory<RendererImpl> weak_factory_;
xhwangbe9da702014-08-23 21:44:55190
191 DISALLOW_COPY_AND_ASSIGN(RendererImpl);
192};
193
194} // namespace media
195
servolkf54f5c8f2015-02-24 20:32:39196#endif // MEDIA_RENDERERS_RENDERER_IMPL_H_