BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / media / base / android / media_source_player.h
blobab3bda3e72c3f4eaadd3647b7238a73b5ac81b61
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_
8 #include <jni.h>
9 #include <map>
10 #include <string>
11 #include <vector>
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"
29 namespace media {
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 {
38 public:
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 scoped_ptr<DemuxerAndroid> demuxer,
45 const GURL& frame_url);
46 ~MediaSourcePlayer() override;
48 // MediaPlayerAndroid implementation.
49 void SetVideoSurface(gfx::ScopedJavaSurface surface) override;
50 void Start() override;
51 void Pause(bool is_media_related_action) override;
52 void SeekTo(base::TimeDelta timestamp) override;
53 void Release() override;
54 void SetVolume(double volume) override;
55 int GetVideoWidth() override;
56 int GetVideoHeight() override;
57 base::TimeDelta GetCurrentTime() override;
58 base::TimeDelta GetDuration() override;
59 bool IsPlaying() override;
60 bool CanPause() override;
61 bool CanSeekForward() override;
62 bool CanSeekBackward() override;
63 bool IsPlayerReady() override;
64 void SetCdm(BrowserCdm* cdm) override;
66 // DemuxerAndroidClient implementation.
67 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override;
68 void OnDemuxerDataAvailable(const DemuxerData& params) override;
69 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override;
70 void OnDemuxerDurationChanged(base::TimeDelta duration) override;
72 private:
73 friend class MediaSourcePlayerTest;
75 // Update the current timestamp.
76 void UpdateTimestamps(base::TimeDelta current_presentation_timestamp,
77 base::TimeDelta max_presentation_timestamp);
79 // Helper function for starting media playback.
80 void StartInternal();
82 // Playback is completed for one channel.
83 void PlaybackCompleted(bool is_audio);
85 // Called when the decoder finishes its task.
86 void MediaDecoderCallback(
87 bool is_audio, MediaCodecStatus status,
88 base::TimeDelta current_presentation_timestamp,
89 base::TimeDelta max_presentation_timestamp);
91 bool IsPrerollFinished(bool is_audio) const;
93 // Gets MediaCrypto object from |drm_bridge_|.
94 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
96 // Callback to notify that MediaCrypto is ready in |drm_bridge_|.
97 void OnMediaCryptoReady();
99 // Handle pending events if all the decoder jobs are not currently decoding.
100 void ProcessPendingEvents();
102 // Flush the decoders and clean up all the data needs to be decoded.
103 void ClearDecodingData();
105 // Called to decode more data.
106 void DecodeMoreAudio();
107 void DecodeMoreVideo();
109 // Functions check whether audio/video is present.
110 bool HasVideo() const;
111 bool HasAudio() const;
113 // Functions that check whether audio/video stream has reached end of output
114 // or are not present in player configuration.
115 bool AudioFinished();
116 bool VideoFinished();
118 // Determine seekability based on duration.
119 bool Seekable();
121 // Called when the |decoder_starvation_callback_| times out.
122 void OnDecoderStarved();
124 // Starts the |decoder_starvation_callback_| task with the timeout value.
125 // |current_presentation_timestamp| - The presentation timestamp used for
126 // starvation timeout computations. It represents the current timestamp of
127 // rendered data.
128 // |max_presentation_timestamp| - The presentation timestamp if all the
129 // decoded data are rendered.
130 void StartStarvationCallback(
131 base::TimeDelta current_presentation_timestamp,
132 base::TimeDelta max_presentation_timestamp);
134 // Schedules a seek event in |pending_events_| and calls StopDecode() on all
135 // the MediaDecoderJobs. Sets clock to |seek_time|, and resets
136 // |pending_seek_|. There must not already be a seek event in
137 // |pending_events_|.
138 void ScheduleSeekEventAndStopDecoding(base::TimeDelta seek_time);
140 // Schedules a browser seek event. We must not currently be processing any
141 // seek. Note that there is possibility that browser seek of renderer demuxer
142 // may unexpectedly stall due to lack of buffered data at or after the browser
143 // seek time.
144 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data
145 // since last keyframe. See http://crbug.com/304234.
146 void BrowserSeekToCurrentTime();
148 // Called when a MediaDecoderJob finishes prefetching data. Once all
149 // MediaDecoderJobs have prefetched data, then this method updates
150 // |start_time_ticks_| and |start_presentation_timestamp_| so that video can
151 // resync with audio and starts decoding.
152 void OnPrefetchDone();
154 // Called when the demuxer config changes.
155 void OnDemuxerConfigsChanged();
157 // Called when new decryption key becomes available.
158 void OnKeyAdded();
160 // Called to resume playback after NO_KEY is received, but a new key is
161 // available.
162 void ResumePlaybackAfterKeyAdded();
164 // Called when the CDM is detached.
165 void OnCdmUnset();
167 // Test-only method to setup hook for the completion of the next decode cycle.
168 // This callback state is cleared when it is next run.
169 // Prevent usage creep by only calling this from the
170 // ReleaseWithOnPrefetchDoneAlreadyPosted MediaSourcePlayerTest.
171 void set_decode_callback_for_testing(const base::Closure& test_decode_cb) {
172 decode_callback_for_testing_ = test_decode_cb;
175 // Please keep this in sync with |kPendingEventNames| in GetEventName().
176 enum PendingEventFlags {
177 NO_EVENT_PENDING = 0,
178 PREFETCH_DONE_EVENT_PENDING = 1 << 0,
179 SEEK_EVENT_PENDING = 1 << 1,
180 DECODER_CREATION_EVENT_PENDING = 1 << 2,
181 PREFETCH_REQUEST_EVENT_PENDING = 1 << 3,
184 static const char* GetEventName(PendingEventFlags event);
185 bool IsEventPending(PendingEventFlags event) const;
186 void SetPendingEvent(PendingEventFlags event);
187 void ClearPendingEvent(PendingEventFlags event);
189 // If the player is previously waiting for audio or video decoder job, retry
190 // creating the decoders identified by |audio| and |video|.
191 void RetryDecoderCreation(bool audio, bool video);
193 scoped_ptr<DemuxerAndroid> demuxer_;
195 // Pending event that the player needs to do.
196 unsigned pending_event_;
198 // Stats about the media.
199 base::TimeDelta duration_;
200 bool playing_;
202 // base::TickClock used by |interpolator_|.
203 base::DefaultTickClock default_tick_clock_;
205 // Tracks the most recent media time update and provides interpolated values
206 // as playback progresses.
207 TimeDeltaInterpolator interpolator_;
209 // Timestamps for providing simple A/V sync. When start decoding an audio
210 // chunk, we record its presentation timestamp and the current system time.
211 // Then we use this information to estimate when the next audio/video frame
212 // should be rendered.
213 // TODO(qinmin): Need to fix the problem if audio/video lagged too far behind
214 // due to network or decoding problem.
215 base::TimeTicks start_time_ticks_;
216 base::TimeDelta start_presentation_timestamp_;
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.
228 bool pending_seek_;
229 base::TimeDelta pending_seek_time_;
231 // Decoder jobs.
232 scoped_ptr<AudioDecoderJob, MediaDecoderJob::Deleter> audio_decoder_job_;
233 scoped_ptr<VideoDecoderJob, MediaDecoderJob::Deleter> video_decoder_job_;
235 // Track the most recent preroll target. Decoder re-creation needs this to
236 // resume any in-progress preroll.
237 base::TimeDelta preroll_timestamp_;
239 // A cancelable task that is posted when the audio decoder starts requesting
240 // new data. This callback runs if no data arrives before the timeout period
241 // elapses.
242 base::CancelableClosure decoder_starvation_callback_;
244 MediaDrmBridge* drm_bridge_;
245 int cdm_registration_id_;
247 // No decryption key available to decrypt the encrypted buffer. In this case,
248 // the player should pause. When a new key is added (OnKeyAdded()), we should
249 // try to start playback again.
250 bool is_waiting_for_key_;
252 // Indicates the situation where new key is added during pending decode
253 // (this variable can only be set when *_decoder_job_->is_decoding()). If this
254 // variable is true and MEDIA_CODEC_NO_KEY is returned then we need to try
255 // decoding again in case the newly added key is the correct decryption key.
256 bool key_added_while_decode_pending_;
258 // Indicates whether the player is waiting for audio or video decoder to be
259 // created. This could happen if video surface is not available or key is
260 // not added.
261 bool is_waiting_for_audio_decoder_;
262 bool is_waiting_for_video_decoder_;
264 // Test-only callback for hooking the completion of the next decode cycle.
265 base::Closure decode_callback_for_testing_;
267 // Whether audio or video decoder is in the process of prerolling.
268 bool prerolling_;
270 // Weak pointer passed to media decoder jobs for callbacks.
271 base::WeakPtr<MediaSourcePlayer> weak_this_;
272 // NOTE: Weak pointers must be invalidated before all other member variables.
273 base::WeakPtrFactory<MediaSourcePlayer> weak_factory_;
275 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer);
278 } // namespace media
280 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_