• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class  AWakeLock;
29 struct MediaClock;
30 class MediaCodecBuffer;
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<MediaCodecBuffer> &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 signalDisableOffloadAudio();
64     void signalEnableOffloadAudio();
65 
66     void pause();
67     void resume();
68 
69     void setVideoFrameRate(float fps);
70 
71     status_t getCurrentPosition(int64_t *mediaUs);
72     int64_t getVideoLateByUs();
73 
74     status_t openAudioSink(
75             const sp<AMessage> &format,
76             bool offloadOnly,
77             bool hasVideo,
78             uint32_t flags,
79             bool *isOffloaded,
80             bool isStreaming);
81     void closeAudioSink();
82 
83     // re-open audio sink after all pending audio buffers played.
84     void changeAudioFormat(
85             const sp<AMessage> &format,
86             bool offloadOnly,
87             bool hasVideo,
88             uint32_t flags,
89             bool isStreaming,
90             const sp<AMessage> &notify);
91 
92     enum {
93         kWhatEOS                      = 'eos ',
94         kWhatFlushComplete            = 'fluC',
95         kWhatPosition                 = 'posi',
96         kWhatVideoRenderingStart      = 'vdrd',
97         kWhatMediaRenderingStart      = 'mdrd',
98         kWhatAudioTearDown            = 'adTD',
99         kWhatAudioOffloadPauseTimeout = 'aOPT',
100     };
101 
102     enum AudioTearDownReason {
103         kDueToError = 0,   // Could restart with either offload or non-offload.
104         kDueToTimeout,
105         kForceNonOffload,  // Restart only with non-offload.
106     };
107 
108 protected:
109     virtual ~Renderer();
110 
111     virtual void onMessageReceived(const sp<AMessage> &msg);
112 
113 private:
114     enum {
115         kWhatDrainAudioQueue     = 'draA',
116         kWhatDrainVideoQueue     = 'draV',
117         kWhatPostDrainVideoQueue = 'pDVQ',
118         kWhatQueueBuffer         = 'queB',
119         kWhatQueueEOS            = 'qEOS',
120         kWhatConfigPlayback      = 'cfPB',
121         kWhatConfigSync          = 'cfSy',
122         kWhatGetPlaybackSettings = 'gPbS',
123         kWhatGetSyncSettings     = 'gSyS',
124         kWhatFlush               = 'flus',
125         kWhatPause               = 'paus',
126         kWhatResume              = 'resm',
127         kWhatOpenAudioSink       = 'opnA',
128         kWhatCloseAudioSink      = 'clsA',
129         kWhatChangeAudioFormat   = 'chgA',
130         kWhatStopAudioSink       = 'stpA',
131         kWhatDisableOffloadAudio = 'noOA',
132         kWhatEnableOffloadAudio  = 'enOA',
133         kWhatSetVideoFrameRate   = 'sVFR',
134     };
135 
136     // if mBuffer != nullptr, it's a buffer containing real data.
137     // else if mNotifyConsumed == nullptr, it's EOS.
138     // else it's a tag for re-opening audio sink in different format.
139     struct QueueEntry {
140         sp<MediaCodecBuffer> mBuffer;
141         sp<AMessage> mMeta;
142         sp<AMessage> mNotifyConsumed;
143         size_t mOffset;
144         status_t mFinalResult;
145         int32_t mBufferOrdinal;
146     };
147 
148     static const int64_t kMinPositionUpdateDelayUs;
149 
150     sp<MediaPlayerBase::AudioSink> mAudioSink;
151     bool mUseVirtualAudioSink;
152     sp<AMessage> mNotify;
153     Mutex mLock;
154     uint32_t mFlags;
155     List<QueueEntry> mAudioQueue;
156     List<QueueEntry> mVideoQueue;
157     uint32_t mNumFramesWritten;
158     sp<VideoFrameScheduler> mVideoScheduler;
159 
160     bool mDrainAudioQueuePending;
161     bool mDrainVideoQueuePending;
162     int32_t mAudioQueueGeneration;
163     int32_t mVideoQueueGeneration;
164     int32_t mAudioDrainGeneration;
165     int32_t mVideoDrainGeneration;
166     int32_t mAudioEOSGeneration;
167 
168     sp<MediaClock> mMediaClock;
169     float mPlaybackRate; // audio track rate
170 
171     AudioPlaybackRate mPlaybackSettings;
172     AVSyncSettings mSyncSettings;
173     float mVideoFpsHint;
174 
175     int64_t mAudioFirstAnchorTimeMediaUs;
176     int64_t mAnchorTimeMediaUs;
177     int64_t mAnchorNumFramesWritten;
178     int64_t mVideoLateByUs;
179     bool mHasAudio;
180     bool mHasVideo;
181 
182     bool mNotifyCompleteAudio;
183     bool mNotifyCompleteVideo;
184 
185     bool mSyncQueues;
186 
187     // modified on only renderer's thread.
188     bool mPaused;
189     int64_t mPauseDrainAudioAllowedUs; // time when we can drain/deliver audio in pause mode.
190 
191     bool mVideoSampleReceived;
192     bool mVideoRenderingStarted;
193     int32_t mVideoRenderingStartGeneration;
194     int32_t mAudioRenderingStartGeneration;
195     bool mRenderingDataDelivered;
196 
197     int64_t mNextAudioClockUpdateTimeUs;
198     // the media timestamp of last audio sample right before EOS.
199     int64_t mLastAudioMediaTimeUs;
200 
201     int32_t mAudioOffloadPauseTimeoutGeneration;
202     bool mAudioTornDown;
203     audio_offload_info_t mCurrentOffloadInfo;
204 
205     struct PcmInfo {
206         audio_channel_mask_t mChannelMask;
207         audio_output_flags_t mFlags;
208         audio_format_t mFormat;
209         int32_t mNumChannels;
210         int32_t mSampleRate;
211     };
212     PcmInfo mCurrentPcmInfo;
213     static const PcmInfo AUDIO_PCMINFO_INITIALIZER;
214 
215     int32_t mTotalBuffersQueued;
216     int32_t mLastAudioBufferDrained;
217     bool mUseAudioCallback;
218 
219     sp<AWakeLock> mWakeLock;
220 
221     status_t getCurrentPositionOnLooper(int64_t *mediaUs);
222     status_t getCurrentPositionOnLooper(
223             int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
224     bool getCurrentPositionIfPaused_l(int64_t *mediaUs);
225     status_t getCurrentPositionFromAnchor(
226             int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
227 
228     void notifyEOSCallback();
229     size_t fillAudioBuffer(void *buffer, size_t size);
230 
231     bool onDrainAudioQueue();
232     void drainAudioQueueUntilLastEOS();
233     int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
234     void postDrainAudioQueue_l(int64_t delayUs = 0);
235 
236     void clearAnchorTime();
237     void clearAudioFirstAnchorTime_l();
238     void setAudioFirstAnchorTimeIfNeeded_l(int64_t mediaUs);
239     void setVideoLateByUs(int64_t lateUs);
240 
241     void onNewAudioMediaTime(int64_t mediaTimeUs);
242     int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs);
243 
244     void onDrainVideoQueue();
245     void postDrainVideoQueue();
246 
247     void prepareForMediaRenderingStart_l();
248     void notifyIfMediaRenderingStarted_l();
249 
250     void onQueueBuffer(const sp<AMessage> &msg);
251     void onQueueEOS(const sp<AMessage> &msg);
252     void onFlush(const sp<AMessage> &msg);
253     void onAudioSinkChanged();
254     void onDisableOffloadAudio();
255     void onEnableOffloadAudio();
256     status_t onConfigPlayback(const AudioPlaybackRate &rate /* sanitized */);
257     status_t onGetPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
258     status_t onConfigSync(const AVSyncSettings &sync, float videoFpsHint);
259     status_t onGetSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
260 
261     void onPause();
262     void onResume();
263     void onSetVideoFrameRate(float fps);
264     int32_t getQueueGeneration(bool audio);
265     int32_t getDrainGeneration(bool audio);
266     bool getSyncQueues();
267     void onAudioTearDown(AudioTearDownReason reason);
268     status_t onOpenAudioSink(
269             const sp<AMessage> &format,
270             bool offloadOnly,
271             bool hasVideo,
272             uint32_t flags,
273             bool isStreaming);
274     void onCloseAudioSink();
275     void onChangeAudioFormat(const sp<AMessage> &meta, const sp<AMessage> &notify);
276 
277     void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
278     void notifyEOS_l(bool audio, status_t finalResult, int64_t delayUs = 0);
279     void notifyFlushComplete(bool audio);
280     void notifyPosition();
281     void notifyVideoLateBy(int64_t lateByUs);
282     void notifyVideoRenderingStart();
283     void notifyAudioTearDown(AudioTearDownReason reason);
284 
285     void flushQueue(List<QueueEntry> *queue);
286     bool dropBufferIfStale(bool audio, const sp<AMessage> &msg);
287     void syncQueuesDone_l();
288 
offloadingAudioRenderer289     bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
290 
291     void startAudioOffloadPauseTimeout();
292     void cancelAudioOffloadPauseTimeout();
293 
294     int64_t getDurationUsIfPlayedAtSampleRate(uint32_t numFrames);
295 
296     DISALLOW_EVIL_CONSTRUCTORS(Renderer);
297 };
298 
299 } // namespace android
300 
301 #endif  // NUPLAYER_RENDERER_H_
302