1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (C) 2023 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#include "av_hardware_memory.h"
17fa7767c5Sopenharmony_ci#include "av_shared_memory_ext.h"
18fa7767c5Sopenharmony_ci#include "av_surface_memory.h"
19fa7767c5Sopenharmony_ci#include "av_virtual_memory.h"
20fa7767c5Sopenharmony_ci#include "buffer/avallocator.h"
21fa7767c5Sopenharmony_ci#include "buffer/avbuffer.h"
22fa7767c5Sopenharmony_ci#include "common/log.h"
23fa7767c5Sopenharmony_ci#include "common/status.h"
24fa7767c5Sopenharmony_ci#include "message_parcel.h"
25fa7767c5Sopenharmony_ci#include "securec.h"
26fa7767c5Sopenharmony_ci#include "surface_buffer.h"
27fa7767c5Sopenharmony_ci
28fa7767c5Sopenharmony_cinamespace {
29fa7767c5Sopenharmony_ciconstexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "AVMemory" };
30fa7767c5Sopenharmony_ci}
31fa7767c5Sopenharmony_ci
32fa7767c5Sopenharmony_cinamespace {
33fa7767c5Sopenharmony_ciuint64_t GetUniqueId()
34fa7767c5Sopenharmony_ci{
35fa7767c5Sopenharmony_ci#ifdef MEDIA_OHOS
36fa7767c5Sopenharmony_ci    using namespace std::chrono;
37fa7767c5Sopenharmony_ci    static const uint64_t startTime =
38fa7767c5Sopenharmony_ci        static_cast<uint64_t>(time_point_cast<seconds>(system_clock::now()).time_since_epoch().count());
39fa7767c5Sopenharmony_ci    static const uint16_t processId = static_cast<uint16_t>(getpid());
40fa7767c5Sopenharmony_ci#else
41fa7767c5Sopenharmony_ci    static const uint64_t startTime = 0;
42fa7767c5Sopenharmony_ci    static const uint16_t processId = 0;
43fa7767c5Sopenharmony_ci#endif
44fa7767c5Sopenharmony_ci    static std::atomic<uint32_t> bufferId = 0;
45fa7767c5Sopenharmony_ci    if (bufferId == UINT32_MAX) {
46fa7767c5Sopenharmony_ci        bufferId = 0;
47fa7767c5Sopenharmony_ci    }
48fa7767c5Sopenharmony_ci    union UniqueId {
49fa7767c5Sopenharmony_ci        uint64_t startTime;    //  1--16, 16: time
50fa7767c5Sopenharmony_ci        uint16_t processId[4]; // 17--32, 16: process id
51fa7767c5Sopenharmony_ci        uint32_t bufferId[2];  // 33--64, 32: atomic val
52fa7767c5Sopenharmony_ci    } uid = {.startTime = startTime};
53fa7767c5Sopenharmony_ci    ++bufferId;
54fa7767c5Sopenharmony_ci    uid.processId[1] = processId;
55fa7767c5Sopenharmony_ci    uid.bufferId[1] = bufferId;
56fa7767c5Sopenharmony_ci    return uid.startTime;
57fa7767c5Sopenharmony_ci}
58fa7767c5Sopenharmony_ci} // namespace
59fa7767c5Sopenharmony_cinamespace OHOS {
60fa7767c5Sopenharmony_cinamespace Media {
61fa7767c5Sopenharmony_cistd::shared_ptr<AVMemory> AVMemory::CreateAVMemory(std::shared_ptr<AVAllocator> allocator, int32_t capacity,
62fa7767c5Sopenharmony_ci                                                   int32_t align)
63fa7767c5Sopenharmony_ci{
64fa7767c5Sopenharmony_ci    MemoryType type = allocator->GetMemoryType();
65fa7767c5Sopenharmony_ci    std::shared_ptr<AVMemory> mem = nullptr;
66fa7767c5Sopenharmony_ci    switch (type) {
67fa7767c5Sopenharmony_ci        case MemoryType::VIRTUAL_MEMORY: {
68fa7767c5Sopenharmony_ci            mem = std::shared_ptr<AVMemory>(new AVVirtualMemory());
69fa7767c5Sopenharmony_ci            break;
70fa7767c5Sopenharmony_ci        }
71fa7767c5Sopenharmony_ci        case MemoryType::SURFACE_MEMORY: {
72fa7767c5Sopenharmony_ci            mem = std::shared_ptr<AVMemory>(new AVSurfaceMemory());
73fa7767c5Sopenharmony_ci            break;
74fa7767c5Sopenharmony_ci        }
75fa7767c5Sopenharmony_ci        case MemoryType::SHARED_MEMORY: {
76fa7767c5Sopenharmony_ci            mem = std::shared_ptr<AVMemory>(new AVSharedMemoryExt());
77fa7767c5Sopenharmony_ci            break;
78fa7767c5Sopenharmony_ci        }
79fa7767c5Sopenharmony_ci        case MemoryType::HARDWARE_MEMORY: {
80fa7767c5Sopenharmony_ci            mem = std::shared_ptr<AVMemory>(new AVHardwareMemory());
81fa7767c5Sopenharmony_ci            break;
82fa7767c5Sopenharmony_ci        }
83fa7767c5Sopenharmony_ci        default:
84fa7767c5Sopenharmony_ci            break;
85fa7767c5Sopenharmony_ci    }
86fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem != nullptr, nullptr, "Create AVMemory failed, no memory");
87fa7767c5Sopenharmony_ci
88fa7767c5Sopenharmony_ci    mem->allocator_ = allocator;
89fa7767c5Sopenharmony_ci    mem->capacity_ = capacity;
90fa7767c5Sopenharmony_ci    mem->align_ = align;
91fa7767c5Sopenharmony_ci    Status ret = mem->Init();
92fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret == Status::OK, nullptr, "Init AVMemory failed, uid:" PUBLIC_LOG_U64, mem->uid_);
93fa7767c5Sopenharmony_ci    return mem;
94fa7767c5Sopenharmony_ci}
95fa7767c5Sopenharmony_ci
96fa7767c5Sopenharmony_cistd::shared_ptr<AVMemory> AVMemory::CreateAVMemory(uint8_t *ptr, int32_t capacity, int32_t size)
97fa7767c5Sopenharmony_ci{
98fa7767c5Sopenharmony_ci    std::shared_ptr<AVMemory> mem = std::shared_ptr<AVMemory>(new AVVirtualMemory());
99fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem != nullptr, nullptr, "Create AVVirtualMemory failed, no memory");
100fa7767c5Sopenharmony_ci    mem->allocator_ = nullptr;
101fa7767c5Sopenharmony_ci    mem->capacity_ = capacity;
102fa7767c5Sopenharmony_ci    mem->size_ = size;
103fa7767c5Sopenharmony_ci    mem->base_ = ptr;
104fa7767c5Sopenharmony_ci    return mem;
105fa7767c5Sopenharmony_ci}
106fa7767c5Sopenharmony_ci
107fa7767c5Sopenharmony_cistd::shared_ptr<AVMemory> AVMemory::CreateAVMemory(MessageParcel &parcel, bool isSurfaceBuffer)
108fa7767c5Sopenharmony_ci{
109fa7767c5Sopenharmony_ci#ifdef MEDIA_OHOS
110fa7767c5Sopenharmony_ci    if (isSurfaceBuffer) {
111fa7767c5Sopenharmony_ci        auto mem = std::shared_ptr<AVMemory>(new AVSurfaceMemory());
112fa7767c5Sopenharmony_ci        Status ret = mem->InitSurfaceBuffer(parcel);
113fa7767c5Sopenharmony_ci        FALSE_RETURN_V_MSG_E(ret == Status::OK, nullptr, "Init AVSurfaceMemory failed");
114fa7767c5Sopenharmony_ci        return mem;
115fa7767c5Sopenharmony_ci    }
116fa7767c5Sopenharmony_ci    MemoryType type = static_cast<MemoryType>(parcel.ReadUint8());
117fa7767c5Sopenharmony_ci    std::shared_ptr<AVMemory> mem = nullptr;
118fa7767c5Sopenharmony_ci    switch (type) {
119fa7767c5Sopenharmony_ci        case MemoryType::VIRTUAL_MEMORY: {
120fa7767c5Sopenharmony_ci            return nullptr;
121fa7767c5Sopenharmony_ci        }
122fa7767c5Sopenharmony_ci        case MemoryType::SURFACE_MEMORY: {
123fa7767c5Sopenharmony_ci            mem = std::shared_ptr<AVMemory>(new AVSurfaceMemory());
124fa7767c5Sopenharmony_ci            break;
125fa7767c5Sopenharmony_ci        }
126fa7767c5Sopenharmony_ci        case MemoryType::SHARED_MEMORY: {
127fa7767c5Sopenharmony_ci            mem = std::shared_ptr<AVMemory>(new AVSharedMemoryExt());
128fa7767c5Sopenharmony_ci            break;
129fa7767c5Sopenharmony_ci        }
130fa7767c5Sopenharmony_ci        case MemoryType::HARDWARE_MEMORY: {
131fa7767c5Sopenharmony_ci            mem = std::shared_ptr<AVMemory>(new AVHardwareMemory());
132fa7767c5Sopenharmony_ci            break;
133fa7767c5Sopenharmony_ci        }
134fa7767c5Sopenharmony_ci        default:
135fa7767c5Sopenharmony_ci            break;
136fa7767c5Sopenharmony_ci    }
137fa7767c5Sopenharmony_ci
138fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem != nullptr, nullptr, "Create AVMemory failed, no memory");
139fa7767c5Sopenharmony_ci    bool isReadParcel = mem->ReadCommonFromMessageParcel(parcel);
140fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(isReadParcel == true, nullptr, "Read common memory info from parcel failed");
141fa7767c5Sopenharmony_ci
142fa7767c5Sopenharmony_ci    Status ret = mem->Init(parcel);
143fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret == Status::OK, nullptr, "Init AVMemory failed, uid:" PUBLIC_LOG_U64, mem->uid_);
144fa7767c5Sopenharmony_ci    return mem;
145fa7767c5Sopenharmony_ci#else
146fa7767c5Sopenharmony_ci    return nullptr;
147fa7767c5Sopenharmony_ci#endif
148fa7767c5Sopenharmony_ci}
149fa7767c5Sopenharmony_ci
150fa7767c5Sopenharmony_cistd::shared_ptr<AVMemory> AVMemory::CreateAVMemory(sptr<SurfaceBuffer> surfaceBuffer)
151fa7767c5Sopenharmony_ci{
152fa7767c5Sopenharmony_ci    auto mem = std::shared_ptr<AVMemory>(new AVSurfaceMemory());
153fa7767c5Sopenharmony_ci    Status ret = mem->InitSurfaceBuffer(surfaceBuffer);
154fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret == Status::OK, nullptr, "Init AVSurfaceMemory failed");
155fa7767c5Sopenharmony_ci    return mem;
156fa7767c5Sopenharmony_ci}
157fa7767c5Sopenharmony_ci
158fa7767c5Sopenharmony_ciAVMemory::AVMemory() : align_(0), offset_(0), size_(0), base_(nullptr), uid_(GetUniqueId()), allocator_(nullptr)
159fa7767c5Sopenharmony_ci{
160fa7767c5Sopenharmony_ci    MEDIA_LOG_DD("enter ctor, instance:0x%{public}06" PRIXPTR ", uid:" PUBLIC_LOG_U64, FAKE_POINTER(this), uid_);
161fa7767c5Sopenharmony_ci}
162fa7767c5Sopenharmony_ci
163fa7767c5Sopenharmony_ciAVMemory::~AVMemory()
164fa7767c5Sopenharmony_ci{
165fa7767c5Sopenharmony_ci    MEDIA_LOG_DD("enter dtor, instance:0x%{public}06" PRIXPTR ", uid:" PUBLIC_LOG_U64, FAKE_POINTER(this), uid_);
166fa7767c5Sopenharmony_ci}
167fa7767c5Sopenharmony_ci
168fa7767c5Sopenharmony_ciStatus AVMemory::Init()
169fa7767c5Sopenharmony_ci{
170fa7767c5Sopenharmony_ci    return Status::ERROR_UNIMPLEMENTED;
171fa7767c5Sopenharmony_ci}
172fa7767c5Sopenharmony_ci
173fa7767c5Sopenharmony_ciStatus AVMemory::Init(MessageParcel &parcel)
174fa7767c5Sopenharmony_ci{
175fa7767c5Sopenharmony_ci    (void)parcel;
176fa7767c5Sopenharmony_ci    return Status::ERROR_UNIMPLEMENTED;
177fa7767c5Sopenharmony_ci}
178fa7767c5Sopenharmony_ci
179fa7767c5Sopenharmony_ciStatus AVMemory::InitSurfaceBuffer(MessageParcel &parcel)
180fa7767c5Sopenharmony_ci{
181fa7767c5Sopenharmony_ci    (void)parcel;
182fa7767c5Sopenharmony_ci    return Status::ERROR_UNIMPLEMENTED;
183fa7767c5Sopenharmony_ci}
184fa7767c5Sopenharmony_ci
185fa7767c5Sopenharmony_ciStatus AVMemory::InitSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer)
186fa7767c5Sopenharmony_ci{
187fa7767c5Sopenharmony_ci    (void)surfaceBuffer;
188fa7767c5Sopenharmony_ci    return Status::ERROR_UNIMPLEMENTED;
189fa7767c5Sopenharmony_ci}
190fa7767c5Sopenharmony_ci
191fa7767c5Sopenharmony_cibool AVMemory::ReadFromMessageParcel(MessageParcel &parcel)
192fa7767c5Sopenharmony_ci{
193fa7767c5Sopenharmony_ci    (void)parcel;
194fa7767c5Sopenharmony_ci    return false;
195fa7767c5Sopenharmony_ci}
196fa7767c5Sopenharmony_ci
197fa7767c5Sopenharmony_cibool AVMemory::WriteToMessageParcel(MessageParcel &parcel)
198fa7767c5Sopenharmony_ci{
199fa7767c5Sopenharmony_ci    (void)parcel;
200fa7767c5Sopenharmony_ci    return false;
201fa7767c5Sopenharmony_ci}
202fa7767c5Sopenharmony_ci
203fa7767c5Sopenharmony_cibool AVMemory::ReadCommonFromMessageParcel(MessageParcel &parcel)
204fa7767c5Sopenharmony_ci{
205fa7767c5Sopenharmony_ci#ifdef MEDIA_OHOS
206fa7767c5Sopenharmony_ci    (void)parcel.ReadUint64();
207fa7767c5Sopenharmony_ci    int32_t capacity = -1;
208fa7767c5Sopenharmony_ci    int32_t align = -1;
209fa7767c5Sopenharmony_ci    int32_t offset = -1;
210fa7767c5Sopenharmony_ci    int32_t size = -1;
211fa7767c5Sopenharmony_ci
212fa7767c5Sopenharmony_ci    bool ret = parcel.ReadInt32(capacity);
213fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret && (capacity >= 0), false, "capacity is invalid");
214fa7767c5Sopenharmony_ci
215fa7767c5Sopenharmony_ci    ret = parcel.ReadInt32(align);
216fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret && (capacity >= align) && (align >= 0), false, "align is invalid");
217fa7767c5Sopenharmony_ci
218fa7767c5Sopenharmony_ci    ret = parcel.ReadInt32(offset);
219fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret && (capacity >= offset) && (offset >= 0), false, "offset is invalid");
220fa7767c5Sopenharmony_ci
221fa7767c5Sopenharmony_ci    ret = parcel.ReadInt32(size);
222fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret && (capacity >= size) && (size >= 0), false, "size is invalid");
223fa7767c5Sopenharmony_ci
224fa7767c5Sopenharmony_ci    capacity_ = capacity;
225fa7767c5Sopenharmony_ci    align_ = align;
226fa7767c5Sopenharmony_ci    offset_ = offset;
227fa7767c5Sopenharmony_ci    size_ = size;
228fa7767c5Sopenharmony_ci    return true;
229fa7767c5Sopenharmony_ci#else
230fa7767c5Sopenharmony_ci    return true;
231fa7767c5Sopenharmony_ci#endif
232fa7767c5Sopenharmony_ci}
233fa7767c5Sopenharmony_ci
234fa7767c5Sopenharmony_cibool AVMemory::SkipCommonFromMessageParcel(MessageParcel &parcel)
235fa7767c5Sopenharmony_ci{
236fa7767c5Sopenharmony_ci#ifdef MEDIA_OHOS
237fa7767c5Sopenharmony_ci    uint64_t skipSize = 0;
238fa7767c5Sopenharmony_ci    bool ret = parcel.ReadUint64(skipSize);
239fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret, false, "unknown parcel");
240fa7767c5Sopenharmony_ci    parcel.SkipBytes(static_cast<size_t>(skipSize) - 2 * sizeof(int32_t)); // 2: the size of size_ and offset_
241fa7767c5Sopenharmony_ci
242fa7767c5Sopenharmony_ci    int32_t size = -1;
243fa7767c5Sopenharmony_ci    int32_t offset = -1;
244fa7767c5Sopenharmony_ci
245fa7767c5Sopenharmony_ci    ret = parcel.ReadInt32(offset);
246fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret && (capacity_ >= offset) && (offset >= 0), false, "offset is invalid");
247fa7767c5Sopenharmony_ci
248fa7767c5Sopenharmony_ci    ret = parcel.ReadInt32(size);
249fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(ret && (capacity_ >= size) && (size >= 0), false, "size is invalid");
250fa7767c5Sopenharmony_ci
251fa7767c5Sopenharmony_ci    size_ = size;
252fa7767c5Sopenharmony_ci    offset_ = offset;
253fa7767c5Sopenharmony_ci    return true;
254fa7767c5Sopenharmony_ci#else
255fa7767c5Sopenharmony_ci    return true;
256fa7767c5Sopenharmony_ci#endif
257fa7767c5Sopenharmony_ci}
258fa7767c5Sopenharmony_ci
259fa7767c5Sopenharmony_cibool AVMemory::WriteCommonToMessageParcel(MessageParcel &parcel)
260fa7767c5Sopenharmony_ci{
261fa7767c5Sopenharmony_ci#ifdef MEDIA_OHOS
262fa7767c5Sopenharmony_ci    bool ret = true;
263fa7767c5Sopenharmony_ci    MessageParcel bufferParcel;
264fa7767c5Sopenharmony_ci    ret = bufferParcel.WriteInt32(capacity_) && bufferParcel.WriteInt32(align_) && bufferParcel.WriteInt32(offset_) &&
265fa7767c5Sopenharmony_ci          bufferParcel.WriteInt32(size_);
266fa7767c5Sopenharmony_ci
267fa7767c5Sopenharmony_ci    size_t size = bufferParcel.GetDataSize();
268fa7767c5Sopenharmony_ci    return ret && parcel.WriteUint64(static_cast<uint64_t>(size)) && parcel.Append(bufferParcel);
269fa7767c5Sopenharmony_ci#endif
270fa7767c5Sopenharmony_ci    return true;
271fa7767c5Sopenharmony_ci}
272fa7767c5Sopenharmony_ci
273fa7767c5Sopenharmony_ciMemoryType AVMemory::GetMemoryType()
274fa7767c5Sopenharmony_ci{
275fa7767c5Sopenharmony_ci    return MemoryType::VIRTUAL_MEMORY;
276fa7767c5Sopenharmony_ci}
277fa7767c5Sopenharmony_ci
278fa7767c5Sopenharmony_ciMemoryFlag AVMemory::GetMemoryFlag()
279fa7767c5Sopenharmony_ci{
280fa7767c5Sopenharmony_ci    return MemoryFlag::MEMORY_READ_WRITE;
281fa7767c5Sopenharmony_ci}
282fa7767c5Sopenharmony_ci
283fa7767c5Sopenharmony_ciint32_t AVMemory::GetCapacity()
284fa7767c5Sopenharmony_ci{
285fa7767c5Sopenharmony_ci    return capacity_;
286fa7767c5Sopenharmony_ci}
287fa7767c5Sopenharmony_ci
288fa7767c5Sopenharmony_ciint32_t AVMemory::GetSize()
289fa7767c5Sopenharmony_ci{
290fa7767c5Sopenharmony_ci    return size_;
291fa7767c5Sopenharmony_ci}
292fa7767c5Sopenharmony_ci
293fa7767c5Sopenharmony_ciStatus AVMemory::SetSize(int32_t size)
294fa7767c5Sopenharmony_ci{
295fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E((capacity_ >= size) && (size >= 0), Status::ERROR_INVALID_PARAMETER,
296fa7767c5Sopenharmony_ci                         "size out of range, "
297fa7767c5Sopenharmony_ci                         "current size:%{public}d , capacity:%{public}d",
298fa7767c5Sopenharmony_ci                         size_, capacity_);
299fa7767c5Sopenharmony_ci    size_ = size;
300fa7767c5Sopenharmony_ci    return Status::OK;
301fa7767c5Sopenharmony_ci}
302fa7767c5Sopenharmony_ci
303fa7767c5Sopenharmony_ciint32_t AVMemory::GetOffset()
304fa7767c5Sopenharmony_ci{
305fa7767c5Sopenharmony_ci    return offset_;
306fa7767c5Sopenharmony_ci}
307fa7767c5Sopenharmony_ci
308fa7767c5Sopenharmony_ciStatus AVMemory::SetOffset(int32_t offset)
309fa7767c5Sopenharmony_ci{
310fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E((capacity_ >= offset) && (offset >= 0), Status::ERROR_INVALID_PARAMETER,
311fa7767c5Sopenharmony_ci                         "offset out of range, "
312fa7767c5Sopenharmony_ci                         "current offset:%{public}d , capacity:%{public}d",
313fa7767c5Sopenharmony_ci                         offset_, capacity_);
314fa7767c5Sopenharmony_ci    offset_ = offset;
315fa7767c5Sopenharmony_ci    return Status::OK;
316fa7767c5Sopenharmony_ci}
317fa7767c5Sopenharmony_ci
318fa7767c5Sopenharmony_ciuint8_t *AVMemory::GetAddr()
319fa7767c5Sopenharmony_ci{
320fa7767c5Sopenharmony_ci    return base_;
321fa7767c5Sopenharmony_ci}
322fa7767c5Sopenharmony_ci
323fa7767c5Sopenharmony_ciint32_t AVMemory::GetFileDescriptor()
324fa7767c5Sopenharmony_ci{
325fa7767c5Sopenharmony_ci    return -1;
326fa7767c5Sopenharmony_ci}
327fa7767c5Sopenharmony_ci
328fa7767c5Sopenharmony_ciint32_t AVMemory::Write(const uint8_t *in, int32_t writeSize, int32_t position)
329fa7767c5Sopenharmony_ci{
330fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(in != nullptr, 0, "Input buffer is nullptr");
331fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(writeSize > 0, 0, "Input writeSize:%{public}d is invalid", writeSize);
332fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E((GetMemoryFlag() & MemoryFlag::MEMORY_WRITE_ONLY) != 0, 0, "Lack write permission");
333fa7767c5Sopenharmony_ci    uint8_t *addr = GetAddr();
334fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(addr != nullptr, 0, "Base buffer is nullptr");
335fa7767c5Sopenharmony_ci    int32_t start = 0;
336fa7767c5Sopenharmony_ci    if (position <= INVALID_POSITION) {
337fa7767c5Sopenharmony_ci        start = size_;
338fa7767c5Sopenharmony_ci    } else {
339fa7767c5Sopenharmony_ci        start = std::min(position, capacity_);
340fa7767c5Sopenharmony_ci    }
341fa7767c5Sopenharmony_ci    int32_t unusedSize = capacity_ - start;
342fa7767c5Sopenharmony_ci    int32_t length = std::min(writeSize, unusedSize);
343fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E((length + start) <= capacity_, 0,
344fa7767c5Sopenharmony_ci                         "Write out of bounds, length:%{public}d , start:%{public}d , capacity:%{public}d", length,
345fa7767c5Sopenharmony_ci                         start, capacity_);
346fa7767c5Sopenharmony_ci    uint8_t *dstPtr = addr + start;
347fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(dstPtr != nullptr, 0, "Inner dstPtr is nullptr");
348fa7767c5Sopenharmony_ci    auto error = memcpy_s(dstPtr, length, in, length);
349fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(error == EOK, 0, "Inner memcpy_s failed, uid:" PUBLIC_LOG_U64 ", %{public}s", uid_,
350fa7767c5Sopenharmony_ci                         strerror(error));
351fa7767c5Sopenharmony_ci    size_ = start + length;
352fa7767c5Sopenharmony_ci    return length;
353fa7767c5Sopenharmony_ci}
354fa7767c5Sopenharmony_ci
355fa7767c5Sopenharmony_ciint32_t AVMemory::Read(uint8_t *out, int32_t readSize, int32_t position)
356fa7767c5Sopenharmony_ci{
357fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(out != nullptr, 0, "Output buffer is nullptr");
358fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(readSize > 0, 0, "Output readSize:%{public}d is invalid", readSize);
359fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E((GetMemoryFlag() & MemoryFlag::MEMORY_READ_ONLY) != 0, 0, "Lack read permission");
360fa7767c5Sopenharmony_ci    uint8_t *addr = GetAddr();
361fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(addr != nullptr, 0, "Base buffer is nullptr");
362fa7767c5Sopenharmony_ci    int32_t start = 0;
363fa7767c5Sopenharmony_ci    int32_t maxLength = size_;
364fa7767c5Sopenharmony_ci    if (position > INVALID_POSITION) {
365fa7767c5Sopenharmony_ci        start = std::min(position, size_);
366fa7767c5Sopenharmony_ci        maxLength = size_ - start;
367fa7767c5Sopenharmony_ci    }
368fa7767c5Sopenharmony_ci    int32_t length = std::min(readSize, maxLength);
369fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E((length + start) <= capacity_, 0,
370fa7767c5Sopenharmony_ci                         "Read out of bounds, length:%{public}d, start:%{public}d, capacity:%{public}d", length, start,
371fa7767c5Sopenharmony_ci                         capacity_);
372fa7767c5Sopenharmony_ci    uint8_t *srcPtr = addr + start;
373fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(srcPtr != nullptr, 0, "Inner srcPtr is nullptr");
374fa7767c5Sopenharmony_ci    auto error = memcpy_s(out, length, srcPtr, length);
375fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(error == EOK, 0, "Inner memcpy_s failed, uid:%" PUBLIC_LOG_U64 ", %{public}s", uid_,
376fa7767c5Sopenharmony_ci                         strerror(error));
377fa7767c5Sopenharmony_ci    return length;
378fa7767c5Sopenharmony_ci}
379fa7767c5Sopenharmony_ci
380fa7767c5Sopenharmony_civoid AVMemory::Reset()
381fa7767c5Sopenharmony_ci{
382fa7767c5Sopenharmony_ci    size_ = 0;
383fa7767c5Sopenharmony_ci}
384fa7767c5Sopenharmony_ci
385fa7767c5Sopenharmony_cisptr<SurfaceBuffer> AVMemory::GetSurfaceBuffer()
386fa7767c5Sopenharmony_ci{
387fa7767c5Sopenharmony_ci    return nullptr;
388fa7767c5Sopenharmony_ci}
389fa7767c5Sopenharmony_ci} // namespace Media
390fa7767c5Sopenharmony_ci} // namespace OHOS