libxcoder  5.3.1
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
69 jmp_buf env;
70 #endif
71 #endif
72 
73 uint32_t g_xcoder_stop_process = 0;
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__);
91  retval = NI_RETCODE_INVALID_PARAM;
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 
110  strncpy(p_device_info->dev_cap[0].profiles_supported,
111  "Baseline, Main, High, High10", NI_PROFILES_SUPP_STR_LEN);
112  strncpy(p_device_info->dev_cap[0].level, "6.2", NI_LEVELS_SUPP_STR_LEN);
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  strncpy(p_device_info->dev_cap[1].profiles_supported, "Main, Main10",
122  strncpy(p_device_info->dev_cap[1].level, "6.2", NI_LEVELS_SUPP_STR_LEN);
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  strncpy(p_device_info->dev_cap[2].profiles_supported, "Baseline",
133  strncpy(p_device_info->dev_cap[2].level, "6.2", NI_LEVELS_SUPP_STR_LEN);
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 
141  strncpy(p_device_info->dev_cap[3].profiles_supported, "0, 2",
143  strncpy(p_device_info->dev_cap[3].level, "6.2", NI_LEVELS_SUPP_STR_LEN);
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 
152  strncpy(p_device_info->dev_cap[0].profiles_supported,
153  "Baseline, Main, High, High10", NI_PROFILES_SUPP_STR_LEN);
154  strncpy(p_device_info->dev_cap[0].level, "6.2", NI_LEVELS_SUPP_STR_LEN);
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  strncpy(p_device_info->dev_cap[1].profiles_supported, "Main, Main10",
164  strncpy(p_device_info->dev_cap[1].level, "6.2", NI_LEVELS_SUPP_STR_LEN);
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_PARAM_MIN_WIDTH;
170  p_device_info->dev_cap[2].min_res_height = NI_PARAM_MIN_HEIGHT;
171 
172  strncpy(p_device_info->dev_cap[2].profiles_supported, "Main",
174  strncpy(p_device_info->dev_cap[2].level, "5.1", NI_LEVELS_SUPP_STR_LEN);
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;
178  p_device_info->dev_cap[3].max_res_height = NI_PARAM_AV1_MAX_HEIGHT;
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 
182  strncpy(p_device_info->dev_cap[3].profiles_supported, "Main",
184  strncpy(p_device_info->dev_cap[3].level, "5.1", NI_LEVELS_SUPP_STR_LEN);
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  }
192  strncpy(p_device_info->dev_cap[3].additional_info, "Set", NI_ADDITIONAL_INFO_STR_LEN);
193 END:
194 
195  return retval;
196 }
197 
198 /*!******************************************************************************
199  * \brief
200  *
201  * \param
202  *
203  * \return
204  *******************************************************************************/
205 int 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  *******************************************************************************/
242 void 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  *******************************************************************************/
265 void 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 
282 static 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 
294  strncpy(device_info->dev_name, device_name, (MAX_CHAR_IN_DEVICE_NAME-1));
295  strncpy(device_info->blk_name, device_name, (MAX_CHAR_IN_DEVICE_NAME-1));
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 
337 static 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 
375 bool 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 
408 bool 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  uint32_t max_io_size;
416  char fw_api_ver_str[5];
417 
418  ni_device_capability_t device_capability = {0};
419  ni_device_handle_t device_handle;
420  ni_device_info_t device_info = {0};
421  bool success = true;
422 
423  max_io_size = NI_INVALID_IO_SIZE;
424  device_handle = ni_device_open(device_name, &max_io_size);
425  if (device_handle == NI_INVALID_DEVICE_HANDLE)
426  {
427  if (device_open_should_succeed)
428  {
430  "ERROR: %s(): Failed to add %s\n: Failed ni_device_open()\n",
431  __FUNCTION__,
432  device_name);
433  return false;
434  }
435  return true;
436  }
437 
438  if (ni_device_capability_query2(device_handle, &device_capability, false) != NI_RETCODE_SUCCESS)
439  {
440  ni_log(NI_LOG_INFO, "Skipping %s init: unable to query capability\n",
441  device_name);
442  LRETURN;
443  }
444  if (!is_supported_xcoder(device_capability.device_is_xcoder))
445  {
446  ni_log(NI_LOG_INFO, "Skipping %s init: model not supported\n", device_name);
447  LRETURN;
448  }
450  &fw_api_ver_str[0]);
451  if (should_match_rev && \
452  ((uint8_t) device_capability.fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX] != \
454  {
456  "Skipping %s init: device FW v%s incompatible with this version of Libxcoder\n",
457  device_name,
458  fw_api_ver_str);
459  LRETURN;
460  }
461  fw_compat_cmp = ni_cmp_fw_api_ver((char*) &device_capability.fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX],
463 
465  {
466  if (!device_capability.xcoder_cnt[i])
467  {
469  "%s %s disabled...\n",
470  device_name,
472  continue;
473  }
474 
475  j = device_queue->xcoder_cnt[i];
476  guid = j ? device_queue->xcoders[i][j-1] + 1 : 0;
477  if (guid >= NI_MAX_DEVICE_CNT && !find_available_guid(device_queue, i, &guid))
478  {
479  ni_log(NI_LOG_ERROR, "ERROR: %s(): Skipping %s init: number of "
480  "initialized devices exceeds %d\n", __FUNCTION__,
481  device_name, NI_MAX_DEVICE_CNT);
482  success = false;
483  LRETURN;
484  }
485  device_queue->xcoders[i][j] = guid;
486  device_queue->xcoder_cnt[i]++;
487 
488  fill_device_info(&device_info,
489  guid,
490  device_handle,
491  device_name,
492  (should_match_rev && fw_compat_cmp) ? 1 : 0,
493  &device_capability,
494  (ni_device_type_t)i);
495 
496  ni_rsrc_get_one_device_info(&device_info);
497  }
498 
499  if (fw_compat_cmp < 0) {
500  ni_log(NI_LOG_INFO, "Initialized %s with FW API v%s that is older than "
501  "Libxcoder supported FW API version\n", device_name,
502  fw_api_ver_str);
503  } else if (fw_compat_cmp > 0) {
504  ni_log(NI_LOG_INFO, "Initialized %s with FW API v%s that is newer than "
505  "Libxcoder supported FW API version\n", device_name,
506  fw_api_ver_str);
507  } else {
508  ni_log(NI_LOG_INFO, "Initialized %s\n", device_name, fw_api_ver_str);
509  }
510 
511 END:
512  ni_device_close(device_handle);
513 
514  return success;
515 }
516 
518 {
519 #ifdef _WIN32
520  return NI_RETCODE_SUCCESS;
521 #else
522  size_t i;
523  int retry_shm_fd;
524  for(i = 0; i < sizeof(XCODERS_RETRY_LCK_NAME) / sizeof(XCODERS_RETRY_LCK_NAME[0]); ++i)
525  {
526  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);
527  if(retry_shm_fd < 0)
528  {
529  if(errno != EEXIST)
530  {
531  ni_log(NI_LOG_ERROR, "Failed to create %s ERROR: %s\n", XCODERS_RETRY_LCK_NAME[i], strerror(errno));
533  }
534  else
535  {
536  ni_log(NI_LOG_DEBUG, "%s already exists\n", XCODERS_RETRY_LCK_NAME[i]);
537  }
538  }
539  else
540  {
541  ni_log(NI_LOG_DEBUG, "Create XCODERS_RETRY_LCK: %s\n", XCODERS_RETRY_LCK_NAME[i]);
542  close(retry_shm_fd);
543  }
544  }
545 
546  return NI_RETCODE_SUCCESS;
547 
548 #endif
549 }
550 
551 #ifdef _WIN32
552 
553 /*!******************************************************************************
554  * \brief
555  *
556  * \param
557  *
558  * \return
559  *******************************************************************************/
561  char ni_devices[][NI_MAX_DEVICE_NAME_LEN],
562  int max_handles
563 )
564 {
565  return ni_nvme_enumerate_devices(ni_devices, max_handles);
566 }
567 
568 /*!******************************************************************************
569  * \brief
570  *
571  * \param
572  *
573  * \return
574  *******************************************************************************/
576 {
577  char shm_name[32] = { 0 };
578  char lck_name[32] = { 0 };
579  DWORD rc = 0;
580  ni_device_info_t * p_coder_info_map = NULL;
581  HANDLE map_file_handle = NULL;
582  ni_lock_handle_t mutex_handle = NULL;
583  SECURITY_DESCRIPTOR security_descriptor = { 0 };
584 
585  if(!p_device_info)
586  {
587  return;
588  }
589 
590  ni_rsrc_get_shm_name(p_device_info->device_type, p_device_info->module_id, shm_name, sizeof(shm_name));
591  ni_rsrc_get_lock_name(p_device_info->device_type, p_device_info->module_id, lck_name, sizeof(lck_name));
592  ni_log(NI_LOG_DEBUG, "%s(): shm_name %s, lck_name %s\n", __func__, shm_name, lck_name);
593 
594  //Create a mutex for protecting the memory area
595  mutex_handle = CreateMutex(NULL, // default security attributes
596  FALSE, // initially owned
597  lck_name); // unnamed mutex
598  if (NULL == mutex_handle)
599  {
600  ni_log(NI_LOG_ERROR, "CreateMutex error: %d\n", NI_ERRNO);
601  LRETURN;
602  }
603 
604  if (WAIT_ABANDONED == WaitForSingleObject(mutex_handle, INFINITE))
605  {
606  ni_log(NI_LOG_ERROR, "ERROR: ni_rsrc_get_device_context() failed to "
607  "obtain mutex: %p\n", mutex_handle);
608  LRETURN;
609  }
610 
611  InitializeSecurityDescriptor(&security_descriptor, SECURITY_DESCRIPTOR_REVISION);
612  //security_descriptor.Control
613 
614  map_file_handle = CreateFileMapping(
615  INVALID_HANDLE_VALUE, // use paging file
616  NULL, // default security
617  PAGE_READWRITE, // read/write access
618  0, // maximum object size (high-order DWORD)
619  sizeof(ni_device_info_t),// maximum object size (low-order DWORD)
620  (LPCSTR)shm_name // name of mapping object
621  );
622 
623  if (NULL == map_file_handle)
624  {
625  rc = NI_ERRNO;
626  ni_log(NI_LOG_ERROR, "ERROR: CreateFileMapping returned (%d) for %s\n",
627  rc, shm_name);
628  LRETURN;
629  }
630  else
631  {
632  rc = NI_ERRNO;
633  if (ERROR_ALREADY_EXISTS == rc)
634  {
635  ni_log(NI_LOG_ERROR, "CreateFileMapping returned existing handle for"
636  " %s\n", shm_name);
637  }
638  else
639  {
640  ni_log(NI_LOG_INFO, "CreateFileMapping created a new mapFile for %s, handle: %p ..\n", shm_name, map_file_handle);
641  }
642  }
643 
644  p_coder_info_map = (ni_device_info_t *) MapViewOfFile(
645  map_file_handle, // handle to map object
646  FILE_MAP_ALL_ACCESS, // read/write permission
647  0,
648  0,
649  sizeof(ni_device_info_t)
650  );
651 
652  if (NULL == p_coder_info_map)
653  {
654  rc = NI_ERRNO;
655  ni_log(NI_LOG_INFO, "Could not map view of file, p_last error (%d).\n", rc);
656  LRETURN;
657  }
658 
659  memcpy(p_coder_info_map, p_device_info, sizeof(ni_device_info_t));
660 
661 END:
662  if (p_coder_info_map)
663  {
664  UnmapViewOfFile(p_coder_info_map);
665  }
666 
667  if (mutex_handle) {
668  ReleaseMutex(mutex_handle);
669  mutex_handle = NULL;
670  }
671 }
672 
673 
674 
675 
676 
677 /*!******************************************************************************
678  * \brief
679  *
680  * \param
681  *
682  * \return
683  *******************************************************************************/
684 void ni_rsrc_update_record(ni_device_context_t* p_device_context, ni_session_context_t* p_session_context)
685 {
686  uint32_t i = 0;
687 
688  if ((!p_device_context) || (!p_session_context))
689  {
690  return;
691  }
692 
693  p_device_context->p_device_info->load = p_session_context->load_query.current_load;
694  p_device_context->p_device_info->active_num_inst = p_session_context->load_query.total_contexts;
695  // Now we get the model load from the FW
696  p_device_context->p_device_info->model_load = p_session_context->load_query.fw_model_load;
697  if ( 0 == p_device_context->p_device_info->active_num_inst )
698  {
699  p_device_context->p_device_info->load = 0;
700  }
701 
702  for (i = 0; i < p_device_context->p_device_info->active_num_inst; i++)
703  {
704  p_device_context->p_device_info->sw_instance[i].id =
705  p_session_context->load_query.context_status[i].context_id;
706  p_device_context->p_device_info->sw_instance[i].status = (ni_sw_instance_status_t)
707  p_session_context->load_query.context_status[i].context_status;
708  p_device_context->p_device_info->sw_instance[i].codec = (ni_codec_t)
709  p_session_context->load_query.context_status[i].codec_format;
710  p_device_context->p_device_info->sw_instance[i].width =
711  p_session_context->load_query.context_status[i].video_width;
712  p_device_context->p_device_info->sw_instance[i].height =
713  p_session_context->load_query.context_status[i].video_height;
714  p_device_context->p_device_info->sw_instance[i].fps =
715  p_session_context->load_query.context_status[i].fps;
716  }
717 }
718 
719 /*!******************************************************************************
720  * \brief Initialize and create all resources required to work with NETINT NVMe
721  * transcoder devices. This is a high level API function which is used
722  * mostly with user application like FFmpeg that relies on those resources.
723  * In case of custom application integration, revised functionality might
724  * be necessary utilizing corresponding API functions.
725  *
726  * \param[in] should_match_rev 0: transcoder firmware revision matching the
727  * library's version is NOT required for placing
728  * the transcoder into resource pool; 1: otherwise
729  * timeout_seconds 0: No timeout amount, loop until init success
730  * or fail; else: timeout will fail init once reached
731  *
732  * \return
733  * NI_RETCODE_SUCCESS on success
734  * NI_RETCODE_FAILURE on failure
735  *
736  *******************************************************************************/
737 int ni_rsrc_init_priv(const int should_match_rev,
738  const int existing_number_of_devices,
739  const char existing_device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN])
740 {
741  DWORD rc = 0;
742  HANDLE lock = NULL;
743  HANDLE map_file_handle = NULL;
744  ni_device_queue_t* p_device_queue = NULL;
745  map_file_handle = CreateFileMapping(
746  INVALID_HANDLE_VALUE, // use paging file
747  NULL, // default security
748  PAGE_READWRITE, // read/write access
749  0, // maximum object size (high-order DWORD)
750  sizeof(ni_device_queue_t),// maximum object size (low-order DWORD)
751  CODERS_SHM_NAME // name of mapping object
752  );
753 
754  if (NULL == map_file_handle)
755  {
756  rc = NI_ERRNO;
757  ni_log(NI_LOG_ERROR, "ERROR: CreateFileMapping returned: %d\n", rc);
758  return NI_RETCODE_FAILURE;
759  }
760  else
761  {
762  rc = NI_ERRNO;
763  if(ERROR_ALREADY_EXISTS == rc)
764  {
765  ni_log(NI_LOG_INFO, "NETINT resources have been initialized already, exiting ..\n");
766  CloseHandle(map_file_handle);
767  return NI_RETCODE_SUCCESS;
768  }
769  else
770  {
771  ni_log(NI_LOG_INFO, "NETINT resources not initialized, starting initialization ..\n");
772  }
773  }
774 
775  p_device_queue = (ni_device_queue_t*)MapViewOfFile(
776  map_file_handle, // handle to map object
777  FILE_MAP_ALL_ACCESS, // read/write permission
778  0,
779  0,
780  sizeof(ni_device_queue_t)
781  );
782 
783  if (NULL == p_device_queue)
784  {
785  ni_log(NI_LOG_ERROR, "Could not map view of file, p_last error (%d).\n",
786  NI_ERRNO);
787  CloseHandle(map_file_handle);
788  return NI_RETCODE_FAILURE;
789  }
790 
791  lock = CreateMutex(NULL, FALSE, CODERS_LCK_NAME);
792  if (NULL == lock)
793  {
794  ni_log(NI_LOG_ERROR, "Init CreateMutex %s failed: %d\n", CODERS_LCK_NAME,
795  NI_ERRNO);
796  UnmapViewOfFile(p_device_queue);
797  CloseHandle(map_file_handle);
798  return NI_RETCODE_FAILURE;
799  }
800 
801  if (WAIT_ABANDONED == WaitForSingleObject(lock, INFINITE))
802  {
803  ni_log(NI_LOG_ERROR, "ERROR %d: failed to obtain mutex: %p\n",
804  NI_ERRNO, lock);
805  ReleaseMutex(lock);
806  UnmapViewOfFile(p_device_queue);
807  CloseHandle(map_file_handle);
808  return NI_RETCODE_FAILURE;
809  }
810 
811  fill_shared_memory(p_device_queue,
812  should_match_rev,
813  existing_number_of_devices,
814  existing_device_names);
815 
816  UnmapViewOfFile(p_device_queue);
817  ReleaseMutex(lock);
818  return NI_RETCODE_SUCCESS;
819 }
820 
821 #elif __linux__ || __APPLE__
822 
826 static bool check_correctness_count(const ni_device_queue_t *existing_device_queue,
827  const int should_match_rev,
828  const int existing_number_of_devices,
829  const char device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN])
830 {
831  uint32_t max_io_size;
832  int i, j, k;
833 
834  ni_device_capability_t device_capability;
835  ni_device_handle_t device_handle;
836  ni_device_queue_t device_queue;
837 
838  memset(device_queue.xcoder_cnt, 0, sizeof(device_queue.xcoder_cnt));
839 
840  max_io_size = NI_INVALID_IO_SIZE;
841  for (i = 0; i < existing_number_of_devices; i++)
842  {
843  device_handle = ni_device_open(device_names[i], &max_io_size);
844  if (device_handle == NI_INVALID_DEVICE_HANDLE)
845  {
846  continue;
847  }
848 
849  memset(&device_capability, 0, sizeof(ni_device_capability_t));
850  if (ni_device_capability_query2(device_handle, &device_capability, false) != NI_RETCODE_SUCCESS || \
851  !is_supported_xcoder(device_capability.device_is_xcoder) ||
852  (should_match_rev && \
853  ((uint8_t) device_capability.fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX] != \
855  {
856  goto NEXT;
857  }
858 
860  {
861  //Don't count if not in queue.
862  if (existing_device_queue->xcoders[j][i] == -1 && device_capability.xcoder_cnt[j] != 0)
863  {
864  //If not in queue then it shouldn't be in any device module.
866  {
867  if (existing_device_queue->xcoders[k][i] != -1)
868  {
870  "ERROR: %s(): Discovered device %s is not in queue for module %s but is in %s\n",
871  __func__,
872  device_names[i],
874  ni_device_close(device_handle);
875  return false;
876  }
877  }
878  continue;
879  }
880  device_queue.xcoder_cnt[j] += device_capability.xcoder_cnt[j];
881  }
882 NEXT:
883  ni_device_close(device_handle);
884  }
885 
887  {
888  if (device_queue.xcoder_cnt[i] == existing_device_queue->xcoder_cnt[i])
889  {
890  continue;
891  }
893  "WARNING: %s(): Discovered %u %s, expected %u\n",
894  __func__,
895  device_queue.xcoder_cnt[i],
897  existing_device_queue->xcoder_cnt[i]);
898  return false;
899  }
900 
901  return true;
902 }
903 
904 static bool check_device_queue(const ni_device_queue_t *existing_device_queue,
905  const int existing_number_of_devices,
906  const char device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN])
907 {
908  int32_t module_id;
909  int i, j;
910 
911  ni_device_context_t *device_context;
912 
913  for (i = 0; i < existing_number_of_devices; i++)
914  {
916  {
917  module_id = existing_device_queue->xcoders[j][i];
918  if (module_id == -1)
919  {
920  break;
921  }
922  device_context = ni_rsrc_get_device_context((ni_device_type_t)j,
923  module_id);
924  if (!device_context)
925  {
927  "WARNING: %s(): Missing device context for %s %s\n",
928  __func__,
929  device_names[i],
931  return false;
932  }
933  if (strcmp(device_context->p_device_info->dev_cap[3].additional_info, "Set") != 0)
934  {
935  ni_log(NI_LOG_ERROR, "ERROR %s() device info not populated %s\n", __func__, device_context->p_device_info->dev_cap[3].additional_info);
936  return false;
937  }
938  ni_rsrc_free_device_context(device_context);
939  }
940  }
941 
942  return true;
943 }
944 
945 #ifndef _ANDROID
946 static void sigbus_handler(int signal)
947 {
948  siglongjmp(env, 1);
949 }
950 
951 static void setup_signal_handler(struct sigaction *p, const int signum)
952 {
953  struct sigaction c;
954 
955  memset(&c, 0, sizeof(struct sigaction));
956  if (sigemptyset(&c.sa_mask) == -1)
957  {
959  "ERROR: %s(): Could not initialize signal set: %d\n",
960  __func__,
961  NI_ERRNO);
962  exit(EXIT_FAILURE);
963  }
964  c.sa_handler = sigbus_handler;
965 
966  if (sigaction(signum, NULL, p) == -1)
967  {
969  "ERROR: %s(): Could not save previous signal handler: %d\n",
970  __func__,
971  NI_ERRNO);
972  exit(EXIT_FAILURE);
973  }
974 
975  if (sigaction(signum, &c, NULL) == -1)
976  {
978  "ERROR: %s(): Could not register signal handler: %d\n",
979  __func__,
980  NI_ERRNO);
981  exit(EXIT_FAILURE);
982  }
983 }
984 #endif
985 
986 static bool check_correctness(const ni_device_queue_t *existing_device_queue,
987  const int should_match_rev,
988  const int existing_number_of_devices,
989  const char device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN])
990 {
991  bool result = false;
992 #ifndef _ANDROID
993  const int signum = SIGBUS;
994  struct sigaction p;
995 
996  setup_signal_handler(&p, signum);
997 
998  if (sigsetjmp(env, 1))
999  {
1000  LRETURN;
1001  }
1002 #endif
1003 
1004  if (!check_correctness_count(existing_device_queue,
1005  should_match_rev,
1006  existing_number_of_devices,
1007  device_names))
1008  {
1009  LRETURN;
1010  }
1011 
1012  if (!check_device_queue(existing_device_queue,
1013  existing_number_of_devices,
1014  device_names))
1015  {
1016  LRETURN;
1017  }
1018 
1019  result = true;
1020  ni_log(NI_LOG_INFO, "%s ok\n", CODERS_SHM_NAME);
1021 
1022 end:
1023 #ifndef _ANDROID
1024  if (sigaction(signum, &p, NULL) == -1)
1025  {
1027  "ERROR: %s(): Could not restore previous signal handler: %d\n",
1028  __func__,
1029  NI_ERRNO);
1030  exit(EXIT_FAILURE);
1031  }
1032 #endif
1033  return result;
1034 }
1035 
1036 int ni_rsrc_init_priv(const int should_match_rev,
1037  const int existing_number_of_devices,
1038  const char existing_device_names[NI_MAX_DEVICE_CNT][NI_MAX_DEVICE_NAME_LEN],
1039  int limit_depth)
1040 {
1041  int return_value = 0;
1042  int lck_fd = -1;
1043  int shm_fd = -1;
1044  ni_rsrc_shm_state state = NI_RSRC_SHM_IS_INVALID;
1045  int flags = O_CREAT | O_RDWR | O_CLOEXEC;
1046  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1047  ni_device_queue_t *p_device_queue = NULL;
1048 
1049  if ((ni_rsrc_try_get_shm_lock(CODERS_LCK_NAME, flags, mode, &lck_fd) < 0) ||
1050  (ni_rsrc_open_shm(CODERS_SHM_NAME, sizeof(ni_device_queue_t), &state, &shm_fd) < 0)) {
1051  return_value = 1;
1052  LRETURN;
1053  }
1054 
1055  if ((ni_rsrc_mmap_shm(CODERS_SHM_NAME,
1056  shm_fd,
1057  sizeof(ni_device_queue_t),
1058  (void **)&p_device_queue)) < 0) {
1059  ni_log(NI_LOG_ERROR, "%s(): Failed to ni_rsrc_mmap_shm\n", __func__);
1060  return_value = 1;
1061  LRETURN;
1062  }
1063 
1064  //share momery exist
1065  if (NI_RSRC_SHM_IS_EXISTED == state) {
1066  if (check_correctness(p_device_queue,
1067  should_match_rev,
1068  existing_number_of_devices,
1069  existing_device_names))
1070  {
1071  LRETURN;
1072  }
1073 
1074  ni_rsrc_munmap_shm((void *)p_device_queue, sizeof(ni_device_queue_t));
1075  ni_log(NI_LOG_DEBUG, "in %s do munmap for %s, shm_flag is O_RDWR\n", __func__, CODERS_SHM_NAME);
1076 
1077  if (lockf(lck_fd, F_ULOCK, 0) < 0) {
1078  ni_log(NI_LOG_ERROR, "%s(): Failed to unlock lck_fd for %s\n", __func__, CODERS_SHM_NAME);
1079  }
1080 
1081  close(lck_fd);
1082 
1083 #ifndef __OPENHARMONY__
1084  close(shm_fd);
1085 #endif
1086  ni_rsrc_remove_all_shm();
1087 
1088  if (limit_depth <= 0)
1089  {
1090  return 1;
1091  }
1092  return ni_rsrc_init_priv(should_match_rev,
1093  existing_number_of_devices,
1094  existing_device_names,
1095  limit_depth - 1);
1096  }
1097 
1098  fill_shared_memory(p_device_queue,
1099  should_match_rev,
1100  existing_number_of_devices,
1101  existing_device_names);
1102 
1103 end:
1104  if (p_device_queue && p_device_queue != MAP_FAILED) {
1105  ni_rsrc_munmap_shm((void *)p_device_queue, sizeof(ni_device_queue_t));
1106  p_device_queue = NULL;
1107  ni_log(NI_LOG_DEBUG, "in %s do munmap for %s\n", __func__, CODERS_SHM_NAME);
1108  }
1109 
1110  if (lck_fd != -1) {
1111  if (lockf(lck_fd, F_ULOCK, 0) < 0) {
1112  ni_log(NI_LOG_ERROR, "Will exit from %s(), but failed to unlock lck_fd for %s\n", __func__, CODERS_SHM_NAME);
1113  }
1114 
1115  close(lck_fd);
1116  }
1117 
1118 #ifndef __OPENHARMONY__
1119  if (shm_fd >= 0) {
1120  close(shm_fd);
1121  }
1122 #endif
1123 
1124  return return_value;
1125 }
1126 
1127 /*!******************************************************************************
1128  * \brief
1129  *
1130  * \param
1131  *
1132  * \return
1133  *******************************************************************************/
1134 void ni_rsrc_get_one_device_info (ni_device_info_t * p_device_info)
1135 {
1136  int32_t shm_fd = -1;
1137  ni_rsrc_shm_state state = NI_RSRC_SHM_IS_INVALID;
1138  int32_t lock = -1;
1139  int flags = O_CREAT | O_RDWR | O_CLOEXEC;
1140  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1141  char shm_name[32] = { 0 };
1142  char lck_name[32] = { 0 };
1143  ni_device_info_t * p_coder_info_dst = NULL;
1144 
1145  if(! p_device_info) {
1146  return;
1147  }
1148 
1149  ni_rsrc_get_shm_name(p_device_info->device_type, p_device_info->module_id, shm_name, sizeof(shm_name));
1150  ni_rsrc_get_lock_name(p_device_info->device_type, p_device_info->module_id, lck_name, sizeof(lck_name));
1151 
1152  ni_log(NI_LOG_DEBUG, "%s(): shm_name %s, lck_name %s\n", __func__, shm_name, lck_name);
1153 
1154  if (ni_rsrc_try_get_shm_lock(lck_name, flags, mode, (int *)&lock) < 0) {
1155  ni_log(NI_LOG_ERROR, "%s: Failed to get lock\n", __func__);
1156  return ;
1157  }
1158 
1159  if (ni_rsrc_open_shm(shm_name,
1160  sizeof(ni_device_info_t),
1161  &state,
1162  (int *)&shm_fd) < 0) {
1163  ni_log(NI_LOG_ERROR, "%s: Failed to ni_rsrc_open_shm\n", __func__);
1164  LRETURN;
1165  }
1166 
1167  if ((ni_rsrc_mmap_shm(shm_name,
1168  (int)shm_fd,
1169  sizeof(ni_device_info_t),
1170  (void **)&p_coder_info_dst)) < 0) {
1171  ni_log(NI_LOG_ERROR, "%s(): Failed to ni_rsrc_mmap_shm\n", __func__);
1172  LRETURN;
1173  }
1174 
1175  memcpy(p_coder_info_dst, p_device_info, sizeof(ni_device_info_t));
1176 
1177 #ifndef __OPENHARMONY__
1178  if (msync((void*)p_coder_info_dst, sizeof(ni_device_info_t), MS_SYNC | MS_INVALIDATE)) {
1179  ni_log(NI_LOG_ERROR, "ERROR %s() msync() p_coder_info_dst: %s\n",
1180  __func__, strerror(NI_ERRNO));
1181  } else {
1182  ni_log(NI_LOG_DEBUG, "%s(): written out\n", __func__);
1183  }
1184 #endif
1185 
1186 END:
1187  if (p_coder_info_dst && p_coder_info_dst != MAP_FAILED) {
1188  ni_rsrc_munmap_shm((void *)p_coder_info_dst, sizeof(ni_device_info_t));
1189  p_coder_info_dst = NULL;
1190  ni_log(NI_LOG_DEBUG, "in %s do munmap for %s\n", __func__, shm_name);
1191  }
1192 
1193 #ifndef __OPENHARMONY__
1194  if (shm_fd >= 0) {
1195  close(shm_fd);
1196  }
1197 #endif
1198 
1199  if (lockf(lock, F_ULOCK, 0) < 0) {
1200  ni_log(NI_LOG_ERROR, "Will exit from %s(), but failed to unlock lck_fd for %s\n", __func__, shm_name);
1201  }
1202 
1203  if (lock >= 0) {
1204  close(lock);
1205  }
1206 }
1207 
1208 /*!******************************************************************************
1209  * \brief
1210  *
1211  * \param
1212  *
1213  * \return
1214  *******************************************************************************/
1215 void ni_rsrc_update_record(ni_device_context_t *p_device_context, ni_session_context_t *p_session_context)
1216 {
1217  uint32_t j;
1218 
1219  if ((!p_device_context) || (!p_session_context))
1220  {
1221  return;
1222  }
1223 
1224  p_device_context->p_device_info->load = p_session_context->load_query.current_load;
1225  p_device_context->p_device_info->active_num_inst = p_session_context->load_query.total_contexts;
1226  // Now we get the model load from the FW
1227  p_device_context->p_device_info->model_load = p_session_context->load_query.fw_model_load;
1228  if (0 == p_device_context->p_device_info->active_num_inst)
1229  {
1230  p_device_context->p_device_info->load = 0;
1231  }
1232  for (j = 0; j < p_device_context->p_device_info->active_num_inst; j++)
1233  {
1234  p_device_context->p_device_info->sw_instance[j].id =
1235  p_session_context->load_query.context_status[j].context_id;
1236  p_device_context->p_device_info->sw_instance[j].status = (ni_sw_instance_status_t)
1237  p_session_context->load_query.context_status[j]
1238  .context_status;
1239  p_device_context->p_device_info->sw_instance[j].codec = (ni_codec_t)
1240  p_session_context->load_query.context_status[j]
1241  .codec_format;
1242  p_device_context->p_device_info->sw_instance[j].width =
1243  p_session_context->load_query.context_status[j].video_width;
1244  p_device_context->p_device_info->sw_instance[j].height =
1245  p_session_context->load_query.context_status[j].video_height;
1246  p_device_context->p_device_info->sw_instance[j].fps =
1247  p_session_context->load_query.context_status[j].fps;
1248  }
1249  if (msync((void *)p_device_context->p_device_info, sizeof(ni_device_info_t), MS_SYNC | MS_INVALIDATE))
1250  {
1251  ni_log(NI_LOG_ERROR, "ERROR %s() msync() p_device_context->"
1252  "p_device_info: %s\n", __func__, strerror(NI_ERRNO));
1253  }
1254 }
1255 
1256 
1257 /*!******************************************************************************
1258  * \brief get PCIe address information from device name
1259  *
1260  * \param[in] char *device_name e.g. /dev/nvme0n1.
1261  * \param[out] char *pcie e.g. 0000:0a:00.0. Should be at least 13 bytes including null terminator
1262  * \param[out] char *domain, optional. Should be at least 5 bytes including null terminator
1263  * \param[out] char *slot, optional. Should be at least 3 bytes including null terminator
1264  * \param[out] char *dev, optional. Should be at least 3 bytes including null terminator
1265  * \param[out] char *func, optional. Should be at least 2 bytes including null terminator
1266  *
1267  * \return void
1268  * *******************************************************************************/
1269 void get_dev_pcie_addr(char *device_name,
1270  char *pcie,
1271  char *domain, char *slot, char *dev, char *func)
1272 {
1273 #ifndef __linux__
1274  return;
1275 #else
1276  int i=0;
1277  char *ptr = NULL;
1278  // path to nvme drive
1279  char path[PATH_MAX];
1280  int ret;
1281 
1282  if(!device_name || !strstr(device_name, "/dev/nvme") || !pcie)
1283  {
1284  return ;
1285  }
1286 
1287  // we need to skip '/dev/' in the device name
1288  char *start = device_name + 5;
1289 
1290  // construct the path to /sys/block
1291  snprintf(path, sizeof(path), "/sys/block/%s", start);
1292  ni_log2(NULL, NI_LOG_DEBUG,"path:%s\n", path);
1293 
1294  // read the target of the symbolic link
1295  char target[PATH_MAX];
1296  //e.g.: ../devices/pci0000:00/0000:00:03.1/0000:09:00.0/nvme/nvme0/nvme0n1
1297  ssize_t len = readlink(path, target, sizeof(target) - 1);
1298  if (len == -1) {
1299  perror("readlink");
1300  return;
1301  }
1302  target[len] = '\0'; // set the null-terminating character
1303  ni_log2(NULL, NI_LOG_DEBUG,"target:%s\n", target);
1304 
1305  // and find domain and slot from it
1306  char *saveptr = NULL;
1307  ptr = ni_strtok(target, "/", &saveptr);
1308  pcie[4] = pcie[7] = ':';
1309  pcie[10] = '.';
1310  //last pcie info is for the device
1311  while(ptr != NULL) {
1312  ni_log2(NULL, NI_LOG_DEBUG, "===%d ptr:%s\n", ++i, ptr);
1313  if (strlen(ptr) == 12)//e.g.: 0000:09:00.0
1314  {
1315  ret = sscanf(ptr, "%4c:%2c:%2c.%1c", pcie, pcie+5,pcie+8,pcie+11);
1316  if (ret != 4)
1317  {
1318  ni_log2(NULL, NI_LOG_DEBUG, "\tsscanf error %d errno %d %s\n", ret, errno, strerror(errno));
1319  }
1320  }
1321  ni_log2(NULL, NI_LOG_DEBUG, "=====\n");
1322  ptr = ni_strtok(NULL, "/", &saveptr);
1323  }
1324  pcie[12] = '\0';
1325  ni_log2(NULL, NI_LOG_DEBUG, "PCIE:%s\n", pcie);
1326  if (!domain || !slot || !dev || !func)
1327  {
1328  goto end;
1329  }
1330  domain[4] = slot[2] = dev[2] = func[1] = '\0';
1331  sscanf(pcie, "%4[^:]:%2[^:]:%2[^.].%1s", domain, slot, dev, func);
1332  ni_log2(NULL, NI_LOG_DEBUG, "\t%d: Domain: %s, Slot: %s, Device: %s, Function: %s\n", i, domain, slot, dev, func);
1333 end:
1334  return;
1335 #endif
1336 }
1337 
1338 /*!******************************************************************************
1339  * \brief try to get lock for specified shared memory
1340  *
1341  * \param[in] const char *lck_name, specified lock name for shared memory
1342  * \param[in] int flags, open lock with specified flags
1343  * \param[in] const mode_t mode, open lock with specified mode
1344  * \param[out] int *lck_fd, Pointer to opened file handle for lock
1345  *
1346  * \return On success
1347  * NI_RETCODE_SUCCESS
1348  * On failure
1349  * NI_RETCODE_INVALID_PARAM
1350  * NI_RETCODE_FAILURE
1351  *******************************************************************************/
1352 ni_retcode_t ni_rsrc_try_get_shm_lock(const char *lck_name,
1353  int flags,
1354  const mode_t mode,
1355  int *lck_fd)
1356 {
1357  int lock = -1;
1358 
1359  if (!lck_name || !lck_fd) {
1360  ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1361  return NI_RETCODE_INVALID_PARAM;
1362  }
1363 
1364 #ifdef _ANDROID
1365  if (0 != access(LOCK_DIR, F_OK)) {
1366  if (0 != mkdir(LOCK_DIR, S_IRWXU | S_IRWXG | S_IRWXO)) {
1367  ni_log(NI_LOG_ERROR, "ERROR: Could not mkdir : %s directory", LOCK_DIR);
1368  return NI_RETCODE_FAILURE;
1369  }
1370  }
1371 #endif
1372 
1373  lock = open(lck_name, flags, mode);
1374  if (lock < 0) {
1375  ni_log(NI_LOG_ERROR, "ERROR: %s() open() %s fail: %s\n",
1376  __func__, lck_name, strerror(NI_ERRNO));
1377  return NI_RETCODE_FAILURE;
1378  }
1379 
1380  int retry_cnt = 0;
1381  //use non blocking F_TLOCK in case broken instance has indefinitely locked it
1382  while (lockf(lock, F_TLOCK, 0) != 0)
1383  {
1384  retry_cnt++;
1385  ni_usleep(LOCK_WAIT); //10ms
1386  if (retry_cnt >= 900) //10s
1387  {
1388  ni_log(NI_LOG_ERROR, "ERROR %s() lockf() %s fail: %s\n", __func__, lck_name, strerror(NI_ERRNO));
1389  ni_log(NI_LOG_ERROR, "ERROR %s() If persists, stop traffic and run rm /dev/shm/NI_*\n", __func__);
1390  close(lock);
1391  return NI_RETCODE_FAILURE;
1392  }
1393  }
1394 
1395  *lck_fd = lock;
1396 
1397  return NI_RETCODE_SUCCESS;
1398 }
1399 
1400 #if defined(__OPENHARMONY__)
1401 static ni_retcode_t openharmony_open_shm(const char *shm_name,
1402  int shm_size,
1403  ni_rsrc_shm_state *state,
1404  int *shm_fd)
1405 {
1406  int shm_id = -1;
1407  int flag = IPC_CREAT | IPC_EXCL;
1408  char shm_path[PATH_MAX];
1409 
1410  if (!shm_name || !shm_fd) {
1411  ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1412  return NI_RETCODE_INVALID_PARAM;
1413  }
1414 
1415  memset(shm_path, 0, PATH_MAX);
1416  snprintf(shm_path, PATH_MAX, "%s/%s", LOCK_DIR, shm_name);
1417 
1418  //If file not exist, create the file for ftok
1419  if (0 != access(shm_path, F_OK)) {
1420  int fd = open(shm_path, O_RDWR | O_CREAT | O_CLOEXEC,
1421  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1422  if (fd < 0) {
1423  ni_log(NI_LOG_ERROR, "ERROR: %s() open() %s fail: %s\n", __func__, shm_name, strerror(NI_ERRNO));
1424  return NI_RETCODE_FAILURE;
1425  }
1426 
1427  close(fd);
1428  }
1429 
1430  //create unique key for share memory
1431  key_t key = ftok(shm_path, PROJ_ID);
1432  if (key == -1) {
1433  ni_log(NI_LOG_ERROR, "ERROR: %s() ftok() fail: %s\n", __func__, strerror(NI_ERRNO));
1434  return NI_RETCODE_FAILURE;
1435  }
1436 
1437  *state = NI_RSRC_SHM_IS_CREATED;
1438 
1439  //create share memory
1440  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1441  shm_id = shmget(key, shm_size, mode | flag);
1442  if (shm_id < 0) {
1443  if (EEXIST == NI_ERRNO) {
1444  *state = NI_RSRC_SHM_IS_EXISTED;
1445  flag = IPC_CREAT;
1446  shm_id = shmget(key, shm_size, mode | flag);
1447  }
1448 
1449  if (shm_id < 0) {
1450  ni_log(NI_LOG_ERROR, "ERROR: %s() shmget() fail: %s\n", __func__, strerror(NI_ERRNO));
1451  return NI_RETCODE_FAILURE;
1452  }
1453  }
1454  *shm_fd = shm_id;
1455 
1456  return NI_RETCODE_SUCCESS;
1457 }
1458 
1459 static ni_retcode_t openharmony_remove_shm(const char *shm_name,
1460  int shm_size)
1461 {
1462  int shm_id = -1;
1463  char shm_path[PATH_MAX];
1464 
1465  if (!shm_name) {
1466  ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1467  return NI_RETCODE_INVALID_PARAM;
1468  }
1469 
1470  memset(shm_path, 0, PATH_MAX);
1471  snprintf(shm_path, PATH_MAX, "%s/%s", LOCK_DIR, shm_name);
1472 
1473  //If file not exist, exit directly
1474  if (0 != access(shm_path, F_OK)) {
1475  return NI_RETCODE_SUCCESS;
1476  }
1477 
1478  //create unique key for share memory
1479  key_t key = ftok(shm_path, PROJ_ID);
1480  if (key == -1) {
1481  ni_log(NI_LOG_ERROR, "ERROR: %s() ftok() fail: %s\n", __func__, strerror(NI_ERRNO));
1482  return NI_RETCODE_FAILURE;
1483  }
1484 
1485  //create share memory
1486  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1487  shm_id = shmget(key, shm_size, mode);
1488  if (shm_id < 0) {
1489  ni_log(NI_LOG_ERROR, "ERROR: %s() shmget() fail: %s\n", __func__, strerror(NI_ERRNO));
1490  return NI_RETCODE_FAILURE;
1491  }
1492 
1493  shmctl(shm_id, IPC_RMID, nullptr);
1494 
1495  return NI_RETCODE_SUCCESS;
1496 }
1497 
1498 #elif defined(_ANDROID)
1499 static ni_retcode_t android_open_shm(const char *shm_name,
1500  int shm_size,
1501  ni_rsrc_shm_state *state,
1502  int *shm_fd)
1503 {
1504  int shm_fd_tmp = -1;
1505 
1506  if (!shm_name || !shm_fd) {
1507  ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1508  return NI_RETCODE_INVALID_PARAM;
1509  }
1510 
1511  int ret = ni_rsrc_android_init();
1512  if (ret < 0) {
1513  ni_log(NI_LOG_ERROR, "ERROR: %s() failed to get android service\n", __func__);
1514  return NI_RETCODE_FAILURE;
1515  }
1516 
1517  *state = NI_RSRC_SHM_IS_CREATED;
1518 
1519  string param = shm_name;
1520  Return<void> retvalue =
1521  service->GetAppFlag(param, [&](int32_t ret, hidl_handle handle) {
1522  if (ret > 0) {
1523  *state = NI_RSRC_SHM_IS_EXISTED;
1524  shm_fd_tmp = dup(handle->data[0]);
1525  } else {
1526  ni_log(NI_LOG_ERROR, "ERROR: failed to get shm_fd when call GetAppFlag\n");
1527  }
1528  });
1529 
1530  if (!retvalue.isOk()) {
1531  ni_log(NI_LOG_ERROR, "ERROR: %s() maybe something is wrong with Android service\n");
1532  return NI_RETCODE_FAILURE;
1533  }
1534 
1535  if (shm_fd_tmp < 0) {
1536  int fd = ashmem_create_region(shm_name, shm_size);
1537  if (fd >= 0) {
1538  native_handle_t *native_handle = native_handle_create(1, 0);
1539  if (!native_handle) {
1540  close(fd);
1541  return NI_RETCODE_FAILURE;
1542  }
1543  native_handle->data[0] = fd;
1544 
1545  hidl_handle handle;
1546  handle.setTo(native_handle, true);
1547  service->SetAppFlag(param, handle);
1548  shm_fd_tmp = dup(fd);
1549  } else {
1550  ni_log(NI_LOG_ERROR, "Could not create ashmem under Android\n");
1551  return NI_RETCODE_FAILURE;
1552  }
1553  }
1554  *shm_fd = shm_fd_tmp;
1555 
1556  return NI_RETCODE_SUCCESS;
1557 }
1558 
1559 static ni_retcode_t android_remove_shm(const char *shm_name,
1560  int shm_size)
1561 {
1562  if (!shm_name) {
1563  ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1564  return NI_RETCODE_INVALID_PARAM;
1565  }
1566 
1567  int ret = ni_rsrc_android_init();
1568  if (ret < 0) {
1569  ni_log(NI_LOG_ERROR, "ERROR: %s() failed to get android service\n", __func__);
1570  return NI_RETCODE_FAILURE;
1571  }
1572 
1573  string param = shm_name;
1574  Return<void> retvalue = service->RemoveAppFlag(param);
1575  if (!retvalue.isOk()) {
1576  ni_log(NI_LOG_ERROR, "ERROR: %s() failed to remove shm\n", __func__);
1577  return NI_RETCODE_FAILURE;
1578  }
1579 
1580  return NI_RETCODE_SUCCESS;
1581 }
1582 
1583 static ni_retcode_t android_remove_all_shm()
1584 {
1585  int ret = ni_rsrc_android_init();
1586  if (ret < 0) {
1587  ni_log(NI_LOG_ERROR, "ERROR: %s() failed to get android service\n", __func__);
1588  return NI_RETCODE_FAILURE;
1589  }
1590 
1591  Return<void> retvalue = service->RemoveAllAppFlags();
1592  if (!retvalue.isOk()) {
1593  ni_log(NI_LOG_ERROR, "ERROR: %s() failed to remove all shm\n", __func__);
1594  return NI_RETCODE_FAILURE;
1595  }
1596 
1597  return NI_RETCODE_SUCCESS;
1598 }
1599 
1600 #else
1601 static ni_retcode_t linux_open_shm(const char *shm_name,
1602  int shm_size,
1603  ni_rsrc_shm_state *state,
1604  int *shm_fd)
1605 {
1606  int shm_fd_tmp = -1;
1607  const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
1608  int flag = O_CREAT | O_EXCL | O_RDWR;
1609  bool skip_ftruncate = false;
1610 
1611  if (!shm_name || !shm_fd) {
1612  ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1613  return NI_RETCODE_INVALID_PARAM;
1614  }
1615 
1616  *state = NI_RSRC_SHM_IS_CREATED;
1617 
1618  //create share memory
1619  shm_fd_tmp = shm_open(shm_name, flag, mode);
1620  if (shm_fd_tmp < 0) {
1621  if (EEXIST == NI_ERRNO) {
1622  skip_ftruncate = true;
1623  *state = NI_RSRC_SHM_IS_EXISTED;
1624  flag = O_RDWR;
1625  shm_fd_tmp = shm_open(shm_name, flag, mode);
1626  }
1627 
1628  if (shm_fd_tmp < 0) {
1629  ni_log(NI_LOG_ERROR, "ERROR: %s() %s shm_open() fail: %s\n",
1630  __func__, shm_name, strerror(NI_ERRNO));
1631  return NI_RETCODE_FAILURE;
1632  }
1633  }
1634 
1635  //set share memory size
1636  if (!skip_ftruncate && ftruncate(shm_fd_tmp, shm_size) < 0) {
1637  close(shm_fd_tmp);
1638  shm_unlink(shm_name);
1639  return NI_RETCODE_FAILURE;
1640  }
1641 
1642  *shm_fd = shm_fd_tmp;
1643 
1644  return NI_RETCODE_SUCCESS;
1645 }
1646 #endif
1647 
1648 /*!******************************************************************************
1649  * \brief open shared memory for specified shm_name
1650  *
1651  * \param[in] const char *shm_name, specified shared memory name
1652  * \param[in] int shm_size, the size of shared memory
1653  * \param[out] ni_rsrc_shm_state *state, Pointer to the shared memory's state
1654  * if the shared memor isn't created, it will be created at first and
1655  * then open it, param "state" will be set to 0
1656  * if the shared memor has been created yet, only open it,
1657  * param "state" will be set to 1
1658  * \param[out] int *shm_fd, Pointer to opened file handle for shared memory
1659  *
1660  * \return On success
1661  * NI_RETCODE_SUCCESS
1662  * On failure
1663  * NI_RETCODE_INVALID_PARAM
1664  * NI_RETCODE_FAILURE
1665  *******************************************************************************/
1666 ni_retcode_t ni_rsrc_open_shm(const char *shm_name,
1667  int shm_size,
1668  ni_rsrc_shm_state *state,
1669  int *shm_fd)
1670 {
1671 #if defined(__OPENHARMONY__)
1672  return openharmony_open_shm(shm_name, shm_size, state, shm_fd);
1673 #elif defined(_ANDROID)
1674  return android_open_shm(shm_name, shm_size, state, shm_fd);
1675 #else
1676  return linux_open_shm(shm_name, shm_size, state, shm_fd);
1677 #endif
1678 }
1679 
1680 /*!******************************************************************************
1681  * \brief map shared memory to the address space of the calling process
1682  *
1683  * \param[in] const char *shm_name, specified shared memory name
1684  * \param[in] int shm_fd, file handle for shared memory
1685  * \param[in] int shm_size, the size of shared memory
1686  * \param[out] int *shm_addr, pointer to the mapped area
1687  *
1688  * \return On success
1689  * NI_RETCODE_SUCCESS
1690  * On failure
1691  * NI_RETCODE_INVALID_PARAM
1692  * NI_RETCODE_FAILURE
1693  *******************************************************************************/
1694 ni_retcode_t ni_rsrc_mmap_shm(const char *shm_name,
1695  int shm_fd,
1696  int shm_size,
1697  void **shm_addr)
1698 {
1699  if (!shm_name || !shm_addr) {
1700  ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1701  return NI_RETCODE_INVALID_PARAM;
1702  }
1703 
1704 #ifdef __OPENHARMONY__
1705  *shm_addr = shmat(shm_fd, nullptr, 0);
1706  if ((void *)(-1) == *shm_addr) {
1707  ni_log(NI_LOG_ERROR, "%s(): %s shmat() fail: %s\n", __func__,
1708  shm_name, strerror(NI_ERRNO));
1709  return NI_RETCODE_FAILURE;
1710  }
1711 #else
1712  *shm_addr = mmap(0, shm_size, PROT_READ | PROT_WRITE,
1713  MAP_SHARED, shm_fd, 0);
1714  if (MAP_FAILED == *shm_addr) {
1715  ni_log(NI_LOG_ERROR, "%s(): %s mmap() fail: %s\n", __func__,
1716  shm_name, strerror(NI_ERRNO));
1717  return NI_RETCODE_FAILURE;
1718  }
1719 #endif
1720 
1721  return NI_RETCODE_SUCCESS;
1722 }
1723 
1724 /*!******************************************************************************
1725  * \brief do munmap for shared memory
1726  *
1727  * \param[in] int *shm_addr, pointer to the mapped area
1728  * \param[in] int shm_size, the size of shared memory
1729  *
1730  * \return On success
1731  * NI_RETCODE_SUCCESS
1732  * On failure
1733  * NI_RETCODE_INVALID_PARAM
1734  *******************************************************************************/
1735 ni_retcode_t ni_rsrc_munmap_shm(void *shm_addr,
1736  int shm_size)
1737 {
1738  if (!shm_addr) {
1739  ni_log(NI_LOG_ERROR, "ERROR: %s() input params is invalid\n", __func__);
1740  return NI_RETCODE_INVALID_PARAM;
1741  }
1742 
1743 #ifdef __OPENHARMONY__
1744  shmdt(shm_addr);
1745 #else
1746  munmap(shm_addr, shm_size);
1747 #endif
1748 
1749  return NI_RETCODE_SUCCESS;
1750 }
1751 
1752 /*!******************************************************************************
1753  * \brief delete shared memory
1754  *
1755  * \param[in] const char *shm_name, specified shared memory name
1756  * \param[in] int shm_size, the size of shared memory
1757  *
1758  * \return On success
1759  * NI_RETCODE_SUCCESS
1760  * On failure
1761  * NI_RETCODE_INVALID_PARAM
1762  * NI_RETCODE_FAILURE
1763  *******************************************************************************/
1764 ni_retcode_t ni_rsrc_remove_shm(const char *shm_name,
1765  int shm_size)
1766 {
1767 #ifdef __OPENHARMONY__
1768  return openharmony_remove_shm(shm_name, shm_size);
1769 #elif defined(_ANDROID)
1770  return android_remove_shm(shm_name, shm_size);
1771 #else
1772  shm_unlink(shm_name);
1773 #endif
1774 
1775  return NI_RETCODE_SUCCESS;
1776 }
1777 
1778 /*!******************************************************************************
1779  * \brief delete all shared memory
1780  *
1781  *
1782  * \return On success
1783  * NI_RETCODE_SUCCESS
1784  * On failure
1785  * NI_RETCODE_INVALID_PARAM
1786  * NI_RETCODE_FAILURE
1787  *******************************************************************************/
1788 ni_retcode_t ni_rsrc_remove_all_shm()
1789 {
1790  DIR *dir;
1791  struct dirent *dirent;
1792  char path_to_remove[PATH_MAX];
1793 
1794  ni_log(NI_LOG_ERROR, "Deleting shared memory files in %s\n", LOCK_DIR);
1795 
1796  dir = opendir(LOCK_DIR);
1797  if (!dir) {
1798  ni_log(NI_LOG_ERROR, "ERROR: %s(): opendir failed for %s: %s\n",
1799  __func__, LOCK_DIR, strerror(NI_ERRNO));
1800  return NI_RETCODE_FAILURE;
1801  }
1802 
1803  while ((dirent = readdir(dir)) != NULL) {
1804  if (strncmp(dirent->d_name, "NI_", 3) != 0) {
1805  continue;
1806  }
1807 
1808  snprintf(path_to_remove, PATH_MAX, "%s/%s", LOCK_DIR, dirent->d_name);
1809 
1810 #ifndef _ANDROID
1811  if (strncasecmp(dirent->d_name, "NI_SHM", 6) == 0) {
1812 #ifdef __OPENHARMONY__
1813  openharmony_remove_shm(dirent->d_name, 1); //for created share mem we can ignore size
1814 #else
1815  shm_unlink(dirent->d_name);
1816 #endif
1817  }
1818 #endif
1819 
1820  remove(path_to_remove);
1821  }
1822 
1823 #ifdef _ANDROID
1824  android_remove_all_shm();
1825 #endif
1826 
1827  if (closedir(dir) == -1) {
1828  ni_log(NI_LOG_ERROR, "ERROR: %s(): closedir failed for %s: %s\n",
1829  __func__, LOCK_DIR, strerror(NI_ERRNO));
1830  return NI_RETCODE_FAILURE;
1831  }
1832 
1833  ni_log(NI_LOG_INFO, "Deleted shared memory files in %s\n", LOCK_DIR);
1834 
1835  return NI_RETCODE_SUCCESS;
1836 }
1837 
1838 #endif
_ni_device_info::fw_rev
uint8_t fw_rev[8]
Definition: ni_rsrc_api.h:115
_ni_context_query::codec_format
uint32_t codec_format
Definition: ni_device_api.h:1191
_ni_device_capability::fw_commit_time
uint8_t fw_commit_time[26]
Definition: ni_device_api.h:1168
_ni_device_info::serial_number
uint8_t serial_number[20]
Definition: ni_rsrc_api.h:122
EN_INVALID
@ EN_INVALID
Definition: ni_rsrc_api.h:49
NI_DEVICE_TYPE_ENCODER
@ NI_DEVICE_TYPE_ENCODER
Definition: ni_defs.h:359
_ni_hw_capability
hardware capability type
Definition: ni_device_api.h:1136
_ni_device_info::dev_cap
ni_device_video_capability_t dev_cap[EN_CODEC_MAX]
Definition: ni_rsrc_api.h:132
NI_MIN_RESOLUTION_WIDTH_JPEG
#define NI_MIN_RESOLUTION_WIDTH_JPEG
Definition: ni_device_api.h:83
ni_rsrc_get_device_context
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....
g_xcoder_stop_process
uint32_t g_xcoder_stop_process
Definition: ni_rsrc_priv.cpp:73
_ni_load_query::current_load
uint32_t current_load
Definition: ni_device_api.h:1200
NI_INVALID_IO_SIZE
#define NI_INVALID_IO_SIZE
Definition: ni_defs.h:233
_ni_load_query::total_contexts
uint32_t total_contexts
Definition: ni_device_api.h:1203
ni_device_close
void ni_device_close(ni_device_handle_t device_handle)
Close device and release resources.
Definition: ni_device_api.c:503
NI_DEVICE_TYPE_DECODER
@ NI_DEVICE_TYPE_DECODER
Definition: ni_defs.h:358
LOCK_DIR
#define LOCK_DIR
Definition: ni_rsrc_priv.h:42
_ni_device_info::fw_commit_hash
uint8_t fw_commit_hash[41]
Definition: ni_rsrc_api.h:118
NI_PARAM_AV1_MAX_WIDTH
#define NI_PARAM_AV1_MAX_WIDTH
Definition: ni_device_api.h:145
_ni_device_video_capability::max_res_height
int max_res_height
Definition: ni_rsrc_api.h:78
_ni_device_capability::xcoder_devices
ni_hw_capability_t xcoder_devices[NI_MAX_DEVICES_PER_HW_INSTANCE]
Definition: ni_device_api.h:1161
_ni_device_capability::fw_branch_name
uint8_t fw_branch_name[256]
Definition: ni_device_api.h:1167
_ni_hw_capability::hw_id
uint8_t hw_id
Definition: ni_device_api.h:1138
ni_device_type_t
ni_device_type_t
Definition: ni_defs.h:353
ni_nvme_enumerate_devices
int ni_nvme_enumerate_devices(char ni_devices[][NI_MAX_DEVICE_NAME_LEN], int max_handles)
LOCK_WAIT
#define LOCK_WAIT
Definition: ni_rsrc_priv.h:59
ni_codec_t
ni_codec_t
Definition: ni_rsrc_api.h:47
_ni_device_capability::model_number
uint8_t model_number[40]
Definition: ni_device_api.h:1164
NI_RETCODE_SUCCESS
@ NI_RETCODE_SUCCESS
Definition: ni_defs.h:439
ni_sw_instance_status_t
ni_sw_instance_status_t
Definition: ni_rsrc_api.h:61
ni_rsrc_init_priv
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)
ni_device_open
ni_device_handle_t ni_device_open(const char *p_dev, uint32_t *p_max_io_size_out)
Open device and return device device_handle if successful.
Definition: ni_device_api.c:360
_ni_device_video_capability::min_res_width
int min_res_width
Definition: ni_rsrc_api.h:79
_ni_device_capability::xcoder_cnt
uint8_t xcoder_cnt[NI_DEVICE_TYPE_XCODER_MAX]
Definition: ni_device_api.h:1160
_ni_device_info::active_num_inst
uint32_t active_num_inst
Definition: ni_rsrc_api.h:128
EN_H265
@ EN_H265
Definition: ni_rsrc_api.h:51
NI_PARAM_MIN_WIDTH
#define NI_PARAM_MIN_WIDTH
Definition: ni_device_api.h:136
ni_rsrc_strcmp
int ni_rsrc_strcmp(const void *p_str, const void *p_str1)
Definition: ni_rsrc_priv.cpp:205
ni_rsrc_api.h
Public definitions for managing NETINT video processing devices.
NI_XCODER_REVISION
#define NI_XCODER_REVISION
Definition: ni_defs.h:98
ni_rsrc_update_record
void ni_rsrc_update_record(ni_device_context_t *p_device_context, ni_session_context_t *p_session_ctx)
_ni_hw_capability::max_4k_fps
uint8_t max_4k_fps
Definition: ni_device_api.h:1140
_ni_hw_capability::min_video_height
uint16_t min_video_height
Definition: ni_device_api.h:1146
_ni_sw_instance_info::height
int height
Definition: ni_rsrc_api.h:92
_ni_device_video_capability::additional_info
char additional_info[NI_ADDITIONAL_INFO_STR_LEN]
Definition: ni_rsrc_api.h:83
_ni_sw_instance_info::width
int width
Definition: ni_rsrc_api.h:91
NI_RETCODE_INVALID_PARAM
@ NI_RETCODE_INVALID_PARAM
Definition: ni_defs.h:441
_ni_sw_instance_info::fps
int fps
Definition: ni_rsrc_api.h:93
_ni_device_capability::fw_commit_hash
uint8_t fw_commit_hash[41]
Definition: ni_device_api.h:1169
service
android::sp< INidec > service
_ni_device_info::fw_ver_compat_warning
int fw_ver_compat_warning
Definition: ni_rsrc_api.h:111
EN_H264
@ EN_H264
Definition: ni_rsrc_api.h:50
_ni_device_info::fw_commit_time
uint8_t fw_commit_time[26]
Definition: ni_rsrc_api.h:117
ni_rsrc_api_android.h
Public definitions for managing NETINT video processing devices on Android.
_ni_device_info::hw_id
int hw_id
Definition: ni_rsrc_api.h:106
ni_cmp_fw_api_ver
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:3674
_ni_device_capability::fw_build_id
uint8_t fw_build_id[256]
Definition: ni_device_api.h:1171
_ni_device_info::dev_name
char dev_name[NI_MAX_DEVICE_NAME_LEN]
Definition: ni_rsrc_api.h:104
ni_retcode_t
ni_retcode_t
Definition: ni_defs.h:437
_ni_load_query::fw_model_load
uint32_t fw_model_load
Definition: ni_device_api.h:1201
ni_log.h
Logging definitions.
add_to_shared_memory
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)
Definition: ni_rsrc_priv.cpp:408
_ni_context_query::fps
uint32_t fps
Definition: ni_device_api.h:1194
ni_rsrc_android_init
int ni_rsrc_android_init()
Init android net.int.SharedBuffer service for binder using.
NI_LOG_INFO
@ NI_LOG_INFO
Definition: ni_log.h:61
NI_LOG_ERROR
@ NI_LOG_ERROR
Definition: ni_log.h:60
_ni_device_queue::xcoders
int32_t xcoders[NI_DEVICE_TYPE_XCODER_MAX][NI_MAX_DEVICE_CNT]
Definition: ni_rsrc_api.h:70
NI_PROFILES_SUPP_STR_LEN
#define NI_PROFILES_SUPP_STR_LEN
Definition: ni_rsrc_api.h:36
ni_rsrc_get_one_device_info
void ni_rsrc_get_one_device_info(ni_device_info_t *p_device_info)
EN_JPEG
@ EN_JPEG
Definition: ni_rsrc_api.h:53
_ni_device_video_capability::profiles_supported
char profiles_supported[NI_PROFILES_SUPP_STR_LEN]
Definition: ni_rsrc_api.h:81
_ni_device_info::max_instance_cnt
int max_instance_cnt
Definition: ni_rsrc_api.h:127
_ni_device_info
Definition: ni_rsrc_api.h:102
NI_DEVICE_TYPE_AI
@ NI_DEVICE_TYPE_AI
Definition: ni_defs.h:361
_ni_context_query::context_id
uint32_t context_id
Definition: ni_device_api.h:1188
_ni_hw_capability::max_video_height
uint16_t max_video_height
Definition: ni_device_api.h:1144
ni_rsrc_fill_device_info
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)
Definition: ni_rsrc_priv.cpp:82
NI_MIN_RESOLUTION_HEIGHT_JPEG
#define NI_MIN_RESOLUTION_HEIGHT_JPEG
Definition: ni_device_api.h:84
EN_AV1
@ EN_AV1
Definition: ni_rsrc_api.h:54
ni_rsrc_free_device_context
void ni_rsrc_free_device_context(ni_device_context_t *p_device_context)
Free previously allocated device context.
Definition: ni_rsrc_api.cpp:1322
_ni_device_queue
Definition: ni_rsrc_api.h:67
ni_rsrc_create_retry_lck
ni_retcode_t ni_rsrc_create_retry_lck()
Definition: ni_rsrc_priv.cpp:517
_ni_hw_capability::min_video_width
uint16_t min_video_width
Definition: ni_device_api.h:1145
_ni_context_query::context_status
uint32_t context_status
Definition: ni_device_api.h:1189
_ni_device_info::sw_instance
ni_sw_instance_info_t sw_instance[NI_MAX_CONTEXTS_PER_HW_INSTANCE]
Definition: ni_rsrc_api.h:134
ni_log
void ni_log(ni_log_level_t level, const char *fmt,...)
print log message using ni_log_callback
Definition: ni_log.c:183
NI_ADDITIONAL_INFO_STR_LEN
#define NI_ADDITIONAL_INFO_STR_LEN
Definition: ni_rsrc_api.h:38
NI_XCODER_REVISION_API_MAJOR_VER_IDX
#define NI_XCODER_REVISION_API_MAJOR_VER_IDX
Definition: ni_defs.h:99
_ni_device_info::model_load
int model_load
Definition: ni_rsrc_api.h:109
_ni_device_info::module_id
int module_id
Definition: ni_rsrc_api.h:107
GET_XCODER_DEVICE_TYPE
#define GET_XCODER_DEVICE_TYPE(t)
Definition: ni_defs.h:425
CODERS_SHM_NAME
#define CODERS_SHM_NAME
Definition: ni_rsrc_priv.h:46
ni_fmt_fw_api_ver_str
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:3644
ni_usleep
void ni_usleep(int64_t usec)
Definition: ni_util.c:358
ni_query_fl_fw_versions
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.
Definition: ni_device_api.c:11487
LRETURN
#define LRETURN
Definition: ni_defs.h:335
_ni_sw_instance_info::codec
ni_codec_t codec
Definition: ni_rsrc_api.h:90
_ni_device_info::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_rsrc_api.h:119
NI_LEVELS_SUPP_STR_LEN
#define NI_LEVELS_SUPP_STR_LEN
Definition: ni_rsrc_api.h:37
_ni_device_context
Definition: ni_rsrc_api.h:145
EN_VP9
@ EN_VP9
Definition: ni_rsrc_api.h:52
_ni_device_capability::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_device_api.h:1170
_ni_device_info::blk_name
char blk_name[NI_MAX_DEVICE_NAME_LEN]
Definition: ni_rsrc_api.h:105
NI_DEVICE_TYPE_SCALER
@ NI_DEVICE_TYPE_SCALER
Definition: ni_defs.h:360
EN_CODEC_MAX
@ EN_CODEC_MAX
Definition: ni_rsrc_api.h:55
NI_ERRNO
#define NI_ERRNO
Definition: ni_defs.h:229
_ni_session_context
Definition: ni_device_api.h:1410
NI_MAX_DEVICE_CNT
#define NI_MAX_DEVICE_CNT
Definition: ni_defs.h:235
_ni_sw_instance_info::status
ni_sw_instance_status_t status
Definition: ni_rsrc_api.h:89
NI_MAX_DEVICE_NAME_LEN
#define NI_MAX_DEVICE_NAME_LEN
Definition: ni_defs.h:236
NI_PARAM_AV1_MAX_HEIGHT
#define NI_PARAM_AV1_MAX_HEIGHT
Definition: ni_device_api.h:146
_ni_context_query::video_width
uint32_t video_width
Definition: ni_device_api.h:1192
ni_rsrc_priv.h
Private definitions used by ni_rsrc_api.cpp for management of NETINT video processing devices.
ni_strtok
char * ni_strtok(char *s, const char *delim, char **saveptr)
Definition: ni_util.c:420
find_available_guid
bool find_available_guid(ni_device_queue_t *device_queue, int device_type, int *guidn)
Definition: ni_rsrc_priv.cpp:375
ni_rsrc_get_lock_name
void ni_rsrc_get_lock_name(ni_device_type_t device_type, int32_t guid, char *p_name, size_t max_name_len)
Definition: ni_rsrc_priv.cpp:242
_ni_device_capability::fw_rev
uint8_t fw_rev[8]
Definition: ni_device_api.h:1166
ni_rsrc_enumerate_devices
int ni_rsrc_enumerate_devices(char ni_devices[][NI_MAX_DEVICE_NAME_LEN], int max_handles)
_ni_device_capability
device capability type
Definition: ni_device_api.h:1155
_ni_device_info::max_fps_4k
int max_fps_4k
Definition: ni_rsrc_api.h:126
ni_log2
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
CODERS_LCK_NAME
#define CODERS_LCK_NAME
Definition: ni_rsrc_priv.h:45
_ni_device_context::p_device_info
ni_device_info_t * p_device_info
Definition: ni_rsrc_api.h:149
ni_nvme.h
Private definitions for interfacing with NETINT video processing devices over NVMe.
_ni_session_context::load_query
ni_load_query_t load_query
Definition: ni_device_api.h:1502
_ni_device_video_capability::min_res_height
int min_res_height
Definition: ni_rsrc_api.h:80
_ni_sw_instance_info::id
int id
Definition: ni_rsrc_api.h:88
atoi
#define atoi(p_str)
Definition: ni_device_api.c:7263
_ni_device_video_capability::max_res_width
int max_res_width
Definition: ni_rsrc_api.h:77
END
#define END
Definition: ni_defs.h:336
ni_device_api.h
Public definitions for operating NETINT video processing devices for video processing.
_ni_device_capability::serial_number
uint8_t serial_number[20]
Definition: ni_device_api.h:1163
_ni_device_info::model_number
uint8_t model_number[40]
Definition: ni_rsrc_api.h:123
NI_DEVICE_TYPE_XCODER_MAX
@ NI_DEVICE_TYPE_XCODER_MAX
Definition: ni_defs.h:362
_ni_hw_capability::codec_format
uint8_t codec_format
Definition: ni_device_api.h:1141
NI_RETCODE_FAILURE
@ NI_RETCODE_FAILURE
Definition: ni_defs.h:440
_ni_hw_capability::max_video_width
uint16_t max_video_width
Definition: ni_device_api.h:1143
_ni_device_video_capability::supports_codec
int supports_codec
Definition: ni_rsrc_api.h:76
_ni_device_info::fw_build_id
uint8_t fw_build_id[256]
Definition: ni_rsrc_api.h:120
ni_device_capability_query2
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...
Definition: ni_device_api.c:647
_ni_context_query::video_height
uint32_t video_height
Definition: ni_device_api.h:1193
ni_util.h
Utility definitions.
GET_XCODER_DEVICE_TYPE_STR
#define GET_XCODER_DEVICE_TYPE_STR(t)
Definition: ni_defs.h:429
ni_rsrc_get_shm_name
void ni_rsrc_get_shm_name(ni_device_type_t device_type, int32_t guid, char *p_name, size_t max_name_len)
Definition: ni_rsrc_priv.cpp:265
_ni_device_queue::xcoder_cnt
uint32_t xcoder_cnt[NI_DEVICE_TYPE_XCODER_MAX]
Definition: ni_rsrc_api.h:69
_ni_device_info::load
int load
Definition: ni_rsrc_api.h:108
MAX_CHAR_IN_DEVICE_NAME
#define MAX_CHAR_IN_DEVICE_NAME
Definition: ni_device_api.h:185
_ni_device_info::device_type
ni_device_type_t device_type
Definition: ni_rsrc_api.h:129
NI_LOG_DEBUG
@ NI_LOG_DEBUG
Definition: ni_log.h:62
get_dev_pcie_addr
void get_dev_pcie_addr(char *device_name, char *pcie, char *domain, char *slot, char *dev, char *func)
_ni_load_query::context_status
ni_context_query_t context_status[NI_MAX_CONTEXTS_PER_HW_INSTANCE]
Definition: ni_device_api.h:1221
NI_PARAM_MIN_HEIGHT
#define NI_PARAM_MIN_HEIGHT
Definition: ni_device_api.h:138
_ni_device_info::fw_branch_name
uint8_t fw_branch_name[256]
Definition: ni_rsrc_api.h:116
_ni_device_capability::device_is_xcoder
uint8_t device_is_xcoder
Definition: ni_device_api.h:1157
_ni_device_video_capability::level
char level[NI_LEVELS_SUPP_STR_LEN]
Definition: ni_rsrc_api.h:82
_ni_hw_capability::max_number_of_contexts
uint8_t max_number_of_contexts
Definition: ni_device_api.h:1139
NI_RETCODE_ERROR_LOCK_DOWN_DEVICE
@ NI_RETCODE_ERROR_LOCK_DOWN_DEVICE
Definition: ni_defs.h:519