1 /*
2 * Copyright (c) 2021-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 "camera_counting_timer.h"
17 #include "camera_log.h"
18
19 namespace OHOS {
20 namespace CameraStandard {
GetInstance()21 CameraCountingTimer& CameraCountingTimer::GetInstance()
22 {
23 static CameraCountingTimer instance;
24 return instance;
25 }
26
CameraCountingTimer()27 CameraCountingTimer::CameraCountingTimer()
28 : userCount_(0),
29 timer_(nullptr)
30 {
31 MEDIA_INFO_LOG("entered.");
32 };
33
~CameraCountingTimer()34 CameraCountingTimer::~CameraCountingTimer()
35 {
36 MEDIA_INFO_LOG("entered.");
37 CHECK_RETURN(!timer_);
38 timer_->Shutdown();
39 timer_ = nullptr;
40 }
41
IncreaseUserCount()42 void CameraCountingTimer::IncreaseUserCount()
43 {
44 MEDIA_INFO_LOG("entered, num of user: %d + 1", static_cast<int>(userCount_.load()));
45 if (timer_ == nullptr) {
46 timer_ = std::make_unique<OHOS::Utils::Timer>("CameraServiceTimer");
47 timer_->Setup();
48 MEDIA_INFO_LOG("create timer thread");
49 }
50 ++userCount_;
51 }
52
DecreaseUserCount()53 void CameraCountingTimer::DecreaseUserCount()
54 {
55 MEDIA_INFO_LOG("entered, num of user: %u - 1", userCount_.load());
56 --userCount_;
57 if (userCount_.load() == 0 && timer_ != nullptr) {
58 MEDIA_INFO_LOG("delete timer thread");
59 }
60 }
61
Register(const TimerCallback & callback,uint32_t interval,bool once)62 uint32_t CameraCountingTimer::Register(const TimerCallback& callback, uint32_t interval, bool once)
63 {
64 CHECK_RETURN_RET_ELOG(timer_ == nullptr, 0, "timer is nullptr");
65
66 uint32_t timerId = timer_->Register(callback, interval, once);
67 MEDIA_DEBUG_LOG("timerId: %{public}u", timerId);
68 return timerId;
69 }
70
Unregister(uint32_t timerId)71 void CameraCountingTimer::Unregister(uint32_t timerId)
72 {
73 MEDIA_DEBUG_LOG("timerId: %{public}d", timerId);
74 CHECK_RETURN(!timer_);
75 MEDIA_DEBUG_LOG("timerId: %{public}d", timerId);
76 timer_->Unregister(timerId);
77 }
78 } // namespace CameraStandard
79 } // namespace OHOS