1 /*
2 * Copyright (C) 2025 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 "buffer/avsharedmemorybase.h"
19 #include "avdatasrcmemory.h"
20 #include "media_errors.h"
21 #include "media_log.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "AVSharedMemoryIPC"};
25 }
26
27 namespace OHOS {
28 namespace Media {
WriteAVSharedMemoryToParcel(const std::shared_ptr<AVSharedMemory> & memory,MessageParcel & parcel)29 int32_t WriteAVSharedMemoryToParcel(const std::shared_ptr<AVSharedMemory> &memory, MessageParcel &parcel)
30 {
31 std::shared_ptr<AVSharedMemoryBase> baseMem = std::static_pointer_cast<AVSharedMemoryBase>(memory);
32 CHECK_AND_RETURN_RET_LOG(baseMem != nullptr, MSERR_INVALID_VAL, "memory is nullptr");
33
34 int32_t fd = baseMem->GetFd();
35 CHECK_AND_RETURN_RET_LOG(fd >= 0, MSERR_INVALID_VAL, "write fd is invalid, fd = %{public}d", fd);
36
37 int32_t size = baseMem->GetSize();
38
39 bool res = parcel.WriteFileDescriptor(fd);
40 CHECK_AND_RETURN_RET_LOG(res, MSERR_UNKNOWN, "write file descriptor failed, fd = %{public}d", fd);
41
42 parcel.WriteInt32(size);
43 parcel.WriteUint32(baseMem->GetFlags());
44 parcel.WriteString(baseMem->GetName());
45
46 return MSERR_OK;
47 }
48
ReadAVSharedMemoryFromParcel(MessageParcel & parcel)49 std::shared_ptr<AVSharedMemory> ReadAVSharedMemoryFromParcel(MessageParcel &parcel)
50 {
51 int32_t fd = parcel.ReadFileDescriptor();
52 CHECK_AND_RETURN_RET_LOG(fd >= 0, nullptr, "read fd is invalid, fd = %{public}d", fd);
53
54 int32_t size = parcel.ReadInt32();
55 uint32_t flags = parcel.ReadUint32();
56 std::string name = parcel.ReadString();
57
58 std::shared_ptr<AVSharedMemory> memory = AVSharedMemoryBase::CreateFromRemote(fd, size, flags, name);
59 CHECK_AND_RETURN_RET_LOG(memory != nullptr && memory->GetBase() != nullptr, nullptr,
60 "create remote AVSharedMemoryBase failed");
61
62 (void)::close(fd);
63 return memory;
64 }
65
ReadAVDataSrcMemoryFromParcel(MessageParcel & parcel)66 std::shared_ptr<AVSharedMemory> ReadAVDataSrcMemoryFromParcel(MessageParcel &parcel)
67 {
68 int32_t fd = parcel.ReadFileDescriptor();
69 CHECK_AND_RETURN_RET_LOG(fd >= 0, nullptr, "read fd is invalid, fd = %{public}d", fd);
70
71 int32_t size = parcel.ReadInt32();
72 uint32_t flags = parcel.ReadUint32();
73 std::string name = parcel.ReadString();
74
75 std::shared_ptr<AVSharedMemory> memory = AVDataSrcMemory::CreateFromRemote(fd, size, flags, name);
76 CHECK_AND_RETURN_RET_LOG(memory != nullptr && memory->GetBase() != nullptr, nullptr,
77 "create remote AVDataSrcMemory failed");
78
79 (void)::close(fd);
80 return memory;
81 }
82 } // namespace Media
83 } // namespace OHOS
84