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