• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "dcamera_low_latency.h"
17 #include "device_type.h"
18 #include "distributed_camera_errno.h"
19 #include "distributed_hardware_log.h"
20 #include "ipublisher_listener.h"
21 #include "cJSON.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
25 IMPLEMENT_SINGLE_INSTANCE(DCameraLowLatency);
26 
EnableLowLatency()27 int32_t DCameraLowLatency::EnableLowLatency()
28 {
29     DHLOGD("Enable low latency start.");
30     if (refCount_ > REF_INITIAL) {
31         refCount_++;
32         DHLOGD("No need to enable low latency, refCount just plus one and now is: %{public}d.", refCount_.load());
33         return DCAMERA_OK;
34     }
35     std::shared_ptr<DistributedHardwareFwkKit> dHFwkKit = GetDHFwkKit();
36     if (dHFwkKit == nullptr) {
37         DHLOGE("Get dHFwkKit is null when enable low latency.");
38         return DCAMERA_BAD_VALUE;
39     }
40     cJSON *rootValue = cJSON_CreateObject();
41     if (rootValue == nullptr) {
42         return DCAMERA_BAD_VALUE;
43     }
44     cJSON_AddItemToObject(rootValue, DH_TYPE.c_str(), cJSON_CreateNumber(static_cast<uint32_t>(DHType::CAMERA)));
45     cJSON_AddItemToObject(rootValue, LOW_LATENCY_ENABLE.c_str(), cJSON_CreateBool(true));
46     char *jsonstr = cJSON_Print(rootValue);
47     if (jsonstr == nullptr) {
48         cJSON_Delete(rootValue);
49         return DCAMERA_BAD_VALUE;
50     }
51     dHFwkKit->PublishMessage(DHTopic::TOPIC_LOW_LATENCY, std::string(jsonstr));
52     refCount_++;
53     DHLOGD("Enable low latency success and now refCount is: %d", refCount_.load());
54     cJSON_Delete(rootValue);
55     cJSON_free(jsonstr);
56     return DCAMERA_OK;
57 }
58 
DisableLowLatency()59 int32_t DCameraLowLatency::DisableLowLatency()
60 {
61     DHLOGD("Disable low latency start.");
62     if (refCount_ == REF_INITIAL) {
63         DHLOGD("No need to disable low latency, refCount is zero.");
64         return DCAMERA_OK;
65     }
66     if (refCount_ > REF_NORMAL) {
67         refCount_--;
68         DHLOGD("No need to disable low latency, refCount just minus one and now is: %{public}d.", refCount_.load());
69         return DCAMERA_OK;
70     }
71     std::shared_ptr<DistributedHardwareFwkKit> dHFwkKit = GetDHFwkKit();
72     if (dHFwkKit == nullptr) {
73         DHLOGE("Get dHFwkKit is null when disable low latency.");
74         return DCAMERA_BAD_VALUE;
75     }
76     cJSON *rootValue = cJSON_CreateObject();
77     if (rootValue == nullptr) {
78         return DCAMERA_BAD_VALUE;
79     }
80     cJSON_AddItemToObject(rootValue, DH_TYPE.c_str(), cJSON_CreateNumber(static_cast<uint32_t>(DHType::CAMERA)));
81     cJSON_AddItemToObject(rootValue, LOW_LATENCY_ENABLE.c_str(), cJSON_CreateBool(false));
82     char *jsonstr = cJSON_Print(rootValue);
83     if (jsonstr == nullptr) {
84         cJSON_Delete(rootValue);
85         return DCAMERA_BAD_VALUE;
86     }
87     dHFwkKit->PublishMessage(DHTopic::TOPIC_LOW_LATENCY, std::string(jsonstr));
88     refCount_--;
89     DHLOGD("Disable low latency success.");
90     cJSON_Delete(rootValue);
91     cJSON_free(jsonstr);
92     return DCAMERA_OK;
93 }
94 
GetDHFwkKit()95 std::shared_ptr<DistributedHardwareFwkKit> DCameraLowLatency::GetDHFwkKit()
96 {
97     if (dHFwkKit_ == nullptr) {
98         std::lock_guard<std::mutex> lock(dHFwkKitMutex_);
99         if (dHFwkKit_ == nullptr) {
100             dHFwkKit_ = std::make_shared<DistributedHardwareFwkKit>();
101         }
102     }
103     return dHFwkKit_;
104 }
105 } // namespace DistributedHardware
106 } // namespace OHOS