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)
31 // | <------------------[ WaitingForConfig ] [ Error ]
36 // [ Prefetching ] -------------------
39 // | <-----------------[ WaitingForSurface ]
48 // Events and actions for pause/resume workflow.
49 // ---------------------------------------------
52 // ------------------------> [ Paused ] -----------------> [ Waiting ]
53 // | StopDone: [ for configs ]
56 // | Pause: | | Start w/config: /
57 // | | | dec.Prefetch /
61 // | | | / DemuxerConfigs:
62 // | | | / dec.Prefetch
67 // | ------------------> [ Prefetching ] <--------/ [ Waiting ]
68 // | | [ ] --------------> [ for surface ]
69 // | | | PrefetchDone, /
70 // | | | no surface: /
73 // | | StopDone w/ | /
74 // | | pending start: | PrefetchDone: /
75 // | | dec.Prefetch | dec.Start /
76 // | | | / SetSurface:
81 // | | [ Playing ] <----------/
85 // | | | Pause: dec.RequestToStop
90 // ------------------------- [ Stopping ]
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
{
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
)>
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.
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
;
149 // The state machine states.
152 STATE_WAITING_FOR_CONFIG
,
156 STATE_WAITING_FOR_SURFACE
,
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();
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();
197 void SetDemuxerConfigs(const DemuxerConfigs
& configs
);
198 void StartPrefetchDecoders();
199 void StartPlaybackDecoders();
201 void RequestToStopDecoders();
202 void ReleaseDecoderResources();
205 void CreateDecoders();
206 bool AudioFinished();
207 bool VideoFinished();
208 base::TimeDelta
GetInterpolatedTime();
210 static const char* AsString(PlayerState state
);
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.
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_
;
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
);
271 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_