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