blob: 9124e0343f4f9fede3d8e11167375966cbeeb7a2 [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 Huberd5e56232013-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 Huberd5e56232013-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',
Lajos Molnarcbaffcf2013-08-14 18:30:38 -070057 kWhatMediaRenderingStart = 'mdrd',
Andreas Huberf9334412010-12-15 15:17:42 -080058 };
59
Martin Storsjoda38df52013-09-25 16:05:36 +030060 void setSoftRenderer(SoftwareRenderer *softRenderer);
61
Andreas Huberf9334412010-12-15 15:17:42 -080062protected:
63 virtual ~Renderer();
64
65 virtual void onMessageReceived(const sp<AMessage> &msg);
66
67private:
68 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -070069 kWhatDrainAudioQueue = 'draA',
70 kWhatDrainVideoQueue = 'draV',
71 kWhatQueueBuffer = 'queB',
72 kWhatQueueEOS = 'qEOS',
73 kWhatFlush = 'flus',
74 kWhatAudioSinkChanged = 'auSC',
75 kWhatPause = 'paus',
76 kWhatResume = 'resm',
Andreas Huberf9334412010-12-15 15:17:42 -080077 };
78
79 struct QueueEntry {
80 sp<ABuffer> mBuffer;
81 sp<AMessage> mNotifyConsumed;
82 size_t mOffset;
83 status_t mFinalResult;
84 };
85
Andreas Huber714aa7b2011-09-13 08:28:38 -070086 static const int64_t kMinPositionUpdateDelayUs;
87
Andreas Huberf9334412010-12-15 15:17:42 -080088 sp<MediaPlayerBase::AudioSink> mAudioSink;
Martin Storsjoda38df52013-09-25 16:05:36 +030089 SoftwareRenderer *mSoftRenderer;
Andreas Huberf9334412010-12-15 15:17:42 -080090 sp<AMessage> mNotify;
Andreas Huberd5e56232013-03-12 11:01:43 -070091 uint32_t mFlags;
Andreas Huberf9334412010-12-15 15:17:42 -080092 List<QueueEntry> mAudioQueue;
93 List<QueueEntry> mVideoQueue;
94 uint32_t mNumFramesWritten;
95
96 bool mDrainAudioQueuePending;
97 bool mDrainVideoQueuePending;
98 int32_t mAudioQueueGeneration;
99 int32_t mVideoQueueGeneration;
100
101 int64_t mAnchorTimeMediaUs;
102 int64_t mAnchorTimeRealUs;
103
104 Mutex mFlushLock; // protects the following 2 member vars.
105 bool mFlushingAudio;
106 bool mFlushingVideo;
107
Andreas Huber3831a062010-12-21 10:22:33 -0800108 bool mHasAudio;
109 bool mHasVideo;
Andreas Huberf9334412010-12-15 15:17:42 -0800110 bool mSyncQueues;
111
Andreas Huberb4082222011-01-20 15:23:04 -0800112 bool mPaused;
James Dongf57b4ea2012-07-20 13:38:36 -0700113 bool mVideoRenderingStarted;
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700114 int32_t mVideoRenderingStartGeneration;
115 int32_t mAudioRenderingStartGeneration;
Andreas Huberb4082222011-01-20 15:23:04 -0800116
Andreas Huber714aa7b2011-09-13 08:28:38 -0700117 int64_t mLastPositionUpdateUs;
Andreas Huber3fe62152011-09-16 15:09:22 -0700118 int64_t mVideoLateByUs;
Andreas Huber714aa7b2011-09-13 08:28:38 -0700119
Andreas Huber078cfcf2011-09-15 12:25:04 -0700120 bool onDrainAudioQueue();
121 void postDrainAudioQueue(int64_t delayUs = 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800122
123 void onDrainVideoQueue();
124 void postDrainVideoQueue();
125
Lajos Molnarcbaffcf2013-08-14 18:30:38 -0700126 void prepareForMediaRenderingStart();
127 void notifyIfMediaRenderingStarted();
128
Andreas Huberf9334412010-12-15 15:17:42 -0800129 void onQueueBuffer(const sp<AMessage> &msg);
130 void onQueueEOS(const sp<AMessage> &msg);
131 void onFlush(const sp<AMessage> &msg);
Andreas Huber3831a062010-12-21 10:22:33 -0800132 void onAudioSinkChanged();
Andreas Huberb4082222011-01-20 15:23:04 -0800133 void onPause();
134 void onResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800135
Andreas Huberc92fd242011-08-16 13:48:44 -0700136 void notifyEOS(bool audio, status_t finalResult);
Andreas Huberf9334412010-12-15 15:17:42 -0800137 void notifyFlushComplete(bool audio);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800138 void notifyPosition();
Andreas Huber3fe62152011-09-16 15:09:22 -0700139 void notifyVideoLateBy(int64_t lateByUs);
James Dongf57b4ea2012-07-20 13:38:36 -0700140 void notifyVideoRenderingStart();
Andreas Huberf9334412010-12-15 15:17:42 -0800141
142 void flushQueue(List<QueueEntry> *queue);
143 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
144 void syncQueuesDone();
145
146 DISALLOW_EVIL_CONSTRUCTORS(Renderer);
147};
148
149} // namespace android
150
151#endif // NUPLAYER_RENDERER_H_