NuPlayerRenderer.h revision eecb7805bbbb712925d4372c505f8c7f5c4fb5ed
13d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet/*
23d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet * Copyright (C) 2010 The Android Open Source Project
33d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet *
43d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet * Licensed under the Apache License, Version 2.0 (the "License");
53d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet * you may not use this file except in compliance with the License.
670f2a0485dd81e6684553eb5537a27db4176ed45Gabor Greif * You may obtain a copy of the License at
73d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet *
870f2a0485dd81e6684553eb5537a27db4176ed45Gabor Greif *      http://www.apache.org/licenses/LICENSE-2.0
970f2a0485dd81e6684553eb5537a27db4176ed45Gabor Greif *
103d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet * Unless required by applicable law or agreed to in writing, software
113d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet * distributed under the License is distributed on an "AS IS" BASIS,
123d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet * See the License for the specific language governing permissions and
143d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet * limitations under the License.
153d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet */
163d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
173d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet#ifndef NUPLAYER_RENDERER_H_
183d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
193d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet#define NUPLAYER_RENDERER_H_
203d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
213d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet#include "NuPlayer.h"
223d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
233d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venetnamespace android {
243d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
253d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venetstruct ABuffer;
263d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venetstruct VideoFrameScheduler;
273d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
283d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venetstruct NuPlayer::Renderer : public AHandler {
293d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    enum Flags {
303d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        FLAG_REAL_TIME = 1,
313d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        FLAG_OFFLOAD_AUDIO = 2,
323d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    };
33cec8e775895fadfc324fe97a90b3c9601f86491cChris Lattner    Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
343d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet             const sp<AMessage> &notify,
353d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet             uint32_t flags = 0);
363d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
373d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    static size_t AudioSinkCallback(
383d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            MediaPlayerBase::AudioSink *audioSink,
393d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            void *data, size_t size, void *me,
403d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            MediaPlayerBase::AudioSink::cb_event_t event);
413d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
423d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void queueBuffer(
433d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            bool audio,
443d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            const sp<ABuffer> &buffer,
453d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            const sp<AMessage> &notifyConsumed);
463d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
473d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void queueEOS(bool audio, status_t finalResult);
483d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
493d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void flush(bool audio);
503d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
513d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void signalTimeDiscontinuity();
523d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
533d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void signalAudioSinkChanged();
543d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
553d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void signalDisableOffloadAudio();
563d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
573d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void pause();
583d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void resume();
593d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
603d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void setVideoFrameRate(float fps);
613d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
623d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    // Following setters and getters are protected by mTimeLock.
633d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    status_t getCurrentPosition(int64_t *mediaUs);
643d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    status_t getCurrentPosition(int64_t *mediaUs, int64_t nowUs);
653d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void setHasMedia(bool audio);
663d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void setAudioFirstAnchorTime(int64_t mediaUs);
673d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void setAudioFirstAnchorTimeIfNeeded(int64_t mediaUs);
683d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void setAnchorTime(int64_t mediaUs, int64_t realUs, bool resume = false);
693d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void setVideoLateByUs(int64_t lateUs);
703d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    int64_t getVideoLateByUs();
713d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void setPauseStartedTimeRealUs(int64_t realUs);
723d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
733d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    bool openAudioSink(
743d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            const sp<AMessage> &format,
753d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            bool offloadOnly,
763d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            bool hasVideo,
773d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet            uint32_t flags);
783d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    void closeAudioSink();
793d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
803d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    enum {
813d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatEOS                 = 'eos ',
823d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatFlushComplete       = 'fluC',
833d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatPosition            = 'posi',
843d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatVideoRenderingStart = 'vdrd',
853d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatMediaRenderingStart = 'mdrd',
863d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatAudioOffloadTearDown = 'aOTD',
873d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatAudioOffloadPauseTimeout = 'aOPT',
883d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    };
893d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
903d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    enum AudioOffloadTearDownReason {
913d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kDueToError = 0,
9257f1a00bf28e32a10414bb383e1cfc8dce3f0462Douglas Gregor        kDueToTimeout,
9300406d0ab613f1ce1fa1b664c0fd21b8d898f44dChris Lattner    };
9400406d0ab613f1ce1fa1b664c0fd21b8d898f44dChris Lattner
9500406d0ab613f1ce1fa1b664c0fd21b8d898f44dChris Lattnerprotected:
9657f1a00bf28e32a10414bb383e1cfc8dce3f0462Douglas Gregor    virtual ~Renderer();
97e6e68b53778bb5a15c10a73a5bf18d8ab73f75e3Richard Smith
9857f1a00bf28e32a10414bb383e1cfc8dce3f0462Douglas Gregor    virtual void onMessageReceived(const sp<AMessage> &msg);
9957f1a00bf28e32a10414bb383e1cfc8dce3f0462Douglas Gregor
1003d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venetprivate:
1013d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    enum {
1023d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatDrainAudioQueue     = 'draA',
1033d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatDrainVideoQueue     = 'draV',
1043d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatQueueBuffer         = 'queB',
1053d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatQueueEOS            = 'qEOS',
1063d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatFlush               = 'flus',
1073d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatAudioSinkChanged    = 'auSC',
1083d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatPause               = 'paus',
1093d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatResume              = 'resm',
1103d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatOpenAudioSink       = 'opnA',
1113d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatCloseAudioSink      = 'clsA',
1123d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatStopAudioSink       = 'stpA',
1133d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatDisableOffloadAudio = 'noOA',
1143d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        kWhatSetVideoFrameRate   = 'sVFR',
1153d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    };
1163d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet
1173d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet    struct QueueEntry {
1183d658640abc128dcc84a5a5201456395c86c4fa6Cedric Venet        sp<ABuffer> mBuffer;
119        sp<AMessage> mNotifyConsumed;
120        size_t mOffset;
121        status_t mFinalResult;
122    };
123
124    static const int64_t kMinPositionUpdateDelayUs;
125
126    sp<MediaPlayerBase::AudioSink> mAudioSink;
127    sp<AMessage> mNotify;
128    Mutex mLock;
129    uint32_t mFlags;
130    List<QueueEntry> mAudioQueue;
131    List<QueueEntry> mVideoQueue;
132    uint32_t mNumFramesWritten;
133    sp<VideoFrameScheduler> mVideoScheduler;
134
135    bool mDrainAudioQueuePending;
136    bool mDrainVideoQueuePending;
137    int32_t mAudioQueueGeneration;
138    int32_t mVideoQueueGeneration;
139
140    Mutex mTimeLock;
141    // |mTimeLock| protects the following 7 member vars that are related to time.
142    // Note: those members are only written on Renderer thread, so reading on Renderer thread
143    // doesn't need to be protected. Otherwise accessing those members must be protected by
144    // |mTimeLock|.
145    // TODO: move those members to a seperated media clock class.
146    int64_t mAudioFirstAnchorTimeMediaUs;
147    int64_t mAnchorTimeMediaUs;
148    int64_t mAnchorTimeRealUs;
149    int64_t mVideoLateByUs;
150    bool mHasAudio;
151    bool mHasVideo;
152    int64_t mPauseStartedTimeRealUs;
153
154    Mutex mFlushLock;  // protects the following 2 member vars.
155    bool mFlushingAudio;
156    bool mFlushingVideo;
157
158    bool mSyncQueues;
159
160    bool mPaused;
161    bool mVideoSampleReceived;
162    bool mVideoRenderingStarted;
163    int32_t mVideoRenderingStartGeneration;
164    int32_t mAudioRenderingStartGeneration;
165
166    int64_t mLastPositionUpdateUs;
167
168    int32_t mAudioOffloadPauseTimeoutGeneration;
169    bool mAudioOffloadTornDown;
170    audio_offload_info_t mCurrentOffloadInfo;
171
172    size_t fillAudioBuffer(void *buffer, size_t size);
173
174    bool onDrainAudioQueue();
175    int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
176    int64_t getPlayedOutAudioDurationUs(int64_t nowUs);
177    void postDrainAudioQueue_l(int64_t delayUs = 0);
178
179    void onNewAudioMediaTime(int64_t mediaTimeUs);
180    int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs);
181
182    void onDrainVideoQueue();
183    void postDrainVideoQueue();
184
185    void prepareForMediaRenderingStart();
186    void notifyIfMediaRenderingStarted();
187
188    void onQueueBuffer(const sp<AMessage> &msg);
189    void onQueueEOS(const sp<AMessage> &msg);
190    void onFlush(const sp<AMessage> &msg);
191    void onAudioSinkChanged();
192    void onDisableOffloadAudio();
193    void onPause();
194    void onResume();
195    void onSetVideoFrameRate(float fps);
196    void onAudioOffloadTearDown(AudioOffloadTearDownReason reason);
197    bool onOpenAudioSink(
198            const sp<AMessage> &format,
199            bool offloadOnly,
200            bool hasVideo,
201            uint32_t flags);
202    void onCloseAudioSink();
203
204    void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
205    void notifyFlushComplete(bool audio);
206    void notifyPosition();
207    void notifyVideoLateBy(int64_t lateByUs);
208    void notifyVideoRenderingStart();
209    void notifyAudioOffloadTearDown();
210
211    void flushQueue(List<QueueEntry> *queue);
212    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
213    void syncQueuesDone_l();
214
215    bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
216
217    void startAudioOffloadPauseTimeout();
218    void cancelAudioOffloadPauseTimeout();
219
220    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
221};
222
223}  // namespace android
224
225#endif  // NUPLAYER_RENDERER_H_
226