• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CreateTask(std::string playerId)61 void LiveController::CreateTask(std::string playerId)
62 {
63     task_ = std::make_unique<Task>("checkliveDelayThread", playerId, TaskType::GLOBAL, TaskPriority::NORMAL, false);
64 }
65 
StartCheckLiveDelayTime(int64_t updateIntervalMs)66 void LiveController::StartCheckLiveDelayTime(int64_t updateIntervalMs)
67 {
68     MEDIA_LOG_I("LiveController StartCheckLiveDelayTime, check interval is " PUBLIC_LOG_D64 " ms", updateIntervalMs);
69     checkLiveDelayTimeIntervalMs_ = updateIntervalMs;
70     if (isCheckLiveDelayTimeSet_.load()) { // already set
71         return;
72     }
73     isCheckLiveDelayTimeSet_.store(true);
74     Enqueue(std::make_shared<Event>(WHAT_LIVE_DELAY_TIME,
75             SteadyClock::GetCurrentTimeMs() + checkLiveDelayTimeIntervalMs_, Any()));
76 }
77 
StopCheckLiveDelayTime()78 void LiveController::StopCheckLiveDelayTime()
79 {
80     MEDIA_LOG_I("LiveController::StopCheckLiveDelayTime");
81     isCheckLiveDelayTimeSet_.store(false);
82 }
83 
Enqueue(const std::shared_ptr<LiveController::Event> & event)84 void LiveController::Enqueue(const std::shared_ptr<LiveController::Event>& event)
85 {
86     FALSE_RETURN(event != nullptr && task_ != nullptr);
87     if (event->what == WHAT_NONE) {
88         MEDIA_LOG_I("invalid event");
89     }
90     int64_t delayUs = (event->whenMs - SteadyClock::GetCurrentTimeMs()) * 1000;
91     task_->SubmitJob([this, event]() {
92         LoopOnce(event);
93         }, delayUs);
94 }
95 
LoopOnce(const std::shared_ptr<Event> & item)96 void LiveController::LoopOnce(const std::shared_ptr<Event>& item)
97 {
98     FALSE_RETURN(item != nullptr);
99     switch (item->what) {
100         case WHAT_LIVE_DELAY_TIME:
101             DoCheckLiveDelayTime();
102             break;
103         default:
104             break;
105     }
106 }
107 
DoCheckLiveDelayTime()108 void LiveController::DoCheckLiveDelayTime()
109 {
110     if (!isCheckLiveDelayTimeSet_.load()) {
111         return;
112     }
113     auto obs = obs_.lock();
114     if (obs) {
115         obs->OnSystemOperation(OPERATION_TYPE_CHECK_LIVE_DELAY, OPERATION_REASON_CHECK_LIVE_DELAY_TIME);
116     }
117     if (isCheckLiveDelayTimeSet_.load()) {
118         Enqueue(std::make_shared<Event>(WHAT_LIVE_DELAY_TIME,
119             SteadyClock::GetCurrentTimeMs() + checkLiveDelayTimeIntervalMs_, Any()));
120     }
121 }
122 
OnError(PlayerErrorType errorType,int32_t errorCode)123 void LiveController::OnError(PlayerErrorType errorType, int32_t errorCode)
124 {
125     (void)errorType;
126     (void)errorCode;
127 }
128 
OnInfo(PlayerOnInfoType type,int32_t extra,const Format & infoBody)129 void LiveController::OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody)
130 {
131     (void)type;
132     (void)extra;
133     (void)infoBody;
134 }
135 
OnSystemOperation(PlayerOnSystemOperationType type,PlayerOperationReason reason)136 void LiveController::OnSystemOperation(PlayerOnSystemOperationType type, PlayerOperationReason reason)
137 {
138     (void)type;
139     (void)reason;
140 }
141 }  // namespace Media
142 }  // namespace OHOS