1 /*
2 * Copyright (c) 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 <memory>
17 #include <string>
18 #include <map>
19 #include <uv.h>
20 #include "node_api.h"
21 #include "hilog/log.h"
22 #include "image/image_receiver_native.h"
23 #include "image_receiver_test.h"
24
25 namespace OHOS {
26 namespace Media {
27
28 using namespace std;
29
30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD002B05, "ImageReceiverTest"};
31 static constexpr uint32_t NUM_0 = 0;
32 static constexpr uint32_t NUM_1 = 1;
33 static constexpr uint32_t NUM_2 = 2;
34 static constexpr uint32_t NUM_3 = 3;
35 static constexpr int32_t NUM_1000 = 1000;
36 static constexpr int32_t NUM_2000 = 2000;
37 static constexpr int32_t NUM_3000 = 3000;
38
39 class MyMap {
40 public:
41 /*
42 * constructor
43 */
MyMap(int32_t startCount)44 explicit MyMap(int32_t startCount) : count_(startCount)
45 {
46 }
47
48 /*
49 * destructor
50 */
~MyMap()51 ~MyMap()
52 {
53 }
54
55 /*
56 * insert item
57 */
save(uintptr_t ptr)58 int32_t save(uintptr_t ptr)
59 {
60 for (auto itor = map_.begin(); itor != map_.end(); ++itor) {
61 if (itor->second == ptr) {
62 return itor->first;
63 }
64 }
65 increase();
66 map_.insert(std::pair<int32_t, uintptr_t>(count_, ptr));
67 return count_;
68 }
69
70 /*
71 * find item
72 */
find(int32_t key)73 uintptr_t find(int32_t key)
74 {
75 auto itor = map_.find(key);
76 if (itor == map_.end()) {
77 return 0;
78 }
79 return itor->second;
80 }
81
82 /*
83 * find item
84 */
find(uintptr_t ptr)85 int32_t find(uintptr_t ptr)
86 {
87 for (auto itor = map_.begin(); itor != map_.end(); ++itor) {
88 if (itor->second == ptr) {
89 return itor->first;
90 }
91 }
92 return 0;
93 }
94
95 /*
96 * remove item
97 */
remove(uintptr_t ptr)98 bool remove(uintptr_t ptr)
99 {
100 for (auto itor = map_.begin(); itor != map_.end(); ++itor) {
101 if (itor->second == ptr) {
102 map_.erase(itor);
103 return true;
104 }
105 }
106 return true;
107 }
108
109 /*
110 * remove item
111 */
remove(int32_t key)112 bool remove(int32_t key)
113 {
114 map_.erase(key);
115 return true;
116 }
117
118 /*
119 * check item
120 */
check(uintptr_t ptr)121 bool check(uintptr_t ptr)
122 {
123 for (auto itor = map_.begin(); itor != map_.end(); ++itor) {
124 if (itor->second == ptr) {
125 return true;
126 }
127 }
128 return false;
129 }
130
131 /*
132 * check item
133 */
check(int32_t key)134 bool check(int32_t key)
135 {
136 auto itor = map_.find(key);
137 return itor != map_.end();
138 }
139
140 protected:
increase()141 void increase()
142 {
143 count_++;
144 }
145
146 protected:
147 std::map<int32_t, uintptr_t> map_;
148 int32_t count_;
149 };
150
151 static MyMap receiverMap(NUM_1000);
152 static MyMap imageMap(NUM_2000);
153 static MyMap optionMap(NUM_3000);
154
155 struct ImageReceiverContext {
156 napi_env env = nullptr;
157 napi_ref callbackRef = nullptr;
158 OH_ImageReceiverNative* receiver = nullptr;
159 };
160
Callback(uv_work_t * work,int status)161 static void Callback(uv_work_t *work, int status)
162 {
163 ImageReceiverContext *context = reinterpret_cast<ImageReceiverContext *>(work->data);
164 if (context == nullptr) {
165 HiviewDFX::HiLog::Error(LABEL, "context is empty");
166 } else {
167 napi_value result[1] = {0};
168 napi_value retVal;
169 napi_value callback = nullptr;
170 if (context->env != nullptr && context->callbackRef != nullptr) {
171 napi_handle_scope scope = nullptr;
172 napi_open_handle_scope(context->env, &scope);
173 if (scope == nullptr) {
174 delete context;
175 delete work;
176 return;
177 }
178 int32_t receiverKey = receiverMap.find((uintptr_t)context->receiver);
179 napi_get_reference_value(context->env, context->callbackRef, &callback);
180 napi_create_uint32(context->env, receiverKey, &result[0]);
181 if (callback != nullptr && receiverKey > NUM_1000) {
182 napi_call_function(context->env, nullptr, callback, 1, result, &retVal);
183 } else {
184 HiviewDFX::HiLog::Error(LABEL, "napi_get_reference_value callback is empty");
185 }
186 napi_close_handle_scope(context->env, scope);
187 } else {
188 HiviewDFX::HiLog::Error(LABEL, "env or callbackRef is empty");
189 }
190 }
191 delete context;
192 delete work;
193 }
194
195 class JSImageBufferAvaliableHandler {
196 public:
JSImageBufferAvaliableHandler(napi_env env,napi_ref jsHandler)197 JSImageBufferAvaliableHandler(napi_env env, napi_ref jsHandler)
198 {
199 handler_ = jsHandler;
200 env_ = env;
201 napi_get_uv_event_loop(env_, &loop_);
202 if (loop_ == nullptr) {
203 HiviewDFX::HiLog::Error(LABEL, "napi_get_uv_event_loop failed");
204 return;
205 }
206 }
207
208 ~JSImageBufferAvaliableHandler() = default;
209
OnBufferAvaliable(OH_ImageReceiverNative * receiver)210 static void OnBufferAvaliable(OH_ImageReceiverNative* receiver)
211 {
212 unique_ptr<uv_work_t> work = make_unique<uv_work_t>();
213 if (work == nullptr) {
214 HiviewDFX::HiLog::Error(LABEL, "DoCallBack: No memory");
215 return;
216 }
217 ImageReceiverContext *context = new ImageReceiverContext;
218 context->env = env_;
219 context->callbackRef = handler_;
220 context->receiver = receiver;
221 work->data = reinterpret_cast<void *>(context);
222 int ret = uv_queue_work(loop_, work.get(), [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
223 Callback(work, status);
224 });
225 if (ret != 0) {
226 HiviewDFX::HiLog::Error(LABEL, "Failed to execute DoCallBack work queue");
227 } else {
228 work.release();
229 }
230 }
231
232 private:
233 static napi_ref handler_;
234 static napi_env env_;
235 static uv_loop_s *loop_;
236 };
237
238 napi_ref JSImageBufferAvaliableHandler::handler_ = nullptr;
239 napi_env JSImageBufferAvaliableHandler::env_ = nullptr;
240 uv_loop_s *JSImageBufferAvaliableHandler::loop_ = nullptr;
241
CheckArgs(size_t argc,const napi_value * argv,size_t expectedCount)242 static bool CheckArgs(size_t argc, const napi_value* argv, size_t expectedCount)
243 {
244 if (argc != expectedCount) {
245 return false;
246 }
247 for (size_t i = NUM_0; i < argc; i++) {
248 if (argv[i] == nullptr) {
249 return false;
250 }
251 }
252 return true;
253 }
254
255 template <int NUM>
GetArgs(napi_env env,napi_callback_info info,napi_value (& argv)[NUM],int32_t (& params)[NUM])256 bool GetArgs(napi_env env, napi_callback_info info, napi_value (&argv)[NUM], int32_t (¶ms)[NUM])
257 {
258 size_t argc = NUM;
259 if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok ||
260 !CheckArgs(argc, argv, NUM)) {
261 return false;
262 }
263
264 for (size_t i = NUM_0; i < argc; i++) {
265 if (napi_get_value_int32(env, argv[i], ¶ms[i]) != napi_ok) {
266 return false;
267 }
268 }
269
270 return true;
271 }
272
setInt32NamedProperty(napi_env env,napi_value object,const char * utf8name,int32_t value)273 static void setInt32NamedProperty(napi_env env, napi_value object, const char* utf8name, int32_t value)
274 {
275 napi_value tmp;
276 napi_create_int32(env, value, &tmp);
277 napi_set_named_property(env, object, utf8name, tmp);
278 }
279
JsCreateImageReceiverOptions(napi_env env,napi_callback_info info)280 napi_value ImageReceiverTest::JsCreateImageReceiverOptions(napi_env env, napi_callback_info info)
281 {
282 napi_value udfVar = nullptr;
283 OH_ImageReceiverOptions* options = nullptr;
284 napi_get_undefined(env, &udfVar);
285 Image_ErrorCode nRst = OH_ImageReceiverOptions_Create(&options);
286 int32_t optionKey = 0;
287 if (options != nullptr) {
288 optionKey = optionMap.save((uintptr_t)options);
289 } else {
290 HiviewDFX::HiLog::Error(LABEL, "JsCreateImageReceiverOptions failed to create options");
291 return udfVar;
292 }
293
294 napi_value thisVar = nullptr;
295 napi_value argValue[NUM_3] = {0};
296 size_t argCount = NUM_3;
297 Image_Size size = {0};
298 int32_t capacity = 0;
299 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok) {
300 HiviewDFX::HiLog::Error(LABEL, "JsCreateImageReceiverOptions failed to parse params");
301 return udfVar;
302 }
303 if (!CheckArgs(argCount, argValue, NUM_3) ||
304 napi_get_value_uint32(env, argValue[NUM_0], &(size.width)) != napi_ok ||
305 napi_get_value_uint32(env, argValue[NUM_1], &(size.height)) != napi_ok ||
306 napi_get_value_int32(env, argValue[NUM_2], &(capacity)) != napi_ok) {
307 HiviewDFX::HiLog::Error(LABEL, "JsCreateImageReceiverOptions check params failed");
308 return udfVar;
309 }
310 OH_ImageReceiverOptions_SetSize(options, size);
311 HiviewDFX::HiLog::Error(LABEL, "size: %{public}dx%{public}d", size.width, size.height);
312 OH_ImageReceiverOptions_SetCapacity(options, capacity);
313 HiviewDFX::HiLog::Error(LABEL, "capacity: %{public}d", capacity);
314
315 if (nRst != IMAGE_SUCCESS || options == nullptr || optionKey == 0) {
316 return udfVar;
317 }
318
319 napi_value result = nullptr;
320 napi_create_int32(env, optionKey, &result);
321 return result;
322 }
323
JsImageReceiverOptionsGetSize(napi_env env,napi_callback_info info)324 napi_value ImageReceiverTest::JsImageReceiverOptionsGetSize(napi_env env, napi_callback_info info)
325 {
326 napi_value argv[NUM_1] = {nullptr};
327 int32_t params[NUM_1] = {0};
328 napi_value udfVar = nullptr;
329 napi_get_undefined(env, &udfVar);
330 if (!GetArgs(env, info, argv, params)) {
331 return udfVar;
332 }
333
334 OH_ImageReceiverOptions* options = (OH_ImageReceiverOptions*)optionMap.find(params[NUM_0]);
335 if (options == nullptr) {
336 return udfVar;
337 }
338 Image_Size size;
339 if (OH_ImageReceiverOptions_GetSize(options, &size) != IMAGE_SUCCESS) {
340 return udfVar;
341 }
342
343 napi_value result = nullptr;
344 napi_create_object(env, &result);
345 setInt32NamedProperty(env, result, "width", size.width);
346 setInt32NamedProperty(env, result, "height", size.height);
347 return result;
348 }
349
JsImageReceiverOptionsSetSize(napi_env env,napi_callback_info info)350 napi_value ImageReceiverTest::JsImageReceiverOptionsSetSize(napi_env env, napi_callback_info info)
351 {
352 napi_value argv[NUM_3] = {nullptr};
353 int32_t params[NUM_3] = {0};
354 Image_Size size = {0};
355 napi_value udfVar = nullptr;
356 napi_get_undefined(env, &udfVar);
357 if (!GetArgs(env, info, argv, params)) {
358 return udfVar;
359 }
360
361 OH_ImageReceiverOptions* options = (OH_ImageReceiverOptions*)optionMap.find(params[NUM_0]);
362 if (options == nullptr) {
363 return udfVar;
364 }
365 size.width = params[NUM_1];
366 size.height = params[NUM_2];
367 Image_ErrorCode nRst = OH_ImageReceiverOptions_SetSize(options, size);
368 napi_value result = nullptr;
369 napi_create_int32(env, nRst, &result);
370 return result;
371 }
372
JsImageReceiverOptionsGetCapacity(napi_env env,napi_callback_info info)373 napi_value ImageReceiverTest::JsImageReceiverOptionsGetCapacity(napi_env env, napi_callback_info info)
374 {
375 napi_value argv[NUM_1] = {nullptr};
376 int32_t params[NUM_1] = {0};
377 napi_value udfVar = nullptr;
378 napi_get_undefined(env, &udfVar);
379 if (!GetArgs(env, info, argv, params)) {
380 return udfVar;
381 }
382
383 OH_ImageReceiverOptions* options = (OH_ImageReceiverOptions*)optionMap.find(params[NUM_0]);
384 if (options == nullptr) {
385 return udfVar;
386 }
387 int32_t capacity = 0;
388 if (OH_ImageReceiverOptions_GetCapacity(options, &capacity) != IMAGE_SUCCESS) {
389 return udfVar;
390 }
391
392 napi_value result = nullptr;
393 napi_create_int32(env, capacity, &result);
394 return result;
395 }
396
JsImageReceiverOptionsSetCapacity(napi_env env,napi_callback_info info)397 napi_value ImageReceiverTest::JsImageReceiverOptionsSetCapacity(napi_env env, napi_callback_info info)
398 {
399 napi_value argv[NUM_2] = {nullptr};
400 int32_t params[NUM_2] = {0};
401 napi_value udfVar = nullptr;
402 napi_get_undefined(env, &udfVar);
403 if (!GetArgs(env, info, argv, params)) {
404 return udfVar;
405 }
406
407 OH_ImageReceiverOptions* options = (OH_ImageReceiverOptions*)optionMap.find(params[NUM_0]);
408 if (options == nullptr) {
409 return udfVar;
410 }
411 Image_ErrorCode nRst = OH_ImageReceiverOptions_SetCapacity(options, params[NUM_1]);
412 napi_value result = nullptr;
413 napi_create_int32(env, nRst, &result);
414 return result;
415 }
416
JsReleaseImageReceiverOptions(napi_env env,napi_callback_info info)417 napi_value ImageReceiverTest::JsReleaseImageReceiverOptions(napi_env env, napi_callback_info info)
418 {
419 napi_value argv[NUM_1] = {nullptr};
420 int32_t params[NUM_1] = {0};
421 napi_value udfVar = nullptr;
422 napi_get_undefined(env, &udfVar);
423 if (!GetArgs(env, info, argv, params)) {
424 return udfVar;
425 }
426
427 OH_ImageReceiverOptions* options = (OH_ImageReceiverOptions*)optionMap.find(params[NUM_0]);
428 if (options == nullptr) {
429 return udfVar;
430 }
431 Image_ErrorCode nRst = OH_ImageReceiverOptions_Release(options);
432 optionMap.remove(params[NUM_0]);
433 napi_value result = nullptr;
434 napi_create_int32(env, nRst, &result);
435 return result;
436 }
437
JsCreateImageReceiver(napi_env env,napi_callback_info info)438 napi_value ImageReceiverTest::JsCreateImageReceiver(napi_env env, napi_callback_info info)
439 {
440 napi_value argv[NUM_1] = {nullptr};
441 int32_t params[NUM_1] = {0};
442 napi_value udfVar = nullptr;
443 napi_get_undefined(env, &udfVar);
444 if (!GetArgs(env, info, argv, params)) {
445 return udfVar;
446 }
447
448 OH_ImageReceiverOptions* options = (OH_ImageReceiverOptions*)optionMap.find(params[NUM_0]);
449 if (options == nullptr) {
450 return udfVar;
451 }
452
453 OH_ImageReceiverNative* receiver = nullptr;
454 int32_t receiverKey = 0;
455 Image_ErrorCode nRst = OH_ImageReceiverNative_Create(options, &receiver);
456 if (receiver != nullptr) {
457 receiverKey = receiverMap.save((uintptr_t)receiver);
458 }
459
460 if (nRst != IMAGE_SUCCESS || receiver == nullptr || receiverKey == 0) {
461 HiviewDFX::HiLog::Error(LABEL, "JsCreateImageReceiver failed to create receiver");
462 return udfVar;
463 }
464
465 napi_value result = nullptr;
466 napi_create_int32(env, receiverKey, &result);
467 return result;
468 }
469
JsGetReceivingSurfaceId(napi_env env,napi_callback_info info)470 napi_value ImageReceiverTest::JsGetReceivingSurfaceId(napi_env env, napi_callback_info info)
471 {
472 napi_value argv[NUM_1] = {nullptr};
473 int32_t params[NUM_1] = {0};
474 napi_value udfVar = nullptr;
475 napi_get_undefined(env, &udfVar);
476 if (!GetArgs(env, info, argv, params)) {
477 return udfVar;
478 }
479
480 OH_ImageReceiverNative* receiver = (OH_ImageReceiverNative*)receiverMap.find(params[NUM_0]);
481 if (receiver == nullptr) {
482 return udfVar;
483 }
484 uint64_t id = 0;
485 if (OH_ImageReceiverNative_GetReceivingSurfaceId(receiver, &id) != IMAGE_SUCCESS) {
486 return udfVar;
487 }
488
489 std::string surfaceId = std::to_string(id);
490 napi_value result = nullptr;
491 napi_create_string_utf8(env, surfaceId.c_str(), NAPI_AUTO_LENGTH, &result);
492 return result;
493 }
494
JsReadLatestImage(napi_env env,napi_callback_info info)495 napi_value ImageReceiverTest::JsReadLatestImage(napi_env env, napi_callback_info info)
496 {
497 napi_value argv[NUM_1] = {nullptr};
498 int32_t params[NUM_1] = {0};
499 napi_value udfVar = nullptr;
500 napi_get_undefined(env, &udfVar);
501 if (!GetArgs(env, info, argv, params)) {
502 return udfVar;
503 }
504
505 OH_ImageReceiverNative* receiver = (OH_ImageReceiverNative*)receiverMap.find(params[NUM_0]);
506 if (receiver == nullptr) {
507 return udfVar;
508 }
509 OH_ImageNative* image = nullptr;
510 if (OH_ImageReceiverNative_ReadLatestImage(receiver, &image) != IMAGE_SUCCESS) {
511 return udfVar;
512 }
513 int32_t imageKey = 0;
514 if (image != nullptr) {
515 imageKey = imageMap.save((uintptr_t)image);
516 }
517 if (imageKey == 0) {
518 return udfVar;
519 }
520
521 napi_value result = nullptr;
522 napi_create_int32(env, imageKey, &result);
523 return result;
524 }
525
JsReadNextImage(napi_env env,napi_callback_info info)526 napi_value ImageReceiverTest::JsReadNextImage(napi_env env, napi_callback_info info)
527 {
528 napi_value argv[NUM_1] = {nullptr};
529 int32_t params[NUM_1] = {0};
530 napi_value udfVar = nullptr;
531 napi_get_undefined(env, &udfVar);
532 if (!GetArgs(env, info, argv, params)) {
533 return udfVar;
534 }
535
536 OH_ImageReceiverNative* receiver = (OH_ImageReceiverNative*)receiverMap.find(params[NUM_0]);
537 if (receiver == nullptr) {
538 return udfVar;
539 }
540 OH_ImageNative* image = nullptr;
541 if (OH_ImageReceiverNative_ReadNextImage(receiver, &image) != IMAGE_SUCCESS) {
542 return udfVar;
543 }
544 int32_t imageKey = 0;
545 if (image != nullptr) {
546 imageKey = imageMap.save((uintptr_t)image);
547 }
548 if (imageKey == 0) {
549 return udfVar;
550 }
551
552 napi_value result = nullptr;
553 napi_create_int32(env, imageKey, &result);
554 return result;
555 }
556
JsOn(napi_env env,napi_callback_info info)557 napi_value ImageReceiverTest::JsOn(napi_env env, napi_callback_info info)
558 {
559 napi_value argv[NUM_2] = {nullptr};
560 int32_t params[NUM_2] = {0};
561 napi_value udfVar = nullptr;
562 napi_get_undefined(env, &udfVar);
563
564 napi_value thisVar = nullptr;
565 size_t argCount = NUM_2;
566 if (napi_get_cb_info(env, info, &argCount, argv, &thisVar, nullptr) != napi_ok) {
567 HiviewDFX::HiLog::Error(LABEL, "JsOn failed to parse params");
568 return udfVar;
569 }
570 if (!CheckArgs(argCount, argv, NUM_2) ||
571 napi_get_value_int32(env, argv[NUM_0], &(params[NUM_0])) != napi_ok) {
572 HiviewDFX::HiLog::Error(LABEL, "JsOn failed to parse OH_ImageReceiverNative handle");
573 return udfVar;
574 }
575
576 OH_ImageReceiverNative* receiver = (OH_ImageReceiverNative*)receiverMap.find(params[NUM_0]);
577 if (receiver == nullptr) {
578 return udfVar;
579 }
580 napi_ref jsCallbackHandler;
581 napi_create_reference(env, argv[NUM_1], 1, &(jsCallbackHandler));
582
583 JSImageBufferAvaliableHandler bufferAvaliableHandler(env, jsCallbackHandler);
584 const OH_ImageReceiver_OnCallback callback = bufferAvaliableHandler.OnBufferAvaliable;
585
586 Image_ErrorCode nRst = OH_ImageReceiverNative_On(receiver, callback);
587 napi_value result = nullptr;
588 napi_create_int32(env, nRst, &result);
589 return result;
590 }
591
JsOff(napi_env env,napi_callback_info info)592 napi_value ImageReceiverTest::JsOff(napi_env env, napi_callback_info info)
593 {
594 napi_value argv[NUM_1] = {nullptr};
595 int32_t params[NUM_1] = {0};
596 napi_value udfVar = nullptr;
597 napi_get_undefined(env, &udfVar);
598 if (!GetArgs(env, info, argv, params)) {
599 return udfVar;
600 }
601
602 OH_ImageReceiverNative* receiver = (OH_ImageReceiverNative*)receiverMap.find(params[NUM_0]);
603 if (receiver == nullptr) {
604 return udfVar;
605 }
606 Image_ErrorCode nRst = OH_ImageReceiverNative_Off(receiver);
607 napi_value result = nullptr;
608 napi_create_int32(env, nRst, &result);
609 return result;
610 }
611
JsGetSize(napi_env env,napi_callback_info info)612 napi_value ImageReceiverTest::JsGetSize(napi_env env, napi_callback_info info)
613 {
614 napi_value argv[NUM_1] = {nullptr};
615 int32_t params[NUM_1] = {0};
616 napi_value udfVar = nullptr;
617 napi_get_undefined(env, &udfVar);
618 if (!GetArgs(env, info, argv, params)) {
619 return udfVar;
620 }
621
622 OH_ImageReceiverNative* receiver = (OH_ImageReceiverNative*)receiverMap.find(params[NUM_0]);
623 if (receiver == nullptr) {
624 return udfVar;
625 }
626 Image_Size size;
627 if (OH_ImageReceiverNative_GetSize(receiver, &size) != IMAGE_SUCCESS) {
628 return udfVar;
629 }
630
631 napi_value result = nullptr;
632 napi_create_object(env, &result);
633 setInt32NamedProperty(env, result, "width", size.width);
634 setInt32NamedProperty(env, result, "height", size.height);
635 return result;
636 }
637
JsGetCapacity(napi_env env,napi_callback_info info)638 napi_value ImageReceiverTest::JsGetCapacity(napi_env env, napi_callback_info info)
639 {
640 napi_value argv[NUM_1] = {nullptr};
641 int32_t params[NUM_1] = {0};
642 napi_value udfVar = nullptr;
643 napi_get_undefined(env, &udfVar);
644 if (!GetArgs(env, info, argv, params)) {
645 return udfVar;
646 }
647
648 OH_ImageReceiverNative* receiver = (OH_ImageReceiverNative*)receiverMap.find(params[NUM_0]);
649 if (receiver == nullptr) {
650 return udfVar;
651 }
652 int32_t capacity = 0;
653 if (OH_ImageReceiverNative_GetCapacity(receiver, &capacity) != IMAGE_SUCCESS) {
654 return udfVar;
655 }
656
657 napi_value result = nullptr;
658 napi_create_int32(env, capacity, &result);
659 return result;
660 }
661
JsReleaseImageReceiver(napi_env env,napi_callback_info info)662 napi_value ImageReceiverTest::JsReleaseImageReceiver(napi_env env, napi_callback_info info)
663 {
664 napi_value argv[NUM_1] = {nullptr};
665 int32_t params[NUM_1] = {0};
666 napi_value udfVar = nullptr;
667 napi_get_undefined(env, &udfVar);
668 if (!GetArgs(env, info, argv, params)) {
669 return udfVar;
670 }
671
672 OH_ImageReceiverNative* receiver = (OH_ImageReceiverNative*)receiverMap.find(params[NUM_0]);
673 if (receiver == nullptr) {
674 return udfVar;
675 }
676 Image_ErrorCode nRst = OH_ImageReceiverNative_Release(receiver);
677 receiverMap.remove(params[NUM_0]);
678 napi_value result = nullptr;
679 napi_create_int32(env, nRst, &result);
680 return result;
681 }
682
JsGetImageSize(napi_env env,napi_callback_info info)683 napi_value ImageReceiverTest::JsGetImageSize(napi_env env, napi_callback_info info)
684 {
685 napi_value argv[NUM_1] = {nullptr};
686 int32_t params[NUM_1] = {0};
687 napi_value udfVar = nullptr;
688 napi_get_undefined(env, &udfVar);
689 if (!GetArgs(env, info, argv, params)) {
690 return udfVar;
691 }
692
693 OH_ImageNative* image = (OH_ImageNative*)imageMap.find(params[NUM_0]);
694 if (image == nullptr) {
695 return udfVar;
696 }
697 Image_Size size;
698 if (OH_ImageNative_GetImageSize(image, &size) != IMAGE_SUCCESS) {
699 return udfVar;
700 }
701
702 napi_value result = nullptr;
703 napi_create_object(env, &result);
704 setInt32NamedProperty(env, result, "width", size.width);
705 setInt32NamedProperty(env, result, "height", size.height);
706 return result;
707 }
708
JsGetImageComponentTypes(napi_env env,napi_callback_info info)709 napi_value ImageReceiverTest::JsGetImageComponentTypes(napi_env env, napi_callback_info info)
710 {
711 napi_value argv[NUM_1] = {nullptr};
712 int32_t params[NUM_1] = {0};
713 napi_value udfVar = nullptr;
714 napi_get_undefined(env, &udfVar);
715 if (!GetArgs(env, info, argv, params)) {
716 return udfVar;
717 }
718
719 OH_ImageNative* image = (OH_ImageNative*)imageMap.find(params[NUM_0]);
720 if (image == nullptr) {
721 return udfVar;
722 }
723 size_t typeSize = 0;
724 if (OH_ImageNative_GetComponentTypes(image, nullptr, &typeSize) != IMAGE_SUCCESS) {
725 return udfVar;
726 }
727
728 uint32_t* arrayTypes = nullptr;
729 napi_value result = nullptr;
730 napi_create_array(env, &result);
731 if (typeSize <= 0) {
732 return result;
733 } else {
734 arrayTypes = new uint32_t[typeSize];
735 }
736
737 if (arrayTypes == nullptr) {
738 return result;
739 }
740
741 if (OH_ImageNative_GetComponentTypes(image, &arrayTypes, &typeSize) != IMAGE_SUCCESS) {
742 delete [] arrayTypes;
743 return result;
744 }
745
746 for (size_t i = 0; i < typeSize; i++) {
747 napi_value type = nullptr;
748 napi_create_uint32(env, arrayTypes[i], &type);
749 napi_set_element(env, result, i, type);
750 }
751
752 delete [] arrayTypes;
753 return result;
754 }
755
JsGetImageByteBuffer(napi_env env,napi_callback_info info)756 napi_value ImageReceiverTest::JsGetImageByteBuffer(napi_env env, napi_callback_info info)
757 {
758 napi_value argv[NUM_2] = {nullptr};
759 int32_t params[NUM_2] = {0};
760 napi_value udfVar = nullptr;
761 napi_get_undefined(env, &udfVar);
762 if (!GetArgs(env, info, argv, params)) {
763 return udfVar;
764 }
765
766 OH_ImageNative* image = (OH_ImageNative*)imageMap.find(params[NUM_0]);
767 if (image == nullptr) {
768 return udfVar;
769 }
770 uint32_t componentType = params[NUM_1];
771 OH_NativeBuffer* nativeBuffer = nullptr;
772 if (OH_ImageNative_GetByteBuffer(image, componentType, &nativeBuffer) != IMAGE_SUCCESS) {
773 return udfVar;
774 }
775
776 napi_value result = nullptr;
777 napi_create_int32(env, (int32_t)(uintptr_t)nativeBuffer, &result);
778 return result;
779 }
780
JsGetImageBufferSize(napi_env env,napi_callback_info info)781 napi_value ImageReceiverTest::JsGetImageBufferSize(napi_env env, napi_callback_info info)
782 {
783 napi_value argv[NUM_2] = {nullptr};
784 int32_t params[NUM_2] = {0};
785 napi_value udfVar = nullptr;
786 napi_get_undefined(env, &udfVar);
787 if (!GetArgs(env, info, argv, params)) {
788 return udfVar;
789 }
790
791 OH_ImageNative* image = (OH_ImageNative*)imageMap.find(params[NUM_0]);
792 if (image == nullptr) {
793 return udfVar;
794 }
795 uint32_t componentType = params[NUM_1];
796 size_t bufferSize = 0;
797 if (OH_ImageNative_GetBufferSize(image, componentType, &bufferSize) != IMAGE_SUCCESS) {
798 return udfVar;
799 }
800
801 napi_value result = nullptr;
802 napi_create_int32(env, bufferSize, &result);
803 return result;
804 }
805
JsGetImageRowStride(napi_env env,napi_callback_info info)806 napi_value ImageReceiverTest::JsGetImageRowStride(napi_env env, napi_callback_info info)
807 {
808 napi_value argv[NUM_2] = {nullptr};
809 int32_t params[NUM_2] = {0};
810 napi_value udfVar = nullptr;
811 napi_get_undefined(env, &udfVar);
812 if (!GetArgs(env, info, argv, params)) {
813 return udfVar;
814 }
815
816 OH_ImageNative* image = (OH_ImageNative*)imageMap.find(params[NUM_0]);
817 if (image == nullptr) {
818 return udfVar;
819 }
820 uint32_t componentType = params[NUM_1];
821 int32_t rowStride = 0;
822 if (OH_ImageNative_GetRowStride(image, componentType, &rowStride) != IMAGE_SUCCESS) {
823 return udfVar;
824 }
825
826 napi_value result = nullptr;
827 napi_create_int32(env, rowStride, &result);
828 return result;
829 }
830
JsGetImagePixelStride(napi_env env,napi_callback_info info)831 napi_value ImageReceiverTest::JsGetImagePixelStride(napi_env env, napi_callback_info info)
832 {
833 napi_value argv[NUM_2] = {nullptr};
834 int32_t params[NUM_2] = {0};
835 napi_value udfVar = nullptr;
836 napi_get_undefined(env, &udfVar);
837 if (!GetArgs(env, info, argv, params)) {
838 return udfVar;
839 }
840
841 OH_ImageNative* image = (OH_ImageNative*)imageMap.find(params[NUM_0]);
842 if (image == nullptr) {
843 return udfVar;
844 }
845 uint32_t componentType = params[NUM_1];
846 int32_t pixelStride = 0;
847 if (OH_ImageNative_GetPixelStride(image, componentType, &pixelStride) != IMAGE_SUCCESS) {
848 return udfVar;
849 }
850
851 napi_value result = nullptr;
852 napi_create_int32(env, pixelStride, &result);
853 return result;
854 }
855
JsReleaseImage(napi_env env,napi_callback_info info)856 napi_value ImageReceiverTest::JsReleaseImage(napi_env env, napi_callback_info info)
857 {
858 napi_value argv[NUM_1] = {nullptr};
859 int32_t params[NUM_1] = {0};
860 napi_value udfVar = nullptr;
861 napi_get_undefined(env, &udfVar);
862 if (!GetArgs(env, info, argv, params)) {
863 return udfVar;
864 }
865
866 OH_ImageNative* image = (OH_ImageNative*)imageMap.find(params[NUM_0]);
867 if (image == nullptr) {
868 return udfVar;
869 }
870 Image_ErrorCode nRst = OH_ImageNative_Release(image);
871 imageMap.remove(params[NUM_0]);
872 napi_value result = nullptr;
873 napi_create_int32(env, nRst, &result);
874 return result;
875 }
876
877 EXTERN_C_START
Init(napi_env env,napi_value exports)878 static napi_value Init(napi_env env, napi_value exports)
879 {
880 napi_property_descriptor desc[] = {
881 { "JsCreateImageReceiverOptions", nullptr, ImageReceiverTest::JsCreateImageReceiverOptions,
882 nullptr, nullptr, nullptr, napi_static, nullptr },
883 { "JsImageReceiverOptionsGetSize", nullptr, ImageReceiverTest::JsImageReceiverOptionsGetSize,
884 nullptr, nullptr, nullptr, napi_static, nullptr },
885 { "JsImageReceiverOptionsSetSize", nullptr, ImageReceiverTest::JsImageReceiverOptionsSetSize,
886 nullptr, nullptr, nullptr, napi_static, nullptr },
887 { "JsImageReceiverOptionsGetCapacity", nullptr, ImageReceiverTest::JsImageReceiverOptionsGetCapacity,
888 nullptr, nullptr, nullptr, napi_static, nullptr },
889 { "JsImageReceiverOptionsSetCapacity", nullptr, ImageReceiverTest::JsImageReceiverOptionsSetCapacity,
890 nullptr, nullptr, nullptr, napi_static, nullptr },
891 { "JsReleaseImageReceiverOptions", nullptr, ImageReceiverTest::JsReleaseImageReceiverOptions,
892 nullptr, nullptr, nullptr, napi_static, nullptr },
893 { "JsCreateImageReceiver", nullptr, ImageReceiverTest::JsCreateImageReceiver,
894 nullptr, nullptr, nullptr, napi_static, nullptr },
895 { "JsGetReceivingSurfaceId", nullptr, ImageReceiverTest::JsGetReceivingSurfaceId,
896 nullptr, nullptr, nullptr, napi_static, nullptr },
897 { "JsReadLatestImage", nullptr, ImageReceiverTest::JsReadLatestImage,
898 nullptr, nullptr, nullptr, napi_static, nullptr },
899 { "JsReadNextImage", nullptr, ImageReceiverTest::JsReadNextImage,
900 nullptr, nullptr, nullptr, napi_static, nullptr },
901 { "JsOn", nullptr, ImageReceiverTest::JsOn,
902 nullptr, nullptr, nullptr, napi_static, nullptr },
903 { "JsOff", nullptr, ImageReceiverTest::JsOff,
904 nullptr, nullptr, nullptr, napi_static, nullptr },
905 { "JsGetSize", nullptr, ImageReceiverTest::JsGetSize,
906 nullptr, nullptr, nullptr, napi_static, nullptr },
907 { "JsGetCapacity", nullptr, ImageReceiverTest::JsGetCapacity,
908 nullptr, nullptr, nullptr, napi_static, nullptr },
909 { "JsReleaseImageReceiver", nullptr, ImageReceiverTest::JsReleaseImageReceiver,
910 nullptr, nullptr, nullptr, napi_static, nullptr },
911 { "JsGetImageSize", nullptr, ImageReceiverTest::JsGetImageSize,
912 nullptr, nullptr, nullptr, napi_static, nullptr },
913 { "JsGetImageComponentTypes", nullptr, ImageReceiverTest::JsGetImageComponentTypes,
914 nullptr, nullptr, nullptr, napi_static, nullptr },
915 { "JsGetImageByteBuffer", nullptr, ImageReceiverTest::JsGetImageByteBuffer,
916 nullptr, nullptr, nullptr, napi_static, nullptr },
917 { "JsGetImageBufferSize", nullptr, ImageReceiverTest::JsGetImageBufferSize,
918 nullptr, nullptr, nullptr, napi_static, nullptr },
919 { "JsGetImageRowStride", nullptr, ImageReceiverTest::JsGetImageRowStride,
920 nullptr, nullptr, nullptr, napi_static, nullptr },
921 { "JsGetImagePixelStride", nullptr, ImageReceiverTest::JsGetImagePixelStride,
922 nullptr, nullptr, nullptr, napi_static, nullptr },
923 { "JsReleaseImage", nullptr, ImageReceiverTest::JsReleaseImage,
924 nullptr, nullptr, nullptr, napi_static, nullptr },
925 };
926 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
927 return exports;
928 }
929 EXTERN_C_END
930
931 static napi_module demoModule = {
932 .nm_version = 1,
933 .nm_flags = 0,
934 .nm_filename = nullptr,
935 .nm_register_func = Init,
936 .nm_modname = "entry",
937 .nm_priv = ((void *)0),
938 .reserved = {0},
939 };
940
RegisterEntryModule(void)941 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
942 {
943 napi_module_register(&demoModule);
944 }
945
946 } // namespace Media
947 } // namespace OHOS
948