1 /* 2 * Copyright (C) 2022 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 #pragma once 18 19 #include <aidl/android/hardware/media/bufferpool2/IAccessor.h> 20 #include <aidl/android/hardware/media/bufferpool2/BnClientManager.h> 21 #include <memory> 22 #include "BufferPoolTypes.h" 23 24 namespace aidl::android::hardware::media::bufferpool2::implementation { 25 26 using aidl::android::hardware::media::bufferpool2::BnClientManager; 27 using aidl::android::hardware::media::bufferpool2::IClientManager; 28 using aidl::android::hardware::media::bufferpool2::IAccessor; 29 30 struct ClientManager : public BnClientManager { 31 // Methods from ::aidl::android::hardware::media::bufferpool2::IClientManager follow. 32 ::ndk::ScopedAStatus registerSender( 33 const std::shared_ptr<IAccessor>& in_bufferPool, 34 ::aidl::android::hardware::media::bufferpool2::IClientManager::Registration* _aidl_return) 35 override; 36 37 /** Gets an instance. */ 38 static std::shared_ptr<ClientManager> getInstance(); 39 40 /** 41 * Creates a local connection with a newly created buffer pool. 42 * 43 * @param allocator for new buffer allocation. 44 * @param pConnectionId Id of the created connection. This is 45 * system-wide unique. 46 * 47 * @return OK when a buffer pool and a local connection is successfully 48 * created. 49 * ResultStatus::NO_MEMORY when there is no memory. 50 * CRITICAL_ERROR otherwise. 51 */ 52 BufferPoolStatus create(const std::shared_ptr<BufferPoolAllocator> &allocator, 53 ConnectionId *pConnectionId); 54 55 /** 56 * Register a created connection as sender for remote process. 57 * 58 * @param receiver The remote receiving process. 59 * @param senderId A local connection which will send buffers to. 60 * @param receiverId Id of the created receiving connection on the receiver 61 * process. 62 * @param isNew @true when the receiving connection is newly created. 63 * 64 * @return OK when the receiving connection is successfully created on the 65 * receiver process. 66 * NOT_FOUND when the sender connection was not found. 67 * CRITICAL_ERROR otherwise. 68 */ 69 BufferPoolStatus registerSender(const std::shared_ptr<IClientManager> &receiver, 70 ConnectionId senderId, 71 ConnectionId *receiverId, 72 bool *isNew); 73 74 /** 75 * Closes the specified connection. 76 * 77 * @param connectionId The id of the connection. 78 * 79 * @return OK when the connection is closed. 80 * NOT_FOUND when the specified connection was not found. 81 * CRITICAL_ERROR otherwise. 82 */ 83 BufferPoolStatus close(ConnectionId connectionId); 84 85 /** 86 * Evicts cached allocations. If it's local connection, release the 87 * previous allocations and do not recycle current active allocations. 88 * 89 * @param connectionId The id of the connection. 90 * 91 * @return OK when the connection is resetted. 92 * NOT_FOUND when the specified connection was not found. 93 * CRITICAL_ERROR otherwise. 94 */ 95 BufferPoolStatus flush(ConnectionId connectionId); 96 97 /** 98 * Allocates a buffer from the specified connection. The output parameter 99 * handle is cloned from the internal handle. So it is safe to use directly, 100 * and it should be deleted and destroyed after use. 101 * 102 * @param connectionId The id of the connection. 103 * @param params The allocation parameters. 104 * @param handle The native handle to the allocated buffer. handle 105 * should be cloned before use. 106 * @param buffer The allocated buffer. 107 * 108 * @return OK when a buffer was allocated successfully. 109 * NOT_FOUND when the specified connection was not found. 110 * NO_MEMORY when there is no memory. 111 * CRITICAL_ERROR otherwise. 112 */ 113 BufferPoolStatus allocate(ConnectionId connectionId, 114 const std::vector<uint8_t> ¶ms, 115 native_handle_t **handle, 116 std::shared_ptr<BufferPoolData> *buffer); 117 118 /** 119 * Receives a buffer for the transaction. The output parameter handle is 120 * cloned from the internal handle. So it is safe to use directly, and it 121 * should be deleted and destoyed after use. 122 * 123 * @param connectionId The id of the receiving connection. 124 * @param transactionId The id for the transaction. 125 * @param bufferId The id for the buffer. 126 * @param timestampMs The timestamp of the buffer is being sent. 127 * @param handle The native handle to the allocated buffer. handle 128 * should be cloned before use. 129 * @param buffer The received buffer. 130 * 131 * @return OK when a buffer was received successfully. 132 * NOT_FOUND when the specified connection was not found. 133 * NO_MEMORY when there is no memory. 134 * CRITICAL_ERROR otherwise. 135 */ 136 BufferPoolStatus receive(ConnectionId connectionId, 137 TransactionId transactionId, 138 BufferId bufferId, 139 int64_t timestampMs, 140 native_handle_t **handle, 141 std::shared_ptr<BufferPoolData> *buffer); 142 143 /** 144 * Posts a buffer transfer transaction to the buffer pool. Sends a buffer 145 * to other remote clients(connection) after this call has been succeeded. 146 * 147 * @param receiverId The id of the receiving connection. 148 * @param buffer to transfer 149 * @param transactionId Id of the transfer transaction. 150 * @param timestampMs The timestamp of the buffer transaction is being 151 * posted. 152 * 153 * @return OK when a buffer transaction was posted successfully. 154 * NOT_FOUND when the sending connection was not found. 155 * CRITICAL_ERROR otherwise. 156 */ 157 BufferPoolStatus postSend(ConnectionId receiverId, 158 const std::shared_ptr<BufferPoolData> &buffer, 159 TransactionId *transactionId, 160 int64_t *timestampMs); 161 162 /** 163 * Time out inactive lingering connections and close. 164 */ 165 void cleanUp(); 166 167 /** Destructs the manager of buffer pool clients. */ 168 ~ClientManager(); 169 private: 170 static std::shared_ptr<ClientManager> sInstance; 171 static std::mutex sInstanceLock; 172 173 class Impl; 174 const std::unique_ptr<Impl> mImpl; 175 176 friend class ::ndk::SharedRefBase; 177 178 ClientManager(); 179 }; 180 181 } // namespace aidl::android::hardware::media::bufferpool2::implementation 182 183