1 /*
2  * Copyright (c) 2021-2021 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();
56 
57     /// Destructor
58     ~Buffer() = default;
59 
60     static std::shared_ptr<Buffer> CreateDefaultBuffer(size_t capacity, std::shared_ptr<Allocator> allocator = nullptr,
61                                                        size_t align = 1);
62 
63     std::shared_ptr<Memory> WrapMemory(uint8_t* data, size_t capacity, size_t size);
64 
65     std::shared_ptr<Memory> WrapMemoryPtr(std::shared_ptr<uint8_t> data, size_t capacity, size_t size);
66 
67     std::shared_ptr<Memory> AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align = 1);
68 
69 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
70     std::shared_ptr<Memory> WrapSurfaceMemory(sptr<SurfaceBuffer> surfaceBuffer);
71 #endif
72 
73     uint32_t GetMemoryCount();
74 
75     std::shared_ptr<Memory> GetMemory(uint32_t index = 0);
76 
77     void Reset();
78 
79     /// no memory in the buffer.
80     bool IsEmpty();
81 
82     /// next buffer stream index(dash)
83     int32_t streamID;
84 
85     /// track index.
86     uint32_t trackID;
87 
88     /// presentation timestamp of the buffer based on {@link HST_TIME_BASE}.
89     int64_t pts;
90 
91     /// decoding timestamp of the buffer based on {@link HST_TIME_BASE}.
92     int64_t dts;
93 
94     /// duration in time of the buffer data based on {@link HST_TIME_BASE}.
95     int64_t duration;
96 
97     /// flag of the buffer, which is used to record extra information.
98     /// @see BUFFER_FLAG_EOS
99     uint64_t flag;
100 
101 private:
102     /// Data described by this buffer.
103     std::vector<std::shared_ptr<Memory>> data {};
104 };
105 } // namespace Plugins
106 } // namespace Media
107 } // namespace OHOS
108 #endif // HISTREAMER_PLUGINS_COMMON_BUFFER_H
109