Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 2 | * Copyright 2018 The Android Open Source Project |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 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 "Codec2Client" |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 20 | |
| 21 | #include <codec2/hidl/client.h> |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 22 | #include <C2Debug.h> |
| 23 | #include <C2BufferPriv.h> |
| 24 | #include <C2PlatformSupport.h> |
| 25 | |
| 26 | #include <android/hardware/media/bufferpool/2.0/IClientManager.h> |
| 27 | #include <android/hardware/media/c2/1.0/IComponent.h> |
| 28 | #include <android/hardware/media/c2/1.0/IComponentInterface.h> |
| 29 | #include <android/hardware/media/c2/1.0/IComponentListener.h> |
| 30 | #include <android/hardware/media/c2/1.0/IComponentStore.h> |
| 31 | #include <android/hardware/media/c2/1.0/IConfigurable.h> |
| 32 | #include <android/hidl/manager/1.2/IServiceManager.h> |
| 33 | |
| 34 | #include <android-base/properties.h> |
| 35 | #include <bufferpool/ClientManager.h> |
| 36 | #include <codec2/hidl/1.0/OutputBufferQueue.h> |
| 37 | #include <codec2/hidl/1.0/types.h> |
| 38 | #include <codec2/hidl/1.1/OutputBufferQueue.h> |
| 39 | #include <codec2/hidl/1.1/types.h> |
| 40 | |
| 41 | #include <cutils/native_handle.h> |
| 42 | #include <gui/bufferqueue/2.0/B2HGraphicBufferProducer.h> |
| 43 | #include <gui/bufferqueue/2.0/H2BGraphicBufferProducer.h> |
| 44 | #include <hidl/HidlSupport.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 45 | |
| 46 | #include <deque> |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 47 | #include <iterator> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 48 | #include <limits> |
| 49 | #include <map> |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 50 | #include <mutex> |
| 51 | #include <sstream> |
| 52 | #include <thread> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 53 | #include <type_traits> |
| 54 | #include <vector> |
| 55 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 56 | namespace android { |
| 57 | |
| 58 | using ::android::hardware::hidl_vec; |
| 59 | using ::android::hardware::hidl_string; |
| 60 | using ::android::hardware::Return; |
| 61 | using ::android::hardware::Void; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 62 | |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 63 | using namespace ::android::hardware::media::c2::V1_1; |
| 64 | using namespace ::android::hardware::media::c2::V1_1::utils; |
Sungtak Lee | d331808 | 2018-09-07 15:52:43 -0700 | [diff] [blame] | 65 | using namespace ::android::hardware::media::bufferpool::V2_0; |
| 66 | using namespace ::android::hardware::media::bufferpool::V2_0::implementation; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 67 | |
Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 68 | using HGraphicBufferProducer1 = ::android::hardware::graphics::bufferqueue:: |
| 69 | V1_0::IGraphicBufferProducer; |
| 70 | using HGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue:: |
| 71 | V2_0::IGraphicBufferProducer; |
| 72 | using B2HGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue:: |
| 73 | V2_0::utils::B2HGraphicBufferProducer; |
| 74 | using H2BGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue:: |
| 75 | V2_0::utils::H2BGraphicBufferProducer; |
| 76 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 77 | namespace /* unnamed */ { |
| 78 | |
| 79 | // c2_status_t value that corresponds to hwbinder transaction failure. |
| 80 | constexpr c2_status_t C2_TRANSACTION_FAILED = C2_CORRUPTED; |
| 81 | |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 82 | // Searches for a name in GetServiceNames() and returns the index found. If the |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 83 | // name is not found, the returned index will be equal to |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 84 | // GetServiceNames().size(). |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 85 | size_t getServiceIndex(char const* name) { |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 86 | std::vector<std::string> const& names = Codec2Client::GetServiceNames(); |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 87 | size_t i = 0; |
| 88 | for (; i < names.size(); ++i) { |
| 89 | if (name == names[i]) { |
| 90 | break; |
| 91 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 92 | } |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 93 | return i; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Pawin Vongmasa | 83d2c55 | 2020-03-05 04:36:08 -0800 | [diff] [blame] | 96 | class Client2Store : public C2ComponentStore { |
| 97 | std::shared_ptr<Codec2Client> mClient; |
| 98 | |
| 99 | public: |
| 100 | Client2Store(std::shared_ptr<Codec2Client> const& client) |
| 101 | : mClient(client) { } |
| 102 | |
| 103 | virtual ~Client2Store() = default; |
| 104 | |
| 105 | virtual c2_status_t config_sm( |
| 106 | std::vector<C2Param*> const ¶ms, |
| 107 | std::vector<std::unique_ptr<C2SettingResult>>* const failures) { |
| 108 | return mClient->config(params, C2_MAY_BLOCK, failures); |
| 109 | }; |
| 110 | |
| 111 | virtual c2_status_t copyBuffer( |
| 112 | std::shared_ptr<C2GraphicBuffer>, |
| 113 | std::shared_ptr<C2GraphicBuffer>) { |
| 114 | return C2_OMITTED; |
| 115 | } |
| 116 | |
| 117 | virtual c2_status_t createComponent( |
| 118 | C2String, std::shared_ptr<C2Component>* const component) { |
| 119 | component->reset(); |
| 120 | return C2_OMITTED; |
| 121 | } |
| 122 | |
| 123 | virtual c2_status_t createInterface( |
| 124 | C2String, std::shared_ptr<C2ComponentInterface>* const interface) { |
| 125 | interface->reset(); |
| 126 | return C2_OMITTED; |
| 127 | } |
| 128 | |
| 129 | virtual c2_status_t query_sm( |
| 130 | std::vector<C2Param*> const& stackParams, |
| 131 | std::vector<C2Param::Index> const& heapParamIndices, |
| 132 | std::vector<std::unique_ptr<C2Param>>* const heapParams) const { |
| 133 | return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams); |
| 134 | } |
| 135 | |
| 136 | virtual c2_status_t querySupportedParams_nb( |
| 137 | std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const { |
| 138 | return mClient->querySupportedParams(params); |
| 139 | } |
| 140 | |
| 141 | virtual c2_status_t querySupportedValues_sm( |
| 142 | std::vector<C2FieldSupportedValuesQuery>& fields) const { |
| 143 | return mClient->querySupportedValues(fields, C2_MAY_BLOCK); |
| 144 | } |
| 145 | |
| 146 | virtual C2String getName() const { |
| 147 | return mClient->getName(); |
| 148 | } |
| 149 | |
| 150 | virtual std::shared_ptr<C2ParamReflector> getParamReflector() const { |
| 151 | return mClient->getParamReflector(); |
| 152 | } |
| 153 | |
| 154 | virtual std::vector<std::shared_ptr<C2Component::Traits const>> listComponents() { |
| 155 | return std::vector<std::shared_ptr<C2Component::Traits const>>(); |
| 156 | } |
| 157 | }; |
| 158 | |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 159 | } // unnamed namespace |
| 160 | |
| 161 | // This class caches a Codec2Client object and its component traits. The client |
| 162 | // will be created the first time it is needed, and it can be refreshed if the |
| 163 | // service dies (by calling invalidate()). The first time listComponents() is |
| 164 | // called from the client, the result will be cached. |
| 165 | class Codec2Client::Cache { |
| 166 | // Cached client |
| 167 | std::shared_ptr<Codec2Client> mClient; |
| 168 | mutable std::mutex mClientMutex; |
| 169 | |
| 170 | // Cached component traits |
| 171 | std::vector<C2Component::Traits> mTraits; |
| 172 | std::once_flag mTraitsInitializationFlag; |
| 173 | |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 174 | // The index of the service. This is based on GetServiceNames(). |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 175 | size_t mIndex; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 176 | // Called by s() exactly once to initialize the cache. The index must be a |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 177 | // valid index into the vector returned by GetServiceNames(). Calling |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 178 | // init(index) will associate the cache to the service with name |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 179 | // GetServiceNames()[index]. |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 180 | void init(size_t index) { |
| 181 | mIndex = index; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | public: |
| 185 | Cache() = default; |
| 186 | |
| 187 | // Initializes mClient if needed, then returns mClient. |
| 188 | // If the service is unavailable but listed in the manifest, this function |
| 189 | // will block indefinitely. |
| 190 | std::shared_ptr<Codec2Client> getClient() { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 191 | std::scoped_lock lock{mClientMutex}; |
| 192 | if (!mClient) { |
| 193 | mClient = Codec2Client::_CreateFromIndex(mIndex); |
| 194 | } |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 195 | CHECK(mClient) << "Failed to create Codec2Client to service \"" |
| 196 | << GetServiceNames()[mIndex] << "\". (Index = " |
| 197 | << mIndex << ")."; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 198 | return mClient; |
| 199 | } |
| 200 | |
| 201 | // Causes a subsequent call to getClient() to create a new client. This |
| 202 | // function should be called after the service dies. |
| 203 | // |
| 204 | // Note: This function is called only by ForAllServices(). |
| 205 | void invalidate() { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 206 | std::scoped_lock lock{mClientMutex}; |
| 207 | mClient = nullptr; |
| 208 | } |
| 209 | |
| 210 | // Returns a list of traits for components supported by the service. This |
| 211 | // list is cached. |
| 212 | std::vector<C2Component::Traits> const& getTraits() { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 213 | std::call_once(mTraitsInitializationFlag, [this]() { |
| 214 | bool success{false}; |
| 215 | // Spin until _listComponents() is successful. |
| 216 | while (true) { |
| 217 | std::shared_ptr<Codec2Client> client = getClient(); |
| 218 | mTraits = client->_listComponents(&success); |
| 219 | if (success) { |
| 220 | break; |
| 221 | } |
| 222 | using namespace std::chrono_literals; |
| 223 | static constexpr auto kServiceRetryPeriod = 5s; |
| 224 | LOG(INFO) << "Failed to retrieve component traits from service " |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 225 | "\"" << GetServiceNames()[mIndex] << "\". " |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 226 | "Retrying..."; |
| 227 | std::this_thread::sleep_for(kServiceRetryPeriod); |
| 228 | } |
| 229 | }); |
| 230 | return mTraits; |
| 231 | } |
| 232 | |
| 233 | // List() returns the list of all caches. |
| 234 | static std::vector<Cache>& List() { |
| 235 | static std::vector<Cache> sCaches{[]() { |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 236 | size_t numServices = GetServiceNames().size(); |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 237 | std::vector<Cache> caches(numServices); |
| 238 | for (size_t i = 0; i < numServices; ++i) { |
| 239 | caches[i].init(i); |
| 240 | } |
| 241 | return caches; |
| 242 | }()}; |
| 243 | return sCaches; |
| 244 | } |
| 245 | }; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 246 | |
| 247 | // Codec2ConfigurableClient |
| 248 | |
| 249 | const C2String& Codec2ConfigurableClient::getName() const { |
| 250 | return mName; |
| 251 | } |
| 252 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 253 | Codec2ConfigurableClient::Codec2ConfigurableClient( |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 254 | const sp<IConfigurable>& base) |
| 255 | : mBase{base}, |
| 256 | mName{[base]() -> C2String { |
| 257 | C2String outName; |
| 258 | Return<void> transStatus = base->getName( |
| 259 | [&outName](const hidl_string& name) { |
| 260 | outName = name.c_str(); |
| 261 | }); |
| 262 | return transStatus.isOk() ? outName : ""; |
| 263 | }()} { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | c2_status_t Codec2ConfigurableClient::query( |
| 267 | const std::vector<C2Param*> &stackParams, |
| 268 | const std::vector<C2Param::Index> &heapParamIndices, |
| 269 | c2_blocking_t mayBlock, |
| 270 | std::vector<std::unique_ptr<C2Param>>* const heapParams) const { |
| 271 | hidl_vec<ParamIndex> indices( |
| 272 | stackParams.size() + heapParamIndices.size()); |
| 273 | size_t numIndices = 0; |
| 274 | for (C2Param* const& stackParam : stackParams) { |
| 275 | if (!stackParam) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 276 | LOG(WARNING) << "query -- null stack param encountered."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 277 | continue; |
| 278 | } |
| 279 | indices[numIndices++] = static_cast<ParamIndex>(stackParam->index()); |
| 280 | } |
| 281 | size_t numStackIndices = numIndices; |
| 282 | for (const C2Param::Index& index : heapParamIndices) { |
| 283 | indices[numIndices++] = |
| 284 | static_cast<ParamIndex>(static_cast<uint32_t>(index)); |
| 285 | } |
| 286 | indices.resize(numIndices); |
| 287 | if (heapParams) { |
| 288 | heapParams->reserve(heapParams->size() + numIndices); |
| 289 | } |
| 290 | c2_status_t status; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 291 | Return<void> transStatus = mBase->query( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 292 | indices, |
| 293 | mayBlock == C2_MAY_BLOCK, |
| 294 | [&status, &numStackIndices, &stackParams, heapParams]( |
| 295 | Status s, const Params& p) { |
| 296 | status = static_cast<c2_status_t>(s); |
| 297 | if (status != C2_OK && status != C2_BAD_INDEX) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 298 | LOG(DEBUG) << "query -- call failed: " |
| 299 | << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | std::vector<C2Param*> paramPointers; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 303 | if (!parseParamsBlob(¶mPointers, p)) { |
| 304 | LOG(ERROR) << "query -- error while parsing params."; |
| 305 | status = C2_CORRUPTED; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 306 | return; |
| 307 | } |
| 308 | size_t i = 0; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 309 | for (auto it = paramPointers.begin(); |
| 310 | it != paramPointers.end(); ) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 311 | C2Param* paramPointer = *it; |
| 312 | if (numStackIndices > 0) { |
| 313 | --numStackIndices; |
| 314 | if (!paramPointer) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 315 | LOG(WARNING) << "query -- null stack param."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 316 | ++it; |
| 317 | continue; |
| 318 | } |
| 319 | for (; i < stackParams.size() && !stackParams[i]; ) { |
| 320 | ++i; |
| 321 | } |
| 322 | if (i >= stackParams.size()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 323 | LOG(ERROR) << "query -- unexpected error."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 324 | status = C2_CORRUPTED; |
| 325 | return; |
| 326 | } |
| 327 | if (stackParams[i]->index() != paramPointer->index()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 328 | LOG(WARNING) << "query -- param skipped: " |
| 329 | "index = " |
| 330 | << stackParams[i]->index() << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 331 | stackParams[i++]->invalidate(); |
| 332 | continue; |
| 333 | } |
| 334 | if (!stackParams[i++]->updateFrom(*paramPointer)) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 335 | LOG(WARNING) << "query -- param update failed: " |
| 336 | "index = " |
| 337 | << paramPointer->index() << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 338 | } |
| 339 | } else { |
| 340 | if (!paramPointer) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 341 | LOG(WARNING) << "query -- null heap param."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 342 | ++it; |
| 343 | continue; |
| 344 | } |
| 345 | if (!heapParams) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 346 | LOG(WARNING) << "query -- " |
| 347 | "unexpected extra stack param."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 348 | } else { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 349 | heapParams->emplace_back( |
| 350 | C2Param::Copy(*paramPointer)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | ++it; |
| 354 | } |
| 355 | }); |
| 356 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 357 | LOG(ERROR) << "query -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 358 | return C2_TRANSACTION_FAILED; |
| 359 | } |
| 360 | return status; |
| 361 | } |
| 362 | |
| 363 | c2_status_t Codec2ConfigurableClient::config( |
| 364 | const std::vector<C2Param*> ¶ms, |
| 365 | c2_blocking_t mayBlock, |
| 366 | std::vector<std::unique_ptr<C2SettingResult>>* const failures) { |
| 367 | Params hidlParams; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 368 | if (!createParamsBlob(&hidlParams, params)) { |
| 369 | LOG(ERROR) << "config -- bad input."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 370 | return C2_TRANSACTION_FAILED; |
| 371 | } |
| 372 | c2_status_t status; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 373 | Return<void> transStatus = mBase->config( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 374 | hidlParams, |
| 375 | mayBlock == C2_MAY_BLOCK, |
| 376 | [&status, ¶ms, failures]( |
| 377 | Status s, |
| 378 | const hidl_vec<SettingResult> f, |
| 379 | const Params& o) { |
| 380 | status = static_cast<c2_status_t>(s); |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 381 | if (status != C2_OK && status != C2_BAD_INDEX) { |
| 382 | LOG(DEBUG) << "config -- call failed: " |
| 383 | << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 384 | } |
| 385 | size_t i = failures->size(); |
| 386 | failures->resize(i + f.size()); |
| 387 | for (const SettingResult& sf : f) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 388 | if (!objcpy(&(*failures)[i++], sf)) { |
| 389 | LOG(ERROR) << "config -- " |
| 390 | << "invalid SettingResult returned."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 394 | if (!updateParamsFromBlob(params, o)) { |
| 395 | LOG(ERROR) << "config -- " |
| 396 | << "failed to parse returned params."; |
| 397 | status = C2_CORRUPTED; |
| 398 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 399 | }); |
| 400 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 401 | LOG(ERROR) << "config -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 402 | return C2_TRANSACTION_FAILED; |
| 403 | } |
| 404 | return status; |
| 405 | } |
| 406 | |
| 407 | c2_status_t Codec2ConfigurableClient::querySupportedParams( |
| 408 | std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const { |
| 409 | // TODO: Cache and query properly! |
| 410 | c2_status_t status; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 411 | Return<void> transStatus = mBase->querySupportedParams( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 412 | std::numeric_limits<uint32_t>::min(), |
| 413 | std::numeric_limits<uint32_t>::max(), |
| 414 | [&status, params]( |
| 415 | Status s, |
| 416 | const hidl_vec<ParamDescriptor>& p) { |
| 417 | status = static_cast<c2_status_t>(s); |
| 418 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 419 | LOG(DEBUG) << "querySupportedParams -- call failed: " |
| 420 | << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 421 | return; |
| 422 | } |
| 423 | size_t i = params->size(); |
| 424 | params->resize(i + p.size()); |
| 425 | for (const ParamDescriptor& sp : p) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 426 | if (!objcpy(&(*params)[i++], sp)) { |
| 427 | LOG(ERROR) << "querySupportedParams -- " |
| 428 | << "invalid returned ParamDescriptor."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | } |
| 432 | }); |
| 433 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 434 | LOG(ERROR) << "querySupportedParams -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 435 | return C2_TRANSACTION_FAILED; |
| 436 | } |
| 437 | return status; |
| 438 | } |
| 439 | |
| 440 | c2_status_t Codec2ConfigurableClient::querySupportedValues( |
| 441 | std::vector<C2FieldSupportedValuesQuery>& fields, |
| 442 | c2_blocking_t mayBlock) const { |
| 443 | hidl_vec<FieldSupportedValuesQuery> inFields(fields.size()); |
| 444 | for (size_t i = 0; i < fields.size(); ++i) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 445 | if (!objcpy(&inFields[i], fields[i])) { |
| 446 | LOG(ERROR) << "querySupportedValues -- bad input"; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 447 | return C2_TRANSACTION_FAILED; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | c2_status_t status; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 452 | Return<void> transStatus = mBase->querySupportedValues( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 453 | inFields, |
| 454 | mayBlock == C2_MAY_BLOCK, |
| 455 | [&status, &inFields, &fields]( |
| 456 | Status s, |
| 457 | const hidl_vec<FieldSupportedValuesQueryResult>& r) { |
| 458 | status = static_cast<c2_status_t>(s); |
| 459 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 460 | LOG(DEBUG) << "querySupportedValues -- call failed: " |
| 461 | << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 462 | return; |
| 463 | } |
| 464 | if (r.size() != fields.size()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 465 | LOG(ERROR) << "querySupportedValues -- " |
| 466 | "input and output lists " |
| 467 | "have different sizes."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 468 | status = C2_CORRUPTED; |
| 469 | return; |
| 470 | } |
| 471 | for (size_t i = 0; i < fields.size(); ++i) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 472 | if (!objcpy(&fields[i], inFields[i], r[i])) { |
| 473 | LOG(ERROR) << "querySupportedValues -- " |
| 474 | "invalid returned value."; |
| 475 | status = C2_CORRUPTED; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | } |
| 479 | }); |
| 480 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 481 | LOG(ERROR) << "querySupportedValues -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 482 | return C2_TRANSACTION_FAILED; |
| 483 | } |
| 484 | return status; |
| 485 | } |
| 486 | |
| 487 | // Codec2Client::Component::HidlListener |
| 488 | struct Codec2Client::Component::HidlListener : public IComponentListener { |
| 489 | std::weak_ptr<Component> component; |
| 490 | std::weak_ptr<Listener> base; |
| 491 | |
| 492 | virtual Return<void> onWorkDone(const WorkBundle& workBundle) override { |
| 493 | std::list<std::unique_ptr<C2Work>> workItems; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 494 | if (!objcpy(&workItems, workBundle)) { |
| 495 | LOG(DEBUG) << "onWorkDone -- received corrupted WorkBundle."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 496 | return Void(); |
| 497 | } |
| 498 | // release input buffers potentially held by the component from queue |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 499 | std::shared_ptr<Codec2Client::Component> strongComponent = |
| 500 | component.lock(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 501 | if (strongComponent) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 502 | strongComponent->handleOnWorkDone(workItems); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 503 | } |
| 504 | if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 505 | listener->onWorkDone(component, workItems); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 506 | } else { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 507 | LOG(DEBUG) << "onWorkDone -- listener died."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 508 | } |
| 509 | return Void(); |
| 510 | } |
| 511 | |
| 512 | virtual Return<void> onTripped( |
| 513 | const hidl_vec<SettingResult>& settingResults) override { |
| 514 | std::vector<std::shared_ptr<C2SettingResult>> c2SettingResults( |
| 515 | settingResults.size()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 516 | for (size_t i = 0; i < settingResults.size(); ++i) { |
| 517 | std::unique_ptr<C2SettingResult> c2SettingResult; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 518 | if (!objcpy(&c2SettingResult, settingResults[i])) { |
| 519 | LOG(DEBUG) << "onTripped -- received corrupted SettingResult."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 520 | return Void(); |
| 521 | } |
| 522 | c2SettingResults[i] = std::move(c2SettingResult); |
| 523 | } |
| 524 | if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) { |
| 525 | listener->onTripped(component, c2SettingResults); |
| 526 | } else { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 527 | LOG(DEBUG) << "onTripped -- listener died."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 528 | } |
| 529 | return Void(); |
| 530 | } |
| 531 | |
| 532 | virtual Return<void> onError(Status s, uint32_t errorCode) override { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 533 | LOG(DEBUG) << "onError --" |
| 534 | << " status = " << s |
| 535 | << ", errorCode = " << errorCode |
| 536 | << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 537 | if (std::shared_ptr<Listener> listener = base.lock()) { |
| 538 | listener->onError(component, s == Status::OK ? |
| 539 | errorCode : static_cast<c2_status_t>(s)); |
| 540 | } else { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 541 | LOG(DEBUG) << "onError -- listener died."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 542 | } |
| 543 | return Void(); |
| 544 | } |
| 545 | |
| 546 | virtual Return<void> onFramesRendered( |
| 547 | const hidl_vec<RenderedFrame>& renderedFrames) override { |
| 548 | std::shared_ptr<Listener> listener = base.lock(); |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 549 | if (!listener) { |
| 550 | LOG(DEBUG) << "onFramesRendered -- listener died."; |
| 551 | return Void(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 552 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 553 | for (const RenderedFrame& renderedFrame : renderedFrames) { |
| 554 | listener->onFrameRendered( |
| 555 | renderedFrame.bufferQueueId, |
| 556 | renderedFrame.slotId, |
| 557 | renderedFrame.timestampNs); |
| 558 | } |
| 559 | return Void(); |
| 560 | } |
| 561 | |
| 562 | virtual Return<void> onInputBuffersReleased( |
| 563 | const hidl_vec<InputBuffer>& inputBuffers) override { |
| 564 | std::shared_ptr<Listener> listener = base.lock(); |
| 565 | if (!listener) { |
| 566 | LOG(DEBUG) << "onInputBuffersReleased -- listener died."; |
| 567 | return Void(); |
| 568 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 569 | for (const InputBuffer& inputBuffer : inputBuffers) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 570 | LOG(VERBOSE) << "onInputBuffersReleased --" |
| 571 | " received death notification of" |
| 572 | " input buffer:" |
| 573 | " frameIndex = " << inputBuffer.frameIndex |
| 574 | << ", bufferIndex = " << inputBuffer.arrayIndex |
| 575 | << "."; |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 576 | listener->onInputBufferDone( |
| 577 | inputBuffer.frameIndex, inputBuffer.arrayIndex); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 578 | } |
| 579 | return Void(); |
| 580 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 581 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 582 | }; |
| 583 | |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 584 | // Codec2Client::Component::BufferPoolSender |
| 585 | struct Codec2Client::Component::BufferPoolSender : |
| 586 | hardware::media::c2::V1_1::utils::DefaultBufferPoolSender { |
| 587 | BufferPoolSender() |
| 588 | : hardware::media::c2::V1_1::utils::DefaultBufferPoolSender() { |
| 589 | } |
| 590 | }; |
| 591 | |
| 592 | // Codec2Client::Component::OutputBufferQueue |
| 593 | struct Codec2Client::Component::OutputBufferQueue : |
| 594 | hardware::media::c2::V1_1::utils::OutputBufferQueue { |
| 595 | OutputBufferQueue() |
| 596 | : hardware::media::c2::V1_1::utils::OutputBufferQueue() { |
| 597 | } |
| 598 | }; |
| 599 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 600 | // Codec2Client |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 601 | Codec2Client::Codec2Client(sp<Base> const& base, |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 602 | size_t serviceIndex) |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 603 | : Configurable{ |
| 604 | [base]() -> sp<IConfigurable> { |
| 605 | Return<sp<IConfigurable>> transResult = |
| 606 | base->getConfigurable(); |
| 607 | return transResult.isOk() ? |
| 608 | static_cast<sp<IConfigurable>>(transResult) : |
| 609 | nullptr; |
| 610 | }() |
| 611 | }, |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 612 | mBase1_0{base}, |
| 613 | mBase1_1{Base1_1::castFrom(base)}, |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 614 | mServiceIndex{serviceIndex} { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 615 | Return<sp<IClientManager>> transResult = base->getPoolClientManager(); |
| 616 | if (!transResult.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 617 | LOG(ERROR) << "getPoolClientManager -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 618 | } else { |
| 619 | mHostPoolManager = static_cast<sp<IClientManager>>(transResult); |
| 620 | } |
| 621 | } |
| 622 | |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 623 | sp<Codec2Client::Base> const& Codec2Client::getBase() const { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 624 | return mBase1_0; |
| 625 | } |
| 626 | |
| 627 | sp<Codec2Client::Base1_0> const& Codec2Client::getBase1_0() const { |
| 628 | return mBase1_0; |
| 629 | } |
| 630 | |
| 631 | sp<Codec2Client::Base1_1> const& Codec2Client::getBase1_1() const { |
| 632 | return mBase1_1; |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 635 | std::string const& Codec2Client::getServiceName() const { |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 636 | return GetServiceNames()[mServiceIndex]; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 639 | c2_status_t Codec2Client::createComponent( |
| 640 | const C2String& name, |
| 641 | const std::shared_ptr<Codec2Client::Listener>& listener, |
| 642 | std::shared_ptr<Codec2Client::Component>* const component) { |
| 643 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 644 | c2_status_t status; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 645 | sp<Component::HidlListener> hidlListener = new Component::HidlListener{}; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 646 | hidlListener->base = listener; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 647 | Return<void> transStatus = mBase1_1 ? |
| 648 | mBase1_1->createComponent_1_1( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 649 | name, |
| 650 | hidlListener, |
| 651 | ClientManager::getInstance(), |
| 652 | [&status, component, hidlListener]( |
| 653 | Status s, |
| 654 | const sp<IComponent>& c) { |
| 655 | status = static_cast<c2_status_t>(s); |
| 656 | if (status != C2_OK) { |
| 657 | return; |
| 658 | } |
| 659 | *component = std::make_shared<Codec2Client::Component>(c); |
| 660 | hidlListener->component = *component; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 661 | }) : |
| 662 | mBase1_0->createComponent( |
| 663 | name, |
| 664 | hidlListener, |
| 665 | ClientManager::getInstance(), |
| 666 | [&status, component, hidlListener]( |
| 667 | Status s, |
| 668 | const sp<hardware::media::c2::V1_0::IComponent>& c) { |
| 669 | status = static_cast<c2_status_t>(s); |
| 670 | if (status != C2_OK) { |
| 671 | return; |
| 672 | } |
| 673 | *component = std::make_shared<Codec2Client::Component>(c); |
| 674 | hidlListener->component = *component; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 675 | }); |
| 676 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 677 | LOG(ERROR) << "createComponent(" << name.c_str() |
| 678 | << ") -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 679 | return C2_TRANSACTION_FAILED; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 680 | } else if (status != C2_OK) { |
Pawin Vongmasa | 9aac304 | 2020-03-05 02:30:32 -0800 | [diff] [blame] | 681 | if (status == C2_NOT_FOUND) { |
| 682 | LOG(VERBOSE) << "createComponent(" << name.c_str() |
| 683 | << ") -- component not found."; |
| 684 | } else { |
| 685 | LOG(ERROR) << "createComponent(" << name.c_str() |
| 686 | << ") -- call failed: " << status << "."; |
| 687 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 688 | return status; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 689 | } else if (!*component) { |
| 690 | LOG(ERROR) << "createComponent(" << name.c_str() |
| 691 | << ") -- null component."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 692 | return C2_CORRUPTED; |
| 693 | } |
| 694 | |
| 695 | status = (*component)->setDeathListener(*component, listener); |
| 696 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 697 | LOG(ERROR) << "createComponent(" << name.c_str() |
| 698 | << ") -- failed to set up death listener: " |
| 699 | << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 702 | (*component)->mBufferPoolSender->setReceiver(mHostPoolManager); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 703 | return status; |
| 704 | } |
| 705 | |
| 706 | c2_status_t Codec2Client::createInterface( |
| 707 | const C2String& name, |
| 708 | std::shared_ptr<Codec2Client::Interface>* const interface) { |
| 709 | c2_status_t status; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 710 | Return<void> transStatus = mBase1_0->createInterface( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 711 | name, |
| 712 | [&status, interface]( |
| 713 | Status s, |
| 714 | const sp<IComponentInterface>& i) { |
| 715 | status = static_cast<c2_status_t>(s); |
| 716 | if (status != C2_OK) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 717 | return; |
| 718 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 719 | *interface = std::make_shared<Interface>(i); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 720 | }); |
| 721 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 722 | LOG(ERROR) << "createInterface(" << name.c_str() |
| 723 | << ") -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 724 | return C2_TRANSACTION_FAILED; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 725 | } else if (status != C2_OK) { |
Pawin Vongmasa | 9aac304 | 2020-03-05 02:30:32 -0800 | [diff] [blame] | 726 | if (status == C2_NOT_FOUND) { |
| 727 | LOG(VERBOSE) << "createInterface(" << name.c_str() |
| 728 | << ") -- component not found."; |
| 729 | } else { |
| 730 | LOG(ERROR) << "createInterface(" << name.c_str() |
| 731 | << ") -- call failed: " << status << "."; |
| 732 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 733 | return status; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 734 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 735 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 736 | return status; |
| 737 | } |
| 738 | |
| 739 | c2_status_t Codec2Client::createInputSurface( |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 740 | std::shared_ptr<InputSurface>* const inputSurface) { |
| 741 | c2_status_t status; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 742 | Return<void> transStatus = mBase1_0->createInputSurface( |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 743 | [&status, inputSurface]( |
| 744 | Status s, |
| 745 | const sp<IInputSurface>& i) { |
| 746 | status = static_cast<c2_status_t>(s); |
| 747 | if (status != C2_OK) { |
| 748 | return; |
| 749 | } |
| 750 | *inputSurface = std::make_shared<InputSurface>(i); |
| 751 | }); |
| 752 | if (!transStatus.isOk()) { |
| 753 | LOG(ERROR) << "createInputSurface -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 754 | return C2_TRANSACTION_FAILED; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 755 | } else if (status != C2_OK) { |
| 756 | LOG(DEBUG) << "createInputSurface -- call failed: " |
| 757 | << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 758 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 759 | return status; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 760 | } |
| 761 | |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 762 | std::vector<C2Component::Traits> const& Codec2Client::listComponents() const { |
| 763 | return Cache::List()[mServiceIndex].getTraits(); |
| 764 | } |
| 765 | |
| 766 | std::vector<C2Component::Traits> Codec2Client::_listComponents( |
| 767 | bool* success) const { |
| 768 | std::vector<C2Component::Traits> traits; |
| 769 | std::string const& serviceName = getServiceName(); |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 770 | Return<void> transStatus = mBase1_0->listComponents( |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 771 | [&traits, &serviceName](Status s, |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 772 | const hidl_vec<IComponentStore::ComponentTraits>& t) { |
| 773 | if (s != Status::OK) { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 774 | LOG(DEBUG) << "_listComponents -- call failed: " |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 775 | << static_cast<c2_status_t>(s) << "."; |
| 776 | return; |
| 777 | } |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 778 | traits.resize(t.size()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 779 | for (size_t i = 0; i < t.size(); ++i) { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 780 | if (!objcpy(&traits[i], t[i])) { |
| 781 | LOG(ERROR) << "_listComponents -- corrupted output."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 782 | return; |
| 783 | } |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 784 | traits[i].owner = serviceName; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 785 | } |
| 786 | }); |
| 787 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 788 | LOG(ERROR) << "_listComponents -- transaction failed."; |
| 789 | *success = false; |
| 790 | } else { |
| 791 | *success = true; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 792 | } |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 793 | return traits; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | c2_status_t Codec2Client::copyBuffer( |
| 797 | const std::shared_ptr<C2Buffer>& src, |
| 798 | const std::shared_ptr<C2Buffer>& dst) { |
| 799 | // TODO: Implement? |
| 800 | (void)src; |
| 801 | (void)dst; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 802 | LOG(ERROR) << "copyBuffer not implemented"; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 803 | return C2_OMITTED; |
| 804 | } |
| 805 | |
| 806 | std::shared_ptr<C2ParamReflector> |
| 807 | Codec2Client::getParamReflector() { |
| 808 | // TODO: this is not meant to be exposed as C2ParamReflector on the client side; instead, it |
| 809 | // should reflect the HAL API. |
| 810 | struct SimpleParamReflector : public C2ParamReflector { |
| 811 | virtual std::unique_ptr<C2StructDescriptor> describe(C2Param::CoreIndex coreIndex) const { |
| 812 | hidl_vec<ParamIndex> indices(1); |
| 813 | indices[0] = static_cast<ParamIndex>(coreIndex.coreIndex()); |
| 814 | std::unique_ptr<C2StructDescriptor> descriptor; |
| 815 | Return<void> transStatus = mBase->getStructDescriptors( |
| 816 | indices, |
| 817 | [&descriptor]( |
| 818 | Status s, |
| 819 | const hidl_vec<StructDescriptor>& sd) { |
| 820 | c2_status_t status = static_cast<c2_status_t>(s); |
| 821 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 822 | LOG(DEBUG) << "SimpleParamReflector -- " |
| 823 | "getStructDescriptors() failed: " |
| 824 | << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 825 | descriptor.reset(); |
| 826 | return; |
| 827 | } |
| 828 | if (sd.size() != 1) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 829 | LOG(DEBUG) << "SimpleParamReflector -- " |
| 830 | "getStructDescriptors() " |
| 831 | "returned vector of size " |
| 832 | << sd.size() << ". " |
| 833 | "It should be 1."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 834 | descriptor.reset(); |
| 835 | return; |
| 836 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 837 | if (!objcpy(&descriptor, sd[0])) { |
| 838 | LOG(DEBUG) << "SimpleParamReflector -- " |
| 839 | "getStructDescriptors() returned " |
| 840 | "corrupted data."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 841 | descriptor.reset(); |
| 842 | return; |
| 843 | } |
| 844 | }); |
| 845 | return descriptor; |
| 846 | } |
| 847 | |
| 848 | SimpleParamReflector(sp<Base> base) |
| 849 | : mBase(base) { } |
| 850 | |
| 851 | sp<Base> mBase; |
| 852 | }; |
| 853 | |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 854 | return std::make_shared<SimpleParamReflector>(mBase1_0); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 855 | }; |
| 856 | |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 857 | std::vector<std::string> const& Codec2Client::GetServiceNames() { |
| 858 | static std::vector<std::string> sServiceNames{[]() { |
| 859 | using ::android::hardware::media::c2::V1_0::IComponentStore; |
| 860 | using ::android::hidl::manager::V1_2::IServiceManager; |
| 861 | |
| 862 | while (true) { |
| 863 | sp<IServiceManager> serviceManager = IServiceManager::getService(); |
| 864 | CHECK(serviceManager) << "Hardware service manager is not running."; |
| 865 | |
| 866 | // There are three categories of services based on names. |
| 867 | std::vector<std::string> defaultNames; // Prefixed with "default" |
| 868 | std::vector<std::string> vendorNames; // Prefixed with "vendor" |
| 869 | std::vector<std::string> otherNames; // Others |
| 870 | Return<void> transResult; |
| 871 | transResult = serviceManager->listManifestByInterface( |
| 872 | IComponentStore::descriptor, |
| 873 | [&defaultNames, &vendorNames, &otherNames]( |
| 874 | hidl_vec<hidl_string> const& instanceNames) { |
| 875 | for (hidl_string const& instanceName : instanceNames) { |
| 876 | char const* name = instanceName.c_str(); |
| 877 | if (strncmp(name, "default", 7) == 0) { |
| 878 | defaultNames.emplace_back(name); |
| 879 | } else if (strncmp(name, "vendor", 6) == 0) { |
| 880 | vendorNames.emplace_back(name); |
| 881 | } else { |
| 882 | otherNames.emplace_back(name); |
| 883 | } |
| 884 | } |
| 885 | }); |
| 886 | if (transResult.isOk()) { |
| 887 | // Sort service names in each category. |
| 888 | std::sort(defaultNames.begin(), defaultNames.end()); |
| 889 | std::sort(vendorNames.begin(), vendorNames.end()); |
| 890 | std::sort(otherNames.begin(), otherNames.end()); |
| 891 | |
| 892 | // Concatenate the three lists in this order: default, vendor, |
| 893 | // other. |
| 894 | std::vector<std::string>& names = defaultNames; |
| 895 | names.reserve(names.size() + vendorNames.size() + otherNames.size()); |
| 896 | names.insert(names.end(), |
| 897 | std::make_move_iterator(vendorNames.begin()), |
| 898 | std::make_move_iterator(vendorNames.end())); |
| 899 | names.insert(names.end(), |
| 900 | std::make_move_iterator(otherNames.begin()), |
| 901 | std::make_move_iterator(otherNames.end())); |
| 902 | |
| 903 | // Summarize to logcat. |
| 904 | if (names.empty()) { |
| 905 | LOG(INFO) << "No Codec2 services declared in the manifest."; |
| 906 | } else { |
| 907 | std::stringstream stringOutput; |
| 908 | stringOutput << "Available Codec2 services:"; |
| 909 | for (std::string const& name : names) { |
| 910 | stringOutput << " \"" << name << "\""; |
| 911 | } |
| 912 | LOG(INFO) << stringOutput.str(); |
| 913 | } |
| 914 | |
| 915 | return names; |
| 916 | } |
| 917 | LOG(ERROR) << "Could not retrieve the list of service instances of " |
| 918 | << IComponentStore::descriptor |
| 919 | << ". Retrying..."; |
| 920 | } |
| 921 | }()}; |
| 922 | return sServiceNames; |
| 923 | } |
| 924 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 925 | std::shared_ptr<Codec2Client> Codec2Client::CreateFromService( |
Pawin Vongmasa | 83d2c55 | 2020-03-05 04:36:08 -0800 | [diff] [blame] | 926 | const char* name, |
| 927 | bool setAsPreferredCodec2ComponentStore) { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 928 | size_t index = getServiceIndex(name); |
Pawin Vongmasa | 83d2c55 | 2020-03-05 04:36:08 -0800 | [diff] [blame] | 929 | if (index == GetServiceNames().size()) { |
| 930 | if (setAsPreferredCodec2ComponentStore) { |
| 931 | LOG(WARNING) << "CreateFromService(" << name |
| 932 | << ") -- preferred C2ComponentStore not set."; |
| 933 | } |
| 934 | return nullptr; |
| 935 | } |
| 936 | std::shared_ptr<Codec2Client> client = _CreateFromIndex(index); |
| 937 | if (setAsPreferredCodec2ComponentStore) { |
| 938 | SetPreferredCodec2ComponentStore( |
| 939 | std::make_shared<Client2Store>(client)); |
| 940 | LOG(INFO) << "CreateFromService(" << name |
| 941 | << ") -- service set as preferred C2ComponentStore."; |
| 942 | } |
| 943 | return client; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 944 | } |
| 945 | |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 946 | std::vector<std::shared_ptr<Codec2Client>> Codec2Client:: |
| 947 | CreateFromAllServices() { |
| 948 | std::vector<std::shared_ptr<Codec2Client>> clients( |
| 949 | GetServiceNames().size()); |
| 950 | for (size_t i = GetServiceNames().size(); i > 0; ) { |
| 951 | --i; |
| 952 | clients[i] = _CreateFromIndex(i); |
| 953 | } |
| 954 | return clients; |
| 955 | } |
| 956 | |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 957 | std::shared_ptr<Codec2Client> Codec2Client::_CreateFromIndex(size_t index) { |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 958 | std::string const& name = GetServiceNames()[index]; |
Pawin Vongmasa | 9aac304 | 2020-03-05 02:30:32 -0800 | [diff] [blame] | 959 | LOG(VERBOSE) << "Creating a Codec2 client to service \"" << name << "\""; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 960 | sp<Base> baseStore = Base::getService(name); |
| 961 | CHECK(baseStore) << "Codec2 service \"" << name << "\"" |
| 962 | " inaccessible for unknown reasons."; |
Pawin Vongmasa | 9aac304 | 2020-03-05 02:30:32 -0800 | [diff] [blame] | 963 | LOG(VERBOSE) << "Client to Codec2 service \"" << name << "\" created"; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 964 | return std::make_shared<Codec2Client>(baseStore, index); |
| 965 | } |
| 966 | |
| 967 | c2_status_t Codec2Client::ForAllServices( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 968 | const std::string &key, |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 969 | size_t numberOfAttempts, |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 970 | std::function<c2_status_t(const std::shared_ptr<Codec2Client>&)> |
| 971 | predicate) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 972 | c2_status_t status = C2_NO_INIT; // no IComponentStores present |
| 973 | |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 974 | // Cache the mapping key -> index of Codec2Client in Cache::List(). |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 975 | static std::mutex key2IndexMutex; |
| 976 | static std::map<std::string, size_t> key2Index; |
| 977 | |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 978 | // By default try all stores. However, try the last known client first. If |
| 979 | // the last known client fails, retry once. We do this by pushing the last |
| 980 | // known client in front of the list of all clients. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 981 | std::deque<size_t> indices; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 982 | for (size_t index = Cache::List().size(); index > 0; ) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 983 | indices.push_front(--index); |
| 984 | } |
| 985 | |
| 986 | bool wasMapped = false; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 987 | { |
| 988 | std::scoped_lock lock{key2IndexMutex}; |
| 989 | auto it = key2Index.find(key); |
| 990 | if (it != key2Index.end()) { |
| 991 | indices.push_front(it->second); |
| 992 | wasMapped = true; |
| 993 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 994 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 995 | |
| 996 | for (size_t index : indices) { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 997 | Cache& cache = Cache::List()[index]; |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 998 | for (size_t tries = numberOfAttempts; tries > 0; --tries) { |
| 999 | std::shared_ptr<Codec2Client> client{cache.getClient()}; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1000 | status = predicate(client); |
| 1001 | if (status == C2_OK) { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1002 | std::scoped_lock lock{key2IndexMutex}; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1003 | key2Index[key] = index; // update last known client index |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1004 | return C2_OK; |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 1005 | } else if (status == C2_TRANSACTION_FAILED) { |
| 1006 | LOG(WARNING) << "\"" << key << "\" failed for service \"" |
| 1007 | << client->getName() |
| 1008 | << "\" due to transaction failure. " |
| 1009 | << "(Service may have crashed.)" |
| 1010 | << (tries > 1 ? " Retrying..." : ""); |
| 1011 | cache.invalidate(); |
| 1012 | continue; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1013 | } |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 1014 | if (wasMapped) { |
| 1015 | LOG(INFO) << "\"" << key << "\" became invalid in service \"" |
| 1016 | << client->getName() << "\". Retrying..."; |
| 1017 | wasMapped = false; |
| 1018 | } |
| 1019 | break; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1020 | } |
| 1021 | } |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 1022 | return status; // return the last status from a valid client |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | std::shared_ptr<Codec2Client::Component> |
| 1026 | Codec2Client::CreateComponentByName( |
| 1027 | const char* componentName, |
| 1028 | const std::shared_ptr<Listener>& listener, |
Pawin Vongmasa | 23c90c8 | 2019-09-03 00:44:42 -0700 | [diff] [blame] | 1029 | std::shared_ptr<Codec2Client>* owner, |
| 1030 | size_t numberOfAttempts) { |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 1031 | std::string key{"create:"}; |
| 1032 | key.append(componentName); |
| 1033 | std::shared_ptr<Component> component; |
| 1034 | c2_status_t status = ForAllServices( |
| 1035 | key, |
| 1036 | numberOfAttempts, |
| 1037 | [owner, &component, componentName, &listener]( |
| 1038 | const std::shared_ptr<Codec2Client> &client) |
| 1039 | -> c2_status_t { |
| 1040 | c2_status_t status = client->createComponent(componentName, |
| 1041 | listener, |
| 1042 | &component); |
| 1043 | if (status == C2_OK) { |
| 1044 | if (owner) { |
| 1045 | *owner = client; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1046 | } |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 1047 | } else if (status != C2_NOT_FOUND) { |
| 1048 | LOG(DEBUG) << "IComponentStore(" |
| 1049 | << client->getServiceName() |
| 1050 | << ")::createComponent(\"" << componentName |
| 1051 | << "\") returned status = " |
| 1052 | << status << "."; |
| 1053 | } |
| 1054 | return status; |
| 1055 | }); |
| 1056 | if (status != C2_OK) { |
| 1057 | LOG(DEBUG) << "Failed to create component \"" << componentName |
| 1058 | << "\" from all known services. " |
| 1059 | "Last returned status = " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1060 | } |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 1061 | return component; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | std::shared_ptr<Codec2Client::Interface> |
| 1065 | Codec2Client::CreateInterfaceByName( |
| 1066 | const char* interfaceName, |
Pawin Vongmasa | 23c90c8 | 2019-09-03 00:44:42 -0700 | [diff] [blame] | 1067 | std::shared_ptr<Codec2Client>* owner, |
| 1068 | size_t numberOfAttempts) { |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 1069 | std::string key{"create:"}; |
| 1070 | key.append(interfaceName); |
| 1071 | std::shared_ptr<Interface> interface; |
| 1072 | c2_status_t status = ForAllServices( |
| 1073 | key, |
| 1074 | numberOfAttempts, |
| 1075 | [owner, &interface, interfaceName]( |
| 1076 | const std::shared_ptr<Codec2Client> &client) |
| 1077 | -> c2_status_t { |
| 1078 | c2_status_t status = client->createInterface(interfaceName, |
| 1079 | &interface); |
| 1080 | if (status == C2_OK) { |
| 1081 | if (owner) { |
| 1082 | *owner = client; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1083 | } |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 1084 | } else if (status != C2_NOT_FOUND) { |
| 1085 | LOG(DEBUG) << "IComponentStore(" |
| 1086 | << client->getServiceName() |
| 1087 | << ")::createInterface(\"" << interfaceName |
| 1088 | << "\") returned status = " |
| 1089 | << status << "."; |
| 1090 | } |
| 1091 | return status; |
| 1092 | }); |
| 1093 | if (status != C2_OK) { |
| 1094 | LOG(DEBUG) << "Failed to create interface \"" << interfaceName |
| 1095 | << "\" from all known services. " |
| 1096 | "Last returned status = " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1097 | } |
Pawin Vongmasa | 1e7015a | 2019-10-09 02:15:50 -0700 | [diff] [blame] | 1098 | return interface; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1101 | std::vector<C2Component::Traits> const& Codec2Client::ListComponents() { |
| 1102 | static std::vector<C2Component::Traits> sList{[]() { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1103 | std::vector<C2Component::Traits> list; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1104 | for (Cache& cache : Cache::List()) { |
| 1105 | std::vector<C2Component::Traits> const& traits = cache.getTraits(); |
| 1106 | list.insert(list.end(), traits.begin(), traits.end()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1107 | } |
| 1108 | return list; |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1109 | }()}; |
| 1110 | return sList; |
| 1111 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1112 | |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1113 | std::shared_ptr<Codec2Client::InputSurface> Codec2Client::CreateInputSurface( |
| 1114 | char const* serviceName) { |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1115 | int32_t inputSurfaceSetting = ::android::base::GetIntProperty( |
| 1116 | "debug.stagefright.c2inputsurface", int32_t(0)); |
| 1117 | if (inputSurfaceSetting <= 0) { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1118 | return nullptr; |
| 1119 | } |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 1120 | size_t index = GetServiceNames().size(); |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1121 | if (serviceName) { |
| 1122 | index = getServiceIndex(serviceName); |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 1123 | if (index == GetServiceNames().size()) { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1124 | LOG(DEBUG) << "CreateInputSurface -- invalid service name: \"" |
| 1125 | << serviceName << "\""; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | std::shared_ptr<Codec2Client::InputSurface> inputSurface; |
Pawin Vongmasa | 270dd6a | 2019-04-06 04:41:15 -0700 | [diff] [blame] | 1130 | if (index != GetServiceNames().size()) { |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 1131 | std::shared_ptr<Codec2Client> client = Cache::List()[index].getClient(); |
| 1132 | if (client->createInputSurface(&inputSurface) == C2_OK) { |
| 1133 | return inputSurface; |
| 1134 | } |
| 1135 | } |
| 1136 | LOG(INFO) << "CreateInputSurface -- attempting to create an input surface " |
| 1137 | "from all services..."; |
| 1138 | for (Cache& cache : Cache::List()) { |
| 1139 | std::shared_ptr<Codec2Client> client = cache.getClient(); |
| 1140 | if (client->createInputSurface(&inputSurface) == C2_OK) { |
| 1141 | LOG(INFO) << "CreateInputSurface -- input surface obtained from " |
| 1142 | "service \"" << client->getServiceName() << "\""; |
| 1143 | return inputSurface; |
| 1144 | } |
| 1145 | } |
| 1146 | LOG(WARNING) << "CreateInputSurface -- failed to create an input surface " |
| 1147 | "from all services"; |
| 1148 | return nullptr; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | // Codec2Client::Listener |
| 1152 | |
| 1153 | Codec2Client::Listener::~Listener() { |
| 1154 | } |
| 1155 | |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1156 | // Codec2Client::Interface |
| 1157 | Codec2Client::Interface::Interface(const sp<Base>& base) |
| 1158 | : Configurable{ |
| 1159 | [base]() -> sp<IConfigurable> { |
| 1160 | Return<sp<IConfigurable>> transResult = |
| 1161 | base->getConfigurable(); |
| 1162 | return transResult.isOk() ? |
| 1163 | static_cast<sp<IConfigurable>>(transResult) : |
| 1164 | nullptr; |
| 1165 | }() |
| 1166 | }, |
| 1167 | mBase{base} { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1168 | } |
| 1169 | |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1170 | // Codec2Client::Component |
| 1171 | Codec2Client::Component::Component(const sp<Base>& base) |
| 1172 | : Configurable{ |
| 1173 | [base]() -> sp<IConfigurable> { |
| 1174 | Return<sp<IComponentInterface>> transResult1 = |
| 1175 | base->getInterface(); |
| 1176 | if (!transResult1.isOk()) { |
| 1177 | return nullptr; |
| 1178 | } |
| 1179 | Return<sp<IConfigurable>> transResult2 = |
| 1180 | static_cast<sp<IComponentInterface>>(transResult1)-> |
| 1181 | getConfigurable(); |
| 1182 | return transResult2.isOk() ? |
| 1183 | static_cast<sp<IConfigurable>>(transResult2) : |
| 1184 | nullptr; |
| 1185 | }() |
| 1186 | }, |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1187 | mBase1_0{base}, |
| 1188 | mBase1_1{Base1_1::castFrom(base)}, |
| 1189 | mBufferPoolSender{std::make_unique<BufferPoolSender>()}, |
| 1190 | mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} { |
| 1191 | } |
| 1192 | |
| 1193 | Codec2Client::Component::Component(const sp<Base1_1>& base) |
| 1194 | : Configurable{ |
| 1195 | [base]() -> sp<IConfigurable> { |
| 1196 | Return<sp<IComponentInterface>> transResult1 = |
| 1197 | base->getInterface(); |
| 1198 | if (!transResult1.isOk()) { |
| 1199 | return nullptr; |
| 1200 | } |
| 1201 | Return<sp<IConfigurable>> transResult2 = |
| 1202 | static_cast<sp<IComponentInterface>>(transResult1)-> |
| 1203 | getConfigurable(); |
| 1204 | return transResult2.isOk() ? |
| 1205 | static_cast<sp<IConfigurable>>(transResult2) : |
| 1206 | nullptr; |
| 1207 | }() |
| 1208 | }, |
| 1209 | mBase1_0{base}, |
| 1210 | mBase1_1{base}, |
| 1211 | mBufferPoolSender{std::make_unique<BufferPoolSender>()}, |
| 1212 | mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | Codec2Client::Component::~Component() { |
| 1216 | } |
| 1217 | |
| 1218 | c2_status_t Codec2Client::Component::createBlockPool( |
| 1219 | C2Allocator::id_t id, |
| 1220 | C2BlockPool::local_id_t* blockPoolId, |
| 1221 | std::shared_ptr<Codec2Client::Configurable>* configurable) { |
| 1222 | c2_status_t status; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1223 | Return<void> transStatus = mBase1_0->createBlockPool( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1224 | static_cast<uint32_t>(id), |
| 1225 | [&status, blockPoolId, configurable]( |
| 1226 | Status s, |
| 1227 | uint64_t pId, |
| 1228 | const sp<IConfigurable>& c) { |
| 1229 | status = static_cast<c2_status_t>(s); |
| 1230 | configurable->reset(); |
| 1231 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1232 | LOG(DEBUG) << "createBlockPool -- call failed: " |
| 1233 | << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1234 | return; |
| 1235 | } |
| 1236 | *blockPoolId = static_cast<C2BlockPool::local_id_t>(pId); |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1237 | *configurable = std::make_shared<Configurable>(c); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1238 | }); |
| 1239 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1240 | LOG(ERROR) << "createBlockPool -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1241 | return C2_TRANSACTION_FAILED; |
| 1242 | } |
| 1243 | return status; |
| 1244 | } |
| 1245 | |
| 1246 | c2_status_t Codec2Client::Component::destroyBlockPool( |
| 1247 | C2BlockPool::local_id_t localId) { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1248 | Return<Status> transResult = mBase1_0->destroyBlockPool( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1249 | static_cast<uint64_t>(localId)); |
| 1250 | if (!transResult.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1251 | LOG(ERROR) << "destroyBlockPool -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1252 | return C2_TRANSACTION_FAILED; |
| 1253 | } |
| 1254 | return static_cast<c2_status_t>(static_cast<Status>(transResult)); |
| 1255 | } |
| 1256 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1257 | void Codec2Client::Component::handleOnWorkDone( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1258 | const std::list<std::unique_ptr<C2Work>> &workItems) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1259 | // Output bufferqueue-based blocks' lifetime management |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1260 | mOutputBufferQueue->holdBufferQueueBlocks(workItems); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | c2_status_t Codec2Client::Component::queue( |
| 1264 | std::list<std::unique_ptr<C2Work>>* const items) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1265 | WorkBundle workBundle; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1266 | if (!objcpy(&workBundle, *items, mBufferPoolSender.get())) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1267 | LOG(ERROR) << "queue -- bad input."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1268 | return C2_TRANSACTION_FAILED; |
| 1269 | } |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1270 | Return<Status> transStatus = mBase1_0->queue(workBundle); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1271 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1272 | LOG(ERROR) << "queue -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1273 | return C2_TRANSACTION_FAILED; |
| 1274 | } |
| 1275 | c2_status_t status = |
| 1276 | static_cast<c2_status_t>(static_cast<Status>(transStatus)); |
| 1277 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1278 | LOG(DEBUG) << "queue -- call failed: " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1279 | } |
| 1280 | return status; |
| 1281 | } |
| 1282 | |
| 1283 | c2_status_t Codec2Client::Component::flush( |
| 1284 | C2Component::flush_mode_t mode, |
| 1285 | std::list<std::unique_ptr<C2Work>>* const flushedWork) { |
| 1286 | (void)mode; // Flush mode isn't supported in HIDL yet. |
| 1287 | c2_status_t status; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1288 | Return<void> transStatus = mBase1_0->flush( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1289 | [&status, flushedWork]( |
| 1290 | Status s, const WorkBundle& wb) { |
| 1291 | status = static_cast<c2_status_t>(s); |
| 1292 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1293 | LOG(DEBUG) << "flush -- call failed: " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1294 | return; |
| 1295 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1296 | if (!objcpy(flushedWork, wb)) { |
| 1297 | status = C2_CORRUPTED; |
| 1298 | } else { |
| 1299 | status = C2_OK; |
| 1300 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1301 | }); |
| 1302 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1303 | LOG(ERROR) << "flush -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1304 | return C2_TRANSACTION_FAILED; |
| 1305 | } |
| 1306 | |
| 1307 | // Indices of flushed work items. |
| 1308 | std::vector<uint64_t> flushedIndices; |
| 1309 | for (const std::unique_ptr<C2Work> &work : *flushedWork) { |
| 1310 | if (work) { |
| 1311 | if (work->worklets.empty() |
| 1312 | || !work->worklets.back() |
| 1313 | || (work->worklets.back()->output.flags & |
| 1314 | C2FrameData::FLAG_INCOMPLETE) == 0) { |
| 1315 | // input is complete |
| 1316 | flushedIndices.emplace_back( |
| 1317 | work->input.ordinal.frameIndex.peeku()); |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1322 | // Output bufferqueue-based blocks' lifetime management |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1323 | mOutputBufferQueue->holdBufferQueueBlocks(*flushedWork); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1324 | |
| 1325 | return status; |
| 1326 | } |
| 1327 | |
| 1328 | c2_status_t Codec2Client::Component::drain(C2Component::drain_mode_t mode) { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1329 | Return<Status> transStatus = mBase1_0->drain( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1330 | mode == C2Component::DRAIN_COMPONENT_WITH_EOS); |
| 1331 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1332 | LOG(ERROR) << "drain -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1333 | return C2_TRANSACTION_FAILED; |
| 1334 | } |
| 1335 | c2_status_t status = |
| 1336 | static_cast<c2_status_t>(static_cast<Status>(transStatus)); |
| 1337 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1338 | LOG(DEBUG) << "drain -- call failed: " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1339 | } |
| 1340 | return status; |
| 1341 | } |
| 1342 | |
| 1343 | c2_status_t Codec2Client::Component::start() { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1344 | Return<Status> transStatus = mBase1_0->start(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1345 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1346 | LOG(ERROR) << "start -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1347 | return C2_TRANSACTION_FAILED; |
| 1348 | } |
| 1349 | c2_status_t status = |
| 1350 | static_cast<c2_status_t>(static_cast<Status>(transStatus)); |
| 1351 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1352 | LOG(DEBUG) << "start -- call failed: " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1353 | } |
| 1354 | return status; |
| 1355 | } |
| 1356 | |
| 1357 | c2_status_t Codec2Client::Component::stop() { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1358 | Return<Status> transStatus = mBase1_0->stop(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1359 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1360 | LOG(ERROR) << "stop -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1361 | return C2_TRANSACTION_FAILED; |
| 1362 | } |
| 1363 | c2_status_t status = |
| 1364 | static_cast<c2_status_t>(static_cast<Status>(transStatus)); |
| 1365 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1366 | LOG(DEBUG) << "stop -- call failed: " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1367 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1368 | return status; |
| 1369 | } |
| 1370 | |
| 1371 | c2_status_t Codec2Client::Component::reset() { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1372 | Return<Status> transStatus = mBase1_0->reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1373 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1374 | LOG(ERROR) << "reset -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1375 | return C2_TRANSACTION_FAILED; |
| 1376 | } |
| 1377 | c2_status_t status = |
| 1378 | static_cast<c2_status_t>(static_cast<Status>(transStatus)); |
| 1379 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1380 | LOG(DEBUG) << "reset -- call failed: " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1381 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1382 | return status; |
| 1383 | } |
| 1384 | |
| 1385 | c2_status_t Codec2Client::Component::release() { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1386 | Return<Status> transStatus = mBase1_0->release(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1387 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1388 | LOG(ERROR) << "release -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1389 | return C2_TRANSACTION_FAILED; |
| 1390 | } |
| 1391 | c2_status_t status = |
| 1392 | static_cast<c2_status_t>(static_cast<Status>(transStatus)); |
| 1393 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1394 | LOG(DEBUG) << "release -- call failed: " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1395 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1396 | return status; |
| 1397 | } |
| 1398 | |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1399 | c2_status_t Codec2Client::Component::configureVideoTunnel( |
| 1400 | uint32_t avSyncHwId, |
| 1401 | native_handle_t** sidebandHandle) { |
| 1402 | *sidebandHandle = nullptr; |
| 1403 | if (!mBase1_1) { |
| 1404 | return C2_OMITTED; |
| 1405 | } |
| 1406 | c2_status_t status{}; |
| 1407 | Return<void> transStatus = mBase1_1->configureVideoTunnel(avSyncHwId, |
| 1408 | [&status, sidebandHandle]( |
| 1409 | Status s, hardware::hidl_handle const& h) { |
| 1410 | status = static_cast<c2_status_t>(s); |
| 1411 | if (h.getNativeHandle()) { |
| 1412 | *sidebandHandle = native_handle_clone(h.getNativeHandle()); |
| 1413 | } |
| 1414 | }); |
| 1415 | if (!transStatus.isOk()) { |
| 1416 | LOG(ERROR) << "configureVideoTunnel -- transaction failed."; |
| 1417 | return C2_TRANSACTION_FAILED; |
| 1418 | } |
| 1419 | return status; |
| 1420 | } |
| 1421 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1422 | c2_status_t Codec2Client::Component::setOutputSurface( |
| 1423 | C2BlockPool::local_id_t blockPoolId, |
| 1424 | const sp<IGraphicBufferProducer>& surface, |
| 1425 | uint32_t generation) { |
Sungtak Lee | 0851581 | 2019-06-05 11:16:32 -0700 | [diff] [blame] | 1426 | uint64_t bqId = 0; |
| 1427 | sp<IGraphicBufferProducer> nullIgbp; |
| 1428 | sp<HGraphicBufferProducer2> nullHgbp; |
Pawin Vongmasa | 3866c7e | 2019-01-31 05:21:29 -0800 | [diff] [blame] | 1429 | |
Sungtak Lee | 0851581 | 2019-06-05 11:16:32 -0700 | [diff] [blame] | 1430 | sp<HGraphicBufferProducer2> igbp = surface ? |
| 1431 | surface->getHalInterface<HGraphicBufferProducer2>() : nullHgbp; |
| 1432 | if (surface && !igbp) { |
Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 1433 | igbp = new B2HGraphicBufferProducer2(surface); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
Sungtak Lee | 0851581 | 2019-06-05 11:16:32 -0700 | [diff] [blame] | 1436 | if (!surface) { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1437 | mOutputBufferQueue->configure(nullIgbp, generation, 0); |
Sungtak Lee | 0851581 | 2019-06-05 11:16:32 -0700 | [diff] [blame] | 1438 | } else if (surface->getUniqueId(&bqId) != OK) { |
| 1439 | LOG(ERROR) << "setOutputSurface -- " |
| 1440 | "cannot obtain bufferqueue id."; |
| 1441 | bqId = 0; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1442 | mOutputBufferQueue->configure(nullIgbp, generation, 0); |
Sungtak Lee | 0851581 | 2019-06-05 11:16:32 -0700 | [diff] [blame] | 1443 | } else { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1444 | mOutputBufferQueue->configure(surface, generation, bqId); |
Sungtak Lee | 0851581 | 2019-06-05 11:16:32 -0700 | [diff] [blame] | 1445 | } |
| 1446 | ALOGD("generation remote change %u", generation); |
| 1447 | |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1448 | Return<Status> transStatus = mBase1_0->setOutputSurface( |
Sungtak Lee | 0851581 | 2019-06-05 11:16:32 -0700 | [diff] [blame] | 1449 | static_cast<uint64_t>(blockPoolId), |
| 1450 | bqId == 0 ? nullHgbp : igbp); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1451 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1452 | LOG(ERROR) << "setOutputSurface -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1453 | return C2_TRANSACTION_FAILED; |
| 1454 | } |
| 1455 | c2_status_t status = |
| 1456 | static_cast<c2_status_t>(static_cast<Status>(transStatus)); |
| 1457 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1458 | LOG(DEBUG) << "setOutputSurface -- call failed: " << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1459 | } |
| 1460 | return status; |
| 1461 | } |
| 1462 | |
| 1463 | status_t Codec2Client::Component::queueToOutputSurface( |
| 1464 | const C2ConstGraphicBlock& block, |
| 1465 | const QueueBufferInput& input, |
| 1466 | QueueBufferOutput* output) { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1467 | return mOutputBufferQueue->outputBuffer(block, input, output); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1470 | c2_status_t Codec2Client::Component::connectToInputSurface( |
| 1471 | const std::shared_ptr<InputSurface>& inputSurface, |
| 1472 | std::shared_ptr<InputSurfaceConnection>* connection) { |
| 1473 | c2_status_t status; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1474 | Return<void> transStatus = mBase1_0->connectToInputSurface( |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1475 | inputSurface->mBase, |
| 1476 | [&status, connection]( |
| 1477 | Status s, const sp<IInputSurfaceConnection>& c) { |
| 1478 | status = static_cast<c2_status_t>(s); |
| 1479 | if (status != C2_OK) { |
| 1480 | LOG(DEBUG) << "connectToInputSurface -- call failed: " |
| 1481 | << status << "."; |
| 1482 | return; |
| 1483 | } |
| 1484 | *connection = std::make_shared<InputSurfaceConnection>(c); |
| 1485 | }); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1486 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1487 | LOG(ERROR) << "connectToInputSurface -- transaction failed"; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1488 | return C2_TRANSACTION_FAILED; |
| 1489 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1490 | return status; |
| 1491 | } |
| 1492 | |
| 1493 | c2_status_t Codec2Client::Component::connectToOmxInputSurface( |
Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 1494 | const sp<HGraphicBufferProducer1>& producer, |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1495 | const sp<HGraphicBufferSource>& source, |
| 1496 | std::shared_ptr<InputSurfaceConnection>* connection) { |
| 1497 | c2_status_t status; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1498 | Return<void> transStatus = mBase1_0->connectToOmxInputSurface( |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1499 | producer, source, |
| 1500 | [&status, connection]( |
| 1501 | Status s, const sp<IInputSurfaceConnection>& c) { |
| 1502 | status = static_cast<c2_status_t>(s); |
| 1503 | if (status != C2_OK) { |
| 1504 | LOG(DEBUG) << "connectToOmxInputSurface -- call failed: " |
| 1505 | << status << "."; |
| 1506 | return; |
| 1507 | } |
| 1508 | *connection = std::make_shared<InputSurfaceConnection>(c); |
| 1509 | }); |
| 1510 | if (!transStatus.isOk()) { |
| 1511 | LOG(ERROR) << "connectToOmxInputSurface -- transaction failed."; |
| 1512 | return C2_TRANSACTION_FAILED; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1513 | } |
| 1514 | return status; |
| 1515 | } |
| 1516 | |
| 1517 | c2_status_t Codec2Client::Component::disconnectFromInputSurface() { |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1518 | Return<Status> transStatus = mBase1_0->disconnectFromInputSurface(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1519 | if (!transStatus.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1520 | LOG(ERROR) << "disconnectToInputSurface -- transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1521 | return C2_TRANSACTION_FAILED; |
| 1522 | } |
| 1523 | c2_status_t status = |
| 1524 | static_cast<c2_status_t>(static_cast<Status>(transStatus)); |
| 1525 | if (status != C2_OK) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1526 | LOG(DEBUG) << "disconnectFromInputSurface -- call failed: " |
| 1527 | << status << "."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1528 | } |
| 1529 | return status; |
| 1530 | } |
| 1531 | |
| 1532 | c2_status_t Codec2Client::Component::setDeathListener( |
| 1533 | const std::shared_ptr<Component>& component, |
| 1534 | const std::shared_ptr<Listener>& listener) { |
| 1535 | |
| 1536 | struct HidlDeathRecipient : public hardware::hidl_death_recipient { |
| 1537 | std::weak_ptr<Component> component; |
| 1538 | std::weak_ptr<Listener> base; |
| 1539 | |
| 1540 | virtual void serviceDied( |
| 1541 | uint64_t /* cookie */, |
| 1542 | const wp<::android::hidl::base::V1_0::IBase>& /* who */ |
| 1543 | ) override { |
| 1544 | if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) { |
| 1545 | listener->onDeath(component); |
| 1546 | } else { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1547 | LOG(DEBUG) << "onDeath -- listener died."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | }; |
| 1551 | |
| 1552 | sp<HidlDeathRecipient> deathRecipient = new HidlDeathRecipient(); |
| 1553 | deathRecipient->base = listener; |
| 1554 | deathRecipient->component = component; |
| 1555 | |
| 1556 | component->mDeathRecipient = deathRecipient; |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 1557 | Return<bool> transResult = component->mBase1_0->linkToDeath( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1558 | component->mDeathRecipient, 0); |
| 1559 | if (!transResult.isOk()) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1560 | LOG(ERROR) << "setDeathListener -- linkToDeath() transaction failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1561 | return C2_TRANSACTION_FAILED; |
| 1562 | } |
| 1563 | if (!static_cast<bool>(transResult)) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1564 | LOG(DEBUG) << "setDeathListener -- linkToDeath() call failed."; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1565 | return C2_CORRUPTED; |
| 1566 | } |
| 1567 | return C2_OK; |
| 1568 | } |
| 1569 | |
| 1570 | // Codec2Client::InputSurface |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1571 | Codec2Client::InputSurface::InputSurface(const sp<IInputSurface>& base) |
| 1572 | : Configurable{ |
| 1573 | [base]() -> sp<IConfigurable> { |
| 1574 | Return<sp<IConfigurable>> transResult = |
| 1575 | base->getConfigurable(); |
| 1576 | return transResult.isOk() ? |
| 1577 | static_cast<sp<IConfigurable>>(transResult) : |
| 1578 | nullptr; |
| 1579 | }() |
| 1580 | }, |
| 1581 | mBase{base}, |
| 1582 | mGraphicBufferProducer{new |
Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 1583 | H2BGraphicBufferProducer2([base]() -> sp<HGraphicBufferProducer2> { |
| 1584 | Return<sp<HGraphicBufferProducer2>> transResult = |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1585 | base->getGraphicBufferProducer(); |
| 1586 | return transResult.isOk() ? |
Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 1587 | static_cast<sp<HGraphicBufferProducer2>>(transResult) : |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1588 | nullptr; |
| 1589 | }())} { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1590 | } |
| 1591 | |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1592 | sp<IGraphicBufferProducer> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1593 | Codec2Client::InputSurface::getGraphicBufferProducer() const { |
| 1594 | return mGraphicBufferProducer; |
| 1595 | } |
| 1596 | |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1597 | sp<IInputSurface> Codec2Client::InputSurface::getHalInterface() const { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1598 | return mBase; |
| 1599 | } |
| 1600 | |
| 1601 | // Codec2Client::InputSurfaceConnection |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1602 | Codec2Client::InputSurfaceConnection::InputSurfaceConnection( |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1603 | const sp<IInputSurfaceConnection>& base) |
| 1604 | : Configurable{ |
| 1605 | [base]() -> sp<IConfigurable> { |
| 1606 | Return<sp<IConfigurable>> transResult = |
| 1607 | base->getConfigurable(); |
| 1608 | return transResult.isOk() ? |
| 1609 | static_cast<sp<IConfigurable>>(transResult) : |
| 1610 | nullptr; |
| 1611 | }() |
| 1612 | }, |
| 1613 | mBase{base} { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | c2_status_t Codec2Client::InputSurfaceConnection::disconnect() { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1617 | Return<Status> transResult = mBase->disconnect(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1618 | return static_cast<c2_status_t>(static_cast<Status>(transResult)); |
| 1619 | } |
| 1620 | |
| 1621 | } // namespace android |
| 1622 | |