refactor
This commit is contained in:
parent
d239b31dfc
commit
c321e67ab5
|
@ -456,7 +456,7 @@ static inline void switch_core_codec_add_implementation(switch_memory_pool_t *po
|
|||
|
||||
static inline switch_bool_t switch_core_codec_ready(switch_codec_t *codec)
|
||||
{
|
||||
return (codec && codec->implementation && (codec->flags & SWITCH_CODEC_FLAG_READY)) ? SWITCH_TRUE : SWITCH_FALSE;
|
||||
return (codec && (codec->flags & SWITCH_CODEC_FLAG_READY) && codec->mutex && codec->codec_interface && codec->implementation) ? SWITCH_TRUE : SWITCH_FALSE;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -47,17 +47,14 @@ SWITCH_DECLARE(void) switch_core_session_unset_read_codec(switch_core_session_t
|
|||
switch_mutex_t *mutex = NULL;
|
||||
|
||||
switch_mutex_lock(session->codec_read_mutex);
|
||||
if (session->read_codec)
|
||||
mutex = session->read_codec->mutex;
|
||||
if (mutex)
|
||||
switch_mutex_lock(mutex);
|
||||
if (session->read_codec) mutex = session->read_codec->mutex;
|
||||
if (mutex) switch_mutex_lock(mutex);
|
||||
session->real_read_codec = session->read_codec = NULL;
|
||||
session->raw_read_frame.codec = session->read_codec;
|
||||
session->raw_write_frame.codec = session->read_codec;
|
||||
session->enc_read_frame.codec = session->read_codec;
|
||||
session->enc_write_frame.codec = session->read_codec;
|
||||
if (mutex)
|
||||
switch_mutex_unlock(mutex);
|
||||
if (mutex) switch_mutex_unlock(mutex);
|
||||
switch_mutex_unlock(session->codec_read_mutex);
|
||||
}
|
||||
|
||||
|
@ -86,13 +83,10 @@ SWITCH_DECLARE(void) switch_core_session_unset_write_codec(switch_core_session_t
|
|||
switch_mutex_t *mutex = NULL;
|
||||
|
||||
switch_mutex_lock(session->codec_write_mutex);
|
||||
if (session->write_codec)
|
||||
mutex = session->write_codec->mutex;
|
||||
if (mutex)
|
||||
switch_mutex_lock(mutex);
|
||||
if (session->write_codec) mutex = session->write_codec->mutex;
|
||||
if (mutex) switch_mutex_lock(mutex);
|
||||
session->real_write_codec = session->write_codec = NULL;
|
||||
if (mutex)
|
||||
switch_mutex_unlock(mutex);
|
||||
if (mutex) switch_mutex_unlock(mutex);
|
||||
switch_mutex_unlock(session->codec_write_mutex);
|
||||
}
|
||||
|
||||
|
@ -495,6 +489,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_copy(switch_codec_t *codec, sw
|
|||
|
||||
new_codec->implementation->init(new_codec, new_codec->flags, NULL);
|
||||
|
||||
switch_mutex_init(&new_codec->mutex, SWITCH_MUTEX_NESTED, new_codec->memory_pool);
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -598,12 +594,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_encode(switch_codec_t *codec,
|
|||
return SWITCH_STATUS_NOT_INITALIZED;
|
||||
}
|
||||
|
||||
if (codec->mutex)
|
||||
switch_mutex_lock(codec->mutex);
|
||||
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);
|
||||
if (codec->mutex) switch_mutex_unlock(codec->mutex);
|
||||
|
||||
return status;
|
||||
|
||||
|
@ -624,6 +618,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_decode(switch_codec_t *codec,
|
|||
|
||||
if (!codec->implementation || !switch_core_codec_ready(codec)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec is not initialized!\n");
|
||||
abort();
|
||||
return SWITCH_STATUS_NOT_INITALIZED;
|
||||
}
|
||||
|
||||
|
@ -641,12 +636,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_decode(switch_codec_t *codec,
|
|||
}
|
||||
}
|
||||
|
||||
if (codec->mutex)
|
||||
switch_mutex_lock(codec->mutex);
|
||||
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);
|
||||
if (codec->mutex) switch_mutex_unlock(codec->mutex);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
@ -659,7 +652,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_destroy(switch_codec_t *codec)
|
|||
|
||||
switch_assert(codec != NULL);
|
||||
|
||||
if (!codec->implementation || !switch_core_codec_ready(codec)) {
|
||||
if (!switch_core_codec_ready(codec)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Codec is not initialized!\n");
|
||||
return SWITCH_STATUS_NOT_INITALIZED;
|
||||
}
|
||||
|
@ -671,21 +664,27 @@ SWITCH_DECLARE(switch_status_t) switch_core_codec_destroy(switch_codec_t *codec)
|
|||
pool = codec->memory_pool;
|
||||
mutex = codec->mutex;
|
||||
|
||||
if (mutex)
|
||||
if (mutex) {
|
||||
switch_mutex_lock(mutex);
|
||||
switch_clear_flag(codec, SWITCH_CODEC_FLAG_READY);
|
||||
switch_mutex_unlock(mutex);
|
||||
switch_mutex_lock(mutex);
|
||||
}
|
||||
|
||||
codec->implementation->destroy(codec);
|
||||
switch_clear_flag(codec, SWITCH_CODEC_FLAG_READY);
|
||||
|
||||
|
||||
UNPROTECT_INTERFACE(codec->codec_interface);
|
||||
|
||||
if (mutex)
|
||||
if (mutex) {
|
||||
switch_mutex_unlock(mutex);
|
||||
|
||||
}
|
||||
|
||||
if (free_pool) {
|
||||
switch_core_destroy_memory_pool(&pool);
|
||||
}
|
||||
|
||||
memset(codec, 0, sizeof(*codec));
|
||||
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
|
|
@ -127,7 +127,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi
|
|||
|
||||
top:
|
||||
|
||||
if (switch_channel_down(session->channel)) {
|
||||
if (switch_channel_down(session->channel) || !switch_core_codec_ready(session->read_codec)) {
|
||||
*frame = NULL;
|
||||
status = SWITCH_STATUS_FALSE;
|
||||
goto even_more_done;
|
||||
|
@ -170,13 +170,19 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi
|
|||
}
|
||||
}
|
||||
|
||||
if (!SWITCH_READ_ACCEPTABLE(status) || !session->read_codec || !session->read_codec->mutex) {
|
||||
if (!SWITCH_READ_ACCEPTABLE(status) || !session->read_codec || !switch_core_codec_ready(session->read_codec)) {
|
||||
*frame = NULL;
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
switch_mutex_lock(session->codec_read_mutex);
|
||||
switch_mutex_lock(session->read_codec->mutex);
|
||||
if (!switch_core_codec_ready(session->read_codec)) {
|
||||
*frame = NULL;
|
||||
status = SWITCH_STATUS_FALSE;
|
||||
goto even_more_done;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (status != SWITCH_STATUS_SUCCESS) {
|
||||
|
@ -273,10 +279,12 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi
|
|||
} else {
|
||||
switch_codec_t *use_codec = read_frame->codec;
|
||||
if (do_bugs) {
|
||||
switch_thread_rwlock_wrlock(session->bug_rwlock);
|
||||
if (!switch_core_codec_ready(&session->bug_codec)) {
|
||||
switch_core_codec_copy(read_frame->codec, &session->bug_codec, NULL);
|
||||
}
|
||||
use_codec = &session->bug_codec;
|
||||
switch_thread_rwlock_unlock(session->bug_rwlock);
|
||||
}
|
||||
|
||||
status = switch_core_codec_decode(use_codec,
|
||||
|
@ -630,7 +638,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess
|
|||
|
||||
switch_mutex_lock(session->codec_write_mutex);
|
||||
|
||||
if (!(session->write_codec && session->write_codec->mutex && frame->codec) ||
|
||||
if (!(switch_core_codec_ready(session->write_codec) && frame->codec) ||
|
||||
!switch_channel_ready(session->channel) || !switch_channel_media_ready(session->channel)) {
|
||||
switch_mutex_unlock(session->codec_write_mutex);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
|
@ -639,7 +647,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess
|
|||
switch_mutex_lock(session->write_codec->mutex);
|
||||
switch_mutex_lock(frame->codec->mutex);
|
||||
|
||||
if (!frame->codec->implementation || !session->write_codec->implementation) goto error;
|
||||
if (!(switch_core_codec_ready(session->write_codec) && switch_core_codec_ready(frame->codec))) goto error;
|
||||
|
||||
if ((session->write_codec && frame->codec && session->write_codec->implementation != frame->codec->implementation)) {
|
||||
if (session->write_impl.codec_id == frame->codec->implementation->codec_id ||
|
||||
|
|
|
@ -47,11 +47,6 @@ static void switch_core_media_bug_destroy(switch_media_bug_t *bug)
|
|||
switch_buffer_destroy(&bug->raw_write_buffer);
|
||||
}
|
||||
|
||||
if (switch_core_codec_ready(&bug->session->bug_codec)) {
|
||||
switch_core_codec_destroy(&bug->session->bug_codec);
|
||||
memset(&bug->session->bug_codec, 0, sizeof(bug->session->bug_codec));
|
||||
}
|
||||
|
||||
if (switch_event_create(&event, SWITCH_EVENT_MEDIA_BUG_STOP) == SWITCH_STATUS_SUCCESS) {
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Media-Bug-Function", "%s", bug->function);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Media-Bug-Target", "%s", bug->target);
|
||||
|
@ -424,7 +419,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove_all(switch_core_ses
|
|||
|
||||
if (switch_core_codec_ready(&session->bug_codec)) {
|
||||
switch_core_codec_destroy(&session->bug_codec);
|
||||
memset(&session->bug_codec, 0, sizeof(session->bug_codec));
|
||||
}
|
||||
|
||||
return status;
|
||||
|
@ -458,8 +452,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove(switch_core_session
|
|||
switch_media_bug_t *bp = NULL, *last = NULL;
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
|
||||
switch_thread_rwlock_wrlock(session->bug_rwlock);
|
||||
if (session->bugs) {
|
||||
switch_thread_rwlock_wrlock(session->bug_rwlock);
|
||||
for (bp = session->bugs; bp; bp = bp->next) {
|
||||
if ((!bp->thread_id || bp->thread_id == switch_thread_self()) && bp->ready && bp == *bug) {
|
||||
if (last) {
|
||||
|
@ -472,17 +466,18 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove(switch_core_session
|
|||
|
||||
last = bp;
|
||||
}
|
||||
switch_thread_rwlock_unlock(session->bug_rwlock);
|
||||
if (bp) {
|
||||
status = switch_core_media_bug_close(&bp);
|
||||
}
|
||||
}
|
||||
|
||||
if (!session->bugs && switch_core_codec_ready(&session->bug_codec)) {
|
||||
switch_core_codec_destroy(&session->bug_codec);
|
||||
memset(&session->bug_codec, 0, sizeof(session->bug_codec));
|
||||
}
|
||||
|
||||
switch_thread_rwlock_unlock(session->bug_rwlock);
|
||||
|
||||
if (bp) {
|
||||
status = switch_core_media_bug_close(&bp);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
@ -496,8 +491,8 @@ SWITCH_DECLARE(uint32_t) switch_core_media_bug_prune(switch_core_session_t *sess
|
|||
|
||||
top:
|
||||
|
||||
switch_thread_rwlock_wrlock(session->bug_rwlock);
|
||||
if (session->bugs) {
|
||||
switch_thread_rwlock_wrlock(session->bug_rwlock);
|
||||
for (bp = session->bugs; bp; bp = bp->next) {
|
||||
if (switch_core_media_bug_test_flag(bp, SMBF_PRUNE)) {
|
||||
if (last) {
|
||||
|
@ -510,19 +505,20 @@ SWITCH_DECLARE(uint32_t) switch_core_media_bug_prune(switch_core_session_t *sess
|
|||
|
||||
last = bp;
|
||||
}
|
||||
switch_thread_rwlock_unlock(session->bug_rwlock);
|
||||
if (bp) {
|
||||
status = switch_core_media_bug_close(&bp);
|
||||
ttl++;
|
||||
goto top;
|
||||
}
|
||||
}
|
||||
|
||||
if (!session->bugs && switch_core_codec_ready(&session->bug_codec)) {
|
||||
switch_core_codec_destroy(&session->bug_codec);
|
||||
memset(&session->bug_codec, 0, sizeof(session->bug_codec));
|
||||
}
|
||||
|
||||
switch_thread_rwlock_unlock(session->bug_rwlock);
|
||||
|
||||
if (bp) {
|
||||
status = switch_core_media_bug_close(&bp);
|
||||
ttl++;
|
||||
goto top;
|
||||
}
|
||||
|
||||
return ttl;
|
||||
}
|
||||
|
||||
|
@ -532,9 +528,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove_callback(switch_cor
|
|||
switch_media_bug_t *cur = NULL, *bp = NULL, *last = NULL;
|
||||
int total = 0;
|
||||
|
||||
switch_thread_rwlock_wrlock(session->bug_rwlock);
|
||||
if (session->bugs) {
|
||||
switch_thread_rwlock_wrlock(session->bug_rwlock);
|
||||
|
||||
bp = session->bugs;
|
||||
while (bp) {
|
||||
cur = bp;
|
||||
|
@ -553,14 +548,15 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_bug_remove_callback(switch_cor
|
|||
last = cur;
|
||||
}
|
||||
}
|
||||
switch_thread_rwlock_unlock(session->bug_rwlock);
|
||||
}
|
||||
|
||||
if (!session->bugs && switch_core_codec_ready(&session->bug_codec)) {
|
||||
switch_core_codec_destroy(&session->bug_codec);
|
||||
memset(&session->bug_codec, 0, sizeof(session->bug_codec));
|
||||
}
|
||||
|
||||
switch_thread_rwlock_unlock(session->bug_rwlock);
|
||||
|
||||
|
||||
return total ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue