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 A_CODEC_H_
18 #define A_CODEC_H_
19 
20 #include <stdint.h>
21 #include <android/native_window.h>
22 #include <media/hardware/MetadataBufferType.h>
23 #include <media/MediaCodecInfo.h>
24 #include <media/IOMX.h>
25 #include <media/stagefright/AHierarchicalStateMachine.h>
26 #include <media/stagefright/CodecBase.h>
27 #include <media/stagefright/FrameRenderTracker.h>
28 #include <media/stagefright/MediaDefs.h>
29 #include <media/stagefright/SkipCutBuffer.h>
30 #include <utils/NativeHandle.h>
31 #include <OMX_Audio.h>
32 #include <hardware/gralloc.h>
33 #include <nativebase/nativebase.h>
34 #include <android/hidl/allocator/1.0/IAllocator.h>
35 #include <android/hidl/memory/1.0/IMemory.h>
36 
37 #define TRACK_BUFFER_TIMING     0
38 
39 namespace android {
40 
41 struct ABuffer;
42 class ACodecBufferChannel;
43 class MediaCodecBuffer;
44 class MemoryDealer;
45 struct DescribeColorFormat2Params;
46 struct DataConverter;
47 
48 typedef hidl::allocator::V1_0::IAllocator TAllocator;
49 typedef hidl::memory::V1_0::IMemory TMemory;
50 
51 struct ACodec : public AHierarchicalStateMachine, public CodecBase {
52     ACodec();
53 
54     void initiateSetup(const sp<AMessage> &msg);
55 
56     virtual std::shared_ptr<BufferChannelBase> getBufferChannel() override;
57     virtual void initiateAllocateComponent(const sp<AMessage> &msg);
58     virtual void initiateConfigureComponent(const sp<AMessage> &msg);
59     virtual void initiateCreateInputSurface();
60     virtual void initiateSetInputSurface(const sp<PersistentSurface> &surface);
61     virtual void initiateStart();
62     virtual void initiateShutdown(bool keepComponentAllocated = false);
63 
64     status_t queryCapabilities(
65             const char* owner, const char* name,
66             const char* mime, bool isEncoder,
67             MediaCodecInfo::CapabilitiesWriter* caps);
68 
69     virtual status_t setSurface(const sp<Surface> &surface);
70 
71     virtual void signalFlush();
72     virtual void signalResume();
73 
74     virtual void signalSetParameters(const sp<AMessage> &msg);
75     virtual void signalEndOfInputStream();
76     virtual void signalRequestIDRFrame();
77 
78     // AHierarchicalStateMachine implements the message handling
onMessageReceivedACodec79     virtual void onMessageReceived(const sp<AMessage> &msg) {
80         handleMessage(msg);
81     }
82 
83     // Returns 0 if configuration is not supported.  NOTE: this is treated by
84     // some OMX components as auto level, and by others as invalid level.
85     static int /* OMX_VIDEO_AVCLEVELTYPE */ getAVCLevelFor(
86             int width, int height, int rate, int bitrate,
87             OMX_VIDEO_AVCPROFILEEXTTYPE profile =
88                 (OMX_VIDEO_AVCPROFILEEXTTYPE)OMX_VIDEO_AVCProfileBaseline);
89 
90     // Quirk still supported, even though deprecated
91     enum Quirks {
92         kRequiresAllocateBufferOnInputPorts   = 1,
93         kRequiresAllocateBufferOnOutputPorts  = 2,
94     };
95 
96     static status_t getOMXChannelMapping(size_t numChannels, OMX_AUDIO_CHANNELTYPE map[]);
97 
98 protected:
99     virtual ~ACodec();
100 
101 private:
102     struct BaseState;
103     struct UninitializedState;
104     struct LoadedState;
105     struct LoadedToIdleState;
106     struct IdleToExecutingState;
107     struct ExecutingState;
108     struct OutputPortSettingsChangedState;
109     struct ExecutingToIdleState;
110     struct IdleToLoadedState;
111     struct FlushingState;
112     struct DeathNotifier;
113 
114     enum {
115         kWhatSetup                   = 'setu',
116         kWhatOMXMessage              = 'omx ',
117         // same as kWhatOMXMessage - but only used with
118         // handleMessage during OMX message-list handling
119         kWhatOMXMessageItem          = 'omxI',
120         kWhatOMXMessageList          = 'omxL',
121         kWhatInputBufferFilled       = 'inpF',
122         kWhatOutputBufferDrained     = 'outD',
123         kWhatShutdown                = 'shut',
124         kWhatFlush                   = 'flus',
125         kWhatResume                  = 'resm',
126         kWhatDrainDeferredMessages   = 'drai',
127         kWhatAllocateComponent       = 'allo',
128         kWhatConfigureComponent      = 'conf',
129         kWhatSetSurface              = 'setS',
130         kWhatCreateInputSurface      = 'cisf',
131         kWhatSetInputSurface         = 'sisf',
132         kWhatSignalEndOfInputStream  = 'eois',
133         kWhatStart                   = 'star',
134         kWhatRequestIDRFrame         = 'ridr',
135         kWhatSetParameters           = 'setP',
136         kWhatSubmitOutputMetadataBufferIfEOS = 'subm',
137         kWhatOMXDied                 = 'OMXd',
138         kWhatReleaseCodecInstance    = 'relC',
139         kWhatForceStateTransition    = 'fstt',
140         kWhatCheckIfStuck            = 'Cstk',
141     };
142 
143     enum {
144         kPortIndexInput  = 0,
145         kPortIndexOutput = 1
146     };
147 
148     enum {
149         kFlagIsSecure                                 = 1,
150         kFlagPushBlankBuffersToNativeWindowOnShutdown = 2,
151         kFlagIsGrallocUsageProtected                  = 4,
152     };
153 
154     enum {
155         kVideoGrallocUsage = (GRALLOC_USAGE_HW_TEXTURE
156                             | GRALLOC_USAGE_HW_COMPOSER
157                             | GRALLOC_USAGE_EXTERNAL_DISP),
158     };
159 
160     struct BufferInfo {
161         enum Status {
162             OWNED_BY_US,
163             OWNED_BY_COMPONENT,
164             OWNED_BY_UPSTREAM,
165             OWNED_BY_DOWNSTREAM,
166             OWNED_BY_NATIVE_WINDOW,
167             UNRECOGNIZED,            // not a tracked buffer
168         };
169 
getSafeStatusACodec::BufferInfo170         static inline Status getSafeStatus(BufferInfo *info) {
171             return info == NULL ? UNRECOGNIZED : info->mStatus;
172         }
173 
174         IOMX::buffer_id mBufferID;
175         Status mStatus;
176         unsigned mDequeuedAt;
177 
178         sp<MediaCodecBuffer> mData;  // the client's buffer; if not using data conversion, this is
179                                      // the codec buffer; otherwise, it is allocated separately
180         sp<RefBase> mMemRef;         // and a reference to the IMemory, so it does not go away
181         sp<MediaCodecBuffer> mCodecData;  // the codec's buffer
182         sp<RefBase> mCodecRef;            // and a reference to the IMemory
183 
184         sp<GraphicBuffer> mGraphicBuffer;
185         bool mNewGraphicBuffer;
186         int mFenceFd;
187         FrameRenderTracker::Info *mRenderInfo;
188 
189         // The following field and 4 methods are used for debugging only
190         bool mIsReadFence;
191         // Store |fenceFd| and set read/write flag. Log error, if there is already a fence stored.
192         void setReadFence(int fenceFd, const char *dbg);
193         void setWriteFence(int fenceFd, const char *dbg);
194         // Log error, if the current fence is not a read/write fence.
195         void checkReadFence(const char *dbg);
196         void checkWriteFence(const char *dbg);
197     };
198 
199     static const char *_asString(BufferInfo::Status s);
200     void dumpBuffers(OMX_U32 portIndex);
201 
202     // If |fd| is non-negative, waits for fence with |fd| and logs an error if it fails. Returns
203     // the error code or OK on success. If |fd| is negative, it returns OK
204     status_t waitForFence(int fd, const char *dbg);
205 
206 #if TRACK_BUFFER_TIMING
207     struct BufferStats {
208         int64_t mEmptyBufferTimeUs;
209         int64_t mFillBufferDoneTimeUs;
210     };
211 
212     KeyedVector<int64_t, BufferStats> mBufferStats;
213 #endif
214 
215     sp<UninitializedState> mUninitializedState;
216     sp<LoadedState> mLoadedState;
217     sp<LoadedToIdleState> mLoadedToIdleState;
218     sp<IdleToExecutingState> mIdleToExecutingState;
219     sp<ExecutingState> mExecutingState;
220     sp<OutputPortSettingsChangedState> mOutputPortSettingsChangedState;
221     sp<ExecutingToIdleState> mExecutingToIdleState;
222     sp<IdleToLoadedState> mIdleToLoadedState;
223     sp<FlushingState> mFlushingState;
224     sp<SkipCutBuffer> mSkipCutBuffer;
225     int32_t mSampleRate;
226 
227     AString mComponentName;
228     uint32_t mFlags;
229     sp<IOMX> mOMX;
230     sp<IOMXNode> mOMXNode;
231     int32_t mNodeGeneration;
232     sp<TAllocator> mAllocator[2];
233 
234     bool mUsingNativeWindow;
235     sp<ANativeWindow> mNativeWindow;
236     int mNativeWindowUsageBits;
237     android_native_rect_t mLastNativeWindowCrop;
238     int32_t mLastNativeWindowDataSpace;
239     HDRStaticInfo mLastHDRStaticInfo;
240     sp<ABuffer> mHdr10PlusScratchBuffer;
241     sp<ABuffer> mLastHdr10PlusBuffer;
242     sp<AMessage> mConfigFormat;
243     sp<AMessage> mInputFormat;
244     sp<AMessage> mOutputFormat;
245 
246     // Initial output format + configuration params that is reused as the base for all subsequent
247     // format updates. This will equal to mOutputFormat until the first actual frame is received.
248     sp<AMessage> mBaseOutputFormat;
249 
250     FrameRenderTracker mRenderTracker; // render information for buffers rendered by ACodec
251     Vector<BufferInfo> mBuffers[2];
252     bool mPortEOS[2];
253     status_t mInputEOSResult;
254 
255     List<sp<AMessage> > mDeferredQueue;
256 
257     sp<AMessage> mLastOutputFormat;
258     bool mIsVideo;
259     bool mIsImage;
260     bool mIsEncoder;
261     bool mFatalError;
262     bool mShutdownInProgress;
263     bool mExplicitShutdown;
264     bool mIsLegacyVP9Decoder;
265 
266     // If "mKeepComponentAllocated" we only transition back to Loaded state
267     // and do not release the component instance.
268     bool mKeepComponentAllocated;
269 
270     int32_t mEncoderDelay;
271     int32_t mEncoderPadding;
272     int32_t mRotationDegrees;
273 
274     bool mChannelMaskPresent;
275     int32_t mChannelMask;
276     unsigned mDequeueCounter;
277     IOMX::PortMode mPortMode[2];
278     int32_t mMetadataBuffersToSubmit;
279     size_t mNumUndequeuedBuffers;
280     sp<DataConverter> mConverter[2];
281 
282     sp<IGraphicBufferSource> mGraphicBufferSource;
283     int64_t mRepeatFrameDelayUs;
284     int64_t mMaxPtsGapUs;
285     float mMaxFps;
286     double mFps;
287     double mCaptureFps;
288     bool mCreateInputBuffersSuspended;
289     std::optional<uint32_t> mLatency;
290 
291     bool mTunneled;
292 
293     OMX_INDEXTYPE mDescribeColorAspectsIndex;
294     OMX_INDEXTYPE mDescribeHDRStaticInfoIndex;
295     OMX_INDEXTYPE mDescribeHDR10PlusInfoIndex;
296 
297     std::shared_ptr<ACodecBufferChannel> mBufferChannel;
298 
299     int32_t mStateGeneration;
300 
301     enum {
302         kExtensionsUnchecked,
303         kExtensionsNone,
304         kExtensionsExist,
305     } mVendorExtensionsStatus;
306 
307     status_t setCyclicIntraMacroblockRefresh(const sp<AMessage> &msg, int32_t mode);
308     status_t allocateBuffersOnPort(OMX_U32 portIndex);
309     status_t freeBuffersOnPort(OMX_U32 portIndex);
310     status_t freeBuffer(OMX_U32 portIndex, size_t i);
311 
312     status_t handleSetSurface(const sp<Surface> &surface);
313     status_t setPortMode(int32_t portIndex, IOMX::PortMode mode);
314     status_t setupNativeWindowSizeFormatAndUsage(
315             ANativeWindow *nativeWindow /* nonnull */, int *finalUsage /* nonnull */,
316             bool reconnect);
317 
318     status_t configureOutputBuffersFromNativeWindow(
319             OMX_U32 *nBufferCount, OMX_U32 *nBufferSize,
320             OMX_U32 *nMinUndequeuedBuffers, bool preregister);
321     status_t allocateOutputMetadataBuffers();
322     status_t submitOutputMetadataBuffer();
323     void signalSubmitOutputMetadataBufferIfEOS_workaround();
324     status_t allocateOutputBuffersFromNativeWindow();
325     status_t cancelBufferToNativeWindow(BufferInfo *info);
326     status_t freeOutputBuffersNotOwnedByComponent();
327     BufferInfo *dequeueBufferFromNativeWindow();
328 
storingMetadataInDecodedBuffersACodec329     inline bool storingMetadataInDecodedBuffers() {
330         return (mPortMode[kPortIndexOutput] == IOMX::kPortModeDynamicANWBuffer) && !mIsEncoder;
331     }
332 
usingSecureBufferOnEncoderOutputACodec333     inline bool usingSecureBufferOnEncoderOutput() {
334         return (mPortMode[kPortIndexOutput] == IOMX::kPortModePresetSecureBuffer) && mIsEncoder;
335     }
336 
337     BufferInfo *findBufferByID(
338             uint32_t portIndex, IOMX::buffer_id bufferID,
339             ssize_t *index = NULL);
340 
341     status_t fillBuffer(BufferInfo *info);
342 
343     status_t setComponentRole(bool isEncoder, const char *mime);
344 
345     status_t configureCodec(const char *mime, const sp<AMessage> &msg);
346 
347     status_t configureTunneledVideoPlayback(int32_t audioHwSync,
348             const sp<ANativeWindow> &nativeWindow);
349 
350     status_t setVideoPortFormatType(
351             OMX_U32 portIndex,
352             OMX_VIDEO_CODINGTYPE compressionFormat,
353             OMX_COLOR_FORMATTYPE colorFormat,
354             bool usingNativeBuffers = false);
355 
356     status_t setSupportedOutputFormat(bool getLegacyFlexibleFormat);
357 
358     status_t setupVideoDecoder(
359             const char *mime, const sp<AMessage> &msg, bool usingNativeBuffers, bool haveSwRenderer,
360             sp<AMessage> &outputformat);
361 
362     status_t setupVideoEncoder(
363             const char *mime, const sp<AMessage> &msg,
364             sp<AMessage> &outputformat, sp<AMessage> &inputformat);
365 
366     status_t setVideoFormatOnPort(
367             OMX_U32 portIndex,
368             int32_t width, int32_t height,
369             OMX_VIDEO_CODINGTYPE compressionFormat, float frameRate = -1.0);
370 
371     // sets |portIndex| port buffer numbers to be |bufferNum|. NOTE: Component could reject
372     // this setting if the |bufferNum| is less than the minimum buffer num of the port.
373     status_t setPortBufferNum(OMX_U32 portIndex, int bufferNum);
374 
375     // gets index or sets it to 0 on error. Returns error from codec.
376     status_t initDescribeColorAspectsIndex();
377 
378     // sets |params|. If |readBack| is true, it re-gets them afterwards if set succeeded.
379     // returns the codec error.
380     status_t setCodecColorAspects(DescribeColorAspectsParams &params, bool readBack = false);
381 
382     // gets |params|; returns the codec error. |param| should not change on error.
383     status_t getCodecColorAspects(DescribeColorAspectsParams &params);
384 
385     // gets dataspace guidance from codec and platform. |params| should be set up with the color
386     // aspects to use. If |tryCodec| is true, the codec is queried first. If it succeeds, we
387     // return OK. Otherwise, we fall back to the platform guidance and return the codec error;
388     // though, we return OK if the codec failed with UNSUPPORTED, as codec guidance is optional.
389     status_t getDataSpace(
390             DescribeColorAspectsParams &params, android_dataspace *dataSpace /* nonnull */,
391             bool tryCodec);
392 
393     // sets color aspects for the encoder for certain |width/height| based on |configFormat|, and
394     // set resulting color config into |outputFormat|. If |usingNativeWindow| is true, we use
395     // video defaults if config is unspecified. Returns error from the codec.
396     status_t setColorAspectsForVideoDecoder(
397             int32_t width, int32_t height, bool usingNativeWindow,
398             const sp<AMessage> &configFormat, sp<AMessage> &outputFormat);
399 
400     // gets color aspects for the encoder for certain |width/height| based on |configFormat|, and
401     // set resulting color config into |outputFormat|. If |dataSpace| is non-null, it requests
402     // dataspace guidance from the codec and platform and sets it into |dataSpace|. Returns the
403     // error from the codec.
404     status_t getColorAspectsAndDataSpaceForVideoDecoder(
405             int32_t width, int32_t height, const sp<AMessage> &configFormat,
406             sp<AMessage> &outputFormat, android_dataspace *dataSpace);
407 
408     // sets color aspects for the video encoder assuming bytebuffer mode for certain |configFormat|
409     // and sets resulting color config into |outputFormat|. For mediarecorder, also set dataspace
410     // into |inputFormat|. Returns the error from the codec.
411     status_t setColorAspectsForVideoEncoder(
412             const sp<AMessage> &configFormat,
413             sp<AMessage> &outputFormat, sp<AMessage> &inputFormat);
414 
415     // sets color aspects for the video encoder in surface mode. This basically sets the default
416     // video values for unspecified aspects and sets the dataspace to use in the input format.
417     // Also sets the dataspace into |dataSpace|.
418     // Returns any codec errors during this configuration, except for optional steps.
419     status_t setInitialColorAspectsForVideoEncoderSurfaceAndGetDataSpace(
420             android_dataspace *dataSpace /* nonnull */);
421 
422     // gets color aspects for the video encoder input port and sets them into the |format|.
423     // Returns any codec errors.
424     status_t getInputColorAspectsForVideoEncoder(sp<AMessage> &format);
425 
426     // updates the encoder output format with |aspects| defaulting to |dataSpace| for
427     // unspecified values.
428     void onDataSpaceChanged(android_dataspace dataSpace, const ColorAspects &aspects);
429 
430     // notifies the codec that the config with |configIndex| has changed, the value
431     // can be queried by OMX getConfig, and the config should be applied to the next
432     // output buffer notified after this callback.
433     void onConfigUpdate(OMX_INDEXTYPE configIndex);
434 
435     // gets index or sets it to 0 on error. Returns error from codec.
436     status_t initDescribeHDRStaticInfoIndex();
437 
438     // sets HDR static metadata for the video encoder/decoder based on |configFormat|, and
439     // sets resulting HDRStaticInfo config into |outputFormat|. Returns error from the codec.
440     status_t setHDRStaticInfoForVideoCodec(
441             OMX_U32 portIndex, const sp<AMessage> &configFormat, sp<AMessage> &outputFormat);
442 
443     // sets |params|. Returns the codec error.
444     status_t setHDRStaticInfo(const DescribeHDRStaticInfoParams &params);
445 
446     // sets |hdr10PlusInfo|. Returns the codec error.
447     status_t setHdr10PlusInfo(const sp<ABuffer> &hdr10PlusInfo);
448 
449     // gets |params|. Returns the codec error.
450     status_t getHDRStaticInfo(DescribeHDRStaticInfoParams &params);
451 
452     // gets HDR static information for the video encoder/decoder port and sets them into |format|.
453     status_t getHDRStaticInfoForVideoCodec(OMX_U32 portIndex, sp<AMessage> &format);
454 
455     // gets DescribeHDR10PlusInfoParams params. If |paramSizeUsed| is zero, it's
456     // possible that the returned DescribeHDR10PlusInfoParams only has the
457     // nParamSizeUsed field updated, because the size of the storage is insufficient.
458     // In this case, getHDR10PlusInfo() should be called again with |paramSizeUsed|
459     // specified to the previous returned value.
460     DescribeHDR10PlusInfoParams* getHDR10PlusInfo(size_t paramSizeUsed = 0);
461 
462     typedef struct drcParams {
463         int32_t drcCut;
464         int32_t drcBoost;
465         int32_t heavyCompression;
466         int32_t targetRefLevel;
467         int32_t encodedTargetLevel;
468         int32_t effectType;
469     } drcParams_t;
470 
471     status_t setupAACCodec(
472             bool encoder,
473             int32_t numChannels, int32_t sampleRate, int32_t bitRate,
474             int32_t aacProfile, bool isADTS, int32_t sbrMode,
475             int32_t maxOutputChannelCount, const drcParams_t& drc,
476             int32_t pcmLimiterEnable);
477 
478     status_t setupAC3Codec(bool encoder, int32_t numChannels, int32_t sampleRate);
479 
480     status_t setupEAC3Codec(bool encoder, int32_t numChannels, int32_t sampleRate);
481 
482     status_t setupAC4Codec(bool encoder, int32_t numChannels, int32_t sampleRate);
483 
484     status_t selectAudioPortFormat(
485             OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat);
486 
487     status_t setupAMRCodec(bool encoder, bool isWAMR, int32_t bitRate);
488     status_t setupG711Codec(bool encoder, int32_t sampleRate, int32_t numChannels);
489 
490     status_t setupFlacCodec(
491             bool encoder, int32_t numChannels, int32_t sampleRate, int32_t compressionLevel,
492             AudioEncoding encoding);
493 
494     status_t setupRawAudioFormat(
495             OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels,
496             AudioEncoding encoding = kAudioEncodingPcm16bit);
497 
498     status_t setPriority(int32_t priority);
499     status_t setLatency(uint32_t latency);
500     status_t getLatency(uint32_t *latency);
501     status_t setAudioPresentation(int32_t presentationId, int32_t programId);
502     status_t setOperatingRate(float rateFloat, bool isVideo);
503     status_t getIntraRefreshPeriod(uint32_t *intraRefreshPeriod);
504     status_t setIntraRefreshPeriod(uint32_t intraRefreshPeriod, bool inConfigure);
505 
506     // Configures temporal layering based on |msg|. |inConfigure| shall be true iff this is called
507     // during configure() call. on success the configured layering is set in |outputFormat|. If
508     // |outputFormat| is mOutputFormat, it is copied to trigger an output format changed event.
509     status_t configureTemporalLayers(
510             const sp<AMessage> &msg, bool inConfigure, sp<AMessage> &outputFormat);
511 
512     status_t setMinBufferSize(OMX_U32 portIndex, size_t size);
513 
514     status_t setupMPEG4EncoderParameters(const sp<AMessage> &msg);
515     status_t setupH263EncoderParameters(const sp<AMessage> &msg);
516     status_t setupAVCEncoderParameters(const sp<AMessage> &msg);
517     status_t setupHEVCEncoderParameters(const sp<AMessage> &msg, sp<AMessage> &outputFormat);
518     status_t setupVPXEncoderParameters(const sp<AMessage> &msg, sp<AMessage> &outputFormat);
519 
520     status_t verifySupportForProfileAndLevel(
521             OMX_U32 portIndex, int32_t profile, int32_t level);
522 
523     status_t configureImageGrid(const sp<AMessage> &msg, sp<AMessage> &outputFormat);
524     status_t configureBitrate(
525             OMX_VIDEO_CONTROLRATETYPE bitrateMode, int32_t bitrate, int32_t quality = 0);
526     void configureEncoderLatency(const sp<AMessage> &msg);
527 
528     status_t setupErrorCorrectionParameters();
529 
530     // Returns true iff all buffers on the given port have status
531     // OWNED_BY_US or OWNED_BY_NATIVE_WINDOW.
532     bool allYourBuffersAreBelongToUs(OMX_U32 portIndex);
533 
534     bool allYourBuffersAreBelongToUs();
535 
536     void waitUntilAllPossibleNativeWindowBuffersAreReturnedToUs();
537 
538     size_t countBuffersOwnedByComponent(OMX_U32 portIndex) const;
539     size_t countBuffersOwnedByNativeWindow() const;
540 
541     void deferMessage(const sp<AMessage> &msg);
542     void processDeferredMessages();
543 
544     void onFrameRendered(int64_t mediaTimeUs, nsecs_t systemNano);
545     // called when we have dequeued a buffer |buf| from the native window to track render info.
546     // |fenceFd| is the dequeue fence, and |info| points to the buffer info where this buffer is
547     // stored.
548     void updateRenderInfoForDequeuedBuffer(
549             ANativeWindowBuffer *buf, int fenceFd, BufferInfo *info);
550 
551     // Checks to see if any frames have rendered up until |until|, and to notify client
552     // (MediaCodec) of rendered frames up-until the frame pointed to by |until| or the first
553     // unrendered frame. These frames are removed from the render queue.
554     // If |dropIncomplete| is true, unrendered frames up-until |until| will be dropped from the
555     // queue, allowing all rendered framed up till then to be notified of.
556     // (This will effectively clear the render queue up-until (and including) |until|.)
557     // If |until| is NULL, or is not in the rendered queue, this method will check all frames.
558     void notifyOfRenderedFrames(
559             bool dropIncomplete = false, FrameRenderTracker::Info *until = NULL);
560 
561     // Pass |expectedFormat| to print a warning if the format differs from it.
562     // Using sp<> instead of const sp<>& because expectedFormat is likely the current mOutputFormat
563     // which will get updated inside.
564     void onOutputFormatChanged(sp<const AMessage> expectedFormat = NULL);
565     void addKeyFormatChangesToRenderBufferNotification(sp<AMessage> &notify);
566     void sendFormatChange();
567 
568     status_t getPortFormat(OMX_U32 portIndex, sp<AMessage> &notify);
569 
570     void signalError(
571             OMX_ERRORTYPE error = OMX_ErrorUndefined,
572             status_t internalError = UNKNOWN_ERROR);
573 
574     status_t requestIDRFrame();
575     status_t setParameters(const sp<AMessage> &params);
576 
577     // set vendor extension parameters specified in params that are supported by the codec
578     status_t setVendorParameters(const sp<AMessage> &params);
579 
580     // get vendor extension parameters supported by the codec for a specific port and add it to
581     // |format|
582     status_t getVendorParameters(OMX_U32 portIndex, sp<AMessage> &format);
583 
584     // Send EOS on input stream.
585     void onSignalEndOfInputStream();
586 
587     // Force EXEC->IDLE->LOADED shutdown sequence if not stale.
588     void forceStateTransition(int generation);
589 
590     DISALLOW_EVIL_CONSTRUCTORS(ACodec);
591 };
592 
593 }  // namespace android
594 
595 #endif  // A_CODEC_H_
596