NuPlayerRenderer.h revision a73d9e0b3d171d2bfcd9eb07df9d6d36ae74df57
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;
26struct VideoFrameScheduler;
27
28struct NuPlayer::Renderer : public AHandler {
29    enum Flags {
30        FLAG_REAL_TIME = 1,
31        FLAG_OFFLOAD_AUDIO = 2,
32    };
33    Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
34             const sp<AMessage> &notify,
35             uint32_t flags = 0);
36
37    static size_t AudioSinkCallback(
38            MediaPlayerBase::AudioSink *audioSink,
39            void *data, size_t size, void *me,
40            MediaPlayerBase::AudioSink::cb_event_t event);
41
42    void queueBuffer(
43            bool audio,
44            const sp<ABuffer> &buffer,
45            const sp<AMessage> &notifyConsumed);
46
47    void queueEOS(bool audio, status_t finalResult);
48
49    void flush(bool audio);
50
51    void signalTimeDiscontinuity();
52
53    void signalAudioSinkChanged();
54
55    void signalDisableOffloadAudio();
56
57    void pause();
58    void resume();
59
60    void setVideoFrameRate(float fps);
61
62    // Following setters and getters are protected by mTimeLock.
63    status_t getCurrentPosition(int64_t *mediaUs);
64    status_t getCurrentPosition(int64_t *mediaUs, int64_t nowUs);
65    void setHasMedia(bool audio);
66    void setAudioFirstAnchorTime(int64_t mediaUs);
67    void setAudioFirstAnchorTimeIfNeeded(int64_t mediaUs);
68    void setVideoAnchorTime(int64_t mediaUs, int64_t realUs);
69    void setVideoLateByUs(int64_t lateUs);
70    int64_t getVideoLateByUs();
71    void setPauseStartedTimeRealUs(int64_t realUs);
72
73    enum {
74        kWhatEOS                 = 'eos ',
75        kWhatFlushComplete       = 'fluC',
76        kWhatPosition            = 'posi',
77        kWhatVideoRenderingStart = 'vdrd',
78        kWhatMediaRenderingStart = 'mdrd',
79        kWhatAudioOffloadTearDown = 'aOTD',
80        kWhatAudioOffloadPauseTimeout = 'aOPT',
81    };
82
83    enum AudioOffloadTearDownReason {
84        kDueToError = 0,
85        kDueToTimeout,
86    };
87
88protected:
89    virtual ~Renderer();
90
91    virtual void onMessageReceived(const sp<AMessage> &msg);
92
93private:
94    enum {
95        kWhatDrainAudioQueue     = 'draA',
96        kWhatDrainVideoQueue     = 'draV',
97        kWhatQueueBuffer         = 'queB',
98        kWhatQueueEOS            = 'qEOS',
99        kWhatFlush               = 'flus',
100        kWhatAudioSinkChanged    = 'auSC',
101        kWhatPause               = 'paus',
102        kWhatResume              = 'resm',
103        kWhatStopAudioSink       = 'stpA',
104        kWhatDisableOffloadAudio = 'noOA',
105        kWhatSetVideoFrameRate   = 'sVFR',
106    };
107
108    struct QueueEntry {
109        sp<ABuffer> mBuffer;
110        sp<AMessage> mNotifyConsumed;
111        size_t mOffset;
112        status_t mFinalResult;
113    };
114
115    static const int64_t kMinPositionUpdateDelayUs;
116
117    sp<MediaPlayerBase::AudioSink> mAudioSink;
118    sp<AMessage> mNotify;
119    Mutex mLock;
120    uint32_t mFlags;
121    List<QueueEntry> mAudioQueue;
122    List<QueueEntry> mVideoQueue;
123    uint32_t mNumFramesWritten;
124    sp<VideoFrameScheduler> mVideoScheduler;
125
126    bool mDrainAudioQueuePending;
127    bool mDrainVideoQueuePending;
128    int32_t mAudioQueueGeneration;
129    int32_t mVideoQueueGeneration;
130
131    Mutex mTimeLock;
132    // |mTimeLock| protects the following 7 member vars that are related to time.
133    // Note: those members are only written on Renderer thread, so reading on Renderer thread
134    // doesn't need to be protected. Otherwise accessing those members must be protected by
135    // |mTimeLock|.
136    // TODO: move those members to a seperated media clock class.
137    int64_t mAudioFirstAnchorTimeMediaUs;
138    int64_t mVideoAnchorTimeMediaUs;
139    int64_t mVideoAnchorTimeRealUs;
140    int64_t mVideoLateByUs;
141    bool mHasAudio;
142    bool mHasVideo;
143    int64_t mPauseStartedTimeRealUs;
144
145    Mutex mFlushLock;  // protects the following 2 member vars.
146    bool mFlushingAudio;
147    bool mFlushingVideo;
148
149    bool mSyncQueues;
150
151    bool mPaused;
152    bool mVideoSampleReceived;
153    bool mVideoRenderingStarted;
154    int32_t mVideoRenderingStartGeneration;
155    int32_t mAudioRenderingStartGeneration;
156
157    int64_t mLastPositionUpdateUs;
158
159    int32_t mAudioOffloadPauseTimeoutGeneration;
160    bool mAudioOffloadTornDown;
161
162    size_t fillAudioBuffer(void *buffer, size_t size);
163
164    bool onDrainAudioQueue();
165    int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
166    int64_t getPlayedOutAudioDurationUs(int64_t nowUs);
167    void postDrainAudioQueue_l(int64_t delayUs = 0);
168
169    int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs);
170
171    void onDrainVideoQueue();
172    void postDrainVideoQueue();
173
174    void prepareForMediaRenderingStart();
175    void notifyIfMediaRenderingStarted();
176
177    void onQueueBuffer(const sp<AMessage> &msg);
178    void onQueueEOS(const sp<AMessage> &msg);
179    void onFlush(const sp<AMessage> &msg);
180    void onAudioSinkChanged();
181    void onDisableOffloadAudio();
182    void onPause();
183    void onResume();
184    void onSetVideoFrameRate(float fps);
185    void onAudioOffloadTearDown(AudioOffloadTearDownReason reason);
186
187    void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
188    void notifyFlushComplete(bool audio);
189    void notifyPosition();
190    void notifyVideoLateBy(int64_t lateByUs);
191    void notifyVideoRenderingStart();
192    void notifyAudioOffloadTearDown();
193
194    void flushQueue(List<QueueEntry> *queue);
195    bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
196    void syncQueuesDone_l();
197
198    bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
199
200    void startAudioOffloadPauseTimeout();
201    void cancelAudioOffloadPauseTimeout();
202
203    DISALLOW_EVIL_CONSTRUCTORS(Renderer);
204};
205
206}  // namespace android
207
208#endif  // NUPLAYER_RENDERER_H_
209