1049e185fSopenharmony_ci/*
2049e185fSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3049e185fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4049e185fSopenharmony_ci * you may not use this file except in compliance with the License.
5049e185fSopenharmony_ci * You may obtain a copy of the License at
6049e185fSopenharmony_ci *
7049e185fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8049e185fSopenharmony_ci *
9049e185fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10049e185fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11049e185fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12049e185fSopenharmony_ci * See the License for the specific language governing permissions and
13049e185fSopenharmony_ci * limitations under the License.
14049e185fSopenharmony_ci */
15049e185fSopenharmony_ci
16049e185fSopenharmony_ci#ifndef AVDATASRCMEMORY_H
17049e185fSopenharmony_ci#define AVDATASRCMEMORY_H
18049e185fSopenharmony_ci
19049e185fSopenharmony_ci#include <string>
20049e185fSopenharmony_ci#include "nocopyable.h"
21049e185fSopenharmony_ci#include "buffer/avsharedmemorybase.h"
22049e185fSopenharmony_ci
23049e185fSopenharmony_cinamespace OHOS {
24049e185fSopenharmony_cinamespace Media {
25049e185fSopenharmony_ciclass __attribute__((visibility("default"))) AVDataSrcMemory
26049e185fSopenharmony_ci    : public AVSharedMemoryBase {
27049e185fSopenharmony_cipublic:
28049e185fSopenharmony_ci    /**
29049e185fSopenharmony_ci     * @brief Construct a new AVDataSrcMemory object. This function should only be used in the
30049e185fSopenharmony_ci     * local process.
31049e185fSopenharmony_ci     *
32049e185fSopenharmony_ci     * @param size the memory's size, bytes.
33049e185fSopenharmony_ci     * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}.
34049e185fSopenharmony_ci     * @param name the debug string
35049e185fSopenharmony_ci     */
36049e185fSopenharmony_ci    static std::shared_ptr<AVSharedMemory> CreateFromLocal(
37049e185fSopenharmony_ci        int32_t size, uint32_t flags, const std::string &name);
38049e185fSopenharmony_ci
39049e185fSopenharmony_ci    /**
40049e185fSopenharmony_ci     * @brief Construct a new AVDataSrcMemory object. This function should only be used in the
41049e185fSopenharmony_ci     * remote process.
42049e185fSopenharmony_ci     *
43049e185fSopenharmony_ci     * @param fd the memory's fd
44049e185fSopenharmony_ci     * @param size the memory's size, bytes.
45049e185fSopenharmony_ci     * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}.
46049e185fSopenharmony_ci     * @param name the debug string
47049e185fSopenharmony_ci     */
48049e185fSopenharmony_ci    static std::shared_ptr<AVSharedMemory> CreateFromRemote(
49049e185fSopenharmony_ci        int32_t fd, int32_t size, uint32_t flags, const std::string &name);
50049e185fSopenharmony_ci
51049e185fSopenharmony_ci    virtual ~AVDataSrcMemory();
52049e185fSopenharmony_ci
53049e185fSopenharmony_ci    /**
54049e185fSopenharmony_ci     * @brief Construct a new AVSharedMemoryBase object. This function should only be used in the
55049e185fSopenharmony_ci     * local process.
56049e185fSopenharmony_ci     *
57049e185fSopenharmony_ci     * @param size the memory's size, bytes.
58049e185fSopenharmony_ci     * @param flags the memory's accessible flags, refer to {@AVSharedMemory::Flags}.
59049e185fSopenharmony_ci     * @param name the debug string
60049e185fSopenharmony_ci     */
61049e185fSopenharmony_ci    AVDataSrcMemory(int32_t size, uint32_t flags, const std::string &name);
62049e185fSopenharmony_ci
63049e185fSopenharmony_ci    /**
64049e185fSopenharmony_ci     * @brief Get the memory's virtual address
65049e185fSopenharmony_ci     * @return the memory's virtual address if the memory is valid, otherwise nullptr.
66049e185fSopenharmony_ci     */
67049e185fSopenharmony_ci    virtual uint8_t *GetBase() const override
68049e185fSopenharmony_ci    {
69049e185fSopenharmony_ci        return AVSharedMemoryBase::GetBase() + offset_;
70049e185fSopenharmony_ci    }
71049e185fSopenharmony_ci
72049e185fSopenharmony_ci    uint8_t *GetInnerBase() const
73049e185fSopenharmony_ci    {
74049e185fSopenharmony_ci        return AVSharedMemoryBase::GetBase();
75049e185fSopenharmony_ci    }
76049e185fSopenharmony_ci
77049e185fSopenharmony_ci    void SetOffset(uint32_t offset)
78049e185fSopenharmony_ci    {
79049e185fSopenharmony_ci        offset_ = offset;
80049e185fSopenharmony_ci    }
81049e185fSopenharmony_ci
82049e185fSopenharmony_ci    uint32_t GetOffset() const
83049e185fSopenharmony_ci    {
84049e185fSopenharmony_ci        return offset_;
85049e185fSopenharmony_ci    }
86049e185fSopenharmony_ciprotected:
87049e185fSopenharmony_ci    AVDataSrcMemory(int32_t fd, int32_t size, uint32_t flags, const std::string &name);
88049e185fSopenharmony_ciprivate:
89049e185fSopenharmony_ci    uint32_t offset_;
90049e185fSopenharmony_ci};
91049e185fSopenharmony_ci} // namespace Media
92049e185fSopenharmony_ci} // namespace OHOS
93049e185fSopenharmony_ci#endif