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 
23 namespace android {
24 
25 struct ABuffer;
26 class  AWakeLock;
27 struct VideoFrameScheduler;
28 
29 struct NuPlayer::Renderer : public AHandler {
30     enum Flags {
31         FLAG_REAL_TIME = 1,
32         FLAG_OFFLOAD_AUDIO = 2,
33     };
34     Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
35              const sp<AMessage> &notify,
36              uint32_t flags = 0);
37 
38     static size_t AudioSinkCallback(
39             MediaPlayerBase::AudioSink *audioSink,
40             void *data, size_t size, void *me,
41             MediaPlayerBase::AudioSink::cb_event_t event);
42 
43     void queueBuffer(
44             bool audio,
45             const sp<ABuffer> &buffer,
46             const sp<AMessage> &notifyConsumed);
47 
48     void queueEOS(bool audio, status_t finalResult);
49 
50     void flush(bool audio, bool notifyComplete);
51 
52     void signalTimeDiscontinuity();
53 
54     void signalAudioSinkChanged();
55 
56     void signalDisableOffloadAudio();
57     void signalEnableOffloadAudio();
58 
59     void pause();
60     void resume();
61 
62     void setVideoFrameRate(float fps);
63 
64     // Following setters and getters are protected by mTimeLock.
65     status_t getCurrentPosition(int64_t *mediaUs);
66     void setHasMedia(bool audio);
67     void setAudioFirstAnchorTime(int64_t mediaUs);
68     void setAudioFirstAnchorTimeIfNeeded(int64_t mediaUs);
69     void setAnchorTime(
70             int64_t mediaUs, int64_t realUs, int64_t numFramesWritten = -1, bool resume = false);
71     void setVideoLateByUs(int64_t lateUs);
72     int64_t getVideoLateByUs();
73     void setPauseStartedTimeRealUs(int64_t realUs);
74 
75     status_t openAudioSink(
76             const sp<AMessage> &format,
77             bool offloadOnly,
78             bool hasVideo,
79             uint32_t flags,
80             bool *isOffloaded);
81     void closeAudioSink();
82 
83     enum {
84         kWhatEOS                 = 'eos ',
85         kWhatFlushComplete       = 'fluC',
86         kWhatPosition            = 'posi',
87         kWhatVideoRenderingStart = 'vdrd',
88         kWhatMediaRenderingStart = 'mdrd',
89         kWhatAudioOffloadTearDown = 'aOTD',
90         kWhatAudioOffloadPauseTimeout = 'aOPT',
91     };
92 
93     enum AudioOffloadTearDownReason {
94         kDueToError = 0,
95         kDueToTimeout,
96     };
97 
98 protected:
99     virtual ~Renderer();
100 
101     virtual void onMessageReceived(const sp<AMessage> &msg);
102 
103 private:
104     enum {
105         kWhatDrainAudioQueue     = 'draA',
106         kWhatDrainVideoQueue     = 'draV',
107         kWhatPostDrainVideoQueue = 'pDVQ',
108         kWhatQueueBuffer         = 'queB',
109         kWhatQueueEOS            = 'qEOS',
110         kWhatFlush               = 'flus',
111         kWhatAudioSinkChanged    = 'auSC',
112         kWhatPause               = 'paus',
113         kWhatResume              = 'resm',
114         kWhatOpenAudioSink       = 'opnA',
115         kWhatCloseAudioSink      = 'clsA',
116         kWhatStopAudioSink       = 'stpA',
117         kWhatDisableOffloadAudio = 'noOA',
118         kWhatEnableOffloadAudio  = 'enOA',
119         kWhatSetVideoFrameRate   = 'sVFR',
120     };
121 
122     struct QueueEntry {
123         sp<ABuffer> mBuffer;
124         sp<AMessage> mNotifyConsumed;
125         size_t mOffset;
126         status_t mFinalResult;
127         int32_t mBufferOrdinal;
128     };
129 
130     static const int64_t kMinPositionUpdateDelayUs;
131 
132     sp<MediaPlayerBase::AudioSink> mAudioSink;
133     sp<AMessage> mNotify;
134     Mutex mLock;
135     uint32_t mFlags;
136     List<QueueEntry> mAudioQueue;
137     List<QueueEntry> mVideoQueue;
138     uint32_t mNumFramesWritten;
139     sp<VideoFrameScheduler> mVideoScheduler;
140 
141     bool mDrainAudioQueuePending;
142     bool mDrainVideoQueuePending;
143     int32_t mAudioQueueGeneration;
144     int32_t mVideoQueueGeneration;
145 
146     Mutex mTimeLock;
147     // |mTimeLock| protects the following 7 member vars that are related to time.
148     // Note: those members are only written on Renderer thread, so reading on Renderer thread
149     // doesn't need to be protected. Otherwise accessing those members must be protected by
150     // |mTimeLock|.
151     // TODO: move those members to a seperated media clock class.
152     int64_t mAudioFirstAnchorTimeMediaUs;
153     int64_t mAnchorTimeMediaUs;
154     int64_t mAnchorTimeRealUs;
155     int64_t mAnchorNumFramesWritten;
156     int64_t mAnchorMaxMediaUs;
157     int64_t mVideoLateByUs;
158     bool mHasAudio;
159     bool mHasVideo;
160     int64_t mPauseStartedTimeRealUs;
161 
162     Mutex mFlushLock;  // protects the following 2 member vars.
163     bool mFlushingAudio;
164     bool mFlushingVideo;
165     bool mNotifyCompleteAudio;
166     bool mNotifyCompleteVideo;
167 
168     bool mSyncQueues;
169 
170     // modified on only renderer's thread.
171     bool mPaused;
172     int64_t mPausePositionMediaTimeUs;
173 
174     bool mVideoSampleReceived;
175     bool mVideoRenderingStarted;
176     int32_t mVideoRenderingStartGeneration;
177     int32_t mAudioRenderingStartGeneration;
178 
179     int64_t mLastPositionUpdateUs;
180 
181     int32_t mAudioOffloadPauseTimeoutGeneration;
182     bool mAudioOffloadTornDown;
183     audio_offload_info_t mCurrentOffloadInfo;
184 
185     struct PcmInfo {
186         audio_channel_mask_t mChannelMask;
187         audio_output_flags_t mFlags;
188         audio_format_t mFormat;
189         int32_t mNumChannels;
190         int32_t mSampleRate;
191     };
192     PcmInfo mCurrentPcmInfo;
193     static const PcmInfo AUDIO_PCMINFO_INITIALIZER;
194 
195     int32_t mTotalBuffersQueued;
196     int32_t mLastAudioBufferDrained;
197 
198     sp<AWakeLock> mWakeLock;
199 
200     status_t getCurrentPositionOnLooper(int64_t *mediaUs);
201     status_t getCurrentPositionOnLooper(
202             int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
203     bool getCurrentPositionIfPaused_l(int64_t *mediaUs);
204     status_t getCurrentPositionFromAnchor(
205             int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
206 
207     size_t fillAudioBuffer(void *buffer, size_t size);
208 
209     bool onDrainAudioQueue();
210     int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
211     int64_t getPlayedOutAudioDurationUs(int64_t nowUs);
212     void postDrainAudioQueue_l(int64_t delayUs = 0);
213 
214     void onNewAudioMediaTime(int64_t mediaTimeUs);
215     int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs);
216 
217     void onDrainVideoQueue();
218     void postDrainVideoQueue_l();
219 
220     void prepareForMediaRenderingStart();
221     void notifyIfMediaRenderingStarted();
222 
223     void onQueueBuffer(const sp<AMessage> &msg);
224     void onQueueEOS(const sp<AMessage> &msg);
225     void onFlush(const sp<AMessage> &msg);
226     void onAudioSinkChanged();
227     void onDisableOffloadAudio();
228     void onEnableOffloadAudio();
229     void onPause();
230     void onResume();
231     void onSetVideoFrameRate(float fps);
232     void onAudioOffloadTearDown(AudioOffloadTearDownReason reason);
233     status_t onOpenAudioSink(
234             const sp<AMessage> &format,
235             bool offloadOnly,
236             bool hasVideo,
237             uint32_t flags);
238     void onCloseAudioSink();
239 
240     void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
241     void notifyFlushComplete(bool audio);
242     void notifyPosition();
243     void notifyVideoLateBy(int64_t lateByUs);
244     void notifyVideoRenderingStart();
245     void notifyAudioOffloadTearDown();
246 
247     void flushQueue(List<QueueEntry> *queue);
248     bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
249     void syncQueuesDone_l();
250 
offloadingAudioRenderer251     bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
252 
253     void startAudioOffloadPauseTimeout();
254     void cancelAudioOffloadPauseTimeout();
255 
256     DISALLOW_EVIL_CONSTRUCTORS(Renderer);
257 };
258 
259 }  // namespace android
260 
261 #endif  // NUPLAYER_RENDERER_H_
262