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