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