ACodec.cpp revision e96ee699aca0f711d41e6c0833e5de2341c4a36d
127c174483a8ae9688d5d4897c19074f62c7f1701James Dong/*
227c174483a8ae9688d5d4897c19074f62c7f1701James Dong * Copyright (C) 2010 The Android Open Source Project
327c174483a8ae9688d5d4897c19074f62c7f1701James Dong *
427c174483a8ae9688d5d4897c19074f62c7f1701James Dong * Licensed under the Apache License, Version 2.0 (the "License");
527c174483a8ae9688d5d4897c19074f62c7f1701James Dong * you may not use this file except in compliance with the License.
627c174483a8ae9688d5d4897c19074f62c7f1701James Dong * You may obtain a copy of the License at
727c174483a8ae9688d5d4897c19074f62c7f1701James Dong *
827c174483a8ae9688d5d4897c19074f62c7f1701James Dong *      http://www.apache.org/licenses/LICENSE-2.0
927c174483a8ae9688d5d4897c19074f62c7f1701James Dong *
1027c174483a8ae9688d5d4897c19074f62c7f1701James Dong * Unless required by applicable law or agreed to in writing, software
1127c174483a8ae9688d5d4897c19074f62c7f1701James Dong * distributed under the License is distributed on an "AS IS" BASIS,
1227c174483a8ae9688d5d4897c19074f62c7f1701James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1327c174483a8ae9688d5d4897c19074f62c7f1701James Dong * See the License for the specific language governing permissions and
1427c174483a8ae9688d5d4897c19074f62c7f1701James Dong * limitations under the License.
1527c174483a8ae9688d5d4897c19074f62c7f1701James Dong */
1627c174483a8ae9688d5d4897c19074f62c7f1701James Dong
17f933441648ef6a71dee783d733aac17b9508b452Andreas Huber//#define LOG_NDEBUG 0
18f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#define LOG_TAG "ACodec"
19f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
20f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/ACodec.h>
21f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
22f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <binder/MemoryDealer.h>
23f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
24f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/foundation/hexdump.h>
25f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/foundation/ABuffer.h>
26f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/foundation/ADebug.h>
27f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/foundation/AMessage.h>
28f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
297cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden#include <media/stagefright/BufferProducerWrapper.h>
30afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber#include <media/stagefright/MediaCodecList.h>
31f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/MediaDefs.h>
321173118eace0e9e347cb007f0da817cee87579edGlenn Kasten#include <media/stagefright/NativeWindowWrapper.h>
33f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <media/stagefright/OMXClient.h>
341065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber#include <media/stagefright/OMXCodec.h>
35f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
363a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber#include <media/hardware/HardwareAPI.h>
373a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber
38f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#include <OMX_Component.h>
39f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
40496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber#include "include/avc_utils.h"
41496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber
42f933441648ef6a71dee783d733aac17b9508b452Andreas Hubernamespace android {
43f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
44f933441648ef6a71dee783d733aac17b9508b452Andreas Hubertemplate<class T>
45f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatic void InitOMXParams(T *params) {
46f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    params->nSize = sizeof(T);
47f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    params->nVersion.s.nVersionMajor = 1;
48f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    params->nVersion.s.nVersionMinor = 0;
49f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    params->nVersion.s.nRevision = 0;
50f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    params->nVersion.s.nStep = 0;
51f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
52f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
53f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct CodecObserver : public BnOMXObserver {
54f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CodecObserver() {}
55f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
56f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void setNotificationMessage(const sp<AMessage> &msg) {
57f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mNotify = msg;
58f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
59f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
60f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    // from IOMXObserver
61f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void onMessage(const omx_message &omx_msg) {
62f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        sp<AMessage> msg = mNotify->dup();
63f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
64f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        msg->setInt32("type", omx_msg.type);
65f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        msg->setPointer("node", omx_msg.node);
66f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
67f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        switch (omx_msg.type) {
68f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            case omx_message::EVENT:
69f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            {
70f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setInt32("event", omx_msg.u.event_data.event);
71f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setInt32("data1", omx_msg.u.event_data.data1);
72f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setInt32("data2", omx_msg.u.event_data.data2);
73f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                break;
74f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
75f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
76f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            case omx_message::EMPTY_BUFFER_DONE:
77f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            {
78f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setPointer("buffer", omx_msg.u.buffer_data.buffer);
79f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                break;
80f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
81f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
82f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            case omx_message::FILL_BUFFER_DONE:
83f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            {
84f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setPointer(
85f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        "buffer", omx_msg.u.extended_buffer_data.buffer);
86f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setInt32(
87f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        "range_offset",
88f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        omx_msg.u.extended_buffer_data.range_offset);
89f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setInt32(
90f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        "range_length",
91f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        omx_msg.u.extended_buffer_data.range_length);
92f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setInt32(
93f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        "flags",
94f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        omx_msg.u.extended_buffer_data.flags);
95f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setInt64(
96f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        "timestamp",
97f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        omx_msg.u.extended_buffer_data.timestamp);
98f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setPointer(
99f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        "platform_private",
100f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        omx_msg.u.extended_buffer_data.platform_private);
101f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                msg->setPointer(
102f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        "data_ptr",
103f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        omx_msg.u.extended_buffer_data.data_ptr);
104f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                break;
105f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
106f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
107f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            default:
108f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                TRESPASS();
109f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                break;
110f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
111f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
112f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        msg->post();
113f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
114f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
115f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
116f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual ~CodecObserver() {}
117f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
118f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
119f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    sp<AMessage> mNotify;
120f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
121f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(CodecObserver);
122f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
123f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
124f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
125f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
126f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct ACodec::BaseState : public AState {
127f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BaseState(ACodec *codec, const sp<AState> &parentState = NULL);
128f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
129f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
130f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    enum PortMode {
131f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        KEEP_BUFFERS,
132f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        RESUBMIT_BUFFERS,
133f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        FREE_BUFFERS,
134f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    };
135f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
136f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    ACodec *mCodec;
137f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
138f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual PortMode getPortMode(OMX_U32 portIndex);
139f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
140f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
141f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
142f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
143f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
144f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void onOutputBufferDrained(const sp<AMessage> &msg);
145f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void onInputBufferFilled(const sp<AMessage> &msg);
146f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
147f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void postFillThisBuffer(BufferInfo *info);
148f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
149f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
150f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool onOMXMessage(const sp<AMessage> &msg);
151f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
152f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool onOMXEmptyBufferDone(IOMX::buffer_id bufferID);
153f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
154f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool onOMXFillBufferDone(
155f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            IOMX::buffer_id bufferID,
156f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            size_t rangeOffset, size_t rangeLength,
157f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            OMX_U32 flags,
158f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            int64_t timeUs,
159f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            void *platformPrivate,
160f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            void *dataPtr);
161f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
162f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void getMoreInputDataIfPossible();
163f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
164f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(BaseState);
165f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
166f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
167f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
168f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
169ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huberstruct ACodec::DeathNotifier : public IBinder::DeathRecipient {
170ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    DeathNotifier(const sp<AMessage> &notify)
171ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        : mNotify(notify) {
172ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    }
173ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
174ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    virtual void binderDied(const wp<IBinder> &) {
175ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        mNotify->post();
176ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    }
177ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
178ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huberprotected:
179ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    virtual ~DeathNotifier() {}
180ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
181ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huberprivate:
182ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    sp<AMessage> mNotify;
183ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
184ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(DeathNotifier);
185ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber};
186ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
187f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct ACodec::UninitializedState : public ACodec::BaseState {
188f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    UninitializedState(ACodec *codec);
189f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
190f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
191f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
192c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    virtual void stateEntered();
193f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
194f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
195f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void onSetup(const sp<AMessage> &msg);
196c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    bool onAllocateComponent(const sp<AMessage> &msg);
197f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
198ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    sp<DeathNotifier> mDeathNotifier;
199ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
200f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(UninitializedState);
201f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
202f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
203f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
204f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
205c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huberstruct ACodec::LoadedState : public ACodec::BaseState {
206c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    LoadedState(ACodec *codec);
207c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
208c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huberprotected:
209c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
210c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    virtual void stateEntered();
211c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
212c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huberprivate:
213c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    friend struct ACodec::UninitializedState;
214c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
215c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    bool onConfigureComponent(const sp<AMessage> &msg);
2167cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    void onCreateInputSurface(const sp<AMessage> &msg);
217c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    void onStart();
218c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    void onShutdown(bool keepComponentAllocated);
219c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
220c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(LoadedState);
221c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber};
222c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
223c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber////////////////////////////////////////////////////////////////////////////////
224c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
225f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct ACodec::LoadedToIdleState : public ACodec::BaseState {
226f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    LoadedToIdleState(ACodec *codec);
227f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
228f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
229f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
230f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
231f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void stateEntered();
232f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
233f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
234f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    status_t allocateBuffers();
235f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
236f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(LoadedToIdleState);
237f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
238f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
239f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
240f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
241f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct ACodec::IdleToExecutingState : public ACodec::BaseState {
242f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    IdleToExecutingState(ACodec *codec);
243f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
244f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
245f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
246f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
247f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void stateEntered();
248f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
249f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
250f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(IdleToExecutingState);
251f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
252f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
253f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
254f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
255f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct ACodec::ExecutingState : public ACodec::BaseState {
256f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    ExecutingState(ACodec *codec);
257f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
258f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void submitOutputBuffers();
259f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
260f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    // Submit output buffers to the decoder, submit input buffers to client
261f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    // to fill with data.
262f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void resume();
263f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
264349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber    // Returns true iff input and output buffers are in play.
265349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber    bool active() const { return mActive; }
266349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
267f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
268f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual PortMode getPortMode(OMX_U32 portIndex);
269f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
270f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void stateEntered();
271f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
272f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
273f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
274f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
275349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber    bool mActive;
276349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
277f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(ExecutingState);
278f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
279f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
280f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
281f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
282f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct ACodec::OutputPortSettingsChangedState : public ACodec::BaseState {
283f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OutputPortSettingsChangedState(ACodec *codec);
284f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
285f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
286f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual PortMode getPortMode(OMX_U32 portIndex);
287f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
288f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void stateEntered();
289f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
290f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
291f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
292f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
293f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(OutputPortSettingsChangedState);
294f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
295f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
296f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
297f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
298f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct ACodec::ExecutingToIdleState : public ACodec::BaseState {
299f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    ExecutingToIdleState(ACodec *codec);
300f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
301f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
302f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
303f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void stateEntered();
304f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
305f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
306f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
307f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void onOutputBufferDrained(const sp<AMessage> &msg);
308f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void onInputBufferFilled(const sp<AMessage> &msg);
309f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
310f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
311f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void changeStateIfWeOwnAllBuffers();
312f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3135778822d86b0337407514b9372562b86edfa91cdAndreas Huber    bool mComponentNowIdle;
3145778822d86b0337407514b9372562b86edfa91cdAndreas Huber
315f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(ExecutingToIdleState);
316f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
317f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
318f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
319f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
320f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct ACodec::IdleToLoadedState : public ACodec::BaseState {
321f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    IdleToLoadedState(ACodec *codec);
322f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
323f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
324f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
325f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void stateEntered();
326f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
327f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
328f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
329f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
330f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(IdleToLoadedState);
331f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
332f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
333f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
334f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
335f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstruct ACodec::FlushingState : public ACodec::BaseState {
336f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    FlushingState(ACodec *codec);
337f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
338f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprotected:
339f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onMessageReceived(const sp<AMessage> &msg);
340f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void stateEntered();
341f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
342f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual bool onOMXEvent(OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2);
343f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
344f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void onOutputBufferDrained(const sp<AMessage> &msg);
345f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    virtual void onInputBufferFilled(const sp<AMessage> &msg);
346f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
347f933441648ef6a71dee783d733aac17b9508b452Andreas Huberprivate:
348f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool mFlushComplete[2];
349f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
350f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    void changeStateIfWeOwnAllBuffers();
351f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
352f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(FlushingState);
353f933441648ef6a71dee783d733aac17b9508b452Andreas Huber};
354f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
355f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
356f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
357f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::ACodec()
358afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber    : mQuirks(0),
359afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber      mNode(NULL),
3605778822d86b0337407514b9372562b86edfa91cdAndreas Huber      mSentFormat(false),
361c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber      mIsEncoder(false),
3629806555d3930be43e11106281dee354820ac1c88Andreas Huber      mShutdownInProgress(false),
3639806555d3930be43e11106281dee354820ac1c88Andreas Huber      mEncoderDelay(0),
3649806555d3930be43e11106281dee354820ac1c88Andreas Huber      mEncoderPadding(0),
3659806555d3930be43e11106281dee354820ac1c88Andreas Huber      mChannelMaskPresent(false),
3669806555d3930be43e11106281dee354820ac1c88Andreas Huber      mChannelMask(0) {
367f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mUninitializedState = new UninitializedState(this);
368c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    mLoadedState = new LoadedState(this);
369f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mLoadedToIdleState = new LoadedToIdleState(this);
370f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mIdleToExecutingState = new IdleToExecutingState(this);
371f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mExecutingState = new ExecutingState(this);
372f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
373f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mOutputPortSettingsChangedState =
374f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        new OutputPortSettingsChangedState(this);
375f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
376f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mExecutingToIdleState = new ExecutingToIdleState(this);
377f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mIdleToLoadedState = new IdleToLoadedState(this);
378f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mFlushingState = new FlushingState(this);
379f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
380f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mPortEOS[kPortIndexInput] = mPortEOS[kPortIndexOutput] = false;
381dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber    mInputEOSResult = OK;
382f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
383f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    changeState(mUninitializedState);
384f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
385f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
386f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::~ACodec() {
387f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
388f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
389f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::setNotificationMessage(const sp<AMessage> &msg) {
390f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mNotify = msg;
391f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
392f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
393f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::initiateSetup(const sp<AMessage> &msg) {
394f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    msg->setWhat(kWhatSetup);
395f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    msg->setTarget(id());
396f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    msg->post();
397f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
398f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
399a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Hubervoid ACodec::signalSetParameters(const sp<AMessage> &params) {
400a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber    sp<AMessage> msg = new AMessage(kWhatSetParameters, id());
401a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber    msg->setMessage("params", params);
402a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber    msg->post();
403a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber}
404a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
4055778822d86b0337407514b9372562b86edfa91cdAndreas Hubervoid ACodec::initiateAllocateComponent(const sp<AMessage> &msg) {
4065778822d86b0337407514b9372562b86edfa91cdAndreas Huber    msg->setWhat(kWhatAllocateComponent);
4075778822d86b0337407514b9372562b86edfa91cdAndreas Huber    msg->setTarget(id());
4085778822d86b0337407514b9372562b86edfa91cdAndreas Huber    msg->post();
4095778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
4105778822d86b0337407514b9372562b86edfa91cdAndreas Huber
4115778822d86b0337407514b9372562b86edfa91cdAndreas Hubervoid ACodec::initiateConfigureComponent(const sp<AMessage> &msg) {
4125778822d86b0337407514b9372562b86edfa91cdAndreas Huber    msg->setWhat(kWhatConfigureComponent);
4135778822d86b0337407514b9372562b86edfa91cdAndreas Huber    msg->setTarget(id());
4145778822d86b0337407514b9372562b86edfa91cdAndreas Huber    msg->post();
4155778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
4165778822d86b0337407514b9372562b86edfa91cdAndreas Huber
4177cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFaddenvoid ACodec::initiateCreateInputSurface() {
4187cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    (new AMessage(kWhatCreateInputSurface, id()))->post();
4197cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden}
4207cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden
4217cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFaddenvoid ACodec::signalEndOfInputStream() {
4227cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    (new AMessage(kWhatSignalEndOfInputStream, id()))->post();
4237cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden}
4247cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden
4255778822d86b0337407514b9372562b86edfa91cdAndreas Hubervoid ACodec::initiateStart() {
4265778822d86b0337407514b9372562b86edfa91cdAndreas Huber    (new AMessage(kWhatStart, id()))->post();
4275778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
4285778822d86b0337407514b9372562b86edfa91cdAndreas Huber
429f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::signalFlush() {
4307a3a2b2f9bb9421dcf83fbd47276e57917078aefJames Dong    ALOGV("[%s] signalFlush", mComponentName.c_str());
431f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    (new AMessage(kWhatFlush, id()))->post();
432f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
433f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
434f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::signalResume() {
435f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    (new AMessage(kWhatResume, id()))->post();
436f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
437f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
438c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Hubervoid ACodec::initiateShutdown(bool keepComponentAllocated) {
439c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    sp<AMessage> msg = new AMessage(kWhatShutdown, id());
440c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    msg->setInt32("keepComponentAllocated", keepComponentAllocated);
441c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    msg->post();
442f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
443f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
444496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Hubervoid ACodec::signalRequestIDRFrame() {
445496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber    (new AMessage(kWhatRequestIDRFrame, id()))->post();
446496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber}
447496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber
448f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::allocateBuffersOnPort(OMX_U32 portIndex) {
449f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK(portIndex == kPortIndexInput || portIndex == kPortIndexOutput);
450f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
451f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK(mDealer[portIndex] == NULL);
452f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK(mBuffers[portIndex].isEmpty());
453f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4545778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err;
455f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (mNativeWindow != NULL && portIndex == kPortIndexOutput) {
4565778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = allocateOutputBuffersFromNativeWindow();
4575778822d86b0337407514b9372562b86edfa91cdAndreas Huber    } else {
4585778822d86b0337407514b9372562b86edfa91cdAndreas Huber        OMX_PARAM_PORTDEFINITIONTYPE def;
4595778822d86b0337407514b9372562b86edfa91cdAndreas Huber        InitOMXParams(&def);
4605778822d86b0337407514b9372562b86edfa91cdAndreas Huber        def.nPortIndex = portIndex;
461f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4625778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = mOMX->getParameter(
4635778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
464f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4655778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err == OK) {
4665778822d86b0337407514b9372562b86edfa91cdAndreas Huber            ALOGV("[%s] Allocating %lu buffers of size %lu on %s port",
4675778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    mComponentName.c_str(),
4685778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    def.nBufferCountActual, def.nBufferSize,
4695778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    portIndex == kPortIndexInput ? "input" : "output");
470f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4715778822d86b0337407514b9372562b86edfa91cdAndreas Huber            size_t totalSize = def.nBufferCountActual * def.nBufferSize;
4725778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mDealer[portIndex] = new MemoryDealer(totalSize, "ACodec");
473f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4745778822d86b0337407514b9372562b86edfa91cdAndreas Huber            for (OMX_U32 i = 0; i < def.nBufferCountActual; ++i) {
4755778822d86b0337407514b9372562b86edfa91cdAndreas Huber                sp<IMemory> mem = mDealer[portIndex]->allocate(def.nBufferSize);
4765778822d86b0337407514b9372562b86edfa91cdAndreas Huber                CHECK(mem.get() != NULL);
477f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
478ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                BufferInfo info;
479ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                info.mStatus = BufferInfo::OWNED_BY_US;
480f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
481afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber                uint32_t requiresAllocateBufferBit =
482afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber                    (portIndex == kPortIndexInput)
483afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber                        ? OMXCodec::kRequiresAllocateBufferOnInputPorts
484afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber                        : OMXCodec::kRequiresAllocateBufferOnOutputPorts;
4851065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber
486ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                if (portIndex == kPortIndexInput && (mFlags & kFlagIsSecure)) {
487ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                    mem.clear();
488ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
489ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                    void *ptr;
490ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                    err = mOMX->allocateBuffer(
491ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                            mNode, portIndex, def.nBufferSize, &info.mBufferID,
492ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                            &ptr);
493ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
494ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                    info.mData = new ABuffer(ptr, def.nBufferSize);
495ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                } else if (mQuirks & requiresAllocateBufferBit) {
4965778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    err = mOMX->allocateBufferWithBackup(
497ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                            mNode, portIndex, mem, &info.mBufferID);
4985778822d86b0337407514b9372562b86edfa91cdAndreas Huber                } else {
499ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                    err = mOMX->useBuffer(mNode, portIndex, mem, &info.mBufferID);
500ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                }
501ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
502ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                if (mem != NULL) {
503ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                    info.mData = new ABuffer(mem->pointer(), def.nBufferSize);
5045778822d86b0337407514b9372562b86edfa91cdAndreas Huber                }
5051065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber
5065778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mBuffers[portIndex].push(info);
5071065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber            }
5081065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber        }
5095778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
510f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
5115778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
5125778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
5135778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
514f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
5155778822d86b0337407514b9372562b86edfa91cdAndreas Huber    sp<AMessage> notify = mNotify->dup();
5165778822d86b0337407514b9372562b86edfa91cdAndreas Huber    notify->setInt32("what", ACodec::kWhatBuffersAllocated);
5175778822d86b0337407514b9372562b86edfa91cdAndreas Huber
5185778822d86b0337407514b9372562b86edfa91cdAndreas Huber    notify->setInt32("portIndex", portIndex);
519eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber
520eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber    sp<PortDescription> desc = new PortDescription;
521eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber
5225778822d86b0337407514b9372562b86edfa91cdAndreas Huber    for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) {
523eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber        const BufferInfo &info = mBuffers[portIndex][i];
5245778822d86b0337407514b9372562b86edfa91cdAndreas Huber
525eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber        desc->addBuffer(info.mBufferID, info.mData);
526f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
527f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
528eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber    notify->setObject("portDesc", desc);
5295778822d86b0337407514b9372562b86edfa91cdAndreas Huber    notify->post();
5305778822d86b0337407514b9372562b86edfa91cdAndreas Huber
531f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return OK;
532f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
533f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
534f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::allocateOutputBuffersFromNativeWindow() {
535f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_PARAM_PORTDEFINITIONTYPE def;
536f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    InitOMXParams(&def);
537f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    def.nPortIndex = kPortIndexOutput;
538f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
539f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    status_t err = mOMX->getParameter(
540f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
541f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
542f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
543f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
544f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
545f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
546f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    err = native_window_set_buffers_geometry(
547f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNativeWindow.get(),
548f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            def.format.video.nFrameWidth,
549f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            def.format.video.nFrameHeight,
550f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            def.format.video.eColorFormat);
551f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
552f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != 0) {
55329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("native_window_set_buffers_geometry failed: %s (%d)",
554f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                strerror(-err), -err);
555f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
556f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
557f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
558f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    // Set up the native window.
5593c14b9745c4afc88cec247d9dd0b003e087cbb52Jamie Gennis    OMX_U32 usage = 0;
5603c14b9745c4afc88cec247d9dd0b003e087cbb52Jamie Gennis    err = mOMX->getGraphicBufferUsage(mNode, kPortIndexOutput, &usage);
5613c14b9745c4afc88cec247d9dd0b003e087cbb52Jamie Gennis    if (err != 0) {
5625ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("querying usage flags from OMX IL component failed: %d", err);
5633c14b9745c4afc88cec247d9dd0b003e087cbb52Jamie Gennis        // XXX: Currently this error is logged, but not fatal.
5643c14b9745c4afc88cec247d9dd0b003e087cbb52Jamie Gennis        usage = 0;
5653c14b9745c4afc88cec247d9dd0b003e087cbb52Jamie Gennis    }
5663c14b9745c4afc88cec247d9dd0b003e087cbb52Jamie Gennis
567bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    if (mFlags & kFlagIsSecure) {
568bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        usage |= GRALLOC_USAGE_PROTECTED;
569bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
570bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
571bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    // Make sure to check whether either Stagefright or the video decoder
572bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    // requested protected buffers.
573bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    if (usage & GRALLOC_USAGE_PROTECTED) {
574bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        // Verify that the ANativeWindow sends images directly to
575bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        // SurfaceFlinger.
576bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        int queuesToNativeWindow = 0;
577bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        err = mNativeWindow->query(
578bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                mNativeWindow.get(), NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER,
579bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                &queuesToNativeWindow);
580bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (err != 0) {
581bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            ALOGE("error authenticating native window: %d", err);
582bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            return err;
583bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
584bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (queuesToNativeWindow != 1) {
585bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            ALOGE("native window could not be authenticated");
586bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            return PERMISSION_DENIED;
587bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
588bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
589bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
590f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    err = native_window_set_usage(
591f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNativeWindow.get(),
5923c14b9745c4afc88cec247d9dd0b003e087cbb52Jamie Gennis            usage | GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_EXTERNAL_DISP);
593f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
594f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != 0) {
59529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("native_window_set_usage failed: %s (%d)", strerror(-err), -err);
596f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
597f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
598f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
599258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis    int minUndequeuedBufs = 0;
600258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis    err = mNativeWindow->query(
601258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis            mNativeWindow.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
602258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis            &minUndequeuedBufs);
603258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis
604258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis    if (err != 0) {
60529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d)",
606258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis                strerror(-err), -err);
607258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis        return err;
608258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis    }
609258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis
610258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis    // XXX: Is this the right logic to use?  It's not clear to me what the OMX
611258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis    // buffer counts refer to - how do they account for the renderer holding on
612258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis    // to buffers?
613258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis    if (def.nBufferCountActual < def.nBufferCountMin + minUndequeuedBufs) {
614258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis        OMX_U32 newBufferCount = def.nBufferCountMin + minUndequeuedBufs;
615258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis        def.nBufferCountActual = newBufferCount;
616258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis        err = mOMX->setParameter(
617258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis                mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
618258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis
619258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis        if (err != OK) {
62029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block            ALOGE("[%s] setting nBufferCountActual to %lu failed: %d",
621258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis                    mComponentName.c_str(), newBufferCount, err);
622258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis            return err;
623258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis        }
624258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis    }
625258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis
626f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    err = native_window_set_buffer_count(
627f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNativeWindow.get(), def.nBufferCountActual);
628f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
629f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != 0) {
63029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("native_window_set_buffer_count failed: %s (%d)", strerror(-err),
631f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                -err);
632f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
633f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
634f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
6353856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] Allocating %lu buffers from a native window of size %lu on "
636f933441648ef6a71dee783d733aac17b9508b452Andreas Huber         "output port",
637f933441648ef6a71dee783d733aac17b9508b452Andreas Huber         mComponentName.c_str(), def.nBufferCountActual, def.nBufferSize);
638f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
639f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    // Dequeue buffers and send them to OMX
64074006804065941841883c4b46ee785070164023fJamie Gennis    for (OMX_U32 i = 0; i < def.nBufferCountActual; i++) {
6418ce2364512f7c32c824f5ec5719688830ba72427Iliyan Malchev        ANativeWindowBuffer *buf;
6421e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis        err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(), &buf);
643f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (err != 0) {
64429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block            ALOGE("dequeueBuffer failed: %s (%d)", strerror(-err), -err);
645f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
646f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
647f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
648f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false));
64974006804065941841883c4b46ee785070164023fJamie Gennis        BufferInfo info;
65074006804065941841883c4b46ee785070164023fJamie Gennis        info.mStatus = BufferInfo::OWNED_BY_US;
65121ad778dcfcddb8f8fd9dc3fe4992fbef246c511Marco Nelissen        info.mData = new ABuffer(NULL /* data */, def.nBufferSize /* capacity */);
65274006804065941841883c4b46ee785070164023fJamie Gennis        info.mGraphicBuffer = graphicBuffer;
65374006804065941841883c4b46ee785070164023fJamie Gennis        mBuffers[kPortIndexOutput].push(info);
65474006804065941841883c4b46ee785070164023fJamie Gennis
655f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        IOMX::buffer_id bufferId;
656f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer,
657f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                &bufferId);
658f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (err != 0) {
65929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block            ALOGE("registering GraphicBuffer %lu with OMX IL component failed: "
66074006804065941841883c4b46ee785070164023fJamie Gennis                 "%d", i, err);
661f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
662f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
663f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
66474006804065941841883c4b46ee785070164023fJamie Gennis        mBuffers[kPortIndexOutput].editItemAt(i).mBufferID = bufferId;
66574006804065941841883c4b46ee785070164023fJamie Gennis
6663856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("[%s] Registered graphic buffer with ID %p (pointer = %p)",
667f933441648ef6a71dee783d733aac17b9508b452Andreas Huber             mComponentName.c_str(),
668f933441648ef6a71dee783d733aac17b9508b452Andreas Huber             bufferId, graphicBuffer.get());
669f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
670f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
671f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_U32 cancelStart;
672f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_U32 cancelEnd;
673f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
674f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != 0) {
675f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // If an error occurred while dequeuing we need to cancel any buffers
676f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // that were dequeued.
677f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        cancelStart = 0;
67874006804065941841883c4b46ee785070164023fJamie Gennis        cancelEnd = mBuffers[kPortIndexOutput].size();
679f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    } else {
680f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // Return the last two buffers to the native window.
681258d4e3aef7984574b0972a66871afc8a13d8e4eJamie Gennis        cancelStart = def.nBufferCountActual - minUndequeuedBufs;
682f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        cancelEnd = def.nBufferCountActual;
683f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
684f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
685f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (OMX_U32 i = cancelStart; i < cancelEnd; i++) {
686f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        BufferInfo *info = &mBuffers[kPortIndexOutput].editItemAt(i);
687f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        cancelBufferToNativeWindow(info);
688f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
689f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
690f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return err;
691f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
692f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
693f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::cancelBufferToNativeWindow(BufferInfo *info) {
694f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ((int)info->mStatus, (int)BufferInfo::OWNED_BY_US);
695f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
6963856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] Calling cancelBuffer on buffer %p",
697f933441648ef6a71dee783d733aac17b9508b452Andreas Huber         mComponentName.c_str(), info->mBufferID);
698f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
699f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    int err = mNativeWindow->cancelBuffer(
7001e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis        mNativeWindow.get(), info->mGraphicBuffer.get(), -1);
701f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
702f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(err, 0);
703f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
704f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    info->mStatus = BufferInfo::OWNED_BY_NATIVE_WINDOW;
705f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
706f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return OK;
707f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
708f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
709f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::BufferInfo *ACodec::dequeueBufferFromNativeWindow() {
7108ce2364512f7c32c824f5ec5719688830ba72427Iliyan Malchev    ANativeWindowBuffer *buf;
7111e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    int fenceFd = -1;
7121e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    if (native_window_dequeue_buffer_and_wait(mNativeWindow.get(), &buf) != 0) {
71329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("dequeueBuffer failed.");
714c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber        return NULL;
715c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber    }
716f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
717f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = mBuffers[kPortIndexOutput].size(); i-- > 0;) {
718f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        BufferInfo *info =
719f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            &mBuffers[kPortIndexOutput].editItemAt(i);
720f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
721f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (info->mGraphicBuffer->handle == buf->handle) {
722f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ((int)info->mStatus,
723f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                     (int)BufferInfo::OWNED_BY_NATIVE_WINDOW);
724f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
725f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            info->mStatus = BufferInfo::OWNED_BY_US;
726f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
727f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return info;
728f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
729f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
730f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
731f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    TRESPASS();
732f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
733f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return NULL;
734f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
735f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
736f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::freeBuffersOnPort(OMX_U32 portIndex) {
737f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = mBuffers[portIndex].size(); i-- > 0;) {
738f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK_EQ((status_t)OK, freeBuffer(portIndex, i));
739f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
740f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
741f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mDealer[portIndex].clear();
742f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
743f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return OK;
744f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
745f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
746349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huberstatus_t ACodec::freeOutputBuffersNotOwnedByComponent() {
747f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = mBuffers[kPortIndexOutput].size(); i-- > 0;) {
748f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        BufferInfo *info =
749f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            &mBuffers[kPortIndexOutput].editItemAt(i);
750f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
751349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        if (info->mStatus !=
752349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber                BufferInfo::OWNED_BY_COMPONENT) {
753349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            // We shouldn't have sent out any buffers to the client at this
754349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            // point.
755349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            CHECK_NE((int)info->mStatus, (int)BufferInfo::OWNED_BY_DOWNSTREAM);
756349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
757f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ((status_t)OK, freeBuffer(kPortIndexOutput, i));
758f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
759f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
760f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
761f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return OK;
762f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
763f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
764f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::freeBuffer(OMX_U32 portIndex, size_t i) {
765f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BufferInfo *info = &mBuffers[portIndex].editItemAt(i);
766f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
767f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK(info->mStatus == BufferInfo::OWNED_BY_US
768f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            || info->mStatus == BufferInfo::OWNED_BY_NATIVE_WINDOW);
769f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
770f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (portIndex == kPortIndexOutput && mNativeWindow != NULL
771f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            && info->mStatus == BufferInfo::OWNED_BY_US) {
772f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK_EQ((status_t)OK, cancelBufferToNativeWindow(info));
773f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
774f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
775f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(mOMX->freeBuffer(
776f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                mNode, portIndex, info->mBufferID),
777f933441648ef6a71dee783d733aac17b9508b452Andreas Huber             (status_t)OK);
778f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
779f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mBuffers[portIndex].removeAt(i);
780f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
781f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return OK;
782f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
783f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
784f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::BufferInfo *ACodec::findBufferByID(
785f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        uint32_t portIndex, IOMX::buffer_id bufferID,
786f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        ssize_t *index) {
787f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) {
788f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        BufferInfo *info = &mBuffers[portIndex].editItemAt(i);
789f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
790f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (info->mBufferID == bufferID) {
791f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            if (index != NULL) {
792f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                *index = i;
793f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
794f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return info;
795f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
796f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
797f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
798f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    TRESPASS();
799f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
800f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return NULL;
801f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
802f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
8035778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setComponentRole(
804f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        bool isEncoder, const char *mime) {
805f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    struct MimeToRole {
806f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        const char *mime;
807f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        const char *decoderRole;
808f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        const char *encoderRole;
809f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    };
810f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
811f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    static const MimeToRole kMimeToRole[] = {
812f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        { MEDIA_MIMETYPE_AUDIO_MPEG,
813f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            "audio_decoder.mp3", "audio_encoder.mp3" },
8142944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber        { MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I,
8152944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber            "audio_decoder.mp1", "audio_encoder.mp1" },
8162944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber        { MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II,
8172944eca607304a095ea43ba2b8f0b9de61249f9fAndreas Huber            "audio_decoder.mp2", "audio_encoder.mp2" },
818f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        { MEDIA_MIMETYPE_AUDIO_AMR_NB,
819f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            "audio_decoder.amrnb", "audio_encoder.amrnb" },
820f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        { MEDIA_MIMETYPE_AUDIO_AMR_WB,
821f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            "audio_decoder.amrwb", "audio_encoder.amrwb" },
822f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        { MEDIA_MIMETYPE_AUDIO_AAC,
823f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            "audio_decoder.aac", "audio_encoder.aac" },
824729de186450f78c099637e1fce743fe531862c52Andreas Huber        { MEDIA_MIMETYPE_AUDIO_VORBIS,
825729de186450f78c099637e1fce743fe531862c52Andreas Huber            "audio_decoder.vorbis", "audio_encoder.vorbis" },
826c5255ac5b1bd313dcb50159566163b24dce7483fAndreas Huber        { MEDIA_MIMETYPE_AUDIO_G711_MLAW,
827c5255ac5b1bd313dcb50159566163b24dce7483fAndreas Huber            "audio_decoder.g711mlaw", "audio_encoder.g711mlaw" },
828c5255ac5b1bd313dcb50159566163b24dce7483fAndreas Huber        { MEDIA_MIMETYPE_AUDIO_G711_ALAW,
829c5255ac5b1bd313dcb50159566163b24dce7483fAndreas Huber            "audio_decoder.g711alaw", "audio_encoder.g711alaw" },
830f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        { MEDIA_MIMETYPE_VIDEO_AVC,
831f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            "video_decoder.avc", "video_encoder.avc" },
832f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        { MEDIA_MIMETYPE_VIDEO_MPEG4,
833f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            "video_decoder.mpeg4", "video_encoder.mpeg4" },
834f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        { MEDIA_MIMETYPE_VIDEO_H263,
835f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            "video_decoder.h263", "video_encoder.h263" },
8365778822d86b0337407514b9372562b86edfa91cdAndreas Huber        { MEDIA_MIMETYPE_VIDEO_VPX,
8375778822d86b0337407514b9372562b86edfa91cdAndreas Huber            "video_decoder.vpx", "video_encoder.vpx" },
838ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber        { MEDIA_MIMETYPE_AUDIO_RAW,
839ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber            "audio_decoder.raw", "audio_encoder.raw" },
8402f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        { MEDIA_MIMETYPE_AUDIO_FLAC,
8412f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            "audio_decoder.flac", "audio_encoder.flac" },
842ba933df89521d63f75ca66af12ce9d7ae9496b9eMarco Nelissen        { MEDIA_MIMETYPE_AUDIO_MSGSM,
843ba933df89521d63f75ca66af12ce9d7ae9496b9eMarco Nelissen            "audio_decoder.gsm", "audio_encoder.gsm" },
844f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    };
845f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
846f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    static const size_t kNumMimeToRole =
847f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        sizeof(kMimeToRole) / sizeof(kMimeToRole[0]);
848f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
849f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    size_t i;
850f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (i = 0; i < kNumMimeToRole; ++i) {
851f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (!strcasecmp(mime, kMimeToRole[i].mime)) {
852f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
853f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
854f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
855f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
856f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (i == kNumMimeToRole) {
8575778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return ERROR_UNSUPPORTED;
858f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
859f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
860f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    const char *role =
861f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        isEncoder ? kMimeToRole[i].encoderRole
862f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                  : kMimeToRole[i].decoderRole;
863f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
864f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (role != NULL) {
865f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_PARAM_COMPONENTROLETYPE roleParams;
866f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        InitOMXParams(&roleParams);
867f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
868f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        strncpy((char *)roleParams.cRole,
869f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                role, OMX_MAX_STRINGNAME_SIZE - 1);
870f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
871f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        roleParams.cRole[OMX_MAX_STRINGNAME_SIZE - 1] = '\0';
872f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
873f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        status_t err = mOMX->setParameter(
874f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                mNode, OMX_IndexParamStandardComponentRole,
875f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                &roleParams, sizeof(roleParams));
876f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
877f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (err != OK) {
8785ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block            ALOGW("[%s] Failed to set standard component role '%s'.",
879f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                 mComponentName.c_str(), role);
8805778822d86b0337407514b9372562b86edfa91cdAndreas Huber
8815778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
882f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
883f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
8845778822d86b0337407514b9372562b86edfa91cdAndreas Huber
8855778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return OK;
886f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
887f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
8885778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::configureCodec(
889f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        const char *mime, const sp<AMessage> &msg) {
8905778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t encoder;
8915778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findInt32("encoder", &encoder)) {
8925778822d86b0337407514b9372562b86edfa91cdAndreas Huber        encoder = false;
8935778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
894f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
8955778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mIsEncoder = encoder;
896f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
8975778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err = setComponentRole(encoder /* isEncoder */, mime);
8985778822d86b0337407514b9372562b86edfa91cdAndreas Huber
8995778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
9005778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
9015778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
9025778822d86b0337407514b9372562b86edfa91cdAndreas Huber
9035778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t bitRate = 0;
9042f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi    // FLAC encoder doesn't need a bitrate, other encoders do
9052f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi    if (encoder && strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_FLAC)
9062f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            && !msg->findInt32("bitrate", &bitRate)) {
9075778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return INVALID_OPERATION;
9085778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
9095778822d86b0337407514b9372562b86edfa91cdAndreas Huber
910d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    int32_t storeMeta;
911d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (encoder
912d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            && msg->findInt32("store-metadata-in-buffers", &storeMeta)
913d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            && storeMeta != 0) {
914d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        err = mOMX->storeMetaDataInBuffers(mNode, kPortIndexInput, OMX_TRUE);
915d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
916d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        if (err != OK) {
917d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            ALOGE("[%s] storeMetaDataInBuffers failed w/ err %d",
918d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                  mComponentName.c_str(), err);
919d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
920d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            return err;
921d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        }
922d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
923d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
9243a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber    int32_t prependSPSPPS;
9253a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber    if (encoder
9263a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber            && msg->findInt32("prepend-sps-pps-to-idr-frames", &prependSPSPPS)
9273a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber            && prependSPSPPS != 0) {
9283a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber        OMX_INDEXTYPE index;
9293a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber        err = mOMX->getExtensionIndex(
9303a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber                mNode,
9313a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber                "OMX.google.android.index.prependSPSPPSToIDRFrames",
9323a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber                &index);
9333a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber
9343a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber        if (err == OK) {
9353a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber            PrependSPSPPSToIDRFramesParams params;
9363a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber            InitOMXParams(&params);
9373a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber            params.bEnable = OMX_TRUE;
9383a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber
9393a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber            err = mOMX->setParameter(
9403a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber                    mNode, index, &params, sizeof(params));
9413a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber        }
9423a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber
9433a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber        if (err != OK) {
9443a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber            ALOGE("Encoder could not be configured to emit SPS/PPS before "
9453a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber                  "IDR frames. (err %d)", err);
9463a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber
9473a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber            return err;
9483a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber        }
9493a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber    }
9503a0ef0dc71f3776d115f30d87f1d8867daefea88Andreas Huber
9515778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!strncasecmp(mime, "video/", 6)) {
9525778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (encoder) {
9535778822d86b0337407514b9372562b86edfa91cdAndreas Huber            err = setupVideoEncoder(mime, msg);
9545778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else {
9555778822d86b0337407514b9372562b86edfa91cdAndreas Huber            int32_t width, height;
9565778822d86b0337407514b9372562b86edfa91cdAndreas Huber            if (!msg->findInt32("width", &width)
9575778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    || !msg->findInt32("height", &height)) {
9585778822d86b0337407514b9372562b86edfa91cdAndreas Huber                err = INVALID_OPERATION;
9595778822d86b0337407514b9372562b86edfa91cdAndreas Huber            } else {
9605778822d86b0337407514b9372562b86edfa91cdAndreas Huber                err = setupVideoDecoder(mime, width, height);
9615778822d86b0337407514b9372562b86edfa91cdAndreas Huber            }
9625778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
96342392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)) {
96442392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber        int32_t numChannels, sampleRate;
96542392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber        if (!msg->findInt32("channel-count", &numChannels)
96642392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber                || !msg->findInt32("sample-rate", &sampleRate)) {
96742392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber            // Since we did not always check for these, leave them optional
96842392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber            // and have the decoder figure it all out.
96942392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber            err = OK;
97042392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber        } else {
97142392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber            err = setupRawAudioFormat(
97242392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber                    encoder ? kPortIndexInput : kPortIndexOutput,
97342392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber                    sampleRate,
97442392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber                    numChannels);
97542392e49e167c6a0c573e55e1c1b4c7fa0ceb213Andreas Huber        }
976f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) {
977f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        int32_t numChannels, sampleRate;
9785778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("channel-count", &numChannels)
9795778822d86b0337407514b9372562b86edfa91cdAndreas Huber                || !msg->findInt32("sample-rate", &sampleRate)) {
9805778822d86b0337407514b9372562b86edfa91cdAndreas Huber            err = INVALID_OPERATION;
9815778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else {
982aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke            int32_t isADTS, aacProfile;
983ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber            if (!msg->findInt32("is-adts", &isADTS)) {
984ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber                isADTS = 0;
985ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber            }
986aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke            if (!msg->findInt32("aac-profile", &aacProfile)) {
987aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke                aacProfile = OMX_AUDIO_AACObjectNull;
988aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke            }
989ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
990ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber            err = setupAACCodec(
9914471e47ece77be5b4159cc9cbbcef4d43900d36fAndreas Huber                    encoder, numChannels, sampleRate, bitRate, aacProfile,
9924471e47ece77be5b4159cc9cbbcef4d43900d36fAndreas Huber                    isADTS != 0);
9935778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
994729de186450f78c099637e1fce743fe531862c52Andreas Huber    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)) {
9955778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = setupAMRCodec(encoder, false /* isWAMR */, bitRate);
996729de186450f78c099637e1fce743fe531862c52Andreas Huber    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
9975778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = setupAMRCodec(encoder, true /* isWAMR */, bitRate);
998729de186450f78c099637e1fce743fe531862c52Andreas Huber    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_G711_ALAW)
999729de186450f78c099637e1fce743fe531862c52Andreas Huber            || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_G711_MLAW)) {
1000729de186450f78c099637e1fce743fe531862c52Andreas Huber        // These are PCM-like formats with a fixed sample rate but
1001729de186450f78c099637e1fce743fe531862c52Andreas Huber        // a variable number of channels.
1002729de186450f78c099637e1fce743fe531862c52Andreas Huber
1003729de186450f78c099637e1fce743fe531862c52Andreas Huber        int32_t numChannels;
10045778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("channel-count", &numChannels)) {
10055778822d86b0337407514b9372562b86edfa91cdAndreas Huber            err = INVALID_OPERATION;
10065778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else {
10075778822d86b0337407514b9372562b86edfa91cdAndreas Huber            err = setupG711Codec(encoder, numChannels);
10085778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
10092f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_FLAC)) {
10102f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        int32_t numChannels, sampleRate, compressionLevel = -1;
10112f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        if (encoder &&
10122f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi                (!msg->findInt32("channel-count", &numChannels)
10132f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi                        || !msg->findInt32("sample-rate", &sampleRate))) {
10142f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            ALOGE("missing channel count or sample rate for FLAC encoder");
10152f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            err = INVALID_OPERATION;
10162f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        } else {
10172f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            if (encoder) {
1018516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber                if (!msg->findInt32(
1019516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber                            "flac-compression-level", &compressionLevel)) {
10202f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi                    compressionLevel = 5;// default FLAC compression level
10212f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi                } else if (compressionLevel < 0) {
1022516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber                    ALOGW("compression level %d outside [0..8] range, "
1023516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber                          "using 0",
1024516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber                          compressionLevel);
10252f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi                    compressionLevel = 0;
10262f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi                } else if (compressionLevel > 8) {
1027516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber                    ALOGW("compression level %d outside [0..8] range, "
1028516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber                          "using 8",
1029516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber                          compressionLevel);
10302f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi                    compressionLevel = 8;
10312f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi                }
10322f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            }
1033516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber            err = setupFlacCodec(
1034516dacfb02d0b0eafe21114330c98ce0e7d90da9Andreas Huber                    encoder, numChannels, sampleRate, compressionLevel);
10352f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        }
1036ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber    } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW)) {
1037ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber        int32_t numChannels, sampleRate;
1038ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber        if (encoder
1039ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber                || !msg->findInt32("channel-count", &numChannels)
1040ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber                || !msg->findInt32("sample-rate", &sampleRate)) {
1041ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber            err = INVALID_OPERATION;
1042ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber        } else {
1043ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber            err = setupRawAudioFormat(kPortIndexInput, sampleRate, numChannels);
1044ecdd39c5af016e2fa57cbfd837aa670b706dabd3Andreas Huber        }
10455778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
1046729de186450f78c099637e1fce743fe531862c52Andreas Huber
10474471e47ece77be5b4159cc9cbbcef4d43900d36fAndreas Huber    if (err != OK) {
10484471e47ece77be5b4159cc9cbbcef4d43900d36fAndreas Huber        return err;
10494471e47ece77be5b4159cc9cbbcef4d43900d36fAndreas Huber    }
10504471e47ece77be5b4159cc9cbbcef4d43900d36fAndreas Huber
10518b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen    if (!msg->findInt32("encoder-delay", &mEncoderDelay)) {
10528b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen        mEncoderDelay = 0;
10538b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen    }
10549806555d3930be43e11106281dee354820ac1c88Andreas Huber
10558b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen    if (!msg->findInt32("encoder-padding", &mEncoderPadding)) {
10568b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen        mEncoderPadding = 0;
10578b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen    }
10588b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen
10599806555d3930be43e11106281dee354820ac1c88Andreas Huber    if (msg->findInt32("channel-mask", &mChannelMask)) {
10609806555d3930be43e11106281dee354820ac1c88Andreas Huber        mChannelMaskPresent = true;
10619806555d3930be43e11106281dee354820ac1c88Andreas Huber    } else {
10629806555d3930be43e11106281dee354820ac1c88Andreas Huber        mChannelMaskPresent = false;
10639806555d3930be43e11106281dee354820ac1c88Andreas Huber    }
10649806555d3930be43e11106281dee354820ac1c88Andreas Huber
1065f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    int32_t maxInputSize;
1066f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (msg->findInt32("max-input-size", &maxInputSize)) {
10675778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = setMinBufferSize(kPortIndexInput, (size_t)maxInputSize);
1068f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    } else if (!strcmp("OMX.Nvidia.aac.decoder", mComponentName.c_str())) {
10695778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = setMinBufferSize(kPortIndexInput, 8192);  // XXX
1070f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
10715778822d86b0337407514b9372562b86edfa91cdAndreas Huber
10725778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return err;
1073f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
1074f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1075f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::setMinBufferSize(OMX_U32 portIndex, size_t size) {
1076f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_PARAM_PORTDEFINITIONTYPE def;
1077f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    InitOMXParams(&def);
1078f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    def.nPortIndex = portIndex;
1079f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1080f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    status_t err = mOMX->getParameter(
1081f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1082f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1083f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
1084f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
1085f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1086f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1087f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (def.nBufferSize >= size) {
1088f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return OK;
1089f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1090f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1091f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    def.nBufferSize = size;
1092f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1093f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    err = mOMX->setParameter(
1094f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1095f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1096f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
1097f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
1098f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1099f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1100f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    err = mOMX->getParameter(
1101f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1102f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1103f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
1104f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
1105f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1106f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1107f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK(def.nBufferSize >= size);
1108f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1109f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return OK;
1110f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
1111f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
11125778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::selectAudioPortFormat(
11135778822d86b0337407514b9372562b86edfa91cdAndreas Huber        OMX_U32 portIndex, OMX_AUDIO_CODINGTYPE desiredFormat) {
11145778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_AUDIO_PARAM_PORTFORMATTYPE format;
11155778822d86b0337407514b9372562b86edfa91cdAndreas Huber    InitOMXParams(&format);
11165778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11175778822d86b0337407514b9372562b86edfa91cdAndreas Huber    format.nPortIndex = portIndex;
11185778822d86b0337407514b9372562b86edfa91cdAndreas Huber    for (OMX_U32 index = 0;; ++index) {
11195778822d86b0337407514b9372562b86edfa91cdAndreas Huber        format.nIndex = index;
11205778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11215778822d86b0337407514b9372562b86edfa91cdAndreas Huber        status_t err = mOMX->getParameter(
11225778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mNode, OMX_IndexParamAudioPortFormat,
11235778822d86b0337407514b9372562b86edfa91cdAndreas Huber                &format, sizeof(format));
11245778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11255778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
11265778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
11275778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
11285778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11295778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (format.eEncoding == desiredFormat) {
11305778822d86b0337407514b9372562b86edfa91cdAndreas Huber            break;
11315778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
11325778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
11335778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11345778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return mOMX->setParameter(
11355778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamAudioPortFormat, &format, sizeof(format));
11365778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
11375778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11385778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setupAACCodec(
1139aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke        bool encoder, int32_t numChannels, int32_t sampleRate,
1140aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke        int32_t bitRate, int32_t aacProfile, bool isADTS) {
1141ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber    if (encoder && isADTS) {
1142ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber        return -EINVAL;
1143ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber    }
1144ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
11455778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err = setupRawAudioFormat(
11465778822d86b0337407514b9372562b86edfa91cdAndreas Huber            encoder ? kPortIndexInput : kPortIndexOutput,
11475778822d86b0337407514b9372562b86edfa91cdAndreas Huber            sampleRate,
11485778822d86b0337407514b9372562b86edfa91cdAndreas Huber            numChannels);
11495778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11505778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
11515778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
11525778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
11535778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11545778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (encoder) {
11555778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = selectAudioPortFormat(kPortIndexOutput, OMX_AUDIO_CodingAAC);
11565778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11575778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
11585778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
11595778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
11605778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11615778822d86b0337407514b9372562b86edfa91cdAndreas Huber        OMX_PARAM_PORTDEFINITIONTYPE def;
11625778822d86b0337407514b9372562b86edfa91cdAndreas Huber        InitOMXParams(&def);
11635778822d86b0337407514b9372562b86edfa91cdAndreas Huber        def.nPortIndex = kPortIndexOutput;
11645778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11655778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = mOMX->getParameter(
11665778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
11675778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11685778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
11695778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
11705778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
11715778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11725778822d86b0337407514b9372562b86edfa91cdAndreas Huber        def.format.audio.bFlagErrorConcealment = OMX_TRUE;
11735778822d86b0337407514b9372562b86edfa91cdAndreas Huber        def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
11745778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11755778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = mOMX->setParameter(
11765778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
11775778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11785778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
11795778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
11805778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
11815778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11825778822d86b0337407514b9372562b86edfa91cdAndreas Huber        OMX_AUDIO_PARAM_AACPROFILETYPE profile;
11835778822d86b0337407514b9372562b86edfa91cdAndreas Huber        InitOMXParams(&profile);
11845778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.nPortIndex = kPortIndexOutput;
11855778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11865778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = mOMX->getParameter(
11875778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
11885778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11895778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
11905778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
11915778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
11925778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11935778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.nChannels = numChannels;
11945778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11955778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.eChannelMode =
11965778822d86b0337407514b9372562b86edfa91cdAndreas Huber            (numChannels == 1)
11975778822d86b0337407514b9372562b86edfa91cdAndreas Huber                ? OMX_AUDIO_ChannelModeMono: OMX_AUDIO_ChannelModeStereo;
11985778822d86b0337407514b9372562b86edfa91cdAndreas Huber
11995778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.nSampleRate = sampleRate;
12005778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.nBitRate = bitRate;
12015778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.nAudioBandWidth = 0;
12025778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.nFrameLength = 0;
12035778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.nAACtools = OMX_AUDIO_AACToolAll;
12045778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.nAACERtools = OMX_AUDIO_AACERNone;
1205aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke        profile.eAACProfile = (OMX_AUDIO_AACPROFILETYPE) aacProfile;
12065778822d86b0337407514b9372562b86edfa91cdAndreas Huber        profile.eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4FF;
12075778822d86b0337407514b9372562b86edfa91cdAndreas Huber
12085778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = mOMX->setParameter(
12095778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
12105778822d86b0337407514b9372562b86edfa91cdAndreas Huber
12115778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
12125778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
12135778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
12145778822d86b0337407514b9372562b86edfa91cdAndreas Huber
12155778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
12165778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
12175778822d86b0337407514b9372562b86edfa91cdAndreas Huber
1218f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_AUDIO_PARAM_AACPROFILETYPE profile;
1219f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    InitOMXParams(&profile);
1220f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    profile.nPortIndex = kPortIndexInput;
1221f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
12225778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = mOMX->getParameter(
1223f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
1224f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1225f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
1226f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
1227f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1228f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1229f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    profile.nChannels = numChannels;
1230f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    profile.nSampleRate = sampleRate;
1231ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
1232ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber    profile.eAACStreamFormat =
1233ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber        isADTS
1234ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber            ? OMX_AUDIO_AACStreamFormatMP4ADTS
1235ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber            : OMX_AUDIO_AACStreamFormatMP4FF;
1236f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
12375778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return mOMX->setParameter(
1238f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamAudioAac, &profile, sizeof(profile));
12395778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
1240f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
12415778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatic OMX_AUDIO_AMRBANDMODETYPE pickModeFromBitRate(
12425778822d86b0337407514b9372562b86edfa91cdAndreas Huber        bool isAMRWB, int32_t bps) {
12435778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (isAMRWB) {
12445778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (bps <= 6600) {
12455778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeWB0;
12465778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 8850) {
12475778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeWB1;
12485778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 12650) {
12495778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeWB2;
12505778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 14250) {
12515778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeWB3;
12525778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 15850) {
12535778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeWB4;
12545778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 18250) {
12555778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeWB5;
12565778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 19850) {
12575778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeWB6;
12585778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 23050) {
12595778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeWB7;
12605778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
12615778822d86b0337407514b9372562b86edfa91cdAndreas Huber
12625778822d86b0337407514b9372562b86edfa91cdAndreas Huber        // 23850 bps
12635778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return OMX_AUDIO_AMRBandModeWB8;
12645778822d86b0337407514b9372562b86edfa91cdAndreas Huber    } else {  // AMRNB
12655778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (bps <= 4750) {
12665778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeNB0;
12675778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 5150) {
12685778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeNB1;
12695778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 5900) {
12705778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeNB2;
12715778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 6700) {
12725778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeNB3;
12735778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 7400) {
12745778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeNB4;
12755778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 7950) {
12765778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeNB5;
12775778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else if (bps <= 10200) {
12785778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OMX_AUDIO_AMRBandModeNB6;
12795778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
12805778822d86b0337407514b9372562b86edfa91cdAndreas Huber
12815778822d86b0337407514b9372562b86edfa91cdAndreas Huber        // 12200 bps
12825778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return OMX_AUDIO_AMRBandModeNB7;
12835778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
1284f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
1285f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
12865778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setupAMRCodec(bool encoder, bool isWAMR, int32_t bitrate) {
1287729de186450f78c099637e1fce743fe531862c52Andreas Huber    OMX_AUDIO_PARAM_AMRTYPE def;
1288729de186450f78c099637e1fce743fe531862c52Andreas Huber    InitOMXParams(&def);
12895778822d86b0337407514b9372562b86edfa91cdAndreas Huber    def.nPortIndex = encoder ? kPortIndexOutput : kPortIndexInput;
1290729de186450f78c099637e1fce743fe531862c52Andreas Huber
1291729de186450f78c099637e1fce743fe531862c52Andreas Huber    status_t err =
1292729de186450f78c099637e1fce743fe531862c52Andreas Huber        mOMX->getParameter(mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1293729de186450f78c099637e1fce743fe531862c52Andreas Huber
1294729de186450f78c099637e1fce743fe531862c52Andreas Huber    if (err != OK) {
1295729de186450f78c099637e1fce743fe531862c52Andreas Huber        return err;
1296729de186450f78c099637e1fce743fe531862c52Andreas Huber    }
1297729de186450f78c099637e1fce743fe531862c52Andreas Huber
1298729de186450f78c099637e1fce743fe531862c52Andreas Huber    def.eAMRFrameFormat = OMX_AUDIO_AMRFrameFormatFSF;
12995778822d86b0337407514b9372562b86edfa91cdAndreas Huber    def.eAMRBandMode = pickModeFromBitRate(isWAMR, bitrate);
13005778822d86b0337407514b9372562b86edfa91cdAndreas Huber
13015778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = mOMX->setParameter(
13025778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamAudioAmr, &def, sizeof(def));
1303729de186450f78c099637e1fce743fe531862c52Andreas Huber
13045778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
13055778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
13065778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
1307729de186450f78c099637e1fce743fe531862c52Andreas Huber
13085778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return setupRawAudioFormat(
13095778822d86b0337407514b9372562b86edfa91cdAndreas Huber            encoder ? kPortIndexInput : kPortIndexOutput,
13105778822d86b0337407514b9372562b86edfa91cdAndreas Huber            isWAMR ? 16000 : 8000 /* sampleRate */,
13115778822d86b0337407514b9372562b86edfa91cdAndreas Huber            1 /* numChannels */);
1312729de186450f78c099637e1fce743fe531862c52Andreas Huber}
1313729de186450f78c099637e1fce743fe531862c52Andreas Huber
13145778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setupG711Codec(bool encoder, int32_t numChannels) {
13155778822d86b0337407514b9372562b86edfa91cdAndreas Huber    CHECK(!encoder);  // XXX TODO
13165778822d86b0337407514b9372562b86edfa91cdAndreas Huber
1317729de186450f78c099637e1fce743fe531862c52Andreas Huber    return setupRawAudioFormat(
1318729de186450f78c099637e1fce743fe531862c52Andreas Huber            kPortIndexInput, 8000 /* sampleRate */, numChannels);
1319729de186450f78c099637e1fce743fe531862c52Andreas Huber}
1320729de186450f78c099637e1fce743fe531862c52Andreas Huber
13212f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivistatus_t ACodec::setupFlacCodec(
13222f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        bool encoder, int32_t numChannels, int32_t sampleRate, int32_t compressionLevel) {
13232f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi
13242f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi    if (encoder) {
13252f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        OMX_AUDIO_PARAM_FLACTYPE def;
13262f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        InitOMXParams(&def);
13272f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        def.nPortIndex = kPortIndexOutput;
13282f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi
13292f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        // configure compression level
13302f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        status_t err = mOMX->getParameter(mNode, OMX_IndexParamAudioFlac, &def, sizeof(def));
13312f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        if (err != OK) {
13322f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            ALOGE("setupFlacCodec(): Error %d getting OMX_IndexParamAudioFlac parameter", err);
13332f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            return err;
13342f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        }
13352f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        def.nCompressionLevel = compressionLevel;
13362f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        err = mOMX->setParameter(mNode, OMX_IndexParamAudioFlac, &def, sizeof(def));
13372f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        if (err != OK) {
13382f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            ALOGE("setupFlacCodec(): Error %d setting OMX_IndexParamAudioFlac parameter", err);
13392f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            return err;
13402f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi        }
13412f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi    }
13422f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi
13432f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi    return setupRawAudioFormat(
13442f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            encoder ? kPortIndexInput : kPortIndexOutput,
13452f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            sampleRate,
13462f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi            numChannels);
13472f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi}
13482f74ef3cdc192f817ee1121f41765f0852c1d81eJean-Michel Trivi
1349729de186450f78c099637e1fce743fe531862c52Andreas Huberstatus_t ACodec::setupRawAudioFormat(
1350729de186450f78c099637e1fce743fe531862c52Andreas Huber        OMX_U32 portIndex, int32_t sampleRate, int32_t numChannels) {
1351729de186450f78c099637e1fce743fe531862c52Andreas Huber    OMX_PARAM_PORTDEFINITIONTYPE def;
1352729de186450f78c099637e1fce743fe531862c52Andreas Huber    InitOMXParams(&def);
1353729de186450f78c099637e1fce743fe531862c52Andreas Huber    def.nPortIndex = portIndex;
1354729de186450f78c099637e1fce743fe531862c52Andreas Huber
1355729de186450f78c099637e1fce743fe531862c52Andreas Huber    status_t err = mOMX->getParameter(
1356729de186450f78c099637e1fce743fe531862c52Andreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1357729de186450f78c099637e1fce743fe531862c52Andreas Huber
1358729de186450f78c099637e1fce743fe531862c52Andreas Huber    if (err != OK) {
1359729de186450f78c099637e1fce743fe531862c52Andreas Huber        return err;
1360729de186450f78c099637e1fce743fe531862c52Andreas Huber    }
1361729de186450f78c099637e1fce743fe531862c52Andreas Huber
1362729de186450f78c099637e1fce743fe531862c52Andreas Huber    def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
1363729de186450f78c099637e1fce743fe531862c52Andreas Huber
1364729de186450f78c099637e1fce743fe531862c52Andreas Huber    err = mOMX->setParameter(
1365729de186450f78c099637e1fce743fe531862c52Andreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
1366729de186450f78c099637e1fce743fe531862c52Andreas Huber
1367729de186450f78c099637e1fce743fe531862c52Andreas Huber    if (err != OK) {
1368729de186450f78c099637e1fce743fe531862c52Andreas Huber        return err;
1369729de186450f78c099637e1fce743fe531862c52Andreas Huber    }
1370729de186450f78c099637e1fce743fe531862c52Andreas Huber
1371729de186450f78c099637e1fce743fe531862c52Andreas Huber    OMX_AUDIO_PARAM_PCMMODETYPE pcmParams;
1372729de186450f78c099637e1fce743fe531862c52Andreas Huber    InitOMXParams(&pcmParams);
1373729de186450f78c099637e1fce743fe531862c52Andreas Huber    pcmParams.nPortIndex = portIndex;
1374729de186450f78c099637e1fce743fe531862c52Andreas Huber
1375729de186450f78c099637e1fce743fe531862c52Andreas Huber    err = mOMX->getParameter(
1376729de186450f78c099637e1fce743fe531862c52Andreas Huber            mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1377729de186450f78c099637e1fce743fe531862c52Andreas Huber
1378729de186450f78c099637e1fce743fe531862c52Andreas Huber    if (err != OK) {
1379729de186450f78c099637e1fce743fe531862c52Andreas Huber        return err;
1380729de186450f78c099637e1fce743fe531862c52Andreas Huber    }
1381729de186450f78c099637e1fce743fe531862c52Andreas Huber
1382729de186450f78c099637e1fce743fe531862c52Andreas Huber    pcmParams.nChannels = numChannels;
1383729de186450f78c099637e1fce743fe531862c52Andreas Huber    pcmParams.eNumData = OMX_NumericalDataSigned;
1384729de186450f78c099637e1fce743fe531862c52Andreas Huber    pcmParams.bInterleaved = OMX_TRUE;
1385729de186450f78c099637e1fce743fe531862c52Andreas Huber    pcmParams.nBitPerSample = 16;
1386729de186450f78c099637e1fce743fe531862c52Andreas Huber    pcmParams.nSamplingRate = sampleRate;
1387729de186450f78c099637e1fce743fe531862c52Andreas Huber    pcmParams.ePCMMode = OMX_AUDIO_PCMModeLinear;
1388729de186450f78c099637e1fce743fe531862c52Andreas Huber
1389c1d8115e8a0bdaeb2b723d395b9a85a02c90c933Andreas Huber    if (getOMXChannelMapping(numChannels, pcmParams.eChannelMapping) != OK) {
1390c1d8115e8a0bdaeb2b723d395b9a85a02c90c933Andreas Huber        return OMX_ErrorNone;
1391729de186450f78c099637e1fce743fe531862c52Andreas Huber    }
1392729de186450f78c099637e1fce743fe531862c52Andreas Huber
1393729de186450f78c099637e1fce743fe531862c52Andreas Huber    return mOMX->setParameter(
1394729de186450f78c099637e1fce743fe531862c52Andreas Huber            mNode, OMX_IndexParamAudioPcm, &pcmParams, sizeof(pcmParams));
1395729de186450f78c099637e1fce743fe531862c52Andreas Huber}
1396729de186450f78c099637e1fce743fe531862c52Andreas Huber
1397f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::setVideoPortFormatType(
1398f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_U32 portIndex,
1399f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_VIDEO_CODINGTYPE compressionFormat,
1400f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_COLOR_FORMATTYPE colorFormat) {
1401f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_VIDEO_PARAM_PORTFORMATTYPE format;
1402f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    InitOMXParams(&format);
1403f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    format.nPortIndex = portIndex;
1404f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    format.nIndex = 0;
1405f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool found = false;
1406f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1407f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_U32 index = 0;
1408f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (;;) {
1409f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        format.nIndex = index;
1410f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        status_t err = mOMX->getParameter(
1411f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                mNode, OMX_IndexParamVideoPortFormat,
1412f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                &format, sizeof(format));
1413f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1414f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (err != OK) {
1415f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return err;
1416f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
1417f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1418f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // The following assertion is violated by TI's video decoder.
1419f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // CHECK_EQ(format.nIndex, index);
1420f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1421f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (!strcmp("OMX.TI.Video.encoder", mComponentName.c_str())) {
1422f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            if (portIndex == kPortIndexInput
1423f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    && colorFormat == format.eColorFormat) {
1424f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                // eCompressionFormat does not seem right.
1425f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                found = true;
1426f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                break;
1427f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
1428f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            if (portIndex == kPortIndexOutput
1429f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    && compressionFormat == format.eCompressionFormat) {
1430f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                // eColorFormat does not seem right.
1431f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                found = true;
1432f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                break;
1433f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
1434f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
1435f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1436f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (format.eCompressionFormat == compressionFormat
1437f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            && format.eColorFormat == colorFormat) {
1438f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            found = true;
1439f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
1440f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
1441f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1442f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        ++index;
1443f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1444f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1445f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (!found) {
1446f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return UNKNOWN_ERROR;
1447f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1448f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1449f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    status_t err = mOMX->setParameter(
1450f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamVideoPortFormat,
1451f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            &format, sizeof(format));
1452f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1453f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return err;
1454f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
1455f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1456f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::setSupportedOutputFormat() {
1457f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_VIDEO_PARAM_PORTFORMATTYPE format;
1458f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    InitOMXParams(&format);
1459f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    format.nPortIndex = kPortIndexOutput;
1460f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    format.nIndex = 0;
1461f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1462f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    status_t err = mOMX->getParameter(
1463f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamVideoPortFormat,
1464f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            &format, sizeof(format));
1465f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(err, (status_t)OK);
1466f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ((int)format.eCompressionFormat, (int)OMX_VIDEO_CodingUnused);
1467f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1468f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return mOMX->setParameter(
1469f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamVideoPortFormat,
1470f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            &format, sizeof(format));
1471f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
1472f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1473e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huberstatic const struct VideoCodingMapEntry {
1474e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    const char *mMime;
1475e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    OMX_VIDEO_CODINGTYPE mVideoCodingType;
1476e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber} kVideoCodingMapEntry[] = {
1477e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    { MEDIA_MIMETYPE_VIDEO_AVC, OMX_VIDEO_CodingAVC },
1478e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    { MEDIA_MIMETYPE_VIDEO_MPEG4, OMX_VIDEO_CodingMPEG4 },
1479e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    { MEDIA_MIMETYPE_VIDEO_H263, OMX_VIDEO_CodingH263 },
1480e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    { MEDIA_MIMETYPE_VIDEO_MPEG2, OMX_VIDEO_CodingMPEG2 },
1481e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    { MEDIA_MIMETYPE_VIDEO_VPX, OMX_VIDEO_CodingVPX },
1482e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber};
1483e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
14845778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatic status_t GetVideoCodingTypeFromMime(
14855778822d86b0337407514b9372562b86edfa91cdAndreas Huber        const char *mime, OMX_VIDEO_CODINGTYPE *codingType) {
1486e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    for (size_t i = 0;
1487e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber         i < sizeof(kVideoCodingMapEntry) / sizeof(kVideoCodingMapEntry[0]);
1488e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber         ++i) {
1489e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber        if (!strcasecmp(mime, kVideoCodingMapEntry[i].mMime)) {
1490e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            *codingType = kVideoCodingMapEntry[i].mVideoCodingType;
1491e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            return OK;
1492e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber        }
1493f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1494f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1495e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    *codingType = OMX_VIDEO_CodingUnused;
1496e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
1497e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    return ERROR_UNSUPPORTED;
1498e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber}
1499e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
1500e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huberstatic status_t GetMimeTypeForVideoCoding(
1501e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber        OMX_VIDEO_CODINGTYPE codingType, AString *mime) {
1502e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    for (size_t i = 0;
1503e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber         i < sizeof(kVideoCodingMapEntry) / sizeof(kVideoCodingMapEntry[0]);
1504e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber         ++i) {
1505e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber        if (codingType == kVideoCodingMapEntry[i].mVideoCodingType) {
1506e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            *mime = kVideoCodingMapEntry[i].mMime;
1507e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            return OK;
1508e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber        }
1509e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    }
1510e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
1511e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    mime->clear();
1512e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
1513e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber    return ERROR_UNSUPPORTED;
15145778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
15155778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15165778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setupVideoDecoder(
15175778822d86b0337407514b9372562b86edfa91cdAndreas Huber        const char *mime, int32_t width, int32_t height) {
15185778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_VIDEO_CODINGTYPE compressionFormat;
15195778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err = GetVideoCodingTypeFromMime(mime, &compressionFormat);
15205778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15215778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
15225778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
15235778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
15245778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15255778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = setVideoPortFormatType(
1526f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            kPortIndexInput, compressionFormat, OMX_COLOR_FormatUnused);
1527f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1528f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
1529f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
1530f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1531f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1532f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    err = setSupportedOutputFormat();
1533f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1534f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
1535f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
1536f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1537f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1538f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    err = setVideoFormatOnPort(
1539f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            kPortIndexInput, width, height, compressionFormat);
1540f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1541f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
1542f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
1543f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1544f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1545f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    err = setVideoFormatOnPort(
1546f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            kPortIndexOutput, width, height, OMX_VIDEO_CodingUnused);
1547f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1548f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
1549f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
1550f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
1551f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
1552f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return OK;
1553f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
1554f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
15555778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setupVideoEncoder(const char *mime, const sp<AMessage> &msg) {
15565778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t tmp;
15575778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findInt32("color-format", &tmp)) {
15585778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return INVALID_OPERATION;
15595778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
15605778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15615778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_COLOR_FORMATTYPE colorFormat =
15625778822d86b0337407514b9372562b86edfa91cdAndreas Huber        static_cast<OMX_COLOR_FORMATTYPE>(tmp);
15635778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15645778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err = setVideoPortFormatType(
15655778822d86b0337407514b9372562b86edfa91cdAndreas Huber            kPortIndexInput, OMX_VIDEO_CodingUnused, colorFormat);
15665778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15675778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
15685778822d86b0337407514b9372562b86edfa91cdAndreas Huber        ALOGE("[%s] does not support color format %d",
15695778822d86b0337407514b9372562b86edfa91cdAndreas Huber              mComponentName.c_str(), colorFormat);
15705778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15715778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
15725778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
15735778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15745778822d86b0337407514b9372562b86edfa91cdAndreas Huber    /* Input port configuration */
15755778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15765778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_PARAM_PORTDEFINITIONTYPE def;
15775778822d86b0337407514b9372562b86edfa91cdAndreas Huber    InitOMXParams(&def);
15785778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15795778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
15805778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15815778822d86b0337407514b9372562b86edfa91cdAndreas Huber    def.nPortIndex = kPortIndexInput;
15825778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15835778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = mOMX->getParameter(
15845778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
15855778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15865778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
15875778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
15885778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
15895778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15905778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t width, height, bitrate;
15915778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findInt32("width", &width)
15925778822d86b0337407514b9372562b86edfa91cdAndreas Huber            || !msg->findInt32("height", &height)
15935778822d86b0337407514b9372562b86edfa91cdAndreas Huber            || !msg->findInt32("bitrate", &bitrate)) {
15945778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return INVALID_OPERATION;
15955778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
15965778822d86b0337407514b9372562b86edfa91cdAndreas Huber
15975778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->nFrameWidth = width;
15985778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->nFrameHeight = height;
15995778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16005778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t stride;
16015778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findInt32("stride", &stride)) {
16025778822d86b0337407514b9372562b86edfa91cdAndreas Huber        stride = width;
16035778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
16045778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16055778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->nStride = stride;
16065778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16075778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t sliceHeight;
16085778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findInt32("slice-height", &sliceHeight)) {
16095778822d86b0337407514b9372562b86edfa91cdAndreas Huber        sliceHeight = height;
16105778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
16115778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16125778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->nSliceHeight = sliceHeight;
16135778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16145778822d86b0337407514b9372562b86edfa91cdAndreas Huber    def.nBufferSize = (video_def->nStride * video_def->nSliceHeight * 3) / 2;
16155778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16165778822d86b0337407514b9372562b86edfa91cdAndreas Huber    float frameRate;
16175778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findFloat("frame-rate", &frameRate)) {
16185778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t tmp;
16195778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("frame-rate", &tmp)) {
16205778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return INVALID_OPERATION;
16215778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
16225778822d86b0337407514b9372562b86edfa91cdAndreas Huber        frameRate = (float)tmp;
16235778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
16245778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16255778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->xFramerate = (OMX_U32)(frameRate * 65536.0f);
16265778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->eCompressionFormat = OMX_VIDEO_CodingUnused;
16275778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->eColorFormat = colorFormat;
16285778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16295778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = mOMX->setParameter(
16305778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
16315778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16325778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
16335778822d86b0337407514b9372562b86edfa91cdAndreas Huber        ALOGE("[%s] failed to set input port definition parameters.",
16345778822d86b0337407514b9372562b86edfa91cdAndreas Huber              mComponentName.c_str());
16355778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16365778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
16375778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
16385778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16395778822d86b0337407514b9372562b86edfa91cdAndreas Huber    /* Output port configuration */
16405778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16415778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_VIDEO_CODINGTYPE compressionFormat;
16425778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = GetVideoCodingTypeFromMime(mime, &compressionFormat);
16435778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16445778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
16455778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
16465778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
16475778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16485778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = setVideoPortFormatType(
16495778822d86b0337407514b9372562b86edfa91cdAndreas Huber            kPortIndexOutput, compressionFormat, OMX_COLOR_FormatUnused);
16505778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16515778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
16525778822d86b0337407514b9372562b86edfa91cdAndreas Huber        ALOGE("[%s] does not support compression format %d",
16535778822d86b0337407514b9372562b86edfa91cdAndreas Huber             mComponentName.c_str(), compressionFormat);
16545778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16555778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
16565778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
16575778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16585778822d86b0337407514b9372562b86edfa91cdAndreas Huber    def.nPortIndex = kPortIndexOutput;
16595778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16605778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = mOMX->getParameter(
16615778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
16625778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16635778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
16645778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
16655778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
16665778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16675778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->nFrameWidth = width;
16685778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->nFrameHeight = height;
16695778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->xFramerate = 0;
16705778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->nBitrate = bitrate;
16715778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->eCompressionFormat = compressionFormat;
16725778822d86b0337407514b9372562b86edfa91cdAndreas Huber    video_def->eColorFormat = OMX_COLOR_FormatUnused;
16735778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16745778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = mOMX->setParameter(
16755778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
16765778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16775778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
16785778822d86b0337407514b9372562b86edfa91cdAndreas Huber        ALOGE("[%s] failed to set output port definition parameters.",
16795778822d86b0337407514b9372562b86edfa91cdAndreas Huber              mComponentName.c_str());
16805778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16815778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
16825778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
16835778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16845778822d86b0337407514b9372562b86edfa91cdAndreas Huber    switch (compressionFormat) {
16855778822d86b0337407514b9372562b86edfa91cdAndreas Huber        case OMX_VIDEO_CodingMPEG4:
16865778822d86b0337407514b9372562b86edfa91cdAndreas Huber            err = setupMPEG4EncoderParameters(msg);
16875778822d86b0337407514b9372562b86edfa91cdAndreas Huber            break;
16885778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16895778822d86b0337407514b9372562b86edfa91cdAndreas Huber        case OMX_VIDEO_CodingH263:
16905778822d86b0337407514b9372562b86edfa91cdAndreas Huber            err = setupH263EncoderParameters(msg);
16915778822d86b0337407514b9372562b86edfa91cdAndreas Huber            break;
16925778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16935778822d86b0337407514b9372562b86edfa91cdAndreas Huber        case OMX_VIDEO_CodingAVC:
16945778822d86b0337407514b9372562b86edfa91cdAndreas Huber            err = setupAVCEncoderParameters(msg);
16955778822d86b0337407514b9372562b86edfa91cdAndreas Huber            break;
16965778822d86b0337407514b9372562b86edfa91cdAndreas Huber
16975778822d86b0337407514b9372562b86edfa91cdAndreas Huber        default:
16985778822d86b0337407514b9372562b86edfa91cdAndreas Huber            break;
16995778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
17005778822d86b0337407514b9372562b86edfa91cdAndreas Huber
17015778822d86b0337407514b9372562b86edfa91cdAndreas Huber    ALOGI("setupVideoEncoder succeeded");
17025778822d86b0337407514b9372562b86edfa91cdAndreas Huber
17035778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return err;
17045778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
17055778822d86b0337407514b9372562b86edfa91cdAndreas Huber
17060dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dongstatus_t ACodec::setCyclicIntraMacroblockRefresh(const sp<AMessage> &msg, int32_t mode) {
17070dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    OMX_VIDEO_PARAM_INTRAREFRESHTYPE params;
17080dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    InitOMXParams(&params);
17090dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    params.nPortIndex = kPortIndexOutput;
17100dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong
17110dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    params.eRefreshMode = static_cast<OMX_VIDEO_INTRAREFRESHTYPE>(mode);
17120dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong
17130dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    if (params.eRefreshMode == OMX_VIDEO_IntraRefreshCyclic ||
17140dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong            params.eRefreshMode == OMX_VIDEO_IntraRefreshBoth) {
17150dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        int32_t mbs;
17160dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        if (!msg->findInt32("intra-refresh-CIR-mbs", &mbs)) {
17170dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong            return INVALID_OPERATION;
17180dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        }
17190dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        params.nCirMBs = mbs;
17200dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    }
17210dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong
17220dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    if (params.eRefreshMode == OMX_VIDEO_IntraRefreshAdaptive ||
17230dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong            params.eRefreshMode == OMX_VIDEO_IntraRefreshBoth) {
17240dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        int32_t mbs;
17250dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        if (!msg->findInt32("intra-refresh-AIR-mbs", &mbs)) {
17260dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong            return INVALID_OPERATION;
17270dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        }
17280dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        params.nAirMBs = mbs;
17290dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong
17300dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        int32_t ref;
17310dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        if (!msg->findInt32("intra-refresh-AIR-ref", &ref)) {
17320dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong            return INVALID_OPERATION;
17330dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        }
17340dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        params.nAirRef = ref;
17350dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    }
17360dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong
17370dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    status_t err = mOMX->setParameter(
17380dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong            mNode, OMX_IndexParamVideoIntraRefresh,
17390dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong            &params, sizeof(params));
17400dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    return err;
17410dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong}
17420dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong
17435778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatic OMX_U32 setPFramesSpacing(int32_t iFramesInterval, int32_t frameRate) {
17445778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (iFramesInterval < 0) {
17455778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return 0xFFFFFFFF;
17465778822d86b0337407514b9372562b86edfa91cdAndreas Huber    } else if (iFramesInterval == 0) {
17475778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return 0;
17485778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
17495778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_U32 ret = frameRate * iFramesInterval;
17505778822d86b0337407514b9372562b86edfa91cdAndreas Huber    CHECK(ret > 1);
17515778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return ret;
17525778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
17535778822d86b0337407514b9372562b86edfa91cdAndreas Huber
175496076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huberstatic OMX_VIDEO_CONTROLRATETYPE getBitrateMode(const sp<AMessage> &msg) {
175596076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    int32_t tmp;
175696076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    if (!msg->findInt32("bitrate-mode", &tmp)) {
175796076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber        return OMX_Video_ControlRateVariable;
175896076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    }
175996076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber
176096076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    return static_cast<OMX_VIDEO_CONTROLRATETYPE>(tmp);
176196076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber}
176296076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber
17635778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setupMPEG4EncoderParameters(const sp<AMessage> &msg) {
17645778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t bitrate, iFrameInterval;
17655778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findInt32("bitrate", &bitrate)
17665778822d86b0337407514b9372562b86edfa91cdAndreas Huber            || !msg->findInt32("i-frame-interval", &iFrameInterval)) {
17675778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return INVALID_OPERATION;
17685778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
17695778822d86b0337407514b9372562b86edfa91cdAndreas Huber
177096076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    OMX_VIDEO_CONTROLRATETYPE bitrateMode = getBitrateMode(msg);
177196076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber
17725778822d86b0337407514b9372562b86edfa91cdAndreas Huber    float frameRate;
17735778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findFloat("frame-rate", &frameRate)) {
17745778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t tmp;
17755778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("frame-rate", &tmp)) {
17765778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return INVALID_OPERATION;
17775778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
17785778822d86b0337407514b9372562b86edfa91cdAndreas Huber        frameRate = (float)tmp;
17795778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
17805778822d86b0337407514b9372562b86edfa91cdAndreas Huber
17815778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_VIDEO_PARAM_MPEG4TYPE mpeg4type;
17825778822d86b0337407514b9372562b86edfa91cdAndreas Huber    InitOMXParams(&mpeg4type);
17835778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.nPortIndex = kPortIndexOutput;
17845778822d86b0337407514b9372562b86edfa91cdAndreas Huber
17855778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err = mOMX->getParameter(
17865778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
17875778822d86b0337407514b9372562b86edfa91cdAndreas Huber
17885778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
17895778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
17905778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
17915778822d86b0337407514b9372562b86edfa91cdAndreas Huber
17925778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.nSliceHeaderSpacing = 0;
17935778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.bSVH = OMX_FALSE;
17945778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.bGov = OMX_FALSE;
17955778822d86b0337407514b9372562b86edfa91cdAndreas Huber
17965778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.nAllowedPictureTypes =
17975778822d86b0337407514b9372562b86edfa91cdAndreas Huber        OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
17985778822d86b0337407514b9372562b86edfa91cdAndreas Huber
17995778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate);
18005778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (mpeg4type.nPFrames == 0) {
18015778822d86b0337407514b9372562b86edfa91cdAndreas Huber        mpeg4type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
18025778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
18035778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.nBFrames = 0;
18045778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.nIDCVLCThreshold = 0;
18055778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.bACPred = OMX_TRUE;
18065778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.nMaxPacketSize = 256;
18075778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.nTimeIncRes = 1000;
18085778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.nHeaderExtension = 0;
18095778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mpeg4type.bReversibleVLC = OMX_FALSE;
18105778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18115778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t profile;
18125778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (msg->findInt32("profile", &profile)) {
18135778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t level;
18145778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("level", &level)) {
18155778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return INVALID_OPERATION;
18165778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
18175778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18185778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = verifySupportForProfileAndLevel(profile, level);
18195778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18205778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
18215778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
18225778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
18235778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18245778822d86b0337407514b9372562b86edfa91cdAndreas Huber        mpeg4type.eProfile = static_cast<OMX_VIDEO_MPEG4PROFILETYPE>(profile);
18255778822d86b0337407514b9372562b86edfa91cdAndreas Huber        mpeg4type.eLevel = static_cast<OMX_VIDEO_MPEG4LEVELTYPE>(level);
18265778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
18275778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18285778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = mOMX->setParameter(
18295778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoMpeg4, &mpeg4type, sizeof(mpeg4type));
18305778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18315778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
18325778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
18335778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
18345778822d86b0337407514b9372562b86edfa91cdAndreas Huber
183596076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    err = configureBitrate(bitrate, bitrateMode);
18365778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18375778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
18385778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
18395778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
18405778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18415778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return setupErrorCorrectionParameters();
18425778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
18435778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18445778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setupH263EncoderParameters(const sp<AMessage> &msg) {
18455778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t bitrate, iFrameInterval;
18465778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findInt32("bitrate", &bitrate)
18475778822d86b0337407514b9372562b86edfa91cdAndreas Huber            || !msg->findInt32("i-frame-interval", &iFrameInterval)) {
18485778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return INVALID_OPERATION;
18495778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
18505778822d86b0337407514b9372562b86edfa91cdAndreas Huber
185196076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    OMX_VIDEO_CONTROLRATETYPE bitrateMode = getBitrateMode(msg);
185296076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber
18535778822d86b0337407514b9372562b86edfa91cdAndreas Huber    float frameRate;
18545778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findFloat("frame-rate", &frameRate)) {
18555778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t tmp;
18565778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("frame-rate", &tmp)) {
18575778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return INVALID_OPERATION;
18585778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
18595778822d86b0337407514b9372562b86edfa91cdAndreas Huber        frameRate = (float)tmp;
18605778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
18615778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18625778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_VIDEO_PARAM_H263TYPE h263type;
18635778822d86b0337407514b9372562b86edfa91cdAndreas Huber    InitOMXParams(&h263type);
18645778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h263type.nPortIndex = kPortIndexOutput;
18655778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18665778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err = mOMX->getParameter(
18675778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
18685778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18695778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
18705778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
18715778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
18725778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18735778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h263type.nAllowedPictureTypes =
18745778822d86b0337407514b9372562b86edfa91cdAndreas Huber        OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
18755778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18765778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h263type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate);
18775778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (h263type.nPFrames == 0) {
18785778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h263type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
18795778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
18805778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h263type.nBFrames = 0;
18815778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18825778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t profile;
18835778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (msg->findInt32("profile", &profile)) {
18845778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t level;
18855778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("level", &level)) {
18865778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return INVALID_OPERATION;
18875778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
18885778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18895778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = verifySupportForProfileAndLevel(profile, level);
18905778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18915778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
18925778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
18935778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
18945778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18955778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h263type.eProfile = static_cast<OMX_VIDEO_H263PROFILETYPE>(profile);
18965778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h263type.eLevel = static_cast<OMX_VIDEO_H263LEVELTYPE>(level);
18975778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
18985778822d86b0337407514b9372562b86edfa91cdAndreas Huber
18995778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h263type.bPLUSPTYPEAllowed = OMX_FALSE;
19005778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h263type.bForceRoundingTypeToZero = OMX_FALSE;
19015778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h263type.nPictureHeaderRepetition = 0;
19025778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h263type.nGOBHeaderInterval = 0;
19035778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19045778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = mOMX->setParameter(
19055778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoH263, &h263type, sizeof(h263type));
19065778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19075778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
19085778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
19095778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
19105778822d86b0337407514b9372562b86edfa91cdAndreas Huber
191196076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    err = configureBitrate(bitrate, bitrateMode);
19125778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19135778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
19145778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
19155778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
19165778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19175778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return setupErrorCorrectionParameters();
19185778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
19195778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19205778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setupAVCEncoderParameters(const sp<AMessage> &msg) {
19215778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t bitrate, iFrameInterval;
19225778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findInt32("bitrate", &bitrate)
19235778822d86b0337407514b9372562b86edfa91cdAndreas Huber            || !msg->findInt32("i-frame-interval", &iFrameInterval)) {
19245778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return INVALID_OPERATION;
19255778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
19265778822d86b0337407514b9372562b86edfa91cdAndreas Huber
192796076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    OMX_VIDEO_CONTROLRATETYPE bitrateMode = getBitrateMode(msg);
192896076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber
19295778822d86b0337407514b9372562b86edfa91cdAndreas Huber    float frameRate;
19305778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (!msg->findFloat("frame-rate", &frameRate)) {
19315778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t tmp;
19325778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("frame-rate", &tmp)) {
19335778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return INVALID_OPERATION;
19345778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
19355778822d86b0337407514b9372562b86edfa91cdAndreas Huber        frameRate = (float)tmp;
19365778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
19375778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19380dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    status_t err = OK;
19390dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    int32_t intraRefreshMode = 0;
19400dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    if (msg->findInt32("intra-refresh-mode", &intraRefreshMode)) {
19410dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        err = setCyclicIntraMacroblockRefresh(msg, intraRefreshMode);
19420dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        if (err != OK) {
19430dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong            ALOGE("Setting intra macroblock refresh mode (%d) failed: 0x%x",
19440dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong                    err, intraRefreshMode);
19450dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong            return err;
19460dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong        }
19470dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    }
19480dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong
19495778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_VIDEO_PARAM_AVCTYPE h264type;
19505778822d86b0337407514b9372562b86edfa91cdAndreas Huber    InitOMXParams(&h264type);
19515778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h264type.nPortIndex = kPortIndexOutput;
19525778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19530dbe5a9321b24b6883fbb2fe97cd9d525128b0b5James Dong    err = mOMX->getParameter(
19545778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
19555778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19565778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
19575778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
19585778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
19595778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19605778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h264type.nAllowedPictureTypes =
19615778822d86b0337407514b9372562b86edfa91cdAndreas Huber        OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
19625778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19635778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t profile;
19645778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (msg->findInt32("profile", &profile)) {
19655778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t level;
19665778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("level", &level)) {
19675778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return INVALID_OPERATION;
19685778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
19695778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19705778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = verifySupportForProfileAndLevel(profile, level);
19715778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19725778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
19735778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
19745778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
19755778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19765778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.eProfile = static_cast<OMX_VIDEO_AVCPROFILETYPE>(profile);
19775778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(level);
19785778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
19795778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19805778822d86b0337407514b9372562b86edfa91cdAndreas Huber    // XXX
19817c25df82dfc8bbedb58608242f0d923a4594bb14James Dong    if (h264type.eProfile != OMX_VIDEO_AVCProfileBaseline) {
19827c25df82dfc8bbedb58608242f0d923a4594bb14James Dong        ALOGW("Use baseline profile instead of %d for AVC recording",
19837c25df82dfc8bbedb58608242f0d923a4594bb14James Dong            h264type.eProfile);
19845778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.eProfile = OMX_VIDEO_AVCProfileBaseline;
19855778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
19865778822d86b0337407514b9372562b86edfa91cdAndreas Huber
19875778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (h264type.eProfile == OMX_VIDEO_AVCProfileBaseline) {
19885778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.nSliceHeaderSpacing = 0;
19895778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.bUseHadamard = OMX_TRUE;
19905778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.nRefFrames = 1;
19915778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.nBFrames = 0;
19925778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.nPFrames = setPFramesSpacing(iFrameInterval, frameRate);
19935778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (h264type.nPFrames == 0) {
19945778822d86b0337407514b9372562b86edfa91cdAndreas Huber            h264type.nAllowedPictureTypes = OMX_VIDEO_PictureTypeI;
19955778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
19965778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.nRefIdx10ActiveMinus1 = 0;
19975778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.nRefIdx11ActiveMinus1 = 0;
19985778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.bEntropyCodingCABAC = OMX_FALSE;
19995778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.bWeightedPPrediction = OMX_FALSE;
20005778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.bconstIpred = OMX_FALSE;
20015778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.bDirect8x8Inference = OMX_FALSE;
20025778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.bDirectSpatialTemporal = OMX_FALSE;
20035778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.nCabacInitIdc = 0;
20045778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
20055778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20065778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (h264type.nBFrames != 0) {
20075778822d86b0337407514b9372562b86edfa91cdAndreas Huber        h264type.nAllowedPictureTypes |= OMX_VIDEO_PictureTypeB;
20085778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
20095778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20105778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h264type.bEnableUEP = OMX_FALSE;
20115778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h264type.bEnableFMO = OMX_FALSE;
20125778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h264type.bEnableASO = OMX_FALSE;
20135778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h264type.bEnableRS = OMX_FALSE;
20145778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h264type.bFrameMBsOnly = OMX_TRUE;
20155778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h264type.bMBAFF = OMX_FALSE;
20165778822d86b0337407514b9372562b86edfa91cdAndreas Huber    h264type.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
20175778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20185778822d86b0337407514b9372562b86edfa91cdAndreas Huber    err = mOMX->setParameter(
20195778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoAvc, &h264type, sizeof(h264type));
20205778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20215778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
20225778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
20235778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
20245778822d86b0337407514b9372562b86edfa91cdAndreas Huber
202596076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    return configureBitrate(bitrate, bitrateMode);
20265778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
20275778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20285778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::verifySupportForProfileAndLevel(
20295778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t profile, int32_t level) {
20305778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_VIDEO_PARAM_PROFILELEVELTYPE params;
20315778822d86b0337407514b9372562b86edfa91cdAndreas Huber    InitOMXParams(&params);
20325778822d86b0337407514b9372562b86edfa91cdAndreas Huber    params.nPortIndex = kPortIndexOutput;
20335778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20345778822d86b0337407514b9372562b86edfa91cdAndreas Huber    for (params.nProfileIndex = 0;; ++params.nProfileIndex) {
20355778822d86b0337407514b9372562b86edfa91cdAndreas Huber        status_t err = mOMX->getParameter(
20365778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mNode,
20375778822d86b0337407514b9372562b86edfa91cdAndreas Huber                OMX_IndexParamVideoProfileLevelQuerySupported,
20385778822d86b0337407514b9372562b86edfa91cdAndreas Huber                &params,
20395778822d86b0337407514b9372562b86edfa91cdAndreas Huber                sizeof(params));
20405778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20415778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (err != OK) {
20425778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return err;
20435778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
20445778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20455778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t supportedProfile = static_cast<int32_t>(params.eProfile);
20465778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t supportedLevel = static_cast<int32_t>(params.eLevel);
20475778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20485778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (profile == supportedProfile && level <= supportedLevel) {
20495778822d86b0337407514b9372562b86edfa91cdAndreas Huber            return OK;
20505778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
20515778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
20525778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
20535778822d86b0337407514b9372562b86edfa91cdAndreas Huber
205496076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huberstatus_t ACodec::configureBitrate(
205596076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber        int32_t bitrate, OMX_VIDEO_CONTROLRATETYPE bitrateMode) {
20565778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_VIDEO_PARAM_BITRATETYPE bitrateType;
20575778822d86b0337407514b9372562b86edfa91cdAndreas Huber    InitOMXParams(&bitrateType);
20585778822d86b0337407514b9372562b86edfa91cdAndreas Huber    bitrateType.nPortIndex = kPortIndexOutput;
20595778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20605778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err = mOMX->getParameter(
20615778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoBitrate,
20625778822d86b0337407514b9372562b86edfa91cdAndreas Huber            &bitrateType, sizeof(bitrateType));
20635778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20645778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
20655778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return err;
20665778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
20675778822d86b0337407514b9372562b86edfa91cdAndreas Huber
206896076964863ee6887f0bed9d0f11f424b48ab9b9Andreas Huber    bitrateType.eControlRate = bitrateMode;
20695778822d86b0337407514b9372562b86edfa91cdAndreas Huber    bitrateType.nTargetBitrate = bitrate;
20705778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20715778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return mOMX->setParameter(
20725778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoBitrate,
20735778822d86b0337407514b9372562b86edfa91cdAndreas Huber            &bitrateType, sizeof(bitrateType));
20745778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
20755778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20765778822d86b0337407514b9372562b86edfa91cdAndreas Huberstatus_t ACodec::setupErrorCorrectionParameters() {
20775778822d86b0337407514b9372562b86edfa91cdAndreas Huber    OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE errorCorrectionType;
20785778822d86b0337407514b9372562b86edfa91cdAndreas Huber    InitOMXParams(&errorCorrectionType);
20795778822d86b0337407514b9372562b86edfa91cdAndreas Huber    errorCorrectionType.nPortIndex = kPortIndexOutput;
20805778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20815778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err = mOMX->getParameter(
20825778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoErrorCorrection,
20835778822d86b0337407514b9372562b86edfa91cdAndreas Huber            &errorCorrectionType, sizeof(errorCorrectionType));
20845778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20855778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
20865778822d86b0337407514b9372562b86edfa91cdAndreas Huber        return OK;  // Optional feature. Ignore this failure
20875778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
20885778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20895778822d86b0337407514b9372562b86edfa91cdAndreas Huber    errorCorrectionType.bEnableHEC = OMX_FALSE;
20905778822d86b0337407514b9372562b86edfa91cdAndreas Huber    errorCorrectionType.bEnableResync = OMX_TRUE;
20915778822d86b0337407514b9372562b86edfa91cdAndreas Huber    errorCorrectionType.nResynchMarkerSpacing = 256;
20925778822d86b0337407514b9372562b86edfa91cdAndreas Huber    errorCorrectionType.bEnableDataPartitioning = OMX_FALSE;
20935778822d86b0337407514b9372562b86edfa91cdAndreas Huber    errorCorrectionType.bEnableRVLC = OMX_FALSE;
20945778822d86b0337407514b9372562b86edfa91cdAndreas Huber
20955778822d86b0337407514b9372562b86edfa91cdAndreas Huber    return mOMX->setParameter(
20965778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mNode, OMX_IndexParamVideoErrorCorrection,
20975778822d86b0337407514b9372562b86edfa91cdAndreas Huber            &errorCorrectionType, sizeof(errorCorrectionType));
20985778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
20995778822d86b0337407514b9372562b86edfa91cdAndreas Huber
2100f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::setVideoFormatOnPort(
2101f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_U32 portIndex,
2102f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        int32_t width, int32_t height, OMX_VIDEO_CODINGTYPE compressionFormat) {
2103f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_PARAM_PORTDEFINITIONTYPE def;
2104f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    InitOMXParams(&def);
2105f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    def.nPortIndex = portIndex;
2106f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2107f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMX_VIDEO_PORTDEFINITIONTYPE *video_def = &def.format.video;
2108f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2109f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    status_t err = mOMX->getParameter(
2110f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2111f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2112f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(err, (status_t)OK);
2113f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2114f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (portIndex == kPortIndexInput) {
2115f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // XXX Need a (much) better heuristic to compute input buffer sizes.
2116f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        const size_t X = 64 * 1024;
2117f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (def.nBufferSize < X) {
2118f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            def.nBufferSize = X;
2119f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2120f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2121f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2122f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ((int)def.eDomain, (int)OMX_PortDomainVideo);
2123f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2124f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    video_def->nFrameWidth = width;
2125f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    video_def->nFrameHeight = height;
2126f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2127f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (portIndex == kPortIndexInput) {
2128f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        video_def->eCompressionFormat = compressionFormat;
2129f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        video_def->eColorFormat = OMX_COLOR_FormatUnused;
2130f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2131f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2132f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    err = mOMX->setParameter(
2133f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mNode, OMX_IndexParamPortDefinition, &def, sizeof(def));
2134f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2135f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return err;
2136f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2137f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2138f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::initNativeWindow() {
2139f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (mNativeWindow != NULL) {
2140f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_TRUE);
2141f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2142f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2143f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mOMX->enableGraphicBuffers(mNode, kPortIndexOutput, OMX_FALSE);
2144f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return OK;
2145f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2146f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2147d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Hubersize_t ACodec::countBuffersOwnedByComponent(OMX_U32 portIndex) const {
2148d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber    size_t n = 0;
2149d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber
2150d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber    for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) {
2151d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber        const BufferInfo &info = mBuffers[portIndex].itemAt(i);
2152d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber
2153d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber        if (info.mStatus == BufferInfo::OWNED_BY_COMPONENT) {
2154d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber            ++n;
2155d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber        }
2156d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber    }
2157d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber
2158d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber    return n;
2159d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber}
2160d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber
21617e7013392e302a28364df1dcee79b82ad90978b4Andreas Hubersize_t ACodec::countBuffersOwnedByNativeWindow() const {
21627e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    size_t n = 0;
21637e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
21647e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    for (size_t i = 0; i < mBuffers[kPortIndexOutput].size(); ++i) {
21657e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber        const BufferInfo &info = mBuffers[kPortIndexOutput].itemAt(i);
21667e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
21677e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber        if (info.mStatus == BufferInfo::OWNED_BY_NATIVE_WINDOW) {
21687e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber            ++n;
21697e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber        }
21707e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    }
21717e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
21727e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    return n;
21737e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber}
21747e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
21757e7013392e302a28364df1dcee79b82ad90978b4Andreas Hubervoid ACodec::waitUntilAllPossibleNativeWindowBuffersAreReturnedToUs() {
21767e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    if (mNativeWindow == NULL) {
21777e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber        return;
21787e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    }
21797e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
21807e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    int minUndequeuedBufs = 0;
21817e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    status_t err = mNativeWindow->query(
21827e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber            mNativeWindow.get(), NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
21837e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber            &minUndequeuedBufs);
21847e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
21857e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    if (err != OK) {
21867e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber        ALOGE("[%s] NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS query failed: %s (%d)",
21877e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber                mComponentName.c_str(), strerror(-err), -err);
21887e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
21897e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber        minUndequeuedBufs = 0;
21907e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    }
21917e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
21927e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    while (countBuffersOwnedByNativeWindow() > (size_t)minUndequeuedBufs
21937e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber            && dequeueBufferFromNativeWindow() != NULL) {
21947e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber    }
21957e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber}
21967e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
2197f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::allYourBuffersAreBelongToUs(
2198f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_U32 portIndex) {
2199f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) {
2200f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        BufferInfo *info = &mBuffers[portIndex].editItemAt(i);
2201f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2202f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (info->mStatus != BufferInfo::OWNED_BY_US
2203f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                && info->mStatus != BufferInfo::OWNED_BY_NATIVE_WINDOW) {
22043856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("[%s] Buffer %p on port %ld still has status %d",
2205f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    mComponentName.c_str(),
2206f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    info->mBufferID, portIndex, info->mStatus);
2207f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return false;
2208f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2209f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2210f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2211f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return true;
2212f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2213f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2214f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::allYourBuffersAreBelongToUs() {
2215f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return allYourBuffersAreBelongToUs(kPortIndexInput)
2216f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        && allYourBuffersAreBelongToUs(kPortIndexOutput);
2217f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2218f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2219f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::deferMessage(const sp<AMessage> &msg) {
2220f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool wasEmptyBefore = mDeferredQueue.empty();
2221f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mDeferredQueue.push_back(msg);
2222f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2223f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2224f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::processDeferredMessages() {
2225f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    List<sp<AMessage> > queue = mDeferredQueue;
2226f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mDeferredQueue.clear();
2227f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2228f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    List<sp<AMessage> >::iterator it = queue.begin();
2229f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    while (it != queue.end()) {
2230f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        onMessageReceived(*it++);
2231f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2232f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2233f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
223431e2508c75018145a8238925ff1a08cbde4e799aAndreas Hubervoid ACodec::sendFormatChange() {
223531e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    sp<AMessage> notify = mNotify->dup();
223631e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    notify->setInt32("what", kWhatOutputFormatChanged);
223731e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
223831e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    OMX_PARAM_PORTDEFINITIONTYPE def;
223931e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    InitOMXParams(&def);
224031e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    def.nPortIndex = kPortIndexOutput;
224131e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
224231e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    CHECK_EQ(mOMX->getParameter(
224331e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber                mNode, OMX_IndexParamPortDefinition, &def, sizeof(def)),
224431e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber             (status_t)OK);
224531e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
224631e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    CHECK_EQ((int)def.eDir, (int)OMX_DirOutput);
224731e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
224831e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    switch (def.eDomain) {
224931e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber        case OMX_PortDomainVideo:
225031e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber        {
225131e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            OMX_VIDEO_PORTDEFINITIONTYPE *videoDef = &def.format.video;
225231e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
2253e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            AString mime;
2254e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            if (!mIsEncoder) {
2255e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                notify->setString("mime", MEDIA_MIMETYPE_VIDEO_RAW);
2256e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            } else if (GetMimeTypeForVideoCoding(
2257e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        videoDef->eCompressionFormat, &mime) != OK) {
2258e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                notify->setString("mime", "application/octet-stream");
2259e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            } else {
2260e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                notify->setString("mime", mime.c_str());
2261e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            }
2262e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
226331e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            notify->setInt32("width", videoDef->nFrameWidth);
226431e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            notify->setInt32("height", videoDef->nFrameHeight);
226531e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
2266e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            if (!mIsEncoder) {
2267e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                notify->setInt32("stride", videoDef->nStride);
2268e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                notify->setInt32("slice-height", videoDef->nSliceHeight);
2269e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                notify->setInt32("color-format", videoDef->eColorFormat);
2270e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2271e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                OMX_CONFIG_RECTTYPE rect;
2272e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                InitOMXParams(&rect);
2273e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                rect.nPortIndex = kPortIndexOutput;
2274e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2275e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                if (mOMX->getConfig(
2276e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                            mNode, OMX_IndexConfigCommonOutputCrop,
2277e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                            &rect, sizeof(rect)) != OK) {
2278e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    rect.nLeft = 0;
2279e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    rect.nTop = 0;
2280e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    rect.nWidth = videoDef->nFrameWidth;
2281e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    rect.nHeight = videoDef->nFrameHeight;
2282e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                }
2283e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2284e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                CHECK_GE(rect.nLeft, 0);
2285e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                CHECK_GE(rect.nTop, 0);
2286e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                CHECK_GE(rect.nWidth, 0u);
2287e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                CHECK_GE(rect.nHeight, 0u);
2288e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                CHECK_LE(rect.nLeft + rect.nWidth - 1, videoDef->nFrameWidth);
2289e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                CHECK_LE(rect.nTop + rect.nHeight - 1, videoDef->nFrameHeight);
2290e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2291e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                notify->setRect(
2292e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        "crop",
2293e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        rect.nLeft,
2294e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        rect.nTop,
2295e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        rect.nLeft + rect.nWidth - 1,
2296e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        rect.nTop + rect.nHeight - 1);
2297e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2298e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                if (mNativeWindow != NULL) {
2299e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    android_native_rect_t crop;
2300e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    crop.left = rect.nLeft;
2301e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    crop.top = rect.nTop;
2302e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    crop.right = rect.nLeft + rect.nWidth;
2303e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    crop.bottom = rect.nTop + rect.nHeight;
2304e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2305e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    CHECK_EQ(0, native_window_set_crop(
2306e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                mNativeWindow.get(), &crop));
2307e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                }
230831e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            }
230931e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            break;
231031e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber        }
231131e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
231231e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber        case OMX_PortDomainAudio:
231331e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber        {
231431e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            OMX_AUDIO_PORTDEFINITIONTYPE *audioDef = &def.format.audio;
231531e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
2316e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            switch (audioDef->eEncoding) {
2317e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                case OMX_AUDIO_CodingPCM:
2318e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                {
2319e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    OMX_AUDIO_PARAM_PCMMODETYPE params;
2320e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    InitOMXParams(&params);
2321e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    params.nPortIndex = kPortIndexOutput;
2322e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2323e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    CHECK_EQ(mOMX->getParameter(
2324e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                mNode, OMX_IndexParamAudioPcm,
2325e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                &params, sizeof(params)),
2326e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                             (status_t)OK);
2327e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2328e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    CHECK(params.nChannels == 1 || params.bInterleaved);
2329e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    CHECK_EQ(params.nBitPerSample, 16u);
2330e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    CHECK_EQ((int)params.eNumData, (int)OMX_NumericalDataSigned);
2331e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    CHECK_EQ((int)params.ePCMMode, (int)OMX_AUDIO_PCMModeLinear);
2332e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2333e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setString("mime", MEDIA_MIMETYPE_AUDIO_RAW);
2334e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setInt32("channel-count", params.nChannels);
2335e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setInt32("sample-rate", params.nSamplingRate);
2336e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    if (mEncoderDelay + mEncoderPadding) {
2337e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        size_t frameSize = params.nChannels * sizeof(int16_t);
2338e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        if (mSkipCutBuffer != NULL) {
2339e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                            size_t prevbufsize = mSkipCutBuffer->size();
2340e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                            if (prevbufsize != 0) {
2341e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                ALOGW("Replacing SkipCutBuffer holding %d bytes", prevbufsize);
2342e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                            }
2343e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        }
2344e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        mSkipCutBuffer = new SkipCutBuffer(mEncoderDelay * frameSize,
2345e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                                           mEncoderPadding * frameSize);
2346e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    }
2347e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2348e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    if (mChannelMaskPresent) {
2349e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        notify->setInt32("channel-mask", mChannelMask);
23508b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen                    }
2351e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    break;
23528b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen                }
23539806555d3930be43e11106281dee354820ac1c88Andreas Huber
2354e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                case OMX_AUDIO_CodingAAC:
2355e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                {
2356e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    OMX_AUDIO_PARAM_AACPROFILETYPE params;
2357e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    InitOMXParams(&params);
2358e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    params.nPortIndex = kPortIndexOutput;
2359e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2360e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    CHECK_EQ(mOMX->getParameter(
2361e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                mNode, OMX_IndexParamAudioAac,
2362e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                &params, sizeof(params)),
2363e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                             (status_t)OK);
2364e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2365e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setString("mime", MEDIA_MIMETYPE_AUDIO_AAC);
2366e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setInt32("channel-count", params.nChannels);
2367e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setInt32("sample-rate", params.nSampleRate);
2368e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    break;
2369e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                }
2370e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2371e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                case OMX_AUDIO_CodingAMR:
2372e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                {
2373e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    OMX_AUDIO_PARAM_AMRTYPE params;
2374e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    InitOMXParams(&params);
2375e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    params.nPortIndex = kPortIndexOutput;
23769806555d3930be43e11106281dee354820ac1c88Andreas Huber
2377e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    CHECK_EQ(mOMX->getParameter(
2378e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                mNode, OMX_IndexParamAudioAmr,
2379e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                &params, sizeof(params)),
2380e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                             (status_t)OK);
2381e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2382e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setInt32("channel-count", 1);
2383e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    if (params.eAMRBandMode >= OMX_AUDIO_AMRBandModeWB0) {
2384e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        notify->setString(
2385e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                "mime", MEDIA_MIMETYPE_AUDIO_AMR_WB);
2386e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2387e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        notify->setInt32("sample-rate", 16000);
2388e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    } else {
2389e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        notify->setString(
2390e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                "mime", MEDIA_MIMETYPE_AUDIO_AMR_NB);
2391e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2392e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                        notify->setInt32("sample-rate", 8000);
2393e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    }
2394e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    break;
2395e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                }
2396e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2397e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                case OMX_AUDIO_CodingFLAC:
2398e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                {
2399e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    OMX_AUDIO_PARAM_FLACTYPE params;
2400e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    InitOMXParams(&params);
2401e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    params.nPortIndex = kPortIndexOutput;
2402e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2403e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    CHECK_EQ(mOMX->getParameter(
2404e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                mNode, OMX_IndexParamAudioFlac,
2405e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                                &params, sizeof(params)),
2406e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                             (status_t)OK);
2407e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2408e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setString("mime", MEDIA_MIMETYPE_AUDIO_FLAC);
2409e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setInt32("channel-count", params.nChannels);
2410e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    notify->setInt32("sample-rate", params.nSampleRate);
2411e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    break;
2412e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                }
2413e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber
2414e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                default:
2415e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber                    TRESPASS();
2416e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            }
241731e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            break;
241831e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber        }
241931e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
242031e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber        default:
242131e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            TRESPASS();
242231e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    }
242331e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
242431e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    notify->post();
242531e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
242631e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    mSentFormat = true;
242731e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber}
242831e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
24295778822d86b0337407514b9372562b86edfa91cdAndreas Hubervoid ACodec::signalError(OMX_ERRORTYPE error, status_t internalError) {
2430cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber    sp<AMessage> notify = mNotify->dup();
2431cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber    notify->setInt32("what", ACodec::kWhatError);
2432cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber    notify->setInt32("omx-error", error);
24335778822d86b0337407514b9372562b86edfa91cdAndreas Huber    notify->setInt32("err", internalError);
2434cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber    notify->post();
2435cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber}
2436cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber
2437bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huberstatus_t ACodec::pushBlankBuffersToNativeWindow() {
2438bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    status_t err = NO_ERROR;
2439bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    ANativeWindowBuffer* anb = NULL;
2440bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    int numBufs = 0;
2441bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    int minUndequeuedBufs = 0;
2442bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2443bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    // We need to reconnect to the ANativeWindow as a CPU client to ensure that
2444bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    // no frames get dropped by SurfaceFlinger assuming that these are video
2445bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    // frames.
2446bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    err = native_window_api_disconnect(mNativeWindow.get(),
2447bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            NATIVE_WINDOW_API_MEDIA);
2448bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    if (err != NO_ERROR) {
2449bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        ALOGE("error pushing blank frames: api_disconnect failed: %s (%d)",
2450bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                strerror(-err), -err);
2451bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        return err;
2452bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
2453bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2454bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    err = native_window_api_connect(mNativeWindow.get(),
2455bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            NATIVE_WINDOW_API_CPU);
2456bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    if (err != NO_ERROR) {
2457bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        ALOGE("error pushing blank frames: api_connect failed: %s (%d)",
2458bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                strerror(-err), -err);
2459bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        return err;
2460bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
2461bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2462bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    err = native_window_set_buffers_geometry(mNativeWindow.get(), 1, 1,
2463bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            HAL_PIXEL_FORMAT_RGBX_8888);
2464bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    if (err != NO_ERROR) {
2465bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        ALOGE("error pushing blank frames: set_buffers_geometry failed: %s (%d)",
2466bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                strerror(-err), -err);
2467bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        goto error;
2468bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
2469bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2470bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    err = native_window_set_usage(mNativeWindow.get(),
2471bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            GRALLOC_USAGE_SW_WRITE_OFTEN);
2472bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    if (err != NO_ERROR) {
2473bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        ALOGE("error pushing blank frames: set_usage failed: %s (%d)",
2474bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                strerror(-err), -err);
2475bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        goto error;
2476bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
2477bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2478bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    err = mNativeWindow->query(mNativeWindow.get(),
2479bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufs);
2480bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    if (err != NO_ERROR) {
2481bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        ALOGE("error pushing blank frames: MIN_UNDEQUEUED_BUFFERS query "
2482bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                "failed: %s (%d)", strerror(-err), -err);
2483bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        goto error;
2484bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
2485bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2486bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    numBufs = minUndequeuedBufs + 1;
2487bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    err = native_window_set_buffer_count(mNativeWindow.get(), numBufs);
2488bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    if (err != NO_ERROR) {
2489bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        ALOGE("error pushing blank frames: set_buffer_count failed: %s (%d)",
2490bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                strerror(-err), -err);
2491bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        goto error;
2492bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
2493bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2494bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    // We  push numBufs + 1 buffers to ensure that we've drawn into the same
2495bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    // buffer twice.  This should guarantee that the buffer has been displayed
2496bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    // on the screen and then been replaced, so an previous video frames are
2497bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    // guaranteed NOT to be currently displayed.
2498bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    for (int i = 0; i < numBufs + 1; i++) {
24991e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis        int fenceFd = -1;
25001e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis        err = native_window_dequeue_buffer_and_wait(mNativeWindow.get(), &anb);
2501bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (err != NO_ERROR) {
2502bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            ALOGE("error pushing blank frames: dequeueBuffer failed: %s (%d)",
2503bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                    strerror(-err), -err);
2504bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            goto error;
2505bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
2506bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2507bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
2508bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2509bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        // Fill the buffer with the a 1x1 checkerboard pattern ;)
2510bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        uint32_t* img = NULL;
2511bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
2512bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (err != NO_ERROR) {
2513bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            ALOGE("error pushing blank frames: lock failed: %s (%d)",
2514bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                    strerror(-err), -err);
2515bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            goto error;
2516bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
2517bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2518bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        *img = 0;
2519bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2520bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        err = buf->unlock();
2521bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (err != NO_ERROR) {
2522bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            ALOGE("error pushing blank frames: unlock failed: %s (%d)",
2523bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                    strerror(-err), -err);
2524bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            goto error;
2525bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
2526bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2527bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        err = mNativeWindow->queueBuffer(mNativeWindow.get(),
25281e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis                buf->getNativeBuffer(), -1);
2529bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (err != NO_ERROR) {
2530bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            ALOGE("error pushing blank frames: queueBuffer failed: %s (%d)",
2531bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                    strerror(-err), -err);
2532bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            goto error;
2533bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
2534bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2535bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        anb = NULL;
2536bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
2537bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2538bc098410be55f9d96f394b3981a0c482b83859b6Andreas Hubererror:
2539bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2540bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    if (err != NO_ERROR) {
2541bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        // Clean up after an error.
2542bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (anb != NULL) {
25431e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis            mNativeWindow->cancelBuffer(mNativeWindow.get(), anb, -1);
2544bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
2545bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2546bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        native_window_api_disconnect(mNativeWindow.get(),
2547bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                NATIVE_WINDOW_API_CPU);
2548bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        native_window_api_connect(mNativeWindow.get(),
2549bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                NATIVE_WINDOW_API_MEDIA);
2550bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2551bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        return err;
2552bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    } else {
2553bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        // Clean up after success.
2554bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        err = native_window_api_disconnect(mNativeWindow.get(),
2555bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                NATIVE_WINDOW_API_CPU);
2556bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (err != NO_ERROR) {
2557bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            ALOGE("error pushing blank frames: api_disconnect failed: %s (%d)",
2558bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                    strerror(-err), -err);
2559bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            return err;
2560bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
2561bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2562bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        err = native_window_api_connect(mNativeWindow.get(),
2563bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                NATIVE_WINDOW_API_MEDIA);
2564bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (err != NO_ERROR) {
2565bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            ALOGE("error pushing blank frames: api_connect failed: %s (%d)",
2566bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber                    strerror(-err), -err);
2567bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            return err;
2568bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
2569bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2570bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        return NO_ERROR;
2571bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber    }
2572bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber}
2573bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
2574f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
2575f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2576eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas HuberACodec::PortDescription::PortDescription() {
2577eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber}
2578eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber
2579496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huberstatus_t ACodec::requestIDRFrame() {
2580496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber    if (!mIsEncoder) {
2581496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber        return ERROR_UNSUPPORTED;
2582496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber    }
2583496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber
2584496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber    OMX_CONFIG_INTRAREFRESHVOPTYPE params;
2585496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber    InitOMXParams(&params);
2586496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber
2587496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber    params.nPortIndex = kPortIndexOutput;
2588496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber    params.IntraRefreshVOP = OMX_TRUE;
2589496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber
2590496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber    return mOMX->setConfig(
2591496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber            mNode,
2592496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber            OMX_IndexConfigVideoIntraVOPRefresh,
2593496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber            &params,
2594496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber            sizeof(params));
2595496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber}
2596496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber
2597eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Hubervoid ACodec::PortDescription::addBuffer(
2598eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber        IOMX::buffer_id id, const sp<ABuffer> &buffer) {
2599eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber    mBufferIDs.push_back(id);
2600eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber    mBuffers.push_back(buffer);
2601eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber}
2602eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber
2603eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Hubersize_t ACodec::PortDescription::countBuffers() {
2604eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber    return mBufferIDs.size();
2605eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber}
2606eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber
2607eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas HuberIOMX::buffer_id ACodec::PortDescription::bufferIDAt(size_t index) const {
2608eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber    return mBufferIDs.itemAt(index);
2609eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber}
2610eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber
2611eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Hubersp<ABuffer> ACodec::PortDescription::bufferAt(size_t index) const {
2612eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber    return mBuffers.itemAt(index);
2613eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber}
2614eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber
2615eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber////////////////////////////////////////////////////////////////////////////////
2616eb61431af13741aa8b7e57a39f69bba5a6c190dcAndreas Huber
2617f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::BaseState::BaseState(ACodec *codec, const sp<AState> &parentState)
2618f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : AState(parentState),
2619f933441648ef6a71dee783d733aac17b9508b452Andreas Huber      mCodec(codec) {
2620f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2621f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2622f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::BaseState::PortMode ACodec::BaseState::getPortMode(OMX_U32 portIndex) {
2623f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return KEEP_BUFFERS;
2624f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2625f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2626f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::BaseState::onMessageReceived(const sp<AMessage> &msg) {
2627f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (msg->what()) {
2628f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatInputBufferFilled:
2629f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
2630f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            onInputBufferFilled(msg);
2631f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
2632f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2633f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2634f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatOutputBufferDrained:
2635f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
2636f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            onOutputBufferDrained(msg);
2637f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
2638f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2639f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2640f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case ACodec::kWhatOMXMessage:
2641f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
2642f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return onOMXMessage(msg);
2643f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2644f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
26457cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        case ACodec::kWhatCreateInputSurface:
26467cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        case ACodec::kWhatSignalEndOfInputStream:
26477cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        {
26487cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden            ALOGE("Message 0x%x was not handled", msg->what());
26497cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden            mCodec->signalError(OMX_ErrorUndefined, INVALID_OPERATION);
26507cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden            return true;
26517cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        }
26527cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden
2653ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        case ACodec::kWhatOMXDied:
2654ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        {
2655ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber            ALOGE("OMX/mediaserver died, signalling error!");
2656ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber            mCodec->signalError(OMX_ErrorResourcesLost, DEAD_OBJECT);
2657ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber            break;
2658ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        }
2659ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
2660f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
2661f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return false;
2662f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2663f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2664f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return true;
2665f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2666f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2667f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::BaseState::onOMXMessage(const sp<AMessage> &msg) {
2668f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    int32_t type;
2669f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK(msg->findInt32("type", &type));
2670f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2671f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    IOMX::node_id nodeID;
2672f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK(msg->findPointer("node", &nodeID));
2673f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(nodeID, mCodec->mNode);
2674f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2675f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (type) {
2676f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case omx_message::EVENT:
2677f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
2678f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            int32_t event, data1, data2;
2679f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findInt32("event", &event));
2680f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findInt32("data1", &data1));
2681f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findInt32("data2", &data2));
2682f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
26830af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber            if (event == OMX_EventCmdComplete
26840af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber                    && data1 == OMX_CommandFlush
26850af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber                    && data2 == (int32_t)OMX_ALL) {
26860af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber                // Use of this notification is not consistent across
26870af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber                // implementations. We'll drop this notification and rely
26880af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber                // on flush-complete notifications on the individual port
26890af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber                // indices instead.
26900af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber
26910af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber                return true;
26920af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber            }
26930af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber
2694f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return onOMXEvent(
2695f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    static_cast<OMX_EVENTTYPE>(event),
2696f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    static_cast<OMX_U32>(data1),
2697f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    static_cast<OMX_U32>(data2));
2698f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2699f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2700f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case omx_message::EMPTY_BUFFER_DONE:
2701f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
2702f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            IOMX::buffer_id bufferID;
2703f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findPointer("buffer", &bufferID));
2704f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2705f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return onOMXEmptyBufferDone(bufferID);
2706f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2707f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2708f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case omx_message::FILL_BUFFER_DONE:
2709f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
2710f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            IOMX::buffer_id bufferID;
2711f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findPointer("buffer", &bufferID));
2712f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2713f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            int32_t rangeOffset, rangeLength, flags;
2714f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            int64_t timeUs;
2715f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            void *platformPrivate;
2716f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            void *dataPtr;
2717f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2718f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findInt32("range_offset", &rangeOffset));
2719f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findInt32("range_length", &rangeLength));
2720f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findInt32("flags", &flags));
2721f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findInt64("timestamp", &timeUs));
2722f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findPointer("platform_private", &platformPrivate));
2723f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(msg->findPointer("data_ptr", &dataPtr));
2724f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2725f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return onOMXFillBufferDone(
2726f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    bufferID,
2727f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    (size_t)rangeOffset, (size_t)rangeLength,
2728f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    (OMX_U32)flags,
2729f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    timeUs,
2730f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    platformPrivate,
2731f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    dataPtr);
2732f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2733f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2734f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
2735f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            TRESPASS();
2736f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
2737f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2738f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2739f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2740f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::BaseState::onOMXEvent(
2741f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
2742f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (event != OMX_EventError) {
27433856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("[%s] EVENT(%d, 0x%08lx, 0x%08lx)",
2744f933441648ef6a71dee783d733aac17b9508b452Andreas Huber             mCodec->mComponentName.c_str(), event, data1, data2);
2745f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2746f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return false;
2747f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2748f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
274929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block    ALOGE("[%s] ERROR(0x%08lx)", mCodec->mComponentName.c_str(), data1);
2750f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2751cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber    mCodec->signalError((OMX_ERRORTYPE)data1);
2752f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2753f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return true;
2754f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2755f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2756f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::BaseState::onOMXEmptyBufferDone(IOMX::buffer_id bufferID) {
27573856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] onOMXEmptyBufferDone %p",
2758349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber         mCodec->mComponentName.c_str(), bufferID);
2759349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
2760f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BufferInfo *info =
2761f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mCodec->findBufferByID(kPortIndexInput, bufferID);
2762f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2763f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ((int)info->mStatus, (int)BufferInfo::OWNED_BY_COMPONENT);
2764f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    info->mStatus = BufferInfo::OWNED_BY_US;
2765f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2766d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    const sp<AMessage> &bufferMeta = info->mData->meta();
2767d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    void *mediaBuffer;
2768d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (bufferMeta->findPointer("mediaBuffer", &mediaBuffer)
2769d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber            && mediaBuffer != NULL) {
2770d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        // We're in "store-metadata-in-buffers" mode, the underlying
2771d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        // OMX component had access to data that's implicitly refcounted
2772d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        // by this "mediaBuffer" object. Now that the OMX component has
2773d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        // told us that it's done with the input buffer, we can decrement
2774d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        // the mediaBuffer's reference count.
277596fc6cc65ca93009a759a3a874b82a35771b9714Andreas Huber
277696fc6cc65ca93009a759a3a874b82a35771b9714Andreas Huber        ALOGV("releasing mbuf %p", mediaBuffer);
277796fc6cc65ca93009a759a3a874b82a35771b9714Andreas Huber
2778d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        ((MediaBuffer *)mediaBuffer)->release();
2779d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mediaBuffer = NULL;
2780d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
2781d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        bufferMeta->setPointer("mediaBuffer", NULL);
2782d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
2783d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
2784f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    PortMode mode = getPortMode(kPortIndexInput);
2785f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2786f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (mode) {
2787f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case KEEP_BUFFERS:
2788f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
2789f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2790f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case RESUBMIT_BUFFERS:
2791f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            postFillThisBuffer(info);
2792f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
2793f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2794f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
2795f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
2796f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ((int)mode, (int)FREE_BUFFERS);
2797f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            TRESPASS();  // Not currently used
2798f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
2799f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2800f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2801f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2802f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return true;
2803f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2804f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2805f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::BaseState::postFillThisBuffer(BufferInfo *info) {
2806f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (mCodec->mPortEOS[kPortIndexInput]) {
2807f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return;
2808f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2809f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2810f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ((int)info->mStatus, (int)BufferInfo::OWNED_BY_US);
2811f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2812f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    sp<AMessage> notify = mCodec->mNotify->dup();
2813f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    notify->setInt32("what", ACodec::kWhatFillThisBuffer);
2814f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    notify->setPointer("buffer-id", info->mBufferID);
2815f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2816f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    info->mData->meta()->clear();
28172d8bedd05437b6fccdbc6bf70f673ffd86744d59Andreas Huber    notify->setBuffer("buffer", info->mData);
2818f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2819f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    sp<AMessage> reply = new AMessage(kWhatInputBufferFilled, mCodec->id());
2820f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    reply->setPointer("buffer-id", info->mBufferID);
2821f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2822f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    notify->setMessage("reply", reply);
2823f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2824f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    notify->post();
2825f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2826f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    info->mStatus = BufferInfo::OWNED_BY_UPSTREAM;
2827f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2828f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2829f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::BaseState::onInputBufferFilled(const sp<AMessage> &msg) {
2830f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    IOMX::buffer_id bufferID;
2831f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK(msg->findPointer("buffer-id", &bufferID));
2832f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
28332d8bedd05437b6fccdbc6bf70f673ffd86744d59Andreas Huber    sp<ABuffer> buffer;
2834f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    int32_t err = OK;
28355778822d86b0337407514b9372562b86edfa91cdAndreas Huber    bool eos = false;
28365778822d86b0337407514b9372562b86edfa91cdAndreas Huber
28372d8bedd05437b6fccdbc6bf70f673ffd86744d59Andreas Huber    if (!msg->findBuffer("buffer", &buffer)) {
2838f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK(msg->findInt32("err", &err));
2839f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
28403856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("[%s] saw error %d instead of an input buffer",
28413831a066bcf1019864a94d2bc7b4c9241efc5c22Andreas Huber             mCodec->mComponentName.c_str(), err);
28423831a066bcf1019864a94d2bc7b4c9241efc5c22Andreas Huber
28432d8bedd05437b6fccdbc6bf70f673ffd86744d59Andreas Huber        buffer.clear();
28445778822d86b0337407514b9372562b86edfa91cdAndreas Huber
28455778822d86b0337407514b9372562b86edfa91cdAndreas Huber        eos = true;
2846f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2847f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
28485778822d86b0337407514b9372562b86edfa91cdAndreas Huber    int32_t tmp;
28495778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (buffer != NULL && buffer->meta()->findInt32("eos", &tmp) && tmp) {
28505778822d86b0337407514b9372562b86edfa91cdAndreas Huber        eos = true;
28515778822d86b0337407514b9372562b86edfa91cdAndreas Huber        err = ERROR_END_OF_STREAM;
28525778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
28535778822d86b0337407514b9372562b86edfa91cdAndreas Huber
2854f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BufferInfo *info = mCodec->findBufferByID(kPortIndexInput, bufferID);
2855f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ((int)info->mStatus, (int)BufferInfo::OWNED_BY_UPSTREAM);
2856f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2857f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    info->mStatus = BufferInfo::OWNED_BY_US;
2858f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2859f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    PortMode mode = getPortMode(kPortIndexInput);
2860f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2861f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (mode) {
2862f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case KEEP_BUFFERS:
2863f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
28645778822d86b0337407514b9372562b86edfa91cdAndreas Huber            if (eos) {
2865dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                if (!mCodec->mPortEOS[kPortIndexInput]) {
2866dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                    mCodec->mPortEOS[kPortIndexInput] = true;
2867dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                    mCodec->mInputEOSResult = err;
2868dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                }
2869f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
2870f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
2871f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2872f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2873f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case RESUBMIT_BUFFERS:
2874f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
28755778822d86b0337407514b9372562b86edfa91cdAndreas Huber            if (buffer != NULL && !mCodec->mPortEOS[kPortIndexInput]) {
2876f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                int64_t timeUs;
2877f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2878f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2879f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                OMX_U32 flags = OMX_BUFFERFLAG_ENDOFFRAME;
2880f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2881f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                int32_t isCSD;
2882f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                if (buffer->meta()->findInt32("csd", &isCSD) && isCSD != 0) {
2883f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    flags |= OMX_BUFFERFLAG_CODECCONFIG;
2884f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                }
2885f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
28865778822d86b0337407514b9372562b86edfa91cdAndreas Huber                if (eos) {
28875778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    flags |= OMX_BUFFERFLAG_EOS;
28885778822d86b0337407514b9372562b86edfa91cdAndreas Huber                }
28895778822d86b0337407514b9372562b86edfa91cdAndreas Huber
2890f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                if (buffer != info->mData) {
2891d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    ALOGV("[%s] Needs to copy input data for buffer %p. (%p != %p)",
2892d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                         mCodec->mComponentName.c_str(),
2893d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                         bufferID,
2894d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                         buffer.get(), info->mData.get());
2895f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2896f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    CHECK_LE(buffer->size(), info->mData->capacity());
2897f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    memcpy(info->mData->data(), buffer->data(), buffer->size());
2898f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                }
2899f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2900078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber                if (flags & OMX_BUFFERFLAG_CODECCONFIG) {
29013856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("[%s] calling emptyBuffer %p w/ codec specific data",
2902078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber                         mCodec->mComponentName.c_str(), bufferID);
29035778822d86b0337407514b9372562b86edfa91cdAndreas Huber                } else if (flags & OMX_BUFFERFLAG_EOS) {
29045778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    ALOGV("[%s] calling emptyBuffer %p w/ EOS",
29055778822d86b0337407514b9372562b86edfa91cdAndreas Huber                         mCodec->mComponentName.c_str(), bufferID);
2906078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber                } else {
2907d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#if TRACK_BUFFER_TIMING
2908d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                    ALOGI("[%s] calling emptyBuffer %p w/ time %lld us",
2909d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                         mCodec->mComponentName.c_str(), bufferID, timeUs);
2910d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#else
29113856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("[%s] calling emptyBuffer %p w/ time %lld us",
2912078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber                         mCodec->mComponentName.c_str(), bufferID, timeUs);
2913d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#endif
2914078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber                }
2915349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
2916d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#if TRACK_BUFFER_TIMING
2917d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                ACodec::BufferStats stats;
2918d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                stats.mEmptyBufferTimeUs = ALooper::GetNowUs();
2919d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                stats.mFillBufferDoneTimeUs = -1ll;
2920d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                mCodec->mBufferStats.add(timeUs, stats);
2921d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#endif
2922d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
2923f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK_EQ(mCodec->mOMX->emptyBuffer(
2924f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            mCodec->mNode,
2925f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            bufferID,
2926f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            0,
2927f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            buffer->size(),
2928f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            flags,
2929f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            timeUs),
2930f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                         (status_t)OK);
2931f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2932f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                info->mStatus = BufferInfo::OWNED_BY_COMPONENT;
2933f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
29345778822d86b0337407514b9372562b86edfa91cdAndreas Huber                if (!eos) {
29355778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    getMoreInputDataIfPossible();
29365778822d86b0337407514b9372562b86edfa91cdAndreas Huber                } else {
29375778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    ALOGV("[%s] Signalled EOS on the input port",
29385778822d86b0337407514b9372562b86edfa91cdAndreas Huber                         mCodec->mComponentName.c_str());
29395778822d86b0337407514b9372562b86edfa91cdAndreas Huber
29405778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    mCodec->mPortEOS[kPortIndexInput] = true;
29415778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    mCodec->mInputEOSResult = err;
29425778822d86b0337407514b9372562b86edfa91cdAndreas Huber                }
2943f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            } else if (!mCodec->mPortEOS[kPortIndexInput]) {
2944dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                if (err != ERROR_END_OF_STREAM) {
29453856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("[%s] Signalling EOS on the input port "
2946dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                         "due to error %d",
2947dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                         mCodec->mComponentName.c_str(), err);
2948dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                } else {
29493856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("[%s] Signalling EOS on the input port",
2950dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                         mCodec->mComponentName.c_str());
2951dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                }
2952f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
29533856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("[%s] calling emptyBuffer %p signalling EOS",
2954349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber                     mCodec->mComponentName.c_str(), bufferID);
2955349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
2956f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK_EQ(mCodec->mOMX->emptyBuffer(
2957f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            mCodec->mNode,
2958f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            bufferID,
2959f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            0,
2960f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            0,
2961f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            OMX_BUFFERFLAG_EOS,
2962f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            0),
2963f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                         (status_t)OK);
2964f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2965f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                info->mStatus = BufferInfo::OWNED_BY_COMPONENT;
2966f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2967f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                mCodec->mPortEOS[kPortIndexInput] = true;
2968dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                mCodec->mInputEOSResult = err;
2969f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
2970f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
2971f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2972f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            default:
2973f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK_EQ((int)mode, (int)FREE_BUFFERS);
2974f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                break;
2975f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2976f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2977f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
2978f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2979f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::BaseState::getMoreInputDataIfPossible() {
2980f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (mCodec->mPortEOS[kPortIndexInput]) {
2981f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return;
2982f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
2983f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2984f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BufferInfo *eligible = NULL;
2985f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2986f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = 0; i < mCodec->mBuffers[kPortIndexInput].size(); ++i) {
2987f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        BufferInfo *info = &mCodec->mBuffers[kPortIndexInput].editItemAt(i);
2988f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2989f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#if 0
2990f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (info->mStatus == BufferInfo::OWNED_BY_UPSTREAM) {
2991f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            // There's already a "read" pending.
2992f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return;
2993f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2994f933441648ef6a71dee783d733aac17b9508b452Andreas Huber#endif
2995f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
2996f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (info->mStatus == BufferInfo::OWNED_BY_US) {
2997f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            eligible = info;
2998f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
2999f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3000f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3001f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (eligible == NULL) {
3002f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return;
3003f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3004f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3005f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    postFillThisBuffer(eligible);
3006f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3007f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3008f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::BaseState::onOMXFillBufferDone(
3009f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        IOMX::buffer_id bufferID,
3010f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        size_t rangeOffset, size_t rangeLength,
3011f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_U32 flags,
3012f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        int64_t timeUs,
3013f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        void *platformPrivate,
3014f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        void *dataPtr) {
30155778822d86b0337407514b9372562b86edfa91cdAndreas Huber    ALOGV("[%s] onOMXFillBufferDone %p time %lld us, flags = 0x%08lx",
30165778822d86b0337407514b9372562b86edfa91cdAndreas Huber         mCodec->mComponentName.c_str(), bufferID, timeUs, flags);
3017349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
3018f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    ssize_t index;
3019d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
3020d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#if TRACK_BUFFER_TIMING
3021d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    index = mCodec->mBufferStats.indexOfKey(timeUs);
3022d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    if (index >= 0) {
3023d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        ACodec::BufferStats *stats = &mCodec->mBufferStats.editValueAt(index);
3024d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        stats->mFillBufferDoneTimeUs = ALooper::GetNowUs();
3025d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
3026d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        ALOGI("frame PTS %lld: %lld",
3027d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                timeUs,
3028d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber                stats->mFillBufferDoneTimeUs - stats->mEmptyBufferTimeUs);
3029d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
3030d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        mCodec->mBufferStats.removeItemsAt(index);
3031d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber        stats = NULL;
3032d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    }
3033d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber#endif
3034d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber
3035f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BufferInfo *info =
3036f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mCodec->findBufferByID(kPortIndexOutput, bufferID, &index);
3037f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3038f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ((int)info->mStatus, (int)BufferInfo::OWNED_BY_COMPONENT);
3039f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3040f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    info->mStatus = BufferInfo::OWNED_BY_US;
3041f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3042f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    PortMode mode = getPortMode(kPortIndexOutput);
3043f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3044f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (mode) {
3045f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case KEEP_BUFFERS:
3046f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3047f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3048f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case RESUBMIT_BUFFERS:
3049f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
30505778822d86b0337407514b9372562b86edfa91cdAndreas Huber            if (rangeLength == 0 && !(flags & OMX_BUFFERFLAG_EOS)) {
30515778822d86b0337407514b9372562b86edfa91cdAndreas Huber                ALOGV("[%s] calling fillBuffer %p",
30525778822d86b0337407514b9372562b86edfa91cdAndreas Huber                     mCodec->mComponentName.c_str(), info->mBufferID);
3053349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
30545778822d86b0337407514b9372562b86edfa91cdAndreas Huber                CHECK_EQ(mCodec->mOMX->fillBuffer(
30555778822d86b0337407514b9372562b86edfa91cdAndreas Huber                            mCodec->mNode, info->mBufferID),
30565778822d86b0337407514b9372562b86edfa91cdAndreas Huber                         (status_t)OK);
3057f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
30585778822d86b0337407514b9372562b86edfa91cdAndreas Huber                info->mStatus = BufferInfo::OWNED_BY_COMPONENT;
30595778822d86b0337407514b9372562b86edfa91cdAndreas Huber                break;
30605778822d86b0337407514b9372562b86edfa91cdAndreas Huber            }
306131e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
3062e96ee699aca0f711d41e6c0833e5de2341c4a36dAndreas Huber            if (!mCodec->mSentFormat) {
30635778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mCodec->sendFormatChange();
30645778822d86b0337407514b9372562b86edfa91cdAndreas Huber            }
3065f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
306621ad778dcfcddb8f8fd9dc3fe4992fbef246c511Marco Nelissen            info->mData->setRange(rangeOffset, rangeLength);
3067496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber#if 0
306821ad778dcfcddb8f8fd9dc3fe4992fbef246c511Marco Nelissen            if (mCodec->mNativeWindow == NULL) {
3069496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber                if (IsIDR(info->mData)) {
3070496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber                    ALOGI("IDR frame");
3071496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber                }
30725778822d86b0337407514b9372562b86edfa91cdAndreas Huber            }
307321ad778dcfcddb8f8fd9dc3fe4992fbef246c511Marco Nelissen#endif
3074f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
30758b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen            if (mCodec->mSkipCutBuffer != NULL) {
30768b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen                mCodec->mSkipCutBuffer->submit(info->mData);
30778b71241ce7353731ab75322c46e090ee35014a33Marco Nelissen            }
30785778822d86b0337407514b9372562b86edfa91cdAndreas Huber            info->mData->meta()->setInt64("timeUs", timeUs);
3079f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
30805778822d86b0337407514b9372562b86edfa91cdAndreas Huber            sp<AMessage> notify = mCodec->mNotify->dup();
30815778822d86b0337407514b9372562b86edfa91cdAndreas Huber            notify->setInt32("what", ACodec::kWhatDrainThisBuffer);
30825778822d86b0337407514b9372562b86edfa91cdAndreas Huber            notify->setPointer("buffer-id", info->mBufferID);
30832d8bedd05437b6fccdbc6bf70f673ffd86744d59Andreas Huber            notify->setBuffer("buffer", info->mData);
30845778822d86b0337407514b9372562b86edfa91cdAndreas Huber            notify->setInt32("flags", flags);
3085f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
30865778822d86b0337407514b9372562b86edfa91cdAndreas Huber            sp<AMessage> reply =
30875778822d86b0337407514b9372562b86edfa91cdAndreas Huber                new AMessage(kWhatOutputBufferDrained, mCodec->id());
3088f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
30895778822d86b0337407514b9372562b86edfa91cdAndreas Huber            reply->setPointer("buffer-id", info->mBufferID);
3090f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
30915778822d86b0337407514b9372562b86edfa91cdAndreas Huber            notify->setMessage("reply", reply);
3092f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
30935778822d86b0337407514b9372562b86edfa91cdAndreas Huber            notify->post();
30945778822d86b0337407514b9372562b86edfa91cdAndreas Huber
30955778822d86b0337407514b9372562b86edfa91cdAndreas Huber            info->mStatus = BufferInfo::OWNED_BY_DOWNSTREAM;
3096f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3097f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            if (flags & OMX_BUFFERFLAG_EOS) {
30985778822d86b0337407514b9372562b86edfa91cdAndreas Huber                ALOGV("[%s] saw output EOS", mCodec->mComponentName.c_str());
30995778822d86b0337407514b9372562b86edfa91cdAndreas Huber
3100f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                sp<AMessage> notify = mCodec->mNotify->dup();
3101f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                notify->setInt32("what", ACodec::kWhatEOS);
3102dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber                notify->setInt32("err", mCodec->mInputEOSResult);
3103f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                notify->post();
3104f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3105f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                mCodec->mPortEOS[kPortIndexOutput] = true;
3106f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
3107f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3108f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3109f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3110f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3111f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3112f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ((int)mode, (int)FREE_BUFFERS);
3113f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3114f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ((status_t)OK,
3115f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                     mCodec->freeBuffer(kPortIndexOutput, index));
3116f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3117f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3118f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3119f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3120f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return true;
3121f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3122f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3123f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::BaseState::onOutputBufferDrained(const sp<AMessage> &msg) {
3124f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    IOMX::buffer_id bufferID;
3125f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK(msg->findPointer("buffer-id", &bufferID));
3126f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3127f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    ssize_t index;
3128f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BufferInfo *info =
3129f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mCodec->findBufferByID(kPortIndexOutput, bufferID, &index);
3130f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ((int)info->mStatus, (int)BufferInfo::OWNED_BY_DOWNSTREAM);
3131f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3132f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    int32_t render;
3133f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (mCodec->mNativeWindow != NULL
3134f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            && msg->findInt32("render", &render) && render != 0) {
3135f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        // The client wants this buffer to be rendered.
3136f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
31375778822d86b0337407514b9372562b86edfa91cdAndreas Huber        status_t err;
31385778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if ((err = mCodec->mNativeWindow->queueBuffer(
3139f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    mCodec->mNativeWindow.get(),
31401e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis                    info->mGraphicBuffer.get(), -1)) == OK) {
3141cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber            info->mStatus = BufferInfo::OWNED_BY_NATIVE_WINDOW;
3142cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber        } else {
31435778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mCodec->signalError(OMX_ErrorUndefined, err);
3144cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber            info->mStatus = BufferInfo::OWNED_BY_US;
3145cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber        }
3146f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    } else {
3147f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        info->mStatus = BufferInfo::OWNED_BY_US;
3148f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3149f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3150f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    PortMode mode = getPortMode(kPortIndexOutput);
3151f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3152f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (mode) {
3153f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case KEEP_BUFFERS:
3154f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3155f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            // XXX fishy, revisit!!! What about the FREE_BUFFERS case below?
3156f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3157f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            if (info->mStatus == BufferInfo::OWNED_BY_NATIVE_WINDOW) {
3158f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                // We cannot resubmit the buffer we just rendered, dequeue
3159f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                // the spare instead.
3160f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3161f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                info = mCodec->dequeueBufferFromNativeWindow();
3162f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
3163f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3164f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3165f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3166f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case RESUBMIT_BUFFERS:
3167f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3168f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            if (!mCodec->mPortEOS[kPortIndexOutput]) {
3169f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                if (info->mStatus == BufferInfo::OWNED_BY_NATIVE_WINDOW) {
3170f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    // We cannot resubmit the buffer we just rendered, dequeue
3171f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    // the spare instead.
3172f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3173f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    info = mCodec->dequeueBufferFromNativeWindow();
3174f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                }
3175f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3176c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber                if (info != NULL) {
31773856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("[%s] calling fillBuffer %p",
3178c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber                         mCodec->mComponentName.c_str(), info->mBufferID);
3179349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
3180c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber                    CHECK_EQ(mCodec->mOMX->fillBuffer(mCodec->mNode, info->mBufferID),
3181c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber                             (status_t)OK);
3182f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3183c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber                    info->mStatus = BufferInfo::OWNED_BY_COMPONENT;
3184c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber                }
3185f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
3186f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3187f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3188f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3189f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3190f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3191f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ((int)mode, (int)FREE_BUFFERS);
3192f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3193f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ((status_t)OK,
3194f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                     mCodec->freeBuffer(kPortIndexOutput, index));
3195f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3196f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3197f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3198f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3199f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3200f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
3201f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3202f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::UninitializedState::UninitializedState(ACodec *codec)
3203f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : BaseState(codec) {
3204f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3205f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3206c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Hubervoid ACodec::UninitializedState::stateEntered() {
3207c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    ALOGV("Now uninitialized");
3208ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
3209ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    if (mDeathNotifier != NULL) {
3210ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        mCodec->mOMX->asBinder()->unlinkToDeath(mDeathNotifier);
3211ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        mDeathNotifier.clear();
3212ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    }
3213ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
3214ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    mCodec->mNativeWindow.clear();
3215ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    mCodec->mNode = NULL;
3216ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    mCodec->mOMX.clear();
3217ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    mCodec->mQuirks = 0;
3218ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    mCodec->mFlags = 0;
3219ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    mCodec->mComponentName.clear();
3220c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber}
3221c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3222f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::UninitializedState::onMessageReceived(const sp<AMessage> &msg) {
3223f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool handled = false;
3224f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3225f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (msg->what()) {
3226f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case ACodec::kWhatSetup:
3227f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3228f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            onSetup(msg);
3229f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3230f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
3231f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3232f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3233f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
32345778822d86b0337407514b9372562b86edfa91cdAndreas Huber        case ACodec::kWhatAllocateComponent:
32355778822d86b0337407514b9372562b86edfa91cdAndreas Huber        {
32365778822d86b0337407514b9372562b86edfa91cdAndreas Huber            onAllocateComponent(msg);
32375778822d86b0337407514b9372562b86edfa91cdAndreas Huber            handled = true;
32385778822d86b0337407514b9372562b86edfa91cdAndreas Huber            break;
32395778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
32405778822d86b0337407514b9372562b86edfa91cdAndreas Huber
3241f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case ACodec::kWhatShutdown:
3242f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3243c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            int32_t keepComponentAllocated;
3244c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            CHECK(msg->findInt32(
3245c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber                        "keepComponentAllocated", &keepComponentAllocated));
3246c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            CHECK(!keepComponentAllocated);
3247c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3248f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            sp<AMessage> notify = mCodec->mNotify->dup();
3249f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            notify->setInt32("what", ACodec::kWhatShutdownCompleted);
3250f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            notify->post();
3251f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3252f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
3253c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber            break;
3254f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3255f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3256f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case ACodec::kWhatFlush:
3257f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3258f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            sp<AMessage> notify = mCodec->mNotify->dup();
3259f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            notify->setInt32("what", ACodec::kWhatFlushCompleted);
3260f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            notify->post();
3261f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3262f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
3263c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber            break;
3264f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3265f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3266f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3267f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return BaseState::onMessageReceived(msg);
3268f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3269f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3270f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return handled;
3271f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3272f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3273f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::UninitializedState::onSetup(
3274f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        const sp<AMessage> &msg) {
3275c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    if (onAllocateComponent(msg)
3276c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            && mCodec->mLoadedState->onConfigureComponent(msg)) {
3277c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        mCodec->mLoadedState->onStart();
3278c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    }
32795778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
32805778822d86b0337407514b9372562b86edfa91cdAndreas Huber
3281c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huberbool ACodec::UninitializedState::onAllocateComponent(const sp<AMessage> &msg) {
32825778822d86b0337407514b9372562b86edfa91cdAndreas Huber    ALOGV("onAllocateComponent");
32835778822d86b0337407514b9372562b86edfa91cdAndreas Huber
3284c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    CHECK(mCodec->mNode == NULL);
32855778822d86b0337407514b9372562b86edfa91cdAndreas Huber
3286f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    OMXClient client;
3287f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(client.connect(), (status_t)OK);
3288f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3289f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    sp<IOMX> omx = client.interface();
3290f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3291ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    sp<AMessage> notify = new AMessage(kWhatOMXDied, mCodec->id());
3292ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
3293ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    mDeathNotifier = new DeathNotifier(notify);
3294ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    if (omx->asBinder()->linkToDeath(mDeathNotifier) != OK) {
3295ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        // This was a local binder, if it dies so do we, we won't care
3296ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        // about any notifications in the afterlife.
3297ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber        mDeathNotifier.clear();
3298ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    }
3299ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber
3300e671207115fac3914134c61b336d5fa0242c68caAndreas Huber    Vector<OMXCodec::CodecNameAndQuirks> matchingCodecs;
33015778822d86b0337407514b9372562b86edfa91cdAndreas Huber
3302f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    AString mime;
3303f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
33045778822d86b0337407514b9372562b86edfa91cdAndreas Huber    AString componentName;
3305d7bee3a9d2ad76d073d91f0ee36d5ac5f9df480cAndreas Huber    uint32_t quirks = 0;
33065778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (msg->findString("componentName", &componentName)) {
3307e671207115fac3914134c61b336d5fa0242c68caAndreas Huber        ssize_t index = matchingCodecs.add();
3308e671207115fac3914134c61b336d5fa0242c68caAndreas Huber        OMXCodec::CodecNameAndQuirks *entry = &matchingCodecs.editItemAt(index);
3309e671207115fac3914134c61b336d5fa0242c68caAndreas Huber        entry->mName = String8(componentName.c_str());
3310afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber
3311e671207115fac3914134c61b336d5fa0242c68caAndreas Huber        if (!OMXCodec::findCodecQuirks(
3312e671207115fac3914134c61b336d5fa0242c68caAndreas Huber                    componentName.c_str(), &entry->mQuirks)) {
3313e671207115fac3914134c61b336d5fa0242c68caAndreas Huber            entry->mQuirks = 0;
3314afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber        }
33155778822d86b0337407514b9372562b86edfa91cdAndreas Huber    } else {
33165778822d86b0337407514b9372562b86edfa91cdAndreas Huber        CHECK(msg->findString("mime", &mime));
33175778822d86b0337407514b9372562b86edfa91cdAndreas Huber
33185778822d86b0337407514b9372562b86edfa91cdAndreas Huber        int32_t encoder;
33195778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!msg->findInt32("encoder", &encoder)) {
33205778822d86b0337407514b9372562b86edfa91cdAndreas Huber            encoder = false;
33215778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
33225778822d86b0337407514b9372562b86edfa91cdAndreas Huber
33235778822d86b0337407514b9372562b86edfa91cdAndreas Huber        OMXCodec::findMatchingCodecs(
33245778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mime.c_str(),
33255778822d86b0337407514b9372562b86edfa91cdAndreas Huber                encoder, // createEncoder
33265778822d86b0337407514b9372562b86edfa91cdAndreas Huber                NULL,  // matchComponentName
33275778822d86b0337407514b9372562b86edfa91cdAndreas Huber                0,     // flags
3328e671207115fac3914134c61b336d5fa0242c68caAndreas Huber                &matchingCodecs);
33295778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
33301065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber
33311065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber    sp<CodecObserver> observer = new CodecObserver;
33321065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber    IOMX::node_id node = NULL;
33331065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber
33341065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber    for (size_t matchIndex = 0; matchIndex < matchingCodecs.size();
33351065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber            ++matchIndex) {
3336e671207115fac3914134c61b336d5fa0242c68caAndreas Huber        componentName = matchingCodecs.itemAt(matchIndex).mName.string();
3337e671207115fac3914134c61b336d5fa0242c68caAndreas Huber        quirks = matchingCodecs.itemAt(matchIndex).mQuirks;
3338f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3339078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber        pid_t tid = androidGetTid();
3340078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber        int prevPriority = androidGetThreadPriority(tid);
3341078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber        androidSetThreadPriority(tid, ANDROID_PRIORITY_FOREGROUND);
33421065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber        status_t err = omx->allocateNode(componentName.c_str(), observer, &node);
3343078cfcf7cce9185ec7559910d08b0bc02bfc88a3Andreas Huber        androidSetThreadPriority(tid, prevPriority);
3344f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
33451065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber        if (err == OK) {
33461065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber            break;
33471065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber        }
33481065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber
33491065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber        node = NULL;
33501065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber    }
33511065b3f17d3048948e7d522049d1980b90df3dc1Andreas Huber
3352c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber    if (node == NULL) {
33535778822d86b0337407514b9372562b86edfa91cdAndreas Huber        if (!mime.empty()) {
33545778822d86b0337407514b9372562b86edfa91cdAndreas Huber            ALOGE("Unable to instantiate a decoder for type '%s'.",
33555778822d86b0337407514b9372562b86edfa91cdAndreas Huber                 mime.c_str());
33565778822d86b0337407514b9372562b86edfa91cdAndreas Huber        } else {
33575778822d86b0337407514b9372562b86edfa91cdAndreas Huber            ALOGE("Unable to instantiate decoder '%s'.", componentName.c_str());
33585778822d86b0337407514b9372562b86edfa91cdAndreas Huber        }
3359c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber
3360cc54fbaa69c0b69929467449d2c19192f15b5039Andreas Huber        mCodec->signalError(OMX_ErrorComponentNotFound);
3361c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        return false;
3362c92fd24c10a6bf80b346d7e261325434d9c6964bAndreas Huber    }
3363f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3364ac0230da14a3d223c2144b165a3a163e8519d239Andreas Huber    notify = new AMessage(kWhatOMXMessage, mCodec->id());
3365f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    observer->setNotificationMessage(notify);
3366f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3367f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mCodec->mComponentName = componentName;
3368ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber    mCodec->mFlags = 0;
3369ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
3370ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber    if (componentName.endsWith(".secure")) {
3371ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber        mCodec->mFlags |= kFlagIsSecure;
3372ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber    }
3373ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber
3374afc16d667afa23f5aa00154ccad62f8c45cf5419Andreas Huber    mCodec->mQuirks = quirks;
3375f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mCodec->mOMX = omx;
3376f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mCodec->mNode = node;
3377f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
33785778822d86b0337407514b9372562b86edfa91cdAndreas Huber    {
33795778822d86b0337407514b9372562b86edfa91cdAndreas Huber        sp<AMessage> notify = mCodec->mNotify->dup();
33805778822d86b0337407514b9372562b86edfa91cdAndreas Huber        notify->setInt32("what", ACodec::kWhatComponentAllocated);
33815778822d86b0337407514b9372562b86edfa91cdAndreas Huber        notify->setString("componentName", mCodec->mComponentName.c_str());
33825778822d86b0337407514b9372562b86edfa91cdAndreas Huber        notify->post();
33835778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
3384c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3385c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    mCodec->changeState(mCodec->mLoadedState);
3386c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3387c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    return true;
33885778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
33895778822d86b0337407514b9372562b86edfa91cdAndreas Huber
3390c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber////////////////////////////////////////////////////////////////////////////////
3391c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3392c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas HuberACodec::LoadedState::LoadedState(ACodec *codec)
3393c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    : BaseState(codec) {
3394c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber}
3395c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3396c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Hubervoid ACodec::LoadedState::stateEntered() {
3397c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    ALOGV("[%s] Now Loaded", mCodec->mComponentName.c_str());
3398c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3399f6f38287b97ec69b169387add6458f859b770e65Andreas Huber    mCodec->mPortEOS[kPortIndexInput] =
3400f6f38287b97ec69b169387add6458f859b770e65Andreas Huber        mCodec->mPortEOS[kPortIndexOutput] = false;
3401f6f38287b97ec69b169387add6458f859b770e65Andreas Huber
3402f6f38287b97ec69b169387add6458f859b770e65Andreas Huber    mCodec->mInputEOSResult = OK;
3403f6f38287b97ec69b169387add6458f859b770e65Andreas Huber
3404c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    if (mCodec->mShutdownInProgress) {
3405c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        bool keepComponentAllocated = mCodec->mKeepComponentAllocated;
3406c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3407c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        mCodec->mShutdownInProgress = false;
3408c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        mCodec->mKeepComponentAllocated = false;
3409c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3410c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        onShutdown(keepComponentAllocated);
3411c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    }
3412c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber}
3413c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3414c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Hubervoid ACodec::LoadedState::onShutdown(bool keepComponentAllocated) {
3415c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    if (!keepComponentAllocated) {
3416c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        CHECK_EQ(mCodec->mOMX->freeNode(mCodec->mNode), (status_t)OK);
3417c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3418c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        mCodec->changeState(mCodec->mUninitializedState);
3419c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    }
3420c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3421c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    sp<AMessage> notify = mCodec->mNotify->dup();
3422c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    notify->setInt32("what", ACodec::kWhatShutdownCompleted);
3423c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    notify->post();
3424c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber}
3425c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3426c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huberbool ACodec::LoadedState::onMessageReceived(const sp<AMessage> &msg) {
3427c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    bool handled = false;
3428c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3429c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    switch (msg->what()) {
3430c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        case ACodec::kWhatConfigureComponent:
3431c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        {
3432c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            onConfigureComponent(msg);
3433c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            handled = true;
3434c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            break;
3435c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        }
3436c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
34377cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        case ACodec::kWhatCreateInputSurface:
34387cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        {
34397cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden            onCreateInputSurface(msg);
34407cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden            handled = true;
34417cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden            break;
34427cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        }
34437cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden
3444c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        case ACodec::kWhatStart:
3445c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        {
3446c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            onStart();
3447c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            handled = true;
3448c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            break;
3449c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        }
3450c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3451c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        case ACodec::kWhatShutdown:
3452c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        {
3453c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            int32_t keepComponentAllocated;
3454c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            CHECK(msg->findInt32(
3455c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber                        "keepComponentAllocated", &keepComponentAllocated));
3456c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3457c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            onShutdown(keepComponentAllocated);
3458c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3459c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            handled = true;
3460c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            break;
3461c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        }
3462c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3463c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        case ACodec::kWhatFlush:
3464c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        {
3465c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            sp<AMessage> notify = mCodec->mNotify->dup();
3466c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            notify->setInt32("what", ACodec::kWhatFlushCompleted);
3467c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            notify->post();
3468c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3469c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            handled = true;
3470c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            break;
3471c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        }
3472c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3473c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        default:
3474c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            return BaseState::onMessageReceived(msg);
3475c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    }
3476c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3477c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    return handled;
3478c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber}
3479c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3480c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huberbool ACodec::LoadedState::onConfigureComponent(
34815778822d86b0337407514b9372562b86edfa91cdAndreas Huber        const sp<AMessage> &msg) {
34825778822d86b0337407514b9372562b86edfa91cdAndreas Huber    ALOGV("onConfigureComponent");
34835778822d86b0337407514b9372562b86edfa91cdAndreas Huber
34845778822d86b0337407514b9372562b86edfa91cdAndreas Huber    CHECK(mCodec->mNode != NULL);
34855778822d86b0337407514b9372562b86edfa91cdAndreas Huber
34865778822d86b0337407514b9372562b86edfa91cdAndreas Huber    AString mime;
34875778822d86b0337407514b9372562b86edfa91cdAndreas Huber    CHECK(msg->findString("mime", &mime));
34885778822d86b0337407514b9372562b86edfa91cdAndreas Huber
34895778822d86b0337407514b9372562b86edfa91cdAndreas Huber    status_t err = mCodec->configureCodec(mime.c_str(), msg);
34905778822d86b0337407514b9372562b86edfa91cdAndreas Huber
34915778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (err != OK) {
3492c5255ac5b1bd313dcb50159566163b24dce7483fAndreas Huber        ALOGE("[%s] configureCodec returning error %d",
3493c5255ac5b1bd313dcb50159566163b24dce7483fAndreas Huber              mCodec->mComponentName.c_str(), err);
3494c5255ac5b1bd313dcb50159566163b24dce7483fAndreas Huber
34955778822d86b0337407514b9372562b86edfa91cdAndreas Huber        mCodec->signalError(OMX_ErrorUndefined, err);
3496c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber        return false;
34975778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
3498f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3499f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    sp<RefBase> obj;
3500386d609dc513e838c7e7c4c46c604493ccd560beAndreas Huber    if (msg->findObject("native-window", &obj)
35015778822d86b0337407514b9372562b86edfa91cdAndreas Huber            && strncmp("OMX.google.", mCodec->mComponentName.c_str(), 11)) {
35021173118eace0e9e347cb007f0da817cee87579edGlenn Kasten        sp<NativeWindowWrapper> nativeWindow(
35031173118eace0e9e347cb007f0da817cee87579edGlenn Kasten                static_cast<NativeWindowWrapper *>(obj.get()));
35041173118eace0e9e347cb007f0da817cee87579edGlenn Kasten        CHECK(nativeWindow != NULL);
35051173118eace0e9e347cb007f0da817cee87579edGlenn Kasten        mCodec->mNativeWindow = nativeWindow->getNativeWindow();
35061ec498e67f78fb0c3ab2a1cd9a071e6356618dadAndreas Huber
35071ec498e67f78fb0c3ab2a1cd9a071e6356618dadAndreas Huber        native_window_set_scaling_mode(
35081ec498e67f78fb0c3ab2a1cd9a071e6356618dadAndreas Huber                mCodec->mNativeWindow.get(),
35091ec498e67f78fb0c3ab2a1cd9a071e6356618dadAndreas Huber                NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
3510f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3511f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ((status_t)OK, mCodec->initNativeWindow());
3512f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
35135778822d86b0337407514b9372562b86edfa91cdAndreas Huber    {
35145778822d86b0337407514b9372562b86edfa91cdAndreas Huber        sp<AMessage> notify = mCodec->mNotify->dup();
35155778822d86b0337407514b9372562b86edfa91cdAndreas Huber        notify->setInt32("what", ACodec::kWhatComponentConfigured);
35165778822d86b0337407514b9372562b86edfa91cdAndreas Huber        notify->post();
35175778822d86b0337407514b9372562b86edfa91cdAndreas Huber    }
3518c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3519c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber    return true;
35205778822d86b0337407514b9372562b86edfa91cdAndreas Huber}
35215778822d86b0337407514b9372562b86edfa91cdAndreas Huber
35227cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFaddenvoid ACodec::LoadedState::onCreateInputSurface(
35237cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        const sp<AMessage> &msg) {
35247cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    ALOGV("onCreateInputSurface");
35257cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden
35267cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    sp<AMessage> notify = mCodec->mNotify->dup();
35277cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    notify->setInt32("what", ACodec::kWhatInputSurfaceCreated);
35287cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden
35297cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    sp<IGraphicBufferProducer> bufferProducer;
35307cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    status_t err;
35317cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden
35327cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    err = mCodec->mOMX->createInputSurface(mCodec->mNode, kPortIndexInput,
35337cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden            &bufferProducer);
35347cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    if (err == OK) {
35357cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        notify->setObject("input-surface",
35367cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden                new BufferProducerWrapper(bufferProducer));
35377cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    } else {
35387cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        // Can't use mCodec->signalError() here -- MediaCodec won't forward
35397cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        // the error through because it's in the "configured" state.  We
35407cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        // send a kWhatInputSurfaceCreated with an error value instead.
35417cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        ALOGE("[%s] onCreateInputSurface returning error %d",
35427cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden                mCodec->mComponentName.c_str(), err);
35437cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        notify->setInt32("err", err);
35447cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    }
35457cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden    notify->post();
35467cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden}
35477cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden
3548c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Hubervoid ACodec::LoadedState::onStart() {
35495778822d86b0337407514b9372562b86edfa91cdAndreas Huber    ALOGV("onStart");
35505778822d86b0337407514b9372562b86edfa91cdAndreas Huber
35515778822d86b0337407514b9372562b86edfa91cdAndreas Huber    CHECK_EQ(mCodec->mOMX->sendCommand(
35525778822d86b0337407514b9372562b86edfa91cdAndreas Huber                mCodec->mNode, OMX_CommandStateSet, OMX_StateIdle),
3553f933441648ef6a71dee783d733aac17b9508b452Andreas Huber             (status_t)OK);
3554f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3555f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mCodec->changeState(mCodec->mLoadedToIdleState);
3556f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3557f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3558f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
3559f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3560f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::LoadedToIdleState::LoadedToIdleState(ACodec *codec)
3561f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : BaseState(codec) {
3562f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3563f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3564f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::LoadedToIdleState::stateEntered() {
35653856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] Now Loaded->Idle", mCodec->mComponentName.c_str());
3566f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3567cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber    status_t err;
3568cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber    if ((err = allocateBuffers()) != OK) {
356929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("Failed to allocate buffers after transitioning to IDLE state "
3570cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber             "(error 0x%08x)",
3571cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber             err);
3572cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber
35735778822d86b0337407514b9372562b86edfa91cdAndreas Huber        mCodec->signalError(OMX_ErrorUndefined, err);
357491bfadb4aa75444c60e9949232de3c184cfe060aAndreas Huber
357591bfadb4aa75444c60e9949232de3c184cfe060aAndreas Huber        mCodec->changeState(mCodec->mLoadedState);
3576cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber    }
3577f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3578f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3579f933441648ef6a71dee783d733aac17b9508b452Andreas Huberstatus_t ACodec::LoadedToIdleState::allocateBuffers() {
3580f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    status_t err = mCodec->allocateBuffersOnPort(kPortIndexInput);
3581f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3582f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (err != OK) {
3583f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return err;
3584f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3585f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3586f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return mCodec->allocateBuffersOnPort(kPortIndexOutput);
3587f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3588f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3589f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::LoadedToIdleState::onMessageReceived(const sp<AMessage> &msg) {
3590f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (msg->what()) {
3591f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatShutdown:
3592f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3593f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->deferMessage(msg);
3594f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return true;
3595f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3596f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
35976507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden        case kWhatSignalEndOfInputStream:
35986507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden        {
35996507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden            mCodec->onSignalEndOfInputStream();
36006507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden            return true;
36016507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden        }
36026507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden
36036463e76d41430f9b03a79b221de84255f2475658Marco Nelissen        case kWhatResume:
36046463e76d41430f9b03a79b221de84255f2475658Marco Nelissen        {
36056463e76d41430f9b03a79b221de84255f2475658Marco Nelissen            // We'll be active soon enough.
36066463e76d41430f9b03a79b221de84255f2475658Marco Nelissen            return true;
36076463e76d41430f9b03a79b221de84255f2475658Marco Nelissen        }
36086463e76d41430f9b03a79b221de84255f2475658Marco Nelissen
36096463e76d41430f9b03a79b221de84255f2475658Marco Nelissen        case kWhatFlush:
36106463e76d41430f9b03a79b221de84255f2475658Marco Nelissen        {
36116463e76d41430f9b03a79b221de84255f2475658Marco Nelissen            // We haven't even started yet, so we're flushed alright...
36126463e76d41430f9b03a79b221de84255f2475658Marco Nelissen            sp<AMessage> notify = mCodec->mNotify->dup();
36136463e76d41430f9b03a79b221de84255f2475658Marco Nelissen            notify->setInt32("what", ACodec::kWhatFlushCompleted);
36146463e76d41430f9b03a79b221de84255f2475658Marco Nelissen            notify->post();
36156463e76d41430f9b03a79b221de84255f2475658Marco Nelissen            return true;
36166463e76d41430f9b03a79b221de84255f2475658Marco Nelissen        }
36176463e76d41430f9b03a79b221de84255f2475658Marco Nelissen
3618f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3619f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return BaseState::onMessageReceived(msg);
3620f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3621f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3622f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3623f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::LoadedToIdleState::onOMXEvent(
3624f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
3625f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (event) {
3626f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case OMX_EventCmdComplete:
3627f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3628f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data1, (OMX_U32)OMX_CommandStateSet);
3629f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data2, (OMX_U32)OMX_StateIdle);
3630f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3631f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(mCodec->mOMX->sendCommand(
3632f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        mCodec->mNode, OMX_CommandStateSet, OMX_StateExecuting),
3633f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                     (status_t)OK);
3634f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3635f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->changeState(mCodec->mIdleToExecutingState);
3636f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3637f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return true;
3638f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3639f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3640f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3641f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return BaseState::onOMXEvent(event, data1, data2);
3642f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3643f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3644f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3645f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
3646f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3647f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::IdleToExecutingState::IdleToExecutingState(ACodec *codec)
3648f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : BaseState(codec) {
3649f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3650f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3651f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::IdleToExecutingState::stateEntered() {
36523856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] Now Idle->Executing", mCodec->mComponentName.c_str());
3653f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3654f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3655f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::IdleToExecutingState::onMessageReceived(const sp<AMessage> &msg) {
3656f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (msg->what()) {
3657f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatShutdown:
3658f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3659f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->deferMessage(msg);
3660f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return true;
3661f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3662f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3663d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber        case kWhatResume:
3664d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber        {
3665d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber            // We'll be active soon enough.
3666d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber            return true;
3667d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber        }
3668d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber
3669d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber        case kWhatFlush:
3670d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber        {
3671d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber            // We haven't even started yet, so we're flushed alright...
3672d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber            sp<AMessage> notify = mCodec->mNotify->dup();
3673d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber            notify->setInt32("what", ACodec::kWhatFlushCompleted);
3674d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber            notify->post();
3675d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber
3676d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber            return true;
3677d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber        }
3678d477b8d071826c0768620f7ac302f31d8b12b1caAndreas Huber
36796507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden        case kWhatSignalEndOfInputStream:
36806507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden        {
36816507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden            mCodec->onSignalEndOfInputStream();
36826507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden            return true;
36836507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden        }
36846507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden
3685f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3686f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return BaseState::onMessageReceived(msg);
3687f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3688f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3689f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3690f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::IdleToExecutingState::onOMXEvent(
3691f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
3692f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (event) {
3693f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case OMX_EventCmdComplete:
3694f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3695f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data1, (OMX_U32)OMX_CommandStateSet);
3696f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data2, (OMX_U32)OMX_StateExecuting);
3697f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3698f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->mExecutingState->resume();
3699f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->changeState(mCodec->mExecutingState);
3700f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3701f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return true;
3702f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3703f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3704f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3705f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return BaseState::onOMXEvent(event, data1, data2);
3706f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3707f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3708f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3709f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
3710f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3711f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::ExecutingState::ExecutingState(ACodec *codec)
3712349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber    : BaseState(codec),
3713349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber      mActive(false) {
3714f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3715f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3716f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::BaseState::PortMode ACodec::ExecutingState::getPortMode(
3717f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_U32 portIndex) {
3718f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return RESUBMIT_BUFFERS;
3719f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3720f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3721f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::ExecutingState::submitOutputBuffers() {
3722f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    for (size_t i = 0; i < mCodec->mBuffers[kPortIndexOutput].size(); ++i) {
3723f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        BufferInfo *info = &mCodec->mBuffers[kPortIndexOutput].editItemAt(i);
3724f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3725f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        if (mCodec->mNativeWindow != NULL) {
3726f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK(info->mStatus == BufferInfo::OWNED_BY_US
3727f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    || info->mStatus == BufferInfo::OWNED_BY_NATIVE_WINDOW);
3728f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3729f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            if (info->mStatus == BufferInfo::OWNED_BY_NATIVE_WINDOW) {
3730f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                continue;
3731f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
3732f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        } else {
3733f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ((int)info->mStatus, (int)BufferInfo::OWNED_BY_US);
3734f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3735f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
37363856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("[%s] calling fillBuffer %p",
3737349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber             mCodec->mComponentName.c_str(), info->mBufferID);
3738349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
3739f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK_EQ(mCodec->mOMX->fillBuffer(mCodec->mNode, info->mBufferID),
3740f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                 (status_t)OK);
3741f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3742f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        info->mStatus = BufferInfo::OWNED_BY_COMPONENT;
3743f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3744f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3745f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3746f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::ExecutingState::resume() {
3747349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber    if (mActive) {
37483856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("[%s] We're already active, no need to resume.",
3749349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber             mCodec->mComponentName.c_str());
3750349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
3751349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        return;
3752349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber    }
3753349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
3754f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    submitOutputBuffers();
3755f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3756f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    // Post the first input buffer.
3757f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_GT(mCodec->mBuffers[kPortIndexInput].size(), 0u);
3758f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BufferInfo *info = &mCodec->mBuffers[kPortIndexInput].editItemAt(0);
3759f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3760f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    postFillThisBuffer(info);
3761349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
3762349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber    mActive = true;
3763f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3764f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3765f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::ExecutingState::stateEntered() {
37663856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] Now Executing", mCodec->mComponentName.c_str());
3767f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3768f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mCodec->processDeferredMessages();
3769f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3770f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3771f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::ExecutingState::onMessageReceived(const sp<AMessage> &msg) {
3772f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool handled = false;
3773f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3774f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (msg->what()) {
3775f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatShutdown:
3776f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3777c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            int32_t keepComponentAllocated;
3778c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            CHECK(msg->findInt32(
3779c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber                        "keepComponentAllocated", &keepComponentAllocated));
3780c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3781c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            mCodec->mShutdownInProgress = true;
3782c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            mCodec->mKeepComponentAllocated = keepComponentAllocated;
3783c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber
3784349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            mActive = false;
3785349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
3786f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(mCodec->mOMX->sendCommand(
3787f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        mCodec->mNode, OMX_CommandStateSet, OMX_StateIdle),
3788f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                     (status_t)OK);
3789f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3790f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->changeState(mCodec->mExecutingToIdleState);
3791f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3792f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
3793f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3794f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3795f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3796f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatFlush:
3797f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
37987a3a2b2f9bb9421dcf83fbd47276e57917078aefJames Dong            ALOGV("[%s] ExecutingState flushing now "
3799d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber                 "(codec owns %d/%d input, %d/%d output).",
3800d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber                    mCodec->mComponentName.c_str(),
3801d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber                    mCodec->countBuffersOwnedByComponent(kPortIndexInput),
3802d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber                    mCodec->mBuffers[kPortIndexInput].size(),
3803d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber                    mCodec->countBuffersOwnedByComponent(kPortIndexOutput),
3804d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber                    mCodec->mBuffers[kPortIndexOutput].size());
3805d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber
3806349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            mActive = false;
3807349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
3808f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(mCodec->mOMX->sendCommand(
3809f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        mCodec->mNode, OMX_CommandFlush, OMX_ALL),
3810f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                     (status_t)OK);
3811f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3812f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->changeState(mCodec->mFlushingState);
3813f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
3814f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3815f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3816f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3817f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatResume:
3818f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3819f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            resume();
3820f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3821f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
3822f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3823f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3824f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3825496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber        case kWhatRequestIDRFrame:
3826496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber        {
3827496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber            status_t err = mCodec->requestIDRFrame();
3828496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber            if (err != OK) {
3829496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber                ALOGW("Requesting an IDR frame failed.");
3830496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber            }
3831496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber
3832496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber            handled = true;
3833496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber            break;
3834496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber        }
3835496238cc7551d414067dcbbb4fe3bd801f205f95Andreas Huber
3836a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        case kWhatSetParameters:
3837a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        {
3838a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            sp<AMessage> params;
3839a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            CHECK(msg->findMessage("params", &params));
3840a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
3841a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            status_t err = mCodec->setParameters(params);
3842a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
3843a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            sp<AMessage> reply;
3844a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            if (msg->findMessage("reply", &reply)) {
3845a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber                reply->setInt32("err", err);
3846a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber                reply->post();
3847a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            }
3848a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
3849a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            handled = true;
3850a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            break;
3851a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        }
3852a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
38537cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        case ACodec::kWhatSignalEndOfInputStream:
38547cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        {
38556507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden            mCodec->onSignalEndOfInputStream();
38567cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden            handled = true;
38577cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden            break;
38587cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden        }
38597cd58537932ef6f481f68be0b9c597a89cebdfecAndy McFadden
3860f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3861f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = BaseState::onMessageReceived(msg);
3862f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3863f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3864f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3865f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return handled;
3866f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3867f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3868a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huberstatus_t ACodec::setParameters(const sp<AMessage> &params) {
3869a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber    int32_t videoBitrate;
3870a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber    if (params->findInt32("videoBitrate", &videoBitrate)) {
3871a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        OMX_VIDEO_CONFIG_BITRATETYPE configParams;
3872a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        InitOMXParams(&configParams);
3873a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        configParams.nPortIndex = kPortIndexOutput;
3874a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        configParams.nEncodeBitrate = videoBitrate;
3875a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
3876a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        status_t err = mOMX->setConfig(
3877a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber                mNode,
3878a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber                OMX_IndexConfigVideoBitrate,
3879a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber                &configParams,
3880a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber                sizeof(configParams));
3881a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
3882a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        if (err != OK) {
3883a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            ALOGE("setConfig(OMX_IndexConfigVideoBitrate, %d) failed w/ err %d",
3884a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber                   videoBitrate, err);
3885a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
3886a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber            return err;
3887a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber        }
3888a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber    }
3889a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
3890a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber    return OK;
3891a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber}
3892a2eb22c1de262aa3fa7c356537ac2fe165afdf3dAndreas Huber
38936507d14c6d10f93d390de62b9eed267f9b544985Andy McFaddenvoid ACodec::onSignalEndOfInputStream() {
38946507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden    sp<AMessage> notify = mNotify->dup();
38956507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden    notify->setInt32("what", ACodec::kWhatSignaledInputEOS);
38966507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden
38976507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden    status_t err = mOMX->signalEndOfInputStream(mNode);
38986507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden    if (err != OK) {
38996507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden        notify->setInt32("err", err);
39006507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden    }
39016507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden    notify->post();
39026507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden}
39036507d14c6d10f93d390de62b9eed267f9b544985Andy McFadden
3904f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::ExecutingState::onOMXEvent(
3905f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
3906f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (event) {
3907f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case OMX_EventPortSettingsChanged:
3908f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3909f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data1, (OMX_U32)kPortIndexOutput);
3910f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
391131e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            if (data2 == 0 || data2 == OMX_IndexParamPortDefinition) {
3912f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK_EQ(mCodec->mOMX->sendCommand(
3913f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            mCodec->mNode,
3914f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            OMX_CommandPortDisable, kPortIndexOutput),
3915f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                         (status_t)OK);
3916f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3917349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber                mCodec->freeOutputBuffersNotOwnedByComponent();
3918f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3919f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                mCodec->changeState(mCodec->mOutputPortSettingsChangedState);
392031e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber            } else if (data2 == OMX_IndexConfigCommonOutputCrop) {
392131e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber                mCodec->mSentFormat = false;
3922f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            } else {
39233856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("[%s] OMX_EventPortSettingsChanged 0x%08lx",
3924f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                     mCodec->mComponentName.c_str(), data2);
3925f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
3926f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3927f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return true;
3928f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3929f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3930f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case OMX_EventBufferFlag:
3931f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3932f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return true;
3933f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3934f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3935f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3936f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return BaseState::onOMXEvent(event, data1, data2);
3937f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3938f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3939f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3940f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
3941f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3942f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::OutputPortSettingsChangedState::OutputPortSettingsChangedState(
3943f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        ACodec *codec)
3944f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : BaseState(codec) {
3945f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3946f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3947f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::BaseState::PortMode ACodec::OutputPortSettingsChangedState::getPortMode(
3948f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_U32 portIndex) {
3949f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (portIndex == kPortIndexOutput) {
3950f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        return FREE_BUFFERS;
3951f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3952f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3953f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    CHECK_EQ(portIndex, (OMX_U32)kPortIndexInput);
3954f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3955f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return RESUBMIT_BUFFERS;
3956f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3957f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3958f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::OutputPortSettingsChangedState::onMessageReceived(
3959f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        const sp<AMessage> &msg) {
3960f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool handled = false;
3961f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3962f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (msg->what()) {
3963f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatFlush:
3964f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatShutdown:
3965349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        case kWhatResume:
3966f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3967349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            if (msg->what() == kWhatResume) {
39683856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("[%s] Deferring resume", mCodec->mComponentName.c_str());
3969349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            }
3970349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
3971f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->deferMessage(msg);
3972f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
3973f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3974f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
3975f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3976f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
3977f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = BaseState::onMessageReceived(msg);
3978f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
3979f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
3980f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3981f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return handled;
3982f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3983f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3984f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::OutputPortSettingsChangedState::stateEntered() {
39853856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] Now handling output port settings change",
3986f933441648ef6a71dee783d733aac17b9508b452Andreas Huber         mCodec->mComponentName.c_str());
3987f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
3988f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
3989f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::OutputPortSettingsChangedState::onOMXEvent(
3990f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
3991f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (event) {
3992f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case OMX_EventCmdComplete:
3993f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
3994f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            if (data1 == (OMX_U32)OMX_CommandPortDisable) {
3995f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK_EQ(data2, (OMX_U32)kPortIndexOutput);
3996f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
39973856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("[%s] Output port now disabled.",
3998f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        mCodec->mComponentName.c_str());
3999f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4000f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK(mCodec->mBuffers[kPortIndexOutput].isEmpty());
4001f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                mCodec->mDealer[kPortIndexOutput].clear();
4002f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4003f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK_EQ(mCodec->mOMX->sendCommand(
4004f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                            mCodec->mNode, OMX_CommandPortEnable, kPortIndexOutput),
4005f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                         (status_t)OK);
4006f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4007cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber                status_t err;
4008cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber                if ((err = mCodec->allocateBuffersOnPort(
4009cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber                                kPortIndexOutput)) != OK) {
401029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                    ALOGE("Failed to allocate output port buffers after "
4011cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber                         "port reconfiguration (error 0x%08x)",
4012cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber                         err);
4013cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber
40145778822d86b0337407514b9372562b86edfa91cdAndreas Huber                    mCodec->signalError(OMX_ErrorUndefined, err);
4015d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber
4016755b9861197f1566f916785ba7d35cf86fd5b2d5Andreas Huber                    // This is technically not correct, but appears to be
4017755b9861197f1566f916785ba7d35cf86fd5b2d5Andreas Huber                    // the only way to free the component instance.
4018755b9861197f1566f916785ba7d35cf86fd5b2d5Andreas Huber                    // Controlled transitioning from excecuting->idle
4019755b9861197f1566f916785ba7d35cf86fd5b2d5Andreas Huber                    // and idle->loaded seem impossible probably because
4020755b9861197f1566f916785ba7d35cf86fd5b2d5Andreas Huber                    // the output port never finishes re-enabling.
4021755b9861197f1566f916785ba7d35cf86fd5b2d5Andreas Huber                    mCodec->mShutdownInProgress = true;
4022755b9861197f1566f916785ba7d35cf86fd5b2d5Andreas Huber                    mCodec->mKeepComponentAllocated = false;
4023755b9861197f1566f916785ba7d35cf86fd5b2d5Andreas Huber                    mCodec->changeState(mCodec->mLoadedState);
4024cb67cd1b51ff9ab221e3124cf7e546515fef3c87Andreas Huber                }
4025f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4026f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                return true;
4027f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            } else if (data1 == (OMX_U32)OMX_CommandPortEnable) {
4028f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK_EQ(data2, (OMX_U32)kPortIndexOutput);
4029f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
403031e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber                mCodec->mSentFormat = false;
403131e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
40323856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("[%s] Output port now reenabled.",
4033f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                        mCodec->mComponentName.c_str());
4034f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4035349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber                if (mCodec->mExecutingState->active()) {
4036349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber                    mCodec->mExecutingState->submitOutputBuffers();
4037349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber                }
4038349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
4039f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                mCodec->changeState(mCodec->mExecutingState);
4040f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4041f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                return true;
4042f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
4043f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4044f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return false;
4045f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4046f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4047f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
4048f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return false;
4049f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
4050f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4051f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4052f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
4053f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4054f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::ExecutingToIdleState::ExecutingToIdleState(ACodec *codec)
40555778822d86b0337407514b9372562b86edfa91cdAndreas Huber    : BaseState(codec),
40565778822d86b0337407514b9372562b86edfa91cdAndreas Huber      mComponentNowIdle(false) {
4057f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4058f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4059f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::ExecutingToIdleState::onMessageReceived(const sp<AMessage> &msg) {
4060f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool handled = false;
4061f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4062f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (msg->what()) {
4063f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatFlush:
4064f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
4065f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            // Don't send me a flush request if you previously wanted me
4066f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            // to shutdown.
4067f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            TRESPASS();
4068f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
4069f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4070f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4071f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatShutdown:
4072f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
4073f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            // We're already doing that...
4074f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4075f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
4076f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
4077f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4078f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4079f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
4080f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = BaseState::onMessageReceived(msg);
4081f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
4082f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
4083f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4084f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return handled;
4085f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4086f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4087f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::ExecutingToIdleState::stateEntered() {
40883856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] Now Executing->Idle", mCodec->mComponentName.c_str());
408931e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber
40905778822d86b0337407514b9372562b86edfa91cdAndreas Huber    mComponentNowIdle = false;
409131e2508c75018145a8238925ff1a08cbde4e799aAndreas Huber    mCodec->mSentFormat = false;
4092f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4093f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4094f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::ExecutingToIdleState::onOMXEvent(
4095f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
4096f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (event) {
4097f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case OMX_EventCmdComplete:
4098f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
4099f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data1, (OMX_U32)OMX_CommandStateSet);
4100f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data2, (OMX_U32)OMX_StateIdle);
4101f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
41025778822d86b0337407514b9372562b86edfa91cdAndreas Huber            mComponentNowIdle = true;
41035778822d86b0337407514b9372562b86edfa91cdAndreas Huber
4104f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            changeStateIfWeOwnAllBuffers();
4105f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4106f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return true;
4107f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4108f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4109349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        case OMX_EventPortSettingsChanged:
4110349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        case OMX_EventBufferFlag:
4111349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        {
4112349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            // We're shutting down and don't care about this anymore.
4113349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            return true;
4114349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        }
4115349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
4116f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
4117f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return BaseState::onOMXEvent(event, data1, data2);
4118f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
4119f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
41200af941927ef8f35f2da5380dfd8d04b7f4ed3532Andreas Huber
4121f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::ExecutingToIdleState::changeStateIfWeOwnAllBuffers() {
41225778822d86b0337407514b9372562b86edfa91cdAndreas Huber    if (mComponentNowIdle && mCodec->allYourBuffersAreBelongToUs()) {
4123f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK_EQ(mCodec->mOMX->sendCommand(
4124f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                    mCodec->mNode, OMX_CommandStateSet, OMX_StateLoaded),
4125f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                 (status_t)OK);
4126f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4127f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK_EQ(mCodec->freeBuffersOnPort(kPortIndexInput), (status_t)OK);
4128f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        CHECK_EQ(mCodec->freeBuffersOnPort(kPortIndexOutput), (status_t)OK);
4129f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4130bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        if (mCodec->mFlags & kFlagIsSecure && mCodec->mNativeWindow != NULL) {
4131bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            // We push enough 1x1 blank buffers to ensure that one of
4132bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            // them has made it to the display.  This allows the OMX
4133bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            // component teardown to zero out any protected buffers
4134bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            // without the risk of scanning out one of those buffers.
4135bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber            mCodec->pushBlankBuffersToNativeWindow();
4136bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber        }
4137bc098410be55f9d96f394b3981a0c482b83859b6Andreas Huber
4138f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mCodec->changeState(mCodec->mIdleToLoadedState);
4139f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
4140f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4141f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4142f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::ExecutingToIdleState::onInputBufferFilled(
4143f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        const sp<AMessage> &msg) {
4144f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BaseState::onInputBufferFilled(msg);
4145f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4146f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    changeStateIfWeOwnAllBuffers();
4147f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4148f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4149f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::ExecutingToIdleState::onOutputBufferDrained(
4150f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        const sp<AMessage> &msg) {
4151f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BaseState::onOutputBufferDrained(msg);
4152f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4153f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    changeStateIfWeOwnAllBuffers();
4154f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4155f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4156f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
4157f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4158f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::IdleToLoadedState::IdleToLoadedState(ACodec *codec)
4159f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : BaseState(codec) {
4160f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4161f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4162f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::IdleToLoadedState::onMessageReceived(const sp<AMessage> &msg) {
4163f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool handled = false;
4164f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4165f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (msg->what()) {
4166f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatShutdown:
4167f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
4168f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            // We're already doing that...
4169f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4170f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
4171f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
4172f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4173f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4174f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatFlush:
4175f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
4176f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            // Don't send me a flush request if you previously wanted me
4177f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            // to shutdown.
4178f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            TRESPASS();
4179f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
4180f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4181f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4182f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
4183f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = BaseState::onMessageReceived(msg);
4184f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
4185f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
4186f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4187f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return handled;
4188f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4189f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4190f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::IdleToLoadedState::stateEntered() {
41913856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] Now Idle->Loaded", mCodec->mComponentName.c_str());
4192f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4193f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4194f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::IdleToLoadedState::onOMXEvent(
4195f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
4196f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (event) {
4197f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case OMX_EventCmdComplete:
4198f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
4199f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data1, (OMX_U32)OMX_CommandStateSet);
4200f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data2, (OMX_U32)OMX_StateLoaded);
4201f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4202c95c2ddcdfc974f42408a377fbe2de51b94a8c94Andreas Huber            mCodec->changeState(mCodec->mLoadedState);
4203f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4204f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return true;
4205f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4206f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4207f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
4208f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return BaseState::onOMXEvent(event, data1, data2);
4209f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
4210f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4211f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4212f933441648ef6a71dee783d733aac17b9508b452Andreas Huber////////////////////////////////////////////////////////////////////////////////
4213f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4214f933441648ef6a71dee783d733aac17b9508b452Andreas HuberACodec::FlushingState::FlushingState(ACodec *codec)
4215f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    : BaseState(codec) {
4216f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4217f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4218f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::FlushingState::stateEntered() {
42193856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("[%s] Now Flushing", mCodec->mComponentName.c_str());
4220f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4221f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    mFlushComplete[kPortIndexInput] = mFlushComplete[kPortIndexOutput] = false;
4222f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4223f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4224f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::FlushingState::onMessageReceived(const sp<AMessage> &msg) {
4225f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    bool handled = false;
4226f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4227f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (msg->what()) {
4228f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatShutdown:
4229f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
4230f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->deferMessage(msg);
4231f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
4232f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4233f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4234f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case kWhatFlush:
4235f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
4236f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            // We're already doing this right now.
4237f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = true;
4238f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
4239f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4240f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4241f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
4242f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            handled = BaseState::onMessageReceived(msg);
4243f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            break;
4244f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
4245f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4246f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return handled;
4247f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4248f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4249f933441648ef6a71dee783d733aac17b9508b452Andreas Huberbool ACodec::FlushingState::onOMXEvent(
4250f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2) {
42517a3a2b2f9bb9421dcf83fbd47276e57917078aefJames Dong    ALOGV("[%s] FlushingState onOMXEvent(%d,%ld)",
4252d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber            mCodec->mComponentName.c_str(), event, data1);
4253d3d822204249eaca4259bdf46f6f0357b96e8e21Andreas Huber
4254f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    switch (event) {
4255f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        case OMX_EventCmdComplete:
4256f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        {
4257f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            CHECK_EQ(data1, (OMX_U32)OMX_CommandFlush);
4258f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4259f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            if (data2 == kPortIndexInput || data2 == kPortIndexOutput) {
4260f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK(!mFlushComplete[data2]);
4261f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                mFlushComplete[data2] = true;
4262e5bf6409774fe73f968b7db215f535b64de66139Andreas Huber
4263e5bf6409774fe73f968b7db215f535b64de66139Andreas Huber                if (mFlushComplete[kPortIndexInput]
4264e5bf6409774fe73f968b7db215f535b64de66139Andreas Huber                        && mFlushComplete[kPortIndexOutput]) {
4265e5bf6409774fe73f968b7db215f535b64de66139Andreas Huber                    changeStateIfWeOwnAllBuffers();
4266e5bf6409774fe73f968b7db215f535b64de66139Andreas Huber                }
4267f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            } else {
4268f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK_EQ(data2, OMX_ALL);
4269f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK(mFlushComplete[kPortIndexInput]);
4270f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                CHECK(mFlushComplete[kPortIndexOutput]);
4271f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4272f933441648ef6a71dee783d733aac17b9508b452Andreas Huber                changeStateIfWeOwnAllBuffers();
4273f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            }
4274f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4275f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return true;
4276f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        }
4277f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4278349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        case OMX_EventPortSettingsChanged:
4279349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        {
4280349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            sp<AMessage> msg = new AMessage(kWhatOMXMessage, mCodec->id());
4281349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            msg->setInt32("type", omx_message::EVENT);
4282349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            msg->setPointer("node", mCodec->mNode);
4283349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            msg->setInt32("event", event);
4284349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            msg->setInt32("data1", data1);
4285349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            msg->setInt32("data2", data2);
4286349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
42873856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("[%s] Deferring OMX_EventPortSettingsChanged",
4288349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber                 mCodec->mComponentName.c_str());
4289349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
4290349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            mCodec->deferMessage(msg);
4291349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
4292349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber            return true;
4293349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber        }
4294349d3fcb4afacf754f7b5b5186d2f258f5bf35e7Andreas Huber
4295f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        default:
4296f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            return BaseState::onOMXEvent(event, data1, data2);
4297f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
4298f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4299f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    return true;
4300f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4301f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4302f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::FlushingState::onOutputBufferDrained(const sp<AMessage> &msg) {
4303f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BaseState::onOutputBufferDrained(msg);
4304f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4305f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    changeStateIfWeOwnAllBuffers();
4306f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4307f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4308f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::FlushingState::onInputBufferFilled(const sp<AMessage> &msg) {
4309f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    BaseState::onInputBufferFilled(msg);
4310f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4311f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    changeStateIfWeOwnAllBuffers();
4312f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4313f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4314f933441648ef6a71dee783d733aac17b9508b452Andreas Hubervoid ACodec::FlushingState::changeStateIfWeOwnAllBuffers() {
4315f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    if (mFlushComplete[kPortIndexInput]
4316f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            && mFlushComplete[kPortIndexOutput]
4317f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            && mCodec->allYourBuffersAreBelongToUs()) {
43187e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber        // We now own all buffers except possibly those still queued with
43197e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber        // the native window for rendering. Let's get those back as well.
43207e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber        mCodec->waitUntilAllPossibleNativeWindowBuffersAreReturnedToUs();
43217e7013392e302a28364df1dcee79b82ad90978b4Andreas Huber
4322f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        sp<AMessage> notify = mCodec->mNotify->dup();
4323f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        notify->setInt32("what", ACodec::kWhatFlushCompleted);
4324f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        notify->post();
4325f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4326f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mCodec->mPortEOS[kPortIndexInput] =
4327f933441648ef6a71dee783d733aac17b9508b452Andreas Huber            mCodec->mPortEOS[kPortIndexOutput] = false;
4328f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4329dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber        mCodec->mInputEOSResult = OK;
4330dc9bacd838442a524585887e6ea6696836be2edaAndreas Huber
4331f3bd1972e039c6ded5154db715e5a32f1813a239Marco Nelissen        if (mCodec->mSkipCutBuffer != NULL) {
4332f3bd1972e039c6ded5154db715e5a32f1813a239Marco Nelissen            mCodec->mSkipCutBuffer->clear();
4333f3bd1972e039c6ded5154db715e5a32f1813a239Marco Nelissen        }
4334f3bd1972e039c6ded5154db715e5a32f1813a239Marco Nelissen
4335f933441648ef6a71dee783d733aac17b9508b452Andreas Huber        mCodec->changeState(mCodec->mExecutingState);
4336f933441648ef6a71dee783d733aac17b9508b452Andreas Huber    }
4337f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}
4338f933441648ef6a71dee783d733aac17b9508b452Andreas Huber
4339f933441648ef6a71dee783d733aac17b9508b452Andreas Huber}  // namespace android
4340