NuPlayerRenderer.h revision f5b1db11734358d979a23a1ac4903872186ef60b
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;
26
27struct NuPlayer::Renderer : public AHandler {
28    enum Flags {
29        FLAG_REAL_TIME = 1,
30        FLAG_OFFLOAD_AUDIO = 2,
31    };
32    Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
33             const sp<AMessage> &notify,
34             uint32_t flags = 0);
35
36    static size_t AudioSinkCallback(
37            MediaPlayerBase::AudioSink *audioSink,
38            void *data, size_t size, void *me,
39            MediaPlayerBase::AudioSink::cb_event_t event);
40
41    void queueBuffer(
42            bool audio,
43            const sp<ABuffer> &buffer,
44            const sp<AMessage> &notifyConsumed);
45
46    void queueEOS(bool audio, status_t finalResult);
47
48    void flush(bool audio);
49
50    void signalTimeDiscontinuity();
51
52    void signalAudioSinkChanged();
53
54    void signalDisableOffloadAudio();
55
56    void pause();
57    void resume();
58
59    enum {
60        kWhatEOS                 = 'eos ',
61        kWhatFlushComplete       = 'fluC',
62        kWhatPosition            = 'posi',
63        kWhatVideoRenderingStart = 'vdrd',
64        kWhatMediaRenderingStart = 'mdrd',
65        kWhatAudioOffloadTearDown = 'aOTD',
66        kWhatAudioOffloadPauseTimeout = 'aOPT',
67    };
68
69protected:
70    virtual ~Renderer();
71
72    virtual void onMessageReceived(const sp<AMessage> &msg);
73
74private:
75    enum {
76        kWhatDrainAudioQueue     = 'draA',
77        kWhatDrainVideoQueue     = 'draV',
78        kWhatQueueBuffer         = 'queB',
79        kWhatQueueEOS            = 'qEOS',
80        kWhatFlush               = 'flus',
81        kWhatAudioSinkChanged    = 'auSC',
82        kWhatPause               = 'paus',
83        kWhatResume              = 'resm',
84        kWhatStopAudioSink       = 'stpA',
85        kWhatDisableOffloadAudio = 'noOA',
86    };
87
88    struct QueueEntry {
89        sp<ABuffer> mBuffer;
90        sp<AMessage> mNotifyConsumed;
91        size_t mOffset;
92        status_t mFinalResult;
93    };
94
95    static const int64_t kMinPositionUpdateDelayUs;
96
97    sp<MediaPlayerBase::AudioSink> mAudioSink;
98    sp<AMessage> mNotify;
99    Mutex mLock;
100    uint32_t mFlags;
101    List<QueueEntry> mAudioQueue;
102    List<QueueEntry> mVideoQueue;
103    uint32_t mNumFramesWritten;
104
105    bool mDrainAudioQueuePending;
106    bool mDrainVideoQueuePending;
107    int32_t mAudioQueueGeneration;
108    int32_t mVideoQueueGeneration;
109
110    int64_t mFirstAnchorTimeMediaUs;
111    int64_t mAnchorTimeMediaUs;
112    int64_t mAnchorTimeRealUs;
113
114    Mutex mFlushLock;  // protects the following 2 member vars.
115    bool mFlushingAudio;
116    bool mFlushingVideo;
117
118    bool mHasAudio;
119    bool mHasVideo;
120    bool mSyncQueues;
121
122    bool mPaused;
123    bool mVideoSampleReceived;
124    bool mVideoRenderingStarted;
125    int32_t mVideoRenderingStartGeneration;
126    int32_t mAudioRenderingStartGeneration;
127
128    int64_t mLastPositionUpdateUs;
129    int64_t mVideoLateByUs;
130
131    int32_t mAudioOffloadPauseTimeoutGeneration;
132    bool mAudioOffloadTornDown;
133
134    size_t fillAudioBuffer(void *buffer, size_t size);
135
136    bool onDrainAudioQueue();
137    int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
138    int64_t getPlayedOutAudioDurationUs(int64_t nowUs);
139    void postDrainAudioQueue_l(int64_t delayUs = 0);
140
141    void onDrainVideoQueue();
142    void postDrainVideoQueue();
143
144    void prepareForMediaRenderingStart();
145    void notifyIfMediaRenderingStarted();
146
147    void onQueueBuffer(const sp<AMessage> &msg);
148    void onQueueEOS(const sp<AMessage> &msg);
149    void onFlush(const sp<AMessage> &msg);
150    void onAudioSinkChanged();
151    void onDisableOffloadAudio();
152    void onPause();
153    void onResume();
154    void onAudioOffloadTearDown();
155
156    void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
157    void notifyFlushComplete(bool audio);
158    void notifyPosition();
159    void notifyVideoLateBy(int64_t lateByUs);
160    void notifyVideoRenderingStart();
161    void notifyAudioOffloadTearDown();
162
163    void flushQueue(List<QueueEntry> *queue);
164    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
165    void syncQueuesDone_l();
166
167    bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
168
169    void startAudioOffloadPauseTimeout();
170    void cancelAudioOffloadPauseTimeout();
171
172    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
173};
174
175}  // namespace android
176
177#endif  // NUPLAYER_RENDERER_H_
178