• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_helperdatasourcecallback.h"
17 #include "cj_lambda.h"
18 #include "media_errors.h"
19 #include "media_log.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_METADATA, "CJHelperDataSourceCallback"};
23 }
24 
25 namespace OHOS {
26 namespace Media {
27 const std::string HELPER_READAT_CALLBACK_NAME = "readAt";
28 
~HelperDataSourceCJCallback()29 HelperDataSourceCJCallback::~HelperDataSourceCJCallback()
30 {
31     isExit_ = true;
32     cond_.notify_all();
33     memory_ = nullptr;
34 }
35 
WaitResult()36 void HelperDataSourceCJCallback::WaitResult()
37 {
38     std::unique_lock<std::mutex> lock(mutexCond_);
39     if (!setResult_) {
40         static constexpr int32_t timeout = 100;
41         cond_.wait_for(lock, std::chrono::milliseconds(timeout), [this]() { return setResult_ || isExit_; });
42         if (!setResult_) {
43             readSize_ = 0;
44             if (isExit_) {
45                 MEDIA_LOGW("Reset, ReadAt has been cancel!");
46             } else {
47                 MEDIA_LOGW("timeout 100ms!");
48             }
49         }
50     }
51     setResult_ = false;
52 }
53 
CJHelperDataSourceCallback(int64_t fileSize)54 CJHelperDataSourceCallback::CJHelperDataSourceCallback(int64_t fileSize) : size_(fileSize)
55 {
56     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
57 }
58 
~CJHelperDataSourceCallback()59 CJHelperDataSourceCallback::~CJHelperDataSourceCallback()
60 {
61     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
62 }
63 
ReadAt(const std::shared_ptr<AVSharedMemory> & mem,uint32_t length,int64_t pos)64 int32_t CJHelperDataSourceCallback::ReadAt(
65     const std::shared_ptr<AVSharedMemory> &mem, uint32_t length, int64_t pos)
66 {
67     int32_t ret = 0;
68     MEDIA_LOGD("ReadAt in");
69     {
70         std::lock_guard<std::mutex> lock(mutex_);
71         if (funcMap_.find(HELPER_READAT_CALLBACK_NAME) == funcMap_.end()) {
72             return SOURCE_ERROR_IO;
73         }
74         cb_ = std::make_shared<HelperDataSourceCJCallback>(HELPER_READAT_CALLBACK_NAME, mem, length, pos);
75         CHECK_AND_RETURN_RET_LOG(cb_ != nullptr, 0, "Failed to Create HelperDataSourceCJCallback");
76         cb_->callback_ = funcMap_.at(HELPER_READAT_CALLBACK_NAME).second;
77     }
78     do {
79         MEDIA_LOGD("length is %{public}u", length);
80         auto arraybuffer = mem->GetBase();
81         if (arraybuffer == nullptr) {
82             MEDIA_LOGE("Failed to get arraybuffer.");
83         }
84         ret = cb_->callback_(arraybuffer, length, pos);
85         std::unique_lock<std::mutex> lock(cb_->mutexCond_);
86         cb_->setResult_ = true;
87         cb_->cond_.notify_all();
88     } while (0);
89     cb_->WaitResult();
90     return ret;
91 }
92 
GetSize(int64_t & size)93 int32_t CJHelperDataSourceCallback::GetSize(int64_t &size)
94 {
95     size = size_;
96     return MSERR_OK;
97 }
98 
GetCallbackId(int64_t & id)99 int32_t CJHelperDataSourceCallback::GetCallbackId(int64_t &id)
100 {
101     return MSERR_OK;
102 }
103 
ReadAt(int64_t pos,uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)104 int32_t CJHelperDataSourceCallback::ReadAt(int64_t pos, uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
105 {
106     (void)pos;
107     (void)length;
108     (void)mem;
109     return MSERR_OK;
110 }
111 
ReadAt(uint32_t length,const std::shared_ptr<AVSharedMemory> & mem)112 int32_t CJHelperDataSourceCallback::ReadAt(uint32_t length, const std::shared_ptr<AVSharedMemory> &mem)
113 {
114     (void)length;
115     (void)mem;
116     return MSERR_OK;
117 }
118 
SaveCallbackReference(const std::string & name,int64_t id)119 int32_t CJHelperDataSourceCallback::SaveCallbackReference(const std::string &name, int64_t id)
120 {
121     std::lock_guard<std::mutex> lock(mutex_);
122     funcMap_[name].first = id;
123     auto func = reinterpret_cast<int32_t(*)(uint8_t*, uint32_t, int64_t)>(id);
124     funcMap_[name].second = [lambda = CJLambda::Create(func)](uint8_t* mem, uint32_t length, int64_t pos) -> int32_t {
125         auto res = lambda(mem, length, pos);
126         return res;
127     };
128     if (funcMap_[name].second == nullptr) {
129         return MSERR_INVALID_VAL;
130     }
131     return MSERR_OK;
132 }
133 
ClearCallbackReference()134 void CJHelperDataSourceCallback::ClearCallbackReference()
135 {
136     std::lock_guard<std::mutex> lock(mutex_);
137     std::map<std::string, std::pair<int64_t, std::function<int32_t(uint8_t *, uint32_t, int64_t)>>> temp;
138     temp.swap(funcMap_);
139     MEDIA_LOGD("callback has been clear");
140 }
141 
142 } // namespace Media
143 } // namespace OHOS
144