1/*
2 * Copyright (C) 2023 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 "avdatasrcmemory.h"
17#include "media_errors.h"
18#include "media_log.h"
19
20namespace {
21    constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "AVDataSrcMemory"};
22}
23
24namespace OHOS {
25namespace Media {
26struct AVSharedMemoryBaseImpl : public AVDataSrcMemory {
27public:
28    AVSharedMemoryBaseImpl(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
29        : AVDataSrcMemory(fd, size, flags, name) {}
30};
31
32std::shared_ptr<AVSharedMemory> AVDataSrcMemory::CreateFromLocal(
33    int32_t size, uint32_t flags, const std::string &name)
34{
35    std::shared_ptr<AVDataSrcMemory> memory = std::make_shared<AVDataSrcMemory>(size, flags, name);
36    int32_t ret = memory->Init();
37    CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, nullptr, "Create avsharedmemory failed, ret = %{public}d", ret);
38
39    return memory;
40}
41
42std::shared_ptr<AVSharedMemory> AVDataSrcMemory::CreateFromRemote(
43    int32_t fd, int32_t size, uint32_t flags, const std::string &name)
44{
45    std::shared_ptr<AVDataSrcMemory> memory = std::make_shared<AVSharedMemoryBaseImpl>(fd, size, flags, name);
46    int32_t ret = memory->Init();
47    if (ret != MSERR_OK) {
48        MEDIA_LOGE("Create avsharedmemory failed, ret = %{public}d", ret);
49        return nullptr;
50    }
51
52    return memory;
53}
54
55AVDataSrcMemory::AVDataSrcMemory(int32_t size, uint32_t flags, const std::string &name)
56    : AVSharedMemoryBase(size, flags, name)
57{
58    MEDIA_LOGD("enter ctor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
59               FAKE_POINTER(this), name.c_str());
60    offset_ = 0;
61}
62
63AVDataSrcMemory::AVDataSrcMemory(int32_t fd, int32_t size, uint32_t flags, const std::string &name)
64    : AVSharedMemoryBase(fd, size, flags, name)
65{
66    MEDIA_LOGD("enter ctor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
67               FAKE_POINTER(this), name.c_str());
68    offset_ = 0;
69}
70
71AVDataSrcMemory::~AVDataSrcMemory()
72{
73    MEDIA_LOGD("enter dtor, instance: 0x%{public}06" PRIXPTR ", name = %{public}s",
74               FAKE_POINTER(this), GetName().c_str());
75}
76} // namespace Media
77} // namespace OHOS
78