• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 UTILS_BLOCK_QUEUE_POOL_H
17 #define UTILS_BLOCK_QUEUE_POOL_H
18 #include <vector>
19 #include <map>
20 #include <cstdint>
21 #include "block_queue.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 #include "libavcodec/avcodec.h"
27 #ifdef __cplusplus
28 }
29 #endif
30 
31 namespace OHOS {
32 namespace MediaAVCodec {
33 struct SamplePacket {
34     uint64_t offset;
35     AVPacket* pkt;
36 };
37 
38 class BlockQueuePool {
39 public:
40     explicit BlockQueuePool(std::string name, size_t singleQueSize = SINGLE_QUEUE_SIZE)
name_(std::move (name))41         : name_(std::move(name)), singleQueSize_(singleQueSize)
42     {
43     }
44     ~BlockQueuePool();
45 
46     int32_t AddTrackQueue(uint32_t trackIndex);
47     int32_t RemoveTrackQueue(uint32_t trackIndex);
48     bool HasCache(uint32_t trackIndex);
49     void ResetQueue(uint32_t queueIndex);
50     void FreeQueue(uint32_t queueIndex);
51     bool Push(uint32_t trackIndex, std::shared_ptr<SamplePacket> block);
52     std::shared_ptr<SamplePacket> Pop(uint32_t trackIndex);
53     std::shared_ptr<SamplePacket> Front(uint32_t trackIndex);
54 
55 private:
56     struct InnerQueue {
57         bool isValid;
58         std::shared_ptr<BlockQueue<std::shared_ptr<SamplePacket>>> blockQue;
59     };
60     static constexpr size_t SINGLE_QUEUE_SIZE = 100;
61     std::string name_;
62     uint32_t queCount_ = 0;
63     std::map<uint32_t, InnerQueue> quePool_;
64     std::map<uint32_t, std::vector<uint32_t>> queMap_;
65     size_t singleQueSize_;
66     uint32_t GetValidQueue();
67     bool InnerQueueIsFull(uint32_t queueIndex);
68     bool HasQueue(uint32_t trackIndex);
69 };
70 } // namespace MediaAVCodec
71 } // namespace OHOS
72 #endif // !UTILS_BLOCK_QUEUE_POOL_H