Changeset 3791 for pjproject/trunk
- Timestamp:
- Oct 6, 2011 5:56:27 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia/session.c
r3664 r3791 67 67 static const pj_str_t STR_RECVONLY = { "recvonly", 8 }; 68 68 69 70 /*71 * Parse fmtp for specified format/payload type.72 */73 static void parse_fmtp( pj_pool_t *pool,74 const pjmedia_sdp_media *m,75 unsigned pt,76 pjmedia_codec_fmtp *fmtp)77 {78 const pjmedia_sdp_attr *attr;79 pjmedia_sdp_fmtp sdp_fmtp;80 char *p, *p_end, fmt_buf[8];81 pj_str_t fmt;82 83 pj_assert(m && fmtp);84 85 pj_bzero(fmtp, sizeof(pjmedia_codec_fmtp));86 87 /* Get "fmtp" attribute for the format */88 pj_ansi_sprintf(fmt_buf, "%d", pt);89 fmt = pj_str(fmt_buf);90 attr = pjmedia_sdp_media_find_attr2(m, "fmtp", &fmt);91 if (attr == NULL)92 return;93 94 /* Parse "fmtp" attribute */95 if (pjmedia_sdp_attr_get_fmtp(attr, &sdp_fmtp) != PJ_SUCCESS)96 return;97 98 /* Prepare parsing */99 p = sdp_fmtp.fmt_param.ptr;100 p_end = p + sdp_fmtp.fmt_param.slen;101 102 /* Parse */103 while (p < p_end) {104 char *token, *start, *end;105 106 /* Skip whitespaces */107 while (p < p_end && (*p == ' ' || *p == '\t')) ++p;108 if (p == p_end)109 break;110 111 /* Get token */112 start = p;113 while (p < p_end && *p != ';' && *p != '=') ++p;114 end = p - 1;115 116 /* Right trim */117 while (end >= start && (*end == ' ' || *end == '\t' ||118 *end == '\r' || *end == '\n' ))119 --end;120 121 /* Forward a char after trimming */122 ++end;123 124 /* Store token */125 if (end > start) {126 token = (char*)pj_pool_alloc(pool, end - start);127 pj_ansi_strncpy(token, start, end - start);128 if (*p == '=')129 /* Got param name */130 pj_strset(&fmtp->param[fmtp->cnt].name, token, end - start);131 else132 /* Got param value */133 pj_strset(&fmtp->param[fmtp->cnt++].val, token, end - start);134 } else if (*p != '=') {135 ++fmtp->cnt;136 }137 138 /* Next */139 ++p;140 }141 }142 143 #if 0 // Moved to stream.c144 /*145 * Create stream info from SDP media line.146 */147 PJ_DEF(pj_status_t) pjmedia_stream_info_from_sdp(148 pjmedia_stream_info *si,149 pj_pool_t *pool,150 pjmedia_endpt *endpt,151 const pjmedia_sdp_session *local,152 const pjmedia_sdp_session *remote,153 unsigned stream_idx)154 {155 pjmedia_codec_mgr *mgr;156 const pjmedia_sdp_attr *attr;157 const pjmedia_sdp_media *local_m;158 const pjmedia_sdp_media *rem_m;159 const pjmedia_sdp_conn *local_conn;160 const pjmedia_sdp_conn *rem_conn;161 int rem_af, local_af;162 pj_sockaddr local_addr;163 pjmedia_sdp_rtpmap *rtpmap;164 unsigned i, pt, fmti;165 pj_status_t status;166 167 168 /* Validate arguments: */169 PJ_ASSERT_RETURN(pool && si && local && remote, PJ_EINVAL);170 PJ_ASSERT_RETURN(stream_idx < local->media_count, PJ_EINVAL);171 PJ_ASSERT_RETURN(stream_idx < remote->media_count, PJ_EINVAL);172 173 174 /* Get codec manager. */175 mgr = pjmedia_endpt_get_codec_mgr(endpt);176 177 /* Keep SDP shortcuts */178 local_m = local->media[stream_idx];179 rem_m = remote->media[stream_idx];180 181 local_conn = local_m->conn ? local_m->conn : local->conn;182 if (local_conn == NULL)183 return PJMEDIA_SDP_EMISSINGCONN;184 185 rem_conn = rem_m->conn ? rem_m->conn : remote->conn;186 if (rem_conn == NULL)187 return PJMEDIA_SDP_EMISSINGCONN;188 189 190 /* Reset: */191 192 pj_bzero(si, sizeof(*si));193 194 #if PJMEDIA_HAS_RTCP_XR && PJMEDIA_STREAM_ENABLE_XR195 /* Set default RTCP XR enabled/disabled */196 si->rtcp_xr_enabled = PJ_TRUE;197 #endif198 199 /* Media type: */200 201 if (pj_stricmp(&local_m->desc.media, &ID_AUDIO) == 0) {202 203 si->type = PJMEDIA_TYPE_AUDIO;204 205 } else if (pj_stricmp(&local_m->desc.media, &ID_VIDEO) == 0) {206 207 si->type = PJMEDIA_TYPE_VIDEO;208 209 } else {210 211 si->type = PJMEDIA_TYPE_UNKNOWN;212 213 /* Avoid rejecting call because of unrecognized media,214 * just return PJ_SUCCESS, this media will be deactivated later.215 */216 //return PJMEDIA_EINVALIMEDIATYPE;217 return PJ_SUCCESS;218 219 }220 221 /* Transport protocol */222 223 /* At this point, transport type must be compatible,224 * the transport instance will do more validation later.225 */226 status = pjmedia_sdp_transport_cmp(&rem_m->desc.transport,227 &local_m->desc.transport);228 if (status != PJ_SUCCESS)229 return PJMEDIA_SDPNEG_EINVANSTP;230 231 if (pj_stricmp(&local_m->desc.transport, &ID_RTP_AVP) == 0) {232 233 si->proto = PJMEDIA_TP_PROTO_RTP_AVP;234 235 } else if (pj_stricmp(&local_m->desc.transport, &ID_RTP_SAVP) == 0) {236 237 si->proto = PJMEDIA_TP_PROTO_RTP_SAVP;238 239 } else {240 241 si->proto = PJMEDIA_TP_PROTO_UNKNOWN;242 return PJ_SUCCESS;243 }244 245 246 /* Check address family in remote SDP */247 rem_af = pj_AF_UNSPEC();248 if (pj_stricmp(&rem_conn->net_type, &ID_IN)==0) {249 if (pj_stricmp(&rem_conn->addr_type, &ID_IP4)==0) {250 rem_af = pj_AF_INET();251 } else if (pj_stricmp(&rem_conn->addr_type, &ID_IP6)==0) {252 rem_af = pj_AF_INET6();253 }254 }255 256 if (rem_af==pj_AF_UNSPEC()) {257 /* Unsupported address family */258 return PJ_EAFNOTSUP;259 }260 261 /* Set remote address: */262 status = pj_sockaddr_init(rem_af, &si->rem_addr, &rem_conn->addr,263 rem_m->desc.port);264 if (status != PJ_SUCCESS) {265 /* Invalid IP address. */266 return PJMEDIA_EINVALIDIP;267 }268 269 /* Check address family of local info */270 local_af = pj_AF_UNSPEC();271 if (pj_stricmp(&local_conn->net_type, &ID_IN)==0) {272 if (pj_stricmp(&local_conn->addr_type, &ID_IP4)==0) {273 local_af = pj_AF_INET();274 } else if (pj_stricmp(&local_conn->addr_type, &ID_IP6)==0) {275 local_af = pj_AF_INET6();276 }277 }278 279 if (local_af==pj_AF_UNSPEC()) {280 /* Unsupported address family */281 return PJ_SUCCESS;282 }283 284 /* Set remote address: */285 status = pj_sockaddr_init(local_af, &local_addr, &local_conn->addr,286 local_m->desc.port);287 if (status != PJ_SUCCESS) {288 /* Invalid IP address. */289 return PJMEDIA_EINVALIDIP;290 }291 292 /* Local and remote address family must match */293 if (local_af != rem_af)294 return PJ_EAFNOTSUP;295 296 /* Media direction: */297 298 if (local_m->desc.port == 0 ||299 pj_sockaddr_has_addr(&local_addr)==PJ_FALSE ||300 pj_sockaddr_has_addr(&si->rem_addr)==PJ_FALSE ||301 pjmedia_sdp_media_find_attr(local_m, &STR_INACTIVE, NULL)!=NULL)302 {303 /* Inactive stream. */304 305 si->dir = PJMEDIA_DIR_NONE;306 307 } else if (pjmedia_sdp_media_find_attr(local_m, &STR_SENDONLY, NULL)!=NULL) {308 309 /* Send only stream. */310 311 si->dir = PJMEDIA_DIR_ENCODING;312 313 } else if (pjmedia_sdp_media_find_attr(local_m, &STR_RECVONLY, NULL)!=NULL) {314 315 /* Recv only stream. */316 317 si->dir = PJMEDIA_DIR_DECODING;318 319 } else {320 321 /* Send and receive stream. */322 323 si->dir = PJMEDIA_DIR_ENCODING_DECODING;324 325 }326 327 /* No need to do anything else if stream is rejected */328 if (local_m->desc.port == 0) {329 return PJ_SUCCESS;330 }331 332 /* If "rtcp" attribute is present in the SDP, set the RTCP address333 * from that attribute. Otherwise, calculate from RTP address.334 */335 attr = pjmedia_sdp_attr_find2(rem_m->attr_count, rem_m->attr,336 "rtcp", NULL);337 if (attr) {338 pjmedia_sdp_rtcp_attr rtcp;339 status = pjmedia_sdp_attr_get_rtcp(attr, &rtcp);340 if (status == PJ_SUCCESS) {341 if (rtcp.addr.slen) {342 status = pj_sockaddr_init(rem_af, &si->rem_rtcp, &rtcp.addr,343 (pj_uint16_t)rtcp.port);344 } else {345 pj_sockaddr_init(rem_af, &si->rem_rtcp, NULL,346 (pj_uint16_t)rtcp.port);347 pj_memcpy(pj_sockaddr_get_addr(&si->rem_rtcp),348 pj_sockaddr_get_addr(&si->rem_addr),349 pj_sockaddr_get_addr_len(&si->rem_addr));350 }351 }352 }353 354 if (!pj_sockaddr_has_addr(&si->rem_rtcp)) {355 int rtcp_port;356 357 pj_memcpy(&si->rem_rtcp, &si->rem_addr, sizeof(pj_sockaddr));358 rtcp_port = pj_sockaddr_get_port(&si->rem_addr) + 1;359 pj_sockaddr_set_port(&si->rem_rtcp, (pj_uint16_t)rtcp_port);360 }361 362 363 /* Get the payload number for receive channel. */364 /*365 Previously we used to rely on fmt[0] being the selected codec,366 but some UA sends telephone-event as fmt[0] and this would367 cause assert failure below.368 369 Thanks Chris Hamilton <chamilton .at. cs.dal.ca> for this patch.370 371 // And codec must be numeric!372 if (!pj_isdigit(*local_m->desc.fmt[0].ptr) ||373 !pj_isdigit(*rem_m->desc.fmt[0].ptr))374 {375 return PJMEDIA_EINVALIDPT;376 }377 378 pt = pj_strtoul(&local_m->desc.fmt[0]);379 pj_assert(PJMEDIA_RTP_PT_TELEPHONE_EVENTS==0 ||380 pt != PJMEDIA_RTP_PT_TELEPHONE_EVENTS);381 */382 383 /* This is to suppress MSVC warning about uninitialized var */384 pt = 0;385 386 /* Find the first codec which is not telephone-event */387 for ( fmti = 0; fmti < local_m->desc.fmt_count; ++fmti ) {388 if ( !pj_isdigit(*local_m->desc.fmt[fmti].ptr) )389 return PJMEDIA_EINVALIDPT;390 pt = pj_strtoul(&local_m->desc.fmt[fmti]);391 if ( PJMEDIA_RTP_PT_TELEPHONE_EVENTS == 0 ||392 pt != PJMEDIA_RTP_PT_TELEPHONE_EVENTS )393 break;394 }395 if ( fmti >= local_m->desc.fmt_count )396 return PJMEDIA_EINVALIDPT;397 398 /* Get codec info.399 * For static payload types, get the info from codec manager.400 * For dynamic payload types, MUST get the rtpmap.401 */402 if (pt < 96) {403 pj_bool_t has_rtpmap;404 405 rtpmap = NULL;406 has_rtpmap = PJ_TRUE;407 408 attr = pjmedia_sdp_media_find_attr(local_m, &ID_RTPMAP,409 &local_m->desc.fmt[fmti]);410 if (attr == NULL) {411 has_rtpmap = PJ_FALSE;412 }413 if (attr != NULL) {414 status = pjmedia_sdp_attr_to_rtpmap(pool, attr, &rtpmap);415 if (status != PJ_SUCCESS)416 has_rtpmap = PJ_FALSE;417 }418 419 /* Build codec format info: */420 if (has_rtpmap) {421 si->fmt.type = si->type;422 si->fmt.pt = pj_strtoul(&local_m->desc.fmt[fmti]);423 pj_strdup(pool, &si->fmt.encoding_name, &rtpmap->enc_name);424 si->fmt.clock_rate = rtpmap->clock_rate;425 426 #if defined(PJMEDIA_HANDLE_G722_MPEG_BUG) && (PJMEDIA_HANDLE_G722_MPEG_BUG != 0)427 /* The session info should have the actual clock rate, because428 * this info is used for calculationg buffer size, etc in stream429 */430 if (si->fmt.pt == PJMEDIA_RTP_PT_G722)431 si->fmt.clock_rate = 16000;432 #endif433 434 /* For audio codecs, rtpmap parameters denotes the number of435 * channels.436 */437 if (si->type == PJMEDIA_TYPE_AUDIO && rtpmap->param.slen) {438 si->fmt.channel_cnt = (unsigned) pj_strtoul(&rtpmap->param);439 } else {440 si->fmt.channel_cnt = 1;441 }442 443 } else {444 const pjmedia_codec_info *p_info;445 446 status = pjmedia_codec_mgr_get_codec_info( mgr, pt, &p_info);447 if (status != PJ_SUCCESS)448 return status;449 450 pj_memcpy(&si->fmt, p_info, sizeof(pjmedia_codec_info));451 }452 453 /* For static payload type, pt's are symetric */454 si->tx_pt = pt;455 456 } else {457 458 attr = pjmedia_sdp_media_find_attr(local_m, &ID_RTPMAP,459 &local_m->desc.fmt[fmti]);460 if (attr == NULL)461 return PJMEDIA_EMISSINGRTPMAP;462 463 status = pjmedia_sdp_attr_to_rtpmap(pool, attr, &rtpmap);464 if (status != PJ_SUCCESS)465 return status;466 467 /* Build codec format info: */468 469 si->fmt.type = si->type;470 si->fmt.pt = pj_strtoul(&local_m->desc.fmt[fmti]);471 pj_strdup(pool, &si->fmt.encoding_name, &rtpmap->enc_name);472 si->fmt.clock_rate = rtpmap->clock_rate;473 474 /* For audio codecs, rtpmap parameters denotes the number of475 * channels.476 */477 if (si->type == PJMEDIA_TYPE_AUDIO && rtpmap->param.slen) {478 si->fmt.channel_cnt = (unsigned) pj_strtoul(&rtpmap->param);479 } else {480 si->fmt.channel_cnt = 1;481 }482 483 /* Determine payload type for outgoing channel, by finding484 * dynamic payload type in remote SDP that matches the answer.485 */486 si->tx_pt = 0xFFFF;487 for (i=0; i<rem_m->desc.fmt_count; ++i) {488 unsigned rpt;489 pjmedia_sdp_attr *r_attr;490 pjmedia_sdp_rtpmap r_rtpmap;491 492 rpt = pj_strtoul(&rem_m->desc.fmt[i]);493 if (rpt < 96)494 continue;495 496 r_attr = pjmedia_sdp_media_find_attr(rem_m, &ID_RTPMAP,497 &rem_m->desc.fmt[i]);498 if (!r_attr)499 continue;500 501 if (pjmedia_sdp_attr_get_rtpmap(r_attr, &r_rtpmap) != PJ_SUCCESS)502 continue;503 504 if (!pj_stricmp(&rtpmap->enc_name, &r_rtpmap.enc_name) &&505 rtpmap->clock_rate == r_rtpmap.clock_rate)506 {507 /* Found matched codec. */508 si->tx_pt = rpt;509 510 break;511 }512 }513 514 if (si->tx_pt == 0xFFFF)515 return PJMEDIA_EMISSINGRTPMAP;516 }517 518 519 /* Now that we have codec info, get the codec param. */520 si->param = PJ_POOL_ALLOC_T(pool, pjmedia_codec_param);521 status = pjmedia_codec_mgr_get_default_param(mgr, &si->fmt, si->param);522 523 /* Get remote fmtp for our encoder. */524 parse_fmtp(pool, rem_m, si->tx_pt, &si->param->setting.enc_fmtp);525 526 /* Get local fmtp for our decoder. */527 parse_fmtp(pool, local_m, si->fmt.pt, &si->param->setting.dec_fmtp);528 529 /* Get the remote ptime for our encoder. */530 attr = pjmedia_sdp_attr_find2(rem_m->attr_count, rem_m->attr,531 "ptime", NULL);532 if (attr) {533 pj_str_t tmp_val = attr->value;534 unsigned frm_per_pkt;535 536 pj_strltrim(&tmp_val);537 538 /* Round up ptime when the specified is not multiple of frm_ptime */539 frm_per_pkt = (pj_strtoul(&tmp_val) + si->param->info.frm_ptime/2) /540 si->param->info.frm_ptime;541 if (frm_per_pkt != 0) {542 si->param->setting.frm_per_pkt = (pj_uint8_t)frm_per_pkt;543 }544 }545 546 /* Get remote maxptime for our encoder. */547 attr = pjmedia_sdp_attr_find2(rem_m->attr_count, rem_m->attr,548 "maxptime", NULL);549 if (attr) {550 pj_str_t tmp_val = attr->value;551 552 pj_strltrim(&tmp_val);553 si->tx_maxptime = pj_strtoul(&tmp_val);554 }555 556 /* When direction is NONE (it means SDP negotiation has failed) we don't557 * need to return a failure here, as returning failure will cause558 * the whole SDP to be rejected. See ticket #:559 * http://560 *561 * Thanks Alain Totouom562 */563 if (status != PJ_SUCCESS && si->dir != PJMEDIA_DIR_NONE)564 return status;565 566 567 /* Get incomming payload type for telephone-events */568 si->rx_event_pt = -1;569 for (i=0; i<local_m->attr_count; ++i) {570 pjmedia_sdp_rtpmap r;571 572 attr = local_m->attr[i];573 if (pj_strcmp(&attr->name, &ID_RTPMAP) != 0)574 continue;575 if (pjmedia_sdp_attr_get_rtpmap(attr, &r) != PJ_SUCCESS)576 continue;577 if (pj_strcmp(&r.enc_name, &ID_TELEPHONE_EVENT) == 0) {578 si->rx_event_pt = pj_strtoul(&r.pt);579 break;580 }581 }582 583 /* Get outgoing payload type for telephone-events */584 si->tx_event_pt = -1;585 for (i=0; i<rem_m->attr_count; ++i) {586 pjmedia_sdp_rtpmap r;587 588 attr = rem_m->attr[i];589 if (pj_strcmp(&attr->name, &ID_RTPMAP) != 0)590 continue;591 if (pjmedia_sdp_attr_get_rtpmap(attr, &r) != PJ_SUCCESS)592 continue;593 if (pj_strcmp(&r.enc_name, &ID_TELEPHONE_EVENT) == 0) {594 si->tx_event_pt = pj_strtoul(&r.pt);595 break;596 }597 }598 599 /* Leave SSRC to random. */600 si->ssrc = pj_rand();601 602 /* Set default jitter buffer parameter. */603 si->jb_init = si->jb_max = si->jb_min_pre = si->jb_max_pre = -1;604 605 return PJ_SUCCESS;606 }607 #endif608 609 69 /* 610 70 * Initialize session info from SDP session descriptors.
Note: See TracChangeset
for help on using the changeset viewer.