res_pjsip_sdp_rtp.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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/utils.h"
  49. #include "asterisk/res_pjsip.h"
  50. #include "asterisk/res_pjsip_session.h"
  51. /*! \brief Scheduler for RTCP purposes */
  52. static struct ast_sched_context *sched;
  53. /*! \brief Address for RTP */
  54. static struct ast_sockaddr address_rtp;
  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. static int send_keepalive(const void *data)
  93. {
  94. struct ast_sip_session_media *session_media = (struct ast_sip_session_media *) data;
  95. struct ast_rtp_instance *rtp = session_media->rtp;
  96. int keepalive;
  97. time_t interval;
  98. int send_keepalive;
  99. if (!rtp) {
  100. return 0;
  101. }
  102. keepalive = ast_rtp_instance_get_keepalive(rtp);
  103. if (!ast_sockaddr_isnull(&session_media->direct_media_addr)) {
  104. ast_debug(3, "Not sending RTP keepalive on RTP instance %p since direct media is in use\n", rtp);
  105. return keepalive * 1000;
  106. }
  107. interval = time(NULL) - ast_rtp_instance_get_last_tx(rtp);
  108. send_keepalive = interval >= keepalive;
  109. ast_debug(3, "It has been %d seconds since RTP was last sent on instance %p. %sending keepalive\n",
  110. (int) interval, rtp, send_keepalive ? "S" : "Not s");
  111. if (send_keepalive) {
  112. ast_rtp_instance_sendcng(rtp, 0);
  113. return keepalive * 1000;
  114. }
  115. return (keepalive - interval) * 1000;
  116. }
  117. /*! \brief Check whether RTP is being received or not */
  118. static int rtp_check_timeout(const void *data)
  119. {
  120. struct ast_sip_session_media *session_media = (struct ast_sip_session_media *)data;
  121. struct ast_rtp_instance *rtp = session_media->rtp;
  122. int elapsed;
  123. struct ast_channel *chan;
  124. if (!rtp) {
  125. return 0;
  126. }
  127. elapsed = time(NULL) - ast_rtp_instance_get_last_rx(rtp);
  128. if (elapsed < ast_rtp_instance_get_timeout(rtp)) {
  129. return (ast_rtp_instance_get_timeout(rtp) - elapsed) * 1000;
  130. }
  131. chan = ast_channel_get_by_name(ast_rtp_instance_get_channel_id(rtp));
  132. if (!chan) {
  133. return 0;
  134. }
  135. ast_log(LOG_NOTICE, "Disconnecting channel '%s' for lack of RTP activity in %d seconds\n",
  136. ast_channel_name(chan), elapsed);
  137. ast_channel_lock(chan);
  138. ast_channel_hangupcause_set(chan, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
  139. ast_channel_unlock(chan);
  140. ast_softhangup(chan, AST_SOFTHANGUP_DEV);
  141. ast_channel_unref(chan);
  142. return 0;
  143. }
  144. /*!
  145. * \brief Enable RTCP on an RTP session.
  146. */
  147. static void enable_rtcp(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  148. const struct pjmedia_sdp_media *remote_media)
  149. {
  150. enum ast_rtp_instance_rtcp rtcp_type;
  151. if (session->endpoint->rtcp_mux && session_media->remote_rtcp_mux) {
  152. rtcp_type = AST_RTP_INSTANCE_RTCP_MUX;
  153. } else {
  154. rtcp_type = AST_RTP_INSTANCE_RTCP_STANDARD;
  155. }
  156. ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, rtcp_type);
  157. }
  158. /*! \brief Internal function which creates an RTP instance */
  159. static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
  160. {
  161. struct ast_rtp_engine_ice *ice;
  162. struct ast_sockaddr temp_media_address;
  163. struct ast_sockaddr *media_address = &address_rtp;
  164. if (session->endpoint->media.bind_rtp_to_media_address && !ast_strlen_zero(session->endpoint->media.address)) {
  165. if (ast_sockaddr_parse(&temp_media_address, session->endpoint->media.address, 0)) {
  166. ast_debug(1, "Endpoint %s: Binding RTP media to %s\n",
  167. ast_sorcery_object_get_id(session->endpoint),
  168. session->endpoint->media.address);
  169. media_address = &temp_media_address;
  170. } else {
  171. ast_debug(1, "Endpoint %s: RTP media address invalid: %s\n",
  172. ast_sorcery_object_get_id(session->endpoint),
  173. session->endpoint->media.address);
  174. }
  175. } else {
  176. struct ast_sip_transport *transport;
  177. transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
  178. session->endpoint->transport);
  179. if (transport) {
  180. struct ast_sip_transport_state *trans_state;
  181. trans_state = ast_sip_get_transport_state(ast_sorcery_object_get_id(transport));
  182. if (trans_state) {
  183. char hoststr[PJ_INET6_ADDRSTRLEN];
  184. pj_sockaddr_print(&trans_state->host, hoststr, sizeof(hoststr), 0);
  185. if (ast_sockaddr_parse(&temp_media_address, hoststr, 0)) {
  186. ast_debug(1, "Transport %s bound to %s: Using it for RTP media.\n",
  187. session->endpoint->transport, hoststr);
  188. media_address = &temp_media_address;
  189. } else {
  190. ast_debug(1, "Transport %s bound to %s: Invalid for RTP media.\n",
  191. session->endpoint->transport, hoststr);
  192. }
  193. ao2_ref(trans_state, -1);
  194. }
  195. ao2_ref(transport, -1);
  196. }
  197. }
  198. if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, media_address, NULL))) {
  199. ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n", session->endpoint->media.rtp.engine);
  200. return -1;
  201. }
  202. ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric);
  203. ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_ASYMMETRIC_CODEC, session->endpoint->asymmetric_rtp_codec);
  204. if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) {
  205. ice->stop(session_media->rtp);
  206. }
  207. if (session->dtmf == AST_SIP_DTMF_RFC_4733 || session->dtmf == AST_SIP_DTMF_AUTO || session->dtmf == AST_SIP_DTMF_AUTO_INFO) {
  208. ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
  209. ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1);
  210. } else if (session->dtmf == AST_SIP_DTMF_INBAND) {
  211. ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
  212. }
  213. if (!strcmp(session_media->stream_type, STR_AUDIO) &&
  214. (session->endpoint->media.tos_audio || session->endpoint->media.cos_audio)) {
  215. ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_audio,
  216. session->endpoint->media.cos_audio, "SIP RTP Audio");
  217. } else if (!strcmp(session_media->stream_type, STR_VIDEO) &&
  218. (session->endpoint->media.tos_video || session->endpoint->media.cos_video)) {
  219. ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_video,
  220. session->endpoint->media.cos_video, "SIP RTP Video");
  221. }
  222. ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL));
  223. return 0;
  224. }
  225. static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs,
  226. struct ast_sip_session_media *session_media)
  227. {
  228. pjmedia_sdp_attr *attr;
  229. pjmedia_sdp_rtpmap *rtpmap;
  230. pjmedia_sdp_fmtp fmtp;
  231. struct ast_format *format;
  232. int i, num = 0, tel_event = 0;
  233. char name[256];
  234. char media[20];
  235. char fmt_param[256];
  236. enum ast_rtp_options options = session->endpoint->media.g726_non_standard ?
  237. AST_RTP_OPT_G726_NONSTANDARD : 0;
  238. ast_rtp_codecs_payloads_initialize(codecs);
  239. /* Iterate through provided formats */
  240. for (i = 0; i < stream->desc.fmt_count; ++i) {
  241. /* The payload is kept as a string for things like t38 but for video it is always numerical */
  242. ast_rtp_codecs_payloads_set_m_type(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]));
  243. /* Look for the optional rtpmap attribute */
  244. if (!(attr = pjmedia_sdp_media_find_attr2(stream, "rtpmap", &stream->desc.fmt[i]))) {
  245. continue;
  246. }
  247. /* Interpret the attribute as an rtpmap */
  248. if ((pjmedia_sdp_attr_to_rtpmap(session->inv_session->pool_prov, attr, &rtpmap)) != PJ_SUCCESS) {
  249. continue;
  250. }
  251. ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name));
  252. if (strcmp(name, "telephone-event") == 0) {
  253. tel_event++;
  254. }
  255. ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media));
  256. ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL,
  257. pj_strtoul(&stream->desc.fmt[i]), media, name, options, rtpmap->clock_rate);
  258. /* Look for an optional associated fmtp attribute */
  259. if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) {
  260. continue;
  261. }
  262. if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) {
  263. ast_copy_pj_str(fmt_param, &fmtp.fmt, sizeof(fmt_param));
  264. if (sscanf(fmt_param, "%30d", &num) != 1) {
  265. continue;
  266. }
  267. if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) {
  268. struct ast_format *format_parsed;
  269. ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param));
  270. format_parsed = ast_format_parse_sdp_fmtp(format, fmt_param);
  271. if (format_parsed) {
  272. ast_rtp_codecs_payload_replace_format(codecs, num, format_parsed);
  273. ao2_ref(format_parsed, -1);
  274. }
  275. ao2_ref(format, -1);
  276. }
  277. }
  278. }
  279. if (!tel_event && (session->dtmf == AST_SIP_DTMF_AUTO)) {
  280. ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
  281. }
  282. if (session->dtmf == AST_SIP_DTMF_AUTO_INFO) {
  283. if (tel_event) {
  284. ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
  285. } else {
  286. ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_NONE);
  287. }
  288. }
  289. /* Get the packetization, if it exists */
  290. if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) {
  291. unsigned long framing = pj_strtoul(pj_strltrim(&attr->value));
  292. if (framing && session->endpoint->media.rtp.use_ptime) {
  293. ast_rtp_codecs_set_framing(codecs, framing);
  294. }
  295. }
  296. }
  297. static int set_caps(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  298. const struct pjmedia_sdp_media *stream)
  299. {
  300. RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
  301. RAII_VAR(struct ast_format_cap *, peer, NULL, ao2_cleanup);
  302. RAII_VAR(struct ast_format_cap *, joint, NULL, ao2_cleanup);
  303. enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
  304. struct ast_rtp_codecs codecs = AST_RTP_CODECS_NULL_INIT;
  305. int fmts = 0;
  306. int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
  307. ast_format_cap_count(session->direct_media_cap);
  308. int dsp_features = 0;
  309. if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
  310. !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
  311. !(joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  312. ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type);
  313. return -1;
  314. }
  315. /* get the endpoint capabilities */
  316. if (direct_media_enabled) {
  317. ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
  318. format_cap_only_type(caps, media_type);
  319. } else {
  320. ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type);
  321. }
  322. /* get the capabilities on the peer */
  323. get_codecs(session, stream, &codecs, session_media);
  324. ast_rtp_codecs_payload_formats(&codecs, peer, &fmts);
  325. /* get the joint capabilities between peer and endpoint */
  326. ast_format_cap_get_compatible(caps, peer, joint);
  327. if (!ast_format_cap_count(joint)) {
  328. struct ast_str *usbuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  329. struct ast_str *thembuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  330. ast_rtp_codecs_payloads_destroy(&codecs);
  331. ast_log(LOG_NOTICE, "No joint capabilities for '%s' media stream between our configuration(%s) and incoming SDP(%s)\n",
  332. session_media->stream_type,
  333. ast_format_cap_get_names(caps, &usbuf),
  334. ast_format_cap_get_names(peer, &thembuf));
  335. return -1;
  336. }
  337. ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp),
  338. session_media->rtp);
  339. ast_format_cap_append_from_cap(session->req_caps, joint, AST_MEDIA_TYPE_UNKNOWN);
  340. if (session->channel) {
  341. ast_channel_lock(session->channel);
  342. ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_UNKNOWN);
  343. ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(session->channel),
  344. AST_MEDIA_TYPE_UNKNOWN);
  345. ast_format_cap_remove_by_type(caps, media_type);
  346. /*
  347. * If we don't allow the sending codec to be changed on our side
  348. * then get the best codec from the joint capabilities of the media
  349. * type and use only that. This ensures the core won't start sending
  350. * out a format that we aren't currently sending.
  351. */
  352. if (!session->endpoint->asymmetric_rtp_codec) {
  353. struct ast_format *best;
  354. best = ast_format_cap_get_best_by_type(joint, media_type);
  355. if (best) {
  356. ast_format_cap_append(caps, best, ast_format_cap_get_framing(joint));
  357. ao2_ref(best, -1);
  358. }
  359. } else {
  360. ast_format_cap_append_from_cap(caps, joint, media_type);
  361. }
  362. /*
  363. * Apply the new formats to the channel, potentially changing
  364. * raw read/write formats and translation path while doing so.
  365. */
  366. ast_channel_nativeformats_set(session->channel, caps);
  367. if (media_type == AST_MEDIA_TYPE_AUDIO) {
  368. ast_set_read_format(session->channel, ast_channel_readformat(session->channel));
  369. ast_set_write_format(session->channel, ast_channel_writeformat(session->channel));
  370. }
  371. if ( ((session->dtmf == AST_SIP_DTMF_AUTO) || (session->dtmf == AST_SIP_DTMF_AUTO_INFO) )
  372. && (ast_rtp_instance_dtmf_mode_get(session_media->rtp) == AST_RTP_DTMF_MODE_RFC2833)
  373. && (session->dsp)) {
  374. dsp_features = ast_dsp_get_features(session->dsp);
  375. dsp_features &= ~DSP_FEATURE_DIGIT_DETECT;
  376. if (dsp_features) {
  377. ast_dsp_set_features(session->dsp, dsp_features);
  378. } else {
  379. ast_dsp_free(session->dsp);
  380. session->dsp = NULL;
  381. }
  382. }
  383. if (ast_channel_is_bridged(session->channel)) {
  384. ast_channel_set_unbridged_nolock(session->channel, 1);
  385. }
  386. ast_channel_unlock(session->channel);
  387. }
  388. ast_rtp_codecs_payloads_destroy(&codecs);
  389. return 0;
  390. }
  391. static pjmedia_sdp_attr* generate_rtpmap_attr(struct ast_sip_session *session, pjmedia_sdp_media *media, pj_pool_t *pool,
  392. int rtp_code, int asterisk_format, struct ast_format *format, int code)
  393. {
  394. pjmedia_sdp_rtpmap rtpmap;
  395. pjmedia_sdp_attr *attr = NULL;
  396. char tmp[64];
  397. enum ast_rtp_options options = session->endpoint->media.g726_non_standard ?
  398. AST_RTP_OPT_G726_NONSTANDARD : 0;
  399. snprintf(tmp, sizeof(tmp), "%d", rtp_code);
  400. pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp);
  401. rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1];
  402. rtpmap.clock_rate = ast_rtp_lookup_sample_rate2(asterisk_format, format, code);
  403. pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, options));
  404. if (!pj_stricmp2(&rtpmap.enc_name, "opus")) {
  405. pj_cstr(&rtpmap.param, "2");
  406. } else {
  407. pj_cstr(&rtpmap.param, NULL);
  408. }
  409. pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr);
  410. return attr;
  411. }
  412. static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format *format, int rtp_code)
  413. {
  414. struct ast_str *fmtp0 = ast_str_alloca(256);
  415. pj_str_t fmtp1;
  416. pjmedia_sdp_attr *attr = NULL;
  417. char *tmp;
  418. ast_format_generate_sdp_fmtp(format, rtp_code, &fmtp0);
  419. if (ast_str_strlen(fmtp0)) {
  420. tmp = ast_str_buffer(fmtp0) + ast_str_strlen(fmtp0) - 1;
  421. /* remove any carriage return line feeds */
  422. while (*tmp == '\r' || *tmp == '\n') --tmp;
  423. *++tmp = '\0';
  424. /* ast...generate gives us everything, just need value */
  425. tmp = strchr(ast_str_buffer(fmtp0), ':');
  426. if (tmp && tmp[1] != '\0') {
  427. fmtp1 = pj_str(tmp + 1);
  428. } else {
  429. fmtp1 = pj_str(ast_str_buffer(fmtp0));
  430. }
  431. attr = pjmedia_sdp_attr_create(pool, "fmtp", &fmtp1);
  432. }
  433. return attr;
  434. }
  435. /*! \brief Function which adds ICE attributes to a media stream */
  436. 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)
  437. {
  438. struct ast_rtp_engine_ice *ice;
  439. struct ao2_container *candidates;
  440. const char *username, *password;
  441. pj_str_t stmp;
  442. pjmedia_sdp_attr *attr;
  443. struct ao2_iterator it_candidates;
  444. struct ast_rtp_engine_ice_candidate *candidate;
  445. if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp)) ||
  446. !(candidates = ice->get_local_candidates(session_media->rtp))) {
  447. return;
  448. }
  449. if (!session_media->remote_ice) {
  450. return;
  451. }
  452. if ((username = ice->get_ufrag(session_media->rtp))) {
  453. attr = pjmedia_sdp_attr_create(pool, "ice-ufrag", pj_cstr(&stmp, username));
  454. media->attr[media->attr_count++] = attr;
  455. }
  456. if ((password = ice->get_password(session_media->rtp))) {
  457. attr = pjmedia_sdp_attr_create(pool, "ice-pwd", pj_cstr(&stmp, password));
  458. media->attr[media->attr_count++] = attr;
  459. }
  460. it_candidates = ao2_iterator_init(candidates, 0);
  461. for (; (candidate = ao2_iterator_next(&it_candidates)); ao2_ref(candidate, -1)) {
  462. struct ast_str *attr_candidate = ast_str_create(128);
  463. ast_str_set(&attr_candidate, -1, "%s %u %s %d %s ", candidate->foundation, candidate->id, candidate->transport,
  464. candidate->priority, ast_sockaddr_stringify_addr_remote(&candidate->address));
  465. ast_str_append(&attr_candidate, -1, "%s typ ", ast_sockaddr_stringify_port(&candidate->address));
  466. switch (candidate->type) {
  467. case AST_RTP_ICE_CANDIDATE_TYPE_HOST:
  468. ast_str_append(&attr_candidate, -1, "host");
  469. break;
  470. case AST_RTP_ICE_CANDIDATE_TYPE_SRFLX:
  471. ast_str_append(&attr_candidate, -1, "srflx");
  472. break;
  473. case AST_RTP_ICE_CANDIDATE_TYPE_RELAYED:
  474. ast_str_append(&attr_candidate, -1, "relay");
  475. break;
  476. }
  477. if (!ast_sockaddr_isnull(&candidate->relay_address)) {
  478. ast_str_append(&attr_candidate, -1, " raddr %s rport", ast_sockaddr_stringify_addr_remote(&candidate->relay_address));
  479. ast_str_append(&attr_candidate, -1, " %s", ast_sockaddr_stringify_port(&candidate->relay_address));
  480. }
  481. attr = pjmedia_sdp_attr_create(pool, "candidate", pj_cstr(&stmp, ast_str_buffer(attr_candidate)));
  482. media->attr[media->attr_count++] = attr;
  483. ast_free(attr_candidate);
  484. }
  485. ao2_iterator_destroy(&it_candidates);
  486. ao2_ref(candidates, -1);
  487. }
  488. /*! \brief Function which checks for ice attributes in an audio stream */
  489. static void check_ice_support(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  490. const struct pjmedia_sdp_media *remote_stream)
  491. {
  492. struct ast_rtp_engine_ice *ice;
  493. const pjmedia_sdp_attr *attr;
  494. unsigned int attr_i;
  495. if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
  496. session_media->remote_ice = 0;
  497. return;
  498. }
  499. /* Find all of the candidates */
  500. for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) {
  501. attr = remote_stream->attr[attr_i];
  502. if (!pj_strcmp2(&attr->name, "candidate")) {
  503. session_media->remote_ice = 1;
  504. break;
  505. }
  506. }
  507. if (attr_i == remote_stream->attr_count) {
  508. session_media->remote_ice = 0;
  509. }
  510. }
  511. /*! \brief Function which processes ICE attributes in an audio stream */
  512. static void process_ice_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  513. const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
  514. {
  515. struct ast_rtp_engine_ice *ice;
  516. const pjmedia_sdp_attr *attr;
  517. char attr_value[256];
  518. unsigned int attr_i;
  519. /* If ICE support is not enabled or available exit early */
  520. if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
  521. return;
  522. }
  523. attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-ufrag", NULL);
  524. if (!attr) {
  525. attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-ufrag", NULL);
  526. }
  527. if (attr) {
  528. ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
  529. ice->set_authentication(session_media->rtp, attr_value, NULL);
  530. } else {
  531. return;
  532. }
  533. attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-pwd", NULL);
  534. if (!attr) {
  535. attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-pwd", NULL);
  536. }
  537. if (attr) {
  538. ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
  539. ice->set_authentication(session_media->rtp, NULL, attr_value);
  540. } else {
  541. return;
  542. }
  543. if (pjmedia_sdp_media_find_attr2(remote_stream, "ice-lite", NULL)) {
  544. ice->ice_lite(session_media->rtp);
  545. }
  546. /* Find all of the candidates */
  547. for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) {
  548. char foundation[33], transport[32], address[PJ_INET6_ADDRSTRLEN + 1], cand_type[6], relay_address[PJ_INET6_ADDRSTRLEN + 1] = "";
  549. unsigned int port, relay_port = 0;
  550. struct ast_rtp_engine_ice_candidate candidate = { 0, };
  551. attr = remote_stream->attr[attr_i];
  552. /* If this is not a candidate line skip it */
  553. if (pj_strcmp2(&attr->name, "candidate")) {
  554. continue;
  555. }
  556. ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
  557. if (sscanf(attr_value, "%32s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u", foundation, &candidate.id, transport,
  558. (unsigned *)&candidate.priority, address, &port, cand_type, relay_address, &relay_port) < 7) {
  559. /* Candidate did not parse properly */
  560. continue;
  561. }
  562. if (session->endpoint->rtcp_mux && session_media->remote_rtcp_mux && candidate.id > 1) {
  563. /* Remote side may have offered RTP and RTCP candidates. However, if we're using RTCP MUX,
  564. * then we should ignore RTCP candidates.
  565. */
  566. continue;
  567. }
  568. candidate.foundation = foundation;
  569. candidate.transport = transport;
  570. ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID);
  571. ast_sockaddr_set_port(&candidate.address, port);
  572. if (!strcasecmp(cand_type, "host")) {
  573. candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
  574. } else if (!strcasecmp(cand_type, "srflx")) {
  575. candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
  576. } else if (!strcasecmp(cand_type, "relay")) {
  577. candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
  578. } else {
  579. continue;
  580. }
  581. if (!ast_strlen_zero(relay_address)) {
  582. ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID);
  583. }
  584. if (relay_port) {
  585. ast_sockaddr_set_port(&candidate.relay_address, relay_port);
  586. }
  587. ice->add_remote_candidate(session_media->rtp, &candidate);
  588. }
  589. ice->set_role(session_media->rtp, pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_TRUE ?
  590. AST_RTP_ICE_ROLE_CONTROLLING : AST_RTP_ICE_ROLE_CONTROLLED);
  591. ice->start(session_media->rtp);
  592. }
  593. /*! \brief figure out if media stream has crypto lines for sdes */
  594. static int media_stream_has_crypto(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. /* check the stream for the required crypto attribute */
  600. attr = stream->attr[i];
  601. if (pj_strcmp2(&attr->name, "crypto")) {
  602. continue;
  603. }
  604. return 1;
  605. }
  606. return 0;
  607. }
  608. /*! \brief figure out media transport encryption type from the media transport string */
  609. static enum ast_sip_session_media_encryption get_media_encryption_type(pj_str_t transport,
  610. const struct pjmedia_sdp_media *stream, unsigned int *optimistic)
  611. {
  612. RAII_VAR(char *, transport_str, ast_strndup(transport.ptr, transport.slen), ast_free);
  613. *optimistic = 0;
  614. if (!transport_str) {
  615. return AST_SIP_MEDIA_TRANSPORT_INVALID;
  616. }
  617. if (strstr(transport_str, "UDP/TLS")) {
  618. return AST_SIP_MEDIA_ENCRYPT_DTLS;
  619. } else if (strstr(transport_str, "SAVP")) {
  620. return AST_SIP_MEDIA_ENCRYPT_SDES;
  621. } else if (media_stream_has_crypto(stream)) {
  622. *optimistic = 1;
  623. return AST_SIP_MEDIA_ENCRYPT_SDES;
  624. } else {
  625. return AST_SIP_MEDIA_ENCRYPT_NONE;
  626. }
  627. }
  628. /*!
  629. * \brief Checks whether the encryption offered in SDP is compatible with the endpoint's configuration
  630. * \internal
  631. *
  632. * \param endpoint_encryption Media encryption configured for the endpoint
  633. * \param stream pjmedia_sdp_media stream description
  634. *
  635. * \retval AST_SIP_MEDIA_TRANSPORT_INVALID on encryption mismatch
  636. * \retval The encryption requested in the SDP
  637. */
  638. static enum ast_sip_session_media_encryption check_endpoint_media_transport(
  639. struct ast_sip_endpoint *endpoint,
  640. const struct pjmedia_sdp_media *stream)
  641. {
  642. enum ast_sip_session_media_encryption incoming_encryption;
  643. char transport_end = stream->desc.transport.ptr[stream->desc.transport.slen - 1];
  644. unsigned int optimistic;
  645. if ((transport_end == 'F' && !endpoint->media.rtp.use_avpf)
  646. || (transport_end != 'F' && endpoint->media.rtp.use_avpf)) {
  647. return AST_SIP_MEDIA_TRANSPORT_INVALID;
  648. }
  649. incoming_encryption = get_media_encryption_type(stream->desc.transport, stream, &optimistic);
  650. if (incoming_encryption == endpoint->media.rtp.encryption) {
  651. return incoming_encryption;
  652. }
  653. if (endpoint->media.rtp.force_avp ||
  654. endpoint->media.rtp.encryption_optimistic) {
  655. return incoming_encryption;
  656. }
  657. /* If an optimistic offer has been made but encryption is not enabled consider it as having
  658. * no offer of crypto at all instead of invalid so the session proceeds.
  659. */
  660. if (optimistic) {
  661. return AST_SIP_MEDIA_ENCRYPT_NONE;
  662. }
  663. return AST_SIP_MEDIA_TRANSPORT_INVALID;
  664. }
  665. static int setup_srtp(struct ast_sip_session_media *session_media)
  666. {
  667. if (!session_media->srtp) {
  668. session_media->srtp = ast_sdp_srtp_alloc();
  669. if (!session_media->srtp) {
  670. return -1;
  671. }
  672. }
  673. if (!session_media->srtp->crypto) {
  674. session_media->srtp->crypto = ast_sdp_crypto_alloc();
  675. if (!session_media->srtp->crypto) {
  676. return -1;
  677. }
  678. }
  679. return 0;
  680. }
  681. static int setup_dtls_srtp(struct ast_sip_session *session,
  682. struct ast_sip_session_media *session_media)
  683. {
  684. struct ast_rtp_engine_dtls *dtls;
  685. if (!session->endpoint->media.rtp.dtls_cfg.enabled || !session_media->rtp) {
  686. return -1;
  687. }
  688. dtls = ast_rtp_instance_get_dtls(session_media->rtp);
  689. if (!dtls) {
  690. return -1;
  691. }
  692. 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);
  693. if (dtls->set_configuration(session_media->rtp, &session->endpoint->media.rtp.dtls_cfg)) {
  694. ast_log(LOG_ERROR, "Attempted to set an invalid DTLS-SRTP configuration on RTP instance '%p'\n",
  695. session_media->rtp);
  696. return -1;
  697. }
  698. if (setup_srtp(session_media)) {
  699. return -1;
  700. }
  701. return 0;
  702. }
  703. static void apply_dtls_attrib(struct ast_sip_session_media *session_media,
  704. pjmedia_sdp_attr *attr)
  705. {
  706. struct ast_rtp_engine_dtls *dtls = ast_rtp_instance_get_dtls(session_media->rtp);
  707. pj_str_t *value;
  708. if (!attr->value.ptr || !dtls) {
  709. return;
  710. }
  711. value = pj_strtrim(&attr->value);
  712. if (!pj_strcmp2(&attr->name, "setup")) {
  713. if (!pj_stricmp2(value, "active")) {
  714. dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTIVE);
  715. } else if (!pj_stricmp2(value, "passive")) {
  716. dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_PASSIVE);
  717. } else if (!pj_stricmp2(value, "actpass")) {
  718. dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTPASS);
  719. } else if (!pj_stricmp2(value, "holdconn")) {
  720. dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_HOLDCONN);
  721. } else {
  722. ast_log(LOG_WARNING, "Unsupported setup attribute value '%*s'\n", (int)value->slen, value->ptr);
  723. }
  724. } else if (!pj_strcmp2(&attr->name, "connection")) {
  725. if (!pj_stricmp2(value, "new")) {
  726. dtls->reset(session_media->rtp);
  727. } else if (!pj_stricmp2(value, "existing")) {
  728. /* Do nothing */
  729. } else {
  730. ast_log(LOG_WARNING, "Unsupported connection attribute value '%*s'\n", (int)value->slen, value->ptr);
  731. }
  732. } else if (!pj_strcmp2(&attr->name, "fingerprint")) {
  733. char hash_value[256], hash[32];
  734. char fingerprint_text[value->slen + 1];
  735. ast_copy_pj_str(fingerprint_text, value, sizeof(fingerprint_text));
  736. if (sscanf(fingerprint_text, "%31s %255s", hash, hash_value) == 2) {
  737. if (!strcasecmp(hash, "sha-1")) {
  738. dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA1, hash_value);
  739. } else if (!strcasecmp(hash, "sha-256")) {
  740. dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA256, hash_value);
  741. } else {
  742. ast_log(LOG_WARNING, "Unsupported fingerprint hash type '%s'\n",
  743. hash);
  744. }
  745. }
  746. }
  747. }
  748. static int parse_dtls_attrib(struct ast_sip_session_media *session_media,
  749. const struct pjmedia_sdp_session *sdp,
  750. const struct pjmedia_sdp_media *stream)
  751. {
  752. int i;
  753. for (i = 0; i < sdp->attr_count; i++) {
  754. apply_dtls_attrib(session_media, sdp->attr[i]);
  755. }
  756. for (i = 0; i < stream->attr_count; i++) {
  757. apply_dtls_attrib(session_media, stream->attr[i]);
  758. }
  759. ast_set_flag(session_media->srtp, AST_SRTP_CRYPTO_OFFER_OK);
  760. return 0;
  761. }
  762. static int setup_sdes_srtp(struct ast_sip_session_media *session_media,
  763. const struct pjmedia_sdp_media *stream)
  764. {
  765. int i;
  766. for (i = 0; i < stream->attr_count; i++) {
  767. pjmedia_sdp_attr *attr;
  768. RAII_VAR(char *, crypto_str, NULL, ast_free);
  769. /* check the stream for the required crypto attribute */
  770. attr = stream->attr[i];
  771. if (pj_strcmp2(&attr->name, "crypto")) {
  772. continue;
  773. }
  774. crypto_str = ast_strndup(attr->value.ptr, attr->value.slen);
  775. if (!crypto_str) {
  776. return -1;
  777. }
  778. if (setup_srtp(session_media)) {
  779. return -1;
  780. }
  781. if (!ast_sdp_crypto_process(session_media->rtp, session_media->srtp, crypto_str)) {
  782. /* found a valid crypto attribute */
  783. return 0;
  784. }
  785. ast_debug(1, "Ignoring crypto offer with unsupported parameters: %s\n", crypto_str);
  786. }
  787. /* no usable crypto attributes found */
  788. return -1;
  789. }
  790. static int setup_media_encryption(struct ast_sip_session *session,
  791. struct ast_sip_session_media *session_media,
  792. const struct pjmedia_sdp_session *sdp,
  793. const struct pjmedia_sdp_media *stream)
  794. {
  795. switch (session_media->encryption) {
  796. case AST_SIP_MEDIA_ENCRYPT_SDES:
  797. if (setup_sdes_srtp(session_media, stream)) {
  798. return -1;
  799. }
  800. break;
  801. case AST_SIP_MEDIA_ENCRYPT_DTLS:
  802. if (setup_dtls_srtp(session, session_media)) {
  803. return -1;
  804. }
  805. if (parse_dtls_attrib(session_media, sdp, stream)) {
  806. return -1;
  807. }
  808. break;
  809. case AST_SIP_MEDIA_TRANSPORT_INVALID:
  810. case AST_SIP_MEDIA_ENCRYPT_NONE:
  811. break;
  812. }
  813. return 0;
  814. }
  815. static void set_ice_components(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
  816. {
  817. struct ast_rtp_engine_ice *ice;
  818. ast_assert(session_media->rtp != NULL);
  819. ice = ast_rtp_instance_get_ice(session_media->rtp);
  820. if (!session->endpoint->media.rtp.ice_support || !ice) {
  821. return;
  822. }
  823. if (session->endpoint->rtcp_mux && session_media->remote_rtcp_mux) {
  824. /* We both support RTCP mux. Only one ICE component necessary */
  825. ice->change_components(session_media->rtp, 1);
  826. } else {
  827. /* They either don't support RTCP mux or we don't know if they do yet. */
  828. ice->change_components(session_media->rtp, 2);
  829. }
  830. }
  831. /*! \brief Function which negotiates an incoming media stream */
  832. static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  833. const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream)
  834. {
  835. char host[NI_MAXHOST];
  836. RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
  837. enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
  838. enum ast_sip_session_media_encryption encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
  839. int res;
  840. /* If port is 0, ignore this media stream */
  841. if (!stream->desc.port) {
  842. ast_debug(3, "Media stream '%s' is already declined\n", session_media->stream_type);
  843. return 0;
  844. }
  845. /* If no type formats have been configured reject this stream */
  846. if (!ast_format_cap_has_type(session->endpoint->media.codecs, media_type)) {
  847. ast_debug(3, "Endpoint has no codecs for media type '%s', declining stream\n", session_media->stream_type);
  848. return 0;
  849. }
  850. /* Ensure incoming transport is compatible with the endpoint's configuration */
  851. if (!session->endpoint->media.rtp.use_received_transport) {
  852. encryption = check_endpoint_media_transport(session->endpoint, stream);
  853. if (encryption == AST_SIP_MEDIA_TRANSPORT_INVALID) {
  854. return -1;
  855. }
  856. }
  857. ast_copy_pj_str(host, stream->conn ? &stream->conn->addr : &sdp->conn->addr, sizeof(host));
  858. /* Ensure that the address provided is valid */
  859. if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
  860. /* The provided host was actually invalid so we error out this negotiation */
  861. return -1;
  862. }
  863. /* Using the connection information create an appropriate RTP instance */
  864. if (!session_media->rtp && create_rtp(session, session_media)) {
  865. return -1;
  866. }
  867. session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(stream, "rtcp-mux", NULL) != NULL);
  868. set_ice_components(session, session_media);
  869. enable_rtcp(session, session_media, stream);
  870. res = setup_media_encryption(session, session_media, sdp, stream);
  871. if (res) {
  872. if (!session->endpoint->media.rtp.encryption_optimistic ||
  873. !pj_strncmp2(&stream->desc.transport, "RTP/SAVP", 8)) {
  874. /* If optimistic encryption is disabled and crypto should have been enabled
  875. * but was not this session must fail. This must also fail if crypto was
  876. * required in the offer but could not be set up.
  877. */
  878. return -1;
  879. }
  880. /* There is no encryption, sad. */
  881. session_media->encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
  882. }
  883. /* If we've been explicitly configured to use the received transport OR if
  884. * encryption is on and crypto is present use the received transport.
  885. * This is done in case of optimistic because it may come in as RTP/AVP or RTP/SAVP depending
  886. * on the configuration of the remote endpoint (optimistic themselves or mandatory).
  887. */
  888. if ((session->endpoint->media.rtp.use_received_transport) ||
  889. ((encryption == AST_SIP_MEDIA_ENCRYPT_SDES) && !res)) {
  890. pj_strdup(session->inv_session->pool, &session_media->transport, &stream->desc.transport);
  891. }
  892. /* If ICE support is enabled find all the needed attributes */
  893. check_ice_support(session, session_media, stream);
  894. if (set_caps(session, session_media, stream)) {
  895. return 0;
  896. }
  897. return 1;
  898. }
  899. static int add_crypto_to_stream(struct ast_sip_session *session,
  900. struct ast_sip_session_media *session_media,
  901. pj_pool_t *pool, pjmedia_sdp_media *media)
  902. {
  903. pj_str_t stmp;
  904. pjmedia_sdp_attr *attr;
  905. enum ast_rtp_dtls_hash hash;
  906. const char *crypto_attribute;
  907. struct ast_rtp_engine_dtls *dtls;
  908. static const pj_str_t STR_NEW = { "new", 3 };
  909. static const pj_str_t STR_EXISTING = { "existing", 8 };
  910. static const pj_str_t STR_ACTIVE = { "active", 6 };
  911. static const pj_str_t STR_PASSIVE = { "passive", 7 };
  912. static const pj_str_t STR_ACTPASS = { "actpass", 7 };
  913. static const pj_str_t STR_HOLDCONN = { "holdconn", 8 };
  914. switch (session_media->encryption) {
  915. case AST_SIP_MEDIA_ENCRYPT_NONE:
  916. case AST_SIP_MEDIA_TRANSPORT_INVALID:
  917. break;
  918. case AST_SIP_MEDIA_ENCRYPT_SDES:
  919. if (!session_media->srtp) {
  920. session_media->srtp = ast_sdp_srtp_alloc();
  921. if (!session_media->srtp) {
  922. return -1;
  923. }
  924. }
  925. crypto_attribute = ast_sdp_srtp_get_attrib(session_media->srtp,
  926. 0 /* DTLS running? No */,
  927. session->endpoint->media.rtp.srtp_tag_32 /* 32 byte tag length? */);
  928. if (!crypto_attribute) {
  929. /* No crypto attribute to add, bad news */
  930. return -1;
  931. }
  932. attr = pjmedia_sdp_attr_create(pool, "crypto", pj_cstr(&stmp, crypto_attribute));
  933. media->attr[media->attr_count++] = attr;
  934. break;
  935. case AST_SIP_MEDIA_ENCRYPT_DTLS:
  936. if (setup_dtls_srtp(session, session_media)) {
  937. return -1;
  938. }
  939. dtls = ast_rtp_instance_get_dtls(session_media->rtp);
  940. if (!dtls) {
  941. return -1;
  942. }
  943. switch (dtls->get_connection(session_media->rtp)) {
  944. case AST_RTP_DTLS_CONNECTION_NEW:
  945. attr = pjmedia_sdp_attr_create(pool, "connection", &STR_NEW);
  946. media->attr[media->attr_count++] = attr;
  947. break;
  948. case AST_RTP_DTLS_CONNECTION_EXISTING:
  949. attr = pjmedia_sdp_attr_create(pool, "connection", &STR_EXISTING);
  950. media->attr[media->attr_count++] = attr;
  951. break;
  952. default:
  953. break;
  954. }
  955. switch (dtls->get_setup(session_media->rtp)) {
  956. case AST_RTP_DTLS_SETUP_ACTIVE:
  957. attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTIVE);
  958. media->attr[media->attr_count++] = attr;
  959. break;
  960. case AST_RTP_DTLS_SETUP_PASSIVE:
  961. attr = pjmedia_sdp_attr_create(pool, "setup", &STR_PASSIVE);
  962. media->attr[media->attr_count++] = attr;
  963. break;
  964. case AST_RTP_DTLS_SETUP_ACTPASS:
  965. attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTPASS);
  966. media->attr[media->attr_count++] = attr;
  967. break;
  968. case AST_RTP_DTLS_SETUP_HOLDCONN:
  969. attr = pjmedia_sdp_attr_create(pool, "setup", &STR_HOLDCONN);
  970. media->attr[media->attr_count++] = attr;
  971. break;
  972. default:
  973. break;
  974. }
  975. hash = dtls->get_fingerprint_hash(session_media->rtp);
  976. crypto_attribute = dtls->get_fingerprint(session_media->rtp);
  977. if (crypto_attribute && (hash == AST_RTP_DTLS_HASH_SHA1 || hash == AST_RTP_DTLS_HASH_SHA256)) {
  978. RAII_VAR(struct ast_str *, fingerprint, ast_str_create(64), ast_free);
  979. if (!fingerprint) {
  980. return -1;
  981. }
  982. if (hash == AST_RTP_DTLS_HASH_SHA1) {
  983. ast_str_set(&fingerprint, 0, "SHA-1 %s", crypto_attribute);
  984. } else {
  985. ast_str_set(&fingerprint, 0, "SHA-256 %s", crypto_attribute);
  986. }
  987. attr = pjmedia_sdp_attr_create(pool, "fingerprint", pj_cstr(&stmp, ast_str_buffer(fingerprint)));
  988. media->attr[media->attr_count++] = attr;
  989. }
  990. break;
  991. }
  992. return 0;
  993. }
  994. /*! \brief Function which creates an outgoing stream */
  995. static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  996. struct pjmedia_sdp_session *sdp)
  997. {
  998. pj_pool_t *pool = session->inv_session->pool_prov;
  999. static const pj_str_t STR_IN = { "IN", 2 };
  1000. static const pj_str_t STR_IP4 = { "IP4", 3};
  1001. static const pj_str_t STR_IP6 = { "IP6", 3};
  1002. static const pj_str_t STR_SENDRECV = { "sendrecv", 8 };
  1003. pjmedia_sdp_media *media;
  1004. const char *hostip = NULL;
  1005. struct ast_sockaddr addr;
  1006. char tmp[512];
  1007. pj_str_t stmp;
  1008. pjmedia_sdp_attr *attr;
  1009. int index = 0;
  1010. int noncodec = (session->dtmf == AST_SIP_DTMF_RFC_4733 || session->dtmf == AST_SIP_DTMF_AUTO || session->dtmf == AST_SIP_DTMF_AUTO_INFO) ? AST_RTP_DTMF : 0;
  1011. int min_packet_size = 0, max_packet_size = 0;
  1012. int rtp_code;
  1013. RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
  1014. enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
  1015. int use_override_prefs = ast_format_cap_count(session->req_caps);
  1016. pj_sockaddr ip;
  1017. int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
  1018. ast_format_cap_count(session->direct_media_cap);
  1019. if ((use_override_prefs && !ast_format_cap_has_type(session->req_caps, media_type)) ||
  1020. (!use_override_prefs && !ast_format_cap_has_type(session->endpoint->media.codecs, media_type))) {
  1021. /* If no type formats are configured don't add a stream */
  1022. return 0;
  1023. } else if (!session_media->rtp && create_rtp(session, session_media)) {
  1024. return -1;
  1025. }
  1026. set_ice_components(session, session_media);
  1027. enable_rtcp(session, session_media, NULL);
  1028. if (!(media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media))) ||
  1029. !(media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn)))) {
  1030. return -1;
  1031. }
  1032. if (add_crypto_to_stream(session, session_media, pool, media)) {
  1033. return -1;
  1034. }
  1035. media->desc.media = pj_str(session_media->stream_type);
  1036. if (pj_strlen(&session_media->transport)) {
  1037. /* If a transport has already been specified use it */
  1038. media->desc.transport = session_media->transport;
  1039. } else {
  1040. media->desc.transport = pj_str(ast_sdp_get_rtp_profile(
  1041. /* Optimistic encryption places crypto in the normal RTP/AVP profile */
  1042. !session->endpoint->media.rtp.encryption_optimistic &&
  1043. (session_media->encryption == AST_SIP_MEDIA_ENCRYPT_SDES),
  1044. session_media->rtp, session->endpoint->media.rtp.use_avpf,
  1045. session->endpoint->media.rtp.force_avp));
  1046. }
  1047. /* Add connection level details */
  1048. if (direct_media_enabled) {
  1049. hostip = ast_sockaddr_stringify_fmt(&session_media->direct_media_addr, AST_SOCKADDR_STR_ADDR);
  1050. } else if (ast_strlen_zero(session->endpoint->media.address)) {
  1051. hostip = ast_sip_get_host_ip_string(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET());
  1052. } else {
  1053. hostip = session->endpoint->media.address;
  1054. }
  1055. if (ast_strlen_zero(hostip)) {
  1056. ast_log(LOG_ERROR, "No local host IP available for stream %s\n", session_media->stream_type);
  1057. return -1;
  1058. }
  1059. media->conn->net_type = STR_IN;
  1060. /* Assume that the connection will use IPv4 until proven otherwise */
  1061. media->conn->addr_type = STR_IP4;
  1062. pj_strdup2(pool, &media->conn->addr, hostip);
  1063. if ((pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &media->conn->addr, &ip) == PJ_SUCCESS) &&
  1064. (ip.addr.sa_family == pj_AF_INET6())) {
  1065. media->conn->addr_type = STR_IP6;
  1066. }
  1067. ast_rtp_instance_get_local_address(session_media->rtp, &addr);
  1068. media->desc.port = direct_media_enabled ? ast_sockaddr_port(&session_media->direct_media_addr) : (pj_uint16_t) ast_sockaddr_port(&addr);
  1069. media->desc.port_count = 1;
  1070. /* Add ICE attributes and candidates */
  1071. add_ice_to_stream(session, session_media, pool, media);
  1072. if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  1073. ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type);
  1074. return -1;
  1075. }
  1076. if (direct_media_enabled) {
  1077. ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
  1078. } else if (!ast_format_cap_count(session->req_caps) ||
  1079. !ast_format_cap_iscompatible(session->req_caps, session->endpoint->media.codecs)) {
  1080. ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type);
  1081. } else {
  1082. ast_format_cap_append_from_cap(caps, session->req_caps, media_type);
  1083. }
  1084. for (index = 0; index < ast_format_cap_count(caps); ++index) {
  1085. struct ast_format *format = ast_format_cap_get_format(caps, index);
  1086. if (ast_format_get_type(format) != media_type) {
  1087. ao2_ref(format, -1);
  1088. continue;
  1089. }
  1090. if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0)) == -1) {
  1091. ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n", ast_format_get_name(format));
  1092. ao2_ref(format, -1);
  1093. continue;
  1094. }
  1095. if (!(attr = generate_rtpmap_attr(session, media, pool, rtp_code, 1, format, 0))) {
  1096. ao2_ref(format, -1);
  1097. continue;
  1098. }
  1099. media->attr[media->attr_count++] = attr;
  1100. if ((attr = generate_fmtp_attr(pool, format, rtp_code))) {
  1101. media->attr[media->attr_count++] = attr;
  1102. }
  1103. if (ast_format_get_maximum_ms(format) &&
  1104. ((ast_format_get_maximum_ms(format) < max_packet_size) || !max_packet_size)) {
  1105. max_packet_size = ast_format_get_maximum_ms(format);
  1106. }
  1107. ao2_ref(format, -1);
  1108. if (media->desc.fmt_count == PJMEDIA_MAX_SDP_FMT) {
  1109. break;
  1110. }
  1111. }
  1112. /* Add non-codec formats */
  1113. if (media_type != AST_MEDIA_TYPE_VIDEO && media->desc.fmt_count < PJMEDIA_MAX_SDP_FMT) {
  1114. for (index = 1LL; index <= AST_RTP_MAX; index <<= 1) {
  1115. if (!(noncodec & index)) {
  1116. continue;
  1117. }
  1118. rtp_code = ast_rtp_codecs_payload_code(
  1119. ast_rtp_instance_get_codecs(session_media->rtp), 0, NULL, index);
  1120. if (rtp_code == -1) {
  1121. continue;
  1122. }
  1123. if (!(attr = generate_rtpmap_attr(session, media, pool, rtp_code, 0, NULL, index))) {
  1124. continue;
  1125. }
  1126. media->attr[media->attr_count++] = attr;
  1127. if (index == AST_RTP_DTMF) {
  1128. snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
  1129. attr = pjmedia_sdp_attr_create(pool, "fmtp", pj_cstr(&stmp, tmp));
  1130. media->attr[media->attr_count++] = attr;
  1131. }
  1132. if (media->desc.fmt_count == PJMEDIA_MAX_SDP_FMT) {
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. /* If no formats were actually added to the media stream don't add it to the SDP */
  1138. if (!media->desc.fmt_count) {
  1139. return 1;
  1140. }
  1141. /* If ptime is set add it as an attribute */
  1142. min_packet_size = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(session_media->rtp));
  1143. if (!min_packet_size) {
  1144. min_packet_size = ast_format_cap_get_framing(caps);
  1145. }
  1146. if (min_packet_size) {
  1147. snprintf(tmp, sizeof(tmp), "%d", min_packet_size);
  1148. attr = pjmedia_sdp_attr_create(pool, "ptime", pj_cstr(&stmp, tmp));
  1149. media->attr[media->attr_count++] = attr;
  1150. }
  1151. if (max_packet_size) {
  1152. snprintf(tmp, sizeof(tmp), "%d", max_packet_size);
  1153. attr = pjmedia_sdp_attr_create(pool, "maxptime", pj_cstr(&stmp, tmp));
  1154. media->attr[media->attr_count++] = attr;
  1155. }
  1156. /* Add the sendrecv attribute - we purposely don't keep track because pjmedia-sdp will automatically change our offer for us */
  1157. attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr);
  1158. attr->name = STR_SENDRECV;
  1159. media->attr[media->attr_count++] = attr;
  1160. /* If we've got rtcp-mux enabled, add it unless we received an offer without it */
  1161. if (session->endpoint->rtcp_mux && session_media->remote_rtcp_mux) {
  1162. attr = pjmedia_sdp_attr_create(pool, "rtcp-mux", NULL);
  1163. pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr);
  1164. }
  1165. /* Add the media stream to the SDP */
  1166. sdp->media[sdp->media_count++] = media;
  1167. return 1;
  1168. }
  1169. static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
  1170. const struct pjmedia_sdp_session *local, const struct pjmedia_sdp_media *local_stream,
  1171. const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
  1172. {
  1173. RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
  1174. enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
  1175. char host[NI_MAXHOST];
  1176. int fdno, res;
  1177. if (!session->channel) {
  1178. return 1;
  1179. }
  1180. if (!local_stream->desc.port || !remote_stream->desc.port) {
  1181. return 1;
  1182. }
  1183. /* Ensure incoming transport is compatible with the endpoint's configuration */
  1184. if (!session->endpoint->media.rtp.use_received_transport &&
  1185. check_endpoint_media_transport(session->endpoint, remote_stream) == AST_SIP_MEDIA_TRANSPORT_INVALID) {
  1186. return -1;
  1187. }
  1188. /* Create an RTP instance if need be */
  1189. if (!session_media->rtp && create_rtp(session, session_media)) {
  1190. return -1;
  1191. }
  1192. session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(remote_stream, "rtcp-mux", NULL) != NULL);
  1193. set_ice_components(session, session_media);
  1194. enable_rtcp(session, session_media, remote_stream);
  1195. res = setup_media_encryption(session, session_media, remote, remote_stream);
  1196. if (!session->endpoint->media.rtp.encryption_optimistic && res) {
  1197. /* If optimistic encryption is disabled and crypto should have been enabled but was not
  1198. * this session must fail.
  1199. */
  1200. return -1;
  1201. }
  1202. if (!remote_stream->conn && !remote->conn) {
  1203. return 1;
  1204. }
  1205. ast_copy_pj_str(host, remote_stream->conn ? &remote_stream->conn->addr : &remote->conn->addr, sizeof(host));
  1206. /* Ensure that the address provided is valid */
  1207. if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
  1208. /* The provided host was actually invalid so we error out this negotiation */
  1209. return -1;
  1210. }
  1211. /* Apply connection information to the RTP instance */
  1212. ast_sockaddr_set_port(addrs, remote_stream->desc.port);
  1213. ast_rtp_instance_set_remote_address(session_media->rtp, addrs);
  1214. if (set_caps(session, session_media, remote_stream)) {
  1215. return 1;
  1216. }
  1217. if ((fdno = media_type_to_fdno(media_type)) < 0) {
  1218. return -1;
  1219. }
  1220. ast_channel_set_fd(session->channel, fdno, ast_rtp_instance_fd(session_media->rtp, 0));
  1221. if (!session->endpoint->rtcp_mux || !session_media->remote_rtcp_mux) {
  1222. ast_channel_set_fd(session->channel, fdno + 1, ast_rtp_instance_fd(session_media->rtp, 1));
  1223. }
  1224. /* If ICE support is enabled find all the needed attributes */
  1225. process_ice_attributes(session, session_media, remote, remote_stream);
  1226. /* Ensure the RTP instance is active */
  1227. ast_rtp_instance_activate(session_media->rtp);
  1228. /* audio stream handles music on hold */
  1229. if (media_type != AST_MEDIA_TYPE_AUDIO) {
  1230. if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE)
  1231. && (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) {
  1232. ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER);
  1233. }
  1234. return 1;
  1235. }
  1236. if (ast_sockaddr_isnull(addrs) ||
  1237. ast_sockaddr_is_any(addrs) ||
  1238. pjmedia_sdp_media_find_attr2(remote_stream, "sendonly", NULL) ||
  1239. pjmedia_sdp_media_find_attr2(remote_stream, "inactive", NULL)) {
  1240. if (!session_media->held) {
  1241. /* The remote side has put us on hold */
  1242. ast_queue_hold(session->channel, session->endpoint->mohsuggest);
  1243. ast_rtp_instance_stop(session_media->rtp);
  1244. ast_queue_frame(session->channel, &ast_null_frame);
  1245. session_media->held = 1;
  1246. }
  1247. } else if (session_media->held) {
  1248. /* The remote side has taken us off hold */
  1249. ast_queue_unhold(session->channel);
  1250. ast_queue_frame(session->channel, &ast_null_frame);
  1251. session_media->held = 0;
  1252. } else if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE)
  1253. && (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) {
  1254. ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER);
  1255. }
  1256. /* This purposely resets the encryption to the configured in case it gets added later */
  1257. session_media->encryption = session->endpoint->media.rtp.encryption;
  1258. if (session->endpoint->media.rtp.keepalive > 0 &&
  1259. stream_to_media_type(session_media->stream_type) == AST_MEDIA_TYPE_AUDIO) {
  1260. ast_rtp_instance_set_keepalive(session_media->rtp, session->endpoint->media.rtp.keepalive);
  1261. /* Schedule the initial keepalive early in case this is being used to punch holes through
  1262. * a NAT. This way there won't be an awkward delay before media starts flowing in some
  1263. * scenarios.
  1264. */
  1265. AST_SCHED_DEL(sched, session_media->keepalive_sched_id);
  1266. session_media->keepalive_sched_id = ast_sched_add_variable(sched, 500, send_keepalive,
  1267. session_media, 1);
  1268. }
  1269. /* As the channel lock is not held during this process the scheduled item won't block if
  1270. * it is hanging up the channel at the same point we are applying this negotiated SDP.
  1271. */
  1272. AST_SCHED_DEL(sched, session_media->timeout_sched_id);
  1273. /* Due to the fact that we only ever have one scheduled timeout item for when we are both
  1274. * off hold and on hold we don't need to store the two timeouts differently on the RTP
  1275. * instance itself.
  1276. */
  1277. ast_rtp_instance_set_timeout(session_media->rtp, 0);
  1278. if (session->endpoint->media.rtp.timeout && !session_media->held) {
  1279. ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout);
  1280. } else if (session->endpoint->media.rtp.timeout_hold && session_media->held) {
  1281. ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout_hold);
  1282. }
  1283. if (ast_rtp_instance_get_timeout(session_media->rtp)) {
  1284. session_media->timeout_sched_id = ast_sched_add_variable(sched,
  1285. ast_rtp_instance_get_timeout(session_media->rtp) * 1000, rtp_check_timeout,
  1286. session_media, 1);
  1287. }
  1288. return 1;
  1289. }
  1290. /*! \brief Function which updates the media stream with external media address, if applicable */
  1291. static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport)
  1292. {
  1293. RAII_VAR(struct ast_sip_transport_state *, transport_state, ast_sip_get_transport_state(ast_sorcery_object_get_id(transport)), ao2_cleanup);
  1294. char host[NI_MAXHOST];
  1295. struct ast_sockaddr our_sdp_addr = { { 0, } };
  1296. /* If the stream has been rejected there will be no connection line */
  1297. if (!stream->conn || !transport_state) {
  1298. return;
  1299. }
  1300. ast_copy_pj_str(host, &stream->conn->addr, sizeof(host));
  1301. ast_sockaddr_parse(&our_sdp_addr, host, PARSE_PORT_FORBID);
  1302. /* Reversed check here. We don't check the remote endpoint being
  1303. * in our local net, but whether our outgoing session IP is
  1304. * local. If it is not, we won't do rewriting. No localnet
  1305. * configured? Always rewrite. */
  1306. if (ast_sip_transport_is_nonlocal(transport_state, &our_sdp_addr) && transport_state->localnet) {
  1307. return;
  1308. }
  1309. ast_debug(5, "Setting media address to %s\n", ast_sockaddr_stringify_host(&transport_state->external_media_address));
  1310. pj_strdup2(tdata->pool, &stream->conn->addr, ast_sockaddr_stringify_host(&transport_state->external_media_address));
  1311. }
  1312. /*! \brief Function which stops the RTP instance */
  1313. static void stream_stop(struct ast_sip_session_media *session_media)
  1314. {
  1315. if (!session_media->rtp) {
  1316. return;
  1317. }
  1318. AST_SCHED_DEL(sched, session_media->keepalive_sched_id);
  1319. AST_SCHED_DEL(sched, session_media->timeout_sched_id);
  1320. ast_rtp_instance_stop(session_media->rtp);
  1321. }
  1322. /*! \brief Function which destroys the RTP instance when session ends */
  1323. static void stream_destroy(struct ast_sip_session_media *session_media)
  1324. {
  1325. if (session_media->rtp) {
  1326. stream_stop(session_media);
  1327. ast_rtp_instance_destroy(session_media->rtp);
  1328. }
  1329. session_media->rtp = NULL;
  1330. }
  1331. /*! \brief SDP handler for 'audio' media stream */
  1332. static struct ast_sip_session_sdp_handler audio_sdp_handler = {
  1333. .id = STR_AUDIO,
  1334. .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
  1335. .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
  1336. .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
  1337. .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
  1338. .stream_stop = stream_stop,
  1339. .stream_destroy = stream_destroy,
  1340. };
  1341. /*! \brief SDP handler for 'video' media stream */
  1342. static struct ast_sip_session_sdp_handler video_sdp_handler = {
  1343. .id = STR_VIDEO,
  1344. .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
  1345. .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
  1346. .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
  1347. .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
  1348. .stream_stop = stream_stop,
  1349. .stream_destroy = stream_destroy,
  1350. };
  1351. static int video_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  1352. {
  1353. struct pjsip_transaction *tsx;
  1354. pjsip_tx_data *tdata;
  1355. if (!session->channel
  1356. || !ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
  1357. "application",
  1358. "media_control+xml")) {
  1359. return 0;
  1360. }
  1361. tsx = pjsip_rdata_get_tsx(rdata);
  1362. ast_queue_control(session->channel, AST_CONTROL_VIDUPDATE);
  1363. if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, 200, NULL, &tdata) == PJ_SUCCESS) {
  1364. pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
  1365. }
  1366. return 0;
  1367. }
  1368. static struct ast_sip_session_supplement video_info_supplement = {
  1369. .method = "INFO",
  1370. .incoming_request = video_info_incoming_request,
  1371. };
  1372. /*! \brief Unloads the sdp RTP/AVP module from Asterisk */
  1373. static int unload_module(void)
  1374. {
  1375. ast_sip_session_unregister_supplement(&video_info_supplement);
  1376. ast_sip_session_unregister_sdp_handler(&video_sdp_handler, STR_VIDEO);
  1377. ast_sip_session_unregister_sdp_handler(&audio_sdp_handler, STR_AUDIO);
  1378. if (sched) {
  1379. ast_sched_context_destroy(sched);
  1380. }
  1381. return 0;
  1382. }
  1383. /*!
  1384. * \brief Load the module
  1385. *
  1386. * Module loading including tests for configuration or dependencies.
  1387. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1388. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1389. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1390. * configuration file or other non-critical problem return
  1391. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1392. */
  1393. static int load_module(void)
  1394. {
  1395. CHECK_PJSIP_SESSION_MODULE_LOADED();
  1396. if (ast_check_ipv6()) {
  1397. ast_sockaddr_parse(&address_rtp, "::", 0);
  1398. } else {
  1399. ast_sockaddr_parse(&address_rtp, "0.0.0.0", 0);
  1400. }
  1401. if (!(sched = ast_sched_context_create())) {
  1402. ast_log(LOG_ERROR, "Unable to create scheduler context.\n");
  1403. goto end;
  1404. }
  1405. if (ast_sched_start_thread(sched)) {
  1406. ast_log(LOG_ERROR, "Unable to create scheduler context thread.\n");
  1407. goto end;
  1408. }
  1409. if (ast_sip_session_register_sdp_handler(&audio_sdp_handler, STR_AUDIO)) {
  1410. ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_AUDIO);
  1411. goto end;
  1412. }
  1413. if (ast_sip_session_register_sdp_handler(&video_sdp_handler, STR_VIDEO)) {
  1414. ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_VIDEO);
  1415. goto end;
  1416. }
  1417. ast_sip_session_register_supplement(&video_info_supplement);
  1418. return AST_MODULE_LOAD_SUCCESS;
  1419. end:
  1420. unload_module();
  1421. return AST_MODULE_LOAD_DECLINE;
  1422. }
  1423. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP SDP RTP/AVP stream handler",
  1424. .support_level = AST_MODULE_SUPPORT_CORE,
  1425. .load = load_module,
  1426. .unload = unload_module,
  1427. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  1428. );