Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use single thread for usrsctp stack #2383

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions conf/janus.jcfg.sample.in
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ general: {
# As such, if you want to use this you should
# provision the correct value according to the
# available resources (e.g., CPUs available).
#usrsctp_singlethread = true # You can also choose whether to use a single thread
# for the usrsctp stack, or use their internal
# threads instead (default=multiple threads).
#opaqueid_in_api = true # Opaque IDs set by applications are typically
# only passed to event handlers for correlation
# purposes, but not sent back to the user or
Expand Down Expand Up @@ -205,6 +208,7 @@ media: {
#slowlink_threshold = 4
#twcc_period = 100
#dtls_timeout = 500
#usrsctp_single_thread = true

# If you need DSCP packet marking and prioritization, you can configure
# the 'dscp' property to a specific values, and Janus will try to
Expand Down
4 changes: 3 additions & 1 deletion janus.c
Original file line number Diff line number Diff line change
Expand Up @@ -4846,7 +4846,9 @@ gint main(int argc, char *argv[])

#ifdef HAVE_SCTP
/* Initialize SCTP for DataChannels */
if(janus_sctp_init() < 0) {
item = janus_config_get(config, config_general, janus_config_type_item, "usrsctp_singlethread");
gboolean st = (item && item->value && janus_is_true(item->value));
if(janus_sctp_init(st) < 0) {
exit(1);
}
#else
Expand Down
60 changes: 56 additions & 4 deletions sctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,58 @@ static GHashTable *sctp_ids = NULL;
static void janus_sctp_association_unref(janus_sctp_association *sctp);

/* SCTP management code */
static gboolean sctp_running;
int janus_sctp_init(void) {
/* Initialize the SCTP stack */
usrsctp_init(0, janus_sctp_data_to_dtls, NULL);
static gboolean sctp_running = FALSE;
static gint64 sctp_last_called = 0;
static GMainContext *sctp_mainctx;
static GMainLoop *sctp_mainloop = NULL;
static GThread *sctp_thread = NULL;
static GSource *sctp_timer = NULL;
static gpointer janus_sctp_thread(gpointer user_data) {
JANUS_LOG(LOG_VERB, "SCTP timer thread started\n");
g_main_loop_run(sctp_mainloop);
/* When the loop quits, we can unref it */
g_main_loop_unref(sctp_mainloop);
g_main_context_unref(sctp_mainctx);
JANUS_LOG(LOG_VERB, "Leaving SCTP timer thread\n");
return NULL;
}
static gboolean janus_sctp_timer(gpointer user_data) {
gint64 now = janus_get_monotonic_time();
guint32 elapsed = sctp_last_called ? (now-sctp_last_called) : 0;
sctp_last_called = now;
usrsctp_handle_timers(elapsed);
return TRUE;
}
int janus_sctp_init(gboolean single_thread) {
JANUS_LOG(LOG_INFO, "Using %s for the usrsctp stack\n",
single_thread ? "a single thread" : "multiple treads");
if(!single_thread) {
/* Initialize the SCTP stack */
usrsctp_init(0, janus_sctp_data_to_dtls, NULL);
} else {
/* Create a loop that we'll use for SCTP timers */
sctp_mainctx = g_main_context_new();
sctp_mainloop = g_main_loop_new(sctp_mainctx, FALSE);
GError *error = NULL;
char tname[16];
g_snprintf(tname, sizeof(tname), "sctp timers");
sctp_thread = g_thread_try_new(tname, &janus_sctp_thread, NULL, &error);
if(error != NULL) {
g_main_loop_unref(sctp_mainloop);
g_main_context_unref(sctp_mainctx);
JANUS_LOG(LOG_FATAL, "Got error %d (%s) trying to launch the SCTP timer thread...\n",
error->code, error->message ? error->message : "??");
g_error_free(error);
return -1;
}
/* Initialize the SCTP stack */
usrsctp_init_nothreads(0, janus_sctp_data_to_dtls, NULL);
/* Add a timer source (every 10ms) */
sctp_timer = g_timeout_source_new(10);
g_source_set_callback(sctp_timer, janus_sctp_timer, sctp_mainctx, NULL);
g_source_attach(sctp_timer, sctp_mainctx);
g_source_unref(sctp_timer);
}
sctp_running = TRUE;

#ifdef DEBUG_SCTP
Expand All @@ -152,6 +200,10 @@ void janus_sctp_deinit(void) {
janus_mutex_lock(&sctp_mutex);
g_hash_table_unref(sctp_ids);
janus_mutex_unlock(&sctp_mutex);
if(sctp_mainloop != NULL && g_main_loop_is_running(sctp_mainloop))
g_main_loop_quit(sctp_mainloop);
if(sctp_thread != NULL)
g_thread_join(sctp_thread);
}

static void janus_sctp_association_unref(janus_sctp_association *sctp) {
Expand Down
2 changes: 1 addition & 1 deletion sctp.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

/*! \brief SCTP stuff initialization
* \returns 0 on success, a negative integer otherwise */
int janus_sctp_init(void);
int janus_sctp_init(gboolean single_thread);

/*! \brief SCTP stuff de-initialization */
void janus_sctp_deinit(void);
Expand Down