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 "native_avmagic.h"
18 #include "avsharedmemorybase.h"
19 #include "avcodec_log.h"
20 #include "avcodec_errors.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "OH_AVMemory"};
24 }
25
26 using namespace OHOS::MediaAVCodec;
27
OH_AVMemory(const std::shared_ptr<OHOS::MediaAVCodec::AVSharedMemory> & mem)28 OH_AVMemory::OH_AVMemory(const std::shared_ptr<OHOS::MediaAVCodec::AVSharedMemory> &mem)
29 : AVObjectMagic(AVMagic::AVCODEC_MAGIC_SHARED_MEMORY), memory_(mem)
30 {
31 }
32
~OH_AVMemory()33 OH_AVMemory::~OH_AVMemory()
34 {
35 }
36
IsEqualMemory(const std::shared_ptr<OHOS::MediaAVCodec::AVSharedMemory> & mem)37 bool OH_AVMemory::IsEqualMemory(const std::shared_ptr<OHOS::MediaAVCodec::AVSharedMemory> &mem)
38 {
39 return (mem == memory_) ? true : false;
40 }
41
OH_AVMemory_Create(int32_t size)42 struct OH_AVMemory *OH_AVMemory_Create(int32_t size)
43 {
44 CHECK_AND_RETURN_RET_LOG(size >= 0, nullptr, "size %{public}d is error!", size);
45 std::shared_ptr<AVSharedMemoryBase> sharedMemory =
46 std::make_shared<AVSharedMemoryBase>(size, AVSharedMemory::FLAGS_READ_WRITE, "userBuffer");
47 CHECK_AND_RETURN_RET_LOG(sharedMemory->Init() == AVCS_ERR_OK, nullptr,
48 "create OH_AVMemory failed");
49
50 struct OH_AVMemory *mem = new(std::nothrow) OH_AVMemory(sharedMemory);
51 CHECK_AND_RETURN_RET_LOG(mem != nullptr, nullptr, "failed to new OH_AVMemory");
52 mem->isUserCreated = true;
53
54 return mem;
55 }
56
OH_AVMemory_GetAddr(struct OH_AVMemory * mem)57 uint8_t *OH_AVMemory_GetAddr(struct OH_AVMemory *mem)
58 {
59 CHECK_AND_RETURN_RET_LOG(mem != nullptr, nullptr, "input mem is nullptr!");
60 CHECK_AND_RETURN_RET_LOG(mem->magic_ == AVMagic::AVCODEC_MAGIC_SHARED_MEMORY, nullptr, "magic error!");
61 CHECK_AND_RETURN_RET_LOG(mem->memory_ != nullptr, nullptr, "memory is nullptr!");
62 return mem->memory_->GetBase();
63 }
64
OH_AVMemory_GetSize(struct OH_AVMemory * mem)65 int32_t OH_AVMemory_GetSize(struct OH_AVMemory *mem)
66 {
67 CHECK_AND_RETURN_RET_LOG(mem != nullptr, -1, "input mem is nullptr!");
68 CHECK_AND_RETURN_RET_LOG(mem->magic_ == AVMagic::AVCODEC_MAGIC_SHARED_MEMORY, -1, "magic error!");
69 CHECK_AND_RETURN_RET_LOG(mem->memory_ != nullptr, -1, "memory is nullptr!");
70 return mem->memory_->GetSize();
71 }
72
OH_AVMemory_Destroy(struct OH_AVMemory * mem)73 OH_AVErrCode OH_AVMemory_Destroy(struct OH_AVMemory *mem)
74 {
75 CHECK_AND_RETURN_RET_LOG(mem != nullptr, AV_ERR_INVALID_VAL, "input mem is nullptr!");
76 CHECK_AND_RETURN_RET_LOG(mem->magic_ == AVMagic::AVCODEC_MAGIC_SHARED_MEMORY, AV_ERR_INVALID_VAL, "magic error!");
77 CHECK_AND_RETURN_RET_LOG(mem->isUserCreated, AV_ERR_INVALID_VAL, "input mem is not user created!");
78 delete mem;
79 return AV_ERR_OK;
80 }