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 "avsharedmemory_ipc.h"
17 #include <unistd.h>
18 #include "avsharedmemorybase.h"
19 #include "media_errors.h"
20 #include "media_log.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVSharedMemoryIPC"};
24 }
25
26 namespace OHOS {
27 namespace Media {
WriteAVSharedMemoryToParcel(const std::shared_ptr<AVSharedMemory> & memory,MessageParcel & parcel)28 int32_t WriteAVSharedMemoryToParcel(const std::shared_ptr<AVSharedMemory> &memory, MessageParcel &parcel)
29 {
30 std::shared_ptr<AVSharedMemoryBase> baseMem = std::static_pointer_cast<AVSharedMemoryBase>(memory);
31 if (baseMem == nullptr) {
32 MEDIA_LOGE("invalid pointer");
33 return MSERR_INVALID_VAL;
34 }
35
36 int32_t fd = baseMem->GetFd();
37 int32_t size = baseMem->GetSize();
38
39 (void)parcel.WriteFileDescriptor(fd);
40 parcel.WriteInt32(size);
41 parcel.WriteUint32(baseMem->GetFlags());
42 parcel.WriteString(baseMem->GetName());
43
44 return MSERR_OK;
45 }
46
ReadAVSharedMemoryFromParcel(MessageParcel & parcel)47 std::shared_ptr<AVSharedMemory> ReadAVSharedMemoryFromParcel(MessageParcel &parcel)
48 {
49 int32_t fd = parcel.ReadFileDescriptor();
50 int32_t size = parcel.ReadInt32();
51 uint32_t flags = parcel.ReadUint32();
52 std::string name = parcel.ReadString();
53
54 std::shared_ptr<AVSharedMemory> memory = AVSharedMemoryBase::CreateFromRemote(fd, size, flags, name);
55 if (memory == nullptr || memory->GetBase() == nullptr) {
56 MEDIA_LOGE("create remote AVSharedMemoryBase failed");
57 memory = nullptr;
58 }
59
60 (void)::close(fd);
61 return memory;
62 }
63 } // namespace Media
64 } // namespace OHOS
65