NuPlayerRenderer.h revision eecb7805bbbb712925d4372c505f8c7f5c4fb5ed
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
57    void pause();
58    void resume();
59
60    void setVideoFrameRate(float fps);
61
62    // Following setters and getters are protected by mTimeLock.
63    status_t getCurrentPosition(int64_t *mediaUs);
64    status_t getCurrentPosition(int64_t *mediaUs, int64_t nowUs);
65    void setHasMedia(bool audio);
66    void setAudioFirstAnchorTime(int64_t mediaUs);
67    void setAudioFirstAnchorTimeIfNeeded(int64_t mediaUs);
68    void setAnchorTime(int64_t mediaUs, int64_t realUs, bool resume = false);
69    void setVideoLateByUs(int64_t lateUs);
70    int64_t getVideoLateByUs();
71    void setPauseStartedTimeRealUs(int64_t realUs);
72
73    bool openAudioSink(
74            const sp<AMessage> &format,
75            bool offloadOnly,
76            bool hasVideo,
77            uint32_t flags);
78    void closeAudioSink();
79
80    enum {
81        kWhatEOS                 = 'eos ',
82        kWhatFlushComplete       = 'fluC',
83        kWhatPosition            = 'posi',
84        kWhatVideoRenderingStart = 'vdrd',
85        kWhatMediaRenderingStart = 'mdrd',
86        kWhatAudioOffloadTearDown = 'aOTD',
87        kWhatAudioOffloadPauseTimeout = 'aOPT',
88    };
89
90    enum AudioOffloadTearDownReason {
91        kDueToError = 0,
92        kDueToTimeout,
93    };
94
95protected:
96    virtual ~Renderer();
97
98    virtual void onMessageReceived(const sp<AMessage> &msg);
99
100private:
101    enum {
102        kWhatDrainAudioQueue     = 'draA',
103        kWhatDrainVideoQueue     = 'draV',
104        kWhatQueueBuffer         = 'queB',
105        kWhatQueueEOS            = 'qEOS',
106        kWhatFlush               = 'flus',
107        kWhatAudioSinkChanged    = 'auSC',
108        kWhatPause               = 'paus',
109        kWhatResume              = 'resm',
110        kWhatOpenAudioSink       = 'opnA',
111        kWhatCloseAudioSink      = 'clsA',
112        kWhatStopAudioSink       = 'stpA',
113        kWhatDisableOffloadAudio = 'noOA',
114        kWhatSetVideoFrameRate   = 'sVFR',
115    };
116
117    struct QueueEntry {
118        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