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    Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
29             const sp<AMessage> &notify);
30
31    void queueBuffer(
32            bool audio,
33            const sp<ABuffer> &buffer,
34            const sp<AMessage> &notifyConsumed);
35
36    void queueEOS(bool audio, status_t finalResult);
37
38    void flush(bool audio);
39
40    void signalTimeDiscontinuity();
41
42    void signalAudioSinkChanged();
43
44    void pause();
45    void resume();
46
47    enum {
48        kWhatEOS                 = 'eos ',
49        kWhatFlushComplete       = 'fluC',
50        kWhatPosition            = 'posi',
51        kWhatVideoRenderingStart = 'vdrd',
52    };
53
54protected:
55    virtual ~Renderer();
56
57    virtual void onMessageReceived(const sp<AMessage> &msg);
58
59private:
60    enum {
61        kWhatDrainAudioQueue    = 'draA',
62        kWhatDrainVideoQueue    = 'draV',
63        kWhatQueueBuffer        = 'queB',
64        kWhatQueueEOS           = 'qEOS',
65        kWhatFlush              = 'flus',
66        kWhatAudioSinkChanged   = 'auSC',
67        kWhatPause              = 'paus',
68        kWhatResume             = 'resm',
69    };
70
71    struct QueueEntry {
72        sp<ABuffer> mBuffer;
73        sp<AMessage> mNotifyConsumed;
74        size_t mOffset;
75        status_t mFinalResult;
76    };
77
78    static const int64_t kMinPositionUpdateDelayUs;
79
80    sp<MediaPlayerBase::AudioSink> mAudioSink;
81    sp<AMessage> mNotify;
82    List<QueueEntry> mAudioQueue;
83    List<QueueEntry> mVideoQueue;
84    uint32_t mNumFramesWritten;
85
86    bool mDrainAudioQueuePending;
87    bool mDrainVideoQueuePending;
88    int32_t mAudioQueueGeneration;
89    int32_t mVideoQueueGeneration;
90
91    int64_t mAnchorTimeMediaUs;
92    int64_t mAnchorTimeRealUs;
93
94    Mutex mFlushLock;  // protects the following 2 member vars.
95    bool mFlushingAudio;
96    bool mFlushingVideo;
97
98    bool mHasAudio;
99    bool mHasVideo;
100    bool mSyncQueues;
101
102    bool mPaused;
103    bool mVideoRenderingStarted;
104
105    int64_t mLastPositionUpdateUs;
106    int64_t mVideoLateByUs;
107
108    bool onDrainAudioQueue();
109    void postDrainAudioQueue(int64_t delayUs = 0);
110
111    void onDrainVideoQueue();
112    void postDrainVideoQueue();
113
114    void onQueueBuffer(const sp<AMessage> &msg);
115    void onQueueEOS(const sp<AMessage> &msg);
116    void onFlush(const sp<AMessage> &msg);
117    void onAudioSinkChanged();
118    void onPause();
119    void onResume();
120
121    void notifyEOS(bool audio, status_t finalResult);
122    void notifyFlushComplete(bool audio);
123    void notifyPosition();
124    void notifyVideoLateBy(int64_t lateByUs);
125    void notifyVideoRenderingStart();
126
127    void flushQueue(List<QueueEntry> *queue);
128    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
129    void syncQueuesDone();
130
131    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
132};
133
134}  // namespace android
135
136#endif  // NUPLAYER_RENDERER_H_
137