res_pjsip_sdp_rtp.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. * Kevin Harwell <kharwell@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \author Joshua Colp <jcolp@digium.com>
  22. *
  23. * \brief SIP SDP media stream handling
  24. */
  25. /*** MODULEINFO
  26. <depend>pjproject</depend>
  27. <depend>res_pjsip</depend>
  28. <depend>res_pjsip_session</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. #include <pjsip.h>
  33. #include <pjsip_ua.h>
  34. #include <pjmedia.h>
  35. #include <pjlib.h>
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include "asterisk/module.h"
  38. #include "asterisk/format.h"
  39. #include "asterisk/format_cap.h"
  40. #include "asterisk/rtp_engine.h"
  41. #include "asterisk/netsock2.h"
  42. #include "asterisk/channel.h"
  43. #include "asterisk/causes.h"
  44. #include "asterisk/sched.h"
  45. #include "asterisk/acl.h"
  46. #include "asterisk/sdp_srtp.h"
  47. #include "asterisk/dsp.h"
  48. #include "asterisk/res_pjsip.h"
  49. #include "asterisk/res_pjsip_session.h"
  50. /*! \brief Scheduler for RTCP purposes */
  51. static struct ast_sched_context *sched;
  52. /*! \brief Address for IPv4 RTP */
  53. static struct ast_sockaddr address_ipv4;
  54. /*! \brief Address for IPv6 RTP */
  55. static struct ast_sockaddr address_ipv6;
  56. static const char STR_AUDIO[] = "audio";
  57. static const int FD_AUDIO = 0;
  58. static const char STR_VIDEO[] = "video";
  59. static const int FD_VIDEO = 2;
  60. /*! \brief Retrieves an ast_format_type based on the given stream_type */
  61. static enum ast_media_type stream_to_media_type(const char *stream_type)
  62. {
  63. if (!strcasecmp(stream_type, STR_AUDIO)) {
  64. return AST_MEDIA_TYPE_AUDIO;
  65. } else if (!strcasecmp(stream_type, STR_VIDEO)) {
  66. return AST_MEDIA_TYPE_VIDEO;
  67. }
  68. return 0;
  69. }
  70. /*! \brief Get the starting descriptor for a media type */
  71. static int media_type_to_fdno(enum ast_media_type media_type)
  72. {
  73. switch (media_type) {
  74. case AST_MEDIA_TYPE_AUDIO: return FD_AUDIO;
  75. case AST_MEDIA_TYPE_VIDEO: return FD_VIDEO;
  76. case AST_MEDIA_TYPE_TEXT:
  77. case AST_MEDIA_TYPE_UNKNOWN:
  78. case AST_MEDIA_TYPE_IMAGE: break;
  79. }
  80. return -1;
  81. }
  82. /*! \brief Remove all other cap types but the one given */
  83. static void format_cap_only_type(struct ast_format_cap *caps, enum ast_media_type media_type)
  84. {
  85. int i = 0;
  86. while (i <= AST_MEDIA_TYPE_TEXT) {
  87. if (i != media_type && i != AST_MEDIA_TYPE_UNKNOWN) {
  88. ast_format_cap_remove_by_type(caps, i);
  89. }
  90. i += 1;
  91. }
  92. }
  93. /*! \brief Internal function which creates an RTP instance */
  94. static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media, unsigned int ipv6)
  95. {
  96. struct ast_rtp_engine_ice *ice;
  97. if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, ipv6 ? &address_ipv6 : &address_ipv4, NULL))) {
  98. ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n", session->endpoint->media.rtp.engine);
  99. return -1;
  100. }
  101. ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, 1);
  102. ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric);
  103. if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) {
  104. ice->stop(session_media->rtp);
  105. }
  106. if (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733 || session->endpoint->dtmf == AST_SIP_DTMF_AUTO) {
  107. ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
  108. ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1);
  109. } else if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
  110. ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
  111. }
  112. if (!strcmp(session_media->stream_type, STR_AUDIO) &&
  113. (session->endpoint->media.tos_audio || session->endpoint->media.cos_video)) {
  114. ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_audio,
  115. session->endpoint->media.cos_audio, "SIP RTP Audio");
  116. } else if (!strcmp(session_media->stream_type, STR_VIDEO) &&
  117. (session->endpoint->media.tos_video || session->endpoint->media.cos_video)) {
  118. ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_video,
  119. session->endpoint->media.cos_video, "SIP RTP Video");
  120. }
  121. return 0;
  122. }
  123. static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs,
  124. struct ast_sip_session_media *session_media)
  125. {
  126. pjmedia_sdp_attr *attr;
  127. pjmedia_sdp_rtpmap *rtpmap;
  128. pjmedia_sdp_fmtp fmtp;
  129. struct ast_format *format;
  130. int i, num = 0, tel_event = 0;
  131. char name[256];
  132. char media[20];
  133. char fmt_param[256];
  134. ast_rtp_codecs_payloads_initialize(codecs);
  135. /* Iterate through provided formats */
  136. for (i = 0; i < stream->desc.fmt_count; ++i) {
  137. /* The payload is kept as a string for things like t38 but for video it is always numerical */
  138. ast_rtp_codecs_payloads_set_m_type(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]));
  139. /* Look for the optional rtpmap attribute */
  140. if (!(attr = pjmedia_sdp_media_find_attr2(stream, "rtpmap", &stream->desc.fmt[i]))) {
  141. continue;
  142. }
  143. /* Interpret the attribute as an rtpmap */
  144. if ((pjmedia_sdp_attr_to_rtpmap(session->inv_session->pool_prov, attr, &rtpmap)) != PJ_SUCCESS) {
  145. continue;
  146. }
  147. ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name));
  148. if (strcmp(name,"telephone-event") == 0) {
  149. tel_event++;
  150. }
  151. ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media));
  152. ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]),
  153. media, name, 0, rtpmap->clock_rate);
  154. /* Look for an optional associated fmtp attribute */
  155. if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) {
  156. continue;
  157. }
  158. if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) {
  159. ast_copy_pj_str(fmt_param, &fmtp.fmt, sizeof(fmt_param));
  160. if (sscanf(fmt_param, "%30d", &num) != 1) {
  161. continue;
  162. }
  163. if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) {
  164. struct ast_format *format_parsed;
  165. ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param));
  166. format_parsed = ast_format_parse_sdp_fmtp(format, fmt_param);
  167. if (format_parsed) {
  168. ast_rtp_codecs_payload_replace_format(codecs, num, format_parsed);
  169. ao2_ref(format_parsed, -1);
  170. }
  171. ao2_ref(format, -1);
  172. }
  173. }
  174. }
  175. if ((tel_event==0) && (session->endpoint->dtmf == AST_SIP_DTMF_AUTO)) {
  176. ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
  177. }
  178. /* Get the packetization, if it exists */
  179. if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) {
  180. unsigned long framing = pj_strtoul(pj_strltrim(&attr->value));
  181. if (framing && session->endpoint->media.rtp.use_ptime) {
  182. ast_rtp_codecs_set_framing(codecs, framing);
  183. }
  184. }
  185. }
  186. static int set_caps(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  187. const struct pjmedia_sdp_media *stream)
  188. {
  189. RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
  190. RAII_VAR(struct ast_format_cap *, peer, NULL, ao2_cleanup);
  191. RAII_VAR(struct ast_format_cap *, joint, NULL, ao2_cleanup);
  192. enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
  193. struct ast_rtp_codecs codecs = AST_RTP_CODECS_NULL_INIT;
  194. int fmts = 0;
  195. int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
  196. ast_format_cap_count(session->direct_media_cap);
  197. int dsp_features = 0;
  198. if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
  199. !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
  200. !(joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  201. ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type);
  202. return -1;
  203. }
  204. /* get the endpoint capabilities */
  205. if (direct_media_enabled) {
  206. ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
  207. format_cap_only_type(caps, media_type);
  208. } else {
  209. ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type);
  210. }
  211. /* get the capabilities on the peer */
  212. get_codecs(session, stream, &codecs, session_media);
  213. ast_rtp_codecs_payload_formats(&codecs, peer, &fmts);
  214. /* get the joint capabilities between peer and endpoint */
  215. ast_format_cap_get_compatible(caps, peer, joint);
  216. if (!ast_format_cap_count(joint)) {
  217. struct ast_str *usbuf = ast_str_alloca(256);
  218. struct ast_str *thembuf = ast_str_alloca(256);
  219. ast_rtp_codecs_payloads_destroy(&codecs);
  220. ast_log(LOG_NOTICE, "No joint capabilities for '%s' media stream between our configuration(%s) and incoming SDP(%s)\n",
  221. session_media->stream_type,
  222. ast_format_cap_get_names(caps, &usbuf),
  223. ast_format_cap_get_names(peer, &thembuf));
  224. return -1;
  225. }
  226. ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp),
  227. session_media->rtp);
  228. ast_format_cap_append_from_cap(session->req_caps, joint, AST_MEDIA_TYPE_UNKNOWN);
  229. if (session->channel) {
  230. ast_channel_lock(session->channel);
  231. ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_UNKNOWN);
  232. ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(session->channel),
  233. AST_MEDIA_TYPE_UNKNOWN);
  234. ast_format_cap_remove_by_type(caps, media_type);
  235. ast_format_cap_append_from_cap(caps, joint, media_type);
  236. /*
  237. * Apply the new formats to the channel, potentially changing
  238. * raw read/write formats and translation path while doing so.
  239. */
  240. ast_channel_nativeformats_set(session->channel, caps);
  241. if (media_type == AST_MEDIA_TYPE_AUDIO) {
  242. ast_set_read_format(session->channel, ast_channel_readformat(session->channel));
  243. ast_set_write_format(session->channel, ast_channel_writeformat(session->channel));
  244. }
  245. if ((session->endpoint->dtmf == AST_SIP_DTMF_AUTO)
  246. && (ast_rtp_instance_dtmf_mode_get(session_media->rtp) == AST_RTP_DTMF_MODE_RFC2833)
  247. && (session->dsp)) {
  248. dsp_features = ast_dsp_get_features(session->dsp);
  249. dsp_features &= ~DSP_FEATURE_DIGIT_DETECT;
  250. if (dsp_features) {
  251. ast_dsp_set_features(session->dsp, dsp_features);
  252. } else {
  253. ast_dsp_free(session->dsp);
  254. session->dsp = NULL;
  255. }
  256. }
  257. ast_channel_unlock(session->channel);
  258. }
  259. ast_rtp_codecs_payloads_destroy(&codecs);
  260. return 0;
  261. }
  262. static pjmedia_sdp_attr* generate_rtpmap_attr(pjmedia_sdp_media *media, pj_pool_t *pool, int rtp_code,
  263. int asterisk_format, struct ast_format *format, int code)
  264. {
  265. pjmedia_sdp_rtpmap rtpmap;
  266. pjmedia_sdp_attr *attr = NULL;
  267. char tmp[64];
  268. snprintf(tmp, sizeof(tmp), "%d", rtp_code);
  269. pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp);
  270. rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1];
  271. rtpmap.clock_rate = ast_rtp_lookup_sample_rate2(asterisk_format, format, code);
  272. pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, 0));
  273. rtpmap.param.slen = 0;
  274. rtpmap.param.ptr = NULL;
  275. pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr);
  276. return attr;
  277. }
  278. static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format *format, int rtp_code)
  279. {
  280. struct ast_str *fmtp0 = ast_str_alloca(256);
  281. pj_str_t fmtp1;
  282. pjmedia_sdp_attr *attr = NULL;
  283. char *tmp;
  284. ast_format_generate_sdp_fmtp(format, rtp_code, &fmtp0);
  285. if (ast_str_strlen(fmtp0)) {
  286. tmp = ast_str_buffer(fmtp0) + ast_str_strlen(fmtp0) - 1;
  287. /* remove any carriage return line feeds */
  288. while (*tmp == '\r' || *tmp == '\n') --tmp;
  289. *++tmp = '\0';
  290. /* ast...generate gives us everything, just need value */
  291. tmp = strchr(ast_str_buffer(fmtp0), ':');
  292. if (tmp && tmp + 1) {
  293. fmtp1 = pj_str(tmp + 1);
  294. } else {
  295. fmtp1 = pj_str(ast_str_buffer(fmtp0));
  296. }
  297. attr = pjmedia_sdp_attr_create(pool, "fmtp", &fmtp1);
  298. }
  299. return attr;
  300. }
  301. /*! \brief Function which adds ICE attributes to a media stream */
  302. static void add_ice_to_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media)
  303. {
  304. struct ast_rtp_engine_ice *ice;
  305. struct ao2_container *candidates;
  306. const char *username, *password;
  307. pj_str_t stmp;
  308. pjmedia_sdp_attr *attr;
  309. struct ao2_iterator it_candidates;
  310. struct ast_rtp_engine_ice_candidate *candidate;
  311. if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp)) ||
  312. !(candidates = ice->get_local_candidates(session_media->rtp))) {
  313. return;
  314. }
  315. if ((username = ice->get_ufrag(session_media->rtp))) {
  316. attr = pjmedia_sdp_attr_create(pool, "ice-ufrag", pj_cstr(&stmp, username));
  317. media->attr[media->attr_count++] = attr;
  318. }
  319. if ((password = ice->get_password(session_media->rtp))) {
  320. attr = pjmedia_sdp_attr_create(pool, "ice-pwd", pj_cstr(&stmp, password));
  321. media->attr[media->attr_count++] = attr;
  322. }
  323. it_candidates = ao2_iterator_init(candidates, 0);
  324. for (; (candidate = ao2_iterator_next(&it_candidates)); ao2_ref(candidate, -1)) {
  325. struct ast_str *attr_candidate = ast_str_create(128);
  326. ast_str_set(&attr_candidate, -1, "%s %u %s %d %s ", candidate->foundation, candidate->id, candidate->transport,
  327. candidate->priority, ast_sockaddr_stringify_addr_remote(&candidate->address));
  328. ast_str_append(&attr_candidate, -1, "%s typ ", ast_sockaddr_stringify_port(&candidate->address));
  329. switch (candidate->type) {
  330. case AST_RTP_ICE_CANDIDATE_TYPE_HOST:
  331. ast_str_append(&attr_candidate, -1, "host");
  332. break;
  333. case AST_RTP_ICE_CANDIDATE_TYPE_SRFLX:
  334. ast_str_append(&attr_candidate, -1, "srflx");
  335. break;
  336. case AST_RTP_ICE_CANDIDATE_TYPE_RELAYED:
  337. ast_str_append(&attr_candidate, -1, "relay");
  338. break;
  339. }
  340. if (!ast_sockaddr_isnull(&candidate->relay_address)) {
  341. ast_str_append(&attr_candidate, -1, " raddr %s rport", ast_sockaddr_stringify_addr_remote(&candidate->relay_address));
  342. ast_str_append(&attr_candidate, -1, " %s", ast_sockaddr_stringify_port(&candidate->relay_address));
  343. }
  344. attr = pjmedia_sdp_attr_create(pool, "candidate", pj_cstr(&stmp, ast_str_buffer(attr_candidate)));
  345. media->attr[media->attr_count++] = attr;
  346. ast_free(attr_candidate);
  347. }
  348. ao2_iterator_destroy(&it_candidates);
  349. ao2_ref(candidates, -1);
  350. }
  351. /*! \brief Function which processes ICE attributes in an audio stream */
  352. static void process_ice_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  353. const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
  354. {
  355. struct ast_rtp_engine_ice *ice;
  356. const pjmedia_sdp_attr *attr;
  357. char attr_value[256];
  358. unsigned int attr_i;
  359. /* If ICE support is not enabled or available exit early */
  360. if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
  361. return;
  362. }
  363. attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-ufrag", NULL);
  364. if (!attr) {
  365. attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-ufrag", NULL);
  366. }
  367. if (attr) {
  368. ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
  369. ice->set_authentication(session_media->rtp, attr_value, NULL);
  370. } else {
  371. return;
  372. }
  373. attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-pwd", NULL);
  374. if (!attr) {
  375. attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-pwd", NULL);
  376. }
  377. if (attr) {
  378. ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
  379. ice->set_authentication(session_media->rtp, NULL, attr_value);
  380. } else {
  381. return;
  382. }
  383. if (pjmedia_sdp_media_find_attr2(remote_stream, "ice-lite", NULL)) {
  384. ice->ice_lite(session_media->rtp);
  385. }
  386. /* Find all of the candidates */
  387. for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) {
  388. char foundation[32], transport[32], address[PJ_INET6_ADDRSTRLEN + 1], cand_type[6], relay_address[PJ_INET6_ADDRSTRLEN + 1] = "";
  389. unsigned int port, relay_port = 0;
  390. struct ast_rtp_engine_ice_candidate candidate = { 0, };
  391. attr = remote_stream->attr[attr_i];
  392. /* If this is not a candidate line skip it */
  393. if (pj_strcmp2(&attr->name, "candidate")) {
  394. continue;
  395. }
  396. ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
  397. if (sscanf(attr_value, "%31s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u", foundation, &candidate.id, transport,
  398. (unsigned *)&candidate.priority, address, &port, cand_type, relay_address, &relay_port) < 7) {
  399. /* Candidate did not parse properly */
  400. continue;
  401. }
  402. candidate.foundation = foundation;
  403. candidate.transport = transport;
  404. ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID);
  405. ast_sockaddr_set_port(&candidate.address, port);
  406. if (!strcasecmp(cand_type, "host")) {
  407. candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
  408. } else if (!strcasecmp(cand_type, "srflx")) {
  409. candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
  410. } else if (!strcasecmp(cand_type, "relay")) {
  411. candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
  412. } else {
  413. continue;
  414. }
  415. if (!ast_strlen_zero(relay_address)) {
  416. ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID);
  417. }
  418. if (relay_port) {
  419. ast_sockaddr_set_port(&candidate.relay_address, relay_port);
  420. }
  421. ice->add_remote_candidate(session_media->rtp, &candidate);
  422. }
  423. ice->set_role(session_media->rtp, pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_TRUE ?
  424. AST_RTP_ICE_ROLE_CONTROLLING : AST_RTP_ICE_ROLE_CONTROLLED);
  425. ice->start(session_media->rtp);
  426. }
  427. /*! \brief figure out if media stream has crypto lines for sdes */
  428. static int media_stream_has_crypto(const struct pjmedia_sdp_media *stream)
  429. {
  430. int i;
  431. for (i = 0; i < stream->attr_count; i++) {
  432. pjmedia_sdp_attr *attr;
  433. /* check the stream for the required crypto attribute */
  434. attr = stream->attr[i];
  435. if (pj_strcmp2(&attr->name, "crypto")) {
  436. continue;
  437. }
  438. return 1;
  439. }
  440. return 0;
  441. }
  442. /*! \brief figure out media transport encryption type from the media transport string */
  443. static enum ast_sip_session_media_encryption get_media_encryption_type(pj_str_t transport,
  444. const struct pjmedia_sdp_media *stream, unsigned int *optimistic)
  445. {
  446. RAII_VAR(char *, transport_str, ast_strndup(transport.ptr, transport.slen), ast_free);
  447. *optimistic = 0;
  448. if (strstr(transport_str, "UDP/TLS")) {
  449. return AST_SIP_MEDIA_ENCRYPT_DTLS;
  450. } else if (strstr(transport_str, "SAVP")) {
  451. return AST_SIP_MEDIA_ENCRYPT_SDES;
  452. } else if (media_stream_has_crypto(stream)) {
  453. *optimistic = 1;
  454. return AST_SIP_MEDIA_ENCRYPT_SDES;
  455. } else {
  456. return AST_SIP_MEDIA_ENCRYPT_NONE;
  457. }
  458. }
  459. /*!
  460. * \brief Checks whether the encryption offered in SDP is compatible with the endpoint's configuration
  461. * \internal
  462. *
  463. * \param endpoint_encryption Media encryption configured for the endpoint
  464. * \param stream pjmedia_sdp_media stream description
  465. *
  466. * \retval AST_SIP_MEDIA_TRANSPORT_INVALID on encryption mismatch
  467. * \retval The encryption requested in the SDP
  468. */
  469. static enum ast_sip_session_media_encryption check_endpoint_media_transport(
  470. struct ast_sip_endpoint *endpoint,
  471. const struct pjmedia_sdp_media *stream)
  472. {
  473. enum ast_sip_session_media_encryption incoming_encryption;
  474. char transport_end = stream->desc.transport.ptr[stream->desc.transport.slen - 1];
  475. unsigned int optimistic;
  476. if ((transport_end == 'F' && !endpoint->media.rtp.use_avpf)
  477. || (transport_end != 'F' && endpoint->media.rtp.use_avpf)) {
  478. return AST_SIP_MEDIA_TRANSPORT_INVALID;
  479. }
  480. incoming_encryption = get_media_encryption_type(stream->desc.transport, stream, &optimistic);
  481. if (incoming_encryption == endpoint->media.rtp.encryption) {
  482. return incoming_encryption;
  483. }
  484. if (endpoint->media.rtp.force_avp ||
  485. endpoint->media.rtp.encryption_optimistic) {
  486. return incoming_encryption;
  487. }
  488. /* If an optimistic offer has been made but encryption is not enabled consider it as having
  489. * no offer of crypto at all instead of invalid so the session proceeds.
  490. */
  491. if (optimistic) {
  492. return AST_SIP_MEDIA_ENCRYPT_NONE;
  493. }
  494. return AST_SIP_MEDIA_TRANSPORT_INVALID;
  495. }
  496. static int setup_srtp(struct ast_sip_session_media *session_media)
  497. {
  498. if (!session_media->srtp) {
  499. session_media->srtp = ast_sdp_srtp_alloc();
  500. if (!session_media->srtp) {
  501. return -1;
  502. }
  503. }
  504. if (!session_media->srtp->crypto) {
  505. session_media->srtp->crypto = ast_sdp_crypto_alloc();
  506. if (!session_media->srtp->crypto) {
  507. return -1;
  508. }
  509. }
  510. return 0;
  511. }
  512. static int setup_dtls_srtp(struct ast_sip_session *session,
  513. struct ast_sip_session_media *session_media)
  514. {
  515. struct ast_rtp_engine_dtls *dtls;
  516. if (!session->endpoint->media.rtp.dtls_cfg.enabled || !session_media->rtp) {
  517. return -1;
  518. }
  519. dtls = ast_rtp_instance_get_dtls(session_media->rtp);
  520. if (!dtls) {
  521. return -1;
  522. }
  523. session->endpoint->media.rtp.dtls_cfg.suite = ((session->endpoint->media.rtp.srtp_tag_32) ? AST_AES_CM_128_HMAC_SHA1_32 : AST_AES_CM_128_HMAC_SHA1_80);
  524. if (dtls->set_configuration(session_media->rtp, &session->endpoint->media.rtp.dtls_cfg)) {
  525. ast_log(LOG_ERROR, "Attempted to set an invalid DTLS-SRTP configuration on RTP instance '%p'\n",
  526. session_media->rtp);
  527. return -1;
  528. }
  529. if (setup_srtp(session_media)) {
  530. return -1;
  531. }
  532. return 0;
  533. }
  534. static void apply_dtls_attrib(struct ast_sip_session_media *session_media,
  535. pjmedia_sdp_attr *attr)
  536. {
  537. struct ast_rtp_engine_dtls *dtls = ast_rtp_instance_get_dtls(session_media->rtp);
  538. pj_str_t *value;
  539. if (!attr->value.ptr) {
  540. return;
  541. }
  542. value = pj_strtrim(&attr->value);
  543. if (!pj_strcmp2(&attr->name, "setup")) {
  544. if (!pj_stricmp2(value, "active")) {
  545. dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTIVE);
  546. } else if (!pj_stricmp2(value, "passive")) {
  547. dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_PASSIVE);
  548. } else if (!pj_stricmp2(value, "actpass")) {
  549. dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTPASS);
  550. } else if (!pj_stricmp2(value, "holdconn")) {
  551. dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_HOLDCONN);
  552. } else {
  553. ast_log(LOG_WARNING, "Unsupported setup attribute value '%*s'\n", (int)value->slen, value->ptr);
  554. }
  555. } else if (!pj_strcmp2(&attr->name, "connection")) {
  556. if (!pj_stricmp2(value, "new")) {
  557. dtls->reset(session_media->rtp);
  558. } else if (!pj_stricmp2(value, "existing")) {
  559. /* Do nothing */
  560. } else {
  561. ast_log(LOG_WARNING, "Unsupported connection attribute value '%*s'\n", (int)value->slen, value->ptr);
  562. }
  563. } else if (!pj_strcmp2(&attr->name, "fingerprint")) {
  564. char hash_value[256], hash[32];
  565. char fingerprint_text[value->slen + 1];
  566. ast_copy_pj_str(fingerprint_text, value, sizeof(fingerprint_text));
  567. if (sscanf(fingerprint_text, "%31s %255s", hash, hash_value) == 2) {
  568. if (!strcasecmp(hash, "sha-1")) {
  569. dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA1, hash_value);
  570. } else if (!strcasecmp(hash, "sha-256")) {
  571. dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA256, hash_value);
  572. } else {
  573. ast_log(LOG_WARNING, "Unsupported fingerprint hash type '%s'\n",
  574. hash);
  575. }
  576. }
  577. }
  578. }
  579. static int parse_dtls_attrib(struct ast_sip_session_media *session_media,
  580. const struct pjmedia_sdp_session *sdp,
  581. const struct pjmedia_sdp_media *stream)
  582. {
  583. int i;
  584. for (i = 0; i < sdp->attr_count; i++) {
  585. apply_dtls_attrib(session_media, sdp->attr[i]);
  586. }
  587. for (i = 0; i < stream->attr_count; i++) {
  588. apply_dtls_attrib(session_media, stream->attr[i]);
  589. }
  590. ast_set_flag(session_media->srtp, AST_SRTP_CRYPTO_OFFER_OK);
  591. return 0;
  592. }
  593. static int setup_sdes_srtp(struct ast_sip_session_media *session_media,
  594. const struct pjmedia_sdp_media *stream)
  595. {
  596. int i;
  597. for (i = 0; i < stream->attr_count; i++) {
  598. pjmedia_sdp_attr *attr;
  599. RAII_VAR(char *, crypto_str, NULL, ast_free);
  600. /* check the stream for the required crypto attribute */
  601. attr = stream->attr[i];
  602. if (pj_strcmp2(&attr->name, "crypto")) {
  603. continue;
  604. }
  605. crypto_str = ast_strndup(attr->value.ptr, attr->value.slen);
  606. if (!crypto_str) {
  607. return -1;
  608. }
  609. if (setup_srtp(session_media)) {
  610. return -1;
  611. }
  612. if (!ast_sdp_crypto_process(session_media->rtp, session_media->srtp, crypto_str)) {
  613. /* found a valid crypto attribute */
  614. return 0;
  615. }
  616. ast_debug(1, "Ignoring crypto offer with unsupported parameters: %s\n", crypto_str);
  617. }
  618. /* no usable crypto attributes found */
  619. return -1;
  620. }
  621. static int setup_media_encryption(struct ast_sip_session *session,
  622. struct ast_sip_session_media *session_media,
  623. const struct pjmedia_sdp_session *sdp,
  624. const struct pjmedia_sdp_media *stream)
  625. {
  626. switch (session_media->encryption) {
  627. case AST_SIP_MEDIA_ENCRYPT_SDES:
  628. if (setup_sdes_srtp(session_media, stream)) {
  629. return -1;
  630. }
  631. break;
  632. case AST_SIP_MEDIA_ENCRYPT_DTLS:
  633. if (setup_dtls_srtp(session, session_media)) {
  634. return -1;
  635. }
  636. if (parse_dtls_attrib(session_media, sdp, stream)) {
  637. return -1;
  638. }
  639. break;
  640. case AST_SIP_MEDIA_TRANSPORT_INVALID:
  641. case AST_SIP_MEDIA_ENCRYPT_NONE:
  642. break;
  643. }
  644. return 0;
  645. }
  646. /*! \brief Function which negotiates an incoming media stream */
  647. static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  648. const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream)
  649. {
  650. char host[NI_MAXHOST];
  651. RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
  652. enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
  653. enum ast_sip_session_media_encryption encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
  654. int res;
  655. /* If port is 0, ignore this media stream */
  656. if (!stream->desc.port) {
  657. ast_debug(3, "Media stream '%s' is already declined\n", session_media->stream_type);
  658. return 0;
  659. }
  660. /* If no type formats have been configured reject this stream */
  661. if (!ast_format_cap_has_type(session->endpoint->media.codecs, media_type)) {
  662. ast_debug(3, "Endpoint has no codecs for media type '%s', declining stream\n", session_media->stream_type);
  663. return 0;
  664. }
  665. /* Ensure incoming transport is compatible with the endpoint's configuration */
  666. if (!session->endpoint->media.rtp.use_received_transport) {
  667. encryption = check_endpoint_media_transport(session->endpoint, stream);
  668. if (encryption == AST_SIP_MEDIA_TRANSPORT_INVALID) {
  669. return -1;
  670. }
  671. }
  672. ast_copy_pj_str(host, stream->conn ? &stream->conn->addr : &sdp->conn->addr, sizeof(host));
  673. /* Ensure that the address provided is valid */
  674. if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
  675. /* The provided host was actually invalid so we error out this negotiation */
  676. return -1;
  677. }
  678. /* Using the connection information create an appropriate RTP instance */
  679. if (!session_media->rtp && create_rtp(session, session_media, ast_sockaddr_is_ipv6(addrs))) {
  680. return -1;
  681. }
  682. res = setup_media_encryption(session, session_media, sdp, stream);
  683. if (res) {
  684. if (!session->endpoint->media.rtp.encryption_optimistic) {
  685. /* If optimistic encryption is disabled and crypto should have been enabled
  686. * but was not this session must fail.
  687. */
  688. return -1;
  689. }
  690. /* There is no encryption, sad. */
  691. session_media->encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
  692. }
  693. /* If we've been explicitly configured to use the received transport OR if
  694. * encryption is on and crypto is present use the received transport.
  695. * This is done in case of optimistic because it may come in as RTP/AVP or RTP/SAVP depending
  696. * on the configuration of the remote endpoint (optimistic themselves or mandatory).
  697. */
  698. if ((session->endpoint->media.rtp.use_received_transport) ||
  699. ((encryption == AST_SIP_MEDIA_ENCRYPT_SDES) && !res)) {
  700. pj_strdup(session->inv_session->pool, &session_media->transport, &stream->desc.transport);
  701. }
  702. if (set_caps(session, session_media, stream)) {
  703. return 0;
  704. }
  705. return 1;
  706. }
  707. static int add_crypto_to_stream(struct ast_sip_session *session,
  708. struct ast_sip_session_media *session_media,
  709. pj_pool_t *pool, pjmedia_sdp_media *media)
  710. {
  711. pj_str_t stmp;
  712. pjmedia_sdp_attr *attr;
  713. enum ast_rtp_dtls_hash hash;
  714. const char *crypto_attribute;
  715. struct ast_rtp_engine_dtls *dtls;
  716. static const pj_str_t STR_NEW = { "new", 3 };
  717. static const pj_str_t STR_EXISTING = { "existing", 8 };
  718. static const pj_str_t STR_ACTIVE = { "active", 6 };
  719. static const pj_str_t STR_PASSIVE = { "passive", 7 };
  720. static const pj_str_t STR_ACTPASS = { "actpass", 7 };
  721. static const pj_str_t STR_HOLDCONN = { "holdconn", 8 };
  722. switch (session_media->encryption) {
  723. case AST_SIP_MEDIA_ENCRYPT_NONE:
  724. case AST_SIP_MEDIA_TRANSPORT_INVALID:
  725. break;
  726. case AST_SIP_MEDIA_ENCRYPT_SDES:
  727. if (!session_media->srtp) {
  728. session_media->srtp = ast_sdp_srtp_alloc();
  729. if (!session_media->srtp) {
  730. return -1;
  731. }
  732. }
  733. crypto_attribute = ast_sdp_srtp_get_attrib(session_media->srtp,
  734. 0 /* DTLS running? No */,
  735. session->endpoint->media.rtp.srtp_tag_32 /* 32 byte tag length? */);
  736. if (!crypto_attribute) {
  737. /* No crypto attribute to add, bad news */
  738. return -1;
  739. }
  740. attr = pjmedia_sdp_attr_create(pool, "crypto", pj_cstr(&stmp, crypto_attribute));
  741. media->attr[media->attr_count++] = attr;
  742. break;
  743. case AST_SIP_MEDIA_ENCRYPT_DTLS:
  744. if (setup_dtls_srtp(session, session_media)) {
  745. return -1;
  746. }
  747. dtls = ast_rtp_instance_get_dtls(session_media->rtp);
  748. if (!dtls) {
  749. return -1;
  750. }
  751. switch (dtls->get_connection(session_media->rtp)) {
  752. case AST_RTP_DTLS_CONNECTION_NEW:
  753. attr = pjmedia_sdp_attr_create(pool, "connection", &STR_NEW);
  754. media->attr[media->attr_count++] = attr;
  755. break;
  756. case AST_RTP_DTLS_CONNECTION_EXISTING:
  757. attr = pjmedia_sdp_attr_create(pool, "connection", &STR_EXISTING);
  758. media->attr[media->attr_count++] = attr;
  759. break;
  760. default:
  761. break;
  762. }
  763. switch (dtls->get_setup(session_media->rtp)) {
  764. case AST_RTP_DTLS_SETUP_ACTIVE:
  765. attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTIVE);
  766. media->attr[media->attr_count++] = attr;
  767. break;
  768. case AST_RTP_DTLS_SETUP_PASSIVE:
  769. attr = pjmedia_sdp_attr_create(pool, "setup", &STR_PASSIVE);
  770. media->attr[media->attr_count++] = attr;
  771. break;
  772. case AST_RTP_DTLS_SETUP_ACTPASS:
  773. attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTPASS);
  774. media->attr[media->attr_count++] = attr;
  775. break;
  776. case AST_RTP_DTLS_SETUP_HOLDCONN:
  777. attr = pjmedia_sdp_attr_create(pool, "setup", &STR_HOLDCONN);
  778. media->attr[media->attr_count++] = attr;
  779. break;
  780. default:
  781. break;
  782. }
  783. hash = dtls->get_fingerprint_hash(session_media->rtp);
  784. crypto_attribute = dtls->get_fingerprint(session_media->rtp);
  785. if (crypto_attribute && (hash == AST_RTP_DTLS_HASH_SHA1 || hash == AST_RTP_DTLS_HASH_SHA256)) {
  786. RAII_VAR(struct ast_str *, fingerprint, ast_str_create(64), ast_free);
  787. if (!fingerprint) {
  788. return -1;
  789. }
  790. if (hash == AST_RTP_DTLS_HASH_SHA1) {
  791. ast_str_set(&fingerprint, 0, "SHA-1 %s", crypto_attribute);
  792. } else {
  793. ast_str_set(&fingerprint, 0, "SHA-256 %s", crypto_attribute);
  794. }
  795. attr = pjmedia_sdp_attr_create(pool, "fingerprint", pj_cstr(&stmp, ast_str_buffer(fingerprint)));
  796. media->attr[media->attr_count++] = attr;
  797. }
  798. break;
  799. }
  800. return 0;
  801. }
  802. /*! \brief Function which creates an outgoing stream */
  803. static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  804. struct pjmedia_sdp_session *sdp)
  805. {
  806. pj_pool_t *pool = session->inv_session->pool_prov;
  807. static const pj_str_t STR_IN = { "IN", 2 };
  808. static const pj_str_t STR_IP4 = { "IP4", 3};
  809. static const pj_str_t STR_IP6 = { "IP6", 3};
  810. static const pj_str_t STR_SENDRECV = { "sendrecv", 8 };
  811. static const pj_str_t STR_SENDONLY = { "sendonly", 8 };
  812. pjmedia_sdp_media *media;
  813. char hostip[PJ_INET6_ADDRSTRLEN+2];
  814. struct ast_sockaddr addr;
  815. char tmp[512];
  816. pj_str_t stmp;
  817. pjmedia_sdp_attr *attr;
  818. int index = 0;
  819. int noncodec = (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733 || session->endpoint->dtmf == AST_SIP_DTMF_AUTO) ? AST_RTP_DTMF : 0;
  820. int min_packet_size = 0, max_packet_size = 0;
  821. int rtp_code;
  822. RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
  823. enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
  824. int use_override_prefs = ast_format_cap_count(session->req_caps);
  825. int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
  826. ast_format_cap_count(session->direct_media_cap);
  827. if ((use_override_prefs && !ast_format_cap_has_type(session->req_caps, media_type)) ||
  828. (!use_override_prefs && !ast_format_cap_has_type(session->endpoint->media.codecs, media_type))) {
  829. /* If no type formats are configured don't add a stream */
  830. return 0;
  831. } else if (!session_media->rtp && create_rtp(session, session_media, session->endpoint->media.rtp.ipv6)) {
  832. return -1;
  833. }
  834. if (!(media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media))) ||
  835. !(media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn)))) {
  836. return -1;
  837. }
  838. if (add_crypto_to_stream(session, session_media, pool, media)) {
  839. return -1;
  840. }
  841. media->desc.media = pj_str(session_media->stream_type);
  842. if (pj_strlen(&session_media->transport)) {
  843. /* If a transport has already been specified use it */
  844. media->desc.transport = session_media->transport;
  845. } else {
  846. media->desc.transport = pj_str(ast_sdp_get_rtp_profile(
  847. /* Optimistic encryption places crypto in the normal RTP/AVP profile */
  848. !session->endpoint->media.rtp.encryption_optimistic &&
  849. (session_media->encryption == AST_SIP_MEDIA_ENCRYPT_SDES),
  850. session_media->rtp, session->endpoint->media.rtp.use_avpf,
  851. session->endpoint->media.rtp.force_avp));
  852. }
  853. /* Add connection level details */
  854. if (direct_media_enabled) {
  855. ast_copy_string(hostip, ast_sockaddr_stringify_fmt(&session_media->direct_media_addr, AST_SOCKADDR_STR_ADDR), sizeof(hostip));
  856. } else if (ast_strlen_zero(session->endpoint->media.address)) {
  857. pj_sockaddr localaddr;
  858. if (pj_gethostip(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET(), &localaddr)) {
  859. return -1;
  860. }
  861. pj_sockaddr_print(&localaddr, hostip, sizeof(hostip), 2);
  862. } else {
  863. ast_copy_string(hostip, session->endpoint->media.address, sizeof(hostip));
  864. }
  865. media->conn->net_type = STR_IN;
  866. media->conn->addr_type = session->endpoint->media.rtp.ipv6 ? STR_IP6 : STR_IP4;
  867. pj_strdup2(pool, &media->conn->addr, hostip);
  868. ast_rtp_instance_get_local_address(session_media->rtp, &addr);
  869. media->desc.port = direct_media_enabled ? ast_sockaddr_port(&session_media->direct_media_addr) : (pj_uint16_t) ast_sockaddr_port(&addr);
  870. media->desc.port_count = 1;
  871. /* Add ICE attributes and candidates */
  872. add_ice_to_stream(session, session_media, pool, media);
  873. if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  874. ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type);
  875. return -1;
  876. }
  877. if (direct_media_enabled) {
  878. ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
  879. } else if (!ast_format_cap_count(session->req_caps) ||
  880. !ast_format_cap_iscompatible(session->req_caps, session->endpoint->media.codecs)) {
  881. ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type);
  882. } else {
  883. ast_format_cap_append_from_cap(caps, session->req_caps, media_type);
  884. }
  885. for (index = 0; index < ast_format_cap_count(caps); ++index) {
  886. struct ast_format *format = ast_format_cap_get_format(caps, index);
  887. if (ast_format_get_type(format) != media_type) {
  888. ao2_ref(format, -1);
  889. continue;
  890. }
  891. if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0)) == -1) {
  892. ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n", ast_format_get_name(format));
  893. ao2_ref(format, -1);
  894. continue;
  895. }
  896. if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 1, format, 0))) {
  897. ao2_ref(format, -1);
  898. continue;
  899. }
  900. media->attr[media->attr_count++] = attr;
  901. if ((attr = generate_fmtp_attr(pool, format, rtp_code))) {
  902. media->attr[media->attr_count++] = attr;
  903. }
  904. if (ast_format_get_maximum_ms(format) &&
  905. ((ast_format_get_maximum_ms(format) < max_packet_size) || !max_packet_size)) {
  906. max_packet_size = ast_format_get_maximum_ms(format);
  907. }
  908. ao2_ref(format, -1);
  909. }
  910. /* Add non-codec formats */
  911. if (media_type != AST_MEDIA_TYPE_VIDEO) {
  912. for (index = 1LL; index <= AST_RTP_MAX; index <<= 1) {
  913. if (!(noncodec & index) || (rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp),
  914. 0, NULL, index)) == -1) {
  915. continue;
  916. }
  917. if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 0, NULL, index))) {
  918. continue;
  919. }
  920. media->attr[media->attr_count++] = attr;
  921. if (index == AST_RTP_DTMF) {
  922. snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
  923. attr = pjmedia_sdp_attr_create(pool, "fmtp", pj_cstr(&stmp, tmp));
  924. media->attr[media->attr_count++] = attr;
  925. }
  926. }
  927. }
  928. /* If no formats were actually added to the media stream don't add it to the SDP */
  929. if (!media->desc.fmt_count) {
  930. return 1;
  931. }
  932. /* If ptime is set add it as an attribute */
  933. min_packet_size = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(session_media->rtp));
  934. if (!min_packet_size) {
  935. min_packet_size = ast_format_cap_get_framing(caps);
  936. }
  937. if (min_packet_size) {
  938. snprintf(tmp, sizeof(tmp), "%d", min_packet_size);
  939. attr = pjmedia_sdp_attr_create(pool, "ptime", pj_cstr(&stmp, tmp));
  940. media->attr[media->attr_count++] = attr;
  941. }
  942. if (max_packet_size) {
  943. snprintf(tmp, sizeof(tmp), "%d", max_packet_size);
  944. attr = pjmedia_sdp_attr_create(pool, "maxptime", pj_cstr(&stmp, tmp));
  945. media->attr[media->attr_count++] = attr;
  946. }
  947. /* Add the sendrecv attribute - we purposely don't keep track because pjmedia-sdp will automatically change our offer for us */
  948. attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr);
  949. attr->name = !session_media->locally_held ? STR_SENDRECV : STR_SENDONLY;
  950. media->attr[media->attr_count++] = attr;
  951. /* Add the media stream to the SDP */
  952. sdp->media[sdp->media_count++] = media;
  953. return 1;
  954. }
  955. static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  956. const struct pjmedia_sdp_session *local, const struct pjmedia_sdp_media *local_stream,
  957. const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
  958. {
  959. RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
  960. enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
  961. char host[NI_MAXHOST];
  962. int fdno, res;
  963. if (!session->channel) {
  964. return 1;
  965. }
  966. if (!local_stream->desc.port || !remote_stream->desc.port) {
  967. return 1;
  968. }
  969. /* Ensure incoming transport is compatible with the endpoint's configuration */
  970. if (!session->endpoint->media.rtp.use_received_transport &&
  971. check_endpoint_media_transport(session->endpoint, remote_stream) == AST_SIP_MEDIA_TRANSPORT_INVALID) {
  972. return -1;
  973. }
  974. /* Create an RTP instance if need be */
  975. if (!session_media->rtp && create_rtp(session, session_media, session->endpoint->media.rtp.ipv6)) {
  976. return -1;
  977. }
  978. res = setup_media_encryption(session, session_media, remote, remote_stream);
  979. if (!session->endpoint->media.rtp.encryption_optimistic && res) {
  980. /* If optimistic encryption is disabled and crypto should have been enabled but was not
  981. * this session must fail.
  982. */
  983. return -1;
  984. }
  985. if (!remote_stream->conn && !remote->conn) {
  986. return 1;
  987. }
  988. ast_copy_pj_str(host, remote_stream->conn ? &remote_stream->conn->addr : &remote->conn->addr, sizeof(host));
  989. /* Ensure that the address provided is valid */
  990. if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
  991. /* The provided host was actually invalid so we error out this negotiation */
  992. return -1;
  993. }
  994. /* Apply connection information to the RTP instance */
  995. ast_sockaddr_set_port(addrs, remote_stream->desc.port);
  996. ast_rtp_instance_set_remote_address(session_media->rtp, addrs);
  997. if (set_caps(session, session_media, local_stream)) {
  998. return 1;
  999. }
  1000. if ((fdno = media_type_to_fdno(media_type)) < 0) {
  1001. return -1;
  1002. }
  1003. ast_channel_set_fd(session->channel, fdno, ast_rtp_instance_fd(session_media->rtp, 0));
  1004. ast_channel_set_fd(session->channel, fdno + 1, ast_rtp_instance_fd(session_media->rtp, 1));
  1005. /* If ICE support is enabled find all the needed attributes */
  1006. process_ice_attributes(session, session_media, remote, remote_stream);
  1007. /* Ensure the RTP instance is active */
  1008. ast_rtp_instance_activate(session_media->rtp);
  1009. /* audio stream handles music on hold */
  1010. if (media_type != AST_MEDIA_TYPE_AUDIO) {
  1011. if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE)
  1012. && (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) {
  1013. ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER);
  1014. }
  1015. return 1;
  1016. }
  1017. if (ast_sockaddr_isnull(addrs) ||
  1018. ast_sockaddr_is_any(addrs) ||
  1019. pjmedia_sdp_media_find_attr2(remote_stream, "sendonly", NULL) ||
  1020. pjmedia_sdp_media_find_attr2(remote_stream, "inactive", NULL)) {
  1021. if (!session_media->remotely_held) {
  1022. /* The remote side has put us on hold */
  1023. ast_queue_hold(session->channel, session->endpoint->mohsuggest);
  1024. ast_rtp_instance_stop(session_media->rtp);
  1025. ast_queue_frame(session->channel, &ast_null_frame);
  1026. session_media->remotely_held = 1;
  1027. }
  1028. } else if (session_media->remotely_held) {
  1029. /* The remote side has taken us off hold */
  1030. ast_queue_unhold(session->channel);
  1031. ast_queue_frame(session->channel, &ast_null_frame);
  1032. session_media->remotely_held = 0;
  1033. } else if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE)
  1034. && (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) {
  1035. ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER);
  1036. }
  1037. /* This purposely resets the encryption to the configured in case it gets added later */
  1038. session_media->encryption = session->endpoint->media.rtp.encryption;
  1039. return 1;
  1040. }
  1041. /*! \brief Function which updates the media stream with external media address, if applicable */
  1042. static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport)
  1043. {
  1044. char host[NI_MAXHOST];
  1045. struct ast_sockaddr addr = { { 0, } };
  1046. /* If the stream has been rejected there will be no connection line */
  1047. if (!stream->conn) {
  1048. return;
  1049. }
  1050. ast_copy_pj_str(host, &stream->conn->addr, sizeof(host));
  1051. ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID);
  1052. /* Is the address within the SDP inside the same network? */
  1053. if (ast_apply_ha(transport->localnet, &addr) == AST_SENSE_ALLOW) {
  1054. return;
  1055. }
  1056. pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address);
  1057. }
  1058. /*! \brief Function which destroys the RTP instance when session ends */
  1059. static void stream_destroy(struct ast_sip_session_media *session_media)
  1060. {
  1061. if (session_media->rtp) {
  1062. ast_rtp_instance_stop(session_media->rtp);
  1063. ast_rtp_instance_destroy(session_media->rtp);
  1064. }
  1065. session_media->rtp = NULL;
  1066. }
  1067. /*! \brief SDP handler for 'audio' media stream */
  1068. static struct ast_sip_session_sdp_handler audio_sdp_handler = {
  1069. .id = STR_AUDIO,
  1070. .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
  1071. .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
  1072. .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
  1073. .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
  1074. .stream_destroy = stream_destroy,
  1075. };
  1076. /*! \brief SDP handler for 'video' media stream */
  1077. static struct ast_sip_session_sdp_handler video_sdp_handler = {
  1078. .id = STR_VIDEO,
  1079. .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
  1080. .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
  1081. .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
  1082. .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
  1083. .stream_destroy = stream_destroy,
  1084. };
  1085. static int video_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  1086. {
  1087. struct pjsip_transaction *tsx;
  1088. pjsip_tx_data *tdata;
  1089. if (!session->channel
  1090. || !ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
  1091. "application",
  1092. "media_control+xml")) {
  1093. return 0;
  1094. }
  1095. tsx = pjsip_rdata_get_tsx(rdata);
  1096. ast_queue_control(session->channel, AST_CONTROL_VIDUPDATE);
  1097. if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, 200, NULL, &tdata) == PJ_SUCCESS) {
  1098. pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
  1099. }
  1100. return 0;
  1101. }
  1102. static struct ast_sip_session_supplement video_info_supplement = {
  1103. .method = "INFO",
  1104. .incoming_request = video_info_incoming_request,
  1105. };
  1106. /*! \brief Unloads the sdp RTP/AVP module from Asterisk */
  1107. static int unload_module(void)
  1108. {
  1109. ast_sip_session_unregister_supplement(&video_info_supplement);
  1110. ast_sip_session_unregister_sdp_handler(&video_sdp_handler, STR_VIDEO);
  1111. ast_sip_session_unregister_sdp_handler(&audio_sdp_handler, STR_AUDIO);
  1112. if (sched) {
  1113. ast_sched_context_destroy(sched);
  1114. }
  1115. return 0;
  1116. }
  1117. /*!
  1118. * \brief Load the module
  1119. *
  1120. * Module loading including tests for configuration or dependencies.
  1121. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1122. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1123. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1124. * configuration file or other non-critical problem return
  1125. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1126. */
  1127. static int load_module(void)
  1128. {
  1129. CHECK_PJSIP_SESSION_MODULE_LOADED();
  1130. ast_sockaddr_parse(&address_ipv4, "0.0.0.0", 0);
  1131. ast_sockaddr_parse(&address_ipv6, "::", 0);
  1132. if (!(sched = ast_sched_context_create())) {
  1133. ast_log(LOG_ERROR, "Unable to create scheduler context.\n");
  1134. goto end;
  1135. }
  1136. if (ast_sched_start_thread(sched)) {
  1137. ast_log(LOG_ERROR, "Unable to create scheduler context thread.\n");
  1138. goto end;
  1139. }
  1140. if (ast_sip_session_register_sdp_handler(&audio_sdp_handler, STR_AUDIO)) {
  1141. ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_AUDIO);
  1142. goto end;
  1143. }
  1144. if (ast_sip_session_register_sdp_handler(&video_sdp_handler, STR_VIDEO)) {
  1145. ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_VIDEO);
  1146. goto end;
  1147. }
  1148. ast_sip_session_register_supplement(&video_info_supplement);
  1149. return AST_MODULE_LOAD_SUCCESS;
  1150. end:
  1151. unload_module();
  1152. return AST_MODULE_LOAD_FAILURE;
  1153. }
  1154. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP SDP RTP/AVP stream handler",
  1155. .support_level = AST_MODULE_SUPPORT_CORE,
  1156. .load = load_module,
  1157. .unload = unload_module,
  1158. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  1159. );