1 /*
2 * Copyright (c) 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 "napi/native_api.h"
17 #include <hilog/log.h>
18 #include <multimedia/image_framework/image/image_native.h>
19 #include <multimedia/image_framework/image/image_receiver_native.h>
20 #include <cstdint>
21 #include <unistd.h>
22 #include <string>
23 #include <thread>
24 #include <chrono>
25 #include <mutex>
26 #include <memory>
27 #include <vector>
28 #include <condition_variable>
29
30 #include "ohcamera/camera.h"
31 #include "ohcamera/camera_input.h"
32 #include "ohcamera/capture_session.h"
33 #include "ohcamera/photo_output.h"
34 #include "ohcamera/preview_output.h"
35 #include "ohcamera/video_output.h"
36 #include "ohcamera/camera_manager.h"
37
38 #undef LOG_DOMAIN
39 #define LOG_DOMAIN 0x3200
40
41 #undef LOG_TAG
42 #define LOG_TAG "MY_TAG"
43
44 #define IMAGE_WIDTH 320
45 #define IMAGE_HEIGHT 480
46 #define IMAGE_CAPACITY 2
47 #define MAX_PATH_SIZE 1024
48
49 #define DECLARE_NAPI_FUNCTION(name, func) \
50 { \
51 (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr \
52 }
53
54 static constexpr int32_t NUM_0 = 0;
55 static constexpr int32_t NUM_1 = 1;
56 static constexpr int32_t NUM_2 = 2;
57 static constexpr int32_t NUM_3 = 3;
58 static constexpr int32_t NUM_4 = 4;
59
60 static const char *MYDATA_KEY_ID = "callBackId";
61 static const char *MYDATA_KEY_NUM = "num";
62 static const char *MYDATA_KEY_STR = "str";
63 static const char *MYDATA_KEY_BOOL = "callbackTriggered";
64 static int32_t g_myDataIndex = NUM_0;
65
BoolToString(bool value)66 std::string BoolToString(bool value)
67 {
68 return value ? "true" : "false";
69 }
70
71 class MyData {
72 public:
MyData()73 MyData() {}
MyData(int32_t id,uint32_t n,std::string s,bool callbackTriggered)74 MyData(int32_t id, uint32_t n, std::string s, bool callbackTriggered):callBackId_(id), num_(n), s_(s),
75 callbackTriggered_(callbackTriggered) {}
~MyData()76 ~MyData() {}
CallbackModifyCallBackId(int32_t id)77 void CallbackModifyCallBackId(int32_t id)
78 {
79 callBackId_ = id;
80 }
81
CallBackModifyUserData()82 void CallBackModifyUserData()
83 {
84 callbackTriggered_ = true;
85 }
86
PrintMyData(std::string tag)87 void PrintMyData(std::string tag)
88 {
89 OH_LOG_INFO(LOG_APP,
90 "UserData %{public}s: callBackId: %{public}d, num: %{public}u, str: %{public}s, Triggered: %{public}s, addr: %{public}p!",
91 tag.c_str(), callBackId_, num_, s_.c_str(), BoolToString(callbackTriggered_).c_str(), this);
92 }
93
94 int32_t callBackId_ = NUM_0;
95 uint32_t num_ = NUM_0;
96 std::string s_;
97 bool callbackTriggered_ = false;
98 };
99 std::mutex g_mtx;
100 std::condition_variable g_condVar;
101 std::vector<std::shared_ptr<MyData>> g_myDataVector;
102 using CallbackFunc = void(*)(OH_ImageReceiverNative*, void*);
103
getJsResult(napi_env env,int result)104 napi_value getJsResult(napi_env env, int result)
105 {
106 napi_value resultNapi = nullptr;
107 napi_create_int32(env, result, &resultNapi);
108 return resultNapi;
109 }
110
Getint32NamedProperty(napi_env env,napi_value object,const char * key)111 static int32_t Getint32NamedProperty(napi_env env, napi_value object, const char* key)
112 {
113 napi_value temp = nullptr;
114 int32_t ulResult = NUM_0;
115 napi_get_named_property(env, object, key, &temp);
116 napi_get_value_int32(env, temp, &ulResult);
117 return ulResult;
118 }
119
GetStringNamedProperty(napi_env env,napi_value object,const char * key)120 static char* GetStringNamedProperty(napi_env env, napi_value object, const char* key)
121 {
122 napi_value temp = nullptr;
123 char* result = nullptr;
124 napi_status status = napi_get_named_property(env, object, key, &temp);
125 size_t uriSize = NUM_0;
126 status = napi_get_value_string_utf8(env, temp, nullptr, NUM_0, &uriSize);
127
128 result = static_cast<char*>(std::malloc(uriSize + NUM_1));
129 if (result == nullptr) {
130 OH_LOG_ERROR(LOG_APP, "Memory allocation failed for property: %{public}s", key);
131 return nullptr;
132 }
133
134 status = napi_get_value_string_utf8(env, temp, result, uriSize + NUM_1, nullptr);
135 if (status != napi_ok) {
136 OH_LOG_ERROR(LOG_APP, "Failed to get string value for property: %{public}s", key);
137 std::free(result);
138 return nullptr;
139 }
140 return result;
141 }
142
GetBoolNamedProperty(napi_env env,napi_value object,const char * key)143 static bool GetBoolNamedProperty(napi_env env, napi_value object, const char* key)
144 {
145 napi_value temp = nullptr;
146 bool ulResult = NUM_0;
147 napi_get_named_property(env, object, key, &temp);
148 napi_get_value_bool(env, temp, &ulResult);
149 return ulResult;
150 }
151
GetMyData(size_t myDataIndex)152 MyData* GetMyData(size_t myDataIndex)
153 {
154 try {
155 return g_myDataVector.at(g_myDataIndex).get();
156 } catch (const std::out_of_range& e) {
157 OH_LOG_ERROR(LOG_APP, "GetMyData error is: %{public}s!", e.what());
158 return nullptr;
159 }
160 }
161
NDKCamera(char * str)162 void NDKCamera(char *str)
163 {
164 Camera_Manager *cameraManager = nullptr;
165 Camera_Device *cameras = nullptr;
166 Camera_OutputCapability *cameraOutputCapability = nullptr;
167 Camera_PreviewOutput *previewOutput = nullptr;
168 const Camera_Profile *previewProfile = nullptr;
169 uint32_t size = NUM_0;
170 uint32_t cameraDeviceIndex = NUM_0;
171 char *previewSurfaceId = str;
172 Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
173 if (cameraManager == nullptr || ret != CAMERA_OK) {
174 OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
175 }
176
177 Camera_Input *cameraInput = nullptr;
178 ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
179 for (int index = NUM_0; index < size; index++) {
180 OH_LOG_INFO(LOG_APP, "cameraId = %{public}s ", cameras[index].cameraId);
181 OH_LOG_INFO(LOG_APP, "cameraPosition = %{public}d ", cameras[index].cameraPosition);
182 OH_LOG_INFO(LOG_APP, "cameraType = %{public}d ", cameras[index].cameraType);
183 OH_LOG_INFO(LOG_APP, "connectionType = %{public}d ", cameras[index].connectionType);
184 }
185 if (cameras == nullptr || size < NUM_0 || ret != CAMERA_OK) {
186 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
187 }
188 ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[NUM_0], &cameraInput);
189 if (cameraInput == nullptr || ret != CAMERA_OK) {
190 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed.ret:%{public}d", ret);
191 }
192
193 ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
194 &cameraOutputCapability);
195 if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
196 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
197 }
198 if (cameraOutputCapability->previewProfilesSize < NUM_0) {
199 OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null");
200 }
201 previewProfile = cameraOutputCapability->previewProfiles[NUM_0];
202 ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceId, &previewOutput);
203 if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) {
204 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed.");
205 }
206
207 ret = OH_CameraInput_Open(cameraInput);
208 if (ret != CAMERA_OK) {
209 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_open failed.");
210 }
211
212 Camera_CaptureSession *captureSession = nullptr;
213 ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
214 if (captureSession == nullptr || ret != CAMERA_OK) {
215 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.");
216 }
217 ret = OH_CaptureSession_SetSessionMode(captureSession, NORMAL_PHOTO);
218 if (ret != CAMERA_OK) {
219 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetSessionMode failed.");
220 }
221
222 ret = OH_CaptureSession_BeginConfig(captureSession);
223 if (ret != CAMERA_OK) {
224 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
225 }
226
227 ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
228 if (ret != CAMERA_OK) {
229 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
230 }
231
232 ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
233 if (ret != CAMERA_OK) {
234 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.");
235 }
236
237 ret = OH_CaptureSession_CommitConfig(captureSession);
238 if (ret != CAMERA_OK) {
239 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
240 }
241
242 ret = OH_CaptureSession_Start(captureSession);
243 if (ret != CAMERA_OK) {
244 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
245 } else {
246 OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Start SUCC!");
247 }
248 return;
249 }
250
ConvertUint64ToChar(uint64_t value)251 std::unique_ptr<char[]> ConvertUint64ToChar(uint64_t value)
252 {
253 std::string strValue = std::to_string(value);
254 auto charBuffer = std::make_unique<char[]>(strValue.size() + 1);
255 std::copy(strValue.begin(), strValue.end(), charBuffer.get());
256 charBuffer[strValue.size()] = '\0';
257
258 return charBuffer;
259 }
260
InitReceiverOptions(OH_ImageReceiverOptions * & options)261 Image_ErrorCode InitReceiverOptions(OH_ImageReceiverOptions *&options)
262 {
263 Image_ErrorCode errCode = OH_ImageReceiverOptions_Create(&options);
264 if (errCode != IMAGE_SUCCESS) {
265 OH_LOG_ERROR(LOG_APP, "InitReceiverOptions create image receiver options failed, errCode: %{public}d.",
266 errCode);
267 return IMAGE_BAD_PARAMETER;
268 }
269
270 Image_Size imgSize;
271 imgSize.width = IMAGE_WIDTH;
272 imgSize.height = IMAGE_HEIGHT;
273 errCode = OH_ImageReceiverOptions_SetSize(options, imgSize);
274 if (errCode != IMAGE_SUCCESS) {
275 OH_LOG_ERROR(LOG_APP, "InitReceiverOptions set image receiver options size failed, errCode: %{public}d.",
276 errCode);
277 OH_ImageReceiverOptions_Release(options);
278 return errCode;
279 }
280
281 errCode = OH_ImageReceiverOptions_SetCapacity(options, IMAGE_CAPACITY);
282 if (errCode != IMAGE_SUCCESS) {
283 OH_LOG_ERROR(LOG_APP, "InitReceiverOptions set image receiver options capacity failed, errCode: %{public}d.",
284 errCode);
285 OH_ImageReceiverOptions_Release(options);
286 return errCode;
287 }
288
289 Image_Size imgSizeRead;
290 errCode = OH_ImageReceiverOptions_GetSize(options, &imgSizeRead);
291 if (errCode != IMAGE_SUCCESS) {
292 OH_LOG_ERROR(LOG_APP, "InitReceiverOptions get image receiver options size failed, errCode: %{public}d.",
293 errCode);
294 OH_ImageReceiverOptions_Release(options);
295 return errCode;
296 }
297
298 if (imgSizeRead.width != IMAGE_WIDTH || imgSizeRead.height != IMAGE_HEIGHT) {
299 OH_LOG_ERROR(LOG_APP, "InitReceiverOptions get image receiver options size failed, width: %{public}d, height:\
300 %{public}d.", imgSizeRead.width, imgSizeRead.height);
301 OH_ImageReceiverOptions_Release(options);
302 return IMAGE_BAD_PARAMETER;
303 }
304
305 int32_t capacity = NUM_0;
306 errCode = OH_ImageReceiverOptions_GetCapacity(options, &capacity);
307 if (errCode != IMAGE_SUCCESS) {
308 OH_LOG_ERROR(LOG_APP, "InitReceiverOptions get image receiver options capacity failed, errCode: %{public}d.",
309 errCode);
310 OH_ImageReceiverOptions_Release(options);
311 return errCode;
312 }
313
314 if (capacity != IMAGE_CAPACITY) {
315 OH_LOG_ERROR(LOG_APP, "InitReceiverOptions get image receiver options capacity failed, capacity: %{public}d.",
316 capacity);
317 OH_ImageReceiverOptions_Release(options);
318 return IMAGE_BAD_PARAMETER;
319 }
320
321 return IMAGE_SUCCESS;
322 }
323
ImageArriveOnCallback_01(OH_ImageReceiverNative * receiver,void * userData)324 static void ImageArriveOnCallback_01(OH_ImageReceiverNative *receiver, void *userData)
325 {
326 OH_LOG_INFO(LOG_APP, "ImageArriveOnCallback_01 IN");
327 std::lock_guard<std::mutex> lock(g_mtx);
328 MyData* myData = static_cast<MyData*>(userData);
329 if (userData != nullptr && myData != nullptr) {
330 OH_LOG_INFO(LOG_APP, "ImageArriveOnCallback_01 triggered successfully.");
331 myData->PrintMyData("before");
332 myData->CallbackModifyCallBackId(NUM_1);
333 myData->CallBackModifyUserData();
334 myData->PrintMyData("after");
335 }
336 g_condVar.notify_all();
337 return;
338 }
339
ImageArriveOnCallback_02(OH_ImageReceiverNative * receiver,void * userData)340 static void ImageArriveOnCallback_02(OH_ImageReceiverNative *receiver, void *userData)
341 {
342 OH_LOG_INFO(LOG_APP, "ImageArriveOnCallback_02 IN");
343 std::lock_guard<std::mutex> lock(g_mtx);
344 MyData* myData = static_cast<MyData*>(userData);
345 if (userData != nullptr && myData != nullptr) {
346 OH_LOG_INFO(LOG_APP, "ImageArriveOnCallback_02 triggered successfully.");
347 myData->PrintMyData("before");
348 myData->CallbackModifyCallBackId(NUM_2);
349 myData->CallBackModifyUserData();
350 myData->PrintMyData("after");
351 }
352 g_condVar.notify_all();
353 return;
354 }
355
ImageArriveOnCallback_03(OH_ImageReceiverNative * receiver,void * userData)356 static void ImageArriveOnCallback_03(OH_ImageReceiverNative *receiver, void *userData)
357 {
358 OH_LOG_INFO(LOG_APP, "ImageArriveOnCallback_03 IN");
359 std::lock_guard<std::mutex> lock(g_mtx);
360 MyData* myData = static_cast<MyData*>(userData);
361 if (userData != nullptr && myData != nullptr) {
362 OH_LOG_INFO(LOG_APP, "ImageArriveOnCallback_03 triggered successfully.");
363 myData->PrintMyData("before");
364 myData->CallbackModifyCallBackId(NUM_3);
365 myData->CallBackModifyUserData();
366 myData->PrintMyData("after");
367 }
368 g_condVar.notify_all();
369 return;
370 }
371
NativeOnCallback(OH_ImageReceiverNative * receiver)372 static void NativeOnCallback(OH_ImageReceiverNative *receiver)
373 {
374 std::lock_guard<std::mutex> lock(g_mtx);
375 MyData *myData = GetMyData(g_myDataIndex);
376
377 OH_LOG_INFO(LOG_APP, "NativeOnCallback triggered successfully.");
378 myData->PrintMyData("before");
379 myData->CallbackModifyCallBackId(NUM_4);
380 myData->CallBackModifyUserData();
381 myData->PrintMyData("after");
382 g_condVar.notify_all();
383 return;
384 }
385
createReceiver(napi_env env,napi_callback_info info)386 static napi_value createReceiver(napi_env env, napi_callback_info info)
387 {
388 OH_ImageReceiverOptions* options = nullptr;
389 Image_ErrorCode errCode = InitReceiverOptions(options);
390 if (errCode != IMAGE_SUCCESS) {
391 OH_LOG_ERROR(LOG_APP, "createReceiver InitReceiverOptions failed, errCode: %{public}d.", errCode);
392 OH_ImageReceiverOptions_Release(options);
393 return getJsResult(env, errCode);
394 }
395
396 OH_ImageReceiverNative *receiver = nullptr;
397 errCode = OH_ImageReceiverNative_Create(options, &receiver);
398 OH_ImageReceiverOptions_Release(options);
399 if (errCode != IMAGE_SUCCESS) {
400 OH_LOG_ERROR(LOG_APP, "createReceiver create image receiver failed, errCode: %{public}d.", errCode);
401 return getJsResult(env, errCode);
402 }
403
404 napi_value result;
405 napi_create_external(env, reinterpret_cast<void *>(receiver), nullptr, nullptr, &result);
406 return result;
407 }
408
nativeOn(napi_env env,napi_callback_info info)409 static napi_value nativeOn(napi_env env, napi_callback_info info)
410 {
411 napi_value result = nullptr;
412 napi_value argValue[NUM_2] = {nullptr};
413 size_t argCount = NUM_2;
414
415 napi_get_undefined(env, &result);
416 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
417 OH_LOG_ERROR(LOG_APP, "nativeOn, the input parameters are smaller than the required parameters");
418 return result;
419 }
420
421 void *ptr = nullptr;
422 napi_get_value_external(env, argValue[NUM_0], &ptr);
423 OH_ImageReceiverNative *receiver = reinterpret_cast<OH_ImageReceiverNative *>(ptr);
424
425 int32_t callBackId = Getint32NamedProperty(env, argValue[NUM_0], MYDATA_KEY_ID);
426 int32_t num = Getint32NamedProperty(env, argValue[NUM_1], MYDATA_KEY_NUM);
427 char *str = GetStringNamedProperty(env, argValue[NUM_1], MYDATA_KEY_STR);
428 bool callbackTriggered = GetBoolNamedProperty(env, argValue[NUM_1], MYDATA_KEY_BOOL);
429 std::shared_ptr<MyData> userData = std::make_shared<MyData>(NUM_0, num, str, callbackTriggered);
430 g_myDataVector.push_back(userData);
431 std::free(str);
432
433 Image_ErrorCode errCode = OH_ImageReceiverNative_On(receiver, NativeOnCallback);
434 if (errCode != IMAGE_SUCCESS) {
435 OH_LOG_ERROR(LOG_APP, "on image receiver on failed, errCode: %{public}d.", errCode);
436 OH_ImageReceiverNative_Release(receiver);
437 return getJsResult(env, errCode);
438 }
439
440 return getJsResult(env, errCode);
441 }
442
imageArriveOn(napi_env env,napi_callback_info info)443 static napi_value imageArriveOn(napi_env env, napi_callback_info info)
444 {
445 napi_value result = nullptr;
446 napi_value argValue[NUM_2] = {nullptr};
447 size_t argCount = NUM_2;
448
449 napi_get_undefined(env, &result);
450 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
451 OH_LOG_ERROR(LOG_APP, "imageArriveOn, the input parameters are smaller than the required parameters");
452 return result;
453 }
454
455 void *ptr = nullptr;
456 napi_get_value_external(env, argValue[NUM_0], &ptr);
457 OH_ImageReceiverNative *receiver = reinterpret_cast<OH_ImageReceiverNative *>(ptr);
458
459 int32_t callBackId = Getint32NamedProperty(env, argValue[NUM_1], MYDATA_KEY_ID);
460 int num = Getint32NamedProperty(env, argValue[NUM_1], MYDATA_KEY_NUM);
461 char *str = GetStringNamedProperty(env, argValue[NUM_1], MYDATA_KEY_STR);
462 bool callbackTriggered = GetBoolNamedProperty(env, argValue[NUM_1], MYDATA_KEY_BOOL);
463 std::shared_ptr<MyData> userData = std::make_shared<MyData>(NUM_0, num, str, callbackTriggered);
464 g_myDataVector.push_back(userData);
465 void* userDataPtr = userData.get();
466 std::free(str);
467
468 CallbackFunc selectedCallback;
469 switch (callBackId) {
470 case NUM_1:
471 selectedCallback = ImageArriveOnCallback_01;
472 break;
473 case NUM_2:
474 selectedCallback = ImageArriveOnCallback_02;
475 break;
476 case NUM_3:
477 selectedCallback = ImageArriveOnCallback_03;
478 break;
479 default:
480 OH_LOG_INFO(LOG_APP, "imageArriveOn invalid callback ID");
481 return getJsResult(env, IMAGE_BAD_PARAMETER);
482 }
483 Image_ErrorCode errCode = OH_ImageReceiverNative_OnImageArrive(receiver, selectedCallback, userDataPtr);
484 if (errCode != IMAGE_SUCCESS) {
485 OH_LOG_ERROR(LOG_APP, "imageArriveOn image receiver on failed, errCode: %{public}d.", errCode);
486 return getJsResult(env, errCode);
487 }
488
489 return getJsResult(env, errCode);
490 }
491
waitForCondition(napi_env env,napi_callback_info info)492 static napi_value waitForCondition(napi_env env, napi_callback_info info)
493 {
494 napi_value result = nullptr;
495 napi_value argValue[NUM_3] = {nullptr};
496 size_t argCount = NUM_3;
497
498 napi_get_undefined(env, &result);
499 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_3) {
500 OH_LOG_ERROR(LOG_APP, "waitForCondition, the input parameters are smaller than the required parameters");
501 return result;
502 }
503
504 int32_t callBackId = NUM_0;
505 napi_get_value_int32(env, argValue[NUM_0], &callBackId);
506 int32_t userNum = NUM_0;
507 napi_get_value_int32(env, argValue[NUM_1], &userNum);
508 napi_get_value_int32(env, argValue[NUM_2], &g_myDataIndex);
509 OH_LOG_INFO(LOG_APP, "waitForCondition callBackId: %{public}d, userNum: %{public}d.", callBackId, userNum);
510 try {
511 MyData *userData = GetMyData(g_myDataIndex);
512 OH_LOG_INFO(LOG_APP, "waitForCondition myDataIndex: %{public}d.", g_myDataIndex);
513 OH_LOG_INFO(LOG_APP, "waitForCondition sleep start!");
514 std::unique_lock<std::mutex> lock(g_mtx);
515 if (!g_condVar.wait_for(lock, std::chrono::seconds(1), [userData, callBackId, userNum] {
516 bool ret = (userData->callbackTriggered_ == true && userData->callBackId_ == callBackId &&
517 userData->num_ == userNum);
518 userData->PrintMyData("waitForCondition userData wait_for");
519 return ret;
520 })) {
521 OH_LOG_ERROR(LOG_APP, "waitForCondition timed out waiting for condition!");
522 return getJsResult(env, IMAGE_SUCCESS);
523 } else {
524 OH_LOG_INFO(LOG_APP, "waitForCondition condition met!");
525 napi_value object = nullptr;
526 napi_create_object(env, &object);
527
528 napi_value temp = nullptr;
529 napi_create_int32(env, userData->callBackId_, &temp);
530 napi_set_named_property(env, object, MYDATA_KEY_ID, temp);
531 napi_create_int32(env, userData->num_, &temp);
532 napi_set_named_property(env, object, MYDATA_KEY_NUM, temp);
533
534 napi_create_string_utf8(env, (const char *)(userData->s_.c_str()), NAPI_AUTO_LENGTH, &temp);
535 napi_set_named_property(env, object, MYDATA_KEY_STR, temp);
536
537 napi_get_boolean(env, userData->callbackTriggered_, &temp);
538 napi_set_named_property(env, object, MYDATA_KEY_BOOL, temp);
539 return object;
540 }
541 } catch (const std::out_of_range& e) {
542 OH_LOG_ERROR(LOG_APP, "waitForCondition error: %{public}s!", e.what());
543 return getJsResult(env, IMAGE_BAD_PARAMETER);
544 }
545 }
546
imageReceiverTest(napi_env env,napi_callback_info info)547 static napi_value imageReceiverTest(napi_env env, napi_callback_info info)
548 {
549 napi_value result = nullptr;
550 napi_value argValue[NUM_1] = {nullptr};
551 size_t argCount = NUM_1;
552
553 napi_get_undefined(env, &result);
554 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
555 OH_LOG_ERROR(LOG_APP, "imageReceiverTest, the input parameters are smaller than the required parameters");
556 return result;
557 }
558
559 void *ptr = nullptr;
560 napi_get_value_external(env, argValue[NUM_0], &ptr);
561 OH_ImageReceiverNative *receiver = reinterpret_cast<OH_ImageReceiverNative *>(ptr);
562
563 uint64_t surfaceID = NUM_0;
564 Image_ErrorCode errCode = OH_ImageReceiverNative_GetReceivingSurfaceId(receiver, &surfaceID);
565 if (errCode != IMAGE_SUCCESS) {
566 OH_LOG_ERROR(LOG_APP, "imageReceiverTest get image receiver surfaceID failed, errCode: %{public}d.",
567 errCode);
568 OH_ImageReceiverNative_Release(receiver);
569 return getJsResult(env, errCode);
570 }
571 OH_LOG_INFO(LOG_APP, "imageReceiverTest get image receiver surfaceID: %{public}llu.", surfaceID);
572 auto surfaceID_c = ConvertUint64ToChar(surfaceID);
573 NDKCamera(surfaceID_c.get());
574 return getJsResult(env, IMAGE_SUCCESS);
575 }
576
nativeOff(napi_env env,napi_callback_info info)577 static napi_value nativeOff(napi_env env, napi_callback_info info)
578 {
579 napi_value result;
580 napi_value argValue[NUM_1] = {nullptr};
581 size_t argCount = NUM_1;
582
583 napi_get_undefined(env, &result);
584 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
585 OH_LOG_ERROR(LOG_APP, "nativeOff, the input parameters are smaller than the required parameters");
586 return result;
587 }
588 void *ptr = nullptr;
589 napi_get_value_external(env, argValue[NUM_0], &ptr);
590 OH_ImageReceiverNative *receiver = reinterpret_cast<OH_ImageReceiverNative *>(ptr);
591
592 Image_ErrorCode errCode = OH_ImageReceiverNative_Off(receiver);
593 if (errCode != IMAGE_SUCCESS) {
594 OH_LOG_ERROR(LOG_APP, "nativeOff image receiver off failed, errCode: %{public}d.", errCode);
595 }
596 return getJsResult(env, errCode);
597 }
598
imageArriveOff(napi_env env,napi_callback_info info)599 static napi_value imageArriveOff(napi_env env, napi_callback_info info)
600 {
601 napi_value result;
602 napi_value argValue[NUM_2] = {nullptr};
603 size_t argCount = NUM_2;
604
605 napi_get_undefined(env, &result);
606 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
607 OH_LOG_ERROR(LOG_APP, "imageArriveOff, the input parameters are smaller than the required parameters");
608 return result;
609 }
610
611 void *ptr = nullptr;
612 napi_get_value_external(env, argValue[NUM_0], &ptr);
613 OH_ImageReceiverNative *receiver = reinterpret_cast<OH_ImageReceiverNative *>(ptr);
614 int32_t callBackId = NUM_0;
615 napi_get_value_int32(env, argValue[NUM_1], &callBackId);
616
617 CallbackFunc selectedCallback;
618 switch (callBackId) {
619 case NUM_1:
620 selectedCallback = ImageArriveOnCallback_01;
621 break;
622 case NUM_2:
623 selectedCallback = ImageArriveOnCallback_02;
624 break;
625 case NUM_3:
626 selectedCallback = ImageArriveOnCallback_03;
627 break;
628 default:
629 selectedCallback = nullptr;
630 }
631 Image_ErrorCode errCode = OH_ImageReceiverNative_OffImageArrive(receiver, selectedCallback);
632 if (errCode != IMAGE_SUCCESS) {
633 OH_LOG_ERROR(LOG_APP, "imageArriveOff image receiver off failed, errCode: %{public}d.", errCode);
634 }
635 return getJsResult(env, errCode);
636 }
637
imageArriveOn_Error(napi_env env,napi_callback_info info)638 static napi_value imageArriveOn_Error(napi_env env, napi_callback_info info)
639 {
640 napi_value result = nullptr;
641 napi_value argValue[NUM_2] = {nullptr};
642 size_t argCount = NUM_2;
643
644 napi_get_undefined(env, &result);
645 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_2) {
646 OH_LOG_ERROR(LOG_APP, "imageArriveOn_Error, the input parameters are smaller than the required parameters");
647 return result;
648 }
649
650 void *ptr = nullptr;
651 napi_get_value_external(env, argValue[NUM_0], &ptr);
652 OH_ImageReceiverNative *receiver = reinterpret_cast<OH_ImageReceiverNative *>(ptr);
653 bool flag = false;
654 napi_get_value_bool(env, argValue[NUM_1], &flag);
655
656 Image_ErrorCode errCode;
657 if (flag) {
658 errCode = OH_ImageReceiverNative_OnImageArrive(nullptr, ImageArriveOnCallback_01, nullptr);
659 } else {
660 errCode = OH_ImageReceiverNative_OnImageArrive(receiver, nullptr, nullptr);
661 }
662 if (errCode != IMAGE_SUCCESS) {
663 OH_LOG_ERROR(LOG_APP, "imageArriveOn_Error image receiver on failed, errCode: %{public}d.", errCode);
664 } else {
665 OH_LOG_INFO(LOG_APP, "imageArriveOn_Error image receiver on SUCC.");
666 }
667 OH_ImageReceiverNative_Release(receiver);
668 return getJsResult(env, errCode);
669 }
670
imageArriveOff_Error(napi_env env,napi_callback_info info)671 static napi_value imageArriveOff_Error(napi_env env, napi_callback_info info)
672 {
673 Image_ErrorCode errCode = OH_ImageReceiverNative_OffImageArrive(nullptr, nullptr);
674 if (errCode != IMAGE_SUCCESS) {
675 OH_LOG_ERROR(LOG_APP, "imageArriveOff_Error image receiver off failed, errCode: %{public}d.", errCode);
676 } else {
677 OH_LOG_INFO(LOG_APP, "imageArriveOff_Error image receiver off succ!");
678 }
679 return getJsResult(env, errCode);
680 }
681
releaseImageReceiver(napi_env env,napi_callback_info info)682 static napi_value releaseImageReceiver(napi_env env, napi_callback_info info)
683 {
684 napi_value result;
685 napi_value argValue[NUM_1] = {nullptr};
686 size_t argCount = NUM_1;
687
688 napi_get_undefined(env, &result);
689 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1) {
690 OH_LOG_ERROR(LOG_APP, "releaseImageReceiver, the input parameters are smaller than the required parameters");
691 return result;
692 }
693 void *ptr = nullptr;
694 napi_get_value_external(env, argValue[NUM_0], &ptr);
695 OH_ImageReceiverNative *receiver = reinterpret_cast<OH_ImageReceiverNative *>(ptr);
696 if (receiver != nullptr) {
697 Image_ErrorCode errCode = OH_ImageReceiverNative_Release(receiver);
698 if (errCode != IMAGE_SUCCESS) {
699 OH_LOG_ERROR(LOG_APP, "releaseImageReceiver release image receiver failed, errCode: %{public}d.", errCode);
700 }
701 g_myDataVector.clear();
702 return getJsResult(env, errCode);
703 }
704 OH_LOG_INFO(LOG_APP, "releaseImageReceiver image receive is nullptr!");
705 g_myDataVector.clear();
706 return getJsResult(env, IMAGE_SUCCESS);
707 }
708
Init(napi_env env,napi_value exports)709 EXTERN_C_START static napi_value Init(napi_env env, napi_value exports)
710 {
711 napi_property_descriptor desc[] = {
712 DECLARE_NAPI_FUNCTION("createReceiver", createReceiver),
713 DECLARE_NAPI_FUNCTION("nativeOn", nativeOn),
714 DECLARE_NAPI_FUNCTION("nativeOff", nativeOff),
715 DECLARE_NAPI_FUNCTION("imageArriveOn", imageArriveOn),
716 DECLARE_NAPI_FUNCTION("imageArriveOff", imageArriveOff),
717 DECLARE_NAPI_FUNCTION("imageReceiverTest", imageReceiverTest),
718 DECLARE_NAPI_FUNCTION("waitForCondition", waitForCondition),
719 DECLARE_NAPI_FUNCTION("imageArriveOn_Error", imageArriveOn_Error),
720 DECLARE_NAPI_FUNCTION("imageArriveOff_Error", imageArriveOff_Error),
721 DECLARE_NAPI_FUNCTION("releaseImageReceiver", releaseImageReceiver)
722 };
723 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
724 return exports;
725 }
726 EXTERN_C_END
727
728 static napi_module demoModule = {
729 .nm_version = 1,
730 .nm_flags = 0,
731 .nm_filename = nullptr,
732 .nm_register_func = Init,
733 .nm_modname = "ArriveOnNdk",
734 .nm_priv = ((void*)0),
735 .reserved = { 0 },
736 };
737
RegisterArriveOnNdkModule(void)738 extern "C" __attribute__((constructor)) void RegisterArriveOnNdkModule(void)
739 {
740 napi_module_register(&demoModule);
741 }