rtp_engine.c 71 KB

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