1 /*
2  * Copyright (c) 2021-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_session_callback.h"
17 
18 #include "dcamera_event_cmd.h"
19 #include "dcamera_utils_tools.h"
20 #include "distributed_camera_constants.h"
21 #include "distributed_hardware_log.h"
22 #include "metadata_utils.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
DCameraSessionCallback(const std::shared_ptr<StateCallback> & callback)26 DCameraSessionCallback::DCameraSessionCallback(const std::shared_ptr<StateCallback>& callback) : callback_(callback)
27 {
28 }
29 
OnError(int32_t errorCode)30 void DCameraSessionCallback::OnError(int32_t errorCode)
31 {
32     DHLOGE("enter, errorCode: %{public}d", errorCode);
33     if (callback_ == nullptr) {
34         DHLOGE("StateCallback is null");
35         return;
36     }
37 
38     std::shared_ptr<DCameraEvent> event = std::make_shared<DCameraEvent>();
39     event->eventType_ = DCAMERA_MESSAGE;
40     event->eventResult_ = DCAMERA_EVENT_DEVICE_ERROR;
41     callback_->OnStateChanged(event);
42 }
43 
OnFocusState(FocusState state)44 void DCameraSessionCallback::OnFocusState(FocusState state)
45 {
46     DHLOGI("enter, state: %{public}d", state);
47     if (callback_ == nullptr) {
48         DHLOGE("StateCallback is null");
49         return;
50     }
51 
52     auto iter = focusStateMap_.find(state);
53     if (iter == focusStateMap_.end()) {
54         DHLOGE("focusStateMap find %{public}d state failed", state);
55         return;
56     }
57 
58     int32_t dataCountStartNum = 1;
59     uint8_t focusState = iter->second;
60     std::shared_ptr<Camera::CameraMetadata> cameraMetadata =
61         std::make_shared<Camera::CameraMetadata>(CAMERA_META_DATA_ITEM_CAPACITY, CAMERA_META_DATA_DATA_CAPACITY);
62     if (!cameraMetadata->addEntry(OHOS_CONTROL_FOCUS_STATE, &focusState, dataCountStartNum)) {
63         DHLOGE("cameraMetadata add entry failed");
64         return;
65     }
66 
67     std::string abilityString = Camera::MetadataUtils::EncodeToString(cameraMetadata);
68     std::string encodeString = Base64Encode(reinterpret_cast<const unsigned char *>(abilityString.c_str()),
69         abilityString.length());
70 
71     std::shared_ptr<DCameraSettings> dcSetting = std::make_shared<DCameraSettings>();
72     dcSetting->type_ = DCSettingsType::METADATA_RESULT;
73     dcSetting->value_ = encodeString;
74     std::vector<std::shared_ptr<DCameraSettings>> settings;
75     settings.push_back(dcSetting);
76 }
77 } // namespace DistributedHardware
78 } // namespace OHOS