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