1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BLOCK_QUEUE_POOL_H 17 #define BLOCK_QUEUE_POOL_H 18 #include <vector> 19 #include <map> 20 #include <cstdint> 21 #include "block_queue.h" 22 #include "common/status.h" 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 #include "libavcodec/avcodec.h" 28 #ifdef __cplusplus 29 } 30 #endif 31 32 namespace OHOS { 33 namespace Media { 34 35 struct SamplePacket { 36 uint32_t offset = 0; 37 std::vector<AVPacket*> pkts {}; 38 bool isEOS = false; ~SamplePacketSamplePacket39 ~SamplePacket() 40 { 41 for (auto pkt : pkts) { 42 if (pkt) { 43 av_packet_free(&pkt); 44 } 45 } 46 } 47 }; 48 49 class BlockQueuePool { 50 public: 51 explicit BlockQueuePool(std::string name, size_t singleQueSize = SINGLE_QUEUE_SIZE) 52 : name_(std::move(name)), quePool_(), queMap_(), sizeMap_(), singleQueSize_(singleQueSize) 53 { 54 } 55 ~BlockQueuePool(); 56 57 Status AddTrackQueue(uint32_t trackIndex); 58 Status RemoveTrackQueue(uint32_t trackIndex); 59 bool HasCache(uint32_t trackIndex); 60 size_t GetCacheSize(uint32_t trackIndex); 61 uint32_t GetCacheDataSize(uint32_t trackIndex); 62 void FreeQueue(uint32_t queueIndex); 63 bool Push(uint32_t trackIndex, std::shared_ptr<SamplePacket> block); 64 std::shared_ptr<SamplePacket> Pop(uint32_t trackIndex); 65 std::shared_ptr<SamplePacket> Front(uint32_t trackIndex); 66 std::shared_ptr<SamplePacket> Back(uint32_t trackIndex); 67 68 private: 69 struct InnerQueue { 70 bool isValid {false}; 71 uint32_t dataSize {0}; 72 std::shared_ptr<BlockQueue<std::shared_ptr<SamplePacket>>> blockQue {nullptr}; 73 }; 74 static constexpr size_t SINGLE_QUEUE_SIZE = 100; 75 std::string name_; 76 uint32_t queCount_ {0}; 77 std::map<uint32_t, InnerQueue> quePool_; 78 std::map<uint32_t, std::vector<uint32_t>> queMap_; 79 std::map<uint32_t, uint32_t> sizeMap_; 80 size_t singleQueSize_ {0}; 81 82 uint32_t GetValidQueue(); 83 bool InnerQueueIsFull(uint32_t queueIndex); 84 bool HasQueue(uint32_t trackIndex); 85 void ResetQueue(uint32_t queueIndex); 86 std::recursive_mutex mutextCacheQ_ {}; 87 }; 88 } // namespace Media 89 } // namespace OHOS 90 #endif // BLOCK_QUEUE_POOL_H