From 06e770bac3440406f05237ebbb5f2e7e7a8b70e7 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 15 Oct 2008 21:54:26 +0000 Subject: [PATCH] add mutexes to codec set/get funcs git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@10037 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/switch_core_codec.c | 115 ++++++++++++++++++++++++++++++++-------- src/switch_pcm.c | 6 +-- 2 files changed, 96 insertions(+), 25 deletions(-) diff --git a/src/switch_core_codec.c b/src/switch_core_codec.c index 6bcb1cf53e..b130c172b1 100644 --- a/src/switch_core_codec.c +++ b/src/switch_core_codec.c @@ -44,12 +44,16 @@ SWITCH_DECLARE(uint32_t) switch_core_codec_next_id(void) SWITCH_DECLARE(void) switch_core_session_unset_read_codec(switch_core_session_t *session) { + switch_mutex_lock(session->resample_mutex); session->real_read_codec = session->read_codec = NULL; + switch_mutex_unlock(session->resample_mutex); } SWITCH_DECLARE(void) switch_core_session_unset_write_codec(switch_core_session_t *session) { + switch_mutex_lock(session->resample_mutex); session->real_write_codec = session->write_codec = NULL; + switch_mutex_unlock(session->resample_mutex); } @@ -58,10 +62,14 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_read_codec(switch_core_s switch_event_t *event; switch_channel_t *channel = switch_core_session_get_channel(session); char tmp[30]; + switch_status_t status = SWITCH_STATUS_SUCCESS; + + switch_mutex_lock(session->resample_mutex); if (codec && !codec->implementation) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set UNINITIALIZED codec!\n"); - return SWITCH_STATUS_FALSE; + status = SWITCH_STATUS_FALSE; + goto end; } if (!codec || codec == session->real_read_codec) { @@ -72,16 +80,19 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_read_codec(switch_core_s } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "resetting to uninitilized codec, setting to NULL\n"); session->read_codec = session->real_read_codec = NULL; - return SWITCH_STATUS_FALSE; + status = SWITCH_STATUS_FALSE; + goto end; } } else { - return SWITCH_STATUS_FALSE; + status = SWITCH_STATUS_FALSE; + goto end; } } else if (codec) { if (session->read_codec != session->real_read_codec) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot double-set codec!\n"); - return SWITCH_STATUS_FALSE; + status = SWITCH_STATUS_FALSE; + goto end; } session->read_codec = codec; @@ -111,21 +122,31 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_read_codec(switch_core_s session->raw_write_frame.codec = session->read_codec; session->enc_read_frame.codec = session->read_codec; session->enc_write_frame.codec = session->read_codec; - return SWITCH_STATUS_SUCCESS; } - return SWITCH_STATUS_FALSE; + end: + switch_mutex_unlock(session->resample_mutex); + + return status; } SWITCH_DECLARE(switch_codec_t *) switch_core_session_get_effective_read_codec(switch_core_session_t *session) { - return session->read_codec; + switch_codec_t *codec; + switch_mutex_lock(session->resample_mutex); + codec = session->read_codec; + switch_mutex_unlock(session->resample_mutex); + return codec; } SWITCH_DECLARE(switch_codec_t *) switch_core_session_get_read_codec(switch_core_session_t *session) { - return session->real_read_codec ? session->real_read_codec : session->read_codec; + switch_codec_t *codec; + switch_mutex_lock(session->resample_mutex); + codec = session->real_read_codec ? session->real_read_codec : session->read_codec; + switch_mutex_unlock(session->resample_mutex); + return codec; } SWITCH_DECLARE(switch_status_t) switch_core_session_set_write_codec(switch_core_session_t *session, switch_codec_t *codec) @@ -133,6 +154,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_write_codec(switch_core_ switch_event_t *event; switch_channel_t *channel = switch_core_session_get_channel(session); char tmp[30]; + switch_status_t status = SWITCH_STATUS_SUCCESS; + + switch_mutex_lock(session->resample_mutex); if (!codec || !codec->implementation) { if (session->real_write_codec) { @@ -140,7 +164,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_write_codec(switch_core_ session->real_write_codec = NULL; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set NULL codec!\n"); - return SWITCH_STATUS_FALSE; + status = SWITCH_STATUS_FALSE; + goto end; } } else if (session->write_codec) { if (session->real_write_codec) { @@ -149,7 +174,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_write_codec(switch_core_ session->real_write_codec = NULL; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot double-set codec!\n"); - return SWITCH_STATUS_FALSE; + status = SWITCH_STATUS_FALSE; + goto end; } } else { session->real_write_codec = session->write_codec; @@ -174,18 +200,33 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_write_codec(switch_core_ switch_snprintf(tmp, sizeof(tmp), "%d", session->write_codec->implementation->actual_samples_per_second); switch_channel_set_variable(channel, "write_rate", tmp); - return SWITCH_STATUS_SUCCESS; + end: + + switch_mutex_unlock(session->resample_mutex); + return status; } SWITCH_DECLARE(switch_codec_t *) switch_core_session_get_effective_write_codec(switch_core_session_t *session) { - return session->write_codec; + switch_codec_t *codec; + + switch_mutex_lock(session->resample_mutex); + codec = session->write_codec; + switch_mutex_unlock(session->resample_mutex); + + return codec; } SWITCH_DECLARE(switch_codec_t *) switch_core_session_get_write_codec(switch_core_session_t *session) { - return session->real_write_codec ? session->real_write_codec : session->write_codec; + switch_codec_t *codec; + + switch_mutex_lock(session->resample_mutex); + codec = session->real_write_codec ? session->real_write_codec : session->write_codec; + switch_mutex_unlock(session->resample_mutex); + + return codec; } @@ -195,14 +236,19 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_video_read_codec(switch_ switch_event_t *event; switch_channel_t *channel = switch_core_session_get_channel(session); char tmp[30]; + switch_status_t status = SWITCH_STATUS_SUCCESS; + + switch_mutex_lock(session->resample_mutex); if (!codec || !codec->implementation) { if (session->video_read_codec) { session->video_read_codec = NULL; - return SWITCH_STATUS_SUCCESS; + status = SWITCH_STATUS_SUCCESS; + goto end; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set NULL codec!\n"); - return SWITCH_STATUS_FALSE; + status = SWITCH_STATUS_FALSE; + goto end; } if (switch_event_create(&event, SWITCH_EVENT_CODEC) == SWITCH_STATUS_SUCCESS) { @@ -218,12 +264,22 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_video_read_codec(switch_ session->video_read_codec = codec; - return SWITCH_STATUS_SUCCESS; + end: + + switch_mutex_unlock(session->resample_mutex); + return status; } SWITCH_DECLARE(switch_codec_t *) switch_core_session_get_video_read_codec(switch_core_session_t *session) { - return session->video_read_codec; + switch_codec_t *codec; + + switch_mutex_lock(session->resample_mutex); + codec = session->video_read_codec; + switch_mutex_unlock(session->resample_mutex); + + return codec; + } SWITCH_DECLARE(switch_status_t) switch_core_session_set_video_write_codec(switch_core_session_t *session, switch_codec_t *codec) @@ -231,14 +287,18 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_video_write_codec(switch switch_event_t *event; switch_channel_t *channel = switch_core_session_get_channel(session); char tmp[30]; - + switch_status_t status = SWITCH_STATUS_SUCCESS; + + switch_mutex_lock(session->resample_mutex); if (!codec || !codec->implementation) { if (session->video_write_codec) { session->video_write_codec = NULL; - return SWITCH_STATUS_SUCCESS; + status = SWITCH_STATUS_SUCCESS; + goto end; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set NULL codec!\n"); - return SWITCH_STATUS_FALSE; + status = SWITCH_STATUS_FALSE; + goto end; } if (switch_event_create(&event, SWITCH_EVENT_CODEC) == SWITCH_STATUS_SUCCESS) { @@ -253,12 +313,23 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_set_video_write_codec(switch switch_channel_set_variable(channel, "video_write_rate", tmp); session->video_write_codec = codec; - return SWITCH_STATUS_SUCCESS; + + end: + + switch_mutex_unlock(session->resample_mutex); + return status; } SWITCH_DECLARE(switch_codec_t *) switch_core_session_get_video_write_codec(switch_core_session_t *session) { - return session->video_write_codec; + switch_codec_t *codec; + + switch_mutex_lock(session->resample_mutex); + codec = session->video_write_codec; + switch_mutex_unlock(session->resample_mutex); + + return codec; + } diff --git a/src/switch_pcm.c b/src/switch_pcm.c index 6349d8dcd9..c6e24fefaf 100644 --- a/src/switch_pcm.c +++ b/src/switch_pcm.c @@ -275,7 +275,7 @@ static void mod_g711_load(switch_loadable_module_interface_t **module_interface, for (count = 12; count > 0; count--) { switch_core_codec_add_implementation(pool, codec_interface, SWITCH_CODEC_TYPE_AUDIO, 0, "PCMU", NULL, 8000, 8000, 64000, - mpf * count, spf * count, bpf * count, ebpf * count, 1, 1, 1, + mpf * count, spf * count, bpf * count, ebpf * count, 1, spf * count, spf * count, switch_g711u_init, switch_g711u_encode, switch_g711u_decode, switch_g711u_destroy); } @@ -283,7 +283,7 @@ static void mod_g711_load(switch_loadable_module_interface_t **module_interface, for (count = 12; count > 0; count--) { switch_core_codec_add_implementation(pool, codec_interface, SWITCH_CODEC_TYPE_AUDIO, 8, "PCMA", NULL, 8000, 8000, 64000, - mpf * count, spf * count, bpf * count, ebpf * count, 1, 1, 1, + mpf * count, spf * count, bpf * count, ebpf * count, 1, spf * count, spf * count, switch_g711a_init, switch_g711a_encode, switch_g711a_decode, switch_g711a_destroy); } @@ -316,7 +316,7 @@ SWITCH_MODULE_LOAD_FUNCTION(core_pcm_load) for (countb = 12; countb > 0; countb--) { switch_core_codec_add_implementation(pool, codec_interface, SWITCH_CODEC_TYPE_AUDIO, ianacode[counta], "L16", NULL, rate, rate, bps, - mpf * countb, spf * countb, bpf * countb, ebpf * countb, 1, 1, 1, + mpf * countb, spf * countb, bpf * countb, ebpf * countb, 1, spf * countb, spf * countb, switch_raw_init, switch_raw_encode, switch_raw_decode, switch_raw_destroy); } rate = rate * 2;