diff --git a/src/include/switch_module_interfaces.h b/src/include/switch_module_interfaces.h index f1c5142f64..36f16bb4ca 100644 --- a/src/include/switch_module_interfaces.h +++ b/src/include/switch_module_interfaces.h @@ -544,6 +544,7 @@ struct switch_codec { /*! private data for the codec module to store handle specific info */ void *private_info; switch_payload_t agreed_pt; + switch_mutex_t *mutex; }; /*! \brief A table of settings and callbacks that define a paticular implementation of a codec */ diff --git a/src/switch_core_codec.c b/src/switch_core_codec.c index 587a1a1b74..b87a6503f5 100644 --- a/src/switch_core_codec.c +++ b/src/switch_core_codec.c @@ -450,7 +450,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_init(switch_codec_t *codec, co } implementation->init(codec, flags, codec_settings); - + switch_mutex_init(&codec->mutex, SWITCH_MUTEX_NESTED, codec->memory_pool); return SWITCH_STATUS_SUCCESS; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Codec %s Exists but not at the desired implementation. %dhz %dms\n", codec_name, rate, @@ -467,6 +467,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_encode(switch_codec_t *codec, uint32_t decoded_rate, void *encoded_data, uint32_t *encoded_data_len, uint32_t *encoded_rate, unsigned int *flag) { + switch_status_t status; + switch_assert(codec != NULL); switch_assert(encoded_data != NULL); switch_assert(decoded_data != NULL); @@ -481,8 +483,13 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_encode(switch_codec_t *codec, return SWITCH_STATUS_NOT_INITALIZED; } - return codec->implementation->encode(codec, other_codec, decoded_data, decoded_data_len, decoded_rate, encoded_data, encoded_data_len, encoded_rate, - flag); + if (codec->mutex) switch_mutex_lock(codec->mutex); + status = codec->implementation->encode(codec, other_codec, decoded_data, decoded_data_len, + decoded_rate, encoded_data, encoded_data_len, encoded_rate, flag); + if (codec->mutex) switch_mutex_unlock(codec->mutex); + + return status; + } SWITCH_DECLARE(switch_status_t) switch_core_codec_decode(switch_codec_t *codec, @@ -492,6 +499,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_decode(switch_codec_t *codec, uint32_t encoded_rate, void *decoded_data, uint32_t *decoded_data_len, uint32_t *decoded_rate, unsigned int *flag) { + switch_status_t status; + switch_assert(codec != NULL); switch_assert(encoded_data != NULL); switch_assert(decoded_data != NULL); @@ -506,27 +515,40 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_decode(switch_codec_t *codec, return SWITCH_STATUS_NOT_INITALIZED; } - return codec->implementation->decode(codec, other_codec, encoded_data, encoded_data_len, encoded_rate, decoded_data, decoded_data_len, decoded_rate, - flag); + if (codec->mutex) switch_mutex_lock(codec->mutex); + status = codec->implementation->decode(codec, other_codec, encoded_data, encoded_data_len, encoded_rate, + decoded_data, decoded_data_len, decoded_rate, flag); + if (codec->mutex) switch_mutex_unlock(codec->mutex); + + return status; } SWITCH_DECLARE(switch_status_t) switch_core_codec_destroy(switch_codec_t *codec) { - switch_assert(codec != NULL); + switch_mutex_t *mutex; + switch_memory_pool_t *pool; + switch_assert(codec != NULL); + if (!codec->implementation) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Codec is not initialized!\n"); return SWITCH_STATUS_NOT_INITALIZED; } + pool = codec->memory_pool; + mutex = codec->mutex; + + if (mutex) switch_mutex_lock(mutex); + codec->implementation->destroy(codec); + memset(codec, 0, sizeof(*codec)); + + if (mutex) switch_mutex_unlock(mutex); if (switch_test_flag(codec, SWITCH_CODEC_FLAG_FREE_POOL)) { - switch_core_destroy_memory_pool(&codec->memory_pool); + switch_core_destroy_memory_pool(&pool); } - memset(codec, 0, sizeof(*codec)); - return SWITCH_STATUS_SUCCESS; }