1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#ifndef HISTREAMER_PLUGIN_COMMON_BUFFER_H
17fa7767c5Sopenharmony_ci#define HISTREAMER_PLUGIN_COMMON_BUFFER_H
18fa7767c5Sopenharmony_ci
19fa7767c5Sopenharmony_ci#include <memory>
20fa7767c5Sopenharmony_ci#include <map>
21fa7767c5Sopenharmony_ci#include <vector>
22fa7767c5Sopenharmony_ci
23fa7767c5Sopenharmony_ci#include "plugin_memory.h"
24fa7767c5Sopenharmony_ci#if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
25fa7767c5Sopenharmony_ci#include "refbase.h"
26fa7767c5Sopenharmony_ci#include "surface/surface.h"
27fa7767c5Sopenharmony_ci#endif
28fa7767c5Sopenharmony_ci
29fa7767c5Sopenharmony_cinamespace OHOS {
30fa7767c5Sopenharmony_cinamespace Media {
31fa7767c5Sopenharmony_cinamespace Plugins {
32fa7767c5Sopenharmony_ci
33fa7767c5Sopenharmony_ci// Align value template
34fa7767c5Sopenharmony_citemplate <typename T>
35fa7767c5Sopenharmony_ciusing MakeUnsigned = typename std::make_unsigned<T>::type;
36fa7767c5Sopenharmony_ci
37fa7767c5Sopenharmony_citemplate <typename T, typename U>
38fa7767c5Sopenharmony_ciconstexpr T AlignUp(T num, U alignment)
39fa7767c5Sopenharmony_ci{
40fa7767c5Sopenharmony_ci    return (alignment > 0) ? (static_cast<uint64_t>((num + static_cast<MakeUnsigned<T>>(alignment) - 1)) &
41fa7767c5Sopenharmony_ci        static_cast<uint64_t>((~(static_cast<MakeUnsigned<T>>(alignment) - 1)))) :
42fa7767c5Sopenharmony_ci        num;
43fa7767c5Sopenharmony_ci}
44fa7767c5Sopenharmony_ci
45fa7767c5Sopenharmony_ci/**
46fa7767c5Sopenharmony_ci* @brief Buffer base class.
47fa7767c5Sopenharmony_ci* Contains the data storage of the buffer (buffer description information).
48fa7767c5Sopenharmony_ci*
49fa7767c5Sopenharmony_ci* @since 1.0
50fa7767c5Sopenharmony_ci* @version 1.0
51fa7767c5Sopenharmony_ci*/
52fa7767c5Sopenharmony_ciclass Buffer {
53fa7767c5Sopenharmony_cipublic:
54fa7767c5Sopenharmony_ci    /// Construct an empty buffer.
55fa7767c5Sopenharmony_ci    explicit Buffer();
56fa7767c5Sopenharmony_ci
57fa7767c5Sopenharmony_ci    /// Destructor
58fa7767c5Sopenharmony_ci    ~Buffer() = default;
59fa7767c5Sopenharmony_ci
60fa7767c5Sopenharmony_ci    static std::shared_ptr<Buffer> CreateDefaultBuffer(size_t capacity, std::shared_ptr<Allocator> allocator = nullptr,
61fa7767c5Sopenharmony_ci                                                       size_t align = 1);
62fa7767c5Sopenharmony_ci
63fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> WrapMemory(uint8_t* data, size_t capacity, size_t size);
64fa7767c5Sopenharmony_ci
65fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> WrapMemoryPtr(std::shared_ptr<uint8_t> data, size_t capacity, size_t size);
66fa7767c5Sopenharmony_ci
67fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> AllocMemory(std::shared_ptr<Allocator> allocator, size_t capacity, size_t align = 1);
68fa7767c5Sopenharmony_ci
69fa7767c5Sopenharmony_ci#if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
70fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> WrapSurfaceMemory(sptr<SurfaceBuffer> surfaceBuffer);
71fa7767c5Sopenharmony_ci#endif
72fa7767c5Sopenharmony_ci
73fa7767c5Sopenharmony_ci    uint32_t GetMemoryCount();
74fa7767c5Sopenharmony_ci
75fa7767c5Sopenharmony_ci    std::shared_ptr<Memory> GetMemory(uint32_t index = 0);
76fa7767c5Sopenharmony_ci
77fa7767c5Sopenharmony_ci    void Reset();
78fa7767c5Sopenharmony_ci
79fa7767c5Sopenharmony_ci    /// no memory in the buffer.
80fa7767c5Sopenharmony_ci    bool IsEmpty();
81fa7767c5Sopenharmony_ci
82fa7767c5Sopenharmony_ci    /// next buffer stream index(dash)
83fa7767c5Sopenharmony_ci    int32_t streamID;
84fa7767c5Sopenharmony_ci
85fa7767c5Sopenharmony_ci    /// track index.
86fa7767c5Sopenharmony_ci    uint32_t trackID;
87fa7767c5Sopenharmony_ci
88fa7767c5Sopenharmony_ci    /// presentation timestamp of the buffer based on {@link HST_TIME_BASE}.
89fa7767c5Sopenharmony_ci    int64_t pts;
90fa7767c5Sopenharmony_ci
91fa7767c5Sopenharmony_ci    /// decoding timestamp of the buffer based on {@link HST_TIME_BASE}.
92fa7767c5Sopenharmony_ci    int64_t dts;
93fa7767c5Sopenharmony_ci
94fa7767c5Sopenharmony_ci    /// duration in time of the buffer data based on {@link HST_TIME_BASE}.
95fa7767c5Sopenharmony_ci    int64_t duration;
96fa7767c5Sopenharmony_ci
97fa7767c5Sopenharmony_ci    /// flag of the buffer, which is used to record extra information.
98fa7767c5Sopenharmony_ci    /// @see BUFFER_FLAG_EOS
99fa7767c5Sopenharmony_ci    uint64_t flag;
100fa7767c5Sopenharmony_ci
101fa7767c5Sopenharmony_ciprivate:
102fa7767c5Sopenharmony_ci    /// Data described by this buffer.
103fa7767c5Sopenharmony_ci    std::vector<std::shared_ptr<Memory>> data {};
104fa7767c5Sopenharmony_ci};
105fa7767c5Sopenharmony_ci} // namespace Plugins
106fa7767c5Sopenharmony_ci} // namespace Media
107fa7767c5Sopenharmony_ci} // namespace OHOS
108fa7767c5Sopenharmony_ci#endif // HISTREAMER_PLUGIN_COMMON_BUFFER_H
109