blob: c9796e20a1cb85dd37f5f0ba9e2ae4fef362c0f2 [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',
Andreas Huberf9334412010-12-15 15:17:42 -080056 };
57
58protected:
59 virtual ~Renderer();
60
61 virtual void onMessageReceived(const sp<AMessage> &msg);
62
63private:
64 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -070065 kWhatDrainAudioQueue = 'draA',
66 kWhatDrainVideoQueue = 'draV',
67 kWhatQueueBuffer = 'queB',
68 kWhatQueueEOS = 'qEOS',
69 kWhatFlush = 'flus',
70 kWhatAudioSinkChanged = 'auSC',
71 kWhatPause = 'paus',
72 kWhatResume = 'resm',
Andreas Huberf9334412010-12-15 15:17:42 -080073 };
74
75 struct QueueEntry {
76 sp<ABuffer> mBuffer;
77 sp<AMessage> mNotifyConsumed;
78 size_t mOffset;
79 status_t mFinalResult;
80 };
81
Andreas Huber714aa7b2011-09-13 08:28:38 -070082 static const int64_t kMinPositionUpdateDelayUs;
83
Andreas Huberf9334412010-12-15 15:17:42 -080084 sp<MediaPlayerBase::AudioSink> mAudioSink;
85 sp<AMessage> mNotify;
Andreas Huberd5e56232013-03-12 11:01:43 -070086 uint32_t mFlags;
Andreas Huberf9334412010-12-15 15:17:42 -080087 List<QueueEntry> mAudioQueue;
88 List<QueueEntry> mVideoQueue;
89 uint32_t mNumFramesWritten;
90
91 bool mDrainAudioQueuePending;
92 bool mDrainVideoQueuePending;
93 int32_t mAudioQueueGeneration;
94 int32_t mVideoQueueGeneration;
95
96 int64_t mAnchorTimeMediaUs;
97 int64_t mAnchorTimeRealUs;
98
99 Mutex mFlushLock; // protects the following 2 member vars.
100 bool mFlushingAudio;
101 bool mFlushingVideo;
102
Andreas Huber3831a062010-12-21 10:22:33 -0800103 bool mHasAudio;
104 bool mHasVideo;
Andreas Huberf9334412010-12-15 15:17:42 -0800105 bool mSyncQueues;
106
Andreas Huberb4082222011-01-20 15:23:04 -0800107 bool mPaused;
James Dongf57b4ea2012-07-20 13:38:36 -0700108 bool mVideoRenderingStarted;
Andreas Huberb4082222011-01-20 15:23:04 -0800109
Andreas Huber714aa7b2011-09-13 08:28:38 -0700110 int64_t mLastPositionUpdateUs;
Andreas Huber3fe62152011-09-16 15:09:22 -0700111 int64_t mVideoLateByUs;
Andreas Huber714aa7b2011-09-13 08:28:38 -0700112
Andreas Huber078cfcf2011-09-15 12:25:04 -0700113 bool onDrainAudioQueue();
114 void postDrainAudioQueue(int64_t delayUs = 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800115
116 void onDrainVideoQueue();
117 void postDrainVideoQueue();
118
119 void onQueueBuffer(const sp<AMessage> &msg);
120 void onQueueEOS(const sp<AMessage> &msg);
121 void onFlush(const sp<AMessage> &msg);
Andreas Huber3831a062010-12-21 10:22:33 -0800122 void onAudioSinkChanged();
Andreas Huberb4082222011-01-20 15:23:04 -0800123 void onPause();
124 void onResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800125
Andreas Huberc92fd242011-08-16 13:48:44 -0700126 void notifyEOS(bool audio, status_t finalResult);
Andreas Huberf9334412010-12-15 15:17:42 -0800127 void notifyFlushComplete(bool audio);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800128 void notifyPosition();
Andreas Huber3fe62152011-09-16 15:09:22 -0700129 void notifyVideoLateBy(int64_t lateByUs);
James Dongf57b4ea2012-07-20 13:38:36 -0700130 void notifyVideoRenderingStart();
Andreas Huberf9334412010-12-15 15:17:42 -0800131
132 void flushQueue(List<QueueEntry> *queue);
133 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
134 void syncQueuesDone();
135
136 DISALLOW_EVIL_CONSTRUCTORS(Renderer);
137};
138
139} // namespace android
140
141#endif // NUPLAYER_RENDERER_H_