1 // Copyright (c) 2013 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_SOURCE_PLAYER_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_
13 #include "base/android/scoped_java_ref.h"
14 #include "base/callback.h"
15 #include "base/cancelable_callback.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/thread.h"
19 #include "base/time/default_tick_clock.h"
20 #include "base/time/time.h"
21 #include "media/base/android/demuxer_android.h"
22 #include "media/base/android/media_codec_bridge.h"
23 #include "media/base/android/media_decoder_job.h"
24 #include "media/base/android/media_player_android.h"
25 #include "media/base/clock.h"
26 #include "media/base/media_export.h"
30 class AudioDecoderJob
;
31 class AudioTimestampHelper
;
32 class VideoDecoderJob
;
34 // This class handles media source extensions on Android. It uses Android
35 // MediaCodec to decode audio and video streams in two separate threads.
36 class MEDIA_EXPORT MediaSourcePlayer
: public MediaPlayerAndroid
,
37 public DemuxerAndroidClient
{
39 // Constructs a player with the given ID and demuxer. |manager| must outlive
40 // the lifetime of this object.
41 MediaSourcePlayer(int player_id
,
42 MediaPlayerManager
* manager
,
43 scoped_ptr
<DemuxerAndroid
> demuxer
);
44 virtual ~MediaSourcePlayer();
46 static bool IsTypeSupported(const std::vector
<uint8
>& scheme_uuid
,
47 const std::string
& security_level
,
48 const std::string
& container
,
49 const std::vector
<std::string
>& codecs
);
51 // MediaPlayerAndroid implementation.
52 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface
) OVERRIDE
;
53 virtual void Start() OVERRIDE
;
54 virtual void Pause(bool is_media_related_action ALLOW_UNUSED
) OVERRIDE
;
55 virtual void SeekTo(const base::TimeDelta
& timestamp
) OVERRIDE
;
56 virtual void Release() OVERRIDE
;
57 virtual void SetVolume(double volume
) OVERRIDE
;
58 virtual int GetVideoWidth() OVERRIDE
;
59 virtual int GetVideoHeight() OVERRIDE
;
60 virtual base::TimeDelta
GetCurrentTime() OVERRIDE
;
61 virtual base::TimeDelta
GetDuration() OVERRIDE
;
62 virtual bool IsPlaying() OVERRIDE
;
63 virtual bool CanPause() OVERRIDE
;
64 virtual bool CanSeekForward() OVERRIDE
;
65 virtual bool CanSeekBackward() OVERRIDE
;
66 virtual bool IsPlayerReady() OVERRIDE
;
67 virtual void SetDrmBridge(MediaDrmBridge
* drm_bridge
) OVERRIDE
;
68 virtual void OnKeyAdded() OVERRIDE
;
70 // DemuxerAndroidClient implementation.
71 virtual void OnDemuxerConfigsAvailable(const DemuxerConfigs
& params
) OVERRIDE
;
72 virtual void OnDemuxerDataAvailable(const DemuxerData
& params
) OVERRIDE
;
73 virtual void OnDemuxerSeekDone(
74 const base::TimeDelta
& actual_browser_seek_time
) OVERRIDE
;
75 virtual void OnDemuxerDurationChanged(base::TimeDelta duration
) OVERRIDE
;
78 // Update the current timestamp.
79 void UpdateTimestamps(const base::TimeDelta
& presentation_timestamp
,
80 size_t audio_output_bytes
);
82 // Helper function for starting media playback.
85 // Playback is completed for one channel.
86 void PlaybackCompleted(bool is_audio
);
88 // Called when the decoder finishes its task.
89 void MediaDecoderCallback(
90 bool is_audio
, MediaCodecStatus status
,
91 const base::TimeDelta
& presentation_timestamp
,
92 size_t audio_output_bytes
);
94 // Gets MediaCrypto object from |drm_bridge_|.
95 base::android::ScopedJavaLocalRef
<jobject
> GetMediaCrypto();
97 // Callback to notify that MediaCrypto is ready in |drm_bridge_|.
98 void OnMediaCryptoReady();
100 // Handle pending events when all the decoder jobs finished.
101 void ProcessPendingEvents();
103 // Helper method to configure the decoder jobs.
104 void ConfigureVideoDecoderJob();
105 void ConfigureAudioDecoderJob();
107 // Flush the decoders and clean up all the data needs to be decoded.
108 void ClearDecodingData();
110 // Called to decoder more data.
111 void DecodeMoreAudio();
112 void DecodeMoreVideo();
114 // Functions check whether audio/video is present.
118 // Determine seekability based on duration.
121 // Called when the |decoder_starvation_callback_| times out.
122 void OnDecoderStarved();
124 // Starts the |decoder_starvation_callback_| task with the timeout value.
125 // |presentation_timestamp| - The presentation timestamp used for starvation
126 // timeout computations. It represents the timestamp of the last piece of
128 void StartStarvationCallback(const base::TimeDelta
& presentation_timestamp
);
130 // Schedules a seek event in |pending_events_| and calls StopDecode() on all
131 // the MediaDecoderJobs. Sets clock to |seek_time|, and resets
132 // |pending_seek_|. There must not already be a seek event in
133 // |pending_events_|.
134 void ScheduleSeekEventAndStopDecoding(const base::TimeDelta
& seek_time
);
136 // Schedules a browser seek event. We must not currently be processing any
137 // seek. Note that there is possibility that browser seek of renderer demuxer
138 // may unexpectedly stall due to lack of buffered data at or after the browser
140 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data
141 // since last keyframe. See http://crbug.com/304234.
142 void BrowserSeekToCurrentTime();
144 // Helper function to set the volume.
145 void SetVolumeInternal();
147 // Helper function to determine whether a protected surface is needed for
149 bool IsProtectedSurfaceRequired();
151 // Called when a MediaDecoderJob finishes prefetching data. Once all
152 // MediaDecoderJobs have prefetched data, then this method updates
153 // |start_time_ticks_| and |start_presentation_timestamp_| so that video can
154 // resync with audio and starts decoding.
155 void OnPrefetchDone();
157 enum PendingEventFlags
{
158 NO_EVENT_PENDING
= 0,
159 SEEK_EVENT_PENDING
= 1 << 0,
160 SURFACE_CHANGE_EVENT_PENDING
= 1 << 1,
161 CONFIG_CHANGE_EVENT_PENDING
= 1 << 2,
162 PREFETCH_REQUEST_EVENT_PENDING
= 1 << 3,
163 PREFETCH_DONE_EVENT_PENDING
= 1 << 4,
166 static const char* GetEventName(PendingEventFlags event
);
167 bool IsEventPending(PendingEventFlags event
) const;
168 void SetPendingEvent(PendingEventFlags event
);
169 void ClearPendingEvent(PendingEventFlags event
);
171 scoped_ptr
<DemuxerAndroid
> demuxer_
;
173 // Pending event that the player needs to do.
174 unsigned pending_event_
;
176 // Stats about the media.
177 base::TimeDelta duration_
;
180 AudioCodec audio_codec_
;
181 VideoCodec video_codec_
;
184 // TODO(xhwang/qinmin): Add |video_extra_data_|.
185 std::vector
<uint8
> audio_extra_data_
;
186 bool audio_finished_
;
187 bool video_finished_
;
189 bool is_audio_encrypted_
;
190 bool is_video_encrypted_
;
193 // base::TickClock used by |clock_|.
194 base::DefaultTickClock default_tick_clock_
;
196 // Reference clock. Keeps track of current playback time.
199 // Timestamps for providing simple A/V sync. When start decoding an audio
200 // chunk, we record its presentation timestamp and the current system time.
201 // Then we use this information to estimate when the next audio/video frame
202 // should be rendered.
203 // TODO(qinmin): Need to fix the problem if audio/video lagged too far behind
204 // due to network or decoding problem.
205 base::TimeTicks start_time_ticks_
;
206 base::TimeDelta start_presentation_timestamp_
;
208 // The surface object currently owned by the player.
209 gfx::ScopedJavaSurface surface_
;
211 // Track whether or not the player has received any video data since the most
212 // recent of player construction, end of last seek, or receiving and
213 // detecting a |kConfigChanged| access unit from the demuxer.
214 // If no such video data has been received, the next video data begins with
215 // an I-frame. Otherwise, we have no such guarantee.
216 bool next_video_data_is_iframe_
;
218 // Flag that is true if doing a hack browser seek or false if doing a
219 // regular seek. Only valid when |SEEK_EVENT_PENDING| is pending.
220 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data
221 // since last keyframe. See http://crbug.com/304234.
222 bool doing_browser_seek_
;
224 // If already doing a browser seek when a regular seek request arrives,
225 // these fields remember the regular seek so OnDemuxerSeekDone() can trigger
226 // it when the browser seek is done. These are only valid when
227 // |SEEK_EVENT_PENDING| is pending.
229 base::TimeDelta pending_seek_time_
;
232 scoped_ptr
<AudioDecoderJob
, MediaDecoderJob::Deleter
> audio_decoder_job_
;
233 scoped_ptr
<VideoDecoderJob
, MediaDecoderJob::Deleter
> video_decoder_job_
;
235 bool reconfig_audio_decoder_
;
236 bool reconfig_video_decoder_
;
238 // Track the most recent preroll target. Decoder re-creation needs this to
239 // resume any in-progress preroll.
240 base::TimeDelta preroll_timestamp_
;
242 // A cancelable task that is posted when the audio decoder starts requesting
243 // new data. This callback runs if no data arrives before the timeout period
245 base::CancelableClosure decoder_starvation_callback_
;
247 // Object to calculate the current audio timestamp for A/V sync.
248 scoped_ptr
<AudioTimestampHelper
> audio_timestamp_helper_
;
250 // Weak pointer passed to media decoder jobs for callbacks.
251 base::WeakPtrFactory
<MediaSourcePlayer
> weak_this_
;
253 MediaDrmBridge
* drm_bridge_
;
255 // No decryption key available to decrypt the encrypted buffer. In this case,
256 // the player should pause. When a new key is added (OnKeyAdded()), we should
257 // try to start playback again.
258 bool is_waiting_for_key_
;
260 friend class MediaSourcePlayerTest
;
261 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer
);
266 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_