1 /*
2 * Copyright (c) 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 "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 "nlohmann/json.hpp"
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: %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 nlohmann::json enable = {
42 {DH_TYPE, DHType::CAMERA},
43 {LOW_LATENCY_ENABLE, true},
44 };
45 dHFwkKit->PublishMessage(DHTopic::TOPIC_LOW_LATENCY, enable.dump());
46 refCount_++;
47 DHLOGD("Enable low latency success and now refCount is: %d", refCount_.load());
48 return DCAMERA_OK;
49 }
50
DisableLowLatency()51 int32_t DCameraLowLatency::DisableLowLatency()
52 {
53 DHLOGD("Disable low latency start.");
54 if (refCount_ == REF_INITIAL) {
55 DHLOGD("No need to disable low latency, refCount is zero.");
56 return DCAMERA_OK;
57 }
58 if (refCount_ > REF_NORMAL) {
59 refCount_--;
60 DHLOGD("No need to disable low latency, refCount just minus one and now is: %d.", refCount_.load());
61 return DCAMERA_OK;
62 }
63 std::shared_ptr<DistributedHardwareFwkKit> dHFwkKit = GetDHFwkKit();
64 if (dHFwkKit == nullptr) {
65 DHLOGE("Get dHFwkKit is null when disable low latency.");
66 return DCAMERA_BAD_VALUE;
67 }
68 nlohmann::json disable = {
69 {DH_TYPE, DHType::CAMERA},
70 {LOW_LATENCY_ENABLE, false},
71 };
72 dHFwkKit->PublishMessage(DHTopic::TOPIC_LOW_LATENCY, disable.dump());
73 refCount_--;
74 DHLOGD("Disable low latency success.");
75 return DCAMERA_OK;
76 }
77
GetDHFwkKit()78 std::shared_ptr<DistributedHardwareFwkKit> DCameraLowLatency::GetDHFwkKit()
79 {
80 std::lock_guard<std::mutex> lock(dHFwkKitMutex_);
81 if (dHFwkKit_ == nullptr) {
82 dHFwkKit_ = std::make_shared<DistributedHardwareFwkKit>();
83 }
84 return dHFwkKit_;
85 }
86 } // namespace DistributedHardware
87 } // namespace OHOS