GenericSource2.h 7.2 KB

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