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