1 /*
2 * Copyright (c) 2025 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 "LiveController"
17
18 #include "live_controller.h"
19 #include "common/log.h"
20 #include "osal/task/autolock.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_PLAYER, "LiveController" };
24 }
25
26 namespace OHOS {
27 namespace Media {
28 namespace {
29 constexpr int32_t WHAT_NONE = 0;
30 constexpr int32_t WHAT_LIVE_DELAY_TIME = 1;
31 }
LiveController()32 LiveController::LiveController()
33 {
34 }
35
~LiveController()36 LiveController::~LiveController()
37 {
38 Stop();
39 }
40
Stop()41 void LiveController::Stop()
42 {
43 if (taskStarted_) {
44 FALSE_RETURN(task_ != nullptr);
45 task_->Stop();
46 taskStarted_ = false;
47 }
48 }
49
StartWithPlayerEngineObs(const std::weak_ptr<IPlayerEngineObs> & obs)50 void LiveController::StartWithPlayerEngineObs(const std::weak_ptr<IPlayerEngineObs>& obs)
51 {
52 obs_ = obs;
53 if (!taskStarted_) {
54 FALSE_RETURN(task_ != nullptr);
55 task_->Start();
56 taskStarted_ = true;
57 MEDIA_LOG_I("start check live delay looper");
58 }
59 }
60
SetPlayEngine(IPlayerEngine * engine,std::string playerId)61 void LiveController::SetPlayEngine(IPlayerEngine* engine, std::string playerId)
62 {
63 playerEngine_ = engine;
64 task_ = std::make_unique<Task>("checkliveDelayThread", playerId, TaskType::GLOBAL, TaskPriority::NORMAL, false);
65 }
66
StartCheckLiveDelayTime(int64_t updateIntervalMs)67 void LiveController::StartCheckLiveDelayTime(int64_t updateIntervalMs)
68 {
69 MEDIA_LOG_I("LiveController StartCheckLiveDalyTime");
70 checkLiveDelayTimeIntervalMs_ = updateIntervalMs;
71 if (isCheckLiveDelayTimeSet_.load()) { // already set
72 return;
73 }
74 isCheckLiveDelayTimeSet_.store(true);
75 Enqueue(std::make_shared<Event>(WHAT_LIVE_DELAY_TIME,
76 SteadyClock::GetCurrentTimeMs() + checkLiveDelayTimeIntervalMs_, Any()));
77 }
78
StopCheckLiveDelayTime()79 void LiveController::StopCheckLiveDelayTime()
80 {
81 MEDIA_LOG_I("LiveController::StopCheckLiveDalyTime");
82 isCheckLiveDelayTimeSet_.store(false);
83 }
84
Enqueue(const std::shared_ptr<LiveController::Event> & event)85 void LiveController::Enqueue(const std::shared_ptr<LiveController::Event>& event)
86 {
87 FALSE_RETURN(event != nullptr && task_ != nullptr);
88 if (event->what == WHAT_NONE) {
89 MEDIA_LOG_I("invalid event");
90 }
91 int64_t delayUs = (event->whenMs - SteadyClock::GetCurrentTimeMs()) * 1000;
92 task_->SubmitJob([this, event]() {
93 LoopOnce(event);
94 }, delayUs);
95 }
96
LoopOnce(const std::shared_ptr<Event> & item)97 void LiveController::LoopOnce(const std::shared_ptr<Event>& item)
98 {
99 FALSE_RETURN(item != nullptr);
100 switch (item->what) {
101 case WHAT_LIVE_DELAY_TIME:
102 DoCheckLiveDalyTime();
103 break;
104 default:
105 break;
106 }
107 }
108
DoCheckLiveDalyTime()109 void LiveController::DoCheckLiveDalyTime()
110 {
111 if (!isCheckLiveDelayTimeSet_.load()) {
112 return;
113 }
114 auto obs = obs_.lock();
115 if (obs) {
116 obs->OnSystemOperation(OPERATION_TYPE_CHECK_LIVE_DELAY, OPERATION_REASON_CHECK_LIVE_DELAY_TIME);
117 }
118 if (isCheckLiveDelayTimeSet_.load()) {
119 Enqueue(std::make_shared<Event>(WHAT_LIVE_DELAY_TIME,
120 SteadyClock::GetCurrentTimeMs() + checkLiveDelayTimeIntervalMs_, Any()));
121 }
122 }
123
OnError(PlayerErrorType errorType,int32_t errorCode)124 void LiveController::OnError(PlayerErrorType errorType, int32_t errorCode)
125 {
126 (void)errorType;
127 (void)errorCode;
128 }
129
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)130 void LiveController::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
131 {
132 (void)type;
133 (void)extra;
134 (void)infoBody;
135 }
136
OnSystemOperation(PlayerOnSystemOperationType type,PlayerOperationReason reason)137 void LiveController::OnSystemOperation(PlayerOnSystemOperationType type, PlayerOperationReason reason)
138 {
139 (void)type;
140 (void)reason;
141 }
142 } // namespace Media
143 } // namespace OHOS