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; 39 bool isAnnexb = false; 40 uint32_t queueIndex = 0; ~SamplePacketSamplePacket41 ~SamplePacket() 42 { 43 for (auto pkt : pkts) { 44 if (pkt) { 45 av_packet_free(&pkt); 46 } 47 } 48 } 49 }; 50 51 class BlockQueuePool { 52 public: 53 explicit BlockQueuePool(std::string name, size_t singleQueSize = SINGLE_QUEUE_SIZE) name_(std::move (name))54 : name_(std::move(name)), quePool_(), queMap_(), sizeMap_(), singleQueSize_(singleQueSize) 55 { 56 } 57 ~BlockQueuePool(); 58 59 Status AddTrackQueue(uint32_t trackIndex); 60 Status RemoveTrackQueue(uint32_t trackIndex); 61 bool HasCache(uint32_t trackIndex); 62 size_t GetCacheSize(uint32_t trackIndex); 63 uint32_t GetCacheDataSize(uint32_t trackIndex); 64 bool ResetInfo(std::shared_ptr<SamplePacket> block); 65 bool SetInfo(std::shared_ptr<SamplePacket> block); 66 void FreeQueue(uint32_t queueIndex); 67 bool Push(uint32_t trackIndex, std::shared_ptr<SamplePacket> block); 68 std::shared_ptr<SamplePacket> Pop(uint32_t trackIndex); 69 std::shared_ptr<SamplePacket> Front(uint32_t trackIndex); 70 std::shared_ptr<SamplePacket> Back(uint32_t trackIndex); 71 Status GetLastPTSByTrackId(uint32_t trackIndex, int64_t& maxPts); 72 73 private: 74 struct InnerQueue { 75 bool isValid {false}; 76 uint32_t dataSize {0}; 77 std::shared_ptr<BlockQueue<std::shared_ptr<SamplePacket>>> blockQue {nullptr}; 78 int64_t maxPts {INT64_MIN}; 79 }; 80 static constexpr size_t SINGLE_QUEUE_SIZE = 100; 81 std::string name_; 82 uint32_t queCount_ {0}; 83 std::map<uint32_t, InnerQueue> quePool_; 84 std::map<uint32_t, std::vector<uint32_t>> queMap_; 85 std::map<uint32_t, uint32_t> sizeMap_; 86 size_t singleQueSize_ {0}; 87 88 uint32_t GetValidQueue(); 89 bool InnerQueueIsFull(uint32_t queueIndex); 90 bool HasQueue(uint32_t trackIndex); 91 void ResetQueue(uint32_t queueIndex); 92 std::recursive_mutex mutextCacheQ_ {}; 93 }; 94 } // namespace Media 95 } // namespace OHOS 96 #endif // BLOCK_QUEUE_POOL_H