blob: 1dca7ffcd15aaf4c3c17d9bafac25eb6cfa758d9 [file] [log] [blame]
Chih-Yu Huang6a7255a2020-03-25 17:01:47 +09001// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//#define LOG_NDEBUG 0
6#define LOG_TAG "V4L2ComponentFactory"
7
8#include <string>
9
10#include <C2ComponentFactory.h>
11#include <SimpleC2Interface.h>
12#include <log/log.h>
13#include <util/C2InterfaceHelper.h>
14
15#include <v4l2_codec2/common/V4L2ComponentCommon.h>
16#include <v4l2_codec2/components/V4L2DecodeComponent.h>
17#include <v4l2_codec2/components/V4L2DecodeInterface.h>
18#include <v4l2_codec2/store/V4L2ComponentStore.h>
19
20namespace android {
21
22class V4L2ComponentFactory : public C2ComponentFactory {
23public:
24 V4L2ComponentFactory(const char* componentName, bool isEncoder);
25 ~V4L2ComponentFactory() override;
26
27 // Implementation of C2ComponentFactory.
28 c2_status_t createComponent(c2_node_id_t id, std::shared_ptr<C2Component>* const component,
29 ComponentDeleter deleter) override;
30 c2_status_t createInterface(c2_node_id_t id,
31 std::shared_ptr<C2ComponentInterface>* const interface,
32 InterfaceDeleter deleter) override;
33
34private:
35 const std::string mComponentName;
36 const bool mIsEncoder;
37 std::shared_ptr<C2ReflectorHelper> mReflector;
38};
39
40V4L2ComponentFactory::V4L2ComponentFactory(const char* componentName, bool isEncoder)
41 : mComponentName(componentName), mIsEncoder(isEncoder) {
42 auto componentStore = V4L2ComponentStore::Create();
43 if (componentStore == nullptr) {
44 ALOGE("Could not create V4L2ComponentStore.");
45 return;
46 }
47 mReflector = std::static_pointer_cast<C2ReflectorHelper>(componentStore->getParamReflector());
48}
49
50V4L2ComponentFactory::~V4L2ComponentFactory() = default;
51
52c2_status_t V4L2ComponentFactory::createComponent(c2_node_id_t id,
53 std::shared_ptr<C2Component>* const component,
54 ComponentDeleter deleter) {
55 ALOGV("%s(%d), componentName: %s, isEncoder: %d", __func__, id, mComponentName.c_str(),
56 mIsEncoder);
57
58 if (mReflector == nullptr) {
59 ALOGE("mReflector doesn't exist.");
60 return C2_CORRUPTED;
61 }
62
63 if (mIsEncoder) {
64 // TODO(b/143333813): Fill the encoder component.
65 return C2_BAD_VALUE;
66 } else {
67 *component = V4L2DecodeComponent::create(mComponentName, id, mReflector, deleter);
68 return *component ? C2_OK : C2_BAD_VALUE;
69 }
70}
71
72c2_status_t V4L2ComponentFactory::createInterface(
73 c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
74 InterfaceDeleter deleter) {
75 ALOGV("%s(), componentName: %s", __func__, mComponentName.c_str());
76
77 if (mReflector == nullptr) {
78 ALOGE("mReflector doesn't exist.");
79 return C2_CORRUPTED;
80 }
81
82 if (mIsEncoder) {
83 // TODO(b/143333813): Fill the encoder component.
84 return C2_BAD_VALUE;
85 } else {
86 *interface = std::shared_ptr<C2ComponentInterface>(
87 new SimpleInterface<V4L2DecodeInterface>(
88 mComponentName.c_str(), id,
89 std::make_shared<V4L2DecodeInterface>(mComponentName, mReflector)),
90 deleter);
91 return C2_OK;
92 }
93}
94
95} // namespace android
96
97extern "C" ::C2ComponentFactory* CreateCodec2Factory(const char* componentName) {
98 ALOGV("%s(%s)", __func__, componentName);
99
100 if (!android::V4L2ComponentName::isValid(componentName)) {
101 ALOGE("Invalid component name: %s", componentName);
102 return nullptr;
103 }
104
105 bool isEncoder = android::V4L2ComponentName::isEncoder(componentName);
106 return new android::V4L2ComponentFactory(componentName, isEncoder);
107}
108
109extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
110 ALOGV("%s()", __func__);
111 delete factory;
112}