libxcoder 5.6.0
Loading...
Searching...
No Matches
ni_rsrc_priv.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 *
3 * Copyright (C) 2022 NETINT Technologies
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 * SOFTWARE.
19 *
20 ******************************************************************************/
21
22/*!*****************************************************************************
23 * \file ni_rsrc_priv.cpp
24 *
25 * \brief Private definitions used by ni_rsrc_api.cpp for management of
26 * NETINT video processing devices
27 ******************************************************************************/
28
29#include <signal.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <fcntl.h>
33#include <string.h>
34#include <errno.h>
35#include <ctype.h>
36#include <setjmp.h>
37
38#if __linux__ || __APPLE__
39#ifdef __OPENHARMONY__
40#include <sys/ipc.h>
41#include <sys/shm.h>
42#endif
43#include <sys/mman.h>
44#include <sys/stat.h>
45#include <sys/types.h>
46#include <fcntl.h>
47#include <unistd.h>
48#include <dirent.h>
49#include <limits.h>
50#endif
51
52#if __APPLE__
53#include <sys/syslimits.h>
54#endif
55
56#ifdef _ANDROID
57#include "ni_rsrc_api_android.h"
58#endif
59
60#include "ni_device_api.h"
61#include "ni_rsrc_api.h"
62#include "ni_rsrc_priv.h"
63#include "ni_nvme.h"
64#include "ni_log.h"
65#include "ni_util.h"
66
67#if __linux__ || __APPLE__
68#ifndef _ANDROID
69jmp_buf env;
70#endif
71#endif
72
74
75/*!******************************************************************************
76 * \brief
77 *
78 * \param
79 *
80 * \return
81 ******************************************************************************/
83 ni_hw_capability_t* p_hw_cap)
84{
85 int i;
87
88 if (!p_device_info)
89 {
90 ni_log(NI_LOG_ERROR, "ERROR: %s() p_device_info is null\n", __func__);
92 LRETURN;
93 }
94
95 ni_log(NI_LOG_DEBUG, "%s type %d fmt %d\n", __func__, type, fmt);
96
97 for (i = 0; i < EN_CODEC_MAX; i++)
98 {
99 p_device_info->dev_cap[i].supports_codec = EN_INVALID;
100 }
101
102 if (NI_DEVICE_TYPE_DECODER == type)
103 {
104 p_device_info->dev_cap[0].supports_codec = EN_H264;
105 p_device_info->dev_cap[0].max_res_width = p_hw_cap->max_video_width;
106 p_device_info->dev_cap[0].max_res_height = p_hw_cap->max_video_height;
107 p_device_info->dev_cap[0].min_res_width = p_hw_cap->min_video_width;
108 p_device_info->dev_cap[0].min_res_height = p_hw_cap->min_video_height;
109
111 "Baseline, Main, High, High10", (NI_PROFILES_SUPP_STR_LEN-1));
113
114 p_device_info->dev_cap[1].supports_codec = EN_H265;
115 p_device_info->dev_cap[1].max_res_width = p_hw_cap->max_video_width;
116 p_device_info->dev_cap[1].max_res_height = p_hw_cap->max_video_height;
117 p_device_info->dev_cap[1].min_res_width = p_hw_cap->min_video_width;
118 p_device_info->dev_cap[1].min_res_height = p_hw_cap->min_video_height;
119
120 ni_strncpy(p_device_info->dev_cap[1].profiles_supported, NI_PROFILES_SUPP_STR_LEN, "Main, Main10",
123
124 p_device_info->dev_cap[2].supports_codec = EN_JPEG;
125 p_device_info->dev_cap[2].max_res_width = p_hw_cap->max_video_width;
126 p_device_info->dev_cap[2].max_res_height = p_hw_cap->max_video_height;
128 p_device_info->dev_cap[2].min_res_height =
130
131 ni_strncpy(p_device_info->dev_cap[2].profiles_supported, NI_PROFILES_SUPP_STR_LEN, "Baseline",
134
135 p_device_info->dev_cap[3].supports_codec = EN_VP9;
136 p_device_info->dev_cap[3].max_res_width = p_hw_cap->max_video_width;
137 p_device_info->dev_cap[3].max_res_height = p_hw_cap->max_video_height;
138 p_device_info->dev_cap[3].min_res_width = p_hw_cap->min_video_width;
139 p_device_info->dev_cap[3].min_res_height = p_hw_cap->min_video_height;
140
144 } else if (NI_DEVICE_TYPE_ENCODER == type)
145 {
146 p_device_info->dev_cap[0].supports_codec = EN_H264;
147 p_device_info->dev_cap[0].max_res_width = p_hw_cap->max_video_width;
148 p_device_info->dev_cap[0].max_res_height = p_hw_cap->max_video_height;
149 p_device_info->dev_cap[0].min_res_width = p_hw_cap->min_video_width;
150 p_device_info->dev_cap[0].min_res_height = p_hw_cap->min_video_height;
151
153 "Baseline, Main, High, High10", (NI_PROFILES_SUPP_STR_LEN-1));
155
156 p_device_info->dev_cap[1].supports_codec = EN_H265;
157 p_device_info->dev_cap[1].max_res_width = p_hw_cap->max_video_width;
158 p_device_info->dev_cap[1].max_res_height = p_hw_cap->max_video_height;
159 p_device_info->dev_cap[1].min_res_width = p_hw_cap->min_video_width;
160 p_device_info->dev_cap[1].min_res_height = p_hw_cap->min_video_height;
161
162 ni_strncpy(p_device_info->dev_cap[1].profiles_supported, NI_PROFILES_SUPP_STR_LEN, "Main, Main10",
165
166 p_device_info->dev_cap[2].supports_codec = EN_JPEG;
167 p_device_info->dev_cap[2].max_res_width = p_hw_cap->max_video_width;
168 p_device_info->dev_cap[2].max_res_height = p_hw_cap->max_video_height;
169 p_device_info->dev_cap[2].min_res_width = NI_MIN_WIDTH;
170 p_device_info->dev_cap[2].min_res_height = NI_MIN_HEIGHT;
171
175
176 p_device_info->dev_cap[3].supports_codec = EN_AV1;
177 p_device_info->dev_cap[3].max_res_width = NI_PARAM_AV1_MAX_WIDTH;
179 p_device_info->dev_cap[3].min_res_width = p_hw_cap->min_video_width;
180 p_device_info->dev_cap[3].min_res_height = p_hw_cap->min_video_height;
181
185 } else if (NI_DEVICE_TYPE_SCALER == type || NI_DEVICE_TYPE_AI == type)
186 {
187 p_device_info->dev_cap[0].supports_codec =
188 p_device_info->dev_cap[1].supports_codec =
189 p_device_info->dev_cap[2].supports_codec =
190 p_device_info->dev_cap[3].supports_codec = EN_INVALID;
191 }
193END:
194
195 return retval;
196}
197
198/*!******************************************************************************
199 * \brief
200 *
201 * \param
202 *
203 * \return
204 *******************************************************************************/
205int ni_rsrc_strcmp(const void * p_str, const void* p_str1)
206{
207 const char *l = (const char *)p_str;
208 const char *r = (const char *)p_str1;
209 int vl, vr;
210
211 while (!isdigit(*l) && (*l) != '\0')
212 {
213 l++;
214 }
215 while (!isdigit(*r) && (*r) != '\0')
216 {
217 r++;
218 }
219 vl = atoi(l);
220 vr = atoi(r);
221 if (vl == vr)
222 {
223 return 0;
224 }
225 else if (vl < vr)
226 {
227 return -1;
228 }
229 else
230 {
231 return 1;
232 }
233}
234
235/*!******************************************************************************
236 * \brief
237 *
238 * \param
239 *
240 * \return
241 *******************************************************************************/
242void ni_rsrc_get_lock_name(ni_device_type_t device_type, int32_t guid, char* p_name, size_t max_name_len)
243{
244 char type = g_device_type_chr[GET_XCODER_DEVICE_TYPE(device_type)];
245 if (NULL != p_name)
246 {
247 if (strcmp(CODERS_SHM_NAME, "NI_QUADRA_SHM_CODERS") == 0)
248 {
249 snprintf(p_name, max_name_len, "%s/NI_QUADRA_lck_%c%d", LOCK_DIR, type, guid);
250 }
251 else
252 {
253 snprintf(p_name, max_name_len, "%s/NI_lck_%c%d", LOCK_DIR, type, guid);
254 }
255 }
256}
257
258/*!******************************************************************************
259 * \brief
260 *
261 * \param
262 *
263 * \return
264 *******************************************************************************/
265void ni_rsrc_get_shm_name(ni_device_type_t device_type, int32_t guid, char* p_name, size_t max_name_len)
266{
267 char type = g_device_type_chr[GET_XCODER_DEVICE_TYPE(device_type)];
269 if (NULL != p_name)
270 {
271 if (strcmp(CODERS_SHM_NAME, "NI_QUADRA_SHM_CODERS") == 0)
272 {
273 snprintf(p_name, max_name_len, "NI_QUADRA_shm_%c%d", type, guid);
274 }
275 else
276 {
277 snprintf(p_name, max_name_len, "NI_shm_%c%d", type, guid);
278 }
279 }
280}
281
282static void fill_device_info(ni_device_info_t *device_info,
283 const int module_id,
284 const ni_device_handle_t device_handle,
285 const char device_name[NI_MAX_DEVICE_NAME_LEN],
286 const int fw_ver_compat_warning,
287 ni_device_capability_t *device_capability,
288 const ni_device_type_t device_type)
289{
290 ni_hw_capability_t *hw_capability;
291
292 hw_capability = &device_capability->xcoder_devices[device_type];
293
296 device_info->hw_id = hw_capability->hw_id;
297 device_info->module_id = module_id;
298 device_info->fw_ver_compat_warning = fw_ver_compat_warning;
299
300 memcpy(device_info->fw_rev,
301 device_capability->fw_rev,
302 sizeof(device_info->fw_rev));
303 memcpy(device_info->fw_branch_name,
304 device_capability->fw_branch_name,
305 sizeof(device_info->fw_branch_name) - 1);
306 memcpy(device_info->fw_commit_time,
307 device_capability->fw_commit_time,
308 sizeof(device_info->fw_commit_time) - 1);
309 memcpy(device_info->fw_commit_hash,
310 device_capability->fw_commit_hash,
311 sizeof(device_info->fw_commit_hash) - 1);
312 memcpy(device_info->fw_build_time,
313 device_capability->fw_build_time,
314 sizeof(device_info->fw_build_time) - 1);
315 memcpy(device_info->fw_build_id,
316 device_capability->fw_build_id,
317 sizeof(device_info->fw_build_id) - 1);
318 memcpy(device_info->serial_number,
319 device_capability->serial_number,
320 sizeof(device_info->serial_number));
321 memcpy(device_info->model_number,
322 device_capability->model_number,
323 sizeof(device_info->model_number));
324
325 ni_query_fl_fw_versions(device_handle, device_info);
326
327 device_info->max_fps_4k = hw_capability->max_4k_fps;
328 device_info->max_instance_cnt = hw_capability->max_number_of_contexts;
329 device_info->device_type = device_type;
330
331 ni_rsrc_fill_device_info(device_info,
332 (ni_codec_t)hw_capability->codec_format,
333 device_type,
334 hw_capability);
335}
336
337static void fill_shared_memory(ni_device_queue_t *device_queue,
338 const int should_match_rev,
339 const int existing_number_of_devices,
340 const char device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN])
341{
342 int i, j, compatible_device_counter;
343
344 memset(device_queue->xcoder_cnt, 0, sizeof(device_queue->xcoder_cnt));
346 {
347 for (j = 0; j < NI_MAX_DEVICE_CNT; j++)
348 {
349 device_queue->xcoders[i][j] = -1;
350 }
351 }
352
353 compatible_device_counter = 0;
354 for (i = 0; i < existing_number_of_devices; i++)
355 {
356 if (!add_to_shared_memory(device_names[i],
357 false,
358 should_match_rev,
359 device_queue))
360 {
361 continue;
362 }
363 compatible_device_counter++;
364 if (compatible_device_counter >= NI_MAX_DEVICE_CNT)
365 {
367 "Maximum number of supported and compatible devices "
368 "reached. Ignoring other supported and compatible "
369 "devices.\n");
370 break;
371 }
372 }
373}
374
375bool find_available_guid(ni_device_queue_t *device_queue, int device_type, int *guidn)
376{
377 /*
378 Create a mask of all 128 guids, mark used by 1 and unused by 0.
379 */
380 int32_t temp_guid;
381 int32_t i, j;
382 uint32_t guid_mask[4] = {0};
383
384 for (i = 0; i < NI_MAX_DEVICE_CNT; i++)
385 {
386 temp_guid = device_queue->xcoders[device_type][i];
387 if (temp_guid >= 0 && temp_guid < NI_MAX_DEVICE_CNT)
388 {
389 guid_mask[temp_guid / 32] |= (1u << ((uint32_t)temp_guid % 32));
390 }
391 }
392 //from the masks find the first available guidn
393 for (i = 0; i < 4; i++)
394 {
395 for (j = 0; j < 32; j++)
396 {
397 if ((guid_mask[i] & (1u << j)) == 0)
398 {
399 *guidn = (i * 32) + j;
400 return true;
401 }
402 }
403 }
404
405 return false;
406}
407
408bool add_to_shared_memory(const char device_name[NI_MAX_DEVICE_NAME_LEN],
409 const bool device_open_should_succeed,
410 const int should_match_rev,
411 ni_device_queue_t *device_queue)
412{
413 int32_t guid;
414 int i, j, fw_compat_cmp;
415 char fw_api_ver_str[5];
416
417 ni_device_capability_t device_capability = {0};
418 ni_device_handle_t device_handle;
419 ni_device_info_t device_info = {0};
420 bool success = true;
421
422#if __linux__
423 int ret_ts;
424#endif
425
426 device_handle = ni_device_open2(device_name, NI_DEVICE_READ_ONLY);
427 if (device_handle == NI_INVALID_DEVICE_HANDLE)
428 {
429 if (device_open_should_succeed)
430 {
432 "ERROR: %s(): Failed to add %s\n: Failed ni_device_open2()\n",
433 __FUNCTION__,
434 device_name);
435 return false;
436 }
437 return true;
438 }
439
440 if (ni_device_capability_query2(device_handle, &device_capability, false) != NI_RETCODE_SUCCESS)
441 {
442 ni_log(NI_LOG_INFO, "Skipping %s init: unable to query capability\n",
443 device_name);
444 LRETURN;
445 }
446 if (!is_supported_xcoder(device_capability.device_is_xcoder))
447 {
448 ni_log(NI_LOG_INFO, "Skipping %s init: model not supported\n", device_name);
449 LRETURN;
450 }
452 &fw_api_ver_str[0]);
453 if (should_match_rev && \
454 ((uint8_t) device_capability.fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX] != \
456 {
458 "Skipping %s init: device FW v%s incompatible with this version of Libxcoder\n",
459 device_name,
460 fw_api_ver_str);
461 LRETURN;
462 }
463 fw_compat_cmp = ni_cmp_fw_api_ver((char*) &device_capability.fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX],
465
467 {
468 if (!device_capability.xcoder_cnt[i])
469 {
471 "%s %s disabled...\n",
472 device_name,
474 continue;
475 }
476
477 j = device_queue->xcoder_cnt[i];
478 guid = j ? device_queue->xcoders[i][j-1] + 1 : 0;
479 if (guid >= NI_MAX_DEVICE_CNT && !find_available_guid(device_queue, i, &guid))
480 {
481 ni_log(NI_LOG_ERROR, "ERROR: %s(): Skipping %s init: number of "
482 "initialized devices exceeds %d\n", __FUNCTION__,
483 device_name, NI_MAX_DEVICE_CNT);
484 success = false;
485 LRETURN;
486 }
487 device_queue->xcoders[i][j] = guid;
488 device_queue->xcoder_cnt[i]++;
489
490 fill_device_info(&device_info,
491 guid,
492 device_handle,
493 device_name,
494 (should_match_rev && fw_compat_cmp) ? 1 : 0,
495 &device_capability,
497
498 ni_rsrc_get_one_device_info(&device_info);
499 }
500
501 if (fw_compat_cmp < 0) {
502 ni_log(NI_LOG_INFO, "Initialized %s with FW API v%s that is older than "
503 "Libxcoder supported FW API version\n", device_name,
504 fw_api_ver_str);
505 } else if (fw_compat_cmp > 0) {
506 ni_log(NI_LOG_INFO, "Initialized %s with FW API v%s that is newer than "
507 "Libxcoder supported FW API version\n", device_name,
508 fw_api_ver_str);
509 } else {
510 ni_log(NI_LOG_INFO, "Initialized %s\n", device_name, fw_api_ver_str);
511 }
512#if __linux__
513 ret_ts = system("host_ts.sh");
514 if (ret_ts != 0)
515 {
516 ni_log(NI_LOG_ERROR,"Unable to send Host time\n");
517 }
518#endif
519
520END:
521 ni_device_close(device_handle);
522
523 return success;
524}
525
527{
528#ifdef _WIN32
529 return NI_RETCODE_SUCCESS;
530#else
531 size_t i;
532 int retry_shm_fd;
533 for(i = 0; i < sizeof(XCODERS_RETRY_LCK_NAME) / sizeof(XCODERS_RETRY_LCK_NAME[0]); ++i)
534 {
535 retry_shm_fd = open(XCODERS_RETRY_LCK_NAME[i], O_RDWR | O_CREAT | O_CLOEXEC | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
536 if(retry_shm_fd < 0)
537 {
538 if(errno != EEXIST)
539 {
540 char errmsg[NI_ERRNO_LEN] = {0};
542 ni_log(NI_LOG_ERROR, "Failed to create %s ERROR: %s\n", XCODERS_RETRY_LCK_NAME[i], errmsg);
544 }
545 else
546 {
547 ni_log(NI_LOG_DEBUG, "%s already exists\n", XCODERS_RETRY_LCK_NAME[i]);
548 }
549 }
550 else
551 {
552 ni_log(NI_LOG_DEBUG, "Create XCODERS_RETRY_LCK: %s\n", XCODERS_RETRY_LCK_NAME[i]);
553 close(retry_shm_fd);
554 }
555 }
556
557 return NI_RETCODE_SUCCESS;
558
559#endif
560}
561
562#ifdef _WIN32
563
564/*!******************************************************************************
565 * \brief
566 *
567 * \param
568 *
569 * \return
570 *******************************************************************************/
572 char ni_devices[][NI_MAX_DEVICE_NAME_LEN],
573 int max_handles
574)
575{
576 return ni_nvme_enumerate_devices(ni_devices, max_handles);
577}
578
579/*!******************************************************************************
580 * \brief
581 *
582 * \param
583 *
584 * \return
585 *******************************************************************************/
587{
588 char shm_name[32] = { 0 };
589 char lck_name[32] = { 0 };
590 DWORD rc = 0;
591 ni_device_info_t * p_coder_info_map = NULL;
592 HANDLE map_file_handle = NULL;
593 ni_lock_handle_t mutex_handle = NULL;
594 SECURITY_DESCRIPTOR security_descriptor = { 0 };
595
596 if(!p_device_info)
597 {
598 return;
599 }
600
601 ni_rsrc_get_shm_name(p_device_info->device_type, p_device_info->module_id, shm_name, sizeof(shm_name));
602 ni_rsrc_get_lock_name(p_device_info->device_type, p_device_info->module_id, lck_name, sizeof(lck_name));
603 ni_log(NI_LOG_DEBUG, "%s(): shm_name %s, lck_name %s\n", __func__, shm_name, lck_name);
604
605 //Create a mutex for protecting the memory area
606 mutex_handle = CreateMutex(NULL, // default security attributes
607 FALSE, // initially owned
608 lck_name); // unnamed mutex
609 if (NULL == mutex_handle)
610 {
611 ni_log(NI_LOG_ERROR, "CreateMutex error: %d\n", NI_ERRNO);
612 LRETURN;
613 }
614
615 if (WAIT_ABANDONED == WaitForSingleObject(mutex_handle, INFINITE))
616 {
617 ni_log(NI_LOG_ERROR, "ERROR: ni_rsrc_get_device_context() failed to "
618 "obtain mutex: %p\n", mutex_handle);
619 LRETURN;
620 }
621
622 InitializeSecurityDescriptor(&security_descriptor, SECURITY_DESCRIPTOR_REVISION);
623 //security_descriptor.Control
624
625 map_file_handle = CreateFileMapping(
626 INVALID_HANDLE_VALUE, // use paging file
627 NULL, // default security
628 PAGE_READWRITE, // read/write access
629 0, // maximum object size (high-order DWORD)
630 sizeof(ni_device_info_t),// maximum object size (low-order DWORD)
631 (LPCSTR)shm_name // name of mapping object
632 );
633
634 if (NULL == map_file_handle)
635 {
636 rc = NI_ERRNO;
637 ni_log(NI_LOG_ERROR, "ERROR: CreateFileMapping returned (%d) for %s\n",
638 rc, shm_name);
639 LRETURN;
640 }
641 else
642 {
643 rc = NI_ERRNO;
644 if (ERROR_ALREADY_EXISTS == rc)
645 {
646 ni_log(NI_LOG_ERROR, "CreateFileMapping returned existing handle for"
647 " %s\n", shm_name);
648 }
649 else
650 {
651 ni_log(NI_LOG_INFO, "CreateFileMapping created a new mapFile for %s, handle: %p ..\n", shm_name, map_file_handle);
652 }
653 }
654
655 p_coder_info_map = (ni_device_info_t *) MapViewOfFile(
656 map_file_handle, // handle to map object
657 FILE_MAP_ALL_ACCESS, // read/write permission
658 0,
659 0,
660 sizeof(ni_device_info_t)
661 );
662
663 if (NULL == p_coder_info_map)
664 {
665 rc = NI_ERRNO;
666 ni_log(NI_LOG_INFO, "Could not map view of file, p_last error (%d).\n", rc);
667 LRETURN;
668 }
669
670 memcpy(p_coder_info_map, p_device_info, sizeof(ni_device_info_t));
671
672END:
673 if (p_coder_info_map)
674 {
675 UnmapViewOfFile(p_coder_info_map);
676 }
677
678 if (mutex_handle) {
679 ReleaseMutex(mutex_handle);
680 mutex_handle = NULL;
681 }
682}
683
684
685
686
687
688/*!******************************************************************************
689 * \brief
690 *
691 * \param
692 *
693 * \return
694 *******************************************************************************/
695void ni_rsrc_update_record(ni_device_context_t* p_device_context, ni_session_context_t* p_session_context)
696{
697 uint32_t i = 0;
698
699 if ((!p_device_context) || (!p_session_context))
700 {
701 return;
702 }
703
704 p_device_context->p_device_info->load = p_session_context->load_query.current_load;
705 p_device_context->p_device_info->active_num_inst = p_session_context->load_query.total_contexts;
706 // Now we get the model load from the FW
707 p_device_context->p_device_info->model_load = p_session_context->load_query.fw_model_load;
708 if ( 0 == p_device_context->p_device_info->active_num_inst )
709 {
710 p_device_context->p_device_info->load = 0;
711 }
712
713 for (i = 0; i < p_device_context->p_device_info->active_num_inst; i++)
714 {
715 p_device_context->p_device_info->sw_instance[i].id =
716 p_session_context->load_query.context_status[i].context_id;
718 p_session_context->load_query.context_status[i].context_status;
719 p_device_context->p_device_info->sw_instance[i].codec = (ni_codec_t)
720 p_session_context->load_query.context_status[i].codec_format;
721 p_device_context->p_device_info->sw_instance[i].width =
722 p_session_context->load_query.context_status[i].video_width;
723 p_device_context->p_device_info->sw_instance[i].height =
724 p_session_context->load_query.context_status[i].video_height;
725 p_device_context->p_device_info->sw_instance[i].fps =
726 p_session_context->load_query.context_status[i].fps;
727 }
728}
729
730/*!******************************************************************************
731 * \brief Initialize and create all resources required to work with NETINT NVMe
732 * transcoder devices. This is a high level API function which is used
733 * mostly with user application like FFmpeg that relies on those resources.
734 * In case of custom application integration, revised functionality might
735 * be necessary utilizing corresponding API functions.
736 *
737 * \param[in] should_match_rev 0: transcoder firmware revision matching the
738 * library's version is NOT required for placing
739 * the transcoder into resource pool; 1: otherwise
740 * timeout_seconds 0: No timeout amount, loop until init success
741 * or fail; else: timeout will fail init once reached
742 *
743 * \return
744 * NI_RETCODE_SUCCESS on success
745 * NI_RETCODE_FAILURE on failure
746 *
747 *******************************************************************************/
748int ni_rsrc_init_priv(const int should_match_rev,
749 const int existing_number_of_devices,
750 const char existing_device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN])
751{
752 DWORD rc = 0;
753 HANDLE lock = NULL;
754 HANDLE map_file_handle = NULL;
755 ni_device_queue_t* p_device_queue = NULL;
756 map_file_handle = CreateFileMapping(
757 INVALID_HANDLE_VALUE, // use paging file
758 NULL, // default security
759 PAGE_READWRITE, // read/write access
760 0, // maximum object size (high-order DWORD)
761 sizeof(ni_device_queue_t),// maximum object size (low-order DWORD)
762 CODERS_SHM_NAME // name of mapping object
763 );
764
765 if (NULL == map_file_handle)
766 {
767 rc = NI_ERRNO;
768 ni_log(NI_LOG_ERROR, "ERROR: CreateFileMapping returned: %d\n", rc);
769 return NI_RETCODE_FAILURE;
770 }
771 else
772 {
773 rc = NI_ERRNO;
774 if(ERROR_ALREADY_EXISTS == rc)
775 {
776 ni_log(NI_LOG_INFO, "NETINT resources have been initialized already, exiting ..\n");
777 CloseHandle(map_file_handle);
778 return NI_RETCODE_SUCCESS;
779 }
780 else
781 {
782 ni_log(NI_LOG_INFO, "NETINT resources not initialized, starting initialization ..\n");
783 }
784 }
785
786 p_device_queue = (ni_device_queue_t*)MapViewOfFile(
787 map_file_handle, // handle to map object
788 FILE_MAP_ALL_ACCESS, // read/write permission
789 0,
790 0,
791 sizeof(ni_device_queue_t)
792 );
793
794 if (NULL == p_device_queue)
795 {
796 ni_log(NI_LOG_ERROR, "Could not map view of file, p_last error (%d).\n",
797 NI_ERRNO);
798 CloseHandle(map_file_handle);
799 return NI_RETCODE_FAILURE;
800 }
801
802 lock = CreateMutex(NULL, FALSE, CODERS_LCK_NAME);
803 if (NULL == lock)
804 {
805 ni_log(NI_LOG_ERROR, "Init CreateMutex %s failed: %d\n", CODERS_LCK_NAME,
806 NI_ERRNO);
807 UnmapViewOfFile(p_device_queue);
808 CloseHandle(map_file_handle);
809 return NI_RETCODE_FAILURE;
810 }
811
812 if (WAIT_ABANDONED == WaitForSingleObject(lock, INFINITE))
813 {
814 ni_log(NI_LOG_ERROR, "ERROR %d: failed to obtain mutex: %p\n",
815 NI_ERRNO, lock);
816 ReleaseMutex(lock);
817 CloseHandle(lock);
818 UnmapViewOfFile(p_device_queue);
819 CloseHandle(map_file_handle);
820 return NI_RETCODE_FAILURE;
821 }
822
823 fill_shared_memory(p_device_queue,
824 should_match_rev,
825 existing_number_of_devices,
826 existing_device_names);
827
828 UnmapViewOfFile(p_device_queue);
829 ReleaseMutex(lock);
830 CloseHandle(lock);
831 return NI_RETCODE_SUCCESS;
832}
833
834#elif __linux__ || __APPLE__
835
836
837typedef struct {
838int32_t guid;
839char dev_name[NI_MAX_DEVICE_NAME_LEN];
840} guid_name_map_t;
844static bool check_correctness_count(const ni_device_queue_t *existing_device_queue,
845 const int should_match_rev,
846 const int existing_number_of_devices,
847 const char device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN])
848{
849 int i, j, k;
850 uint32_t queue_idx;
851 int32_t guid;
852 bool device_found_in_queue;
853
854 ni_device_capability_t device_capability;
855 ni_device_handle_t device_handle;
856 ni_device_queue_t device_queue;
857 ni_device_context_t *device_context;
858
859 guid_name_map_t guid_map[NI_DEVICE_TYPE_XCODER_MAX][NI_MAX_DEVICE_CNT];
860 uint32_t guid_map_count[NI_DEVICE_TYPE_XCODER_MAX] = {0};
861
862 memset(device_queue.xcoder_cnt, 0, sizeof(device_queue.xcoder_cnt));
863
864 // PHASE 1: GUID to device name mapping for all device types
866 {
867 for (queue_idx = 0; queue_idx < existing_device_queue->xcoder_cnt[j]; queue_idx++)
868 {
869 guid = existing_device_queue->xcoders[j][queue_idx];
870 if (guid < 0 || guid >= NI_MAX_DEVICE_CNT)
871 {
872 continue;
873 }
874 device_context = ni_rsrc_get_device_context((ni_device_type_t)j, guid);
875 if (device_context && device_context->p_device_info)
876 {
877 if (guid_map_count[j] >= NI_MAX_DEVICE_CNT)
878 {
879 ni_log(NI_LOG_ERROR, "ERROR: %s(): guid_map_count[%d] exceeds NI_MAX_DEVICE_CNT\n", __func__, j);
880 ni_rsrc_free_device_context(device_context);
881 return false;
882 }
883 guid_map[j][guid_map_count[j]].guid = guid;
884 ni_strncpy(guid_map[j][guid_map_count[j]].dev_name, NI_MAX_DEVICE_NAME_LEN,
885 device_context->p_device_info->dev_name, NI_MAX_DEVICE_NAME_LEN - 1);
886 guid_map_count[j]++;
887 ni_rsrc_free_device_context(device_context);
888 }
889 else if (device_context)
890 {
891 ni_rsrc_free_device_context(device_context);
892 }
893 }
894 }
895
896 // PHASE 2: Check against pre-built maps
897 for (i = 0; i < existing_number_of_devices; i++)
898 {
899 device_handle = ni_device_open2(device_names[i], NI_DEVICE_READ_ONLY);
900 if (device_handle == NI_INVALID_DEVICE_HANDLE)
901 {
902 continue;
903 }
904 memset(&device_capability, 0, sizeof(ni_device_capability_t));
905 if (ni_device_capability_query2(device_handle, &device_capability, false) != NI_RETCODE_SUCCESS || \
906 !is_supported_xcoder(device_capability.device_is_xcoder) ||
907 (should_match_rev && \
908 ((uint8_t) device_capability.fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX] != \
910 {
911 ni_device_close(device_handle);
912 continue;
913 }
915 {
916 if (device_capability.xcoder_cnt[j] == 0)
917 {
918 continue;
919 }
920 device_found_in_queue = false;
921 for (queue_idx = 0; queue_idx < guid_map_count[j]; queue_idx++)
922 {
923 if (strncmp(device_names[i], guid_map[j][queue_idx].dev_name, NI_MAX_DEVICE_NAME_LEN) == 0)
924 {
925 device_found_in_queue = true;
926 break;
927 }
928 }
929 if (!device_found_in_queue)
930 {
932 {
933 if (k == j)
934 {
935 continue;
936 }
937 for (queue_idx = 0; queue_idx < guid_map_count[k]; queue_idx++)
938 {
939 if (strncmp(device_names[i], guid_map[k][queue_idx].dev_name, NI_MAX_DEVICE_NAME_LEN) == 0)
940 {
942 "ERROR: %s(): Discovered device %s is not in queue for module %s but is in %s\n",
943 __func__,
944 device_names[i],
946 ni_device_close(device_handle);
947 return false;
948 }
949 }
950 }
951 continue;
952 }
953 device_queue.xcoder_cnt[j] += device_capability.xcoder_cnt[j];
954 }
955 ni_device_close(device_handle);
956 }
957
959 {
960 if (device_queue.xcoder_cnt[i] == existing_device_queue->xcoder_cnt[i])
961 {
962 continue;
963 }
965 "WARNING: %s(): Discovered %u %s, expected %u\n",
966 __func__,
967 device_queue.xcoder_cnt[i],
969 existing_device_queue->xcoder_cnt[i]);
970 return false;
971 }
972 return true;
973}
974
975static bool check_device_queue(const ni_device_queue_t *existing_device_queue,
976 const int existing_number_of_devices,
977 const char device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN])
978{
979 int32_t module_id;
980 int i, j;
981
982 ni_device_context_t *device_context;
983
984 for (i = 0; i < existing_number_of_devices; i++)
985 {
987 {
988 module_id = existing_device_queue->xcoders[j][i];
989 if (module_id == -1)
990 {
991 break;
992 }
994 module_id);
995 if (!device_context)
996 {
998 "WARNING: %s(): Missing device context for %s %s\n",
999 __func__,
1000 device_names[i],
1002 return false;
1003 }
1004 if (strcmp(device_context->p_device_info->dev_cap[3].additional_info, "Set") != 0)
1005 {
1006 ni_log(NI_LOG_ERROR, "ERROR %s() device info not populated %s\n", __func__, device_context->p_device_info->dev_cap[3].additional_info);
1007 return false;
1008 }
1009 ni_rsrc_free_device_context(device_context);
1010 }
1011 }
1012
1013 return true;
1014}
1015
1016#ifndef _ANDROID
1017static void sigbus_handler(int signal)
1018{
1019 siglongjmp(env, 1);
1020}
1021
1022static void setup_signal_handler(struct sigaction *p, const int signum)
1023{
1024 struct sigaction c;
1025
1026 memset(&c, 0, sizeof(struct sigaction));
1027 if (sigemptyset(&c.sa_mask) == -1)
1028 {
1030 "ERROR: %s(): Could not initialize signal set: %d\n",
1031 __func__,
1032 NI_ERRNO);
1033 exit(EXIT_FAILURE);
1034 }
1035 c.sa_handler = sigbus_handler;
1036
1037 if (sigaction(signum, NULL, p) == -1)
1038 {
1040 "ERROR: %s(): Could not save previous signal handler: %d\n",
1041 __func__,
1042 NI_ERRNO);
1043 exit(EXIT_FAILURE);
1044 }
1045
1046 if (sigaction(signum, &c, NULL) == -1)
1047 {
1049 "ERROR: %s(): Could not register signal handler: %d\n",
1050 __func__,
1051 NI_ERRNO);
1052 exit(EXIT_FAILURE);
1053 }
1054}
1055#endif
1056
1057static bool check_correctness(const ni_device_queue_t *existing_device_queue,
1058 const int should_match_rev,
1059 const int existing_number_of_devices,
1060 const char device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN])
1061{
1062 bool result = false;
1063#ifndef _ANDROID
1064 const int signum = SIGBUS;
1065 struct sigaction p;
1066
1067 setup_signal_handler(&p, signum);
1068
1069 if (sigsetjmp(env, 1))
1070 {
1071 LRETURN;
1072 }
1073#endif
1074
1075 if (!check_correctness_count(existing_device_queue,
1076 should_match_rev,
1077 existing_number_of_devices,
1078 device_names))
1079 {
1080 LRETURN;
1081 }
1082
1083 if (!check_device_queue(existing_device_queue,
1084 existing_number_of_devices,
1085 device_names))
1086 {
1087 LRETURN;
1088 }
1089
1090 result = true;
1091 ni_log(NI_LOG_INFO, "%s ok\n", CODERS_SHM_NAME);
1092
1093end:
1094#ifndef _ANDROID
1095 if (sigaction(signum, &p, NULL) == -1)
1096 {
1098 "ERROR: %s(): Could not restore previous signal handler: %d\n",
1099 __func__,
1100 NI_ERRNO);
1101 exit(EXIT_FAILURE);
1102 }
1103#endif
1104 return result;
1105}
1106
1107int ni_rsrc_init_priv(const int should_match_rev,
1108 const int existing_number_of_devices,
1109 const char existing_device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN],
1110 int limit_depth)
1111{
1112 int return_value = 0;
1113 int lck_fd = -1;
1114 int shm_fd = -1;
1115 ni_rsrc_shm_state state = NI_RSRC_SHM_IS_INVALID;
1116 int flags = O_CREAT | O_RDWR | O_CLOEXEC;
1117 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1118 ni_device_queue_t *p_device_queue = NULL;
1119
1120 if ((ni_rsrc_try_get_shm_lock(CODERS_LCK_NAME, flags, mode, &lck_fd) < 0) ||
1121 (ni_rsrc_open_shm(CODERS_SHM_NAME, sizeof(ni_device_queue_t), &state, &shm_fd) < 0)) {
1122 return_value = 1;
1123 LRETURN;
1124 }
1125
1126 if ((ni_rsrc_mmap_shm(CODERS_SHM_NAME,
1127 shm_fd,
1128 sizeof(ni_device_queue_t),
1129 (void **)&p_device_queue)) < 0) {
1130 ni_log(NI_LOG_ERROR, "%s(): Failed to ni_rsrc_mmap_shm\n", __func__);
1131 return_value = 1;
1132 LRETURN;
1133 }
1134
1135 //share momery exist
1136 if (NI_RSRC_SHM_IS_EXISTED == state) {
1137 if (check_correctness(p_device_queue,
1138 should_match_rev,
1139 existing_number_of_devices,
1140 existing_device_names))
1141 {
1142 LRETURN;
1143 }
1144
1145 ni_rsrc_munmap_shm((void *)p_device_queue, sizeof(ni_device_queue_t));
1146 ni_log(NI_LOG_DEBUG, "in %s do munmap for %s, shm_flag is O_RDWR\n", __func__, CODERS_SHM_NAME);
1147
1148 if (lockf(lck_fd, F_ULOCK, 0) < 0) {
1149 ni_log(NI_LOG_ERROR, "%s(): Failed to unlock lck_fd for %s\n", __func__, CODERS_SHM_NAME);
1150 }
1151
1152 close(lck_fd);
1153
1154#ifndef __OPENHARMONY__
1155 close(shm_fd);
1156#endif
1157 ni_rsrc_remove_all_shm();
1158
1159 if (limit_depth <= 0)
1160 {
1161 return 1;
1162 }
1163 return ni_rsrc_init_priv(should_match_rev,
1164 existing_number_of_devices,
1165 existing_device_names,
1166 limit_depth - 1);
1167 }
1168
1169 fill_shared_memory(p_device_queue,
1170 should_match_rev,
1171 existing_number_of_devices,
1172 existing_device_names);
1173
1174end:
1175 if (p_device_queue && p_device_queue != MAP_FAILED) {
1176 ni_rsrc_munmap_shm((void *)p_device_queue, sizeof(ni_device_queue_t));
1177 p_device_queue = NULL;
1178 ni_log(NI_LOG_DEBUG, "in %s do munmap for %s\n", __func__, CODERS_SHM_NAME);
1179 }
1180
1181 if (lck_fd != -1) {
1182 if (lockf(lck_fd, F_ULOCK, 0) < 0) {
1183 ni_log(NI_LOG_ERROR, "Will exit from %s(), but failed to unlock lck_fd for %s\n", __func__, CODERS_SHM_NAME);
1184 }
1185
1186 close(lck_fd);
1187 }
1188
1189#ifndef __OPENHARMONY__
1190 if (shm_fd >= 0) {
1191 close(shm_fd);
1192 }
1193#endif
1194
1195 return return_value;
1196}
1197
1198/*!******************************************************************************
1199 * \brief
1200 *
1201 * \param
1202 *
1203 * \return
1204 *******************************************************************************/
1205void ni_rsrc_get_one_device_info (ni_device_info_t * p_device_info)
1206{
1207 int32_t shm_fd = -1;
1208 ni_rsrc_shm_state state = NI_RSRC_SHM_IS_INVALID;
1209 int32_t lock = -1;
1210 int flags = O_CREAT | O_RDWR | O_CLOEXEC;
1211 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1212 char shm_name[32] = { 0 };
1213 char lck_name[32] = { 0 };
1214 ni_device_info_t * p_coder_info_dst = NULL;
1215
1216 if(! p_device_info) {
1217 return;
1218 }
1219
1220 ni_rsrc_get_shm_name(p_device_info->device_type, p_device_info->module_id, shm_name, sizeof(shm_name));
1221 ni_rsrc_get_lock_name(p_device_info->device_type, p_device_info->module_id, lck_name, sizeof(lck_name));
1222
1223 ni_log(NI_LOG_DEBUG, "%s(): shm_name %s, lck_name %s\n", __func__, shm_name, lck_name);
1224
1225 if (ni_rsrc_try_get_shm_lock(lck_name, flags, mode, (int *)&lock) < 0) {
1226 ni_log(NI_LOG_ERROR, "%s: Failed to get lock\n", __func__);
1227 return ;
1228 }
1229
1230 if (ni_rsrc_open_shm(shm_name,
1231 sizeof(ni_device_info_t),
1232 &state,
1233 (int *)&shm_fd) < 0) {
1234 ni_log(NI_LOG_ERROR, "%s: Failed to ni_rsrc_open_shm\n", __func__);
1235 LRETURN;
1236 }
1237
1238 if ((ni_rsrc_mmap_shm(shm_name,
1239 (int)shm_fd,
1240 sizeof(ni_device_info_t),
1241 (void **)&p_coder_info_dst)) < 0) {
1242 ni_log(NI_LOG_ERROR, "%s(): Failed to ni_rsrc_mmap_shm\n", __func__);
1243 LRETURN;
1244 }
1245
1246 memcpy(p_coder_info_dst, p_device_info, sizeof(ni_device_info_t));
1247
1248#ifndef __OPENHARMONY__
1249 if (msync((void*)p_coder_info_dst, sizeof(ni_device_info_t), MS_SYNC | MS_INVALIDATE)) {
1250 char errmsg[NI_ERRNO_LEN] = {0};
1252 ni_log(NI_LOG_ERROR, "ERROR %s() msync() p_coder_info_dst: %s\n",
1253 __func__, errmsg);
1254 } else {
1255 ni_log(NI_LOG_DEBUG, "%s(): written out\n", __func__);
1256 }
1257#endif
1258
1259END:
1260 if (p_coder_info_dst && p_coder_info_dst != MAP_FAILED) {
1261 ni_rsrc_munmap_shm((void *)p_coder_info_dst, sizeof(ni_device_info_t));
1262 p_coder_info_dst = NULL;
1263 ni_log(NI_LOG_DEBUG, "in %s do munmap for %s\n", __func__, shm_name);
1264 }
1265
1266#ifndef __OPENHARMONY__
1267 if (shm_fd >= 0) {
1268 close(shm_fd);
1269 }
1270#endif
1271
1272 if (lockf(lock, F_ULOCK, 0) < 0) {
1273 ni_log(NI_LOG_ERROR, "Will exit from %s(), but failed to unlock lck_fd for %s\n", __func__, shm_name);
1274 }
1275
1276 if (lock >= 0) {
1277 close(lock);
1278 }
1279}
1280
1281/*!******************************************************************************
1282 * \brief
1283 *
1284 * \param
1285 *
1286 * \return
1287 *******************************************************************************/
1288void ni_rsrc_update_record(ni_device_context_t *p_device_context, ni_session_context_t *p_session_context)
1289{
1290 uint32_t j;
1291
1292 if ((!p_device_context) || (!p_session_context))
1293 {
1294 return;
1295 }
1296
1297 p_device_context->p_device_info->load = p_session_context->load_query.current_load;
1298 p_device_context->p_device_info->active_num_inst = p_session_context->load_query.total_contexts;
1299 // Now we get the model load from the FW
1300 p_device_context->p_device_info->model_load = p_session_context->load_query.fw_model_load;
1301 if (0 == p_device_context->p_device_info->active_num_inst)
1302 {
1303 p_device_context->p_device_info->load = 0;
1304 }
1305 for (j = 0; j < p_device_context->p_device_info->active_num_inst; j++)
1306 {
1307 p_device_context->p_device_info->sw_instance[j].id =
1308 p_session_context->load_query.context_status[j].context_id;
1310 p_session_context->load_query.context_status[j]
1312 p_device_context->p_device_info->sw_instance[j].codec = (ni_codec_t)
1313 p_session_context->load_query.context_status[j]
1314 .codec_format;
1315 p_device_context->p_device_info->sw_instance[j].width =
1316 p_session_context->load_query.context_status[j].video_width;
1317 p_device_context->p_device_info->sw_instance[j].height =
1318 p_session_context->load_query.context_status[j].video_height;
1319 p_device_context->p_device_info->sw_instance[j].fps =
1320 p_session_context->load_query.context_status[j].fps;
1321 }
1322 if (msync((void *)p_device_context->p_device_info, sizeof(ni_device_info_t), MS_SYNC | MS_INVALIDATE))
1323 {
1324 char errmsg[NI_ERRNO_LEN] = {0};
1326 ni_log(NI_LOG_ERROR, "ERROR %s() msync() p_device_context->"
1327 "p_device_info: %s\n", __func__, errmsg);
1328 }
1329}
1330
1331
1332/*!******************************************************************************
1333 * \brief get PCIe address information from device name
1334 *
1335 * \param[in] char *device_name e.g. /dev/nvme0n1.
1336 * \param[out] char *pcie e.g. 0000:0a:00.0. Should be at least 13 bytes including null terminator
1337 * \param[out] char *domain, optional. Should be at least 5 bytes including null terminator
1338 * \param[out] char *slot, optional. Should be at least 3 bytes including null terminator
1339 * \param[out] char *dev, optional. Should be at least 3 bytes including null terminator
1340 * \param[out] char *func, optional. Should be at least 2 bytes including null terminator
1341 *
1342 * \return void
1343 * *******************************************************************************/
1344void get_dev_pcie_addr(char *device_name,
1345 char *pcie,
1346 char *domain, char *slot, char *dev, char *func)
1347{
1348#ifndef __linux__
1349 return;
1350#else
1351 int i=0;
1352 char *ptr = NULL;
1353 // path to nvme drive
1354 char path[PATH_MAX];
1355 int ret;
1356
1357 if(!device_name || !strstr(device_name, "/dev/nvme") || !pcie)
1358 {
1359 return ;
1360 }
1361
1362 // we need to skip '/dev/' in the device name
1363 char *start = device_name + 5;
1364
1365 // construct the path to /sys/block
1366 snprintf(path, sizeof(path), "/sys/block/%s", start);
1367 ni_log2(NULL, NI_LOG_DEBUG,"path:%s\n", path);
1368
1369 // read the target of the symbolic link
1370 char target[PATH_MAX];
1371 //e.g.: ../devices/pci0000:00/0000:00:03.1/0000:09:00.0/nvme/nvme0/nvme0n1
1372 ssize_t len = readlink(path, target, sizeof(target) - 1);
1373 if (len == -1) {
1374 perror("readlink");
1375 return;
1376 }
1377 target[len] = '\0'; // set the null-terminating character
1378 ni_log2(NULL, NI_LOG_DEBUG,"target:%s\n", target);
1379
1380 // and find domain and slot from it
1381 char *saveptr = NULL;
1382 ptr = ni_strtok(target, "/", &saveptr);
1383 pcie[4] = pcie[7] = ':';
1384 pcie[10] = '.';
1385 //last pcie info is for the device
1386 while(ptr != NULL) {
1387 ni_log2(NULL, NI_LOG_DEBUG, "===%d ptr:%s\n", ++i, ptr);
1388 if (strlen(ptr) == 12)//e.g.: 0000:09:00.0
1389 {
1390 ret = sscanf(ptr, "%4c:%2c:%2c.%1c", pcie, pcie+5,pcie+8,pcie+11);
1391 if (ret != 4)
1392 {
1393 char errmsg[NI_ERRNO_LEN] = {0};
1395 ni_log2(NULL, NI_LOG_DEBUG, "\tsscanf error %d errno %d %s\n", ret, errno, errmsg);
1396 }
1397 }
1398 ni_log2(NULL, NI_LOG_DEBUG, "=====\n");
1399 ptr = ni_strtok(NULL, "/", &saveptr);
1400 }
1401 pcie[12] = '\0';
1402 ni_log2(NULL, NI_LOG_DEBUG, "PCIE:%s\n", pcie);
1403 if (!domain || !slot || !dev || !func)
1404 {
1405 goto end;
1406 }
1407 domain[4] = slot[2] = dev[2] = func[1] = '\0';
1408 sscanf(pcie, "%4[^:]:%2[^:]:%2[^.].%1s", domain, slot, dev, func);
1409 ni_log2(NULL, NI_LOG_DEBUG, "\t%d: Domain: %s, Slot: %s, Device: %s, Function: %s\n", i, domain, slot, dev, func);
1410end:
1411 return;
1412#endif
1413}
1414
1415/*!******************************************************************************
1416 * \brief try to get lock for specified shared memory
1417 *
1418 * \param[in] const char *lck_name, specified lock name for shared memory
1419 * \param[in] int flags, open lock with specified flags
1420 * \param[in] const mode_t mode, open lock with specified mode
1421 * \param[out] int *lck_fd, Pointer to opened file handle for lock
1422 *
1423 * \return On success
1424 * NI_RETCODE_SUCCESS
1425 * On failure
1426 * NI_RETCODE_INVALID_PARAM
1427 * NI_RETCODE_FAILURE
1428 *******************************************************************************/
1429ni_retcode_t ni_rsrc_try_get_shm_lock(const char *lck_name,
1430 int flags,
1431 const mode_t mode,
1432 int *lck_fd)
1433{
1434 int lock = -1;
1435
1436 if (!lck_name || !lck_fd) {
1437 ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1439 }
1440
1441#ifdef _ANDROID
1442 if (0 != access(LOCK_DIR, F_OK)) {
1443 if (0 != mkdir(LOCK_DIR, S_IRWXU | S_IRWXG | S_IRWXO)) {
1444 ni_log(NI_LOG_ERROR, "ERROR: Could not mkdir : %s directory", LOCK_DIR);
1445 return NI_RETCODE_FAILURE;
1446 }
1447 }
1448#endif
1449
1450 char errmsg[NI_ERRNO_LEN] = {0};
1451 lock = open(lck_name, flags, mode);
1452 if (lock < 0) {
1454 ni_log(NI_LOG_ERROR, "ERROR: %s() open() %s fail: %s\n",
1455 __func__, lck_name, errmsg);
1456 return NI_RETCODE_FAILURE;
1457 }
1458
1459 int retry_cnt = 0;
1460 //use non blocking F_TLOCK in case broken instance has indefinitely locked it
1461 while (lockf(lock, F_TLOCK, 0) != 0)
1462 {
1463 retry_cnt++;
1464 ni_usleep(LOCK_WAIT); //10ms
1465 if (retry_cnt >= 900) //10s
1466 {
1468 ni_log(NI_LOG_ERROR, "ERROR %s() lockf() %s fail: %s\n", __func__, lck_name, errmsg);
1469 ni_log(NI_LOG_ERROR, "ERROR %s() If persists, stop traffic and run rm /dev/shm/NI_*\n", __func__);
1470 close(lock);
1471 return NI_RETCODE_FAILURE;
1472 }
1473 }
1474
1475 *lck_fd = lock;
1476
1477 return NI_RETCODE_SUCCESS;
1478}
1479
1480#if defined(__OPENHARMONY__)
1481static ni_retcode_t openharmony_open_shm(const char *shm_name,
1482 int shm_size,
1483 ni_rsrc_shm_state *state,
1484 int *shm_fd)
1485{
1486 int shm_id = -1;
1487 int flag = IPC_CREAT | IPC_EXCL;
1488 char shm_path[PATH_MAX];
1489
1490 if (!shm_name || !shm_fd) {
1491 ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1493 }
1494
1495 memset(shm_path, 0, PATH_MAX);
1496 snprintf(shm_path, PATH_MAX, "%s/%s", LOCK_DIR, shm_name);
1497
1498 char errmsg[NI_ERRNO_LEN] = {0};
1499 //If file not exist, create the file for ftok
1500 if (0 != access(shm_path, F_OK)) {
1501 int fd = open(shm_path, O_RDWR | O_CREAT | O_CLOEXEC,
1502 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1503 if (fd < 0) {
1505 ni_log(NI_LOG_ERROR, "ERROR: %s() open() %s fail: %s\n", __func__, shm_name, errmsg);
1506 return NI_RETCODE_FAILURE;
1507 }
1508
1509 close(fd);
1510 }
1511
1512 //create unique key for share memory
1513 key_t key = ftok(shm_path, PROJ_ID);
1514 if (key == -1) {
1516 ni_log(NI_LOG_ERROR, "ERROR: %s() ftok() fail: %s\n", __func__, errmsg);
1517 return NI_RETCODE_FAILURE;
1518 }
1519
1520 *state = NI_RSRC_SHM_IS_CREATED;
1521
1522 //create share memory
1523 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1524 shm_id = shmget(key, shm_size, mode | flag);
1525 if (shm_id < 0) {
1526 if (EEXIST == NI_ERRNO) {
1527 *state = NI_RSRC_SHM_IS_EXISTED;
1528 flag = IPC_CREAT;
1529 shm_id = shmget(key, shm_size, mode | flag);
1530 }
1531
1532 if (shm_id < 0) {
1534 ni_log(NI_LOG_ERROR, "ERROR: %s() shmget() fail: %s\n", __func__, errmsg);
1535 return NI_RETCODE_FAILURE;
1536 }
1537 }
1538 *shm_fd = shm_id;
1539
1540 return NI_RETCODE_SUCCESS;
1541}
1542
1543static ni_retcode_t openharmony_remove_shm(const char *shm_name,
1544 int shm_size)
1545{
1546 int shm_id = -1;
1547 char shm_path[PATH_MAX];
1548
1549 if (!shm_name) {
1550 ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1552 }
1553
1554 memset(shm_path, 0, PATH_MAX);
1555 snprintf(shm_path, PATH_MAX, "%s/%s", LOCK_DIR, shm_name);
1556
1557 //If file not exist, exit directly
1558 if (0 != access(shm_path, F_OK)) {
1559 return NI_RETCODE_SUCCESS;
1560 }
1561
1562 char errmsg[NI_ERRNO_LEN] = {0};
1563 //create unique key for share memory
1564 key_t key = ftok(shm_path, PROJ_ID);
1565 if (key == -1) {
1567 ni_log(NI_LOG_ERROR, "ERROR: %s() ftok() fail: %s\n", __func__, errmsg);
1568 return NI_RETCODE_FAILURE;
1569 }
1570
1571 //create share memory
1572 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1573 shm_id = shmget(key, shm_size, mode);
1574 if (shm_id < 0) {
1576 ni_log(NI_LOG_ERROR, "ERROR: %s() shmget() fail: %s\n", __func__, errmsg);
1577 return NI_RETCODE_FAILURE;
1578 }
1579
1580 shmctl(shm_id, IPC_RMID, nullptr);
1581
1582 return NI_RETCODE_SUCCESS;
1583}
1584
1585#elif defined(_ANDROID)
1586static ni_retcode_t android_open_shm(const char *shm_name,
1587 int shm_size,
1588 ni_rsrc_shm_state *state,
1589 int *shm_fd)
1590{
1591 int shm_fd_tmp = -1;
1592
1593 if (!shm_name || !shm_fd) {
1594 ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1596 }
1597
1598 int ret = ni_rsrc_android_init();
1599 if (ret < 0) {
1600 ni_log(NI_LOG_ERROR, "ERROR: %s() failed to get android service\n", __func__);
1601 return NI_RETCODE_FAILURE;
1602 }
1603
1604 *state = NI_RSRC_SHM_IS_CREATED;
1605
1606 string param = shm_name;
1607 Return<void> retvalue =
1608 service->GetAppFlag(param, [&](int32_t ret, hidl_handle handle) {
1609 if (ret > 0) {
1610 *state = NI_RSRC_SHM_IS_EXISTED;
1611 shm_fd_tmp = dup(handle->data[0]);
1612 } else {
1613 ni_log(NI_LOG_ERROR, "ERROR: failed to get shm_fd when call GetAppFlag\n");
1614 }
1615 });
1616
1617 if (!retvalue.isOk()) {
1618 ni_log(NI_LOG_ERROR, "ERROR: %s() maybe something is wrong with Android service\n");
1619 return NI_RETCODE_FAILURE;
1620 }
1621
1622 if (shm_fd_tmp < 0) {
1623 int fd = ashmem_create_region(shm_name, shm_size);
1624 if (fd >= 0) {
1625 native_handle_t *native_handle = native_handle_create(1, 0);
1626 if (!native_handle) {
1627 close(fd);
1628 return NI_RETCODE_FAILURE;
1629 }
1630 native_handle->data[0] = fd;
1631
1632 hidl_handle handle;
1633 handle.setTo(native_handle, true);
1634 service->SetAppFlag(param, handle);
1635 shm_fd_tmp = dup(fd);
1636 } else {
1637 ni_log(NI_LOG_ERROR, "Could not create ashmem under Android\n");
1638 return NI_RETCODE_FAILURE;
1639 }
1640 }
1641 *shm_fd = shm_fd_tmp;
1642
1643 return NI_RETCODE_SUCCESS;
1644}
1645
1646static ni_retcode_t android_remove_shm(const char *shm_name,
1647 int shm_size)
1648{
1649 if (!shm_name) {
1650 ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1652 }
1653
1654 int ret = ni_rsrc_android_init();
1655 if (ret < 0) {
1656 ni_log(NI_LOG_ERROR, "ERROR: %s() failed to get android service\n", __func__);
1657 return NI_RETCODE_FAILURE;
1658 }
1659
1660 string param = shm_name;
1661 Return<void> retvalue = service->RemoveAppFlag(param);
1662 if (!retvalue.isOk()) {
1663 ni_log(NI_LOG_ERROR, "ERROR: %s() failed to remove shm\n", __func__);
1664 return NI_RETCODE_FAILURE;
1665 }
1666
1667 return NI_RETCODE_SUCCESS;
1668}
1669
1670static ni_retcode_t android_remove_all_shm()
1671{
1672 int ret = ni_rsrc_android_init();
1673 if (ret < 0) {
1674 ni_log(NI_LOG_ERROR, "ERROR: %s() failed to get android service\n", __func__);
1675 return NI_RETCODE_FAILURE;
1676 }
1677
1678 Return<void> retvalue = service->RemoveAllAppFlags();
1679 if (!retvalue.isOk()) {
1680 ni_log(NI_LOG_ERROR, "ERROR: %s() failed to remove all shm\n", __func__);
1681 return NI_RETCODE_FAILURE;
1682 }
1683
1684 return NI_RETCODE_SUCCESS;
1685}
1686
1687#else
1688static ni_retcode_t linux_open_shm(const char *shm_name,
1689 int shm_size,
1690 ni_rsrc_shm_state *state,
1691 int *shm_fd)
1692{
1693 int shm_fd_tmp = -1;
1694 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1695 int flag = O_CREAT | O_EXCL | O_RDWR;
1696 bool skip_ftruncate = false;
1697
1698 if (!shm_name || !shm_fd) {
1699 ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1701 }
1702
1703 *state = NI_RSRC_SHM_IS_CREATED;
1704
1705 //create share memory
1706 shm_fd_tmp = shm_open(shm_name, flag, mode);
1707 if (shm_fd_tmp < 0) {
1708 if (EEXIST == NI_ERRNO) {
1709 skip_ftruncate = true;
1710 *state = NI_RSRC_SHM_IS_EXISTED;
1711 flag = O_RDWR;
1712 shm_fd_tmp = shm_open(shm_name, flag, mode);
1713 }
1714
1715 if (shm_fd_tmp < 0) {
1716 char errmsg[NI_ERRNO_LEN] = {0};
1718 ni_log(NI_LOG_ERROR, "ERROR: %s() %s shm_open() fail: %s\n",
1719 __func__, shm_name, errmsg);
1720 return NI_RETCODE_FAILURE;
1721 }
1722 }
1723
1724 //set share memory size
1725 if (!skip_ftruncate && ftruncate(shm_fd_tmp, shm_size) < 0) {
1726 close(shm_fd_tmp);
1727 shm_unlink(shm_name);
1728 return NI_RETCODE_FAILURE;
1729 }
1730
1731 *shm_fd = shm_fd_tmp;
1732
1733 return NI_RETCODE_SUCCESS;
1734}
1735#endif
1736
1737/*!******************************************************************************
1738 * \brief open shared memory for specified shm_name
1739 *
1740 * \param[in] const char *shm_name, specified shared memory name
1741 * \param[in] int shm_size, the size of shared memory
1742 * \param[out] ni_rsrc_shm_state *state, Pointer to the shared memory's state
1743 * if the shared memor isn't created, it will be created at first and
1744 * then open it, param "state" will be set to 0
1745 * if the shared memor has been created yet, only open it,
1746 * param "state" will be set to 1
1747 * \param[out] int *shm_fd, Pointer to opened file handle for shared memory
1748 *
1749 * \return On success
1750 * NI_RETCODE_SUCCESS
1751 * On failure
1752 * NI_RETCODE_INVALID_PARAM
1753 * NI_RETCODE_FAILURE
1754 *******************************************************************************/
1755ni_retcode_t ni_rsrc_open_shm(const char *shm_name,
1756 int shm_size,
1757 ni_rsrc_shm_state *state,
1758 int *shm_fd)
1759{
1760#if defined(__OPENHARMONY__)
1761 return openharmony_open_shm(shm_name, shm_size, state, shm_fd);
1762#elif defined(_ANDROID)
1763 return android_open_shm(shm_name, shm_size, state, shm_fd);
1764#else
1765 return linux_open_shm(shm_name, shm_size, state, shm_fd);
1766#endif
1767}
1768
1769/*!******************************************************************************
1770 * \brief map shared memory to the address space of the calling process
1771 *
1772 * \param[in] const char *shm_name, specified shared memory name
1773 * \param[in] int shm_fd, file handle for shared memory
1774 * \param[in] int shm_size, the size of shared memory
1775 * \param[out] int *shm_addr, pointer to the mapped area
1776 *
1777 * \return On success
1778 * NI_RETCODE_SUCCESS
1779 * On failure
1780 * NI_RETCODE_INVALID_PARAM
1781 * NI_RETCODE_FAILURE
1782 *******************************************************************************/
1783ni_retcode_t ni_rsrc_mmap_shm(const char *shm_name,
1784 int shm_fd,
1785 int shm_size,
1786 void **shm_addr)
1787{
1788 if (!shm_name || !shm_addr) {
1789 ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1791 }
1792
1793 char errmsg[NI_ERRNO_LEN] = {0};
1794#ifdef __OPENHARMONY__
1795 *shm_addr = shmat(shm_fd, nullptr, 0);
1796 if ((void *)(-1) == *shm_addr) {
1798 ni_log(NI_LOG_ERROR, "%s(): %s shmat() fail: %s\n", __func__,
1799 shm_name, errmsg);
1800 return NI_RETCODE_FAILURE;
1801 }
1802#else
1803 *shm_addr = mmap(0, shm_size, PROT_READ | PROT_WRITE,
1804 MAP_SHARED, shm_fd, 0);
1805 if (MAP_FAILED == *shm_addr) {
1807 ni_log(NI_LOG_ERROR, "%s(): %s mmap() fail: %s\n", __func__,
1808 shm_name, errmsg);
1809 return NI_RETCODE_FAILURE;
1810 }
1811#endif
1812
1813 return NI_RETCODE_SUCCESS;
1814}
1815
1816/*!******************************************************************************
1817 * \brief do munmap for shared memory
1818 *
1819 * \param[in] int *shm_addr, pointer to the mapped area
1820 * \param[in] int shm_size, the size of shared memory
1821 *
1822 * \return On success
1823 * NI_RETCODE_SUCCESS
1824 * On failure
1825 * NI_RETCODE_INVALID_PARAM
1826 *******************************************************************************/
1827ni_retcode_t ni_rsrc_munmap_shm(void *shm_addr,
1828 int shm_size)
1829{
1830 if (!shm_addr) {
1831 ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1833 }
1834
1835#ifdef __OPENHARMONY__
1836 shmdt(shm_addr);
1837#else
1838 munmap(shm_addr, shm_size);
1839#endif
1840
1841 return NI_RETCODE_SUCCESS;
1842}
1843
1844/*!******************************************************************************
1845 * \brief delete shared memory
1846 *
1847 * \param[in] const char *shm_name, specified shared memory name
1848 * \param[in] int shm_size, the size of shared memory
1849 *
1850 * \return On success
1851 * NI_RETCODE_SUCCESS
1852 * On failure
1853 * NI_RETCODE_INVALID_PARAM
1854 * NI_RETCODE_FAILURE
1855 *******************************************************************************/
1856ni_retcode_t ni_rsrc_remove_shm(const char *shm_name,
1857 int shm_size)
1858{
1859#ifdef __OPENHARMONY__
1860 return openharmony_remove_shm(shm_name, shm_size);
1861#elif defined(_ANDROID)
1862 return android_remove_shm(shm_name, shm_size);
1863#else
1864 shm_unlink(shm_name);
1865#endif
1866
1867 return NI_RETCODE_SUCCESS;
1868}
1869
1870/*!******************************************************************************
1871 * \brief delete all shared memory
1872 *
1873 *
1874 * \return On success
1875 * NI_RETCODE_SUCCESS
1876 * On failure
1877 * NI_RETCODE_INVALID_PARAM
1878 * NI_RETCODE_FAILURE
1879 *******************************************************************************/
1880ni_retcode_t ni_rsrc_remove_all_shm()
1881{
1882 DIR *dir;
1883 struct dirent *dirent;
1884 char path_to_remove[PATH_MAX];
1885
1886 ni_log(NI_LOG_ERROR, "Deleting shared memory files in %s\n", LOCK_DIR);
1887
1888 char errmsg[NI_ERRNO_LEN] = {0};
1889 dir = opendir(LOCK_DIR);
1890 if (!dir) {
1892 ni_log(NI_LOG_ERROR, "ERROR: %s(): opendir failed for %s: %s\n",
1893 __func__, LOCK_DIR, errmsg);
1894 return NI_RETCODE_FAILURE;
1895 }
1896
1897 while ((dirent = readdir(dir)) != NULL) {
1898 if (strncmp(dirent->d_name, "NI_", 3) != 0) {
1899 continue;
1900 }
1901
1902 snprintf(path_to_remove, PATH_MAX, "%s/%s", LOCK_DIR, dirent->d_name);
1903
1904#ifndef _ANDROID
1905 if (strncasecmp(dirent->d_name, "NI_SHM", 6) == 0) {
1906#ifdef __OPENHARMONY__
1907 openharmony_remove_shm(dirent->d_name, 1); //for created share mem we can ignore size
1908#else
1909 shm_unlink(dirent->d_name);
1910#endif
1911 }
1912#endif
1913
1914 remove(path_to_remove);
1915 }
1916
1917#ifdef _ANDROID
1918 android_remove_all_shm();
1919#endif
1920
1921 if (closedir(dir) == -1) {
1923 ni_log(NI_LOG_ERROR, "ERROR: %s(): closedir failed for %s: %s\n",
1924 __func__, LOCK_DIR, errmsg);
1925 return NI_RETCODE_FAILURE;
1926 }
1927
1928 ni_log(NI_LOG_INFO, "Deleted shared memory files in %s\n", LOCK_DIR);
1929
1930 return NI_RETCODE_SUCCESS;
1931}
1932
1933#endif
#define NI_XCODER_REVISION_API_MAJOR_VER_IDX
Definition ni_defs.h:99
#define NI_MAX_DEVICE_CNT
Definition ni_defs.h:235
#define END
Definition ni_defs.h:338
#define NI_XCODER_REVISION
Definition ni_defs.h:98
#define NI_MAX_DEVICE_NAME_LEN
Definition ni_defs.h:236
ni_device_type_t
Definition ni_defs.h:356
@ NI_DEVICE_TYPE_SCALER
Definition ni_defs.h:362
@ NI_DEVICE_TYPE_AI
Definition ni_defs.h:363
@ NI_DEVICE_TYPE_DECODER
Definition ni_defs.h:360
@ NI_DEVICE_TYPE_ENCODER
Definition ni_defs.h:361
@ NI_DEVICE_TYPE_XCODER_MAX
Definition ni_defs.h:364
#define NI_ERRNO
Definition ni_defs.h:229
#define GET_XCODER_DEVICE_TYPE(t)
Definition ni_defs.h:429
#define GET_XCODER_DEVICE_TYPE_STR(t)
Definition ni_defs.h:433
#define LRETURN
Definition ni_defs.h:337
ni_retcode_t
Definition ni_defs.h:442
@ NI_RETCODE_ERROR_LOCK_DOWN_DEVICE
Definition ni_defs.h:523
@ NI_RETCODE_FAILURE
Definition ni_defs.h:444
@ NI_RETCODE_SUCCESS
Definition ni_defs.h:443
@ NI_RETCODE_INVALID_PARAM
Definition ni_defs.h:445
ni_retcode_t ni_query_fl_fw_versions(ni_device_handle_t device_handle, ni_device_info_t *p_dev_info)
Query firmware loader and firmware versions from the device.
ni_retcode_t ni_device_capability_query2(ni_device_handle_t device_handle, ni_device_capability_t *p_cap, bool device_in_ctxt)
Query device and return device capability structure This function had replaced ni_device_capability_q...
#define atoi(p_str)
void ni_device_close(ni_device_handle_t device_handle)
Close device and release resources.
ni_device_handle_t ni_device_open2(const char *p_dev, ni_device_mode_t mode)
Open device and return device device_handle if successful.
Public definitions for operating NETINT video processing devices for video processing.
#define NI_MIN_HEIGHT
#define NI_MIN_WIDTH
#define NI_PARAM_AV1_MAX_HEIGHT
@ NI_DEVICE_READ_ONLY
#define NI_PARAM_AV1_MAX_WIDTH
#define NI_MIN_RESOLUTION_WIDTH_JPEG
#define MAX_CHAR_IN_DEVICE_NAME
#define NI_MIN_RESOLUTION_HEIGHT_JPEG
void ni_log2(const void *p_context, ni_log_level_t level, const char *fmt,...)
print log message and additional information using ni_log_callback,
Definition ni_log.c:337
void ni_log(ni_log_level_t level, const char *fmt,...)
print log message using ni_log_callback
Definition ni_log.c:183
Logging definitions.
#define NI_ERRNO_LEN
Definition ni_log.h:51
@ NI_LOG_DEBUG
Definition ni_log.h:64
@ NI_LOG_ERROR
Definition ni_log.h:62
@ NI_LOG_INFO
Definition ni_log.h:63
Private definitions for interfacing with NETINT video processing devices over NVMe.
int ni_nvme_enumerate_devices(char ni_devices[][NI_MAX_DEVICE_NAME_LEN], int max_handles)
void ni_rsrc_free_device_context(ni_device_context_t *p_device_context)
Free previously allocated device context.
Public definitions for managing NETINT video processing devices.
#define NI_LEVELS_SUPP_STR_LEN
Definition ni_rsrc_api.h:37
#define NI_PROFILES_SUPP_STR_LEN
Definition ni_rsrc_api.h:36
#define NI_ADDITIONAL_INFO_STR_LEN
Definition ni_rsrc_api.h:38
ni_sw_instance_status_t
Definition ni_rsrc_api.h:62
LIB_API ni_device_context_t * ni_rsrc_get_device_context(ni_device_type_t type, int guid)
Allocates and returns a pointer to ni_device_context_t struct based on provided device_type and guid....
ni_codec_t
Definition ni_rsrc_api.h:48
@ EN_JPEG
Definition ni_rsrc_api.h:53
@ EN_INVALID
Definition ni_rsrc_api.h:49
@ EN_AV1
Definition ni_rsrc_api.h:54
@ EN_H264
Definition ni_rsrc_api.h:50
@ EN_H265
Definition ni_rsrc_api.h:51
@ EN_CODEC_MAX
Definition ni_rsrc_api.h:55
@ EN_VP9
Definition ni_rsrc_api.h:52
Public definitions for managing NETINT video processing devices on Android.
android::sp< INidec > service
int ni_rsrc_android_init()
Init android net.int.SharedBuffer service for binder using.
uint32_t g_xcoder_stop_process
void ni_rsrc_get_shm_name(ni_device_type_t device_type, int32_t guid, char *p_name, size_t max_name_len)
ni_retcode_t ni_rsrc_fill_device_info(ni_device_info_t *p_device_info, ni_codec_t fmt, ni_device_type_t type, ni_hw_capability_t *p_hw_cap)
ni_retcode_t ni_rsrc_create_retry_lck()
void ni_rsrc_get_lock_name(ni_device_type_t device_type, int32_t guid, char *p_name, size_t max_name_len)
bool add_to_shared_memory(const char device_name[NI_MAX_DEVICE_NAME_LEN], const bool device_open_should_succeed, const int should_match_rev, ni_device_queue_t *device_queue)
int ni_rsrc_strcmp(const void *p_str, const void *p_str1)
bool find_available_guid(ni_device_queue_t *device_queue, int device_type, int *guidn)
Private definitions used by ni_rsrc_api.cpp for management of NETINT video processing devices.
#define CODERS_LCK_NAME
void ni_rsrc_update_record(ni_device_context_t *p_device_context, ni_session_context_t *p_session_ctx)
#define CODERS_SHM_NAME
void ni_rsrc_get_one_device_info(ni_device_info_t *p_device_info)
#define LOCK_WAIT
#define LOCK_DIR
int ni_rsrc_enumerate_devices(char ni_devices[][NI_MAX_DEVICE_NAME_LEN], int max_handles)
int ni_rsrc_init_priv(const int should_match_rev, const int existing_number_of_devices, const char device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN], int limit_depth)
void get_dev_pcie_addr(char *device_name, char *pcie, char *domain, char *slot, char *dev, char *func)
ni_retcode_t ni_strncpy(char *dest, size_t dmax, const char *src, size_t slen)
Definition ni_util.c:518
void ni_fmt_fw_api_ver_str(const char ver_str[], char fmt_str[])
Get formatted FW API version string from unformatted FW API version string.
Definition ni_util.c:4272
ni_retcode_t ni_strerror(char *dest, size_t dmax, int errnum)
Definition ni_util.c:656
char * ni_strtok(char *s, const char *delim, char **saveptr)
Definition ni_util.c:424
void ni_usleep(int64_t usec)
Definition ni_util.c:362
int ni_cmp_fw_api_ver(const char ver1[], const char ver2[])
Compare two 3 character strings containing a FW API version. Handle comparision when FW API version f...
Definition ni_util.c:4302
Utility definitions.
device capability type
uint8_t xcoder_cnt[NI_DEVICE_TYPE_XCODER_MAX]
ni_hw_capability_t xcoder_devices[NI_MAX_DEVICES_PER_HW_INSTANCE]
uint8_t fw_branch_name[256]
ni_device_info_t * p_device_info
uint8_t fw_build_id[256]
uint8_t fw_commit_time[26]
uint8_t serial_number[20]
char dev_name[NI_MAX_DEVICE_NAME_LEN]
uint8_t fw_rev[8]
uint8_t fw_commit_hash[41]
char blk_name[NI_MAX_DEVICE_NAME_LEN]
ni_sw_instance_info_t sw_instance[NI_MAX_CONTEXTS_PER_HW_INSTANCE]
ni_device_type_t device_type
uint8_t fw_branch_name[256]
uint8_t model_number[40]
uint32_t active_num_inst
uint8_t fw_build_time[26]
ni_device_video_capability_t dev_cap[EN_CODEC_MAX]
int32_t xcoders[NI_DEVICE_TYPE_XCODER_MAX][NI_MAX_DEVICE_CNT]
Definition ni_rsrc_api.h:70
uint32_t xcoder_cnt[NI_DEVICE_TYPE_XCODER_MAX]
Definition ni_rsrc_api.h:69
char level[NI_LEVELS_SUPP_STR_LEN]
Definition ni_rsrc_api.h:82
char profiles_supported[NI_PROFILES_SUPP_STR_LEN]
Definition ni_rsrc_api.h:81
char additional_info[NI_ADDITIONAL_INFO_STR_LEN]
Definition ni_rsrc_api.h:83
hardware capability type
uint8_t max_number_of_contexts
ni_context_query_t context_status[NI_MAX_CONTEXTS_PER_HW_INSTANCE]
uint32_t fw_model_load
uint32_t current_load
uint32_t total_contexts
ni_load_query_t load_query
ni_sw_instance_status_t status
Definition ni_rsrc_api.h:89