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->pcm_tid);
US_THREAD_JOIN(audio->enc_tid); US_THREAD_JOIN(audio->enc_tid);
} }
if (audio->enc) { US_DELETE(audio->enc, opus_encoder_destroy);
opus_encoder_destroy(audio->enc); US_DELETE(audio->res, speex_resampler_destroy);
} US_DELETE(audio->pcm, snd_pcm_close);
if (audio->res) { US_DELETE(audio->pcm_params, snd_pcm_hw_params_free);
speex_resampler_destroy(audio->res); US_QUEUE_DELETE_WITH_ITEMS(audio->enc_queue, free);
} US_QUEUE_DELETE_WITH_ITEMS(audio->pcm_queue, free);
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);
if (audio->tids_created) { if (audio->tids_created) {
US_JLOG_INFO("audio", "Pipeline closed"); 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_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) { if (client->audio_queue != NULL) {
US_THREAD_JOIN(client->audio_tid); 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); free(client);
} }

View File

@@ -231,9 +231,7 @@ static void *_audio_thread(UNUSED void *arg) {
} }
close_audio: close_audio:
if (audio != NULL) { US_DELETE(audio, us_audio_destroy);
us_audio_destroy(audio);
}
sleep(1); // error_delay sleep(1); // error_delay
} }
return NULL; return NULL;
@@ -288,7 +286,7 @@ static void _plugin_destroy(void) {
us_janus_client_destroy(client); 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_rtpa, us_rtpa_destroy);
US_DELETE(_g_rtpv, us_rtpv_destroy); US_DELETE(_g_rtpv, us_rtpv_destroy);

View File

@@ -47,13 +47,12 @@ typedef struct {
} us_queue_s; } 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) { \ if (x_queue) { \
while (!us_queue_get_free(x_queue)) { \ while (!us_queue_get_free(x_queue)) { \
void *m_ptr; \ void *m_ptr; \
assert(!us_queue_get(x_queue, &m_ptr, 0.1)); \ if (!us_queue_get(x_queue, &m_ptr, 0)) { \
if (m_ptr != NULL) { \ US_DELETE(m_ptr, x_free_item); \
x_free_item(m_ptr); \
} \ } \
} \ } \
us_queue_destroy(x_queue); \ us_queue_destroy(x_queue); \

View File

@@ -29,14 +29,14 @@ typedef struct {
int fd; int fd;
us_memsink_shared_s *mem; us_memsink_shared_s *mem;
uint64_t frame_id; uint64_t frame_id;
long double frame_ts; long double frame_ts;
us_frame_s *frame; us_frame_s *frame;
} _MemsinkObject; } _MemsinkObject;
#define _MEM(_next) self->mem->_next #define _MEM(x_next) self->mem->x_next
#define _FRAME(_next) self->frame->_next #define _FRAME(x_next) self->frame->x_next
static void _MemsinkObject_destroy_internals(_MemsinkObject *self) { 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) { void us_output_file_destroy(void *v_output) {
us_output_file_s *output = (us_output_file_s *)v_output; us_output_file_s *output = (us_output_file_s *)v_output;
if (output->base64_data) { US_DELETE(output->base64_data, free);
free(output->base64_data);
}
if (output->fp && output->fp != stdout) { if (output->fp && output->fp != stdout) {
if (fclose(output->fp) < 0) { if (fclose(output->fp) < 0) {
US_LOG_PERROR("Can't close output file"); US_LOG_PERROR("Can't close output file");

View File

@@ -300,9 +300,7 @@ static int _dump_sink(
retval = -1; retval = -1;
ok: ok:
if (sink) { US_DELETE(sink, us_memsink_destroy);
us_memsink_destroy(sink);
}
us_frame_destroy(frame); us_frame_destroy(frame);
US_LOG_INFO("Bye-bye"); 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) { void us_frame_destroy(us_frame_s *frame) {
if (frame->data) { US_DELETE(frame->data, free);
free(frame->data);
}
free(frame); free(frame);
} }

View File

@@ -36,7 +36,6 @@ us_memsink_s *us_memsink_init(
sink->client_ttl = client_ttl; sink->client_ttl = client_ttl;
sink->timeout = timeout; sink->timeout = timeout;
sink->fd = -1; sink->fd = -1;
sink->mem = MAP_FAILED;
atomic_init(&sink->has_clients, false); atomic_init(&sink->has_clients, false);
US_LOG_INFO("Using %s-sink: %s", name, obj); 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) { 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) { if (us_memsink_shared_unmap(sink->mem) < 0) {
US_LOG_PERROR("%s-sink: Can't unmap shared memory", sink->name); 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 (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) { if (munmap(HW(raw.data), HW(raw.allocated)) < 0) {
US_LOG_PERROR("Can't unmap device buffer=%u", index); 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)); US_LOG_PERROR("Can't map device buffer=%u", _RUN(n_bufs));
return -1; return -1;
} }
assert(HW(raw.data) != NULL);
HW(raw.allocated) = buf.length; HW(raw.allocated) = buf.length;
# undef HW # undef HW

View File

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

View File

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