blob: bd51af3cdb723979708c808ad065e18b7fcc8dfa [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;
Martin Storsjoda38df52013-09-25 16:05:36 +030026class SoftwareRenderer;
Andreas Huber5bc087c2010-12-23 10:27:40 -080027
Andreas Huberf9334412010-12-15 15:17:42 -080028struct NuPlayer::Renderer : public AHandler {
Andreas Huber9dffd242013-03-12 11:01:43 -070029 enum Flags {
30 FLAG_REAL_TIME = 1,
31 };
Andreas Huberf9334412010-12-15 15:17:42 -080032 Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
Andreas Huber9dffd242013-03-12 11:01:43 -070033 const sp<AMessage> &notify,
34 uint32_t flags = 0);
Andreas Huberf9334412010-12-15 15:17:42 -080035
36 void queueBuffer(
37 bool audio,
38 const sp<ABuffer> &buffer,
39 const sp<AMessage> &notifyConsumed);
40
41 void queueEOS(bool audio, status_t finalResult);
42
43 void flush(bool audio);
44
45 void signalTimeDiscontinuity();
46
Andreas Huber3831a062010-12-21 10:22:33 -080047 void signalAudioSinkChanged();
48
Andreas Huberb4082222011-01-20 15:23:04 -080049 void pause();
50 void resume();
51
Andreas Huberf9334412010-12-15 15:17:42 -080052 enum {
James Dongf57b4ea2012-07-20 13:38:36 -070053 kWhatEOS = 'eos ',
54 kWhatFlushComplete = 'fluC',
55 kWhatPosition = 'posi',
56 kWhatVideoRenderingStart = 'vdrd',
Andreas Huberf9334412010-12-15 15:17:42 -080057 };
58
Martin Storsjoda38df52013-09-25 16:05:36 +030059 void setSoftRenderer(SoftwareRenderer *softRenderer);
60
Andreas Huberf9334412010-12-15 15:17:42 -080061protected:
62 virtual ~Renderer();
63
64 virtual void onMessageReceived(const sp<AMessage> &msg);
65
66private:
67 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -070068 kWhatDrainAudioQueue = 'draA',
69 kWhatDrainVideoQueue = 'draV',
70 kWhatQueueBuffer = 'queB',
71 kWhatQueueEOS = 'qEOS',
72 kWhatFlush = 'flus',
73 kWhatAudioSinkChanged = 'auSC',
74 kWhatPause = 'paus',
75 kWhatResume = 'resm',
Andreas Huberf9334412010-12-15 15:17:42 -080076 };
77
78 struct QueueEntry {
79 sp<ABuffer> mBuffer;
80 sp<AMessage> mNotifyConsumed;
81 size_t mOffset;
82 status_t mFinalResult;
83 };
84
Andreas Huber714aa7b2011-09-13 08:28:38 -070085 static const int64_t kMinPositionUpdateDelayUs;
86
Andreas Huberf9334412010-12-15 15:17:42 -080087 sp<MediaPlayerBase::AudioSink> mAudioSink;
Martin Storsjoda38df52013-09-25 16:05:36 +030088 SoftwareRenderer *mSoftRenderer;
Andreas Huberf9334412010-12-15 15:17:42 -080089 sp<AMessage> mNotify;
Andreas Huber9dffd242013-03-12 11:01:43 -070090 uint32_t mFlags;
Andreas Huberf9334412010-12-15 15:17:42 -080091 List<QueueEntry> mAudioQueue;
92 List<QueueEntry> mVideoQueue;
93 uint32_t mNumFramesWritten;
94
95 bool mDrainAudioQueuePending;
96 bool mDrainVideoQueuePending;
97 int32_t mAudioQueueGeneration;
98 int32_t mVideoQueueGeneration;
99
100 int64_t mAnchorTimeMediaUs;
101 int64_t mAnchorTimeRealUs;
102
103 Mutex mFlushLock; // protects the following 2 member vars.
104 bool mFlushingAudio;
105 bool mFlushingVideo;
106
Andreas Huber3831a062010-12-21 10:22:33 -0800107 bool mHasAudio;
108 bool mHasVideo;
Andreas Huberf9334412010-12-15 15:17:42 -0800109 bool mSyncQueues;
110
Andreas Huberb4082222011-01-20 15:23:04 -0800111 bool mPaused;
James Dongf57b4ea2012-07-20 13:38:36 -0700112 bool mVideoRenderingStarted;
Andreas Huberb4082222011-01-20 15:23:04 -0800113
Andreas Huber714aa7b2011-09-13 08:28:38 -0700114 int64_t mLastPositionUpdateUs;
Andreas Huber3fe62152011-09-16 15:09:22 -0700115 int64_t mVideoLateByUs;
Andreas Huber714aa7b2011-09-13 08:28:38 -0700116
Andreas Huber078cfcf2011-09-15 12:25:04 -0700117 bool onDrainAudioQueue();
118 void postDrainAudioQueue(int64_t delayUs = 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800119
120 void onDrainVideoQueue();
121 void postDrainVideoQueue();
122
123 void onQueueBuffer(const sp<AMessage> &msg);
124 void onQueueEOS(const sp<AMessage> &msg);
125 void onFlush(const sp<AMessage> &msg);
Andreas Huber3831a062010-12-21 10:22:33 -0800126 void onAudioSinkChanged();
Andreas Huberb4082222011-01-20 15:23:04 -0800127 void onPause();
128 void onResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800129
Andreas Huberc92fd242011-08-16 13:48:44 -0700130 void notifyEOS(bool audio, status_t finalResult);
Andreas Huberf9334412010-12-15 15:17:42 -0800131 void notifyFlushComplete(bool audio);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800132 void notifyPosition();
Andreas Huber3fe62152011-09-16 15:09:22 -0700133 void notifyVideoLateBy(int64_t lateByUs);
James Dongf57b4ea2012-07-20 13:38:36 -0700134 void notifyVideoRenderingStart();
Andreas Huberf9334412010-12-15 15:17:42 -0800135
136 void flushQueue(List<QueueEntry> *queue);
137 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
138 void syncQueuesDone();
139
140 DISALLOW_EVIL_CONSTRUCTORS(Renderer);
141};
142
143} // namespace android
144
145#endif // NUPLAYER_RENDERER_H_