1 /*
2 * Copyright (c) 2025 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 HISTREAMER_PLUGINS_COMMON_BUFFER_H
17 #define HISTREAMER_PLUGINS_COMMON_BUFFER_H
18
19 #include <memory>
20 #include <map>
21 #include <vector>
22
23 #include "plugin_memory.h"
24 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
25 #include "refbase.h"
26 #include "surface/surface.h"
27 #endif
28
29 namespace OHOS {
30 namespace Media {
31 namespace Plugins {
32
33 // Align value template
34 template <typename T>
35 using MakeUnsigned = typename std::make_unsigned<T>::type;
36
37 template <typename T, typename U>
AlignUp(T num,U alignment)38 constexpr T AlignUp(T num, U alignment)
39 {
40 return (alignment > 0) ? (static_cast<uint64_t>((num + static_cast<MakeUnsigned<T>>(alignment) - 1)) &
41 static_cast<uint64_t>((~(static_cast<MakeUnsigned<T>>(alignment) - 1)))) :
42 num;
43 }
44
45 /**
46 * @brief Buffer base class.
47 * Contains the data storage of the buffer (buffer description information).
48 *
49 * @since 1.0
50 * @version 1.0
51 */
52 class Buffer {
53 public:
54 /// Construct an empty buffer.
55 explicit Buffer() = default;
56
57 virtual ~Buffer() = default;
58
59 static std::shared_ptr<Buffer> CreateDefaultBuffer(size_t capacity, std::shared_ptr<Allocator> allocator = nullptr,
60 size_t align = 1)
61 {
62 return nullptr;
63 }
64 virtual std::shared_ptr<Memory> WrapMemory(uint8_t* data, size_t capacity, size_t size) = 0;
65
66 virtual std::shared_ptr<Memory> WrapMemoryPtr(std::shared_ptr<uint8_t> data, size_t capacity, size_t size) = 0;
67
68 virtual std::shared_ptr<Memory> AllocMemory(std::shared_ptr<Allocator> allocator,
69 size_t capacity, size_t align = 1) = 0;
70
71 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
72 virtual std::shared_ptr<Memory> WrapSurfaceMemory(sptr<SurfaceBuffer> surfaceBuffer) = 0;
73 #endif
74
75 virtual uint32_t GetMemoryCount() = 0;
76
77 virtual std::shared_ptr<Memory> GetMemory() = 0;
78
79 virtual void Reset() = 0;
80
81 virtual bool IsEmpty() = 0;
82
83 int32_t streamID;
84
85 uint32_t trackID;
86
87 int64_t pts;
88
89 int64_t dts;
90
91 int64_t duration;
92
93 uint64_t flag;
94
95 std::vector<std::shared_ptr<Memory>> data {};
96 };
97 } // namespace Plugins
98 } // namespace Media
99 } // namespace OHOS
100 #endif // HISTREAMER_PLUGINS_COMMON_BUFFER_H
101