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