[go: nahoru, domu]

blob: 22417857058a9332af7516eae24aa4b33191c586 [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
danakj4d43bc22016-04-26 03:36:048#include <memory>
dalecurtise92934582015-05-12 06:38:009#include <vector>
10
dalecurtisc11e7bb2015-04-03 05:07:0811#include "base/cancelable_callback.h"
avia82b9b52015-12-19 04:27:0812#include "base/macros.h"
xhwangbe9da702014-08-23 21:44:5513#include "base/memory/ref_counted.h"
xhwangbe9da702014-08-23 21:44:5514#include "base/memory/weak_ptr.h"
15#include "base/synchronization/lock.h"
16#include "base/time/clock.h"
17#include "base/time/default_tick_clock.h"
18#include "base/time/time.h"
19#include "media/base/buffering_state.h"
xhwang97de4202014-11-25 08:44:0120#include "media/base/decryptor.h"
alokp16bbeea2016-05-12 23:32:3621#include "media/base/demuxer_stream.h"
xhwangbe9da702014-08-23 21:44:5522#include "media/base/media_export.h"
23#include "media/base/pipeline_status.h"
24#include "media/base/renderer.h"
25
26namespace base {
27class SingleThreadTaskRunner;
28}
29
30namespace media {
31
32class AudioRenderer;
xhwang1c5668e2014-09-10 02:42:0433class DemuxerStreamProvider;
xhwangbe9da702014-08-23 21:44:5534class TimeSource;
35class VideoRenderer;
scherkusece89452014-09-09 23:13:3836class WallClockTimeSource;
xhwangbe9da702014-08-23 21:44:5537
38class MEDIA_EXPORT RendererImpl : public Renderer {
39 public:
xhwangabd95fd2014-10-03 07:10:0140 // Renders audio/video streams using |audio_renderer| and |video_renderer|
41 // provided. All methods except for GetMediaTime() run on the |task_runner|.
42 // GetMediaTime() runs on the render main thread because it's part of JS sync
43 // API.
xhwangbe9da702014-08-23 21:44:5544 RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
danakj4d43bc22016-04-26 03:36:0445 std::unique_ptr<AudioRenderer> audio_renderer,
46 std::unique_ptr<VideoRenderer> video_renderer);
xhwangbe9da702014-08-23 21:44:5547
xhwang97de4202014-11-25 08:44:0148 ~RendererImpl() final;
xhwangbe9da702014-08-23 21:44:5549
50 // Renderer implementation.
dchengc2456542014-10-21 12:23:2751 void Initialize(DemuxerStreamProvider* demuxer_stream_provider,
alokp16bbeea2016-05-12 23:32:3652 RendererClient* client,
53 const PipelineStatusCB& init_cb) final;
xhwang97de4202014-11-25 08:44:0154 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;
a.berwal338bf002015-04-22 11:14:5058 void SetPlaybackRate(double playback_rate) final;
xhwang97de4202014-11-25 08:44:0159 void SetVolume(float volume) final;
60 base::TimeDelta GetMediaTime() final;
61 bool HasAudio() final;
62 bool HasVideo() final;
xhwangbe9da702014-08-23 21:44:5563
64 // Helper functions for testing purposes. Must be called before Initialize().
65 void DisableUnderflowForTesting();
scherkusece89452014-09-09 23:13:3866 void EnableClocklessVideoPlaybackForTesting();
dalecurtisc11e7bb2015-04-03 05:07:0867 void set_time_source_for_testing(TimeSource* time_source) {
68 time_source_ = time_source;
69 }
70 void set_video_underflow_threshold_for_testing(base::TimeDelta threshold) {
71 video_underflow_threshold_ = threshold;
72 }
xhwangbe9da702014-08-23 21:44:5573
74 private:
alokp16bbeea2016-05-12 23:32:3675 class RendererClientInternal;
76
xhwangbe9da702014-08-23 21:44:5577 enum State {
78 STATE_UNINITIALIZED,
xhwanga935e442016-02-11 02:22:4579 STATE_INIT_PENDING_CDM, // Initialization is waiting for the CDM to be set.
80 STATE_INITIALIZING, // Initializing audio/video renderers.
xhwangbe9da702014-08-23 21:44:5581 STATE_FLUSHING,
82 STATE_PLAYING,
83 STATE_ERROR
84 };
85
dalecurtise92934582015-05-12 06:38:0086 bool GetWallClockTimes(const std::vector<base::TimeDelta>& media_timestamps,
87 std::vector<base::TimeTicks>* wall_clock_times);
xhwang97de4202014-11-25 08:44:0188
xhwanga935e442016-02-11 02:22:4589 bool HasEncryptedStream();
90
91 void FinishInitialization(PipelineStatus status);
xhwangbe9da702014-08-23 21:44:5592
93 // Helper functions and callbacks for Initialize().
94 void InitializeAudioRenderer();
95 void OnAudioRendererInitializeDone(PipelineStatus status);
96 void InitializeVideoRenderer();
97 void OnVideoRendererInitializeDone(PipelineStatus status);
98
99 // Helper functions and callbacks for Flush().
100 void FlushAudioRenderer();
101 void OnAudioRendererFlushDone();
102 void FlushVideoRenderer();
103 void OnVideoRendererFlushDone();
xhwangbe9da702014-08-23 21:44:55104
105 // Callback executed by filters to update statistics.
alokp16bbeea2016-05-12 23:32:36106 void OnStatisticsUpdate(const PipelineStatistics& stats);
xhwangbe9da702014-08-23 21:44:55107
108 // Collection of callback methods and helpers for tracking changes in
109 // buffering state and transition from paused/underflow states and playing
110 // states.
111 //
112 // While in the kPlaying state:
113 // - A waiting to non-waiting transition indicates preroll has completed
114 // and StartPlayback() should be called
115 // - A non-waiting to waiting transition indicates underflow has occurred
116 // and PausePlayback() should be called
alokp16bbeea2016-05-12 23:32:36117 void OnBufferingStateChange(DemuxerStream::Type type,
118 BufferingState new_buffering_state);
xhwangbe9da702014-08-23 21:44:55119 bool WaitingForEnoughData() const;
120 void PausePlayback();
121 void StartPlayback();
122
xhwangbe9da702014-08-23 21:44:55123 // Callbacks executed when a renderer has ended.
alokp16bbeea2016-05-12 23:32:36124 void OnRendererEnded(DemuxerStream::Type type);
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);
alokp16bbeea2016-05-12 23:32:36130 void OnWaitingForDecryptionKey();
xhwangbe9da702014-08-23 21:44:55131
xhwangbe9da702014-08-23 21:44:55132 State state_;
133
134 // Task runner used to execute pipeline tasks.
135 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
136
xhwang1c5668e2014-09-10 02:42:04137 DemuxerStreamProvider* demuxer_stream_provider_;
alokp16bbeea2016-05-12 23:32:36138 RendererClient* client_;
xhwangbe9da702014-08-23 21:44:55139
140 // Temporary callback used for Initialize() and Flush().
dalecurtis47046112015-01-23 22:58:20141 PipelineStatusCB init_cb_;
xhwangbe9da702014-08-23 21:44:55142 base::Closure flush_cb_;
143
alokp16bbeea2016-05-12 23:32:36144 std::unique_ptr<RendererClientInternal> audio_renderer_client_;
145 std::unique_ptr<RendererClientInternal> video_renderer_client_;
danakj4d43bc22016-04-26 03:36:04146 std::unique_ptr<AudioRenderer> audio_renderer_;
147 std::unique_ptr<VideoRenderer> video_renderer_;
servolk06e3a9f2016-04-16 02:20:10148
xhwangbe9da702014-08-23 21:44:55149 // Renderer-provided time source used to control playback.
150 TimeSource* time_source_;
danakj4d43bc22016-04-26 03:36:04151 std::unique_ptr<WallClockTimeSource> wall_clock_time_source_;
scherkusece89452014-09-09 23:13:38152 bool time_ticking_;
a.berwal338bf002015-04-22 11:14:50153 double playback_rate_;
xhwangbe9da702014-08-23 21:44:55154
155 // The time to start playback from after starting/seeking has completed.
156 base::TimeDelta start_time_;
157
158 BufferingState audio_buffering_state_;
159 BufferingState video_buffering_state_;
160
161 // Whether we've received the audio/video ended events.
162 bool audio_ended_;
163 bool video_ended_;
164
xhwang97de4202014-11-25 08:44:01165 CdmContext* cdm_context_;
xhwanga935e442016-02-11 02:22:45166 CdmAttachedCB pending_cdm_attached_cb_;
xhwang97de4202014-11-25 08:44:01167
xhwangbe9da702014-08-23 21:44:55168 bool underflow_disabled_for_testing_;
scherkusece89452014-09-09 23:13:38169 bool clockless_video_playback_enabled_for_testing_;
xhwangbe9da702014-08-23 21:44:55170
dalecurtisc11e7bb2015-04-03 05:07:08171 // Used to defer underflow for video when audio is present.
172 base::CancelableClosure deferred_underflow_cb_;
173
174 // The amount of time to wait before declaring underflow if the video renderer
175 // runs out of data but the audio renderer still has enough.
176 base::TimeDelta video_underflow_threshold_;
177
xhwangbe9da702014-08-23 21:44:55178 base::WeakPtr<RendererImpl> weak_this_;
dmichael284c5e92014-12-09 20:02:47179 base::WeakPtrFactory<RendererImpl> weak_factory_;
xhwangbe9da702014-08-23 21:44:55180
181 DISALLOW_COPY_AND_ASSIGN(RendererImpl);
182};
183
184} // namespace media
185
servolkf54f5c8f2015-02-24 20:32:39186#endif // MEDIA_RENDERERS_RENDERER_IMPL_H_