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