• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012, 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 MEDIA_CODEC_H_
18 
19 #define MEDIA_CODEC_H_
20 
21 #include <list>
22 #include <memory>
23 #include <vector>
24 
25 #include <gui/IGraphicBufferProducer.h>
26 #include <media/hardware/CryptoAPI.h>
27 #include <media/MediaCodecInfo.h>
28 #include <media/MediaMetrics.h>
29 #include <media/MediaProfiles.h>
30 #include <media/stagefright/foundation/AHandler.h>
31 #include <media/stagefright/CodecErrorLog.h>
32 #include <media/stagefright/FrameRenderTracker.h>
33 #include <media/stagefright/MediaHistogram.h>
34 #include <media/stagefright/PlaybackDurationAccumulator.h>
35 #include <media/stagefright/VideoRenderQualityTracker.h>
36 #include <utils/Vector.h>
37 
38 class C2Buffer;
39 class C2GraphicBlock;
40 class C2LinearBlock;
41 
42 namespace aidl {
43 namespace android {
44 namespace media {
45 class MediaResourceParcel;
46 class ClientConfigParcel;
47 } // media
48 } // android
49 } // aidl
50 
51 namespace android {
52 
53 struct ABuffer;
54 struct AMessage;
55 struct AReplyToken;
56 struct AString;
57 struct BatteryChecker;
58 class BufferChannelBase;
59 struct CodecBase;
60 struct CodecParameterDescriptor;
61 class IBatteryStats;
62 struct ICrypto;
63 class CryptoAsync;
64 class MediaCodecBuffer;
65 class IMemory;
66 struct PersistentSurface;
67 class RenderedFrameInfo;
68 class SoftwareRenderer;
69 class Surface;
70 namespace hardware {
71 namespace cas {
72 namespace native {
73 namespace V1_0 {
74 struct IDescrambler;
75 }}}}
76 
77 using hardware::cas::native::V1_0::IDescrambler;
78 using aidl::android::media::MediaResourceParcel;
79 using aidl::android::media::ClientConfigParcel;
80 
81 struct MediaCodec : public AHandler {
82     enum Domain {
83         DOMAIN_UNKNOWN = 0,
84         DOMAIN_VIDEO = 1,
85         DOMAIN_AUDIO = 2,
86         DOMAIN_IMAGE = 3
87     };
88 
89     enum ConfigureFlags {
90         CONFIGURE_FLAG_ENCODE           = 1,
91         CONFIGURE_FLAG_USE_BLOCK_MODEL  = 2,
92         CONFIGURE_FLAG_USE_CRYPTO_ASYNC = 4,
93     };
94 
95     enum BufferFlags {
96         BUFFER_FLAG_SYNCFRAME     = 1,
97         BUFFER_FLAG_CODECCONFIG   = 2,
98         BUFFER_FLAG_EOS           = 4,
99         BUFFER_FLAG_PARTIAL_FRAME = 8,
100         BUFFER_FLAG_MUXER_DATA    = 16,
101         BUFFER_FLAG_DECODE_ONLY   = 32,
102     };
103 
104     enum CVODegree {
105         CVO_DEGREE_0   = 0,
106         CVO_DEGREE_90  = 90,
107         CVO_DEGREE_180 = 180,
108         CVO_DEGREE_270 = 270,
109     };
110 
111     enum {
112         CB_INPUT_AVAILABLE = 1,
113         CB_OUTPUT_AVAILABLE = 2,
114         CB_ERROR = 3,
115         CB_OUTPUT_FORMAT_CHANGED = 4,
116         CB_RESOURCE_RECLAIMED = 5,
117         CB_CRYPTO_ERROR = 6,
118     };
119 
120     static const pid_t kNoPid = -1;
121     static const uid_t kNoUid = -1;
122 
123     static sp<MediaCodec> CreateByType(
124             const sp<ALooper> &looper, const AString &mime, bool encoder, status_t *err = NULL,
125             pid_t pid = kNoPid, uid_t uid = kNoUid);
126 
127     static sp<MediaCodec> CreateByType(
128             const sp<ALooper> &looper, const AString &mime, bool encoder, status_t *err,
129             pid_t pid, uid_t uid, sp<AMessage> format);
130 
131     static sp<MediaCodec> CreateByComponentName(
132             const sp<ALooper> &looper, const AString &name, status_t *err = NULL,
133             pid_t pid = kNoPid, uid_t uid = kNoUid);
134 
135     static sp<PersistentSurface> CreatePersistentInputSurface();
136 
137     status_t configure(
138             const sp<AMessage> &format,
139             const sp<Surface> &nativeWindow,
140             const sp<ICrypto> &crypto,
141             uint32_t flags);
142 
143     status_t configure(
144             const sp<AMessage> &format,
145             const sp<Surface> &nativeWindow,
146             const sp<ICrypto> &crypto,
147             const sp<IDescrambler> &descrambler,
148             uint32_t flags);
149 
150     status_t releaseCrypto();
151 
152     status_t setCallback(const sp<AMessage> &callback);
153 
154     status_t setOnFrameRenderedNotification(const sp<AMessage> &notify);
155 
156     status_t setOnFirstTunnelFrameReadyNotification(const sp<AMessage> &notify);
157 
158     status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
159 
160     status_t setInputSurface(const sp<PersistentSurface> &surface);
161 
162     status_t start();
163 
164     // Returns to a state in which the component remains allocated but
165     // unconfigured.
166     status_t stop();
167 
168     // Resets the codec to the INITIALIZED state.  Can be called after an error
169     // has occured to make the codec usable.
170     status_t reset();
171 
172     // Client MUST call release before releasing final reference to this
173     // object.
174     status_t release();
175 
176     status_t releaseAsync(const sp<AMessage> &notify);
177 
178     status_t flush();
179 
180     status_t queueInputBuffer(
181             size_t index,
182             size_t offset,
183             size_t size,
184             int64_t presentationTimeUs,
185             uint32_t flags,
186             AString *errorDetailMsg = NULL);
187 
188     status_t queueSecureInputBuffer(
189             size_t index,
190             size_t offset,
191             const CryptoPlugin::SubSample *subSamples,
192             size_t numSubSamples,
193             const uint8_t key[16],
194             const uint8_t iv[16],
195             CryptoPlugin::Mode mode,
196             const CryptoPlugin::Pattern &pattern,
197             int64_t presentationTimeUs,
198             uint32_t flags,
199             AString *errorDetailMsg = NULL);
200 
201     status_t queueBuffer(
202             size_t index,
203             const std::shared_ptr<C2Buffer> &buffer,
204             int64_t presentationTimeUs,
205             uint32_t flags,
206             const sp<AMessage> &tunings,
207             AString *errorDetailMsg = NULL);
208 
209     status_t queueEncryptedBuffer(
210             size_t index,
211             const sp<hardware::HidlMemory> &memory,
212             size_t offset,
213             const CryptoPlugin::SubSample *subSamples,
214             size_t numSubSamples,
215             const uint8_t key[16],
216             const uint8_t iv[16],
217             CryptoPlugin::Mode mode,
218             const CryptoPlugin::Pattern &pattern,
219             int64_t presentationTimeUs,
220             uint32_t flags,
221             const sp<AMessage> &tunings,
222             AString *errorDetailMsg = NULL);
223 
224     std::shared_ptr<C2Buffer> decrypt(
225             const std::shared_ptr<C2Buffer> &buffer,
226             const CryptoPlugin::SubSample *subSamples,
227             size_t numSubSamples,
228             const uint8_t key[16],
229             const uint8_t iv[16],
230             CryptoPlugin::Mode mode,
231             const CryptoPlugin::Pattern &pattern);
232 
233     status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs = 0ll);
234 
235     status_t dequeueOutputBuffer(
236             size_t *index,
237             size_t *offset,
238             size_t *size,
239             int64_t *presentationTimeUs,
240             uint32_t *flags,
241             int64_t timeoutUs = 0ll);
242 
243     status_t renderOutputBufferAndRelease(size_t index, int64_t timestampNs);
244     status_t renderOutputBufferAndRelease(size_t index);
245     status_t releaseOutputBuffer(size_t index);
246 
247     status_t signalEndOfInputStream();
248 
249     status_t getOutputFormat(sp<AMessage> *format) const;
250     status_t getInputFormat(sp<AMessage> *format) const;
251 
252     status_t getInputBuffers(Vector<sp<MediaCodecBuffer> > *buffers) const;
253     status_t getOutputBuffers(Vector<sp<MediaCodecBuffer> > *buffers) const;
254 
255     status_t getOutputBuffer(size_t index, sp<MediaCodecBuffer> *buffer);
256     status_t getOutputFormat(size_t index, sp<AMessage> *format);
257     status_t getInputBuffer(size_t index, sp<MediaCodecBuffer> *buffer);
258 
259     status_t setSurface(const sp<Surface> &nativeWindow);
260 
261     status_t requestIDRFrame();
262 
263     // Notification will be posted once there "is something to do", i.e.
264     // an input/output buffer has become available, a format change is
265     // pending, an error is pending.
266     void requestActivityNotification(const sp<AMessage> &notify);
267 
268     status_t getName(AString *componentName) const;
269 
270     status_t getCodecInfo(sp<MediaCodecInfo> *codecInfo) const;
271 
272     status_t getMetrics(mediametrics_handle_t &reply);
273 
274     status_t setParameters(const sp<AMessage> &params);
275 
276     status_t querySupportedVendorParameters(std::vector<std::string> *names);
277     status_t describeParameter(const std::string &name, CodecParameterDescriptor *desc);
278     status_t subscribeToVendorParameters(const std::vector<std::string> &names);
279     status_t unsubscribeFromVendorParameters(const std::vector<std::string> &names);
280 
281     // Create a MediaCodec notification message from a list of rendered or dropped render infos
282     // by adding rendered frame information to a base notification message. Returns the number
283     // of frames that were rendered.
284     static size_t CreateFramesRenderedMessage(
285             const std::list<RenderedFrameInfo> &done, sp<AMessage> &msg);
286     static size_t CreateFramesRenderedMessage(
287             const std::list<FrameRenderTracker::Info> &done, sp<AMessage> &msg);
288 
289     static status_t CanFetchLinearBlock(
290             const std::vector<std::string> &names, bool *isCompatible);
291 
292     static std::shared_ptr<C2LinearBlock> FetchLinearBlock(
293             size_t capacity, const std::vector<std::string> &names);
294 
295     static status_t CanFetchGraphicBlock(
296             const std::vector<std::string> &names, bool *isCompatible);
297 
298     static std::shared_ptr<C2GraphicBlock> FetchGraphicBlock(
299             int32_t width,
300             int32_t height,
301             int32_t format,
302             uint64_t usage,
303             const std::vector<std::string> &names);
304 
305     template <typename T>
306     struct WrapperObject : public RefBase {
WrapperObjectMediaCodec::WrapperObject307         WrapperObject(const T& v) : value(v) {}
WrapperObjectMediaCodec::WrapperObject308         WrapperObject(T&& v) : value(std::move(v)) {}
309         T value;
310     };
311 
getErrorLogMediaCodec312     inline CodecErrorLog &getErrorLog() { return mErrorLog; }
313 
314 protected:
315     virtual ~MediaCodec();
316     virtual void onMessageReceived(const sp<AMessage> &msg);
317 
318 private:
319     // used by ResourceManagerClient
320     status_t reclaim(bool force = false);
321     friend struct ResourceManagerClient;
322 
323 private:
324     enum State {
325         UNINITIALIZED,
326         INITIALIZING,
327         INITIALIZED,
328         CONFIGURING,
329         CONFIGURED,
330         STARTING,
331         STARTED,
332         FLUSHING,
333         FLUSHED,
334         STOPPING,
335         RELEASING,
336     };
337     std::string stateString(State state);
338     std::string apiStateString();
339 
340     enum {
341         kPortIndexInput         = 0,
342         kPortIndexOutput        = 1,
343     };
344 
345     enum {
346         kWhatInit                           = 'init',
347         kWhatConfigure                      = 'conf',
348         kWhatSetSurface                     = 'sSur',
349         kWhatCreateInputSurface             = 'cisf',
350         kWhatSetInputSurface                = 'sisf',
351         kWhatStart                          = 'strt',
352         kWhatStop                           = 'stop',
353         kWhatRelease                        = 'rele',
354         kWhatDequeueInputBuffer             = 'deqI',
355         kWhatQueueInputBuffer               = 'queI',
356         kWhatDequeueOutputBuffer            = 'deqO',
357         kWhatReleaseOutputBuffer            = 'relO',
358         kWhatSignalEndOfInputStream         = 'eois',
359         kWhatGetBuffers                     = 'getB',
360         kWhatFlush                          = 'flus',
361         kWhatGetOutputFormat                = 'getO',
362         kWhatGetInputFormat                 = 'getI',
363         kWhatDequeueInputTimedOut           = 'dITO',
364         kWhatDequeueOutputTimedOut          = 'dOTO',
365         kWhatCodecNotify                    = 'codc',
366         kWhatRequestIDRFrame                = 'ridr',
367         kWhatRequestActivityNotification    = 'racN',
368         kWhatGetName                        = 'getN',
369         kWhatGetCodecInfo                   = 'gCoI',
370         kWhatSetParameters                  = 'setP',
371         kWhatSetCallback                    = 'setC',
372         kWhatSetNotification                = 'setN',
373         kWhatDrmReleaseCrypto               = 'rDrm',
374         kWhatCheckBatteryStats              = 'chkB',
375         kWhatGetMetrics                     = 'getM',
376     };
377 
378     enum {
379         kFlagUsesSoftwareRenderer       = 1,
380         kFlagOutputFormatChanged        = 2,
381         kFlagOutputBuffersChanged       = 4,
382         kFlagStickyError                = 8,
383         kFlagDequeueInputPending        = 16,
384         kFlagDequeueOutputPending       = 32,
385         kFlagIsSecure                   = 64,
386         kFlagSawMediaServerDie          = 128,
387         kFlagIsEncoder                  = 256,
388         // 512 skipped
389         kFlagIsAsync                    = 1024,
390         kFlagIsComponentAllocated       = 2048,
391         kFlagPushBlankBuffersOnShutdown = 4096,
392         kFlagUseBlockModel              = 8192,
393         kFlagUseCryptoAsync             = 16384,
394     };
395 
396     struct BufferInfo {
397         BufferInfo();
398 
399         sp<MediaCodecBuffer> mData;
400         bool mOwnedByClient;
401     };
402 
403     // This type is used to track the tunnel mode video peek state machine:
404     //
405     // DisabledNoBuffer -> EnabledNoBuffer  when tunnel-peek = true
406     // DisabledQueued   -> EnabledQueued    when tunnel-peek = true
407     // DisabledNoBuffer -> DisabledQueued   when first frame queued
408     // EnabledNoBuffer  -> DisabledNoBuffer when tunnel-peek = false
409     // EnabledQueued    -> DisabledQueued   when tunnel-peek = false
410     // EnabledNoBuffer  -> EnabledQueued    when first frame queued
411     // DisabledNoBuffer -> BufferDecoded    when kWhatFirstTunnelFrameReady
412     // DisabledQueued   -> BufferDecoded    when kWhatFirstTunnelFrameReady
413     // EnabledNoBuffer  -> BufferDecoded    when kWhatFirstTunnelFrameReady
414     // EnabledQueued    -> BufferDecoded    when kWhatFirstTunnelFrameReady
415     // BufferDecoded    -> BufferRendered   when kWhatFrameRendered
416     // <all states>     -> EnabledNoBuffer  when flush
417     // <all states>     -> EnabledNoBuffer  when stop then configure then start
418     enum struct TunnelPeekState {
419         kLegacyMode,
420         kDisabledNoBuffer,
421         kEnabledNoBuffer,
422         kDisabledQueued,
423         kEnabledQueued,
424         kBufferDecoded,
425         kBufferRendered,
426     };
427 
428     enum class DequeueOutputResult {
429         kNoBuffer,
430         kDiscardedBuffer,
431         kRepliedWithError,
432         kSuccess,
433     };
434 
435     struct ResourceManagerServiceProxy;
436 
437     State mState;
438     bool mReleasedByResourceManager;
439     sp<ALooper> mLooper;
440     sp<ALooper> mCodecLooper;
441     sp<CodecBase> mCodec;
442     AString mComponentName;
443     AString mOwnerName;
444     sp<MediaCodecInfo> mCodecInfo;
445     sp<AReplyToken> mReplyID;
446     std::string mLastReplyOrigin;
447     std::vector<sp<AMessage>> mDeferredMessages;
448     uint32_t mFlags;
449     int64_t mPresentationTimeUs = 0;
450     status_t mStickyError;
451     sp<Surface> mSurface;
452     SoftwareRenderer *mSoftRenderer;
453 
454     Mutex mMetricsLock;
455     mediametrics_handle_t mMetricsHandle = 0;
456     bool mMetricsToUpload = false;
457     nsecs_t mLifetimeStartNs = 0;
458     void initMediametrics();
459     void updateMediametrics();
460     void flushMediametrics();
461     void resetMetricsFields();
462     void updateEphemeralMediametrics(mediametrics_handle_t item);
463     void updateLowLatency(const sp<AMessage> &msg);
464     void onGetMetrics(const sp<AMessage>& msg);
465     constexpr const char *asString(TunnelPeekState state, const char *default_string="?");
466     void updateTunnelPeek(const sp<AMessage> &msg);
467     void processRenderedFrames(const sp<AMessage> &msg);
468 
469     inline void initClientConfigParcel(ClientConfigParcel& clientConfig);
470 
471     sp<AMessage> mOutputFormat;
472     sp<AMessage> mInputFormat;
473     sp<AMessage> mCallback;
474     sp<AMessage> mOnFrameRenderedNotification;
475     sp<AMessage> mAsyncReleaseCompleteNotification;
476     sp<AMessage> mOnFirstTunnelFrameReadyNotification;
477 
478     sp<ResourceManagerServiceProxy> mResourceManagerProxy;
479 
480     Domain mDomain;
481     AString mLogSessionId;
482     int32_t mWidth;
483     int32_t mHeight;
484     int32_t mRotationDegrees;
485     int32_t mAllowFrameDroppingBySurface;
486 
487     enum {
488         kFlagHasHdrStaticInfo   = 1,
489         kFlagHasHdr10PlusInfo   = 2,
490     };
491     uint32_t mHdrInfoFlags;
492     void updateHdrMetrics(bool isConfig);
493     hdr_format getHdrFormat(const AString &mime, const int32_t profile,
494             const int32_t colorTransfer);
495     hdr_format getHdrFormatForEncoder(const AString &mime, const int32_t profile,
496             const int32_t colorTransfer);
497     hdr_format getHdrFormatForDecoder(const AString &mime, const int32_t profile,
498             const int32_t colorTransfer);
499     bool profileSupport10Bits(const AString &mime, const int32_t profile);
500 
501     struct ApiUsageMetrics {
502         bool isArrayMode;
503         enum OperationMode {
504             kUnknownMode = 0,
505             kSynchronousMode = 1,
506             kAsynchronousMode = 2,
507             kBlockMode = 3,
508         };
509         OperationMode operationMode;
510         bool isUsingOutputSurface;
511         struct InputBufferSize {
512             int32_t appMax;  // max size configured by the app
513             int32_t usedMax;  // max size actually used
514             int32_t codecMax;  // max size suggested by the codec
515         } inputBufferSize;
516     } mApiUsageMetrics;
517     struct ReliabilityContextMetrics {
518         int32_t flushCount;
519         int32_t setOutputSurfaceCount;
520         int32_t resolutionChangeCount;
521     } mReliabilityContextMetrics;
522 
523     // initial create parameters
524     AString mInitName;
525 
526     // configure parameter
527     sp<AMessage> mConfigureMsg;
528 
529     // rewrites the format description during configure() for encoding.
530     // format and flags as they exist within configure()
531     // the (possibly) updated format is returned in place.
532     status_t shapeMediaFormat(
533             const sp<AMessage> &format,
534             uint32_t flags,
535             mediametrics_handle_t handle);
536 
537     // populate the format shaper library with information for this codec encoding
538     // for the indicated media type
539     status_t setupFormatShaper(AString mediaType);
540 
541     // Used only to synchronize asynchronous getBufferAndFormat
542     // across all the other (synchronous) buffer state change
543     // operations, such as de/queueIn/OutputBuffer, start and
544     // stop/flush/reset/release.
545     Mutex mBufferLock;
546 
547     std::list<size_t> mAvailPortBuffers[2];
548     std::vector<BufferInfo> mPortBuffers[2];
549 
550     int32_t mDequeueInputTimeoutGeneration;
551     sp<AReplyToken> mDequeueInputReplyID;
552 
553     int32_t mDequeueOutputTimeoutGeneration;
554     sp<AReplyToken> mDequeueOutputReplyID;
555 
556     sp<ICrypto> mCrypto;
557 
558     int32_t mTunneledInputWidth;
559     int32_t mTunneledInputHeight;
560     bool mTunneled;
561     TunnelPeekState mTunnelPeekState;
562 
563     sp<IDescrambler> mDescrambler;
564 
565     std::list<sp<ABuffer> > mCSD;
566 
567     sp<AMessage> mActivityNotify;
568 
569     bool mHaveInputSurface;
570     bool mHavePendingInputBuffers;
571     bool mCpuBoostRequested;
572 
573     std::shared_ptr<BufferChannelBase> mBufferChannel;
574     sp<CryptoAsync> mCryptoAsync;
575     sp<ALooper> mCryptoLooper;
576 
577     bool mIsSurfaceToDisplay;
578     bool mAreRenderMetricsEnabled;
579     PlaybackDurationAccumulator mPlaybackDurationAccumulator;
580     VideoRenderQualityTracker mVideoRenderQualityTracker;
581 
582     MediaCodec(
583             const sp<ALooper> &looper, pid_t pid, uid_t uid,
584             std::function<sp<CodecBase>(const AString &, const char *)> getCodecBase = nullptr,
585             std::function<status_t(const AString &, sp<MediaCodecInfo> *)> getCodecInfo = nullptr);
586 
587     static sp<CodecBase> GetCodecBase(const AString &name, const char *owner = nullptr);
588 
589     static status_t PostAndAwaitResponse(
590             const sp<AMessage> &msg, sp<AMessage> *response);
591 
592     void PostReplyWithError(const sp<AMessage> &msg, int32_t err);
593     void PostReplyWithError(const sp<AReplyToken> &replyID, int32_t err);
594 
595     status_t init(const AString &name);
596 
597     void setState(State newState);
598     void returnBuffersToCodec(bool isReclaim = false);
599     void returnBuffersToCodecOnPort(int32_t portIndex, bool isReclaim = false);
600     size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg);
601     status_t onQueueInputBuffer(const sp<AMessage> &msg);
602     status_t onReleaseOutputBuffer(const sp<AMessage> &msg);
603     BufferInfo *peekNextPortBuffer(int32_t portIndex);
604     ssize_t dequeuePortBuffer(int32_t portIndex);
605 
606     status_t getBufferAndFormat(
607             size_t portIndex, size_t index,
608             sp<MediaCodecBuffer> *buffer, sp<AMessage> *format);
609 
610     bool handleDequeueInputBuffer(const sp<AReplyToken> &replyID, bool newRequest = false);
611     DequeueOutputResult handleDequeueOutputBuffer(
612             const sp<AReplyToken> &replyID,
613             bool newRequest = false);
614     void cancelPendingDequeueOperations();
615 
616     void extractCSD(const sp<AMessage> &format);
617     status_t queueCSDInputBuffer(size_t bufferIndex);
618 
619     status_t handleSetSurface(const sp<Surface> &surface);
620     status_t connectToSurface(const sp<Surface> &surface);
621     status_t disconnectFromSurface();
622 
hasCryptoOrDescramblerMediaCodec623     bool hasCryptoOrDescrambler() {
624         return mCrypto != NULL || mDescrambler != NULL;
625     }
626 
627     void postActivityNotificationIfPossible();
628 
629     void onInputBufferAvailable();
630     void onOutputBufferAvailable();
631     void onCryptoError(const sp<AMessage> &msg);
632     void onError(status_t err, int32_t actionCode, const char *detail = NULL);
633     void onOutputFormatChanged();
634 
635     status_t onSetParameters(const sp<AMessage> &params);
636 
637     status_t amendOutputFormatWithCodecSpecificData(const sp<MediaCodecBuffer> &buffer);
638     void handleOutputFormatChangeIfNeeded(const sp<MediaCodecBuffer> &buffer);
639     bool isExecuting() const;
640 
641     uint64_t getGraphicBufferSize();
642     void requestCpuBoostIfNeeded();
643 
644     bool hasPendingBuffer(int portIndex);
645     bool hasPendingBuffer();
646 
647     void postPendingRepliesAndDeferredMessages(std::string origin, status_t err = OK);
648     void postPendingRepliesAndDeferredMessages(std::string origin, const sp<AMessage> &response);
649 
650     /* called to get the last codec error when the sticky flag is set.
651      * if no such codec error is found, returns UNKNOWN_ERROR.
652      */
getStickyErrorMediaCodec653     inline status_t getStickyError() const {
654         return mStickyError != 0 ? mStickyError : UNKNOWN_ERROR;
655     }
656 
setStickyErrorMediaCodec657     inline void setStickyError(status_t err) {
658         mFlags |= kFlagStickyError;
659         mStickyError = err;
660     }
661 
662     void onReleaseCrypto(const sp<AMessage>& msg);
663 
664     // managing time-of-flight aka latency
665     typedef struct {
666             int64_t presentationUs;
667             int64_t startedNs;
668     } BufferFlightTiming_t;
669     std::deque<BufferFlightTiming_t> mBuffersInFlight;
670     Mutex mLatencyLock;
671     int64_t mLatencyUnknown;    // buffers for which we couldn't calculate latency
672 
673     Mutex mOutputStatsLock;
674     int64_t mBytesEncoded = 0;
675     int64_t mEarliestEncodedPtsUs = INT64_MAX;
676     int64_t mLatestEncodedPtsUs = INT64_MIN;
677     int64_t mFramesEncoded = 0;
678     int64_t mBytesInput = 0;
679     int64_t mFramesInput = 0;
680 
681     int64_t mNumLowLatencyEnables;  // how many times low latency mode is enabled
682     int64_t mNumLowLatencyDisables;  // how many times low latency mode is disabled
683     bool mIsLowLatencyModeOn;  // is low latency mode on currently
684     int64_t mIndexOfFirstFrameWhenLowLatencyOn;  // index of the first frame queued
685                                                  // when low latency is on
686     int64_t mInputBufferCounter;  // number of input buffers queued since last reset/flush
687 
688     // A rescheduleable message that periodically polls for rendered buffers
689     sp<AMessage> mMsgPollForRenderedBuffers;
690 
691     class ReleaseSurface;
692     std::unique_ptr<ReleaseSurface> mReleaseSurface;
693 
694     std::list<sp<AMessage>> mLeftover;
695     status_t handleLeftover(size_t index);
696 
697     sp<BatteryChecker> mBatteryChecker;
698 
699     void statsBufferSent(int64_t presentationUs, const sp<MediaCodecBuffer> &buffer);
700     void statsBufferReceived(int64_t presentationUs, const sp<MediaCodecBuffer> &buffer);
701     bool discardDecodeOnlyOutputBuffer(size_t index);
702 
703     enum {
704         // the default shape of our latency histogram buckets
705         // XXX: should these be configurable in some way?
706         kLatencyHistBuckets = 20,
707         kLatencyHistWidth = 2000,
708         kLatencyHistFloor = 2000,
709 
710         // how many samples are in the 'recent latency' histogram
711         // 300 frames = 5 sec @ 60fps or ~12 sec @ 24fps
712         kRecentLatencyFrames = 300,
713 
714         // how we initialize mRecentSamples
715         kRecentSampleInvalid = -1,
716     };
717 
718     int64_t mRecentSamples[kRecentLatencyFrames];
719     int mRecentHead;
720     Mutex mRecentLock;
721 
722     MediaHistogram<int64_t> mLatencyHist;
723 
724     // An unique ID for the codec - Used by the metrics.
725     uint64_t mCodecId = 0;
726 
727     std::function<sp<CodecBase>(const AString &, const char *)> mGetCodecBase;
728     std::function<status_t(const AString &, sp<MediaCodecInfo> *)> mGetCodecInfo;
729     friend class MediaTestHelper;
730 
731     CodecErrorLog mErrorLog;
732 
733     DISALLOW_EVIL_CONSTRUCTORS(MediaCodec);
734 };
735 
736 }  // namespace android
737 
738 #endif  // MEDIA_CODEC_H_
739