blob: 3a641a22042d62d2edd28459801b58e9187d4c81 [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 {
48 kWhatEOS,
49 kWhatFlushComplete,
Andreas Huber43c3e6c2011-01-05 12:17:08 -080050 kWhatPosition,
Andreas Huberf9334412010-12-15 15:17:42 -080051 };
52
53protected:
54 virtual ~Renderer();
55
56 virtual void onMessageReceived(const sp<AMessage> &msg);
57
58private:
59 enum {
60 kWhatDrainAudioQueue,
61 kWhatDrainVideoQueue,
62 kWhatQueueBuffer,
63 kWhatQueueEOS,
64 kWhatFlush,
Andreas Huber3831a062010-12-21 10:22:33 -080065 kWhatAudioSinkChanged,
Andreas Huberb4082222011-01-20 15:23:04 -080066 kWhatPause,
67 kWhatResume,
Andreas Huberf9334412010-12-15 15:17:42 -080068 };
69
70 struct QueueEntry {
71 sp<ABuffer> mBuffer;
72 sp<AMessage> mNotifyConsumed;
73 size_t mOffset;
74 status_t mFinalResult;
75 };
76
Andreas Huber714aa7b2011-09-13 08:28:38 -070077 static const int64_t kMinPositionUpdateDelayUs;
78
Andreas Huberf9334412010-12-15 15:17:42 -080079 sp<MediaPlayerBase::AudioSink> mAudioSink;
80 sp<AMessage> mNotify;
81 List<QueueEntry> mAudioQueue;
82 List<QueueEntry> mVideoQueue;
83 uint32_t mNumFramesWritten;
84
85 bool mDrainAudioQueuePending;
86 bool mDrainVideoQueuePending;
87 int32_t mAudioQueueGeneration;
88 int32_t mVideoQueueGeneration;
89
90 int64_t mAnchorTimeMediaUs;
91 int64_t mAnchorTimeRealUs;
92
93 Mutex mFlushLock; // protects the following 2 member vars.
94 bool mFlushingAudio;
95 bool mFlushingVideo;
96
Andreas Huber3831a062010-12-21 10:22:33 -080097 bool mHasAudio;
98 bool mHasVideo;
Andreas Huberf9334412010-12-15 15:17:42 -080099 bool mSyncQueues;
100
Andreas Huberb4082222011-01-20 15:23:04 -0800101 bool mPaused;
102
Andreas Huber714aa7b2011-09-13 08:28:38 -0700103 int64_t mLastPositionUpdateUs;
104
Andreas Huberf9334412010-12-15 15:17:42 -0800105 void onDrainAudioQueue();
106 void postDrainAudioQueue();
107
108 void onDrainVideoQueue();
109 void postDrainVideoQueue();
110
111 void onQueueBuffer(const sp<AMessage> &msg);
112 void onQueueEOS(const sp<AMessage> &msg);
113 void onFlush(const sp<AMessage> &msg);
Andreas Huber3831a062010-12-21 10:22:33 -0800114 void onAudioSinkChanged();
Andreas Huberb4082222011-01-20 15:23:04 -0800115 void onPause();
116 void onResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800117
Andreas Huberc92fd242011-08-16 13:48:44 -0700118 void notifyEOS(bool audio, status_t finalResult);
Andreas Huberf9334412010-12-15 15:17:42 -0800119 void notifyFlushComplete(bool audio);
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800120 void notifyPosition();
Andreas Huberf9334412010-12-15 15:17:42 -0800121
122 void flushQueue(List<QueueEntry> *queue);
123 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
124 void syncQueuesDone();
125
126 DISALLOW_EVIL_CONSTRUCTORS(Renderer);
127};
128
129} // namespace android
130
131#endif // NUPLAYER_RENDERER_H_