| Chih-Yu Huang | 6a7255a | 2020-03-25 17:01:47 +0900 | [diff] [blame^] | 1 | // 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 | |
| 20 | namespace android { |
| 21 | |
| 22 | class V4L2ComponentFactory : public C2ComponentFactory { |
| 23 | public: |
| 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 | |
| 34 | private: |
| 35 | const std::string mComponentName; |
| 36 | const bool mIsEncoder; |
| 37 | std::shared_ptr<C2ReflectorHelper> mReflector; |
| 38 | }; |
| 39 | |
| 40 | V4L2ComponentFactory::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 | |
| 50 | V4L2ComponentFactory::~V4L2ComponentFactory() = default; |
| 51 | |
| 52 | c2_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 | |
| 72 | c2_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 | |
| 97 | extern "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 | |
| 109 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) { |
| 110 | ALOGV("%s()", __func__); |
| 111 | delete factory; |
| 112 | } |