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_AIDL_BUFFER_TYPES_H 18 #define CODEC2_AIDL_BUFFER_TYPES_H 19 20 #include <codec2/common/BufferTypes.h> 21 22 #include <aidl/android/hardware/media/bufferpool2/BufferStatusMessage.h> 23 #include <aidl/android/hardware/media/bufferpool2/IClientManager.h> 24 #include <aidl/android/hardware/media/bufferpool2/ResultStatus.h> 25 #include <aidl/android/hardware/media/c2/WorkBundle.h> 26 27 #include <bufferpool2/BufferPoolTypes.h> 28 #include <bufferpool2/ClientManager.h> 29 30 31 namespace aidl { 32 namespace android { 33 namespace hardware { 34 namespace media { 35 namespace c2 { 36 namespace utils { 37 38 namespace bufferpool2 = ::aidl::android::hardware::media::bufferpool2; 39 40 using namespace std::chrono_literals; 41 42 struct BufferPoolTypes { 43 typedef bufferpool2::BufferPoolData BufferPoolData; 44 typedef bufferpool2::ResultStatus ResultStatus; 45 typedef bufferpool2::implementation::BufferPoolStatus BufferPoolStatus; 46 typedef bufferpool2::BufferStatusMessage BufferStatusMessage; 47 }; 48 49 // Default implementation of BufferPoolSender. 50 // 51 // To use DefaultBufferPoolSender, the IClientManager instance of the receiving 52 // process must be set before send() can operate. DefaultBufferPoolSender will 53 // hold a strong reference to the IClientManager instance and use it to call 54 // IClientManager::registerSender() to establish the bufferpool connection when 55 // send() is called. 56 struct DefaultBufferPoolSender : ::android::BufferPoolSender<BufferPoolTypes> { 57 typedef bufferpool2::implementation::ClientManager ClientManager; 58 typedef bufferpool2::IClientManager IClientManager; 59 60 // Set the IClientManager instance of the receiving process and the refresh 61 // interval for the connection. The default interval is 4.5 seconds, which 62 // is slightly shorter than the amount of time the bufferpool will keep an 63 // inactive connection for. 64 DefaultBufferPoolSender( 65 const std::shared_ptr<IClientManager>& receiverManager = nullptr, 66 std::chrono::steady_clock::duration refreshInterval = 4500ms); 67 68 // Set the IClientManager instance of the receiving process and the refresh 69 // interval for the connection. The default interval is 4.5 seconds, which 70 // is slightly shorter than the amount of time the bufferpool will keep an 71 // inactive connection for. 72 void setReceiver( 73 const std::shared_ptr<IClientManager>& receiverManager, 74 std::chrono::steady_clock::duration refreshInterval = 4500ms); 75 76 // Implementation of BufferPoolSender::send(). send() will establish a 77 // bufferpool connection if needed, then send the bufferpool data over to 78 // the receiving process. 79 BufferPoolStatus send( 80 const std::shared_ptr<BufferPoolData>& bpData, 81 BufferStatusMessage* bpMessage) override; 82 83 private: 84 std::mutex mMutex; 85 std::shared_ptr<ClientManager> mSenderManager; 86 std::shared_ptr<IClientManager> mReceiverManager; 87 std::chrono::steady_clock::duration mRefreshInterval; 88 89 struct Connection { 90 int64_t receiverConnectionId; 91 std::chrono::steady_clock::time_point lastSent; ConnectionDefaultBufferPoolSender::Connection92 Connection(int64_t receiverConnectionId, 93 std::chrono::steady_clock::time_point lastSent) 94 : receiverConnectionId(receiverConnectionId), 95 lastSent(lastSent) { 96 } 97 }; 98 99 // Map of connections. 100 // 101 // The key is the connection id. One sender-receiver pair may have multiple 102 // connections. 103 std::map<int64_t, Connection> mConnections; 104 }; 105 106 // std::list<std::unique_ptr<C2Work>> -> WorkBundle 107 // Note: If bufferpool will be used, bpSender must not be null. 108 bool ToAidl( 109 WorkBundle* d, 110 const std::list<std::unique_ptr<C2Work>>& s, 111 ::android::BufferPoolSender<BufferPoolTypes>* bpSender = nullptr); 112 113 // WorkBundle -> std::list<std::unique_ptr<C2Work>> 114 bool FromAidl( 115 std::list<std::unique_ptr<C2Work>>* d, 116 const WorkBundle& s); 117 118 // Return the ownership of output blocks to the client if it is originally 119 // created from the client, after C2Work is returned to the client. 120 // (e.g. C2BqPool / C2IgbaBlockPool) 121 void ReturnOutputBlocksToClientIfNeeded( 122 const std::list<std::unique_ptr<C2Work>>& workList); 123 124 /** 125 * Converts a BufferPool status value to c2_status_t. 126 * \param BufferPool status 127 * \return Corresponding c2_status_t 128 */ 129 c2_status_t toC2Status(BufferPoolTypes::BufferPoolStatus rs); 130 131 } // namespace utils 132 } // namespace c2 133 } // namespace media 134 } // namespace hardware 135 } // namespace android 136 } // namespace aidl 137 138 #endif // CODEC2_AIDL_BUFFER_TYPES_H 139