diff --git a/daemon/codec.c b/daemon/codec.c index a1acec066..32b8330d1 100644 --- a/daemon/codec.c +++ b/daemon/codec.c @@ -5662,11 +5662,11 @@ static void codec_timers_run(void *p) { } void codecs_init(void) { - timerthread_init(&codec_timers_thread, codec_timers_run); + timerthread_init(&codec_timers_thread, rtpe_config.media_num_threads, codec_timers_run); } void codecs_cleanup(void) { timerthread_free(&codec_timers_thread); } -void codec_timers_loop(void *p) { - timerthread_run(&codec_timers_thread); +void codec_timers_launch(void) { + timerthread_launch(&codec_timers_thread, rtpe_config.scheduling, rtpe_config.priority, "codec timer"); } diff --git a/daemon/ice.c b/daemon/ice.c index f565958f2..dfdbcdade 100644 --- a/daemon/ice.c +++ b/daemon/ice.c @@ -749,7 +749,7 @@ static void __agent_deschedule(struct ice_agent *ag) { void ice_init(void) { random_string((void *) &tie_breaker, sizeof(tie_breaker)); - timerthread_init(&ice_agents_timer_thread, ice_agents_timer_run); + timerthread_init(&ice_agents_timer_thread, 1, ice_agents_timer_run); sdp_fragments = fragments_ht_new(); mutex_init(&sdp_fragments_lock); @@ -1527,8 +1527,8 @@ err: -void ice_thread_run(void *p) { - timerthread_run(&ice_agents_timer_thread); +void ice_thread_launch(void) { + timerthread_launch(&ice_agents_timer_thread, NULL, 0, "ICE"); } static void ice_agents_timer_run(void *ptr) { struct ice_agent *ag = ptr; diff --git a/daemon/jitter_buffer.c b/daemon/jitter_buffer.c index b36388a3c..61483eccc 100644 --- a/daemon/jitter_buffer.c +++ b/daemon/jitter_buffer.c @@ -23,7 +23,8 @@ static struct timerthread jitter_buffer_thread; void jitter_buffer_init(void) { //ilog(LOG_DEBUG, "jitter_buffer_init"); - timerthread_init(&jitter_buffer_thread, timerthread_queue_run); + unsigned int num_threads = rtpe_config.jb_length > 0 ? rtpe_config.media_num_threads : 0; + timerthread_init(&jitter_buffer_thread, num_threads, timerthread_queue_run); } void jitter_buffer_init_free(void) { @@ -412,9 +413,8 @@ void __jb_packet_free(void *p) { jb_packet_free(&jbp); } -void jitter_buffer_loop(void *p) { - ilog(LOG_DEBUG, "jitter_buffer_loop"); - timerthread_run(&jitter_buffer_thread); +void jitter_buffer_launch(void) { + timerthread_launch(&jitter_buffer_thread, rtpe_config.scheduling, rtpe_config.priority, "jitter buffer"); } struct jitter_buffer *jitter_buffer_new(call_t *c) { diff --git a/daemon/main.c b/daemon/main.c index 08fde6baa..6e747b064 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -1420,7 +1420,7 @@ int main(int argc, char **argv) { thread_create_detach(mqtt_loop, NULL, "mqtt"); #endif - thread_create_detach(ice_thread_run, NULL, "ICE"); + ice_thread_launch(); websocket_start(); @@ -1436,20 +1436,10 @@ int main(int argc, char **argv) { if (rtpe_config.poller_per_thread) thread_create_detach_prio(poller_loop2, rtpe_poller, rtpe_config.scheduling, rtpe_config.priority, "poller"); - for (idx = 0; idx < rtpe_config.media_num_threads; ++idx) { -#ifdef WITH_TRANSCODING - thread_create_detach_prio(media_player_loop, NULL, rtpe_config.scheduling, - rtpe_config.priority, "media player"); -#endif - thread_create_detach_prio(send_timer_loop, NULL, rtpe_config.scheduling, - rtpe_config.priority, "send timer"); - if (rtpe_config.jb_length > 0) - thread_create_detach_prio(jitter_buffer_loop, NULL, rtpe_config.scheduling, - rtpe_config.priority, "jitter buffer"); - thread_create_detach_prio(codec_timers_loop, NULL, rtpe_config.scheduling, - rtpe_config.priority, "codec timer"); - } - + media_player_launch(); + send_timer_launch(); + jitter_buffer_launch(); + codec_timers_launch(); // reap threads as they shut down during run time threads_join_all(false); diff --git a/daemon/media_player.c b/daemon/media_player.c index 753a37c4d..de11cc354 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -1301,9 +1301,9 @@ void media_player_init(void) { mutex_init(&media_player_cache_lock); } - timerthread_init(&media_player_thread, media_player_run); + timerthread_init(&media_player_thread, rtpe_config.media_num_threads, media_player_run); #endif - timerthread_init(&send_timer_thread, timerthread_queue_run); + timerthread_init(&send_timer_thread, rtpe_config.media_num_threads, timerthread_queue_run); } void media_player_free(void) { @@ -1319,12 +1319,12 @@ void media_player_free(void) { } +void media_player_launch(void) { #ifdef WITH_TRANSCODING -void media_player_loop(void *p) { - timerthread_run(&media_player_thread); -} + timerthread_launch(&media_player_thread, rtpe_config.scheduling, rtpe_config.priority, "media player"); #endif -void send_timer_loop(void *p) { +} +void send_timer_launch(void) { //ilog(LOG_DEBUG, "send_timer_loop"); - timerthread_run(&send_timer_thread); + timerthread_launch(&send_timer_thread, rtpe_config.scheduling, rtpe_config.priority, "media player"); } diff --git a/daemon/timerthread.c b/daemon/timerthread.c index aedae255f..efe175caf 100644 --- a/daemon/timerthread.c +++ b/daemon/timerthread.c @@ -9,11 +9,12 @@ static int tt_obj_cmp(const void *a, const void *b) { return timeval_cmp_ptr(&A->next_check, &B->next_check); } -void timerthread_init(struct timerthread *tt, void (*func)(void *)) { +void timerthread_init(struct timerthread *tt, unsigned int num, void (*func)(void *)) { tt->tree = g_tree_new(tt_obj_cmp); mutex_init(&tt->lock); cond_init(&tt->cond); tt->func = func; + tt->num_threads = num; } static int __tt_put_all(void *k, void *d, void *p) { @@ -29,7 +30,7 @@ void timerthread_free(struct timerthread *tt) { mutex_destroy(&tt->lock); } -void timerthread_run(void *p) { +static void timerthread_run(void *p) { struct timerthread *tt = p; struct thread_waker waker = { .lock = &tt->lock, .cond = &tt->cond }; @@ -80,6 +81,11 @@ sleep:; thread_waker_del(&waker); } +void timerthread_launch(struct timerthread *tt, const char *scheduler, int prio, const char *name) { + for (unsigned int i = 0; i < tt->num_threads; i++) + thread_create_detach_prio(timerthread_run, tt, scheduler, prio, name); +} + void timerthread_obj_schedule_abs_nl(struct timerthread_obj *tt_obj, const struct timeval *tv) { if (!tt_obj) return; diff --git a/include/codec.h b/include/codec.h index 48f75aac0..6c285192a 100644 --- a/include/codec.h +++ b/include/codec.h @@ -96,7 +96,7 @@ typedef union { void codecs_init(void); void codecs_cleanup(void); -void codec_timers_loop(void *); +void codec_timers_launch(void); void rtcp_timer_stop(struct rtcp_timer **); void codec_timer_callback(call_t *, void (*)(call_t *, codec_timer_callback_arg_t), codec_timer_callback_arg_t, uint64_t delay); diff --git a/include/ice.h b/include/ice.h index d9ff31a47..638391090 100644 --- a/include/ice.h +++ b/include/ice.h @@ -159,7 +159,7 @@ void ice_restart(struct ice_agent *); void ice_candidates_free(candidate_q *); void ice_remote_candidates(candidate_q *, struct ice_agent *); -void ice_thread_run(void *); +void ice_thread_launch(void); int ice_request(stream_fd *, const endpoint_t *, struct stun_attrs *); int ice_response(stream_fd *, const endpoint_t *src, diff --git a/include/jitter_buffer.h b/include/jitter_buffer.h index addb8e290..2ff243739 100644 --- a/include/jitter_buffer.h +++ b/include/jitter_buffer.h @@ -48,7 +48,7 @@ void jitter_buffer_free(struct jitter_buffer **); int buffer_packet(struct media_packet *mp, const str *s); void jb_packet_free(struct jb_packet **jbp); -void jitter_buffer_loop(void *p); +void jitter_buffer_launch(void); INLINE void jb_put(struct jitter_buffer **jb) { if (!*jb) diff --git a/include/media_player.h b/include/media_player.h index f9951727e..c47382129 100644 --- a/include/media_player.h +++ b/include/media_player.h @@ -121,12 +121,11 @@ void media_player_add_packet(struct media_player *mp, char *buf, size_t len, void media_player_init(void); void media_player_free(void); -void media_player_loop(void *); +void media_player_launch(void); struct send_timer *send_timer_new(struct packet_stream *); void send_timer_push(struct send_timer *, struct codec_packet *); - -void send_timer_loop(void *p); +void send_timer_launch(void); diff --git a/include/timerthread.h b/include/timerthread.h index 742acebf2..a849d7237 100644 --- a/include/timerthread.h +++ b/include/timerthread.h @@ -8,6 +8,7 @@ #include "obj.h" struct timerthread { + unsigned int num_threads; GTree *tree; mutex_t lock; cond_t cond; @@ -41,9 +42,9 @@ struct timerthread_queue_entry { }; -void timerthread_init(struct timerthread *, void (*)(void *)); +void timerthread_init(struct timerthread *, unsigned int, void (*)(void *)); void timerthread_free(struct timerthread *); -void timerthread_run(void *); +void timerthread_launch(struct timerthread *, const char *scheduler, int prio, const char *name); void timerthread_obj_schedule_abs_nl(struct timerthread_obj *, const struct timeval *); void timerthread_obj_deschedule(struct timerthread_obj *);