[go: nahoru, domu]

blob: 4c1d00db4d6bda68795313d8e8eddd4870c5ac73 [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
5#ifndef MEDIA_FILTERS_RENDERER_IMPL_H_
6#define MEDIA_FILTERS_RENDERER_IMPL_H_
7
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/media_export.h"
17#include "media/base/pipeline_status.h"
18#include "media/base/renderer.h"
19
20namespace base {
21class SingleThreadTaskRunner;
22}
23
24namespace media {
25
26class AudioRenderer;
xhwang1c5668e2014-09-10 02:42:0427class DemuxerStreamProvider;
xhwangbe9da702014-08-23 21:44:5528class TimeSource;
29class VideoRenderer;
scherkusece89452014-09-09 23:13:3830class WallClockTimeSource;
xhwangbe9da702014-08-23 21:44:5531
32class MEDIA_EXPORT RendererImpl : public Renderer {
33 public:
xhwangabd95fd2014-10-03 07:10:0134 // Renders audio/video streams using |audio_renderer| and |video_renderer|
35 // provided. All methods except for GetMediaTime() run on the |task_runner|.
36 // GetMediaTime() runs on the render main thread because it's part of JS sync
37 // API.
xhwangbe9da702014-08-23 21:44:5538 RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
xhwangbe9da702014-08-23 21:44:5539 scoped_ptr<AudioRenderer> audio_renderer,
40 scoped_ptr<VideoRenderer> video_renderer);
41
42 virtual ~RendererImpl();
43
44 // Renderer implementation.
xhwangabd95fd2014-10-03 07:10:0145 virtual void Initialize(DemuxerStreamProvider* demuxer_stream_provider,
46 const base::Closure& init_cb,
xhwangbe9da702014-08-23 21:44:5547 const StatisticsCB& statistics_cb,
48 const base::Closure& ended_cb,
49 const PipelineStatusCB& error_cb,
scherkusece89452014-09-09 23:13:3850 const BufferingStateCB& buffering_state_cb) OVERRIDE;
xhwangbe9da702014-08-23 21:44:5551 virtual void Flush(const base::Closure& flush_cb) OVERRIDE;
52 virtual void StartPlayingFrom(base::TimeDelta time) OVERRIDE;
53 virtual void SetPlaybackRate(float playback_rate) OVERRIDE;
54 virtual void SetVolume(float volume) OVERRIDE;
55 virtual base::TimeDelta GetMediaTime() OVERRIDE;
56 virtual bool HasAudio() OVERRIDE;
57 virtual bool HasVideo() OVERRIDE;
58 virtual void SetCdm(MediaKeys* cdm) OVERRIDE;
59
60 // Helper functions for testing purposes. Must be called before Initialize().
61 void DisableUnderflowForTesting();
scherkusece89452014-09-09 23:13:3862 void EnableClocklessVideoPlaybackForTesting();
xhwangbe9da702014-08-23 21:44:5563
64 private:
65 enum State {
66 STATE_UNINITIALIZED,
67 STATE_INITIALIZING,
68 STATE_FLUSHING,
69 STATE_PLAYING,
70 STATE_ERROR
71 };
72
scherkusece89452014-09-09 23:13:3873 base::TimeDelta GetMediaTimeForSyncingVideo();
xhwangbe9da702014-08-23 21:44:5574
75 // Helper functions and callbacks for Initialize().
76 void InitializeAudioRenderer();
77 void OnAudioRendererInitializeDone(PipelineStatus status);
78 void InitializeVideoRenderer();
79 void OnVideoRendererInitializeDone(PipelineStatus status);
80
81 // Helper functions and callbacks for Flush().
82 void FlushAudioRenderer();
83 void OnAudioRendererFlushDone();
84 void FlushVideoRenderer();
85 void OnVideoRendererFlushDone();
xhwangbe9da702014-08-23 21:44:5586
87 // Callback executed by filters to update statistics.
88 void OnUpdateStatistics(const PipelineStatistics& stats);
89
90 // Collection of callback methods and helpers for tracking changes in
91 // buffering state and transition from paused/underflow states and playing
92 // states.
93 //
94 // While in the kPlaying state:
95 // - A waiting to non-waiting transition indicates preroll has completed
96 // and StartPlayback() should be called
97 // - A non-waiting to waiting transition indicates underflow has occurred
98 // and PausePlayback() should be called
99 void OnBufferingStateChanged(BufferingState* buffering_state,
100 BufferingState new_buffering_state);
101 bool WaitingForEnoughData() const;
102 void PausePlayback();
103 void StartPlayback();
104
xhwangbe9da702014-08-23 21:44:55105 // Callbacks executed when a renderer has ended.
106 void OnAudioRendererEnded();
107 void OnVideoRendererEnded();
scherkusece89452014-09-09 23:13:38108 bool PlaybackHasEnded() const;
xhwangbe9da702014-08-23 21:44:55109 void RunEndedCallbackIfNeeded();
110
111 // Callback executed when a runtime error happens.
112 void OnError(PipelineStatus error);
113
114 void FireAllPendingCallbacks();
115
116 State state_;
117
118 // Task runner used to execute pipeline tasks.
119 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
120
xhwang1c5668e2014-09-10 02:42:04121 DemuxerStreamProvider* demuxer_stream_provider_;
xhwangbe9da702014-08-23 21:44:55122
123 // Permanent callbacks to notify various renderer states/stats.
124 StatisticsCB statistics_cb_;
125 base::Closure ended_cb_;
126 PipelineStatusCB error_cb_;
127 BufferingStateCB buffering_state_cb_;
128
129 // Temporary callback used for Initialize() and Flush().
xhwang3dd339d2014-09-03 02:00:41130 base::Closure init_cb_;
xhwangbe9da702014-08-23 21:44:55131 base::Closure flush_cb_;
132
133 scoped_ptr<AudioRenderer> audio_renderer_;
134 scoped_ptr<VideoRenderer> video_renderer_;
135
136 // Renderer-provided time source used to control playback.
137 TimeSource* time_source_;
scherkusece89452014-09-09 23:13:38138 scoped_ptr<WallClockTimeSource> wall_clock_time_source_;
139 bool time_ticking_;
xhwangbe9da702014-08-23 21:44:55140
141 // The time to start playback from after starting/seeking has completed.
142 base::TimeDelta start_time_;
143
144 BufferingState audio_buffering_state_;
145 BufferingState video_buffering_state_;
146
147 // Whether we've received the audio/video ended events.
148 bool audio_ended_;
149 bool video_ended_;
150
151 bool underflow_disabled_for_testing_;
scherkusece89452014-09-09 23:13:38152 bool clockless_video_playback_enabled_for_testing_;
xhwangbe9da702014-08-23 21:44:55153
154 // NOTE: Weak pointers must be invalidated before all other member variables.
155 base::WeakPtrFactory<RendererImpl> weak_factory_;
156 base::WeakPtr<RendererImpl> weak_this_;
157
158 DISALLOW_COPY_AND_ASSIGN(RendererImpl);
159};
160
161} // namespace media
162
163#endif // MEDIA_FILTERS_RENDERER_IMPL_H_