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 <C2Component.h> 33 #include <C2Buffer.h> 34 #include <C2.h> 35 36 #include <map> 37 #include <memory> 38 #include <mutex> 39 40 namespace android { 41 namespace hardware { 42 namespace media { 43 namespace c2 { 44 namespace V1_2 { 45 46 using ::android::hardware::media::c2::V1_2::IComponent; 47 using ::android::hardware::media::c2::V1_0::IComponentListener; 48 49 namespace utils { 50 51 using ::android::hardware::hidl_array; 52 using ::android::hardware::hidl_memory; 53 using ::android::hardware::hidl_string; 54 using ::android::hardware::hidl_vec; 55 using ::android::hardware::Return; 56 using ::android::hardware::Void; 57 using ::android::hardware::IBinder; 58 using ::android::sp; 59 using ::android::wp; 60 61 struct ComponentStore; 62 63 struct Component : public IComponent, 64 public std::enable_shared_from_this<Component> { 65 Component( 66 const std::shared_ptr<C2Component>&, 67 const sp<IComponentListener>& listener, 68 const sp<ComponentStore>& store, 69 const sp<::android::hardware::media::bufferpool::V2_0:: 70 IClientManager>& clientPoolManager); 71 c2_status_t status() const; 72 // Receives a death notification of the client. 73 void onDeathReceived(); 74 75 typedef ::android::hardware::graphics::bufferqueue::V1_0:: 76 IGraphicBufferProducer HGraphicBufferProducer1; 77 typedef ::android::hardware::graphics::bufferqueue::V2_0:: 78 IGraphicBufferProducer HGraphicBufferProducer2; 79 80 // Methods from IComponent follow. 81 virtual Return<Status> queue(const WorkBundle& workBundle) override; 82 virtual Return<void> flush(flush_cb _hidl_cb) override; 83 virtual Return<Status> drain(bool withEos) override; 84 virtual Return<Status> setOutputSurface( 85 uint64_t blockPoolId, 86 const sp<HGraphicBufferProducer2>& surface) override; 87 virtual Return<void> connectToInputSurface( 88 const sp<IInputSurface>& inputSurface, 89 connectToInputSurface_cb _hidl_cb) override; 90 virtual Return<void> connectToOmxInputSurface( 91 const sp<HGraphicBufferProducer1>& producer, 92 const sp<::android::hardware::media::omx::V1_0:: 93 IGraphicBufferSource>& source, 94 connectToOmxInputSurface_cb _hidl_cb) override; 95 virtual Return<Status> disconnectFromInputSurface() override; 96 virtual Return<void> createBlockPool( 97 uint32_t allocatorId, 98 createBlockPool_cb _hidl_cb) override; 99 virtual Return<Status> destroyBlockPool(uint64_t blockPoolId) override; 100 virtual Return<Status> start() override; 101 virtual Return<Status> stop() override; 102 virtual Return<Status> reset() override; 103 virtual Return<Status> release() override; 104 virtual Return<sp<IComponentInterface>> getInterface() override; 105 virtual Return<sp<IInputSink>> asInputSink() override; 106 virtual Return<void> configureVideoTunnel( 107 uint32_t avSyncHwId, configureVideoTunnel_cb _hidl_cb) override; 108 virtual Return<Status> setOutputSurfaceWithSyncObj( 109 uint64_t blockPoolId, 110 const sp<HGraphicBufferProducer2>& surface, 111 const SurfaceSyncObj& syncObject) override; 112 113 114 // Returns a C2Component associated to the given sink if the sink is indeed 115 // a local component. Returns nullptr otherwise. 116 // 117 // This function is used by InputSurface::connect(). 118 static std::shared_ptr<C2Component> findLocalComponent( 119 const sp<IInputSink>& sink); 120 121 protected: 122 c2_status_t mInit; 123 std::shared_ptr<C2Component> mComponent; 124 sp<ComponentInterface> mInterface; 125 sp<IComponentListener> mListener; 126 sp<ComponentStore> mStore; 127 ::android::hardware::media::c2::V1_2::utils::DefaultBufferPoolSender 128 mBufferPoolSender; 129 130 struct Sink; 131 std::mutex mSinkMutex; 132 sp<Sink> mSink; 133 134 std::mutex mBlockPoolsMutex; 135 // This map keeps C2BlockPool objects that are created by createBlockPool() 136 // alive. These C2BlockPool objects can be deleted by calling 137 // destroyBlockPool(), reset() or release(), or by destroying the component. 138 std::map<uint64_t, std::shared_ptr<C2BlockPool>> mBlockPools; 139 140 void initListener(const sp<Component>& self); 141 142 virtual ~Component() override; 143 144 friend struct ComponentStore; 145 146 struct Listener; 147 148 using HwDeathRecipient = ::android::hardware::hidl_death_recipient; 149 sp<HwDeathRecipient> mDeathRecipient; 150 bool mClientDied{false}; 151 }; 152 153 } // namespace utils 154 } // namespace V1_2 155 } // namespace c2 156 } // namespace media 157 } // namespace hardware 158 } // namespace android 159 160 #endif // CODEC2_HIDL_V1_2_UTILS_COMPONENT_H 161