• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "Create avsharedmemory failed, ret = %{public}d", ret);
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     CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "Create avsharedmemory failed, ret = %{public}d", ret);
52 
53     return memory;
54 }
55 
AVSharedMemoryBase(int32_t size,uint32_t flags,const std::string & name)56 AVSharedMemoryBase::AVSharedMemoryBase(int32_t size, uint32_t flags, const std::string &name)
57     : base_(nullptr), size_(size), flags_(flags), name_(name), fd_(-1)
58 {
59     MEDIA_LOGD("enter ctor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
60                FAKE_POINTER(this), name_.c_str());
61 }
62 
AVSharedMemoryBase(int32_t fd,int32_t size,uint32_t flags,const std::string & name)63 AVSharedMemoryBase::AVSharedMemoryBase(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
64     : base_(nullptr), size_(size), flags_(flags), name_(name), fd_(dup(fd))
65 {
66     MEDIA_LOGD("enter ctor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
67                FAKE_POINTER(this), name_.c_str());
68 }
69 
~AVSharedMemoryBase()70 AVSharedMemoryBase::~AVSharedMemoryBase()
71 {
72     MEDIA_LOGD("enter dtor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
73                FAKE_POINTER(this), name_.c_str());
74     Close();
75 }
76 
Init()77 int32_t AVSharedMemoryBase::Init()
78 {
79     ON_SCOPE_EXIT(0) {
80         MEDIA_LOGE("create avsharedmemory failed, name = %{public}s, size = %{public}d, "
81                    "flags = 0x%{public}x, fd = %{public}d",
82                    name_.c_str(), size_, flags_, fd_);
83         Close();
84     };
85 
86     CHECK_AND_RETURN_RET(size_ > 0, MSERR_INVALID_VAL);
87 
88     bool isRemote = false;
89     if (fd_ > 0) {
90         int size = AshmemGetSize(fd_);
91         CHECK_AND_RETURN_RET(size == size_, MSERR_INVALID_VAL);
92         isRemote = true;
93     } else {
94         fd_ = AshmemCreate(name_.c_str(), static_cast<size_t>(size_));
95         CHECK_AND_RETURN_RET(fd_ > 0, MSERR_INVALID_VAL);
96     }
97 
98     int32_t ret = MapMemory(isRemote);
99     CHECK_AND_RETURN_RET(ret == MSERR_OK, MSERR_INVALID_VAL);
100 
101     CANCEL_SCOPE_EXIT_GUARD(0);
102     return MSERR_OK;
103 }
104 
MapMemory(bool isRemote)105 int32_t AVSharedMemoryBase::MapMemory(bool isRemote)
106 {
107     unsigned int prot = PROT_READ | PROT_WRITE;
108     if (isRemote && (flags_ & FLAGS_READ_ONLY)) {
109         prot &= ~PROT_WRITE;
110     }
111 
112     int result = AshmemSetProt(fd_, static_cast<int>(prot));
113     CHECK_AND_RETURN_RET(result >= 0, MSERR_INVALID_OPERATION);
114 
115     void *addr = ::mmap(nullptr, static_cast<size_t>(size_), static_cast<int>(prot), MAP_SHARED, fd_, 0);
116     CHECK_AND_RETURN_RET(addr != MAP_FAILED, MSERR_INVALID_OPERATION);
117 
118     base_ = reinterpret_cast<uint8_t*>(addr);
119     return MSERR_OK;
120 }
121 
Close()122 void AVSharedMemoryBase::Close() noexcept
123 {
124     if (base_ != nullptr) {
125         (void)::munmap(base_, static_cast<size_t>(size_));
126         base_ = nullptr;
127         size_ = 0;
128         flags_ = 0;
129     }
130 
131     if (fd_ > 0) {
132         (void)::close(fd_);
133         fd_ = -1;
134     }
135 }
136 } // namespace Media
137 } // namespace OHOS
138