using us_ prefixes

This commit is contained in:
Maxim Devaev
2022-07-19 11:02:36 +03:00
parent e3293d6887
commit cbee3adb2e
90 changed files with 2297 additions and 2338 deletions

View File

@@ -23,29 +23,29 @@
#include "audio.h"
#define JLOG_PERROR_ALSA(_err, _prefix, _msg, ...) JLOG_ERROR(_prefix, _msg ": %s", ##__VA_ARGS__, snd_strerror(_err))
#define JLOG_PERROR_RES(_err, _prefix, _msg, ...) JLOG_ERROR(_prefix, _msg ": %s", ##__VA_ARGS__, speex_resampler_strerror(_err))
#define JLOG_PERROR_OPUS(_err, _prefix, _msg, ...) JLOG_ERROR(_prefix, _msg ": %s", ##__VA_ARGS__, opus_strerror(_err))
#define _JLOG_PERROR_ALSA(_err, _prefix, _msg, ...) US_JLOG_ERROR(_prefix, _msg ": %s", ##__VA_ARGS__, snd_strerror(_err))
#define _JLOG_PERROR_RES(_err, _prefix, _msg, ...) US_JLOG_ERROR(_prefix, _msg ": %s", ##__VA_ARGS__, speex_resampler_strerror(_err))
#define _JLOG_PERROR_OPUS(_err, _prefix, _msg, ...) US_JLOG_ERROR(_prefix, _msg ": %s", ##__VA_ARGS__, opus_strerror(_err))
// A number of frames per 1 channel:
// - https://github.com/xiph/opus/blob/7b05f44/src/opus_demo.c#L368
#define HZ_TO_FRAMES(_hz) (6 * (_hz) / 50) // 120ms
#define HZ_TO_BUF16(_hz) (HZ_TO_FRAMES(_hz) * 2) // One stereo frame = (16bit L) + (16bit R)
#define HZ_TO_BUF8(_hz) (HZ_TO_BUF16(_hz) * sizeof(int16_t))
#define _HZ_TO_FRAMES(_hz) (6 * (_hz) / 50) // 120ms
#define _HZ_TO_BUF16(_hz) (_HZ_TO_FRAMES(_hz) * 2) // One stereo frame = (16bit L) + (16bit R)
#define _HZ_TO_BUF8(_hz) (_HZ_TO_BUF16(_hz) * sizeof(int16_t))
#define MIN_PCM_HZ 8000
#define MAX_PCM_HZ 192000
#define MAX_BUF16 HZ_TO_BUF16(MAX_PCM_HZ)
#define MAX_BUF8 HZ_TO_BUF8(MAX_PCM_HZ)
#define ENCODER_INPUT_HZ 48000
#define _MIN_PCM_HZ 8000
#define _MAX_PCM_HZ 192000
#define _MAX_BUF16 _HZ_TO_BUF16(_MAX_PCM_HZ)
#define _MAX_BUF8 _HZ_TO_BUF8(_MAX_PCM_HZ)
#define _ENCODER_INPUT_HZ 48000
typedef struct {
int16_t data[MAX_BUF16];
int16_t data[_MAX_BUF16];
} _pcm_buffer_s;
typedef struct {
uint8_t data[MAX_BUF8]; // Worst case
uint8_t data[_MAX_BUF8]; // Worst case
size_t used;
uint64_t pts;
} _enc_buffer_s;
@@ -55,12 +55,12 @@ static void *_pcm_thread(void *v_audio);
static void *_encoder_thread(void *v_audio);
audio_s *audio_init(const char *name, unsigned pcm_hz) {
audio_s *audio;
A_CALLOC(audio, 1);
us_audio_s *us_audio_init(const char *name, unsigned pcm_hz) {
us_audio_s *audio;
US_CALLOC(audio, 1);
audio->pcm_hz = pcm_hz;
audio->pcm_queue = queue_init(8);
audio->enc_queue = queue_init(8);
audio->pcm_queue = us_queue_init(8);
audio->enc_queue = us_queue_init(8);
atomic_init(&audio->stop, false);
int err;
@@ -68,14 +68,14 @@ audio_s *audio_init(const char *name, unsigned pcm_hz) {
{
if ((err = snd_pcm_open(&audio->pcm, name, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
audio->pcm = NULL;
JLOG_PERROR_ALSA(err, "audio", "Can't open PCM capture");
_JLOG_PERROR_ALSA(err, "audio", "Can't open PCM capture");
goto error;
}
assert(!snd_pcm_hw_params_malloc(&audio->pcm_params));
# define SET_PARAM(_msg, _func, ...) { \
if ((err = _func(audio->pcm, audio->pcm_params, ##__VA_ARGS__)) < 0) { \
JLOG_PERROR_ALSA(err, "audio", _msg); \
_JLOG_PERROR_ALSA(err, "audio", _msg); \
goto error; \
} \
}
@@ -85,30 +85,30 @@ audio_s *audio_init(const char *name, unsigned pcm_hz) {
SET_PARAM("Can't set PCM channels numbre", snd_pcm_hw_params_set_channels, 2);
SET_PARAM("Can't set PCM sampling format", snd_pcm_hw_params_set_format, SND_PCM_FORMAT_S16_LE);
SET_PARAM("Can't set PCM sampling rate", snd_pcm_hw_params_set_rate_near, &audio->pcm_hz, 0);
if (audio->pcm_hz < MIN_PCM_HZ || audio->pcm_hz > MAX_PCM_HZ) {
JLOG_ERROR("audio", "Unsupported PCM freq: %u; should be: %u <= F <= %u",
audio->pcm_hz, MIN_PCM_HZ, MAX_PCM_HZ);
if (audio->pcm_hz < _MIN_PCM_HZ || audio->pcm_hz > _MAX_PCM_HZ) {
US_JLOG_ERROR("audio", "Unsupported PCM freq: %u; should be: %u <= F <= %u",
audio->pcm_hz, _MIN_PCM_HZ, _MAX_PCM_HZ);
goto error;
}
audio->pcm_frames = HZ_TO_FRAMES(audio->pcm_hz);
audio->pcm_size = HZ_TO_BUF8(audio->pcm_hz);
audio->pcm_frames = _HZ_TO_FRAMES(audio->pcm_hz);
audio->pcm_size = _HZ_TO_BUF8(audio->pcm_hz);
SET_PARAM("Can't apply PCM params", snd_pcm_hw_params);
# undef SET_PARAM
}
if (audio->pcm_hz != ENCODER_INPUT_HZ) {
audio->res = speex_resampler_init(2, audio->pcm_hz, ENCODER_INPUT_HZ, SPEEX_RESAMPLER_QUALITY_DESKTOP, &err);
if (audio->pcm_hz != _ENCODER_INPUT_HZ) {
audio->res = speex_resampler_init(2, audio->pcm_hz, _ENCODER_INPUT_HZ, SPEEX_RESAMPLER_QUALITY_DESKTOP, &err);
if (err < 0) {
audio->res = NULL;
JLOG_PERROR_RES(err, "audio", "Can't create resampler");
_JLOG_PERROR_RES(err, "audio", "Can't create resampler");
goto error;
}
}
{
// OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY
audio->enc = opus_encoder_create(ENCODER_INPUT_HZ, 2, OPUS_APPLICATION_AUDIO, &err);
audio->enc = opus_encoder_create(_ENCODER_INPUT_HZ, 2, OPUS_APPLICATION_AUDIO, &err);
assert(err == 0);
assert(!opus_encoder_ctl(audio->enc, OPUS_SET_BITRATE(48000)));
assert(!opus_encoder_ctl(audio->enc, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND)));
@@ -116,23 +116,23 @@ audio_s *audio_init(const char *name, unsigned pcm_hz) {
// OPUS_SET_INBAND_FEC(1), OPUS_SET_PACKET_LOSS_PERC(10): see rtpa.c
}
JLOG_INFO("audio", "Pipeline configured on %uHz; capturing ...", audio->pcm_hz);
US_JLOG_INFO("audio", "Pipeline configured on %uHz; capturing ...", audio->pcm_hz);
audio->tids_created = true;
A_THREAD_CREATE(&audio->enc_tid, _encoder_thread, audio);
A_THREAD_CREATE(&audio->pcm_tid, _pcm_thread, audio);
US_THREAD_CREATE(&audio->enc_tid, _encoder_thread, audio);
US_THREAD_CREATE(&audio->pcm_tid, _pcm_thread, audio);
return audio;
error:
audio_destroy(audio);
us_audio_destroy(audio);
return NULL;
}
void audio_destroy(audio_s *audio) {
void us_audio_destroy(us_audio_s *audio) {
if (audio->tids_created) {
atomic_store(&audio->stop, true);
A_THREAD_JOIN(audio->pcm_tid);
A_THREAD_JOIN(audio->enc_tid);
US_THREAD_JOIN(audio->pcm_tid);
US_THREAD_JOIN(audio->enc_tid);
}
if (audio->enc) {
opus_encoder_destroy(audio->enc);
@@ -146,20 +146,20 @@ void audio_destroy(audio_s *audio) {
if (audio->pcm_params) {
snd_pcm_hw_params_free(audio->pcm_params);
}
QUEUE_FREE_ITEMS_AND_DESTROY(audio->enc_queue, free);
QUEUE_FREE_ITEMS_AND_DESTROY(audio->pcm_queue, free);
US_QUEUE_FREE_ITEMS_AND_DESTROY(audio->enc_queue, free);
US_QUEUE_FREE_ITEMS_AND_DESTROY(audio->pcm_queue, free);
if (audio->tids_created) {
JLOG_INFO("audio", "Pipeline closed");
US_JLOG_INFO("audio", "Pipeline closed");
}
free(audio);
}
int audio_get_encoded(audio_s *audio, uint8_t *data, size_t *size, uint64_t *pts) {
int us_audio_get_encoded(us_audio_s *audio, uint8_t *data, size_t *size, uint64_t *pts) {
if (atomic_load(&audio->stop)) {
return -1;
}
_enc_buffer_s *buf;
if (!queue_get(audio->enc_queue, (void **)&buf, 0.1)) {
if (!us_queue_get(audio->enc_queue, (void **)&buf, 0.1)) {
if (*size < buf->used) {
free(buf);
return -3;
@@ -174,28 +174,28 @@ int audio_get_encoded(audio_s *audio, uint8_t *data, size_t *size, uint64_t *pts
}
static void *_pcm_thread(void *v_audio) {
A_THREAD_RENAME("us_a_pcm");
US_THREAD_RENAME("us_a_pcm");
audio_s *audio = (audio_s *)v_audio;
uint8_t in[MAX_BUF8];
us_audio_s *audio = (us_audio_s *)v_audio;
uint8_t in[_MAX_BUF8];
while (!atomic_load(&audio->stop)) {
int frames = snd_pcm_readi(audio->pcm, in, audio->pcm_frames);
if (frames < 0) {
JLOG_PERROR_ALSA(frames, "audio", "Fatal: Can't capture PCM frames");
_JLOG_PERROR_ALSA(frames, "audio", "Fatal: Can't capture PCM frames");
break;
} else if (frames < (int)audio->pcm_frames) {
JLOG_ERROR("audio", "Fatal: Too few PCM frames captured");
US_JLOG_ERROR("audio", "Fatal: Too few PCM frames captured");
break;
}
if (queue_get_free(audio->pcm_queue)) {
if (us_queue_get_free(audio->pcm_queue)) {
_pcm_buffer_s *out;
A_CALLOC(out, 1);
US_CALLOC(out, 1);
memcpy(out->data, in, audio->pcm_size);
assert(!queue_put(audio->pcm_queue, out, 0));
assert(!us_queue_put(audio->pcm_queue, out, 0));
} else {
JLOG_ERROR("audio", "PCM queue is full");
US_JLOG_ERROR("audio", "PCM queue is full");
}
}
@@ -204,42 +204,42 @@ static void *_pcm_thread(void *v_audio) {
}
static void *_encoder_thread(void *v_audio) {
A_THREAD_RENAME("us_a_enc");
US_THREAD_RENAME("us_a_enc");
audio_s *audio = (audio_s *)v_audio;
int16_t in_res[MAX_BUF16];
us_audio_s *audio = (us_audio_s *)v_audio;
int16_t in_res[_MAX_BUF16];
while (!atomic_load(&audio->stop)) {
_pcm_buffer_s *in;
if (!queue_get(audio->pcm_queue, (void **)&in, 0.1)) {
if (!us_queue_get(audio->pcm_queue, (void **)&in, 0.1)) {
int16_t *in_ptr;
if (audio->res) {
assert(audio->pcm_hz != ENCODER_INPUT_HZ);
assert(audio->pcm_hz != _ENCODER_INPUT_HZ);
uint32_t in_count = audio->pcm_frames;
uint32_t out_count = HZ_TO_FRAMES(ENCODER_INPUT_HZ);
uint32_t out_count = _HZ_TO_FRAMES(_ENCODER_INPUT_HZ);
speex_resampler_process_interleaved_int(audio->res, in->data, &in_count, in_res, &out_count);
in_ptr = in_res;
} else {
assert(audio->pcm_hz == ENCODER_INPUT_HZ);
assert(audio->pcm_hz == _ENCODER_INPUT_HZ);
in_ptr = in->data;
}
_enc_buffer_s *out;
A_CALLOC(out, 1);
int size = opus_encode(audio->enc, in_ptr, HZ_TO_FRAMES(ENCODER_INPUT_HZ), out->data, ARRAY_LEN(out->data));
US_CALLOC(out, 1);
int size = opus_encode(audio->enc, in_ptr, _HZ_TO_FRAMES(_ENCODER_INPUT_HZ), out->data, US_ARRAY_LEN(out->data));
free(in);
if (size < 0) {
JLOG_PERROR_OPUS(size, "audio", "Fatal: Can't encode PCM frame to OPUS");
_JLOG_PERROR_OPUS(size, "audio", "Fatal: Can't encode PCM frame to OPUS");
free(out);
break;
}
out->used = size;
out->pts = audio->pts;
// https://datatracker.ietf.org/doc/html/rfc7587#section-4.2
audio->pts += HZ_TO_FRAMES(ENCODER_INPUT_HZ);
audio->pts += _HZ_TO_FRAMES(_ENCODER_INPUT_HZ);
if (queue_put(audio->enc_queue, out, 0) != 0) {
JLOG_ERROR("audio", "OPUS encoder queue is full");
if (us_queue_put(audio->enc_queue, out, 0) != 0) {
US_JLOG_ERROR("audio", "OPUS encoder queue is full");
free(out);
}
}

View File

@@ -51,18 +51,18 @@ typedef struct {
SpeexResamplerState *res;
OpusEncoder *enc;
queue_s *pcm_queue;
queue_s *enc_queue;
us_queue_s *pcm_queue;
us_queue_s *enc_queue;
uint32_t pts;
pthread_t pcm_tid;
pthread_t enc_tid;
bool tids_created;
atomic_bool stop;
} audio_s;
} us_audio_s;
audio_s *audio_init(const char *name, unsigned pcm_hz);
void audio_destroy(audio_s *audio);
us_audio_s *us_audio_init(const char *name, unsigned pcm_hz);
void us_audio_destroy(us_audio_s *audio);
int audio_get_encoded(audio_s *audio, uint8_t *data, size_t *size, uint64_t *pts);
int us_audio_get_encoded(us_audio_s *audio, uint8_t *data, size_t *size, uint64_t *pts);

View File

@@ -28,53 +28,53 @@ static void *_audio_thread(void *v_client);
static void *_common_thread(void *v_client, bool video);
client_s *client_init(janus_callbacks *gw, janus_plugin_session *session, bool has_audio) {
client_s *client;
A_CALLOC(client, 1);
us_janus_client_s *us_janus_client_init(janus_callbacks *gw, janus_plugin_session *session, bool has_audio) {
us_janus_client_s *client;
US_CALLOC(client, 1);
client->gw = gw;
client->session = session;
atomic_init(&client->transmit, true);
atomic_init(&client->stop, false);
client->video_queue = queue_init(1024);
A_THREAD_CREATE(&client->video_tid, _video_thread, client);
client->video_queue = us_queue_init(1024);
US_THREAD_CREATE(&client->video_tid, _video_thread, client);
if (has_audio) {
client->audio_queue = queue_init(64);
A_THREAD_CREATE(&client->audio_tid, _audio_thread, client);
client->audio_queue = us_queue_init(64);
US_THREAD_CREATE(&client->audio_tid, _audio_thread, client);
}
return client;
}
void client_destroy(client_s *client) {
void us_janus_client_destroy(us_janus_client_s *client) {
atomic_store(&client->stop, true);
queue_put(client->video_queue, NULL, 0);
us_queue_put(client->video_queue, NULL, 0);
if (client->audio_queue != NULL) {
queue_put(client->audio_queue, NULL, 0);
us_queue_put(client->audio_queue, NULL, 0);
}
A_THREAD_JOIN(client->video_tid);
QUEUE_FREE_ITEMS_AND_DESTROY(client->video_queue, rtp_destroy);
US_THREAD_JOIN(client->video_tid);
US_QUEUE_FREE_ITEMS_AND_DESTROY(client->video_queue, us_rtp_destroy);
if (client->audio_queue != NULL) {
A_THREAD_JOIN(client->audio_tid);
QUEUE_FREE_ITEMS_AND_DESTROY(client->audio_queue, rtp_destroy);
US_THREAD_JOIN(client->audio_tid);
US_QUEUE_FREE_ITEMS_AND_DESTROY(client->audio_queue, us_rtp_destroy);
}
free(client);
}
void client_send(client_s *client, const rtp_s *rtp) {
void us_janus_client_send(us_janus_client_s *client, const us_rtp_s *rtp) {
if (
!atomic_load(&client->transmit)
|| (!rtp->video && client->audio_queue == NULL)
) {
return;
}
rtp_s *new = rtp_dup(rtp);
if (queue_put((new->video ? client->video_queue : client->audio_queue), new, 0) != 0) {
JLOG_ERROR("client", "Session %p %s queue is full",
us_rtp_s *new = us_rtp_dup(rtp);
if (us_queue_put((new->video ? client->video_queue : client->audio_queue), new, 0) != 0) {
US_JLOG_ERROR("client", "Session %p %s queue is full",
client->session, (new->video ? "video" : "audio"));
rtp_destroy(new);
us_rtp_destroy(new);
}
}
@@ -87,13 +87,13 @@ static void *_audio_thread(void *v_client) {
}
static void *_common_thread(void *v_client, bool video) {
client_s *client = (client_s *)v_client;
queue_s *queue = (video ? client->video_queue : client->audio_queue);
us_janus_client_s *client = (us_janus_client_s *)v_client;
us_queue_s *queue = (video ? client->video_queue : client->audio_queue);
assert(queue != NULL); // Audio may be NULL
while (!atomic_load(&client->stop)) {
rtp_s *rtp;
if (!queue_get(queue, (void **)&rtp, 0.1)) {
us_rtp_s *rtp;
if (!us_queue_get(queue, (void **)&rtp, 0.1)) {
if (rtp == NULL) {
break;
}
@@ -112,7 +112,7 @@ static void *_common_thread(void *v_client, bool video) {
}
client->gw->relay_rtp(client->session, &packet);
}
rtp_destroy(rtp);
us_rtp_destroy(rtp);
}
}
return NULL;

View File

@@ -39,7 +39,7 @@
#include "rtp.h"
typedef struct client_sx {
typedef struct us_janus_client_sx {
janus_callbacks *gw;
janus_plugin_session *session;
atomic_bool transmit;
@@ -48,14 +48,14 @@ typedef struct client_sx {
pthread_t audio_tid;
atomic_bool stop;
queue_s *video_queue;
queue_s *audio_queue;
us_queue_s *video_queue;
us_queue_s *audio_queue;
LIST_STRUCT(struct client_sx);
} client_s;
US_LIST_STRUCT(struct us_janus_client_sx);
} us_janus_client_s;
client_s *client_init(janus_callbacks *gw, janus_plugin_session *session, bool has_audio);
void client_destroy(client_s *client);
us_janus_client_s *us_janus_client_init(janus_callbacks *gw, janus_plugin_session *session, bool has_audio);
void us_janus_client_destroy(us_janus_client_s *client);
void client_send(client_s *client, const rtp_s *rtp);
void us_janus_client_send(us_janus_client_s *client, const us_rtp_s *rtp);

View File

@@ -27,19 +27,19 @@ static char *_get_value(janus_config *jcfg, const char *section, const char *opt
static bool _get_bool(janus_config *jcfg, const char *section, const char *option, bool def);
plugin_config_s *plugin_config_init(const char *config_dir_path) {
plugin_config_s *config;
A_CALLOC(config, 1);
us_config_s *us_config_init(const char *config_dir_path) {
us_config_s *config;
US_CALLOC(config, 1);
char *config_file_path;
janus_config *jcfg = NULL;
A_ASPRINTF(config_file_path, "%s/%s.jcfg", config_dir_path, PLUGIN_PACKAGE);
JLOG_INFO("config", "Reading config file '%s' ...", config_file_path);
US_ASPRINTF(config_file_path, "%s/%s.jcfg", config_dir_path, US_PLUGIN_PACKAGE);
US_JLOG_INFO("config", "Reading config file '%s' ...", config_file_path);
jcfg = janus_config_parse(config_file_path);
if (jcfg == NULL) {
JLOG_ERROR("config", "Can't read config");
US_JLOG_ERROR("config", "Can't read config");
goto error;
}
janus_config_print(jcfg);
@@ -48,23 +48,23 @@ plugin_config_s *plugin_config_init(const char *config_dir_path) {
(config->video_sink_name = _get_value(jcfg, "memsink", "object")) == NULL
&& (config->video_sink_name = _get_value(jcfg, "video", "sink")) == NULL
) {
JLOG_ERROR("config", "Missing config value: video.sink (ex. memsink.object)");
US_JLOG_ERROR("config", "Missing config value: video.sink (ex. memsink.object)");
goto error;
}
if ((config->video_zero_playout_delay = _get_bool(jcfg, "video", "zero_playout_delay", false)) == true) {
JLOG_INFO("config", "Enabled the experimental Playout-Delay=0 RTP extension for VIDEO");
US_JLOG_INFO("config", "Enabled the experimental Playout-Delay=0 RTP extension for VIDEO");
}
if ((config->audio_dev_name = _get_value(jcfg, "audio", "device")) != NULL) {
JLOG_INFO("config", "Enabled the experimental AUDIO feature");
US_JLOG_INFO("config", "Enabled the experimental AUDIO feature");
if ((config->tc358743_dev_path = _get_value(jcfg, "audio", "tc358743")) == NULL) {
JLOG_INFO("config", "Missing config value: audio.tc358743");
US_JLOG_INFO("config", "Missing config value: audio.tc358743");
goto error;
}
}
goto ok;
error:
plugin_config_destroy(config);
us_config_destroy(config);
config = NULL;
ok:
if (jcfg) {
@@ -74,10 +74,10 @@ plugin_config_s *plugin_config_init(const char *config_dir_path) {
return config;
}
void plugin_config_destroy(plugin_config_s *config) {
DELETE(config->video_sink_name, free);
DELETE(config->audio_dev_name, free);
DELETE(config->tc358743_dev_path, free);
void us_config_destroy(us_config_s *config) {
US_DELETE(config->video_sink_name, free);
US_DELETE(config->audio_dev_name, free);
US_DELETE(config->tc358743_dev_path, free);
free(config);
}

View File

@@ -40,9 +40,8 @@ typedef struct {
char *audio_dev_name;
char *tc358743_dev_path;
} plugin_config_s;
} us_config_s;
// config_init() conflicts with something
plugin_config_s *plugin_config_init(const char *config_dir_path);
void plugin_config_destroy(plugin_config_s *config);
us_config_s *us_config_init(const char *config_dir_path);
void us_config_destroy(us_config_s *config);

View File

@@ -22,5 +22,5 @@
#pragma once
#define PLUGIN_NAME "ustreamer"
#define PLUGIN_PACKAGE "janus.plugin.ustreamer"
#define US_PLUGIN_NAME "ustreamer"
#define US_PLUGIN_PACKAGE "janus.plugin.ustreamer"

View File

@@ -27,12 +27,12 @@
#include "const.h"
#define JLOG_INFO(_prefix, _msg, ...) JANUS_LOG(LOG_INFO, "== %s/%-9s -- " _msg "\n", PLUGIN_NAME, _prefix, ##__VA_ARGS__)
#define JLOG_WARN(_prefix, _msg, ...) JANUS_LOG(LOG_WARN, "== %s/%-9s -- " _msg "\n", PLUGIN_NAME, _prefix, ##__VA_ARGS__)
#define JLOG_ERROR(_prefix, _msg, ...) JANUS_LOG(LOG_ERR, "== %s/%-9s -- " _msg "\n", PLUGIN_NAME, _prefix, ##__VA_ARGS__)
#define US_JLOG_INFO(x_prefix, x_msg, ...) JANUS_LOG(LOG_INFO, "== %s/%-9s -- " x_msg "\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__)
#define US_JLOG_WARN(x_prefix, x_msg, ...) JANUS_LOG(LOG_WARN, "== %s/%-9s -- " x_msg "\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__)
#define US_JLOG_ERROR(x_prefix, x_msg, ...) JANUS_LOG(LOG_ERR, "== %s/%-9s -- " x_msg "\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__)
#define JLOG_PERROR(_prefix, _msg, ...) { \
char _perror_buf[1024] = {0}; \
char *_perror_ptr = errno_to_string(errno, _perror_buf, 1023); \
JANUS_LOG(LOG_ERR, "[%s/%-9s] " _msg ": %s\n", PLUGIN_NAME, _prefix, ##__VA_ARGS__, _perror_ptr); \
#define US_JLOG_PERROR(x_prefix, x_msg, ...) { \
char m_perror_buf[1024] = {0}; \
char *m_perror_ptr = us_errno_to_string(errno, m_perror_buf, 1023); \
JANUS_LOG(LOG_ERR, "[%s/%-9s] " x_msg ": %s\n", US_PLUGIN_NAME, x_prefix, ##__VA_ARGS__, m_perror_ptr); \
}

View File

@@ -23,21 +23,21 @@
#include "memsinkfd.h"
int memsink_fd_wait_frame(int fd, memsink_shared_s* mem, uint64_t last_id) {
long double deadline_ts = get_now_monotonic() + 1; // wait_timeout
int us_memsink_fd_wait_frame(int fd, us_memsink_shared_s* mem, uint64_t last_id) {
long double deadline_ts = us_get_now_monotonic() + 1; // wait_timeout
long double now;
do {
int result = flock_timedwait_monotonic(fd, 1); // lock_timeout
now = get_now_monotonic();
int result = us_flock_timedwait_monotonic(fd, 1); // lock_timeout
now = us_get_now_monotonic();
if (result < 0 && errno != EWOULDBLOCK) {
JLOG_PERROR("video", "Can't lock memsink");
US_JLOG_PERROR("video", "Can't lock memsink");
return -1;
} else if (result == 0) {
if (mem->magic == MEMSINK_MAGIC && mem->version == MEMSINK_VERSION && mem->id != last_id) {
if (mem->magic == US_MEMSINK_MAGIC && mem->version == US_MEMSINK_VERSION && mem->id != last_id) {
return 0;
}
if (flock(fd, LOCK_UN) < 0) {
JLOG_PERROR("video", "Can't unlock memsink");
US_JLOG_PERROR("video", "Can't unlock memsink");
return -1;
}
}
@@ -46,24 +46,24 @@ int memsink_fd_wait_frame(int fd, memsink_shared_s* mem, uint64_t last_id) {
return -2;
}
frame_s *memsink_fd_get_frame(int fd, memsink_shared_s *mem, uint64_t *frame_id) {
frame_s *frame = frame_init();
frame_set_data(frame, mem->data, mem->used);
FRAME_COPY_META(mem, frame);
us_frame_s *us_memsink_fd_get_frame(int fd, us_memsink_shared_s *mem, uint64_t *frame_id) {
us_frame_s *frame = us_frame_init();
us_frame_set_data(frame, mem->data, mem->used);
US_FRAME_COPY_META(mem, frame);
*frame_id = mem->id;
mem->last_client_ts = get_now_monotonic();
mem->last_client_ts = us_get_now_monotonic();
bool ok = true;
if (frame->format != V4L2_PIX_FMT_H264) {
JLOG_ERROR("video", "Got non-H264 frame from memsink");
US_JLOG_ERROR("video", "Got non-H264 frame from memsink");
ok = false;
}
if (flock(fd, LOCK_UN) < 0) {
JLOG_PERROR("video", "Can't unlock memsink");
US_JLOG_PERROR("video", "Can't unlock memsink");
ok = false;
}
if (!ok) {
frame_destroy(frame);
us_frame_destroy(frame);
frame = NULL;
}
return frame;

View File

@@ -34,5 +34,5 @@
#include "logging.h"
int memsink_fd_wait_frame(int fd, memsink_shared_s* mem, uint64_t last_id);
frame_s *memsink_fd_get_frame(int fd, memsink_shared_s *mem, uint64_t *frame_id);
int us_memsink_fd_wait_frame(int fd, us_memsink_shared_s* mem, uint64_t last_id);
us_frame_s *us_memsink_fd_get_frame(int fd, us_memsink_shared_s *mem, uint64_t *frame_id);

View File

@@ -55,14 +55,14 @@
#include "config.h"
static plugin_config_s *_g_config = NULL;
const useconds_t _g_watchers_polling = 100000;
static us_config_s *_g_config = NULL;
const useconds_t _g_watchers_polling = 100000;
static client_s *_g_clients = NULL;
static janus_callbacks *_g_gw = NULL;
static queue_s *_g_video_queue = NULL;
static rtpv_s *_g_rtpv = NULL;
static rtpa_s *_g_rtpa = NULL;
static us_janus_client_s *_g_clients = NULL;
static janus_callbacks *_g_gw = NULL;
static us_queue_s *_g_video_queue = NULL;
static us_rtpv_s *_g_rtpv = NULL;
static us_rtpa_s *_g_rtpa = NULL;
static pthread_t _g_video_rtp_tid;
static atomic_bool _g_video_rtp_tid_created = false;
@@ -78,84 +78,84 @@ static atomic_bool _g_stop = false;
static atomic_bool _g_has_watchers = false;
#define LOCK_VIDEO A_MUTEX_LOCK(&_g_video_lock)
#define UNLOCK_VIDEO A_MUTEX_UNLOCK(&_g_video_lock)
#define _LOCK_VIDEO US_MUTEX_LOCK(&_g_video_lock)
#define _UNLOCK_VIDEO US_MUTEX_UNLOCK(&_g_video_lock)
#define LOCK_AUDIO A_MUTEX_LOCK(&_g_audio_lock)
#define UNLOCK_AUDIO A_MUTEX_UNLOCK(&_g_audio_lock)
#define _LOCK_AUDIO US_MUTEX_LOCK(&_g_audio_lock)
#define _UNLOCK_AUDIO US_MUTEX_UNLOCK(&_g_audio_lock)
#define LOCK_ALL { LOCK_VIDEO; LOCK_AUDIO; }
#define UNLOCK_ALL { UNLOCK_AUDIO; UNLOCK_VIDEO; }
#define _LOCK_ALL { _LOCK_VIDEO; _LOCK_AUDIO; }
#define _UNLOCK_ALL { _UNLOCK_AUDIO; _UNLOCK_VIDEO; }
#define READY atomic_load(&_g_ready)
#define STOP atomic_load(&_g_stop)
#define HAS_WATCHERS atomic_load(&_g_has_watchers)
#define _READY atomic_load(&_g_ready)
#define _STOP atomic_load(&_g_stop)
#define _HAS_WATCHERS atomic_load(&_g_has_watchers)
janus_plugin *create(void);
#define IF_NOT_REPORTED(...) { \
#define _IF_NOT_REPORTED(...) { \
unsigned _error_code = __LINE__; \
if (error_reported != _error_code) { __VA_ARGS__; error_reported = _error_code; } \
}
static void *_video_rtp_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_video_rtp");
US_THREAD_RENAME("us_video_rtp");
atomic_store(&_g_video_rtp_tid_created, true);
while (!STOP) {
frame_s *frame;
if (queue_get(_g_video_queue, (void **)&frame, 0.1) == 0) {
LOCK_VIDEO;
rtpv_wrap(_g_rtpv, frame);
UNLOCK_VIDEO;
frame_destroy(frame);
while (!_STOP) {
us_frame_s *frame;
if (us_queue_get(_g_video_queue, (void **)&frame, 0.1) == 0) {
_LOCK_VIDEO;
us_rtpv_wrap(_g_rtpv, frame);
_UNLOCK_VIDEO;
us_frame_destroy(frame);
}
}
return NULL;
}
static void *_video_sink_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_video_sink");
US_THREAD_RENAME("us_video_sink");
atomic_store(&_g_video_sink_tid_created, true);
uint64_t frame_id = 0;
unsigned error_reported = 0;
while (!STOP) {
if (!HAS_WATCHERS) {
IF_NOT_REPORTED({ JLOG_INFO("video", "No active watchers, memsink disconnected"); });
while (!_STOP) {
if (!_HAS_WATCHERS) {
_IF_NOT_REPORTED({ US_JLOG_INFO("video", "No active watchers, memsink disconnected"); });
usleep(_g_watchers_polling);
continue;
}
int fd = -1;
memsink_shared_s *mem = NULL;
us_memsink_shared_s *mem = NULL;
if ((fd = shm_open(_g_config->video_sink_name, O_RDWR, 0)) <= 0) {
IF_NOT_REPORTED({ JLOG_PERROR("video", "Can't open memsink"); });
_IF_NOT_REPORTED({ US_JLOG_PERROR("video", "Can't open memsink"); });
goto close_memsink;
}
if ((mem = memsink_shared_map(fd)) == NULL) {
IF_NOT_REPORTED({ JLOG_PERROR("video", "Can't map memsink"); });
if ((mem = us_memsink_shared_map(fd)) == NULL) {
_IF_NOT_REPORTED({ US_JLOG_PERROR("video", "Can't map memsink"); });
goto close_memsink;
}
error_reported = 0;
JLOG_INFO("video", "Memsink opened; reading frames ...");
while (!STOP && HAS_WATCHERS) {
int result = memsink_fd_wait_frame(fd, mem, frame_id);
US_JLOG_INFO("video", "Memsink opened; reading frames ...");
while (!_STOP && _HAS_WATCHERS) {
int result = us_memsink_fd_wait_frame(fd, mem, frame_id);
if (result == 0) {
frame_s *frame = memsink_fd_get_frame(fd, mem, &frame_id);
us_frame_s *frame = us_memsink_fd_get_frame(fd, mem, &frame_id);
if (frame == NULL) {
goto close_memsink;
}
if (queue_put(_g_video_queue, frame, 0) != 0) {
IF_NOT_REPORTED({ JLOG_PERROR("video", "Video queue is full"); });
frame_destroy(frame);
if (us_queue_put(_g_video_queue, frame, 0) != 0) {
_IF_NOT_REPORTED({ US_JLOG_PERROR("video", "Video queue is full"); });
us_frame_destroy(frame);
}
} else if (result == -1) {
goto close_memsink;
@@ -164,8 +164,8 @@ static void *_video_sink_thread(UNUSED void *arg) {
close_memsink:
if (mem != NULL) {
JLOG_INFO("video", "Memsink closed");
memsink_shared_unmap(mem);
US_JLOG_INFO("video", "Memsink closed");
us_memsink_shared_unmap(mem);
mem = NULL;
}
if (fd > 0) {
@@ -178,53 +178,53 @@ static void *_video_sink_thread(UNUSED void *arg) {
}
static void *_audio_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_audio");
US_THREAD_RENAME("us_audio");
atomic_store(&_g_audio_tid_created, true);
assert(_g_config->audio_dev_name);
assert(_g_config->tc358743_dev_path);
unsigned error_reported = 0;
while (!STOP) {
if (!HAS_WATCHERS) {
while (!_STOP) {
if (!_HAS_WATCHERS) {
usleep(_g_watchers_polling);
continue;
}
tc358743_info_s info = {0};
audio_s *audio = NULL;
us_tc358743_info_s info = {0};
us_audio_s *audio = NULL;
if (tc358743_read_info(_g_config->tc358743_dev_path, &info) < 0) {
if (us_tc358743_read_info(_g_config->tc358743_dev_path, &info) < 0) {
goto close_audio;
}
if (!info.has_audio) {
IF_NOT_REPORTED({ JLOG_INFO("audio", "No audio presented from the host"); });
_IF_NOT_REPORTED({ US_JLOG_INFO("audio", "No audio presented from the host"); });
goto close_audio;
}
IF_NOT_REPORTED({ JLOG_INFO("audio", "Detected host audio"); });
if ((audio = audio_init(_g_config->audio_dev_name, info.audio_hz)) == NULL) {
_IF_NOT_REPORTED({ US_JLOG_INFO("audio", "Detected host audio"); });
if ((audio = us_audio_init(_g_config->audio_dev_name, info.audio_hz)) == NULL) {
goto close_audio;
}
error_reported = 0;
while (!STOP && HAS_WATCHERS) {
while (!_STOP && _HAS_WATCHERS) {
if (
tc358743_read_info(_g_config->tc358743_dev_path, &info) < 0
us_tc358743_read_info(_g_config->tc358743_dev_path, &info) < 0
|| !info.has_audio
|| audio->pcm_hz != info.audio_hz
) {
goto close_audio;
}
size_t size = RTP_DATAGRAM_SIZE - RTP_HEADER_SIZE;
size_t size = US_RTP_DATAGRAM_SIZE - US_RTP_HEADER_SIZE;
uint8_t data[size];
uint64_t pts;
int result = audio_get_encoded(audio, data, &size, &pts);
int result = us_audio_get_encoded(audio, data, &size, &pts);
if (result == 0) {
LOCK_AUDIO;
rtpa_wrap(_g_rtpa, data, size, pts);
UNLOCK_AUDIO;
_LOCK_AUDIO;
us_rtpa_wrap(_g_rtpa, data, size, pts);
_UNLOCK_AUDIO;
} else if (result == -1) {
goto close_audio;
}
@@ -232,18 +232,18 @@ static void *_audio_thread(UNUSED void *arg) {
close_audio:
if (audio != NULL) {
audio_destroy(audio);
us_audio_destroy(audio);
}
sleep(1); // error_delay
}
return NULL;
}
#undef IF_NOT_REPORTED
#undef _IF_NOT_REPORTED
static void _relay_rtp_clients(const rtp_s *rtp) {
LIST_ITERATE(_g_clients, client, {
client_send(client, rtp);
static void _relay_rtp_clients(const us_rtp_s *rtp) {
US_LIST_ITERATE(_g_clients, client, {
us_janus_client_send(client, rtp);
});
}
@@ -254,117 +254,117 @@ static int _plugin_init(janus_callbacks *gw, const char *config_dir_path) {
// sysctl -w net.core.rmem_max=1000000
// sysctl -w net.core.wmem_max=1000000
JLOG_INFO("main", "Initializing plugin ...");
if (gw == NULL || config_dir_path == NULL || ((_g_config = plugin_config_init(config_dir_path)) == NULL)) {
US_JLOG_INFO("main", "Initializing plugin ...");
if (gw == NULL || config_dir_path == NULL || ((_g_config = us_config_init(config_dir_path)) == NULL)) {
return -1;
}
_g_gw = gw;
_g_video_queue = queue_init(1024);
_g_rtpv = rtpv_init(_relay_rtp_clients, _g_config->video_zero_playout_delay);
_g_video_queue = us_queue_init(1024);
_g_rtpv = us_rtpv_init(_relay_rtp_clients, _g_config->video_zero_playout_delay);
if (_g_config->audio_dev_name) {
_g_rtpa = rtpa_init(_relay_rtp_clients);
A_THREAD_CREATE(&_g_audio_tid, _audio_thread, NULL);
_g_rtpa = us_rtpa_init(_relay_rtp_clients);
US_THREAD_CREATE(&_g_audio_tid, _audio_thread, NULL);
}
A_THREAD_CREATE(&_g_video_rtp_tid, _video_rtp_thread, NULL);
A_THREAD_CREATE(&_g_video_sink_tid, _video_sink_thread, NULL);
US_THREAD_CREATE(&_g_video_rtp_tid, _video_rtp_thread, NULL);
US_THREAD_CREATE(&_g_video_sink_tid, _video_sink_thread, NULL);
atomic_store(&_g_ready, true);
return 0;
}
static void _plugin_destroy(void) {
JLOG_INFO("main", "Destroying plugin ...");
US_JLOG_INFO("main", "Destroying plugin ...");
atomic_store(&_g_stop, true);
# define JOIN(_tid) { if (atomic_load(&_tid##_created)) { A_THREAD_JOIN(_tid); } }
# define JOIN(_tid) { if (atomic_load(&_tid##_created)) { US_THREAD_JOIN(_tid); } }
JOIN(_g_video_sink_tid);
JOIN(_g_video_rtp_tid);
JOIN(_g_audio_tid);
# undef JOIN
LIST_ITERATE(_g_clients, client, {
LIST_REMOVE(_g_clients, client);
client_destroy(client);
US_LIST_ITERATE(_g_clients, client, {
US_LIST_REMOVE(_g_clients, client);
us_janus_client_destroy(client);
});
QUEUE_FREE_ITEMS_AND_DESTROY(_g_video_queue, frame_destroy);
US_QUEUE_FREE_ITEMS_AND_DESTROY(_g_video_queue, us_frame_destroy);
DELETE(_g_rtpa, rtpa_destroy);
DELETE(_g_rtpv, rtpv_destroy);
DELETE(_g_config, plugin_config_destroy);
US_DELETE(_g_rtpa, us_rtpa_destroy);
US_DELETE(_g_rtpv, us_rtpv_destroy);
US_DELETE(_g_config, us_config_destroy);
}
#define IF_DISABLED(...) { if (!READY || STOP) { __VA_ARGS__ } }
#define _IF_DISABLED(...) { if (!_READY || _STOP) { __VA_ARGS__ } }
static void _plugin_create_session(janus_plugin_session *session, int *err) {
IF_DISABLED({ *err = -1; return; });
LOCK_ALL;
JLOG_INFO("main", "Creating session %p ...", session);
client_s *client = client_init(_g_gw, session, (_g_config->audio_dev_name != NULL));
LIST_APPEND(_g_clients, client);
_IF_DISABLED({ *err = -1; return; });
_LOCK_ALL;
US_JLOG_INFO("main", "Creating session %p ...", session);
us_janus_client_s *client = us_janus_client_init(_g_gw, session, (_g_config->audio_dev_name != NULL));
US_LIST_APPEND(_g_clients, client);
atomic_store(&_g_has_watchers, true);
UNLOCK_ALL;
_UNLOCK_ALL;
}
static void _plugin_destroy_session(janus_plugin_session* session, int *err) {
IF_DISABLED({ *err = -1; return; });
LOCK_ALL;
_IF_DISABLED({ *err = -1; return; });
_LOCK_ALL;
bool found = false;
bool has_watchers = false;
LIST_ITERATE(_g_clients, client, {
US_LIST_ITERATE(_g_clients, client, {
if (client->session == session) {
JLOG_INFO("main", "Removing session %p ...", session);
LIST_REMOVE(_g_clients, client);
client_destroy(client);
US_JLOG_INFO("main", "Removing session %p ...", session);
US_LIST_REMOVE(_g_clients, client);
us_janus_client_destroy(client);
found = true;
} else {
has_watchers = (has_watchers || atomic_load(&client->transmit));
}
});
if (!found) {
JLOG_WARN("main", "No session %p", session);
US_JLOG_WARN("main", "No session %p", session);
*err = -2;
}
atomic_store(&_g_has_watchers, has_watchers);
UNLOCK_ALL;
_UNLOCK_ALL;
}
static json_t *_plugin_query_session(janus_plugin_session *session) {
IF_DISABLED({ return NULL; });
_IF_DISABLED({ return NULL; });
json_t *info = NULL;
LOCK_ALL;
LIST_ITERATE(_g_clients, client, {
_LOCK_ALL;
US_LIST_ITERATE(_g_clients, client, {
if (client->session == session) {
info = json_string("session_found");
break;
}
});
UNLOCK_ALL;
_UNLOCK_ALL;
return info;
}
static void _set_transmit(janus_plugin_session *session, UNUSED const char *msg, bool transmit) {
IF_DISABLED({ return; });
LOCK_ALL;
_IF_DISABLED({ return; });
_LOCK_ALL;
bool found = false;
bool has_watchers = false;
LIST_ITERATE(_g_clients, client, {
US_LIST_ITERATE(_g_clients, client, {
if (client->session == session) {
atomic_store(&client->transmit, transmit);
// JLOG_INFO("main", "%s session %p", msg, session);
// US_JLOG_INFO("main", "%s session %p", msg, session);
found = true;
}
has_watchers = (has_watchers || atomic_load(&client->transmit));
});
if (!found) {
JLOG_WARN("main", "No session %p", session);
US_JLOG_WARN("main", "No session %p", session);
}
atomic_store(&_g_has_watchers, has_watchers);
UNLOCK_ALL;
_UNLOCK_ALL;
}
#undef IF_DISABLED
#undef _IF_DISABLED
static void _plugin_setup_media(janus_plugin_session *session) { _set_transmit(session, "Unmuted", true); }
static void _plugin_hangup_media(janus_plugin_session *session) { _set_transmit(session, "Muted", false); }
@@ -385,14 +385,14 @@ static struct janus_plugin_result *_plugin_handle_message(
return janus_plugin_result_new(JANUS_PLUGIN_ERROR, (msg ? "No session" : "No message"), NULL);
}
# define PUSH_ERROR(_error, _reason) { \
/*JLOG_ERROR("main", "Message error in session %p: %s", session, _reason);*/ \
json_t *_event = json_object(); \
json_object_set_new(_event, "ustreamer", json_string("event")); \
json_object_set_new(_event, "error_code", json_integer(_error)); \
json_object_set_new(_event, "error", json_string(_reason)); \
_g_gw->push_event(session, create(), transaction, _event, NULL); \
json_decref(_event); \
# define PUSH_ERROR(x_error, x_reason) { \
/*US_JLOG_ERROR("main", "Message error in session %p: %s", session, x_reason);*/ \
json_t *m_event = json_object(); \
json_object_set_new(m_event, "ustreamer", json_string("event")); \
json_object_set_new(m_event, "error_code", json_integer(x_error)); \
json_object_set_new(m_event, "error", json_string(x_reason)); \
_g_gw->push_event(session, create(), transaction, m_event, NULL); \
json_decref(m_event); \
}
json_t *request_obj = json_object_get(msg, "request");
@@ -406,16 +406,16 @@ static struct janus_plugin_result *_plugin_handle_message(
PUSH_ERROR(400, "Request not a string");
goto ok_wait;
}
// JLOG_INFO("main", "Message: %s", request_str);
// US_JLOG_INFO("main", "Message: %s", request_str);
# define PUSH_STATUS(_status, _jsep) { \
json_t *_event = json_object(); \
json_object_set_new(_event, "ustreamer", json_string("event")); \
json_t *_result = json_object(); \
json_object_set_new(_result, "status", json_string(_status)); \
json_object_set_new(_event, "result", _result); \
_g_gw->push_event(session, create(), transaction, _event, _jsep); \
json_decref(_event); \
# define PUSH_STATUS(x_status, x_jsep) { \
json_t *m_event = json_object(); \
json_object_set_new(m_event, "ustreamer", json_string("event")); \
json_t *m_result = json_object(); \
json_object_set_new(m_result, "status", json_string(x_status)); \
json_object_set_new(m_event, "result", m_result); \
_g_gw->push_event(session, create(), transaction, m_event, x_jsep); \
json_decref(m_event); \
}
if (!strcmp(request_str, "start")) {
@@ -427,19 +427,19 @@ static struct janus_plugin_result *_plugin_handle_message(
} else if (!strcmp(request_str, "watch")) {
char *sdp;
{
char *video_sdp = rtpv_make_sdp(_g_rtpv);
char *video_sdp = us_rtpv_make_sdp(_g_rtpv);
if (video_sdp == NULL) {
PUSH_ERROR(503, "Haven't received SPS/PPS from memsink yet");
goto ok_wait;
}
char *audio_sdp = (_g_rtpa ? rtpa_make_sdp(_g_rtpa) : strdup(""));
A_ASPRINTF(sdp,
char *audio_sdp = (_g_rtpa ? us_rtpa_make_sdp(_g_rtpa) : strdup(""));
US_ASPRINTF(sdp,
"v=0" RN
"o=- %" PRIu64 " 1 IN IP4 0.0.0.0" RN
"s=PiKVM uStreamer" RN
"t=0 0" RN
"%s%s",
get_now_id() >> 1, audio_sdp, video_sdp
us_get_now_id() >> 1, audio_sdp, video_sdp
);
free(audio_sdp);
free(video_sdp);
@@ -466,12 +466,12 @@ static struct janus_plugin_result *_plugin_handle_message(
// ***** Plugin *****
static int _plugin_get_api_compatibility(void) { return JANUS_PLUGIN_API_VERSION; }
static int _plugin_get_version(void) { return VERSION_U; }
static const char *_plugin_get_version_string(void) { return VERSION; }
static int _plugin_get_version(void) { return US_VERSION_U; }
static const char *_plugin_get_version_string(void) { return US_VERSION; }
static const char *_plugin_get_description(void) { return "PiKVM uStreamer Janus plugin for H.264 video"; }
static const char *_plugin_get_name(void) { return PLUGIN_NAME; }
static const char *_plugin_get_name(void) { return US_PLUGIN_NAME; }
static const char *_plugin_get_author(void) { return "Maxim Devaev <mdevaev@gmail.com>"; }
static const char *_plugin_get_package(void) { return PLUGIN_PACKAGE; }
static const char *_plugin_get_package(void) { return US_PLUGIN_PACKAGE; }
static void _plugin_incoming_rtp(UNUSED janus_plugin_session *handle, UNUSED janus_plugin_rtp *packet) {
// Just a stub to avoid logging spam about the plugin's purpose

View File

@@ -23,12 +23,12 @@
#include "queue.h"
queue_s *queue_init(unsigned capacity) {
queue_s *queue;
A_CALLOC(queue, 1);
A_CALLOC(queue->items, capacity);
us_queue_s *us_queue_init(unsigned capacity) {
us_queue_s *queue;
US_CALLOC(queue, 1);
US_CALLOC(queue->items, capacity);
queue->capacity = capacity;
A_MUTEX_INIT(&queue->mutex);
US_MUTEX_INIT(&queue->mutex);
pthread_condattr_t attrs;
assert(!pthread_condattr_init(&attrs));
@@ -39,61 +39,61 @@ queue_s *queue_init(unsigned capacity) {
return queue;
}
void queue_destroy(queue_s *queue) {
void us_queue_destroy(us_queue_s *queue) {
free(queue->items);
free(queue);
}
#define WAIT_OR_UNLOCK(_var, _cond) { \
struct timespec _ts; \
assert(!clock_gettime(CLOCK_MONOTONIC, &_ts)); \
ld_to_timespec(timespec_to_ld(&_ts) + timeout, &_ts); \
while (_var) { \
int err = pthread_cond_timedwait(_cond, &queue->mutex, &_ts); \
#define _WAIT_OR_UNLOCK(x_var, x_cond) { \
struct timespec m_ts; \
assert(!clock_gettime(CLOCK_MONOTONIC, &m_ts)); \
us_ld_to_timespec(us_timespec_to_ld(&m_ts) + timeout, &m_ts); \
while (x_var) { \
int err = pthread_cond_timedwait(x_cond, &queue->mutex, &m_ts); \
if (err == ETIMEDOUT) { \
A_MUTEX_UNLOCK(&queue->mutex); \
US_MUTEX_UNLOCK(&queue->mutex); \
return -1; \
} \
assert(!err); \
} \
}
int queue_put(queue_s *queue, void *item, long double timeout) {
A_MUTEX_LOCK(&queue->mutex);
int us_queue_put(us_queue_s *queue, void *item, long double timeout) {
US_MUTEX_LOCK(&queue->mutex);
if (timeout == 0) {
if (queue->size == queue->capacity) {
A_MUTEX_UNLOCK(&queue->mutex);
US_MUTEX_UNLOCK(&queue->mutex);
return -1;
}
} else {
WAIT_OR_UNLOCK(queue->size == queue->capacity, &queue->full_cond);
_WAIT_OR_UNLOCK(queue->size == queue->capacity, &queue->full_cond);
}
queue->items[queue->in] = item;
++queue->size;
++queue->in;
queue->in %= queue->capacity;
A_MUTEX_UNLOCK(&queue->mutex);
US_MUTEX_UNLOCK(&queue->mutex);
assert(!pthread_cond_broadcast(&queue->empty_cond));
return 0;
}
int queue_get(queue_s *queue, void **item, long double timeout) {
A_MUTEX_LOCK(&queue->mutex);
WAIT_OR_UNLOCK(queue->size == 0, &queue->empty_cond);
int us_queue_get(us_queue_s *queue, void **item, long double timeout) {
US_MUTEX_LOCK(&queue->mutex);
_WAIT_OR_UNLOCK(queue->size == 0, &queue->empty_cond);
*item = queue->items[queue->out];
--queue->size;
++queue->out;
queue->out %= queue->capacity;
A_MUTEX_UNLOCK(&queue->mutex);
US_MUTEX_UNLOCK(&queue->mutex);
assert(!pthread_cond_broadcast(&queue->full_cond));
return 0;
}
#undef WAIT_OR_UNLOCK
#undef _WAIT_OR_UNLOCK
int queue_get_free(queue_s *queue) {
A_MUTEX_LOCK(&queue->mutex);
int us_queue_get_free(us_queue_s *queue) {
US_MUTEX_LOCK(&queue->mutex);
unsigned size = queue->size;
A_MUTEX_UNLOCK(&queue->mutex);
US_MUTEX_UNLOCK(&queue->mutex);
return queue->capacity - size;
}

View File

@@ -44,26 +44,26 @@ typedef struct {
pthread_mutex_t mutex;
pthread_cond_t full_cond;
pthread_cond_t empty_cond;
} queue_s;
} us_queue_s;
#define QUEUE_FREE_ITEMS_AND_DESTROY(_queue, _free_item) { \
if (_queue) { \
while (!queue_get_free(_queue)) { \
void *_ptr; \
assert(!queue_get(_queue, &_ptr, 0.1)); \
if (_ptr != NULL) { \
_free_item(_ptr); \
#define US_QUEUE_FREE_ITEMS_AND_DESTROY(x_queue, x_free_item) { \
if (x_queue) { \
while (!us_queue_get_free(x_queue)) { \
void *m_ptr; \
assert(!us_queue_get(x_queue, &m_ptr, 0.1)); \
if (m_ptr != NULL) { \
x_free_item(m_ptr); \
} \
} \
queue_destroy(_queue); \
us_queue_destroy(x_queue); \
} \
}
queue_s *queue_init(unsigned capacity);
void queue_destroy(queue_s *queue);
us_queue_s *us_queue_init(unsigned capacity);
void us_queue_destroy(us_queue_s *queue);
int queue_put(queue_s *queue, void *item, long double timeout);
int queue_get(queue_s *queue, void **item, long double timeout);
int queue_get_free(queue_s *queue);
int us_queue_put(us_queue_s *queue, void *item, long double timeout);
int us_queue_get(us_queue_s *queue, void **item, long double timeout);
int us_queue_get_free(us_queue_s *queue);

View File

@@ -26,28 +26,28 @@
#include "rtp.h"
rtp_s *rtp_init(unsigned payload, bool video, bool zero_playout_delay) {
rtp_s *rtp;
A_CALLOC(rtp, 1);
us_rtp_s *us_rtp_init(unsigned payload, bool video, bool zero_playout_delay) {
us_rtp_s *rtp;
US_CALLOC(rtp, 1);
rtp->payload = payload;
rtp->video = video;
rtp->zero_playout_delay = zero_playout_delay; // See client.c
rtp->ssrc = triple_u32(get_now_monotonic_u64());
rtp->ssrc = us_triple_u32(us_get_now_monotonic_u64());
return rtp;
}
rtp_s *rtp_dup(const rtp_s *rtp) {
rtp_s *new;
A_CALLOC(new, 1);
memcpy(new, rtp, sizeof(rtp_s));
us_rtp_s *us_rtp_dup(const us_rtp_s *rtp) {
us_rtp_s *new;
US_CALLOC(new, 1);
memcpy(new, rtp, sizeof(us_rtp_s));
return new;
}
void rtp_destroy(rtp_s *rtp) {
void us_rtp_destroy(us_rtp_s *rtp) {
free(rtp);
}
void rtp_write_header(rtp_s *rtp, uint32_t pts, bool marked) {
void us_rtp_write_header(us_rtp_s *rtp, uint32_t pts, bool marked) {
uint32_t word0 = 0x80000000;
if (marked) {
word0 |= 1 << 23;

View File

@@ -32,8 +32,8 @@
// https://stackoverflow.com/questions/47635545/why-webrtc-chose-rtp-max-packet-size-to-1200-bytes
#define RTP_DATAGRAM_SIZE 1200
#define RTP_HEADER_SIZE 12
#define US_RTP_DATAGRAM_SIZE 1200
#define US_RTP_HEADER_SIZE 12
typedef struct {
@@ -43,15 +43,15 @@ typedef struct {
uint32_t ssrc;
uint16_t seq;
uint8_t datagram[RTP_DATAGRAM_SIZE];
uint8_t datagram[US_RTP_DATAGRAM_SIZE];
size_t used;
} rtp_s;
} us_rtp_s;
typedef void (*rtp_callback_f)(const rtp_s *rtp);
typedef void (*us_rtp_callback_f)(const us_rtp_s *rtp);
rtp_s *rtp_init(unsigned payload, bool video, bool zero_playout_delay);
rtp_s *rtp_dup(const rtp_s *rtp);
void rtp_destroy(rtp_s *rtp);
us_rtp_s *us_rtp_init(unsigned payload, bool video, bool zero_playout_delay);
us_rtp_s *us_rtp_dup(const us_rtp_s *rtp);
void us_rtp_destroy(us_rtp_s *rtp);
void rtp_write_header(rtp_s *rtp, uint32_t pts, bool marked);
void us_rtp_write_header(us_rtp_s *rtp, uint32_t pts, bool marked);

View File

@@ -23,23 +23,23 @@
#include "rtpa.h"
rtpa_s *rtpa_init(rtp_callback_f callback) {
rtpa_s *rtpa;
A_CALLOC(rtpa, 1);
rtpa->rtp = rtp_init(111, false, false);
us_rtpa_s *us_rtpa_init(us_rtp_callback_f callback) {
us_rtpa_s *rtpa;
US_CALLOC(rtpa, 1);
rtpa->rtp = us_rtp_init(111, false, false);
rtpa->callback = callback;
return rtpa;
}
void rtpa_destroy(rtpa_s *rtpa) {
rtp_destroy(rtpa->rtp);
void us_rtpa_destroy(us_rtpa_s *rtpa) {
us_rtp_destroy(rtpa->rtp);
free(rtpa);
}
char *rtpa_make_sdp(rtpa_s *rtpa) {
char *us_rtpa_make_sdp(us_rtpa_s *rtpa) {
# define PAYLOAD rtpa->rtp->payload
char *sdp;
A_ASPRINTF(sdp,
US_ASPRINTF(sdp,
"m=audio 1 RTP/SAVPF %u" RN
"c=IN IP4 0.0.0.0" RN
"a=rtpmap:%u OPUS/48000/2" RN
@@ -56,11 +56,11 @@ char *rtpa_make_sdp(rtpa_s *rtpa) {
return sdp;
}
void rtpa_wrap(rtpa_s *rtpa, const uint8_t *data, size_t size, uint32_t pts) {
if (size + RTP_HEADER_SIZE <= RTP_DATAGRAM_SIZE) {
rtp_write_header(rtpa->rtp, pts, false);
memcpy(rtpa->rtp->datagram + RTP_HEADER_SIZE, data, size);
rtpa->rtp->used = size + RTP_HEADER_SIZE;
void us_rtpa_wrap(us_rtpa_s *rtpa, const uint8_t *data, size_t size, uint32_t pts) {
if (size + US_RTP_HEADER_SIZE <= US_RTP_DATAGRAM_SIZE) {
us_rtp_write_header(rtpa->rtp, pts, false);
memcpy(rtpa->rtp->datagram + US_RTP_HEADER_SIZE, data, size);
rtpa->rtp->used = size + US_RTP_HEADER_SIZE;
rtpa->callback(rtpa->rtp);
}
}

View File

@@ -36,13 +36,13 @@
typedef struct {
rtp_s *rtp;
rtp_callback_f callback;
} rtpa_s;
us_rtp_s *rtp;
us_rtp_callback_f callback;
} us_rtpa_s;
rtpa_s *rtpa_init(rtp_callback_f callback);
void rtpa_destroy(rtpa_s *rtpa);
us_rtpa_s *us_rtpa_init(us_rtp_callback_f callback);
void us_rtpa_destroy(us_rtpa_s *rtpa);
char *rtpa_make_sdp(rtpa_s *rtpa);
void rtpa_wrap(rtpa_s *rtpa, const uint8_t *data, size_t size, uint32_t pts);
char *us_rtpa_make_sdp(us_rtpa_s *rtpa);
void us_rtpa_wrap(us_rtpa_s *rtpa, const uint8_t *data, size_t size, uint32_t pts);

View File

@@ -26,50 +26,50 @@
#include "rtpv.h"
void _rtpv_process_nalu(rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t pts, bool marked);
void _rtpv_process_nalu(us_rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t pts, bool marked);
static ssize_t _find_annexb(const uint8_t *data, size_t size);
rtpv_s *rtpv_init(rtp_callback_f callback, bool zero_playout_delay) {
rtpv_s *rtpv;
A_CALLOC(rtpv, 1);
rtpv->rtp = rtp_init(96, true, zero_playout_delay);
us_rtpv_s *us_rtpv_init(us_rtp_callback_f callback, bool zero_playout_delay) {
us_rtpv_s *rtpv;
US_CALLOC(rtpv, 1);
rtpv->rtp = us_rtp_init(96, true, zero_playout_delay);
rtpv->callback = callback;
rtpv->sps = frame_init();
rtpv->pps = frame_init();
A_MUTEX_INIT(&rtpv->mutex);
rtpv->sps = us_frame_init();
rtpv->pps = us_frame_init();
US_MUTEX_INIT(&rtpv->mutex);
return rtpv;
}
void rtpv_destroy(rtpv_s *rtpv) {
A_MUTEX_DESTROY(&rtpv->mutex);
frame_destroy(rtpv->pps);
frame_destroy(rtpv->sps);
rtp_destroy(rtpv->rtp);
void us_rtpv_destroy(us_rtpv_s *rtpv) {
US_MUTEX_DESTROY(&rtpv->mutex);
us_frame_destroy(rtpv->pps);
us_frame_destroy(rtpv->sps);
us_rtp_destroy(rtpv->rtp);
free(rtpv);
}
char *rtpv_make_sdp(rtpv_s *rtpv) {
A_MUTEX_LOCK(&rtpv->mutex);
char *us_rtpv_make_sdp(us_rtpv_s *rtpv) {
US_MUTEX_LOCK(&rtpv->mutex);
if (rtpv->sps->used == 0 || rtpv->pps->used == 0) {
A_MUTEX_UNLOCK(&rtpv->mutex);
US_MUTEX_UNLOCK(&rtpv->mutex);
return NULL;
}
char *sps = NULL;
char *pps = NULL;
base64_encode(rtpv->sps->data, rtpv->sps->used, &sps, NULL);
base64_encode(rtpv->pps->data, rtpv->pps->used, &pps, NULL);
us_base64_encode(rtpv->sps->data, rtpv->sps->used, &sps, NULL);
us_base64_encode(rtpv->pps->data, rtpv->pps->used, &pps, NULL);
A_MUTEX_UNLOCK(&rtpv->mutex);
US_MUTEX_UNLOCK(&rtpv->mutex);
# define PAYLOAD rtpv->rtp->payload
// https://tools.ietf.org/html/rfc6184
// https://github.com/meetecho/janus-gateway/issues/2443
char *sdp;
A_ASPRINTF(sdp,
US_ASPRINTF(sdp,
"m=video 1 RTP/SAVPF %u" RN
"c=IN IP4 0.0.0.0" RN
"a=rtpmap:%u H264/90000" RN
@@ -97,19 +97,19 @@ char *rtpv_make_sdp(rtpv_s *rtpv) {
return sdp;
}
#define PRE 3 // Annex B prefix length
#define _PRE 3 // Annex B prefix length
void rtpv_wrap(rtpv_s *rtpv, const frame_s *frame) {
void us_rtpv_wrap(us_rtpv_s *rtpv, const us_frame_s *frame) {
// There is a complicated logic here but everything works as it should:
// - https://github.com/pikvm/ustreamer/issues/115#issuecomment-893071775
assert(frame->format == V4L2_PIX_FMT_H264);
const uint32_t pts = get_now_monotonic_u64() * 9 / 100; // PTS units are in 90 kHz
ssize_t last_offset = -PRE;
const uint32_t pts = us_get_now_monotonic_u64() * 9 / 100; // PTS units are in 90 kHz
ssize_t last_offset = -_PRE;
while (true) { // Find and iterate by nalus
const size_t next_start = last_offset + PRE;
const size_t next_start = last_offset + _PRE;
ssize_t offset = _find_annexb(frame->data + next_start, frame->used - next_start);
if (offset < 0) {
break;
@@ -117,8 +117,8 @@ void rtpv_wrap(rtpv_s *rtpv, const frame_s *frame) {
offset += next_start;
if (last_offset >= 0) {
const uint8_t *data = frame->data + last_offset + PRE;
size_t size = offset - last_offset - PRE;
const uint8_t *data = frame->data + last_offset + _PRE;
size_t size = offset - last_offset - _PRE;
if (data[size - 1] == 0) { // Check for extra 00
--size;
}
@@ -129,53 +129,53 @@ void rtpv_wrap(rtpv_s *rtpv, const frame_s *frame) {
}
if (last_offset >= 0) {
const uint8_t *data = frame->data + last_offset + PRE;
size_t size = frame->used - last_offset - PRE;
const uint8_t *data = frame->data + last_offset + _PRE;
size_t size = frame->used - last_offset - _PRE;
_rtpv_process_nalu(rtpv, data, size, pts, true);
}
}
void _rtpv_process_nalu(rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t pts, bool marked) {
void _rtpv_process_nalu(us_rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t pts, bool marked) {
const unsigned ref_idc = (data[0] >> 5) & 3;
const unsigned type = data[0] & 0x1F;
frame_s *ps = NULL;
us_frame_s *ps = NULL;
switch (type) {
case 7: ps = rtpv->sps; break;
case 8: ps = rtpv->pps; break;
}
if (ps) {
A_MUTEX_LOCK(&rtpv->mutex);
frame_set_data(ps, data, size);
A_MUTEX_UNLOCK(&rtpv->mutex);
US_MUTEX_LOCK(&rtpv->mutex);
us_frame_set_data(ps, data, size);
US_MUTEX_UNLOCK(&rtpv->mutex);
}
# define DG rtpv->rtp->datagram
if (size + RTP_HEADER_SIZE <= RTP_DATAGRAM_SIZE) {
rtp_write_header(rtpv->rtp, pts, marked);
memcpy(DG + RTP_HEADER_SIZE, data, size);
rtpv->rtp->used = size + RTP_HEADER_SIZE;
if (size + US_RTP_HEADER_SIZE <= US_RTP_DATAGRAM_SIZE) {
us_rtp_write_header(rtpv->rtp, pts, marked);
memcpy(DG + US_RTP_HEADER_SIZE, data, size);
rtpv->rtp->used = size + US_RTP_HEADER_SIZE;
rtpv->callback(rtpv->rtp);
return;
}
const size_t fu_overhead = RTP_HEADER_SIZE + 2; // FU-A overhead
const size_t fu_overhead = US_RTP_HEADER_SIZE + 2; // FU-A overhead
const uint8_t *src = data + 1;
ssize_t remaining = size - 1;
bool first = true;
while (remaining > 0) {
ssize_t frag_size = RTP_DATAGRAM_SIZE - fu_overhead;
ssize_t frag_size = US_RTP_DATAGRAM_SIZE - fu_overhead;
const bool last = (remaining <= frag_size);
if (last) {
frag_size = remaining;
}
rtp_write_header(rtpv->rtp, pts, (marked && last));
us_rtp_write_header(rtpv->rtp, pts, (marked && last));
DG[RTP_HEADER_SIZE] = 28 | (ref_idc << 5);
DG[US_RTP_HEADER_SIZE] = 28 | (ref_idc << 5);
uint8_t fu = type;
if (first) {
@@ -184,7 +184,7 @@ void _rtpv_process_nalu(rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t
if (last) {
fu |= 0x40;
}
DG[RTP_HEADER_SIZE + 1] = fu;
DG[US_RTP_HEADER_SIZE + 1] = fu;
memcpy(DG + fu_overhead, src, frag_size);
rtpv->rtp->used = fu_overhead + frag_size;
@@ -200,8 +200,8 @@ void _rtpv_process_nalu(rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t
static ssize_t _find_annexb(const uint8_t *data, size_t size) {
// Parses buffer for 00 00 01 start codes
if (size >= PRE) {
for (size_t index = 0; index <= size - PRE; ++index) {
if (size >= _PRE) {
for (size_t index = 0; index <= size - _PRE; ++index) {
if (data[index] == 0 && data[index + 1] == 0 && data[index + 2] == 1) {
return index;
}
@@ -210,4 +210,4 @@ static ssize_t _find_annexb(const uint8_t *data, size_t size) {
return -1;
}
#undef PRE
#undef _PRE

View File

@@ -42,16 +42,16 @@
typedef struct {
rtp_s *rtp;
rtp_callback_f callback;
frame_s *sps; // Actually not a frame, just a bytes storage
frame_s *pps;
pthread_mutex_t mutex;
} rtpv_s;
us_rtp_s *rtp;
us_rtp_callback_f callback;
us_frame_s *sps; // Actually not a frame, just a bytes storage
us_frame_s *pps;
pthread_mutex_t mutex;
} us_rtpv_s;
rtpv_s *rtpv_init(rtp_callback_f callback, bool zero_playout_delay);
void rtpv_destroy(rtpv_s *rtpv);
us_rtpv_s *us_rtpv_init(us_rtp_callback_f callback, bool zero_playout_delay);
void us_rtpv_destroy(us_rtpv_s *rtpv);
char *rtpv_make_sdp(rtpv_s *rtpv);
void rtpv_wrap(rtpv_s *rtpv, const frame_s *frame);
char *us_rtpv_make_sdp(us_rtpv_s *rtpv);
void us_rtpv_wrap(us_rtpv_s *rtpv, const us_frame_s *frame);

View File

@@ -34,24 +34,24 @@
#endif
int tc358743_read_info(const char *path, tc358743_info_s *info) {
MEMSET_ZERO(*info);
int us_tc358743_read_info(const char *path, us_tc358743_info_s *info) {
US_MEMSET_ZERO(*info);
int fd = -1;
if ((fd = open(path, O_RDWR)) < 0) {
JLOG_PERROR("audio", "Can't open TC358743 V4L2 device");
US_JLOG_PERROR("audio", "Can't open TC358743 V4L2 device");
return -1;
}
# define READ_CID(_cid, _field) { \
struct v4l2_control ctl = {0}; \
ctl.id = _cid; \
if (xioctl(fd, VIDIOC_G_CTRL, &ctl) < 0) { \
JLOG_PERROR("audio", "Can't get value of " #_cid); \
# define READ_CID(x_cid, x_field) { \
struct v4l2_control m_ctl = {0}; \
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->_field = ctl.value; \
info->x_field = m_ctl.value; \
}
READ_CID(TC358743_CID_AUDIO_PRESENT, has_audio);

View File

@@ -40,7 +40,7 @@
typedef struct {
bool has_audio;
unsigned audio_hz;
} tc358743_info_s;
} us_tc358743_info_s;
int tc358743_read_info(const char *path, tc358743_info_s *info);
int us_tc358743_read_info(const char *path, us_tc358743_info_s *info);