Home | History | Annotate | Download | only in nuplayer
      1 /*
      2  * Copyright (C) 2010 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 NU_PLAYER_H_
     18 
     19 #define NU_PLAYER_H_
     20 
     21 #include <media/MediaPlayerInterface.h>
     22 #include <media/stagefright/foundation/AHandler.h>
     23 #include <media/stagefright/NativeWindowWrapper.h>
     24 
     25 namespace android {
     26 
     27 struct ACodec;
     28 struct MetaData;
     29 struct NuPlayerDriver;
     30 
     31 struct NuPlayer : public AHandler {
     32     NuPlayer();
     33 
     34     void setUID(uid_t uid);
     35 
     36     void setDriver(const wp<NuPlayerDriver> &driver);
     37 
     38     void setDataSourceAsync(const sp<IStreamSource> &source);
     39 
     40     void setDataSourceAsync(
     41             const char *url, const KeyedVector<String8, String8> *headers);
     42 
     43     void setDataSourceAsync(int fd, int64_t offset, int64_t length);
     44 
     45     void prepareAsync();
     46 
     47     void setVideoSurfaceTextureAsync(
     48             const sp<IGraphicBufferProducer> &bufferProducer);
     49 
     50     void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
     51     void start();
     52 
     53     void pause();
     54     void resume();
     55 
     56     // Will notify the driver through "notifyResetComplete" once finished.
     57     void resetAsync();
     58 
     59     // Will notify the driver through "notifySeekComplete" once finished.
     60     void seekToAsync(int64_t seekTimeUs);
     61 
     62     status_t setVideoScalingMode(int32_t mode);
     63 
     64 protected:
     65     virtual ~NuPlayer();
     66 
     67     virtual void onMessageReceived(const sp<AMessage> &msg);
     68 
     69 public:
     70     struct NuPlayerStreamListener;
     71     struct Source;
     72 
     73 private:
     74     struct Decoder;
     75     struct GenericSource;
     76     struct HTTPLiveSource;
     77     struct Renderer;
     78     struct RTSPSource;
     79     struct StreamingSource;
     80     struct Action;
     81     struct SeekAction;
     82     struct SetSurfaceAction;
     83     struct SimpleAction;
     84 
     85     enum {
     86         kWhatSetDataSource              = '=DaS',
     87         kWhatPrepare                    = 'prep',
     88         kWhatSetVideoNativeWindow       = '=NaW',
     89         kWhatSetAudioSink               = '=AuS',
     90         kWhatMoreDataQueued             = 'more',
     91         kWhatStart                      = 'strt',
     92         kWhatScanSources                = 'scan',
     93         kWhatVideoNotify                = 'vidN',
     94         kWhatAudioNotify                = 'audN',
     95         kWhatRendererNotify             = 'renN',
     96         kWhatReset                      = 'rset',
     97         kWhatSeek                       = 'seek',
     98         kWhatPause                      = 'paus',
     99         kWhatResume                     = 'rsme',
    100         kWhatPollDuration               = 'polD',
    101         kWhatSourceNotify               = 'srcN',
    102     };
    103 
    104     wp<NuPlayerDriver> mDriver;
    105     bool mUIDValid;
    106     uid_t mUID;
    107     sp<Source> mSource;
    108     uint32_t mSourceFlags;
    109     sp<NativeWindowWrapper> mNativeWindow;
    110     sp<MediaPlayerBase::AudioSink> mAudioSink;
    111     sp<Decoder> mVideoDecoder;
    112     bool mVideoIsAVC;
    113     sp<Decoder> mAudioDecoder;
    114     sp<Renderer> mRenderer;
    115 
    116     List<sp<Action> > mDeferredActions;
    117 
    118     bool mAudioEOS;
    119     bool mVideoEOS;
    120 
    121     bool mScanSourcesPending;
    122     int32_t mScanSourcesGeneration;
    123 
    124     int32_t mPollDurationGeneration;
    125 
    126     enum FlushStatus {
    127         NONE,
    128         AWAITING_DISCONTINUITY,
    129         FLUSHING_DECODER,
    130         FLUSHING_DECODER_SHUTDOWN,
    131         SHUTTING_DOWN_DECODER,
    132         FLUSHED,
    133         SHUT_DOWN,
    134     };
    135 
    136     // Once the current flush is complete this indicates whether the
    137     // notion of time has changed.
    138     bool mTimeDiscontinuityPending;
    139 
    140     FlushStatus mFlushingAudio;
    141     FlushStatus mFlushingVideo;
    142 
    143     int64_t mSkipRenderingAudioUntilMediaTimeUs;
    144     int64_t mSkipRenderingVideoUntilMediaTimeUs;
    145 
    146     int64_t mVideoLateByUs;
    147     int64_t mNumFramesTotal, mNumFramesDropped;
    148 
    149     int32_t mVideoScalingMode;
    150 
    151     bool mStarted;
    152 
    153     status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
    154 
    155     status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
    156     void renderBuffer(bool audio, const sp<AMessage> &msg);
    157 
    158     void notifyListener(int msg, int ext1, int ext2);
    159 
    160     void finishFlushIfPossible();
    161 
    162     void flushDecoder(bool audio, bool needShutdown);
    163 
    164     static bool IsFlushingState(FlushStatus state, bool *needShutdown = NULL);
    165 
    166     void postScanSources();
    167 
    168     void schedulePollDuration();
    169     void cancelPollDuration();
    170 
    171     void processDeferredActions();
    172 
    173     void performSeek(int64_t seekTimeUs);
    174     void performDecoderFlush();
    175     void performDecoderShutdown();
    176     void performReset();
    177     void performScanSources();
    178     void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
    179 
    180     void onSourceNotify(const sp<AMessage> &msg);
    181 
    182     DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
    183 };
    184 
    185 }  // namespace android
    186 
    187 #endif  // NU_PLAYER_H_
    188