Home | History | Annotate | Download | only in nuplayer2
      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 MediaHTTPService;
     41 struct MediaSource;
     42 class MediaBuffer;
     43 struct MediaClock;
     44 struct NuCachedSource2;
     45 
     46 struct NuPlayer2::GenericSource2 : public NuPlayer2::Source,
     47                                    public MediaBufferObserver // Modular DRM
     48 {
     49     GenericSource2(const sp<AMessage> &notify, uid_t uid,
     50                    const sp<MediaClock> &mediaClock);
     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 
     65     virtual void prepareAsync();
     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 
     89     virtual bool isStreaming() const;
     90 
     91     // Modular DRM
     92     virtual void signalBufferReturned(MediaBufferBase *buffer);
     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:
    103     virtual ~GenericSource2();
    104 
    105     virtual void onMessageReceived(const sp<AMessage> &msg);
    106 
    107     virtual sp<AMessage> getFormat(bool audio);
    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;
    129         sp<AMediaExtractorWrapper> mExtractor;
    130         sp<AnotherPacketSource> mPackets;
    131     };
    132 
    133     int64_t mAudioTimeUs;
    134     int64_t mAudioLastDequeueTimeUs;
    135     int64_t mVideoTimeUs;
    136     int64_t mVideoLastDequeueTimeUs;
    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;
    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;
    166     sp<AMediaDataSourceWrapper> mDataSourceWrapper;
    167     sp<AMediaExtractorWrapper> mExtractor;
    168     Vector<sp<AMediaExtractorWrapper> > mExtractors;
    169     bool mStarted;
    170     bool mPreparing;
    171     int64_t mBitrate;
    172     uint32_t mPendingReadBufferTypes;
    173     sp<ABuffer> mGlobalTimedText;
    174 
    175     Track mVideoTrack;
    176     Track mAudioTrack;
    177     Track mSubtitleTrack;
    178     Track mTimedTextTrack;
    179 
    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 
    197     void onPrepareAsync();
    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(
    212             MediaBufferBase *mbuf,
    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 
    237     sp<AMessage> getFormat_l(bool audio);
    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 
    250     DISALLOW_EVIL_CONSTRUCTORS(GenericSource2);
    251 };
    252 
    253 }  // namespace android
    254 
    255 #endif  // GENERIC_SOURCE2_H_
    256