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