res_pjsip_sdp_rtp.c 42 KB

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