From c0891ce9db9305e042056df0f4e4c0babe048b8f Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 18 Apr 2006 16:20:47 +0000 Subject: [PATCH] update git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@1186 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- Makefile.am | 2 +- Makefile.in | 2 +- src/include/switch_rtp.h | 9 ++++++ src/mod/endpoints/mod_exosip/mod_exosip.c | 23 +++++++++------ src/mod/endpoints/mod_iax/mod_iax.c | 28 +++++++++++++----- src/switch_ivr.c | 8 ++--- src/switch_rtp.c | 36 +++++++++++++++++++++-- 7 files changed, 83 insertions(+), 25 deletions(-) diff --git a/Makefile.am b/Makefile.am index 6a1a9d7dbf..64091a3365 100644 --- a/Makefile.am +++ b/Makefile.am @@ -118,7 +118,7 @@ CLEANFILES = src/include/switch_version.h lib_LTLIBRARIES = libfreeswitch.la libfreeswitch_la_CFLAGS = $(AM_CFLAGS) -Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -g libfreeswitch_la_LDFLAGS = -version-info 1:0:0 -libfreeswitch_la_LIBADD = -lteletone -lresample -lsrtp -lsqlite3 -ljitterbuffer +libfreeswitch_la_LIBADD = -lteletone -lresample -lsrtp -lsqlite3 nodist_libfreeswitch_la_SOURCES = src/include/switch_version.h bin_PROGRAMS = freeswitch diff --git a/Makefile.in b/Makefile.in index 6a6224e4fb..db2ea75b22 100644 --- a/Makefile.in +++ b/Makefile.in @@ -333,7 +333,7 @@ CLEANFILES = src/include/switch_version.h lib_LTLIBRARIES = libfreeswitch.la libfreeswitch_la_CFLAGS = $(AM_CFLAGS) -Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -g libfreeswitch_la_LDFLAGS = -version-info 1:0:0 -libfreeswitch_la_LIBADD = -lteletone -lresample -lsrtp -lsqlite3 -ljitterbuffer +libfreeswitch_la_LIBADD = -lteletone -lresample -lsrtp -lsqlite3 nodist_libfreeswitch_la_SOURCES = src/include/switch_version.h freeswitch_SOURCES = src/switch.c\ src/include/switch_version.h diff --git a/src/include/switch_rtp.h b/src/include/switch_rtp.h index e18a935dcd..129b59670c 100644 --- a/src/include/switch_rtp.h +++ b/src/include/switch_rtp.h @@ -263,6 +263,15 @@ SWITCH_DECLARE(switch_status) switch_rtp_zerocopy_read_frame(switch_rtp *rtp_ses */ SWITCH_DECLARE(int) switch_rtp_write(switch_rtp *rtp_session, void *data, uint32_t datalen, uint32_t ts, switch_frame_flag *flags); +/*! + \brief Write data to a given RTP session + \param rtp_session the RTP session to write to + \param frame the frame to write + \param ts then number of bytes to increment the timestamp by + \return the number of bytes written +*/ +SWITCH_DECLARE(int) switch_rtp_write_frame(switch_rtp *rtp_session, switch_frame *frame, uint32_t ts); + /*! \brief Write data with a specified payload and sequence number to a given RTP session \param rtp_session the RTP session to write to diff --git a/src/mod/endpoints/mod_exosip/mod_exosip.c b/src/mod/endpoints/mod_exosip/mod_exosip.c index 709731b127..a420722b17 100644 --- a/src/mod/endpoints/mod_exosip/mod_exosip.c +++ b/src/mod/endpoints/mod_exosip/mod_exosip.c @@ -503,7 +503,7 @@ static switch_status activate_rtp(struct private_object *tech_pvt) tech_pvt->read_codec.codec_interface->ianacode, tech_pvt->read_codec.implementation->encoded_bytes_per_frame, ms, - 0, + SWITCH_RTP_FLAG_NOBLOCK | SWITCH_RTP_FLAG_RAW_WRITE, key, &err, switch_core_session_get_pool(tech_pvt->session)); @@ -589,8 +589,9 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram return SWITCH_STATUS_TIMEOUT; } } - + if (switch_test_flag(tech_pvt, TFLAG_IO)) { + switch_status status; if (!switch_test_flag(tech_pvt, TFLAG_RTP)) { return SWITCH_STATUS_GENERR; @@ -600,16 +601,18 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram tech_pvt->read_frame.datalen = 0; - while (!switch_test_flag(tech_pvt, TFLAG_BYE) && switch_test_flag(tech_pvt, TFLAG_IO) - && tech_pvt->read_frame.datalen == 0) { + while (!switch_test_flag(tech_pvt, TFLAG_BYE) && switch_test_flag(tech_pvt, TFLAG_IO) && tech_pvt->read_frame.datalen == 0) { now = switch_time_now(); tech_pvt->read_frame.flags = 0; - if (switch_rtp_zerocopy_read_frame(tech_pvt->rtp_session, &tech_pvt->read_frame) != SWITCH_STATUS_SUCCESS) { + + status = switch_rtp_zerocopy_read_frame(tech_pvt->rtp_session, &tech_pvt->read_frame); + if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) { return SWITCH_STATUS_FALSE; } - + payload = tech_pvt->read_frame.payload; + if (timeout > -1) { elapsed = (unsigned int)((switch_time_now() - started) / 1000); if (elapsed >= (unsigned int)timeout) { @@ -732,6 +735,7 @@ static switch_status exosip_write_frame(switch_core_session *session, switch_fra for (x = 0; x < loops; x++) { + frame->flags = 0; switch_rtp_write_payload(tech_pvt->rtp_session, tech_pvt->out_digit_packet, 4, 101, ts, tech_pvt->out_digit_seq, &frame->flags); printf("Send %s packet for [%c] ts=%d sofar=%u dur=%d\n", loops == 1 ? "middle" : "end", @@ -759,6 +763,7 @@ static switch_status exosip_write_frame(switch_core_session *session, switch_fra ts = tech_pvt->timestamp_dtmf += samples; tech_pvt->out_digit_seq++; for (x = 0; x < 3; x++) { + frame->flags = 0; switch_rtp_write_payload(tech_pvt->rtp_session, tech_pvt->out_digit_packet, 4, 101, ts, tech_pvt->out_digit_seq, &frame->flags); printf("Send start packet for [%c] ts=%d sofar=%u dur=%d\n", tech_pvt->out_digit, ts, tech_pvt->out_digit_sofar, 0); @@ -770,10 +775,10 @@ static switch_status exosip_write_frame(switch_core_session *session, switch_fra - //printf("%s %s->%s send %d bytes %d samples in %d frames taking up %d ms ts=%d\n", switch_channel_get_name(channel), tech_pvt->local_sdp_audio_ip, tech_pvt->remote_sdp_audio_ip, frame->datalen, samples, frames, ms, tech_pvt->timestamp_send); + //printf("%s %s->%s send %d bytes %d samples in %d frames ts=%d\n", switch_channel_get_name(channel), tech_pvt->local_sdp_audio_ip, tech_pvt->remote_sdp_audio_ip, frame->datalen, samples, frames, tech_pvt->timestamp_send); - - switch_rtp_write(tech_pvt->rtp_session, frame->data, (int) frame->datalen, samples, &frame->flags); + switch_rtp_write_frame(tech_pvt->rtp_session, frame, samples); + tech_pvt->timestamp_send += (int) samples; switch_clear_flag(tech_pvt, TFLAG_WRITING); diff --git a/src/mod/endpoints/mod_iax/mod_iax.c b/src/mod/endpoints/mod_iax/mod_iax.c index c369f59264..0c26b64f49 100644 --- a/src/mod/endpoints/mod_iax/mod_iax.c +++ b/src/mod/endpoints/mod_iax/mod_iax.c @@ -55,7 +55,8 @@ typedef enum { TFLAG_DTMF = (1 << 3), TFLAG_VOICE = (1 << 4), TFLAG_HANGUP = (1 << 5), - TFLAG_LINEAR = (1 << 6) + TFLAG_LINEAR = (1 << 6), + TFLAG_CODEC = (1 << 7) } TFLAGS; typedef enum { @@ -375,6 +376,7 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s tech_pvt->read_frame.codec = &tech_pvt->read_codec; switch_core_session_set_read_codec(tech_pvt->session, &tech_pvt->read_codec); switch_core_session_set_write_codec(tech_pvt->session, &tech_pvt->write_codec); + switch_set_flag(tech_pvt, TFLAG_CODEC); } tech_pvt->codec = chosen; tech_pvt->codecs = local_cap; @@ -409,7 +411,7 @@ static void iax_err_cb(const char *s) static void iax_out_cb(const char *s) { - if (globals.debug) { + if (globals.debug > 1) { switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_DEBUG, "IAX INFO: %s", s); } } @@ -874,7 +876,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void) return SWITCH_STATUS_TERM; } - if (globals.debug) { + if (globals.debug > 1) { iax_enable_debug(); } if (iax_init(globals.ip, globals.port) < 0) { @@ -909,8 +911,9 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void) if (globals.debug && iaxevent->etype != IAX_EVENT_VOICE) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Event %d [%s]!\n", - iaxevent->etype, IAXNAMES[iaxevent->etype]); + iaxevent->etype, IAXNAMES[iaxevent->etype]); } + switch (iaxevent->etype) { case IAX_EVENT_REGACK: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Registration completed successfully.\n"); @@ -926,7 +929,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void) if (tech_pvt) { unsigned int cap = iax_session_get_capability(iaxevent->session); unsigned int format = iaxevent->ies.format; - + if (iax_set_codec(tech_pvt, iaxevent->session, &format, &cap, &iaxevent->ies.samprate, IAX_SET) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "WTF? %u %u\n", iaxevent->ies.format, @@ -950,7 +953,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void) if ((channel = switch_core_session_get_channel(tech_pvt->session)) != 0) { if (switch_channel_test_flag(channel, CF_ANSWERED)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "WTF Mutiple Answer %s?\n", - switch_channel_get_name(channel)); + switch_channel_get_name(channel)); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Answer %s\n", switch_channel_get_name(channel)); @@ -1054,8 +1057,17 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void) switch_channel *channel; if (((channel = switch_core_session_get_channel(tech_pvt->session)) != 0) && switch_channel_get_state(channel) <= CS_HANGUP) { - int bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame; - int frames = (int) (tech_pvt->read_frame.datalen / bytes); + int bytes, frames; + + if (!switch_test_flag(tech_pvt, TFLAG_CODEC)) { + //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "audio with no codec yet!\n"); + break; + } + + bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame; + frames = (int) (tech_pvt->read_frame.datalen / bytes); + + tech_pvt->read_frame.samples = frames * tech_pvt->read_codec.implementation->samples_per_frame; memcpy(tech_pvt->read_frame.data, iaxevent->data, iaxevent->datalen); /* wake up the i/o thread */ diff --git a/src/switch_ivr.c b/src/switch_ivr.c index 24b1248b5d..771eef05ee 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -759,8 +759,6 @@ static void *audio_bridge_thread(switch_thread *thread, void *obj) /* read audio from 1 channel and write it to the other */ if (switch_core_session_read_frame(session_a, &read_frame, -1, stream_id) == SWITCH_STATUS_SUCCESS && read_frame->datalen) { - - if (switch_core_session_write_frame(session_b, read_frame, -1, stream_id) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "write: %s Bad Frame....[%u] Bubye!\n", switch_channel_get_name(chan_b), read_frame->datalen); data->running = -1; @@ -903,14 +901,14 @@ SWITCH_DECLARE(switch_status) switch_ivr_multi_threaded_bridge(switch_core_sessi !switch_channel_test_flag(peer_channel, CF_ANSWERED) && !switch_channel_test_flag(peer_channel, CF_EARLY_MEDIA) && ((time(NULL) - start) < timelimit)) { - + /* read from the channel while we wait if the audio is up on it */ if (switch_channel_test_flag(caller_channel, CF_ANSWERED) || switch_channel_test_flag(caller_channel, CF_EARLY_MEDIA)) { if (switch_core_session_read_frame(session, &read_frame, 1000, 0) != SWITCH_STATUS_SUCCESS) { break; } if (read_frame) { - memset(read_frame->data, 0, read_frame->datalen); + //memset(read_frame->data, 0, read_frame->datalen); if (switch_core_session_write_frame(session, read_frame, 1000, 0) != SWITCH_STATUS_SUCCESS) { break; } @@ -919,6 +917,7 @@ SWITCH_DECLARE(switch_status) switch_ivr_multi_threaded_bridge(switch_core_sessi } else { switch_yield(1000); } + } if (switch_channel_test_flag(peer_channel, CF_ANSWERED)) { @@ -941,6 +940,7 @@ SWITCH_DECLARE(switch_status) switch_ivr_multi_threaded_bridge(switch_core_sessi msg.pointer_arg = peer_session; switch_core_session_receive_message(session, &msg); + switch_core_session_launch_thread(peer_session, audio_bridge_thread, (void *) &other_audio_thread); audio_bridge_thread(NULL, (void *) &this_audio_thread); diff --git a/src/switch_rtp.c b/src/switch_rtp.c index f95a4bce49..ebe3889827 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -495,7 +495,7 @@ static int rtp_common_read(switch_rtp *rtp_session, int *payload_type, switch_fr for(;;) { bytes = sizeof(rtp_msg_t); status = switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock, 0, (void *)&rtp_session->recv_msg, &bytes); - + if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_AUTOADJ) && rtp_session->from_addr->port != rtp_session->remote_port) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Auto Changing port to %u\n", rtp_session->from_addr->port); rtp_session->remote_addr->port = rtp_session->from_addr->port; @@ -601,6 +601,9 @@ SWITCH_DECLARE(switch_status) switch_rtp_zerocopy_read_frame(switch_rtp *rtp_ses if (bytes < 0) { frame->datalen = 0; return SWITCH_STATUS_GENERR; + } else if (bytes == 0) { + frame->datalen = 0; + return SWITCH_STATUS_BREAK; } else { bytes -= rtp_header_len; } @@ -630,6 +633,7 @@ SWITCH_DECLARE(switch_status) switch_rtp_zerocopy_read(switch_rtp *rtp_session, static int rtp_common_write(switch_rtp *rtp_session, void *data, uint32_t datalen, uint8_t payload, switch_frame_flag *flags) { switch_size_t bytes; + uint8_t packetize = (rtp_session->packet_size > datalen && (payload == rtp_session->payload)) ? 1 : 0; uint8_t fwd = (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_RAW_WRITE) && (*flags & SFF_RAW_RTP)) ? 1 : 0; rtp_msg_t *send_msg; @@ -639,7 +643,7 @@ static int rtp_common_write(switch_rtp *rtp_session, void *data, uint32_t datale } else { send_msg = &rtp_session->send_msg; send_msg->header.pt = rtp_session->payload; - if (rtp_session->packet_size > datalen && (payload == rtp_session->payload)) { + if (packetize) { if (!rtp_session->packet_buffer) { if (switch_buffer_create(rtp_session->pool, &rtp_session->packet_buffer, rtp_session->packet_size * 2) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Buffer memory error\n"); @@ -700,6 +704,34 @@ SWITCH_DECLARE(int) switch_rtp_write(switch_rtp *rtp_session, void *data, uint32 } +SWITCH_DECLARE(int) switch_rtp_write_frame(switch_rtp *rtp_session, switch_frame *frame, uint32_t ts) +{ + uint8_t fwd = (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_RAW_WRITE) && switch_test_flag(frame, SFF_RAW_RTP)) ? 1 : 0; + uint8_t packetize = (rtp_session->packet_size > frame->datalen && (frame->payload == rtp_session->payload)) ? 1 : 0; + void *data; + uint32_t len; + + if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_IO) || !rtp_session->remote_addr) { + return -1; + } + + if (fwd && !packetize) { + data = frame->packet; + len = frame->packetlen; + } else { + data = frame->data; + len = frame->datalen; + rtp_session->ts += ts; + rtp_session->seq = ntohs(rtp_session->seq) + 1; + rtp_session->seq = htons(rtp_session->seq); + rtp_session->send_msg.header.seq = rtp_session->seq; + rtp_session->send_msg.header.ts = htonl(rtp_session->ts); + } + + return rtp_common_write(rtp_session, data, len, rtp_session->payload, &frame->flags); + +} + SWITCH_DECLARE(int) switch_rtp_write_payload(switch_rtp *rtp_session, void *data, uint16_t datalen, uint8_t payload, uint32_t ts, uint16_t mseq, switch_frame_flag *flags) {