Skip to content

Commit

Permalink
Fix: addressing coverity report
Browse files Browse the repository at this point in the history
Fix: addressing coverity report
  • Loading branch information
PanKaker authored Nov 25, 2024
1 parent 8c7f4d0 commit 296ca24
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 124 deletions.
11 changes: 7 additions & 4 deletions app/sample/rdma/rdma_video_rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#endif

#define NANOSECONDS_IN_SECOND 1000000000
#define FRAME_WIDTH 1920
#define FRAME_HEIGHT 1080
#define FRAME_SIZE (FRAME_WIDTH * FRAME_HEIGHT * 2) // UYVY format

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Expand Down Expand Up @@ -117,16 +120,16 @@ int main(int argc, char** argv) {
.log_level = MTL_RDMA_LOG_LEVEL_INFO,
//.flags = MTL_RDMA_FLAG_LOW_LATENCY,
};

mrh = mtl_rdma_init(&p);
if (!mrh) {
printf("Failed to initialize RDMA\n");
ret = -1;
goto out;
}

size_t frame_size = 1920 * 1080 * 2; /* UYVY */
for (int i = 0; i < 3; i++) {
buffers[i] = mmap(NULL, frame_size, PROT_READ | PROT_WRITE,
buffers[i] = mmap(NULL, FRAME_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
if (buffers[i] == MAP_FAILED) {
printf("Failed to allocate buffer\n");
Expand All @@ -142,7 +145,7 @@ int main(int argc, char** argv) {
.port = argv[3],
.num_buffers = 3,
.buffers = buffers,
.buffer_capacity = frame_size,
.buffer_capacity = FRAME_SIZE,
.notify_buffer_ready = rx_notify_buffer_ready,
};

Expand Down Expand Up @@ -214,7 +217,7 @@ int main(int argc, char** argv) {
if (rx) mtl_rdma_rx_free(rx);

for (int i = 0; i < 3; i++) {
if (buffers[i] && buffers[i] != MAP_FAILED) munmap(buffers[i], frame_size);
if (buffers[i] && buffers[i] != MAP_FAILED) munmap(buffers[i], FRAME_SIZE);
}

if (mrh) mtl_rdma_uinit(mrh);
Expand Down
50 changes: 26 additions & 24 deletions app/sample/rdma/rdma_video_rx_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#endif

#define NANOSECONDS_IN_SECOND 1000000000
#define FRAME_WIDTH 1920
#define FRAME_HEIGHT 1080
#define FRAME_SIZE (FRAME_WIDTH * FRAME_HEIGHT * 2) // UYVY format

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Expand Down Expand Up @@ -102,7 +105,6 @@ int main(int argc, char** argv) {
return -1;
}
#endif

if (argc != 5) {
printf("Usage: %s <local_ip> <ip> <port> <port1>\n", argv[0]);
return -1;
Expand All @@ -119,23 +121,23 @@ int main(int argc, char** argv) {
.log_level = MTL_RDMA_LOG_LEVEL_INFO,
//.flags = MTL_RDMA_FLAG_LOW_LATENCY,
};

mrh = mtl_rdma_init(&p);
if (!mrh) {
printf("Failed to initialize RDMA\n");
ret = -1;
goto out;
fprintf(stderr, "Failed to initialize RDMA\n");
ret = -EXIT_FAILURE;
goto cleanup;
}

size_t frame_size = 1920 * 1080 * 2; /* UYVY */
for (int i = 0; i < 3; i++) {
buffers[i] = mmap(NULL, frame_size, PROT_READ | PROT_WRITE,
buffers[i] = mmap(NULL, FRAME_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
if (buffers[i] == MAP_FAILED) {
printf("Failed to allocate buffer\n");
ret = -1;
goto out;
fprintf(stderr, "Failed to allocate buffer\n");
ret = -EXIT_FAILURE;
goto cleanup;
}
buffers1[i] = buffers[i] + frame_size / 2;
buffers1[i] = buffers[i] + FRAME_SIZE / 2;
}

struct mtl_rdma_rx_ops rx_ops = {
Expand All @@ -145,15 +147,15 @@ int main(int argc, char** argv) {
.port = argv[3],
.num_buffers = 3,
.buffers = buffers,
.buffer_capacity = frame_size / 2,
.buffer_capacity = FRAME_SIZE / 2,
.notify_buffer_ready = rx_notify_buffer_ready,
};

rx0 = mtl_rdma_rx_create(mrh, &rx_ops);
if (!rx0) {
printf("Failed to create RDMA RX0\n");
ret = -1;
goto out;
fprintf(stderr, "Failed to create RDMA RX0\n");
ret = -EXIT_FAILURE;
goto cleanup;
}

rx_ops.name = "rdma_rx1";
Expand All @@ -162,9 +164,9 @@ int main(int argc, char** argv) {

rx1 = mtl_rdma_rx_create(mrh, &rx_ops);
if (!rx1) {
printf("Failed to create RDMA RX1\n");
ret = -1;
goto out;
fprintf(stderr, "Failed to create RDMA RX1\n");
ret = -EXIT_FAILURE;
goto cleanup;
}

struct timespec start_time, current_time;
Expand All @@ -174,10 +176,10 @@ int main(int argc, char** argv) {
double fps = 0.0;

printf("Starting to receive frames\n");

int frames_consumed = 0;
struct mtl_rdma_buffer* buffer = NULL;
struct mtl_rdma_buffer* buffer1 = NULL;

while (keep_running) {
if (!buffer) buffer = mtl_rdma_rx_get_buffer(rx0);
if (!buffer) {
Expand Down Expand Up @@ -217,16 +219,16 @@ int main(int argc, char** argv) {

ret = mtl_rdma_rx_put_buffer(rx0, buffer);
if (ret < 0) {
printf("Failed to put buffer\n");
ret = -1;
fprintf(stderr, "Failed to put buffer\n");
ret = -EXIT_FAILURE;
break;
}
buffer = NULL;

ret = mtl_rdma_rx_put_buffer(rx1, buffer1);
if (ret < 0) {
printf("Failed to put buffer\n");
ret = -1;
fprintf(stderr, "Failed to put buffer\n");
ret = -EXIT_FAILURE;
break;
}
buffer1 = NULL;
Expand All @@ -247,12 +249,12 @@ int main(int argc, char** argv) {

printf("Received %d frames\n", frames_consumed);

out:
cleanup:
if (rx0) mtl_rdma_rx_free(rx0);
if (rx1) mtl_rdma_rx_free(rx1);

for (int i = 0; i < 3; i++) {
if (buffers[i] && buffers[i] != MAP_FAILED) munmap(buffers[i], frame_size);
if (buffers[i] && buffers[i] != MAP_FAILED) munmap(buffers[i], FRAME_SIZE);
}

if (mrh) mtl_rdma_uinit(mrh);
Expand Down
17 changes: 10 additions & 7 deletions app/sample/rdma/rdma_video_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
#include <time.h>
#include <unistd.h>

#define TARGET_FPS 30
#define NANOSECONDS_IN_SECOND 1000000000
#define TARGET_FPS 30
#define DESIRED_FRAME_DURATION (NANOSECONDS_IN_SECOND / TARGET_FPS)
#define FRAME_WIDTH 1920
#define FRAME_HEIGHT 1080
#define FRAME_SIZE (FRAME_WIDTH * FRAME_HEIGHT * 2) // UYVY format

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Expand Down Expand Up @@ -86,16 +89,16 @@ int main(int argc, char** argv) {
.log_level = MTL_RDMA_LOG_LEVEL_INFO,
//.flags = MTL_RDMA_FLAG_LOW_LATENCY,
};

mrh = mtl_rdma_init(&p);
if (!mrh) {
printf("Failed to initialize RDMA\n");
ret = -1;
goto out;
}

size_t frame_size = 1920 * 1080 * 2; /* UYVY */
for (int i = 0; i < 3; i++) {
buffers[i] = mmap(NULL, frame_size, PROT_READ | PROT_WRITE,
buffers[i] = mmap(NULL, FRAME_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
if (buffers[i] == MAP_FAILED) {
printf("Failed to allocate buffer\n");
Expand All @@ -110,7 +113,7 @@ int main(int argc, char** argv) {
.port = argv[2],
.num_buffers = 3,
.buffers = buffers,
.buffer_capacity = frame_size,
.buffer_capacity = FRAME_SIZE,
.notify_buffer_done = tx_notify_buffer_done,
.notify_buffer_sent = tx_notify_buffer_sent,
};
Expand Down Expand Up @@ -143,7 +146,7 @@ int main(int argc, char** argv) {
continue;
}

while (fread(buffer->addr, 1, frame_size, yuv_file) != frame_size) {
while (fread(buffer->addr, 1, FRAME_SIZE, yuv_file) != FRAME_SIZE) {
if (feof(yuv_file)) {
/* restart from the beginning if the end of file is reached */
fseek(yuv_file, 0, SEEK_SET);
Expand All @@ -159,7 +162,7 @@ int main(int argc, char** argv) {
clock_gettime(CLOCK_REALTIME, &now);
uint64_t send_time_ns = ((uint64_t)now.tv_sec * NANOSECONDS_IN_SECOND) + now.tv_nsec;

buffer->size = frame_size;
buffer->size = FRAME_SIZE;
buffer->user_meta = &send_time_ns;
buffer->user_meta_size = sizeof(uint64_t);

Expand All @@ -180,7 +183,7 @@ int main(int argc, char** argv) {
if (tx) mtl_rdma_tx_free(tx);

for (int i = 0; i < 3; i++) {
if (buffers[i] && buffers[i] != MAP_FAILED) munmap(buffers[i], frame_size);
if (buffers[i] && buffers[i] != MAP_FAILED) munmap(buffers[i], FRAME_SIZE);
}

if (mrh) mtl_rdma_uinit(mrh);
Expand Down
20 changes: 11 additions & 9 deletions app/sample/rdma/rdma_video_tx_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
#include <time.h>
#include <unistd.h>

#define TARGET_FPS 30
#define NANOSECONDS_IN_SECOND 1000000000
#define TARGET_FPS 30
#define DESIRED_FRAME_DURATION (NANOSECONDS_IN_SECOND / TARGET_FPS)
#define FRAME_WIDTH 1920
#define FRAME_HEIGHT 1080
#define FRAME_SIZE (FRAME_WIDTH * FRAME_HEIGHT * 2) // UYVY format

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Expand Down Expand Up @@ -95,16 +98,15 @@ int main(int argc, char** argv) {
goto out;
}

size_t frame_size = 1920 * 1080 * 2; /* UYVY */
for (int i = 0; i < 3; i++) {
buffers[i] = mmap(NULL, frame_size, PROT_READ | PROT_WRITE,
buffers[i] = mmap(NULL, FRAME_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
if (buffers[i] == MAP_FAILED) {
printf("Failed to allocate buffer\n");
ret = -1;
goto out;
}
buffers1[i] = buffers[i] + frame_size / 2;
buffers1[i] = buffers[i] + FRAME_SIZE / 2;
}

struct mtl_rdma_tx_ops tx_ops = {
Expand All @@ -113,7 +115,7 @@ int main(int argc, char** argv) {
.port = argv[2],
.num_buffers = 3,
.buffers = buffers,
.buffer_capacity = frame_size / 2,
.buffer_capacity = FRAME_SIZE / 2,
.notify_buffer_done = tx_notify_buffer_done,
.notify_buffer_sent = tx_notify_buffer_sent,
};
Expand Down Expand Up @@ -167,7 +169,7 @@ int main(int argc, char** argv) {
continue;
}

while (fread(buffer->addr, 1, frame_size, yuv_file) != frame_size) {
while (fread(buffer->addr, 1, FRAME_SIZE, yuv_file) != FRAME_SIZE) {
if (feof(yuv_file)) {
/* restart from the beginning if the end of file is reached */
fseek(yuv_file, 0, SEEK_SET);
Expand All @@ -183,7 +185,7 @@ int main(int argc, char** argv) {
clock_gettime(CLOCK_REALTIME, &now);
uint64_t send_time_ns = ((uint64_t)now.tv_sec * NANOSECONDS_IN_SECOND) + now.tv_nsec;

buffer->size = frame_size / 2;
buffer->size = FRAME_SIZE / 2;
buffer->user_meta = &send_time_ns;
buffer->user_meta_size = sizeof(uint64_t);

Expand All @@ -195,7 +197,7 @@ int main(int argc, char** argv) {
}
buffer = NULL;

buffer1->size = frame_size / 2;
buffer1->size = FRAME_SIZE / 2;
buffer1->user_meta = &frames_sent;
buffer1->user_meta_size = sizeof(int);

Expand All @@ -218,7 +220,7 @@ int main(int argc, char** argv) {
if (tx1) mtl_rdma_tx_free(tx1);

for (int i = 0; i < 3; i++) {
if (buffers[i] && buffers[i] != MAP_FAILED) munmap(buffers[i], frame_size);
if (buffers[i] && buffers[i] != MAP_FAILED) munmap(buffers[i], FRAME_SIZE);
}

if (mrh) mtl_rdma_uinit(mrh);
Expand Down
65 changes: 19 additions & 46 deletions app/src/parse_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -2378,52 +2378,25 @@ int st_app_parse_json(st_json_context_t* ctx, const char* filename) {
}

/* allocate tx sessions */
ctx->tx_video_sessions = (st_json_video_session_t*)st_app_zmalloc(
ctx->tx_video_session_cnt * sizeof(st_json_video_session_t));
if (!ctx->tx_video_sessions) {
err("%s, failed to allocate tx_video_sessions\n", __func__);
ret = -ST_JSON_NULL;
goto error;
}
ctx->tx_audio_sessions = (st_json_audio_session_t*)st_app_zmalloc(
ctx->tx_audio_session_cnt * sizeof(st_json_audio_session_t));
if (!ctx->tx_audio_sessions) {
err("%s, failed to allocate tx_audio_sessions\n", __func__);
ret = -ST_JSON_NULL;
goto error;
}
ctx->tx_anc_sessions = (st_json_ancillary_session_t*)st_app_zmalloc(
ctx->tx_anc_session_cnt * sizeof(st_json_ancillary_session_t));
if (!ctx->tx_anc_sessions) {
err("%s, failed to allocate tx_anc_sessions\n", __func__);
ret = -ST_JSON_NULL;
goto error;
}
ctx->tx_fmd_sessions = (st_json_fastmetadata_session_t*)st_app_zmalloc(
ctx->tx_fmd_session_cnt * sizeof(st_json_fastmetadata_session_t));
if (!ctx->tx_fmd_sessions) {
err("%s, failed to allocate tx_fmd_sessions\n", __func__);
ret = -ST_JSON_NULL;
goto error;
}
ctx->tx_st22p_sessions = (st_json_st22p_session_t*)st_app_zmalloc(
ctx->tx_st22p_session_cnt * sizeof(st_json_st22p_session_t));
if (!ctx->tx_st22p_sessions) {
err("%s, failed to allocate tx_st22p_sessions\n", __func__);
ret = -ST_JSON_NULL;
goto error;
}
ctx->tx_st20p_sessions = (st_json_st20p_session_t*)st_app_zmalloc(
ctx->tx_st20p_session_cnt * sizeof(st_json_st20p_session_t));
if (!ctx->tx_st20p_sessions) {
err("%s, failed to allocate tx_st20p_sessions\n", __func__);
ret = -ST_JSON_NULL;
goto error;
}
ctx->tx_st30p_sessions = (st_json_st30p_session_t*)st_app_zmalloc(
ctx->tx_st30p_session_cnt * sizeof(st_json_st30p_session_t));
if (!ctx->tx_st30p_sessions) {
err("%s, failed to allocate tx_st30p_sessions\n", __func__);
ctx->tx_video_sessions = st_app_zmalloc((size_t)ctx->tx_video_session_cnt *
sizeof(st_json_video_session_t));
ctx->tx_audio_sessions = st_app_zmalloc((size_t)ctx->tx_audio_session_cnt *
sizeof(st_json_audio_session_t));
ctx->tx_anc_sessions = st_app_zmalloc((size_t)ctx->tx_anc_session_cnt *
sizeof(st_json_ancillary_session_t));
ctx->tx_fmd_sessions = st_app_zmalloc((size_t)ctx->tx_fmd_session_cnt *
sizeof(st_json_fastmetadata_session_t));
ctx->tx_st22p_sessions = st_app_zmalloc((size_t)ctx->tx_st22p_session_cnt *
sizeof(st_json_st22p_session_t));
ctx->tx_st20p_sessions = st_app_zmalloc((size_t)ctx->tx_st20p_session_cnt *
sizeof(st_json_st20p_session_t));
ctx->tx_st30p_sessions = st_app_zmalloc((size_t)ctx->tx_st30p_session_cnt *
sizeof(st_json_st30p_session_t));

if (!ctx->tx_video_sessions || !ctx->tx_audio_sessions || !ctx->tx_anc_sessions ||
!ctx->tx_fmd_sessions || !ctx->tx_st22p_sessions || !ctx->tx_st20p_sessions ||
!ctx->tx_st30p_sessions) {
err("%s, failed to allocate tx sessions\n", __func__);
ret = -ST_JSON_NULL;
goto error;
}
Expand Down
Loading

0 comments on commit 296ca24

Please sign in to comment.