]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add mutex to codecs
authorAnthony Minessale <anthony.minessale@gmail.com>
Wed, 12 Nov 2008 15:34:34 +0000 (15:34 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Wed, 12 Nov 2008 15:34:34 +0000 (15:34 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@10360 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_module_interfaces.h
src/switch_core_codec.c

index f1c5142f644170ec256ad028eb5806685bd8f16d..36f16bb4caaad5919c5f1a60ab47a57e2c7afad3 100644 (file)
@@ -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 */
index 587a1a1b74fcd48e5e7e840b0c1d196405eb66ae..b87a6503f5f52062321d494352249a6fa94c533a 100644 (file)
@@ -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;
 }