blob: eb3b9368554338a0e2d2f6c44e688db770c6a7cb [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08002 * Copyright 2018 The Android Open Source Project
Pawin Vongmasa36653902018-11-15 00:10:25 -08003 *
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 Vongmasa1c75a232019-01-09 04:41:52 -080019#include <android-base/logging.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080020
21#include <codec2/hidl/client.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070022#include <C2Debug.h>
23#include <C2BufferPriv.h>
Lajos Molnar78aa7c92021-02-18 21:39:01 -080024#include <C2Config.h> // for C2StreamUsageTuning
Pawin Vongmasabf69de92019-10-29 06:21:27 -070025#include <C2PlatformSupport.h>
26
Wonsik Kimc8fc2c32022-12-12 14:43:00 -080027#include <android/binder_auto_utils.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070028#include <android/hardware/media/bufferpool/2.0/IClientManager.h>
29#include <android/hardware/media/c2/1.0/IComponent.h>
30#include <android/hardware/media/c2/1.0/IComponentInterface.h>
31#include <android/hardware/media/c2/1.0/IComponentListener.h>
32#include <android/hardware/media/c2/1.0/IComponentStore.h>
33#include <android/hardware/media/c2/1.0/IConfigurable.h>
34#include <android/hidl/manager/1.2/IServiceManager.h>
35
Wonsik Kimc8fc2c32022-12-12 14:43:00 -080036#include <aidl/android/hardware/media/c2/FieldSupportedValues.h>
37#include <aidl/android/hardware/media/c2/FieldSupportedValuesQuery.h>
38#include <aidl/android/hardware/media/c2/FieldSupportedValuesQueryResult.h>
39#include <aidl/android/hardware/media/c2/IComponent.h>
40#include <aidl/android/hardware/media/c2/IComponentInterface.h>
41#include <aidl/android/hardware/media/c2/IComponentStore.h>
42#include <aidl/android/hardware/media/c2/IConfigurable.h>
43#include <aidl/android/hardware/media/c2/ParamDescriptor.h>
44
Pawin Vongmasabf69de92019-10-29 06:21:27 -070045#include <android-base/properties.h>
46#include <bufferpool/ClientManager.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070047#include <codec2/hidl/1.0/types.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070048#include <codec2/hidl/1.1/types.h>
Sungtak Lee8577dab2021-03-12 02:25:50 -080049#include <codec2/hidl/1.2/types.h>
Sungtak Leea714f112021-03-16 05:40:03 -070050#include <codec2/hidl/output.h>
Pawin Vongmasabf69de92019-10-29 06:21:27 -070051
52#include <cutils/native_handle.h>
53#include <gui/bufferqueue/2.0/B2HGraphicBufferProducer.h>
54#include <gui/bufferqueue/2.0/H2BGraphicBufferProducer.h>
Lajos Molnar78aa7c92021-02-18 21:39:01 -080055#include <hardware/gralloc.h> // for GRALLOC_USAGE_*
Pawin Vongmasabf69de92019-10-29 06:21:27 -070056#include <hidl/HidlSupport.h>
Lajos Molnar78aa7c92021-02-18 21:39:01 -080057#include <system/window.h> // for NATIVE_WINDOW_QUERY_*
58#include <media/stagefright/foundation/ADebug.h> // for asString(status_t)
Pawin Vongmasa36653902018-11-15 00:10:25 -080059
60#include <deque>
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070061#include <iterator>
Pawin Vongmasa36653902018-11-15 00:10:25 -080062#include <limits>
63#include <map>
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070064#include <mutex>
65#include <sstream>
66#include <thread>
Pawin Vongmasa36653902018-11-15 00:10:25 -080067#include <type_traits>
68#include <vector>
69
Pawin Vongmasa36653902018-11-15 00:10:25 -080070namespace android {
71
72using ::android::hardware::hidl_vec;
73using ::android::hardware::hidl_string;
74using ::android::hardware::Return;
75using ::android::hardware::Void;
Pawin Vongmasa36653902018-11-15 00:10:25 -080076
Pawin Vongmasaef939bf2019-03-03 04:44:59 -080077using HGraphicBufferProducer1 = ::android::hardware::graphics::bufferqueue::
78 V1_0::IGraphicBufferProducer;
79using HGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue::
80 V2_0::IGraphicBufferProducer;
81using B2HGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue::
82 V2_0::utils::B2HGraphicBufferProducer;
83using H2BGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue::
84 V2_0::utils::H2BGraphicBufferProducer;
Sungtak Leea714f112021-03-16 05:40:03 -070085using ::android::hardware::media::c2::V1_2::SurfaceSyncObj;
Pawin Vongmasaef939bf2019-03-03 04:44:59 -080086
Wonsik Kimc8fc2c32022-12-12 14:43:00 -080087namespace bufferpool_hidl = ::android::hardware::media::bufferpool::V2_0;
88namespace c2_aidl = ::aidl::android::hardware::media::c2;
89namespace c2_hidl_base = ::android::hardware::media::c2;
90namespace c2_hidl = ::android::hardware::media::c2::V1_2;
91
92using c2_hidl::utils::operator<<;
93
Pawin Vongmasa36653902018-11-15 00:10:25 -080094namespace /* unnamed */ {
95
96// c2_status_t value that corresponds to hwbinder transaction failure.
97constexpr c2_status_t C2_TRANSACTION_FAILED = C2_CORRUPTED;
98
Lajos Molnar78aa7c92021-02-18 21:39:01 -080099// By default prepare buffer to be displayed on any of the common surfaces
100constexpr uint64_t kDefaultConsumerUsage =
101 (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_COMPOSER);
102
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700103// Searches for a name in GetServiceNames() and returns the index found. If the
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700104// name is not found, the returned index will be equal to
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700105// GetServiceNames().size().
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700106size_t getServiceIndex(char const* name) {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700107 std::vector<std::string> const& names = Codec2Client::GetServiceNames();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700108 size_t i = 0;
109 for (; i < names.size(); ++i) {
110 if (name == names[i]) {
111 break;
112 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800113 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700114 return i;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800115}
116
Pawin Vongmasa83d2c552020-03-05 04:36:08 -0800117class Client2Store : public C2ComponentStore {
118 std::shared_ptr<Codec2Client> mClient;
119
120public:
121 Client2Store(std::shared_ptr<Codec2Client> const& client)
122 : mClient(client) { }
123
124 virtual ~Client2Store() = default;
125
126 virtual c2_status_t config_sm(
127 std::vector<C2Param*> const &params,
128 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
129 return mClient->config(params, C2_MAY_BLOCK, failures);
130 };
131
132 virtual c2_status_t copyBuffer(
133 std::shared_ptr<C2GraphicBuffer>,
134 std::shared_ptr<C2GraphicBuffer>) {
135 return C2_OMITTED;
136 }
137
138 virtual c2_status_t createComponent(
139 C2String, std::shared_ptr<C2Component>* const component) {
140 component->reset();
141 return C2_OMITTED;
142 }
143
144 virtual c2_status_t createInterface(
145 C2String, std::shared_ptr<C2ComponentInterface>* const interface) {
146 interface->reset();
147 return C2_OMITTED;
148 }
149
150 virtual c2_status_t query_sm(
151 std::vector<C2Param*> const& stackParams,
152 std::vector<C2Param::Index> const& heapParamIndices,
153 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
154 return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams);
155 }
156
157 virtual c2_status_t querySupportedParams_nb(
158 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
159 return mClient->querySupportedParams(params);
160 }
161
162 virtual c2_status_t querySupportedValues_sm(
163 std::vector<C2FieldSupportedValuesQuery>& fields) const {
164 return mClient->querySupportedValues(fields, C2_MAY_BLOCK);
165 }
166
167 virtual C2String getName() const {
168 return mClient->getName();
169 }
170
171 virtual std::shared_ptr<C2ParamReflector> getParamReflector() const {
172 return mClient->getParamReflector();
173 }
174
175 virtual std::vector<std::shared_ptr<C2Component::Traits const>> listComponents() {
176 return std::vector<std::shared_ptr<C2Component::Traits const>>();
177 }
178};
179
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700180} // unnamed namespace
181
182// This class caches a Codec2Client object and its component traits. The client
183// will be created the first time it is needed, and it can be refreshed if the
184// service dies (by calling invalidate()). The first time listComponents() is
185// called from the client, the result will be cached.
186class Codec2Client::Cache {
187 // Cached client
188 std::shared_ptr<Codec2Client> mClient;
189 mutable std::mutex mClientMutex;
190
191 // Cached component traits
192 std::vector<C2Component::Traits> mTraits;
193 std::once_flag mTraitsInitializationFlag;
194
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700195 // The index of the service. This is based on GetServiceNames().
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700196 size_t mIndex;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700197 // Called by s() exactly once to initialize the cache. The index must be a
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700198 // valid index into the vector returned by GetServiceNames(). Calling
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700199 // init(index) will associate the cache to the service with name
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700200 // GetServiceNames()[index].
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700201 void init(size_t index) {
202 mIndex = index;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700203 }
204
205public:
206 Cache() = default;
207
208 // Initializes mClient if needed, then returns mClient.
209 // If the service is unavailable but listed in the manifest, this function
210 // will block indefinitely.
211 std::shared_ptr<Codec2Client> getClient() {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700212 std::scoped_lock lock{mClientMutex};
213 if (!mClient) {
214 mClient = Codec2Client::_CreateFromIndex(mIndex);
215 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -0700216 CHECK(mClient) << "Failed to create Codec2Client to service \""
217 << GetServiceNames()[mIndex] << "\". (Index = "
218 << mIndex << ").";
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700219 return mClient;
220 }
221
222 // Causes a subsequent call to getClient() to create a new client. This
223 // function should be called after the service dies.
224 //
225 // Note: This function is called only by ForAllServices().
226 void invalidate() {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700227 std::scoped_lock lock{mClientMutex};
228 mClient = nullptr;
229 }
230
231 // Returns a list of traits for components supported by the service. This
232 // list is cached.
233 std::vector<C2Component::Traits> const& getTraits() {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700234 std::call_once(mTraitsInitializationFlag, [this]() {
235 bool success{false};
236 // Spin until _listComponents() is successful.
237 while (true) {
238 std::shared_ptr<Codec2Client> client = getClient();
239 mTraits = client->_listComponents(&success);
240 if (success) {
241 break;
242 }
Pawin Vongmasa21617db2020-04-24 06:16:42 -0700243 invalidate();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700244 using namespace std::chrono_literals;
245 static constexpr auto kServiceRetryPeriod = 5s;
246 LOG(INFO) << "Failed to retrieve component traits from service "
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700247 "\"" << GetServiceNames()[mIndex] << "\". "
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700248 "Retrying...";
249 std::this_thread::sleep_for(kServiceRetryPeriod);
250 }
251 });
252 return mTraits;
253 }
254
255 // List() returns the list of all caches.
256 static std::vector<Cache>& List() {
257 static std::vector<Cache> sCaches{[]() {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700258 size_t numServices = GetServiceNames().size();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700259 std::vector<Cache> caches(numServices);
260 for (size_t i = 0; i < numServices; ++i) {
261 caches[i].init(i);
262 }
263 return caches;
264 }()};
265 return sCaches;
266 }
267};
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800268// Codec2ConfigurableClient::HidlImpl
Pawin Vongmasa36653902018-11-15 00:10:25 -0800269
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800270struct Codec2ConfigurableClient::HidlImpl : public Codec2ConfigurableClient::ImplBase {
271 typedef c2_hidl::IConfigurable Base;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800272
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800273 // base cannot be null.
274 explicit HidlImpl(const sp<Base>& base);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800275
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800276 const C2String& getName() const override {
277 return mName;
278 }
279
280 c2_status_t query(
281 const std::vector<C2Param*>& stackParams,
282 const std::vector<C2Param::Index> &heapParamIndices,
283 c2_blocking_t mayBlock,
284 std::vector<std::unique_ptr<C2Param>>* const heapParams) const override;
285
286 c2_status_t config(
287 const std::vector<C2Param*> &params,
288 c2_blocking_t mayBlock,
289 std::vector<std::unique_ptr<C2SettingResult>>* const failures) override;
290
291 c2_status_t querySupportedParams(
292 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params
293 ) const override;
294
295 c2_status_t querySupportedValues(
296 std::vector<C2FieldSupportedValuesQuery>& fields,
297 c2_blocking_t mayBlock) const override;
298
299private:
300 sp<Base> mBase;
301 const C2String mName;
302};
303
304Codec2ConfigurableClient::HidlImpl::HidlImpl(const sp<Base>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800305 : mBase{base},
306 mName{[base]() -> C2String {
307 C2String outName;
308 Return<void> transStatus = base->getName(
309 [&outName](const hidl_string& name) {
310 outName = name.c_str();
311 });
312 return transStatus.isOk() ? outName : "";
313 }()} {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800314}
315
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800316c2_status_t Codec2ConfigurableClient::HidlImpl::query(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800317 const std::vector<C2Param*> &stackParams,
318 const std::vector<C2Param::Index> &heapParamIndices,
319 c2_blocking_t mayBlock,
320 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800321 hidl_vec<c2_hidl::ParamIndex> indices(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800322 stackParams.size() + heapParamIndices.size());
323 size_t numIndices = 0;
324 for (C2Param* const& stackParam : stackParams) {
325 if (!stackParam) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800326 LOG(WARNING) << "query -- null stack param encountered.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800327 continue;
328 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800329 indices[numIndices++] = static_cast<c2_hidl::ParamIndex>(stackParam->index());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800330 }
331 size_t numStackIndices = numIndices;
332 for (const C2Param::Index& index : heapParamIndices) {
333 indices[numIndices++] =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800334 static_cast<c2_hidl::ParamIndex>(static_cast<uint32_t>(index));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800335 }
336 indices.resize(numIndices);
337 if (heapParams) {
338 heapParams->reserve(heapParams->size() + numIndices);
339 }
340 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800341 Return<void> transStatus = mBase->query(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800342 indices,
343 mayBlock == C2_MAY_BLOCK,
344 [&status, &numStackIndices, &stackParams, heapParams](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800345 c2_hidl::Status s, const c2_hidl::Params& p) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800346 status = static_cast<c2_status_t>(s);
347 if (status != C2_OK && status != C2_BAD_INDEX) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800348 LOG(DEBUG) << "query -- call failed: "
349 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800350 return;
351 }
352 std::vector<C2Param*> paramPointers;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800353 if (!c2_hidl::utils::parseParamsBlob(&paramPointers, p)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800354 LOG(ERROR) << "query -- error while parsing params.";
355 status = C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800356 return;
357 }
358 size_t i = 0;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800359 for (auto it = paramPointers.begin();
360 it != paramPointers.end(); ) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800361 C2Param* paramPointer = *it;
362 if (numStackIndices > 0) {
363 --numStackIndices;
364 if (!paramPointer) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800365 LOG(WARNING) << "query -- null stack param.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800366 ++it;
367 continue;
368 }
369 for (; i < stackParams.size() && !stackParams[i]; ) {
370 ++i;
371 }
372 if (i >= stackParams.size()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800373 LOG(ERROR) << "query -- unexpected error.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800374 status = C2_CORRUPTED;
375 return;
376 }
377 if (stackParams[i]->index() != paramPointer->index()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800378 LOG(WARNING) << "query -- param skipped: "
379 "index = "
380 << stackParams[i]->index() << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800381 stackParams[i++]->invalidate();
382 continue;
383 }
384 if (!stackParams[i++]->updateFrom(*paramPointer)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800385 LOG(WARNING) << "query -- param update failed: "
386 "index = "
387 << paramPointer->index() << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800388 }
389 } else {
390 if (!paramPointer) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800391 LOG(WARNING) << "query -- null heap param.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800392 ++it;
393 continue;
394 }
395 if (!heapParams) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800396 LOG(WARNING) << "query -- "
397 "unexpected extra stack param.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800398 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800399 heapParams->emplace_back(
400 C2Param::Copy(*paramPointer));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800401 }
402 }
403 ++it;
404 }
405 });
406 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800407 LOG(ERROR) << "query -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800408 return C2_TRANSACTION_FAILED;
409 }
410 return status;
411}
412
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800413c2_status_t Codec2ConfigurableClient::HidlImpl::config(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800414 const std::vector<C2Param*> &params,
415 c2_blocking_t mayBlock,
416 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800417 c2_hidl::Params hidlParams;
418 if (!c2_hidl::utils::createParamsBlob(&hidlParams, params)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800419 LOG(ERROR) << "config -- bad input.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800420 return C2_TRANSACTION_FAILED;
421 }
422 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800423 Return<void> transStatus = mBase->config(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800424 hidlParams,
425 mayBlock == C2_MAY_BLOCK,
426 [&status, &params, failures](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800427 c2_hidl::Status s,
428 const hidl_vec<c2_hidl::SettingResult> f,
429 const c2_hidl::Params& o) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800430 status = static_cast<c2_status_t>(s);
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800431 if (status != C2_OK && status != C2_BAD_INDEX) {
432 LOG(DEBUG) << "config -- call failed: "
433 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800434 }
435 size_t i = failures->size();
436 failures->resize(i + f.size());
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800437 for (const c2_hidl::SettingResult& sf : f) {
438 if (!c2_hidl::utils::objcpy(&(*failures)[i++], sf)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800439 LOG(ERROR) << "config -- "
440 << "invalid SettingResult returned.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800441 return;
442 }
443 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800444 if (!c2_hidl::utils::updateParamsFromBlob(params, o)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800445 LOG(ERROR) << "config -- "
446 << "failed to parse returned params.";
447 status = C2_CORRUPTED;
448 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800449 });
450 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800451 LOG(ERROR) << "config -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800452 return C2_TRANSACTION_FAILED;
453 }
454 return status;
455}
456
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800457c2_status_t Codec2ConfigurableClient::HidlImpl::querySupportedParams(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800458 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
459 // TODO: Cache and query properly!
460 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800461 Return<void> transStatus = mBase->querySupportedParams(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800462 std::numeric_limits<uint32_t>::min(),
463 std::numeric_limits<uint32_t>::max(),
464 [&status, params](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800465 c2_hidl::Status s,
466 const hidl_vec<c2_hidl::ParamDescriptor>& p) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800467 status = static_cast<c2_status_t>(s);
468 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800469 LOG(DEBUG) << "querySupportedParams -- call failed: "
470 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800471 return;
472 }
473 size_t i = params->size();
474 params->resize(i + p.size());
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800475 for (const c2_hidl::ParamDescriptor& sp : p) {
476 if (!c2_hidl::utils::objcpy(&(*params)[i++], sp)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800477 LOG(ERROR) << "querySupportedParams -- "
478 << "invalid returned ParamDescriptor.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800479 return;
480 }
481 }
482 });
483 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800484 LOG(ERROR) << "querySupportedParams -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800485 return C2_TRANSACTION_FAILED;
486 }
487 return status;
488}
489
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800490c2_status_t Codec2ConfigurableClient::HidlImpl::querySupportedValues(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800491 std::vector<C2FieldSupportedValuesQuery>& fields,
492 c2_blocking_t mayBlock) const {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800493 hidl_vec<c2_hidl::FieldSupportedValuesQuery> inFields(fields.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800494 for (size_t i = 0; i < fields.size(); ++i) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800495 if (!c2_hidl::utils::objcpy(&inFields[i], fields[i])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800496 LOG(ERROR) << "querySupportedValues -- bad input";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800497 return C2_TRANSACTION_FAILED;
498 }
499 }
500
501 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800502 Return<void> transStatus = mBase->querySupportedValues(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800503 inFields,
504 mayBlock == C2_MAY_BLOCK,
505 [&status, &inFields, &fields](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800506 c2_hidl::Status s,
507 const hidl_vec<c2_hidl::FieldSupportedValuesQueryResult>& r) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800508 status = static_cast<c2_status_t>(s);
509 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800510 LOG(DEBUG) << "querySupportedValues -- call failed: "
511 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800512 return;
513 }
514 if (r.size() != fields.size()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800515 LOG(ERROR) << "querySupportedValues -- "
516 "input and output lists "
517 "have different sizes.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800518 status = C2_CORRUPTED;
519 return;
520 }
521 for (size_t i = 0; i < fields.size(); ++i) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800522 if (!c2_hidl::utils::objcpy(&fields[i], inFields[i], r[i])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800523 LOG(ERROR) << "querySupportedValues -- "
524 "invalid returned value.";
525 status = C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800526 return;
527 }
528 }
529 });
530 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800531 LOG(ERROR) << "querySupportedValues -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800532 return C2_TRANSACTION_FAILED;
533 }
534 return status;
535}
536
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800537// Codec2ConfigurableClient::AidlImpl
538
539struct Codec2ConfigurableClient::AidlImpl : public Codec2ConfigurableClient::ImplBase {
540 typedef c2_aidl::IConfigurable Base;
541
542 // base cannot be null.
543 explicit AidlImpl(const std::shared_ptr<Base>& base);
544
545 const C2String& getName() const override {
546 return mName;
547 }
548
549 c2_status_t query(
550 const std::vector<C2Param*>& stackParams,
551 const std::vector<C2Param::Index> &heapParamIndices,
552 c2_blocking_t mayBlock,
553 std::vector<std::unique_ptr<C2Param>>* const heapParams) const override;
554
555 c2_status_t config(
556 const std::vector<C2Param*> &params,
557 c2_blocking_t mayBlock,
558 std::vector<std::unique_ptr<C2SettingResult>>* const failures) override;
559
560 c2_status_t querySupportedParams(
561 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params
562 ) const override;
563
564 c2_status_t querySupportedValues(
565 std::vector<C2FieldSupportedValuesQuery>& fields,
566 c2_blocking_t mayBlock) const override;
567
568private:
569 std::shared_ptr<Base> mBase;
570 const C2String mName;
571};
572
573Codec2ConfigurableClient::AidlImpl::AidlImpl(const std::shared_ptr<Base>& base)
574 : mBase{base},
575 mName{[base]() -> C2String {
576 std::string outName;
577 ndk::ScopedAStatus status = base->getName(&outName);
578 return status.isOk() ? outName : "";
579 }()} {
580}
581
582c2_status_t Codec2ConfigurableClient::AidlImpl::query(
583 const std::vector<C2Param*> &stackParams,
584 const std::vector<C2Param::Index> &heapParamIndices,
585 c2_blocking_t mayBlock,
586 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
587 (void)stackParams, (void)heapParamIndices, (void)mayBlock, (void)heapParams;
588 // TODO: implementation
589 return C2_OMITTED;
590}
591
592c2_status_t Codec2ConfigurableClient::AidlImpl::config(
593 const std::vector<C2Param*> &params,
594 c2_blocking_t mayBlock,
595 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
596 (void)params, (void)mayBlock, (void)failures;
597 // TODO: implementation
598 return C2_OMITTED;
599}
600
601c2_status_t Codec2ConfigurableClient::AidlImpl::querySupportedParams(
602 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
603 (void)params;
604 // TODO: implementation
605 return C2_OMITTED;
606}
607
608c2_status_t Codec2ConfigurableClient::AidlImpl::querySupportedValues(
609 std::vector<C2FieldSupportedValuesQuery>& fields,
610 c2_blocking_t mayBlock) const {
611 (void)fields, (void)mayBlock;
612 // TODO: implementation
613 return C2_OMITTED;
614}
615
616// Codec2ConfigurableClient
617
618Codec2ConfigurableClient::Codec2ConfigurableClient(const sp<HidlBase> &hidlBase)
619 : mImpl(new Codec2ConfigurableClient::HidlImpl(hidlBase)) {
620}
621
622Codec2ConfigurableClient::Codec2ConfigurableClient(
623 const std::shared_ptr<AidlBase> &aidlBase)
624 : mImpl(new Codec2ConfigurableClient::AidlImpl(aidlBase)) {
625}
626
627const C2String& Codec2ConfigurableClient::getName() const {
628 return mImpl->getName();
629}
630
631c2_status_t Codec2ConfigurableClient::query(
632 const std::vector<C2Param*>& stackParams,
633 const std::vector<C2Param::Index> &heapParamIndices,
634 c2_blocking_t mayBlock,
635 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
636 return mImpl->query(stackParams, heapParamIndices, mayBlock, heapParams);
637}
638
639c2_status_t Codec2ConfigurableClient::config(
640 const std::vector<C2Param*> &params,
641 c2_blocking_t mayBlock,
642 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
643 return mImpl->config(params, mayBlock, failures);
644}
645
646c2_status_t Codec2ConfigurableClient::querySupportedParams(
647 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
648 return mImpl->querySupportedParams(params);
649}
650
651c2_status_t Codec2ConfigurableClient::querySupportedValues(
652 std::vector<C2FieldSupportedValuesQuery>& fields,
653 c2_blocking_t mayBlock) const {
654 return mImpl->querySupportedValues(fields, mayBlock);
655}
656
657
Pawin Vongmasa36653902018-11-15 00:10:25 -0800658// Codec2Client::Component::HidlListener
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800659struct Codec2Client::Component::HidlListener : public c2_hidl::IComponentListener {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800660 std::weak_ptr<Component> component;
661 std::weak_ptr<Listener> base;
662
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800663 virtual Return<void> onWorkDone(const c2_hidl::WorkBundle& workBundle) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800664 std::list<std::unique_ptr<C2Work>> workItems;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800665 if (!c2_hidl::utils::objcpy(&workItems, workBundle)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800666 LOG(DEBUG) << "onWorkDone -- received corrupted WorkBundle.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800667 return Void();
668 }
669 // release input buffers potentially held by the component from queue
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800670 std::shared_ptr<Codec2Client::Component> strongComponent =
671 component.lock();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800672 if (strongComponent) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800673 strongComponent->handleOnWorkDone(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800674 }
675 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800676 listener->onWorkDone(component, workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800677 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800678 LOG(DEBUG) << "onWorkDone -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800679 }
680 return Void();
681 }
682
683 virtual Return<void> onTripped(
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800684 const hidl_vec<c2_hidl::SettingResult>& settingResults) override {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800685 std::vector<std::shared_ptr<C2SettingResult>> c2SettingResults(
686 settingResults.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800687 for (size_t i = 0; i < settingResults.size(); ++i) {
688 std::unique_ptr<C2SettingResult> c2SettingResult;
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800689 if (!c2_hidl::utils::objcpy(&c2SettingResult, settingResults[i])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800690 LOG(DEBUG) << "onTripped -- received corrupted SettingResult.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800691 return Void();
692 }
693 c2SettingResults[i] = std::move(c2SettingResult);
694 }
695 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
696 listener->onTripped(component, c2SettingResults);
697 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800698 LOG(DEBUG) << "onTripped -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800699 }
700 return Void();
701 }
702
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800703 virtual Return<void> onError(c2_hidl::Status s, uint32_t errorCode) override {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800704 LOG(DEBUG) << "onError --"
705 << " status = " << s
706 << ", errorCode = " << errorCode
707 << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800708 if (std::shared_ptr<Listener> listener = base.lock()) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800709 listener->onError(component, s == c2_hidl::Status::OK ?
Pawin Vongmasa36653902018-11-15 00:10:25 -0800710 errorCode : static_cast<c2_status_t>(s));
711 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800712 LOG(DEBUG) << "onError -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800713 }
714 return Void();
715 }
716
717 virtual Return<void> onFramesRendered(
718 const hidl_vec<RenderedFrame>& renderedFrames) override {
719 std::shared_ptr<Listener> listener = base.lock();
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800720 if (!listener) {
721 LOG(DEBUG) << "onFramesRendered -- listener died.";
722 return Void();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800723 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800724 for (const RenderedFrame& renderedFrame : renderedFrames) {
725 listener->onFrameRendered(
726 renderedFrame.bufferQueueId,
727 renderedFrame.slotId,
728 renderedFrame.timestampNs);
729 }
730 return Void();
731 }
732
733 virtual Return<void> onInputBuffersReleased(
734 const hidl_vec<InputBuffer>& inputBuffers) override {
735 std::shared_ptr<Listener> listener = base.lock();
736 if (!listener) {
737 LOG(DEBUG) << "onInputBuffersReleased -- listener died.";
738 return Void();
739 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800740 for (const InputBuffer& inputBuffer : inputBuffers) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800741 LOG(VERBOSE) << "onInputBuffersReleased --"
742 " received death notification of"
743 " input buffer:"
744 " frameIndex = " << inputBuffer.frameIndex
745 << ", bufferIndex = " << inputBuffer.arrayIndex
746 << ".";
Wonsik Kimab34ed62019-01-31 15:28:46 -0800747 listener->onInputBufferDone(
748 inputBuffer.frameIndex, inputBuffer.arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800749 }
750 return Void();
751 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800752
Pawin Vongmasa36653902018-11-15 00:10:25 -0800753};
754
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700755// Codec2Client::Component::BufferPoolSender
756struct Codec2Client::Component::BufferPoolSender :
757 hardware::media::c2::V1_1::utils::DefaultBufferPoolSender {
758 BufferPoolSender()
759 : hardware::media::c2::V1_1::utils::DefaultBufferPoolSender() {
760 }
761};
762
763// Codec2Client::Component::OutputBufferQueue
764struct Codec2Client::Component::OutputBufferQueue :
Sungtak Leea714f112021-03-16 05:40:03 -0700765 hardware::media::c2::OutputBufferQueue {
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700766 OutputBufferQueue()
Sungtak Leea714f112021-03-16 05:40:03 -0700767 : hardware::media::c2::OutputBufferQueue() {
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700768 }
769};
770
Pawin Vongmasa36653902018-11-15 00:10:25 -0800771// Codec2Client
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700772Codec2Client::Codec2Client(sp<Base> const& base,
Sungtak Leef4b39512023-05-10 07:17:22 +0000773 sp<c2_hidl::IConfigurable> const& configurable,
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700774 size_t serviceIndex)
Sungtak Leef4b39512023-05-10 07:17:22 +0000775 : Configurable{configurable},
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700776 mBase1_0{base},
777 mBase1_1{Base1_1::castFrom(base)},
Sungtak Lee8577dab2021-03-12 02:25:50 -0800778 mBase1_2{Base1_2::castFrom(base)},
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700779 mServiceIndex{serviceIndex} {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800780 Return<sp<bufferpool_hidl::IClientManager>> transResult = base->getPoolClientManager();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800781 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800782 LOG(ERROR) << "getPoolClientManager -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800783 } else {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800784 mHostPoolManager = static_cast<sp<bufferpool_hidl::IClientManager>>(transResult);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800785 }
786}
787
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700788sp<Codec2Client::Base> const& Codec2Client::getBase() const {
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700789 return mBase1_0;
790}
791
792sp<Codec2Client::Base1_0> const& Codec2Client::getBase1_0() const {
793 return mBase1_0;
794}
795
796sp<Codec2Client::Base1_1> const& Codec2Client::getBase1_1() const {
797 return mBase1_1;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700798}
799
Sungtak Lee8577dab2021-03-12 02:25:50 -0800800sp<Codec2Client::Base1_2> const& Codec2Client::getBase1_2() const {
801 return mBase1_2;
802}
803
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700804std::string const& Codec2Client::getServiceName() const {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700805 return GetServiceNames()[mServiceIndex];
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700806}
807
Pawin Vongmasa36653902018-11-15 00:10:25 -0800808c2_status_t Codec2Client::createComponent(
809 const C2String& name,
810 const std::shared_ptr<Codec2Client::Listener>& listener,
811 std::shared_ptr<Codec2Client::Component>* const component) {
812
Pawin Vongmasa36653902018-11-15 00:10:25 -0800813 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800814 sp<Component::HidlListener> hidlListener = new Component::HidlListener{};
Pawin Vongmasa36653902018-11-15 00:10:25 -0800815 hidlListener->base = listener;
Sungtak Lee8577dab2021-03-12 02:25:50 -0800816 Return<void> transStatus;
817 if (mBase1_2) {
818 transStatus = mBase1_2->createComponent_1_2(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800819 name,
820 hidlListener,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800821 bufferpool_hidl::implementation::ClientManager::getInstance(),
Pawin Vongmasa36653902018-11-15 00:10:25 -0800822 [&status, component, hidlListener](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800823 c2_hidl::Status s,
824 const sp<c2_hidl::IComponent>& c) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800825 status = static_cast<c2_status_t>(s);
826 if (status != C2_OK) {
827 return;
828 }
829 *component = std::make_shared<Codec2Client::Component>(c);
830 hidlListener->component = *component;
Sungtak Lee8577dab2021-03-12 02:25:50 -0800831 });
832 }
833 else if (mBase1_1) {
834 transStatus = mBase1_1->createComponent_1_1(
835 name,
836 hidlListener,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800837 bufferpool_hidl::implementation::ClientManager::getInstance(),
Sungtak Lee8577dab2021-03-12 02:25:50 -0800838 [&status, component, hidlListener](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800839 c2_hidl::Status s,
840 const sp<c2_hidl_base::V1_1::IComponent>& c) {
Sungtak Lee8577dab2021-03-12 02:25:50 -0800841 status = static_cast<c2_status_t>(s);
842 if (status != C2_OK) {
843 return;
844 }
845 *component = std::make_shared<Codec2Client::Component>(c);
846 hidlListener->component = *component;
847 });
848 } else if (mBase1_0) { // ver1_0
849 transStatus = mBase1_0->createComponent(
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700850 name,
851 hidlListener,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800852 bufferpool_hidl::implementation::ClientManager::getInstance(),
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700853 [&status, component, hidlListener](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800854 c2_hidl::Status s,
855 const sp<c2_hidl_base::V1_0::IComponent>& c) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700856 status = static_cast<c2_status_t>(s);
857 if (status != C2_OK) {
858 return;
859 }
860 *component = std::make_shared<Codec2Client::Component>(c);
861 hidlListener->component = *component;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800862 });
Sungtak Lee8577dab2021-03-12 02:25:50 -0800863 } else {
864 status = C2_CORRUPTED;
865 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800866 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800867 LOG(ERROR) << "createComponent(" << name.c_str()
868 << ") -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800869 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800870 } else if (status != C2_OK) {
Pawin Vongmasa9aac3042020-03-05 02:30:32 -0800871 if (status == C2_NOT_FOUND) {
872 LOG(VERBOSE) << "createComponent(" << name.c_str()
873 << ") -- component not found.";
874 } else {
875 LOG(ERROR) << "createComponent(" << name.c_str()
876 << ") -- call failed: " << status << ".";
877 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800878 return status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800879 } else if (!*component) {
880 LOG(ERROR) << "createComponent(" << name.c_str()
881 << ") -- null component.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800882 return C2_CORRUPTED;
883 }
884
885 status = (*component)->setDeathListener(*component, listener);
886 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800887 LOG(ERROR) << "createComponent(" << name.c_str()
888 << ") -- failed to set up death listener: "
889 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800890 }
891
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700892 (*component)->mBufferPoolSender->setReceiver(mHostPoolManager);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800893 return status;
894}
895
896c2_status_t Codec2Client::createInterface(
897 const C2String& name,
898 std::shared_ptr<Codec2Client::Interface>* const interface) {
899 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700900 Return<void> transStatus = mBase1_0->createInterface(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800901 name,
902 [&status, interface](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800903 c2_hidl::Status s,
904 const sp<c2_hidl::IComponentInterface>& i) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800905 status = static_cast<c2_status_t>(s);
906 if (status != C2_OK) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800907 return;
908 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800909 *interface = std::make_shared<Interface>(i);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800910 });
911 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800912 LOG(ERROR) << "createInterface(" << name.c_str()
913 << ") -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800914 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800915 } else if (status != C2_OK) {
Pawin Vongmasa9aac3042020-03-05 02:30:32 -0800916 if (status == C2_NOT_FOUND) {
917 LOG(VERBOSE) << "createInterface(" << name.c_str()
918 << ") -- component not found.";
919 } else {
920 LOG(ERROR) << "createInterface(" << name.c_str()
921 << ") -- call failed: " << status << ".";
922 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800923 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800924 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800925
Pawin Vongmasa36653902018-11-15 00:10:25 -0800926 return status;
927}
928
929c2_status_t Codec2Client::createInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800930 std::shared_ptr<InputSurface>* const inputSurface) {
931 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700932 Return<void> transStatus = mBase1_0->createInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800933 [&status, inputSurface](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800934 c2_hidl::Status s,
935 const sp<c2_hidl::IInputSurface>& i) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800936 status = static_cast<c2_status_t>(s);
937 if (status != C2_OK) {
938 return;
939 }
940 *inputSurface = std::make_shared<InputSurface>(i);
941 });
942 if (!transStatus.isOk()) {
943 LOG(ERROR) << "createInputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800944 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800945 } else if (status != C2_OK) {
946 LOG(DEBUG) << "createInputSurface -- call failed: "
947 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800948 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800949 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800950}
951
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700952std::vector<C2Component::Traits> const& Codec2Client::listComponents() const {
953 return Cache::List()[mServiceIndex].getTraits();
954}
955
956std::vector<C2Component::Traits> Codec2Client::_listComponents(
957 bool* success) const {
958 std::vector<C2Component::Traits> traits;
959 std::string const& serviceName = getServiceName();
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700960 Return<void> transStatus = mBase1_0->listComponents(
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800961 [&traits, &serviceName](c2_hidl::Status s,
962 const hidl_vec<c2_hidl::IComponentStore::ComponentTraits>& t) {
963 if (s != c2_hidl::Status::OK) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700964 LOG(DEBUG) << "_listComponents -- call failed: "
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800965 << static_cast<c2_status_t>(s) << ".";
966 return;
967 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700968 traits.resize(t.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800969 for (size_t i = 0; i < t.size(); ++i) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -0800970 if (!c2_hidl::utils::objcpy(&traits[i], t[i])) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700971 LOG(ERROR) << "_listComponents -- corrupted output.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800972 return;
973 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700974 traits[i].owner = serviceName;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800975 }
976 });
977 if (!transStatus.isOk()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700978 LOG(ERROR) << "_listComponents -- transaction failed.";
979 *success = false;
980 } else {
981 *success = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800982 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700983 return traits;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800984}
985
986c2_status_t Codec2Client::copyBuffer(
987 const std::shared_ptr<C2Buffer>& src,
988 const std::shared_ptr<C2Buffer>& dst) {
989 // TODO: Implement?
990 (void)src;
991 (void)dst;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800992 LOG(ERROR) << "copyBuffer not implemented";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800993 return C2_OMITTED;
994}
995
996std::shared_ptr<C2ParamReflector>
997 Codec2Client::getParamReflector() {
998 // TODO: this is not meant to be exposed as C2ParamReflector on the client side; instead, it
999 // should reflect the HAL API.
1000 struct SimpleParamReflector : public C2ParamReflector {
1001 virtual std::unique_ptr<C2StructDescriptor> describe(C2Param::CoreIndex coreIndex) const {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001002 hidl_vec<c2_hidl::ParamIndex> indices(1);
1003 indices[0] = static_cast<c2_hidl::ParamIndex>(coreIndex.coreIndex());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001004 std::unique_ptr<C2StructDescriptor> descriptor;
1005 Return<void> transStatus = mBase->getStructDescriptors(
1006 indices,
1007 [&descriptor](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001008 c2_hidl::Status s,
1009 const hidl_vec<c2_hidl::StructDescriptor>& sd) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001010 c2_status_t status = static_cast<c2_status_t>(s);
1011 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001012 LOG(DEBUG) << "SimpleParamReflector -- "
1013 "getStructDescriptors() failed: "
1014 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001015 descriptor.reset();
1016 return;
1017 }
1018 if (sd.size() != 1) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001019 LOG(DEBUG) << "SimpleParamReflector -- "
1020 "getStructDescriptors() "
1021 "returned vector of size "
1022 << sd.size() << ". "
1023 "It should be 1.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001024 descriptor.reset();
1025 return;
1026 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001027 if (!c2_hidl::utils::objcpy(&descriptor, sd[0])) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001028 LOG(DEBUG) << "SimpleParamReflector -- "
1029 "getStructDescriptors() returned "
1030 "corrupted data.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001031 descriptor.reset();
1032 return;
1033 }
1034 });
Wonsik Kim492fecd2020-11-19 11:14:11 -08001035 if (!transStatus.isOk()) {
1036 LOG(DEBUG) << "SimpleParamReflector -- transaction failed: "
1037 << transStatus.description();
1038 descriptor.reset();
1039 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001040 return descriptor;
1041 }
1042
1043 SimpleParamReflector(sp<Base> base)
1044 : mBase(base) { }
1045
1046 sp<Base> mBase;
1047 };
1048
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001049 return std::make_shared<SimpleParamReflector>(mBase1_0);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001050};
1051
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001052std::vector<std::string> const& Codec2Client::GetServiceNames() {
1053 static std::vector<std::string> sServiceNames{[]() {
1054 using ::android::hardware::media::c2::V1_0::IComponentStore;
1055 using ::android::hidl::manager::V1_2::IServiceManager;
1056
1057 while (true) {
1058 sp<IServiceManager> serviceManager = IServiceManager::getService();
1059 CHECK(serviceManager) << "Hardware service manager is not running.";
1060
1061 // There are three categories of services based on names.
1062 std::vector<std::string> defaultNames; // Prefixed with "default"
1063 std::vector<std::string> vendorNames; // Prefixed with "vendor"
1064 std::vector<std::string> otherNames; // Others
1065 Return<void> transResult;
1066 transResult = serviceManager->listManifestByInterface(
1067 IComponentStore::descriptor,
1068 [&defaultNames, &vendorNames, &otherNames](
1069 hidl_vec<hidl_string> const& instanceNames) {
1070 for (hidl_string const& instanceName : instanceNames) {
1071 char const* name = instanceName.c_str();
1072 if (strncmp(name, "default", 7) == 0) {
1073 defaultNames.emplace_back(name);
1074 } else if (strncmp(name, "vendor", 6) == 0) {
1075 vendorNames.emplace_back(name);
1076 } else {
1077 otherNames.emplace_back(name);
1078 }
1079 }
1080 });
1081 if (transResult.isOk()) {
1082 // Sort service names in each category.
1083 std::sort(defaultNames.begin(), defaultNames.end());
1084 std::sort(vendorNames.begin(), vendorNames.end());
1085 std::sort(otherNames.begin(), otherNames.end());
1086
1087 // Concatenate the three lists in this order: default, vendor,
1088 // other.
1089 std::vector<std::string>& names = defaultNames;
1090 names.reserve(names.size() + vendorNames.size() + otherNames.size());
1091 names.insert(names.end(),
1092 std::make_move_iterator(vendorNames.begin()),
1093 std::make_move_iterator(vendorNames.end()));
1094 names.insert(names.end(),
1095 std::make_move_iterator(otherNames.begin()),
1096 std::make_move_iterator(otherNames.end()));
1097
1098 // Summarize to logcat.
1099 if (names.empty()) {
1100 LOG(INFO) << "No Codec2 services declared in the manifest.";
1101 } else {
1102 std::stringstream stringOutput;
1103 stringOutput << "Available Codec2 services:";
1104 for (std::string const& name : names) {
1105 stringOutput << " \"" << name << "\"";
1106 }
1107 LOG(INFO) << stringOutput.str();
1108 }
1109
1110 return names;
1111 }
1112 LOG(ERROR) << "Could not retrieve the list of service instances of "
1113 << IComponentStore::descriptor
1114 << ". Retrying...";
1115 }
1116 }()};
1117 return sServiceNames;
1118}
1119
Pawin Vongmasa36653902018-11-15 00:10:25 -08001120std::shared_ptr<Codec2Client> Codec2Client::CreateFromService(
Pawin Vongmasa83d2c552020-03-05 04:36:08 -08001121 const char* name,
1122 bool setAsPreferredCodec2ComponentStore) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001123 size_t index = getServiceIndex(name);
Pawin Vongmasa83d2c552020-03-05 04:36:08 -08001124 if (index == GetServiceNames().size()) {
1125 if (setAsPreferredCodec2ComponentStore) {
1126 LOG(WARNING) << "CreateFromService(" << name
1127 << ") -- preferred C2ComponentStore not set.";
1128 }
1129 return nullptr;
1130 }
1131 std::shared_ptr<Codec2Client> client = _CreateFromIndex(index);
1132 if (setAsPreferredCodec2ComponentStore) {
1133 SetPreferredCodec2ComponentStore(
1134 std::make_shared<Client2Store>(client));
1135 LOG(INFO) << "CreateFromService(" << name
1136 << ") -- service set as preferred C2ComponentStore.";
1137 }
1138 return client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001139}
1140
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001141std::vector<std::shared_ptr<Codec2Client>> Codec2Client::
1142 CreateFromAllServices() {
1143 std::vector<std::shared_ptr<Codec2Client>> clients(
1144 GetServiceNames().size());
1145 for (size_t i = GetServiceNames().size(); i > 0; ) {
1146 --i;
1147 clients[i] = _CreateFromIndex(i);
1148 }
1149 return clients;
1150}
1151
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001152std::shared_ptr<Codec2Client> Codec2Client::_CreateFromIndex(size_t index) {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001153 std::string const& name = GetServiceNames()[index];
Pawin Vongmasa9aac3042020-03-05 02:30:32 -08001154 LOG(VERBOSE) << "Creating a Codec2 client to service \"" << name << "\"";
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001155 sp<Base> baseStore = Base::getService(name);
1156 CHECK(baseStore) << "Codec2 service \"" << name << "\""
1157 " inaccessible for unknown reasons.";
Pawin Vongmasa9aac3042020-03-05 02:30:32 -08001158 LOG(VERBOSE) << "Client to Codec2 service \"" << name << "\" created";
Sungtak Leef4b39512023-05-10 07:17:22 +00001159 Return<sp<IConfigurable>> transResult = baseStore->getConfigurable();
1160 CHECK(transResult.isOk()) << "Codec2 service \"" << name << "\""
1161 "does not have IConfigurable.";
1162 sp<IConfigurable> configurable = static_cast<sp<IConfigurable>>(transResult);
1163 return std::make_shared<Codec2Client>(baseStore, configurable, index);
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001164}
1165
1166c2_status_t Codec2Client::ForAllServices(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001167 const std::string &key,
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001168 size_t numberOfAttempts,
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001169 std::function<c2_status_t(const std::shared_ptr<Codec2Client>&)>
1170 predicate) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001171 c2_status_t status = C2_NO_INIT; // no IComponentStores present
1172
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001173 // Cache the mapping key -> index of Codec2Client in Cache::List().
Pawin Vongmasa36653902018-11-15 00:10:25 -08001174 static std::mutex key2IndexMutex;
1175 static std::map<std::string, size_t> key2Index;
1176
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001177 // By default try all stores. However, try the last known client first. If
1178 // the last known client fails, retry once. We do this by pushing the last
1179 // known client in front of the list of all clients.
Pawin Vongmasa36653902018-11-15 00:10:25 -08001180 std::deque<size_t> indices;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001181 for (size_t index = Cache::List().size(); index > 0; ) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001182 indices.push_front(--index);
1183 }
1184
1185 bool wasMapped = false;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001186 {
1187 std::scoped_lock lock{key2IndexMutex};
1188 auto it = key2Index.find(key);
1189 if (it != key2Index.end()) {
1190 indices.push_front(it->second);
1191 wasMapped = true;
1192 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001193 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001194
1195 for (size_t index : indices) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001196 Cache& cache = Cache::List()[index];
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001197 for (size_t tries = numberOfAttempts; tries > 0; --tries) {
1198 std::shared_ptr<Codec2Client> client{cache.getClient()};
Pawin Vongmasa36653902018-11-15 00:10:25 -08001199 status = predicate(client);
1200 if (status == C2_OK) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001201 std::scoped_lock lock{key2IndexMutex};
Pawin Vongmasa36653902018-11-15 00:10:25 -08001202 key2Index[key] = index; // update last known client index
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001203 return C2_OK;
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001204 } else if (status == C2_NO_MEMORY) {
1205 return C2_NO_MEMORY;
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001206 } else if (status == C2_TRANSACTION_FAILED) {
1207 LOG(WARNING) << "\"" << key << "\" failed for service \""
1208 << client->getName()
1209 << "\" due to transaction failure. "
1210 << "(Service may have crashed.)"
1211 << (tries > 1 ? " Retrying..." : "");
1212 cache.invalidate();
1213 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001214 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001215 if (wasMapped) {
1216 LOG(INFO) << "\"" << key << "\" became invalid in service \""
1217 << client->getName() << "\". Retrying...";
1218 wasMapped = false;
1219 }
1220 break;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001221 }
1222 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001223 return status; // return the last status from a valid client
Pawin Vongmasa36653902018-11-15 00:10:25 -08001224}
1225
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001226c2_status_t Codec2Client::CreateComponentByName(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001227 const char* componentName,
1228 const std::shared_ptr<Listener>& listener,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001229 std::shared_ptr<Component>* component,
Pawin Vongmasa23c90c82019-09-03 00:44:42 -07001230 std::shared_ptr<Codec2Client>* owner,
1231 size_t numberOfAttempts) {
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001232 std::string key{"create:"};
1233 key.append(componentName);
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001234 c2_status_t status = ForAllServices(
1235 key,
1236 numberOfAttempts,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001237 [owner, component, componentName, &listener](
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001238 const std::shared_ptr<Codec2Client> &client)
1239 -> c2_status_t {
1240 c2_status_t status = client->createComponent(componentName,
1241 listener,
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001242 component);
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001243 if (status == C2_OK) {
1244 if (owner) {
1245 *owner = client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001246 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001247 } else if (status != C2_NOT_FOUND) {
1248 LOG(DEBUG) << "IComponentStore("
1249 << client->getServiceName()
1250 << ")::createComponent(\"" << componentName
1251 << "\") returned status = "
1252 << status << ".";
1253 }
1254 return status;
1255 });
1256 if (status != C2_OK) {
1257 LOG(DEBUG) << "Failed to create component \"" << componentName
1258 << "\" from all known services. "
1259 "Last returned status = " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001260 }
Chih-Yu Huangb8fe0792020-12-07 17:14:55 +09001261 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001262}
1263
1264std::shared_ptr<Codec2Client::Interface>
1265 Codec2Client::CreateInterfaceByName(
1266 const char* interfaceName,
Pawin Vongmasa23c90c82019-09-03 00:44:42 -07001267 std::shared_ptr<Codec2Client>* owner,
1268 size_t numberOfAttempts) {
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001269 std::string key{"create:"};
1270 key.append(interfaceName);
1271 std::shared_ptr<Interface> interface;
1272 c2_status_t status = ForAllServices(
1273 key,
1274 numberOfAttempts,
1275 [owner, &interface, interfaceName](
1276 const std::shared_ptr<Codec2Client> &client)
1277 -> c2_status_t {
1278 c2_status_t status = client->createInterface(interfaceName,
1279 &interface);
1280 if (status == C2_OK) {
1281 if (owner) {
1282 *owner = client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001283 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001284 } else if (status != C2_NOT_FOUND) {
1285 LOG(DEBUG) << "IComponentStore("
1286 << client->getServiceName()
1287 << ")::createInterface(\"" << interfaceName
1288 << "\") returned status = "
1289 << status << ".";
1290 }
1291 return status;
1292 });
1293 if (status != C2_OK) {
1294 LOG(DEBUG) << "Failed to create interface \"" << interfaceName
1295 << "\" from all known services. "
1296 "Last returned status = " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001297 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001298 return interface;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001299}
1300
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001301std::vector<C2Component::Traits> const& Codec2Client::ListComponents() {
1302 static std::vector<C2Component::Traits> sList{[]() {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001303 std::vector<C2Component::Traits> list;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001304 for (Cache& cache : Cache::List()) {
1305 std::vector<C2Component::Traits> const& traits = cache.getTraits();
1306 list.insert(list.end(), traits.begin(), traits.end());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001307 }
1308 return list;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001309 }()};
1310 return sList;
1311}
Pawin Vongmasa36653902018-11-15 00:10:25 -08001312
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001313std::shared_ptr<Codec2Client::InputSurface> Codec2Client::CreateInputSurface(
1314 char const* serviceName) {
Pawin Vongmasa18588322019-05-18 01:52:13 -07001315 int32_t inputSurfaceSetting = ::android::base::GetIntProperty(
1316 "debug.stagefright.c2inputsurface", int32_t(0));
1317 if (inputSurfaceSetting <= 0) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001318 return nullptr;
1319 }
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001320 size_t index = GetServiceNames().size();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001321 if (serviceName) {
1322 index = getServiceIndex(serviceName);
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001323 if (index == GetServiceNames().size()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001324 LOG(DEBUG) << "CreateInputSurface -- invalid service name: \""
1325 << serviceName << "\"";
1326 }
1327 }
1328
1329 std::shared_ptr<Codec2Client::InputSurface> inputSurface;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001330 if (index != GetServiceNames().size()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001331 std::shared_ptr<Codec2Client> client = Cache::List()[index].getClient();
1332 if (client->createInputSurface(&inputSurface) == C2_OK) {
1333 return inputSurface;
1334 }
1335 }
1336 LOG(INFO) << "CreateInputSurface -- attempting to create an input surface "
1337 "from all services...";
1338 for (Cache& cache : Cache::List()) {
1339 std::shared_ptr<Codec2Client> client = cache.getClient();
1340 if (client->createInputSurface(&inputSurface) == C2_OK) {
1341 LOG(INFO) << "CreateInputSurface -- input surface obtained from "
1342 "service \"" << client->getServiceName() << "\"";
1343 return inputSurface;
1344 }
1345 }
1346 LOG(WARNING) << "CreateInputSurface -- failed to create an input surface "
1347 "from all services";
1348 return nullptr;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001349}
1350
1351// Codec2Client::Listener
1352
1353Codec2Client::Listener::~Listener() {
1354}
1355
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001356// Codec2Client::Interface
1357Codec2Client::Interface::Interface(const sp<Base>& base)
1358 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001359 [base]() -> sp<c2_hidl::IConfigurable> {
1360 Return<sp<c2_hidl::IConfigurable>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001361 base->getConfigurable();
1362 return transResult.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001363 static_cast<sp<c2_hidl::IConfigurable>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001364 nullptr;
1365 }()
1366 },
1367 mBase{base} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001368}
1369
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001370// Codec2Client::Component
1371Codec2Client::Component::Component(const sp<Base>& base)
1372 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001373 [base]() -> sp<c2_hidl::IConfigurable> {
1374 Return<sp<c2_hidl::IComponentInterface>> transResult1 =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001375 base->getInterface();
1376 if (!transResult1.isOk()) {
1377 return nullptr;
1378 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001379 Return<sp<c2_hidl::IConfigurable>> transResult2 =
1380 static_cast<sp<c2_hidl::IComponentInterface>>(transResult1)->
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001381 getConfigurable();
1382 return transResult2.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001383 static_cast<sp<c2_hidl::IConfigurable>>(transResult2) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001384 nullptr;
1385 }()
1386 },
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001387 mBase1_0{base},
1388 mBase1_1{Base1_1::castFrom(base)},
Sungtak Lee8577dab2021-03-12 02:25:50 -08001389 mBase1_2{Base1_2::castFrom(base)},
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001390 mBufferPoolSender{std::make_unique<BufferPoolSender>()},
1391 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
1392}
1393
1394Codec2Client::Component::Component(const sp<Base1_1>& base)
1395 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001396 [base]() -> sp<c2_hidl::IConfigurable> {
1397 Return<sp<c2_hidl::IComponentInterface>> transResult1 =
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001398 base->getInterface();
1399 if (!transResult1.isOk()) {
1400 return nullptr;
1401 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001402 Return<sp<c2_hidl::IConfigurable>> transResult2 =
1403 static_cast<sp<c2_hidl::IComponentInterface>>(transResult1)->
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001404 getConfigurable();
1405 return transResult2.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001406 static_cast<sp<c2_hidl::IConfigurable>>(transResult2) :
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001407 nullptr;
1408 }()
1409 },
1410 mBase1_0{base},
1411 mBase1_1{base},
Sungtak Lee8577dab2021-03-12 02:25:50 -08001412 mBase1_2{Base1_2::castFrom(base)},
1413 mBufferPoolSender{std::make_unique<BufferPoolSender>()},
1414 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
1415}
1416
1417Codec2Client::Component::Component(const sp<Base1_2>& base)
1418 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001419 [base]() -> sp<c2_hidl::IConfigurable> {
1420 Return<sp<c2_hidl::IComponentInterface>> transResult1 =
Sungtak Lee8577dab2021-03-12 02:25:50 -08001421 base->getInterface();
1422 if (!transResult1.isOk()) {
1423 return nullptr;
1424 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001425 Return<sp<c2_hidl::IConfigurable>> transResult2 =
1426 static_cast<sp<c2_hidl::IComponentInterface>>(transResult1)->
Sungtak Lee8577dab2021-03-12 02:25:50 -08001427 getConfigurable();
1428 return transResult2.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001429 static_cast<sp<c2_hidl::IConfigurable>>(transResult2) :
Sungtak Lee8577dab2021-03-12 02:25:50 -08001430 nullptr;
1431 }()
1432 },
1433 mBase1_0{base},
1434 mBase1_1{base},
1435 mBase1_2{base},
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001436 mBufferPoolSender{std::make_unique<BufferPoolSender>()},
1437 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001438}
1439
1440Codec2Client::Component::~Component() {
1441}
1442
1443c2_status_t Codec2Client::Component::createBlockPool(
1444 C2Allocator::id_t id,
1445 C2BlockPool::local_id_t* blockPoolId,
1446 std::shared_ptr<Codec2Client::Configurable>* configurable) {
1447 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001448 Return<void> transStatus = mBase1_0->createBlockPool(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001449 static_cast<uint32_t>(id),
1450 [&status, blockPoolId, configurable](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001451 c2_hidl::Status s,
Pawin Vongmasa36653902018-11-15 00:10:25 -08001452 uint64_t pId,
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001453 const sp<c2_hidl::IConfigurable>& c) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001454 status = static_cast<c2_status_t>(s);
1455 configurable->reset();
1456 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001457 LOG(DEBUG) << "createBlockPool -- call failed: "
1458 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001459 return;
1460 }
1461 *blockPoolId = static_cast<C2BlockPool::local_id_t>(pId);
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001462 *configurable = std::make_shared<Configurable>(c);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001463 });
1464 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001465 LOG(ERROR) << "createBlockPool -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001466 return C2_TRANSACTION_FAILED;
1467 }
1468 return status;
1469}
1470
1471c2_status_t Codec2Client::Component::destroyBlockPool(
1472 C2BlockPool::local_id_t localId) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001473 Return<c2_hidl::Status> transResult = mBase1_0->destroyBlockPool(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001474 static_cast<uint64_t>(localId));
1475 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001476 LOG(ERROR) << "destroyBlockPool -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001477 return C2_TRANSACTION_FAILED;
1478 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001479 return static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transResult));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001480}
1481
Wonsik Kimab34ed62019-01-31 15:28:46 -08001482void Codec2Client::Component::handleOnWorkDone(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001483 const std::list<std::unique_ptr<C2Work>> &workItems) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001484 // Output bufferqueue-based blocks' lifetime management
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001485 mOutputBufferQueue->holdBufferQueueBlocks(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001486}
1487
1488c2_status_t Codec2Client::Component::queue(
1489 std::list<std::unique_ptr<C2Work>>* const items) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001490 c2_hidl::WorkBundle workBundle;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001491 if (!objcpy(&workBundle, *items, mBufferPoolSender.get())) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001492 LOG(ERROR) << "queue -- bad input.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001493 return C2_TRANSACTION_FAILED;
1494 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001495 Return<c2_hidl::Status> transStatus = mBase1_0->queue(workBundle);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001496 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001497 LOG(ERROR) << "queue -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001498 return C2_TRANSACTION_FAILED;
1499 }
1500 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001501 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001502 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001503 LOG(DEBUG) << "queue -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001504 }
1505 return status;
1506}
1507
1508c2_status_t Codec2Client::Component::flush(
1509 C2Component::flush_mode_t mode,
1510 std::list<std::unique_ptr<C2Work>>* const flushedWork) {
1511 (void)mode; // Flush mode isn't supported in HIDL yet.
1512 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001513 Return<void> transStatus = mBase1_0->flush(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001514 [&status, flushedWork](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001515 c2_hidl::Status s, const c2_hidl::WorkBundle& wb) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001516 status = static_cast<c2_status_t>(s);
1517 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001518 LOG(DEBUG) << "flush -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001519 return;
1520 }
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001521 if (!c2_hidl::utils::objcpy(flushedWork, wb)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001522 status = C2_CORRUPTED;
1523 } else {
1524 status = C2_OK;
1525 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001526 });
1527 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001528 LOG(ERROR) << "flush -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001529 return C2_TRANSACTION_FAILED;
1530 }
1531
1532 // Indices of flushed work items.
1533 std::vector<uint64_t> flushedIndices;
1534 for (const std::unique_ptr<C2Work> &work : *flushedWork) {
1535 if (work) {
1536 if (work->worklets.empty()
1537 || !work->worklets.back()
1538 || (work->worklets.back()->output.flags &
1539 C2FrameData::FLAG_INCOMPLETE) == 0) {
1540 // input is complete
1541 flushedIndices.emplace_back(
1542 work->input.ordinal.frameIndex.peeku());
1543 }
1544 }
1545 }
1546
Pawin Vongmasa36653902018-11-15 00:10:25 -08001547 // Output bufferqueue-based blocks' lifetime management
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001548 mOutputBufferQueue->holdBufferQueueBlocks(*flushedWork);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001549
1550 return status;
1551}
1552
1553c2_status_t Codec2Client::Component::drain(C2Component::drain_mode_t mode) {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001554 Return<c2_hidl::Status> transStatus = mBase1_0->drain(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001555 mode == C2Component::DRAIN_COMPONENT_WITH_EOS);
1556 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001557 LOG(ERROR) << "drain -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001558 return C2_TRANSACTION_FAILED;
1559 }
1560 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001561 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001562 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001563 LOG(DEBUG) << "drain -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001564 }
1565 return status;
1566}
1567
1568c2_status_t Codec2Client::Component::start() {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001569 Return<c2_hidl::Status> transStatus = mBase1_0->start();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001570 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001571 LOG(ERROR) << "start -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001572 return C2_TRANSACTION_FAILED;
1573 }
1574 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001575 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001576 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001577 LOG(DEBUG) << "start -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001578 }
1579 return status;
1580}
1581
1582c2_status_t Codec2Client::Component::stop() {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001583 Return<c2_hidl::Status> transStatus = mBase1_0->stop();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001584 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001585 LOG(ERROR) << "stop -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001586 return C2_TRANSACTION_FAILED;
1587 }
1588 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001589 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001590 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001591 LOG(DEBUG) << "stop -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001592 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001593 return status;
1594}
1595
1596c2_status_t Codec2Client::Component::reset() {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001597 Return<c2_hidl::Status> transStatus = mBase1_0->reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001598 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001599 LOG(ERROR) << "reset -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001600 return C2_TRANSACTION_FAILED;
1601 }
1602 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001603 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001604 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001605 LOG(DEBUG) << "reset -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001606 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001607 return status;
1608}
1609
1610c2_status_t Codec2Client::Component::release() {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001611 Return<c2_hidl::Status> transStatus = mBase1_0->release();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001612 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001613 LOG(ERROR) << "release -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001614 return C2_TRANSACTION_FAILED;
1615 }
1616 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001617 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001618 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001619 LOG(DEBUG) << "release -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001620 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001621 return status;
1622}
1623
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001624c2_status_t Codec2Client::Component::configureVideoTunnel(
1625 uint32_t avSyncHwId,
1626 native_handle_t** sidebandHandle) {
1627 *sidebandHandle = nullptr;
1628 if (!mBase1_1) {
1629 return C2_OMITTED;
1630 }
1631 c2_status_t status{};
1632 Return<void> transStatus = mBase1_1->configureVideoTunnel(avSyncHwId,
1633 [&status, sidebandHandle](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001634 c2_hidl::Status s, hardware::hidl_handle const& h) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001635 status = static_cast<c2_status_t>(s);
1636 if (h.getNativeHandle()) {
1637 *sidebandHandle = native_handle_clone(h.getNativeHandle());
1638 }
1639 });
1640 if (!transStatus.isOk()) {
1641 LOG(ERROR) << "configureVideoTunnel -- transaction failed.";
1642 return C2_TRANSACTION_FAILED;
1643 }
1644 return status;
1645}
1646
Pawin Vongmasa36653902018-11-15 00:10:25 -08001647c2_status_t Codec2Client::Component::setOutputSurface(
1648 C2BlockPool::local_id_t blockPoolId,
1649 const sp<IGraphicBufferProducer>& surface,
Sungtak Leedb14cba2021-04-10 00:50:23 -07001650 uint32_t generation,
1651 int maxDequeueCount) {
Sungtak Lee08515812019-06-05 11:16:32 -07001652 uint64_t bqId = 0;
1653 sp<IGraphicBufferProducer> nullIgbp;
1654 sp<HGraphicBufferProducer2> nullHgbp;
Pawin Vongmasa3866c7e2019-01-31 05:21:29 -08001655
Sungtak Lee08515812019-06-05 11:16:32 -07001656 sp<HGraphicBufferProducer2> igbp = surface ?
1657 surface->getHalInterface<HGraphicBufferProducer2>() : nullHgbp;
1658 if (surface && !igbp) {
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08001659 igbp = new B2HGraphicBufferProducer2(surface);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001660 }
1661
Sungtak Leefb579022022-05-10 06:36:15 +00001662 std::scoped_lock lock(mOutputMutex);
Sungtak Leea714f112021-03-16 05:40:03 -07001663 std::shared_ptr<SurfaceSyncObj> syncObj;
1664
Sungtak Lee08515812019-06-05 11:16:32 -07001665 if (!surface) {
Sungtak Leedb14cba2021-04-10 00:50:23 -07001666 mOutputBufferQueue->configure(nullIgbp, generation, 0, maxDequeueCount, nullptr);
Sungtak Lee08515812019-06-05 11:16:32 -07001667 } else if (surface->getUniqueId(&bqId) != OK) {
1668 LOG(ERROR) << "setOutputSurface -- "
1669 "cannot obtain bufferqueue id.";
1670 bqId = 0;
Sungtak Leedb14cba2021-04-10 00:50:23 -07001671 mOutputBufferQueue->configure(nullIgbp, generation, 0, maxDequeueCount, nullptr);
Sungtak Lee08515812019-06-05 11:16:32 -07001672 } else {
Sungtak Leedb14cba2021-04-10 00:50:23 -07001673 mOutputBufferQueue->configure(surface, generation, bqId, maxDequeueCount, mBase1_2 ?
1674 &syncObj : nullptr);
Sungtak Lee08515812019-06-05 11:16:32 -07001675 }
Lajos Molnar78aa7c92021-02-18 21:39:01 -08001676
1677 // set consumer bits
1678 // TODO: should this get incorporated into setOutputSurface method so that consumer bits
1679 // can be set atomically?
1680 uint64_t consumerUsage = kDefaultConsumerUsage;
1681 {
1682 if (surface) {
1683 int usage = 0;
1684 status_t err = surface->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &usage);
1685 if (err != NO_ERROR) {
1686 ALOGD("setOutputSurface -- failed to get consumer usage bits (%d/%s). ignoring",
1687 err, asString(err));
1688 } else {
1689 // Note: we are adding the default usage because components must support
1690 // producing output frames that can be displayed an all output surfaces.
1691
1692 // TODO: do not set usage for tunneled scenario. It is unclear if consumer usage
1693 // is meaningful in a tunneled scenario; on one hand output buffers exist, but
1694 // they do not exist inside of C2 scope. Any buffer usage shall be communicated
1695 // through the sideband channel.
1696
1697 // do an unsigned conversion as bit-31 may be 1
1698 consumerUsage = (uint32_t)usage | kDefaultConsumerUsage;
1699 }
1700 }
1701
1702 C2StreamUsageTuning::output outputUsage{
1703 0u, C2AndroidMemoryUsage::FromGrallocUsage(consumerUsage).expected};
1704 std::vector<std::unique_ptr<C2SettingResult>> failures;
1705 c2_status_t err = config({&outputUsage}, C2_MAY_BLOCK, &failures);
1706 if (err != C2_OK) {
1707 ALOGD("setOutputSurface -- failed to set consumer usage (%d/%s)",
1708 err, asString(err));
1709 }
1710 }
1711 ALOGD("setOutputSurface -- generation=%u consumer usage=%#llx%s",
1712 generation, (long long)consumerUsage, syncObj ? " sync" : "");
Sungtak Lee08515812019-06-05 11:16:32 -07001713
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001714 Return<c2_hidl::Status> transStatus = syncObj ?
Sungtak Leea714f112021-03-16 05:40:03 -07001715 mBase1_2->setOutputSurfaceWithSyncObj(
1716 static_cast<uint64_t>(blockPoolId),
1717 bqId == 0 ? nullHgbp : igbp, *syncObj) :
1718 mBase1_0->setOutputSurface(
1719 static_cast<uint64_t>(blockPoolId),
1720 bqId == 0 ? nullHgbp : igbp);
Lajos Molnar78aa7c92021-02-18 21:39:01 -08001721
Pawin Vongmasa36653902018-11-15 00:10:25 -08001722 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001723 LOG(ERROR) << "setOutputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001724 return C2_TRANSACTION_FAILED;
1725 }
1726 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001727 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001728 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001729 LOG(DEBUG) << "setOutputSurface -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001730 }
Sungtak Leea714f112021-03-16 05:40:03 -07001731 ALOGD("Surface configure completed");
Pawin Vongmasa36653902018-11-15 00:10:25 -08001732 return status;
1733}
1734
1735status_t Codec2Client::Component::queueToOutputSurface(
1736 const C2ConstGraphicBlock& block,
1737 const QueueBufferInput& input,
1738 QueueBufferOutput* output) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001739 return mOutputBufferQueue->outputBuffer(block, input, output);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001740}
1741
Brian Lindahlc953b462023-01-27 16:21:43 -07001742void Codec2Client::Component::pollForRenderedFrames(FrameEventHistoryDelta* delta) {
1743 mOutputBufferQueue->pollForRenderedFrames(delta);
1744}
1745
Sungtak Leea714f112021-03-16 05:40:03 -07001746void Codec2Client::Component::setOutputSurfaceMaxDequeueCount(
1747 int maxDequeueCount) {
1748 mOutputBufferQueue->updateMaxDequeueBufferCount(maxDequeueCount);
1749}
1750
Sungtak Leec7da7a02022-05-05 08:45:33 +00001751void Codec2Client::Component::stopUsingOutputSurface(
1752 C2BlockPool::local_id_t blockPoolId) {
Sungtak Leefb579022022-05-10 06:36:15 +00001753 std::scoped_lock lock(mOutputMutex);
Sungtak Leec7da7a02022-05-05 08:45:33 +00001754 mOutputBufferQueue->stop();
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001755 Return<c2_hidl::Status> transStatus = mBase1_0->setOutputSurface(
Sungtak Leec7da7a02022-05-05 08:45:33 +00001756 static_cast<uint64_t>(blockPoolId), nullptr);
1757 if (!transStatus.isOk()) {
1758 LOG(ERROR) << "setOutputSurface(stopUsingOutputSurface) -- transaction failed.";
1759 } else {
1760 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001761 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Sungtak Leec7da7a02022-05-05 08:45:33 +00001762 if (status != C2_OK) {
1763 LOG(DEBUG) << "setOutputSurface(stopUsingOutputSurface) -- call failed: "
1764 << status << ".";
1765 }
1766 }
1767}
1768
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001769c2_status_t Codec2Client::Component::connectToInputSurface(
1770 const std::shared_ptr<InputSurface>& inputSurface,
1771 std::shared_ptr<InputSurfaceConnection>* connection) {
1772 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001773 Return<void> transStatus = mBase1_0->connectToInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001774 inputSurface->mBase,
1775 [&status, connection](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001776 c2_hidl::Status s, const sp<c2_hidl::IInputSurfaceConnection>& c) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001777 status = static_cast<c2_status_t>(s);
1778 if (status != C2_OK) {
1779 LOG(DEBUG) << "connectToInputSurface -- call failed: "
1780 << status << ".";
1781 return;
1782 }
1783 *connection = std::make_shared<InputSurfaceConnection>(c);
1784 });
Pawin Vongmasa36653902018-11-15 00:10:25 -08001785 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001786 LOG(ERROR) << "connectToInputSurface -- transaction failed";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001787 return C2_TRANSACTION_FAILED;
1788 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001789 return status;
1790}
1791
1792c2_status_t Codec2Client::Component::connectToOmxInputSurface(
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08001793 const sp<HGraphicBufferProducer1>& producer,
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001794 const sp<HGraphicBufferSource>& source,
1795 std::shared_ptr<InputSurfaceConnection>* connection) {
1796 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001797 Return<void> transStatus = mBase1_0->connectToOmxInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001798 producer, source,
1799 [&status, connection](
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001800 c2_hidl::Status s, const sp<c2_hidl::IInputSurfaceConnection>& c) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001801 status = static_cast<c2_status_t>(s);
1802 if (status != C2_OK) {
1803 LOG(DEBUG) << "connectToOmxInputSurface -- call failed: "
1804 << status << ".";
1805 return;
1806 }
1807 *connection = std::make_shared<InputSurfaceConnection>(c);
1808 });
1809 if (!transStatus.isOk()) {
1810 LOG(ERROR) << "connectToOmxInputSurface -- transaction failed.";
1811 return C2_TRANSACTION_FAILED;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001812 }
1813 return status;
1814}
1815
1816c2_status_t Codec2Client::Component::disconnectFromInputSurface() {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001817 Return<c2_hidl::Status> transStatus = mBase1_0->disconnectFromInputSurface();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001818 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001819 LOG(ERROR) << "disconnectToInputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001820 return C2_TRANSACTION_FAILED;
1821 }
1822 c2_status_t status =
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001823 static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transStatus));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001824 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001825 LOG(DEBUG) << "disconnectFromInputSurface -- call failed: "
1826 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001827 }
1828 return status;
1829}
1830
1831c2_status_t Codec2Client::Component::setDeathListener(
1832 const std::shared_ptr<Component>& component,
1833 const std::shared_ptr<Listener>& listener) {
1834
1835 struct HidlDeathRecipient : public hardware::hidl_death_recipient {
1836 std::weak_ptr<Component> component;
1837 std::weak_ptr<Listener> base;
1838
1839 virtual void serviceDied(
1840 uint64_t /* cookie */,
1841 const wp<::android::hidl::base::V1_0::IBase>& /* who */
1842 ) override {
1843 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
1844 listener->onDeath(component);
1845 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001846 LOG(DEBUG) << "onDeath -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001847 }
1848 }
1849 };
1850
1851 sp<HidlDeathRecipient> deathRecipient = new HidlDeathRecipient();
1852 deathRecipient->base = listener;
1853 deathRecipient->component = component;
1854
1855 component->mDeathRecipient = deathRecipient;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001856 Return<bool> transResult = component->mBase1_0->linkToDeath(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001857 component->mDeathRecipient, 0);
1858 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001859 LOG(ERROR) << "setDeathListener -- linkToDeath() transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001860 return C2_TRANSACTION_FAILED;
1861 }
1862 if (!static_cast<bool>(transResult)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001863 LOG(DEBUG) << "setDeathListener -- linkToDeath() call failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001864 return C2_CORRUPTED;
1865 }
1866 return C2_OK;
1867}
1868
1869// Codec2Client::InputSurface
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001870Codec2Client::InputSurface::InputSurface(const sp<c2_hidl::IInputSurface>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001871 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001872 [base]() -> sp<c2_hidl::IConfigurable> {
1873 Return<sp<c2_hidl::IConfigurable>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001874 base->getConfigurable();
1875 return transResult.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001876 static_cast<sp<c2_hidl::IConfigurable>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001877 nullptr;
1878 }()
1879 },
1880 mBase{base},
1881 mGraphicBufferProducer{new
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08001882 H2BGraphicBufferProducer2([base]() -> sp<HGraphicBufferProducer2> {
1883 Return<sp<HGraphicBufferProducer2>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001884 base->getGraphicBufferProducer();
1885 return transResult.isOk() ?
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08001886 static_cast<sp<HGraphicBufferProducer2>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001887 nullptr;
1888 }())} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001889}
1890
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001891sp<IGraphicBufferProducer>
Pawin Vongmasa36653902018-11-15 00:10:25 -08001892 Codec2Client::InputSurface::getGraphicBufferProducer() const {
1893 return mGraphicBufferProducer;
1894}
1895
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001896sp<c2_hidl::IInputSurface> Codec2Client::InputSurface::getHalInterface() const {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001897 return mBase;
1898}
1899
1900// Codec2Client::InputSurfaceConnection
Pawin Vongmasa36653902018-11-15 00:10:25 -08001901Codec2Client::InputSurfaceConnection::InputSurfaceConnection(
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001902 const sp<c2_hidl::IInputSurfaceConnection>& base)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001903 : Configurable{
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001904 [base]() -> sp<c2_hidl::IConfigurable> {
1905 Return<sp<c2_hidl::IConfigurable>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001906 base->getConfigurable();
1907 return transResult.isOk() ?
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001908 static_cast<sp<c2_hidl::IConfigurable>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001909 nullptr;
1910 }()
1911 },
1912 mBase{base} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001913}
1914
1915c2_status_t Codec2Client::InputSurfaceConnection::disconnect() {
Wonsik Kimc8fc2c32022-12-12 14:43:00 -08001916 Return<c2_hidl::Status> transResult = mBase->disconnect();
1917 return static_cast<c2_status_t>(static_cast<c2_hidl::Status>(transResult));
Pawin Vongmasa36653902018-11-15 00:10:25 -08001918}
1919
1920} // namespace android