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