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