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