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/AudioResamplerPublic.h>
     22 #include <media/ICrypto.h>
     23 #include <media/MediaPlayerInterface.h>
     24 #include <media/stagefright/foundation/AHandler.h>
     25 
     26 namespace android {
     27 
     28 struct ABuffer;
     29 struct AMessage;
     30 struct AudioPlaybackRate;
     31 struct AVSyncSettings;
     32 class IDataSource;
     33 struct MediaClock;
     34 class MetaData;
     35 struct NuPlayerDriver;
     36 
     37 struct NuPlayer : public AHandler {
     38     explicit NuPlayer(pid_t pid, const sp<MediaClock> &mediaClock);
     39 
     40     void setUID(uid_t uid);
     41 
     42     void init(const wp<NuPlayerDriver> &driver);
     43 
     44     void setDataSourceAsync(const sp<IStreamSource> &source);
     45 
     46     void setDataSourceAsync(
     47             const sp<IMediaHTTPService> &httpService,
     48             const char *url,
     49             const KeyedVector<String8, String8> *headers);
     50 
     51     void setDataSourceAsync(int fd, int64_t offset, int64_t length);
     52 
     53     void setDataSourceAsync(const sp<DataSource> &source);
     54 
     55     status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
     56     status_t setBufferingSettings(const BufferingSettings& buffering);
     57 
     58     void prepareAsync();
     59 
     60     void setVideoSurfaceTextureAsync(
     61             const sp<IGraphicBufferProducer> &bufferProducer);
     62 
     63     void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
     64     status_t setPlaybackSettings(const AudioPlaybackRate &rate);
     65     status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
     66     status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
     67     status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
     68 
     69     void start();
     70 
     71     void pause();
     72 
     73     // Will notify the driver through "notifyResetComplete" once finished.
     74     void resetAsync();
     75 
     76     // Request a notification when specified media time is reached.
     77     status_t notifyAt(int64_t mediaTimeUs);
     78 
     79     // Will notify the driver through "notifySeekComplete" once finished
     80     // and needNotify is true.
     81     void seekToAsync(
     82             int64_t seekTimeUs,
     83             MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC,
     84             bool needNotify = false);
     85 
     86     status_t setVideoScalingMode(int32_t mode);
     87     status_t getTrackInfo(Parcel* reply) const;
     88     status_t getSelectedTrack(int32_t type, Parcel* reply) const;
     89     status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
     90     status_t getCurrentPosition(int64_t *mediaUs);
     91     void getStats(Vector<sp<AMessage> > *trackStats);
     92 
     93     sp<MetaData> getFileMeta();
     94     float getFrameRate();
     95 
     96     // Modular DRM
     97     status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
     98     status_t releaseDrm();
     99 
    100     const char *getDataSourceType();
    101 
    102     void updateInternalTimers();
    103 
    104 protected:
    105     virtual ~NuPlayer();
    106 
    107     virtual void onMessageReceived(const sp<AMessage> &msg);
    108 
    109 public:
    110     struct NuPlayerStreamListener;
    111     struct Source;
    112 
    113 private:
    114     struct Decoder;
    115     struct DecoderBase;
    116     struct DecoderPassThrough;
    117     struct CCDecoder;
    118     struct GenericSource;
    119     struct HTTPLiveSource;
    120     struct Renderer;
    121     struct RTSPSource;
    122     struct StreamingSource;
    123     struct Action;
    124     struct SeekAction;
    125     struct SetSurfaceAction;
    126     struct ResumeDecoderAction;
    127     struct FlushDecoderAction;
    128     struct PostMessageAction;
    129     struct SimpleAction;
    130 
    131     enum {
    132         kWhatSetDataSource              = '=DaS',
    133         kWhatPrepare                    = 'prep',
    134         kWhatSetVideoSurface            = '=VSu',
    135         kWhatSetAudioSink               = '=AuS',
    136         kWhatMoreDataQueued             = 'more',
    137         kWhatConfigPlayback             = 'cfPB',
    138         kWhatConfigSync                 = 'cfSy',
    139         kWhatGetPlaybackSettings        = 'gPbS',
    140         kWhatGetSyncSettings            = 'gSyS',
    141         kWhatStart                      = 'strt',
    142         kWhatScanSources                = 'scan',
    143         kWhatVideoNotify                = 'vidN',
    144         kWhatAudioNotify                = 'audN',
    145         kWhatClosedCaptionNotify        = 'capN',
    146         kWhatRendererNotify             = 'renN',
    147         kWhatReset                      = 'rset',
    148         kWhatNotifyTime                 = 'nfyT',
    149         kWhatSeek                       = 'seek',
    150         kWhatPause                      = 'paus',
    151         kWhatResume                     = 'rsme',
    152         kWhatPollDuration               = 'polD',
    153         kWhatSourceNotify               = 'srcN',
    154         kWhatGetTrackInfo               = 'gTrI',
    155         kWhatGetSelectedTrack           = 'gSel',
    156         kWhatSelectTrack                = 'selT',
    157         kWhatGetBufferingSettings       = 'gBus',
    158         kWhatSetBufferingSettings       = 'sBuS',
    159         kWhatPrepareDrm                 = 'pDrm',
    160         kWhatReleaseDrm                 = 'rDrm',
    161         kWhatMediaClockNotify           = 'mckN',
    162     };
    163 
    164     wp<NuPlayerDriver> mDriver;
    165     bool mUIDValid;
    166     uid_t mUID;
    167     pid_t mPID;
    168     const sp<MediaClock> mMediaClock;
    169     Mutex mSourceLock;  // guard |mSource|.
    170     sp<Source> mSource;
    171     uint32_t mSourceFlags;
    172     sp<Surface> mSurface;
    173     sp<MediaPlayerBase::AudioSink> mAudioSink;
    174     sp<DecoderBase> mVideoDecoder;
    175     bool mOffloadAudio;
    176     sp<DecoderBase> mAudioDecoder;
    177     Mutex mDecoderLock;  // guard |mAudioDecoder| and |mVideoDecoder|.
    178     sp<CCDecoder> mCCDecoder;
    179     sp<Renderer> mRenderer;
    180     sp<ALooper> mRendererLooper;
    181     int32_t mAudioDecoderGeneration;
    182     int32_t mVideoDecoderGeneration;
    183     int32_t mRendererGeneration;
    184 
    185     Mutex mPlayingTimeLock;
    186     int64_t mLastStartedPlayingTimeNs;
    187     void updatePlaybackTimer(bool stopping, const char *where);
    188     void startPlaybackTimer(const char *where);
    189 
    190     int64_t mLastStartedRebufferingTimeNs;
    191     void startRebufferingTimer();
    192     void updateRebufferingTimer(bool stopping, bool exitingPlayback);
    193 
    194     int64_t mPreviousSeekTimeUs;
    195 
    196     List<sp<Action> > mDeferredActions;
    197 
    198     bool mAudioEOS;
    199     bool mVideoEOS;
    200 
    201     bool mScanSourcesPending;
    202     int32_t mScanSourcesGeneration;
    203 
    204     int32_t mPollDurationGeneration;
    205     int32_t mTimedTextGeneration;
    206 
    207     enum FlushStatus {
    208         NONE,
    209         FLUSHING_DECODER,
    210         FLUSHING_DECODER_SHUTDOWN,
    211         SHUTTING_DOWN_DECODER,
    212         FLUSHED,
    213         SHUT_DOWN,
    214     };
    215 
    216     enum FlushCommand {
    217         FLUSH_CMD_NONE,
    218         FLUSH_CMD_FLUSH,
    219         FLUSH_CMD_SHUTDOWN,
    220     };
    221 
    222     // Status of flush responses from the decoder and renderer.
    223     bool mFlushComplete[2][2];
    224 
    225     FlushStatus mFlushingAudio;
    226     FlushStatus mFlushingVideo;
    227 
    228     // Status of flush responses from the decoder and renderer.
    229     bool mResumePending;
    230 
    231     int32_t mVideoScalingMode;
    232 
    233     AudioPlaybackRate mPlaybackSettings;
    234     AVSyncSettings mSyncSettings;
    235     float mVideoFpsHint;
    236     bool mStarted;
    237     bool mPrepared;
    238     bool mResetting;
    239     bool mSourceStarted;
    240     bool mAudioDecoderError;
    241     bool mVideoDecoderError;
    242 
    243     // Actual pause state, either as requested by client or due to buffering.
    244     bool mPaused;
    245 
    246     // Pause state as requested by client. Note that if mPausedByClient is
    247     // true, mPaused is always true; if mPausedByClient is false, mPaused could
    248     // still become true, when we pause internally due to buffering.
    249     bool mPausedByClient;
    250 
    251     // Pause state as requested by source (internally) due to buffering
    252     bool mPausedForBuffering;
    253 
    254     // Modular DRM
    255     sp<ICrypto> mCrypto;
    256     bool mIsDrmProtected;
    257 
    258     typedef enum {
    259         DATA_SOURCE_TYPE_NONE,
    260         DATA_SOURCE_TYPE_HTTP_LIVE,
    261         DATA_SOURCE_TYPE_RTSP,
    262         DATA_SOURCE_TYPE_GENERIC_URL,
    263         DATA_SOURCE_TYPE_GENERIC_FD,
    264         DATA_SOURCE_TYPE_MEDIA,
    265         DATA_SOURCE_TYPE_STREAM,
    266     } DATA_SOURCE_TYPE;
    267 
    268     std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
    269 
    270     inline const sp<DecoderBase> &getDecoder(bool audio) {
    271         return audio ? mAudioDecoder : mVideoDecoder;
    272     }
    273 
    274     inline void clearFlushComplete() {
    275         mFlushComplete[0][0] = false;
    276         mFlushComplete[0][1] = false;
    277         mFlushComplete[1][0] = false;
    278         mFlushComplete[1][1] = false;
    279     }
    280 
    281     void tryOpenAudioSinkForOffload(
    282             const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
    283     void closeAudioSink();
    284     void restartAudio(
    285             int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
    286     void determineAudioModeChange(const sp<AMessage> &audioFormat);
    287 
    288     status_t instantiateDecoder(
    289             bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
    290 
    291     status_t onInstantiateSecureDecoders();
    292 
    293     void updateVideoSize(
    294             const sp<AMessage> &inputFormat,
    295             const sp<AMessage> &outputFormat = NULL);
    296 
    297     void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
    298 
    299     void handleFlushComplete(bool audio, bool isDecoder);
    300     void finishFlushIfPossible();
    301 
    302     void onStart(
    303             int64_t startPositionUs = -1,
    304             MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC);
    305     void onResume();
    306     void onPause();
    307 
    308     bool audioDecoderStillNeeded();
    309 
    310     void flushDecoder(bool audio, bool needShutdown);
    311 
    312     void finishResume();
    313     void notifyDriverSeekComplete();
    314 
    315     void postScanSources();
    316 
    317     void schedulePollDuration();
    318     void cancelPollDuration();
    319 
    320     void processDeferredActions();
    321 
    322     void performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode);
    323     void performDecoderFlush(FlushCommand audio, FlushCommand video);
    324     void performReset();
    325     void performScanSources();
    326     void performSetSurface(const sp<Surface> &wrapper);
    327     void performResumeDecoders(bool needNotify);
    328 
    329     void onSourceNotify(const sp<AMessage> &msg);
    330     void onClosedCaptionNotify(const sp<AMessage> &msg);
    331 
    332     void queueDecoderShutdown(
    333             bool audio, bool video, const sp<AMessage> &reply);
    334 
    335     void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
    336     void sendTimedMetaData(const sp<ABuffer> &buffer);
    337     void sendTimedTextData(const sp<ABuffer> &buffer);
    338 
    339     void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
    340 
    341     status_t onPrepareDrm(const sp<AMessage> &msg);
    342     status_t onReleaseDrm();
    343 
    344     DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
    345 };
    346 
    347 }  // namespace android
    348 
    349 #endif  // NU_PLAYER_H_
    350