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 "avsharedmemorybase.h"
17 #include <sys/mman.h>
18 #include <unistd.h>
19 #include "ashmem.h"
20 #include "avcodec_errors.h"
21 #include "avcodec_log.h"
22 #include "scope_guard.h"
23 #include "securec.h"
24
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVSharedMemoryBase"};
27 constexpr uint8_t LOGD_FREQUENCY = 5;
28 }
29
30 namespace OHOS {
31 namespace MediaAVCodec {
32 struct AVSharedMemoryBaseImpl : public AVSharedMemoryBase {
33 public:
AVSharedMemoryBaseImplOHOS::MediaAVCodec::AVSharedMemoryBaseImpl34 AVSharedMemoryBaseImpl(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
35 : AVSharedMemoryBase(fd, size, flags, name) {}
36 };
37
CreateFromLocal(int32_t size,uint32_t flags,const std::string & name)38 std::shared_ptr<AVSharedMemory> AVSharedMemoryBase::CreateFromLocal(
39 int32_t size, uint32_t flags, const std::string &name)
40 {
41 std::shared_ptr<AVSharedMemoryBase> memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
42 int32_t ret = memory->Init();
43 if (ret != AVCS_ERR_OK) {
44 AVCODEC_LOGE("Create avsharedmemory failed, ret = %{public}d", ret);
45 return nullptr;
46 }
47
48 return memory;
49 }
50
CreateFromRemote(int32_t fd,int32_t size,uint32_t flags,const std::string & name)51 std::shared_ptr<AVSharedMemory> AVSharedMemoryBase::CreateFromRemote(
52 int32_t fd, int32_t size, uint32_t flags, const std::string &name)
53 {
54 std::shared_ptr<AVSharedMemoryBase> memory = std::make_shared<AVSharedMemoryBaseImpl>(fd, size, flags, name);
55 int32_t ret = memory->Init();
56 if (ret != AVCS_ERR_OK) {
57 AVCODEC_LOGE("Create avsharedmemory failed, ret = %{public}d", ret);
58 return nullptr;
59 }
60
61 return memory;
62 }
63
AVSharedMemoryBase(int32_t size,uint32_t flags,const std::string & name)64 AVSharedMemoryBase::AVSharedMemoryBase(int32_t size, uint32_t flags, const std::string &name)
65 : base_(nullptr), capacity_(size), flags_(flags), name_(name), fd_(-1), size_(0)
66 {
67 AVCODEC_LOGD_LIMIT(LOGD_FREQUENCY, "enter ctor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
68 FAKE_POINTER(this), name_.c_str());
69 }
70
AVSharedMemoryBase(int32_t fd,int32_t size,uint32_t flags,const std::string & name)71 AVSharedMemoryBase::AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
72 : base_(nullptr), capacity_(size), flags_(flags), name_(name), fd_(dup(fd)), size_(0)
73 {
74 AVCODEC_LOGD_LIMIT(LOGD_FREQUENCY, "enter ctor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
75 FAKE_POINTER(this), name_.c_str());
76 }
77
~AVSharedMemoryBase()78 AVSharedMemoryBase::~AVSharedMemoryBase()
79 {
80 AVCODEC_LOGD_LIMIT(LOGD_FREQUENCY, "enter dtor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
81 FAKE_POINTER(this), name_.c_str());
82 Close();
83 }
84
Init(bool isMapVirAddr)85 int32_t AVSharedMemoryBase::Init(bool isMapVirAddr)
86 {
87 ON_SCOPE_EXIT(0) {
88 AVCODEC_LOGE("create avsharedmemory failed, name = %{public}s, size = %{public}d, "
89 "flags = 0x%{public}x, fd = %{public}d", name_.c_str(), capacity_, flags_, fd_);
90 Close();
91 };
92
93 CHECK_AND_RETURN_RET_LOG(capacity_ > 0, AVCS_ERR_INVALID_VAL, "size is invalid, size = %{public}d", capacity_);
94
95 bool isRemote = false;
96 if (fd_ > 0) {
97 int size = AshmemGetSize(fd_);
98 CHECK_AND_RETURN_RET_LOG(size == capacity_, AVCS_ERR_INVALID_VAL,
99 "size not equal capacity_, size = %{public}d, capacity_ = %{public}d", size, capacity_);
100 isRemote = true;
101 } else {
102 fd_ = AshmemCreate(name_.c_str(), static_cast<size_t>(capacity_));
103 CHECK_AND_RETURN_RET_LOG(fd_ > 0, AVCS_ERR_INVALID_VAL, "fd is invalid, fd = %{public}d", fd_);
104 }
105 if (isMapVirAddr) {
106 int32_t ret = MapMemory(isRemote);
107 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_INVALID_VAL, "MapMemory failed, ret = %{plublic}d", ret);
108 }
109 CANCEL_SCOPE_EXIT_GUARD(0);
110 return AVCS_ERR_OK;
111 }
112
MapMemory(bool isRemote)113 int32_t AVSharedMemoryBase::MapMemory(bool isRemote)
114 {
115 unsigned int prot = PROT_READ | PROT_WRITE;
116 if (isRemote && (flags_ & FLAGS_READ_ONLY)) {
117 prot &= ~PROT_WRITE;
118 }
119
120 int result = AshmemSetProt(fd_, static_cast<int>(prot));
121 CHECK_AND_RETURN_RET_LOG(result >= 0, AVCS_ERR_INVALID_OPERATION,
122 "AshmemSetProt failed, result = %{public}d", result);
123
124 void *addr = ::mmap(nullptr, static_cast<size_t>(capacity_), static_cast<int>(prot), MAP_SHARED, fd_, 0);
125 CHECK_AND_RETURN_RET_LOG(addr != MAP_FAILED, AVCS_ERR_INVALID_OPERATION, "mmap failed, please check params");
126
127 base_ = reinterpret_cast<uint8_t*>(addr);
128 return AVCS_ERR_OK;
129 }
130
Close()131 void AVSharedMemoryBase::Close() noexcept
132 {
133 if (base_ != nullptr) {
134 (void)::munmap(base_, static_cast<size_t>(capacity_));
135 base_ = nullptr;
136 capacity_ = 0;
137 flags_ = 0;
138 size_ = 0;
139 }
140
141 if (fd_ > 0) {
142 (void)::close(fd_);
143 fd_ = -1;
144 }
145 }
146
Write(const uint8_t * in,int32_t writeSize,int32_t position)147 int32_t AVSharedMemoryBase::Write(const uint8_t *in, int32_t writeSize, int32_t position)
148 {
149 CHECK_AND_RETURN_RET_LOG(in != nullptr, 0, "Input buffer is nullptr");
150 CHECK_AND_RETURN_RET_LOG(writeSize > 0, 0, "Input writeSize:%{public}d is invalid", writeSize);
151 int32_t start = 0;
152 if (position == INVALID_POSITION) {
153 start = size_;
154 } else {
155 start = std::min(position, capacity_);
156 }
157 int32_t unusedSize = capacity_ - start;
158 int32_t length = std::min(writeSize, unusedSize);
159 AVCODEC_LOGD("write data,length:%{public}d, start:%{public}d, name:%{public}s", length, start, name_.c_str());
160 CHECK_AND_RETURN_RET_LOG((length + start) <= capacity_, 0, "Write out of bounds, length:%{public}d, "
161 "start:%{public}d, capacity_:%{public}d", length, start, capacity_);
162 uint8_t *dstPtr = base_ + start;
163 CHECK_AND_RETURN_RET_LOG(dstPtr != nullptr, 0, "Inner dstPtr is nullptr");
164
165 auto error = memcpy_s(dstPtr, length, in, length);
166 CHECK_AND_RETURN_RET_LOG(error == EOK, 0, "Inner memcpy_s failed,name:%{public}s, %{public}s",
167 name_.c_str(), strerror(error));
168 size_ = start + length;
169 return length;
170 }
171
Read(uint8_t * out,int32_t readSize,int32_t position)172 int32_t AVSharedMemoryBase::Read(uint8_t *out, int32_t readSize, int32_t position)
173 {
174 CHECK_AND_RETURN_RET_LOG(out != nullptr, 0, "Input buffer is nullptr");
175 CHECK_AND_RETURN_RET_LOG(readSize > 0, 0, "Input readSize:%{public}d is invalid", readSize);
176 int32_t start = 0;
177 int32_t maxLength = size_;
178 if (position != INVALID_POSITION) {
179 start = std::min(position, size_);
180 maxLength = size_ - start;
181 }
182 int32_t length = std::min(readSize, maxLength);
183 CHECK_AND_RETURN_RET_LOG((length + start) <= capacity_, 0, "Read out of bounds, length:%{public}d, "
184 "start:%{public}d, capacity_:%{public}d", length, start, capacity_);
185 uint8_t *srcPtr = base_ + start;
186 CHECK_AND_RETURN_RET_LOG(srcPtr != nullptr, 0, "Inner srcPtr is nullptr");
187 auto error = memcpy_s(out, length, srcPtr, length);
188 CHECK_AND_RETURN_RET_LOG(error == EOK, 0, "Inner memcpy_s failed,name:%{public}s, %{public}s",
189 name_.c_str(), strerror(error));
190 return length;
191 }
192
ClearUsedSize()193 void AVSharedMemoryBase::ClearUsedSize()
194 {
195 size_ = 0;
196 }
197
GetUsedSize() const198 int32_t AVSharedMemoryBase::GetUsedSize() const
199 {
200 return size_;
201 }
202 } // namespace MediaAVCodec
203 } // namespace OHOS
204