1 /* 2 * Copyright 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef CODEC2_HIDL_V1_2_UTILS_COMPONENT_H 18 #define CODEC2_HIDL_V1_2_UTILS_COMPONENT_H 19 20 #include <android/hardware/media/bufferpool/2.0/IClientManager.h> 21 #include <android/hardware/media/c2/1.2/IComponent.h> 22 #include <android/hardware/media/c2/1.0/IComponentInterface.h> 23 #include <android/hardware/media/c2/1.0/IComponentListener.h> 24 #include <android/hardware/media/c2/1.2/IComponentStore.h> 25 #include <android/hardware/media/c2/1.0/IInputSink.h> 26 #include <codec2/hidl/1.2/ComponentInterface.h> 27 #include <codec2/hidl/1.2/Configurable.h> 28 #include <codec2/hidl/1.2/types.h> 29 #include <hidl/Status.h> 30 #include <hwbinder/IBinder.h> 31 32 #include <codec2/common/MultiAccessUnitHelper.h> 33 34 #include <C2Component.h> 35 #include <C2Buffer.h> 36 #include <C2.h> 37 38 #include <map> 39 #include <memory> 40 #include <mutex> 41 42 namespace android { 43 namespace hardware { 44 namespace media { 45 namespace c2 { 46 namespace V1_2 { 47 48 using ::android::hardware::media::c2::V1_2::IComponent; 49 using ::android::hardware::media::c2::V1_0::IComponentListener; 50 51 namespace utils { 52 53 using ::android::hardware::hidl_array; 54 using ::android::hardware::hidl_memory; 55 using ::android::hardware::hidl_string; 56 using ::android::hardware::hidl_vec; 57 using ::android::hardware::Return; 58 using ::android::hardware::Void; 59 using ::android::hardware::IBinder; 60 using ::android::sp; 61 using ::android::wp; 62 using ::android::MultiAccessUnitInterface; 63 using ::android::MultiAccessUnitHelper; 64 65 struct ComponentStore; 66 67 struct Component : public IComponent, 68 public std::enable_shared_from_this<Component> { 69 Component( 70 const std::shared_ptr<C2Component>&, 71 const sp<IComponentListener>& listener, 72 const sp<ComponentStore>& store, 73 const sp<::android::hardware::media::bufferpool::V2_0:: 74 IClientManager>& clientPoolManager); 75 c2_status_t status() const; 76 // Receives a death notification of the client. 77 void onDeathReceived(); 78 79 typedef ::android::hardware::graphics::bufferqueue::V1_0:: 80 IGraphicBufferProducer HGraphicBufferProducer1; 81 typedef ::android::hardware::graphics::bufferqueue::V2_0:: 82 IGraphicBufferProducer HGraphicBufferProducer2; 83 84 // Methods from IComponent follow. 85 virtual Return<Status> queue(const WorkBundle& workBundle) override; 86 virtual Return<void> flush(flush_cb _hidl_cb) override; 87 virtual Return<Status> drain(bool withEos) override; 88 virtual Return<Status> setOutputSurface( 89 uint64_t blockPoolId, 90 const sp<HGraphicBufferProducer2>& surface) override; 91 virtual Return<void> connectToInputSurface( 92 const sp<IInputSurface>& inputSurface, 93 connectToInputSurface_cb _hidl_cb) override; 94 virtual Return<void> connectToOmxInputSurface( 95 const sp<HGraphicBufferProducer1>& producer, 96 const sp<::android::hardware::media::omx::V1_0:: 97 IGraphicBufferSource>& source, 98 connectToOmxInputSurface_cb _hidl_cb) override; 99 virtual Return<Status> disconnectFromInputSurface() override; 100 virtual Return<void> createBlockPool( 101 uint32_t allocatorId, 102 createBlockPool_cb _hidl_cb) override; 103 virtual Return<Status> destroyBlockPool(uint64_t blockPoolId) override; 104 virtual Return<Status> start() override; 105 virtual Return<Status> stop() override; 106 virtual Return<Status> reset() override; 107 virtual Return<Status> release() override; 108 virtual Return<sp<IComponentInterface>> getInterface() override; 109 virtual Return<sp<IInputSink>> asInputSink() override; 110 virtual Return<void> configureVideoTunnel( 111 uint32_t avSyncHwId, configureVideoTunnel_cb _hidl_cb) override; 112 virtual Return<Status> setOutputSurfaceWithSyncObj( 113 uint64_t blockPoolId, 114 const sp<HGraphicBufferProducer2>& surface, 115 const SurfaceSyncObj& syncObject) override; 116 117 118 // Returns a C2Component associated to the given sink if the sink is indeed 119 // a local component. Returns nullptr otherwise. 120 // 121 // This function is used by InputSurface::connect(). 122 static std::shared_ptr<C2Component> findLocalComponent( 123 const sp<IInputSink>& sink); 124 125 protected: 126 c2_status_t mInit; 127 std::shared_ptr<C2Component> mComponent; 128 sp<ComponentInterface> mInterface; 129 sp<IComponentListener> mListener; 130 std::shared_ptr<MultiAccessUnitInterface> mMultiAccessUnitIntf; 131 std::shared_ptr<MultiAccessUnitHelper> mMultiAccessUnitHelper; 132 sp<ComponentStore> mStore; 133 ::android::hardware::media::c2::V1_2::utils::DefaultBufferPoolSender 134 mBufferPoolSender; 135 136 struct Sink; 137 std::mutex mSinkMutex; 138 sp<Sink> mSink; 139 140 std::mutex mBlockPoolsMutex; 141 // This map keeps C2BlockPool objects that are created by createBlockPool() 142 // alive. These C2BlockPool objects can be deleted by calling 143 // destroyBlockPool(), reset() or release(), or by destroying the component. 144 std::map<uint64_t, std::shared_ptr<C2BlockPool>> mBlockPools; 145 146 void initListener(const sp<Component>& self); 147 148 virtual ~Component() override; 149 150 friend struct ComponentStore; 151 152 struct Listener; 153 154 friend struct MultiAccessUnitListener; 155 156 using HwDeathRecipient = ::android::hardware::hidl_death_recipient; 157 sp<HwDeathRecipient> mDeathRecipient; 158 bool mClientDied{false}; 159 }; 160 161 } // namespace utils 162 } // namespace V1_2 163 } // namespace c2 164 } // namespace media 165 } // namespace hardware 166 } // namespace android 167 168 #endif // CODEC2_HIDL_V1_2_UTILS_COMPONENT_H 169