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 #include <map> 10 #include <string> 11 #include <vector> 12 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" 27 28 namespace media { 29 30 class AudioDecoderJob; 31 class AudioTimestampHelper; 32 class VideoDecoderJob; 33 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 scoped_ptr<DemuxerAndroid> demuxer); 44 virtual ~MediaSourcePlayer(); 45 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); 50 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; 69 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; 76 77 private: 78 // Update the current timestamp. 79 void UpdateTimestamps(const base::TimeDelta& presentation_timestamp, 80 size_t audio_output_bytes); 81 82 // Helper function for starting media playback. 83 void StartInternal(); 84 85 // Playback is completed for one channel. 86 void PlaybackCompleted(bool is_audio); 87 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); 93 94 // Gets MediaCrypto object from |drm_bridge_|. 95 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); 96 97 // Callback to notify that MediaCrypto is ready in |drm_bridge_|. 98 void OnMediaCryptoReady(); 99 100 // Handle pending events if all the decoder jobs are not currently decoding. 101 void ProcessPendingEvents(); 102 103 // Helper method to clear any pending |SURFACE_CHANGE_EVENT_PENDING| 104 // and reset |video_decoder_job_| to null. 105 void ResetVideoDecoderJob(); 106 107 // Helper methods to configure the decoder jobs. 108 void ConfigureVideoDecoderJob(); 109 void ConfigureAudioDecoderJob(); 110 111 // Flush the decoders and clean up all the data needs to be decoded. 112 void ClearDecodingData(); 113 114 // Called to decode more data. 115 void DecodeMoreAudio(); 116 void DecodeMoreVideo(); 117 118 // Functions check whether audio/video is present. 119 bool HasVideo(); 120 bool HasAudio(); 121 122 // Functions that check whether audio/video stream has reached end of output 123 // or are not present in player configuration. 124 bool AudioFinished(); 125 bool VideoFinished(); 126 127 // Determine seekability based on duration. 128 bool Seekable(); 129 130 // Called when the |decoder_starvation_callback_| times out. 131 void OnDecoderStarved(); 132 133 // Starts the |decoder_starvation_callback_| task with the timeout value. 134 // |presentation_timestamp| - The presentation timestamp used for starvation 135 // timeout computations. It represents the timestamp of the last piece of 136 // decoded data. 137 void StartStarvationCallback(const base::TimeDelta& presentation_timestamp); 138 139 // Schedules a seek event in |pending_events_| and calls StopDecode() on all 140 // the MediaDecoderJobs. Sets clock to |seek_time|, and resets 141 // |pending_seek_|. There must not already be a seek event in 142 // |pending_events_|. 143 void ScheduleSeekEventAndStopDecoding(const base::TimeDelta& seek_time); 144 145 // Schedules a browser seek event. We must not currently be processing any 146 // seek. Note that there is possibility that browser seek of renderer demuxer 147 // may unexpectedly stall due to lack of buffered data at or after the browser 148 // seek time. 149 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data 150 // since last keyframe. See http://crbug.com/304234. 151 void BrowserSeekToCurrentTime(); 152 153 // Helper function to set the volume. 154 void SetVolumeInternal(); 155 156 // Helper function to determine whether a protected surface is needed for 157 // video playback. 158 bool IsProtectedSurfaceRequired(); 159 160 // Called when a MediaDecoderJob finishes prefetching data. Once all 161 // MediaDecoderJobs have prefetched data, then this method updates 162 // |start_time_ticks_| and |start_presentation_timestamp_| so that video can 163 // resync with audio and starts decoding. 164 void OnPrefetchDone(); 165 166 // Test-only method to setup hook for the completion of the next decode cycle. 167 // This callback state is cleared when it is next run. 168 // Prevent usage creep by only calling this from the 169 // ReleaseWithOnPrefetchDoneAlreadyPosted MediaSourcePlayerTest. set_decode_callback_for_testing(const base::Closure & test_decode_cb)170 void set_decode_callback_for_testing(const base::Closure& test_decode_cb) { 171 decode_callback_for_testing_ = test_decode_cb; 172 } 173 174 // TODO(qinmin/wolenetz): Reorder these based on their priority from 175 // ProcessPendingEvents(). Release() and other routines are dependent upon 176 // priority consistency. 177 enum PendingEventFlags { 178 NO_EVENT_PENDING = 0, 179 SEEK_EVENT_PENDING = 1 << 0, 180 SURFACE_CHANGE_EVENT_PENDING = 1 << 1, 181 CONFIG_CHANGE_EVENT_PENDING = 1 << 2, 182 PREFETCH_REQUEST_EVENT_PENDING = 1 << 3, 183 PREFETCH_DONE_EVENT_PENDING = 1 << 4, 184 }; 185 186 static const char* GetEventName(PendingEventFlags event); 187 bool IsEventPending(PendingEventFlags event) const; 188 void SetPendingEvent(PendingEventFlags event); 189 void ClearPendingEvent(PendingEventFlags event); 190 191 scoped_ptr<DemuxerAndroid> demuxer_; 192 193 // Pending event that the player needs to do. 194 unsigned pending_event_; 195 196 // Stats about the media. 197 base::TimeDelta duration_; 198 int width_; 199 int height_; 200 AudioCodec audio_codec_; 201 VideoCodec video_codec_; 202 int num_channels_; 203 int sampling_rate_; 204 // TODO(xhwang/qinmin): Add |video_extra_data_|. 205 std::vector<uint8> audio_extra_data_; 206 bool reached_audio_eos_; 207 bool reached_video_eos_; 208 bool playing_; 209 bool is_audio_encrypted_; 210 bool is_video_encrypted_; 211 double volume_; 212 213 // base::TickClock used by |clock_|. 214 base::DefaultTickClock default_tick_clock_; 215 216 // Reference clock. Keeps track of current playback time. 217 Clock clock_; 218 219 // Timestamps for providing simple A/V sync. When start decoding an audio 220 // chunk, we record its presentation timestamp and the current system time. 221 // Then we use this information to estimate when the next audio/video frame 222 // should be rendered. 223 // TODO(qinmin): Need to fix the problem if audio/video lagged too far behind 224 // due to network or decoding problem. 225 base::TimeTicks start_time_ticks_; 226 base::TimeDelta start_presentation_timestamp_; 227 228 // The surface object currently owned by the player. 229 gfx::ScopedJavaSurface surface_; 230 231 // Track whether or not the player has received any video data since the most 232 // recent of player construction, end of last seek, or receiving and 233 // detecting a |kConfigChanged| access unit from the demuxer. 234 // If no such video data has been received, the next video data begins with 235 // an I-frame. Otherwise, we have no such guarantee. 236 bool next_video_data_is_iframe_; 237 238 // Flag that is true if doing a hack browser seek or false if doing a 239 // regular seek. Only valid when |SEEK_EVENT_PENDING| is pending. 240 // TODO(wolenetz): Instead of doing hack browser seek, replay cached data 241 // since last keyframe. See http://crbug.com/304234. 242 bool doing_browser_seek_; 243 244 // If already doing a browser seek when a regular seek request arrives, 245 // these fields remember the regular seek so OnDemuxerSeekDone() can trigger 246 // it when the browser seek is done. These are only valid when 247 // |SEEK_EVENT_PENDING| is pending. 248 bool pending_seek_; 249 base::TimeDelta pending_seek_time_; 250 251 // Decoder jobs. 252 scoped_ptr<AudioDecoderJob, MediaDecoderJob::Deleter> audio_decoder_job_; 253 scoped_ptr<VideoDecoderJob, MediaDecoderJob::Deleter> video_decoder_job_; 254 255 bool reconfig_audio_decoder_; 256 bool reconfig_video_decoder_; 257 258 // Track the most recent preroll target. Decoder re-creation needs this to 259 // resume any in-progress preroll. 260 base::TimeDelta preroll_timestamp_; 261 262 // A cancelable task that is posted when the audio decoder starts requesting 263 // new data. This callback runs if no data arrives before the timeout period 264 // elapses. 265 base::CancelableClosure decoder_starvation_callback_; 266 267 // Object to calculate the current audio timestamp for A/V sync. 268 scoped_ptr<AudioTimestampHelper> audio_timestamp_helper_; 269 270 // Weak pointer passed to media decoder jobs for callbacks. 271 base::WeakPtrFactory<MediaSourcePlayer> weak_this_; 272 273 MediaDrmBridge* drm_bridge_; 274 275 // No decryption key available to decrypt the encrypted buffer. In this case, 276 // the player should pause. When a new key is added (OnKeyAdded()), we should 277 // try to start playback again. 278 bool is_waiting_for_key_; 279 280 // Test-only callback for hooking the completion of the next decode cycle. 281 base::Closure decode_callback_for_testing_; 282 283 friend class MediaSourcePlayerTest; 284 DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayer); 285 }; 286 287 } // namespace media 288 289 #endif // MEDIA_BASE_ANDROID_MEDIA_SOURCE_PLAYER_H_ 290