• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "camera_device_proxy.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include <message_parcel.h>
20 #include "metadata_utils.h"
21 #include "istream_operator.h"
22 #include "istream_operator_callback.h"
23 
24 namespace OHOS::Camera {
GetStreamOperator(const OHOS::sptr<IStreamOperatorCallback> & callback,OHOS::sptr<IStreamOperator> & streamOperator)25 CamRetCode CameraDeviceProxy::GetStreamOperator(
26     const OHOS::sptr<IStreamOperatorCallback> &callback,
27     OHOS::sptr<IStreamOperator> &streamOperator)
28 {
29     MessageParcel data;
30     MessageParcel reply;
31     MessageOption option;
32 
33     if (!data.WriteInterfaceToken(CameraDeviceProxy::GetDescriptor())) {
34         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
35         return INVALID_ARGUMENT;
36     }
37 
38     bool nullFlag = (callback != nullptr);
39     if (!data.WriteBool(nullFlag)) {
40         HDF_LOGE("%s: stream operator callback flag write failed!", __func__);
41         return INVALID_ARGUMENT;
42     }
43 
44     if (nullFlag && !data.WriteRemoteObject(callback->AsObject())) {
45         HDF_LOGE("%s: write stream operator failed", __func__);
46         return INVALID_ARGUMENT;
47     }
48 
49     int32_t ret = Remote()->SendRequest(
50         CMD_CAMERA_DEVICE_GET_STREAM_OPERATOR, data, reply, option);
51     if (ret != HDF_SUCCESS) {
52         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
53         return INVALID_ARGUMENT;
54     }
55 
56     CamRetCode retCode = static_cast<CamRetCode>(reply.ReadInt32());
57     bool flag = reply.ReadBool();
58     if (flag) {
59         sptr<IRemoteObject> remoteStreamOperator = reply.ReadRemoteObject();
60         streamOperator = OHOS::iface_cast<IStreamOperator>(remoteStreamOperator);
61     }
62 
63     return retCode;
64 }
65 
UpdateSettings(const std::shared_ptr<CameraSetting> & settings)66 CamRetCode CameraDeviceProxy::UpdateSettings(const std::shared_ptr<CameraSetting> &settings)
67 {
68     MessageParcel data;
69     MessageParcel reply;
70     MessageOption option;
71 
72     if (!data.WriteInterfaceToken(CameraDeviceProxy::GetDescriptor())) {
73         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
74         return INVALID_ARGUMENT;
75     }
76 
77     bool bRet = MetadataUtils::EncodeCameraMetadata(settings, data);
78     if (!bRet) {
79         HDF_LOGE("%s: write metadata failed", __func__);
80         return INVALID_ARGUMENT;
81     }
82 
83     int32_t ret = Remote()->SendRequest(
84         CMD_CAMERA_DEVICE_UPDATE_SETTINGS, data, reply, option);
85     if (ret != HDF_SUCCESS) {
86         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
87         return INVALID_ARGUMENT;
88     }
89 
90     return static_cast<CamRetCode>(reply.ReadInt32());
91 }
92 
SetResultMode(const ResultCallbackMode & mode)93 CamRetCode CameraDeviceProxy::SetResultMode(const ResultCallbackMode &mode)
94 {
95     MessageParcel data;
96     MessageParcel reply;
97     MessageOption option;
98 
99     if (!data.WriteInterfaceToken(CameraDeviceProxy::GetDescriptor())) {
100         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
101         return INVALID_ARGUMENT;
102     }
103 
104     if (!data.WriteInt32(static_cast<int32_t>(mode))) {
105         HDF_LOGE("%s: write result callback mode failed", __func__);
106         return INVALID_ARGUMENT;
107     }
108 
109     int32_t ret = Remote()->SendRequest(
110         CMD_CAMERA_DEVICE_SET_RESULT_MODE, data, reply, option);
111     if (ret != HDF_SUCCESS) {
112         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
113         return INVALID_ARGUMENT;
114     }
115 
116     return static_cast<CamRetCode>(reply.ReadInt32());
117 }
118 
GetEnabledResults(std::vector<MetaType> & results)119 CamRetCode CameraDeviceProxy::GetEnabledResults(std::vector<MetaType> &results)
120 {
121     MessageParcel data;
122     MessageParcel reply;
123     MessageOption option;
124 
125     if (!data.WriteInterfaceToken(CameraDeviceProxy::GetDescriptor())) {
126         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
127         return INVALID_ARGUMENT;
128     }
129 
130     int32_t ret = Remote()->SendRequest(
131         CMD_CAMERA_DEVICE_GET_ENABLED_RESULTS, data, reply, option);
132     if (ret != HDF_SUCCESS) {
133         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
134         return INVALID_ARGUMENT;
135     }
136 
137     CamRetCode retCode = static_cast<CamRetCode>(reply.ReadInt32());
138     if (!reply.ReadInt32Vector(&results)) {
139         HDF_LOGE("%s: read results failed", __func__);
140         return INVALID_ARGUMENT;
141     }
142 
143     return retCode;
144 }
145 
EnableResult(const std::vector<MetaType> & results)146 CamRetCode CameraDeviceProxy::EnableResult(const std::vector<MetaType> &results)
147 {
148     MessageParcel data;
149     MessageParcel reply;
150     MessageOption option;
151 
152     if (!data.WriteInterfaceToken(CameraDeviceProxy::GetDescriptor())) {
153         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
154         return INVALID_ARGUMENT;
155     }
156 
157     if (!data.WriteInt32Vector(results)) {
158         HDF_LOGE("%s: write results failed", __func__);
159         return INVALID_ARGUMENT;
160     }
161 
162     int32_t ret = Remote()->SendRequest(
163         CMD_CAMERA_DEVICE_ENABLE_RESULT, data, reply, option);
164     if (ret != HDF_SUCCESS) {
165         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
166         return INVALID_ARGUMENT;
167     }
168 
169     return static_cast<CamRetCode>(reply.ReadInt32());
170 }
171 
DisableResult(const std::vector<MetaType> & results)172 CamRetCode CameraDeviceProxy::DisableResult(const std::vector<MetaType> &results)
173 {
174     MessageParcel data;
175     MessageParcel reply;
176     MessageOption option;
177 
178     if (!data.WriteInterfaceToken(CameraDeviceProxy::GetDescriptor())) {
179         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
180         return INVALID_ARGUMENT;
181     }
182 
183     if (!data.WriteInt32Vector(results)) {
184         HDF_LOGE("%s: write results failed", __func__);
185         return INVALID_ARGUMENT;
186     }
187 
188     int32_t ret = Remote()->SendRequest(
189         CMD_CAMERA_DEVICE_DISABLE_RESULT, data, reply, option);
190     if (ret != HDF_SUCCESS) {
191         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
192         return INVALID_ARGUMENT;
193     }
194 
195     return static_cast<CamRetCode>(reply.ReadInt32());
196 }
197 
Close()198 void CameraDeviceProxy::Close()
199 {
200     MessageParcel data;
201     MessageParcel reply;
202     MessageOption option;
203 
204     if (!data.WriteInterfaceToken(CameraDeviceProxy::GetDescriptor())) {
205         HDF_LOGE("%{public}s: write interface descriptor failed.", __func__);
206         return;
207     }
208 
209     int32_t ret = Remote()->SendRequest(
210         CMD_CAMERA_DEVICE_CLOSE, data, reply, option);
211     if (ret != HDF_SUCCESS) {
212         HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret);
213     }
214 }
215 }