Roll src/third_party/WebKit d10c917:a1123a1 (svn 198729:198730)
[chromium-blink-merge.git] / media / base / android / media_codec_player.h
blobf9f95703bd997648b7171838aa946aa7d4b1c41b
1 // Copyright 2015 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_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
8 #include "base/android/scoped_java_ref.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/threading/thread.h"
12 #include "base/time/default_tick_clock.h"
13 #include "media/base/android/demuxer_android.h"
14 #include "media/base/android/media_player_android.h"
15 #include "media/base/demuxer_stream.h"
16 #include "media/base/media_export.h"
17 #include "media/base/time_delta_interpolator.h"
18 #include "ui/gfx/geometry/size.h"
19 #include "ui/gl/android/scoped_java_surface.h"
21 // The MediaCodecPlayer class implements the media player by using Android's
22 // MediaCodec. It differs from MediaSourcePlayer in that it removes most
23 // processing away from the UI thread: it uses a dedicated Media thread to
24 // receive the data and to handle the commands.
26 // The player works as a state machine. Here are relationships between states:
28 // [ Paused ] ------------------------ (Any state)
29 // | | |
30 // | v v
31 // | <------------------[ WaitingForConfig ] [ Error ]
32 // |
33 // |
34 // |
35 // v
36 // [ Prefetching ] -------------------
37 // | |
38 // | v
39 // | <-----------------[ WaitingForSurface ]
40 // v
41 // [ Playing ]
42 // |
43 // |
44 // v
45 // [ Stopping ]
48 // Events and actions for pause/resume workflow.
49 // ---------------------------------------------
51 // Start, no config:
52 // ------------------------> [ Paused ] -----------------> [ Waiting ]
53 // | StopDone: [ for configs ]
54 // | ^ | /
55 // | | | /
56 // | Pause: | | Start w/config: /
57 // | | | dec.Prefetch /
58 // | | | /
59 // | | | /
60 // | | | /
61 // | | | / DemuxerConfigs:
62 // | | | / dec.Prefetch
63 // | | | /
64 // | | | /
65 // | | v /
66 // | /
67 // | ------------------> [ Prefetching ] <--------/ [ Waiting ]
68 // | | [ ] --------------> [ for surface ]
69 // | | | PrefetchDone, /
70 // | | | no surface: /
71 // | | | /
72 // | | | /
73 // | | StopDone w/ | /
74 // | | pending start: | PrefetchDone: /
75 // | | dec.Prefetch | dec.Start /
76 // | | | / SetSurface:
77 // | | | / dec.Start
78 // | | | /
79 // | | v /
80 // | | /
81 // | | [ Playing ] <----------/
82 // | |
83 // | | |
84 // | | |
85 // | | | Pause: dec.RequestToStop
86 // | | |
87 // | | |
88 // | | v
89 // | |
90 // ------------------------- [ Stopping ]
92 namespace media {
94 class BrowserCdm;
95 class MediaCodecAudioDecoder;
96 class MediaCodecVideoDecoder;
98 // Returns the task runner for the media thread
99 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner();
101 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid,
102 public DemuxerAndroidClient {
103 public:
104 // Typedefs for the notification callbacks
105 typedef base::Callback<void(base::TimeDelta, const gfx::Size&)>
106 MetadataChangedCallback;
108 typedef base::Callback<void(base::TimeDelta, base::TimeTicks)>
109 TimeUpdateCallback;
111 // Constructs a player with the given ID and demuxer. |manager| must outlive
112 // the lifetime of this object.
113 MediaCodecPlayer(int player_id,
114 base::WeakPtr<MediaPlayerManager> manager,
115 const RequestMediaResourcesCB& request_media_resources_cb,
116 scoped_ptr<DemuxerAndroid> demuxer,
117 const GURL& frame_url);
118 ~MediaCodecPlayer() override;
120 // A helper method that performs the media thread part of initialization.
121 void Initialize();
123 // MediaPlayerAndroid implementation.
124 void DeleteOnCorrectThread() override;
125 void SetVideoSurface(gfx::ScopedJavaSurface surface) override;
126 void Start() override;
127 void Pause(bool is_media_related_action) override;
128 void SeekTo(base::TimeDelta timestamp) override;
129 void Release() override;
130 void SetVolume(double volume) override;
131 int GetVideoWidth() override;
132 int GetVideoHeight() override;
133 base::TimeDelta GetCurrentTime() override;
134 base::TimeDelta GetDuration() override;
135 bool IsPlaying() override;
136 bool CanPause() override;
137 bool CanSeekForward() override;
138 bool CanSeekBackward() override;
139 bool IsPlayerReady() override;
140 void SetCdm(BrowserCdm* cdm) override;
142 // DemuxerAndroidClient implementation.
143 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override;
144 void OnDemuxerDataAvailable(const DemuxerData& params) override;
145 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override;
146 void OnDemuxerDurationChanged(base::TimeDelta duration) override;
148 private:
149 // The state machine states.
150 enum PlayerState {
151 STATE_PAUSED,
152 STATE_WAITING_FOR_CONFIG,
153 STATE_PREFETCHING,
154 STATE_PLAYING,
155 STATE_STOPPING,
156 STATE_WAITING_FOR_SURFACE,
157 STATE_ERROR,
160 // Cached values for the manager.
161 struct MediaMetadata {
162 base::TimeDelta duration;
163 gfx::Size video_size;
166 // MediaPlayerAndroid implementation.
167 // This method caches the data and calls manager's OnMediaMetadataChanged().
168 void OnMediaMetadataChanged(base::TimeDelta duration,
169 const gfx::Size& video_size) override;
171 // This method caches the current time and calls manager's OnTimeUpdate().
172 void OnTimeUpdate(base::TimeDelta current_timestamp,
173 base::TimeTicks current_time_ticks) override;
175 // Callbacks from decoders
176 void RequestDemuxerData(DemuxerStream::Type stream_type);
177 void OnPrefetchDone();
178 void OnStopDone();
179 void OnError();
180 void OnStarvation(DemuxerStream::Type stream_type);
181 void OnTimeIntervalUpdate(DemuxerStream::Type stream_type,
182 base::TimeDelta now_playing,
183 base::TimeDelta last_buffered);
185 // Callbacks from video decoder
186 void OnVideoCodecCreated();
187 void OnVideoResolutionChanged(const gfx::Size& size);
189 // Operations called from the state machine.
190 void SetState(PlayerState new_state);
191 void SetPendingSurface(gfx::ScopedJavaSurface surface);
192 bool HasPendingSurface();
193 void SetPendingStart(bool need_to_start);
194 bool HasPendingStart();
195 bool HasVideo();
196 bool HasAudio();
197 void SetDemuxerConfigs(const DemuxerConfigs& configs);
198 void StartPrefetchDecoders();
199 void StartPlaybackDecoders();
200 void StopDecoders();
201 void RequestToStopDecoders();
202 void ReleaseDecoderResources();
204 // Helper methods.
205 void CreateDecoders();
206 bool AudioFinished();
207 bool VideoFinished();
208 base::TimeDelta GetInterpolatedTime();
210 static const char* AsString(PlayerState state);
212 // Data.
214 // Object for posting tasks on UI thread.
215 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
217 // Major components: demuxer, audio and video decoders.
218 scoped_ptr<DemuxerAndroid> demuxer_;
219 scoped_ptr<MediaCodecAudioDecoder> audio_decoder_;
220 scoped_ptr<MediaCodecVideoDecoder> video_decoder_;
222 // The state of the state machine.
223 PlayerState state_;
225 // Notification callbacks, they call MediaPlayerManager.
226 base::Closure request_resources_cb_;
227 TimeUpdateCallback time_update_cb_;
228 base::Closure completion_cb_;
230 // A callback that updates metadata cache and calls the manager.
231 MetadataChangedCallback metadata_changed_cb_;
233 // We call the base class' AttachListener() and DetachListener() methods on UI
234 // thread with these callbacks.
235 base::Closure attach_listener_cb_;
236 base::Closure detach_listener_cb_;
238 // Error callback is posted by decoders or by this class itself if we cannot
239 // configure or start decoder.
240 base::Closure error_cb_;
242 // Total duration reported by demuxer.
243 base::TimeDelta duration_;
245 // base::TickClock used by |interpolator_|.
246 base::DefaultTickClock default_tick_clock_;
248 // Tracks the most recent media time update and provides interpolated values
249 // as playback progresses.
250 TimeDeltaInterpolator interpolator_;
252 // Pending data to be picked up by the upcoming state.
253 gfx::ScopedJavaSurface pending_surface_;
254 bool pending_start_;
256 // Configuration data for the manager, accessed on the UI thread.
257 MediaMetadata metadata_cache_;
259 // Cached current time, accessed on UI thread.
260 base::TimeDelta current_time_cache_;
262 base::WeakPtr<MediaCodecPlayer> media_weak_this_;
263 // NOTE: Weak pointers must be invalidated before all other member variables.
264 base::WeakPtrFactory<MediaCodecPlayer> media_weak_factory_;
266 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer);
269 } // namespace media
271 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_