NuPlayerRenderer.h revision dc43dfa1294470a4413c37e863ef3b621da8681f
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    enum {
61        kWhatEOS                 = 'eos ',
62        kWhatFlushComplete       = 'fluC',
63        kWhatPosition            = 'posi',
64        kWhatVideoRenderingStart = 'vdrd',
65        kWhatMediaRenderingStart = 'mdrd',
66        kWhatAudioOffloadTearDown = 'aOTD',
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    sp<VideoFrameScheduler> mVideoScheduler;
105
106    bool mDrainAudioQueuePending;
107    bool mDrainVideoQueuePending;
108    int32_t mAudioQueueGeneration;
109    int32_t mVideoQueueGeneration;
110
111    int64_t mFirstAnchorTimeMediaUs;
112    int64_t mAnchorTimeMediaUs;
113    int64_t mAnchorTimeRealUs;
114
115    Mutex mFlushLock;  // protects the following 2 member vars.
116    bool mFlushingAudio;
117    bool mFlushingVideo;
118
119    bool mHasAudio;
120    bool mHasVideo;
121    bool mSyncQueues;
122
123    bool mPaused;
124    bool mVideoSampleReceived;
125    bool mVideoRenderingStarted;
126    int32_t mVideoRenderingStartGeneration;
127    int32_t mAudioRenderingStartGeneration;
128
129    int64_t mLastPositionUpdateUs;
130    int64_t mVideoLateByUs;
131
132    size_t fillAudioBuffer(void *buffer, size_t size);
133
134    bool onDrainAudioQueue();
135    int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
136    int64_t getPlayedOutAudioDurationUs(int64_t nowUs);
137    void postDrainAudioQueue_l(int64_t delayUs = 0);
138
139    void onDrainVideoQueue();
140    void postDrainVideoQueue();
141
142    void prepareForMediaRenderingStart();
143    void notifyIfMediaRenderingStarted();
144
145    void onQueueBuffer(const sp<AMessage> &msg);
146    void onQueueEOS(const sp<AMessage> &msg);
147    void onFlush(const sp<AMessage> &msg);
148    void onAudioSinkChanged();
149    void onDisableOffloadAudio();
150    void onPause();
151    void onResume();
152    void onAudioOffloadTearDown();
153
154    void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
155    void notifyFlushComplete(bool audio);
156    void notifyPosition();
157    void notifyVideoLateBy(int64_t lateByUs);
158    void notifyVideoRenderingStart();
159    void notifyAudioOffloadTearDown();
160
161    void flushQueue(List<QueueEntry> *queue);
162    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
163    void syncQueuesDone_l();
164
165    bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
166
167    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
168};
169
170}  // namespace android
171
172#endif  // NUPLAYER_RENDERER_H_
173