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 #include "photo_trailing_state.h"
17
18 #include "dp_log.h"
19 #include "dp_timer.h"
20 #include "dp_utils.h"
21 #include "events_monitor.h"
22 #include "state_factory.h"
23
24 namespace OHOS {
25 namespace CameraStandard {
26 namespace DeferredProcessing {
27 REGISTER_STATE(PhotoTrailingState, PHOTO_TRAILING_STATE, CAMERA_ON_STOP_TRAILING);
28
29 namespace {
30 constexpr int32_t DURATIONMS_25_SEC = 25 * 1000;
31 }
32
PhotoTrailingState(SchedulerType type,int32_t stateValue)33 PhotoTrailingState::PhotoTrailingState(SchedulerType type, int32_t stateValue)
34 : IState(type, stateValue)
35 {
36 DP_DEBUG_LOG("entered.");
37 }
38
ReevaluateSchedulerInfo()39 SchedulerInfo PhotoTrailingState::ReevaluateSchedulerInfo()
40 {
41 DP_DEBUG_LOG("PhotoTrailingState: %{public}d", stateValue_);
42 if (stateValue_ == TrailingStatus::SYSTEM_CAMERA_OFF_START_TRAILING) {
43 StartTrailing(DURATIONMS_25_SEC);
44 } else if (stateValue_ == TrailingStatus::CAMERA_ON_STOP_TRAILING) {
45 StartTrailing(DEFAULT_TRAILING_TIME);
46 } else if (stateValue_ == TrailingStatus::CAMERA_ON_STOP_TRAILING) {
47 StopTrailing();
48 }
49 return {!isTrailing_};
50 }
51
StartTrailing(uint32_t duration)52 void PhotoTrailingState::StartTrailing(uint32_t duration)
53 {
54 DP_CHECK_EXECUTE(isTrailing_, StopTrailing());
55 remainingTrailingTime_ = std::max(duration, remainingTrailingTime_);
56 DP_CHECK_RETURN(remainingTrailingTime_ == 0);
57
58 startTimer_ = GetSteadyNow();
59 isTrailing_ = true;
60 DP_INFO_LOG("DPS_PHOTO: StartTrailing time: %{public}u, state: %{public}d",
61 remainingTrailingTime_, isTrailing_);
62 timerId_ = DpsTimer::GetInstance().StartTimer([&]() {OnTimerOut();}, remainingTrailingTime_);
63 }
64
StopTrailing()65 void PhotoTrailingState::StopTrailing()
66 {
67 DP_CHECK_RETURN(!isTrailing_);
68 DP_INFO_LOG("DPS_PHOTO: StopTrailing state: %{public}d", isTrailing_);
69 DpsTimer::GetInstance().StopTimer(timerId_);
70 isTrailing_ = false;
71 remainingTrailingTime_ -= static_cast<uint32_t>(GetDiffTime<Seconds>(startTimer_));
72 remainingTrailingTime_ = std::max(DEFAULT_TRAILING_TIME, remainingTrailingTime_);
73 }
74
OnTimerOut()75 void PhotoTrailingState::OnTimerOut()
76 {
77 DP_INFO_LOG("DPS_PHOTO: TrailingTimeOut");
78 EventsMonitor::GetInstance().NotifyTrailingStatus(TrailingStatus::CAMERA_ON_STOP_TRAILING);
79 }
80 } // namespace DeferredProcessing
81 } // namespace CameraStandard
82 } // namespace OHOS