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     reportMediaProgress_ = false;
95 }
96 
DoReportCompletedTime()97 void HiTransCoderCallbackLooper::DoReportCompletedTime()
98 {
99     OHOS::Media::AutoLock lock(loopMutex_);
100     auto obs = obs_.lock();
101     if (obs) {
102         Format format;
103         int32_t currentPositionMs;
104         if (transCoderEngine_->GetDuration(currentPositionMs) == 0) {
105             MEDIA_LOG_D("EVENT_AUDIO_PROGRESS completed position updated: " PUBLIC_LOG_D32, currentPositionMs);
106             obs->OnInfo(TransCoderOnInfoType::INFO_TYPE_PROGRESS_UPDATE, currentPositionMs);
107         } else {
108             MEDIA_LOG_W("get transCoder engine current time error");
109         }
110     }
111 }
112 
DoReportMediaProgress()113 void HiTransCoderCallbackLooper::DoReportMediaProgress()
114 {
115     OHOS::Media::AutoLock lock(loopMutex_);
116     if (!reportMediaProgress_) {
117         return;
118     }
119     auto obs = obs_.lock();
120     if (obs && !isDropMediaProgress_) {
121         Format format;
122         int32_t currentPositionMs;
123         int32_t durationMs;
124         if (transCoderEngine_->GetCurrentTime(currentPositionMs) == 0 &&
125             transCoderEngine_->GetDuration(durationMs) == 0) {
126             int32_t progress = currentPositionMs * 100 / durationMs;
127             MEDIA_LOG_D("EVENT_AUDIO_PROGRESS position updated: " PUBLIC_LOG_D32, progress);
128             obs->OnInfo(TransCoderOnInfoType::INFO_TYPE_PROGRESS_UPDATE, progress);
129         } else {
130             MEDIA_LOG_W("get transcoder engine current time error");
131         }
132     }
133     isDropMediaProgress_ = false;
134     FALSE_RETURN(reportMediaProgress_);
135     Enqueue(std::make_shared<Event>(WHAT_MEDIA_PROGRESS,
136         SteadyClock::GetCurrentTimeMs() + reportProgressIntervalMs_, Any()));
137 }
138 
OnError(TransCoderErrorType errorType,int32_t errorCode)139 void HiTransCoderCallbackLooper::OnError(TransCoderErrorType errorType, int32_t errorCode)
140 {
141     Enqueue(std::make_shared<HiTransCoderCallbackLooper::Event>(WHAT_ERROR, SteadyClock::GetCurrentTimeMs(),
142     std::make_pair(errorType, errorCode)));
143 }
144 
DoReportError(const Any & error)145 void HiTransCoderCallbackLooper::DoReportError(const Any &error)
146 {
147     OHOS::Media::AutoLock lock(loopMutex_);
148     auto obs = obs_.lock();
149     FALSE_RETURN(obs != nullptr);
150     auto ptr = AnyCast<std::pair<TransCoderErrorType, int32_t>>(&error);
151     if (ptr == nullptr) {
152         MEDIA_LOG_E("Error: ptr is nullptr");
153         return;
154     }
155     MEDIA_LOG_E("Report error, error type: " PUBLIC_LOG_D32 " error value: " PUBLIC_LOG_D32,
156         static_cast<int32_t>(ptr->first), static_cast<int32_t>(ptr->second));
157     obs->OnError(ptr->first, ptr->second);
158 }
159 
OnInfo(TransCoderOnInfoType type,int32_t extra)160 void HiTransCoderCallbackLooper::OnInfo(TransCoderOnInfoType type, int32_t extra)
161 {
162     Enqueue(std::make_shared<HiTransCoderCallbackLooper::Event>(WHAT_INFO, SteadyClock::GetCurrentTimeMs(),
163         std::make_tuple(type, extra)));
164 }
165 
DoReportInfo(const Any & info)166 void HiTransCoderCallbackLooper::DoReportInfo(const Any& info)
167 {
168     auto obs = obs_.lock();
169     FALSE_RETURN(obs != nullptr);
170     auto ptr = AnyCast<std::tuple<TransCoderOnInfoType, int32_t>>(&info);
171     if (ptr == nullptr) {
172         MEDIA_LOG_E("Error: ptr is nullptr");
173         return;
174     }
175     MEDIA_LOG_I("Report info, info type: " PUBLIC_LOG_D32 " info value: " PUBLIC_LOG_D32,
176         static_cast<int32_t>(std::get<TUPLE_POS_0>(*ptr)), static_cast<int32_t>(std::get<TUPLE_POS_1>(*ptr)));
177     obs->OnInfo(std::get<TUPLE_POS_0>(*ptr), std::get<TUPLE_POS_1>(*ptr));
178 }
179 
LoopOnce(const std::shared_ptr<HiTransCoderCallbackLooper::Event> & item)180 void HiTransCoderCallbackLooper::LoopOnce(const std::shared_ptr<HiTransCoderCallbackLooper::Event>& item)
181 {
182     switch (item->what) {
183         case WHAT_MEDIA_PROGRESS:
184             DoReportMediaProgress();
185             break;
186         case WHAT_INFO:
187             DoReportInfo(item->detail);
188             break;
189         case WHAT_ERROR:
190             DoReportError(item->detail);
191             break;
192         default:
193             break;
194     }
195 }
196 
Enqueue(const std::shared_ptr<HiTransCoderCallbackLooper::Event> & event)197 void HiTransCoderCallbackLooper::Enqueue(const std::shared_ptr<HiTransCoderCallbackLooper::Event>& event)
198 {
199     if (event->what == WHAT_NONE) {
200         MEDIA_LOG_I("invalid event");
201     }
202     int64_t delayUs = (event->whenMs - SteadyClock::GetCurrentTimeMs()) * 1000;
203     task_->SubmitJob([this, event]() {
204             LoopOnce(event);
205         }, delayUs);
206 }
207 }  // namespace Media
208 }  // namespace OHOS