refactoring, const

This commit is contained in:
Maxim Devaev
2022-07-20 06:42:18 +03:00
parent 5c747a5b5d
commit 2c9334d53f
35 changed files with 246 additions and 258 deletions

View File

@@ -168,11 +168,11 @@ int us_audio_get_encoded(us_audio_s *audio, uint8_t *data, size_t *size, uint64_
static void *_pcm_thread(void *v_audio) {
US_THREAD_RENAME("us_a_pcm");
us_audio_s *audio = (us_audio_s *)v_audio;
us_audio_s *const 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);
const 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");
break;
@@ -198,14 +198,14 @@ static void *_pcm_thread(void *v_audio) {
static void *_encoder_thread(void *v_audio) {
US_THREAD_RENAME("us_a_enc");
us_audio_s *audio = (us_audio_s *)v_audio;
us_audio_s *const audio = (us_audio_s *)v_audio;
int16_t in_res[_MAX_BUF16];
while (!atomic_load(&audio->stop)) {
_pcm_buffer_s *in;
if (!us_queue_get(audio->pcm_queue, (void **)&in, 0.1)) {
int16_t *in_ptr;
if (audio->res) {
if (audio->res != NULL) {
assert(audio->pcm_hz != _ENCODER_INPUT_HZ);
uint32_t in_count = audio->pcm_frames;
uint32_t out_count = _HZ_TO_FRAMES(_ENCODER_INPUT_HZ);
@@ -218,7 +218,7 @@ static void *_encoder_thread(void *v_audio) {
_enc_buffer_s *out;
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));
const 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");

View File

@@ -70,7 +70,7 @@ void us_janus_client_send(us_janus_client_s *client, const us_rtp_s *rtp) {
) {
return;
}
us_rtp_s *new = us_rtp_dup(rtp);
us_rtp_s *const 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"));
@@ -87,8 +87,8 @@ static void *_audio_thread(void *v_client) {
}
static void *_common_thread(void *v_client, bool video) {
us_janus_client_s *client = (us_janus_client_s *)v_client;
us_queue_s *queue = (video ? client->video_queue : client->audio_queue);
us_janus_client_s *const client = (us_janus_client_s *)v_client;
us_queue_s *const queue = (video ? client->video_queue : client->audio_queue);
assert(queue != NULL); // Audio may be NULL
while (!atomic_load(&client->stop)) {

View File

@@ -67,9 +67,7 @@ us_config_s *us_config_init(const char *config_dir_path) {
us_config_destroy(config);
config = NULL;
ok:
if (jcfg) {
janus_config_destroy(jcfg);
}
US_DELETE(jcfg, janus_config_destroy);
free(config_file_path);
return config;
}
@@ -87,11 +85,11 @@ static char *_get_value(janus_config *jcfg, const char *section, const char *opt
if (option_obj == NULL || option_obj->value == NULL || option_obj->value[0] == '\0') {
return NULL;
}
return strdup(option_obj->value);
return us_strdup(option_obj->value);
}
static bool _get_bool(janus_config *jcfg, const char *section, const char *option, bool def) {
char *tmp = _get_value(jcfg, section, option);
char *const tmp = _get_value(jcfg, section, option);
bool value = def;
if (tmp != NULL) {
value = (!strcasecmp(tmp, "1") || !strcasecmp(tmp, "true") || !strcasecmp(tmp, "yes"));

View File

@@ -24,10 +24,10 @@
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
const long double deadline_ts = us_get_now_monotonic() + 1; // wait_timeout
long double now;
do {
int result = us_flock_timedwait_monotonic(fd, 1); // lock_timeout
const int result = us_flock_timedwait_monotonic(fd, 1); // lock_timeout
now = us_get_now_monotonic();
if (result < 0 && errno != EWOULDBLOCK) {
US_JLOG_PERROR("video", "Can't lock memsink");

View File

@@ -96,7 +96,7 @@ janus_plugin *create(void);
#define _IF_NOT_REPORTED(...) { \
unsigned _error_code = __LINE__; \
const unsigned _error_code = __LINE__; \
if (error_reported != _error_code) { __VA_ARGS__; error_reported = _error_code; } \
}
@@ -147,9 +147,9 @@ static void *_video_sink_thread(UNUSED void *arg) {
US_JLOG_INFO("video", "Memsink opened; reading frames ...");
while (!_STOP && _HAS_WATCHERS) {
int result = us_memsink_fd_wait_frame(fd, mem, frame_id);
const int result = us_memsink_fd_wait_frame(fd, mem, frame_id);
if (result == 0) {
us_frame_s *frame = us_memsink_fd_get_frame(fd, mem, &frame_id);
us_frame_s *const frame = us_memsink_fd_get_frame(fd, mem, &frame_id);
if (frame == NULL) {
goto close_memsink;
}
@@ -180,8 +180,8 @@ static void *_video_sink_thread(UNUSED void *arg) {
static void *_audio_thread(UNUSED void *arg) {
US_THREAD_RENAME("us_audio");
atomic_store(&_g_audio_tid_created, true);
assert(_g_config->audio_dev_name);
assert(_g_config->tc358743_dev_path);
assert(_g_config->audio_dev_name != NULL);
assert(_g_config->tc358743_dev_path != NULL);
unsigned error_reported = 0;
@@ -220,7 +220,7 @@ static void *_audio_thread(UNUSED void *arg) {
size_t size = US_RTP_DATAGRAM_SIZE - US_RTP_HEADER_SIZE;
uint8_t data[size];
uint64_t pts;
int result = us_audio_get_encoded(audio, data, &size, &pts);
const int result = us_audio_get_encoded(audio, data, &size, &pts);
if (result == 0) {
_LOCK_AUDIO;
us_rtpa_wrap(_g_rtpa, data, size, pts);
@@ -260,7 +260,7 @@ static int _plugin_init(janus_callbacks *gw, const char *config_dir_path) {
_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) {
if (_g_config->audio_dev_name != NULL) {
_g_rtpa = us_rtpa_init(_relay_rtp_clients);
US_THREAD_CREATE(&_g_audio_tid, _audio_thread, NULL);
}
@@ -299,7 +299,7 @@ static void _plugin_create_session(janus_plugin_session *session, int *err) {
_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_janus_client_s *const 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;
@@ -373,8 +373,8 @@ static struct janus_plugin_result *_plugin_handle_message(
assert(transaction != NULL);
# define FREE_MSG_JSEP { \
if (msg) json_decref(msg); \
if (jsep) json_decref(jsep); \
US_DELETE(msg, json_decref); \
US_DELETE(jsep, json_decref); \
}
if (session == NULL || msg == NULL) {
@@ -393,23 +393,23 @@ static struct janus_plugin_result *_plugin_handle_message(
json_decref(m_event); \
}
json_t *request_obj = json_object_get(msg, "request");
json_t *const request_obj = json_object_get(msg, "request");
if (request_obj == NULL) {
PUSH_ERROR(400, "Request missing");
goto ok_wait;
}
const char *request_str = json_string_value(request_obj);
if (!request_str) {
const char *const request_str = json_string_value(request_obj);
if (request_str == NULL) {
PUSH_ERROR(400, "Request not a string");
goto ok_wait;
}
// US_JLOG_INFO("main", "Message: %s", request_str);
# define PUSH_STATUS(x_status, x_jsep) { \
json_t *m_event = json_object(); \
json_t *const m_event = json_object(); \
json_object_set_new(m_event, "ustreamer", json_string("event")); \
json_t *m_result = json_object(); \
json_t *const 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); \
@@ -425,12 +425,12 @@ static struct janus_plugin_result *_plugin_handle_message(
} else if (!strcmp(request_str, "watch")) {
char *sdp;
{
char *video_sdp = us_rtpv_make_sdp(_g_rtpv);
char *const 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 ? us_rtpa_make_sdp(_g_rtpa) : strdup(""));
char *const audio_sdp = (_g_rtpa ? us_rtpa_make_sdp(_g_rtpa) : us_strdup(""));
US_ASPRINTF(sdp,
"v=0" RN
"o=- %" PRIu64 " 1 IN IP4 0.0.0.0" RN
@@ -442,7 +442,7 @@ static struct janus_plugin_result *_plugin_handle_message(
free(audio_sdp);
free(video_sdp);
}
json_t *offer_jsep = json_pack("{ssss}", "type", "offer", "sdp", sdp);
json_t *const offer_jsep = json_pack("{ssss}", "type", "offer", "sdp", sdp);
free(sdp);
PUSH_STATUS("started", offer_jsep);
json_decref(offer_jsep);

View File

@@ -49,7 +49,7 @@ void us_queue_destroy(us_queue_s *queue) {
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); \
const int err = pthread_cond_timedwait(x_cond, &queue->mutex, &m_ts); \
if (err == ETIMEDOUT) { \
US_MUTEX_UNLOCK(&queue->mutex); \
return -1; \
@@ -93,7 +93,7 @@ int us_queue_get(us_queue_s *queue, void **item, long double timeout) {
int us_queue_get_free(us_queue_s *queue) {
US_MUTEX_LOCK(&queue->mutex);
unsigned size = queue->size;
const unsigned size = queue->size;
US_MUTEX_UNLOCK(&queue->mutex);
return queue->capacity - size;
}

View File

@@ -117,7 +117,7 @@ void us_rtpv_wrap(us_rtpv_s *rtpv, const us_frame_s *frame) {
offset += next_start;
if (last_offset >= 0) {
const uint8_t *data = frame->data + last_offset + _PRE;
const uint8_t *const data = frame->data + last_offset + _PRE;
size_t size = offset - last_offset - _PRE;
if (data[size - 1] == 0) { // Check for extra 00
--size;
@@ -129,7 +129,7 @@ void us_rtpv_wrap(us_rtpv_s *rtpv, const us_frame_s *frame) {
}
if (last_offset >= 0) {
const uint8_t *data = frame->data + last_offset + _PRE;
const uint8_t *const data = frame->data + last_offset + _PRE;
size_t size = frame->used - last_offset - _PRE;
_rtpv_process_nalu(rtpv, data, size, pts, true);
}
@@ -144,7 +144,7 @@ void _rtpv_process_nalu(us_rtpv_s *rtpv, const uint8_t *data, size_t size, uint3
case 7: ps = rtpv->sps; break;
case 8: ps = rtpv->pps; break;
}
if (ps) {
if (ps != NULL) {
US_MUTEX_LOCK(&rtpv->mutex);
us_frame_set_data(ps, data, size);
US_MUTEX_UNLOCK(&rtpv->mutex);