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