refactoring

This commit is contained in:
Maxim Devaev
2022-07-20 06:05:05 +03:00
parent cbee3adb2e
commit 5c747a5b5d
12 changed files with 33 additions and 61 deletions

View File

@@ -134,20 +134,12 @@ void us_audio_destroy(us_audio_s *audio) {
US_THREAD_JOIN(audio->pcm_tid);
US_THREAD_JOIN(audio->enc_tid);
}
if (audio->enc) {
opus_encoder_destroy(audio->enc);
}
if (audio->res) {
speex_resampler_destroy(audio->res);
}
if (audio->pcm) {
snd_pcm_close(audio->pcm);
}
if (audio->pcm_params) {
snd_pcm_hw_params_free(audio->pcm_params);
}
US_QUEUE_FREE_ITEMS_AND_DESTROY(audio->enc_queue, free);
US_QUEUE_FREE_ITEMS_AND_DESTROY(audio->pcm_queue, free);
US_DELETE(audio->enc, opus_encoder_destroy);
US_DELETE(audio->res, speex_resampler_destroy);
US_DELETE(audio->pcm, snd_pcm_close);
US_DELETE(audio->pcm_params, snd_pcm_hw_params_free);
US_QUEUE_DELETE_WITH_ITEMS(audio->enc_queue, free);
US_QUEUE_DELETE_WITH_ITEMS(audio->pcm_queue, free);
if (audio->tids_created) {
US_JLOG_INFO("audio", "Pipeline closed");
}

View File

@@ -55,10 +55,10 @@ void us_janus_client_destroy(us_janus_client_s *client) {
}
US_THREAD_JOIN(client->video_tid);
US_QUEUE_FREE_ITEMS_AND_DESTROY(client->video_queue, us_rtp_destroy);
US_QUEUE_DELETE_WITH_ITEMS(client->video_queue, us_rtp_destroy);
if (client->audio_queue != NULL) {
US_THREAD_JOIN(client->audio_tid);
US_QUEUE_FREE_ITEMS_AND_DESTROY(client->audio_queue, us_rtp_destroy);
US_QUEUE_DELETE_WITH_ITEMS(client->audio_queue, us_rtp_destroy);
}
free(client);
}

View File

@@ -231,9 +231,7 @@ static void *_audio_thread(UNUSED void *arg) {
}
close_audio:
if (audio != NULL) {
us_audio_destroy(audio);
}
US_DELETE(audio, us_audio_destroy);
sleep(1); // error_delay
}
return NULL;
@@ -288,7 +286,7 @@ static void _plugin_destroy(void) {
us_janus_client_destroy(client);
});
US_QUEUE_FREE_ITEMS_AND_DESTROY(_g_video_queue, us_frame_destroy);
US_QUEUE_DELETE_WITH_ITEMS(_g_video_queue, us_frame_destroy);
US_DELETE(_g_rtpa, us_rtpa_destroy);
US_DELETE(_g_rtpv, us_rtpv_destroy);

View File

@@ -47,13 +47,12 @@ typedef struct {
} us_queue_s;
#define US_QUEUE_FREE_ITEMS_AND_DESTROY(x_queue, x_free_item) { \
#define US_QUEUE_DELETE_WITH_ITEMS(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); \
if (!us_queue_get(x_queue, &m_ptr, 0)) { \
US_DELETE(m_ptr, x_free_item); \
} \
} \
us_queue_destroy(x_queue); \

View File

@@ -29,14 +29,14 @@ typedef struct {
int fd;
us_memsink_shared_s *mem;
uint64_t frame_id;
long double frame_ts;
uint64_t frame_id;
long double frame_ts;
us_frame_s *frame;
} _MemsinkObject;
#define _MEM(_next) self->mem->_next
#define _FRAME(_next) self->frame->_next
#define _MEM(x_next) self->mem->x_next
#define _FRAME(x_next) self->frame->x_next
static void _MemsinkObject_destroy_internals(_MemsinkObject *self) {

View File

@@ -67,9 +67,7 @@ void us_output_file_write(void *v_output, const us_frame_s *frame) {
void us_output_file_destroy(void *v_output) {
us_output_file_s *output = (us_output_file_s *)v_output;
if (output->base64_data) {
free(output->base64_data);
}
US_DELETE(output->base64_data, free);
if (output->fp && output->fp != stdout) {
if (fclose(output->fp) < 0) {
US_LOG_PERROR("Can't close output file");

View File

@@ -300,9 +300,7 @@ static int _dump_sink(
retval = -1;
ok:
if (sink) {
us_memsink_destroy(sink);
}
US_DELETE(sink, us_memsink_destroy);
us_frame_destroy(frame);
US_LOG_INFO("Bye-bye");

View File

@@ -32,9 +32,7 @@ us_frame_s *us_frame_init(void) {
}
void us_frame_destroy(us_frame_s *frame) {
if (frame->data) {
free(frame->data);
}
US_DELETE(frame->data, free);
free(frame);
}

View File

@@ -36,7 +36,6 @@ us_memsink_s *us_memsink_init(
sink->client_ttl = client_ttl;
sink->timeout = timeout;
sink->fd = -1;
sink->mem = MAP_FAILED;
atomic_init(&sink->has_clients, false);
US_LOG_INFO("Using %s-sink: %s", name, obj);
@@ -68,7 +67,7 @@ us_memsink_s *us_memsink_init(
}
void us_memsink_destroy(us_memsink_s *sink) {
if (sink->mem != MAP_FAILED) {
if (sink->mem != NULL) {
if (us_memsink_shared_unmap(sink->mem) < 0) {
US_LOG_PERROR("%s-sink: Can't unmap shared memory", sink->name);
}

View File

@@ -185,7 +185,7 @@ void us_device_close(us_device_s *dev) {
}
if (dev->io_method == V4L2_MEMORY_MMAP) {
if (HW(raw.allocated) > 0 && HW(raw.data) != MAP_FAILED) {
if (HW(raw.allocated) > 0 && HW(raw.data) != NULL) {
if (munmap(HW(raw.data), HW(raw.allocated)) < 0) {
US_LOG_PERROR("Can't unmap device buffer=%u", index);
}
@@ -694,6 +694,7 @@ static int _device_open_io_method_mmap(us_device_s *dev) {
US_LOG_PERROR("Can't map device buffer=%u", _RUN(n_bufs));
return -1;
}
assert(HW(raw.data) != NULL);
HW(raw.allocated) = buf.length;
# undef HW

View File

@@ -93,7 +93,7 @@ void us_server_destroy(us_server_s *server) {
}
evhttp_free(_RUN(http));
if (_RUN(ext_fd)) {
if (_RUN(ext_fd) >= 0) {
close(_RUN(ext_fd));
}
event_base_free(_RUN(base));
@@ -108,9 +108,7 @@ void us_server_destroy(us_server_s *server) {
free(client);
});
if (_RUN(auth_token)) {
free(_RUN(auth_token));
}
US_DELETE(_RUN(auth_token), free);
us_frame_destroy(_EX(frame));
free(_RUN(exposed));
@@ -377,18 +375,10 @@ static void _http_callback_static(struct evhttp_request *request, void *v_server
if (fd >= 0) {
close(fd);
}
if (static_path) {
free(static_path);
}
if (buf) {
evbuffer_free(buf);
}
if (decoded_path) {
free(decoded_path);
}
if (uri) {
evhttp_uri_free(uri);
}
US_DELETE(static_path, free);
US_DELETE(buf, evbuffer_free);
US_DELETE(decoded_path, free);
US_DELETE(uri, evhttp_uri_free);
}
#undef COMPAT_REQUEST
@@ -927,9 +917,7 @@ static char *_http_get_client_hostport(struct evhttp_request *request) {
const char *xff = _http_get_header(request, "X-Forwarded-For");
if (xff) {
if (addr) {
free(addr);
}
US_DELETE(addr, free);
assert(addr = strndup(xff, 1024));
for (unsigned index = 0; addr[index]; ++index) {
if (addr[index] == ',') {

View File

@@ -312,10 +312,11 @@ static int _m2m_encoder_init_buffers(
MAP_SHARED,
_RUN(fd),
plane.m.mem_offset
)) == MAP_FAILED) {
)) == NULL) {
_E_LOG_PERROR("Can't map %s buffer=%u", name, *n_bufs_ptr);
goto error;
}
assert((*bufs_ptr)[*n_bufs_ptr].data != NULL);
(*bufs_ptr)[*n_bufs_ptr].allocated = plane.length;
_E_LOG_DEBUG("Queuing %s buffer=%u ...", name, *n_bufs_ptr);
@@ -347,7 +348,7 @@ static void _m2m_encoder_cleanup(us_m2m_encoder_s *enc) {
# define DESTROY_BUFFERS(x_name, x_target) { \
if (_RUN(x_target##_bufs)) { \
for (unsigned m_index = 0; m_index < _RUN(n_##x_target##_bufs); ++m_index) { \
if (_RUN(x_target##_bufs[m_index].allocated) > 0 && _RUN(x_target##_bufs[m_index].data) != MAP_FAILED) { \
if (_RUN(x_target##_bufs[m_index].allocated) > 0 && _RUN(x_target##_bufs[m_index].data) != NULL) { \
if (munmap(_RUN(x_target##_bufs[m_index].data), _RUN(x_target##_bufs[m_index].allocated)) < 0) { \
_E_LOG_PERROR("Can't unmap %s buffer=%u", #x_name, m_index); \
} \