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