NuPlayerRenderer.h revision 714aa7b7c52ce07d5fb44870c0853b4d8e5a758e
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,
49        kWhatFlushComplete,
50        kWhatPosition,
51    };
52
53protected:
54    virtual ~Renderer();
55
56    virtual void onMessageReceived(const sp<AMessage> &msg);
57
58private:
59    enum {
60        kWhatDrainAudioQueue,
61        kWhatDrainVideoQueue,
62        kWhatQueueBuffer,
63        kWhatQueueEOS,
64        kWhatFlush,
65        kWhatAudioSinkChanged,
66        kWhatPause,
67        kWhatResume,
68    };
69
70    struct QueueEntry {
71        sp<ABuffer> mBuffer;
72        sp<AMessage> mNotifyConsumed;
73        size_t mOffset;
74        status_t mFinalResult;
75    };
76
77    static const int64_t kMinPositionUpdateDelayUs;
78
79    sp<MediaPlayerBase::AudioSink> mAudioSink;
80    sp<AMessage> mNotify;
81    List<QueueEntry> mAudioQueue;
82    List<QueueEntry> mVideoQueue;
83    uint32_t mNumFramesWritten;
84
85    bool mDrainAudioQueuePending;
86    bool mDrainVideoQueuePending;
87    int32_t mAudioQueueGeneration;
88    int32_t mVideoQueueGeneration;
89
90    int64_t mAnchorTimeMediaUs;
91    int64_t mAnchorTimeRealUs;
92
93    Mutex mFlushLock;  // protects the following 2 member vars.
94    bool mFlushingAudio;
95    bool mFlushingVideo;
96
97    bool mHasAudio;
98    bool mHasVideo;
99    bool mSyncQueues;
100
101    bool mPaused;
102
103    int64_t mLastPositionUpdateUs;
104
105    void onDrainAudioQueue();
106    void postDrainAudioQueue();
107
108    void onDrainVideoQueue();
109    void postDrainVideoQueue();
110
111    void onQueueBuffer(const sp<AMessage> &msg);
112    void onQueueEOS(const sp<AMessage> &msg);
113    void onFlush(const sp<AMessage> &msg);
114    void onAudioSinkChanged();
115    void onPause();
116    void onResume();
117
118    void notifyEOS(bool audio, status_t finalResult);
119    void notifyFlushComplete(bool audio);
120    void notifyPosition();
121
122    void flushQueue(List<QueueEntry> *queue);
123    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
124    void syncQueuesDone();
125
126    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
127};
128
129}  // namespace android
130
131#endif  // NUPLAYER_RENDERER_H_
132