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