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 "cj_media_data_source_callback.h"
17
18 #include "media_errors.h"
19 #include "media_log.h"
20 #include "cj_lambda.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "CJMediaDataSourceCallback"};
24 }
25
26 namespace OHOS {
27 namespace Media {
CJMediaDataSourceCallback(int64_t fileSize)28 CJMediaDataSourceCallback::CJMediaDataSourceCallback(int64_t fileSize) : size_(fileSize)
29 {
30 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
31 }
32
~CJMediaDataSourceCallback()33 CJMediaDataSourceCallback::~CJMediaDataSourceCallback()
34 {
35 MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
36 }
37
ReadAt(const std::shared_ptr<AVSharedMemory> & mem,uint32_t length,int64_t pos)38 int32_t CJMediaDataSourceCallback::ReadAt(const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos)
39 {
40 if (cb_ == nullptr || mem == nullptr) {
41 return SOURCE_ERROR_IO;
42 }
43 int32_t size = mem->GetSize();
44 if (size <= 0) {
45 return SOURCE_ERROR_IO;
46 }
47 uint8_t *raw = mem->GetBase();
48 if (raw == nullptr) {
49 return SOURCE_ERROR_IO;
50 }
51 CArrUI8 buffer = CArrUI8{.head = raw, .size = size};
52 auto res = cb_(buffer, length, pos);
53 return res;
54 }
55
ReadAt(int64_t pos,uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)56 int32_t CJMediaDataSourceCallback::ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
57 {
58 (void)pos;
59 (void)length;
60 (void)mem;
61 return MSERR_OK;
62 }
63
ReadAt(uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)64 int32_t CJMediaDataSourceCallback::ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
65 {
66 (void)length;
67 (void)mem;
68 return MSERR_OK;
69 }
70
GetSize(int64_t & size)71 int32_t CJMediaDataSourceCallback::GetSize(int64_t &size)
72 {
73 size = size_;
74 return MSERR_OK;
75 }
76
GetCallbackId()77 int64_t CJMediaDataSourceCallback::GetCallbackId()
78 {
79 return callbackId_;
80 }
81
SetCallback(int64_t callbackId)82 void CJMediaDataSourceCallback::SetCallback(int64_t callbackId)
83 {
84 auto cFunc = reinterpret_cast<int32_t (*)(const CArrUI8 mem, uint32_t length, int64_t pos)>(callbackId);
85 cb_ = [lambda = CJLambda::Create(cFunc)](const CArrUI8 mem, uint32_t length, int64_t pos) -> int32_t {
86 auto res = lambda(mem, length, pos);
87 return res;
88 };
89 callbackId_ = callbackId;
90 }
91
ClearCallbackReference()92 void CJMediaDataSourceCallback::ClearCallbackReference()
93 {
94 cb_ = nullptr;
95 callbackId_ = -1;
96 MEDIA_LOGD("callback has been clear");
97 }
98 } // namespace Media
99 } // namespace OHOS
100