| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | |
| 7 | |
| 8 #include <jni.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/android/scoped_java_ref.h" | |
| 16 #include "base/callback.h" | |
| 17 #include "base/cancelable_callback.h" | |
| 18 #include "base/macros.h" | |
| 19 #include "base/memory/weak_ptr.h" | |
| 20 #include "base/threading/thread.h" | |
| 21 #include "base/time/default_tick_clock.h" | |
| 22 #include "base/time/time.h" | |
| 23 #include "media/base/android/demuxer_android.h" | |
| 24 #include "media/base/android/media_codec_bridge.h" | |
| 25 #include "media/base/android/media_decoder_job.h" | |
| 26 #include "media/base/android/media_drm_bridge.h" | |
| 27 #include "media/base/android/media_player_android.h" | |
| 28 #include "media/base/android/media_statistics.h" | |
| 29 #include "media/base/media_export.h" | |
| 30 #include "media/base/time_delta_interpolator.h" | |
| 31 | |
| 32 namespace media { | |
| 33 | |
| 34 class AudioDecoderJob; | |
| 35 class VideoDecoderJob; | |
| 36 | |
| 37 // This class handles media source extensions on Android. It uses Android | |
| 38 // MediaCodec to decode audio and video streams in two separate threads. | |
| 39 class MEDIA_EXPORT MediaSourcePlayer : public MediaPlayerAndroid, | |
| 40 public DemuxerAndroidClient { | |
| 41 public: | |
| 42 // Constructs a player with the given ID and demuxer. |manager| must outlive | |
| 43 // the lifetime of this object. | |
| 44 MediaSourcePlayer( | |
| 45 int player_id, | |
| 46 MediaPlayerManager* manager, | |
| 47 const OnDecoderResourcesReleasedCB& on_decoder_resources_released_cb, | |
| 48 std::unique_ptr<DemuxerAndroid> demuxer, | |
| 49 const GURL& frame_url); | |
| 50 ~MediaSourcePlayer() override; | |
| 51 | |
| 52 // MediaPlayerAndroid implementation. | |
| 53 void SetVideoSurface(gl::ScopedJavaSurface surface) override; | |
| 54 void Start() override; | |
| 55 void Pause(bool is_media_related_action) override; | |
| 56 void SeekTo(base::TimeDelta timestamp) override; | |
| 57 void Release() override; | |
| 58 bool HasVideo() const override; | |
| 59 bool HasAudio() const override; | |
| 60 int GetVideoWidth() override; | |
| 61 int GetVideoHeight() override; | |
| 62 base::TimeDelta GetCurrentTime() override; | |
| 63 base::TimeDelta GetDuration() override; | |
| 64 bool IsPlaying() override; | |
| 65 bool CanPause() override; | |
| 66 bool CanSeekForward() override; | |
| 67 bool CanSeekBackward() override; | |
| 68 bool IsPlayerReady() override; | |
| 69 void SetCdm(const scoped_refptr<MediaKeys>& cdm) override; | |
| 70 | |
| 71 // DemuxerAndroidClient implementation. | |
| 72 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; | |
| 73 void OnDemuxerDataAvailable(const DemuxerData& params) override; | |
| 74 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; | |
| 75 void OnDemuxerDurationChanged(base::TimeDelta duration) override; | |
| 76 | |
| 77 private: | |
| 78 friend class MediaSourcePlayerTest; | |
| 79 | |
| 80 // MediaPlayerAndroid implementation | |
| 81 void UpdateEffectiveVolumeInternal(double effective_volume) override; | |
| 82 | |
| 83 // Update the current timestamp. | |
| 84 void UpdateTimestamps(base::TimeDelta current_presentation_timestamp, | |
| 85 base::TimeDelta max_presentation_timestamp); | |
| 86 | |
| 87 // Helper function for starting media playback. | |
| 88 void StartInternal(); | |
| 89 | |
| 90 // Playback is completed for one channel. | |
| 91 void PlaybackCompleted(bool is_audio); | |
| 92 | |
| 93 // Called when the decoder finishes its task. | |
| 94 void MediaDecoderCallback(bool is_audio, | |
| 95 MediaCodecStatus status, | |
| 96 bool is_late_frame, | |
| 97 base::TimeDelta current_presentation_timestamp, | |
| 98 base::TimeDelta max_presentation_timestamp); | |
| 99 | |
| 100 bool IsPrerollFinished(bool is_audio) const; | |
| 101 | |
| 102 // Gets MediaCrypto object from |drm_bridge_|. | |
| 103 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | |
| 104 | |
| 105 // Callback to notify that MediaCrypto is ready in |drm_bridge_|. | |
| 106 void OnMediaCryptoReady(MediaDrmBridge::JavaObjectPtr media_crypto, | |
| 107 bool needs_protected_surface); | |
| 108 | |
| 109 // Handle pending events if all the decoder jobs are not currently decoding. | |
| 110 void ProcessPendingEvents(); | |
| 111 | |
| 112 // Flush the decoders and clean up all the data needs to be decoded. | |
| 113 void ClearDecodingData(); | |
| 114 | |
| 115 // Called to decode more data. | |
| 116 void DecodeMoreAudio(); | |
| 117 void DecodeMoreVideo(); | |
| 118 | |
| 119 // Functions that check whether audio/video stream has reached end of output | |
| 120 // or are not present in player configuration. | |
| 121 bool AudioFinished(); | |
| 122 bool VideoFinished(); | |
| 123 | |
| 124 // Determine seekability based on duration. | |
| 125 bool Seekable(); | |
| 126 | |
| 127 // Called when the |decoder_starvation_callback_| times out. | |
| 128 void OnDecoderStarved(); | |
| 129 | |
| 130 // Starts the |decoder_starvation_callback_| task with the timeout value. | |
| 131 // |current_presentation_timestamp| - The presentation timestamp used for | |
| 132 // starvation timeout computations. It represents the current timestamp of | |
| 133 // rendered data. | |
| 134 // |max_presentation_timestamp| - The presentation timestamp if all the | |
| 135 // decoded data are rendered. | |
| 136 void StartStarvationCallback( | |
| 137 base::TimeDelta current_presentation_timestamp, | |
| 138 base::TimeDelta max_presentation_timestamp); | |
| 139 | |
| 140 // Schedules a seek event in |pending_events_| and calls StopDecode() on all | |
| 141 // the MediaDecoderJobs. Sets clock to |seek_time|, and resets | |
| 142 // |pending_seek_|. There must not already be a seek event in | |
| 143 // |pending_events_|. | |
| 144 void ScheduleSeekEventAndStopDecoding(base::TimeDelta seek_time); | |
| 145 | |
| 146 // Schedules a browser seek event. We must not currently be processing any | |
| 147 // seek. Note that there is possibility that browser seek of renderer demuxer | |
| 148 // may unexpectedly stall due to lack of buffered data at or after the browser | |
| 149 // seek time. | |
| 150 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data | |
| 151 // since last keyframe. See http://crbug.com/304234. | |
| 152 void BrowserSeekToCurrentTime(); | |
| 153 | |
| 154 // Called when a MediaDecoderJob finishes prefetching data. Once all | |
| 155 // MediaDecoderJobs have prefetched data, then this method updates | |
| 156 // |start_time_ticks_| and |start_presentation_timestamp_| so that video can | |
| 157 // resync with audio and starts decoding. | |
| 158 void OnPrefetchDone(); | |
| 159 | |
| 160 // Called when the demuxer config changes. | |
| 161 void OnDemuxerConfigsChanged(); | |
| 162 | |
| 163 // Called when new decryption key becomes available. | |
| 164 void OnKeyAdded(); | |
| 165 | |
| 166 // Called to resume playback after NO_KEY is received, but a new key is | |
| 167 // available. | |
| 168 void ResumePlaybackAfterKeyAdded(); | |
| 169 | |
| 170 // Test-only method to setup hook for the completion of the next decode cycle. | |
| 171 // This callback state is cleared when it is next run. | |
| 172 // Prevent usage creep by only calling this from the | |
| 173 // ReleaseWithOnPrefetchDoneAlreadyPosted MediaSourcePlayerTest. | |
| 174 void set_decode_callback_for_testing(const base::Closure& test_decode_cb) { | |
| 175 decode_callback_for_testing_ = test_decode_cb; | |
| 176 } | |
| 177 | |
| 178 // Please keep this in sync with |kPendingEventNames| in GetEventName(). | |
| 179 enum PendingEventFlags { | |
| 180 NO_EVENT_PENDING = 0, | |
| 181 PREFETCH_DONE_EVENT_PENDING = 1 << 0, | |
| 182 SEEK_EVENT_PENDING = 1 << 1, | |
| 183 DECODER_CREATION_EVENT_PENDING = 1 << 2, | |
| 184 PREFETCH_REQUEST_EVENT_PENDING = 1 << 3, | |
| 185 }; | |
| 186 | |
| 187 static const char* GetEventName(PendingEventFlags event); | |
| 188 bool IsEventPending(PendingEventFlags event) const; | |
| 189 void SetPendingEvent(PendingEventFlags event); | |
| 190 void ClearPendingEvent(PendingEventFlags event); | |
| 191 | |
| 192 // If the player is previously waiting for audio or video decoder job, retry | |
| 193 // creating the decoders identified by |audio| and |video|. | |
| 194 void RetryDecoderCreation(bool audio, bool video); | |
| 195 | |
| 196 std::unique_ptr<DemuxerAndroid> demuxer_; | |
| 197 | |
| 198 // Pending event that the player needs to do. | |
| 199 unsigned pending_event_; | |
| 200 | |
| 201 // Stats about the media. | |
| 202 base::TimeDelta duration_; | |
| 203 bool playing_; | |
| 204 | |
| 205 // base::TickClock used by |interpolator_|. | |
| 206 base::DefaultTickClock default_tick_clock_; | |
| 207 | |
| 208 // Tracks the most recent media time update and provides interpolated values | |
| 209 // as playback progresses. | |
| 210 TimeDeltaInterpolator interpolator_; | |
| 211 | |
| 212 // Timestamps for providing simple A/V sync. When start decoding an audio | |
| 213 // chunk, we record its presentation timestamp and the current system time. | |
| 214 // Then we use this information to estimate when the next audio/video frame | |
| 215 // should be rendered. | |
| 216 // TODO(qinmin): Need to fix the problem if audio/video lagged too far behind | |
| 217 // due to network or decoding problem. | |
| 218 base::TimeTicks start_time_ticks_; | |
| 219 base::TimeDelta start_presentation_timestamp_; | |
| 220 | |
| 221 // Flag that is true if doing a hack browser seek or false if doing a | |
| 222 // regular seek. Only valid when |SEEK_EVENT_PENDING| is pending. | |
| 223 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data | |
| 224 // since last keyframe. See http://crbug.com/304234. | |
| 225 bool doing_browser_seek_; | |
| 226 | |
| 227 // If already doing a browser seek when a regular seek request arrives, | |
| 228 // these fields remember the regular seek so OnDemuxerSeekDone() can trigger | |
| 229 // it when the browser seek is done. These are only valid when | |
| 230 // |SEEK_EVENT_PENDING| is pending. | |
| 231 bool pending_seek_; | |
| 232 base::TimeDelta pending_seek_time_; | |
| 233 | |
| 234 // Decoder jobs. | |
| 235 std::unique_ptr<AudioDecoderJob, MediaDecoderJob::Deleter> audio_decoder_job_; | |
| 236 std::unique_ptr<VideoDecoderJob, MediaDecoderJob::Deleter> video_decoder_job_; | |
| 237 | |
| 238 // Track the most recent preroll target. Decoder re-creation needs this to | |
| 239 // resume any in-progress preroll. | |
| 240 base::TimeDelta preroll_timestamp_; | |
| 241 | |
| 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 | |
| 244 // elapses. | |
| 245 base::CancelableClosure decoder_starvation_callback_; | |
| 246 | |
| 247 // Holds a ref-count to the CDM. | |
| 248 scoped_refptr<MediaKeys> cdm_; | |
| 249 | |
| 250 int cdm_registration_id_; | |
| 251 | |
| 252 // No decryption key available to decrypt the encrypted buffer. In this case, | |
| 253 // the player should pause. When a new key is added (OnKeyAdded()), we should | |
| 254 // try to start playback again. | |
| 255 bool is_waiting_for_key_; | |
| 256 | |
| 257 // Indicates the situation where new key is added during pending decode | |
| 258 // (this variable can only be set when *_decoder_job_->is_decoding()). If this | |
| 259 // variable is true and MEDIA_CODEC_NO_KEY is returned then we need to try | |
| 260 // decoding again in case the newly added key is the correct decryption key. | |
| 261 bool key_added_while_decode_pending_; | |
| 262 | |
| 263 // Indicates whether the player is waiting for audio or video decoder to be | |
| 264 // created. This could happen if video surface is not available or key is | |
| 265 // not added. | |
| 266 bool is_waiting_for_audio_decoder_; | |
| 267 bool is_waiting_for_video_decoder_; | |
| 268 | |
| 269 // Test-only callback for hooking the completion of the next decode cycle. | |
| 270 base::Closure decode_callback_for_testing_; | |
| 271 | |
| 272 // Whether audio or video decoder is in the process of prerolling. | |
| 273 bool prerolling_; | |
| 274 | |
| 275 // Gathers and reports playback quality statistics to UMA. | |
| 276 // Use pointer to enable replacement of this object for tests. | |
| 277 std::unique_ptr<MediaStatistics> media_stat_; | |
| 278 | |
| 279 // Weak pointer passed to media decoder jobs for callbacks. | |
| 280 base::WeakPtr<MediaSourcePlayer> weak_this_; | |
| 281 // NOTE: Weak pointers must be invalidated before all other member variables. | |
| 282 base::WeakPtrFactory<MediaSourcePlayer> weak_factory_; | |
| 283 | |
| 284 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer); | |
| 285 }; | |
| 286 | |
| 287 } // namespace media | |
| 288 | |
| 289 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | |
| OLD | NEW |