1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef GENERIC_SOURCE2_H_ 18 19 #define GENERIC_SOURCE2_H_ 20 21 #include "NuPlayer2.h" 22 #include "NuPlayer2Source.h" 23 24 #include "ATSParser.h" 25 26 #include <media/stagefright/MediaBuffer.h> 27 #include <mediaplayer2/mediaplayer2.h> 28 #include <media/NdkMediaDataSource.h> 29 #include <media/NdkMediaExtractor.h> 30 #include <media/NdkWrapper.h> 31 32 namespace android { 33 34 class DecryptHandle; 35 struct AnotherPacketSource; 36 struct ARTSPController; 37 class DataSource; 38 class IDataSource; 39 class IMediaSource; 40 struct MediaSource; 41 class MediaBuffer; 42 struct MediaClock; 43 44 struct NuPlayer2::GenericSource2 : public NuPlayer2::Source, 45 public MediaBufferObserver // Modular DRM 46 { 47 GenericSource2(const sp<AMessage> ¬ify, uid_t uid, 48 const sp<MediaClock> &mediaClock); 49 50 status_t setDataSource( 51 const char *url, 52 const KeyedVector<String8, String8> *headers); 53 54 status_t setDataSource(int fd, int64_t offset, int64_t length); 55 56 status_t setDataSource(const sp<DataSource>& dataSource); 57 58 virtual status_t getBufferingSettings( 59 BufferingSettings* buffering /* nonnull */) override; 60 virtual status_t setBufferingSettings(const BufferingSettings& buffering) override; 61 62 virtual void prepareAsync(int64_t startTimeUs); 63 64 virtual void start(); 65 virtual void stop(); 66 virtual void pause(); 67 virtual void resume(); 68 69 virtual void disconnect(); 70 71 virtual status_t feedMoreTSData(); 72 73 virtual sp<MetaData> getFileFormatMeta() const; 74 75 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit); 76 77 virtual status_t getDuration(int64_t *durationUs); 78 virtual size_t getTrackCount() const; 79 virtual sp<AMessage> getTrackInfo(size_t trackIndex) const; 80 virtual ssize_t getSelectedTrack(media_track_type type) const; 81 virtual status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs); 82 virtual status_t seekTo( 83 int64_t seekTimeUs, 84 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) override; 85 86 virtual bool isStreaming() const; 87 88 // Modular DRM 89 virtual void signalBufferReturned(MediaBufferBase *buffer); 90 91 virtual status_t prepareDrm( 92 const uint8_t uuid[16], 93 const Vector<uint8_t> &drmSessionId, 94 sp<AMediaCryptoWrapper> *outCrypto); 95 96 virtual status_t releaseDrm(); 97 98 99 protected: 100 virtual ~GenericSource2(); 101 102 virtual void onMessageReceived(const sp<AMessage> &msg); 103 104 virtual sp<AMessage> getFormat(bool audio); 105 virtual sp<MetaData> getFormatMeta(bool audio); 106 107 private: 108 enum { 109 kWhatPrepareAsync, 110 kWhatFetchSubtitleData, 111 kWhatFetchTimedTextData, 112 kWhatSendSubtitleData, 113 kWhatSendGlobalTimedTextData, 114 kWhatSendTimedTextData, 115 kWhatChangeAVSource, 116 kWhatPollBuffering, 117 kWhatSeek, 118 kWhatReadBuffer, 119 kWhatStart, 120 kWhatResume, 121 kWhatSecureDecodersInstantiated, 122 }; 123 124 struct Track { 125 size_t mIndex; 126 sp<AMediaExtractorWrapper> mExtractor; 127 sp<AnotherPacketSource> mPackets; 128 }; 129 130 int64_t mAudioTimeUs; 131 int64_t mAudioLastDequeueTimeUs; 132 int64_t mVideoTimeUs; 133 int64_t mVideoLastDequeueTimeUs; 134 135 BufferingSettings mBufferingSettings; 136 int32_t mPrevBufferPercentage; 137 int32_t mPollBufferingGeneration; 138 bool mSentPauseOnBuffering; 139 140 int32_t mAudioDataGeneration; 141 int32_t mVideoDataGeneration; 142 int32_t mFetchSubtitleDataGeneration; 143 int32_t mFetchTimedTextDataGeneration; 144 int64_t mDurationUs; 145 bool mAudioIsVorbis; 146 // Secure codec is required. 147 bool mIsSecure; 148 bool mIsStreaming; 149 uid_t mUID; 150 const sp<MediaClock> mMediaClock; 151 AString mUri; 152 KeyedVector<String8, String8> mUriHeaders; 153 int mFd; 154 int64_t mOffset; 155 int64_t mLength; 156 157 bool mDisconnected; 158 sp<MetaData> mFileMeta; 159 sp<AMediaDataSourceWrapper> mDataSourceWrapper; 160 sp<AMediaExtractorWrapper> mExtractor; 161 Vector<sp<AMediaExtractorWrapper> > mExtractors; 162 bool mStarted; 163 bool mPreparing; 164 int64_t mBitrate; 165 uint32_t mPendingReadBufferTypes; 166 sp<ABuffer> mGlobalTimedText; 167 168 Track mVideoTrack; 169 Track mAudioTrack; 170 Track mSubtitleTrack; 171 Track mTimedTextTrack; 172 173 mutable Mutex mLock; 174 175 sp<ALooper> mLooper; 176 177 void resetDataSource(); 178 179 status_t initFromDataSource(); 180 int64_t getLastReadPosition(); 181 182 void notifyPreparedAndCleanup(status_t err); 183 void onSecureDecodersInstantiated(status_t err); 184 void finishPrepareAsync(); 185 status_t startSources(); 186 187 void onSeek(const sp<AMessage>& msg); 188 status_t doSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode); 189 190 void onPrepareAsync(int64_t startTimeUs); 191 192 void fetchTextData( 193 uint32_t what, media_track_type type, 194 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg); 195 196 void sendGlobalTextData( 197 uint32_t what, 198 int32_t curGen, sp<AMessage> msg); 199 200 void sendTextData( 201 uint32_t what, media_track_type type, 202 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg); 203 204 sp<ABuffer> mediaBufferToABuffer( 205 MediaBufferBase *mbuf, 206 media_track_type trackType); 207 208 void postReadBuffer(media_track_type trackType); 209 void onReadBuffer(const sp<AMessage>& msg); 210 // When |mode| is MediaPlayer2SeekMode::SEEK_CLOSEST, the buffer read shall 211 // include an item indicating skipping rendering all buffers with timestamp 212 // earlier than |seekTimeUs|. 213 // For other modes, the buffer read will not include the item as above in order 214 // to facilitate fast seek operation. 215 void readBuffer( 216 media_track_type trackType, 217 int64_t seekTimeUs = -1ll, 218 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC, 219 int64_t *actualTimeUs = NULL, bool formatChange = false); 220 221 void queueDiscontinuityIfNeeded( 222 bool seeking, bool formatChange, media_track_type trackType, Track *track); 223 224 void schedulePollBuffering(); 225 void onPollBuffering(); 226 void notifyBufferingUpdate(int32_t percentage); 227 228 sp<AMessage> getFormat_l(bool audio); 229 sp<MetaData> getFormatMeta_l(bool audio); 230 int32_t getDataGeneration(media_track_type type) const; 231 232 // Modular DRM 233 // The source is DRM protected and is prepared for DRM. 234 bool mIsDrmProtected; 235 // releaseDrm has been processed. 236 bool mIsDrmReleased; 237 Vector<String8> mMimes; 238 239 status_t checkDrmInfo(); 240 241 DISALLOW_EVIL_CONSTRUCTORS(GenericSource2); 242 }; 243 244 } // namespace android 245 246 #endif // GENERIC_SOURCE2_H_ 247