From f48695a04ea0d5e2117b1e94a0b28671922c15b0 Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Wed, 13 Mar 2024 13:09:27 +0200 Subject: [PATCH] refactoring --- janus/src/plugin.c | 33 ++++++++++++++++-------- janus/src/uslibs/tc358743.c | 1 + janus/src/uslibs/tc358743.h | 1 + {janus/src => src/libs}/tc358743.c | 40 ++++++++++++------------------ {janus/src => src/libs}/tc358743.h | 10 ++------ 5 files changed, 42 insertions(+), 43 deletions(-) create mode 120000 janus/src/uslibs/tc358743.c create mode 120000 janus/src/uslibs/tc358743.h rename {janus/src => src/libs}/tc358743.c (74%) rename {janus/src => src/libs}/tc358743.h (90%) diff --git a/janus/src/plugin.c b/janus/src/plugin.c index dbb3b788b..cec6db302 100644 --- a/janus/src/plugin.c +++ b/janus/src/plugin.c @@ -42,12 +42,12 @@ #include "uslibs/list.h" #include "uslibs/ring.h" #include "uslibs/memsinksh.h" +#include "uslibs/tc358743.h" #include "const.h" #include "logging.h" #include "client.h" #include "audio.h" -#include "tc358743.h" #include "rtp.h" #include "rtpv.h" #include "rtpa.h" @@ -188,6 +188,22 @@ static void *_video_sink_thread(void *arg) { return NULL; } +static int _check_tc358743_audio(uint *audio_hz) { + int fd; + if ((fd = open(_g_config->tc358743_dev_path, O_RDWR)) < 0) { + US_JLOG_PERROR("audio", "Can't open TC358743 V4L2 device"); + return -1; + } + const int checked = us_tc358743_xioctl_get_audio_hz(fd, audio_hz); + if (checked < 0) { + US_JLOG_PERROR("audio", "Can't check TC358743 audio state (%d)", checked); + close(fd); + return -1; + } + close(fd); + return 0; +} + static void *_audio_thread(void *arg) { (void)arg; US_THREAD_SETTLE("us_audio"); @@ -204,32 +220,27 @@ static void *_audio_thread(void *arg) { continue; } - us_tc358743_info_s info = {0}; + uint audio_hz = 0; us_audio_s *audio = NULL; - if (us_tc358743_read_info(_g_config->tc358743_dev_path, &info) < 0) { + if (_check_tc358743_audio(&audio_hz) < 0) { goto close_audio; } - if (!info.has_audio) { + if (audio_hz == 0) { US_ONCE({ US_JLOG_INFO("audio", "No audio presented from the host"); }); goto close_audio; } US_ONCE({ US_JLOG_INFO("audio", "Detected host audio"); }); - if ((audio = us_audio_init(_g_config->audio_dev_name, info.audio_hz)) == NULL) { + if ((audio = us_audio_init(_g_config->audio_dev_name, audio_hz)) == NULL) { goto close_audio; } once = 0; while (!_STOP && _HAS_WATCHERS && _HAS_LISTENERS) { - if ( - us_tc358743_read_info(_g_config->tc358743_dev_path, &info) < 0 - || !info.has_audio - || audio->pcm_hz != info.audio_hz - ) { + if (_check_tc358743_audio(&audio_hz) < 0 || audio->pcm_hz != audio_hz) { goto close_audio; } - uz size = US_RTP_DATAGRAM_SIZE - US_RTP_HEADER_SIZE; u8 data[size]; u64 pts; diff --git a/janus/src/uslibs/tc358743.c b/janus/src/uslibs/tc358743.c new file mode 120000 index 000000000..80c3d4d60 --- /dev/null +++ b/janus/src/uslibs/tc358743.c @@ -0,0 +1 @@ +../../../src/libs/tc358743.c \ No newline at end of file diff --git a/janus/src/uslibs/tc358743.h b/janus/src/uslibs/tc358743.h new file mode 120000 index 000000000..7fa590306 --- /dev/null +++ b/janus/src/uslibs/tc358743.h @@ -0,0 +1 @@ +../../../src/libs/tc358743.h \ No newline at end of file diff --git a/janus/src/tc358743.c b/src/libs/tc358743.c similarity index 74% rename from janus/src/tc358743.c rename to src/libs/tc358743.c index 8b1c29ba1..fd6f1b639 100644 --- a/janus/src/tc358743.c +++ b/src/libs/tc358743.c @@ -28,11 +28,9 @@ #include #include -#include "uslibs/types.h" -#include "uslibs/tools.h" -#include "uslibs/xioctl.h" - -#include "logging.h" +#include "types.h" +#include "tools.h" +#include "xioctl.h" #ifndef V4L2_CID_USER_TC358743_BASE @@ -46,28 +44,22 @@ #endif -int us_tc358743_read_info(const char *path, us_tc358743_info_s *info) { - US_MEMSET_ZERO(*info); +int us_tc358743_xioctl_get_audio_hz(int fd, uint *audio_hz) { + *audio_hz = 0; - int fd = -1; - if ((fd = open(path, O_RDWR)) < 0) { - US_JLOG_PERROR("audio", "Can't open TC358743 V4L2 device"); + struct v4l2_control ctl = {.id = TC358743_CID_AUDIO_PRESENT}; + if (us_xioctl(fd, VIDIOC_G_CTRL, &ctl) < 0) { return -1; } + if (!ctl.value) { + return 0; // No audio + } -# define READ_CID(x_cid, x_field) { \ - struct v4l2_control m_ctl = {.id = x_cid}; \ - if (us_xioctl(fd, VIDIOC_G_CTRL, &m_ctl) < 0) { \ - US_JLOG_PERROR("audio", "Can't get value of " #x_cid); \ - close(fd); \ - return -1; \ - } \ - info->x_field = m_ctl.value; \ - } - READ_CID(TC358743_CID_AUDIO_PRESENT, has_audio); - READ_CID(TC358743_CID_AUDIO_SAMPLING_RATE, audio_hz); -# undef READ_CID - - close(fd); + US_MEMSET_ZERO(ctl); + ctl.id = TC358743_CID_AUDIO_SAMPLING_RATE; + if (us_xioctl(fd, VIDIOC_G_CTRL, &ctl) < 0) { + return -2; + } + *audio_hz = ctl.value; return 0; } diff --git a/janus/src/tc358743.h b/src/libs/tc358743.h similarity index 90% rename from janus/src/tc358743.h rename to src/libs/tc358743.h index 499f3ca44..d33856b7e 100644 --- a/janus/src/tc358743.h +++ b/src/libs/tc358743.h @@ -22,13 +22,7 @@ #pragma once -#include "uslibs/types.h" +#include "types.h" -typedef struct { - bool has_audio; - uint audio_hz; -} us_tc358743_info_s; - - -int us_tc358743_read_info(const char *path, us_tc358743_info_s *info); +int us_tc358743_xioctl_get_audio_hz(int fd, uint *audio_hz);