1318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber/*
2318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber * Copyright (C) 2009 The Android Open Source Project
3318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber *
4318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
5318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber * you may not use this file except in compliance with the License.
6318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber * You may obtain a copy of the License at
7318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber *
8318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
9318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber *
10318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber * Unless required by applicable law or agreed to in writing, software
11318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
12318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber * See the License for the specific language governing permissions and
14318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber * limitations under the License.
15318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber */
16318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
170c03d5c7c2fa4d17f7f5159e3fddd2adf6bfc923Andreas Huber//#define LOG_NDEBUG 0
18318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber#define LOG_TAG "OMXNodeInstance"
19318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber#include <utils/Log.h>
20318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
21f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <inttypes.h>
22f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
23318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber#include "../include/OMXNodeInstance.h"
24f0fb96c352f30b812a4903a1d783a715e1e817bdAndreas Huber#include "OMXMaster.h"
25f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include "GraphicBufferSource.h"
26318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
27e3ec3cec3a2e27033249ff82964d2cbd441d9873Andreas Huber#include <OMX_Component.h>
28f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <OMX_IndexExt.h>
29f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <OMX_AsString.h>
30318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
31318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber#include <binder/IMemory.h>
32f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden#include <gui/BufferQueue.h>
336c6b4d0d2b98a7ceee8b697daaf611f8df3254fbJames Dong#include <HardwareAPI.h>
34f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong#include <media/stagefright/foundation/ADebug.h>
352a3847ee1cbdaa8a65eee397a0173bb02211c459Andreas Huber#include <media/stagefright/MediaErrors.h>
36318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
37f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#include <utils/misc.h>
38f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
39f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenstatic const OMX_U32 kPortIndexInput = 0;
40f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnarstatic const OMX_U32 kPortIndexOutput = 1;
41f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
42f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOGW(fmt, ...) ALOGW("[%x:%s] " fmt, mNodeID, mName, ##__VA_ARGS__)
43f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
44f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_ERROR_IF(cond, fn, err, fmt, ...) \
45f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    ALOGE_IF(cond, #fn "(%x:%s, " fmt ") ERROR: %s(%#x)", \
46f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    mNodeID, mName, ##__VA_ARGS__, asString(err), err)
47f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_ERROR(fn, err, fmt, ...) CLOG_ERROR_IF(true, fn, err, fmt, ##__VA_ARGS__)
48f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_IF_ERROR(fn, err, fmt, ...) \
49f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_ERROR_IF((err) != OMX_ErrorNone, fn, err, fmt, ##__VA_ARGS__)
50f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
51f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOGI_(level, fn, fmt, ...) \
52f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    ALOGI_IF(DEBUG >= (level), #fn "(%x:%s, " fmt ")", mNodeID, mName, ##__VA_ARGS__)
53f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOGD_(level, fn, fmt, ...) \
54f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    ALOGD_IF(DEBUG >= (level), #fn "(%x:%s, " fmt ")", mNodeID, mName, ##__VA_ARGS__)
55f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
56f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_LIFE(fn, fmt, ...)     CLOGI_(ADebug::kDebugLifeCycle,     fn, fmt, ##__VA_ARGS__)
57f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_STATE(fn, fmt, ...)    CLOGI_(ADebug::kDebugState,         fn, fmt, ##__VA_ARGS__)
58f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_CONFIG(fn, fmt, ...)   CLOGI_(ADebug::kDebugConfig,        fn, fmt, ##__VA_ARGS__)
59f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_INTERNAL(fn, fmt, ...) CLOGD_(ADebug::kDebugInternalState, fn, fmt, ##__VA_ARGS__)
60f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
61f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_DEBUG_IF(cond, fn, fmt, ...) \
62f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    ALOGD_IF(cond, #fn "(%x, " fmt ")", mNodeID, ##__VA_ARGS__)
63f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
64f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_BUFFER(fn, fmt, ...) \
65f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_DEBUG_IF(DEBUG >= ADebug::kDebugAll, fn, fmt, ##__VA_ARGS__)
66f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define CLOG_BUMPED_BUFFER(fn, fmt, ...) \
67f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_DEBUG_IF(DEBUG_BUMP >= ADebug::kDebugAll, fn, fmt, ##__VA_ARGS__)
68f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
69f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar/* buffer formatting */
70f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define BUFFER_FMT(port, fmt, ...) "%s:%u " fmt, portString(port), (port), ##__VA_ARGS__
71f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define NEW_BUFFER_FMT(buffer_id, port, fmt, ...) \
72f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    BUFFER_FMT(port, fmt ") (#%zu => %#x", ##__VA_ARGS__, mActiveBuffers.size(), (buffer_id))
73f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
74f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define SIMPLE_BUFFER(port, size, data) BUFFER_FMT(port, "%zu@%p", (size), (data))
75f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define SIMPLE_NEW_BUFFER(buffer_id, port, size, data) \
76f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    NEW_BUFFER_FMT(buffer_id, port, "%zu@%p", (size), (data))
77f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
78f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define EMPTY_BUFFER(addr, header) "%#x [%u@%p]", \
79f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    (addr), (header)->nAllocLen, (header)->pBuffer
80f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define FULL_BUFFER(addr, header) "%#" PRIxPTR " [%u@%p (%u..+%u) f=%x ts=%lld]", \
81f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    (intptr_t)(addr), (header)->nAllocLen, (header)->pBuffer, \
82f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    (header)->nOffset, (header)->nFilledLen, (header)->nFlags, (header)->nTimeStamp
83f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
84f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define WITH_STATS_WRAPPER(fmt, ...) fmt " { IN=%zu/%zu OUT=%zu/%zu }", ##__VA_ARGS__, \
85f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    mInputBuffersWithCodec.size(), mNumPortBuffers[kPortIndexInput], \
86f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    mOutputBuffersWithCodec.size(), mNumPortBuffers[kPortIndexOutput]
87f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar// TRICKY: this is needed so formatting macros expand before substitution
88f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar#define WITH_STATS(fmt, ...) WITH_STATS_WRAPPER(fmt, ##__VA_ARGS__)
89f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
90f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnartemplate<class T>
91f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnarstatic void InitOMXParams(T *params) {
92f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    memset(params, 0, sizeof(T));
93f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    params->nSize = sizeof(T);
94f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    params->nVersion.s.nVersionMajor = 1;
95f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    params->nVersion.s.nVersionMinor = 0;
96f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    params->nVersion.s.nRevision = 0;
97f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    params->nVersion.s.nStep = 0;
98f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar}
99f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
100318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Hubernamespace android {
101318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
102318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huberstruct BufferMeta {
103318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    BufferMeta(const sp<IMemory> &mem, bool is_backup = false)
104318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        : mMem(mem),
105318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber          mIsBackup(is_backup) {
106318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    }
107318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
108318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    BufferMeta(size_t size)
109318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        : mSize(size),
110318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber          mIsBackup(false) {
111318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    }
112318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
11383750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    BufferMeta(const sp<GraphicBuffer> &graphicBuffer)
11483750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        : mGraphicBuffer(graphicBuffer),
11583750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis          mIsBackup(false) {
11683750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    }
11783750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
118318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    void CopyFromOMX(const OMX_BUFFERHEADERTYPE *header) {
119318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        if (!mIsBackup) {
120318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber            return;
121318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        }
122318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
123318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        memcpy((OMX_U8 *)mMem->pointer() + header->nOffset,
124f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                header->pBuffer + header->nOffset,
125f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                header->nFilledLen);
126318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    }
127318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
128318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    void CopyToOMX(const OMX_BUFFERHEADERTYPE *header) {
129318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        if (!mIsBackup) {
130318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber            return;
131318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        }
132318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
133318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        memcpy(header->pBuffer + header->nOffset,
134f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                (const OMX_U8 *)mMem->pointer() + header->nOffset,
135f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                header->nFilledLen);
136318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    }
137318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
138d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar    void setGraphicBuffer(const sp<GraphicBuffer> &graphicBuffer) {
139d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar        mGraphicBuffer = graphicBuffer;
140d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar    }
141d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar
142318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huberprivate:
14383750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    sp<GraphicBuffer> mGraphicBuffer;
144318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    sp<IMemory> mMem;
145318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    size_t mSize;
146318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    bool mIsBackup;
147318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
148318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    BufferMeta(const BufferMeta &);
149318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    BufferMeta &operator=(const BufferMeta &);
150318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber};
151318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
152318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber// static
153318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas HuberOMX_CALLBACKTYPE OMXNodeInstance::kCallbacks = {
154318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    &OnEvent, &OnEmptyBufferDone, &OnFillBufferDone
155318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber};
156318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
157f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnarstatic inline const char *portString(OMX_U32 portIndex) {
158f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    switch (portIndex) {
159f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        case kPortIndexInput:  return "Input";
160f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        case kPortIndexOutput: return "Output";
161f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        case ~0:               return "All";
162f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        default:               return "port";
163f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    }
164f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar}
165f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
166318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas HuberOMXNodeInstance::OMXNodeInstance(
167f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        OMX *owner, const sp<IOMXObserver> &observer, const char *name)
168318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    : mOwner(owner),
169609b815a3131d22da38b2f452faa9f89daad4039Andy Hung      mNodeID(0),
170318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber      mHandle(NULL),
171134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber      mObserver(observer),
172609b815a3131d22da38b2f452faa9f89daad4039Andy Hung      mDying(false)
173609b815a3131d22da38b2f452faa9f89daad4039Andy Hung#ifdef __LP64__
174609b815a3131d22da38b2f452faa9f89daad4039Andy Hung      , mBufferIDCount(0)
175609b815a3131d22da38b2f452faa9f89daad4039Andy Hung#endif
176609b815a3131d22da38b2f452faa9f89daad4039Andy Hung{
177f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    mName = ADebug::GetDebugName(name);
178f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    DEBUG = ADebug::GetDebugLevelFromProperty(name, "debug.stagefright.omx-debug");
179f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    ALOGV("debug level for %s is %d", name, DEBUG);
180f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    DEBUG_BUMP = DEBUG;
181f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    mNumPortBuffers[0] = 0;
182f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    mNumPortBuffers[1] = 0;
183f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    mDebugLevelBumpPendingBuffers[0] = 0;
184f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    mDebugLevelBumpPendingBuffers[1] = 0;
185318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
186318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
187318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas HuberOMXNodeInstance::~OMXNodeInstance() {
188f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    free(mName);
189f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong    CHECK(mHandle == NULL);
190318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
191318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
192318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Hubervoid OMXNodeInstance::setHandle(OMX::node_id node_id, OMX_HANDLETYPE handle) {
193318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    mNodeID = node_id;
194f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_LIFE(allocateNode, "handle=%p", handle);
195f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CHECK(mHandle == NULL);
196318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    mHandle = handle;
197318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
198318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
199f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddensp<GraphicBufferSource> OMXNodeInstance::getGraphicBufferSource() {
200f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    Mutex::Autolock autoLock(mGraphicBufferSourceLock);
201f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    return mGraphicBufferSource;
202f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden}
203f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
204f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenvoid OMXNodeInstance::setGraphicBufferSource(
205f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        const sp<GraphicBufferSource>& bufferSource) {
206f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    Mutex::Autolock autoLock(mGraphicBufferSourceLock);
207f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_INTERNAL(setGraphicBufferSource, "%p", bufferSource.get());
208f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    mGraphicBufferSource = bufferSource;
209f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden}
210f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
211318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas HuberOMX *OMXNodeInstance::owner() {
212318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return mOwner;
213318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
214318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
215318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Hubersp<IOMXObserver> OMXNodeInstance::observer() {
216318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return mObserver;
217318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
218318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
219318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas HuberOMX::node_id OMXNodeInstance::nodeID() {
220318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return mNodeID;
221318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
222318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
223318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huberstatic status_t StatusFromOMXError(OMX_ERRORTYPE err) {
2242a3847ee1cbdaa8a65eee397a0173bb02211c459Andreas Huber    switch (err) {
2252a3847ee1cbdaa8a65eee397a0173bb02211c459Andreas Huber        case OMX_ErrorNone:
2262a3847ee1cbdaa8a65eee397a0173bb02211c459Andreas Huber            return OK;
2272a3847ee1cbdaa8a65eee397a0173bb02211c459Andreas Huber        case OMX_ErrorUnsupportedSetting:
228f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        case OMX_ErrorUnsupportedIndex:
2292a3847ee1cbdaa8a65eee397a0173bb02211c459Andreas Huber            return ERROR_UNSUPPORTED;
2302a3847ee1cbdaa8a65eee397a0173bb02211c459Andreas Huber        default:
2312a3847ee1cbdaa8a65eee397a0173bb02211c459Andreas Huber            return UNKNOWN_ERROR;
2322a3847ee1cbdaa8a65eee397a0173bb02211c459Andreas Huber    }
233318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
234318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
235f0fb96c352f30b812a4903a1d783a715e1e817bdAndreas Huberstatus_t OMXNodeInstance::freeNode(OMXMaster *master) {
236f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_LIFE(freeNode, "handle=%p", mHandle);
23743e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber    static int32_t kMaxNumIterations = 10;
23843e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber
23930358faf33fb9b638257b017fadb4c5f7352d903Lajos Molnar    // exit if we have already freed the node
24030358faf33fb9b638257b017fadb4c5f7352d903Lajos Molnar    if (mHandle == NULL) {
24130358faf33fb9b638257b017fadb4c5f7352d903Lajos Molnar        return OK;
24230358faf33fb9b638257b017fadb4c5f7352d903Lajos Molnar    }
24330358faf33fb9b638257b017fadb4c5f7352d903Lajos Molnar
244d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    // Transition the node from its current state all the way down
245d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    // to "Loaded".
246d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    // This ensures that all active buffers are properly freed even
247d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    // for components that don't do this themselves on a call to
248d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    // "FreeHandle".
249d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
250134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    // The code below may trigger some more events to be dispatched
251134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    // by the OMX component - we want to ignore them as our client
252134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    // does not expect them.
253134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber    mDying = true;
254134ee6a324c35f39e3576172e4eae4c6de6eb9dcAndreas Huber
255d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    OMX_STATETYPE state;
256d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    CHECK_EQ(OMX_GetState(mHandle, &state), OMX_ErrorNone);
257d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    switch (state) {
258d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber        case OMX_StateExecuting:
259d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber        {
2603856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("forcing Executing->Idle");
261d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            sendCommand(OMX_CommandStateSet, OMX_StateIdle);
262d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            OMX_ERRORTYPE err;
26343e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber            int32_t iteration = 0;
264d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            while ((err = OMX_GetState(mHandle, &state)) == OMX_ErrorNone
265f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                    && state != OMX_StateIdle
266f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                    && state != OMX_StateInvalid) {
26743e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber                if (++iteration > kMaxNumIterations) {
268f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                    CLOGW("failed to enter Idle state (now %s(%d), aborting.",
269f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                            asString(state), state);
27043e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber                    state = OMX_StateInvalid;
27143e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber                    break;
27243e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber                }
27343e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber
274d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber                usleep(100000);
275d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            }
276d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            CHECK_EQ(err, OMX_ErrorNone);
277d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
2780d681df3b0ded2c1e335b6b5785439da4ce2c238Andreas Huber            if (state == OMX_StateInvalid) {
2790d681df3b0ded2c1e335b6b5785439da4ce2c238Andreas Huber                break;
2800d681df3b0ded2c1e335b6b5785439da4ce2c238Andreas Huber            }
2810d681df3b0ded2c1e335b6b5785439da4ce2c238Andreas Huber
282d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            // fall through
283d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber        }
284d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
285d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber        case OMX_StateIdle:
286d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber        {
2873856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("forcing Idle->Loaded");
288d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            sendCommand(OMX_CommandStateSet, OMX_StateLoaded);
289d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
290d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            freeActiveBuffers();
291d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
292d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            OMX_ERRORTYPE err;
29343e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber            int32_t iteration = 0;
294d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            while ((err = OMX_GetState(mHandle, &state)) == OMX_ErrorNone
295f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                    && state != OMX_StateLoaded
296f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                    && state != OMX_StateInvalid) {
29743e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber                if (++iteration > kMaxNumIterations) {
298f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                    CLOGW("failed to enter Loaded state (now %s(%d), aborting.",
299f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                            asString(state), state);
30043e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber                    state = OMX_StateInvalid;
30143e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber                    break;
30243e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber                }
30343e5eca7048a3b7b3ee0223b7f3cbd837ed10ae5Andreas Huber
3043856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("waiting for Loaded state...");
305d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber                usleep(100000);
306d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            }
307d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            CHECK_EQ(err, OMX_ErrorNone);
308d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
309d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            // fall through
310d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber        }
311d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
312d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber        case OMX_StateLoaded:
313fa70cad40b01627ac1c22e04cdd548ece9c2654fAndreas Huber        case OMX_StateInvalid:
314d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            break;
315d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
316d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber        default:
317f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            LOG_ALWAYS_FATAL("unknown state %s(%#x).", asString(state), state);
318d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber            break;
319d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    }
320d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
321f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    ALOGV("[%x:%s] calling destroyComponentInstance", mNodeID, mName);
322f0fb96c352f30b812a4903a1d783a715e1e817bdAndreas Huber    OMX_ERRORTYPE err = master->destroyComponentInstance(
323f0fb96c352f30b812a4903a1d783a715e1e817bdAndreas Huber            static_cast<OMX_COMPONENTTYPE *>(mHandle));
324f0fb96c352f30b812a4903a1d783a715e1e817bdAndreas Huber
325318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    mHandle = NULL;
326f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_IF_ERROR(freeNode, err, "");
327f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    free(mName);
328f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    mName = NULL;
329318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
330318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    mOwner->invalidateNodeID(mNodeID);
331609b815a3131d22da38b2f452faa9f89daad4039Andy Hung    mNodeID = 0;
332318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
3333856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("OMXNodeInstance going away.");
33447bed1a7755ed58fa5d4c0d35b20468deb83bd60Andreas Huber    delete this;
335318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
336318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return StatusFromOMXError(err);
337318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
338318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
339318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huberstatus_t OMXNodeInstance::sendCommand(
340318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        OMX_COMMANDTYPE cmd, OMX_S32 param) {
341e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    const sp<GraphicBufferSource>& bufferSource(getGraphicBufferSource());
342ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber    if (bufferSource != NULL && cmd == OMX_CommandStateSet) {
343ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber        if (param == OMX_StateIdle) {
344ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber            // Initiating transition from Executing -> Idle
345ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber            // ACodec is waiting for all buffers to be returned, do NOT
346ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber            // submit any more buffers to the codec.
347ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber            bufferSource->omxIdle();
348ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber        } else if (param == OMX_StateLoaded) {
349ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber            // Initiating transition from Idle/Executing -> Loaded
350ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber            // Buffers are about to be freed.
351ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber            bufferSource->omxLoaded();
352ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber            setGraphicBufferSource(NULL);
353ad3b7e8d40bf1c97347f1538d30bba78ca371f67Andreas Huber        }
354e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber
355e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber        // fall through
356e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber    }
357e40cda70eec141fa05cbcca1de420fdb22b98be6Andreas Huber
358318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    Mutex::Autolock autoLock(mLock);
359318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
360f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // bump internal-state debug level for 2 input and output frames past a command
361f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    {
362f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        Mutex::Autolock _l(mDebugLock);
363f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        bumpDebugLevel_l(2 /* numInputBuffers */, 2 /* numOutputBuffers */);
364f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    }
365f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
366f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    const char *paramString =
367f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        cmd == OMX_CommandStateSet ? asString((OMX_STATETYPE)param) : portString(param);
368f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_STATE(sendCommand, "%s(%d), %s(%d)", asString(cmd), cmd, paramString, param);
369318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    OMX_ERRORTYPE err = OMX_SendCommand(mHandle, cmd, param, NULL);
370f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_IF_ERROR(sendCommand, err, "%s(%d), %s(%d)", asString(cmd), cmd, paramString, param);
371318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return StatusFromOMXError(err);
372318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
373318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
374318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huberstatus_t OMXNodeInstance::getParameter(
37584333e0475bc911adc16417f4ca327c975cf6c36Andreas Huber        OMX_INDEXTYPE index, void *params, size_t /* size */) {
376318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    Mutex::Autolock autoLock(mLock);
377318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
378318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    OMX_ERRORTYPE err = OMX_GetParameter(mHandle, index, params);
379f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    OMX_INDEXEXTTYPE extIndex = (OMX_INDEXEXTTYPE)index;
380f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // some errors are expected for getParameter
381f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    if (err != OMX_ErrorNoMore) {
382f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_IF_ERROR(getParameter, err, "%s(%#x)", asString(extIndex), index);
383f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    }
384318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return StatusFromOMXError(err);
385318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
386318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
387318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huberstatus_t OMXNodeInstance::setParameter(
388f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        OMX_INDEXTYPE index, const void *params, size_t size) {
389318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    Mutex::Autolock autoLock(mLock);
390f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    OMX_INDEXEXTTYPE extIndex = (OMX_INDEXEXTTYPE)index;
391f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_CONFIG(setParameter, "%s(%#x), %zu@%p)", asString(extIndex), index, size, params);
392318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
393318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    OMX_ERRORTYPE err = OMX_SetParameter(
394318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber            mHandle, index, const_cast<void *>(params));
395f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_IF_ERROR(setParameter, err, "%s(%#x)", asString(extIndex), index);
396318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return StatusFromOMXError(err);
397318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
398318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
399318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huberstatus_t OMXNodeInstance::getConfig(
40084333e0475bc911adc16417f4ca327c975cf6c36Andreas Huber        OMX_INDEXTYPE index, void *params, size_t /* size */) {
401318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    Mutex::Autolock autoLock(mLock);
402318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
403318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    OMX_ERRORTYPE err = OMX_GetConfig(mHandle, index, params);
404f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    OMX_INDEXEXTTYPE extIndex = (OMX_INDEXEXTTYPE)index;
405f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // some errors are expected for getConfig
406f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    if (err != OMX_ErrorNoMore) {
407f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_IF_ERROR(getConfig, err, "%s(%#x)", asString(extIndex), index);
408f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    }
409318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return StatusFromOMXError(err);
410318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
411318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
412318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huberstatus_t OMXNodeInstance::setConfig(
413f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        OMX_INDEXTYPE index, const void *params, size_t size) {
414318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    Mutex::Autolock autoLock(mLock);
415f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    OMX_INDEXEXTTYPE extIndex = (OMX_INDEXEXTTYPE)index;
416f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_CONFIG(setConfig, "%s(%#x), %zu@%p)", asString(extIndex), index, size, params);
417318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
418318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    OMX_ERRORTYPE err = OMX_SetConfig(
419318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber            mHandle, index, const_cast<void *>(params));
420f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_IF_ERROR(setConfig, err, "%s(%#x)", asString(extIndex), index);
421318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return StatusFromOMXError(err);
422b1d666f5cb555d135eb69e005e88a03330bbb54cJamie Gennis}
423b1d666f5cb555d135eb69e005e88a03330bbb54cJamie Gennis
424b1d666f5cb555d135eb69e005e88a03330bbb54cJamie Gennisstatus_t OMXNodeInstance::getState(OMX_STATETYPE* state) {
425b1d666f5cb555d135eb69e005e88a03330bbb54cJamie Gennis    Mutex::Autolock autoLock(mLock);
426b1d666f5cb555d135eb69e005e88a03330bbb54cJamie Gennis
427b1d666f5cb555d135eb69e005e88a03330bbb54cJamie Gennis    OMX_ERRORTYPE err = OMX_GetState(mHandle, state);
428f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_IF_ERROR(getState, err, "");
429b1d666f5cb555d135eb69e005e88a03330bbb54cJamie Gennis    return StatusFromOMXError(err);
430318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
431318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
43283750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennisstatus_t OMXNodeInstance::enableGraphicBuffers(
43383750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        OMX_U32 portIndex, OMX_BOOL enable) {
43483750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    Mutex::Autolock autoLock(mLock);
435f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_CONFIG(enableGraphicBuffers, "%s:%u, %d", portString(portIndex), portIndex, enable);
436ce18d7d85a78ac6642624fef1b5831eff4c72d56Jamie Gennis    OMX_STRING name = const_cast<OMX_STRING>(
437ce18d7d85a78ac6642624fef1b5831eff4c72d56Jamie Gennis            "OMX.google.android.index.enableAndroidNativeBuffers");
43883750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
43983750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    OMX_INDEXTYPE index;
440ce18d7d85a78ac6642624fef1b5831eff4c72d56Jamie Gennis    OMX_ERRORTYPE err = OMX_GetExtensionIndex(mHandle, name, &index);
44183750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
44283750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    if (err != OMX_ErrorNone) {
443f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR_IF(enable, getExtensionIndex, err, "%s", name);
44483750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        return StatusFromOMXError(err);
44583750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    }
44683750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
447f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    EnableAndroidNativeBuffersParams params;
448f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    InitOMXParams(&params);
449f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    params.nPortIndex = portIndex;
450f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    params.enable = enable;
45183750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
45283750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    err = OMX_SetParameter(mHandle, index, &params);
453f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_IF_ERROR(setParameter, err, "%s(%#x): %s:%u en=%d", name, index,
454f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            portString(portIndex), portIndex, enable);
455f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    return StatusFromOMXError(err);
45683750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis}
45783750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
458e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennisstatus_t OMXNodeInstance::getGraphicBufferUsage(
459e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis        OMX_U32 portIndex, OMX_U32* usage) {
460e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis    Mutex::Autolock autoLock(mLock);
461e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis
462e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis    OMX_INDEXTYPE index;
463ce18d7d85a78ac6642624fef1b5831eff4c72d56Jamie Gennis    OMX_STRING name = const_cast<OMX_STRING>(
464ce18d7d85a78ac6642624fef1b5831eff4c72d56Jamie Gennis            "OMX.google.android.index.getAndroidNativeBufferUsage");
465ce18d7d85a78ac6642624fef1b5831eff4c72d56Jamie Gennis    OMX_ERRORTYPE err = OMX_GetExtensionIndex(mHandle, name, &index);
466e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis
467e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis    if (err != OMX_ErrorNone) {
468f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR(getExtensionIndex, err, "%s", name);
469e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis        return StatusFromOMXError(err);
470e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis    }
471e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis
472f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    GetAndroidNativeBufferUsageParams params;
473f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    InitOMXParams(&params);
474f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    params.nPortIndex = portIndex;
475e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis
476e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis    err = OMX_GetParameter(mHandle, index, &params);
477e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis    if (err != OMX_ErrorNone) {
478f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR(getParameter, err, "%s(%#x): %s:%u", name, index,
479f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                portString(portIndex), portIndex);
480f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        return StatusFromOMXError(err);
481e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis    }
482e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis
483e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis    *usage = params.nUsage;
484e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis
485e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis    return OK;
486e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis}
487e2ce6458659c6e1bad420357b61dc10cd8bbe2abJamie Gennis
488e870772a78ffe08b1c14a791e368f1499f1be0f3James Dongstatus_t OMXNodeInstance::storeMetaDataInBuffers(
489e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong        OMX_U32 portIndex,
490e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong        OMX_BOOL enable) {
491e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong    Mutex::Autolock autolock(mLock);
492f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_CONFIG(storeMetaDataInBuffers, "%s:%u en:%d", portString(portIndex), portIndex, enable);
493512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar    return storeMetaDataInBuffers_l(
494512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar            portIndex, enable,
495512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar            OMX_FALSE /* useGraphicBuffer */, NULL /* usingGraphicBufferInMetadata */);
496f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden}
497e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong
498f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFaddenstatus_t OMXNodeInstance::storeMetaDataInBuffers_l(
499f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        OMX_U32 portIndex,
500512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar        OMX_BOOL enable,
501512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar        OMX_BOOL useGraphicBuffer,
502512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar        OMX_BOOL *usingGraphicBufferInMetadata) {
503e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong    OMX_INDEXTYPE index;
504e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong    OMX_STRING name = const_cast<OMX_STRING>(
505e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong            "OMX.google.android.index.storeMetaDataInBuffers");
506e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong
507512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar    OMX_STRING graphicBufferName = const_cast<OMX_STRING>(
508512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar            "OMX.google.android.index.storeGraphicBufferInMetaData");
509512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar    if (usingGraphicBufferInMetadata == NULL) {
510512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar        usingGraphicBufferInMetadata = &useGraphicBuffer;
511512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar    }
512512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar
513512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar    OMX_ERRORTYPE err =
514512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar        (useGraphicBuffer && portIndex == kPortIndexInput)
515512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar                ? OMX_GetExtensionIndex(mHandle, graphicBufferName, &index)
516512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar                : OMX_ErrorBadParameter;
517512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar    if (err == OMX_ErrorNone) {
518512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar        *usingGraphicBufferInMetadata = OMX_TRUE;
519f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        name = graphicBufferName;
520512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar    } else {
521512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar        err = OMX_GetExtensionIndex(mHandle, name, &index);
522512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar    }
523512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar
524f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    OMX_ERRORTYPE xerr = err;
525f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    if (err == OMX_ErrorNone) {
526f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        StoreMetaDataInBuffersParams params;
527f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        InitOMXParams(&params);
528f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        params.nPortIndex = portIndex;
529f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        params.bStoreMetaData = enable;
530e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong
531f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        err = OMX_SetParameter(mHandle, index, &params);
532f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    }
533e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong
534f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    // don't log loud error if component does not support metadata mode on the output
535f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    if (err != OMX_ErrorNone) {
536512e979284de984427e5b2f73b9054ae1b5e2b0aLajos Molnar        *usingGraphicBufferInMetadata = OMX_FALSE;
537f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        if (err == OMX_ErrorUnsupportedIndex && portIndex == kPortIndexOutput) {
538f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            CLOGW("component does not support metadata mode; using fallback");
539f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        } else if (xerr != OMX_ErrorNone) {
540f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            CLOG_ERROR(getExtensionIndex, xerr, "%s", name);
541f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        } else {
542f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            CLOG_ERROR(setParameter, err, "%s(%#x): %s:%u en=%d GB=%d", name, index,
543f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                    portString(portIndex), portIndex, enable, useGraphicBuffer);
544f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        }
545e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong    }
546f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    return StatusFromOMXError(err);
547e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong}
548e870772a78ffe08b1c14a791e368f1499f1be0f3James Dong
54956ce726019f700a95ce5b45beebceadae4836e30Lajos Molnarstatus_t OMXNodeInstance::prepareForAdaptivePlayback(
55056ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar        OMX_U32 portIndex, OMX_BOOL enable, OMX_U32 maxFrameWidth,
55156ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar        OMX_U32 maxFrameHeight) {
55256ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    Mutex::Autolock autolock(mLock);
553f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_CONFIG(prepareForAdaptivePlayback, "%s:%u en=%d max=%ux%u",
554f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            portString(portIndex), portIndex, enable, maxFrameWidth, maxFrameHeight);
55556ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar
55656ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    OMX_INDEXTYPE index;
55756ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    OMX_STRING name = const_cast<OMX_STRING>(
55856ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar            "OMX.google.android.index.prepareForAdaptivePlayback");
55956ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar
56056ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    OMX_ERRORTYPE err = OMX_GetExtensionIndex(mHandle, name, &index);
56156ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    if (err != OMX_ErrorNone) {
562f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR_IF(enable, getExtensionIndex, err, "%s", name);
56356ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar        return StatusFromOMXError(err);
56456ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    }
56556ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar
56656ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    PrepareForAdaptivePlaybackParams params;
567f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    InitOMXParams(&params);
56856ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    params.nPortIndex = portIndex;
56956ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    params.bEnable = enable;
57056ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    params.nMaxFrameWidth = maxFrameWidth;
57156ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar    params.nMaxFrameHeight = maxFrameHeight;
572f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar
573f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    err = OMX_SetParameter(mHandle, index, &params);
574f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_IF_ERROR(setParameter, err, "%s(%#x): %s:%u en=%d max=%ux%u", name, index,
575f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            portString(portIndex), portIndex, enable, maxFrameWidth, maxFrameHeight);
576f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    return StatusFromOMXError(err);
57756ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar}
57856ce726019f700a95ce5b45beebceadae4836e30Lajos Molnar
5795a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachadstatus_t OMXNodeInstance::configureVideoTunnelMode(
5805a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad        OMX_U32 portIndex, OMX_BOOL tunneled, OMX_U32 audioHwSync,
5815a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad        native_handle_t **sidebandHandle) {
5825a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    Mutex::Autolock autolock(mLock);
583f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_CONFIG(configureVideoTunnelMode, "%s:%u tun=%d sync=%u",
584f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            portString(portIndex), portIndex, tunneled, audioHwSync);
5855a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad
5865a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    OMX_INDEXTYPE index;
5875a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    OMX_STRING name = const_cast<OMX_STRING>(
5885a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad            "OMX.google.android.index.configureVideoTunnelMode");
5895a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad
5905a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    OMX_ERRORTYPE err = OMX_GetExtensionIndex(mHandle, name, &index);
5915a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    if (err != OMX_ErrorNone) {
592f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR_IF(tunneled, getExtensionIndex, err, "%s", name);
5935a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad        return StatusFromOMXError(err);
5945a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    }
5955a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad
5965a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    ConfigureVideoTunnelModeParams tunnelParams;
597f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    InitOMXParams(&tunnelParams);
5985a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    tunnelParams.nPortIndex = portIndex;
5995a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    tunnelParams.bTunneled = tunneled;
6005a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    tunnelParams.nAudioHwSync = audioHwSync;
6015a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    err = OMX_SetParameter(mHandle, index, &tunnelParams);
6025a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    if (err != OMX_ErrorNone) {
603f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR(setParameter, err, "%s(%#x): %s:%u tun=%d sync=%u", name, index,
604f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                portString(portIndex), portIndex, tunneled, audioHwSync);
605f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        return StatusFromOMXError(err);
6065a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    }
6075a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad
6085a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    err = OMX_GetParameter(mHandle, index, &tunnelParams);
6095a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    if (err != OMX_ErrorNone) {
610f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR(getParameter, err, "%s(%#x): %s:%u tun=%d sync=%u", name, index,
611f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                portString(portIndex), portIndex, tunneled, audioHwSync);
612f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        return StatusFromOMXError(err);
6135a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    }
6145a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    if (sidebandHandle) {
6155a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad        *sidebandHandle = (native_handle_t*)tunnelParams.pSidebandWindow;
6165a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad    }
6175a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad
618f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    return OK;
6195a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad}
6205a446aafff3020d607ad6fb14cc7ae76dd8f7947Rachad
621318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huberstatus_t OMXNodeInstance::useBuffer(
622318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        OMX_U32 portIndex, const sp<IMemory> &params,
623318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        OMX::buffer_id *buffer) {
624318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    Mutex::Autolock autoLock(mLock);
625318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
626318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    BufferMeta *buffer_meta = new BufferMeta(params);
627318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
628318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    OMX_BUFFERHEADERTYPE *header;
629318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
630318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    OMX_ERRORTYPE err = OMX_UseBuffer(
631318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber            mHandle, &header, portIndex, buffer_meta,
632318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber            params->size(), static_cast<OMX_U8 *>(params->pointer()));
633318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
634318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    if (err != OMX_ErrorNone) {
635f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR(useBuffer, err, SIMPLE_BUFFER(portIndex, params->size(), params->pointer()));
636318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
637318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        delete buffer_meta;
638318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        buffer_meta = NULL;
639318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
640318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber        *buffer = 0;
641318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
642f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        return StatusFromOMXError(err);
643318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    }
644318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
64503b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber    CHECK_EQ(header->pAppPrivate, buffer_meta);
64603b268eac37ca2589bfff0bf58daf79d29cc14f4Andreas Huber
647609b815a3131d22da38b2f452faa9f89daad4039Andy Hung    *buffer = makeBufferID(header);
648318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
649d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber    addActiveBuffer(portIndex, *buffer);
650d6703ab22c85b43fdb2deb78a37e51465c902a5fAndreas Huber
651f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    sp<GraphicBufferSource> bufferSource(getGraphicBufferSource());
652f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    if (bufferSource != NULL && portIndex == kPortIndexInput) {
653f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden        bufferSource->addCodecBuffer(header);
654f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden    }
655f779bb50d9746d9526541c3e6dcdf619cac941b7Andy McFadden
656f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_BUFFER(useBuffer, NEW_BUFFER_FMT(
657f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            *buffer, portIndex, "%zu@%p", params->size(), params->pointer()));
658318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    return OK;
659318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber}
660318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber
661c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajanstatus_t OMXNodeInstance::useGraphicBuffer2_l(
662c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan        OMX_U32 portIndex, const sp<GraphicBuffer>& graphicBuffer,
663c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan        OMX::buffer_id *buffer) {
664c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
665c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    // port definition
666c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    OMX_PARAM_PORTDEFINITIONTYPE def;
667f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    InitOMXParams(&def);
668c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    def.nPortIndex = portIndex;
669c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    OMX_ERRORTYPE err = OMX_GetParameter(mHandle, OMX_IndexParamPortDefinition, &def);
670f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    if (err != OMX_ErrorNone) {
671f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        OMX_INDEXTYPE index = OMX_IndexParamPortDefinition;
672f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR(getParameter, err, "%s(%#x): %s:%u",
673f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                asString(index), index, portString(portIndex), portIndex);
674f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        return UNKNOWN_ERROR;
675c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    }
676c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
677c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    BufferMeta *bufferMeta = new BufferMeta(graphicBuffer);
678c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
679c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    OMX_BUFFERHEADERTYPE *header = NULL;
680c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    OMX_U8* bufferHandle = const_cast<OMX_U8*>(
681c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            reinterpret_cast<const OMX_U8*>(graphicBuffer->handle));
682c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
683c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    err = OMX_UseBuffer(
684c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            mHandle,
685c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            &header,
686c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            portIndex,
687c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            bufferMeta,
688c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            def.nBufferSize,
689c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            bufferHandle);
690c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
691c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    if (err != OMX_ErrorNone) {
692f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR(useBuffer, err, BUFFER_FMT(portIndex, "%u@%p", def.nBufferSize, bufferHandle));
693c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan        delete bufferMeta;
694c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan        bufferMeta = NULL;
695c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan        *buffer = 0;
696f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        return StatusFromOMXError(err);
697c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    }
698c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
699c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    CHECK_EQ(header->pBuffer, bufferHandle);
700c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    CHECK_EQ(header->pAppPrivate, bufferMeta);
701c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
702609b815a3131d22da38b2f452faa9f89daad4039Andy Hung    *buffer = makeBufferID(header);
703c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
704c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    addActiveBuffer(portIndex, *buffer);
705f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_BUFFER(useGraphicBuffer2, NEW_BUFFER_FMT(
706f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            *buffer, portIndex, "%u@%p", def.nBufferSize, bufferHandle));
707c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    return OK;
708c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan}
709c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
710c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan// XXX: This function is here for backwards compatibility.  Once the OMX
711c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan// implementations have been updated this can be removed and useGraphicBuffer2
712c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan// can be renamed to useGraphicBuffer.
71383750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennisstatus_t OMXNodeInstance::useGraphicBuffer(
71483750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        OMX_U32 portIndex, const sp<GraphicBuffer>& graphicBuffer,
71583750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        OMX::buffer_id *buffer) {
71683750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    Mutex::Autolock autoLock(mLock);
71783750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
718c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    // See if the newer version of the extension is present.
71983750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    OMX_INDEXTYPE index;
720c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    if (OMX_GetExtensionIndex(
721c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            mHandle,
722c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            const_cast<OMX_STRING>("OMX.google.android.index.useAndroidNativeBuffer2"),
723c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan            &index) == OMX_ErrorNone) {
724c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan        return useGraphicBuffer2_l(portIndex, graphicBuffer, buffer);
725c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan    }
726c5a57efb706ec79032fc09c43b16d11ed0876604Anu Sundararajan
727ce18d7d85a78ac6642624fef1b5831eff4c72d56Jamie Gennis    OMX_STRING name = const_cast<OMX_STRING>(
728ce18d7d85a78ac6642624fef1b5831eff4c72d56Jamie Gennis        "OMX.google.android.index.useAndroidNativeBuffer");
729ce18d7d85a78ac6642624fef1b5831eff4c72d56Jamie Gennis    OMX_ERRORTYPE err = OMX_GetExtensionIndex(mHandle, name, &index);
73083750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    if (err != OMX_ErrorNone) {
731f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR(getExtensionIndex, err, "%s", name);
73283750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        return StatusFromOMXError(err);
73383750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    }
73483750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
73583750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    BufferMeta *bufferMeta = new BufferMeta(graphicBuffer);
73683750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
73783750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    OMX_BUFFERHEADERTYPE *header;
73883750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
73983750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    OMX_VERSIONTYPE ver;
74083750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    ver.s.nVersionMajor = 1;
74183750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    ver.s.nVersionMinor = 0;
74283750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    ver.s.nRevision = 0;
74383750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    ver.s.nStep = 0;
74483750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    UseAndroidNativeBufferParams params = {
74583750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        sizeof(UseAndroidNativeBufferParams), ver, portIndex, bufferMeta,
74683750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        &header, graphicBuffer,
74783750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    };
74883750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
74983750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    err = OMX_SetParameter(mHandle, index, &params);
75083750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
75183750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    if (err != OMX_ErrorNone) {
752f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        CLOG_ERROR(setParameter, err, "%s(%#x): %s:%u meta=%p GB=%p", name, index,
753f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar                portString(portIndex), portIndex, bufferMeta, graphicBuffer->handle);
75483750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
75583750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        delete bufferMeta;
75683750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        bufferMeta = NULL;
75783750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
75883750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis        *buffer = 0;
75983750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
760f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        return StatusFromOMXError(err);
76183750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    }
76283750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
76383750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    CHECK_EQ(header->pAppPrivate, bufferMeta);
76483750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
765609b815a3131d22da38b2f452faa9f89daad4039Andy Hung    *buffer = makeBufferID(header);
76683750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
76783750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    addActiveBuffer(portIndex, *buffer);
768f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_BUFFER(useGraphicBuffer, NEW_BUFFER_FMT(
769f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            *buffer, portIndex, "GB=%p", graphicBuffer->handle));
77083750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis    return OK;
77183750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis}
77283750eaf5a3f38c243a9e7eb81d4b2421e3a0d88Jamie Gennis
773d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnarstatus_t OMXNodeInstance::updateGraphicBufferInMeta(
774f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar        OMX_U32 portIndex, const sp<GraphicBuffer>& graphicBuffer,
775d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar        OMX::buffer_id buffer) {
776d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar    Mutex::Autolock autoLock(mLock);
777d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar
778609b815a3131d22da38b2f452faa9f89daad4039Andy Hung    OMX_BUFFERHEADERTYPE *header = findBufferHeader(buffer);
779d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar    VideoDecoderOutputMetaData *metadata =
780d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar        (VideoDecoderOutputMetaData *)(header->pBuffer);
781d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar    BufferMeta *bufferMeta = (BufferMeta *)(header->pAppPrivate);
782d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar    bufferMeta->setGraphicBuffer(graphicBuffer);
783d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar    metadata->eType = kMetadataBufferTypeGrallocSource;
784d0715867861c216e88a4a7523b6da8a3cb128724Lajos Molnar    metadata->pHandle = graphicBuffer->handle;
785f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar    CLOG_BUFFER(updateGraphicBufferInMeta, "%s:%u, %#x := %p",
786f296e2b262d2a8f7c570eaed454a28cca99eb976Lajos Molnar            portString(portIndex), portIndex, buffer, graphicBuffer->handle);
787