From 984573a3ca8f60bf6b90eb5fbb27a677191a1dbc Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 8 Apr 2009 18:43:10 +0000 Subject: [PATCH] FSCORE-349 git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12953 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_frame.h | 1 + src/mod/endpoints/mod_loopback/mod_loopback.c | 60 +++++------- src/switch_core_io.c | 91 ++++++++++++------- 3 files changed, 84 insertions(+), 68 deletions(-) diff --git a/src/include/switch_frame.h b/src/include/switch_frame.h index 19701374d8..f78ddc08ca 100644 --- a/src/include/switch_frame.h +++ b/src/include/switch_frame.h @@ -68,6 +68,7 @@ SWITCH_BEGIN_EXTERN_C switch_bool_t m; /*! frame flags */ switch_frame_flag_t flags; + switch_core_session_t *session; }; SWITCH_END_EXTERN_C diff --git a/src/mod/endpoints/mod_loopback/mod_loopback.c b/src/mod/endpoints/mod_loopback/mod_loopback.c index f1b525394e..46d64872cd 100644 --- a/src/mod/endpoints/mod_loopback/mod_loopback.c +++ b/src/mod/endpoints/mod_loopback/mod_loopback.c @@ -115,12 +115,10 @@ static switch_status_t tech_init(private_t *tech_pvt, switch_core_session_t *ses if (tech_pvt->read_codec.implementation) { switch_core_codec_destroy(&tech_pvt->read_codec); - memset(&tech_pvt->read_codec, 0, sizeof(tech_pvt->read_codec)); } if (tech_pvt->write_codec.implementation) { switch_core_codec_destroy(&tech_pvt->write_codec); - memset(&tech_pvt->write_codec, 0, sizeof(tech_pvt->write_codec)); } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s setup codec %s/%d/%d\n", switch_channel_get_name(channel), iananame, rate, interval); @@ -161,7 +159,9 @@ static switch_status_t tech_init(private_t *tech_pvt, switch_core_session_t *ses tech_pvt->write_frame.data = tech_pvt->write_databuf; tech_pvt->write_frame.buflen = sizeof(tech_pvt->write_databuf); + tech_pvt->write_frame.codec = &tech_pvt->write_codec; + tech_pvt->cng_frame.data = tech_pvt->cng_databuf; tech_pvt->cng_frame.buflen = sizeof(tech_pvt->cng_databuf); switch_set_flag((&tech_pvt->cng_frame), SFF_CNG); @@ -362,7 +362,6 @@ static switch_status_t channel_on_hangup(switch_core_session_t *session) switch_clear_flag_locked(tech_pvt->other_tech_pvt, TFLAG_LINKED); tech_pvt->other_tech_pvt = NULL; } - switch_mutex_unlock(tech_pvt->mutex); if (tech_pvt->other_session) { switch_channel_hangup(tech_pvt->other_channel, switch_channel_get_cause(channel)); @@ -370,17 +369,16 @@ static switch_status_t channel_on_hangup(switch_core_session_t *session) tech_pvt->other_channel = NULL; tech_pvt->other_session = NULL; } + switch_mutex_unlock(tech_pvt->mutex); switch_core_timer_destroy(&tech_pvt->timer); if (tech_pvt->read_codec.implementation) { switch_core_codec_destroy(&tech_pvt->read_codec); - memset(&tech_pvt->read_codec, 0, sizeof(tech_pvt->read_codec)); } if (tech_pvt->write_codec.implementation) { switch_core_codec_destroy(&tech_pvt->write_codec); - memset(&tech_pvt->write_codec, 0, sizeof(tech_pvt->write_codec)); } return SWITCH_STATUS_SUCCESS; @@ -518,33 +516,21 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch mutex = tech_pvt->mutex; switch_mutex_lock(mutex); - while(switch_test_flag(tech_pvt, TFLAG_LINKED) && tech_pvt->other_tech_pvt) { - if (!switch_channel_ready(channel)) { - goto end; - } - if (switch_test_flag(tech_pvt, TFLAG_CNG)) { - break; - } - - if (tech_pvt->other_tech_pvt && switch_test_flag(tech_pvt->other_tech_pvt, TFLAG_WRITE)) { - break; - } - - if (switch_core_timer_check(&tech_pvt->timer, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) { - switch_set_flag(tech_pvt, TFLAG_CNG); - break; - } - - switch_cond_next(); + + if (!switch_channel_ready(channel)) { + goto end; } - if (switch_test_flag(tech_pvt, TFLAG_LINKED)) { - if (tech_pvt->other_tech_pvt && switch_test_flag(tech_pvt->other_tech_pvt, TFLAG_WRITE)) { - switch_core_timer_sync(&tech_pvt->timer); - *frame = &tech_pvt->other_tech_pvt->write_frame; - switch_clear_flag_locked(tech_pvt->other_tech_pvt, TFLAG_WRITE); - switch_clear_flag_locked(tech_pvt, TFLAG_CNG); - } + if (!switch_test_flag(tech_pvt, TFLAG_CNG) && !switch_test_flag(tech_pvt, TFLAG_WRITE)) { + switch_core_timer_next(&tech_pvt->timer); + } + + if (switch_test_flag(tech_pvt, TFLAG_WRITE)) { + *frame = &tech_pvt->write_frame; + switch_clear_flag_locked(tech_pvt, TFLAG_WRITE); + switch_clear_flag_locked(tech_pvt, TFLAG_CNG); + } else { + switch_set_flag(tech_pvt, TFLAG_CNG); } if (switch_test_flag(tech_pvt, TFLAG_CNG)) { @@ -555,7 +541,7 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch } - if (*frame && switch_test_flag(tech_pvt, TFLAG_LINKED)) { + if (*frame) { status = SWITCH_STATUS_SUCCESS; } else { status = SWITCH_STATUS_FALSE; @@ -624,12 +610,12 @@ static switch_status_t channel_write_frame(switch_core_session_t *session, switc tech_init(tech_pvt->other_tech_pvt, tech_pvt->other_session, frame->codec); } - memcpy(&tech_pvt->write_frame, frame, sizeof(*frame)); - tech_pvt->write_frame.data = tech_pvt->write_databuf; - tech_pvt->write_frame.buflen = sizeof(tech_pvt->write_databuf); - tech_pvt->write_frame.codec = &tech_pvt->write_codec; - memcpy(tech_pvt->write_frame.data, frame->data, frame->datalen); - switch_set_flag_locked(tech_pvt, TFLAG_WRITE); + memcpy(&tech_pvt->other_tech_pvt->write_frame, frame, sizeof(*frame)); + tech_pvt->other_tech_pvt->write_frame.data = tech_pvt->other_tech_pvt->write_databuf; + tech_pvt->other_tech_pvt->write_frame.buflen = sizeof(tech_pvt->other_tech_pvt->write_databuf); + //tech_pvt->other_tech_pvt->write_frame.codec = &tech_pvt->other_tech_pvt->write_codec; + memcpy(tech_pvt->other_tech_pvt->write_frame.data, frame->data, frame->datalen); + switch_set_flag_locked(tech_pvt->other_tech_pvt, TFLAG_WRITE); status = SWITCH_STATUS_SUCCESS; } diff --git a/src/switch_core_io.c b/src/switch_core_io.c index 398f8837b0..3c2c835671 100644 --- a/src/switch_core_io.c +++ b/src/switch_core_io.c @@ -206,11 +206,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi need_codec = TRUE; } - if (session->read_codec && !(*frame)->codec) { - need_codec = TRUE; - } - - if (!session->read_codec && (*frame)->codec) { + if (!(session->read_codec && (*frame)->codec && (*frame)->codec->implementation)) { status = SWITCH_STATUS_FALSE; goto done; } @@ -533,6 +529,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi *frame = &runtime.dummy_cng_frame; } + (*frame)->session = session; + switch_mutex_unlock(session->read_codec->mutex); switch_mutex_unlock(session->codec_read_mutex); @@ -610,9 +608,8 @@ 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 ((session->write_codec && frame->codec && session->write_codec->implementation != frame->codec->implementation)) { if (session->write_impl.codec_id == frame->codec->implementation->codec_id || session->write_impl.microseconds_per_packet != frame->codec->implementation->microseconds_per_packet) { @@ -661,8 +658,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess frame->datalen, session->write_impl.actual_samples_per_second, session->raw_write_frame.data, &session->raw_write_frame.datalen, &session->raw_write_frame.rate, &flag); - - + + if (do_resample && status == SWITCH_STATUS_SUCCESS) { @@ -682,6 +679,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess session->write_impl.decoded_bytes_per_packet, SWITCH_RESAMPLE_QUALITY); + switch_mutex_unlock(session->resample_mutex); if (status != SWITCH_STATUS_SUCCESS) { goto done; @@ -725,19 +723,31 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess } } + + if (session->write_resampler) { short *data = write_frame->data; switch_mutex_lock(session->resample_mutex); - switch_resample_process(session->write_resampler, data, write_frame->datalen / 2); - memcpy(data, session->write_resampler->to, session->write_resampler->to_len * 2); - write_frame->samples = session->write_resampler->to_len; - write_frame->datalen = write_frame->samples * 2; - write_frame->rate = session->write_resampler->to_rate; - did_write_resample = 1; + if (session->write_resampler) { + + switch_resample_process(session->write_resampler, data, write_frame->datalen / 2); + + memcpy(data, session->write_resampler->to, session->write_resampler->to_len * 2); + + write_frame->samples = session->write_resampler->to_len; + + write_frame->datalen = write_frame->samples * 2; + + write_frame->rate = session->write_resampler->to_rate; + + did_write_resample = 1; + } switch_mutex_unlock(session->resample_mutex); } + + if (session->bugs && !switch_channel_test_flag(session->channel, CF_PAUSE_BUGS)) { switch_media_bug_t *bp, *dp, *last = NULL; @@ -804,7 +814,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess write_frame->codec->implementation->decoded_bytes_per_packet == session->write_impl.decoded_bytes_per_packet) { perfect = TRUE; } + + if (perfect) { if (write_frame->datalen < session->write_impl.decoded_bytes_per_packet) { @@ -822,6 +834,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess session->write_impl.actual_samples_per_second, session->enc_write_frame.data, &session->enc_write_frame.datalen, &session->enc_write_frame.rate, &flag); + + + switch (status) { case SWITCH_STATUS_RESAMPLE: resample++; @@ -864,6 +879,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess if (flag & SFF_CNG) { switch_set_flag(write_frame, SFF_CNG); } + status = perform_write(session, write_frame, flags, stream_id); goto error; } else { @@ -924,6 +940,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess &session->enc_write_frame.rate, &flag); + + switch (status) { case SWITCH_STATUS_RESAMPLE: resample++; @@ -935,14 +953,17 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess write_frame = &session->enc_write_frame; if (!session->write_resampler) { switch_mutex_lock(session->resample_mutex); - status = switch_resample_create(&session->write_resampler, - frame->codec->implementation->actual_samples_per_second, - session->write_impl.actual_samples_per_second, - session->write_impl.decoded_bytes_per_packet, - SWITCH_RESAMPLE_QUALITY); - + if (!session->write_resampler) { + status = switch_resample_create(&session->write_resampler, + frame->codec->implementation->actual_samples_per_second, + session->write_impl.actual_samples_per_second, + session->write_impl.decoded_bytes_per_packet, + SWITCH_RESAMPLE_QUALITY); + } switch_mutex_unlock(session->resample_mutex); + + if (status != SWITCH_STATUS_SUCCESS) { goto done; } @@ -959,8 +980,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess case SWITCH_STATUS_NOOP: if (session->write_resampler) { switch_mutex_lock(session->resample_mutex); - switch_resample_destroy(&session->write_resampler); - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Deactivating write resampler\n"); + if (session->write_resampler) { + switch_resample_destroy(&session->write_resampler); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Deactivating write resampler\n"); + } switch_mutex_unlock(session->resample_mutex); } enc_frame->codec = session->write_codec; @@ -985,11 +1008,13 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess if (!did_write_resample && session->read_resampler) { short *data = write_frame->data; switch_mutex_lock(session->resample_mutex); - switch_resample_process(session->read_resampler, data, write_frame->datalen / 2); - memcpy(data, session->read_resampler->to, session->read_resampler->to_len * 2); - write_frame->samples = session->read_resampler->to_len; - write_frame->datalen = session->read_resampler->to_len * 2; - write_frame->rate = session->read_resampler->to_rate; + if (session->read_resampler) { + switch_resample_process(session->read_resampler, data, write_frame->datalen / 2); + memcpy(data, session->read_resampler->to, session->read_resampler->to_len * 2); + write_frame->samples = session->read_resampler->to_len; + write_frame->datalen = session->read_resampler->to_len * 2; + write_frame->rate = session->read_resampler->to_rate; + } switch_mutex_unlock(session->resample_mutex); } @@ -1002,9 +1027,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess write_frame->timestamp = 0; } - status = perform_write(session, write_frame, flags, stream_id); - - if (status != SWITCH_STATUS_SUCCESS) { + if ((status = perform_write(session, write_frame, flags, stream_id)) != SWITCH_STATUS_SUCCESS) { break; } } @@ -1021,12 +1044,18 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_write_frame(switch_core_sess } if (do_write) { + + + status = perform_write(session, write_frame, flags, stream_id); + + } error: switch_mutex_unlock(session->write_codec->mutex); + switch_mutex_unlock(frame->codec->mutex); switch_mutex_unlock(session->codec_write_mutex); return status;