NuPlayerRenderer.h revision 3a2956d148d81194e297408179e84a47a309ef48
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 mFirstAudioTimeUs;
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 mVideoRenderingStarted;
123    int32_t mVideoRenderingStartGeneration;
124    int32_t mAudioRenderingStartGeneration;
125
126    int64_t mLastPositionUpdateUs;
127    int64_t mVideoLateByUs;
128
129    size_t fillAudioBuffer(void *buffer, size_t size);
130
131    bool onDrainAudioQueue();
132    void postDrainAudioQueue_l(int64_t delayUs = 0);
133
134    void onDrainVideoQueue();
135    void postDrainVideoQueue();
136
137    void prepareForMediaRenderingStart();
138    void notifyIfMediaRenderingStarted();
139
140    void onQueueBuffer(const sp<AMessage> &msg);
141    void onQueueEOS(const sp<AMessage> &msg);
142    void onFlush(const sp<AMessage> &msg);
143    void onAudioSinkChanged();
144    void onDisableOffloadAudio();
145    void onPause();
146    void onResume();
147    void onAudioOffloadTearDown();
148
149    void notifyEOS(bool audio, status_t finalResult);
150    void notifyFlushComplete(bool audio);
151    void notifyPosition();
152    void notifyVideoLateBy(int64_t lateByUs);
153    void notifyVideoRenderingStart();
154    void notifyAudioOffloadTearDown();
155
156    void flushQueue(List<QueueEntry> *queue);
157    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
158    void syncQueuesDone_l();
159
160    bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
161
162    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
163};
164
165}  // namespace android
166
167#endif  // NUPLAYER_RENDERER_H_
168