blob: 94a05ea25e9757cab2c9b9aa14e6903709552324 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
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
Andreas Huber5bc087c2010-12-23 10:27:40 -080025struct ABuffer;
26
Andreas Huberf9334412010-12-15 15:17:42 -080027struct NuPlayer::Renderer : public AHandler {
Andreas Huberd5e56232013-03-12 11:01:43 -070028 enum Flags {
29 FLAG_REAL_TIME = 1,
30 };
Andreas Huberf9334412010-12-15 15:17:42 -080031 Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
Andreas Huberd5e56232013-03-12 11:01:43 -070032 const sp<AMessage> &notify,
33 uint32_t flags = 0);
Andreas Huberf9334412010-12-15 15:17:42 -080034
35 void queueBuffer(
36 bool audio,
37 const sp<ABuffer> &buffer,
38 const sp<AMessage> &notifyConsumed);
39
40 void queueEOS(bool audio, status_t finalResult);
41
42 void flush(bool audio);
43
44 void signalTimeDiscontinuity();
45
Andreas Huber3831a062010-12-21 10:22:33 -080046 void signalAudioSinkChanged();
47
Andreas Huberb4082222011-01-20 15:23:04 -080048 void pause();
49 void resume();
50
Andreas Huberf9334412010-12-15 15:17:42 -080051 enum {
James Dongf57b4ea2012-07-20 13:38:36 -070052 kWhatEOS = 'eos ',
53 kWhatFlushComplete = 'fluC',
54 kWhatPosition = 'posi',
55 kWhatVideoRenderingStart = 'vdrd',
Lajos Molnarcbaffcf2013-08-14 18:30:38 -070056 kWhatMediaRenderingStart = 'mdrd',
Andreas Huberf9334412010-12-15 15:17:42 -080057 };
58
59protected:
60 virtual ~Renderer();
61
62 virtual void onMessageReceived(const sp<AMessage> &msg);
63
64private:
65 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -070066 kWhatDrainAudioQueue = 'draA',
67 kWhatDrainVideoQueue = 'draV',
68 kWhatQueueBuffer = 'queB',
69 kWhatQueueEOS = 'qEOS',
70 kWhatFlush = 'flus',
71 kWhatAudioSinkChanged = 'auSC',
72 kWhatPause = 'paus',
73 kWhatResume = 'resm',
Andreas Huberf9334412010-12-15 15:17:42 -080074 };
75
76 struct QueueEntry {
77 sp<ABuffer> mBuffer;
78 sp<AMessage> mNotifyConsumed;
79 size_t mOffset;
80 status_t mFinalResult;
81 };
82
Andreas Huber714aa7b2011-09-13 08:28:38 -070083 static const int64_t kMinPositionUpdateDelayUs;
84
Andreas Huberf9334412010-12-15 15:17:42 -080085 sp<MediaPlayerBase::AudioSink> mAudioSink;
86 sp<AMessage> mNotify;
Andreas Huberd5e56232013-03-12 11:01:43 -070087 uint32_t mFlags;
Andreas Huberf9334412010-12-15 15:17:42 -080088 List<QueueEntry> mAudioQueue;
89 List<QueueEntry> mVideoQueue;
90 uint32_t mNumFramesWritten;
91
92 bool mDrainAudioQueuePending;
93 bool mDrainVideoQueuePending;
94 int32_t mAudioQueueGeneration;
95 int32_t mVideoQueueGeneration;
96
97 int64_t mAnchorTimeMediaUs;
98 int64_t mAnchorTimeRealUs;
99
100 Mutex mFlushLock; // protects the following 2 member vars.
101 bool mFlushingAudio;
102 bool mFlushingVideo;
103
Andreas Huber3831a062010-12-21 10:22:33 -0800104 bool mHasAudio;
105 bool mHasVideo;
Andreas Huberf9334412010-12-15 15:17:42 -0800106 bool mSyncQueues;
107
Andreas Huberb4082222011-01-20 15:23:04 -0800108 bool mPaused;
James Dongf57b4ea2012-07-20 13:38:36 -0700109 bool mVideoRenderingStarted;
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700110 int32_t mVideoRenderingStartGeneration;
111 int32_t mAudioRenderingStartGeneration;
Andreas Huberb4082222011-01-20 15:23:04 -0800112
Andreas Huber714aa7b2011-09-13 08:28:38 -0700113 int64_t mLastPositionUpdateUs;
Andreas Huber3fe62152011-09-16 15:09:22 -0700114 int64_t mVideoLateByUs;
Andreas Huber714aa7b2011-09-13 08:28:38 -0700115
Andreas Huber078cfcf2011-09-15 12:25:04 -0700116 bool onDrainAudioQueue();
117 void postDrainAudioQueue(int64_t delayUs = 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800118
119 void onDrainVideoQueue();
120 void postDrainVideoQueue();
121
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700122 void prepareForMediaRenderingStart();
123 void notifyIfMediaRenderingStarted();
124
Andreas Huberf9334412010-12-15 15:17:42 -0800125 void onQueueBuffer(const sp<AMessage> &msg);
126 void onQueueEOS(const sp<AMessage> &msg);
127 void onFlush(const sp<AMessage> &msg);
Andreas Huber3831a062010-12-21 10:22:33 -0800128 void onAudioSinkChanged();
Andreas Huberb4082222011-01-20 15:23:04 -0800129 void onPause();
130 void onResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800131
Andreas Huberc92fd242011-08-16 13:48:44 -0700132 void notifyEOS(bool audio, status_t finalResult);
Andreas Huberf9334412010-12-15 15:17:42 -0800133 void notifyFlushComplete(bool audio);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800134 void notifyPosition();
Andreas Huber3fe62152011-09-16 15:09:22 -0700135 void notifyVideoLateBy(int64_t lateByUs);
James Dongf57b4ea2012-07-20 13:38:36 -0700136 void notifyVideoRenderingStart();
Andreas Huberf9334412010-12-15 15:17:42 -0800137
138 void flushQueue(List<QueueEntry> *queue);
139 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
140 void syncQueuesDone();
141
142 DISALLOW_EVIL_CONSTRUCTORS(Renderer);
143};
144
145} // namespace android
146
147#endif // NUPLAYER_RENDERER_H_