MediaCodec.h revision be8f14c64b3af442dc1341ea9bf1ca68e56dd824
15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/*
25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * Copyright 2012, The Android Open Source Project
35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * Licensed under the Apache License, Version 2.0 (the "License");
55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * you may not use this file except in compliance with the License.
65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * You may obtain a copy of the License at
75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *     http://www.apache.org/licenses/LICENSE-2.0
95460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * Unless required by applicable law or agreed to in writing, software
11affc150dc44fab1911775a49636d0ce85333b634Zonr Chang * distributed under the License is distributed on an "AS IS" BASIS,
125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * See the License for the specific language governing permissions and
145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * limitations under the License.
155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */
165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#ifndef MEDIA_CODEC_H_
185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#define MEDIA_CODEC_H_
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
21affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <gui/IGraphicBufferProducer.h>
22affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <media/hardware/CryptoAPI.h>
2387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#include <media/MediaCodecInfo.h>
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <media/MediaResource.h>
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <media/stagefright/foundation/AHandler.h>
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <media/stagefright/FrameRenderTracker.h>
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <utils/Vector.h>
285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace android {
3087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct ABuffer;
325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct AMessage;
335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct AReplyToken;
345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct AString;
35affc150dc44fab1911775a49636d0ce85333b634Zonr Changstruct CodecBase;
36affc150dc44fab1911775a49636d0ce85333b634Zonr Changclass IBatteryStats;
375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct ICrypto;
385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass IMemory;
395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct MemoryDealer;
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass IResourceManagerClient;
41affc150dc44fab1911775a49636d0ce85333b634Zonr Changclass IResourceManagerService;
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct PersistentSurface;
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct SoftwareRenderer;
445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass Surface;
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaostruct MediaCodec : public AHandler {
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    enum ConfigureFlags {
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        CONFIGURE_FLAG_ENCODE   = 1,
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    };
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    enum BufferFlags {
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        BUFFER_FLAG_SYNCFRAME   = 1,
535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        BUFFER_FLAG_CODECCONFIG = 2,
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        BUFFER_FLAG_EOS         = 4,
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    };
565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    enum {
585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        CB_INPUT_AVAILABLE = 1,
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        CB_OUTPUT_AVAILABLE = 2,
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        CB_ERROR = 3,
615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        CB_OUTPUT_FORMAT_CHANGED = 4,
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        CB_RESOURCE_RECLAIMED = 5,
635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    };
645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    static const pid_t kNoPid = -1;
665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    static sp<MediaCodec> CreateByType(
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const sp<ALooper> &looper, const AString &mime, bool encoder, status_t *err = NULL,
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            pid_t pid = kNoPid);
705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    static sp<MediaCodec> CreateByComponentName(
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const sp<ALooper> &looper, const AString &name, status_t *err = NULL,
735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            pid_t pid = kNoPid);
74affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
75affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    static sp<PersistentSurface> CreatePersistentInputSurface();
76affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
77affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    // utility method to query capabilities
785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    static status_t QueryCapabilities(
795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const AString &name, const AString &mime, bool isEncoder,
805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            sp<MediaCodecInfo::Capabilities> *caps /* nonnull */);
815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t configure(
835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const sp<AMessage> &format,
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const sp<Surface> &nativeWindow,
855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const sp<ICrypto> &crypto,
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            uint32_t flags);
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t setCallback(const sp<AMessage> &callback);
895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t setOnFrameRenderedNotification(const sp<AMessage> &notify);
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t createInputSurface(sp<IGraphicBufferProducer>* bufferProducer);
935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t setInputSurface(const sp<PersistentSurface> &surface);
955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t start();
975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // Returns to a state in which the component remains allocated but
995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // unconfigured.
100affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    status_t stop();
1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // Resets the codec to the INITIALIZED state.  Can be called after an error
1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // has occured to make the codec usable.
1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t reset();
1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // Client MUST call release before releasing final reference to this
107affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    // object.
1085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t release();
1095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t flush();
1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t queueInputBuffer(
113affc150dc44fab1911775a49636d0ce85333b634Zonr Chang            size_t index,
1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            size_t offset,
1155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            size_t size,
1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            int64_t presentationTimeUs,
1175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            uint32_t flags,
1185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            AString *errorDetailMsg = NULL);
1195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t queueSecureInputBuffer(
1215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            size_t index,
122affc150dc44fab1911775a49636d0ce85333b634Zonr Chang            size_t offset,
1235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const CryptoPlugin::SubSample *subSamples,
1245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            size_t numSubSamples,
1255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const uint8_t key[16],
1265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const uint8_t iv[16],
1275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            CryptoPlugin::Mode mode,
1285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            const CryptoPlugin::Pattern &pattern,
1295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            int64_t presentationTimeUs,
1305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            uint32_t flags,
1315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            AString *errorDetailMsg = NULL);
1325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t dequeueInputBuffer(size_t *index, int64_t timeoutUs = 0ll);
1345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t dequeueOutputBuffer(
1365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            size_t *index,
1375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            size_t *offset,
1385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            size_t *size,
1395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            int64_t *presentationTimeUs,
1405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            uint32_t *flags,
1415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao            int64_t timeoutUs = 0ll);
1425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
143affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    status_t renderOutputBufferAndRelease(size_t index, int64_t timestampNs);
1445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t renderOutputBufferAndRelease(size_t index);
1455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t releaseOutputBuffer(size_t index);
1465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
147affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    status_t signalEndOfInputStream();
148affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
1495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t getOutputFormat(sp<AMessage> *format) const;
1505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t getInputFormat(sp<AMessage> *format) const;
1515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t getWidevineLegacyBuffers(Vector<sp<ABuffer> > *buffers) const;
1535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t getInputBuffers(Vector<sp<ABuffer> > *buffers) const;
1555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t getOutputBuffers(Vector<sp<ABuffer> > *buffers) const;
1565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t getOutputBuffer(size_t index, sp<ABuffer> *buffer);
1585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t getOutputFormat(size_t index, sp<AMessage> *format);
1595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t getInputBuffer(size_t index, sp<ABuffer> *buffer);
1605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t setSurface(const sp<Surface> &nativeWindow);
1625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t requestIDRFrame();
1645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // Notification will be posted once there "is something to do", i.e.
1665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // an input/output buffer has become available, a format change is
1675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // pending, an error is pending.
1685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    void requestActivityNotification(const sp<AMessage> &notify);
1695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    status_t getName(AString *componentName) const;
1715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
17287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    status_t setParameters(const sp<AMessage> &params);
17387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
17487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    // Create a MediaCodec notification message from a list of rendered or dropped render infos
17587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    // by adding rendered frame information to a base notification message. Returns the number
17687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    // of frames that were rendered.
17787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    static size_t CreateFramesRenderedMessage(
17887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines            const std::list<FrameRenderTracker::Info> &done, sp<AMessage> &msg);
17987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
18087f34658dec9097d987d254a990ea7f311bfc95fStephen Hinesprotected:
18187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    virtual ~MediaCodec();
18287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    virtual void onMessageReceived(const sp<AMessage> &msg);
18387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
18487f34658dec9097d987d254a990ea7f311bfc95fStephen Hinesprivate:
18587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    // used by ResourceManagerClient
186affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    status_t reclaim(bool force = false);
1875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    friend struct ResourceManagerClient;
1885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
190affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    enum State {
191affc150dc44fab1911775a49636d0ce85333b634Zonr Chang        UNINITIALIZED,
1925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        INITIALIZING,
1935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        INITIALIZED,
1945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        CONFIGURING,
1955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        CONFIGURED,
1965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        STARTING,
1975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        STARTED,
1985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        FLUSHING,
1995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        FLUSHED,
200affc150dc44fab1911775a49636d0ce85333b634Zonr Chang        STOPPING,
201affc150dc44fab1911775a49636d0ce85333b634Zonr Chang        RELEASING,
202affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    };
203affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
2045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    enum {
2055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao        kPortIndexInput         = 0,
206affc150dc44fab1911775a49636d0ce85333b634Zonr Chang        kPortIndexOutput        = 1,
2075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    };
2085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
209    enum {
210        kWhatInit                           = 'init',
211        kWhatConfigure                      = 'conf',
212        kWhatSetSurface                     = 'sSur',
213        kWhatCreateInputSurface             = 'cisf',
214        kWhatSetInputSurface                = 'sisf',
215        kWhatStart                          = 'strt',
216        kWhatStop                           = 'stop',
217        kWhatRelease                        = 'rele',
218        kWhatDequeueInputBuffer             = 'deqI',
219        kWhatQueueInputBuffer               = 'queI',
220        kWhatDequeueOutputBuffer            = 'deqO',
221        kWhatReleaseOutputBuffer            = 'relO',
222        kWhatSignalEndOfInputStream         = 'eois',
223        kWhatGetBuffers                     = 'getB',
224        kWhatFlush                          = 'flus',
225        kWhatGetOutputFormat                = 'getO',
226        kWhatGetInputFormat                 = 'getI',
227        kWhatDequeueInputTimedOut           = 'dITO',
228        kWhatDequeueOutputTimedOut          = 'dOTO',
229        kWhatCodecNotify                    = 'codc',
230        kWhatRequestIDRFrame                = 'ridr',
231        kWhatRequestActivityNotification    = 'racN',
232        kWhatGetName                        = 'getN',
233        kWhatSetParameters                  = 'setP',
234        kWhatSetCallback                    = 'setC',
235        kWhatSetNotification                = 'setN',
236    };
237
238    enum {
239        kFlagUsesSoftwareRenderer       = 1,
240        kFlagOutputFormatChanged        = 2,
241        kFlagOutputBuffersChanged       = 4,
242        kFlagStickyError                = 8,
243        kFlagDequeueInputPending        = 16,
244        kFlagDequeueOutputPending       = 32,
245        kFlagIsSecure                   = 64,
246        kFlagSawMediaServerDie          = 128,
247        kFlagIsEncoder                  = 256,
248        kFlagGatherCodecSpecificData    = 512,
249        kFlagIsAsync                    = 1024,
250        kFlagIsComponentAllocated       = 2048,
251        kFlagPushBlankBuffersOnShutdown = 4096,
252    };
253
254    struct BufferInfo {
255        uint32_t mBufferID;
256        sp<ABuffer> mData;
257        sp<NativeHandle> mNativeHandle;
258        sp<RefBase> mMemRef;
259        sp<ABuffer> mEncryptedData;
260        sp<IMemory> mSharedEncryptedBuffer;
261        sp<AMessage> mNotify;
262        sp<AMessage> mFormat;
263        bool mOwnedByClient;
264    };
265
266    struct ResourceManagerServiceProxy : public IBinder::DeathRecipient {
267        ResourceManagerServiceProxy(pid_t pid);
268        ~ResourceManagerServiceProxy();
269
270        void init();
271
272        // implements DeathRecipient
273        virtual void binderDied(const wp<IBinder>& /*who*/);
274
275        void addResource(
276                int64_t clientId,
277                const sp<IResourceManagerClient> &client,
278                const Vector<MediaResource> &resources);
279
280        void removeResource(int64_t clientId);
281
282        bool reclaimResource(const Vector<MediaResource> &resources);
283
284    private:
285        Mutex mLock;
286        sp<IResourceManagerService> mService;
287        pid_t mPid;
288    };
289
290    State mState;
291    bool mReleasedByResourceManager;
292    sp<ALooper> mLooper;
293    sp<ALooper> mCodecLooper;
294    sp<CodecBase> mCodec;
295    AString mComponentName;
296    sp<AReplyToken> mReplyID;
297    uint32_t mFlags;
298    status_t mStickyError;
299    sp<Surface> mSurface;
300    SoftwareRenderer *mSoftRenderer;
301
302    sp<AMessage> mOutputFormat;
303    sp<AMessage> mInputFormat;
304    sp<AMessage> mCallback;
305    sp<AMessage> mOnFrameRenderedNotification;
306    sp<MemoryDealer> mDealer;
307
308    sp<IResourceManagerClient> mResourceManagerClient;
309    sp<ResourceManagerServiceProxy> mResourceManagerService;
310
311    bool mBatteryStatNotified;
312    bool mIsVideo;
313    int32_t mVideoWidth;
314    int32_t mVideoHeight;
315    int32_t mRotationDegrees;
316
317    // initial create parameters
318    AString mInitName;
319    bool mInitNameIsType;
320    bool mInitIsEncoder;
321
322    // configure parameter
323    sp<AMessage> mConfigureMsg;
324
325    // Used only to synchronize asynchronous getBufferAndFormat
326    // across all the other (synchronous) buffer state change
327    // operations, such as de/queueIn/OutputBuffer, start and
328    // stop/flush/reset/release.
329    Mutex mBufferLock;
330
331    List<size_t> mAvailPortBuffers[2];
332    Vector<BufferInfo> mPortBuffers[2];
333
334    int32_t mDequeueInputTimeoutGeneration;
335    sp<AReplyToken> mDequeueInputReplyID;
336
337    int32_t mDequeueOutputTimeoutGeneration;
338    sp<AReplyToken> mDequeueOutputReplyID;
339
340    sp<ICrypto> mCrypto;
341
342    List<sp<ABuffer> > mCSD;
343
344    sp<AMessage> mActivityNotify;
345
346    bool mHaveInputSurface;
347    bool mHavePendingInputBuffers;
348
349    MediaCodec(const sp<ALooper> &looper, pid_t pid);
350
351    static sp<CodecBase> GetCodecBase(const AString &name, bool nameIsType = false);
352
353    static status_t PostAndAwaitResponse(
354            const sp<AMessage> &msg, sp<AMessage> *response);
355
356    void PostReplyWithError(const sp<AReplyToken> &replyID, int32_t err);
357
358    status_t init(const AString &name, bool nameIsType, bool encoder);
359
360    void setState(State newState);
361    void returnBuffersToCodec(bool isReclaim = false);
362    void returnBuffersToCodecOnPort(int32_t portIndex, bool isReclaim = false);
363    size_t updateBuffers(int32_t portIndex, const sp<AMessage> &msg);
364    status_t onQueueInputBuffer(const sp<AMessage> &msg);
365    status_t onReleaseOutputBuffer(const sp<AMessage> &msg);
366    ssize_t dequeuePortBuffer(int32_t portIndex);
367
368    status_t getBufferAndFormat(
369            size_t portIndex, size_t index,
370            sp<ABuffer> *buffer, sp<AMessage> *format);
371
372    bool handleDequeueInputBuffer(const sp<AReplyToken> &replyID, bool newRequest = false);
373    bool handleDequeueOutputBuffer(const sp<AReplyToken> &replyID, bool newRequest = false);
374    void cancelPendingDequeueOperations();
375
376    void extractCSD(const sp<AMessage> &format);
377    status_t queueCSDInputBuffer(size_t bufferIndex);
378
379    status_t handleSetSurface(const sp<Surface> &surface);
380    status_t connectToSurface(const sp<Surface> &surface);
381    status_t disconnectFromSurface();
382
383    void postActivityNotificationIfPossible();
384
385    void onInputBufferAvailable();
386    void onOutputBufferAvailable();
387    void onError(status_t err, int32_t actionCode, const char *detail = NULL);
388    void onOutputFormatChanged();
389
390    status_t onSetParameters(const sp<AMessage> &params);
391
392    status_t amendOutputFormatWithCodecSpecificData(const sp<ABuffer> &buffer);
393    void updateBatteryStat();
394    bool isExecuting() const;
395
396    uint64_t getGraphicBufferSize();
397    void addResource(MediaResource::Type type, MediaResource::SubType subtype, uint64_t value);
398
399    bool hasPendingBuffer(int portIndex);
400    bool hasPendingBuffer();
401
402    /* called to get the last codec error when the sticky flag is set.
403     * if no such codec error is found, returns UNKNOWN_ERROR.
404     */
405    inline status_t getStickyError() const {
406        return mStickyError != 0 ? mStickyError : UNKNOWN_ERROR;
407    }
408
409    inline void setStickyError(status_t err) {
410        mFlags |= kFlagStickyError;
411        mStickyError = err;
412    }
413
414    DISALLOW_EVIL_CONSTRUCTORS(MediaCodec);
415};
416
417}  // namespace android
418
419#endif  // MEDIA_CODEC_H_
420