1 /*
2 * Copyright (C) 2025 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
17 #include <sys/mman.h>
18 #include <unistd.h>
19 #include "ashmem.h"
20 #include "avsession_errors.h"
21 #include "avsession_log.h"
22 #include "securec.h"
23 #include "av_shared_memory_base.h"
24
25 namespace {
26 static std::atomic<uint64_t> g_uniqueSharedMemoryID = 0;
27 }
28
29 namespace OHOS {
30 namespace AVSession {
31 struct AVSharedMemoryBaseImpl : public AVSharedMemoryBase {
32 public:
AVSharedMemoryBaseImplOHOS::AVSession::AVSharedMemoryBaseImpl33 AVSharedMemoryBaseImpl(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
34 : AVSharedMemoryBase(fd, size, flags, name) {}
35 };
36
CreateFromLocal(int32_t size,uint32_t flags,const std::string & name)37 std::shared_ptr<AVSharedMemory> AVSharedMemoryBase::CreateFromLocal(
38 int32_t size, uint32_t flags, const std::string &name)
39 {
40 std::shared_ptr<AVSharedMemoryBase> memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
41 int32_t ret = memory->Init();
42 if (ret != static_cast<int32_t>(AVSESSION_SUCCESS)) {
43 SLOGE("Create avsharedmemory failed, ret = %{public}d", ret);
44 return nullptr;
45 }
46
47 return memory;
48 }
49
CreateFromRemote(int32_t fd,int32_t size,uint32_t flags,const std::string & name)50 std::shared_ptr<AVSharedMemory> AVSharedMemoryBase::CreateFromRemote(
51 int32_t fd, int32_t size, uint32_t flags, const std::string &name)
52 {
53 std::shared_ptr<AVSharedMemoryBase> memory = std::make_shared<AVSharedMemoryBaseImpl>(fd, size, flags, name);
54 int32_t ret = memory->Init();
55 if (ret != static_cast<int32_t>(AVSESSION_SUCCESS)) {
56 SLOGE("Create avsharedmemory failed, ret = %{public}d", ret);
57 return nullptr;
58 }
59
60 return memory;
61 }
62
AVSharedMemoryBase(int32_t size,uint32_t flags,const std::string & name)63 AVSharedMemoryBase::AVSharedMemoryBase(int32_t size, uint32_t flags, const std::string &name)
64 : base_(nullptr), capacity_(size), flags_(flags), name_(name), fd_(-1), size_(0)
65 {
66 SLOGD("AVSharedMemoryBase size in name = %{public}s", name_.c_str());
67 uniqueSharedMemoryID_ = g_uniqueSharedMemoryID++;
68 }
69
AVSharedMemoryBase(int32_t fd,int32_t size,uint32_t flags,const std::string & name)70 AVSharedMemoryBase::AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
71 : base_(nullptr), capacity_(size), flags_(flags), name_(name), fd_(dup(fd)), size_(0)
72 {
73 SLOGD("AVSharedMemoryBase fd in, name = %{public}s", name_.c_str());
74 uniqueSharedMemoryID_ = g_uniqueSharedMemoryID++;
75 }
76
~AVSharedMemoryBase()77 AVSharedMemoryBase::~AVSharedMemoryBase()
78 {
79 SLOGD("~AVSharedMemoryBase, name = %{public}s", name_.c_str());
80 Close();
81 }
82
Init(bool isMapVirAddr)83 int32_t AVSharedMemoryBase::Init(bool isMapVirAddr)
84 {
85 if (capacity_ <= 0) {
86 SLOGE("size is invalid, size = %{public}d", capacity_);
87 return static_cast<int32_t>(ERR_INVALID_PARAM);
88 }
89
90 bool isRemote = false;
91 if (fd_ > 0) {
92 int size = AshmemGetSize(fd_);
93 if (size != capacity_) {
94 SLOGE("size not equal capacity_, size = %{public}d, capacity_ = %{public}d", size, capacity_);
95 return static_cast<int32_t>(ERR_INVALID_PARAM);
96 }
97 isRemote = true;
98 } else {
99 fd_ = AshmemCreate(name_.c_str(), static_cast<size_t>(capacity_));
100 if (fd_ <= 0) {
101 SLOGE("fd is invalid, fd = %{public}d", fd_);
102 return static_cast<int32_t>(ERR_INVALID_PARAM);
103 }
104 }
105 if (isMapVirAddr) {
106 int32_t ret = MapMemory(isRemote);
107 if (ret != static_cast<int32_t>(AVSESSION_SUCCESS)) {
108 SLOGE("MapMemory failed, ret = %{public}d", ret);
109 return static_cast<int32_t>(ERR_INVALID_PARAM);
110 }
111 }
112 return static_cast<int32_t>(AVSESSION_SUCCESS);
113 }
114
MapMemory(bool isRemote)115 int32_t AVSharedMemoryBase::MapMemory(bool isRemote)
116 {
117 unsigned int prot = PROT_READ | PROT_WRITE;
118 if (isRemote && (flags_ & FLAGS_READ_ONLY)) {
119 prot &= ~PROT_WRITE;
120 }
121
122 int result = AshmemSetProt(fd_, static_cast<int>(prot));
123 if (result < 0) {
124 SLOGE("AshmemSetProt failed, result = %{public}d", result);
125 return static_cast<int32_t>(AVSESSION_ERROR);
126 }
127
128 void *addr = ::mmap(nullptr, static_cast<size_t>(capacity_), static_cast<int>(prot), MAP_SHARED, fd_, 0);
129 if (addr == MAP_FAILED) {
130 SLOGE("mmap failed, please check params");
131 return static_cast<int32_t>(AVSESSION_ERROR);
132 }
133
134 base_ = reinterpret_cast<uint8_t*>(addr);
135 return static_cast<int32_t>(AVSESSION_SUCCESS);
136 }
137
Close()138 void AVSharedMemoryBase::Close() noexcept
139 {
140 if (base_ != nullptr) {
141 (void)::munmap(base_, static_cast<size_t>(capacity_));
142 base_ = nullptr;
143 capacity_ = 0;
144 flags_ = 0;
145 size_ = 0;
146 }
147 if (fd_ > 0) {
148 (void)::close(fd_);
149 fd_ = -1;
150 }
151 }
152
Write(const uint8_t * in,int32_t writeSize,int32_t position)153 int32_t AVSharedMemoryBase::Write(const uint8_t *in, int32_t writeSize, int32_t position)
154 {
155 if (in == nullptr) {
156 SLOGE("Input buffer is nullptr");
157 return 0;
158 }
159 if (writeSize <= 0) {
160 SLOGE("Input writeSize:%{public}d is invalid", writeSize);
161 return 0;
162 }
163 int32_t start = 0;
164 if (position == INVALID_POSITION) {
165 start = size_;
166 } else {
167 start = std::min(position, capacity_);
168 }
169 int32_t unusedSize = capacity_ - start;
170 int32_t length = std::min(writeSize, unusedSize);
171 SLOGD("write data,length:%{public}d, start:%{public}d, name:%{public}s", length, start, name_.c_str());
172 if ((length + start) > capacity_) {
173 SLOGE("Write out of bounds, length:%{public}d, "
174 "start:%{public}d, capacity_:%{public}d", length, start, capacity_);
175 return 0;
176 }
177 uint8_t *dstPtr = base_ + start;
178 if (dstPtr == nullptr) {
179 SLOGE("Inner dstPtr is nullptr");
180 return 0;
181 }
182 auto error = memcpy_s(dstPtr, length, in, length);
183 if (error != EOK) {
184 SLOGE("Inner memcpy_s failed,name:%{public}s, %{public}s", name_.c_str(), strerror(error));
185 return 0;
186 }
187 size_ = start + length;
188 return length;
189 }
190
Read(uint8_t * out,int32_t readSize,int32_t position)191 int32_t AVSharedMemoryBase::Read(uint8_t *out, int32_t readSize, int32_t position)
192 {
193 if (out == nullptr) {
194 SLOGE("Input buffer is nullptr");
195 return 0;
196 }
197 if (readSize <= 0) {
198 SLOGE("Input readSize:%{public}d is invalid", readSize);
199 return 0;
200 }
201 int32_t start = 0;
202 int32_t maxLength = size_;
203 if (position != INVALID_POSITION) {
204 start = std::min(position, size_);
205 maxLength = size_ - start;
206 }
207 int32_t length = std::min(readSize, maxLength);
208 if ((length + start) > capacity_) {
209 SLOGE("Read out of bounds, length:%{public}d, "
210 "start:%{public}d, capacity_:%{public}d", length, start, capacity_);
211 return 0;
212 }
213 uint8_t *srcPtr = base_ + start;
214 if (srcPtr == nullptr) {
215 SLOGE("Inner srcPtr is nullptr");
216 return 0;
217 }
218 auto error = memcpy_s(out, length, srcPtr, length);
219 if (error != EOK) {
220 SLOGE("Inner memcpy_s failed,name:%{public}s, %{public}s", name_.c_str(), strerror(error));
221 return 0;
222 }
223 return length;
224 }
225
ClearUsedSize()226 void AVSharedMemoryBase::ClearUsedSize()
227 {
228 size_ = 0;
229 }
230
GetUsedSize() const231 int32_t AVSharedMemoryBase::GetUsedSize() const
232 {
233 return size_;
234 }
235 } // namespace AVSession
236 } // namespace OHOS