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 "NuHTTPDataSource.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 
     30 namespace android {
     31 
     32 struct AudioPlayer;
     33 struct DataSource;
     34 struct MediaBuffer;
     35 struct MediaExtractor;
     36 struct MediaSource;
     37 struct NuCachedSource2;
     38 
     39 struct ALooper;
     40 struct ARTSPController;
     41 struct ARTPSession;
     42 struct UDPPusher;
     43 
     44 struct AwesomeRenderer : public RefBase {
     45     AwesomeRenderer() {}
     46 
     47     virtual status_t initCheck() const = 0;
     48     virtual void render(MediaBuffer *buffer) = 0;
     49 
     50 private:
     51     AwesomeRenderer(const AwesomeRenderer &);
     52     AwesomeRenderer &operator=(const AwesomeRenderer &);
     53 };
     54 
     55 struct AwesomePlayer {
     56     AwesomePlayer();
     57     ~AwesomePlayer();
     58 
     59     void setListener(const wp<MediaPlayerBase> &listener);
     60 
     61     status_t setDataSource(
     62             const char *uri,
     63             const KeyedVector<String8, String8> *headers = NULL);
     64 
     65     status_t setDataSource(int fd, int64_t offset, int64_t length);
     66 
     67     void reset();
     68 
     69     status_t prepare();
     70     status_t prepare_l();
     71     status_t prepareAsync();
     72     status_t prepareAsync_l();
     73 
     74     status_t play();
     75     status_t pause();
     76 
     77     bool isPlaying() const;
     78 
     79     void setISurface(const sp<ISurface> &isurface);
     80     void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
     81     status_t setLooping(bool shouldLoop);
     82 
     83     status_t getDuration(int64_t *durationUs);
     84     status_t getPosition(int64_t *positionUs);
     85 
     86     status_t seekTo(int64_t timeUs);
     87 
     88     status_t getVideoDimensions(int32_t *width, int32_t *height) const;
     89 
     90     status_t suspend();
     91     status_t resume();
     92 
     93     // This is a mask of MediaExtractor::Flags.
     94     uint32_t flags() const;
     95 
     96     void postAudioEOS();
     97     void postAudioSeekComplete();
     98 
     99 private:
    100     friend struct AwesomeEvent;
    101 
    102     enum {
    103         PLAYING             = 1,
    104         LOOPING             = 2,
    105         FIRST_FRAME         = 4,
    106         PREPARING           = 8,
    107         PREPARED            = 16,
    108         AT_EOS              = 32,
    109         PREPARE_CANCELLED   = 64,
    110         CACHE_UNDERRUN      = 128,
    111         AUDIO_AT_EOS        = 256,
    112         VIDEO_AT_EOS        = 512,
    113         AUTO_LOOPING        = 1024,
    114     };
    115 
    116     mutable Mutex mLock;
    117     Mutex mMiscStateLock;
    118 
    119     OMXClient mClient;
    120     TimedEventQueue mQueue;
    121     bool mQueueStarted;
    122     wp<MediaPlayerBase> mListener;
    123 
    124     sp<ISurface> mISurface;
    125     sp<MediaPlayerBase::AudioSink> mAudioSink;
    126 
    127     SystemTimeSource mSystemTimeSource;
    128     TimeSource *mTimeSource;
    129 
    130     String8 mUri;
    131     KeyedVector<String8, String8> mUriHeaders;
    132 
    133     sp<DataSource> mFileSource;
    134 
    135     sp<MediaSource> mVideoTrack;
    136     sp<MediaSource> mVideoSource;
    137     sp<AwesomeRenderer> mVideoRenderer;
    138     bool mVideoRendererIsPreview;
    139 
    140     sp<MediaSource> mAudioTrack;
    141     sp<MediaSource> mAudioSource;
    142     AudioPlayer *mAudioPlayer;
    143     int64_t mDurationUs;
    144 
    145     uint32_t mFlags;
    146     uint32_t mExtractorFlags;
    147 
    148     int32_t mVideoWidth, mVideoHeight;
    149     int64_t mTimeSourceDeltaUs;
    150     int64_t mVideoTimeUs;
    151 
    152     bool mSeeking;
    153     bool mSeekNotificationSent;
    154     int64_t mSeekTimeUs;
    155 
    156     int64_t mBitrate;  // total bitrate of the file (in bps) or -1 if unknown.
    157 
    158     bool mWatchForAudioSeekComplete;
    159     bool mWatchForAudioEOS;
    160 
    161     sp<TimedEventQueue::Event> mVideoEvent;
    162     bool mVideoEventPending;
    163     sp<TimedEventQueue::Event> mStreamDoneEvent;
    164     bool mStreamDoneEventPending;
    165     sp<TimedEventQueue::Event> mBufferingEvent;
    166     bool mBufferingEventPending;
    167     sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
    168     bool mAudioStatusEventPending;
    169 
    170     sp<TimedEventQueue::Event> mAsyncPrepareEvent;
    171     Condition mPreparedCondition;
    172     bool mIsAsyncPrepare;
    173     status_t mPrepareResult;
    174     status_t mStreamDoneStatus;
    175 
    176     void postVideoEvent_l(int64_t delayUs = -1);
    177     void postBufferingEvent_l();
    178     void postStreamDoneEvent_l(status_t status);
    179     void postCheckAudioStatusEvent_l();
    180     status_t play_l();
    181 
    182     MediaBuffer *mLastVideoBuffer;
    183     MediaBuffer *mVideoBuffer;
    184 
    185     sp<NuHTTPDataSource> mConnectingDataSource;
    186     sp<NuCachedSource2> mCachedSource;
    187 
    188     sp<ALooper> mLooper;
    189     sp<ARTSPController> mRTSPController;
    190     sp<ARTPSession> mRTPSession;
    191     sp<UDPPusher> mRTPPusher, mRTCPPusher;
    192 
    193     struct SuspensionState {
    194         String8 mUri;
    195         KeyedVector<String8, String8> mUriHeaders;
    196         sp<DataSource> mFileSource;
    197 
    198         uint32_t mFlags;
    199         int64_t mPositionUs;
    200 
    201         void *mLastVideoFrame;
    202         size_t mLastVideoFrameSize;
    203         int32_t mColorFormat;
    204         int32_t mVideoWidth, mVideoHeight;
    205         int32_t mDecodedWidth, mDecodedHeight;
    206 
    207         SuspensionState()
    208             : mLastVideoFrame(NULL) {
    209         }
    210 
    211         ~SuspensionState() {
    212             if (mLastVideoFrame) {
    213                 free(mLastVideoFrame);
    214                 mLastVideoFrame = NULL;
    215             }
    216         }
    217     } *mSuspensionState;
    218 
    219     status_t setDataSource_l(
    220             const char *uri,
    221             const KeyedVector<String8, String8> *headers = NULL);
    222 
    223     status_t setDataSource_l(const sp<DataSource> &dataSource);
    224     status_t setDataSource_l(const sp<MediaExtractor> &extractor);
    225     void reset_l();
    226     void partial_reset_l();
    227     status_t seekTo_l(int64_t timeUs);
    228     status_t pause_l(bool at_eos = false);
    229     status_t initRenderer_l();
    230     void seekAudioIfNecessary_l();
    231 
    232     void cancelPlayerEvents(bool keepBufferingGoing = false);
    233 
    234     void setAudioSource(sp<MediaSource> source);
    235     status_t initAudioDecoder();
    236 
    237     void setVideoSource(sp<MediaSource> source);
    238     status_t initVideoDecoder(uint32_t flags = 0);
    239 
    240     void onStreamDone();
    241 
    242     void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
    243 
    244     void onVideoEvent();
    245     void onBufferingUpdate();
    246     void onCheckAudioStatus();
    247     void onPrepareAsyncEvent();
    248     void abortPrepare(status_t err);
    249     void finishAsyncPrepare_l();
    250 
    251     bool getCachedDuration_l(int64_t *durationUs, bool *eos);
    252 
    253     status_t finishSetDataSource_l();
    254 
    255     static bool ContinuePreparation(void *cookie);
    256 
    257     static void OnRTSPSeekDoneWrapper(void *cookie);
    258     void onRTSPSeekDone();
    259 
    260     bool getBitrate(int64_t *bitrate);
    261 
    262     void finishSeekIfNecessary(int64_t videoTimeUs);
    263 
    264     AwesomePlayer(const AwesomePlayer &);
    265     AwesomePlayer &operator=(const AwesomePlayer &);
    266 };
    267 
    268 }  // namespace android
    269 
    270 #endif  // AWESOME_PLAYER_H_
    271 
    272