1 /* 2 * Copyright (C) 2012 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_SOURCE_H_ 18 19 #define GENERIC_SOURCE_H_ 20 21 #include "NuPlayer.h" 22 #include "NuPlayerSource.h" 23 24 #include "ATSParser.h" 25 26 #include <media/mediaplayer.h> 27 #include <media/stagefright/MediaBuffer.h> 28 29 namespace android { 30 31 class DecryptHandle; 32 struct AnotherPacketSource; 33 struct ARTSPController; 34 class DataSource; 35 class IDataSource; 36 struct IMediaHTTPService; 37 struct MediaSource; 38 class IMediaSource; 39 class MediaBuffer; 40 struct MediaClock; 41 struct NuCachedSource2; 42 43 struct NuPlayer::GenericSource : public NuPlayer::Source, 44 public MediaBufferObserver // Modular DRM 45 { 46 GenericSource(const sp<AMessage> ¬ify, bool uidValid, uid_t uid, 47 const sp<MediaClock> &mediaClock); 48 49 status_t setDataSource( 50 const sp<IMediaHTTPService> &httpService, 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(); 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 MediaPlayerSeekMode mode = MediaPlayerSeekMode::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], const Vector<uint8_t> &drmSessionId, sp<ICrypto> *outCrypto); 93 94 virtual status_t releaseDrm(); 95 96 97 protected: 98 virtual ~GenericSource(); 99 100 virtual void onMessageReceived(const sp<AMessage> &msg); 101 102 virtual sp<MetaData> getFormatMeta(bool audio); 103 104 private: 105 enum { 106 kWhatPrepareAsync, 107 kWhatFetchSubtitleData, 108 kWhatFetchTimedTextData, 109 kWhatSendSubtitleData, 110 kWhatSendGlobalTimedTextData, 111 kWhatSendTimedTextData, 112 kWhatChangeAVSource, 113 kWhatPollBuffering, 114 kWhatSeek, 115 kWhatReadBuffer, 116 kWhatStart, 117 kWhatResume, 118 kWhatSecureDecodersInstantiated, 119 }; 120 121 struct Track { 122 size_t mIndex; 123 sp<IMediaSource> mSource; 124 sp<AnotherPacketSource> mPackets; 125 }; 126 127 Vector<sp<IMediaSource> > mSources; 128 Track mAudioTrack; 129 int64_t mAudioTimeUs; 130 int64_t mAudioLastDequeueTimeUs; 131 Track mVideoTrack; 132 int64_t mVideoTimeUs; 133 int64_t mVideoLastDequeueTimeUs; 134 Track mSubtitleTrack; 135 Track mTimedTextTrack; 136 137 BufferingSettings mBufferingSettings; 138 int32_t mPrevBufferPercentage; 139 int32_t mPollBufferingGeneration; 140 bool mSentPauseOnBuffering; 141 142 int32_t mAudioDataGeneration; 143 int32_t mVideoDataGeneration; 144 int32_t mFetchSubtitleDataGeneration; 145 int32_t mFetchTimedTextDataGeneration; 146 int64_t mDurationUs; 147 bool mAudioIsVorbis; 148 // Secure codec is required. 149 bool mIsSecure; 150 bool mIsStreaming; 151 bool mUIDValid; 152 uid_t mUID; 153 const sp<MediaClock> mMediaClock; 154 sp<IMediaHTTPService> 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; 166 bool mStarted; 167 bool mPreparing; 168 int64_t mBitrate; 169 uint32_t mPendingReadBufferTypes; 170 sp<ABuffer> mGlobalTimedText; 171 172 mutable Mutex mLock; 173 174 sp<ALooper> mLooper; 175 176 void resetDataSource(); 177 178 status_t initFromDataSource(); 179 int64_t getLastReadPosition(); 180 181 void notifyPreparedAndCleanup(status_t err); 182 void onSecureDecodersInstantiated(status_t err); 183 void finishPrepareAsync(); 184 status_t startSources(); 185 186 void onSeek(const sp<AMessage>& msg); 187 status_t doSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode); 188 189 void onPrepareAsync(); 190 191 void fetchTextData( 192 uint32_t what, media_track_type type, 193 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg); 194 195 void sendGlobalTextData( 196 uint32_t what, 197 int32_t curGen, sp<AMessage> msg); 198 199 void sendTextData( 200 uint32_t what, media_track_type type, 201 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg); 202 203 sp<ABuffer> mediaBufferToABuffer( 204 MediaBufferBase *mbuf, 205 media_track_type trackType); 206 207 void postReadBuffer(media_track_type trackType); 208 void onReadBuffer(const sp<AMessage>& msg); 209 // When |mode| is MediaPlayerSeekMode::SEEK_CLOSEST, the buffer read shall 210 // include an item indicating skipping rendering all buffers with timestamp 211 // earlier than |seekTimeUs|. 212 // For other modes, the buffer read will not include the item as above in order 213 // to facilitate fast seek operation. 214 void readBuffer( 215 media_track_type trackType, 216 int64_t seekTimeUs = -1ll, 217 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC, 218 int64_t *actualTimeUs = NULL, bool formatChange = false); 219 220 void queueDiscontinuityIfNeeded( 221 bool seeking, bool formatChange, media_track_type trackType, Track *track); 222 223 void schedulePollBuffering(); 224 void onPollBuffering(); 225 void notifyBufferingUpdate(int32_t percentage); 226 227 void sendCacheStats(); 228 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(GenericSource); 242 }; 243 244 } // namespace android 245 246 #endif // GENERIC_SOURCE_H_ 247