• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "dcamera_softbus_latency.h"
17 
18 #include "distributed_camera_constants.h"
19 #include "distributed_camera_errno.h"
20 #include "distributed_hardware_log.h"
21 #include "dcamera_softbus_adapter.h"
22 #include "softbus_bus_center.h"
23 #include "softbus_common.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 IMPLEMENT_SINGLE_INSTANCE(DCameraSoftbusLatency);
28 
29 constexpr static int32_t MICROSECONDS = 1000;
30 
OnTimeSyncResult(const TimeSyncResultInfo * info,int32_t retCode)31 static void OnTimeSyncResult(const TimeSyncResultInfo *info, int32_t retCode)
32 {
33     int32_t microsecond = info->result.millisecond * MICROSECONDS + info->result.microsecond;
34     DHLOGD("DCameraSoftbusLatency OnTimeSyncResult retcode %d, millisecond: %d, microsecond %d microsecond_ %d",
35         retCode, info->result.millisecond, info->result.microsecond, microsecond);
36     DCameraSoftbusLatency::GetInstance().SetTimeSyncInfo(microsecond, info->target.targetNetworkId);
37 }
38 
StartSoftbusTimeSync(const std::string & devId)39 int32_t DCameraSoftbusLatency::StartSoftbusTimeSync(const std::string& devId)
40 {
41     DHLOGI("StartSoftbusTimeSync latency start.");
42     {
43         std::lock_guard<std::mutex> lock(micLock_);
44         if (refCount_[devId] > REF_INITIAL) {
45             refCount_[devId]++;
46             DHLOGD("No need to start time offset, refCount just plus one and now is: %d.", refCount_[devId]);
47             return DCAMERA_OK;
48         }
49     }
50     ITimeSyncCb timeSyncCb;
51     timeSyncCb.onTimeSyncResult = OnTimeSyncResult;
52     int32_t ret = StartTimeSync(DCAMERA_PKG_NAME.c_str(), devId.c_str(), LOW_ACCURACY, NORMAL_PERIOD,
53         &timeSyncCb);
54     if (ret != DCAMERA_OK) {
55         DHLOGE("DCameraSoftbusLatency:: StartSoftbusTimeSync failed networkId %s", devId.c_str());
56     }
57     {
58         std::lock_guard<std::mutex> lock(micLock_);
59         refCount_[devId]++;
60         offsets_.emplace(devId, 0);
61     }
62     DHLOGI("DCameraSoftbusLatency:: StartSoftbusTimeSync success ");
63     return DCAMERA_OK;
64 }
65 
StopSoftbusTimeSync(const std::string & devId)66 int32_t DCameraSoftbusLatency::StopSoftbusTimeSync(const std::string& devId)
67 {
68     DHLOGI("DCameraSoftbusLatency::StopSoftbusTimeSync start.");
69     {
70         std::lock_guard<std::mutex> lock(micLock_);
71         if (refCount_[devId] == REF_INITIAL) {
72             DHLOGD("No need to stop time offset, refCount is zero.");
73             return DCAMERA_OK;
74         }
75         if (refCount_[devId] > REF_NORMAL) {
76             refCount_[devId]--;
77             DHLOGD("No need to stop time offset, refCount just minus one and now is: %d.", refCount_[devId]);
78             return DCAMERA_OK;
79         }
80     }
81     int32_t ret = StopTimeSync(DCAMERA_PKG_NAME.c_str(), devId.c_str());
82     if (ret != DCAMERA_OK) {
83         DHLOGE("DCameraSoftbusLatency:: StopSoftbusTimeSync failed ret:%d", ret);
84     }
85     {
86         std::lock_guard<std::mutex> lock(micLock_);
87         refCount_[devId]--;
88         offsets_.erase(devId);
89         refCount_.erase(devId);
90     }
91     return DCAMERA_OK;
92 }
93 
SetTimeSyncInfo(const int32_t microsecond,const std::string & devId)94 void DCameraSoftbusLatency::SetTimeSyncInfo(const int32_t microsecond, const std::string& devId)
95 {
96     std::lock_guard<std::mutex> lock(offsetLock_);
97     offsets_[devId] = microsecond;
98 }
99 
GetTimeSyncInfo(const std::string & devId)100 int32_t DCameraSoftbusLatency::GetTimeSyncInfo(const std::string& devId)
101 {
102     std::lock_guard<std::mutex> lock(offsetLock_);
103     auto dev = offsets_.find(devId);
104     if (dev == offsets_.end()) {
105         return DCAMERA_OK;
106     }
107     return offsets_[devId];
108 }
109 } // namespace DistributedHardware
110 }