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