Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | namespace android { |
| 26 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 27 | struct ABuffer; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 28 | struct MediaCodec; |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 29 | struct MediaBuffer; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 30 | |
| 31 | struct NuPlayer::Decoder : public AHandler { |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 32 | Decoder(const sp<AMessage> ¬ify, |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 33 | const sp<Source> &source, |
| 34 | const sp<Renderer> &renderer = NULL, |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 35 | const sp<NativeWindowWrapper> &nativeWindow = NULL); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 36 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 37 | virtual void configure(const sp<AMessage> &format); |
| 38 | virtual void init(); |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 39 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 40 | virtual void setRenderer(const sp<Renderer> &renderer); |
| 41 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 42 | status_t getInputBuffers(Vector<sp<ABuffer> > *dstBuffers) const; |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 43 | virtual void signalFlush(const sp<AMessage> &format = NULL); |
| 44 | virtual void signalUpdateFormat(const sp<AMessage> &format); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 45 | virtual void signalResume(); |
| 46 | virtual void initiateShutdown(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 47 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 48 | virtual bool supportsSeamlessFormatChange(const sp<AMessage> &to) const; |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 49 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 50 | enum { |
| 51 | kWhatFillThisBuffer = 'flTB', |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 52 | kWhatRenderBufferTime = 'rnBT', |
| 53 | kWhatVideoSizeChanged = 'viSC', |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 54 | kWhatFlushCompleted = 'flsC', |
| 55 | kWhatShutdownCompleted = 'shDC', |
| 56 | kWhatEOS = 'eos ', |
| 57 | kWhatError = 'err ', |
| 58 | }; |
| 59 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 60 | protected: |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 61 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 62 | virtual ~Decoder(); |
| 63 | |
| 64 | virtual void onMessageReceived(const sp<AMessage> &msg); |
| 65 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 66 | enum { |
Andreas Huber | 078cfcf | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 67 | kWhatCodecNotify = 'cdcN', |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 68 | kWhatConfigure = 'conf', |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 69 | kWhatSetRenderer = 'setR', |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 70 | kWhatGetInputBuffers = 'gInB', |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 71 | kWhatInputBufferFilled = 'inpF', |
| 72 | kWhatRenderBuffer = 'rndr', |
| 73 | kWhatFlush = 'flus', |
| 74 | kWhatShutdown = 'shuD', |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 75 | kWhatUpdateFormat = 'uFmt', |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 78 | private: |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 79 | sp<AMessage> mNotify; |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 80 | sp<NativeWindowWrapper> mNativeWindow; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 81 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 82 | sp<Source> mSource; |
| 83 | sp<Renderer> mRenderer; |
| 84 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 85 | sp<AMessage> mInputFormat; |
| 86 | sp<AMessage> mOutputFormat; |
| 87 | sp<MediaCodec> mCodec; |
Andreas Huber | 078cfcf | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 88 | sp<ALooper> mCodecLooper; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 89 | sp<ALooper> mDecoderLooper; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 90 | |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 91 | List<sp<AMessage> > mPendingInputMessages; |
| 92 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 93 | Vector<sp<ABuffer> > mInputBuffers; |
| 94 | Vector<sp<ABuffer> > mOutputBuffers; |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 95 | Vector<sp<ABuffer> > mCSDsForCurrentFormat; |
| 96 | Vector<sp<ABuffer> > mCSDsToSubmit; |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 97 | Vector<bool> mInputBufferIsDequeued; |
| 98 | Vector<MediaBuffer *> mMediaBuffers; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 99 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 100 | int64_t mSkipRenderingUntilMediaTimeUs; |
| 101 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 102 | void handleError(int32_t err); |
| 103 | bool handleAnInputBuffer(); |
| 104 | bool handleAnOutputBuffer(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 105 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 106 | void releaseAndResetMediaBuffers(); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 107 | void requestCodecNotification(); |
| 108 | bool isStaleReply(const sp<AMessage> &msg); |
| 109 | |
| 110 | void onConfigure(const sp<AMessage> &format); |
| 111 | void onFlush(); |
Wei Jia | 704e726 | 2014-06-04 16:21:56 -0700 | [diff] [blame] | 112 | void onResume(); |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 113 | bool onInputBufferFilled(const sp<AMessage> &msg); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 114 | void onRenderBuffer(const sp<AMessage> &msg); |
| 115 | void onShutdown(); |
| 116 | |
| 117 | int32_t mBufferGeneration; |
Wei Jia | 704e726 | 2014-06-04 16:21:56 -0700 | [diff] [blame] | 118 | bool mPaused; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 119 | AString mComponentName; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 120 | |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 121 | bool supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const; |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 122 | void rememberCodecSpecificData(const sp<AMessage> &format); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 123 | bool isVideo(); |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 124 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 125 | DISALLOW_EVIL_CONSTRUCTORS(Decoder); |
| 126 | }; |
| 127 | |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 128 | struct NuPlayer::CCDecoder : public RefBase { |
| 129 | enum { |
| 130 | kWhatClosedCaptionData, |
| 131 | kWhatTrackAdded, |
| 132 | }; |
| 133 | |
| 134 | CCDecoder(const sp<AMessage> ¬ify); |
| 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 Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 142 | void flush(); |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 143 | |
| 144 | private: |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 145 | sp<AMessage> mNotify; |
| 146 | KeyedVector<int64_t, sp<ABuffer> > mCCMap; |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 147 | size_t mCurrentChannel; |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 148 | int32_t mSelectedTrack; |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 149 | int32_t mTrackIndices[4]; |
| 150 | Vector<size_t> mFoundChannels; |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 151 | |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 152 | bool isTrackValid(size_t index) const; |
| 153 | int32_t getTrackIndex(size_t channel) const; |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 154 | bool extractFromSEI(const sp<ABuffer> &accessUnit); |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 155 | sp<ABuffer> filterCCBuf(const sp<ABuffer> &ccBuf, size_t index); |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 156 | |
| 157 | DISALLOW_EVIL_CONSTRUCTORS(CCDecoder); |
| 158 | }; |
| 159 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 160 | } // namespace android |
| 161 | |
| 162 | #endif // NUPLAYER_DECODER_H_ |