NuPlayerRenderer.h revision d4cdba18ba7d0057ae54ec7efa5871b1a9d8beca
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
23namespace android {
24
25struct ABuffer;
26struct VideoFrameScheduler;
27
28struct NuPlayer::Renderer : public AHandler {
29    enum Flags {
30        FLAG_REAL_TIME = 1,
31        FLAG_OFFLOAD_AUDIO = 2,
32    };
33    Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
34             const sp<AMessage> &notify,
35             uint32_t flags = 0);
36
37    static size_t AudioSinkCallback(
38            MediaPlayerBase::AudioSink *audioSink,
39            void *data, size_t size, void *me,
40            MediaPlayerBase::AudioSink::cb_event_t event);
41
42    void queueBuffer(
43            bool audio,
44            const sp<ABuffer> &buffer,
45            const sp<AMessage> &notifyConsumed);
46
47    void queueEOS(bool audio, status_t finalResult);
48
49    void flush(bool audio, bool notifyComplete);
50
51    void signalTimeDiscontinuity();
52
53    void signalAudioSinkChanged();
54
55    void signalDisableOffloadAudio();
56    void signalEnableOffloadAudio();
57
58    void pause();
59    void resume();
60
61    void setVideoFrameRate(float fps);
62
63    // Following setters and getters are protected by mTimeLock.
64    status_t getCurrentPosition(int64_t *mediaUs);
65    void setHasMedia(bool audio);
66    void setAudioFirstAnchorTime(int64_t mediaUs);
67    void setAudioFirstAnchorTimeIfNeeded(int64_t mediaUs);
68    void setAnchorTime(
69            int64_t mediaUs, int64_t realUs, int64_t numFramesWritten = -1, bool resume = false);
70    void setVideoLateByUs(int64_t lateUs);
71    int64_t getVideoLateByUs();
72    void setPauseStartedTimeRealUs(int64_t realUs);
73
74    status_t openAudioSink(
75            const sp<AMessage> &format,
76            bool offloadOnly,
77            bool hasVideo,
78            uint32_t flags,
79            bool *isOffloaded);
80    void closeAudioSink();
81
82    enum {
83        kWhatEOS                 = 'eos ',
84        kWhatFlushComplete       = 'fluC',
85        kWhatPosition            = 'posi',
86        kWhatVideoRenderingStart = 'vdrd',
87        kWhatMediaRenderingStart = 'mdrd',
88        kWhatAudioOffloadTearDown = 'aOTD',
89        kWhatAudioOffloadPauseTimeout = 'aOPT',
90    };
91
92    enum AudioOffloadTearDownReason {
93        kDueToError = 0,
94        kDueToTimeout,
95    };
96
97protected:
98    virtual ~Renderer();
99
100    virtual void onMessageReceived(const sp<AMessage> &msg);
101
102private:
103    enum {
104        kWhatDrainAudioQueue     = 'draA',
105        kWhatDrainVideoQueue     = 'draV',
106        kWhatPostDrainVideoQueue = 'pDVQ',
107        kWhatQueueBuffer         = 'queB',
108        kWhatQueueEOS            = 'qEOS',
109        kWhatFlush               = 'flus',
110        kWhatAudioSinkChanged    = 'auSC',
111        kWhatPause               = 'paus',
112        kWhatResume              = 'resm',
113        kWhatOpenAudioSink       = 'opnA',
114        kWhatCloseAudioSink      = 'clsA',
115        kWhatStopAudioSink       = 'stpA',
116        kWhatDisableOffloadAudio = 'noOA',
117        kWhatEnableOffloadAudio  = 'enOA',
118        kWhatSetVideoFrameRate   = 'sVFR',
119    };
120
121    struct QueueEntry {
122        sp<ABuffer> mBuffer;
123        sp<AMessage> mNotifyConsumed;
124        size_t mOffset;
125        status_t mFinalResult;
126        int32_t mBufferOrdinal;
127    };
128
129    static const int64_t kMinPositionUpdateDelayUs;
130
131    sp<MediaPlayerBase::AudioSink> mAudioSink;
132    sp<AMessage> mNotify;
133    Mutex mLock;
134    uint32_t mFlags;
135    List<QueueEntry> mAudioQueue;
136    List<QueueEntry> mVideoQueue;
137    uint32_t mNumFramesWritten;
138    sp<VideoFrameScheduler> mVideoScheduler;
139
140    bool mDrainAudioQueuePending;
141    bool mDrainVideoQueuePending;
142    int32_t mAudioQueueGeneration;
143    int32_t mVideoQueueGeneration;
144
145    Mutex mTimeLock;
146    // |mTimeLock| protects the following 7 member vars that are related to time.
147    // Note: those members are only written on Renderer thread, so reading on Renderer thread
148    // doesn't need to be protected. Otherwise accessing those members must be protected by
149    // |mTimeLock|.
150    // TODO: move those members to a seperated media clock class.
151    int64_t mAudioFirstAnchorTimeMediaUs;
152    int64_t mAnchorTimeMediaUs;
153    int64_t mAnchorTimeRealUs;
154    int64_t mAnchorNumFramesWritten;
155    int64_t mAnchorMaxMediaUs;
156    int64_t mVideoLateByUs;
157    bool mHasAudio;
158    bool mHasVideo;
159    int64_t mPauseStartedTimeRealUs;
160
161    Mutex mFlushLock;  // protects the following 2 member vars.
162    bool mFlushingAudio;
163    bool mFlushingVideo;
164    bool mNotifyCompleteAudio;
165    bool mNotifyCompleteVideo;
166
167    bool mSyncQueues;
168
169    // modified on only renderer's thread.
170    bool mPaused;
171    int64_t mPausePositionMediaTimeUs;
172
173    bool mVideoSampleReceived;
174    bool mVideoRenderingStarted;
175    int32_t mVideoRenderingStartGeneration;
176    int32_t mAudioRenderingStartGeneration;
177
178    int64_t mLastPositionUpdateUs;
179
180    int32_t mAudioOffloadPauseTimeoutGeneration;
181    bool mAudioOffloadTornDown;
182    audio_offload_info_t mCurrentOffloadInfo;
183
184    int32_t mTotalBuffersQueued;
185    int32_t mLastAudioBufferDrained;
186
187    status_t getCurrentPositionOnLooper(int64_t *mediaUs);
188    status_t getCurrentPositionOnLooper(
189            int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
190    bool getCurrentPositionIfPaused_l(int64_t *mediaUs);
191    status_t getCurrentPositionFromAnchor(
192            int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
193
194    size_t fillAudioBuffer(void *buffer, size_t size);
195
196    bool onDrainAudioQueue();
197    int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
198    int64_t getPlayedOutAudioDurationUs(int64_t nowUs);
199    void postDrainAudioQueue_l(int64_t delayUs = 0);
200
201    void onNewAudioMediaTime(int64_t mediaTimeUs);
202    int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs);
203
204    void onDrainVideoQueue();
205    void postDrainVideoQueue();
206
207    void prepareForMediaRenderingStart();
208    void notifyIfMediaRenderingStarted();
209
210    void onQueueBuffer(const sp<AMessage> &msg);
211    void onQueueEOS(const sp<AMessage> &msg);
212    void onFlush(const sp<AMessage> &msg);
213    void onAudioSinkChanged();
214    void onDisableOffloadAudio();
215    void onEnableOffloadAudio();
216    void onPause();
217    void onResume();
218    void onSetVideoFrameRate(float fps);
219    void onAudioOffloadTearDown(AudioOffloadTearDownReason reason);
220    status_t onOpenAudioSink(
221            const sp<AMessage> &format,
222            bool offloadOnly,
223            bool hasVideo,
224            uint32_t flags);
225    void onCloseAudioSink();
226
227    void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
228    void notifyFlushComplete(bool audio);
229    void notifyPosition();
230    void notifyVideoLateBy(int64_t lateByUs);
231    void notifyVideoRenderingStart();
232    void notifyAudioOffloadTearDown();
233
234    void flushQueue(List<QueueEntry> *queue);
235    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
236    void syncQueuesDone_l();
237
238    bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
239
240    void startAudioOffloadPauseTimeout();
241    void cancelAudioOffloadPauseTimeout();
242
243    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
244};
245
246}  // namespace android
247
248#endif  // NUPLAYER_RENDERER_H_
249