Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | //#define LOG_NDEBUG 0 |
michael@0 | 8 | #define LOG_TAG "OMXCodecProxy" |
michael@0 | 9 | |
michael@0 | 10 | #include <binder/IPCThreadState.h> |
michael@0 | 11 | #include <cutils/properties.h> |
michael@0 | 12 | #include <stagefright/foundation/ADebug.h> |
michael@0 | 13 | #include <stagefright/MetaData.h> |
michael@0 | 14 | #include <stagefright/OMXCodec.h> |
michael@0 | 15 | #include <utils/Log.h> |
michael@0 | 16 | |
michael@0 | 17 | #include "nsDebug.h" |
michael@0 | 18 | |
michael@0 | 19 | #include "IMediaResourceManagerService.h" |
michael@0 | 20 | |
michael@0 | 21 | #include "OMXCodecProxy.h" |
michael@0 | 22 | |
michael@0 | 23 | namespace android { |
michael@0 | 24 | |
michael@0 | 25 | // static |
michael@0 | 26 | sp<OMXCodecProxy> OMXCodecProxy::Create( |
michael@0 | 27 | const sp<IOMX> &omx, |
michael@0 | 28 | const sp<MetaData> &meta, bool createEncoder, |
michael@0 | 29 | const sp<MediaSource> &source, |
michael@0 | 30 | const char *matchComponentName, |
michael@0 | 31 | uint32_t flags, |
michael@0 | 32 | const sp<ANativeWindow> &nativeWindow) |
michael@0 | 33 | { |
michael@0 | 34 | sp<OMXCodecProxy> proxy; |
michael@0 | 35 | |
michael@0 | 36 | const char *mime; |
michael@0 | 37 | if (!meta->findCString(kKeyMIMEType, &mime)) { |
michael@0 | 38 | return nullptr; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | if (!strncasecmp(mime, "video/", 6)) { |
michael@0 | 42 | proxy = new OMXCodecProxy(omx, meta, createEncoder, source, matchComponentName, flags, nativeWindow); |
michael@0 | 43 | } |
michael@0 | 44 | return proxy; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | |
michael@0 | 48 | OMXCodecProxy::OMXCodecProxy( |
michael@0 | 49 | const sp<IOMX> &omx, |
michael@0 | 50 | const sp<MetaData> &meta, |
michael@0 | 51 | bool createEncoder, |
michael@0 | 52 | const sp<MediaSource> &source, |
michael@0 | 53 | const char *matchComponentName, |
michael@0 | 54 | uint32_t flags, |
michael@0 | 55 | const sp<ANativeWindow> &nativeWindow) |
michael@0 | 56 | : mOMX(omx), |
michael@0 | 57 | mSrcMeta(meta), |
michael@0 | 58 | mIsEncoder(createEncoder), |
michael@0 | 59 | mSource(source), |
michael@0 | 60 | mComponentName(nullptr), |
michael@0 | 61 | mFlags(flags), |
michael@0 | 62 | mNativeWindow(nativeWindow), |
michael@0 | 63 | mState(MediaResourceManagerClient::CLIENT_STATE_WAIT_FOR_RESOURCE) |
michael@0 | 64 | { |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | OMXCodecProxy::~OMXCodecProxy() |
michael@0 | 68 | { |
michael@0 | 69 | mState = MediaResourceManagerClient::CLIENT_STATE_SHUTDOWN; |
michael@0 | 70 | |
michael@0 | 71 | if (mOMXCodec.get()) { |
michael@0 | 72 | wp<MediaSource> tmp = mOMXCodec; |
michael@0 | 73 | mOMXCodec.clear(); |
michael@0 | 74 | while (tmp.promote() != nullptr) { |
michael@0 | 75 | // this value come from stagefrigh's AwesomePlayer. |
michael@0 | 76 | usleep(1000); |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | // Complete all pending Binder ipc transactions |
michael@0 | 80 | IPCThreadState::self()->flushCommands(); |
michael@0 | 81 | |
michael@0 | 82 | if (mManagerService.get() && mClient.get()) { |
michael@0 | 83 | mManagerService->cancelClient(mClient); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | mSource.clear(); |
michael@0 | 87 | free(mComponentName); |
michael@0 | 88 | mComponentName = nullptr; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | MediaResourceManagerClient::State OMXCodecProxy::getState() |
michael@0 | 92 | { |
michael@0 | 93 | Mutex::Autolock autoLock(mLock); |
michael@0 | 94 | return mState; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | void OMXCodecProxy::setEventListener(const wp<OMXCodecProxy::EventListener>& listener) |
michael@0 | 98 | { |
michael@0 | 99 | Mutex::Autolock autoLock(mLock); |
michael@0 | 100 | mEventListener = listener; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | void OMXCodecProxy::notifyStatusChangedLocked() |
michael@0 | 104 | { |
michael@0 | 105 | if (mEventListener != nullptr) { |
michael@0 | 106 | sp<EventListener> listener = mEventListener.promote(); |
michael@0 | 107 | if (listener != nullptr) { |
michael@0 | 108 | listener->statusChanged(); |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | void OMXCodecProxy::requestResource() |
michael@0 | 114 | { |
michael@0 | 115 | Mutex::Autolock autoLock(mLock); |
michael@0 | 116 | |
michael@0 | 117 | if (mClient.get()) { |
michael@0 | 118 | return; |
michael@0 | 119 | } |
michael@0 | 120 | sp<MediaResourceManagerClient::EventListener> listener = this; |
michael@0 | 121 | mClient = new MediaResourceManagerClient(listener); |
michael@0 | 122 | |
michael@0 | 123 | mManagerService = mClient->getMediaResourceManagerService(); |
michael@0 | 124 | if (!mManagerService.get()) { |
michael@0 | 125 | mClient = nullptr; |
michael@0 | 126 | return; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | mManagerService->requestMediaResource(mClient, MediaResourceManagerClient::HW_VIDEO_DECODER); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | bool OMXCodecProxy::IsWaitingResources() |
michael@0 | 133 | { |
michael@0 | 134 | Mutex::Autolock autoLock(mLock); |
michael@0 | 135 | return mState == MediaResourceManagerClient::CLIENT_STATE_WAIT_FOR_RESOURCE; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // called on Binder ipc thread |
michael@0 | 139 | void OMXCodecProxy::statusChanged(int event) |
michael@0 | 140 | { |
michael@0 | 141 | Mutex::Autolock autoLock(mLock); |
michael@0 | 142 | |
michael@0 | 143 | if (mState != MediaResourceManagerClient::CLIENT_STATE_WAIT_FOR_RESOURCE) { |
michael@0 | 144 | return; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | mState = (MediaResourceManagerClient::State) event; |
michael@0 | 148 | if (mState != MediaResourceManagerClient::CLIENT_STATE_RESOURCE_ASSIGNED) { |
michael@0 | 149 | return; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | const char *mime; |
michael@0 | 153 | if (!mSrcMeta->findCString(kKeyMIMEType, &mime)) { |
michael@0 | 154 | mState = MediaResourceManagerClient::CLIENT_STATE_SHUTDOWN; |
michael@0 | 155 | notifyStatusChangedLocked(); |
michael@0 | 156 | return; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | if (!strncasecmp(mime, "video/", 6)) { |
michael@0 | 160 | sp<MediaSource> codec; |
michael@0 | 161 | mOMXCodec = OMXCodec::Create(mOMX, mSrcMeta, mIsEncoder, mSource, mComponentName, mFlags, mNativeWindow); |
michael@0 | 162 | if (mOMXCodec == nullptr) { |
michael@0 | 163 | mState = MediaResourceManagerClient::CLIENT_STATE_SHUTDOWN; |
michael@0 | 164 | notifyStatusChangedLocked(); |
michael@0 | 165 | return; |
michael@0 | 166 | } |
michael@0 | 167 | // Check if this video is sized such that we're comfortable |
michael@0 | 168 | // possibly using an OMX decoder. |
michael@0 | 169 | int32_t maxWidth, maxHeight; |
michael@0 | 170 | char propValue[PROPERTY_VALUE_MAX]; |
michael@0 | 171 | property_get("ro.moz.omx.hw.max_width", propValue, "-1"); |
michael@0 | 172 | maxWidth = atoi(propValue); |
michael@0 | 173 | property_get("ro.moz.omx.hw.max_height", propValue, "-1"); |
michael@0 | 174 | maxHeight = atoi(propValue); |
michael@0 | 175 | |
michael@0 | 176 | int32_t width = -1, height = -1; |
michael@0 | 177 | if (maxWidth > 0 && maxHeight > 0 && |
michael@0 | 178 | !(mOMXCodec->getFormat()->findInt32(kKeyWidth, &width) && |
michael@0 | 179 | mOMXCodec->getFormat()->findInt32(kKeyHeight, &height) && |
michael@0 | 180 | width * height <= maxWidth * maxHeight)) { |
michael@0 | 181 | printf_stderr("Failed to get video size, or it was too large for HW decoder (<w=%d, h=%d> but <maxW=%d, maxH=%d>)", |
michael@0 | 182 | width, height, maxWidth, maxHeight); |
michael@0 | 183 | mOMXCodec.clear(); |
michael@0 | 184 | mState = MediaResourceManagerClient::CLIENT_STATE_SHUTDOWN; |
michael@0 | 185 | notifyStatusChangedLocked(); |
michael@0 | 186 | return; |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | if (mOMXCodec->start() != OK) { |
michael@0 | 190 | NS_WARNING("Couldn't start OMX video source"); |
michael@0 | 191 | mOMXCodec.clear(); |
michael@0 | 192 | mState = MediaResourceManagerClient::CLIENT_STATE_SHUTDOWN; |
michael@0 | 193 | notifyStatusChangedLocked(); |
michael@0 | 194 | return; |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | notifyStatusChangedLocked(); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | status_t OMXCodecProxy::start(MetaData *params) |
michael@0 | 201 | { |
michael@0 | 202 | Mutex::Autolock autoLock(mLock); |
michael@0 | 203 | |
michael@0 | 204 | if (mState != MediaResourceManagerClient::CLIENT_STATE_RESOURCE_ASSIGNED) { |
michael@0 | 205 | return NO_INIT; |
michael@0 | 206 | } |
michael@0 | 207 | CHECK(mOMXCodec.get() != nullptr); |
michael@0 | 208 | return mOMXCodec->start(); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | status_t OMXCodecProxy::stop() |
michael@0 | 212 | { |
michael@0 | 213 | Mutex::Autolock autoLock(mLock); |
michael@0 | 214 | |
michael@0 | 215 | if (mState != MediaResourceManagerClient::CLIENT_STATE_RESOURCE_ASSIGNED) { |
michael@0 | 216 | return NO_INIT; |
michael@0 | 217 | } |
michael@0 | 218 | CHECK(mOMXCodec.get() != nullptr); |
michael@0 | 219 | return mOMXCodec->stop(); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | sp<MetaData> OMXCodecProxy::getFormat() |
michael@0 | 223 | { |
michael@0 | 224 | Mutex::Autolock autoLock(mLock); |
michael@0 | 225 | |
michael@0 | 226 | if (mState != MediaResourceManagerClient::CLIENT_STATE_RESOURCE_ASSIGNED) { |
michael@0 | 227 | sp<MetaData> meta = new MetaData; |
michael@0 | 228 | return meta; |
michael@0 | 229 | } |
michael@0 | 230 | CHECK(mOMXCodec.get() != nullptr); |
michael@0 | 231 | return mOMXCodec->getFormat(); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | status_t OMXCodecProxy::read(MediaBuffer **buffer, const ReadOptions *options) |
michael@0 | 235 | { |
michael@0 | 236 | Mutex::Autolock autoLock(mLock); |
michael@0 | 237 | |
michael@0 | 238 | if (mState != MediaResourceManagerClient::CLIENT_STATE_RESOURCE_ASSIGNED) { |
michael@0 | 239 | return NO_INIT; |
michael@0 | 240 | } |
michael@0 | 241 | CHECK(mOMXCodec.get() != nullptr); |
michael@0 | 242 | return mOMXCodec->read(buffer, options); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | status_t OMXCodecProxy::pause() |
michael@0 | 246 | { |
michael@0 | 247 | Mutex::Autolock autoLock(mLock); |
michael@0 | 248 | |
michael@0 | 249 | if (mState != MediaResourceManagerClient::CLIENT_STATE_RESOURCE_ASSIGNED) { |
michael@0 | 250 | return NO_INIT; |
michael@0 | 251 | } |
michael@0 | 252 | CHECK(mOMXCodec.get() != nullptr); |
michael@0 | 253 | return mOMXCodec->pause(); |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | } // namespace android |