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_drm_bridge.h"
25 #include "media/base/android/media_player_android.h"
26 #include "media/base/media_export.h"
27 #include "media/base/time_delta_interpolator.h"
31 class AudioDecoderJob;
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 const RequestMediaResourcesCB& request_media_resources_cb,
44 const ReleaseMediaResourcesCB& release_media_resources_cb,
45 scoped_ptr<DemuxerAndroid> demuxer,
46 const GURL& frame_url);
47 virtual ~MediaSourcePlayer();
49 // MediaPlayerAndroid implementation.
50 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) OVERRIDE;
51 virtual void Start() OVERRIDE;
52 virtual void Pause(bool is_media_related_action ALLOW_UNUSED) OVERRIDE;
53 virtual void SeekTo(base::TimeDelta timestamp) OVERRIDE;
54 virtual void Release() OVERRIDE;
55 virtual void SetVolume(double volume) OVERRIDE;
56 virtual int GetVideoWidth() OVERRIDE;
57 virtual int GetVideoHeight() OVERRIDE;
58 virtual base::TimeDelta GetCurrentTime() OVERRIDE;
59 virtual base::TimeDelta GetDuration() OVERRIDE;
60 virtual bool IsPlaying() OVERRIDE;
61 virtual bool CanPause() OVERRIDE;
62 virtual bool CanSeekForward() OVERRIDE;
63 virtual bool CanSeekBackward() OVERRIDE;
64 virtual bool IsPlayerReady() OVERRIDE;
65 virtual void SetCdm(BrowserCdm* cdm) OVERRIDE;
66 virtual bool IsSurfaceInUse() const OVERRIDE;
68 // DemuxerAndroidClient implementation.
69 virtual void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) OVERRIDE;
70 virtual void OnDemuxerDataAvailable(const DemuxerData& params) OVERRIDE;
71 virtual void OnDemuxerSeekDone(
72 base::TimeDelta actual_browser_seek_time) OVERRIDE;
73 virtual void OnDemuxerDurationChanged(base::TimeDelta duration) OVERRIDE;
76 friend class MediaSourcePlayerTest;
78 // Update the current timestamp.
79 void UpdateTimestamps(base::TimeDelta current_presentation_timestamp,
80 base::TimeDelta max_presentation_timestamp);
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 base::TimeDelta current_presentation_timestamp,
92 base::TimeDelta max_presentation_timestamp);
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 if all the decoder jobs are not currently decoding.
101 void ProcessPendingEvents();
103 // Flush the decoders and clean up all the data needs to be decoded.
104 void ClearDecodingData();
106 // Called to decode more data.
107 void DecodeMoreAudio();
108 void DecodeMoreVideo();
110 // Functions check whether audio/video is present.
114 // Functions that check whether audio/video stream has reached end of output
115 // or are not present in player configuration.
116 bool AudioFinished();
117 bool VideoFinished();
119 // Determine seekability based on duration.
122 // Called when the |decoder_starvation_callback_| times out.
123 void OnDecoderStarved();
125 // Starts the |decoder_starvation_callback_| task with the timeout value.
126 // |current_presentation_timestamp| - The presentation timestamp used for
127 // starvation timeout computations. It represents the current timestamp of
129 // |max_presentation_timestamp| - The presentation timestamp if all the
130 // decoded data are rendered.
131 void StartStarvationCallback(
132 base::TimeDelta current_presentation_timestamp,
133 base::TimeDelta max_presentation_timestamp);
135 // Schedules a seek event in |pending_events_| and calls StopDecode() on all
136 // the MediaDecoderJobs. Sets clock to |seek_time|, and resets
137 // |pending_seek_|. There must not already be a seek event in
138 // |pending_events_|.
139 void ScheduleSeekEventAndStopDecoding(base::TimeDelta seek_time);
141 // Schedules a browser seek event. We must not currently be processing any
142 // seek. Note that there is possibility that browser seek of renderer demuxer
143 // may unexpectedly stall due to lack of buffered data at or after the browser
145 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data
146 // since last keyframe. See http://crbug.com/304234.
147 void BrowserSeekToCurrentTime();
149 // Helper function to determine whether a protected surface is needed for
151 bool IsProtectedSurfaceRequired();
153 // Called when a MediaDecoderJob finishes prefetching data. Once all
154 // MediaDecoderJobs have prefetched data, then this method updates
155 // |start_time_ticks_| and |start_presentation_timestamp_| so that video can
156 // resync with audio and starts decoding.
157 void OnPrefetchDone();
159 // Called when the demuxer config changes.
160 void OnDemuxerConfigsChanged();
162 // Called when new decryption key becomes available.
165 // Called when the CDM is detached.
168 // Test-only method to setup hook for the completion of the next decode cycle.
169 // This callback state is cleared when it is next run.
170 // Prevent usage creep by only calling this from the
171 // ReleaseWithOnPrefetchDoneAlreadyPosted MediaSourcePlayerTest.
172 void set_decode_callback_for_testing(const base::Closure& test_decode_cb) {
173 decode_callback_for_testing_ = test_decode_cb;
176 // Please keep this in sync with |kPendingEventNames| in GetEventName().
177 enum PendingEventFlags {
178 NO_EVENT_PENDING = 0,
179 PREFETCH_DONE_EVENT_PENDING = 1 << 0,
180 SEEK_EVENT_PENDING = 1 << 1,
181 DECODER_CREATION_EVENT_PENDING = 1 << 2,
182 PREFETCH_REQUEST_EVENT_PENDING = 1 << 3,
185 static const char* GetEventName(PendingEventFlags event);
186 bool IsEventPending(PendingEventFlags event) const;
187 void SetPendingEvent(PendingEventFlags event);
188 void ClearPendingEvent(PendingEventFlags event);
190 // If the player is previously waiting for audio or video decoder job, retry
191 // creating the decoders identified by |audio| and |video|.
192 void RetryDecoderCreation(bool audio, bool video);
194 scoped_ptr<DemuxerAndroid> demuxer_;
196 // Pending event that the player needs to do.
197 unsigned pending_event_;
199 // Stats about the media.
200 base::TimeDelta duration_;
203 // base::TickClock used by |interpolator_|.
204 base::DefaultTickClock default_tick_clock_;
206 // Tracks the most recent media time update and provides interpolated values
207 // as playback progresses.
208 TimeDeltaInterpolator interpolator_;
210 // Timestamps for providing simple A/V sync. When start decoding an audio
211 // chunk, we record its presentation timestamp and the current system time.
212 // Then we use this information to estimate when the next audio/video frame
213 // should be rendered.
214 // TODO(qinmin): Need to fix the problem if audio/video lagged too far behind
215 // due to network or decoding problem.
216 base::TimeTicks start_time_ticks_;
217 base::TimeDelta start_presentation_timestamp_;
219 // Flag that is true if doing a hack browser seek or false if doing a
220 // regular seek. Only valid when |SEEK_EVENT_PENDING| is pending.
221 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data
222 // since last keyframe. See http://crbug.com/304234.
223 bool doing_browser_seek_;
225 // If already doing a browser seek when a regular seek request arrives,
226 // these fields remember the regular seek so OnDemuxerSeekDone() can trigger
227 // it when the browser seek is done. These are only valid when
228 // |SEEK_EVENT_PENDING| is pending.
230 base::TimeDelta pending_seek_time_;
233 scoped_ptr<AudioDecoderJob, MediaDecoderJob::Deleter> audio_decoder_job_;
234 scoped_ptr<VideoDecoderJob, MediaDecoderJob::Deleter> video_decoder_job_;
236 // Track the most recent preroll target. Decoder re-creation needs this to
237 // resume any in-progress preroll.
238 base::TimeDelta preroll_timestamp_;
240 // A cancelable task that is posted when the audio decoder starts requesting
241 // new data. This callback runs if no data arrives before the timeout period
243 base::CancelableClosure decoder_starvation_callback_;
245 MediaDrmBridge* drm_bridge_;
246 int cdm_registration_id_;
248 // No decryption key available to decrypt the encrypted buffer. In this case,
249 // the player should pause. When a new key is added (OnKeyAdded()), we should
250 // try to start playback again.
251 bool is_waiting_for_key_;
253 // Indicates whether the player is waiting for audio or video decoder to be
254 // created. This could happen if video surface is not available or key is
256 bool is_waiting_for_audio_decoder_;
257 bool is_waiting_for_video_decoder_;
259 // Test-only callback for hooking the completion of the next decode cycle.
260 base::Closure decode_callback_for_testing_;
262 // Whether |surface_| is currently used by the player.
263 bool is_surface_in_use_;
265 // Weak pointer passed to media decoder jobs for callbacks.
266 // NOTE: Weak pointers must be invalidated before all other member variables.
267 base::WeakPtrFactory<MediaSourcePlayer> weak_factory_;
268 base::WeakPtr<MediaSourcePlayer> weak_this_;
270 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer);
275 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_