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 <media/AudioResamplerPublic.h>
22 #include <media/AVSyncSettings.h>
23 
24 #include "NuPlayer.h"
25 
26 namespace android {
27 
28 struct ABuffer;
29 class  AWakeLock;
30 struct MediaClock;
31 struct VideoFrameScheduler;
32 
33 struct NuPlayer::Renderer : public AHandler {
34     enum Flags {
35         FLAG_REAL_TIME = 1,
36         FLAG_OFFLOAD_AUDIO = 2,
37     };
38     Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
39              const sp<AMessage> &notify,
40              uint32_t flags = 0);
41 
42     static size_t AudioSinkCallback(
43             MediaPlayerBase::AudioSink *audioSink,
44             void *data, size_t size, void *me,
45             MediaPlayerBase::AudioSink::cb_event_t event);
46 
47     void queueBuffer(
48             bool audio,
49             const sp<ABuffer> &buffer,
50             const sp<AMessage> &notifyConsumed);
51 
52     void queueEOS(bool audio, status_t finalResult);
53 
54     status_t setPlaybackSettings(const AudioPlaybackRate &rate /* sanitized */);
55     status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
56     status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
57     status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
58 
59     void flush(bool audio, bool notifyComplete);
60 
61     void signalTimeDiscontinuity();
62 
63     void signalAudioSinkChanged();
64 
65     void signalDisableOffloadAudio();
66     void signalEnableOffloadAudio();
67 
68     void pause();
69     void resume();
70 
71     void setVideoFrameRate(float fps);
72 
73     status_t getCurrentPosition(int64_t *mediaUs);
74     int64_t getVideoLateByUs();
75 
76     status_t openAudioSink(
77             const sp<AMessage> &format,
78             bool offloadOnly,
79             bool hasVideo,
80             uint32_t flags,
81             bool *isOffloaded);
82     void closeAudioSink();
83 
84     enum {
85         kWhatEOS                      = 'eos ',
86         kWhatFlushComplete            = 'fluC',
87         kWhatPosition                 = 'posi',
88         kWhatVideoRenderingStart      = 'vdrd',
89         kWhatMediaRenderingStart      = 'mdrd',
90         kWhatAudioTearDown            = 'adTD',
91         kWhatAudioOffloadPauseTimeout = 'aOPT',
92     };
93 
94     enum AudioTearDownReason {
95         kDueToError = 0,
96         kDueToTimeout,
97     };
98 
99 protected:
100     virtual ~Renderer();
101 
102     virtual void onMessageReceived(const sp<AMessage> &msg);
103 
104 private:
105     enum {
106         kWhatDrainAudioQueue     = 'draA',
107         kWhatDrainVideoQueue     = 'draV',
108         kWhatPostDrainVideoQueue = 'pDVQ',
109         kWhatQueueBuffer         = 'queB',
110         kWhatQueueEOS            = 'qEOS',
111         kWhatConfigPlayback      = 'cfPB',
112         kWhatConfigSync          = 'cfSy',
113         kWhatGetPlaybackSettings = 'gPbS',
114         kWhatGetSyncSettings     = 'gSyS',
115         kWhatFlush               = 'flus',
116         kWhatPause               = 'paus',
117         kWhatResume              = 'resm',
118         kWhatOpenAudioSink       = 'opnA',
119         kWhatCloseAudioSink      = 'clsA',
120         kWhatStopAudioSink       = 'stpA',
121         kWhatDisableOffloadAudio = 'noOA',
122         kWhatEnableOffloadAudio  = 'enOA',
123         kWhatSetVideoFrameRate   = 'sVFR',
124     };
125 
126     struct QueueEntry {
127         sp<ABuffer> mBuffer;
128         sp<AMessage> mNotifyConsumed;
129         size_t mOffset;
130         status_t mFinalResult;
131         int32_t mBufferOrdinal;
132     };
133 
134     static const int64_t kMinPositionUpdateDelayUs;
135 
136     sp<MediaPlayerBase::AudioSink> mAudioSink;
137     sp<AMessage> mNotify;
138     Mutex mLock;
139     uint32_t mFlags;
140     List<QueueEntry> mAudioQueue;
141     List<QueueEntry> mVideoQueue;
142     uint32_t mNumFramesWritten;
143     sp<VideoFrameScheduler> mVideoScheduler;
144 
145     bool mDrainAudioQueuePending;
146     bool mDrainVideoQueuePending;
147     int32_t mAudioQueueGeneration;
148     int32_t mVideoQueueGeneration;
149     int32_t mAudioDrainGeneration;
150     int32_t mVideoDrainGeneration;
151 
152     sp<MediaClock> mMediaClock;
153     float mPlaybackRate; // audio track rate
154 
155     AudioPlaybackRate mPlaybackSettings;
156     AVSyncSettings mSyncSettings;
157     float mVideoFpsHint;
158 
159     int64_t mAudioFirstAnchorTimeMediaUs;
160     int64_t mAnchorTimeMediaUs;
161     int64_t mAnchorNumFramesWritten;
162     int64_t mVideoLateByUs;
163     bool mHasAudio;
164     bool mHasVideo;
165 
166     bool mNotifyCompleteAudio;
167     bool mNotifyCompleteVideo;
168 
169     bool mSyncQueues;
170 
171     // modified on only renderer's thread.
172     bool mPaused;
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 mAudioTornDown;
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     bool mUseAudioCallback;
198 
199     sp<AWakeLock> mWakeLock;
200 
201     status_t getCurrentPositionOnLooper(int64_t *mediaUs);
202     status_t getCurrentPositionOnLooper(
203             int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
204     bool getCurrentPositionIfPaused_l(int64_t *mediaUs);
205     status_t getCurrentPositionFromAnchor(
206             int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
207 
208     size_t fillAudioBuffer(void *buffer, size_t size);
209 
210     bool onDrainAudioQueue();
211     void drainAudioQueueUntilLastEOS();
212     int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
213     int64_t getPlayedOutAudioDurationUs(int64_t nowUs);
214     void postDrainAudioQueue_l(int64_t delayUs = 0);
215 
216     void clearAnchorTime_l();
217     void clearAudioFirstAnchorTime_l();
218     void setAudioFirstAnchorTimeIfNeeded_l(int64_t mediaUs);
219     void setVideoLateByUs(int64_t lateUs);
220 
221     void onNewAudioMediaTime(int64_t mediaTimeUs);
222     int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs);
223 
224     void onDrainVideoQueue();
225     void postDrainVideoQueue();
226 
227     void prepareForMediaRenderingStart_l();
228     void notifyIfMediaRenderingStarted_l();
229 
230     void onQueueBuffer(const sp<AMessage> &msg);
231     void onQueueEOS(const sp<AMessage> &msg);
232     void onFlush(const sp<AMessage> &msg);
233     void onAudioSinkChanged();
234     void onDisableOffloadAudio();
235     void onEnableOffloadAudio();
236     status_t onConfigPlayback(const AudioPlaybackRate &rate /* sanitized */);
237     status_t onGetPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
238     status_t onConfigSync(const AVSyncSettings &sync, float videoFpsHint);
239     status_t onGetSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
240 
241     void onPause();
242     void onResume();
243     void onSetVideoFrameRate(float fps);
244     int32_t getQueueGeneration(bool audio);
245     int32_t getDrainGeneration(bool audio);
246     bool getSyncQueues();
247     void onAudioTearDown(AudioTearDownReason reason);
248     status_t onOpenAudioSink(
249             const sp<AMessage> &format,
250             bool offloadOnly,
251             bool hasVideo,
252             uint32_t flags);
253     void onCloseAudioSink();
254 
255     void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
256     void notifyFlushComplete(bool audio);
257     void notifyPosition();
258     void notifyVideoLateBy(int64_t lateByUs);
259     void notifyVideoRenderingStart();
260     void notifyAudioTearDown();
261 
262     void flushQueue(List<QueueEntry> *queue);
263     bool dropBufferIfStale(bool audio, const sp<AMessage> &msg);
264     void syncQueuesDone_l();
265 
offloadingAudioRenderer266     bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
267 
268     void startAudioOffloadPauseTimeout();
269     void cancelAudioOffloadPauseTimeout();
270 
271     int64_t getDurationUsIfPlayedAtSampleRate(uint32_t numFrames);
272 
273     DISALLOW_EVIL_CONSTRUCTORS(Renderer);
274 };
275 
276 } // namespace android
277 
278 #endif  // NUPLAYER_RENDERER_H_
279