rtp_engine.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Pluggable RTP Architecture
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. /*** DOCUMENTATION
  28. <managerEvent language="en_US" name="RTCPSent">
  29. <managerEventInstance class="EVENT_FLAG_REPORTING">
  30. <synopsis>Raised when an RTCP packet is sent.</synopsis>
  31. <syntax>
  32. <channel_snapshot/>
  33. <parameter name="SSRC">
  34. <para>The SSRC identifier for our stream</para>
  35. </parameter>
  36. <parameter name="PT">
  37. <para>The type of packet for this RTCP report.</para>
  38. <enumlist>
  39. <enum name="200(SR)"/>
  40. <enum name="201(RR)"/>
  41. </enumlist>
  42. </parameter>
  43. <parameter name="To">
  44. <para>The address the report is sent to.</para>
  45. </parameter>
  46. <parameter name="ReportCount">
  47. <para>The number of reports that were sent.</para>
  48. <para>The report count determines the number of ReportX headers in
  49. the message. The X for each set of report headers will range from 0 to
  50. <literal>ReportCount - 1</literal>.</para>
  51. </parameter>
  52. <parameter name="SentNTP" required="false">
  53. <para>The time the sender generated the report. Only valid when
  54. PT is <literal>200(SR)</literal>.</para>
  55. </parameter>
  56. <parameter name="SentRTP" required="false">
  57. <para>The sender's last RTP timestamp. Only valid when PT is
  58. <literal>200(SR)</literal>.</para>
  59. </parameter>
  60. <parameter name="SentPackets" required="false">
  61. <para>The number of packets the sender has sent. Only valid when PT
  62. is <literal>200(SR)</literal>.</para>
  63. </parameter>
  64. <parameter name="SentOctets" required="false">
  65. <para>The number of bytes the sender has sent. Only valid when PT is
  66. <literal>200(SR)</literal>.</para>
  67. </parameter>
  68. <parameter name="ReportXSourceSSRC">
  69. <para>The SSRC for the source of this report block.</para>
  70. </parameter>
  71. <parameter name="ReportXFractionLost">
  72. <para>The fraction of RTP data packets from <literal>ReportXSourceSSRC</literal>
  73. lost since the previous SR or RR report was sent.</para>
  74. </parameter>
  75. <parameter name="ReportXCumulativeLost">
  76. <para>The total number of RTP data packets from <literal>ReportXSourceSSRC</literal>
  77. lost since the beginning of reception.</para>
  78. </parameter>
  79. <parameter name="ReportXHighestSequence">
  80. <para>The highest sequence number received in an RTP data packet from
  81. <literal>ReportXSourceSSRC</literal>.</para>
  82. </parameter>
  83. <parameter name="ReportXSequenceNumberCycles">
  84. <para>The number of sequence number cycles seen for the RTP data
  85. received from <literal>ReportXSourceSSRC</literal>.</para>
  86. </parameter>
  87. <parameter name="ReportXIAJitter">
  88. <para>An estimate of the statistical variance of the RTP data packet
  89. interarrival time, measured in timestamp units.</para>
  90. </parameter>
  91. <parameter name="ReportXLSR">
  92. <para>The last SR timestamp received from <literal>ReportXSourceSSRC</literal>.
  93. If no SR has been received from <literal>ReportXSourceSSRC</literal>,
  94. then 0.</para>
  95. </parameter>
  96. <parameter name="ReportXDLSR">
  97. <para>The delay, expressed in units of 1/65536 seconds, between
  98. receiving the last SR packet from <literal>ReportXSourceSSRC</literal>
  99. and sending this report.</para>
  100. </parameter>
  101. </syntax>
  102. </managerEventInstance>
  103. </managerEvent>
  104. <managerEvent language="en_US" name="RTCPReceived">
  105. <managerEventInstance class="EVENT_FLAG_REPORTING">
  106. <synopsis>Raised when an RTCP packet is received.</synopsis>
  107. <syntax>
  108. <channel_snapshot/>
  109. <parameter name="SSRC">
  110. <para>The SSRC identifier for the remote system</para>
  111. </parameter>
  112. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='PT'])" />
  113. <parameter name="From">
  114. <para>The address the report was received from.</para>
  115. </parameter>
  116. <parameter name="RTT">
  117. <para>Calculated Round-Trip Time in seconds</para>
  118. </parameter>
  119. <parameter name="ReportCount">
  120. <para>The number of reports that were received.</para>
  121. <para>The report count determines the number of ReportX headers in
  122. the message. The X for each set of report headers will range from 0 to
  123. <literal>ReportCount - 1</literal>.</para>
  124. </parameter>
  125. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentNTP'])" />
  126. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentRTP'])" />
  127. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentPackets'])" />
  128. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentOctets'])" />
  129. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[contains(@name, 'ReportX')])" />
  130. </syntax>
  131. </managerEventInstance>
  132. </managerEvent>
  133. ***/
  134. #include "asterisk.h"
  135. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  136. #include <math.h>
  137. #include "asterisk/channel.h"
  138. #include "asterisk/frame.h"
  139. #include "asterisk/module.h"
  140. #include "asterisk/rtp_engine.h"
  141. #include "asterisk/manager.h"
  142. #include "asterisk/options.h"
  143. #include "asterisk/astobj2.h"
  144. #include "asterisk/pbx.h"
  145. #include "asterisk/translate.h"
  146. #include "asterisk/netsock2.h"
  147. #include "asterisk/_private.h"
  148. #include "asterisk/framehook.h"
  149. #include "asterisk/stasis.h"
  150. #include "asterisk/json.h"
  151. #include "asterisk/stasis_channels.h"
  152. struct ast_srtp_res *res_srtp = NULL;
  153. struct ast_srtp_policy_res *res_srtp_policy = NULL;
  154. /*! Structure that represents an RTP session (instance) */
  155. struct ast_rtp_instance {
  156. /*! Engine that is handling this RTP instance */
  157. struct ast_rtp_engine *engine;
  158. /*! Data unique to the RTP engine */
  159. void *data;
  160. /*! RTP properties that have been set and their value */
  161. int properties[AST_RTP_PROPERTY_MAX];
  162. /*! Address that we are expecting RTP to come in to */
  163. struct ast_sockaddr local_address;
  164. /*! The original source address */
  165. struct ast_sockaddr requested_target_address;
  166. /*! Address that we are sending RTP to */
  167. struct ast_sockaddr incoming_source_address;
  168. /*! Instance that we are bridged to if doing remote or local bridging */
  169. struct ast_rtp_instance *bridged;
  170. /*! Payload and packetization information */
  171. struct ast_rtp_codecs codecs;
  172. /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
  173. int timeout;
  174. /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
  175. int holdtimeout;
  176. /*! RTP keepalive interval */
  177. int keepalive;
  178. /*! Glue currently in use */
  179. struct ast_rtp_glue *glue;
  180. /*! SRTP info associated with the instance */
  181. struct ast_srtp *srtp;
  182. /*! Channel unique ID */
  183. char channel_uniqueid[AST_MAX_UNIQUEID];
  184. };
  185. /*! List of RTP engines that are currently registered */
  186. static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
  187. /*! List of RTP glues */
  188. static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
  189. /*! The following array defines the MIME Media type (and subtype) for each
  190. of our codecs, or RTP-specific data type. */
  191. static struct ast_rtp_mime_type {
  192. struct ast_rtp_payload_type payload_type;
  193. char *type;
  194. char *subtype;
  195. unsigned int sample_rate;
  196. } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
  197. static ast_rwlock_t mime_types_lock;
  198. static int mime_types_len = 0;
  199. /*!
  200. * \brief Mapping between Asterisk codecs and rtp payload types
  201. *
  202. * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
  203. * also, our own choices for dynamic payload types. This is our master
  204. * table for transmission
  205. *
  206. * See http://www.iana.org/assignments/rtp-parameters for a list of
  207. * assigned values
  208. */
  209. static struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT];
  210. static ast_rwlock_t static_RTP_PT_lock;
  211. /*! \brief \ref stasis topic for RTP related messages */
  212. static struct stasis_topic *rtp_topic;
  213. int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
  214. {
  215. struct ast_rtp_engine *current_engine;
  216. /* Perform a sanity check on the engine structure to make sure it has the basics */
  217. if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
  218. ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
  219. return -1;
  220. }
  221. /* Link owner module to the RTP engine for reference counting purposes */
  222. engine->mod = module;
  223. AST_RWLIST_WRLOCK(&engines);
  224. /* Ensure that no two modules with the same name are registered at the same time */
  225. AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
  226. if (!strcmp(current_engine->name, engine->name)) {
  227. ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
  228. AST_RWLIST_UNLOCK(&engines);
  229. return -1;
  230. }
  231. }
  232. /* The engine survived our critique. Off to the list it goes to be used */
  233. AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
  234. AST_RWLIST_UNLOCK(&engines);
  235. ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
  236. return 0;
  237. }
  238. int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
  239. {
  240. struct ast_rtp_engine *current_engine = NULL;
  241. AST_RWLIST_WRLOCK(&engines);
  242. if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
  243. ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
  244. }
  245. AST_RWLIST_UNLOCK(&engines);
  246. return current_engine ? 0 : -1;
  247. }
  248. int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
  249. {
  250. struct ast_rtp_glue *current_glue = NULL;
  251. if (ast_strlen_zero(glue->type)) {
  252. return -1;
  253. }
  254. glue->mod = module;
  255. AST_RWLIST_WRLOCK(&glues);
  256. AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
  257. if (!strcasecmp(current_glue->type, glue->type)) {
  258. ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
  259. AST_RWLIST_UNLOCK(&glues);
  260. return -1;
  261. }
  262. }
  263. AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
  264. AST_RWLIST_UNLOCK(&glues);
  265. ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
  266. return 0;
  267. }
  268. int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
  269. {
  270. struct ast_rtp_glue *current_glue = NULL;
  271. AST_RWLIST_WRLOCK(&glues);
  272. if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
  273. ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
  274. }
  275. AST_RWLIST_UNLOCK(&glues);
  276. return current_glue ? 0 : -1;
  277. }
  278. static void instance_destructor(void *obj)
  279. {
  280. struct ast_rtp_instance *instance = obj;
  281. /* Pass us off to the engine to destroy */
  282. if (instance->data && instance->engine->destroy(instance)) {
  283. ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
  284. return;
  285. }
  286. if (instance->srtp) {
  287. res_srtp->destroy(instance->srtp);
  288. }
  289. ast_rtp_codecs_payloads_destroy(&instance->codecs);
  290. /* Drop our engine reference */
  291. ast_module_unref(instance->engine->mod);
  292. ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
  293. }
  294. int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
  295. {
  296. ao2_ref(instance, -1);
  297. return 0;
  298. }
  299. struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
  300. struct ast_sched_context *sched, const struct ast_sockaddr *sa,
  301. void *data)
  302. {
  303. struct ast_sockaddr address = {{0,}};
  304. struct ast_rtp_instance *instance = NULL;
  305. struct ast_rtp_engine *engine = NULL;
  306. AST_RWLIST_RDLOCK(&engines);
  307. /* If an engine name was specified try to use it or otherwise use the first one registered */
  308. if (!ast_strlen_zero(engine_name)) {
  309. AST_RWLIST_TRAVERSE(&engines, engine, entry) {
  310. if (!strcmp(engine->name, engine_name)) {
  311. break;
  312. }
  313. }
  314. } else {
  315. engine = AST_RWLIST_FIRST(&engines);
  316. }
  317. /* If no engine was actually found bail out now */
  318. if (!engine) {
  319. ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
  320. AST_RWLIST_UNLOCK(&engines);
  321. return NULL;
  322. }
  323. /* Bump up the reference count before we return so the module can not be unloaded */
  324. ast_module_ref(engine->mod);
  325. AST_RWLIST_UNLOCK(&engines);
  326. /* Allocate a new RTP instance */
  327. if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
  328. ast_module_unref(engine->mod);
  329. return NULL;
  330. }
  331. instance->engine = engine;
  332. ast_sockaddr_copy(&instance->local_address, sa);
  333. ast_sockaddr_copy(&address, sa);
  334. if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
  335. ao2_ref(instance, -1);
  336. return NULL;
  337. }
  338. ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
  339. /* And pass it off to the engine to setup */
  340. if (instance->engine->new(instance, sched, &address, data)) {
  341. ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
  342. ao2_ref(instance, -1);
  343. return NULL;
  344. }
  345. ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
  346. return instance;
  347. }
  348. const char *ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
  349. {
  350. return instance->channel_uniqueid;
  351. }
  352. void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
  353. {
  354. ast_copy_string(instance->channel_uniqueid, uniqueid, sizeof(instance->channel_uniqueid));
  355. }
  356. void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
  357. {
  358. instance->data = data;
  359. }
  360. void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
  361. {
  362. return instance->data;
  363. }
  364. int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
  365. {
  366. return instance->engine->write(instance, frame);
  367. }
  368. struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
  369. {
  370. return instance->engine->read(instance, rtcp);
  371. }
  372. int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
  373. const struct ast_sockaddr *address)
  374. {
  375. ast_sockaddr_copy(&instance->local_address, address);
  376. return 0;
  377. }
  378. int ast_rtp_instance_set_incoming_source_address(struct ast_rtp_instance *instance,
  379. const struct ast_sockaddr *address)
  380. {
  381. ast_sockaddr_copy(&instance->incoming_source_address, address);
  382. /* moo */
  383. if (instance->engine->remote_address_set) {
  384. instance->engine->remote_address_set(instance, &instance->incoming_source_address);
  385. }
  386. return 0;
  387. }
  388. int ast_rtp_instance_set_requested_target_address(struct ast_rtp_instance *instance,
  389. const struct ast_sockaddr *address)
  390. {
  391. ast_sockaddr_copy(&instance->requested_target_address, address);
  392. return ast_rtp_instance_set_incoming_source_address(instance, address);
  393. }
  394. int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
  395. struct ast_sockaddr *address)
  396. {
  397. if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
  398. ast_sockaddr_copy(address, &instance->local_address);
  399. return 1;
  400. }
  401. return 0;
  402. }
  403. void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
  404. struct ast_sockaddr *address)
  405. {
  406. ast_sockaddr_copy(address, &instance->local_address);
  407. }
  408. int ast_rtp_instance_get_and_cmp_requested_target_address(struct ast_rtp_instance *instance,
  409. struct ast_sockaddr *address)
  410. {
  411. if (ast_sockaddr_cmp(address, &instance->requested_target_address) != 0) {
  412. ast_sockaddr_copy(address, &instance->requested_target_address);
  413. return 1;
  414. }
  415. return 0;
  416. }
  417. void ast_rtp_instance_get_incoming_source_address(struct ast_rtp_instance *instance,
  418. struct ast_sockaddr *address)
  419. {
  420. ast_sockaddr_copy(address, &instance->incoming_source_address);
  421. }
  422. void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance,
  423. struct ast_sockaddr *address)
  424. {
  425. ast_sockaddr_copy(address, &instance->requested_target_address);
  426. }
  427. void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
  428. {
  429. if (instance->engine->extended_prop_set) {
  430. instance->engine->extended_prop_set(instance, property, value);
  431. }
  432. }
  433. void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
  434. {
  435. if (instance->engine->extended_prop_get) {
  436. return instance->engine->extended_prop_get(instance, property);
  437. }
  438. return NULL;
  439. }
  440. void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
  441. {
  442. instance->properties[property] = value;
  443. if (instance->engine->prop_set) {
  444. instance->engine->prop_set(instance, property, value);
  445. }
  446. }
  447. int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
  448. {
  449. return instance->properties[property];
  450. }
  451. struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
  452. {
  453. return &instance->codecs;
  454. }
  455. static int rtp_payload_type_hash(const void *obj, const int flags)
  456. {
  457. const struct ast_rtp_payload_type *type = obj;
  458. const int *payload = obj;
  459. return (flags & OBJ_KEY) ? *payload : type->payload;
  460. }
  461. static int rtp_payload_type_cmp(void *obj, void *arg, int flags)
  462. {
  463. struct ast_rtp_payload_type *type1 = obj, *type2 = arg;
  464. const int *payload = arg;
  465. return (type1->payload == (OBJ_KEY ? *payload : type2->payload)) ? CMP_MATCH | CMP_STOP : 0;
  466. }
  467. int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
  468. {
  469. if (!(codecs->payloads = ao2_container_alloc(AST_RTP_MAX_PT, rtp_payload_type_hash, rtp_payload_type_cmp))) {
  470. return -1;
  471. }
  472. return 0;
  473. }
  474. void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
  475. {
  476. ao2_cleanup(codecs->payloads);
  477. }
  478. void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
  479. {
  480. ast_rtp_codecs_payloads_destroy(codecs);
  481. if (instance && instance->engine && instance->engine->payload_set) {
  482. int i;
  483. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  484. instance->engine->payload_set(instance, i, 0, NULL, 0);
  485. }
  486. }
  487. ast_rtp_codecs_payloads_initialize(codecs);
  488. }
  489. void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
  490. {
  491. int i;
  492. ast_rwlock_rdlock(&static_RTP_PT_lock);
  493. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  494. if (static_RTP_PT[i].rtp_code || static_RTP_PT[i].asterisk_format) {
  495. struct ast_rtp_payload_type *type;
  496. if (!(type = ao2_alloc(sizeof(*type), NULL))) {
  497. /* Unfortunately if this occurs the payloads container will not contain all possible default payloads
  498. * but we err on the side of doing what we can in the hopes that the extreme memory conditions which
  499. * caused this to occur will go away.
  500. */
  501. continue;
  502. }
  503. type->payload = i;
  504. type->asterisk_format = static_RTP_PT[i].asterisk_format;
  505. type->rtp_code = static_RTP_PT[i].rtp_code;
  506. ast_format_copy(&type->format, &static_RTP_PT[i].format);
  507. ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
  508. if (instance && instance->engine && instance->engine->payload_set) {
  509. instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
  510. }
  511. ao2_ref(type, -1);
  512. }
  513. }
  514. ast_rwlock_unlock(&static_RTP_PT_lock);
  515. }
  516. void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
  517. {
  518. int i;
  519. struct ast_rtp_payload_type *type;
  520. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  521. struct ast_rtp_payload_type *new_type;
  522. if (!(type = ao2_find(src->payloads, &i, OBJ_KEY | OBJ_NOLOCK))) {
  523. continue;
  524. }
  525. if (!(new_type = ao2_alloc(sizeof(*new_type), NULL))) {
  526. continue;
  527. }
  528. ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
  529. new_type->payload = i;
  530. *new_type = *type;
  531. ao2_link_flags(dest->payloads, new_type, OBJ_NOLOCK);
  532. ao2_ref(new_type, -1);
  533. if (instance && instance->engine && instance->engine->payload_set) {
  534. instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
  535. }
  536. ao2_ref(type, -1);
  537. }
  538. }
  539. void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
  540. {
  541. struct ast_rtp_payload_type *type;
  542. ast_rwlock_rdlock(&static_RTP_PT_lock);
  543. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  544. ast_rwlock_unlock(&static_RTP_PT_lock);
  545. return;
  546. }
  547. if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
  548. if (!(type = ao2_alloc(sizeof(*type), NULL))) {
  549. ast_rwlock_unlock(&static_RTP_PT_lock);
  550. return;
  551. }
  552. type->payload = payload;
  553. ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
  554. }
  555. type->asterisk_format = static_RTP_PT[payload].asterisk_format;
  556. type->rtp_code = static_RTP_PT[payload].rtp_code;
  557. type->payload = payload;
  558. ast_format_copy(&type->format, &static_RTP_PT[payload].format);
  559. ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
  560. if (instance && instance->engine && instance->engine->payload_set) {
  561. instance->engine->payload_set(instance, payload, type->asterisk_format, &type->format, type->rtp_code);
  562. }
  563. ao2_ref(type, -1);
  564. ast_rwlock_unlock(&static_RTP_PT_lock);
  565. }
  566. int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
  567. char *mimetype, char *mimesubtype,
  568. enum ast_rtp_options options,
  569. unsigned int sample_rate)
  570. {
  571. unsigned int i;
  572. int found = 0;
  573. if (pt < 0 || pt >= AST_RTP_MAX_PT)
  574. return -1; /* bogus payload type */
  575. ast_rwlock_rdlock(&mime_types_lock);
  576. for (i = 0; i < mime_types_len; ++i) {
  577. const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
  578. struct ast_rtp_payload_type *type;
  579. if (strcasecmp(mimesubtype, t->subtype)) {
  580. continue;
  581. }
  582. if (strcasecmp(mimetype, t->type)) {
  583. continue;
  584. }
  585. /* if both sample rates have been supplied, and they don't match,
  586. * then this not a match; if one has not been supplied, then the
  587. * rates are not compared */
  588. if (sample_rate && t->sample_rate &&
  589. (sample_rate != t->sample_rate)) {
  590. continue;
  591. }
  592. found = 1;
  593. if (!(type = ao2_find(codecs->payloads, &pt, OBJ_KEY | OBJ_NOLOCK))) {
  594. if (!(type = ao2_alloc(sizeof(*type), NULL))) {
  595. continue;
  596. }
  597. type->payload = pt;
  598. ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
  599. }
  600. *type = t->payload_type;
  601. type->payload = pt;
  602. if ((t->payload_type.format.id == AST_FORMAT_G726) && t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
  603. ast_format_set(&type->format, AST_FORMAT_G726_AAL2, 0);
  604. }
  605. if (instance && instance->engine && instance->engine->payload_set) {
  606. instance->engine->payload_set(instance, pt, type->asterisk_format, &type->format, type->rtp_code);
  607. }
  608. ao2_ref(type, -1);
  609. break;
  610. }
  611. ast_rwlock_unlock(&mime_types_lock);
  612. return (found ? 0 : -2);
  613. }
  614. int ast_rtp_codecs_payloads_set_rtpmap_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload, char *mimetype, char *mimesubtype, enum ast_rtp_options options)
  615. {
  616. return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
  617. }
  618. void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
  619. {
  620. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  621. return;
  622. }
  623. ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
  624. ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK | OBJ_NODATA | OBJ_UNLINK);
  625. if (instance && instance->engine && instance->engine->payload_set) {
  626. instance->engine->payload_set(instance, payload, 0, NULL, 0);
  627. }
  628. }
  629. struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
  630. {
  631. struct ast_rtp_payload_type result = { .asterisk_format = 0, }, *type;
  632. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  633. return result;
  634. }
  635. if ((type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
  636. result = *type;
  637. ao2_ref(type, -1);
  638. }
  639. if (!result.rtp_code && !result.asterisk_format) {
  640. ast_rwlock_rdlock(&static_RTP_PT_lock);
  641. result = static_RTP_PT[payload];
  642. ast_rwlock_unlock(&static_RTP_PT_lock);
  643. }
  644. return result;
  645. }
  646. struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
  647. {
  648. struct ast_rtp_payload_type *type;
  649. struct ast_format *format;
  650. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  651. return NULL;
  652. }
  653. if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
  654. return NULL;
  655. }
  656. format = type->asterisk_format ? &type->format : NULL;
  657. ao2_ref(type, -1);
  658. return format;
  659. }
  660. static int rtp_payload_type_add_ast(void *obj, void *arg, int flags)
  661. {
  662. struct ast_rtp_payload_type *type = obj;
  663. struct ast_format_cap *astformats = arg;
  664. if (type->asterisk_format) {
  665. ast_format_cap_add(astformats, &type->format);
  666. }
  667. return 0;
  668. }
  669. static int rtp_payload_type_add_nonast(void *obj, void *arg, int flags)
  670. {
  671. struct ast_rtp_payload_type *type = obj;
  672. int *nonastformats = arg;
  673. if (!type->asterisk_format) {
  674. *nonastformats |= type->rtp_code;
  675. }
  676. return 0;
  677. }
  678. void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
  679. {
  680. ast_format_cap_remove_all(astformats);
  681. *nonastformats = 0;
  682. ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_ast, astformats);
  683. ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_nonast, nonastformats);
  684. }
  685. static int rtp_payload_type_find_format(void *obj, void *arg, int flags)
  686. {
  687. struct ast_rtp_payload_type *type = obj;
  688. struct ast_format *format = arg;
  689. return (type->asterisk_format && (ast_format_cmp(&type->format, format) != AST_FORMAT_CMP_NOT_EQUAL)) ? CMP_MATCH | CMP_STOP : 0;
  690. }
  691. static int rtp_payload_type_find_nonast_format(void *obj, void *arg, int flags)
  692. {
  693. struct ast_rtp_payload_type *type = obj;
  694. int *rtp_code = arg;
  695. return ((!type->asterisk_format && (type->rtp_code == *rtp_code)) ? CMP_MATCH | CMP_STOP : 0);
  696. }
  697. int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
  698. {
  699. struct ast_rtp_payload_type *type;
  700. int i, res = -1;
  701. if (asterisk_format && format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_format, (void*)format))) {
  702. res = type->payload;
  703. ao2_ref(type, -1);
  704. return res;
  705. } else if (!asterisk_format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_nonast_format, (void*)&code))) {
  706. res = type->payload;
  707. ao2_ref(type, -1);
  708. return res;
  709. }
  710. ast_rwlock_rdlock(&static_RTP_PT_lock);
  711. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  712. if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
  713. (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  714. res = i;
  715. break;
  716. } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
  717. (static_RTP_PT[i].rtp_code == code)) {
  718. res = i;
  719. break;
  720. }
  721. }
  722. ast_rwlock_unlock(&static_RTP_PT_lock);
  723. return res;
  724. }
  725. int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
  726. {
  727. struct ast_rtp_payload_type *type;
  728. int res = -1;
  729. /* Search the payload type in the codecs passed */
  730. if ((type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY)))
  731. {
  732. res = type->payload;
  733. ao2_ref(type, -1);
  734. return res;
  735. }
  736. return res;
  737. }
  738. const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
  739. {
  740. int i;
  741. const char *res = "";
  742. ast_rwlock_rdlock(&mime_types_lock);
  743. for (i = 0; i < mime_types_len; i++) {
  744. if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
  745. (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  746. if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
  747. res = "G726-32";
  748. break;
  749. } else {
  750. res = ast_rtp_mime_types[i].subtype;
  751. break;
  752. }
  753. } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
  754. ast_rtp_mime_types[i].payload_type.rtp_code == code) {
  755. res = ast_rtp_mime_types[i].subtype;
  756. break;
  757. }
  758. }
  759. ast_rwlock_unlock(&mime_types_lock);
  760. return res;
  761. }
  762. unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
  763. {
  764. unsigned int i;
  765. unsigned int res = 0;
  766. ast_rwlock_rdlock(&mime_types_lock);
  767. for (i = 0; i < mime_types_len; ++i) {
  768. if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
  769. (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  770. res = ast_rtp_mime_types[i].sample_rate;
  771. break;
  772. } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
  773. ast_rtp_mime_types[i].payload_type.rtp_code == code) {
  774. res = ast_rtp_mime_types[i].sample_rate;
  775. break;
  776. }
  777. }
  778. ast_rwlock_unlock(&mime_types_lock);
  779. return res;
  780. }
  781. char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, struct ast_format_cap *ast_format_capability, int rtp_capability, const int asterisk_format, enum ast_rtp_options options)
  782. {
  783. int found = 0;
  784. const char *name;
  785. if (!buf) {
  786. return NULL;
  787. }
  788. if (asterisk_format) {
  789. struct ast_format tmp_fmt;
  790. ast_format_cap_iter_start(ast_format_capability);
  791. while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
  792. name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
  793. ast_str_append(&buf, 0, "%s|", name);
  794. found = 1;
  795. }
  796. ast_format_cap_iter_end(ast_format_capability);
  797. } else {
  798. int x;
  799. ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
  800. for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
  801. if (rtp_capability & x) {
  802. name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
  803. ast_str_append(&buf, 0, "%s|", name);
  804. found = 1;
  805. }
  806. }
  807. }
  808. ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
  809. return ast_str_buffer(buf);
  810. }
  811. void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
  812. {
  813. codecs->pref = *prefs;
  814. if (instance && instance->engine->packetization_set) {
  815. instance->engine->packetization_set(instance, &instance->codecs.pref);
  816. }
  817. }
  818. int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
  819. {
  820. return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
  821. }
  822. int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
  823. {
  824. return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
  825. }
  826. int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
  827. {
  828. return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
  829. }
  830. int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
  831. {
  832. return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
  833. }
  834. enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
  835. {
  836. return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
  837. }
  838. void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
  839. {
  840. if (instance->engine->update_source) {
  841. instance->engine->update_source(instance);
  842. }
  843. }
  844. void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
  845. {
  846. if (instance->engine->change_source) {
  847. instance->engine->change_source(instance);
  848. }
  849. }
  850. int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
  851. {
  852. return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
  853. }
  854. void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
  855. {
  856. if (instance->engine->stop) {
  857. instance->engine->stop(instance);
  858. }
  859. }
  860. int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
  861. {
  862. return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
  863. }
  864. struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
  865. {
  866. struct ast_rtp_glue *glue = NULL;
  867. AST_RWLIST_RDLOCK(&glues);
  868. AST_RWLIST_TRAVERSE(&glues, glue, entry) {
  869. if (!strcasecmp(glue->type, type)) {
  870. break;
  871. }
  872. }
  873. AST_RWLIST_UNLOCK(&glues);
  874. return glue;
  875. }
  876. /*!
  877. * \brief Conditionally unref an rtp instance
  878. */
  879. static void unref_instance_cond(struct ast_rtp_instance **instance)
  880. {
  881. if (*instance) {
  882. ao2_ref(*instance, -1);
  883. *instance = NULL;
  884. }
  885. }
  886. struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
  887. {
  888. return instance->bridged;
  889. }
  890. void ast_rtp_instance_set_bridged(struct ast_rtp_instance *instance, struct ast_rtp_instance *bridged)
  891. {
  892. instance->bridged = bridged;
  893. }
  894. void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
  895. {
  896. struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
  897. *vinstance_dst = NULL, *vinstance_src = NULL,
  898. *tinstance_dst = NULL, *tinstance_src = NULL;
  899. struct ast_rtp_glue *glue_dst, *glue_src;
  900. enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
  901. enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
  902. struct ast_format_cap *cap_dst = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  903. struct ast_format_cap *cap_src = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  904. /* Lock both channels so we can look for the glue that binds them together */
  905. ast_channel_lock_both(c_dst, c_src);
  906. if (!cap_src || !cap_dst) {
  907. goto done;
  908. }
  909. /* Grab glue that binds each channel to something using the RTP engine */
  910. if (!(glue_dst = ast_rtp_instance_get_glue(ast_channel_tech(c_dst)->type)) || !(glue_src = ast_rtp_instance_get_glue(ast_channel_tech(c_src)->type))) {
  911. ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
  912. goto done;
  913. }
  914. audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
  915. video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
  916. audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
  917. video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
  918. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  919. if (video_glue_dst_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE)) {
  920. audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
  921. }
  922. if (video_glue_src_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE)) {
  923. audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
  924. }
  925. if (audio_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_dst_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_dst->get_codec) {
  926. glue_dst->get_codec(c_dst, cap_dst);
  927. }
  928. if (audio_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_src_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_src->get_codec) {
  929. glue_src->get_codec(c_src, cap_src);
  930. }
  931. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  932. if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
  933. goto done;
  934. }
  935. /* Make sure we have matching codecs */
  936. if (!ast_format_cap_has_joint(cap_dst, cap_src)) {
  937. goto done;
  938. }
  939. ast_rtp_codecs_payloads_copy(&instance_src->codecs, &instance_dst->codecs, instance_dst);
  940. if (vinstance_dst && vinstance_src) {
  941. ast_rtp_codecs_payloads_copy(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
  942. }
  943. if (tinstance_dst && tinstance_src) {
  944. ast_rtp_codecs_payloads_copy(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
  945. }
  946. if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
  947. ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
  948. ast_channel_name(c_dst), ast_channel_name(c_src));
  949. } else {
  950. ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
  951. ast_channel_name(c_dst), ast_channel_name(c_src));
  952. }
  953. done:
  954. ast_channel_unlock(c_dst);
  955. ast_channel_unlock(c_src);
  956. ast_format_cap_destroy(cap_dst);
  957. ast_format_cap_destroy(cap_src);
  958. unref_instance_cond(&instance_dst);
  959. unref_instance_cond(&instance_src);
  960. unref_instance_cond(&vinstance_dst);
  961. unref_instance_cond(&vinstance_src);
  962. unref_instance_cond(&tinstance_dst);
  963. unref_instance_cond(&tinstance_src);
  964. }
  965. int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
  966. {
  967. struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
  968. *vinstance0 = NULL, *vinstance1 = NULL,
  969. *tinstance0 = NULL, *tinstance1 = NULL;
  970. struct ast_rtp_glue *glue0, *glue1;
  971. enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  972. enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  973. struct ast_format_cap *cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  974. struct ast_format_cap *cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  975. /* If there is no second channel just immediately bail out, we are of no use in that scenario */
  976. if (!c1 || !cap1 || !cap0) {
  977. ast_format_cap_destroy(cap0);
  978. ast_format_cap_destroy(cap1);
  979. return -1;
  980. }
  981. /* Lock both channels so we can look for the glue that binds them together */
  982. ast_channel_lock_both(c0, c1);
  983. /* Grab glue that binds each channel to something using the RTP engine */
  984. if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
  985. ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
  986. goto done;
  987. }
  988. audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
  989. video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
  990. audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
  991. video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
  992. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  993. if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
  994. audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  995. }
  996. if (video_glue1_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
  997. audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  998. }
  999. if (audio_glue0_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue0_res == AST_RTP_GLUE_RESULT_FORBID || video_glue0_res == AST_RTP_GLUE_RESULT_REMOTE) && glue0->get_codec) {
  1000. glue0->get_codec(c0, cap0);
  1001. }
  1002. if (audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue1_res == AST_RTP_GLUE_RESULT_FORBID || video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) && glue1->get_codec) {
  1003. glue1->get_codec(c1, cap1);
  1004. }
  1005. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  1006. if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
  1007. goto done;
  1008. }
  1009. /* Make sure we have matching codecs */
  1010. if (!ast_format_cap_has_joint(cap0, cap1)) {
  1011. goto done;
  1012. }
  1013. /* Bridge media early */
  1014. if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
  1015. ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
  1016. }
  1017. done:
  1018. ast_channel_unlock(c0);
  1019. ast_channel_unlock(c1);
  1020. ast_format_cap_destroy(cap0);
  1021. ast_format_cap_destroy(cap1);
  1022. unref_instance_cond(&instance0);
  1023. unref_instance_cond(&instance1);
  1024. unref_instance_cond(&vinstance0);
  1025. unref_instance_cond(&vinstance1);
  1026. unref_instance_cond(&tinstance0);
  1027. unref_instance_cond(&tinstance1);
  1028. ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
  1029. return 0;
  1030. }
  1031. int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
  1032. {
  1033. return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
  1034. }
  1035. int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
  1036. {
  1037. return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
  1038. }
  1039. int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
  1040. {
  1041. return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
  1042. }
  1043. char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
  1044. {
  1045. struct ast_rtp_instance_stats stats = { 0, };
  1046. enum ast_rtp_instance_stat stat;
  1047. /* Determine what statistics we will need to retrieve based on field passed in */
  1048. if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
  1049. stat = AST_RTP_INSTANCE_STAT_ALL;
  1050. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
  1051. stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
  1052. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
  1053. stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
  1054. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
  1055. stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
  1056. } else {
  1057. return NULL;
  1058. }
  1059. /* Attempt to actually retrieve the statistics we need to generate the quality string */
  1060. if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
  1061. return NULL;
  1062. }
  1063. /* Now actually fill the buffer with the good information */
  1064. if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
  1065. snprintf(buf, size, "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
  1066. stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
  1067. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
  1068. snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
  1069. stats.local_minjitter, stats.local_maxjitter, stats.local_normdevjitter, sqrt(stats.local_stdevjitter), stats.remote_minjitter, stats.remote_maxjitter, stats.remote_normdevjitter, sqrt(stats.remote_stdevjitter));
  1070. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
  1071. snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
  1072. stats.local_minrxploss, stats.local_maxrxploss, stats.local_normdevrxploss, sqrt(stats.local_stdevrxploss), stats.remote_minrxploss, stats.remote_maxrxploss, stats.remote_normdevrxploss, sqrt(stats.remote_stdevrxploss));
  1073. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
  1074. snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
  1075. }
  1076. return buf;
  1077. }
  1078. void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
  1079. {
  1080. char quality_buf[AST_MAX_USER_FIELD];
  1081. char *quality;
  1082. struct ast_channel *bridge = ast_channel_bridge_peer(chan);
  1083. ast_channel_lock(chan);
  1084. ast_channel_stage_snapshot(chan);
  1085. ast_channel_unlock(chan);
  1086. if (bridge) {
  1087. ast_channel_lock(bridge);
  1088. ast_channel_stage_snapshot(bridge);
  1089. ast_channel_unlock(bridge);
  1090. }
  1091. quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY,
  1092. quality_buf, sizeof(quality_buf));
  1093. if (quality) {
  1094. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
  1095. if (bridge) {
  1096. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
  1097. }
  1098. }
  1099. quality = ast_rtp_instance_get_quality(instance,
  1100. AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf));
  1101. if (quality) {
  1102. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
  1103. if (bridge) {
  1104. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
  1105. }
  1106. }
  1107. quality = ast_rtp_instance_get_quality(instance,
  1108. AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf));
  1109. if (quality) {
  1110. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
  1111. if (bridge) {
  1112. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
  1113. }
  1114. }
  1115. quality = ast_rtp_instance_get_quality(instance,
  1116. AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf));
  1117. if (quality) {
  1118. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
  1119. if (bridge) {
  1120. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
  1121. }
  1122. }
  1123. ast_channel_lock(chan);
  1124. ast_channel_stage_snapshot_done(chan);
  1125. ast_channel_unlock(chan);
  1126. if (bridge) {
  1127. ast_channel_lock(bridge);
  1128. ast_channel_stage_snapshot_done(bridge);
  1129. ast_channel_unlock(bridge);
  1130. ast_channel_unref(bridge);
  1131. }
  1132. }
  1133. int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
  1134. {
  1135. return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
  1136. }
  1137. int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
  1138. {
  1139. return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
  1140. }
  1141. int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
  1142. {
  1143. struct ast_rtp_glue *glue;
  1144. struct ast_rtp_instance *peer_instance = NULL;
  1145. int res = -1;
  1146. if (!instance->engine->make_compatible) {
  1147. return -1;
  1148. }
  1149. ast_channel_lock(peer);
  1150. if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
  1151. ast_channel_unlock(peer);
  1152. return -1;
  1153. }
  1154. glue->get_rtp_info(peer, &peer_instance);
  1155. if (!peer_instance) {
  1156. ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
  1157. ast_channel_unlock(peer);
  1158. return -1;
  1159. }
  1160. if (peer_instance->engine != instance->engine) {
  1161. ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
  1162. ast_channel_unlock(peer);
  1163. ao2_ref(peer_instance, -1);
  1164. return -1;
  1165. }
  1166. res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
  1167. ast_channel_unlock(peer);
  1168. ao2_ref(peer_instance, -1);
  1169. peer_instance = NULL;
  1170. return res;
  1171. }
  1172. void ast_rtp_instance_available_formats(struct ast_rtp_instance *instance, struct ast_format_cap *to_endpoint, struct ast_format_cap *to_asterisk, struct ast_format_cap *result)
  1173. {
  1174. if (instance->engine->available_formats) {
  1175. instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
  1176. if (!ast_format_cap_is_empty(result)) {
  1177. return;
  1178. }
  1179. }
  1180. ast_translate_available_formats(to_endpoint, to_asterisk, result);
  1181. }
  1182. int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
  1183. {
  1184. return instance->engine->activate ? instance->engine->activate(instance) : 0;
  1185. }
  1186. void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
  1187. struct ast_sockaddr *suggestion,
  1188. const char *username)
  1189. {
  1190. if (instance->engine->stun_request) {
  1191. instance->engine->stun_request(instance, suggestion, username);
  1192. }
  1193. }
  1194. void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
  1195. {
  1196. instance->timeout = timeout;
  1197. }
  1198. void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
  1199. {
  1200. instance->holdtimeout = timeout;
  1201. }
  1202. void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
  1203. {
  1204. instance->keepalive = interval;
  1205. }
  1206. int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
  1207. {
  1208. return instance->timeout;
  1209. }
  1210. int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
  1211. {
  1212. return instance->holdtimeout;
  1213. }
  1214. int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
  1215. {
  1216. return instance->keepalive;
  1217. }
  1218. struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
  1219. {
  1220. return instance->engine;
  1221. }
  1222. struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
  1223. {
  1224. return instance->glue;
  1225. }
  1226. int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
  1227. {
  1228. if (res_srtp || res_srtp_policy) {
  1229. return -1;
  1230. }
  1231. if (!srtp_res || !policy_res) {
  1232. return -1;
  1233. }
  1234. res_srtp = srtp_res;
  1235. res_srtp_policy = policy_res;
  1236. return 0;
  1237. }
  1238. void ast_rtp_engine_unregister_srtp(void)
  1239. {
  1240. res_srtp = NULL;
  1241. res_srtp_policy = NULL;
  1242. }
  1243. int ast_rtp_engine_srtp_is_registered(void)
  1244. {
  1245. return res_srtp && res_srtp_policy;
  1246. }
  1247. int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
  1248. {
  1249. int res = 0;
  1250. if (!res_srtp) {
  1251. return -1;
  1252. }
  1253. if (!instance->srtp) {
  1254. res = res_srtp->create(&instance->srtp, instance, remote_policy);
  1255. } else {
  1256. res = res_srtp->replace(&instance->srtp, instance, remote_policy);
  1257. }
  1258. if (!res) {
  1259. res = res_srtp->add_stream(instance->srtp, local_policy);
  1260. }
  1261. return res;
  1262. }
  1263. struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
  1264. {
  1265. return instance->srtp;
  1266. }
  1267. int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
  1268. {
  1269. if (instance->engine->sendcng) {
  1270. return instance->engine->sendcng(instance, level);
  1271. }
  1272. return -1;
  1273. }
  1274. struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
  1275. {
  1276. return instance->engine->ice;
  1277. }
  1278. struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
  1279. {
  1280. return instance->engine->dtls;
  1281. }
  1282. int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
  1283. {
  1284. if (!strcasecmp(name, "dtlsenable")) {
  1285. dtls_cfg->enabled = ast_true(value) ? 1 : 0;
  1286. } else if (!strcasecmp(name, "dtlsverify")) {
  1287. if (!strcasecmp(value, "yes")) {
  1288. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT | AST_RTP_DTLS_VERIFY_CERTIFICATE;
  1289. } else if (!strcasecmp(value, "fingerprint")) {
  1290. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
  1291. } else if (!strcasecmp(value, "certificate")) {
  1292. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_CERTIFICATE;
  1293. } else if (!strcasecmp(value, "no")) {
  1294. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_NONE;
  1295. } else {
  1296. return -1;
  1297. }
  1298. } else if (!strcasecmp(name, "dtlsrekey")) {
  1299. if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
  1300. return -1;
  1301. }
  1302. } else if (!strcasecmp(name, "dtlscertfile")) {
  1303. ast_free(dtls_cfg->certfile);
  1304. dtls_cfg->certfile = ast_strdup(value);
  1305. } else if (!strcasecmp(name, "dtlsprivatekey")) {
  1306. ast_free(dtls_cfg->pvtfile);
  1307. dtls_cfg->pvtfile = ast_strdup(value);
  1308. } else if (!strcasecmp(name, "dtlscipher")) {
  1309. ast_free(dtls_cfg->cipher);
  1310. dtls_cfg->cipher = ast_strdup(value);
  1311. } else if (!strcasecmp(name, "dtlscafile")) {
  1312. ast_free(dtls_cfg->cafile);
  1313. dtls_cfg->cafile = ast_strdup(value);
  1314. } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
  1315. ast_free(dtls_cfg->capath);
  1316. dtls_cfg->capath = ast_strdup(value);
  1317. } else if (!strcasecmp(name, "dtlssetup")) {
  1318. if (!strcasecmp(value, "active")) {
  1319. dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
  1320. } else if (!strcasecmp(value, "passive")) {
  1321. dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
  1322. } else if (!strcasecmp(value, "actpass")) {
  1323. dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
  1324. }
  1325. } else if (!strcasecmp(name, "dtlsfingerprint")) {
  1326. if (!strcasecmp(value, "sha-256")) {
  1327. dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA256;
  1328. } else if (!strcasecmp(value, "sha-1")) {
  1329. dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA1;
  1330. }
  1331. } else {
  1332. return -1;
  1333. }
  1334. return 0;
  1335. }
  1336. void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
  1337. {
  1338. dst_cfg->enabled = src_cfg->enabled;
  1339. dst_cfg->verify = src_cfg->verify;
  1340. dst_cfg->rekey = src_cfg->rekey;
  1341. dst_cfg->suite = src_cfg->suite;
  1342. dst_cfg->hash = src_cfg->hash;
  1343. dst_cfg->certfile = ast_strdup(src_cfg->certfile);
  1344. dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
  1345. dst_cfg->cipher = ast_strdup(src_cfg->cipher);
  1346. dst_cfg->cafile = ast_strdup(src_cfg->cafile);
  1347. dst_cfg->capath = ast_strdup(src_cfg->capath);
  1348. dst_cfg->default_setup = src_cfg->default_setup;
  1349. }
  1350. void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
  1351. {
  1352. ast_free(dtls_cfg->certfile);
  1353. ast_free(dtls_cfg->pvtfile);
  1354. ast_free(dtls_cfg->cipher);
  1355. ast_free(dtls_cfg->cafile);
  1356. ast_free(dtls_cfg->capath);
  1357. }
  1358. static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
  1359. {
  1360. int x = mime_types_len;
  1361. if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
  1362. return;
  1363. }
  1364. ast_rwlock_wrlock(&mime_types_lock);
  1365. if (format) {
  1366. ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
  1367. ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
  1368. } else {
  1369. ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
  1370. }
  1371. ast_rtp_mime_types[x].type = type;
  1372. ast_rtp_mime_types[x].subtype = subtype;
  1373. ast_rtp_mime_types[x].sample_rate = sample_rate;
  1374. mime_types_len++;
  1375. ast_rwlock_unlock(&mime_types_lock);
  1376. }
  1377. static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
  1378. {
  1379. int x;
  1380. ast_rwlock_wrlock(&static_RTP_PT_lock);
  1381. if (map < 0) {
  1382. /* find next available dynamic payload slot */
  1383. for (x = 96; x < 127; x++) {
  1384. if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
  1385. map = x;
  1386. break;
  1387. }
  1388. }
  1389. }
  1390. if (map < 0) {
  1391. ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n" ,ast_getformatname(format));
  1392. ast_rwlock_unlock(&static_RTP_PT_lock);
  1393. return;
  1394. }
  1395. if (format) {
  1396. static_RTP_PT[map].asterisk_format = 1;
  1397. ast_format_copy(&static_RTP_PT[map].format, format);
  1398. } else {
  1399. static_RTP_PT[map].rtp_code = rtp_code;
  1400. }
  1401. ast_rwlock_unlock(&static_RTP_PT_lock);
  1402. }
  1403. int ast_rtp_engine_load_format(const struct ast_format *format)
  1404. {
  1405. switch (format->id) {
  1406. case AST_FORMAT_SILK:
  1407. set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
  1408. add_static_payload(-1, format, 0);
  1409. break;
  1410. case AST_FORMAT_CELT:
  1411. set_next_mime_type(format, 0, "audio", "CELT", ast_format_rate(format));
  1412. add_static_payload(-1, format, 0);
  1413. break;
  1414. default:
  1415. break;
  1416. }
  1417. return 0;
  1418. }
  1419. int ast_rtp_engine_unload_format(const struct ast_format *format)
  1420. {
  1421. int x;
  1422. int y = 0;
  1423. ast_rwlock_wrlock(&static_RTP_PT_lock);
  1424. /* remove everything pertaining to this format id from the lists */
  1425. for (x = 0; x < AST_RTP_MAX_PT; x++) {
  1426. if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
  1427. memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
  1428. }
  1429. }
  1430. ast_rwlock_unlock(&static_RTP_PT_lock);
  1431. ast_rwlock_wrlock(&mime_types_lock);
  1432. /* rebuild the list skipping the items matching this id */
  1433. for (x = 0; x < mime_types_len; x++) {
  1434. if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
  1435. continue;
  1436. }
  1437. ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
  1438. y++;
  1439. }
  1440. mime_types_len = y;
  1441. ast_rwlock_unlock(&mime_types_lock);
  1442. return 0;
  1443. }
  1444. /*!
  1445. * \internal
  1446. * \brief \ref stasis message payload for RTCP messages
  1447. */
  1448. struct rtcp_message_payload {
  1449. struct ast_channel_snapshot *snapshot; /*< The channel snapshot, if available */
  1450. struct ast_rtp_rtcp_report *report; /*< The RTCP report */
  1451. struct ast_json *blob; /*< Extra JSON data to publish */
  1452. };
  1453. static void rtcp_message_payload_dtor(void *obj)
  1454. {
  1455. struct rtcp_message_payload *payload = obj;
  1456. ao2_cleanup(payload->report);
  1457. ao2_cleanup(payload->snapshot);
  1458. ast_json_unref(payload->blob);
  1459. }
  1460. static struct ast_manager_event_blob *rtcp_report_to_ami(struct stasis_message *msg)
  1461. {
  1462. struct rtcp_message_payload *payload = stasis_message_data(msg);
  1463. RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
  1464. RAII_VAR(struct ast_str *, packet_string, ast_str_create(512), ast_free);
  1465. unsigned int ssrc = payload->report->ssrc;
  1466. unsigned int type = payload->report->type;
  1467. unsigned int report_count = payload->report->reception_report_count;
  1468. int i;
  1469. if (!packet_string) {
  1470. return NULL;
  1471. }
  1472. if (payload->snapshot) {
  1473. channel_string = ast_manager_build_channel_state_string(payload->snapshot);
  1474. if (!channel_string) {
  1475. return NULL;
  1476. }
  1477. }
  1478. if (payload->blob) {
  1479. /* Optional data */
  1480. struct ast_json *to = ast_json_object_get(payload->blob, "to");
  1481. struct ast_json *from = ast_json_object_get(payload->blob, "from");
  1482. struct ast_json *rtt = ast_json_object_get(payload->blob, "rtt");
  1483. if (to) {
  1484. ast_str_append(&packet_string, 0, "To: %s\r\n", ast_json_string_get(to));
  1485. }
  1486. if (from) {
  1487. ast_str_append(&packet_string, 0, "From: %s\r\n", ast_json_string_get(from));
  1488. }
  1489. if (rtt) {
  1490. ast_str_append(&packet_string, 0, "RTT: %4.4f\r\n", ast_json_real_get(rtt));
  1491. }
  1492. }
  1493. ast_str_append(&packet_string, 0, "SSRC: 0x%.8x\r\n", ssrc);
  1494. ast_str_append(&packet_string, 0, "PT: %u(%s)\r\n", type, type== AST_RTP_RTCP_SR ? "SR" : "RR");
  1495. ast_str_append(&packet_string, 0, "ReportCount: %u\r\n", report_count);
  1496. if (type == AST_RTP_RTCP_SR) {
  1497. ast_str_append(&packet_string, 0, "SentNTP: %lu.%06lu\r\n",
  1498. (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec,
  1499. (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec * 4096);
  1500. ast_str_append(&packet_string, 0, "SentRTP: %u\r\n",
  1501. payload->report->sender_information.rtp_timestamp);
  1502. ast_str_append(&packet_string, 0, "SentPackets: %u\r\n",
  1503. payload->report->sender_information.packet_count);
  1504. ast_str_append(&packet_string, 0, "SentOctets: %u\r\n",
  1505. payload->report->sender_information.octet_count);
  1506. }
  1507. for (i = 0; i < report_count; i++) {
  1508. RAII_VAR(struct ast_str *, report_string, NULL, ast_free);
  1509. if (!payload->report->report_block[i]) {
  1510. break;
  1511. }
  1512. report_string = ast_str_create(256);
  1513. if (!report_string) {
  1514. return NULL;
  1515. }
  1516. ast_str_append(&report_string, 0, "Report%dSourceSSRC: 0x%.8x\r\n",
  1517. i, payload->report->report_block[i]->source_ssrc);
  1518. ast_str_append(&report_string, 0, "Report%dFractionLost: %d\r\n",
  1519. i, payload->report->report_block[i]->lost_count.fraction);
  1520. ast_str_append(&report_string, 0, "Report%dCumulativeLost: %u\r\n",
  1521. i, payload->report->report_block[i]->lost_count.packets);
  1522. ast_str_append(&report_string, 0, "Report%dHighestSequence: %u\r\n",
  1523. i, payload->report->report_block[i]->highest_seq_no & 0xffff);
  1524. ast_str_append(&report_string, 0, "Report%dSequenceNumberCycles: %u\r\n",
  1525. i, payload->report->report_block[i]->highest_seq_no >> 16);
  1526. ast_str_append(&report_string, 0, "Report%dIAJitter: %u\r\n",
  1527. i, payload->report->report_block[i]->ia_jitter);
  1528. ast_str_append(&report_string, 0, "Report%dLSR: %u\r\n",
  1529. i, payload->report->report_block[i]->lsr);
  1530. ast_str_append(&report_string, 0, "Report%dDLSR: %4.4f\r\n",
  1531. i, ((double)payload->report->report_block[i]->dlsr) / 65536);
  1532. ast_str_append(&packet_string, 0, "%s", ast_str_buffer(report_string));
  1533. }
  1534. return ast_manager_event_blob_create(EVENT_FLAG_REPORTING,
  1535. stasis_message_type(msg) == ast_rtp_rtcp_received_type() ? "RTCPReceived" : "RTCPSent",
  1536. "%s%s",
  1537. AS_OR(channel_string, ""),
  1538. ast_str_buffer(packet_string));
  1539. }
  1540. static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
  1541. const struct stasis_message_sanitizer *sanitize)
  1542. {
  1543. struct rtcp_message_payload *payload = stasis_message_data(msg);
  1544. RAII_VAR(struct ast_json *, json_rtcp_report, NULL, ast_json_unref);
  1545. RAII_VAR(struct ast_json *, json_rtcp_report_blocks, NULL, ast_json_unref);
  1546. RAII_VAR(struct ast_json *, json_rtcp_sender_info, NULL, ast_json_unref);
  1547. RAII_VAR(struct ast_json *, json_channel, NULL, ast_json_unref);
  1548. int i;
  1549. json_rtcp_report_blocks = ast_json_array_create();
  1550. if (!json_rtcp_report_blocks) {
  1551. return NULL;
  1552. }
  1553. for (i = 0; i < payload->report->reception_report_count && payload->report->report_block[i]; i++) {
  1554. struct ast_json *json_report_block;
  1555. char str_lsr[32];
  1556. snprintf(str_lsr, sizeof(str_lsr), "%u", payload->report->report_block[i]->lsr);
  1557. json_report_block = ast_json_pack("{s: i, s: i, s: i, s: i, s: i, s: s, s: i}",
  1558. "source_ssrc", payload->report->report_block[i]->source_ssrc,
  1559. "fraction_lost", payload->report->report_block[i]->lost_count.fraction,
  1560. "packets_lost", payload->report->report_block[i]->lost_count.packets,
  1561. "highest_seq_no", payload->report->report_block[i]->highest_seq_no,
  1562. "ia_jitter", payload->report->report_block[i]->ia_jitter,
  1563. "lsr", str_lsr,
  1564. "dlsr", payload->report->report_block[i]->dlsr);
  1565. if (!json_report_block) {
  1566. return NULL;
  1567. }
  1568. if (ast_json_array_append(json_rtcp_report_blocks, json_report_block)) {
  1569. return NULL;
  1570. }
  1571. }
  1572. if (payload->report->type == AST_RTP_RTCP_SR) {
  1573. char sec[32];
  1574. char usec[32];
  1575. snprintf(sec, sizeof(sec), "%lu", payload->report->sender_information.ntp_timestamp.tv_sec);
  1576. snprintf(usec, sizeof(usec), "%lu", payload->report->sender_information.ntp_timestamp.tv_usec);
  1577. json_rtcp_sender_info = ast_json_pack("{s: s, s: s, s: i, s: i, s: i}",
  1578. "ntp_timestamp_sec", sec,
  1579. "ntp_timestamp_usec", usec,
  1580. "rtp_timestamp", payload->report->sender_information.rtp_timestamp,
  1581. "packets", payload->report->sender_information.packet_count,
  1582. "octets", payload->report->sender_information.octet_count);
  1583. if (!json_rtcp_sender_info) {
  1584. return NULL;
  1585. }
  1586. }
  1587. json_rtcp_report = ast_json_pack("{s: i, s: i, s: i, s: O, s: O}",
  1588. "ssrc", payload->report->ssrc,
  1589. "type", payload->report->type,
  1590. "report_count", payload->report->reception_report_count,
  1591. "sender_information", json_rtcp_sender_info ? json_rtcp_sender_info : ast_json_null(),
  1592. "report_blocks", json_rtcp_report_blocks);
  1593. if (!json_rtcp_report) {
  1594. return NULL;
  1595. }
  1596. if (payload->snapshot) {
  1597. json_channel = ast_channel_snapshot_to_json(payload->snapshot, sanitize);
  1598. if (!json_channel) {
  1599. return NULL;
  1600. }
  1601. }
  1602. return ast_json_pack("{s: O, s: O, s: O}",
  1603. "channel", payload->snapshot ? json_channel : ast_json_null(),
  1604. "rtcp_report", json_rtcp_report,
  1605. "blob", payload->blob);
  1606. }
  1607. static void rtp_rtcp_report_dtor(void *obj)
  1608. {
  1609. int i;
  1610. struct ast_rtp_rtcp_report *rtcp_report = obj;
  1611. for (i = 0; i < rtcp_report->reception_report_count; i++) {
  1612. ast_free(rtcp_report->report_block[i]);
  1613. }
  1614. }
  1615. struct ast_rtp_rtcp_report *ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
  1616. {
  1617. struct ast_rtp_rtcp_report *rtcp_report;
  1618. /* Size of object is sizeof the report + the number of report_blocks * sizeof pointer */
  1619. rtcp_report = ao2_alloc((sizeof(*rtcp_report) + report_blocks * sizeof(struct ast_rtp_rtcp_report_block *)),
  1620. rtp_rtcp_report_dtor);
  1621. return rtcp_report;
  1622. }
  1623. void ast_rtp_publish_rtcp_message(struct ast_rtp_instance *rtp,
  1624. struct stasis_message_type *message_type,
  1625. struct ast_rtp_rtcp_report *report,
  1626. struct ast_json *blob)
  1627. {
  1628. RAII_VAR(struct rtcp_message_payload *, payload, NULL, ao2_cleanup);
  1629. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  1630. payload = ao2_alloc(sizeof(*payload), rtcp_message_payload_dtor);
  1631. if (!payload || !report) {
  1632. return;
  1633. }
  1634. if (!ast_strlen_zero(rtp->channel_uniqueid)) {
  1635. payload->snapshot = ast_channel_snapshot_get_latest(rtp->channel_uniqueid);
  1636. }
  1637. if (blob) {
  1638. payload->blob = blob;
  1639. ast_json_ref(blob);
  1640. }
  1641. ao2_ref(report, +1);
  1642. payload->report = report;
  1643. message = stasis_message_create(message_type, payload);
  1644. if (!message) {
  1645. return;
  1646. }
  1647. stasis_publish(ast_rtp_topic(), message);
  1648. }
  1649. /*!
  1650. * @{ \brief Define RTCP/RTP message types.
  1651. */
  1652. STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_sent_type,
  1653. .to_ami = rtcp_report_to_ami,
  1654. .to_json = rtcp_report_to_json,);
  1655. STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_received_type,
  1656. .to_ami = rtcp_report_to_ami,
  1657. .to_json = rtcp_report_to_json,);
  1658. /*! @} */
  1659. struct stasis_topic *ast_rtp_topic(void)
  1660. {
  1661. return rtp_topic;
  1662. }
  1663. static void rtp_engine_shutdown(void)
  1664. {
  1665. ao2_cleanup(rtp_topic);
  1666. rtp_topic = NULL;
  1667. STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_received_type);
  1668. STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_sent_type);
  1669. }
  1670. int ast_rtp_engine_init()
  1671. {
  1672. struct ast_format tmpfmt;
  1673. ast_rwlock_init(&mime_types_lock);
  1674. ast_rwlock_init(&static_RTP_PT_lock);
  1675. rtp_topic = stasis_topic_create("rtp_topic");
  1676. if (!rtp_topic) {
  1677. return -1;
  1678. }
  1679. STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_sent_type);
  1680. STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_received_type);
  1681. ast_register_atexit(rtp_engine_shutdown);
  1682. /* Define all the RTP mime types available */
  1683. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
  1684. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
  1685. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
  1686. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
  1687. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
  1688. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
  1689. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
  1690. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
  1691. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
  1692. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
  1693. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16-256", 16000);
  1694. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
  1695. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
  1696. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
  1697. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
  1698. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
  1699. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0, "audio", "speex", 16000);
  1700. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0, "audio", "speex", 32000);
  1701. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
  1702. /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
  1703. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
  1704. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
  1705. set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
  1706. set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
  1707. set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
  1708. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
  1709. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
  1710. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
  1711. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
  1712. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "H263-1998", 90000);
  1713. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
  1714. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
  1715. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
  1716. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
  1717. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
  1718. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
  1719. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
  1720. /* Opus and VP8 */
  1721. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_OPUS, 0), 0, "audio", "opus", 48000);
  1722. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_VP8, 0), 0, "video", "VP8", 90000);
  1723. /* Define the static rtp payload mappings */
  1724. add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
  1725. #ifdef USE_DEPRECATED_G726
  1726. add_static_payload(2, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
  1727. #endif
  1728. add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
  1729. add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
  1730. add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
  1731. add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
  1732. add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
  1733. add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
  1734. add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
  1735. add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
  1736. add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
  1737. add_static_payload(13, NULL, AST_RTP_CN);
  1738. add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
  1739. add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
  1740. add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
  1741. add_static_payload(19, NULL, AST_RTP_CN); /* Also used for CN */
  1742. add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
  1743. add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
  1744. add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
  1745. add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
  1746. add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
  1747. add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
  1748. add_static_payload(101, NULL, AST_RTP_DTMF);
  1749. add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
  1750. add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
  1751. add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
  1752. add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0); /* Real time text chat (with redundancy encoding) */
  1753. add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0); /* Real time text chat */
  1754. add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
  1755. add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
  1756. add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
  1757. add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
  1758. add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
  1759. add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
  1760. add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
  1761. add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
  1762. add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */
  1763. /* Opus and VP8 */
  1764. add_static_payload(100, ast_format_set(&tmpfmt, AST_FORMAT_VP8, 0), 0);
  1765. add_static_payload(107, ast_format_set(&tmpfmt, AST_FORMAT_OPUS, 0), 0);
  1766. return 0;
  1767. }