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 "native_avmemory.h"
17fa7767c5Sopenharmony_ci#include "buffer/avsharedmemorybase.h"
18fa7767c5Sopenharmony_ci#include "common/log.h"
19fa7767c5Sopenharmony_ci#include "common/native_mfmagic.h"
20fa7767c5Sopenharmony_ci#include "common/status.h"
21fa7767c5Sopenharmony_ci
22fa7767c5Sopenharmony_cinamespace {
23fa7767c5Sopenharmony_ciconstexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "NativeAVMemory" };
24fa7767c5Sopenharmony_ci}
25fa7767c5Sopenharmony_ci
26fa7767c5Sopenharmony_ciusing namespace OHOS::Media;
27fa7767c5Sopenharmony_ci
28fa7767c5Sopenharmony_cistruct OH_AVMemory *OH_AVMemory_Create(int32_t size)
29fa7767c5Sopenharmony_ci{
30fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(size >= 0, nullptr, "size %{public}d is error!", size);
31fa7767c5Sopenharmony_ci    std::shared_ptr<AVSharedMemoryBase> sharedMemory =
32fa7767c5Sopenharmony_ci        std::make_shared<AVSharedMemoryBase>(size, AVSharedMemory::FLAGS_READ_WRITE, "userBuffer");
33fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(sharedMemory->Init() == static_cast<int32_t>(Status::OK), nullptr,
34fa7767c5Sopenharmony_ci                         "create OH_AVMemory failed");
35fa7767c5Sopenharmony_ci
36fa7767c5Sopenharmony_ci    struct OH_AVMemory *mem = new (std::nothrow) OH_AVMemory(sharedMemory);
37fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem != nullptr, nullptr, "failed to new OH_AVMemory");
38fa7767c5Sopenharmony_ci    mem->isUserCreated = true;
39fa7767c5Sopenharmony_ci
40fa7767c5Sopenharmony_ci    return mem;
41fa7767c5Sopenharmony_ci}
42fa7767c5Sopenharmony_ci
43fa7767c5Sopenharmony_ciuint8_t *OH_AVMemory_GetAddr(struct OH_AVMemory *mem)
44fa7767c5Sopenharmony_ci{
45fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem != nullptr, nullptr, "input mem is nullptr!");
46fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem->magic_ == MFMagic::MFMAGIC_SHARED_MEMORY, nullptr, "magic error!");
47fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem->memory_ != nullptr, nullptr, "memory is nullptr!");
48fa7767c5Sopenharmony_ci    return mem->memory_->GetBase();
49fa7767c5Sopenharmony_ci}
50fa7767c5Sopenharmony_ci
51fa7767c5Sopenharmony_ciint32_t OH_AVMemory_GetSize(struct OH_AVMemory *mem)
52fa7767c5Sopenharmony_ci{
53fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem != nullptr, -1, "input mem is nullptr!");
54fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem->magic_ == MFMagic::MFMAGIC_SHARED_MEMORY, -1, "magic error!");
55fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem->memory_ != nullptr, -1, "memory is nullptr!");
56fa7767c5Sopenharmony_ci    return mem->memory_->GetSize();
57fa7767c5Sopenharmony_ci}
58fa7767c5Sopenharmony_ci
59fa7767c5Sopenharmony_ciOH_AVErrCode OH_AVMemory_Destroy(struct OH_AVMemory *mem)
60fa7767c5Sopenharmony_ci{
61fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem != nullptr, AV_ERR_INVALID_VAL, "input mem is nullptr!");
62fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem->magic_ == MFMagic::MFMAGIC_SHARED_MEMORY, AV_ERR_INVALID_VAL, "magic error!");
63fa7767c5Sopenharmony_ci    FALSE_RETURN_V_MSG_E(mem->isUserCreated, AV_ERR_INVALID_VAL, "input mem is not user created!");
64fa7767c5Sopenharmony_ci    delete mem;
65fa7767c5Sopenharmony_ci    return AV_ERR_OK;
66fa7767c5Sopenharmony_ci}