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 NUPLAYER_RENDERER_H_
     18 
     19 #define NUPLAYER_RENDERER_H_
     20 
     21 #include "NuPlayer.h"
     22 
     23 namespace android {
     24 
     25 struct ABuffer;
     26 
     27 struct NuPlayer::Renderer : public AHandler {
     28     enum Flags {
     29         FLAG_REAL_TIME = 1,
     30     };
     31     Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
     32              const sp<AMessage> &notify,
     33              uint32_t flags = 0);
     34 
     35     void queueBuffer(
     36             bool audio,
     37             const sp<ABuffer> &buffer,
     38             const sp<AMessage> &notifyConsumed);
     39 
     40     void queueEOS(bool audio, status_t finalResult);
     41 
     42     void flush(bool audio);
     43 
     44     void signalTimeDiscontinuity();
     45 
     46     void signalAudioSinkChanged();
     47 
     48     void pause();
     49     void resume();
     50 
     51     enum {
     52         kWhatEOS                 = 'eos ',
     53         kWhatFlushComplete       = 'fluC',
     54         kWhatPosition            = 'posi',
     55         kWhatVideoRenderingStart = 'vdrd',
     56         kWhatMediaRenderingStart = 'mdrd',
     57     };
     58 
     59 protected:
     60     virtual ~Renderer();
     61 
     62     virtual void onMessageReceived(const sp<AMessage> &msg);
     63 
     64 private:
     65     enum {
     66         kWhatDrainAudioQueue    = 'draA',
     67         kWhatDrainVideoQueue    = 'draV',
     68         kWhatQueueBuffer        = 'queB',
     69         kWhatQueueEOS           = 'qEOS',
     70         kWhatFlush              = 'flus',
     71         kWhatAudioSinkChanged   = 'auSC',
     72         kWhatPause              = 'paus',
     73         kWhatResume             = 'resm',
     74     };
     75 
     76     struct QueueEntry {
     77         sp<ABuffer> mBuffer;
     78         sp<AMessage> mNotifyConsumed;
     79         size_t mOffset;
     80         status_t mFinalResult;
     81     };
     82 
     83     static const int64_t kMinPositionUpdateDelayUs;
     84 
     85     sp<MediaPlayerBase::AudioSink> mAudioSink;
     86     sp<AMessage> mNotify;
     87     uint32_t mFlags;
     88     List<QueueEntry> mAudioQueue;
     89     List<QueueEntry> mVideoQueue;
     90     uint32_t mNumFramesWritten;
     91 
     92     bool mDrainAudioQueuePending;
     93     bool mDrainVideoQueuePending;
     94     int32_t mAudioQueueGeneration;
     95     int32_t mVideoQueueGeneration;
     96 
     97     int64_t mAnchorTimeMediaUs;
     98     int64_t mAnchorTimeRealUs;
     99 
    100     Mutex mFlushLock;  // protects the following 2 member vars.
    101     bool mFlushingAudio;
    102     bool mFlushingVideo;
    103 
    104     bool mHasAudio;
    105     bool mHasVideo;
    106     bool mSyncQueues;
    107 
    108     bool mPaused;
    109     bool mVideoRenderingStarted;
    110     int32_t mVideoRenderingStartGeneration;
    111     int32_t mAudioRenderingStartGeneration;
    112 
    113     int64_t mLastPositionUpdateUs;
    114     int64_t mVideoLateByUs;
    115 
    116     bool onDrainAudioQueue();
    117     void postDrainAudioQueue(int64_t delayUs = 0);
    118 
    119     void onDrainVideoQueue();
    120     void postDrainVideoQueue();
    121 
    122     void prepareForMediaRenderingStart();
    123     void notifyIfMediaRenderingStarted();
    124 
    125     void onQueueBuffer(const sp<AMessage> &msg);
    126     void onQueueEOS(const sp<AMessage> &msg);
    127     void onFlush(const sp<AMessage> &msg);
    128     void onAudioSinkChanged();
    129     void onPause();
    130     void onResume();
    131 
    132     void notifyEOS(bool audio, status_t finalResult);
    133     void notifyFlushComplete(bool audio);
    134     void notifyPosition();
    135     void notifyVideoLateBy(int64_t lateByUs);
    136     void notifyVideoRenderingStart();
    137 
    138     void flushQueue(List<QueueEntry> *queue);
    139     bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
    140     void syncQueuesDone();
    141 
    142     DISALLOW_EVIL_CONSTRUCTORS(Renderer);
    143 };
    144 
    145 }  // namespace android
    146 
    147 #endif  // NUPLAYER_RENDERER_H_
    148