blob: 49b49a4341b08e02d906e833117b640884558b4a [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>
24#include <C2PlatformSupport.h>
25
26#include <android/hardware/media/bufferpool/2.0/IClientManager.h>
27#include <android/hardware/media/c2/1.0/IComponent.h>
28#include <android/hardware/media/c2/1.0/IComponentInterface.h>
29#include <android/hardware/media/c2/1.0/IComponentListener.h>
30#include <android/hardware/media/c2/1.0/IComponentStore.h>
31#include <android/hardware/media/c2/1.0/IConfigurable.h>
32#include <android/hidl/manager/1.2/IServiceManager.h>
33
34#include <android-base/properties.h>
35#include <bufferpool/ClientManager.h>
36#include <codec2/hidl/1.0/OutputBufferQueue.h>
37#include <codec2/hidl/1.0/types.h>
38#include <codec2/hidl/1.1/OutputBufferQueue.h>
39#include <codec2/hidl/1.1/types.h>
40
41#include <cutils/native_handle.h>
42#include <gui/bufferqueue/2.0/B2HGraphicBufferProducer.h>
43#include <gui/bufferqueue/2.0/H2BGraphicBufferProducer.h>
44#include <hidl/HidlSupport.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080045
46#include <deque>
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070047#include <iterator>
Pawin Vongmasa36653902018-11-15 00:10:25 -080048#include <limits>
49#include <map>
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070050#include <mutex>
51#include <sstream>
52#include <thread>
Pawin Vongmasa36653902018-11-15 00:10:25 -080053#include <type_traits>
54#include <vector>
55
Pawin Vongmasa36653902018-11-15 00:10:25 -080056namespace android {
57
58using ::android::hardware::hidl_vec;
59using ::android::hardware::hidl_string;
60using ::android::hardware::Return;
61using ::android::hardware::Void;
Pawin Vongmasa36653902018-11-15 00:10:25 -080062
Pawin Vongmasabf69de92019-10-29 06:21:27 -070063using namespace ::android::hardware::media::c2::V1_1;
64using namespace ::android::hardware::media::c2::V1_1::utils;
Sungtak Leed3318082018-09-07 15:52:43 -070065using namespace ::android::hardware::media::bufferpool::V2_0;
66using namespace ::android::hardware::media::bufferpool::V2_0::implementation;
Pawin Vongmasa36653902018-11-15 00:10:25 -080067
Pawin Vongmasaef939bf2019-03-03 04:44:59 -080068using HGraphicBufferProducer1 = ::android::hardware::graphics::bufferqueue::
69 V1_0::IGraphicBufferProducer;
70using HGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue::
71 V2_0::IGraphicBufferProducer;
72using B2HGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue::
73 V2_0::utils::B2HGraphicBufferProducer;
74using H2BGraphicBufferProducer2 = ::android::hardware::graphics::bufferqueue::
75 V2_0::utils::H2BGraphicBufferProducer;
76
Pawin Vongmasa36653902018-11-15 00:10:25 -080077namespace /* unnamed */ {
78
79// c2_status_t value that corresponds to hwbinder transaction failure.
80constexpr c2_status_t C2_TRANSACTION_FAILED = C2_CORRUPTED;
81
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -070082// Searches for a name in GetServiceNames() and returns the index found. If the
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070083// name is not found, the returned index will be equal to
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -070084// GetServiceNames().size().
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070085size_t getServiceIndex(char const* name) {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -070086 std::vector<std::string> const& names = Codec2Client::GetServiceNames();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070087 size_t i = 0;
88 for (; i < names.size(); ++i) {
89 if (name == names[i]) {
90 break;
91 }
Pawin Vongmasa36653902018-11-15 00:10:25 -080092 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -070093 return i;
Pawin Vongmasa36653902018-11-15 00:10:25 -080094}
95
Pawin Vongmasa83d2c552020-03-05 04:36:08 -080096class Client2Store : public C2ComponentStore {
97 std::shared_ptr<Codec2Client> mClient;
98
99public:
100 Client2Store(std::shared_ptr<Codec2Client> const& client)
101 : mClient(client) { }
102
103 virtual ~Client2Store() = default;
104
105 virtual c2_status_t config_sm(
106 std::vector<C2Param*> const &params,
107 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
108 return mClient->config(params, C2_MAY_BLOCK, failures);
109 };
110
111 virtual c2_status_t copyBuffer(
112 std::shared_ptr<C2GraphicBuffer>,
113 std::shared_ptr<C2GraphicBuffer>) {
114 return C2_OMITTED;
115 }
116
117 virtual c2_status_t createComponent(
118 C2String, std::shared_ptr<C2Component>* const component) {
119 component->reset();
120 return C2_OMITTED;
121 }
122
123 virtual c2_status_t createInterface(
124 C2String, std::shared_ptr<C2ComponentInterface>* const interface) {
125 interface->reset();
126 return C2_OMITTED;
127 }
128
129 virtual c2_status_t query_sm(
130 std::vector<C2Param*> const& stackParams,
131 std::vector<C2Param::Index> const& heapParamIndices,
132 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
133 return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams);
134 }
135
136 virtual c2_status_t querySupportedParams_nb(
137 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
138 return mClient->querySupportedParams(params);
139 }
140
141 virtual c2_status_t querySupportedValues_sm(
142 std::vector<C2FieldSupportedValuesQuery>& fields) const {
143 return mClient->querySupportedValues(fields, C2_MAY_BLOCK);
144 }
145
146 virtual C2String getName() const {
147 return mClient->getName();
148 }
149
150 virtual std::shared_ptr<C2ParamReflector> getParamReflector() const {
151 return mClient->getParamReflector();
152 }
153
154 virtual std::vector<std::shared_ptr<C2Component::Traits const>> listComponents() {
155 return std::vector<std::shared_ptr<C2Component::Traits const>>();
156 }
157};
158
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700159} // unnamed namespace
160
161// This class caches a Codec2Client object and its component traits. The client
162// will be created the first time it is needed, and it can be refreshed if the
163// service dies (by calling invalidate()). The first time listComponents() is
164// called from the client, the result will be cached.
165class Codec2Client::Cache {
166 // Cached client
167 std::shared_ptr<Codec2Client> mClient;
168 mutable std::mutex mClientMutex;
169
170 // Cached component traits
171 std::vector<C2Component::Traits> mTraits;
172 std::once_flag mTraitsInitializationFlag;
173
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700174 // The index of the service. This is based on GetServiceNames().
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700175 size_t mIndex;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700176 // Called by s() exactly once to initialize the cache. The index must be a
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700177 // valid index into the vector returned by GetServiceNames(). Calling
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700178 // init(index) will associate the cache to the service with name
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700179 // GetServiceNames()[index].
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700180 void init(size_t index) {
181 mIndex = index;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700182 }
183
184public:
185 Cache() = default;
186
187 // Initializes mClient if needed, then returns mClient.
188 // If the service is unavailable but listed in the manifest, this function
189 // will block indefinitely.
190 std::shared_ptr<Codec2Client> getClient() {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700191 std::scoped_lock lock{mClientMutex};
192 if (!mClient) {
193 mClient = Codec2Client::_CreateFromIndex(mIndex);
194 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -0700195 CHECK(mClient) << "Failed to create Codec2Client to service \""
196 << GetServiceNames()[mIndex] << "\". (Index = "
197 << mIndex << ").";
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700198 return mClient;
199 }
200
201 // Causes a subsequent call to getClient() to create a new client. This
202 // function should be called after the service dies.
203 //
204 // Note: This function is called only by ForAllServices().
205 void invalidate() {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700206 std::scoped_lock lock{mClientMutex};
207 mClient = nullptr;
208 }
209
210 // Returns a list of traits for components supported by the service. This
211 // list is cached.
212 std::vector<C2Component::Traits> const& getTraits() {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700213 std::call_once(mTraitsInitializationFlag, [this]() {
214 bool success{false};
215 // Spin until _listComponents() is successful.
216 while (true) {
217 std::shared_ptr<Codec2Client> client = getClient();
218 mTraits = client->_listComponents(&success);
219 if (success) {
220 break;
221 }
222 using namespace std::chrono_literals;
223 static constexpr auto kServiceRetryPeriod = 5s;
224 LOG(INFO) << "Failed to retrieve component traits from service "
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700225 "\"" << GetServiceNames()[mIndex] << "\". "
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700226 "Retrying...";
227 std::this_thread::sleep_for(kServiceRetryPeriod);
228 }
229 });
230 return mTraits;
231 }
232
233 // List() returns the list of all caches.
234 static std::vector<Cache>& List() {
235 static std::vector<Cache> sCaches{[]() {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700236 size_t numServices = GetServiceNames().size();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700237 std::vector<Cache> caches(numServices);
238 for (size_t i = 0; i < numServices; ++i) {
239 caches[i].init(i);
240 }
241 return caches;
242 }()};
243 return sCaches;
244 }
245};
Pawin Vongmasa36653902018-11-15 00:10:25 -0800246
247// Codec2ConfigurableClient
248
249const C2String& Codec2ConfigurableClient::getName() const {
250 return mName;
251}
252
Pawin Vongmasa36653902018-11-15 00:10:25 -0800253Codec2ConfigurableClient::Codec2ConfigurableClient(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800254 const sp<IConfigurable>& base)
255 : mBase{base},
256 mName{[base]() -> C2String {
257 C2String outName;
258 Return<void> transStatus = base->getName(
259 [&outName](const hidl_string& name) {
260 outName = name.c_str();
261 });
262 return transStatus.isOk() ? outName : "";
263 }()} {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800264}
265
266c2_status_t Codec2ConfigurableClient::query(
267 const std::vector<C2Param*> &stackParams,
268 const std::vector<C2Param::Index> &heapParamIndices,
269 c2_blocking_t mayBlock,
270 std::vector<std::unique_ptr<C2Param>>* const heapParams) const {
271 hidl_vec<ParamIndex> indices(
272 stackParams.size() + heapParamIndices.size());
273 size_t numIndices = 0;
274 for (C2Param* const& stackParam : stackParams) {
275 if (!stackParam) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800276 LOG(WARNING) << "query -- null stack param encountered.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800277 continue;
278 }
279 indices[numIndices++] = static_cast<ParamIndex>(stackParam->index());
280 }
281 size_t numStackIndices = numIndices;
282 for (const C2Param::Index& index : heapParamIndices) {
283 indices[numIndices++] =
284 static_cast<ParamIndex>(static_cast<uint32_t>(index));
285 }
286 indices.resize(numIndices);
287 if (heapParams) {
288 heapParams->reserve(heapParams->size() + numIndices);
289 }
290 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800291 Return<void> transStatus = mBase->query(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800292 indices,
293 mayBlock == C2_MAY_BLOCK,
294 [&status, &numStackIndices, &stackParams, heapParams](
295 Status s, const Params& p) {
296 status = static_cast<c2_status_t>(s);
297 if (status != C2_OK && status != C2_BAD_INDEX) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800298 LOG(DEBUG) << "query -- call failed: "
299 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800300 return;
301 }
302 std::vector<C2Param*> paramPointers;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800303 if (!parseParamsBlob(&paramPointers, p)) {
304 LOG(ERROR) << "query -- error while parsing params.";
305 status = C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800306 return;
307 }
308 size_t i = 0;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800309 for (auto it = paramPointers.begin();
310 it != paramPointers.end(); ) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800311 C2Param* paramPointer = *it;
312 if (numStackIndices > 0) {
313 --numStackIndices;
314 if (!paramPointer) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800315 LOG(WARNING) << "query -- null stack param.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800316 ++it;
317 continue;
318 }
319 for (; i < stackParams.size() && !stackParams[i]; ) {
320 ++i;
321 }
322 if (i >= stackParams.size()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800323 LOG(ERROR) << "query -- unexpected error.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800324 status = C2_CORRUPTED;
325 return;
326 }
327 if (stackParams[i]->index() != paramPointer->index()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800328 LOG(WARNING) << "query -- param skipped: "
329 "index = "
330 << stackParams[i]->index() << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800331 stackParams[i++]->invalidate();
332 continue;
333 }
334 if (!stackParams[i++]->updateFrom(*paramPointer)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800335 LOG(WARNING) << "query -- param update failed: "
336 "index = "
337 << paramPointer->index() << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800338 }
339 } else {
340 if (!paramPointer) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800341 LOG(WARNING) << "query -- null heap param.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800342 ++it;
343 continue;
344 }
345 if (!heapParams) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800346 LOG(WARNING) << "query -- "
347 "unexpected extra stack param.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800348 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800349 heapParams->emplace_back(
350 C2Param::Copy(*paramPointer));
Pawin Vongmasa36653902018-11-15 00:10:25 -0800351 }
352 }
353 ++it;
354 }
355 });
356 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800357 LOG(ERROR) << "query -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800358 return C2_TRANSACTION_FAILED;
359 }
360 return status;
361}
362
363c2_status_t Codec2ConfigurableClient::config(
364 const std::vector<C2Param*> &params,
365 c2_blocking_t mayBlock,
366 std::vector<std::unique_ptr<C2SettingResult>>* const failures) {
367 Params hidlParams;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800368 if (!createParamsBlob(&hidlParams, params)) {
369 LOG(ERROR) << "config -- bad input.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800370 return C2_TRANSACTION_FAILED;
371 }
372 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800373 Return<void> transStatus = mBase->config(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800374 hidlParams,
375 mayBlock == C2_MAY_BLOCK,
376 [&status, &params, failures](
377 Status s,
378 const hidl_vec<SettingResult> f,
379 const Params& o) {
380 status = static_cast<c2_status_t>(s);
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800381 if (status != C2_OK && status != C2_BAD_INDEX) {
382 LOG(DEBUG) << "config -- call failed: "
383 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800384 }
385 size_t i = failures->size();
386 failures->resize(i + f.size());
387 for (const SettingResult& sf : f) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800388 if (!objcpy(&(*failures)[i++], sf)) {
389 LOG(ERROR) << "config -- "
390 << "invalid SettingResult returned.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800391 return;
392 }
393 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800394 if (!updateParamsFromBlob(params, o)) {
395 LOG(ERROR) << "config -- "
396 << "failed to parse returned params.";
397 status = C2_CORRUPTED;
398 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800399 });
400 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800401 LOG(ERROR) << "config -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800402 return C2_TRANSACTION_FAILED;
403 }
404 return status;
405}
406
407c2_status_t Codec2ConfigurableClient::querySupportedParams(
408 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const {
409 // TODO: Cache and query properly!
410 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800411 Return<void> transStatus = mBase->querySupportedParams(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800412 std::numeric_limits<uint32_t>::min(),
413 std::numeric_limits<uint32_t>::max(),
414 [&status, params](
415 Status s,
416 const hidl_vec<ParamDescriptor>& p) {
417 status = static_cast<c2_status_t>(s);
418 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800419 LOG(DEBUG) << "querySupportedParams -- call failed: "
420 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800421 return;
422 }
423 size_t i = params->size();
424 params->resize(i + p.size());
425 for (const ParamDescriptor& sp : p) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800426 if (!objcpy(&(*params)[i++], sp)) {
427 LOG(ERROR) << "querySupportedParams -- "
428 << "invalid returned ParamDescriptor.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800429 return;
430 }
431 }
432 });
433 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800434 LOG(ERROR) << "querySupportedParams -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800435 return C2_TRANSACTION_FAILED;
436 }
437 return status;
438}
439
440c2_status_t Codec2ConfigurableClient::querySupportedValues(
441 std::vector<C2FieldSupportedValuesQuery>& fields,
442 c2_blocking_t mayBlock) const {
443 hidl_vec<FieldSupportedValuesQuery> inFields(fields.size());
444 for (size_t i = 0; i < fields.size(); ++i) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800445 if (!objcpy(&inFields[i], fields[i])) {
446 LOG(ERROR) << "querySupportedValues -- bad input";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800447 return C2_TRANSACTION_FAILED;
448 }
449 }
450
451 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800452 Return<void> transStatus = mBase->querySupportedValues(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800453 inFields,
454 mayBlock == C2_MAY_BLOCK,
455 [&status, &inFields, &fields](
456 Status s,
457 const hidl_vec<FieldSupportedValuesQueryResult>& r) {
458 status = static_cast<c2_status_t>(s);
459 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800460 LOG(DEBUG) << "querySupportedValues -- call failed: "
461 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800462 return;
463 }
464 if (r.size() != fields.size()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800465 LOG(ERROR) << "querySupportedValues -- "
466 "input and output lists "
467 "have different sizes.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800468 status = C2_CORRUPTED;
469 return;
470 }
471 for (size_t i = 0; i < fields.size(); ++i) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800472 if (!objcpy(&fields[i], inFields[i], r[i])) {
473 LOG(ERROR) << "querySupportedValues -- "
474 "invalid returned value.";
475 status = C2_CORRUPTED;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800476 return;
477 }
478 }
479 });
480 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800481 LOG(ERROR) << "querySupportedValues -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800482 return C2_TRANSACTION_FAILED;
483 }
484 return status;
485}
486
487// Codec2Client::Component::HidlListener
488struct Codec2Client::Component::HidlListener : public IComponentListener {
489 std::weak_ptr<Component> component;
490 std::weak_ptr<Listener> base;
491
492 virtual Return<void> onWorkDone(const WorkBundle& workBundle) override {
493 std::list<std::unique_ptr<C2Work>> workItems;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800494 if (!objcpy(&workItems, workBundle)) {
495 LOG(DEBUG) << "onWorkDone -- received corrupted WorkBundle.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800496 return Void();
497 }
498 // release input buffers potentially held by the component from queue
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800499 std::shared_ptr<Codec2Client::Component> strongComponent =
500 component.lock();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800501 if (strongComponent) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800502 strongComponent->handleOnWorkDone(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800503 }
504 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
Wonsik Kimab34ed62019-01-31 15:28:46 -0800505 listener->onWorkDone(component, workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800506 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800507 LOG(DEBUG) << "onWorkDone -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800508 }
509 return Void();
510 }
511
512 virtual Return<void> onTripped(
513 const hidl_vec<SettingResult>& settingResults) override {
514 std::vector<std::shared_ptr<C2SettingResult>> c2SettingResults(
515 settingResults.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800516 for (size_t i = 0; i < settingResults.size(); ++i) {
517 std::unique_ptr<C2SettingResult> c2SettingResult;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800518 if (!objcpy(&c2SettingResult, settingResults[i])) {
519 LOG(DEBUG) << "onTripped -- received corrupted SettingResult.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800520 return Void();
521 }
522 c2SettingResults[i] = std::move(c2SettingResult);
523 }
524 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
525 listener->onTripped(component, c2SettingResults);
526 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800527 LOG(DEBUG) << "onTripped -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800528 }
529 return Void();
530 }
531
532 virtual Return<void> onError(Status s, uint32_t errorCode) override {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800533 LOG(DEBUG) << "onError --"
534 << " status = " << s
535 << ", errorCode = " << errorCode
536 << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800537 if (std::shared_ptr<Listener> listener = base.lock()) {
538 listener->onError(component, s == Status::OK ?
539 errorCode : static_cast<c2_status_t>(s));
540 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800541 LOG(DEBUG) << "onError -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800542 }
543 return Void();
544 }
545
546 virtual Return<void> onFramesRendered(
547 const hidl_vec<RenderedFrame>& renderedFrames) override {
548 std::shared_ptr<Listener> listener = base.lock();
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800549 if (!listener) {
550 LOG(DEBUG) << "onFramesRendered -- listener died.";
551 return Void();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800552 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800553 for (const RenderedFrame& renderedFrame : renderedFrames) {
554 listener->onFrameRendered(
555 renderedFrame.bufferQueueId,
556 renderedFrame.slotId,
557 renderedFrame.timestampNs);
558 }
559 return Void();
560 }
561
562 virtual Return<void> onInputBuffersReleased(
563 const hidl_vec<InputBuffer>& inputBuffers) override {
564 std::shared_ptr<Listener> listener = base.lock();
565 if (!listener) {
566 LOG(DEBUG) << "onInputBuffersReleased -- listener died.";
567 return Void();
568 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800569 for (const InputBuffer& inputBuffer : inputBuffers) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800570 LOG(VERBOSE) << "onInputBuffersReleased --"
571 " received death notification of"
572 " input buffer:"
573 " frameIndex = " << inputBuffer.frameIndex
574 << ", bufferIndex = " << inputBuffer.arrayIndex
575 << ".";
Wonsik Kimab34ed62019-01-31 15:28:46 -0800576 listener->onInputBufferDone(
577 inputBuffer.frameIndex, inputBuffer.arrayIndex);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800578 }
579 return Void();
580 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800581
Pawin Vongmasa36653902018-11-15 00:10:25 -0800582};
583
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700584// Codec2Client::Component::BufferPoolSender
585struct Codec2Client::Component::BufferPoolSender :
586 hardware::media::c2::V1_1::utils::DefaultBufferPoolSender {
587 BufferPoolSender()
588 : hardware::media::c2::V1_1::utils::DefaultBufferPoolSender() {
589 }
590};
591
592// Codec2Client::Component::OutputBufferQueue
593struct Codec2Client::Component::OutputBufferQueue :
594 hardware::media::c2::V1_1::utils::OutputBufferQueue {
595 OutputBufferQueue()
596 : hardware::media::c2::V1_1::utils::OutputBufferQueue() {
597 }
598};
599
Pawin Vongmasa36653902018-11-15 00:10:25 -0800600// Codec2Client
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700601Codec2Client::Codec2Client(sp<Base> const& base,
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700602 size_t serviceIndex)
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800603 : Configurable{
604 [base]() -> sp<IConfigurable> {
605 Return<sp<IConfigurable>> transResult =
606 base->getConfigurable();
607 return transResult.isOk() ?
608 static_cast<sp<IConfigurable>>(transResult) :
609 nullptr;
610 }()
611 },
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700612 mBase1_0{base},
613 mBase1_1{Base1_1::castFrom(base)},
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700614 mServiceIndex{serviceIndex} {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800615 Return<sp<IClientManager>> transResult = base->getPoolClientManager();
616 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800617 LOG(ERROR) << "getPoolClientManager -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800618 } else {
619 mHostPoolManager = static_cast<sp<IClientManager>>(transResult);
620 }
621}
622
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700623sp<Codec2Client::Base> const& Codec2Client::getBase() const {
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700624 return mBase1_0;
625}
626
627sp<Codec2Client::Base1_0> const& Codec2Client::getBase1_0() const {
628 return mBase1_0;
629}
630
631sp<Codec2Client::Base1_1> const& Codec2Client::getBase1_1() const {
632 return mBase1_1;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700633}
634
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700635std::string const& Codec2Client::getServiceName() const {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700636 return GetServiceNames()[mServiceIndex];
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700637}
638
Pawin Vongmasa36653902018-11-15 00:10:25 -0800639c2_status_t Codec2Client::createComponent(
640 const C2String& name,
641 const std::shared_ptr<Codec2Client::Listener>& listener,
642 std::shared_ptr<Codec2Client::Component>* const component) {
643
Pawin Vongmasa36653902018-11-15 00:10:25 -0800644 c2_status_t status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800645 sp<Component::HidlListener> hidlListener = new Component::HidlListener{};
Pawin Vongmasa36653902018-11-15 00:10:25 -0800646 hidlListener->base = listener;
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700647 Return<void> transStatus = mBase1_1 ?
648 mBase1_1->createComponent_1_1(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800649 name,
650 hidlListener,
651 ClientManager::getInstance(),
652 [&status, component, hidlListener](
653 Status s,
654 const sp<IComponent>& c) {
655 status = static_cast<c2_status_t>(s);
656 if (status != C2_OK) {
657 return;
658 }
659 *component = std::make_shared<Codec2Client::Component>(c);
660 hidlListener->component = *component;
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700661 }) :
662 mBase1_0->createComponent(
663 name,
664 hidlListener,
665 ClientManager::getInstance(),
666 [&status, component, hidlListener](
667 Status s,
668 const sp<hardware::media::c2::V1_0::IComponent>& c) {
669 status = static_cast<c2_status_t>(s);
670 if (status != C2_OK) {
671 return;
672 }
673 *component = std::make_shared<Codec2Client::Component>(c);
674 hidlListener->component = *component;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800675 });
676 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800677 LOG(ERROR) << "createComponent(" << name.c_str()
678 << ") -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800679 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800680 } else if (status != C2_OK) {
681 LOG(ERROR) << "createComponent(" << name.c_str()
682 << ") -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800683 return status;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800684 } else if (!*component) {
685 LOG(ERROR) << "createComponent(" << name.c_str()
686 << ") -- null component.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800687 return C2_CORRUPTED;
688 }
689
690 status = (*component)->setDeathListener(*component, listener);
691 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800692 LOG(ERROR) << "createComponent(" << name.c_str()
693 << ") -- failed to set up death listener: "
694 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800695 }
696
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700697 (*component)->mBufferPoolSender->setReceiver(mHostPoolManager);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800698 return status;
699}
700
701c2_status_t Codec2Client::createInterface(
702 const C2String& name,
703 std::shared_ptr<Codec2Client::Interface>* const interface) {
704 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700705 Return<void> transStatus = mBase1_0->createInterface(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800706 name,
707 [&status, interface](
708 Status s,
709 const sp<IComponentInterface>& i) {
710 status = static_cast<c2_status_t>(s);
711 if (status != C2_OK) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800712 return;
713 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800714 *interface = std::make_shared<Interface>(i);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800715 });
716 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800717 LOG(ERROR) << "createInterface(" << name.c_str()
718 << ") -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800719 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800720 } else if (status != C2_OK) {
721 LOG(ERROR) << "createComponent(" << name.c_str()
722 << ") -- call failed: " << status << ".";
723 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800724 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800725
Pawin Vongmasa36653902018-11-15 00:10:25 -0800726 return status;
727}
728
729c2_status_t Codec2Client::createInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800730 std::shared_ptr<InputSurface>* const inputSurface) {
731 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700732 Return<void> transStatus = mBase1_0->createInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800733 [&status, inputSurface](
734 Status s,
735 const sp<IInputSurface>& i) {
736 status = static_cast<c2_status_t>(s);
737 if (status != C2_OK) {
738 return;
739 }
740 *inputSurface = std::make_shared<InputSurface>(i);
741 });
742 if (!transStatus.isOk()) {
743 LOG(ERROR) << "createInputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800744 return C2_TRANSACTION_FAILED;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800745 } else if (status != C2_OK) {
746 LOG(DEBUG) << "createInputSurface -- call failed: "
747 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800748 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800749 return status;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800750}
751
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700752std::vector<C2Component::Traits> const& Codec2Client::listComponents() const {
753 return Cache::List()[mServiceIndex].getTraits();
754}
755
756std::vector<C2Component::Traits> Codec2Client::_listComponents(
757 bool* success) const {
758 std::vector<C2Component::Traits> traits;
759 std::string const& serviceName = getServiceName();
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700760 Return<void> transStatus = mBase1_0->listComponents(
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700761 [&traits, &serviceName](Status s,
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800762 const hidl_vec<IComponentStore::ComponentTraits>& t) {
763 if (s != Status::OK) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700764 LOG(DEBUG) << "_listComponents -- call failed: "
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800765 << static_cast<c2_status_t>(s) << ".";
766 return;
767 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700768 traits.resize(t.size());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800769 for (size_t i = 0; i < t.size(); ++i) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700770 if (!objcpy(&traits[i], t[i])) {
771 LOG(ERROR) << "_listComponents -- corrupted output.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800772 return;
773 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700774 traits[i].owner = serviceName;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800775 }
776 });
777 if (!transStatus.isOk()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700778 LOG(ERROR) << "_listComponents -- transaction failed.";
779 *success = false;
780 } else {
781 *success = true;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800782 }
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700783 return traits;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800784}
785
786c2_status_t Codec2Client::copyBuffer(
787 const std::shared_ptr<C2Buffer>& src,
788 const std::shared_ptr<C2Buffer>& dst) {
789 // TODO: Implement?
790 (void)src;
791 (void)dst;
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800792 LOG(ERROR) << "copyBuffer not implemented";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800793 return C2_OMITTED;
794}
795
796std::shared_ptr<C2ParamReflector>
797 Codec2Client::getParamReflector() {
798 // TODO: this is not meant to be exposed as C2ParamReflector on the client side; instead, it
799 // should reflect the HAL API.
800 struct SimpleParamReflector : public C2ParamReflector {
801 virtual std::unique_ptr<C2StructDescriptor> describe(C2Param::CoreIndex coreIndex) const {
802 hidl_vec<ParamIndex> indices(1);
803 indices[0] = static_cast<ParamIndex>(coreIndex.coreIndex());
804 std::unique_ptr<C2StructDescriptor> descriptor;
805 Return<void> transStatus = mBase->getStructDescriptors(
806 indices,
807 [&descriptor](
808 Status s,
809 const hidl_vec<StructDescriptor>& sd) {
810 c2_status_t status = static_cast<c2_status_t>(s);
811 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800812 LOG(DEBUG) << "SimpleParamReflector -- "
813 "getStructDescriptors() failed: "
814 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800815 descriptor.reset();
816 return;
817 }
818 if (sd.size() != 1) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800819 LOG(DEBUG) << "SimpleParamReflector -- "
820 "getStructDescriptors() "
821 "returned vector of size "
822 << sd.size() << ". "
823 "It should be 1.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800824 descriptor.reset();
825 return;
826 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800827 if (!objcpy(&descriptor, sd[0])) {
828 LOG(DEBUG) << "SimpleParamReflector -- "
829 "getStructDescriptors() returned "
830 "corrupted data.";
Pawin Vongmasa36653902018-11-15 00:10:25 -0800831 descriptor.reset();
832 return;
833 }
834 });
835 return descriptor;
836 }
837
838 SimpleParamReflector(sp<Base> base)
839 : mBase(base) { }
840
841 sp<Base> mBase;
842 };
843
Pawin Vongmasabf69de92019-10-29 06:21:27 -0700844 return std::make_shared<SimpleParamReflector>(mBase1_0);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800845};
846
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700847std::vector<std::string> const& Codec2Client::GetServiceNames() {
848 static std::vector<std::string> sServiceNames{[]() {
849 using ::android::hardware::media::c2::V1_0::IComponentStore;
850 using ::android::hidl::manager::V1_2::IServiceManager;
851
852 while (true) {
853 sp<IServiceManager> serviceManager = IServiceManager::getService();
854 CHECK(serviceManager) << "Hardware service manager is not running.";
855
856 // There are three categories of services based on names.
857 std::vector<std::string> defaultNames; // Prefixed with "default"
858 std::vector<std::string> vendorNames; // Prefixed with "vendor"
859 std::vector<std::string> otherNames; // Others
860 Return<void> transResult;
861 transResult = serviceManager->listManifestByInterface(
862 IComponentStore::descriptor,
863 [&defaultNames, &vendorNames, &otherNames](
864 hidl_vec<hidl_string> const& instanceNames) {
865 for (hidl_string const& instanceName : instanceNames) {
866 char const* name = instanceName.c_str();
867 if (strncmp(name, "default", 7) == 0) {
868 defaultNames.emplace_back(name);
869 } else if (strncmp(name, "vendor", 6) == 0) {
870 vendorNames.emplace_back(name);
871 } else {
872 otherNames.emplace_back(name);
873 }
874 }
875 });
876 if (transResult.isOk()) {
877 // Sort service names in each category.
878 std::sort(defaultNames.begin(), defaultNames.end());
879 std::sort(vendorNames.begin(), vendorNames.end());
880 std::sort(otherNames.begin(), otherNames.end());
881
882 // Concatenate the three lists in this order: default, vendor,
883 // other.
884 std::vector<std::string>& names = defaultNames;
885 names.reserve(names.size() + vendorNames.size() + otherNames.size());
886 names.insert(names.end(),
887 std::make_move_iterator(vendorNames.begin()),
888 std::make_move_iterator(vendorNames.end()));
889 names.insert(names.end(),
890 std::make_move_iterator(otherNames.begin()),
891 std::make_move_iterator(otherNames.end()));
892
893 // Summarize to logcat.
894 if (names.empty()) {
895 LOG(INFO) << "No Codec2 services declared in the manifest.";
896 } else {
897 std::stringstream stringOutput;
898 stringOutput << "Available Codec2 services:";
899 for (std::string const& name : names) {
900 stringOutput << " \"" << name << "\"";
901 }
902 LOG(INFO) << stringOutput.str();
903 }
904
905 return names;
906 }
907 LOG(ERROR) << "Could not retrieve the list of service instances of "
908 << IComponentStore::descriptor
909 << ". Retrying...";
910 }
911 }()};
912 return sServiceNames;
913}
914
Pawin Vongmasa36653902018-11-15 00:10:25 -0800915std::shared_ptr<Codec2Client> Codec2Client::CreateFromService(
Pawin Vongmasa83d2c552020-03-05 04:36:08 -0800916 const char* name,
917 bool setAsPreferredCodec2ComponentStore) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700918 size_t index = getServiceIndex(name);
Pawin Vongmasa83d2c552020-03-05 04:36:08 -0800919 if (index == GetServiceNames().size()) {
920 if (setAsPreferredCodec2ComponentStore) {
921 LOG(WARNING) << "CreateFromService(" << name
922 << ") -- preferred C2ComponentStore not set.";
923 }
924 return nullptr;
925 }
926 std::shared_ptr<Codec2Client> client = _CreateFromIndex(index);
927 if (setAsPreferredCodec2ComponentStore) {
928 SetPreferredCodec2ComponentStore(
929 std::make_shared<Client2Store>(client));
930 LOG(INFO) << "CreateFromService(" << name
931 << ") -- service set as preferred C2ComponentStore.";
932 }
933 return client;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800934}
935
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700936std::vector<std::shared_ptr<Codec2Client>> Codec2Client::
937 CreateFromAllServices() {
938 std::vector<std::shared_ptr<Codec2Client>> clients(
939 GetServiceNames().size());
940 for (size_t i = GetServiceNames().size(); i > 0; ) {
941 --i;
942 clients[i] = _CreateFromIndex(i);
943 }
944 return clients;
945}
946
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700947std::shared_ptr<Codec2Client> Codec2Client::_CreateFromIndex(size_t index) {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700948 std::string const& name = GetServiceNames()[index];
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700949 LOG(INFO) << "Creating a Codec2 client to service \"" << name << "\"";
950 sp<Base> baseStore = Base::getService(name);
951 CHECK(baseStore) << "Codec2 service \"" << name << "\""
952 " inaccessible for unknown reasons.";
953 LOG(INFO) << "Client to Codec2 service \"" << name << "\" created";
954 return std::make_shared<Codec2Client>(baseStore, index);
955}
956
957c2_status_t Codec2Client::ForAllServices(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800958 const std::string &key,
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -0700959 size_t numberOfAttempts,
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800960 std::function<c2_status_t(const std::shared_ptr<Codec2Client>&)>
961 predicate) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800962 c2_status_t status = C2_NO_INIT; // no IComponentStores present
963
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700964 // Cache the mapping key -> index of Codec2Client in Cache::List().
Pawin Vongmasa36653902018-11-15 00:10:25 -0800965 static std::mutex key2IndexMutex;
966 static std::map<std::string, size_t> key2Index;
967
Pawin Vongmasa1c75a232019-01-09 04:41:52 -0800968 // By default try all stores. However, try the last known client first. If
969 // the last known client fails, retry once. We do this by pushing the last
970 // known client in front of the list of all clients.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800971 std::deque<size_t> indices;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700972 for (size_t index = Cache::List().size(); index > 0; ) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800973 indices.push_front(--index);
974 }
975
976 bool wasMapped = false;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700977 {
978 std::scoped_lock lock{key2IndexMutex};
979 auto it = key2Index.find(key);
980 if (it != key2Index.end()) {
981 indices.push_front(it->second);
982 wasMapped = true;
983 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800984 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800985
986 for (size_t index : indices) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700987 Cache& cache = Cache::List()[index];
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -0700988 for (size_t tries = numberOfAttempts; tries > 0; --tries) {
989 std::shared_ptr<Codec2Client> client{cache.getClient()};
Pawin Vongmasa36653902018-11-15 00:10:25 -0800990 status = predicate(client);
991 if (status == C2_OK) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700992 std::scoped_lock lock{key2IndexMutex};
Pawin Vongmasa36653902018-11-15 00:10:25 -0800993 key2Index[key] = index; // update last known client index
Pawin Vongmasa892c81d2019-03-12 00:56:50 -0700994 return C2_OK;
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -0700995 } else if (status == C2_TRANSACTION_FAILED) {
996 LOG(WARNING) << "\"" << key << "\" failed for service \""
997 << client->getName()
998 << "\" due to transaction failure. "
999 << "(Service may have crashed.)"
1000 << (tries > 1 ? " Retrying..." : "");
1001 cache.invalidate();
1002 continue;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001003 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001004 if (wasMapped) {
1005 LOG(INFO) << "\"" << key << "\" became invalid in service \""
1006 << client->getName() << "\". Retrying...";
1007 wasMapped = false;
1008 }
1009 break;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001010 }
1011 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001012 return status; // return the last status from a valid client
Pawin Vongmasa36653902018-11-15 00:10:25 -08001013}
1014
1015std::shared_ptr<Codec2Client::Component>
1016 Codec2Client::CreateComponentByName(
1017 const char* componentName,
1018 const std::shared_ptr<Listener>& listener,
Pawin Vongmasa23c90c82019-09-03 00:44:42 -07001019 std::shared_ptr<Codec2Client>* owner,
1020 size_t numberOfAttempts) {
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001021 std::string key{"create:"};
1022 key.append(componentName);
1023 std::shared_ptr<Component> component;
1024 c2_status_t status = ForAllServices(
1025 key,
1026 numberOfAttempts,
1027 [owner, &component, componentName, &listener](
1028 const std::shared_ptr<Codec2Client> &client)
1029 -> c2_status_t {
1030 c2_status_t status = client->createComponent(componentName,
1031 listener,
1032 &component);
1033 if (status == C2_OK) {
1034 if (owner) {
1035 *owner = client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001036 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001037 } else if (status != C2_NOT_FOUND) {
1038 LOG(DEBUG) << "IComponentStore("
1039 << client->getServiceName()
1040 << ")::createComponent(\"" << componentName
1041 << "\") returned status = "
1042 << status << ".";
1043 }
1044 return status;
1045 });
1046 if (status != C2_OK) {
1047 LOG(DEBUG) << "Failed to create component \"" << componentName
1048 << "\" from all known services. "
1049 "Last returned status = " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001050 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001051 return component;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001052}
1053
1054std::shared_ptr<Codec2Client::Interface>
1055 Codec2Client::CreateInterfaceByName(
1056 const char* interfaceName,
Pawin Vongmasa23c90c82019-09-03 00:44:42 -07001057 std::shared_ptr<Codec2Client>* owner,
1058 size_t numberOfAttempts) {
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001059 std::string key{"create:"};
1060 key.append(interfaceName);
1061 std::shared_ptr<Interface> interface;
1062 c2_status_t status = ForAllServices(
1063 key,
1064 numberOfAttempts,
1065 [owner, &interface, interfaceName](
1066 const std::shared_ptr<Codec2Client> &client)
1067 -> c2_status_t {
1068 c2_status_t status = client->createInterface(interfaceName,
1069 &interface);
1070 if (status == C2_OK) {
1071 if (owner) {
1072 *owner = client;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001073 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001074 } else if (status != C2_NOT_FOUND) {
1075 LOG(DEBUG) << "IComponentStore("
1076 << client->getServiceName()
1077 << ")::createInterface(\"" << interfaceName
1078 << "\") returned status = "
1079 << status << ".";
1080 }
1081 return status;
1082 });
1083 if (status != C2_OK) {
1084 LOG(DEBUG) << "Failed to create interface \"" << interfaceName
1085 << "\" from all known services. "
1086 "Last returned status = " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001087 }
Pawin Vongmasa1e7015a2019-10-09 02:15:50 -07001088 return interface;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001089}
1090
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001091std::vector<C2Component::Traits> const& Codec2Client::ListComponents() {
1092 static std::vector<C2Component::Traits> sList{[]() {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001093 std::vector<C2Component::Traits> list;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001094 for (Cache& cache : Cache::List()) {
1095 std::vector<C2Component::Traits> const& traits = cache.getTraits();
1096 list.insert(list.end(), traits.begin(), traits.end());
Pawin Vongmasa36653902018-11-15 00:10:25 -08001097 }
1098 return list;
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001099 }()};
1100 return sList;
1101}
Pawin Vongmasa36653902018-11-15 00:10:25 -08001102
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001103std::shared_ptr<Codec2Client::InputSurface> Codec2Client::CreateInputSurface(
1104 char const* serviceName) {
Pawin Vongmasa18588322019-05-18 01:52:13 -07001105 int32_t inputSurfaceSetting = ::android::base::GetIntProperty(
1106 "debug.stagefright.c2inputsurface", int32_t(0));
1107 if (inputSurfaceSetting <= 0) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001108 return nullptr;
1109 }
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001110 size_t index = GetServiceNames().size();
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001111 if (serviceName) {
1112 index = getServiceIndex(serviceName);
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001113 if (index == GetServiceNames().size()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001114 LOG(DEBUG) << "CreateInputSurface -- invalid service name: \""
1115 << serviceName << "\"";
1116 }
1117 }
1118
1119 std::shared_ptr<Codec2Client::InputSurface> inputSurface;
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -07001120 if (index != GetServiceNames().size()) {
Pawin Vongmasa892c81d2019-03-12 00:56:50 -07001121 std::shared_ptr<Codec2Client> client = Cache::List()[index].getClient();
1122 if (client->createInputSurface(&inputSurface) == C2_OK) {
1123 return inputSurface;
1124 }
1125 }
1126 LOG(INFO) << "CreateInputSurface -- attempting to create an input surface "
1127 "from all services...";
1128 for (Cache& cache : Cache::List()) {
1129 std::shared_ptr<Codec2Client> client = cache.getClient();
1130 if (client->createInputSurface(&inputSurface) == C2_OK) {
1131 LOG(INFO) << "CreateInputSurface -- input surface obtained from "
1132 "service \"" << client->getServiceName() << "\"";
1133 return inputSurface;
1134 }
1135 }
1136 LOG(WARNING) << "CreateInputSurface -- failed to create an input surface "
1137 "from all services";
1138 return nullptr;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001139}
1140
1141// Codec2Client::Listener
1142
1143Codec2Client::Listener::~Listener() {
1144}
1145
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001146// Codec2Client::Interface
1147Codec2Client::Interface::Interface(const sp<Base>& base)
1148 : Configurable{
1149 [base]() -> sp<IConfigurable> {
1150 Return<sp<IConfigurable>> transResult =
1151 base->getConfigurable();
1152 return transResult.isOk() ?
1153 static_cast<sp<IConfigurable>>(transResult) :
1154 nullptr;
1155 }()
1156 },
1157 mBase{base} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001158}
1159
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001160// Codec2Client::Component
1161Codec2Client::Component::Component(const sp<Base>& base)
1162 : Configurable{
1163 [base]() -> sp<IConfigurable> {
1164 Return<sp<IComponentInterface>> transResult1 =
1165 base->getInterface();
1166 if (!transResult1.isOk()) {
1167 return nullptr;
1168 }
1169 Return<sp<IConfigurable>> transResult2 =
1170 static_cast<sp<IComponentInterface>>(transResult1)->
1171 getConfigurable();
1172 return transResult2.isOk() ?
1173 static_cast<sp<IConfigurable>>(transResult2) :
1174 nullptr;
1175 }()
1176 },
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001177 mBase1_0{base},
1178 mBase1_1{Base1_1::castFrom(base)},
1179 mBufferPoolSender{std::make_unique<BufferPoolSender>()},
1180 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
1181}
1182
1183Codec2Client::Component::Component(const sp<Base1_1>& base)
1184 : Configurable{
1185 [base]() -> sp<IConfigurable> {
1186 Return<sp<IComponentInterface>> transResult1 =
1187 base->getInterface();
1188 if (!transResult1.isOk()) {
1189 return nullptr;
1190 }
1191 Return<sp<IConfigurable>> transResult2 =
1192 static_cast<sp<IComponentInterface>>(transResult1)->
1193 getConfigurable();
1194 return transResult2.isOk() ?
1195 static_cast<sp<IConfigurable>>(transResult2) :
1196 nullptr;
1197 }()
1198 },
1199 mBase1_0{base},
1200 mBase1_1{base},
1201 mBufferPoolSender{std::make_unique<BufferPoolSender>()},
1202 mOutputBufferQueue{std::make_unique<OutputBufferQueue>()} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001203}
1204
1205Codec2Client::Component::~Component() {
1206}
1207
1208c2_status_t Codec2Client::Component::createBlockPool(
1209 C2Allocator::id_t id,
1210 C2BlockPool::local_id_t* blockPoolId,
1211 std::shared_ptr<Codec2Client::Configurable>* configurable) {
1212 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001213 Return<void> transStatus = mBase1_0->createBlockPool(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001214 static_cast<uint32_t>(id),
1215 [&status, blockPoolId, configurable](
1216 Status s,
1217 uint64_t pId,
1218 const sp<IConfigurable>& c) {
1219 status = static_cast<c2_status_t>(s);
1220 configurable->reset();
1221 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001222 LOG(DEBUG) << "createBlockPool -- call failed: "
1223 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001224 return;
1225 }
1226 *blockPoolId = static_cast<C2BlockPool::local_id_t>(pId);
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001227 *configurable = std::make_shared<Configurable>(c);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001228 });
1229 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001230 LOG(ERROR) << "createBlockPool -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001231 return C2_TRANSACTION_FAILED;
1232 }
1233 return status;
1234}
1235
1236c2_status_t Codec2Client::Component::destroyBlockPool(
1237 C2BlockPool::local_id_t localId) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001238 Return<Status> transResult = mBase1_0->destroyBlockPool(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001239 static_cast<uint64_t>(localId));
1240 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001241 LOG(ERROR) << "destroyBlockPool -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001242 return C2_TRANSACTION_FAILED;
1243 }
1244 return static_cast<c2_status_t>(static_cast<Status>(transResult));
1245}
1246
Wonsik Kimab34ed62019-01-31 15:28:46 -08001247void Codec2Client::Component::handleOnWorkDone(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001248 const std::list<std::unique_ptr<C2Work>> &workItems) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001249 // Output bufferqueue-based blocks' lifetime management
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001250 mOutputBufferQueue->holdBufferQueueBlocks(workItems);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001251}
1252
1253c2_status_t Codec2Client::Component::queue(
1254 std::list<std::unique_ptr<C2Work>>* const items) {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001255 WorkBundle workBundle;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001256 if (!objcpy(&workBundle, *items, mBufferPoolSender.get())) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001257 LOG(ERROR) << "queue -- bad input.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001258 return C2_TRANSACTION_FAILED;
1259 }
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001260 Return<Status> transStatus = mBase1_0->queue(workBundle);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001261 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001262 LOG(ERROR) << "queue -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001263 return C2_TRANSACTION_FAILED;
1264 }
1265 c2_status_t status =
1266 static_cast<c2_status_t>(static_cast<Status>(transStatus));
1267 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001268 LOG(DEBUG) << "queue -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001269 }
1270 return status;
1271}
1272
1273c2_status_t Codec2Client::Component::flush(
1274 C2Component::flush_mode_t mode,
1275 std::list<std::unique_ptr<C2Work>>* const flushedWork) {
1276 (void)mode; // Flush mode isn't supported in HIDL yet.
1277 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001278 Return<void> transStatus = mBase1_0->flush(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001279 [&status, flushedWork](
1280 Status s, const WorkBundle& wb) {
1281 status = static_cast<c2_status_t>(s);
1282 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001283 LOG(DEBUG) << "flush -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001284 return;
1285 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001286 if (!objcpy(flushedWork, wb)) {
1287 status = C2_CORRUPTED;
1288 } else {
1289 status = C2_OK;
1290 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001291 });
1292 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001293 LOG(ERROR) << "flush -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001294 return C2_TRANSACTION_FAILED;
1295 }
1296
1297 // Indices of flushed work items.
1298 std::vector<uint64_t> flushedIndices;
1299 for (const std::unique_ptr<C2Work> &work : *flushedWork) {
1300 if (work) {
1301 if (work->worklets.empty()
1302 || !work->worklets.back()
1303 || (work->worklets.back()->output.flags &
1304 C2FrameData::FLAG_INCOMPLETE) == 0) {
1305 // input is complete
1306 flushedIndices.emplace_back(
1307 work->input.ordinal.frameIndex.peeku());
1308 }
1309 }
1310 }
1311
Pawin Vongmasa36653902018-11-15 00:10:25 -08001312 // Output bufferqueue-based blocks' lifetime management
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001313 mOutputBufferQueue->holdBufferQueueBlocks(*flushedWork);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001314
1315 return status;
1316}
1317
1318c2_status_t Codec2Client::Component::drain(C2Component::drain_mode_t mode) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001319 Return<Status> transStatus = mBase1_0->drain(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001320 mode == C2Component::DRAIN_COMPONENT_WITH_EOS);
1321 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001322 LOG(ERROR) << "drain -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001323 return C2_TRANSACTION_FAILED;
1324 }
1325 c2_status_t status =
1326 static_cast<c2_status_t>(static_cast<Status>(transStatus));
1327 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001328 LOG(DEBUG) << "drain -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001329 }
1330 return status;
1331}
1332
1333c2_status_t Codec2Client::Component::start() {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001334 Return<Status> transStatus = mBase1_0->start();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001335 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001336 LOG(ERROR) << "start -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001337 return C2_TRANSACTION_FAILED;
1338 }
1339 c2_status_t status =
1340 static_cast<c2_status_t>(static_cast<Status>(transStatus));
1341 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001342 LOG(DEBUG) << "start -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001343 }
1344 return status;
1345}
1346
1347c2_status_t Codec2Client::Component::stop() {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001348 Return<Status> transStatus = mBase1_0->stop();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001349 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001350 LOG(ERROR) << "stop -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001351 return C2_TRANSACTION_FAILED;
1352 }
1353 c2_status_t status =
1354 static_cast<c2_status_t>(static_cast<Status>(transStatus));
1355 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001356 LOG(DEBUG) << "stop -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001357 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001358 return status;
1359}
1360
1361c2_status_t Codec2Client::Component::reset() {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001362 Return<Status> transStatus = mBase1_0->reset();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001363 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001364 LOG(ERROR) << "reset -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001365 return C2_TRANSACTION_FAILED;
1366 }
1367 c2_status_t status =
1368 static_cast<c2_status_t>(static_cast<Status>(transStatus));
1369 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001370 LOG(DEBUG) << "reset -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001371 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001372 return status;
1373}
1374
1375c2_status_t Codec2Client::Component::release() {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001376 Return<Status> transStatus = mBase1_0->release();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001377 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001378 LOG(ERROR) << "release -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001379 return C2_TRANSACTION_FAILED;
1380 }
1381 c2_status_t status =
1382 static_cast<c2_status_t>(static_cast<Status>(transStatus));
1383 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001384 LOG(DEBUG) << "release -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001385 }
Pawin Vongmasa36653902018-11-15 00:10:25 -08001386 return status;
1387}
1388
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001389c2_status_t Codec2Client::Component::configureVideoTunnel(
1390 uint32_t avSyncHwId,
1391 native_handle_t** sidebandHandle) {
1392 *sidebandHandle = nullptr;
1393 if (!mBase1_1) {
1394 return C2_OMITTED;
1395 }
1396 c2_status_t status{};
1397 Return<void> transStatus = mBase1_1->configureVideoTunnel(avSyncHwId,
1398 [&status, sidebandHandle](
1399 Status s, hardware::hidl_handle const& h) {
1400 status = static_cast<c2_status_t>(s);
1401 if (h.getNativeHandle()) {
1402 *sidebandHandle = native_handle_clone(h.getNativeHandle());
1403 }
1404 });
1405 if (!transStatus.isOk()) {
1406 LOG(ERROR) << "configureVideoTunnel -- transaction failed.";
1407 return C2_TRANSACTION_FAILED;
1408 }
1409 return status;
1410}
1411
Pawin Vongmasa36653902018-11-15 00:10:25 -08001412c2_status_t Codec2Client::Component::setOutputSurface(
1413 C2BlockPool::local_id_t blockPoolId,
1414 const sp<IGraphicBufferProducer>& surface,
1415 uint32_t generation) {
Sungtak Lee08515812019-06-05 11:16:32 -07001416 uint64_t bqId = 0;
1417 sp<IGraphicBufferProducer> nullIgbp;
1418 sp<HGraphicBufferProducer2> nullHgbp;
Pawin Vongmasa3866c7e2019-01-31 05:21:29 -08001419
Sungtak Lee08515812019-06-05 11:16:32 -07001420 sp<HGraphicBufferProducer2> igbp = surface ?
1421 surface->getHalInterface<HGraphicBufferProducer2>() : nullHgbp;
1422 if (surface && !igbp) {
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08001423 igbp = new B2HGraphicBufferProducer2(surface);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001424 }
1425
Sungtak Lee08515812019-06-05 11:16:32 -07001426 if (!surface) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001427 mOutputBufferQueue->configure(nullIgbp, generation, 0);
Sungtak Lee08515812019-06-05 11:16:32 -07001428 } else if (surface->getUniqueId(&bqId) != OK) {
1429 LOG(ERROR) << "setOutputSurface -- "
1430 "cannot obtain bufferqueue id.";
1431 bqId = 0;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001432 mOutputBufferQueue->configure(nullIgbp, generation, 0);
Sungtak Lee08515812019-06-05 11:16:32 -07001433 } else {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001434 mOutputBufferQueue->configure(surface, generation, bqId);
Sungtak Lee08515812019-06-05 11:16:32 -07001435 }
1436 ALOGD("generation remote change %u", generation);
1437
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001438 Return<Status> transStatus = mBase1_0->setOutputSurface(
Sungtak Lee08515812019-06-05 11:16:32 -07001439 static_cast<uint64_t>(blockPoolId),
1440 bqId == 0 ? nullHgbp : igbp);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001441 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001442 LOG(ERROR) << "setOutputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001443 return C2_TRANSACTION_FAILED;
1444 }
1445 c2_status_t status =
1446 static_cast<c2_status_t>(static_cast<Status>(transStatus));
1447 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001448 LOG(DEBUG) << "setOutputSurface -- call failed: " << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001449 }
1450 return status;
1451}
1452
1453status_t Codec2Client::Component::queueToOutputSurface(
1454 const C2ConstGraphicBlock& block,
1455 const QueueBufferInput& input,
1456 QueueBufferOutput* output) {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001457 return mOutputBufferQueue->outputBuffer(block, input, output);
Pawin Vongmasa36653902018-11-15 00:10:25 -08001458}
1459
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001460c2_status_t Codec2Client::Component::connectToInputSurface(
1461 const std::shared_ptr<InputSurface>& inputSurface,
1462 std::shared_ptr<InputSurfaceConnection>* connection) {
1463 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001464 Return<void> transStatus = mBase1_0->connectToInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001465 inputSurface->mBase,
1466 [&status, connection](
1467 Status s, const sp<IInputSurfaceConnection>& c) {
1468 status = static_cast<c2_status_t>(s);
1469 if (status != C2_OK) {
1470 LOG(DEBUG) << "connectToInputSurface -- call failed: "
1471 << status << ".";
1472 return;
1473 }
1474 *connection = std::make_shared<InputSurfaceConnection>(c);
1475 });
Pawin Vongmasa36653902018-11-15 00:10:25 -08001476 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001477 LOG(ERROR) << "connectToInputSurface -- transaction failed";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001478 return C2_TRANSACTION_FAILED;
1479 }
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001480 return status;
1481}
1482
1483c2_status_t Codec2Client::Component::connectToOmxInputSurface(
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08001484 const sp<HGraphicBufferProducer1>& producer,
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001485 const sp<HGraphicBufferSource>& source,
1486 std::shared_ptr<InputSurfaceConnection>* connection) {
1487 c2_status_t status;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001488 Return<void> transStatus = mBase1_0->connectToOmxInputSurface(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001489 producer, source,
1490 [&status, connection](
1491 Status s, const sp<IInputSurfaceConnection>& c) {
1492 status = static_cast<c2_status_t>(s);
1493 if (status != C2_OK) {
1494 LOG(DEBUG) << "connectToOmxInputSurface -- call failed: "
1495 << status << ".";
1496 return;
1497 }
1498 *connection = std::make_shared<InputSurfaceConnection>(c);
1499 });
1500 if (!transStatus.isOk()) {
1501 LOG(ERROR) << "connectToOmxInputSurface -- transaction failed.";
1502 return C2_TRANSACTION_FAILED;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001503 }
1504 return status;
1505}
1506
1507c2_status_t Codec2Client::Component::disconnectFromInputSurface() {
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001508 Return<Status> transStatus = mBase1_0->disconnectFromInputSurface();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001509 if (!transStatus.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001510 LOG(ERROR) << "disconnectToInputSurface -- transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001511 return C2_TRANSACTION_FAILED;
1512 }
1513 c2_status_t status =
1514 static_cast<c2_status_t>(static_cast<Status>(transStatus));
1515 if (status != C2_OK) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001516 LOG(DEBUG) << "disconnectFromInputSurface -- call failed: "
1517 << status << ".";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001518 }
1519 return status;
1520}
1521
1522c2_status_t Codec2Client::Component::setDeathListener(
1523 const std::shared_ptr<Component>& component,
1524 const std::shared_ptr<Listener>& listener) {
1525
1526 struct HidlDeathRecipient : public hardware::hidl_death_recipient {
1527 std::weak_ptr<Component> component;
1528 std::weak_ptr<Listener> base;
1529
1530 virtual void serviceDied(
1531 uint64_t /* cookie */,
1532 const wp<::android::hidl::base::V1_0::IBase>& /* who */
1533 ) override {
1534 if (std::shared_ptr<Codec2Client::Listener> listener = base.lock()) {
1535 listener->onDeath(component);
1536 } else {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001537 LOG(DEBUG) << "onDeath -- listener died.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001538 }
1539 }
1540 };
1541
1542 sp<HidlDeathRecipient> deathRecipient = new HidlDeathRecipient();
1543 deathRecipient->base = listener;
1544 deathRecipient->component = component;
1545
1546 component->mDeathRecipient = deathRecipient;
Pawin Vongmasabf69de92019-10-29 06:21:27 -07001547 Return<bool> transResult = component->mBase1_0->linkToDeath(
Pawin Vongmasa36653902018-11-15 00:10:25 -08001548 component->mDeathRecipient, 0);
1549 if (!transResult.isOk()) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001550 LOG(ERROR) << "setDeathListener -- linkToDeath() transaction failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001551 return C2_TRANSACTION_FAILED;
1552 }
1553 if (!static_cast<bool>(transResult)) {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001554 LOG(DEBUG) << "setDeathListener -- linkToDeath() call failed.";
Pawin Vongmasa36653902018-11-15 00:10:25 -08001555 return C2_CORRUPTED;
1556 }
1557 return C2_OK;
1558}
1559
1560// Codec2Client::InputSurface
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001561Codec2Client::InputSurface::InputSurface(const sp<IInputSurface>& base)
1562 : Configurable{
1563 [base]() -> sp<IConfigurable> {
1564 Return<sp<IConfigurable>> transResult =
1565 base->getConfigurable();
1566 return transResult.isOk() ?
1567 static_cast<sp<IConfigurable>>(transResult) :
1568 nullptr;
1569 }()
1570 },
1571 mBase{base},
1572 mGraphicBufferProducer{new
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08001573 H2BGraphicBufferProducer2([base]() -> sp<HGraphicBufferProducer2> {
1574 Return<sp<HGraphicBufferProducer2>> transResult =
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001575 base->getGraphicBufferProducer();
1576 return transResult.isOk() ?
Pawin Vongmasaef939bf2019-03-03 04:44:59 -08001577 static_cast<sp<HGraphicBufferProducer2>>(transResult) :
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001578 nullptr;
1579 }())} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001580}
1581
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001582sp<IGraphicBufferProducer>
Pawin Vongmasa36653902018-11-15 00:10:25 -08001583 Codec2Client::InputSurface::getGraphicBufferProducer() const {
1584 return mGraphicBufferProducer;
1585}
1586
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001587sp<IInputSurface> Codec2Client::InputSurface::getHalInterface() const {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001588 return mBase;
1589}
1590
1591// Codec2Client::InputSurfaceConnection
Pawin Vongmasa36653902018-11-15 00:10:25 -08001592Codec2Client::InputSurfaceConnection::InputSurfaceConnection(
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001593 const sp<IInputSurfaceConnection>& base)
1594 : Configurable{
1595 [base]() -> sp<IConfigurable> {
1596 Return<sp<IConfigurable>> transResult =
1597 base->getConfigurable();
1598 return transResult.isOk() ?
1599 static_cast<sp<IConfigurable>>(transResult) :
1600 nullptr;
1601 }()
1602 },
1603 mBase{base} {
Pawin Vongmasa36653902018-11-15 00:10:25 -08001604}
1605
1606c2_status_t Codec2Client::InputSurfaceConnection::disconnect() {
Pawin Vongmasa1c75a232019-01-09 04:41:52 -08001607 Return<Status> transResult = mBase->disconnect();
Pawin Vongmasa36653902018-11-15 00:10:25 -08001608 return static_cast<c2_status_t>(static_cast<Status>(transResult));
1609}
1610
1611} // namespace android
1612