Home | History | Annotate | Download | only in nuplayer
      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 
     28 namespace android {
     29 
     30 class DecryptHandle;
     31 class DrmManagerClient;
     32 struct AnotherPacketSource;
     33 struct ARTSPController;
     34 struct DataSource;
     35 struct IMediaHTTPService;
     36 struct MediaSource;
     37 class MediaBuffer;
     38 struct NuCachedSource2;
     39 struct WVMExtractor;
     40 
     41 struct NuPlayer::GenericSource : public NuPlayer::Source {
     42     GenericSource(const sp<AMessage> &notify, bool uidValid, uid_t uid);
     43 
     44     status_t setDataSource(
     45             const sp<IMediaHTTPService> &httpService,
     46             const char *url,
     47             const KeyedVector<String8, String8> *headers);
     48 
     49     status_t setDataSource(int fd, int64_t offset, int64_t length);
     50 
     51     virtual void prepareAsync();
     52 
     53     virtual void start();
     54     virtual void stop();
     55     virtual void pause();
     56     virtual void resume();
     57 
     58     virtual void disconnect();
     59 
     60     virtual status_t feedMoreTSData();
     61 
     62     virtual sp<MetaData> getFileFormatMeta() const;
     63 
     64     virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
     65 
     66     virtual status_t getDuration(int64_t *durationUs);
     67     virtual size_t getTrackCount() const;
     68     virtual sp<AMessage> getTrackInfo(size_t trackIndex) const;
     69     virtual ssize_t getSelectedTrack(media_track_type type) const;
     70     virtual status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
     71     virtual status_t seekTo(int64_t seekTimeUs);
     72 
     73     virtual status_t setBuffers(bool audio, Vector<MediaBuffer *> &buffers);
     74 
     75 protected:
     76     virtual ~GenericSource();
     77 
     78     virtual void onMessageReceived(const sp<AMessage> &msg);
     79 
     80     virtual sp<MetaData> getFormatMeta(bool audio);
     81 
     82 private:
     83     enum {
     84         kWhatPrepareAsync,
     85         kWhatFetchSubtitleData,
     86         kWhatFetchTimedTextData,
     87         kWhatSendSubtitleData,
     88         kWhatSendTimedTextData,
     89         kWhatChangeAVSource,
     90         kWhatPollBuffering,
     91         kWhatGetFormat,
     92         kWhatGetSelectedTrack,
     93         kWhatSelectTrack,
     94         kWhatSeek,
     95         kWhatReadBuffer,
     96         kWhatStopWidevine,
     97         kWhatStart,
     98         kWhatResume,
     99         kWhatSecureDecodersInstantiated,
    100     };
    101 
    102     struct Track {
    103         size_t mIndex;
    104         sp<MediaSource> mSource;
    105         sp<AnotherPacketSource> mPackets;
    106     };
    107 
    108     Vector<sp<MediaSource> > mSources;
    109     Track mAudioTrack;
    110     int64_t mAudioTimeUs;
    111     int64_t mAudioLastDequeueTimeUs;
    112     Track mVideoTrack;
    113     int64_t mVideoTimeUs;
    114     int64_t mVideoLastDequeueTimeUs;
    115     Track mSubtitleTrack;
    116     Track mTimedTextTrack;
    117 
    118     int32_t mFetchSubtitleDataGeneration;
    119     int32_t mFetchTimedTextDataGeneration;
    120     int64_t mDurationUs;
    121     bool mAudioIsVorbis;
    122     bool mIsWidevine;
    123     bool mIsSecure;
    124     bool mIsStreaming;
    125     bool mUIDValid;
    126     uid_t mUID;
    127     sp<IMediaHTTPService> mHTTPService;
    128     AString mUri;
    129     KeyedVector<String8, String8> mUriHeaders;
    130     int mFd;
    131     int64_t mOffset;
    132     int64_t mLength;
    133 
    134     sp<DataSource> mDataSource;
    135     sp<NuCachedSource2> mCachedSource;
    136     sp<DataSource> mHttpSource;
    137     sp<WVMExtractor> mWVMExtractor;
    138     sp<MetaData> mFileMeta;
    139     DrmManagerClient *mDrmManagerClient;
    140     sp<DecryptHandle> mDecryptHandle;
    141     bool mStarted;
    142     bool mStopRead;
    143     String8 mContentType;
    144     AString mSniffedMIME;
    145     off64_t mMetaDataSize;
    146     int64_t mBitrate;
    147     int32_t mPollBufferingGeneration;
    148     uint32_t mPendingReadBufferTypes;
    149     bool mBuffering;
    150     bool mPrepareBuffering;
    151     mutable Mutex mReadBufferLock;
    152 
    153     sp<ALooper> mLooper;
    154 
    155     void resetDataSource();
    156 
    157     status_t initFromDataSource();
    158     void checkDrmStatus(const sp<DataSource>& dataSource);
    159     int64_t getLastReadPosition();
    160     void setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position);
    161 
    162     status_t prefillCacheIfNecessary();
    163 
    164     void notifyPreparedAndCleanup(status_t err);
    165     void onSecureDecodersInstantiated(status_t err);
    166     void finishPrepareAsync();
    167     status_t startSources();
    168 
    169     void onGetFormatMeta(sp<AMessage> msg) const;
    170     sp<MetaData> doGetFormatMeta(bool audio) const;
    171 
    172     void onGetSelectedTrack(sp<AMessage> msg) const;
    173     ssize_t doGetSelectedTrack(media_track_type type) const;
    174 
    175     void onSelectTrack(sp<AMessage> msg);
    176     status_t doSelectTrack(size_t trackIndex, bool select, int64_t timeUs);
    177 
    178     void onSeek(sp<AMessage> msg);
    179     status_t doSeek(int64_t seekTimeUs);
    180 
    181     void onPrepareAsync();
    182 
    183     void fetchTextData(
    184             uint32_t what, media_track_type type,
    185             int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
    186 
    187     void sendTextData(
    188             uint32_t what, media_track_type type,
    189             int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
    190 
    191     sp<ABuffer> mediaBufferToABuffer(
    192             MediaBuffer *mbuf,
    193             media_track_type trackType,
    194             int64_t seekTimeUs,
    195             int64_t *actualTimeUs = NULL);
    196 
    197     void postReadBuffer(media_track_type trackType);
    198     void onReadBuffer(sp<AMessage> msg);
    199     void readBuffer(
    200             media_track_type trackType,
    201             int64_t seekTimeUs = -1ll, int64_t *actualTimeUs = NULL, bool formatChange = false);
    202 
    203     void schedulePollBuffering();
    204     void cancelPollBuffering();
    205     void restartPollBuffering();
    206     void onPollBuffering();
    207     void notifyBufferingUpdate(int percentage);
    208     void startBufferingIfNecessary();
    209     void stopBufferingIfNecessary();
    210     void sendCacheStats();
    211     void ensureCacheIsFetching();
    212 
    213     DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
    214 };
    215 
    216 }  // namespace android
    217 
    218 #endif  // GENERIC_SOURCE_H_
    219