• 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 "avsharedmemorylocal.h"
17 #include "media_errors.h"
18 #include "media_log.h"
19 
20 namespace {
21     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVSharedMemoryLocal"};
22     constexpr uint32_t MAX_ALLOWED_SIZE = 30 * 1024 * 1024;
23 }
24 
25 namespace OHOS {
26 namespace Media {
Create(int32_t size,uint32_t flags,const std::string & name)27 std::shared_ptr<AVSharedMemory> AVSharedMemory::Create(int32_t size, uint32_t flags, const std::string &name)
28 {
29     std::shared_ptr<AVSharedMemoryLocal> memory = std::make_shared<AVSharedMemoryLocal>(size, flags, name);
30     int32_t ret = memory->Init();
31     if (ret != MSERR_OK) {
32         MEDIA_LOGE("Create avsharedmemory failed, ret = %{public}d", ret);
33         return nullptr;
34     }
35 
36     return memory;
37 }
38 
AVSharedMemoryLocal(int32_t size,uint32_t flags,const std::string & name)39 AVSharedMemoryLocal::AVSharedMemoryLocal(int32_t size, uint32_t flags, const std::string &name)
40     : base_(nullptr), size_(size), flags_(flags), name_(name)
41 {
42     MEDIA_LOGD("enter ctor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
43                FAKE_POINTER(this), name_.c_str());
44 }
45 
~AVSharedMemoryLocal()46 AVSharedMemoryLocal::~AVSharedMemoryLocal()
47 {
48     MEDIA_LOGD("enter dtor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
49                FAKE_POINTER(this), name_.c_str());
50     if (base_ != nullptr) {
51         delete [] base_;
52         base_ = nullptr;
53     }
54 }
55 
Init()56 int32_t AVSharedMemoryLocal::Init()
57 {
58     if (size_ > MAX_ALLOWED_SIZE) {
59         MEDIA_LOGE("create avsharedmemory failed, size %{public}d is too large, name = %{public}s",
60                    size_, name_.c_str());
61         return MSERR_INVALID_VAL;
62     }
63     base_ = new (std::nothrow) uint8_t[size_];
64     if (base_ == nullptr) {
65         MEDIA_LOGE("create avsharedmemory failed, new failed, size = %{public}d , name = %{public}s",
66                    size_, name_.c_str());
67         return MSERR_INVALID_OPERATION;
68     }
69     return MSERR_OK;
70 }
71 
GetBase()72 uint8_t *AVSharedMemoryLocal::GetBase()
73 {
74     return base_;
75 }
76 
GetSize()77 int32_t AVSharedMemoryLocal::GetSize()
78 {
79     return size_;
80 }
81 
GetFlags()82 uint32_t AVSharedMemoryLocal::GetFlags()
83 {
84     return flags_;
85 }
86 } // namespace Media
87 } // namespace OHOS