diff options
Diffstat (limited to 'res')
-rw-r--r-- | res/Makefile | 3 | ||||
-rw-r--r-- | res/res_musiconhold.c | 1 | ||||
-rw-r--r-- | res/res_pjsip/location.c | 5 | ||||
-rw-r--r-- | res/res_pjsip/pjsip_configuration.c | 2 | ||||
-rw-r--r-- | res/res_pjsip_outbound_registration.c | 2 | ||||
-rw-r--r-- | res/res_pjsip_pubsub.c | 45 | ||||
-rw-r--r-- | res/res_pjsip_registrar.c | 34 | ||||
-rw-r--r-- | res/res_pjsip_sdp_rtp.c | 3 | ||||
-rw-r--r-- | res/res_rtp_asterisk.c | 171 | ||||
-rw-r--r-- | res/res_sdp_translator_pjmedia.c | 605 |
10 files changed, 242 insertions, 629 deletions
diff --git a/res/Makefile b/res/Makefile index ecaa03d3c..ec3417b35 100644 --- a/res/Makefile +++ b/res/Makefile @@ -45,7 +45,8 @@ endif $(ECHO_PREFIX) echo " [FLEX] $< -> $@" $(CMD_PREFIX) (cd ael; $(FLEX) ael.flex) $(CMD_PREFIX) sed 's@#if __STDC_VERSION__ >= 199901L@#if !defined __STDC_VERSION__ || __STDC_VERSION__ >= 199901L@' $@ > $@.fix - $(CMD_PREFIX) echo "#include \"asterisk.h\"" > $@ + $(CMD_PREFIX) echo "#define ASTMM_LIBC ASTMM_REDIRECT" > $@ + $(CMD_PREFIX) echo "#include \"asterisk.h\"" >> $@ $(CMD_PREFIX) echo >> $@ $(CMD_PREFIX) cat $@.fix >> $@ $(CMD_PREFIX) rm $@.fix diff --git a/res/res_musiconhold.c b/res/res_musiconhold.c index 55b14c934..1c8728cf7 100644 --- a/res/res_musiconhold.c +++ b/res/res_musiconhold.c @@ -333,7 +333,6 @@ static int ast_moh_files_next(struct ast_channel *chan) } } else { state->announcement = 0; - state->samples = 0; } if (!state->class->total_files) { diff --git a/res/res_pjsip/location.c b/res/res_pjsip/location.c index 22da80577..6e79dc40b 100644 --- a/res/res_pjsip/location.c +++ b/res/res_pjsip/location.c @@ -133,11 +133,6 @@ static void *contact_alloc(const char *name) return NULL; } - ast_string_field_init_extended(contact, endpoint_name); - ast_string_field_init_extended(contact, reg_server); - ast_string_field_init_extended(contact, via_addr); - ast_string_field_init_extended(contact, call_id); - /* Dynamic contacts are delimited with ";@" and static ones with "@@" */ if ((aor_separator = strstr(id, ";@")) || (aor_separator = strstr(id, "@@"))) { *aor_separator = '\0'; diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c index 3094f248e..fb84a1f60 100644 --- a/res/res_pjsip/pjsip_configuration.c +++ b/res/res_pjsip/pjsip_configuration.c @@ -2248,8 +2248,6 @@ void *ast_sip_endpoint_alloc(const char *name) return NULL; } - ast_string_field_init_extended(endpoint, incoming_mwi_mailbox); - if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { ao2_cleanup(endpoint); return NULL; diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c index 8a90849c0..0d815ad39 100644 --- a/res/res_pjsip_outbound_registration.c +++ b/res/res_pjsip_outbound_registration.c @@ -834,6 +834,8 @@ static int reregister_immediately_cb(void *obj) * * \param obj What is needed to initiate a reregister attempt. * + * \note Normally executed by the pjsip monitor thread. + * * \return Nothing */ static void registration_transport_shutdown_cb(void *obj) diff --git a/res/res_pjsip_pubsub.c b/res/res_pjsip_pubsub.c index 9e0718f51..d98491495 100644 --- a/res/res_pjsip_pubsub.c +++ b/res/res_pjsip_pubsub.c @@ -560,15 +560,52 @@ static void *publication_resource_alloc(const char *name) return ast_sorcery_generic_alloc(sizeof(struct ast_sip_publication_resource), publication_resource_destroy); } -static void sub_tree_transport_cb(void *data) { +static int sub_tree_subscription_terminate_cb(void *data) +{ struct sip_subscription_tree *sub_tree = data; - ast_debug(3, "Transport destroyed. Removing subscription '%s->%s' prune on restart: %d\n", + if (!sub_tree->evsub) { + /* Something else already terminated the subscription. */ + ao2_ref(sub_tree, -1); + return 0; + } + + ast_debug(3, "Transport destroyed. Removing subscription '%s->%s' prune on boot: %d\n", sub_tree->persistence->endpoint, sub_tree->root->resource, sub_tree->persistence->prune_on_boot); sub_tree->state = SIP_SUB_TREE_TERMINATE_IN_PROGRESS; pjsip_evsub_terminate(sub_tree->evsub, PJ_TRUE); + + ao2_ref(sub_tree, -1); + return 0; +} + +/*! + * \internal + * \brief The reliable transport we used as a subscription contact has shutdown. + * + * \param data What subscription needs to be terminated. + * + * \note Normally executed by the pjsip monitor thread. + * + * \return Nothing + */ +static void sub_tree_transport_cb(void *data) +{ + struct sip_subscription_tree *sub_tree = data; + + /* + * Push off the subscription termination to the serializer to + * avoid deadlock. Another thread could be trying to send a + * message on the subscription that can deadlock with this + * thread. + */ + ao2_ref(sub_tree, +1); + if (ast_sip_push_task(sub_tree->serializer, sub_tree_subscription_terminate_cb, + sub_tree)) { + ao2_ref(sub_tree, -1); + } } /*! \brief Destructor for subscription persistence */ @@ -621,7 +658,7 @@ static void subscription_persistence_update(struct sip_subscription_tree *sub_tr return; } - ast_debug(3, "Updating persistence for '%s->%s' prune on restart: %s\n", + ast_debug(3, "Updating persistence for '%s->%s' prune on boot: %s\n", sub_tree->persistence->endpoint, sub_tree->root->resource, sub_tree->persistence->prune_on_boot ? "yes" : "no"); @@ -645,7 +682,7 @@ static void subscription_persistence_update(struct sip_subscription_tree *sub_tr sub_tree->endpoint, rdata); if (sub_tree->persistence->prune_on_boot) { - ast_debug(3, "adding transport monitor on %s for '%s->%s' prune on restart: %d\n", + ast_debug(3, "adding transport monitor on %s for '%s->%s' prune on boot: %d\n", rdata->tp_info.transport->obj_name, sub_tree->persistence->endpoint, sub_tree->root->resource, sub_tree->persistence->prune_on_boot); diff --git a/res/res_pjsip_registrar.c b/res/res_pjsip_registrar.c index bdee91fb3..985933e2d 100644 --- a/res/res_pjsip_registrar.c +++ b/res/res_pjsip_registrar.c @@ -337,7 +337,7 @@ static int contact_transport_monitor_matcher(void *a, void *b) && strcmp(ma->contact_name, mb->contact_name) == 0; } -static void register_contact_transport_shutdown_cb(void *data) +static int register_contact_transport_remove_cb(void *data) { struct contact_transport_monitor *monitor = data; struct ast_sip_contact *contact; @@ -345,7 +345,8 @@ static void register_contact_transport_shutdown_cb(void *data) aor = ast_sip_location_retrieve_aor(monitor->aor_name); if (!aor) { - return; + ao2_ref(monitor, -1); + return 0; } ao2_lock(aor); @@ -365,6 +366,35 @@ static void register_contact_transport_shutdown_cb(void *data) } ao2_unlock(aor); ao2_ref(aor, -1); + + ao2_ref(monitor, -1); + return 0; +} + +/*! + * \internal + * \brief The reliable transport we registered as a contact has shutdown. + * + * \param data What contact needs to be removed. + * + * \note Normally executed by the pjsip monitor thread. + * + * \return Nothing + */ +static void register_contact_transport_shutdown_cb(void *data) +{ + struct contact_transport_monitor *monitor = data; + + /* + * Push off to a default serializer. This is in case sorcery + * does database accesses for contacts. Database accesses may + * not be on this machine. We don't want to tie up the pjsip + * monitor thread with potentially long access times. + */ + ao2_ref(monitor, +1); + if (ast_sip_push_task(NULL, register_contact_transport_remove_cb, monitor)) { + ao2_ref(monitor, -1); + } } AST_VECTOR(excess_contact_vector, struct ast_sip_contact *); diff --git a/res/res_pjsip_sdp_rtp.c b/res/res_pjsip_sdp_rtp.c index 03d37652f..14ed3b186 100644 --- a/res/res_pjsip_sdp_rtp.c +++ b/res/res_pjsip_sdp_rtp.c @@ -1096,6 +1096,9 @@ static void add_rtcp_fb_to_stream(struct ast_sip_session *session, attr = pjmedia_sdp_attr_create(pool, "rtcp-fb", pj_cstr(&stmp, "* goog-remb")); pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); + + attr = pjmedia_sdp_attr_create(pool, "rtcp-fb", pj_cstr(&stmp, "* nack")); + pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); } /*! \brief Function which negotiates an incoming media stream */ diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c index c88903777..4ac20d551 100644 --- a/res/res_rtp_asterisk.c +++ b/res/res_rtp_asterisk.c @@ -71,6 +71,7 @@ #include "asterisk/smoother.h" #include "asterisk/uuid.h" #include "asterisk/test.h" +#include "asterisk/data_buffer.h" #ifdef HAVE_PJPROJECT #include "asterisk/res_pjproject.h" #endif @@ -92,6 +93,8 @@ #define TURN_STATE_WAIT_TIME 2000 +#define DEFAULT_RTP_BUFFER_SIZE 250 + /*! Full INTRA-frame Request / Fast Update Request (From RFC2032) */ #define RTCP_PT_FUR 192 /*! Sender Report (From RFC3550) */ @@ -373,6 +376,8 @@ struct ast_rtp { struct rtp_red *red; + struct ast_data_buffer *send_buffer; /*!< Buffer for storing sent packets for retransmission */ + #ifdef HAVE_PJPROJECT ast_cond_t cond; /*!< ICE/TURN condition for signaling */ @@ -509,6 +514,12 @@ struct rtp_red { long int prev_ts; }; +/*! \brief Structure for storing RTP packets for retransmission */ +struct ast_rtp_rtcp_nack_payload { + size_t size; /*!< The size of the payload */ + unsigned char buf[0]; /*!< The payload data */ +}; + AST_LIST_HEAD_NOLOCK(frame_list, ast_frame); /* Forward Declarations */ @@ -3675,6 +3686,11 @@ static int ast_rtp_destroy(struct ast_rtp_instance *instance) rtp->red = NULL; } + /* Destroy the send buffer if it was being used */ + if (rtp->send_buffer) { + ast_data_buffer_free(rtp->send_buffer); + } + ao2_cleanup(rtp->lasttxformat); ao2_cleanup(rtp->lastrxformat); ao2_cleanup(rtp->f.subclass.format); @@ -4369,7 +4385,7 @@ static int rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *fr } else { /* This is the first frame with sequence number we've seen, so start keeping track */ rtp->expectedseqno = frame->seqno + 1; - } + } } else { rtp->expectedseqno = -1; } @@ -4383,13 +4399,27 @@ static int rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *fr /* If we know the remote address construct a packet and send it out */ if (!ast_sockaddr_isnull(&remote_address)) { int hdrlen = 12, res, ice; + int packet_len = frame->datalen + hdrlen; unsigned char *rtpheader = (unsigned char *)(frame->data.ptr - hdrlen); put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (seqno) | (mark << 23))); put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts)); put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc)); - if ((res = rtp_sendto(instance, (void *)rtpheader, frame->datalen + hdrlen, 0, &remote_address, &ice)) < 0) { + /* If retransmissions are enabled, we need to store this packet for future use */ + if (rtp->send_buffer) { + struct ast_rtp_rtcp_nack_payload *payload; + + payload = ast_malloc(sizeof(*payload) + packet_len); + if (payload) { + payload->size = packet_len; + memcpy(payload->buf, rtpheader, packet_len); + ast_data_buffer_put(rtp->send_buffer, rtp->seqno, payload); + } + } + + res = rtp_sendto(instance, (void *)rtpheader, packet_len, 0, &remote_address, &ice); + if (res < 0) { if (!ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT) || (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT) && (ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) { ast_debug(1, "RTP Transmission error of packet %d to %s: %s\n", rtp->seqno, @@ -5145,8 +5175,8 @@ static void update_lost_stats(struct ast_rtp *rtp, unsigned int lost_packets) } /*! \pre instance is locked */ -static struct ast_rtp_instance *rtp_find_instance_by_ssrc(struct ast_rtp_instance *instance, - struct ast_rtp *rtp, unsigned int ssrc) +static struct ast_rtp_instance *__rtp_find_instance_by_ssrc(struct ast_rtp_instance *instance, + struct ast_rtp *rtp, unsigned int ssrc, int source) { int index; @@ -5158,8 +5188,9 @@ static struct ast_rtp_instance *rtp_find_instance_by_ssrc(struct ast_rtp_instanc /* Find the bundled child instance */ for (index = 0; index < AST_VECTOR_SIZE(&rtp->ssrc_mapping); ++index) { struct rtp_ssrc_mapping *mapping = AST_VECTOR_GET_ADDR(&rtp->ssrc_mapping, index); + unsigned int mapping_ssrc = source ? ast_rtp_get_ssrc(mapping->instance) : mapping->ssrc; - if (mapping->ssrc_valid && mapping->ssrc == ssrc) { + if (mapping->ssrc_valid && mapping_ssrc == ssrc) { return mapping->instance; } } @@ -5171,6 +5202,20 @@ static struct ast_rtp_instance *rtp_find_instance_by_ssrc(struct ast_rtp_instanc return NULL; } +/*! \pre instance is locked */ +static struct ast_rtp_instance *rtp_find_instance_by_packet_source_ssrc(struct ast_rtp_instance *instance, + struct ast_rtp *rtp, unsigned int ssrc) +{ + return __rtp_find_instance_by_ssrc(instance, rtp, ssrc, 0); +} + +/*! \pre instance is locked */ +static struct ast_rtp_instance *rtp_find_instance_by_media_source_ssrc(struct ast_rtp_instance *instance, + struct ast_rtp *rtp, unsigned int ssrc) +{ + return __rtp_find_instance_by_ssrc(instance, rtp, ssrc, 1); +} + static const char *rtcp_payload_type2str(unsigned int pt) { const char *str; @@ -5203,6 +5248,69 @@ static const char *rtcp_payload_type2str(unsigned int pt) return str; } +/*! \pre instance is locked */ +static int ast_rtp_rtcp_handle_nack(struct ast_rtp_instance *instance, unsigned int *nackdata, unsigned int position, + unsigned int length) +{ + struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); + int res = 0; + int blp_index; + int packet_index; + int ice; + struct ast_rtp_rtcp_nack_payload *payload; + unsigned int current_word; + unsigned int pid; /* Packet ID which refers to seqno of lost packet */ + unsigned int blp; /* Bitmask of following lost packets */ + struct ast_sockaddr remote_address = { {0,} }; + + if (!rtp->send_buffer) { + ast_debug(1, "Tried to handle NACK request, but we don't have a RTP packet storage!\n"); + return res; + } + + ast_rtp_instance_get_remote_address(instance, &remote_address); + + /* + * We use index 3 because with feedback messages, the FCI (Feedback Control Information) + * does not begin until after the version, packet SSRC, and media SSRC words. + */ + for (packet_index = 3; packet_index < length; packet_index++) { + current_word = ntohl(nackdata[position + packet_index]); + pid = current_word >> 16; + /* We know the remote end is missing this packet. Go ahead and send it if we still have it. */ + payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_get(rtp->send_buffer, pid); + if (payload) { + res += rtp_sendto(instance, payload->buf, payload->size, 0, &remote_address, &ice); + } else { + ast_debug(1, "Received NACK request for RTP packet with seqno %d, but we don't have it\n", pid); + } + /* + * The bitmask. Denoting the least significant bit as 1 and its most significant bit + * as 16, then bit i of the bitmask is set to 1 if the receiver has not received RTP + * packet (pid+i)(modulo 2^16). Otherwise, it is set to 0. We cannot assume bits set + * to 0 after a bit set to 1 have actually been received. + */ + blp = current_word & 0xFF; + blp_index = 1; + while (blp) { + if (blp & 1) { + /* Packet (pid + i)(modulo 2^16) is missing too. */ + unsigned int seqno = (pid + blp_index) % 65536; + payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_get(rtp->send_buffer, seqno); + if (payload) { + res += rtp_sendto(instance, payload->buf, payload->size, 0, &remote_address, &ice); + } else { + ast_debug(1, "Remote end also requested RTP packet with seqno %d, but we don't have it\n", seqno); + } + } + blp >>= 1; + blp_index++; + } + } + + return res; +} + /* * Unshifted RTCP header bit field masks */ @@ -5243,6 +5351,7 @@ static const char *rtcp_payload_type2str(unsigned int pt) #define RTCP_RR_BLOCK_WORD_LENGTH 6 #define RTCP_HEADER_SSRC_LENGTH 2 #define RTCP_FB_REMB_BLOCK_WORD_LENGTH 4 +#define RTCP_FB_NACK_BLOCK_WORD_LENGTH 2 static struct ast_frame *ast_rtcp_interpret(struct ast_rtp_instance *instance, const unsigned char *rtcpdata, size_t size, struct ast_sockaddr *addr) { @@ -5319,6 +5428,8 @@ static struct ast_frame *ast_rtcp_interpret(struct ast_rtp_instance *instance, c unsigned int ssrc_valid; unsigned int length; unsigned int min_length; + /*! Always use packet source SSRC to find the rtp instance unless explicitly told not to. */ + unsigned int use_packet_source = 1; struct ast_json *message_blob; RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup); @@ -5341,9 +5452,20 @@ static struct ast_frame *ast_rtcp_interpret(struct ast_rtp_instance *instance, c /* fall through */ case RTCP_PT_RR: min_length += (rc * RTCP_RR_BLOCK_WORD_LENGTH); + use_packet_source = 0; break; case RTCP_PT_FUR: break; + case AST_RTP_RTCP_RTPFB: + switch (rc) { + case AST_RTP_RTCP_FMT_NACK: + min_length += RTCP_FB_NACK_BLOCK_WORD_LENGTH; + break; + default: + break; + } + use_packet_source = 0; + break; case RTCP_PT_PSFB: switch (rc) { case AST_RTP_RTCP_FMT_REMB: @@ -5392,13 +5514,16 @@ static struct ast_frame *ast_rtcp_interpret(struct ast_rtp_instance *instance, c } rtcp_report->reception_report_count = rc; - ssrc = ntohl(rtcpheader[i + 1]); + ssrc = ntohl(rtcpheader[i + 2]); rtcp_report->ssrc = ssrc; break; case RTCP_PT_FUR: case RTCP_PT_PSFB: ssrc = ntohl(rtcpheader[i + 1]); break; + case AST_RTP_RTCP_RTPFB: + ssrc = ntohl(rtcpheader[i + 2]); + break; case RTCP_PT_SDES: case RTCP_PT_BYE: default: @@ -5417,7 +5542,15 @@ static struct ast_frame *ast_rtcp_interpret(struct ast_rtp_instance *instance, c /* Determine the appropriate instance for this */ if (ssrc_valid) { - child = rtp_find_instance_by_ssrc(transport, transport_rtp, ssrc); + /* + * Depending on the payload type, either the packet source or media source + * SSRC is used. + */ + if (use_packet_source) { + child = rtp_find_instance_by_packet_source_ssrc(transport, transport_rtp, ssrc); + } else { + child = rtp_find_instance_by_media_source_ssrc(transport, transport_rtp, ssrc); + } if (child && child != transport) { /* * It is safe to hold the child lock while holding the parent lock. @@ -5438,7 +5571,7 @@ static struct ast_frame *ast_rtcp_interpret(struct ast_rtp_instance *instance, c } if (ssrc_valid && rtp->themssrc_valid) { - if (ssrc != rtp->themssrc) { + if (ssrc != rtp->themssrc && use_packet_source) { /* * Skip over this RTCP record as it does not contain the * correct SSRC. We should not act upon RTCP records @@ -5581,6 +5714,24 @@ static struct ast_frame *ast_rtcp_interpret(struct ast_rtp_instance *instance, c transport_rtp->f.src = "RTP"; f = &transport_rtp->f; break; + case AST_RTP_RTCP_RTPFB: + switch (rc) { + case AST_RTP_RTCP_FMT_NACK: + /* If retransmissions are not enabled ignore this message */ + if (!rtp->send_buffer) { + break; + } + + if (rtcp_debug_test_addr(addr)) { + ast_verbose("Received generic RTCP NACK message\n"); + } + + ast_rtp_rtcp_handle_nack(instance, rtcpheader, position, length); + break; + default: + break; + } + break; case RTCP_PT_FUR: /* Handle RTCP FUR as FIR by setting the format to 4 */ rc = AST_RTP_RTCP_FMT_FIR; @@ -5981,7 +6132,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc ssrc = ntohl(rtpheader[2]); /* Determine the appropriate instance for this */ - child = rtp_find_instance_by_ssrc(instance, rtp, ssrc); + child = rtp_find_instance_by_packet_source_ssrc(instance, rtp, ssrc); if (!child) { /* Neither the bundled parent nor any child has this SSRC */ return &ast_null_frame; @@ -6614,6 +6765,8 @@ static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_pro } } else if (property == AST_RTP_PROPERTY_ASYMMETRIC_CODEC) { rtp->asymmetric_codec = value; + } else if (property == AST_RTP_PROPERTY_RETRANS_SEND) { + rtp->send_buffer = ast_data_buffer_alloc(ast_free_ptr, DEFAULT_RTP_BUFFER_SIZE); } } diff --git a/res/res_sdp_translator_pjmedia.c b/res/res_sdp_translator_pjmedia.c deleted file mode 100644 index 676e740bc..000000000 --- a/res/res_sdp_translator_pjmedia.c +++ /dev/null @@ -1,605 +0,0 @@ -/* - * Asterisk -- An open source telephony toolkit. - * - * Copyright (C) 2017, Digium, Inc. - * - * Mark Michelson <mmichelson@digium.com> - * - * See http://www.asterisk.org for more information about - * the Asterisk project. Please do not directly contact - * any of the maintainers of this project for assistance; - * the project provides a web site, mailing lists and IRC - * channels for your use. - * - * This program is free software, distributed under the terms of - * the GNU General Public License Version 2. See the LICENSE file - * at the top of the source tree. - */ - -#include "asterisk.h" - -#include <pjlib.h> -#include <pjmedia.h> - -#include "asterisk/res_pjproject.h" -#include "asterisk/sdp_translator.h" -#include "asterisk/sdp_options.h" -#include "asterisk/vector.h" -#include "asterisk/netsock2.h" -#include "asterisk/utils.h" -#include "asterisk/config.h" -#include "asterisk/test.h" -#include "asterisk/module.h" - -#include "asterisk/sdp.h" - -/*** MODULEINFO - <depend>pjproject</depend> - <support_level>core</support_level> - ***/ - -/* - * XXX TODO: The memory in the pool is held onto longer than necessary. It - * is kept and grows for the duration of the associated chan_pjsip session. - * - * The translation API does not need to be so generic. The users will know - * at compile time what the non-Asterisk SDP format they have or need. They - * should simply call the specific translation functions. However, to make - * this a loadable module we need to be able to keep it in memory when a - * dependent module is loaded. - * - * To address both issues I propose this API: - * - * void ast_sdp_translate_pjmedia_ref(void) - Inc this module's user ref - * void ast_sdp_translate_pjmedia_unref(void) - Dec this module's user ref. - * The res_pjsip_session.c:ast_sip_session_alloc() can call the module ref - * and the session's destructor can call the module unref. - * - * struct ast_sdp *ast_sdp_translate_pjmedia_from(const pjmedia_sdp_session *pjmedia_sdp); - * - * pjmedia_sdp_session *ast_sdp_translate_pjmedia_to(const struct ast_sdp *sdp, pj_pool_t *pool); - * Passing in a memory pool allows the memory to be obtained from an - * rdata memory pool that will be released when the message processing - * is complete. This prevents memory from accumulating for the duration - * of a call. - * - * int ast_sdp_translate_pjmedia_set_remote_sdp(struct ast_sdp_state *sdp_state, const pjmedia_sdp_session *remote); - * const pjmedia_sdp_session *ast_sdp_translate_pjmedia_get_local_sdp(struct ast_sdp_state *sdp_state, pj_pool_t *pool); - * These two functions just do the bookkeeping to translate and set or get - * the requested SDP. - * - * - * XXX TODO: This code doesn't handle allocation failures very well. i.e., - * It assumes they will never happen. - * - * XXX TODO: This code uses ast_alloca() inside loops. Doing so if the number - * of times through the loop is unconstrained will blow the stack. - * See dupa_pj_str() usage. - */ - -static pj_caching_pool sdp_caching_pool; - - -static void *pjmedia_new(void) -{ - pj_pool_t *pool; - - pool = pj_pool_create(&sdp_caching_pool.factory, "pjmedia sdp translator", 1024, 1024, NULL); - - return pool; -} - -static void pjmedia_free(void *translator_priv) -{ - pj_pool_t *pool = translator_priv; - - pj_pool_release(pool); -} - -#define dupa_pj_str(pjstr) \ -({ \ - char *dest = ast_alloca(pjstr.slen + 1); \ - memcpy(dest, pjstr.ptr, pjstr.slen); \ - dest[pjstr.slen] = '\0'; \ - dest; \ -}) - -static struct ast_sdp_m_line *pjmedia_copy_m_line(struct pjmedia_sdp_media *pjmedia_m_line) -{ - int i; - - struct ast_sdp_c_line *c_line = pjmedia_m_line->conn ? - ast_sdp_c_alloc(dupa_pj_str(pjmedia_m_line->conn->addr_type), - dupa_pj_str(pjmedia_m_line->conn->addr)) : NULL; - - struct ast_sdp_m_line *m_line = ast_sdp_m_alloc(dupa_pj_str(pjmedia_m_line->desc.media), - pjmedia_m_line->desc.port, pjmedia_m_line->desc.port_count, - dupa_pj_str(pjmedia_m_line->desc.transport), c_line); - - for (i = 0; i < pjmedia_m_line->desc.fmt_count; ++i) { - ast_sdp_m_add_payload(m_line, - ast_sdp_payload_alloc(dupa_pj_str(pjmedia_m_line->desc.fmt[i]))); - } - - for (i = 0; i < pjmedia_m_line->attr_count; ++i) { - ast_sdp_m_add_a(m_line, ast_sdp_a_alloc(dupa_pj_str(pjmedia_m_line->attr[i]->name), - dupa_pj_str(pjmedia_m_line->attr[i]->value))); - } - - return m_line; -} - -static void pjmedia_copy_a_lines(struct ast_sdp *new_sdp, const pjmedia_sdp_session *pjmedia_sdp) -{ - int i; - - for (i = 0; i < pjmedia_sdp->attr_count; ++i) { - ast_sdp_add_a(new_sdp, ast_sdp_a_alloc(dupa_pj_str(pjmedia_sdp->attr[i]->name), - dupa_pj_str(pjmedia_sdp->attr[i]->value))); - } -} - -static void pjmedia_copy_m_lines(struct ast_sdp *new_sdp, - const struct pjmedia_sdp_session *pjmedia_sdp) -{ - int i; - - for (i = 0; i < pjmedia_sdp->media_count; ++i) { - ast_sdp_add_m(new_sdp, pjmedia_copy_m_line(pjmedia_sdp->media[i])); - } -} - -static struct ast_sdp *pjmedia_to_sdp(const void *in, void *translator_priv) -{ - const struct pjmedia_sdp_session *pjmedia_sdp = in; - - struct ast_sdp_o_line *o_line = ast_sdp_o_alloc(dupa_pj_str(pjmedia_sdp->origin.user), - pjmedia_sdp->origin.id, pjmedia_sdp->origin.version, - dupa_pj_str(pjmedia_sdp->origin.addr_type), dupa_pj_str(pjmedia_sdp->origin.addr)); - - struct ast_sdp_c_line *c_line = pjmedia_sdp->conn ? - ast_sdp_c_alloc(dupa_pj_str(pjmedia_sdp->conn->addr_type), - dupa_pj_str(pjmedia_sdp->conn->addr)) : NULL; - - struct ast_sdp_s_line *s_line = ast_sdp_s_alloc(dupa_pj_str(pjmedia_sdp->name)); - - struct ast_sdp_t_line *t_line = ast_sdp_t_alloc(pjmedia_sdp->time.start, - pjmedia_sdp->time.stop); - - struct ast_sdp *new_sdp = ast_sdp_alloc(o_line, c_line, s_line, t_line); - - pjmedia_copy_a_lines(new_sdp, pjmedia_sdp); - pjmedia_copy_m_lines(new_sdp, pjmedia_sdp); - - return new_sdp; -} - -static void copy_o_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, - struct ast_sdp_o_line *o_line) -{ - pjmedia_sdp->origin.id = o_line->session_id; - pjmedia_sdp->origin.version = o_line->session_version; - pj_strdup2(pool, &pjmedia_sdp->origin.user, o_line->username); - pj_strdup2(pool, &pjmedia_sdp->origin.addr_type, o_line->address_type); - pj_strdup2(pool, &pjmedia_sdp->origin.addr, o_line->address); - pj_strdup2(pool, &pjmedia_sdp->origin.net_type, "IN"); -} - -static void copy_s_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, - struct ast_sdp_s_line *s_line) -{ - pj_strdup2(pool, &pjmedia_sdp->name, s_line->session_name); -} - -static void copy_t_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, - struct ast_sdp_t_line *t_line) -{ - pjmedia_sdp->time.start = t_line->start_time; - pjmedia_sdp->time.stop = t_line->stop_time; -} - -static void copy_c_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_conn **conn, - struct ast_sdp_c_line *c_line) -{ - pjmedia_sdp_conn *local_conn; - local_conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn); - pj_strdup2(pool, &local_conn->addr_type, c_line->address_type); - pj_strdup2(pool, &local_conn->addr, c_line->address); - pj_strdup2(pool, &local_conn->net_type, "IN"); - - *conn = local_conn; -} - -static void copy_a_lines_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, - const struct ast_sdp *sdp) -{ - int i; - - for (i = 0; i < ast_sdp_get_a_count(sdp); ++i) { - pjmedia_sdp_attr *attr; - pj_str_t value; - struct ast_sdp_a_line *a_line; - - a_line = ast_sdp_get_a(sdp, i); - pj_strdup2(pool, &value, a_line->value); - attr = pjmedia_sdp_attr_create(pool, a_line->name, &value); - pjmedia_sdp_session_add_attr(pjmedia_sdp, attr); - } -} - -static void copy_a_lines_pjmedia_media(pj_pool_t *pool, pjmedia_sdp_media *media, - struct ast_sdp_m_line *m_line) -{ - int i; - - for (i = 0; i < ast_sdp_m_get_a_count(m_line); ++i) { - pjmedia_sdp_attr *attr; - pj_str_t value; - struct ast_sdp_a_line *a_line; - - a_line = ast_sdp_m_get_a(m_line, i); - pj_strdup2(pool, &value, a_line->value); - attr = pjmedia_sdp_attr_create(pool, a_line->name, &value); - pjmedia_sdp_media_add_attr(media, attr); - } -} - -static void copy_m_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_media *media, - struct ast_sdp_m_line *m_line) -{ - int i; - - media->desc.port = m_line->port; - media->desc.port_count = m_line->port_count; - pj_strdup2(pool, &media->desc.transport, m_line->proto); - pj_strdup2(pool, &media->desc.media, m_line->type); - - for (i = 0; i < ast_sdp_m_get_payload_count(m_line); ++i) { - pj_strdup2(pool, &media->desc.fmt[i], ast_sdp_m_get_payload(m_line, i)->fmt); - ++media->desc.fmt_count; - } - if (m_line->c_line && m_line->c_line->address) { - copy_c_line_pjmedia(pool, &media->conn, m_line->c_line); - } - copy_a_lines_pjmedia_media(pool, media, m_line); -} - -static void copy_m_lines_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, - const struct ast_sdp *sdp) -{ - int i; - - for (i = 0; i < ast_sdp_get_m_count(sdp); ++i) { - pjmedia_sdp_media *media; - - media = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media); - copy_m_line_pjmedia(pool, media, ast_sdp_get_m(sdp, i)); - pjmedia_sdp->media[pjmedia_sdp->media_count] = media; - ++pjmedia_sdp->media_count; - } -} - -static const void *sdp_to_pjmedia(const struct ast_sdp *sdp, void *translator_priv) -{ - pj_pool_t *pool = translator_priv; - pjmedia_sdp_session *pjmedia_sdp; - - pjmedia_sdp = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_session); - copy_o_line_pjmedia(pool, pjmedia_sdp, sdp->o_line); - copy_s_line_pjmedia(pool, pjmedia_sdp, sdp->s_line); - copy_t_line_pjmedia(pool, pjmedia_sdp, sdp->t_line); - copy_c_line_pjmedia(pool, &pjmedia_sdp->conn, sdp->c_line); - copy_a_lines_pjmedia(pool, pjmedia_sdp, sdp); - copy_m_lines_pjmedia(pool, pjmedia_sdp, sdp); - return pjmedia_sdp; -} - -static struct ast_sdp_translator_ops pjmedia_translator = { - .repr = AST_SDP_IMPL_PJMEDIA, - .translator_new = pjmedia_new, - .translator_free = pjmedia_free, - .to_sdp = pjmedia_to_sdp, - .from_sdp = sdp_to_pjmedia, -}; - -#ifdef TEST_FRAMEWORK - -static int verify_s_line(struct ast_sdp_s_line *s_line, char *expected) -{ - return strcmp(s_line->session_name, expected) == 0; -} - -static int verify_c_line(struct ast_sdp_c_line *c_line, char *family, char *addr) -{ - return strcmp(c_line->address_type, family) == 0 && strcmp(c_line->address, addr) == 0; -} - -static int verify_t_line(struct ast_sdp_t_line *t_line, uint32_t start, uint32_t end) -{ - return t_line->start_time == start && t_line->stop_time == end; -} - -static int verify_m_line(struct ast_sdp *sdp, int index, char *type, int port, - int port_count, char *profile, ...) -{ - struct ast_sdp_m_line *m_line; - int res; - va_list ap; - int i; - - m_line = ast_sdp_get_m(sdp, index); - - res = strcmp(m_line->type, type) == 0; - res |= m_line->port == port; - res |= m_line->port_count == port_count; - res |= strcmp(m_line->proto, profile) == 0; - - va_start(ap, profile); - for (i = 0; i < ast_sdp_m_get_payload_count(m_line); ++i) { - char *payload; - - payload = va_arg(ap, char *); - if (!payload) { - res = -1; - break; - } - res |= strcmp(ast_sdp_m_get_payload(m_line, i)->fmt, payload) == 0; - } - va_end(ap); - return res; -} - -static int verify_a_line(struct ast_sdp *sdp, int m_index, int a_index, char *name, - char *value) -{ - struct ast_sdp_m_line *m_line; - struct ast_sdp_a_line *a_line; - - m_line = ast_sdp_get_m(sdp, m_index); - a_line = ast_sdp_m_get_a(m_line, a_index); - - return strcmp(a_line->name, name) == 0 && strcmp(a_line->value, value) == 0; -} - -AST_TEST_DEFINE(pjmedia_to_sdp_test) -{ - struct ast_sdp_translator *translator; - pj_pool_t *pool; - char *sdp_str = - "v=0\r\n" - "o=alice 2890844526 2890844527 IN IP4 host.atlanta.example.com\r\n" - "s= \r\n" - "c=IN IP4 host.atlanta.example.com\r\n" - "t=123 456\r\n" - "m=audio 49170 RTP/AVP 0 8 97\r\n" - "a=rtpmap:0 PCMU/8000\r\n" - "a=rtpmap:8 PCMA/8000\r\n" - "a=rtpmap:97 iLBC/8000\r\n" - "a=sendrecv\r\n" - "m=video 51372 RTP/AVP 31 32\r\n" - "a=rtpmap:31 H261/90000\r\n" - "a=rtpmap:32 MPV/90000\r\n"; - pjmedia_sdp_session *pjmedia_sdp; - struct ast_sdp *sdp = NULL; - pj_status_t status; - enum ast_test_result_state res = AST_TEST_PASS; - - switch (cmd) { - case TEST_INIT: - info->name = "pjmedia_to_sdp"; - info->category = "/main/sdp/"; - info->summary = "PJMEDIA to SDP unit test"; - info->description = - "Ensures PJMEDIA SDPs are translated correctly"; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - pool = pj_pool_create(&sdp_caching_pool.factory, "pjmedia to sdp test", 1024, 1024, NULL); - - translator = ast_sdp_translator_new(AST_SDP_IMPL_PJMEDIA); - if (!translator) { - ast_test_status_update(test, "Failed to create SDP translator\n"); - res = AST_TEST_FAIL; - goto cleanup; - } - - status = pjmedia_sdp_parse(pool, sdp_str, strlen(sdp_str), &pjmedia_sdp); - if (status != PJ_SUCCESS) { - ast_test_status_update(test, "Error parsing SDP\n"); - res = AST_TEST_FAIL; - goto cleanup; - } - - sdp = ast_sdp_translator_to_sdp(translator, pjmedia_sdp); - - if (strcmp(sdp->o_line->username, "alice")) { - ast_test_status_update(test, "Unexpected SDP user '%s'\n", sdp->o_line->username); - res = AST_TEST_FAIL; - goto cleanup; - } else if (sdp->o_line->session_id != 2890844526UL) { - ast_test_status_update(test, "Unexpected SDP id '%" PRId64 "lu'\n", sdp->o_line->session_id); - res = AST_TEST_FAIL; - goto cleanup; - } else if (sdp->o_line->session_version != 2890844527UL) { - ast_test_status_update(test, "Unexpected SDP version '%" PRId64 "'\n", sdp->o_line->session_version); - res = AST_TEST_FAIL; - goto cleanup; - } else if (strcmp(sdp->o_line->address_type, "IP4")) { - ast_test_status_update(test, "Unexpected address family '%s'\n", sdp->o_line->address_type); - res = AST_TEST_FAIL; - goto cleanup; - } else if (strcmp(sdp->o_line->address, "host.atlanta.example.com")) { - ast_test_status_update(test, "Unexpected address '%s'\n", sdp->o_line->address); - res = AST_TEST_FAIL; - goto cleanup; - } - - if (!verify_s_line(sdp->s_line, " ")) { - ast_test_status_update(test, "Bad s line\n"); - res = AST_TEST_FAIL; - goto cleanup; - } else if (!verify_c_line(sdp->c_line, "IP4", "host.atlanta.example.com")) { - ast_test_status_update(test, "Bad c line\n"); - res = AST_TEST_FAIL; - goto cleanup; - } else if (!verify_t_line(sdp->t_line, 123, 456)) { - ast_test_status_update(test, "Bad t line\n"); - res = AST_TEST_FAIL; - goto cleanup; - } - - if (!verify_m_line(sdp, 0, "audio", 49170, 1, "RTP/AVP", "0", "8", "97", NULL)) { - ast_test_status_update(test, "Bad m line 1\n"); - res = AST_TEST_FAIL; - goto cleanup; - } else if (!verify_a_line(sdp, 0, 0, "rtpmap", "0 PCMU/8000")) { - ast_test_status_update(test, "Bad a line 1\n"); - res = AST_TEST_FAIL; - goto cleanup; - } else if (!verify_a_line(sdp, 0, 1, "rtpmap", "8 PCMA/8000")) { - ast_test_status_update(test, "Bad a line 2\n"); - res = AST_TEST_FAIL; - goto cleanup; - } else if (!verify_a_line(sdp, 0, 2, "rtpmap", "97 iLBC/8000")) { - ast_test_status_update(test, "Bad a line 3\n"); - res = AST_TEST_FAIL; - goto cleanup; - } else if (!verify_a_line(sdp, 0, 3, "sendrecv", "")) { - ast_test_status_update(test, "Bad a line 3\n"); - res = AST_TEST_FAIL; - goto cleanup; - } else if (!verify_m_line(sdp, 1, "video", 51372, 1, "RTP/AVP", "31", "32", NULL)) { - ast_test_status_update(test, "Bad m line 2\n"); - res = AST_TEST_FAIL; - goto cleanup; - } else if (!verify_a_line(sdp, 1, 0, "rtpmap", "31 H261/90000")) { - ast_test_status_update(test, "Bad a line 4\n"); - res = AST_TEST_FAIL; - goto cleanup; - } else if (!verify_a_line(sdp, 1, 1, "rtpmap", "32 MPV/90000")) { - ast_test_status_update(test, "Bad a line 5\n"); - res = AST_TEST_FAIL; - goto cleanup; - } - -cleanup: - ao2_cleanup(sdp); - ast_sdp_translator_free(translator); - pj_pool_release(pool); - return res; -} - -AST_TEST_DEFINE(sdp_to_pjmedia_test) -{ - struct ast_sdp_translator *translator; - char *sdp_str = - "v=0\r\n" - "o=alice 2890844526 2890844526 IN IP4 host.atlanta.example.com\r\n" - "s= \r\n" - "c=IN IP4 host.atlanta.example.com\r\n" - "t=123 456\r\n" - "m=audio 49170 RTP/AVP 0 8 97\r\n" - "a=rtpmap:0 PCMU/8000\r\n" - "a=rtpmap:8 PCMA/8000\r\n" - "a=rtpmap:97 iLBC/8000\r\n" - "a=sendrecv\r\n" - "m=video 51372 RTP/AVP 31 32\r\n" - "a=rtpmap:31 H261/90000\r\n" - "a=rtpmap:32 MPV/90000\r\n\r\n"; - pj_pool_t *pool; - pjmedia_sdp_session *pjmedia_sdp_orig; - const pjmedia_sdp_session *pjmedia_sdp_dup; - struct ast_sdp *sdp = NULL; - pj_status_t status; - enum ast_test_result_state res = AST_TEST_PASS; - char buf[2048]; - char errbuf[256]; - - switch (cmd) { - case TEST_INIT: - info->name = "sdp_to_pjmedia"; - info->category = "/main/sdp/"; - info->summary = "SDP to PJMEDIA unit test"; - info->description = - "Ensures PJMEDIA SDPs are translated correctly"; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - pool = pj_pool_create(&sdp_caching_pool.factory, "pjmedia to sdp test", 1024, 1024, NULL); - - translator = ast_sdp_translator_new(AST_SDP_IMPL_PJMEDIA); - if (!translator) { - ast_test_status_update(test, "Failed to create SDP translator\n"); - res = AST_TEST_FAIL; - goto cleanup; - } - - status = pjmedia_sdp_parse(pool, sdp_str, strlen(sdp_str), &pjmedia_sdp_orig); - if (status != PJ_SUCCESS) { - ast_test_status_update(test, "Error parsing SDP\n"); - res = AST_TEST_FAIL; - goto cleanup; - } - - sdp = ast_sdp_translator_to_sdp(translator, pjmedia_sdp_orig); - pjmedia_sdp_dup = ast_sdp_translator_from_sdp(translator, sdp); - - if ((status = pjmedia_sdp_session_cmp(pjmedia_sdp_orig, pjmedia_sdp_dup, 0)) != PJ_SUCCESS) { - ast_test_status_update(test, "SDPs aren't equal\n"); - pjmedia_sdp_print(pjmedia_sdp_orig, buf, sizeof(buf)); - ast_test_status_update(test, "Original SDP is %s\n", buf); - pjmedia_sdp_print(pjmedia_sdp_dup, buf, sizeof(buf)); - ast_test_status_update(test, "New SDP is %s\n", buf); - pjmedia_strerror(status, errbuf, sizeof(errbuf)); - ast_test_status_update(test, "PJMEDIA says %d: '%s'\n", status, errbuf); - res = AST_TEST_FAIL; - goto cleanup; - } - -cleanup: - ao2_cleanup(sdp); - ast_sdp_translator_free(translator); - pj_pool_release(pool); - return res; -} - -#endif /* TEST_FRAMEWORK */ - -static int load_module(void) -{ - if (ast_sdp_register_translator(&pjmedia_translator)) { - return AST_MODULE_LOAD_DECLINE; - } - ast_pjproject_caching_pool_init(&sdp_caching_pool, NULL, 1024 * 1024); - AST_TEST_REGISTER(pjmedia_to_sdp_test); - AST_TEST_REGISTER(sdp_to_pjmedia_test); - - return AST_MODULE_LOAD_SUCCESS; -} - -static int unload_module(void) -{ - ast_sdp_unregister_translator(&pjmedia_translator); - ast_pjproject_caching_pool_destroy(&sdp_caching_pool); - AST_TEST_UNREGISTER(pjmedia_to_sdp_test); - AST_TEST_UNREGISTER(sdp_to_pjmedia_test); - return 0; -} - -static int reload_module(void) -{ - return 0; -} - -AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJMEDIA SDP Translator", - .support_level = AST_MODULE_SUPPORT_CORE, - .load = load_module, - .unload = unload_module, - .reload = reload_module, - .load_pri = AST_MODPRI_CHANNEL_DEPEND, - .requires = "res_pjproject", -); |