Home | History | Annotate | Download | only in include
      1 /*
      2  * Copyright (C) 2009 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 AWESOME_PLAYER_H_
     18 
     19 #define AWESOME_PLAYER_H_
     20 
     21 #include "HTTPBase.h"
     22 #include "TimedEventQueue.h"
     23 
     24 #include <media/MediaPlayerInterface.h>
     25 #include <media/stagefright/DataSource.h>
     26 #include <media/stagefright/OMXClient.h>
     27 #include <media/stagefright/TimeSource.h>
     28 #include <utils/threads.h>
     29 #include <drm/DrmManagerClient.h>
     30 
     31 namespace android {
     32 
     33 struct AudioPlayer;
     34 struct DataSource;
     35 struct MediaBuffer;
     36 struct MediaExtractor;
     37 struct MediaSource;
     38 struct NuCachedSource2;
     39 struct ISurfaceTexture;
     40 
     41 struct ALooper;
     42 struct ARTSPController;
     43 
     44 class DrmManagerClinet;
     45 class DecryptHandle;
     46 
     47 class TimedTextPlayer;
     48 struct WVMExtractor;
     49 
     50 struct AwesomeRenderer : public RefBase {
     51     AwesomeRenderer() {}
     52 
     53     virtual void render(MediaBuffer *buffer) = 0;
     54 
     55 private:
     56     AwesomeRenderer(const AwesomeRenderer &);
     57     AwesomeRenderer &operator=(const AwesomeRenderer &);
     58 };
     59 
     60 struct AwesomePlayer {
     61     AwesomePlayer();
     62     ~AwesomePlayer();
     63 
     64     void setListener(const wp<MediaPlayerBase> &listener);
     65     void setUID(uid_t uid);
     66 
     67     status_t setDataSource(
     68             const char *uri,
     69             const KeyedVector<String8, String8> *headers = NULL);
     70 
     71     status_t setDataSource(int fd, int64_t offset, int64_t length);
     72 
     73     status_t setDataSource(const sp<IStreamSource> &source);
     74 
     75     void reset();
     76 
     77     status_t prepare();
     78     status_t prepare_l();
     79     status_t prepareAsync();
     80     status_t prepareAsync_l();
     81 
     82     status_t play();
     83     status_t pause();
     84 
     85     bool isPlaying() const;
     86 
     87     status_t setSurface(const sp<Surface> &surface);
     88     status_t setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
     89     void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
     90     status_t setLooping(bool shouldLoop);
     91 
     92     status_t getDuration(int64_t *durationUs);
     93     status_t getPosition(int64_t *positionUs);
     94 
     95     status_t setParameter(int key, const Parcel &request);
     96     status_t getParameter(int key, Parcel *reply);
     97     status_t setCacheStatCollectFreq(const Parcel &request);
     98 
     99     status_t seekTo(int64_t timeUs);
    100 
    101     // This is a mask of MediaExtractor::Flags.
    102     uint32_t flags() const;
    103 
    104     void postAudioEOS(int64_t delayUs = 0ll);
    105     void postAudioSeekComplete();
    106 
    107     status_t setTimedTextTrackIndex(int32_t index);
    108 
    109     status_t dump(int fd, const Vector<String16> &args) const;
    110 
    111 private:
    112     friend struct AwesomeEvent;
    113     friend struct PreviewPlayer;
    114 
    115     enum {
    116         PLAYING             = 0x01,
    117         LOOPING             = 0x02,
    118         FIRST_FRAME         = 0x04,
    119         PREPARING           = 0x08,
    120         PREPARED            = 0x10,
    121         AT_EOS              = 0x20,
    122         PREPARE_CANCELLED   = 0x40,
    123         CACHE_UNDERRUN      = 0x80,
    124         AUDIO_AT_EOS        = 0x0100,
    125         VIDEO_AT_EOS        = 0x0200,
    126         AUTO_LOOPING        = 0x0400,
    127 
    128         // We are basically done preparing but are currently buffering
    129         // sufficient data to begin playback and finish the preparation phase
    130         // for good.
    131         PREPARING_CONNECTED = 0x0800,
    132 
    133         // We're triggering a single video event to display the first frame
    134         // after the seekpoint.
    135         SEEK_PREVIEW        = 0x1000,
    136 
    137         AUDIO_RUNNING       = 0x2000,
    138         AUDIOPLAYER_STARTED = 0x4000,
    139 
    140         INCOGNITO           = 0x8000,
    141 
    142         TEXT_RUNNING        = 0x10000,
    143         TEXTPLAYER_STARTED  = 0x20000,
    144 
    145         SLOW_DECODER_HACK   = 0x40000,
    146     };
    147 
    148     mutable Mutex mLock;
    149     Mutex mMiscStateLock;
    150     mutable Mutex mStatsLock;
    151     Mutex mAudioLock;
    152 
    153     OMXClient mClient;
    154     TimedEventQueue mQueue;
    155     bool mQueueStarted;
    156     wp<MediaPlayerBase> mListener;
    157     bool mUIDValid;
    158     uid_t mUID;
    159 
    160     sp<Surface> mSurface;
    161     sp<ANativeWindow> mNativeWindow;
    162     sp<MediaPlayerBase::AudioSink> mAudioSink;
    163 
    164     SystemTimeSource mSystemTimeSource;
    165     TimeSource *mTimeSource;
    166 
    167     String8 mUri;
    168     KeyedVector<String8, String8> mUriHeaders;
    169 
    170     sp<DataSource> mFileSource;
    171 
    172     sp<MediaSource> mVideoTrack;
    173     sp<MediaSource> mVideoSource;
    174     sp<AwesomeRenderer> mVideoRenderer;
    175     bool mVideoRendererIsPreview;
    176 
    177     sp<MediaSource> mAudioTrack;
    178     sp<MediaSource> mAudioSource;
    179     AudioPlayer *mAudioPlayer;
    180     int64_t mDurationUs;
    181 
    182     int32_t mDisplayWidth;
    183     int32_t mDisplayHeight;
    184 
    185     uint32_t mFlags;
    186     uint32_t mExtractorFlags;
    187     uint32_t mSinceLastDropped;
    188 
    189     int64_t mTimeSourceDeltaUs;
    190     int64_t mVideoTimeUs;
    191 
    192     enum SeekType {
    193         NO_SEEK,
    194         SEEK,
    195         SEEK_VIDEO_ONLY
    196     };
    197     SeekType mSeeking;
    198 
    199     bool mSeekNotificationSent;
    200     int64_t mSeekTimeUs;
    201 
    202     int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
    203 
    204     bool mWatchForAudioSeekComplete;
    205     bool mWatchForAudioEOS;
    206 
    207     sp<TimedEventQueue::Event> mVideoEvent;
    208     bool mVideoEventPending;
    209     sp<TimedEventQueue::Event> mStreamDoneEvent;
    210     bool mStreamDoneEventPending;
    211     sp<TimedEventQueue::Event> mBufferingEvent;
    212     bool mBufferingEventPending;
    213     sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
    214     bool mAudioStatusEventPending;
    215     sp<TimedEventQueue::Event> mVideoLagEvent;
    216     bool mVideoLagEventPending;
    217 
    218     sp<TimedEventQueue::Event> mAsyncPrepareEvent;
    219     Condition mPreparedCondition;
    220     bool mIsAsyncPrepare;
    221     status_t mPrepareResult;
    222     status_t mStreamDoneStatus;
    223 
    224     void postVideoEvent_l(int64_t delayUs = -1);
    225     void postBufferingEvent_l();
    226     void postStreamDoneEvent_l(status_t status);
    227     void postCheckAudioStatusEvent(int64_t delayUs);
    228     void postVideoLagEvent_l();
    229     status_t play_l();
    230 
    231     MediaBuffer *mVideoBuffer;
    232 
    233     sp<HTTPBase> mConnectingDataSource;
    234     sp<NuCachedSource2> mCachedSource;
    235 
    236     sp<ALooper> mLooper;
    237     sp<ARTSPController> mRTSPController;
    238     sp<ARTSPController> mConnectingRTSPController;
    239 
    240     DrmManagerClient *mDrmManagerClient;
    241     sp<DecryptHandle> mDecryptHandle;
    242 
    243     int64_t mLastVideoTimeUs;
    244     TimedTextPlayer *mTextPlayer;
    245     mutable Mutex mTimedTextLock;
    246 
    247     sp<WVMExtractor> mWVMExtractor;
    248 
    249     status_t setDataSource_l(
    250             const char *uri,
    251             const KeyedVector<String8, String8> *headers = NULL);
    252 
    253     status_t setDataSource_l(const sp<DataSource> &dataSource);
    254     status_t setDataSource_l(const sp<MediaExtractor> &extractor);
    255     void reset_l();
    256     status_t seekTo_l(int64_t timeUs);
    257     status_t pause_l(bool at_eos = false);
    258     void initRenderer_l();
    259     void notifyVideoSize_l();
    260     void seekAudioIfNecessary_l();
    261 
    262     void cancelPlayerEvents(bool keepBufferingGoing = false);
    263 
    264     void setAudioSource(sp<MediaSource> source);
    265     status_t initAudioDecoder();
    266 
    267     void setVideoSource(sp<MediaSource> source);
    268     status_t initVideoDecoder(uint32_t flags = 0);
    269 
    270     void addTextSource(sp<MediaSource> source);
    271 
    272     void onStreamDone();
    273 
    274     void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
    275 
    276     void onVideoEvent();
    277     void onBufferingUpdate();
    278     void onCheckAudioStatus();
    279     void onPrepareAsyncEvent();
    280     void abortPrepare(status_t err);
    281     void finishAsyncPrepare_l();
    282     void onVideoLagUpdate();
    283 
    284     bool getCachedDuration_l(int64_t *durationUs, bool *eos);
    285 
    286     status_t finishSetDataSource_l();
    287 
    288     static bool ContinuePreparation(void *cookie);
    289 
    290     static void OnRTSPSeekDoneWrapper(void *cookie);
    291     void onRTSPSeekDone();
    292 
    293     bool getBitrate(int64_t *bitrate);
    294 
    295     void finishSeekIfNecessary(int64_t videoTimeUs);
    296     void ensureCacheIsFetching_l();
    297 
    298     status_t startAudioPlayer_l(bool sendErrorNotification = true);
    299 
    300     void shutdownVideoDecoder_l();
    301     status_t setNativeWindow_l(const sp<ANativeWindow> &native);
    302 
    303     bool isStreamingHTTP() const;
    304     void sendCacheStats();
    305 
    306     enum FlagMode {
    307         SET,
    308         CLEAR,
    309         ASSIGN
    310     };
    311     void modifyFlags(unsigned value, FlagMode mode);
    312 
    313     struct TrackStat {
    314         String8 mMIME;
    315         String8 mDecoderName;
    316     };
    317 
    318     // protected by mStatsLock
    319     struct Stats {
    320         int mFd;
    321         String8 mURI;
    322         int64_t mBitrate;
    323         ssize_t mAudioTrackIndex;
    324         ssize_t mVideoTrackIndex;
    325         int64_t mNumVideoFramesDecoded;
    326         int64_t mNumVideoFramesDropped;
    327         int32_t mVideoWidth;
    328         int32_t mVideoHeight;
    329         uint32_t mFlags;
    330         Vector<TrackStat> mTracks;
    331     } mStats;
    332 
    333     AwesomePlayer(const AwesomePlayer &);
    334     AwesomePlayer &operator=(const AwesomePlayer &);
    335 };
    336 
    337 }  // namespace android
    338 
    339 #endif  // AWESOME_PLAYER_H_
    340 
    341