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