| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ |
| 7 | 7 |
| 8 #include <jni.h> | |
| 9 #include <map> | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/android/scoped_java_ref.h" | 8 #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" | 9 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 18 #include "base/threading/thread.h" | 11 #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" | 12 #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" | 13 #include "media/base/android/media_player_android.h" |
| 26 #include "media/base/media_export.h" | 14 #include "media/base/media_export.h" |
| 27 #include "media/base/time_delta_interpolator.h" | |
| 28 | 15 |
| 29 namespace media { | 16 namespace media { |
| 30 | 17 |
| 31 class AudioDecoderJob; | 18 // Returns the task runner for the media thread |
| 32 class VideoDecoderJob; | 19 scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); |
| 33 | 20 |
| 34 // This class handles media source extensions on Android. It uses Android | 21 // This class handles media source extensions on Android. |
| 35 // MediaCodec to decode audio and video streams in two separate threads. | |
| 36 class MEDIA_EXPORT MediaSourcePlayer : public MediaPlayerAndroid, | 22 class MEDIA_EXPORT MediaSourcePlayer : public MediaPlayerAndroid, |
| 37 public DemuxerAndroidClient { | 23 public DemuxerAndroidClient { |
| 38 public: | 24 public: |
| 39 // Constructs a player with the given ID and demuxer. |manager| must outlive | 25 // Constructs a player with the given ID and demuxer. |manager| must outlive |
| 40 // the lifetime of this object. | 26 // the lifetime of this object. |
| 41 MediaSourcePlayer(int player_id, | 27 MediaSourcePlayer(int player_id, |
| 42 MediaPlayerManager* manager, | 28 MediaPlayerManager* manager, |
| 43 const RequestMediaResourcesCB& request_media_resources_cb, | 29 const RequestMediaResourcesCB& request_media_resources_cb, |
| 44 scoped_ptr<DemuxerAndroid> demuxer, | 30 scoped_ptr<DemuxerAndroid> demuxer, |
| 45 const GURL& frame_url); | 31 const GURL& frame_url); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 63 bool IsPlayerReady() override; | 49 bool IsPlayerReady() override; |
| 64 void SetCdm(BrowserCdm* cdm) override; | 50 void SetCdm(BrowserCdm* cdm) override; |
| 65 | 51 |
| 66 // DemuxerAndroidClient implementation. | 52 // DemuxerAndroidClient implementation. |
| 67 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; | 53 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; |
| 68 void OnDemuxerDataAvailable(const DemuxerData& params) override; | 54 void OnDemuxerDataAvailable(const DemuxerData& params) override; |
| 69 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; | 55 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; |
| 70 void OnDemuxerDurationChanged(base::TimeDelta duration) override; | 56 void OnDemuxerDurationChanged(base::TimeDelta duration) override; |
| 71 | 57 |
| 72 private: | 58 private: |
| 73 friend class MediaSourcePlayerTest; | |
| 74 | 59 |
| 75 // Update the current timestamp. | 60 // Events from UI posted to Media thread |
| 76 void UpdateTimestamps(base::TimeDelta current_presentation_timestamp, | 61 void SetVideoSurfaceInternal(gfx::ScopedJavaSurface surface); |
| 77 base::TimeDelta max_presentation_timestamp); | 62 void StartInternal(); |
| 63 void PauseInternal(); |
| 64 void SeekToInternal(base::TimeDelta timestamp); |
| 65 void ReleaseInternal(); |
| 78 | 66 |
| 79 // Helper function for starting media playback. | 67 // Data |
| 80 void StartInternal(); | |
| 81 | |
| 82 // Playback is completed for one channel. | |
| 83 void PlaybackCompleted(bool is_audio); | |
| 84 | |
| 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); | |
| 90 | |
| 91 bool IsPrerollFinished(bool is_audio) const; | |
| 92 | |
| 93 // Gets MediaCrypto object from |drm_bridge_|. | |
| 94 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | |
| 95 | |
| 96 // Callback to notify that MediaCrypto is ready in |drm_bridge_|. | |
| 97 void OnMediaCryptoReady(); | |
| 98 | |
| 99 // Handle pending events if all the decoder jobs are not currently decoding. | |
| 100 void ProcessPendingEvents(); | |
| 101 | |
| 102 // Flush the decoders and clean up all the data needs to be decoded. | |
| 103 void ClearDecodingData(); | |
| 104 | |
| 105 // Called to decode more data. | |
| 106 void DecodeMoreAudio(); | |
| 107 void DecodeMoreVideo(); | |
| 108 | |
| 109 // Functions check whether audio/video is present. | |
| 110 bool HasVideo() const; | |
| 111 bool HasAudio() const; | |
| 112 | |
| 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(); | |
| 117 | |
| 118 // Determine seekability based on duration. | |
| 119 bool Seekable(); | |
| 120 | |
| 121 // Called when the |decoder_starvation_callback_| times out. | |
| 122 void OnDecoderStarved(); | |
| 123 | |
| 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); | |
| 133 | |
| 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); | |
| 139 | |
| 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(); | |
| 147 | |
| 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(); | |
| 153 | |
| 154 // Called when the demuxer config changes. | |
| 155 void OnDemuxerConfigsChanged(); | |
| 156 | |
| 157 // Called when new decryption key becomes available. | |
| 158 void OnKeyAdded(); | |
| 159 | |
| 160 // Called to resume playback after NO_KEY is received, but a new key is | |
| 161 // available. | |
| 162 void ResumePlaybackAfterKeyAdded(); | |
| 163 | |
| 164 // Called when the CDM is detached. | |
| 165 void OnCdmUnset(); | |
| 166 | |
| 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; | |
| 173 } | |
| 174 | |
| 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, | |
| 182 }; | |
| 183 | |
| 184 static const char* GetEventName(PendingEventFlags event); | |
| 185 bool IsEventPending(PendingEventFlags event) const; | |
| 186 void SetPendingEvent(PendingEventFlags event); | |
| 187 void ClearPendingEvent(PendingEventFlags event); | |
| 188 | |
| 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); | |
| 192 | 68 |
| 193 scoped_ptr<DemuxerAndroid> demuxer_; | 69 scoped_ptr<DemuxerAndroid> demuxer_; |
| 194 | 70 |
| 195 // Pending event that the player needs to do. | |
| 196 unsigned pending_event_; | |
| 197 | |
| 198 // Stats about the media. | |
| 199 base::TimeDelta duration_; | |
| 200 bool playing_; | |
| 201 | |
| 202 // base::TickClock used by |interpolator_|. | |
| 203 base::DefaultTickClock default_tick_clock_; | |
| 204 | |
| 205 // Tracks the most recent media time update and provides interpolated values | |
| 206 // as playback progresses. | |
| 207 TimeDeltaInterpolator interpolator_; | |
| 208 | |
| 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_; | |
| 217 | |
| 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_; | |
| 223 | |
| 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_; | |
| 230 | |
| 231 // Decoder jobs. | |
| 232 scoped_ptr<AudioDecoderJob, MediaDecoderJob::Deleter> audio_decoder_job_; | |
| 233 scoped_ptr<VideoDecoderJob, MediaDecoderJob::Deleter> video_decoder_job_; | |
| 234 | |
| 235 // Track the most recent preroll target. Decoder re-creation needs this to | |
| 236 // resume any in-progress preroll. | |
| 237 base::TimeDelta preroll_timestamp_; | |
| 238 | |
| 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_; | |
| 243 | |
| 244 MediaDrmBridge* drm_bridge_; | |
| 245 int cdm_registration_id_; | |
| 246 | |
| 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_; | |
| 251 | |
| 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_; | |
| 257 | |
| 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_; | |
| 263 | |
| 264 // Test-only callback for hooking the completion of the next decode cycle. | |
| 265 base::Closure decode_callback_for_testing_; | |
| 266 | |
| 267 // Whether audio or video decoder is in the process of prerolling. | |
| 268 bool prerolling_; | |
| 269 | |
| 270 // Weak pointer passed to media decoder jobs for callbacks. | |
| 271 base::WeakPtr<MediaSourcePlayer> weak_this_; | 71 base::WeakPtr<MediaSourcePlayer> weak_this_; |
| 272 // NOTE: Weak pointers must be invalidated before all other member variables. | 72 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 273 base::WeakPtrFactory<MediaSourcePlayer> weak_factory_; | 73 base::WeakPtrFactory<MediaSourcePlayer> weak_factory_; |
| 274 | 74 |
| 275 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer); | 75 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer); |
| 276 }; | 76 }; |
| 277 | 77 |
| 278 } // namespace media | 78 } // namespace media |
| 279 | 79 |
| 280 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ | 80 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ |
| OLD | NEW |