Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevaev committed Mar 13, 2024
1 parent 8ac2fa2 commit f48695a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 43 deletions.
33 changes: 22 additions & 11 deletions janus/src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions janus/src/uslibs/tc358743.c
1 change: 1 addition & 0 deletions janus/src/uslibs/tc358743.h
40 changes: 16 additions & 24 deletions janus/src/tc358743.c → src/libs/tc358743.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
#include <linux/videodev2.h>
#include <linux/v4l2-controls.h>

#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
Expand All @@ -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;
}
10 changes: 2 additions & 8 deletions janus/src/tc358743.h → src/libs/tc358743.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit f48695a

Please sign in to comment.