Shalaj Jain | 6550662 | 2013-01-29 18:27:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 3 | * Not a Contribution, Apache license notifications and license are retained |
| 4 | * for attribution purposes only. |
| 5 | * |
| 6 | * Copyright (C) 2010 The Android Open Source Project |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #ifndef DASH_CODEC_H_ |
| 22 | #define DASH_CODEC_H_ |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | #include <android/native_window.h> |
| 26 | #include <media/IOMX.h> |
| 27 | #include <media/stagefright/foundation/AHierarchicalStateMachine.h> |
| 28 | #include <media/stagefright/SkipCutBuffer.h> |
| 29 | #include <OMX_Audio.h> |
Eric (Quic | b4042f0 | 2013-04-17 11:00:01 -0700 | [diff] [blame] | 30 | #include <OMX_Component.h> |
Shalaj Jain | 6550662 | 2013-01-29 18:27:08 -0800 | [diff] [blame] | 31 | |
| 32 | #define TRACK_BUFFER_TIMING 0 |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | struct ABuffer; |
| 37 | struct MemoryDealer; |
| 38 | |
| 39 | struct DashCodec : public AHierarchicalStateMachine { |
| 40 | enum { |
| 41 | kWhatFillThisBuffer = 'fill', |
| 42 | kWhatDrainThisBuffer = 'drai', |
| 43 | kWhatEOS = 'eos ', |
| 44 | kWhatShutdownCompleted = 'scom', |
| 45 | kWhatFlushCompleted = 'fcom', |
| 46 | kWhatOutputFormatChanged = 'outC', |
| 47 | kWhatError = 'erro', |
| 48 | kWhatComponentAllocated = 'cAll', |
| 49 | kWhatComponentConfigured = 'cCon', |
| 50 | kWhatBuffersAllocated = 'allc', |
| 51 | }; |
| 52 | |
| 53 | DashCodec(); |
| 54 | |
| 55 | void setNotificationMessage(const sp<AMessage> &msg); |
| 56 | void initiateSetup(const sp<AMessage> &msg); |
| 57 | void signalFlush(); |
| 58 | void signalResume(); |
| 59 | void initiateShutdown(bool keepComponentAllocated = false); |
| 60 | |
| 61 | void initiateAllocateComponent(const sp<AMessage> &msg); |
| 62 | void initiateConfigureComponent(const sp<AMessage> &msg); |
| 63 | void initiateStart(); |
| 64 | |
| 65 | void signalRequestIDRFrame(); |
Eric (Quic | b4042f0 | 2013-04-17 11:00:01 -0700 | [diff] [blame] | 66 | void queueNextFormat(); |
| 67 | void clearCachedFormats(); |
Shalaj Jain | 6550662 | 2013-01-29 18:27:08 -0800 | [diff] [blame] | 68 | struct PortDescription : public RefBase { |
| 69 | size_t countBuffers(); |
| 70 | IOMX::buffer_id bufferIDAt(size_t index) const; |
| 71 | sp<ABuffer> bufferAt(size_t index) const; |
| 72 | |
| 73 | private: |
| 74 | friend struct DashCodec; |
| 75 | |
| 76 | Vector<IOMX::buffer_id> mBufferIDs; |
| 77 | Vector<sp<ABuffer> > mBuffers; |
| 78 | |
| 79 | PortDescription(); |
| 80 | void addBuffer(IOMX::buffer_id id, const sp<ABuffer> &buffer); |
| 81 | |
| 82 | DISALLOW_EVIL_CONSTRUCTORS(PortDescription); |
| 83 | }; |
| 84 | |
| 85 | protected: |
| 86 | virtual ~DashCodec(); |
| 87 | |
| 88 | private: |
| 89 | struct BaseState; |
| 90 | struct UninitializedState; |
| 91 | struct LoadedState; |
| 92 | struct LoadedToIdleState; |
| 93 | struct IdleToExecutingState; |
| 94 | struct ExecutingState; |
| 95 | struct OutputPortSettingsChangedState; |
| 96 | struct ExecutingToIdleState; |
| 97 | struct IdleToLoadedState; |
| 98 | struct FlushingState; |
| 99 | struct FlushingOutputState; |
| 100 | |
| 101 | enum { |
| 102 | kWhatSetup = 'setu', |
| 103 | kWhatOMXMessage = 'omx ', |
| 104 | kWhatInputBufferFilled = 'inpF', |
| 105 | kWhatOutputBufferDrained = 'outD', |
| 106 | kWhatShutdown = 'shut', |
| 107 | kWhatFlush = 'flus', |
| 108 | kWhatResume = 'resm', |
| 109 | kWhatDrainDeferredMessages = 'drai', |
| 110 | kWhatAllocateComponent = 'allo', |
| 111 | kWhatConfigureComponent = 'conf', |
| 112 | kWhatStart = 'star', |
| 113 | kWhatRequestIDRFrame = 'ridr', |
| 114 | }; |
| 115 | |
| 116 | enum { |
| 117 | kPortIndexInput = 0, |
| 118 | kPortIndexOutput = 1 |
| 119 | }; |
| 120 | |
| 121 | enum { |
| 122 | kFlagIsSecure = 1, |
Surajit Podder | 46ad8f1 | 2013-05-03 15:13:42 +0530 | [diff] [blame] | 123 | kFlagIsSecureOPOnly = 2 |
Shalaj Jain | 6550662 | 2013-01-29 18:27:08 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | struct BufferInfo { |
| 127 | enum Status { |
| 128 | OWNED_BY_US, |
| 129 | OWNED_BY_COMPONENT, |
| 130 | OWNED_BY_UPSTREAM, |
| 131 | OWNED_BY_DOWNSTREAM, |
| 132 | OWNED_BY_NATIVE_WINDOW, |
| 133 | }; |
| 134 | |
| 135 | IOMX::buffer_id mBufferID; |
| 136 | Status mStatus; |
| 137 | |
| 138 | sp<ABuffer> mData; |
| 139 | sp<GraphicBuffer> mGraphicBuffer; |
| 140 | }; |
| 141 | |
| 142 | #if TRACK_BUFFER_TIMING |
| 143 | struct BufferStats { |
| 144 | int64_t mEmptyBufferTimeUs; |
| 145 | int64_t mFillBufferDoneTimeUs; |
| 146 | }; |
| 147 | |
| 148 | KeyedVector<int64_t, BufferStats> mBufferStats; |
| 149 | #endif |
| 150 | |
| 151 | sp<AMessage> mNotify; |
| 152 | |
| 153 | sp<UninitializedState> mUninitializedState; |
| 154 | sp<LoadedState> mLoadedState; |
| 155 | sp<LoadedToIdleState> mLoadedToIdleState; |
| 156 | sp<IdleToExecutingState> mIdleToExecutingState; |
| 157 | sp<ExecutingState> mExecutingState; |
| 158 | sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState; |
| 159 | sp<ExecutingToIdleState> mExecutingToIdleState; |
| 160 | sp<IdleToLoadedState> mIdleToLoadedState; |
| 161 | sp<FlushingState> mFlushingState; |
| 162 | sp<FlushingOutputState> mFlushingOutputState; |
| 163 | sp<SkipCutBuffer> mSkipCutBuffer; |
| 164 | |
| 165 | AString mComponentName; |
| 166 | uint32_t mFlags; |
| 167 | uint32_t mQuirks; |
| 168 | sp<IOMX> mOMX; |
| 169 | IOMX::node_id mNode; |
| 170 | sp<MemoryDealer> mDealer[2]; |
| 171 | |
| 172 | sp<ANativeWindow> mNativeWindow; |
| 173 | |
| 174 | Vector<BufferInfo> mBuffers[2]; |
| 175 | bool mPortEOS[2]; |
| 176 | status_t mInputEOSResult; |
| 177 | |
| 178 | List<sp<AMessage> > mDeferredQueue; |
| 179 | |
| 180 | bool mSentFormat; |
Eric (Quic | b4042f0 | 2013-04-17 11:00:01 -0700 | [diff] [blame] | 181 | bool mPostFormat; |
Shalaj Jain | 6550662 | 2013-01-29 18:27:08 -0800 | [diff] [blame] | 182 | bool mIsEncoder; |
| 183 | |
| 184 | bool mShutdownInProgress; |
| 185 | |
| 186 | // If "mKeepComponentAllocated" we only transition back to Loaded state |
| 187 | // and do not release the component instance. |
| 188 | bool mKeepComponentAllocated; |
| 189 | |
| 190 | int32_t mEncoderDelay; |
| 191 | int32_t mEncoderPadding; |
| 192 | |
| 193 | bool mChannelMaskPresent; |
| 194 | int32_t mChannelMask; |
| 195 | |
| 196 | status_t allocateBuffersOnPort(OMX_U32 portIndex); |
| 197 | status_t freeBuffersOnPort(OMX_U32 portIndex); |
| 198 | status_t freeBuffer(OMX_U32 portIndex, size_t i); |
| 199 | |
| 200 | status_t allocateOutputBuffersFromNativeWindow(); |
| 201 | status_t cancelBufferToNativeWindow(BufferInfo *info); |
| 202 | status_t freeOutputBuffersNotOwnedByComponent(); |
| 203 | BufferInfo *dequeueBufferFromNativeWindow(); |
| 204 | |
| 205 | BufferInfo *findBufferByID( |
| 206 | uint32_t portIndex, IOMX::buffer_id bufferID, |
| 207 | ssize_t *index = NULL); |
| 208 | |
| 209 | status_t setComponentRole(bool isEncoder, const char *mime); |
| 210 | status_t configureCodec(const char *mime, const sp<AMessage> &msg); |
| 211 | |
| 212 | status_t setVideoPortFormatType( |
| 213 | OMX_U32 portIndex, |
| 214 | OMX_VIDEO_CODINGTYPE compressionFormat, |
| 215 | OMX_COLOR_FORMATTYPE colorFormat); |
| 216 | |
| 217 | status_t setSupportedOutputFormat(); |
| 218 | |
| 219 | status_t setupVideoDecoder( |
| 220 | const char *mime, int32_t width, int32_t height); |
| 221 | |
| 222 | status_t setupVideoEncoder( |
| 223 | const char *mime, const sp<AMessage> &msg); |
| 224 | |
| 225 | status_t setVideoFormatOnPort( |
| 226 | OMX_U32 portIndex, |
| 227 | int32_t width, int32_t height, |
| 228 | OMX_VIDEO_CODINGTYPE compressionFormat); |
| 229 | |
| 230 | status_t setupAACCodec( |
| 231 | bool encoder, |
| 232 | int32_t numChannels, int32_t sampleRate, int32_t bitRate, |
| 233 | int32_t aacProfile, bool isADTS); |
| 234 | |
| 235 | status_t selectAudioPortFormat( |
| 236 | OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat); |
| 237 | |
| 238 | status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate); |
| 239 | status_t setupG711Codec(bool encoder, int32_t numChannels); |
| 240 | |
| 241 | status_t setupFlacCodec( |
| 242 | bool encoder, int32_t numChannels, int32_t sampleRate, int32_t compressionLevel); |
| 243 | |
| 244 | status_t setupRawAudioFormat( |
| 245 | OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels); |
| 246 | |
| 247 | status_t setMinBufferSize(OMX_U32 portIndex, size_t size); |
| 248 | |
| 249 | status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg); |
| 250 | status_t setupH263EncoderParameters(const sp<AMessage> &msg); |
| 251 | status_t setupAVCEncoderParameters(const sp<AMessage> &msg); |
| 252 | |
| 253 | status_t verifySupportForProfileAndLevel(int32_t profile, int32_t level); |
| 254 | |
| 255 | status_t configureBitrate( |
| 256 | int32_t bitrate, OMX_VIDEO_CONTROLRATETYPE bitrateMode); |
| 257 | |
| 258 | status_t setupErrorCorrectionParameters(); |
| 259 | |
| 260 | status_t initNativeWindow(); |
| 261 | |
| 262 | status_t pushBlankBuffersToNativeWindow(); |
| 263 | |
| 264 | // Returns true iff all buffers on the given port have status OWNED_BY_US. |
| 265 | bool allYourBuffersAreBelongToUs(OMX_U32 portIndex); |
| 266 | |
| 267 | bool allYourBuffersAreBelongToUs(); |
| 268 | |
| 269 | size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const; |
| 270 | |
| 271 | void deferMessage(const sp<AMessage> &msg); |
| 272 | void processDeferredMessages(); |
| 273 | |
| 274 | void sendFormatChange(); |
| 275 | |
| 276 | void signalError( |
| 277 | OMX_ERRORTYPE error = OMX_ErrorUndefined, |
| 278 | status_t internalError = UNKNOWN_ERROR); |
| 279 | |
| 280 | status_t requestIDRFrame(); |
| 281 | |
| 282 | status_t InitSmoothStreaming(); |
| 283 | bool mSmoothStreaming; |
Eric (Quic | b4042f0 | 2013-04-17 11:00:01 -0700 | [diff] [blame] | 284 | Vector<OMX_PARAM_PORTDEFINITIONTYPE*> mFormats; |
| 285 | Vector<OMX_CONFIG_RECTTYPE*> mOutputCrops; |
Shalaj Jain | 6550662 | 2013-01-29 18:27:08 -0800 | [diff] [blame] | 286 | DISALLOW_EVIL_CONSTRUCTORS(DashCodec); |
| 287 | }; |
| 288 | |
| 289 | } // namespace android |
| 290 | |
| 291 | #endif // DASH_CODEC_H_ |