blob: c6ceb4e19defe3420644bdb64f76d9a1f48debf2 [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_DECODER_H_
18
19#define NUPLAYER_DECODER_H_
20
21#include "NuPlayer.h"
22
23#include <media/stagefright/foundation/AHandler.h>
24
25namespace android {
26
Andreas Huber5bc087c2010-12-23 10:27:40 -080027struct ABuffer;
Lajos Molnar1cd13982014-01-17 15:12:51 -080028struct MediaCodec;
Lajos Molnar09524832014-07-17 14:29:51 -070029struct MediaBuffer;
Andreas Huberf9334412010-12-15 15:17:42 -080030
31struct NuPlayer::Decoder : public AHandler {
Glenn Kasten11731182011-02-08 17:26:17 -080032 Decoder(const sp<AMessage> &notify,
Wei Jiac6cfd702014-11-11 16:33:20 -080033 const sp<Source> &source,
34 const sp<Renderer> &renderer = NULL,
Glenn Kasten11731182011-02-08 17:26:17 -080035 const sp<NativeWindowWrapper> &nativeWindow = NULL);
Andreas Huberf9334412010-12-15 15:17:42 -080036
Wei Jiabc2fb722014-07-08 16:37:57 -070037 virtual void configure(const sp<AMessage> &format);
38 virtual void init();
Andreas Huber3831a062010-12-21 10:22:33 -080039
Wei Jiac6cfd702014-11-11 16:33:20 -080040 virtual void setRenderer(const sp<Renderer> &renderer);
41
Lajos Molnar09524832014-07-17 14:29:51 -070042 status_t getInputBuffers(Vector<sp<ABuffer> > *dstBuffers) const;
Lajos Molnar87603c02014-08-20 19:25:30 -070043 virtual void signalFlush(const sp<AMessage> &format = NULL);
44 virtual void signalUpdateFormat(const sp<AMessage> &format);
Wei Jiabc2fb722014-07-08 16:37:57 -070045 virtual void signalResume();
46 virtual void initiateShutdown();
Andreas Huberf9334412010-12-15 15:17:42 -080047
Wei Jiabc2fb722014-07-08 16:37:57 -070048 virtual bool supportsSeamlessFormatChange(const sp<AMessage> &to) const;
Robert Shih6d0a94e2014-01-23 16:18:22 -080049
Lajos Molnar1cd13982014-01-17 15:12:51 -080050 enum {
51 kWhatFillThisBuffer = 'flTB',
Wei Jiac6cfd702014-11-11 16:33:20 -080052 kWhatRenderBufferTime = 'rnBT',
53 kWhatVideoSizeChanged = 'viSC',
Lajos Molnar1cd13982014-01-17 15:12:51 -080054 kWhatFlushCompleted = 'flsC',
55 kWhatShutdownCompleted = 'shDC',
56 kWhatEOS = 'eos ',
57 kWhatError = 'err ',
58 };
59
Andreas Huberf9334412010-12-15 15:17:42 -080060protected:
Lajos Molnar1cd13982014-01-17 15:12:51 -080061
Andreas Huberf9334412010-12-15 15:17:42 -080062 virtual ~Decoder();
63
64 virtual void onMessageReceived(const sp<AMessage> &msg);
65
Andreas Huberf9334412010-12-15 15:17:42 -080066 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -070067 kWhatCodecNotify = 'cdcN',
Lajos Molnar1cd13982014-01-17 15:12:51 -080068 kWhatConfigure = 'conf',
Wei Jiac6cfd702014-11-11 16:33:20 -080069 kWhatSetRenderer = 'setR',
Lajos Molnar09524832014-07-17 14:29:51 -070070 kWhatGetInputBuffers = 'gInB',
Lajos Molnar1cd13982014-01-17 15:12:51 -080071 kWhatInputBufferFilled = 'inpF',
72 kWhatRenderBuffer = 'rndr',
73 kWhatFlush = 'flus',
74 kWhatShutdown = 'shuD',
Lajos Molnar87603c02014-08-20 19:25:30 -070075 kWhatUpdateFormat = 'uFmt',
Andreas Huberf9334412010-12-15 15:17:42 -080076 };
77
Wei Jiac6cfd702014-11-11 16:33:20 -080078private:
Andreas Huberf9334412010-12-15 15:17:42 -080079 sp<AMessage> mNotify;
Glenn Kasten11731182011-02-08 17:26:17 -080080 sp<NativeWindowWrapper> mNativeWindow;
Andreas Huberf9334412010-12-15 15:17:42 -080081
Wei Jiac6cfd702014-11-11 16:33:20 -080082 sp<Source> mSource;
83 sp<Renderer> mRenderer;
84
Lajos Molnar1cd13982014-01-17 15:12:51 -080085 sp<AMessage> mInputFormat;
86 sp<AMessage> mOutputFormat;
87 sp<MediaCodec> mCodec;
Andreas Huber078cfcf2011-09-15 12:25:04 -070088 sp<ALooper> mCodecLooper;
Lajos Molnar1cd13982014-01-17 15:12:51 -080089 sp<ALooper> mDecoderLooper;
Andreas Huberf9334412010-12-15 15:17:42 -080090
Wei Jia2245fc62014-10-02 15:12:25 -070091 List<sp<AMessage> > mPendingInputMessages;
92
Lajos Molnar1cd13982014-01-17 15:12:51 -080093 Vector<sp<ABuffer> > mInputBuffers;
94 Vector<sp<ABuffer> > mOutputBuffers;
Lajos Molnar87603c02014-08-20 19:25:30 -070095 Vector<sp<ABuffer> > mCSDsForCurrentFormat;
96 Vector<sp<ABuffer> > mCSDsToSubmit;
Lajos Molnar09524832014-07-17 14:29:51 -070097 Vector<bool> mInputBufferIsDequeued;
98 Vector<MediaBuffer *> mMediaBuffers;
Andreas Huberf9334412010-12-15 15:17:42 -080099
Wei Jiac6cfd702014-11-11 16:33:20 -0800100 int64_t mSkipRenderingUntilMediaTimeUs;
101
Lajos Molnar1cd13982014-01-17 15:12:51 -0800102 void handleError(int32_t err);
103 bool handleAnInputBuffer();
104 bool handleAnOutputBuffer();
Andreas Huberf9334412010-12-15 15:17:42 -0800105
Lajos Molnar09524832014-07-17 14:29:51 -0700106 void releaseAndResetMediaBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800107 void requestCodecNotification();
108 bool isStaleReply(const sp<AMessage> &msg);
109
110 void onConfigure(const sp<AMessage> &format);
111 void onFlush();
Wei Jia704e7262014-06-04 16:21:56 -0700112 void onResume();
Wei Jia2245fc62014-10-02 15:12:25 -0700113 bool onInputBufferFilled(const sp<AMessage> &msg);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800114 void onRenderBuffer(const sp<AMessage> &msg);
115 void onShutdown();
116
117 int32_t mBufferGeneration;
Wei Jia704e7262014-06-04 16:21:56 -0700118 bool mPaused;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800119 AString mComponentName;
Andreas Huberf9334412010-12-15 15:17:42 -0800120
Robert Shih6d0a94e2014-01-23 16:18:22 -0800121 bool supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const;
Lajos Molnar87603c02014-08-20 19:25:30 -0700122 void rememberCodecSpecificData(const sp<AMessage> &format);
Wei Jiac6cfd702014-11-11 16:33:20 -0800123 bool isVideo();
Robert Shih6d0a94e2014-01-23 16:18:22 -0800124
Andreas Huberf9334412010-12-15 15:17:42 -0800125 DISALLOW_EVIL_CONSTRUCTORS(Decoder);
126};
127
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700128struct NuPlayer::CCDecoder : public RefBase {
129 enum {
130 kWhatClosedCaptionData,
131 kWhatTrackAdded,
132 };
133
134 CCDecoder(const sp<AMessage> &notify);
135
136 size_t getTrackCount() const;
137 sp<AMessage> getTrackInfo(size_t index) const;
138 status_t selectTrack(size_t index, bool select);
139 bool isSelected() const;
140 void decode(const sp<ABuffer> &accessUnit);
141 void display(int64_t timeUs);
Chong Zhangb86e68f2014-08-01 13:46:53 -0700142 void flush();
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700143
144private:
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700145 sp<AMessage> mNotify;
146 KeyedVector<int64_t, sp<ABuffer> > mCCMap;
Chong Zhangb86e68f2014-08-01 13:46:53 -0700147 size_t mCurrentChannel;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700148 int32_t mSelectedTrack;
Chong Zhangb86e68f2014-08-01 13:46:53 -0700149 int32_t mTrackIndices[4];
150 Vector<size_t> mFoundChannels;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700151
Chong Zhangb86e68f2014-08-01 13:46:53 -0700152 bool isTrackValid(size_t index) const;
153 int32_t getTrackIndex(size_t channel) const;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700154 bool extractFromSEI(const sp<ABuffer> &accessUnit);
Chong Zhangb86e68f2014-08-01 13:46:53 -0700155 sp<ABuffer> filterCCBuf(const sp<ABuffer> &ccBuf, size_t index);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700156
157 DISALLOW_EVIL_CONSTRUCTORS(CCDecoder);
158};
159
Andreas Huberf9334412010-12-15 15:17:42 -0800160} // namespace android
161
162#endif // NUPLAYER_DECODER_H_