• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 }
53 
DecreaseUserCount()54 void CameraTimer::DecreaseUserCount()
55 {
56     MEDIA_INFO_LOG("entered, num of user: %u - 1", userCount_.load());
57     --userCount_;
58     if (userCount_.load() == 0 && timer_ != nullptr) {
59         MEDIA_INFO_LOG("delete timer thread");
60     }
61 }
62 
Register(const TimerCallback & callback,uint32_t interval,bool once)63 uint32_t CameraTimer::Register(const TimerCallback& callback, uint32_t interval, bool once)
64 {
65     if (timer_ == nullptr) {
66         MEDIA_ERR_LOG("timer is nullptr");
67         return 0;
68     }
69 
70     uint32_t timerId = timer_->Register(callback, interval, once);
71     MEDIA_DEBUG_LOG("timerId: %{public}u", timerId);
72     return timerId;
73 }
74 
Unregister(uint32_t timerId)75 void CameraTimer::Unregister(uint32_t timerId)
76 {
77     MEDIA_DEBUG_LOG("timerId: %{public}d", timerId);
78     if (timer_) {
79         MEDIA_DEBUG_LOG("timerId: %{public}d", timerId);
80         timer_->Unregister(timerId);
81         return;
82     }
83 }
84 } // namespace CameraStandard
85 } // namespace OHOS