blob: 268628bd8c61e8078ea068633458f248c2625e4d [file] [log] [blame]
Andreas Hubera1587462010-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 Huber54e66492010-12-23 10:27:40 -080025struct ABuffer;
26
Andreas Hubera1587462010-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 Huber41c3f742010-12-21 10:22:33 -080042 void signalAudioSinkChanged();
43
Andreas Huber601fe0e2011-01-20 15:23:04 -080044 void pause();
45 void resume();
46
Andreas Hubera1587462010-12-15 15:17:42 -080047 enum {
Andreas Huber115cac82011-09-15 12:25:04 -070048 kWhatEOS = 'eos ',
49 kWhatFlushComplete = 'fluC',
50 kWhatPosition = 'posi',
Andreas Hubera1587462010-12-15 15:17:42 -080051 };
52
53protected:
54 virtual ~Renderer();
55
56 virtual void onMessageReceived(const sp<AMessage> &msg);
57
58private:
59 enum {
Andreas Huber115cac82011-09-15 12:25:04 -070060 kWhatDrainAudioQueue = 'draA',
61 kWhatDrainVideoQueue = 'draV',
62 kWhatQueueBuffer = 'queB',
63 kWhatQueueEOS = 'qEOS',
64 kWhatFlush = 'flus',
65 kWhatAudioSinkChanged = 'auSC',
66 kWhatPause = 'paus',
67 kWhatResume = 'resm',
Andreas Hubera1587462010-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 Huberfa6dfdc2011-09-13 08:28:38 -070077 static const int64_t kMinPositionUpdateDelayUs;
78
Andreas Hubera1587462010-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 Huber41c3f742010-12-21 10:22:33 -080097 bool mHasAudio;
98 bool mHasVideo;
Andreas Hubera1587462010-12-15 15:17:42 -080099 bool mSyncQueues;
100
Andreas Huber601fe0e2011-01-20 15:23:04 -0800101 bool mPaused;
102
Andreas Huberfa6dfdc2011-09-13 08:28:38 -0700103 int64_t mLastPositionUpdateUs;
Andreas Huber950fc9d2011-09-16 15:09:22 -0700104 int64_t mVideoLateByUs;
Andreas Huberfa6dfdc2011-09-13 08:28:38 -0700105
Andreas Huber115cac82011-09-15 12:25:04 -0700106 bool onDrainAudioQueue();
107 void postDrainAudioQueue(int64_t delayUs = 0);
Andreas Hubera1587462010-12-15 15:17:42 -0800108
109 void onDrainVideoQueue();
110 void postDrainVideoQueue();
111
112 void onQueueBuffer(const sp<AMessage> &msg);
113 void onQueueEOS(const sp<AMessage> &msg);
114 void onFlush(const sp<AMessage> &msg);
Andreas Huber41c3f742010-12-21 10:22:33 -0800115 void onAudioSinkChanged();
Andreas Huber601fe0e2011-01-20 15:23:04 -0800116 void onPause();
117 void onResume();
Andreas Hubera1587462010-12-15 15:17:42 -0800118
Andreas Huberd84fd792011-08-16 13:48:44 -0700119 void notifyEOS(bool audio, status_t finalResult);
Andreas Hubera1587462010-12-15 15:17:42 -0800120 void notifyFlushComplete(bool audio);
Andreas Huber08e10cb2011-01-05 12:17:08 -0800121 void notifyPosition();
Andreas Huber950fc9d2011-09-16 15:09:22 -0700122 void notifyVideoLateBy(int64_t lateByUs);
Andreas Hubera1587462010-12-15 15:17:42 -0800123
124 void flushQueue(List<QueueEntry> *queue);
125 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg);
126 void syncQueuesDone();
127
128 DISALLOW_EVIL_CONSTRUCTORS(Renderer);
129};
130
131} // namespace android
132
133#endif // NUPLAYER_RENDERER_H_