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 class DataSource;
     35 class IDataSource;
     36 struct IMediaHTTPService;
     37 struct MediaSource;
     38 class MediaBuffer;
     39 struct NuCachedSource2;
     40 class WVMExtractor;
     41 
     42 struct NuPlayer::GenericSource : public NuPlayer::Source {
     43     GenericSource(const sp<AMessage> &notify, bool uidValid, uid_t uid);
     44 
     45     status_t setDataSource(
     46             const sp<IMediaHTTPService> &httpService,
     47             const char *url,
     48             const KeyedVector<String8, String8> *headers);
     49 
     50     status_t setDataSource(int fd, int64_t offset, int64_t length);
     51 
     52     status_t setDataSource(const sp<DataSource>& dataSource);
     53 
     54     virtual void prepareAsync();
     55 
     56     virtual void start();
     57     virtual void stop();
     58     virtual void pause();
     59     virtual void resume();
     60 
     61     virtual void disconnect();
     62 
     63     virtual status_t feedMoreTSData();
     64 
     65     virtual sp<MetaData> getFileFormatMeta() const;
     66 
     67     virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
     68 
     69     virtual status_t getDuration(int64_t *durationUs);
     70     virtual size_t getTrackCount() const;
     71     virtual sp<AMessage> getTrackInfo(size_t trackIndex) const;
     72     virtual ssize_t getSelectedTrack(media_track_type type) const;
     73     virtual status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
     74     virtual status_t seekTo(int64_t seekTimeUs);
     75 
     76     virtual status_t setBuffers(bool audio, Vector<MediaBuffer *> &buffers);
     77 
     78     virtual bool isStreaming() const;
     79 
     80     virtual void setOffloadAudio(bool offload);
     81 
     82 protected:
     83     virtual ~GenericSource();
     84 
     85     virtual void onMessageReceived(const sp<AMessage> &msg);
     86 
     87     virtual sp<MetaData> getFormatMeta(bool audio);
     88 
     89 private:
     90     enum {
     91         kWhatPrepareAsync,
     92         kWhatFetchSubtitleData,
     93         kWhatFetchTimedTextData,
     94         kWhatSendSubtitleData,
     95         kWhatSendGlobalTimedTextData,
     96         kWhatSendTimedTextData,
     97         kWhatChangeAVSource,
     98         kWhatPollBuffering,
     99         kWhatGetFormat,
    100         kWhatGetSelectedTrack,
    101         kWhatSelectTrack,
    102         kWhatSeek,
    103         kWhatReadBuffer,
    104         kWhatStopWidevine,
    105         kWhatStart,
    106         kWhatResume,
    107         kWhatSecureDecodersInstantiated,
    108     };
    109 
    110     struct Track {
    111         size_t mIndex;
    112         sp<IMediaSource> mSource;
    113         sp<AnotherPacketSource> mPackets;
    114     };
    115 
    116     // Helper to monitor buffering status. The polling happens every second.
    117     // When necessary, it will send out buffering events to the player.
    118     struct BufferingMonitor : public AHandler {
    119     public:
    120         BufferingMonitor(const sp<AMessage> &notify);
    121 
    122         // Set up state.
    123         void prepare(const sp<NuCachedSource2> &cachedSource,
    124                 const sp<WVMExtractor> &wvmExtractor,
    125                 int64_t durationUs,
    126                 int64_t bitrate,
    127                 bool isStreaming);
    128         // Stop and reset buffering monitor.
    129         void stop();
    130         // Cancel the current monitor task.
    131         void cancelPollBuffering();
    132         // Restart the monitor task.
    133         void restartPollBuffering();
    134         // Stop buffering task and send out corresponding events.
    135         void stopBufferingIfNecessary();
    136         // Make sure data source is getting data.
    137         void ensureCacheIsFetching();
    138         // Update media time of just extracted buffer from data source.
    139         void updateQueuedTime(bool isAudio, int64_t timeUs);
    140 
    141         // Set the offload mode.
    142         void setOffloadAudio(bool offload);
    143         // Update media time of last dequeued buffer which is sent to the decoder.
    144         void updateDequeuedBufferTime(int64_t mediaUs);
    145 
    146     protected:
    147         virtual ~BufferingMonitor();
    148         virtual void onMessageReceived(const sp<AMessage> &msg);
    149 
    150     private:
    151         enum {
    152             kWhatPollBuffering,
    153         };
    154 
    155         sp<AMessage> mNotify;
    156 
    157         sp<NuCachedSource2> mCachedSource;
    158         sp<WVMExtractor> mWVMExtractor;
    159         int64_t mDurationUs;
    160         int64_t mBitrate;
    161         bool mIsStreaming;
    162 
    163         int64_t mAudioTimeUs;
    164         int64_t mVideoTimeUs;
    165         int32_t mPollBufferingGeneration;
    166         bool mPrepareBuffering;
    167         bool mBuffering;
    168         int32_t mPrevBufferPercentage;
    169 
    170         mutable Mutex mLock;
    171 
    172         bool mOffloadAudio;
    173         int64_t mFirstDequeuedBufferRealUs;
    174         int64_t mFirstDequeuedBufferMediaUs;
    175         int64_t mlastDequeuedBufferMediaUs;
    176 
    177         void prepare_l(const sp<NuCachedSource2> &cachedSource,
    178                 const sp<WVMExtractor> &wvmExtractor,
    179                 int64_t durationUs,
    180                 int64_t bitrate,
    181                 bool isStreaming);
    182         void cancelPollBuffering_l();
    183         void notifyBufferingUpdate_l(int32_t percentage);
    184         void startBufferingIfNecessary_l();
    185         void stopBufferingIfNecessary_l();
    186         void sendCacheStats_l();
    187         void ensureCacheIsFetching_l();
    188         int64_t getLastReadPosition_l();
    189         void onPollBuffering_l();
    190         void schedulePollBuffering_l();
    191     };
    192 
    193     Vector<sp<IMediaSource> > mSources;
    194     Track mAudioTrack;
    195     int64_t mAudioTimeUs;
    196     int64_t mAudioLastDequeueTimeUs;
    197     Track mVideoTrack;
    198     int64_t mVideoTimeUs;
    199     int64_t mVideoLastDequeueTimeUs;
    200     Track mSubtitleTrack;
    201     Track mTimedTextTrack;
    202 
    203     int32_t mFetchSubtitleDataGeneration;
    204     int32_t mFetchTimedTextDataGeneration;
    205     int64_t mDurationUs;
    206     bool mAudioIsVorbis;
    207     bool mIsWidevine;
    208     bool mIsSecure;
    209     bool mIsStreaming;
    210     bool mUIDValid;
    211     uid_t mUID;
    212     sp<IMediaHTTPService> mHTTPService;
    213     AString mUri;
    214     KeyedVector<String8, String8> mUriHeaders;
    215     int mFd;
    216     int64_t mOffset;
    217     int64_t mLength;
    218 
    219     sp<DataSource> mDataSource;
    220     sp<NuCachedSource2> mCachedSource;
    221     sp<DataSource> mHttpSource;
    222     sp<WVMExtractor> mWVMExtractor;
    223     sp<MetaData> mFileMeta;
    224     DrmManagerClient *mDrmManagerClient;
    225     sp<DecryptHandle> mDecryptHandle;
    226     bool mStarted;
    227     bool mStopRead;
    228     int64_t mBitrate;
    229     sp<BufferingMonitor> mBufferingMonitor;
    230     uint32_t mPendingReadBufferTypes;
    231     sp<ABuffer> mGlobalTimedText;
    232 
    233     mutable Mutex mReadBufferLock;
    234     mutable Mutex mDisconnectLock;
    235 
    236     sp<ALooper> mLooper;
    237     sp<ALooper> mBufferingMonitorLooper;
    238 
    239     void resetDataSource();
    240 
    241     status_t initFromDataSource();
    242     void checkDrmStatus(const sp<DataSource>& dataSource);
    243     int64_t getLastReadPosition();
    244     void setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position);
    245 
    246     void notifyPreparedAndCleanup(status_t err);
    247     void onSecureDecodersInstantiated(status_t err);
    248     void finishPrepareAsync();
    249     status_t startSources();
    250 
    251     void onGetFormatMeta(sp<AMessage> msg) const;
    252     sp<MetaData> doGetFormatMeta(bool audio) const;
    253 
    254     void onGetSelectedTrack(sp<AMessage> msg) const;
    255     ssize_t doGetSelectedTrack(media_track_type type) const;
    256 
    257     void onSelectTrack(sp<AMessage> msg);
    258     status_t doSelectTrack(size_t trackIndex, bool select, int64_t timeUs);
    259 
    260     void onSeek(sp<AMessage> msg);
    261     status_t doSeek(int64_t seekTimeUs);
    262 
    263     void onPrepareAsync();
    264 
    265     void fetchTextData(
    266             uint32_t what, media_track_type type,
    267             int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
    268 
    269     void sendGlobalTextData(
    270             uint32_t what,
    271             int32_t curGen, sp<AMessage> msg);
    272 
    273     void sendTextData(
    274             uint32_t what, media_track_type type,
    275             int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
    276 
    277     sp<ABuffer> mediaBufferToABuffer(
    278             MediaBuffer *mbuf,
    279             media_track_type trackType,
    280             int64_t seekTimeUs,
    281             int64_t *actualTimeUs = NULL);
    282 
    283     void postReadBuffer(media_track_type trackType);
    284     void onReadBuffer(sp<AMessage> msg);
    285     void readBuffer(
    286             media_track_type trackType,
    287             int64_t seekTimeUs = -1ll, int64_t *actualTimeUs = NULL, bool formatChange = false);
    288 
    289     void queueDiscontinuityIfNeeded(
    290             bool seeking, bool formatChange, media_track_type trackType, Track *track);
    291 
    292     DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
    293 };
    294 
    295 }  // namespace android
    296 
    297 #endif  // GENERIC_SOURCE_H_
    298