blob: e4368c7449c464c058c715b57963eda0b617e210 [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 {
28 Renderer(const sp<MediaPlayerBase::AudioSink> &sink,
29 const sp<AMessage> &notify);
30
31 void queueBuffer(
32 bool audio,
33 const sp<ABuffer> &buffer,
34 const sp<AMessage> &notifyConsumed);
35
36 void queueEOS(bool audio, status_t finalResult);
37
38 void flush(bool audio);
39
40 void signalTimeDiscontinuity();
41
Andreas Huber3831a062010-12-21 10:22:33 -080042 void signalAudioSinkChanged();
43
Andreas Huberb4082222011-01-20 15:23:04 -080044 void pause();
45 void resume();
46
Andreas Huberf9334412010-12-15 15:17:42 -080047 enum {
James Dongf57b4ea2012-07-20 13:38:36 -070048 kWhatEOS = 'eos ',
49 kWhatFlushComplete = 'fluC',
50 kWhatPosition = 'posi',
51 kWhatVideoRenderingStart = 'vdrd',
Andreas Huberf9334412010-12-15 15:17:42 -080052 };
53
54protected:
55 virtual ~Renderer();
56
57 virtual void onMessageReceived(const sp<AMessage> &msg);
58
59private:
60 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -070061 kWhatDrainAudioQueue = 'draA',
62 kWhatDrainVideoQueue = 'draV',
63 kWhatQueueBuffer = 'queB',
64 kWhatQueueEOS = 'qEOS',
65 kWhatFlush = 'flus',
66 kWhatAudioSinkChanged = 'auSC',
67 kWhatPause = 'paus',
68 kWhatResume = 'resm',
Andreas Huberf9334412010-12-15 15:17:42 -080069 };
70
71 struct QueueEntry {
72 sp<ABuffer> mBuffer;
73 sp<AMessage> mNotifyConsumed;
74 size_t mOffset;
75 status_t mFinalResult;
76 };
77
Andreas Huber714aa7b2011-09-13 08:28:38 -070078 static const int64_t kMinPositionUpdateDelayUs;
79
Andreas Huberf9334412010-12-15 15:17:42 -080080 sp<MediaPlayerBase::AudioSink> mAudioSink;
81 sp<AMessage> mNotify;
82 List<QueueEntry> mAudioQueue;
83 List<QueueEntry> mVideoQueue;
84 uint32_t mNumFramesWritten;
85
86 bool mDrainAudioQueuePending;
87 bool mDrainVideoQueuePending;
88 int32_t mAudioQueueGeneration;
89 int32_t mVideoQueueGeneration;
90
91 int64_t mAnchorTimeMediaUs;
92 int64_t mAnchorTimeRealUs;
93
94 Mutex mFlushLock; // protects the following 2 member vars.
95 bool mFlushingAudio;
96 bool mFlushingVideo;
97
Andreas Huber3831a062010-12-21 10:22:33 -080098 bool mHasAudio;
99 bool mHasVideo;
Andreas Huberf9334412010-12-15 15:17:42 -0800100 bool mSyncQueues;
101
Andreas Huberb4082222011-01-20 15:23:04 -0800102 bool mPaused;
James Dongf57b4ea2012-07-20 13:38:36 -0700103 bool mVideoRenderingStarted;
Andreas Huberb4082222011-01-20 15:23:04 -0800104
Andreas Huber714aa7b2011-09-13 08:28:38 -0700105 int64_t mLastPositionUpdateUs;
Andreas Huber3fe62152011-09-16 15:09:22 -0700106 int64_t mVideoLateByUs;
Andreas Huber714aa7b2011-09-13 08:28:38 -0700107
Andreas Huber078cfcf2011-09-15 12:25:04 -0700108 bool onDrainAudioQueue();
109 void postDrainAudioQueue(int64_t delayUs = 0);
Andreas Huberf9334412010-12-15 15:17:42 -0800110
111 void onDrainVideoQueue();
112 void postDrainVideoQueue();
113
114 void onQueueBuffer(const sp<AMessage> &msg);
115 void onQueueEOS(const sp<AMessage> &msg);
116 void onFlush(const sp<AMessage> &msg);
Andreas Huber3831a062010-12-21 10:22:33 -0800117 void onAudioSinkChanged();
Andreas Huberb4082222011-01-20 15:23:04 -0800118 void onPause();
119 void onResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800120
Andreas Huberc92fd242011-08-16 13:48:44 -0700121 void notifyEOS(bool audio, status_t finalResult);
Andreas Huberf9334412010-12-15 15:17:42 -0800122 void notifyFlushComplete(bool audio);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800123 void notifyPosition();
Andreas Huber3fe62152011-09-16 15:09:22 -0700124 void notifyVideoLateBy(int64_t lateByUs);
James Dongf57b4ea2012-07-20 13:38:36 -0700125 void notifyVideoRenderingStart();
Andreas Huberf9334412010-12-15 15:17:42 -0800126
127 void flushQueue(List<QueueEntry> *queue);
128 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
129 void syncQueuesDone();
130
131 DISALLOW_EVIL_CONSTRUCTORS(Renderer);
132};
133
134} // namespace android
135
136#endif // NUPLAYER_RENDERER_H_