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