36 #if defined(__linux__) || defined(__APPLE__)
38 #include <sys/types.h>
52 static uint32_t decoder_guid = 0;
53 static uint32_t encoder_guid = 0;
54 static int int_cmp(
const void *a,
const void *b);
118 if (ni_logan_devices == NULL || max_handles == 0)
128 const char* dir_name =
"/dev";
129 int i, xcoder_device_cnt = 0;
131 struct dirent* in_file;
134 ni_device_handle_t dev_handle = NI_INVALID_DEVICE_HANDLE;
136 uint32_t tmp_io_size;
138 if (NULL == (FD = opendir(dir_name)))
146 while ((in_file = readdir(FD)))
149 if (!strcmp(in_file->d_name,
".") || !strcmp(in_file->d_name,
".."))
156 size_t lenstr = strlen(in_file->d_name);
163 #ifdef XCODER_LINUX_VIRTIO_DRIVER_ENABLED
166 if (isdigit(in_file->d_name[i]))
168 if (!isdigit(in_file->d_name[i]))
178 sprintf(device_info.
dev_name,
"/dev/%s", in_file->d_name);
184 if (NI_INVALID_DEVICE_HANDLE != dev_handle)
191 ni_logan_devices[xcoder_device_cnt][0] =
'\0';
192 strncat(ni_logan_devices[xcoder_device_cnt],
205 "limit of LOGAN_MAX_DEVICE_CNT(%d) or max_handles(%d)\n",
214 return xcoder_device_cnt;
230 const char *dev_name)
235 int *module_id_arr = NULL;
236 int module_count = 0;
238 if (dev_name == NULL)
246 module_id_arr = (
int *)malloc(
sizeof(
int) * module_count);
252 memcpy(module_id_arr, coders->
decoders,
sizeof(
int) * module_count);
258 module_id_arr = (
int *)malloc(
sizeof(
int) * module_count);
264 memcpy(module_id_arr, coders->
encoders,
sizeof(
int) * module_count);
272 for (
int i = 0; i < module_count; i++)
275 if (! p_device_context)
278 __func__, module_id_arr[i], device_type);
313 HANDLE map_file_handle = NULL;
314 HANDLE mutex_handle = NULL;
317 mutex_handle = CreateMutex(
323 if (NULL == mutex_handle)
331 if (WAIT_ABANDONED == WaitForSingleObject(mutex_handle, INFINITE))
333 ni_log(
NI_LOG_INFO,
"ERROR: %s() failed to obtain mutex: %p\n", __FUNCTION__, mutex_handle);
336 map_file_handle = OpenFileMapping(
342 if (NULL == map_file_handle)
344 ReleaseMutex(mutex_handle);
357 if (NULL == p_device_queue)
359 ReleaseMutex(mutex_handle);
360 CloseHandle(map_file_handle);
365 if (NULL == p_device_pool)
369 UnmapViewOfFile(p_device_queue);
373 p_device_pool->
lock = mutex_handle;
377 ReleaseMutex(mutex_handle);
378 CloseHandle(map_file_handle);
381 ni_lock_handle_t lock;
384 char workDir[] =
"/dev/shm_netint";
385 if (0 != access(workDir,0))
387 if (0 != mkdir(workDir,777))
394 lock = open(
CODERS_LCK_NAME, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
400 flock(lock, LOCK_EX);
412 Return<void> retvalue =
service_logan->GetAppFlag(param, [&](int32_t ret, hidl_handle handle)
417 shm_fd = dup(handle->data[0]);
425 if (!retvalue.isOk())
435 native_handle_t *handle = native_handle_create(1, 0);
436 handle->data[0] = fd;
448 struct stat statbuf = { 0 };
450 shm_fd = shm_open(
CODERS_SHM_NAME, O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
458 if (fstat(shm_fd, &statbuf) < 0)
473 MAP_SHARED, shm_fd, 0);
474 if (MAP_FAILED == p_device_queue)
484 ni_log(
NI_LOG_ERROR,
"Error: CODERS_SHM_NAME decoders_cnt (%d) or encoders_cnt (%d) is out of range\n",
490 if (NULL == p_device_pool)
497 p_device_pool->
lock = lock;
503 flock(lock, LOCK_UN);
505 if (NULL == p_device_pool)
515 return p_device_pool;
547 int i = 0, j = 0, k = 0, xcoder_device_cnt = 0, compat_xcoder_device_cnt = 0,\
549 uint32_t tmp_io_size;
550 ni_lock_handle_t lock = NI_INVALID_DEVICE_HANDLE;
551 ni_lock_handle_t lock_enc = NI_INVALID_DEVICE_HANDLE;
552 ni_lock_handle_t lock_dec = NI_INVALID_DEVICE_HANDLE;
557 ni_device_handle_t dev_handle = NI_INVALID_DEVICE_HANDLE;
561 HANDLE map_file_handle = NULL;
563 map_file_handle = CreateFileMapping(
564 INVALID_HANDLE_VALUE,
572 if (NULL == map_file_handle)
582 if (ERROR_ALREADY_EXISTS == rc)
584 ni_log(
NI_LOG_INFO,
"NETINT resources have been initialized already, exiting ..\n");
590 ni_log(
NI_LOG_INFO,
"NETINT resources not initialized, starting initialization ..\n");
594 while (0 == xcoder_device_cnt)
597 if (xcoder_device_cnt < 0)
599 ni_log(
NI_LOG_INFO,
"ni_logan_rsrc_init() fatal error %d, exiting ...\n", xcoder_device_cnt);
603 else if (0 == xcoder_device_cnt)
612 if (runtime >= timeout_seconds && timeout_seconds != 0)
635 if (NULL == p_device_queue)
655 if (WAIT_ABANDONED == WaitForSingleObject(lock, INFINITE))
677 if (should_match_rev == 2)
679 for (i = 0; i < xcoder_device_cnt; i++)
690 if (NI_INVALID_DEVICE_HANDLE == dev_handle)
700 "compatible FW. Not initializing any \n"
709 for (i = 0; i < xcoder_device_cnt; i++)
719 if (NI_INVALID_DEVICE_HANDLE == dev_handle)
727 (! should_match_rev ||
730 fw_ver_compat_warning = 0;
733 ni_log(
NI_LOG_INFO,
"WARNING - Query %s FW version: %.*s is below the minimum support version for "
734 "this SW version. Some features may be missing.\n",
736 device_capabilites.
fw_rev);
737 fw_ver_compat_warning = 1;
742 for (j = 0; j < total_modules; j++)
744 p_device_info = NULL;
752 sizeof(device_info.
fw_rev));
769 for (k = 0; k < device_cnt_so_far; k++)
788 "decoder" :
"encoder", device_info.
hw_id);
795 "decoder" :
"encoder", device_info.
hw_id);
796 p_device_info = &device_info;
806 p_device_info->
module_id = decoder_guid++;
812 p_device_info->
module_id = encoder_guid++;
823 ni_log(
NI_LOG_INFO,
"Query %s rc %d NOT xcoder-support: %u, or mismatch revision: "
824 "%s; Not added\n", device_info.
dev_name, retval,
826 device_capabilites.
fw_rev);
841 if (NULL != p_device_queue)
843 UnmapViewOfFile(p_device_queue);
845 if (NULL != map_file_handle)
847 CloseHandle(map_file_handle);
857 UnmapViewOfFile(p_device_queue);
863 struct dirent* in_file;
864 const char* dir_name =
"/dev";
871 char workDir[] =
"/dev/shm_netint";
872 if (0 != access(workDir,0))
874 if (0 != mkdir(workDir,777))
876 perror(
"Error create /dev/shm_netint folder...");
888 Return<void> retvalue =
service_logan->GetAppFlag(param, [&](int32_t ret, hidl_handle handle)
893 shm_fd = dup(handle->data[0]);
901 if (!retvalue.isOk())
911 native_handle_t *handle = native_handle_create(1, 0);
912 handle->data[0] = fd;
924 int shm_fd = shm_open(
CODERS_SHM_NAME, O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
948 if (xcoder_device_cnt == 0)
957 if (runtime >= timeout_seconds && timeout_seconds != 0)
969 ni_log(
NI_LOG_INFO,
"Compatible minimum FW ver: %s, FW API flavors: %s, minimum API ver: %d\n",
990 retvalue =
service_logan->GetAppFlag(param, [&](int32_t ret, hidl_handle handle)
995 shm_fd = dup(handle->data[0]);
1003 if (!retvalue.isOk())
1013 native_handle_t *handle = native_handle_create(1, 0);
1014 handle->data[0] = fd;
1021 shm_fd = shm_open(
CODERS_SHM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1040 MAP_SHARED, shm_fd, 0);
1041 if (MAP_FAILED == p_device_queue)
1054 lock = open(
CODERS_LCK_NAME, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1064 lock_enc = open(
CODERS_RETRY_ENLCK_NAME, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1073 lock_dec = open(
CODERS_RETRY_DELCK_NAME, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1082 flock(lock, LOCK_EX);
1097 if (should_match_rev == 2)
1099 for (i = 0; i < xcoder_device_cnt; i++)
1105 strncpy(device_info.
dev_name, dev_names[i],
1114 &device_capabilites);
1118 flock(lock, LOCK_UN);
1120 "compatible FW. Not initializing any \n"
1129 for (i = 0; i < xcoder_device_cnt; i++)
1143 if (NI_INVALID_DEVICE_HANDLE != dev_handle)
1150 fw_ver_compat_warning = 0;
1153 ni_log(
NI_LOG_INFO,
"WARNING - Query %s %s FW version: %.*s is below the minimum support version for "
1154 "this SW version. Some features may be missing.\n",
1157 (
int)
sizeof(device_capabilites.
fw_rev),
1158 device_capabilites.
fw_rev);
1159 fw_ver_compat_warning = 1;
1167 for (j = 0; j < total_modules; j++)
1169 p_device_info = NULL;
1177 sizeof(device_info.
fw_rev));
1193 for (k = 0; k < device_cnt_so_far; k++)
1213 "decoder" :
"encoder", device_info.
hw_id);
1220 "decoder" :
"encoder", device_info.
hw_id);
1221 p_device_info = &device_info;
1231 p_device_info->
module_id = decoder_guid++;
1237 p_device_info->
module_id = encoder_guid++;
1246 compat_xcoder_device_cnt++;
1250 ni_log(
NI_LOG_INFO,
"Device %s not added as it is not an xcoder, or has incompatible FW rev: %s\n",
1252 device_capabilites.
fw_rev);
1259 "system over limit of LOGAN_MAX_DEVICE_CNT(%d)\n",
1271 flock(lock, LOCK_UN);
1273 retval = xcoder_device_cnt;
1296 static int is_str_in_str_array(
const char key[],
1302 for (i = 0; i < array_size; i++)
1304 if (0 == strcmp(key, arr[i]))
1329 int xcoder_dev_count = 0;
1331 int curr_dev_count = 0;
1336 if (NULL == saved_coders) {
1367 xcoder_dev_names[i][0] =
'\0';
1373 ni_log(
NI_LOG_ERROR,
"Error the names of the encoding(%s) and decoding(%s) devices don't match, Index:%d\n",
1389 for (i = 0; i < xcoder_dev_count; i++)
1391 if (! is_str_in_str_array(xcoder_dev_names[i], curr_dev_names,
1395 xcoder_dev_names[i]);
1410 for (i = 0; i < curr_dev_count; i++)
1412 if (! is_str_in_str_array(curr_dev_names[i], xcoder_dev_names,
1415 ni_log(
NI_LOG_INFO,
"\n\n%s NOT in previous list, adding !\n",curr_dev_names[i]);
1452 char shm_name[32] = { 0 };
1453 char lck_name[32] = { 0 };
1469 HANDLE map_file_handle = NULL;
1470 HANDLE mutex_handle = NULL;
1473 mutex_handle = CreateMutex(
1478 if (NULL == mutex_handle)
1481 p_device_context = NULL;
1485 if (WAIT_ABANDONED == WaitForSingleObject(mutex_handle, INFINITE))
1487 ni_log(
NI_LOG_INFO,
"ERROR: %s() failed to obtain mutex: %p\n", __FUNCTION__, mutex_handle);
1490 map_file_handle = OpenFileMapping(
1491 FILE_MAP_ALL_ACCESS,
1495 if (NULL == map_file_handle)
1499 p_device_context = NULL;
1504 FILE_MAP_ALL_ACCESS,
1509 if (NULL == p_device_queue)
1513 p_device_context = NULL;
1518 if (NULL == p_device_context)
1522 p_device_context = NULL;
1526 p_device_context->
shm_name[0] =
'\0';
1527 strncat(p_device_context->
shm_name, shm_name,
sizeof(p_device_context->
shm_name));
1528 p_device_context->
lock = mutex_handle;
1533 if (NULL == p_device_context)
1535 if (NULL != p_device_queue)
1537 UnmapViewOfFile(p_device_queue);
1539 if (NULL != mutex_handle)
1541 ReleaseMutex(mutex_handle);
1542 CloseHandle(mutex_handle);
1545 if (NULL != map_file_handle)
1547 CloseHandle(map_file_handle);
1552 ReleaseMutex(mutex_handle);
1553 CloseHandle(map_file_handle);
1560 lock = open(lck_name, O_CREAT | O_RDWR | O_CLOEXEC, S_IRWXU | S_IRWXG | S_IRWXO);
1562 lock = open(lck_name, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1571 flock(lock, LOCK_EX);
1581 string param = shm_name;
1582 Return<void> retvalue =
service_logan->GetAppFlag(param, [&](int32_t ret, hidl_handle handle)
1587 shm_fd = dup(handle->data[0]);
1595 if (!retvalue.isOk())
1605 native_handle_t *handle = native_handle_create(1, 0);
1606 handle->data[0] = fd;
1613 shm_fd = shm_open(shm_name, O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1623 PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
1624 if (MAP_FAILED == p_device_queue)
1632 if (!p_device_context)
1640 strncpy(p_device_context->
shm_name, shm_name,
sizeof(shm_name));
1641 p_device_context->
lock = lock;
1646 flock(lock, LOCK_UN);
1654 return p_device_context;
1666 if (p_device_context)
1670 CloseHandle(p_device_context->
lock);
1671 #elif defined(__linux__) || defined(__APPLE__)
1672 close(p_device_context->
lock);
1675 free(p_device_context);
1676 p_device_context = NULL;
1696 int * p_device_count)
1703 bool b_release_pool_mtx =
false;
1705 if ( (NULL == p_device_info) || (NULL == p_device_count) )
1712 if (NULL == p_device_pool)
1719 if (WAIT_ABANDONED == WaitForSingleObject(p_device_pool->
lock, INFINITE))
1722 __FUNCTION__, p_device_pool->
lock);
1726 #elif defined(__linux__) || defined(__APPLE__)
1727 flock(p_device_pool->
lock, LOCK_EX);
1730 b_release_pool_mtx =
true;
1742 for (i = 0; i < count; i++)
1747 guid = p_device_queue->
decoders[i];
1751 guid = p_device_queue->
encoders[i];
1755 if (p_device_context)
1759 if (WAIT_ABANDONED == WaitForSingleObject(p_device_context->
lock, INFINITE))
1762 __FUNCTION__, p_device_context->
lock);
1763 ReleaseMutex(p_device_pool->
lock);
1769 ReleaseMutex(p_device_context->
lock);
1770 #elif defined(__linux__) || defined(__APPLE__)
1771 flock(p_device_context->
lock, LOCK_EX);
1773 flock(p_device_context->
lock, LOCK_UN);
1778 (*p_device_count)++;
1790 if (b_release_pool_mtx)
1793 ReleaseMutex(p_device_pool->
lock);
1794 #elif defined(__linux__) || defined(__APPLE__)
1795 flock(p_device_pool->
lock, LOCK_UN);
1821 if (NULL == p_device)
1855 "Decoder" :
"Encoder", p_device_info->
module_id);
1861 "no, possible missing features" :
"yes");
1923 if (NULL != p_xcoders)
1966 if (NULL == p_device_context)
1972 if (NULL == p_device_info)
1981 if (WAIT_ABANDONED == WaitForSingleObject(p_device_context->
lock, INFINITE))
1984 __FUNCTION__, p_device_context->
lock);
1985 free(p_device_info);
1990 ReleaseMutex(p_device_context->
lock);
1992 flock(p_device_context->
lock, LOCK_EX);
1996 flock(p_device_context->
lock, LOCK_UN);
2003 return p_device_info;
2031 int num_sw_instances = 0;
2038 int least_model_load = 0;
2054 if (WAIT_ABANDONED == WaitForSingleObject(p_device_pool->
lock, INFINITE))
2057 __FUNCTION__, p_device_pool->
lock);
2061 #elif defined(__linux__) || defined(__APPLE__)
2062 flock(p_device_pool->
lock, LOCK_EX);
2074 for (i = 0; i < num_coders; i++)
2085 if (! p_device_context)
2087 ni_log(
NI_LOG_ERROR,
"%s: Error get device resource, guid %d, device_type %d\n", __func__, tmp_id, device_type);
2095 if (NI_INVALID_DEVICE_HANDLE == p_session_context.
device_handle)
2108 if (NI_INVALID_EVENT_HANDLE == p_session_context.
event_handle)
2119 if (NI_INVALID_DEVICE_HANDLE != p_session_context.
device_handle)
2125 if (NI_INVALID_EVENT_HANDLE != p_session_context.
event_handle)
2144 if (WAIT_ABANDONED == WaitForSingleObject(p_device_context->
lock, INFINITE))
2147 __FUNCTION__, p_device_context->
lock);
2151 #elif defined(__linux__) || defined(__APPLE__)
2152 flock(p_device_context->
lock, LOCK_EX);
2158 if (i == 0 || p_dev_info->
model_load < least_model_load ||
2159 (p_dev_info->
model_load == least_model_load &&
2170 ReleaseMutex(p_device_context->
lock);
2171 #elif defined(__linux__) || defined(__APPLE__)
2172 flock(p_device_context->
lock, LOCK_UN);
2183 if ((!p_device_context) || (refCap.
fps == 0))
2189 unsigned long total_cap = refCap.
width * refCap.
height * refCap.
fps;
2190 unsigned long xcode_cap = width * height * frame_rate;
2194 "> total %ld (1) ..\n",
2195 xcode_cap, (
float)xcode_cap / total_cap,
2204 float fLoad = (
float)width * height * frame_rate * 100;
2205 if ((fLoad / fRef) > (100.0 - (float)load))
2213 ReleaseMutex(p_device_pool->
lock);
2214 #elif defined(__linux__) || defined(__APPLE__)
2215 flock(p_device_pool->
lock, LOCK_UN);
2224 width, height, frame_rate,
2225 guid, guid == -1 ?
"NULL" : dev_info.
dev_name,
2226 guid == -1 ? -1 : dev_info.
hw_id);
2229 if ( (p_device_info) && (guid >= 0) )
2252 int sw_instance_cnt,
2256 if (!p_device_context || !sw_instance_info)
2258 ni_log(
NI_LOG_INFO,
"Error in resource update device load: invalid input pointers ..\n");
2264 if (WAIT_ABANDONED == WaitForSingleObject(p_device_context->
lock, INFINITE))
2267 __FUNCTION__, p_device_context->
lock);
2270 #elif defined(__linux__) || defined(__APPLE__)
2271 flock(p_device_context->
lock, LOCK_EX);
2276 for (i = 0; i < sw_instance_cnt; i++)
2282 ReleaseMutex(p_device_context->
lock);
2283 #elif defined(__linux__) || defined(__APPLE__)
2284 flock(p_device_context->
lock, LOCK_UN);
2317 unsigned long* p_load)
2327 int num_sw_instances = 0;
2328 int least_model_load = 0;
2339 if (WAIT_ABANDONED == WaitForSingleObject(p_device_pool->
lock, INFINITE))
2342 __FUNCTION__, p_device_pool->
lock);
2344 #elif defined(__linux__) || defined(__APPLE__)
2345 flock(p_device_pool->
lock, LOCK_EX);
2354 for (i = 0; i < count; i++)
2358 if (!p_device_context)
2360 ni_log(
NI_LOG_ERROR,
"%s: Error get device resource, guid %d, device_type %d\n", __func__, coders[i], device_type);
2369 if (NI_INVALID_DEVICE_HANDLE == p_session_context.
device_handle)
2383 if (NI_INVALID_EVENT_HANDLE != p_session_context.
event_handle)
2412 if (WAIT_ABANDONED == WaitForSingleObject(p_device_context->
lock, INFINITE))
2415 __FUNCTION__, p_device_context->
lock);
2417 #elif defined(__linux__) || defined(__APPLE__)
2418 flock(p_device_context->
lock, LOCK_EX);
2426 load = p_device_info->
load;
2427 least_model_load = p_device_info->
model_load;
2431 ni_log(
NI_LOG_INFO,
"Coder [%d]: %d , load: %d (%d), activ_inst: %d , max_inst %d\n",
2452 if (p_device_info->
model_load < least_model_load)
2455 least_model_load = p_device_info->
model_load;
2458 else if (p_device_info->
load < load)
2461 load = p_device_info->
load;
2468 ReleaseMutex(p_device_context->
lock);
2469 #elif defined(__linux__) || defined(__APPLE__)
2470 flock(p_device_context->
lock, LOCK_UN);
2481 unsigned long total_cap = refCap.
width * refCap.
height * refCap.
fps;
2485 p_device_context = NULL;
2490 *p_load = width * height * frame_rate;
2494 if (WAIT_ABANDONED == WaitForSingleObject(p_device_context->
lock, INFINITE))
2497 __FUNCTION__, p_device_context->
lock);
2499 #elif defined(__linux__) || defined(__APPLE__)
2500 flock(p_device_context->
lock, LOCK_EX);
2504 ReleaseMutex(p_device_context->
lock);
2505 #elif defined(__linux__) || defined(__APPLE__)
2507 MS_SYNC | MS_INVALIDATE))
2511 flock(p_device_context->
lock, LOCK_UN);
2519 p_device_context = NULL;
2524 ReleaseMutex(p_device_pool->
lock);
2525 #elif defined(__linux__) || defined(__APPLE__)
2526 flock(p_device_pool->
lock, LOCK_UN);
2531 return p_device_context;
2563 unsigned long* p_load)
2566 if (p_device_context)
2570 if (WAIT_ABANDONED == WaitForSingleObject(p_device_context->
lock, INFINITE))
2573 __FUNCTION__, p_device_context->
lock);
2575 #elif defined(__linux__) || defined(__APPLE__)
2576 flock(p_device_context->
lock, LOCK_EX);
2586 unsigned long total_cap = refCap.
width * refCap.
height * refCap.
fps;
2589 *p_load = width * height * frame_rate;
2591 #if defined(__linux__) || defined(__APPLE__)
2593 MS_SYNC | MS_INVALIDATE))
2602 ReleaseMutex(p_device_context->
lock);
2603 #elif defined(__linux__) || defined(__APPLE__)
2604 flock(p_device_context->
lock, LOCK_UN);
2607 return p_device_context;
2634 return p_device_context;
2655 unsigned long total_cap = refCap.
width * refCap.
height * refCap.
fps;
2659 if (WAIT_ABANDONED == WaitForSingleObject(p_device_context->
lock, INFINITE))
2662 __FUNCTION__, p_device_context->
lock);
2664 #elif defined(__linux__) || defined(__APPLE__)
2665 flock(p_device_context->
lock, LOCK_EX);
2670 ni_log(
NI_LOG_INFO,
"Warning: releasing resource load %ld > current load %ld\n",
2676 #if defined(__linux__) || defined(__APPLE__)
2685 ReleaseMutex(p_device_context->
lock);
2686 #elif defined(__linux__) || defined(__APPLE__)
2687 flock(p_device_context->
lock, LOCK_UN);
2724 if (NI_INVALID_EVENT_HANDLE == session_ctx.
event_handle)
2773 uint32_t max_nvme_io_size = 0;
2774 bool b_release_pool_mtx =
false;
2801 if (WAIT_ABANDONED == WaitForSingleObject(p_device_pool->
lock, INFINITE))
2803 ni_log(
NI_LOG_INFO,
"ERROR: ni_logan_rsrc_list_devices() failed to obtain mutex: %p\n", p_device_pool->
lock);
2807 #elif defined(__linux__) || defined(__APPLE__)
2808 flock(p_device_pool->
lock, LOCK_EX);
2810 b_release_pool_mtx =
true;
2829 if (NI_INVALID_EVENT_HANDLE == session_ctx.
event_handle)
2860 guid, p_device_ctx);
2866 if (b_release_pool_mtx)
2869 ReleaseMutex(p_device_pool->
lock);
2870 #elif defined(__linux__) || defined(__APPLE__)
2871 flock(p_device_pool->
lock, LOCK_UN);
2902 uint32_t max_nvme_io_size = 0;
2903 bool b_release_pool_mtx =
false;
2954 if (WAIT_ABANDONED == WaitForSingleObject(p_device_pool->
lock, INFINITE))
2956 ni_log(
NI_LOG_INFO,
"ERROR: ni_logan_rsrc_list_devices() failed to obtain mutex: %p\n", p_device_pool->
lock);
2960 #elif defined(__linux__) || defined(__APPLE__)
2961 lockf(p_device_pool->
lock, F_LOCK, 0);
2963 b_release_pool_mtx =
true;
2982 if (NI_INVALID_EVENT_HANDLE == session_ctx.
event_handle)
3046 guid, p_device_ctx);
3052 if (b_release_pool_mtx)
3055 ReleaseMutex(p_device_pool->
lock);
3056 #elif defined(__linux__) || defined(__APPLE__)
3057 lockf(p_device_pool->
lock, F_ULOCK, 0);
3090 int load_threshold,
int task_num_threshold,
3093 int should_match_rev = 1;
3094 int module_count = 0;
3096 int32_t *module_id_arr = NULL;
3097 int32_t best_load_module_id = -1;
3102 int *card_remove = NULL;
3103 uint64_t reserved_memory = 0;
3104 int needed_memory = 0;
3109 if (p_hw_device_info == NULL ||
3110 (task_mode != 0 && task_mode != 1) ||
3111 load_threshold <= 0 ||
3112 load_threshold > 100 ||
3113 task_num_threshold < 0 ||
3118 if (p_hw_device_info != NULL)
3123 ni_log(
NI_LOG_ERROR,
"Error Invalid input params: p_hw_device_info %p realtime %d load_thread %d task_num_threshold %d device_type %d\n",
3124 p_hw_device_info, task_mode, load_threshold, task_num_threshold, device_type);
3151 if (WAIT_ABANDONED == WaitForSingleObject(p_device_pool->
lock, INFINITE))
3156 #elif defined(__linux__)
3157 if (flock(p_device_pool->
lock, LOCK_EX))
3176 if (p_hw_device_info->
card_info == NULL) {
3199 card_remove = (int32_t *)malloc(
sizeof(int32_t) * p_hw_device_info->
available_card_num);
3216 module_id_arr = (int32_t *)malloc(
sizeof(int32_t) * module_count);
3225 memcpy(module_id_arr, coders->
decoders,
sizeof(int32_t) * module_count);
3228 memcpy((
void *)&best_load_module_id, module_id_arr,
sizeof(int32_t));
3229 qsort(module_id_arr, module_count,
sizeof(int32_t), int_cmp);
3237 memset(&xCtxt, 0,
sizeof(xCtxt));
3241 if (p_device_context)
3309 (device_type == 0 ?
"Decoder" :
"Encoder"), i,
3331 card_remove[i] == 0)
3340 card_remove[i] == 0 &&
3354 card_remove[i] == 0)
3371 if (resolution <= 1280*720)
3375 else if (resolution <= 1920*1080)
3379 else if (resolution <= 3840*2160)
3389 needed_memory = needed_memory > resolution * 3 / 2 * 12 + 7 * 1024 * 1024 ? needed_memory : resolution * 3 / 2 * 12 + 7 * 1024 * 1024;
3390 if (reserved_memory < needed_memory)
3399 needed_memory = needed_memory > 39 * 1024 * 1024 ? needed_memory : 39 * 1024 * 1024;
3400 if (reserved_memory < needed_memory)
3409 ReleaseMutex((HANDLE)p_device_pool->
lock);
3411 #elif defined(__linux__)
3412 if (flock(p_device_pool->
lock, LOCK_UN))
3424 free(module_id_arr);
3432 ((device_type == 0) ?
"decode" :
"encode"), p_hw_device_info->
card_current_card, retval);
3462 if (WAIT_ABANDONED == WaitForSingleObject(p_device_pool->
lock, INFINITE))
3464 ni_log(
NI_LOG_INFO,
"ERROR: ni_logan_rsrc_release_resource() failed to obtain mutex: %p\n",
3465 p_device_pool->
lock);
3467 #elif defined(__linux__) || defined(__APPLE__)
3468 flock(p_device_pool->
lock, LOCK_EX);
3474 for (i = 0; i < count; i++)
3476 guid = p_device_queue->
decoders[i];
3479 if (p_decoder_device_ctx && p_encoder_device_ctx && 0 == strcmp(p_decoder_device_ctx->
p_device_info->
dev_name, dev) &&
3480 0 == strcmp(p_encoder_device_ctx->p_device_info->dev_name, dev))
3483 char enc_lck_name[32] = {0};
3484 char dec_lck_name[32] = {0};
3488 "enc_guid %d shm_name: %s lck_name: %s \n",
3489 guid, p_decoder_device_ctx->
shm_name, dec_lck_name,
3490 guid, p_encoder_device_ctx->shm_name, enc_lck_name);
3508 CloseHandle(p_decoder_device_ctx->
lock);
3509 CloseHandle(p_encoder_device_ctx->lock);
3511 #elif defined(__linux__) || defined(__APPLE__)
3515 if (0 == shm_unlink(p_decoder_device_ctx->
shm_name))
3525 if (0 == shm_unlink(p_encoder_device_ctx->shm_name))
3527 ni_log(
NI_LOG_INFO,
"enc shm_name %s deleted.\n", p_encoder_device_ctx->shm_name);
3531 ni_log(
NI_LOG_INFO,
"enc shm_name %s deletion failure.\n", p_encoder_device_ctx->shm_name);
3535 if (0 == unlink(dec_lck_name))
3545 if (0 == unlink(enc_lck_name))
3558 for (j = i + 1; j < count; j++)
3563 p_device_queue->
decoders[count - 1] = -1;
3564 p_device_queue->
encoders[count - 1] = -1;
3567 #if defined(__linux__) || defined(__APPLE__)
3569 MS_SYNC | MS_INVALIDATE))
3587 ReleaseMutex(p_device_pool->
lock);
3588 #elif defined(__linux__) || defined(__APPLE__)
3589 flock(p_device_pool->
lock, LOCK_UN);
3597 static int int_cmp(
const void *a,
const void *b)
3599 const int *ia = (
const int *)a;
3600 const int *ib = (
const int *)b;
3620 uint32_t i=0, j=0, k=0, count=0, fw_ver_compat_warning=0;
3622 ni_device_handle_t fd = 0;
3640 if (WAIT_ABANDONED == WaitForSingleObject(p_device_pool->
lock, INFINITE))
3642 ni_log(
NI_LOG_INFO,
"ERROR: ni_logan_rsrc_release_resource() failed to obtain mutex: %p\n",
3643 p_device_pool->
lock);
3645 #elif defined(__linux__) || defined(__APPLE__)
3646 flock(p_device_pool->
lock, LOCK_EX);
3653 for (i = 0; i < count; i++)
3669 guids[i] = p_device_queue->
decoders[i];
3673 qsort(guids, count,
sizeof(
int), int_cmp);
3674 for (i = 0; i < count; i++)
3676 if (1 == guids[i] - guid)
3689 "system reach limit of LOGAN_MAX_DEVICE_CNT(%d)\n",
3702 sprintf(device_info.
dev_name,
"%s", dev);
3705 uint32_t tmp_io_size;
3710 if (NI_INVALID_DEVICE_HANDLE == fd)
3719 (! should_match_rev ||
3722 fw_ver_compat_warning = 0;
3725 ni_log(
NI_LOG_INFO,
"WARNING - Query %s FW version: %.*s is below the minimum support version for "
3726 "this SW version. Some features may be missing.\n",
3728 fw_ver_compat_warning = 1;
3736 int decoders_cnt = 0, encoders_cnt = 0;
3737 for (j = 0; j < total_modules; j++)
3743 sprintf(device_info.
dev_name,
"%s", dev);
3758 if ( (p_device_context) && (p_device_context->p_device_info->hw_id == device_info.
hw_id) )
3760 pCoderInfo = p_device_context->p_device_info;
3766 "encoder", device_info.
hw_id);
3783 "encoder", device_info.
hw_id);
3784 pCoderInfo = &device_info;
3821 #if defined(__linux__) || defined(__APPLE__)
3823 MS_SYNC | MS_INVALIDATE))
3834 ni_log(
NI_LOG_INFO,
"Query %s rc %d NOT xcoder-support: %u, or mismatch revision: "
3839 if (NI_INVALID_DEVICE_HANDLE != fd)
3845 ReleaseMutex(p_device_pool->
lock);
3846 #elif defined(__linux__) || defined(__APPLE__)
3847 flock(p_device_pool->
lock, LOCK_UN);
3867 if (NI_INVALID_LOCK_HANDLE != p_device_pool->
lock)
3870 CloseHandle(p_device_pool->
lock);
3871 #elif defined(__linux__) || defined(__APPLE__)
3872 close(p_device_pool->
lock);
3881 free(p_device_pool);
3882 p_device_pool = NULL;
3902 *lock = CreateMutex(
3911 __FUNCTION__, lock_name,
NI_ERRNO);
3923 *lock = open(lock_name, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3948 DWORD ret = WaitForSingleObject(*lock, 1);
3949 if (WAIT_OBJECT_0 == ret)
3953 else if (WAIT_TIMEOUT != ret)
3959 status = flock(*lock, LOCK_EX);
3971 while (status != 0);
3989 if (lock == NI_INVALID_LOCK_HANDLE)
4002 if (ReleaseMutex(lock))
4007 status = flock(lock, LOCK_UN);
@ NI_LOGAN_RETCODE_ERROR_INVALID_HANDLE
@ NI_LOGAN_RETCODE_ERROR_UNLOCK_DEVICE
@ NI_LOGAN_RETCODE_INVALID_PARAM
@ NI_LOGAN_RETCODE_ERROR_MEM_ALOC
@ NI_LOGAN_RETCODE_ERROR_VERSION_INCOMPATIBLE
@ NI_LOGAN_RETCODE_ERROR_VPU_RECOVERY
@ NI_LOGAN_RETCODE_ERROR_GET_DEVICE_POOL
@ NI_LOGAN_RETCODE_SUCCESS
@ NI_LOGAN_RETCODE_FAILURE
@ NI_LOGAN_RETCODE_ERROR_LOCK_DOWN_DEVICE
#define LOGAN_MAX_DEVICE_CNT
#define NI_LOGAN_RETCODE_INIT_ALREADY
#define NI_LOGAN_MAX_CONTEXTS_PER_HW_INSTANCE
#define NI_LOGAN_XCODER_REVISION
#define NI_LOGAN_XCODER_FW_API_VER_SUPPORTED_MIN
#define NI_LOGAN_XCODER_FW_VER_SUPPORTED_MIN
#define NI_LOGAN_MAX_DEVICE_NAME_LEN
@ NI_LOGAN_DEVICE_TYPE_UPLOAD
@ NI_LOGAN_DEVICE_TYPE_ENCODER
@ NI_LOGAN_DEVICE_TYPE_DECODER
#define VDI_INIT_VMEM_SIZE
#define NI_LOGAN_XCODER_FW_API_FLAVORS_SUPPORTED
ni_logan_retcode_t ni_logan_device_capability_query(ni_device_handle_t device_handle, ni_logan_device_capability_t *p_cap)
Queries device and returns device capability structure.
ni_device_handle_t ni_logan_device_open(const char *p_dev, uint32_t *p_max_io_size_out)
Opens device and returnes device device_handle if successful.
ni_logan_retcode_t ni_logan_device_session_query(ni_logan_session_context_t *p_ctx, ni_logan_device_type_t device_type)
Query session data from the device - Currently not implemented If device_type is NI_LOGAN_DEVICE_TYPE...
ni_logan_retcode_t ni_logan_device_session_close(ni_logan_session_context_t *p_ctx, int eos_recieved, ni_logan_device_type_t device_type)
Closes device session that was previously opened by calling ni_logan_device_session_open() If device_...
void ni_logan_device_session_context_init(ni_logan_session_context_t *p_ctx)
Initialize already allocated session context to a known state.
void ni_logan_close_event(ni_event_handle_t event_handle)
Closes event and releases resources.
void ni_logan_device_session_context_clear(ni_logan_session_context_t *p_ctx)
Clear already allocated session context to all zeros.
ni_logan_retcode_t ni_logan_encoder_init_default_params(ni_logan_encoder_params_t *p_param, int fps_num, int fps_denom, long bit_rate, int width, int height)
Initialize default encoder parameters.
ni_logan_retcode_t ni_logan_device_session_open(ni_logan_session_context_t *p_ctx, ni_logan_device_type_t device_type)
Opens a new device session depending on the device_type parameter If device_type is NI_LOGAN_DEVICE_T...
ni_event_handle_t ni_logan_create_event(void)
Create event and returnes event handle if successful.
void ni_logan_device_close(ni_device_handle_t device_handle)
Closes device and releases resources.
ni_logan_retcode_t ni_logan_decoder_init_default_params(ni_logan_decoder_params_t *p_param, int fps_num, int fps_denom, long bit_rate, int width, int height)
Initialize default decoder parameters.
#define NI_LOGAN_DEFAULT_KEEP_ALIVE_TIMEOUT
#define NI_LOGAN_MIN_BITRATE
void ni_log(ni_log_level_t level, const char *fmt,...)
print log message using ni_log_callback
int ni_logan_rsrc_android_init()
Init android net.int.SharedBuffer service for binder using.
android::sp< INidec > service_logan
ni_logan_rsrc_device_video_ref_cap_t g_device_reference_table[2][2]
ni_logan_retcode_t ni_logan_rsrc_list_all_devices(ni_logan_device_t *p_device)
List all devices with full information including s/w instances on the system.
ni_logan_device_context_t * ni_logan_rsrc_get_device_context(ni_logan_device_type_t device_type, int guid)
Allocates and returns a pointer to ni_logan_device_context_t struct based on provided device_type and...
int ni_logan_rsrc_check_hw_available(int guid, ni_logan_device_type_t device_type)
check the NetInt h/w device in resource pool on the host.
ni_logan_retcode_t ni_logan_rsrc_list_devices(ni_logan_device_type_t device_type, ni_logan_device_info_t *p_device_info, int *p_device_count)
List device(s) based on device type with full information including s/w instances on the system.
void ni_logan_rsrc_free_device_pool(ni_logan_device_pool_t *p_device_pool)
Free all resources taken by the device pool.
int ni_logan_rsrc_add_device(const char *dev, int should_match_rev)
Add an NetInt h/w device into resource pool on the host.
int ni_logan_find_device_index_by_name(void *device_coders, ni_logan_device_type_t device_type, const char *dev_name)
the card with the card name like /dev/nvme0n1 get the device index.
void ni_logan_rsrc_release_resource(ni_logan_device_context_t *p_device_context, ni_codec_t codec, unsigned long load)
Release resources allocated for decoding/encoding. function This must be called at the end of transco...
void ni_logan_rsrc_free_device_context(ni_logan_device_context_t *p_device_context)
Free previously allocated device context.
ni_logan_device_context_t * ni_logan_rsrc_allocate_direct(ni_logan_device_type_t device_type, int guid, ni_codec_t codec, int width, int height, int frame_rate, unsigned long *p_load)
Allocate resources for decoding/encoding, by designating explicitly the device to use.
int ni_logan_rsrc_get_local_device_list(char ni_logan_devices[][NI_LOGAN_MAX_DEVICE_NAME_LEN], int max_handles)
Scans system for all NVMe devices and returns the system device names to the user which were identifi...
ni_logan_device_context_t * ni_logan_rsrc_allocate_auto(ni_logan_device_type_t device_type, ni_alloc_rule_t rule, ni_codec_t codec, int width, int height, int frame_rate, unsigned long *p_load)
Allocate resources for decoding/encoding, based on the provided rule.
int ni_logan_rsrc_codec_is_available(int guid, ni_logan_device_type_t device_type)
check the NetInt h/w device in resource pool on the host.
ni_logan_device_context_t * ni_logan_rsrc_allocate_simple_direct(ni_logan_device_type_t device_type, int guid)
Allocate resources for decoding/encoding, by designating explicitly the device to use....
int ni_logan_rsrc_check_sw_instance(ni_logan_device_context_t *p_device_context, ni_logan_device_type_t device_type)
Check software instance.
int ni_logan_rsrc_remove_device(const char *dev)
Remove an NetInt h/w device from resource pool on the host.
LIB_API void ni_logan_rsrc_print_all_devices_capability(void)
Print detailed capability information of all devices on the system.
int ni_logan_rsrc_init(int should_match_rev, int timeout_seconds)
Initialize and create all resources required to work with NETINT NVMe transcoder devices....
int ni_logan_rsrc_unlock(int device_type, ni_lock_handle_t lock)
unlock a file lock
ni_logan_device_info_t * ni_logan_rsrc_get_device_info(ni_logan_device_type_t device_type, int guid)
Query a specific device with detailed information on the system.
int ni_logan_check_hw_info(hw_device_info_t *p_hw_device_info, int task_mode, int load_threshold, int task_num_threshold, ni_logan_device_type_t device_type, int resolution)
check hw info, return the appropriate card number to use depends on the load&task_num&used resource
ni_logan_retcode_t ni_logan_rsrc_refresh(int should_match_rev)
Scan and refresh all resources on the host, taking into account hot-plugged and pulled out cards.
void ni_logan_rsrc_print_device_info(const ni_logan_device_info_t *p_device_info)
Print the content of the ni_logan_device_info_t struct.
ni_logan_device_pool_t * ni_logan_rsrc_get_device_pool(void)
Create and return the allocated ni_logan_device_pool_t struct.
int ni_logan_rsrc_update_device_load(ni_logan_device_context_t *p_device_context, int load, int sw_instance_cnt, const ni_logan_sw_instance_info_t sw_instance_info[])
Update the load value and s/w instances info of a specific decoder or encoder. This is used by resour...
int ni_logan_rsrc_get_available_device(int width, int height, int frame_rate, ni_codec_t codec, ni_logan_device_type_t device_type, ni_logan_device_info_t *p_device_info)
Get the least used device that can handle decoding or encoding a video stream of certain resolution/f...
int ni_logan_rsrc_lock_and_open(int device_type, ni_lock_handle_t *lock)
lock a file lock and open a session on a device
Exported definitions related to resource management of NI T-408 devices.
@ EN_ALLOC_LEAST_INSTANCE
void ni_logan_rsrc_get_lock_name(ni_logan_device_type_t device_type, int32_t guid, char *p_name, size_t max_name_len)
Returns the device lock name.
ni_logan_retcode_t ni_logan_rsrc_fill_device_info(ni_logan_device_info_t *p_device_info, ni_codec_t fmt, ni_logan_device_type_t type, ni_logan_hw_capability_t *p_hw_cap)
Update codec record info with retrieved device info from HW.
void ni_logan_rsrc_get_shm_name(ni_logan_device_type_t device_type, int32_t guid, char *p_name, size_t max_name_len)
Returns the name of shared memory of the codec record.
uint32_t g_logan_xcoder_stop_process
int ni_logan_is_fw_compatible(uint8_t fw_rev[8])
Check if a FW_rev retrieved from card is supported by libxcoder. Support is checked for API flavor,...
int ni_logan_rsrc_strcmp(const void *p_str, const void *p_str1)
String comparison function.
Private definitions related to resource management of NI T-408 devices.
void ni_logan_rsrc_get_one_device_info(ni_logan_device_info_t *p_device_info)
Retrieve codec record info.
void ni_logan_rsrc_update_record(ni_logan_device_context_t *p_device_context, ni_logan_session_context_t *p_session_ctx)
Updates the codec record.
int ni_logan_rsrc_enumerate_devices(char ni_logan_devices[][NI_LOGAN_MAX_DEVICE_NAME_LEN], int max_handles)
List NETINT nvme devices.
#define CODERS_RETRY_DELCK_NAME
#define CODERS_RETRY_ENLCK_NAME
ni_logan_retcode_t ni_logan_find_blk_name(const char *p_dev, char *p_out_buf, int out_buf_len)
Find NVMe name space block from device name If none is found, assume nvme multi-pathing is disabled a...
void ni_logan_usleep(int64_t usec)
Exported utility routines definition.
#define LOGAN_XCODER_MIN_ENC_PIC_WIDTH
#define LOGAN_XCODER_MIN_ENC_PIC_HEIGHT
#define NI_LOGAN_NVME_PREFIX
uint8_t h265_encoders_cnt
uint8_t h264_encoders_cnt
uint8_t fw_commit_time[26]
uint8_t fw_commit_hash[41]
uint8_t fw_branch_name[256]
uint8_t h265_decoders_cnt
uint8_t h264_decoders_cnt
ni_logan_hw_capability_t xcoder_devices[NI_LOGAN_MAX_DEVICES_PER_HW_INSTANCE]
char shm_name[NI_LOGAN_MAX_DEVICE_NAME_LEN]
ni_logan_device_info_t * p_device_info
uint8_t fw_commit_time[26]
uint8_t fw_commit_hash[41]
ni_logan_device_video_capability_t h264_cap
int fw_ver_compat_warning
ni_logan_device_type_t device_type
uint8_t fw_branch_name[256]
char dev_name[NI_LOGAN_MAX_DEVICE_NAME_LEN]
ni_logan_device_video_capability_t h265_cap
char blk_name[NI_LOGAN_MAX_DEVICE_NAME_LEN]
ni_logan_sw_instance_info_t sw_instance[NI_LOGAN_MAX_CONTEXTS_PER_HW_INSTANCE]
unsigned long xcode_load_pixel
ni_logan_device_queue_t * p_device_queue
int32_t decoders[LOGAN_MAX_DEVICE_CNT]
int32_t encoders[LOGAN_MAX_DEVICE_CNT]
char level[NI_LOGAN_MAX_LEVEL_NAME_LEN]
char additional_info[NI_LOGAN_MAX_ADDITIONAL_INFO_LEN]
char profiles_supported[NI_LOGAN_MAX_PROFILE_NAME_LEN]
ni_logan_device_info_t encoders[LOGAN_MAX_DEVICE_CNT]
ni_logan_device_info_t decoders[LOGAN_MAX_DEVICE_CNT]
uint8_t max_number_of_contexts
uint32_t fw_video_mem_usage
ni_logan_load_query_t load_query
uint32_t keep_alive_timeout
ni_device_handle_t device_handle
ni_event_handle_t event_handle
ni_device_handle_t blk_io_handle
uint32_t max_nvme_io_size
ni_sw_instance_status_t status