1 /*
2 * Copyright (c) 2024 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 #define HST_LOG_TAG "CallbackLooper"
17
18 #include "hitranscoder_callback_looper.h"
19 #include <utility>
20 #include "common/log.h"
21 #include "osal/task/autolock.h"
22 #include "osal/utils/steady_clock.h"
23
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "HiTranscoderCallbackLooper" };
26 }
27
28 namespace OHOS {
29 namespace Media {
30 namespace {
31 constexpr int32_t WHAT_NONE = 0;
32 constexpr int32_t WHAT_MEDIA_PROGRESS = 1;
33 constexpr int32_t WHAT_INFO = 2;
34 constexpr int32_t WHAT_ERROR = 3;
35
36 constexpr int32_t TUPLE_POS_0 = 0;
37 constexpr int32_t TUPLE_POS_1 = 1;
38 }
HiTransCoderCallbackLooper()39 HiTransCoderCallbackLooper::HiTransCoderCallbackLooper()
40 {
41 }
42
~HiTransCoderCallbackLooper()43 HiTransCoderCallbackLooper::~HiTransCoderCallbackLooper()
44 {
45 Stop();
46 }
47
IsStarted()48 bool HiTransCoderCallbackLooper::IsStarted()
49 {
50 return taskStarted_;
51 }
52
Stop()53 void HiTransCoderCallbackLooper::Stop()
54 {
55 FALSE_RETURN(taskStarted_);
56 task_->Stop();
57 taskStarted_ = false;
58 }
59
StartWithTransCoderEngineObs(const std::weak_ptr<ITransCoderEngineObs> & obs)60 void HiTransCoderCallbackLooper::StartWithTransCoderEngineObs(const std::weak_ptr<ITransCoderEngineObs>& obs)
61 {
62 MEDIA_LOG_I("StartWithTransCoderEngineObs");
63 OHOS::Media::AutoLock lock(loopMutex_);
64 obs_ = obs;
65 FALSE_RETURN(!taskStarted_);
66 task_->Start();
67 taskStarted_ = true;
68 MEDIA_LOG_I("start callback looper");
69 }
SetTransCoderEngine(ITransCoderEngine * engine,std::string transCoderId)70 void HiTransCoderCallbackLooper::SetTransCoderEngine(ITransCoderEngine* engine, std::string transCoderId)
71 {
72 OHOS::Media::AutoLock lock(loopMutex_);
73 transCoderEngine_ = engine;
74 task_ = std::make_unique<Task>("callbackThread", transCoderId, TaskType::GLOBAL, TaskPriority::NORMAL, false);
75 }
76
StartReportMediaProgress(int64_t updateIntervalMs)77 void HiTransCoderCallbackLooper::StartReportMediaProgress(int64_t updateIntervalMs)
78 {
79 MEDIA_LOG_I("StartReportMediaProgress");
80 reportProgressIntervalMs_ = updateIntervalMs;
81 FALSE_RETURN(!reportMediaProgress_);
82 reportMediaProgress_ = true;
83 Enqueue(std::make_shared<Event>(WHAT_MEDIA_PROGRESS, SteadyClock::GetCurrentTimeMs(), Any()));
84 }
85
ManualReportMediaProgressOnce()86 void HiTransCoderCallbackLooper::ManualReportMediaProgressOnce()
87 {
88 Enqueue(std::make_shared<Event>(WHAT_MEDIA_PROGRESS, SteadyClock::GetCurrentTimeMs(), Any()));
89 }
90
StopReportMediaProgress()91 void HiTransCoderCallbackLooper::StopReportMediaProgress()
92 {
93 MEDIA_LOG_I("StopReportMediaProgress");
94 OHOS::Media::AutoLock lock(loopMutex_);
95 reportMediaProgress_ = false;
96 }
97
DoReportCompletedTime()98 void HiTransCoderCallbackLooper::DoReportCompletedTime()
99 {
100 OHOS::Media::AutoLock lock(loopMutex_);
101 auto obs = obs_.lock();
102 if (obs) {
103 Format format;
104 int32_t currentPositionMs;
105 if (transCoderEngine_->GetDuration(currentPositionMs) == 0) {
106 MEDIA_LOG_D("EVENT_AUDIO_PROGRESS completed position updated: " PUBLIC_LOG_D32, currentPositionMs);
107 obs->OnInfo(TransCoderOnInfoType::INFO_TYPE_PROGRESS_UPDATE, currentPositionMs);
108 } else {
109 MEDIA_LOG_W("get transCoder engine current time error");
110 }
111 }
112 }
113
DoReportMediaProgress()114 void HiTransCoderCallbackLooper::DoReportMediaProgress()
115 {
116 OHOS::Media::AutoLock lock(loopMutex_);
117 if (!reportMediaProgress_) {
118 return;
119 }
120 auto obs = obs_.lock();
121 if (obs && !isDropMediaProgress_) {
122 Format format;
123 int32_t currentPositionMs;
124 int32_t durationMs;
125 if (transCoderEngine_->GetCurrentTime(currentPositionMs) == 0 &&
126 transCoderEngine_->GetDuration(durationMs) == 0) {
127 FALSE_RETURN(durationMs != 0);
128 int32_t progress = currentPositionMs * 100 / durationMs;
129 MEDIA_LOG_D("EVENT_AUDIO_PROGRESS position updated: " PUBLIC_LOG_D32, progress);
130 obs->OnInfo(TransCoderOnInfoType::INFO_TYPE_PROGRESS_UPDATE, progress);
131 } else {
132 MEDIA_LOG_W("get transcoder engine current time error");
133 }
134 }
135 isDropMediaProgress_ = false;
136 FALSE_RETURN(reportMediaProgress_);
137 Enqueue(std::make_shared<Event>(WHAT_MEDIA_PROGRESS,
138 SteadyClock::GetCurrentTimeMs() + reportProgressIntervalMs_, Any()));
139 }
140
OnError(TransCoderErrorType errorType,int32_t errorCode)141 void HiTransCoderCallbackLooper::OnError(TransCoderErrorType errorType, int32_t errorCode)
142 {
143 Enqueue(std::make_shared<HiTransCoderCallbackLooper::Event>(WHAT_ERROR, SteadyClock::GetCurrentTimeMs(),
144 std::make_pair(errorType, errorCode)));
145 }
146
DoReportError(const Any & error)147 void HiTransCoderCallbackLooper::DoReportError(const Any &error)
148 {
149 OHOS::Media::AutoLock lock(loopMutex_);
150 auto obs = obs_.lock();
151 FALSE_RETURN(obs != nullptr);
152 auto ptr = AnyCast<std::pair<TransCoderErrorType, int32_t>>(&error);
153 if (ptr == nullptr) {
154 MEDIA_LOG_E("Error: ptr is nullptr");
155 return;
156 }
157 MEDIA_LOG_E("Report error, error type: " PUBLIC_LOG_D32 " error value: " PUBLIC_LOG_D32,
158 static_cast<int32_t>(ptr->first), static_cast<int32_t>(ptr->second));
159 obs->OnError(ptr->first, ptr->second);
160 }
161
OnInfo(TransCoderOnInfoType type,int32_t extra)162 void HiTransCoderCallbackLooper::OnInfo(TransCoderOnInfoType type, int32_t extra)
163 {
164 Enqueue(std::make_shared<HiTransCoderCallbackLooper::Event>(WHAT_INFO, SteadyClock::GetCurrentTimeMs(),
165 std::make_tuple(type, extra)));
166 }
167
DoReportInfo(const Any & info)168 void HiTransCoderCallbackLooper::DoReportInfo(const Any& info)
169 {
170 auto obs = obs_.lock();
171 FALSE_RETURN(obs != nullptr);
172 auto ptr = AnyCast<std::tuple<TransCoderOnInfoType, int32_t>>(&info);
173 if (ptr == nullptr) {
174 MEDIA_LOG_E("Error: ptr is nullptr");
175 return;
176 }
177 MEDIA_LOG_I("Report info, info type: " PUBLIC_LOG_D32 " info value: " PUBLIC_LOG_D32,
178 static_cast<int32_t>(std::get<TUPLE_POS_0>(*ptr)), static_cast<int32_t>(std::get<TUPLE_POS_1>(*ptr)));
179 obs->OnInfo(std::get<TUPLE_POS_0>(*ptr), std::get<TUPLE_POS_1>(*ptr));
180 }
181
LoopOnce(const std::shared_ptr<HiTransCoderCallbackLooper::Event> & item)182 void HiTransCoderCallbackLooper::LoopOnce(const std::shared_ptr<HiTransCoderCallbackLooper::Event>& item)
183 {
184 switch (item->what) {
185 case WHAT_MEDIA_PROGRESS:
186 DoReportMediaProgress();
187 break;
188 case WHAT_INFO:
189 DoReportInfo(item->detail);
190 break;
191 case WHAT_ERROR:
192 DoReportError(item->detail);
193 break;
194 default:
195 break;
196 }
197 }
198
Enqueue(const std::shared_ptr<HiTransCoderCallbackLooper::Event> & event)199 void HiTransCoderCallbackLooper::Enqueue(const std::shared_ptr<HiTransCoderCallbackLooper::Event>& event)
200 {
201 if (event->what == WHAT_NONE) {
202 MEDIA_LOG_I("invalid event");
203 }
204 int64_t delayUs = (event->whenMs - SteadyClock::GetCurrentTimeMs()) * 1000;
205 task_->SubmitJob([this, event]() {
206 LoopOnce(event);
207 }, delayUs);
208 }
209 } // namespace Media
210 } // namespace OHOS