Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "OMX" |
| 19 | #include <utils/Log.h> |
| 20 | |
Andreas Huber | b0caf94 | 2009-12-03 11:39:54 -0800 | [diff] [blame] | 21 | #include <dlfcn.h> |
Andreas Huber | 122ff04 | 2010-03-15 09:21:46 -0700 | [diff] [blame] | 22 | |
| 23 | #include <sys/prctl.h> |
Andreas Huber | 98b2956 | 2010-03-12 14:33:12 -0800 | [diff] [blame] | 24 | #include <sys/resource.h> |
Andreas Huber | b0caf94 | 2009-12-03 11:39:54 -0800 | [diff] [blame] | 25 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 26 | #include "../include/OMX.h" |
Andreas Huber | 1de1316 | 2009-07-31 11:52:50 -0700 | [diff] [blame] | 27 | #include "OMXRenderer.h" |
| 28 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 29 | #include "../include/OMXNodeInstance.h" |
Andreas Huber | bd7b43b | 2009-10-13 10:22:55 -0700 | [diff] [blame] | 30 | #include "../include/SoftwareRenderer.h" |
Andreas Huber | bd7b43b | 2009-10-13 10:22:55 -0700 | [diff] [blame] | 31 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 32 | #include <binder/IMemory.h> |
Andreas Huber | b5ceb9e | 2009-08-26 14:48:20 -0700 | [diff] [blame] | 33 | #include <media/stagefright/MediaDebug.h> |
Andreas Huber | 1de1316 | 2009-07-31 11:52:50 -0700 | [diff] [blame] | 34 | #include <media/stagefright/VideoRenderer.h> |
Andreas Huber | 98b2956 | 2010-03-12 14:33:12 -0800 | [diff] [blame] | 35 | #include <utils/threads.h> |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 36 | |
Andreas Huber | b0caf94 | 2009-12-03 11:39:54 -0800 | [diff] [blame] | 37 | #include "OMXMaster.h" |
| 38 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 39 | #include <OMX_Component.h> |
| 40 | |
| 41 | namespace android { |
| 42 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 43 | //////////////////////////////////////////////////////////////////////////////// |
| 44 | |
| 45 | struct OMX::CallbackDispatcher : public RefBase { |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 46 | CallbackDispatcher(OMXNodeInstance *owner); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 47 | |
| 48 | void post(const omx_message &msg); |
| 49 | |
| 50 | protected: |
| 51 | virtual ~CallbackDispatcher(); |
| 52 | |
| 53 | private: |
| 54 | Mutex mLock; |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 55 | |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 56 | OMXNodeInstance *mOwner; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 57 | bool mDone; |
| 58 | Condition mQueueChanged; |
| 59 | List<omx_message> mQueue; |
| 60 | |
| 61 | pthread_t mThread; |
| 62 | |
| 63 | void dispatch(const omx_message &msg); |
| 64 | |
| 65 | static void *ThreadWrapper(void *me); |
| 66 | void threadEntry(); |
| 67 | |
| 68 | CallbackDispatcher(const CallbackDispatcher &); |
| 69 | CallbackDispatcher &operator=(const CallbackDispatcher &); |
| 70 | }; |
| 71 | |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 72 | OMX::CallbackDispatcher::CallbackDispatcher(OMXNodeInstance *owner) |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 73 | : mOwner(owner), |
| 74 | mDone(false) { |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 75 | pthread_attr_t attr; |
| 76 | pthread_attr_init(&attr); |
| 77 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 78 | |
| 79 | pthread_create(&mThread, &attr, ThreadWrapper, this); |
| 80 | |
| 81 | pthread_attr_destroy(&attr); |
| 82 | } |
| 83 | |
| 84 | OMX::CallbackDispatcher::~CallbackDispatcher() { |
| 85 | { |
| 86 | Mutex::Autolock autoLock(mLock); |
| 87 | |
| 88 | mDone = true; |
| 89 | mQueueChanged.signal(); |
| 90 | } |
| 91 | |
| 92 | void *dummy; |
| 93 | pthread_join(mThread, &dummy); |
| 94 | } |
| 95 | |
| 96 | void OMX::CallbackDispatcher::post(const omx_message &msg) { |
| 97 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 98b2956 | 2010-03-12 14:33:12 -0800 | [diff] [blame] | 98 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 99 | mQueue.push_back(msg); |
| 100 | mQueueChanged.signal(); |
| 101 | } |
| 102 | |
| 103 | void OMX::CallbackDispatcher::dispatch(const omx_message &msg) { |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 104 | if (mOwner == NULL) { |
Andreas Huber | 3085c83 | 2009-10-26 16:11:54 -0700 | [diff] [blame] | 105 | LOGV("Would have dispatched a message to a node that's already gone."); |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 106 | return; |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 107 | } |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 108 | mOwner->onMessage(msg); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // static |
| 112 | void *OMX::CallbackDispatcher::ThreadWrapper(void *me) { |
| 113 | static_cast<CallbackDispatcher *>(me)->threadEntry(); |
| 114 | |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | void OMX::CallbackDispatcher::threadEntry() { |
Andreas Huber | 98b2956 | 2010-03-12 14:33:12 -0800 | [diff] [blame] | 119 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
| 120 | prctl(PR_SET_NAME, (unsigned long)"OMXCallbackDisp", 0, 0, 0); |
| 121 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 122 | for (;;) { |
| 123 | omx_message msg; |
| 124 | |
| 125 | { |
| 126 | Mutex::Autolock autoLock(mLock); |
| 127 | while (!mDone && mQueue.empty()) { |
| 128 | mQueueChanged.wait(mLock); |
| 129 | } |
| 130 | |
| 131 | if (mDone) { |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | msg = *mQueue.begin(); |
| 136 | mQueue.erase(mQueue.begin()); |
| 137 | } |
| 138 | |
| 139 | dispatch(msg); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | //////////////////////////////////////////////////////////////////////////////// |
| 144 | |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 145 | OMX::OMX() |
Andreas Huber | b0caf94 | 2009-12-03 11:39:54 -0800 | [diff] [blame] | 146 | : mMaster(new OMXMaster), |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 147 | mNodeCounter(0) { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Andreas Huber | b0caf94 | 2009-12-03 11:39:54 -0800 | [diff] [blame] | 150 | OMX::~OMX() { |
| 151 | delete mMaster; |
| 152 | mMaster = NULL; |
| 153 | } |
| 154 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 155 | void OMX::binderDied(const wp<IBinder> &the_late_who) { |
| 156 | OMXNodeInstance *instance; |
| 157 | |
| 158 | { |
| 159 | Mutex::Autolock autoLock(mLock); |
| 160 | |
| 161 | ssize_t index = mLiveNodes.indexOfKey(the_late_who); |
| 162 | CHECK(index >= 0); |
| 163 | |
| 164 | instance = mLiveNodes.editValueAt(index); |
| 165 | mLiveNodes.removeItemsAt(index); |
| 166 | |
Andreas Huber | 6615def | 2010-07-07 08:20:49 -0700 | [diff] [blame] | 167 | index = mDispatchers.indexOfKey(instance->nodeID()); |
| 168 | CHECK(index >= 0); |
| 169 | mDispatchers.removeItemsAt(index); |
| 170 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 171 | invalidateNodeID_l(instance->nodeID()); |
| 172 | } |
| 173 | |
Andreas Huber | fef6435 | 2009-12-04 12:52:40 -0800 | [diff] [blame] | 174 | instance->onObserverDied(mMaster); |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Andreas Huber | f1fe064 | 2010-01-15 15:28:19 -0800 | [diff] [blame] | 177 | bool OMX::livesLocally(pid_t pid) { |
| 178 | return pid == getpid(); |
| 179 | } |
| 180 | |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 181 | status_t OMX::listNodes(List<ComponentInfo> *list) { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 182 | list->clear(); |
| 183 | |
| 184 | OMX_U32 index = 0; |
| 185 | char componentName[256]; |
Andreas Huber | b0caf94 | 2009-12-03 11:39:54 -0800 | [diff] [blame] | 186 | while (mMaster->enumerateComponents( |
| 187 | componentName, sizeof(componentName), index) == OMX_ErrorNone) { |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 188 | list->push_back(ComponentInfo()); |
| 189 | ComponentInfo &info = *--list->end(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 190 | |
Andreas Huber | 2ea14e2 | 2009-12-16 09:30:55 -0800 | [diff] [blame] | 191 | info.mName = componentName; |
| 192 | |
| 193 | Vector<String8> roles; |
| 194 | OMX_ERRORTYPE err = |
| 195 | mMaster->getRolesOfComponent(componentName, &roles); |
| 196 | |
| 197 | if (err == OMX_ErrorNone) { |
| 198 | for (OMX_U32 i = 0; i < roles.size(); ++i) { |
| 199 | info.mRoles.push_back(roles[i]); |
| 200 | } |
| 201 | } |
Andreas Huber | c7e91ee | 2009-12-15 15:22:08 -0800 | [diff] [blame] | 202 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 203 | ++index; |
| 204 | } |
| 205 | |
| 206 | return OK; |
| 207 | } |
| 208 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 209 | status_t OMX::allocateNode( |
| 210 | const char *name, const sp<IOMXObserver> &observer, node_id *node) { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 211 | Mutex::Autolock autoLock(mLock); |
| 212 | |
| 213 | *node = 0; |
| 214 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 215 | OMXNodeInstance *instance = new OMXNodeInstance(this, observer); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 216 | |
Andreas Huber | b0caf94 | 2009-12-03 11:39:54 -0800 | [diff] [blame] | 217 | OMX_COMPONENTTYPE *handle; |
| 218 | OMX_ERRORTYPE err = mMaster->makeComponentInstance( |
| 219 | name, &OMXNodeInstance::kCallbacks, |
| 220 | instance, &handle); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 221 | |
| 222 | if (err != OMX_ErrorNone) { |
Andreas Huber | 53a76bd | 2009-10-06 16:20:44 -0700 | [diff] [blame] | 223 | LOGV("FAILED to allocate omx component '%s'", name); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 224 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 225 | instance->onGetHandleFailed(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 226 | |
| 227 | return UNKNOWN_ERROR; |
| 228 | } |
| 229 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 230 | *node = makeNodeID(instance); |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 231 | mDispatchers.add(*node, new CallbackDispatcher(instance)); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 232 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 233 | instance->setHandle(*node, handle); |
| 234 | |
| 235 | mLiveNodes.add(observer->asBinder(), instance); |
| 236 | observer->asBinder()->linkToDeath(this); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 237 | |
| 238 | return OK; |
| 239 | } |
| 240 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 241 | status_t OMX::freeNode(node_id node) { |
| 242 | OMXNodeInstance *instance = findInstance(node); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 243 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 244 | ssize_t index = mLiveNodes.indexOfKey(instance->observer()->asBinder()); |
| 245 | CHECK(index >= 0); |
| 246 | mLiveNodes.removeItemsAt(index); |
Andreas Huber | 6615def | 2010-07-07 08:20:49 -0700 | [diff] [blame] | 247 | |
Andreas Huber | f98197a | 2010-09-17 11:49:39 -0700 | [diff] [blame] | 248 | instance->observer()->asBinder()->unlinkToDeath(this); |
| 249 | |
| 250 | status_t err = instance->freeNode(mMaster); |
| 251 | |
Andreas Huber | 6615def | 2010-07-07 08:20:49 -0700 | [diff] [blame] | 252 | index = mDispatchers.indexOfKey(node); |
| 253 | CHECK(index >= 0); |
| 254 | mDispatchers.removeItemsAt(index); |
| 255 | |
Andreas Huber | f98197a | 2010-09-17 11:49:39 -0700 | [diff] [blame] | 256 | return err; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 259 | status_t OMX::sendCommand( |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 260 | node_id node, OMX_COMMANDTYPE cmd, OMX_S32 param) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 261 | return findInstance(node)->sendCommand(cmd, param); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 264 | status_t OMX::getParameter( |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 265 | node_id node, OMX_INDEXTYPE index, |
| 266 | void *params, size_t size) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 267 | return findInstance(node)->getParameter( |
| 268 | index, params, size); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 271 | status_t OMX::setParameter( |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 272 | node_id node, OMX_INDEXTYPE index, |
| 273 | const void *params, size_t size) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 274 | return findInstance(node)->setParameter( |
| 275 | index, params, size); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 278 | status_t OMX::getConfig( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 279 | node_id node, OMX_INDEXTYPE index, |
| 280 | void *params, size_t size) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 281 | return findInstance(node)->getConfig( |
| 282 | index, params, size); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 285 | status_t OMX::setConfig( |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 286 | node_id node, OMX_INDEXTYPE index, |
| 287 | const void *params, size_t size) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 288 | return findInstance(node)->setConfig( |
| 289 | index, params, size); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Jamie Gennis | 33a7814 | 2010-08-30 16:48:38 -0700 | [diff] [blame] | 292 | status_t OMX::enableGraphicBuffers( |
| 293 | node_id node, OMX_U32 port_index, OMX_BOOL enable) { |
| 294 | return findInstance(node)->enableGraphicBuffers(port_index, enable); |
| 295 | } |
| 296 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 297 | status_t OMX::useBuffer( |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 298 | node_id node, OMX_U32 port_index, const sp<IMemory> ¶ms, |
| 299 | buffer_id *buffer) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 300 | return findInstance(node)->useBuffer( |
| 301 | port_index, params, buffer); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Jamie Gennis | 33a7814 | 2010-08-30 16:48:38 -0700 | [diff] [blame] | 304 | status_t OMX::useGraphicBuffer( |
| 305 | node_id node, OMX_U32 port_index, |
| 306 | const sp<GraphicBuffer> &graphicBuffer, buffer_id *buffer) { |
| 307 | return findInstance(node)->useGraphicBuffer( |
| 308 | port_index, graphicBuffer, buffer); |
| 309 | } |
| 310 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 311 | status_t OMX::allocateBuffer( |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 312 | node_id node, OMX_U32 port_index, size_t size, |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 313 | buffer_id *buffer, void **buffer_data) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 314 | return findInstance(node)->allocateBuffer( |
Andreas Huber | c712b9f | 2010-01-20 15:05:46 -0800 | [diff] [blame] | 315 | port_index, size, buffer, buffer_data); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 318 | status_t OMX::allocateBufferWithBackup( |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 319 | node_id node, OMX_U32 port_index, const sp<IMemory> ¶ms, |
| 320 | buffer_id *buffer) { |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 321 | return findInstance(node)->allocateBufferWithBackup( |
| 322 | port_index, params, buffer); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 325 | status_t OMX::freeBuffer(node_id node, OMX_U32 port_index, buffer_id buffer) { |
| 326 | return findInstance(node)->freeBuffer( |
| 327 | port_index, buffer); |
| 328 | } |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 329 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 330 | status_t OMX::fillBuffer(node_id node, buffer_id buffer) { |
| 331 | return findInstance(node)->fillBuffer(buffer); |
| 332 | } |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 333 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 334 | status_t OMX::emptyBuffer( |
| 335 | node_id node, |
| 336 | buffer_id buffer, |
| 337 | OMX_U32 range_offset, OMX_U32 range_length, |
| 338 | OMX_U32 flags, OMX_TICKS timestamp) { |
| 339 | return findInstance(node)->emptyBuffer( |
| 340 | buffer, range_offset, range_length, flags, timestamp); |
| 341 | } |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 342 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 343 | status_t OMX::getExtensionIndex( |
| 344 | node_id node, |
| 345 | const char *parameter_name, |
| 346 | OMX_INDEXTYPE *index) { |
| 347 | return findInstance(node)->getExtensionIndex( |
| 348 | parameter_name, index); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | OMX_ERRORTYPE OMX::OnEvent( |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 352 | node_id node, |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 353 | OMX_IN OMX_EVENTTYPE eEvent, |
| 354 | OMX_IN OMX_U32 nData1, |
| 355 | OMX_IN OMX_U32 nData2, |
| 356 | OMX_IN OMX_PTR pEventData) { |
| 357 | LOGV("OnEvent(%d, %ld, %ld)", eEvent, nData1, nData2); |
| 358 | |
| 359 | omx_message msg; |
| 360 | msg.type = omx_message::EVENT; |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 361 | msg.node = node; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 362 | msg.u.event_data.event = eEvent; |
| 363 | msg.u.event_data.data1 = nData1; |
| 364 | msg.u.event_data.data2 = nData2; |
| 365 | |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 366 | findDispatcher(node)->post(msg); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 367 | |
| 368 | return OMX_ErrorNone; |
| 369 | } |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 370 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 371 | OMX_ERRORTYPE OMX::OnEmptyBufferDone( |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 372 | node_id node, OMX_IN OMX_BUFFERHEADERTYPE *pBuffer) { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 373 | LOGV("OnEmptyBufferDone buffer=%p", pBuffer); |
| 374 | |
| 375 | omx_message msg; |
| 376 | msg.type = omx_message::EMPTY_BUFFER_DONE; |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 377 | msg.node = node; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 378 | msg.u.buffer_data.buffer = pBuffer; |
| 379 | |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 380 | findDispatcher(node)->post(msg); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 381 | |
| 382 | return OMX_ErrorNone; |
| 383 | } |
| 384 | |
| 385 | OMX_ERRORTYPE OMX::OnFillBufferDone( |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 386 | node_id node, OMX_IN OMX_BUFFERHEADERTYPE *pBuffer) { |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 387 | LOGV("OnFillBufferDone buffer=%p", pBuffer); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 388 | |
| 389 | omx_message msg; |
| 390 | msg.type = omx_message::FILL_BUFFER_DONE; |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 391 | msg.node = node; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 392 | msg.u.extended_buffer_data.buffer = pBuffer; |
| 393 | msg.u.extended_buffer_data.range_offset = pBuffer->nOffset; |
| 394 | msg.u.extended_buffer_data.range_length = pBuffer->nFilledLen; |
| 395 | msg.u.extended_buffer_data.flags = pBuffer->nFlags; |
| 396 | msg.u.extended_buffer_data.timestamp = pBuffer->nTimeStamp; |
| 397 | msg.u.extended_buffer_data.platform_private = pBuffer->pPlatformPrivate; |
Andreas Huber | 52733b8 | 2010-01-25 10:41:35 -0800 | [diff] [blame] | 398 | msg.u.extended_buffer_data.data_ptr = pBuffer->pBuffer; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 399 | |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 400 | findDispatcher(node)->post(msg); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 401 | |
| 402 | return OMX_ErrorNone; |
| 403 | } |
| 404 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 405 | OMX::node_id OMX::makeNodeID(OMXNodeInstance *instance) { |
| 406 | // mLock is already held. |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 407 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 408 | node_id node = (node_id)++mNodeCounter; |
| 409 | mNodeIDToInstance.add(node, instance); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 410 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 411 | return node; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 414 | OMXNodeInstance *OMX::findInstance(node_id node) { |
| 415 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 416 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 417 | ssize_t index = mNodeIDToInstance.indexOfKey(node); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 418 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 419 | return index < 0 ? NULL : mNodeIDToInstance.valueAt(index); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 420 | } |
| 421 | |
James Dong | c52ced0 | 2010-06-14 17:28:05 -0700 | [diff] [blame] | 422 | sp<OMX::CallbackDispatcher> OMX::findDispatcher(node_id node) { |
| 423 | Mutex::Autolock autoLock(mLock); |
| 424 | |
| 425 | ssize_t index = mDispatchers.indexOfKey(node); |
| 426 | |
| 427 | return index < 0 ? NULL : mDispatchers.valueAt(index); |
| 428 | } |
| 429 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 430 | void OMX::invalidateNodeID(node_id node) { |
| 431 | Mutex::Autolock autoLock(mLock); |
| 432 | invalidateNodeID_l(node); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 433 | } |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 434 | |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 435 | void OMX::invalidateNodeID_l(node_id node) { |
| 436 | // mLock is held. |
| 437 | mNodeIDToInstance.removeItem(node); |
Andreas Huber | be06d26 | 2009-08-14 14:37:10 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Andreas Huber | 1de1316 | 2009-07-31 11:52:50 -0700 | [diff] [blame] | 440 | //////////////////////////////////////////////////////////////////////////////// |
| 441 | |
Andreas Huber | 078f7f0 | 2010-02-22 11:24:01 -0800 | [diff] [blame] | 442 | struct SharedVideoRenderer : public VideoRenderer { |
| 443 | SharedVideoRenderer(void *libHandle, VideoRenderer *obj) |
| 444 | : mLibHandle(libHandle), |
| 445 | mObj(obj) { |
| 446 | } |
| 447 | |
| 448 | virtual ~SharedVideoRenderer() { |
| 449 | delete mObj; |
| 450 | mObj = NULL; |
| 451 | |
| 452 | dlclose(mLibHandle); |
| 453 | mLibHandle = NULL; |
| 454 | } |
| 455 | |
| 456 | virtual void render( |
| 457 | const void *data, size_t size, void *platformPrivate) { |
| 458 | return mObj->render(data, size, platformPrivate); |
| 459 | } |
| 460 | |
| 461 | private: |
| 462 | void *mLibHandle; |
| 463 | VideoRenderer *mObj; |
| 464 | |
| 465 | SharedVideoRenderer(const SharedVideoRenderer &); |
| 466 | SharedVideoRenderer &operator=(const SharedVideoRenderer &); |
| 467 | }; |
| 468 | |
Andreas Huber | 1de1316 | 2009-07-31 11:52:50 -0700 | [diff] [blame] | 469 | sp<IOMXRenderer> OMX::createRenderer( |
| 470 | const sp<ISurface> &surface, |
| 471 | const char *componentName, |
| 472 | OMX_COLOR_FORMATTYPE colorFormat, |
| 473 | size_t encodedWidth, size_t encodedHeight, |
| 474 | size_t displayWidth, size_t displayHeight) { |
Andreas Huber | c8d7c14 | 2009-11-11 16:33:17 -0800 | [diff] [blame] | 475 | Mutex::Autolock autoLock(mLock); |
| 476 | |
Andreas Huber | 1de1316 | 2009-07-31 11:52:50 -0700 | [diff] [blame] | 477 | VideoRenderer *impl = NULL; |
| 478 | |
Andreas Huber | 078f7f0 | 2010-02-22 11:24:01 -0800 | [diff] [blame] | 479 | void *libHandle = dlopen("libstagefrighthw.so", RTLD_NOW); |
Andreas Huber | c8d7c14 | 2009-11-11 16:33:17 -0800 | [diff] [blame] | 480 | |
| 481 | if (libHandle) { |
| 482 | typedef VideoRenderer *(*CreateRendererFunc)( |
| 483 | const sp<ISurface> &surface, |
| 484 | const char *componentName, |
| 485 | OMX_COLOR_FORMATTYPE colorFormat, |
| 486 | size_t displayWidth, size_t displayHeight, |
| 487 | size_t decodedWidth, size_t decodedHeight); |
| 488 | |
| 489 | CreateRendererFunc func = |
| 490 | (CreateRendererFunc)dlsym( |
| 491 | libHandle, |
| 492 | "_Z14createRendererRKN7android2spINS_8ISurfaceEEEPKc20" |
| 493 | "OMX_COLOR_FORMATTYPEjjjj"); |
| 494 | |
| 495 | if (func) { |
| 496 | impl = (*func)(surface, componentName, colorFormat, |
| 497 | displayWidth, displayHeight, encodedWidth, encodedHeight); |
Andreas Huber | 078f7f0 | 2010-02-22 11:24:01 -0800 | [diff] [blame] | 498 | |
| 499 | if (impl) { |
| 500 | impl = new SharedVideoRenderer(libHandle, impl); |
| 501 | libHandle = NULL; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if (libHandle) { |
| 506 | dlclose(libHandle); |
| 507 | libHandle = NULL; |
Andreas Huber | c8d7c14 | 2009-11-11 16:33:17 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | if (!impl) { |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 512 | #if 0 |
Andreas Huber | 1de1316 | 2009-07-31 11:52:50 -0700 | [diff] [blame] | 513 | LOGW("Using software renderer."); |
| 514 | impl = new SoftwareRenderer( |
Andreas Huber | c966a3c | 2009-09-15 14:32:59 -0700 | [diff] [blame] | 515 | colorFormat, |
Andreas Huber | 1de1316 | 2009-07-31 11:52:50 -0700 | [diff] [blame] | 516 | surface, |
| 517 | displayWidth, displayHeight, |
| 518 | encodedWidth, encodedHeight); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 519 | #else |
| 520 | CHECK(!"Should not be here."); |
| 521 | return NULL; |
| 522 | #endif |
Andreas Huber | 1de1316 | 2009-07-31 11:52:50 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return new OMXRenderer(impl); |
| 526 | } |
| 527 | |
| 528 | OMXRenderer::OMXRenderer(VideoRenderer *impl) |
| 529 | : mImpl(impl) { |
| 530 | } |
| 531 | |
| 532 | OMXRenderer::~OMXRenderer() { |
| 533 | delete mImpl; |
| 534 | mImpl = NULL; |
| 535 | } |
| 536 | |
| 537 | void OMXRenderer::render(IOMX::buffer_id buffer) { |
| 538 | OMX_BUFFERHEADERTYPE *header = (OMX_BUFFERHEADERTYPE *)buffer; |
| 539 | |
| 540 | mImpl->render( |
| 541 | header->pBuffer + header->nOffset, |
| 542 | header->nFilledLen, |
| 543 | header->pPlatformPrivate); |
| 544 | } |
| 545 | |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 546 | } // namespace android |