NuPlayerRenderer.h revision 49966fff32b27f8821ebe280f25688b3c4f5f73f
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    enum {
63        kWhatEOS                 = 'eos ',
64        kWhatFlushComplete       = 'fluC',
65        kWhatPosition            = 'posi',
66        kWhatVideoRenderingStart = 'vdrd',
67        kWhatMediaRenderingStart = 'mdrd',
68        kWhatAudioOffloadTearDown = 'aOTD',
69        kWhatAudioOffloadPauseTimeout = 'aOPT',
70    };
71
72    enum AudioOffloadTearDownReason {
73        kDueToError = 0,
74        kDueToTimeout,
75    };
76
77protected:
78    virtual ~Renderer();
79
80    virtual void onMessageReceived(const sp<AMessage> &msg);
81
82private:
83    enum {
84        kWhatDrainAudioQueue     = 'draA',
85        kWhatDrainVideoQueue     = 'draV',
86        kWhatQueueBuffer         = 'queB',
87        kWhatQueueEOS            = 'qEOS',
88        kWhatFlush               = 'flus',
89        kWhatAudioSinkChanged    = 'auSC',
90        kWhatPause               = 'paus',
91        kWhatResume              = 'resm',
92        kWhatStopAudioSink       = 'stpA',
93        kWhatDisableOffloadAudio = 'noOA',
94        kWhatSetVideoFrameRate   = 'sVFR',
95    };
96
97    struct QueueEntry {
98        sp<ABuffer> mBuffer;
99        sp<AMessage> mNotifyConsumed;
100        size_t mOffset;
101        status_t mFinalResult;
102    };
103
104    static const int64_t kMinPositionUpdateDelayUs;
105
106    sp<MediaPlayerBase::AudioSink> mAudioSink;
107    sp<AMessage> mNotify;
108    Mutex mLock;
109    uint32_t mFlags;
110    List<QueueEntry> mAudioQueue;
111    List<QueueEntry> mVideoQueue;
112    uint32_t mNumFramesWritten;
113    sp<VideoFrameScheduler> mVideoScheduler;
114
115    bool mDrainAudioQueuePending;
116    bool mDrainVideoQueuePending;
117    int32_t mAudioQueueGeneration;
118    int32_t mVideoQueueGeneration;
119
120    int64_t mFirstAnchorTimeMediaUs;
121    int64_t mAnchorTimeMediaUs;
122    int64_t mAnchorTimeRealUs;
123
124    Mutex mFlushLock;  // protects the following 2 member vars.
125    bool mFlushingAudio;
126    bool mFlushingVideo;
127
128    bool mHasAudio;
129    bool mHasVideo;
130    bool mSyncQueues;
131
132    bool mPaused;
133    int64_t mPauseStartedTimeRealUs;
134    bool mVideoSampleReceived;
135    bool mVideoRenderingStarted;
136    int32_t mVideoRenderingStartGeneration;
137    int32_t mAudioRenderingStartGeneration;
138
139    int64_t mLastPositionUpdateUs;
140    int64_t mVideoLateByUs;
141
142    int32_t mAudioOffloadPauseTimeoutGeneration;
143    bool mAudioOffloadTornDown;
144
145    size_t fillAudioBuffer(void *buffer, size_t size);
146
147    bool onDrainAudioQueue();
148    int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
149    int64_t getPlayedOutAudioDurationUs(int64_t nowUs);
150    void postDrainAudioQueue_l(int64_t delayUs = 0);
151
152    void onDrainVideoQueue();
153    void postDrainVideoQueue();
154
155    void prepareForMediaRenderingStart();
156    void notifyIfMediaRenderingStarted();
157
158    void onQueueBuffer(const sp<AMessage> &msg);
159    void onQueueEOS(const sp<AMessage> &msg);
160    void onFlush(const sp<AMessage> &msg);
161    void onAudioSinkChanged();
162    void onDisableOffloadAudio();
163    void onPause();
164    void onResume();
165    void onSetVideoFrameRate(float fps);
166    void onAudioOffloadTearDown(AudioOffloadTearDownReason reason);
167
168    void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
169    void notifyFlushComplete(bool audio);
170    void notifyPosition();
171    void notifyVideoLateBy(int64_t lateByUs);
172    void notifyVideoRenderingStart();
173    void notifyAudioOffloadTearDown();
174
175    void flushQueue(List<QueueEntry> *queue);
176    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
177    void syncQueuesDone_l();
178
179    bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
180
181    void startAudioOffloadPauseTimeout();
182    void cancelAudioOffloadPauseTimeout();
183
184    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
185};
186
187}  // namespace android
188
189#endif  // NUPLAYER_RENDERER_H_
190