1 /*
2 * Copyright (C) 2021 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 "media_errors.h"
21 #include "media_log.h"
22 #include "scope_guard.h"
23
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVSharedMemoryBase"};
26 }
27
28 namespace OHOS {
29 namespace Media {
30 struct AVSharedMemoryBaseImpl : public AVSharedMemoryBase {
31 public:
AVSharedMemoryBaseImplOHOS::Media::AVSharedMemoryBaseImpl32 AVSharedMemoryBaseImpl(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
33 : AVSharedMemoryBase(fd, size, flags, name) {}
34 };
35
CreateFromLocal(int32_t size,uint32_t flags,const std::string & name)36 std::shared_ptr<AVSharedMemory> AVSharedMemoryBase::CreateFromLocal(
37 int32_t size, uint32_t flags, const std::string &name)
38 {
39 std::shared_ptr<AVSharedMemoryBase> memory = std::make_shared<AVSharedMemoryBase>(size, flags, name);
40 int32_t ret = memory->Init();
41 if (ret != MSERR_OK) {
42 MEDIA_LOGE("Create avsharedmemory failed, ret = %{public}d", ret);
43 return nullptr;
44 }
45
46 return memory;
47 }
48
CreateFromRemote(int32_t fd,int32_t size,uint32_t flags,const std::string & name)49 std::shared_ptr<AVSharedMemory> AVSharedMemoryBase::CreateFromRemote(
50 int32_t fd, int32_t size, uint32_t flags, const std::string &name)
51 {
52 std::shared_ptr<AVSharedMemoryBase> memory = std::make_shared<AVSharedMemoryBaseImpl>(fd, size, flags, name);
53 int32_t ret = memory->Init();
54 if (ret != MSERR_OK) {
55 MEDIA_LOGE("Create avsharedmemory failed, ret = %{public}d", ret);
56 return nullptr;
57 }
58
59 return memory;
60 }
61
AVSharedMemoryBase(int32_t size,uint32_t flags,const std::string & name)62 AVSharedMemoryBase::AVSharedMemoryBase(int32_t size, uint32_t flags, const std::string &name)
63 : base_(nullptr), size_(size), flags_(flags), name_(name), fd_(-1)
64 {
65 MEDIA_LOGD("enter ctor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
66 FAKE_POINTER(this), name_.c_str());
67 }
68
AVSharedMemoryBase(int32_t fd,int32_t size,uint32_t flags,const std::string & name)69 AVSharedMemoryBase::AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
70 : base_(nullptr), size_(size), flags_(flags), name_(name), fd_(dup(fd))
71 {
72 MEDIA_LOGD("enter ctor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
73 FAKE_POINTER(this), name_.c_str());
74 }
75
~AVSharedMemoryBase()76 AVSharedMemoryBase::~AVSharedMemoryBase()
77 {
78 MEDIA_LOGD("enter dtor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
79 FAKE_POINTER(this), name_.c_str());
80 Close();
81 }
82
Init()83 int32_t AVSharedMemoryBase::Init()
84 {
85 ON_SCOPE_EXIT(0) {
86 MEDIA_LOGE("create avsharedmemory failed, name = %{public}s, size = %{public}d, "
87 "flags = 0x%{public}x, fd = %{public}d",
88 name_.c_str(), size_, flags_, fd_);
89 Close();
90 };
91
92 CHECK_AND_RETURN_RET(size_ > 0, MSERR_INVALID_VAL);
93
94 bool isRemote = false;
95 if (fd_ > 0) {
96 int size = AshmemGetSize(fd_);
97 CHECK_AND_RETURN_RET(size == size_, MSERR_INVALID_VAL);
98 isRemote = true;
99 } else {
100 fd_ = AshmemCreate(name_.c_str(), static_cast<size_t>(size_));
101 CHECK_AND_RETURN_RET(fd_ > 0, MSERR_INVALID_VAL);
102 }
103
104 int32_t ret = MapMemory(isRemote);
105 CHECK_AND_RETURN_RET(ret == MSERR_OK, MSERR_INVALID_VAL);
106
107 CANCEL_SCOPE_EXIT_GUARD(0);
108 return MSERR_OK;
109 }
110
MapMemory(bool isRemote)111 int32_t AVSharedMemoryBase::MapMemory(bool isRemote)
112 {
113 unsigned int prot = PROT_READ | PROT_WRITE;
114 if (isRemote && (flags_ & FLAGS_READ_ONLY)) {
115 prot &= ~PROT_WRITE;
116 }
117
118 int result = AshmemSetProt(fd_, static_cast<int>(prot));
119 CHECK_AND_RETURN_RET(result >= 0, MSERR_INVALID_OPERATION);
120
121 void *addr = ::mmap(nullptr, static_cast<size_t>(size_), static_cast<int>(prot), MAP_SHARED, fd_, 0);
122 CHECK_AND_RETURN_RET(addr != MAP_FAILED, MSERR_INVALID_OPERATION);
123
124 base_ = reinterpret_cast<uint8_t*>(addr);
125 return MSERR_OK;
126 }
127
Close()128 void AVSharedMemoryBase::Close() noexcept
129 {
130 if (base_ != nullptr) {
131 (void)::munmap(base_, static_cast<size_t>(size_));
132 base_ = nullptr;
133 size_ = 0;
134 flags_ = 0;
135 }
136
137 if (fd_ > 0) {
138 (void)::close(fd_);
139 fd_ = -1;
140 }
141 }
142 } // namespace Media
143 } // namespace OHOS
144