1 /*
2 * Copyright (c) 2022-2022 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 "hiplayer_callback_looper.h"
19 #include <utility>
20 #include "foundation/log.h"
21 #include "foundation/utils/steady_clock.h"
22 #include "media_errors.h"
23
24 namespace OHOS {
25 namespace Media {
26 namespace {
27 constexpr int32_t WHAT_NONE = 0;
28 constexpr int32_t WHAT_MEDIA_PROGRESS = 1;
29 constexpr int32_t WHAT_INFO = 2;
30 constexpr int32_t WHAT_ERROR = 3;
31 }
HiPlayerCallbackLooper()32 HiPlayerCallbackLooper::HiPlayerCallbackLooper() : task_("callbackThread", OSAL::ThreadPriority::NORMAL)
33 {
34 task_.RegisterHandler([this] {LoopOnce();});
35 }
36
~HiPlayerCallbackLooper()37 HiPlayerCallbackLooper::~HiPlayerCallbackLooper()
38 {
39 Stop();
40 }
41
IsStarted()42 bool HiPlayerCallbackLooper::IsStarted()
43 {
44 return taskStarted_;
45 }
46
Stop()47 void HiPlayerCallbackLooper::Stop()
48 {
49 if (taskStarted_) {
50 eventQueue_.Quit();
51 task_.Stop();
52 taskStarted_ = false;
53 }
54 }
55
StartWithPlayerEngineObs(const std::weak_ptr<IPlayerEngineObs> & obs)56 void HiPlayerCallbackLooper::StartWithPlayerEngineObs(const std::weak_ptr<IPlayerEngineObs>& obs)
57 {
58 obs_ = obs;
59 if (!taskStarted_) {
60 task_.Start();
61 taskStarted_ = true;
62 MEDIA_LOG_I("start callback looper");
63 }
64 }
SetPlayEngine(IPlayerEngine * engine)65 void HiPlayerCallbackLooper::SetPlayEngine(IPlayerEngine* engine)
66 {
67 playerEngine_ = engine;
68 }
69
StartReportMediaProgress(int64_t updateIntervalMs)70 void HiPlayerCallbackLooper::StartReportMediaProgress(int64_t updateIntervalMs)
71 {
72 reportProgressIntervalMs_ = updateIntervalMs;
73 if (reportMediaProgress_) { // already set
74 return;
75 }
76 reportMediaProgress_ = true;
77 eventQueue_.Enqueue(std::make_shared<Event>(WHAT_MEDIA_PROGRESS, SteadyClock::GetCurrentTimeMs(), Plugin::Any()));
78 }
79
ManualReportMediaProgressOnce()80 void HiPlayerCallbackLooper::ManualReportMediaProgressOnce()
81 {
82 eventQueue_.Enqueue(std::make_shared<Event>(WHAT_MEDIA_PROGRESS, SteadyClock::GetCurrentTimeMs(), Plugin::Any()));
83 }
84
StopReportMediaProgress()85 void HiPlayerCallbackLooper::StopReportMediaProgress()
86 {
87 reportMediaProgress_ = false;
88 }
89
DoReportMediaProgress()90 void HiPlayerCallbackLooper::DoReportMediaProgress()
91 {
92 auto obs = obs_.lock();
93 if (obs) {
94 Format format;
95 int32_t currentPositionMs;
96 if (playerEngine_->GetCurrentTime(currentPositionMs) == MSERR_OK) {
97 MEDIA_LOG_DD("EVENT_AUDIO_PROGRESS position updated: " PUBLIC_LOG_D32, currentPositionMs);
98 obs->OnInfo(INFO_TYPE_POSITION_UPDATE, currentPositionMs, format);
99 } else {
100 MEDIA_LOG_W("get player engine current time error");
101 }
102 }
103 if (reportMediaProgress_) {
104 eventQueue_.Enqueue(std::make_shared<Event>(WHAT_MEDIA_PROGRESS,
105 SteadyClock::GetCurrentTimeMs() + reportProgressIntervalMs_, Plugin::Any()));
106 }
107 }
108
OnError(PlayerErrorType errorType,int32_t errorCode)109 void HiPlayerCallbackLooper::OnError(PlayerErrorType errorType, int32_t errorCode)
110 {
111 eventQueue_.Enqueue(std::make_shared<HiPlayerCallbackLooper::Event>(WHAT_ERROR, SteadyClock::GetCurrentTimeMs(),
112 std::make_pair(errorType, errorCode)));
113 }
114
DoReportError(const Plugin::Any & error)115 void HiPlayerCallbackLooper::DoReportError(const Plugin::Any &error)
116 {
117 auto obs = obs_.lock();
118 if (obs != nullptr) {
119 auto ptr = Plugin::AnyCast<std::pair<PlayerErrorType, int32_t>>(&error);
120 MEDIA_LOG_E("Report error, error type: " PUBLIC_LOG_D32 " error value: " PUBLIC_LOG_D32,
121 static_cast<int32_t>(ptr->first), static_cast<int32_t>(ptr->second));
122 obs->OnError(ptr->first, ptr->second);
123 }
124 }
125
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)126 void HiPlayerCallbackLooper::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
127 {
128 eventQueue_.Enqueue(std::make_shared<HiPlayerCallbackLooper::Event>(WHAT_INFO, SteadyClock::GetCurrentTimeMs(),
129 std::make_tuple(type, extra, infoBody)));
130 }
131
DoReportInfo(const Plugin::Any & info)132 void HiPlayerCallbackLooper::DoReportInfo(const Plugin::Any& info)
133 {
134 auto obs = obs_.lock();
135 if (obs != nullptr) {
136 auto ptr = Plugin::AnyCast<std::tuple<PlayerOnInfoType, int32_t, Format>>(&info);
137 MEDIA_LOG_I("Report info, info type: " PUBLIC_LOG_D32 " info value: " PUBLIC_LOG_D32,
138 static_cast<int32_t>(std::get<0>(*ptr)), static_cast<int32_t>(std::get<1>(*ptr)));
139 obs->OnInfo(std::get<0>(*ptr), std::get<1>(*ptr), std::get<2>(*ptr)); // indexes
140 }
141 }
142
LoopOnce()143 void HiPlayerCallbackLooper::LoopOnce()
144 {
145 auto item = eventQueue_.Next();
146 switch (item->what) {
147 case WHAT_MEDIA_PROGRESS:
148 DoReportMediaProgress();
149 break;
150 case WHAT_INFO:
151 DoReportInfo(item->detail);
152 break;
153 case WHAT_ERROR:
154 DoReportError(item->detail);
155 break;
156 default:
157 break;
158 }
159 }
160
Enqueue(const std::shared_ptr<HiPlayerCallbackLooper::Event> & event)161 void HiPlayerCallbackLooper::EventQueue::Enqueue(const std::shared_ptr<HiPlayerCallbackLooper::Event>& event)
162 {
163 if (event->what == WHAT_NONE) {
164 MEDIA_LOG_I("invalid event");
165 }
166 OSAL::ScopedLock lock(queueMutex_);
167 if (quit_) {
168 MEDIA_LOG_W("event already quit");
169 return;
170 }
171 auto ite = queue_.begin();
172 for (; ite != queue_.end(); ite++) {
173 if ((*ite)->whenMs > event->whenMs) {
174 break;
175 }
176 }
177 auto pos = queue_.insert(ite, event);
178 if (pos == queue_.begin()) {
179 queueHeadUpdatedCond_.NotifyOne();
180 }
181 }
182
Next()183 std::shared_ptr<HiPlayerCallbackLooper::Event> HiPlayerCallbackLooper::EventQueue::Next()
184 {
185 OSAL::ScopedLock lock(queueMutex_);
186 // not empty
187 while (queue_.empty() && !quit_) {
188 queueHeadUpdatedCond_.Wait(lock);
189 }
190
191 do {
192 if (quit_) {
193 return std::make_shared<HiPlayerCallbackLooper::Event>(WHAT_NONE, 0, Plugin::Any());
194 }
195 auto wakenAtTime = (*queue_.begin())->whenMs;
196 auto leftTime = wakenAtTime - SteadyClock::GetCurrentTimeMs();
197 if (leftTime <= 0) {
198 auto first = *queue_.begin();
199 queue_.erase(queue_.begin());
200 return first;
201 }
202 queueHeadUpdatedCond_.WaitFor(lock, leftTime);
203 } while (1);
204 }
205
Quit()206 void HiPlayerCallbackLooper::EventQueue::Quit()
207 {
208 OSAL::ScopedLock lock(queueMutex_);
209 quit_ = true;
210 queueHeadUpdatedCond_.NotifyOne();
211 }
212 } // namespace Media
213 } // namespace OHOS