Merge "res_pjsip/res_pjsip_callerid: NULL check on caller id name string"
[asterisk/asterisk.git] / res / res_pjsip_sdp_rtp.c
index 21de440..97e365c 100644 (file)
@@ -198,22 +198,41 @@ static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_me
        struct ast_sockaddr *media_address =  &address_rtp;
 
        if (session->endpoint->media.bind_rtp_to_media_address && !ast_strlen_zero(session->endpoint->media.address)) {
-               ast_sockaddr_parse(&temp_media_address, session->endpoint->media.address, 0);
-               media_address = &temp_media_address;
+               if (ast_sockaddr_parse(&temp_media_address, session->endpoint->media.address, 0)) {
+                       ast_debug(1, "Endpoint %s: Binding RTP media to %s\n",
+                               ast_sorcery_object_get_id(session->endpoint),
+                               session->endpoint->media.address);
+                       media_address = &temp_media_address;
+               } else {
+                       ast_debug(1, "Endpoint %s: RTP media address invalid: %s\n",
+                               ast_sorcery_object_get_id(session->endpoint),
+                               session->endpoint->media.address);
+               }
        } else {
-               struct ast_sip_transport *transport =
-                       ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
-                                                                          session->endpoint->transport);
-
-               if (transport && transport->state) {
-                       char hoststr[PJ_INET6_ADDRSTRLEN];
-
-                       pj_sockaddr_print(&transport->state->host, hoststr, sizeof(hoststr), 0);
-                       ast_debug(1, "Transport: %s bound to host: %s, using this for media.\n",
-                                         session->endpoint->transport, hoststr);
-                       ast_sockaddr_parse(media_address, hoststr, 0);
+               struct ast_sip_transport *transport;
+
+               transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
+                       session->endpoint->transport);
+               if (transport) {
+                       struct ast_sip_transport_state *trans_state;
+
+                       trans_state = ast_sip_get_transport_state(ast_sorcery_object_get_id(transport));
+                       if (trans_state) {
+                               char hoststr[PJ_INET6_ADDRSTRLEN];
+
+                               pj_sockaddr_print(&trans_state->host, hoststr, sizeof(hoststr), 0);
+                               if (ast_sockaddr_parse(&temp_media_address, hoststr, 0)) {
+                                       ast_debug(1, "Transport %s bound to %s: Using it for RTP media.\n",
+                                               session->endpoint->transport, hoststr);
+                                       media_address = &temp_media_address;
+                               } else {
+                                       ast_debug(1, "Transport %s bound to %s: Invalid for RTP media.\n",
+                                               session->endpoint->transport, hoststr);
+                               }
+                               ao2_ref(trans_state, -1);
+                       }
+                       ao2_ref(transport, -1);
                }
-               ao2_cleanup(transport);
        }
 
        if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, media_address, NULL))) {
@@ -434,6 +453,7 @@ static int set_caps(struct ast_sip_session *session,
 static pjmedia_sdp_attr* generate_rtpmap_attr(struct ast_sip_session *session, pjmedia_sdp_media *media, pj_pool_t *pool,
                                              int rtp_code, int asterisk_format, struct ast_format *format, int code)
 {
+       extern pj_bool_t pjsip_use_compact_form;
        pjmedia_sdp_rtpmap rtpmap;
        pjmedia_sdp_attr *attr = NULL;
        char tmp[64];
@@ -442,6 +462,11 @@ static pjmedia_sdp_attr* generate_rtpmap_attr(struct ast_sip_session *session, p
 
        snprintf(tmp, sizeof(tmp), "%d", rtp_code);
        pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp);
+
+       if (rtp_code <= AST_RTP_PT_LAST_STATIC && pjsip_use_compact_form) {
+               return NULL;
+       }
+
        rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1];
        rtpmap.clock_rate = ast_rtp_lookup_sample_rate2(asterisk_format, format, code);
        pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, options));
@@ -1241,11 +1266,9 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as
                        continue;
                }
 
-               if (!(attr = generate_rtpmap_attr(session, media, pool, rtp_code, 1, format, 0))) {
-                       ao2_ref(format, -1);
-                       continue;
+               if ((attr = generate_rtpmap_attr(session, media, pool, rtp_code, 1, format, 0))) {
+                       media->attr[media->attr_count++] = attr;
                }
-               media->attr[media->attr_count++] = attr;
 
                if ((attr = generate_fmtp_attr(pool, format, rtp_code))) {
                        media->attr[media->attr_count++] = attr;
@@ -1274,12 +1297,10 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as
                                continue;
                        }
 
-                       if (!(attr = generate_rtpmap_attr(session, media, pool, rtp_code, 0, NULL, index))) {
-                               continue;
+                       if ((attr = generate_rtpmap_attr(session, media, pool, rtp_code, 0, NULL, index))) {
+                               media->attr[media->attr_count++] = attr;
                        }
 
-                       media->attr[media->attr_count++] = attr;
-
                        if (index == AST_RTP_DTMF) {
                                snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
                                attr = pjmedia_sdp_attr_create(pool, "fmtp", pj_cstr(&stmp, tmp));
@@ -1631,7 +1652,7 @@ static int load_module(void)
 end:
        unload_module();
 
-       return AST_MODULE_LOAD_FAILURE;
+       return AST_MODULE_LOAD_DECLINE;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP SDP RTP/AVP stream handler",