NuPlayerRenderer.h revision 231406d597cca1c9c009f870fbb62e46b8475186
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    struct PcmInfo {
185        audio_channel_mask_t mChannelMask;
186        audio_output_flags_t mFlags;
187        audio_format_t mFormat;
188        int32_t mNumChannels;
189        int32_t mSampleRate;
190    };
191    PcmInfo mCurrentPcmInfo;
192    static const PcmInfo AUDIO_PCMINFO_INITIALIZER;
193
194    int32_t mTotalBuffersQueued;
195    int32_t mLastAudioBufferDrained;
196
197    status_t getCurrentPositionOnLooper(int64_t *mediaUs);
198    status_t getCurrentPositionOnLooper(
199            int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
200    bool getCurrentPositionIfPaused_l(int64_t *mediaUs);
201    status_t getCurrentPositionFromAnchor(
202            int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
203
204    size_t fillAudioBuffer(void *buffer, size_t size);
205
206    bool onDrainAudioQueue();
207    int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
208    int64_t getPlayedOutAudioDurationUs(int64_t nowUs);
209    void postDrainAudioQueue_l(int64_t delayUs = 0);
210
211    void onNewAudioMediaTime(int64_t mediaTimeUs);
212    int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs);
213
214    void onDrainVideoQueue();
215    void postDrainVideoQueue_l();
216
217    void prepareForMediaRenderingStart();
218    void notifyIfMediaRenderingStarted();
219
220    void onQueueBuffer(const sp<AMessage> &msg);
221    void onQueueEOS(const sp<AMessage> &msg);
222    void onFlush(const sp<AMessage> &msg);
223    void onAudioSinkChanged();
224    void onDisableOffloadAudio();
225    void onEnableOffloadAudio();
226    void onPause();
227    void onResume();
228    void onSetVideoFrameRate(float fps);
229    void onAudioOffloadTearDown(AudioOffloadTearDownReason reason);
230    status_t onOpenAudioSink(
231            const sp<AMessage> &format,
232            bool offloadOnly,
233            bool hasVideo,
234            uint32_t flags);
235    void onCloseAudioSink();
236
237    void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
238    void notifyFlushComplete(bool audio);
239    void notifyPosition();
240    void notifyVideoLateBy(int64_t lateByUs);
241    void notifyVideoRenderingStart();
242    void notifyAudioOffloadTearDown();
243
244    void flushQueue(List<QueueEntry> *queue);
245    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
246    void syncQueuesDone_l();
247
248    bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
249
250    void startAudioOffloadPauseTimeout();
251    void cancelAudioOffloadPauseTimeout();
252
253    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
254};
255
256}  // namespace android
257
258#endif  // NUPLAYER_RENDERER_H_
259