• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "media_errors.h"
22 #include "utils/steady_clock.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         obs->OnError(ptr->first, ptr->second);
121     }
122 }
123 
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)124 void HiPlayerCallbackLooper::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
125 {
126     eventQueue_.Enqueue(std::make_shared<HiPlayerCallbackLooper::Event>(WHAT_INFO, SteadyClock::GetCurrentTimeMs(),
127         std::make_tuple(type, extra, infoBody)));
128 }
129 
DoReportInfo(const Plugin::Any & info)130 void HiPlayerCallbackLooper::DoReportInfo(const Plugin::Any& info)
131 {
132     auto obs = obs_.lock();
133     if (obs != nullptr) {
134         auto ptr = Plugin::AnyCast<std::tuple<PlayerOnInfoType, int32_t, Format>>(&info);
135         obs->OnInfo(std::get<0>(*ptr), std::get<1>(*ptr), std::get<2>(*ptr)); // indexes
136     }
137 }
138 
LoopOnce()139 void HiPlayerCallbackLooper::LoopOnce()
140 {
141     auto item = eventQueue_.Next();
142     switch (item->what) {
143         case WHAT_MEDIA_PROGRESS:
144             DoReportMediaProgress();
145             break;
146         case WHAT_INFO:
147             DoReportInfo(item->detail);
148             break;
149         case WHAT_ERROR:
150             DoReportError(item->detail);
151             break;
152         default:
153             break;
154     }
155 }
156 
Enqueue(const std::shared_ptr<HiPlayerCallbackLooper::Event> & event)157 void HiPlayerCallbackLooper::EventQueue::Enqueue(const std::shared_ptr<HiPlayerCallbackLooper::Event>& event)
158 {
159     if (event->what == WHAT_NONE) {
160         MEDIA_LOG_I("invalid event");
161     }
162     OSAL::ScopedLock lock(queueMutex_);
163     if (quit_) {
164         MEDIA_LOG_W("event already quit");
165         return;
166     }
167     auto ite = queue_.begin();
168     for (; ite != queue_.end(); ite++) {
169         if ((*ite)->whenMs > event->whenMs) {
170             break;
171         }
172     }
173     auto pos = queue_.insert(ite, event);
174     if (pos == queue_.begin()) {
175         queueHeadUpdatedCond_.NotifyOne();
176     }
177 }
178 
Next()179 std::shared_ptr<HiPlayerCallbackLooper::Event> HiPlayerCallbackLooper::EventQueue::Next()
180 {
181     OSAL::ScopedLock lock(queueMutex_);
182     // not empty
183     while (queue_.empty() && !quit_) {
184         queueHeadUpdatedCond_.Wait(lock);
185     }
186 
187     do {
188         if (quit_) {
189             return std::make_shared<HiPlayerCallbackLooper::Event>(WHAT_NONE, 0, Plugin::Any());
190         }
191         auto wakenAtTime = (*queue_.begin())->whenMs;
192         auto leftTime = wakenAtTime - SteadyClock::GetCurrentTimeMs();
193         if (leftTime <= 0) {
194             auto first = *queue_.begin();
195             queue_.erase(queue_.begin());
196             return first;
197         }
198         queueHeadUpdatedCond_.WaitFor(lock, leftTime);
199     } while (1);
200 }
201 
Quit()202 void HiPlayerCallbackLooper::EventQueue::Quit()
203 {
204     OSAL::ScopedLock lock(queueMutex_);
205     quit_ = true;
206     queueHeadUpdatedCond_.NotifyOne();
207 }
208 }  // namespace Media
209 }  // namespace OHOS