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