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