• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
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 APPSRC_MEMORY_H
17 #define APPSRC_MEMORY_H
18 
19 #include <queue>
20 #include "avsharedmemory.h"
21 #include "nocopyable.h"
22 
23 namespace OHOS {
24 namespace Media {
25 class AppsrcMemory : public NoCopyable {
26 public:
27     AppsrcMemory();
28     ~AppsrcMemory();
29     uint32_t GetBufferSize() const;
30     std::shared_ptr<AVSharedMemory> GetMem();
31     uint32_t GetFreeSize() const;
32     uint32_t GetAvailableSize() const;
33     uint32_t GetBeginPos() const;
34     uint32_t GetAvailableBeginPos() const;
35     uint64_t GetPushOffset() const;
36     uint64_t GetFilePos() const;
37 
38     void SetBufferSize(uint32_t bufferSize);
39     void SetMem(std::shared_ptr<AVSharedMemory> mem);
40     void SetNoFreeBuffer(bool flag);
41     void SetNoAvailableBuffer(bool flag);
42     void ResetMemParam();
43     void RestoreMemParam();
44 
45     void SeekAndChangePos(uint64_t pos);
46     void PullBufferAndChangePos(uint32_t readSize);
47     void PushBufferAndChangePos(uint32_t pushSize, bool isCopy);
48     bool FreeBufferAndChangePos(uint32_t offset, uint32_t length, bool isCopymode);
49     bool CopyBufferAndChangePos(std::shared_ptr<AppsrcMemory> &mem);
50 
51     bool IsMemSuccessive();
52     bool IsNeedCopy(uint32_t copySize);
53     void PrintCurPos();
54     void CheckBufferUsage();
55 private:
56     void PushUnusedBuffers(std::pair<uint32_t, uint32_t> unusedBuffer);
57     void RemoveUnusedBuffer();
58     bool ProcessBuffer(uint32_t offset, uint32_t length);
59     bool IsUnreturnedBuffer(uint32_t offset, uint32_t length,
60         std::deque<std::pair<uint32_t, uint32_t>>::iterator &iter);
61     bool PushBufferToUnreturnedBuffers(uint32_t offset, uint32_t length);
62 
63     std::shared_ptr<AVSharedMemory> mem_;
64     uint32_t bufferSize_;
65     // The position of mem in file
66     uint64_t filePos_;
67     // The start position that can be filled
68     uint32_t begin_;
69     // The end position that can be filled
70     uint32_t end_;
71     // The start position that can be push to appsrc
72     uint32_t availableBegin_;
73     // The offset of the next data to be pushed
74     uint64_t pushOffset_;
75     bool noFreeBuffer_ = false;
76     bool noAvailableBuffer_ = true;
77     // These buffers are not be used
78     std::queue<std::pair<uint32_t, uint32_t>> unusedBuffers_;
79     // These buffers are used but not returned
80     std::deque<std::pair<uint32_t, uint32_t>> unreturnedBuffers_;
81 };
82 } // namespace Media
83 } // namespace OHOS
84 #endif /* APPSRC_MEMORY_H */