rtp_engine.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  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. <see-also>
  103. <ref type="managerEvent">RTCPReceived</ref>
  104. </see-also>
  105. </managerEventInstance>
  106. </managerEvent>
  107. <managerEvent language="en_US" name="RTCPReceived">
  108. <managerEventInstance class="EVENT_FLAG_REPORTING">
  109. <synopsis>Raised when an RTCP packet is received.</synopsis>
  110. <syntax>
  111. <channel_snapshot/>
  112. <parameter name="SSRC">
  113. <para>The SSRC identifier for the remote system</para>
  114. </parameter>
  115. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='PT'])" />
  116. <parameter name="From">
  117. <para>The address the report was received from.</para>
  118. </parameter>
  119. <parameter name="RTT">
  120. <para>Calculated Round-Trip Time in seconds</para>
  121. </parameter>
  122. <parameter name="ReportCount">
  123. <para>The number of reports that were received.</para>
  124. <para>The report count determines the number of ReportX headers in
  125. the message. The X for each set of report headers will range from 0 to
  126. <literal>ReportCount - 1</literal>.</para>
  127. </parameter>
  128. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentNTP'])" />
  129. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentRTP'])" />
  130. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentPackets'])" />
  131. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentOctets'])" />
  132. <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[contains(@name, 'ReportX')])" />
  133. </syntax>
  134. <see-also>
  135. <ref type="managerEvent">RTCPSent</ref>
  136. </see-also>
  137. </managerEventInstance>
  138. </managerEvent>
  139. ***/
  140. #include "asterisk.h"
  141. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  142. #include <math.h> /* for sqrt, MAX */
  143. #include <sched.h> /* for sched_yield */
  144. #include <sys/time.h> /* for timeval */
  145. #include <time.h> /* for time_t */
  146. #include "asterisk/_private.h" /* for ast_rtp_engine_init prototype */
  147. #include "asterisk/astobj2.h" /* for ao2_cleanup, ao2_ref, etc */
  148. #include "asterisk/channel.h" /* for ast_channel_name, etc */
  149. #include "asterisk/codec.h" /* for ast_codec_media_type2str, etc */
  150. #include "asterisk/format.h" /* for ast_format_cmp, etc */
  151. #include "asterisk/format_cache.h" /* for ast_format_adpcm, etc */
  152. #include "asterisk/format_cap.h" /* for ast_format_cap_alloc, etc */
  153. #include "asterisk/json.h" /* for ast_json_ref, etc */
  154. #include "asterisk/linkedlists.h" /* for ast_rtp_engine::<anonymous>, etc */
  155. #include "asterisk/lock.h" /* for ast_rwlock_unlock, etc */
  156. #include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
  157. #include "asterisk/manager.h"
  158. #include "asterisk/module.h" /* for ast_module_unref, etc */
  159. #include "asterisk/netsock2.h" /* for ast_sockaddr_copy, etc */
  160. #include "asterisk/options.h" /* for ast_option_rtpptdynamic */
  161. #include "asterisk/pbx.h" /* for pbx_builtin_setvar_helper */
  162. #include "asterisk/res_srtp.h" /* for ast_srtp_res */
  163. #include "asterisk/rtp_engine.h" /* for ast_rtp_codecs, etc */
  164. #include "asterisk/stasis.h" /* for stasis_message_data, etc */
  165. #include "asterisk/stasis_channels.h" /* for ast_channel_stage_snapshot, etc */
  166. #include "asterisk/strings.h" /* for ast_str_append, etc */
  167. #include "asterisk/time.h" /* for ast_tvdiff_ms, ast_tvnow */
  168. #include "asterisk/translate.h" /* for ast_translate_available_formats */
  169. #include "asterisk/utils.h" /* for ast_free, ast_strdup, etc */
  170. #include "asterisk/vector.h" /* for AST_VECTOR_GET, etc */
  171. struct ast_srtp_res *res_srtp = NULL;
  172. struct ast_srtp_policy_res *res_srtp_policy = NULL;
  173. /*! Structure that represents an RTP session (instance) */
  174. struct ast_rtp_instance {
  175. /*! Engine that is handling this RTP instance */
  176. struct ast_rtp_engine *engine;
  177. /*! Data unique to the RTP engine */
  178. void *data;
  179. /*! RTP properties that have been set and their value */
  180. int properties[AST_RTP_PROPERTY_MAX];
  181. /*! Address that we are expecting RTP to come in to */
  182. struct ast_sockaddr local_address;
  183. /*! The original source address */
  184. struct ast_sockaddr requested_target_address;
  185. /*! Address that we are sending RTP to */
  186. struct ast_sockaddr incoming_source_address;
  187. /*! Instance that we are bridged to if doing remote or local bridging */
  188. struct ast_rtp_instance *bridged;
  189. /*! Payload and packetization information */
  190. struct ast_rtp_codecs codecs;
  191. /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
  192. int timeout;
  193. /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
  194. int holdtimeout;
  195. /*! RTP keepalive interval */
  196. int keepalive;
  197. /*! Glue currently in use */
  198. struct ast_rtp_glue *glue;
  199. /*! SRTP info associated with the instance */
  200. struct ast_srtp *srtp;
  201. /*! SRTP info dedicated for RTCP associated with the instance */
  202. struct ast_srtp *rtcp_srtp;
  203. /*! Channel unique ID */
  204. char channel_uniqueid[AST_MAX_UNIQUEID];
  205. /*! Time of last packet sent */
  206. time_t last_tx;
  207. /*! Time of last packet received */
  208. time_t last_rx;
  209. };
  210. /*! List of RTP engines that are currently registered */
  211. static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
  212. /*! List of RTP glues */
  213. static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
  214. #define MAX_RTP_MIME_TYPES 128
  215. /*! The following array defines the MIME Media type (and subtype) for each
  216. of our codecs, or RTP-specific data type. */
  217. static struct ast_rtp_mime_type {
  218. /*! \brief A mapping object between the Asterisk codec and this RTP payload */
  219. struct ast_rtp_payload_type payload_type;
  220. /*! \brief The media type */
  221. char type[16];
  222. /*! \brief The format type */
  223. char subtype[64];
  224. /*! \brief Expected sample rate of the /c subtype */
  225. unsigned int sample_rate;
  226. } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
  227. static ast_rwlock_t mime_types_lock;
  228. static int mime_types_len = 0;
  229. /*!
  230. * \brief Mapping between Asterisk codecs and rtp payload types
  231. *
  232. * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
  233. * also, our own choices for dynamic payload types. This is our master
  234. * table for transmission
  235. *
  236. * See http://www.iana.org/assignments/rtp-parameters for a list of
  237. * assigned values
  238. */
  239. static struct ast_rtp_payload_type *static_RTP_PT[AST_RTP_MAX_PT];
  240. static ast_rwlock_t static_RTP_PT_lock;
  241. /*! \brief \ref stasis topic for RTP related messages */
  242. static struct stasis_topic *rtp_topic;
  243. /*!
  244. * \internal
  245. * \brief Destructor for \c ast_rtp_payload_type
  246. */
  247. static void rtp_payload_type_dtor(void *obj)
  248. {
  249. struct ast_rtp_payload_type *payload = obj;
  250. ao2_cleanup(payload->format);
  251. }
  252. struct ast_rtp_payload_type *ast_rtp_engine_alloc_payload_type(void)
  253. {
  254. struct ast_rtp_payload_type *payload;
  255. payload = ao2_alloc_options(sizeof(*payload), rtp_payload_type_dtor,
  256. AO2_ALLOC_OPT_LOCK_NOLOCK);
  257. return payload;
  258. }
  259. int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
  260. {
  261. struct ast_rtp_engine *current_engine;
  262. /* Perform a sanity check on the engine structure to make sure it has the basics */
  263. if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
  264. ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
  265. return -1;
  266. }
  267. /* Link owner module to the RTP engine for reference counting purposes */
  268. engine->mod = module;
  269. AST_RWLIST_WRLOCK(&engines);
  270. /* Ensure that no two modules with the same name are registered at the same time */
  271. AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
  272. if (!strcmp(current_engine->name, engine->name)) {
  273. ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
  274. AST_RWLIST_UNLOCK(&engines);
  275. return -1;
  276. }
  277. }
  278. /* The engine survived our critique. Off to the list it goes to be used */
  279. AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
  280. AST_RWLIST_UNLOCK(&engines);
  281. ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
  282. return 0;
  283. }
  284. int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
  285. {
  286. struct ast_rtp_engine *current_engine = NULL;
  287. AST_RWLIST_WRLOCK(&engines);
  288. if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
  289. ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
  290. }
  291. AST_RWLIST_UNLOCK(&engines);
  292. return current_engine ? 0 : -1;
  293. }
  294. int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
  295. {
  296. struct ast_rtp_glue *current_glue = NULL;
  297. if (ast_strlen_zero(glue->type)) {
  298. return -1;
  299. }
  300. glue->mod = module;
  301. AST_RWLIST_WRLOCK(&glues);
  302. AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
  303. if (!strcasecmp(current_glue->type, glue->type)) {
  304. ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
  305. AST_RWLIST_UNLOCK(&glues);
  306. return -1;
  307. }
  308. }
  309. AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
  310. AST_RWLIST_UNLOCK(&glues);
  311. ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
  312. return 0;
  313. }
  314. int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
  315. {
  316. struct ast_rtp_glue *current_glue = NULL;
  317. AST_RWLIST_WRLOCK(&glues);
  318. if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
  319. ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
  320. }
  321. AST_RWLIST_UNLOCK(&glues);
  322. return current_glue ? 0 : -1;
  323. }
  324. static void instance_destructor(void *obj)
  325. {
  326. struct ast_rtp_instance *instance = obj;
  327. /* Pass us off to the engine to destroy */
  328. if (instance->data) {
  329. /*
  330. * Lock in case the RTP engine has other threads that
  331. * need synchronization with the destruction.
  332. */
  333. ao2_lock(instance);
  334. instance->engine->destroy(instance);
  335. ao2_unlock(instance);
  336. }
  337. if (instance->srtp) {
  338. res_srtp->destroy(instance->srtp);
  339. }
  340. if (instance->rtcp_srtp) {
  341. res_srtp->destroy(instance->rtcp_srtp);
  342. }
  343. ast_rtp_codecs_payloads_destroy(&instance->codecs);
  344. /* Drop our engine reference */
  345. ast_module_unref(instance->engine->mod);
  346. ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
  347. }
  348. int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
  349. {
  350. ao2_ref(instance, -1);
  351. return 0;
  352. }
  353. struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
  354. struct ast_sched_context *sched, const struct ast_sockaddr *sa,
  355. void *data)
  356. {
  357. struct ast_sockaddr address = {{0,}};
  358. struct ast_rtp_instance *instance = NULL;
  359. struct ast_rtp_engine *engine = NULL;
  360. AST_RWLIST_RDLOCK(&engines);
  361. /* If an engine name was specified try to use it or otherwise use the first one registered */
  362. if (!ast_strlen_zero(engine_name)) {
  363. AST_RWLIST_TRAVERSE(&engines, engine, entry) {
  364. if (!strcmp(engine->name, engine_name)) {
  365. break;
  366. }
  367. }
  368. } else {
  369. engine = AST_RWLIST_FIRST(&engines);
  370. }
  371. /* If no engine was actually found bail out now */
  372. if (!engine) {
  373. ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
  374. AST_RWLIST_UNLOCK(&engines);
  375. return NULL;
  376. }
  377. /* Bump up the reference count before we return so the module can not be unloaded */
  378. ast_module_ref(engine->mod);
  379. AST_RWLIST_UNLOCK(&engines);
  380. /* Allocate a new RTP instance */
  381. if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
  382. ast_module_unref(engine->mod);
  383. return NULL;
  384. }
  385. instance->engine = engine;
  386. ast_sockaddr_copy(&instance->local_address, sa);
  387. ast_sockaddr_copy(&address, sa);
  388. if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
  389. ao2_ref(instance, -1);
  390. return NULL;
  391. }
  392. ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
  393. /*
  394. * And pass it off to the engine to setup
  395. *
  396. * Lock in case the RTP engine has other threads that
  397. * need synchronization with the construction.
  398. */
  399. ao2_lock(instance);
  400. if (instance->engine->new(instance, sched, &address, data)) {
  401. ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
  402. ao2_unlock(instance);
  403. ao2_ref(instance, -1);
  404. return NULL;
  405. }
  406. ao2_unlock(instance);
  407. ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
  408. return instance;
  409. }
  410. const char *ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
  411. {
  412. return instance->channel_uniqueid;
  413. }
  414. void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
  415. {
  416. ast_copy_string(instance->channel_uniqueid, uniqueid, sizeof(instance->channel_uniqueid));
  417. }
  418. void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
  419. {
  420. instance->data = data;
  421. }
  422. void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
  423. {
  424. return instance->data;
  425. }
  426. int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
  427. {
  428. int res;
  429. ao2_lock(instance);
  430. res = instance->engine->write(instance, frame);
  431. ao2_unlock(instance);
  432. return res;
  433. }
  434. struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
  435. {
  436. struct ast_frame *frame;
  437. ao2_lock(instance);
  438. frame = instance->engine->read(instance, rtcp);
  439. ao2_unlock(instance);
  440. return frame;
  441. }
  442. int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
  443. const struct ast_sockaddr *address)
  444. {
  445. ao2_lock(instance);
  446. ast_sockaddr_copy(&instance->local_address, address);
  447. ao2_unlock(instance);
  448. return 0;
  449. }
  450. static void rtp_instance_set_incoming_source_address_nolock(struct ast_rtp_instance *instance,
  451. const struct ast_sockaddr *address)
  452. {
  453. ast_sockaddr_copy(&instance->incoming_source_address, address);
  454. if (instance->engine->remote_address_set) {
  455. instance->engine->remote_address_set(instance, &instance->incoming_source_address);
  456. }
  457. }
  458. int ast_rtp_instance_set_incoming_source_address(struct ast_rtp_instance *instance,
  459. const struct ast_sockaddr *address)
  460. {
  461. ao2_lock(instance);
  462. rtp_instance_set_incoming_source_address_nolock(instance, address);
  463. ao2_unlock(instance);
  464. return 0;
  465. }
  466. int ast_rtp_instance_set_requested_target_address(struct ast_rtp_instance *instance,
  467. const struct ast_sockaddr *address)
  468. {
  469. ao2_lock(instance);
  470. ast_sockaddr_copy(&instance->requested_target_address, address);
  471. rtp_instance_set_incoming_source_address_nolock(instance, address);
  472. ao2_unlock(instance);
  473. return 0;
  474. }
  475. int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
  476. struct ast_sockaddr *address)
  477. {
  478. ao2_lock(instance);
  479. if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
  480. ast_sockaddr_copy(address, &instance->local_address);
  481. ao2_unlock(instance);
  482. return 1;
  483. }
  484. ao2_unlock(instance);
  485. return 0;
  486. }
  487. void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
  488. struct ast_sockaddr *address)
  489. {
  490. ao2_lock(instance);
  491. ast_sockaddr_copy(address, &instance->local_address);
  492. ao2_unlock(instance);
  493. }
  494. int ast_rtp_instance_get_and_cmp_requested_target_address(struct ast_rtp_instance *instance,
  495. struct ast_sockaddr *address)
  496. {
  497. ao2_lock(instance);
  498. if (ast_sockaddr_cmp(address, &instance->requested_target_address) != 0) {
  499. ast_sockaddr_copy(address, &instance->requested_target_address);
  500. ao2_unlock(instance);
  501. return 1;
  502. }
  503. ao2_unlock(instance);
  504. return 0;
  505. }
  506. void ast_rtp_instance_get_incoming_source_address(struct ast_rtp_instance *instance,
  507. struct ast_sockaddr *address)
  508. {
  509. ao2_lock(instance);
  510. ast_sockaddr_copy(address, &instance->incoming_source_address);
  511. ao2_unlock(instance);
  512. }
  513. void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance,
  514. struct ast_sockaddr *address)
  515. {
  516. ao2_lock(instance);
  517. ast_sockaddr_copy(address, &instance->requested_target_address);
  518. ao2_unlock(instance);
  519. }
  520. void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
  521. {
  522. if (instance->engine->extended_prop_set) {
  523. ao2_lock(instance);
  524. instance->engine->extended_prop_set(instance, property, value);
  525. ao2_unlock(instance);
  526. }
  527. }
  528. void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
  529. {
  530. void *prop;
  531. if (instance->engine->extended_prop_get) {
  532. ao2_lock(instance);
  533. prop = instance->engine->extended_prop_get(instance, property);
  534. ao2_unlock(instance);
  535. } else {
  536. prop = NULL;
  537. }
  538. return prop;
  539. }
  540. void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
  541. {
  542. ao2_lock(instance);
  543. instance->properties[property] = value;
  544. if (instance->engine->prop_set) {
  545. instance->engine->prop_set(instance, property, value);
  546. }
  547. ao2_unlock(instance);
  548. }
  549. int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
  550. {
  551. int prop;
  552. ao2_lock(instance);
  553. prop = instance->properties[property];
  554. ao2_unlock(instance);
  555. return prop;
  556. }
  557. struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
  558. {
  559. return &instance->codecs;
  560. }
  561. int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
  562. {
  563. int res;
  564. codecs->framing = 0;
  565. ast_rwlock_init(&codecs->codecs_lock);
  566. res = AST_VECTOR_INIT(&codecs->payloads, AST_RTP_MAX_PT);
  567. return res;
  568. }
  569. void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
  570. {
  571. int i;
  572. for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
  573. struct ast_rtp_payload_type *type;
  574. type = AST_VECTOR_GET(&codecs->payloads, i);
  575. ao2_t_cleanup(type, "destroying ast_rtp_codec");
  576. }
  577. AST_VECTOR_FREE(&codecs->payloads);
  578. ast_rwlock_destroy(&codecs->codecs_lock);
  579. }
  580. void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
  581. {
  582. ast_rtp_codecs_payloads_destroy(codecs);
  583. ast_rtp_codecs_payloads_initialize(codecs);
  584. if (instance && instance->engine && instance->engine->payload_set) {
  585. int i;
  586. ao2_lock(instance);
  587. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  588. instance->engine->payload_set(instance, i, 0, NULL, 0);
  589. }
  590. ao2_unlock(instance);
  591. }
  592. }
  593. void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
  594. {
  595. int i;
  596. ast_rwlock_wrlock(&dest->codecs_lock);
  597. /* Deadlock avoidance because of held write lock. */
  598. while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
  599. ast_rwlock_unlock(&dest->codecs_lock);
  600. sched_yield();
  601. ast_rwlock_wrlock(&dest->codecs_lock);
  602. }
  603. for (i = 0; i < AST_VECTOR_SIZE(&src->payloads); i++) {
  604. struct ast_rtp_payload_type *type;
  605. type = AST_VECTOR_GET(&src->payloads, i);
  606. if (!type) {
  607. continue;
  608. }
  609. if (i < AST_VECTOR_SIZE(&dest->payloads)) {
  610. ao2_t_cleanup(AST_VECTOR_GET(&dest->payloads, i), "cleaning up vector element about to be replaced");
  611. }
  612. ast_debug(2, "Copying payload %d (%p) from %p to %p\n", i, type, src, dest);
  613. ao2_bump(type);
  614. if (AST_VECTOR_REPLACE(&dest->payloads, i, type)) {
  615. ao2_cleanup(type);
  616. } else if (instance && instance->engine && instance->engine->payload_set) {
  617. ao2_lock(instance);
  618. instance->engine->payload_set(instance, i, type->asterisk_format, type->format, type->rtp_code);
  619. ao2_unlock(instance);
  620. }
  621. }
  622. dest->framing = src->framing;
  623. ast_rwlock_unlock(&src->codecs_lock);
  624. ast_rwlock_unlock(&dest->codecs_lock);
  625. }
  626. void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
  627. {
  628. struct ast_rtp_payload_type *new_type;
  629. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  630. return;
  631. }
  632. ast_rwlock_rdlock(&static_RTP_PT_lock);
  633. new_type = ao2_bump(static_RTP_PT[payload]);
  634. ast_rwlock_unlock(&static_RTP_PT_lock);
  635. if (!new_type) {
  636. ast_debug(1, "Don't have a default tx payload type %d format for m type on %p\n",
  637. payload, codecs);
  638. return;
  639. }
  640. ast_debug(1, "Setting tx payload type %d based on m type on %p\n",
  641. payload, codecs);
  642. ast_rwlock_wrlock(&codecs->codecs_lock);
  643. if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
  644. ao2_t_cleanup(AST_VECTOR_GET(&codecs->payloads, payload), "cleaning up replaced payload type");
  645. }
  646. if (AST_VECTOR_REPLACE(&codecs->payloads, payload, new_type)) {
  647. ao2_ref(new_type, -1);
  648. } else if (instance && instance->engine && instance->engine->payload_set) {
  649. ao2_lock(instance);
  650. instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
  651. ao2_unlock(instance);
  652. }
  653. ast_rwlock_unlock(&codecs->codecs_lock);
  654. }
  655. int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
  656. char *mimetype, char *mimesubtype,
  657. enum ast_rtp_options options,
  658. unsigned int sample_rate)
  659. {
  660. unsigned int i;
  661. int found = 0;
  662. if (pt < 0 || pt >= AST_RTP_MAX_PT) {
  663. return -1; /* bogus payload type */
  664. }
  665. ast_rwlock_rdlock(&mime_types_lock);
  666. ast_rwlock_wrlock(&codecs->codecs_lock);
  667. for (i = 0; i < mime_types_len; ++i) {
  668. const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
  669. struct ast_rtp_payload_type *new_type;
  670. if (strcasecmp(mimesubtype, t->subtype)) {
  671. continue;
  672. }
  673. if (strcasecmp(mimetype, t->type)) {
  674. continue;
  675. }
  676. /* if both sample rates have been supplied, and they don't match,
  677. * then this not a match; if one has not been supplied, then the
  678. * rates are not compared */
  679. if (sample_rate && t->sample_rate &&
  680. (sample_rate != t->sample_rate)) {
  681. continue;
  682. }
  683. found = 1;
  684. new_type = ast_rtp_engine_alloc_payload_type();
  685. if (!new_type) {
  686. continue;
  687. }
  688. if (pt < AST_VECTOR_SIZE(&codecs->payloads)) {
  689. ao2_t_cleanup(AST_VECTOR_GET(&codecs->payloads, pt), "cleaning up replaced payload type");
  690. }
  691. new_type->payload = pt;
  692. new_type->asterisk_format = t->payload_type.asterisk_format;
  693. new_type->rtp_code = t->payload_type.rtp_code;
  694. if ((ast_format_cmp(t->payload_type.format, ast_format_g726) == AST_FORMAT_CMP_EQUAL) &&
  695. t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
  696. new_type->format = ast_format_g726_aal2;
  697. } else {
  698. new_type->format = t->payload_type.format;
  699. }
  700. if (new_type->format) {
  701. /* SDP parsing automatically increases the reference count */
  702. new_type->format = ast_format_parse_sdp_fmtp(new_type->format, "");
  703. }
  704. if (AST_VECTOR_REPLACE(&codecs->payloads, pt, new_type)) {
  705. ao2_ref(new_type, -1);
  706. } else if (instance && instance->engine && instance->engine->payload_set) {
  707. ao2_lock(instance);
  708. instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
  709. ao2_unlock(instance);
  710. }
  711. break;
  712. }
  713. ast_rwlock_unlock(&codecs->codecs_lock);
  714. ast_rwlock_unlock(&mime_types_lock);
  715. return (found ? 0 : -2);
  716. }
  717. 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)
  718. {
  719. return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
  720. }
  721. void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
  722. {
  723. struct ast_rtp_payload_type *type;
  724. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  725. return;
  726. }
  727. ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
  728. ast_rwlock_wrlock(&codecs->codecs_lock);
  729. if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
  730. type = AST_VECTOR_GET(&codecs->payloads, payload);
  731. ao2_cleanup(type);
  732. AST_VECTOR_REPLACE(&codecs->payloads, payload, NULL);
  733. }
  734. if (instance && instance->engine && instance->engine->payload_set) {
  735. ao2_lock(instance);
  736. instance->engine->payload_set(instance, payload, 0, NULL, 0);
  737. ao2_unlock(instance);
  738. }
  739. ast_rwlock_unlock(&codecs->codecs_lock);
  740. }
  741. enum ast_media_type ast_rtp_codecs_get_stream_type(struct ast_rtp_codecs *codecs)
  742. {
  743. enum ast_media_type stream_type = AST_MEDIA_TYPE_UNKNOWN;
  744. int payload;
  745. struct ast_rtp_payload_type *type;
  746. ast_rwlock_rdlock(&codecs->codecs_lock);
  747. for (payload = 0; payload < AST_VECTOR_SIZE(&codecs->payloads); ++payload) {
  748. type = AST_VECTOR_GET(&codecs->payloads, payload);
  749. if (type && type->asterisk_format) {
  750. stream_type = ast_format_get_type(type->format);
  751. break;
  752. }
  753. }
  754. ast_rwlock_unlock(&codecs->codecs_lock);
  755. return stream_type;
  756. }
  757. struct ast_rtp_payload_type *ast_rtp_codecs_get_payload(struct ast_rtp_codecs *codecs, int payload)
  758. {
  759. struct ast_rtp_payload_type *type = NULL;
  760. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  761. return NULL;
  762. }
  763. ast_rwlock_rdlock(&codecs->codecs_lock);
  764. if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
  765. type = AST_VECTOR_GET(&codecs->payloads, payload);
  766. ao2_bump(type);
  767. }
  768. ast_rwlock_unlock(&codecs->codecs_lock);
  769. if (!type) {
  770. ast_rwlock_rdlock(&static_RTP_PT_lock);
  771. type = ao2_bump(static_RTP_PT[payload]);
  772. ast_rwlock_unlock(&static_RTP_PT_lock);
  773. }
  774. return type;
  775. }
  776. int ast_rtp_codecs_payload_replace_format(struct ast_rtp_codecs *codecs, int payload, struct ast_format *format)
  777. {
  778. struct ast_rtp_payload_type *type;
  779. if (payload < 0 || payload >= AST_RTP_MAX_PT || !format) {
  780. return -1;
  781. }
  782. type = ast_rtp_engine_alloc_payload_type();
  783. if (!type) {
  784. return -1;
  785. }
  786. ao2_ref(format, +1);
  787. type->format = format;
  788. type->asterisk_format = 1;
  789. type->payload = payload;
  790. ast_rwlock_wrlock(&codecs->codecs_lock);
  791. if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
  792. ao2_cleanup(AST_VECTOR_GET(&codecs->payloads, payload));
  793. }
  794. if (AST_VECTOR_REPLACE(&codecs->payloads, payload, type)) {
  795. ao2_ref(type, -1);
  796. ast_rwlock_unlock(&codecs->codecs_lock);
  797. return -1;
  798. }
  799. ast_rwlock_unlock(&codecs->codecs_lock);
  800. return 0;
  801. }
  802. struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
  803. {
  804. struct ast_rtp_payload_type *type;
  805. struct ast_format *format = NULL;
  806. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  807. return NULL;
  808. }
  809. ast_rwlock_rdlock(&codecs->codecs_lock);
  810. if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
  811. type = AST_VECTOR_GET(&codecs->payloads, payload);
  812. if (type && type->asterisk_format) {
  813. format = ao2_bump(type->format);
  814. }
  815. }
  816. ast_rwlock_unlock(&codecs->codecs_lock);
  817. return format;
  818. }
  819. void ast_rtp_codecs_set_framing(struct ast_rtp_codecs *codecs, unsigned int framing)
  820. {
  821. if (!framing) {
  822. return;
  823. }
  824. ast_rwlock_wrlock(&codecs->codecs_lock);
  825. codecs->framing = framing;
  826. ast_rwlock_unlock(&codecs->codecs_lock);
  827. }
  828. unsigned int ast_rtp_codecs_get_framing(struct ast_rtp_codecs *codecs)
  829. {
  830. unsigned int framing;
  831. ast_rwlock_rdlock(&codecs->codecs_lock);
  832. framing = codecs->framing;
  833. ast_rwlock_unlock(&codecs->codecs_lock);
  834. return framing;
  835. }
  836. void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
  837. {
  838. int i;
  839. ast_format_cap_remove_by_type(astformats, AST_MEDIA_TYPE_UNKNOWN);
  840. *nonastformats = 0;
  841. ast_rwlock_rdlock(&codecs->codecs_lock);
  842. for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
  843. struct ast_rtp_payload_type *type;
  844. type = AST_VECTOR_GET(&codecs->payloads, i);
  845. if (!type) {
  846. continue;
  847. }
  848. if (type->asterisk_format) {
  849. ast_format_cap_append(astformats, type->format, 0);
  850. } else {
  851. *nonastformats |= type->rtp_code;
  852. }
  853. }
  854. if (codecs->framing) {
  855. ast_format_cap_set_framing(astformats, codecs->framing);
  856. }
  857. ast_rwlock_unlock(&codecs->codecs_lock);
  858. }
  859. int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
  860. {
  861. struct ast_rtp_payload_type *type;
  862. int i;
  863. int payload = -1;
  864. ast_rwlock_rdlock(&codecs->codecs_lock);
  865. for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
  866. type = AST_VECTOR_GET(&codecs->payloads, i);
  867. if (!type) {
  868. continue;
  869. }
  870. if ((asterisk_format && format && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL)
  871. || (!asterisk_format && type->rtp_code == code)) {
  872. payload = i;
  873. break;
  874. }
  875. }
  876. ast_rwlock_unlock(&codecs->codecs_lock);
  877. if (payload < 0) {
  878. ast_rwlock_rdlock(&static_RTP_PT_lock);
  879. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  880. if (!static_RTP_PT[i]) {
  881. continue;
  882. }
  883. if (static_RTP_PT[i]->asterisk_format && asterisk_format && format &&
  884. (ast_format_cmp(format, static_RTP_PT[i]->format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  885. payload = i;
  886. break;
  887. } else if (!static_RTP_PT[i]->asterisk_format && !asterisk_format &&
  888. (static_RTP_PT[i]->rtp_code == code)) {
  889. payload = i;
  890. break;
  891. }
  892. }
  893. ast_rwlock_unlock(&static_RTP_PT_lock);
  894. }
  895. return payload;
  896. }
  897. int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
  898. {
  899. struct ast_rtp_payload_type *type;
  900. int res = -1;
  901. ast_rwlock_rdlock(&codecs->codecs_lock);
  902. if (code < AST_VECTOR_SIZE(&codecs->payloads)) {
  903. type = AST_VECTOR_GET(&codecs->payloads, code);
  904. if (type) {
  905. res = type->payload;
  906. }
  907. }
  908. ast_rwlock_unlock(&codecs->codecs_lock);
  909. return res;
  910. }
  911. const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
  912. {
  913. int i;
  914. const char *res = "";
  915. ast_rwlock_rdlock(&mime_types_lock);
  916. for (i = 0; i < mime_types_len; i++) {
  917. if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
  918. (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  919. if ((ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL) &&
  920. (options & AST_RTP_OPT_G726_NONSTANDARD)) {
  921. res = "G726-32";
  922. break;
  923. } else {
  924. res = ast_rtp_mime_types[i].subtype;
  925. break;
  926. }
  927. } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
  928. ast_rtp_mime_types[i].payload_type.rtp_code == code) {
  929. res = ast_rtp_mime_types[i].subtype;
  930. break;
  931. }
  932. }
  933. ast_rwlock_unlock(&mime_types_lock);
  934. return res;
  935. }
  936. unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
  937. {
  938. unsigned int i;
  939. unsigned int res = 0;
  940. ast_rwlock_rdlock(&mime_types_lock);
  941. for (i = 0; i < mime_types_len; ++i) {
  942. if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
  943. (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  944. res = ast_rtp_mime_types[i].sample_rate;
  945. break;
  946. } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
  947. ast_rtp_mime_types[i].payload_type.rtp_code == code) {
  948. res = ast_rtp_mime_types[i].sample_rate;
  949. break;
  950. }
  951. }
  952. ast_rwlock_unlock(&mime_types_lock);
  953. return res;
  954. }
  955. 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)
  956. {
  957. int found = 0;
  958. const char *name;
  959. if (!buf) {
  960. return NULL;
  961. }
  962. if (asterisk_format) {
  963. int x;
  964. struct ast_format *tmp_fmt;
  965. for (x = 0; x < ast_format_cap_count(ast_format_capability); x++) {
  966. tmp_fmt = ast_format_cap_get_format(ast_format_capability, x);
  967. name = ast_rtp_lookup_mime_subtype2(asterisk_format, tmp_fmt, 0, options);
  968. ao2_ref(tmp_fmt, -1);
  969. ast_str_append(&buf, 0, "%s|", name);
  970. found = 1;
  971. }
  972. } else {
  973. int x;
  974. ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
  975. for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
  976. if (rtp_capability & x) {
  977. name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
  978. ast_str_append(&buf, 0, "%s|", name);
  979. found = 1;
  980. }
  981. }
  982. }
  983. ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
  984. return ast_str_buffer(buf);
  985. }
  986. int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
  987. {
  988. int res;
  989. if (instance->engine->dtmf_begin) {
  990. ao2_lock(instance);
  991. res = instance->engine->dtmf_begin(instance, digit);
  992. ao2_unlock(instance);
  993. } else {
  994. res = -1;
  995. }
  996. return res;
  997. }
  998. int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
  999. {
  1000. int res;
  1001. if (instance->engine->dtmf_end) {
  1002. ao2_lock(instance);
  1003. res = instance->engine->dtmf_end(instance, digit);
  1004. ao2_unlock(instance);
  1005. } else {
  1006. res = -1;
  1007. }
  1008. return res;
  1009. }
  1010. int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
  1011. {
  1012. int res;
  1013. if (instance->engine->dtmf_end_with_duration) {
  1014. ao2_lock(instance);
  1015. res = instance->engine->dtmf_end_with_duration(instance, digit, duration);
  1016. ao2_unlock(instance);
  1017. } else {
  1018. res = -1;
  1019. }
  1020. return res;
  1021. }
  1022. int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
  1023. {
  1024. int res;
  1025. if (instance->engine->dtmf_mode_set) {
  1026. ao2_lock(instance);
  1027. res = instance->engine->dtmf_mode_set(instance, dtmf_mode);
  1028. ao2_unlock(instance);
  1029. } else {
  1030. res = -1;
  1031. }
  1032. return res;
  1033. }
  1034. enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
  1035. {
  1036. int res;
  1037. if (instance->engine->dtmf_mode_get) {
  1038. ao2_lock(instance);
  1039. res = instance->engine->dtmf_mode_get(instance);
  1040. ao2_unlock(instance);
  1041. } else {
  1042. res = 0;
  1043. }
  1044. return res;
  1045. }
  1046. void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
  1047. {
  1048. if (instance->engine->update_source) {
  1049. ao2_lock(instance);
  1050. instance->engine->update_source(instance);
  1051. ao2_unlock(instance);
  1052. }
  1053. }
  1054. void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
  1055. {
  1056. if (instance->engine->change_source) {
  1057. ao2_lock(instance);
  1058. instance->engine->change_source(instance);
  1059. ao2_unlock(instance);
  1060. }
  1061. }
  1062. int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
  1063. {
  1064. int res;
  1065. if (instance->engine->qos) {
  1066. ao2_lock(instance);
  1067. res = instance->engine->qos(instance, tos, cos, desc);
  1068. ao2_unlock(instance);
  1069. } else {
  1070. res = -1;
  1071. }
  1072. return res;
  1073. }
  1074. void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
  1075. {
  1076. if (instance->engine->stop) {
  1077. ao2_lock(instance);
  1078. instance->engine->stop(instance);
  1079. ao2_unlock(instance);
  1080. }
  1081. }
  1082. int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
  1083. {
  1084. int res;
  1085. if (instance->engine->fd) {
  1086. ao2_lock(instance);
  1087. res = instance->engine->fd(instance, rtcp);
  1088. ao2_unlock(instance);
  1089. } else {
  1090. res = -1;
  1091. }
  1092. return res;
  1093. }
  1094. struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
  1095. {
  1096. struct ast_rtp_glue *glue = NULL;
  1097. AST_RWLIST_RDLOCK(&glues);
  1098. AST_RWLIST_TRAVERSE(&glues, glue, entry) {
  1099. if (!strcasecmp(glue->type, type)) {
  1100. break;
  1101. }
  1102. }
  1103. AST_RWLIST_UNLOCK(&glues);
  1104. return glue;
  1105. }
  1106. /*!
  1107. * \brief Conditionally unref an rtp instance
  1108. */
  1109. static void unref_instance_cond(struct ast_rtp_instance **instance)
  1110. {
  1111. if (*instance) {
  1112. ao2_ref(*instance, -1);
  1113. *instance = NULL;
  1114. }
  1115. }
  1116. struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
  1117. {
  1118. struct ast_rtp_instance *bridged;
  1119. ao2_lock(instance);
  1120. bridged = instance->bridged;
  1121. ao2_unlock(instance);
  1122. return bridged;
  1123. }
  1124. void ast_rtp_instance_set_bridged(struct ast_rtp_instance *instance, struct ast_rtp_instance *bridged)
  1125. {
  1126. ao2_lock(instance);
  1127. instance->bridged = bridged;
  1128. ao2_unlock(instance);
  1129. }
  1130. void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
  1131. {
  1132. struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
  1133. *vinstance_dst = NULL, *vinstance_src = NULL,
  1134. *tinstance_dst = NULL, *tinstance_src = NULL;
  1135. struct ast_rtp_glue *glue_dst, *glue_src;
  1136. enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
  1137. enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
  1138. struct ast_format_cap *cap_dst = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1139. struct ast_format_cap *cap_src = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1140. /* Lock both channels so we can look for the glue that binds them together */
  1141. ast_channel_lock_both(c_dst, c_src);
  1142. if (!cap_src || !cap_dst) {
  1143. goto done;
  1144. }
  1145. /* Grab glue that binds each channel to something using the RTP engine */
  1146. 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))) {
  1147. ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
  1148. goto done;
  1149. }
  1150. audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
  1151. video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
  1152. audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
  1153. video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
  1154. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  1155. 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)) {
  1156. audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
  1157. }
  1158. 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)) {
  1159. audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
  1160. }
  1161. 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) {
  1162. glue_dst->get_codec(c_dst, cap_dst);
  1163. }
  1164. 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) {
  1165. glue_src->get_codec(c_src, cap_src);
  1166. }
  1167. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  1168. if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
  1169. goto done;
  1170. }
  1171. /* Make sure we have matching codecs */
  1172. if (!ast_format_cap_iscompatible(cap_dst, cap_src)) {
  1173. goto done;
  1174. }
  1175. ast_rtp_codecs_payloads_copy(&instance_src->codecs, &instance_dst->codecs, instance_dst);
  1176. if (vinstance_dst && vinstance_src) {
  1177. ast_rtp_codecs_payloads_copy(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
  1178. }
  1179. if (tinstance_dst && tinstance_src) {
  1180. ast_rtp_codecs_payloads_copy(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
  1181. }
  1182. if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
  1183. ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
  1184. ast_channel_name(c_dst), ast_channel_name(c_src));
  1185. } else {
  1186. ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
  1187. ast_channel_name(c_dst), ast_channel_name(c_src));
  1188. }
  1189. done:
  1190. ast_channel_unlock(c_dst);
  1191. ast_channel_unlock(c_src);
  1192. ao2_cleanup(cap_dst);
  1193. ao2_cleanup(cap_src);
  1194. unref_instance_cond(&instance_dst);
  1195. unref_instance_cond(&instance_src);
  1196. unref_instance_cond(&vinstance_dst);
  1197. unref_instance_cond(&vinstance_src);
  1198. unref_instance_cond(&tinstance_dst);
  1199. unref_instance_cond(&tinstance_src);
  1200. }
  1201. int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
  1202. {
  1203. struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
  1204. *vinstance0 = NULL, *vinstance1 = NULL,
  1205. *tinstance0 = NULL, *tinstance1 = NULL;
  1206. struct ast_rtp_glue *glue0, *glue1;
  1207. enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1208. enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1209. struct ast_format_cap *cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1210. struct ast_format_cap *cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  1211. /* If there is no second channel just immediately bail out, we are of no use in that scenario */
  1212. if (!c1 || !cap1 || !cap0) {
  1213. ao2_cleanup(cap0);
  1214. ao2_cleanup(cap1);
  1215. return -1;
  1216. }
  1217. /* Lock both channels so we can look for the glue that binds them together */
  1218. ast_channel_lock_both(c0, c1);
  1219. /* Grab glue that binds each channel to something using the RTP engine */
  1220. if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
  1221. ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
  1222. goto done;
  1223. }
  1224. audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
  1225. video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
  1226. audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
  1227. video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
  1228. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  1229. 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)) {
  1230. audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1231. }
  1232. 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)) {
  1233. audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1234. }
  1235. 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) {
  1236. glue0->get_codec(c0, cap0);
  1237. }
  1238. 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) {
  1239. glue1->get_codec(c1, cap1);
  1240. }
  1241. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  1242. if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
  1243. goto done;
  1244. }
  1245. /* Make sure we have matching codecs */
  1246. if (!ast_format_cap_iscompatible(cap0, cap1)) {
  1247. goto done;
  1248. }
  1249. /* Bridge media early */
  1250. if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
  1251. ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
  1252. }
  1253. done:
  1254. ast_channel_unlock(c0);
  1255. ast_channel_unlock(c1);
  1256. ao2_cleanup(cap0);
  1257. ao2_cleanup(cap1);
  1258. unref_instance_cond(&instance0);
  1259. unref_instance_cond(&instance1);
  1260. unref_instance_cond(&vinstance0);
  1261. unref_instance_cond(&vinstance1);
  1262. unref_instance_cond(&tinstance0);
  1263. unref_instance_cond(&tinstance1);
  1264. ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
  1265. return 0;
  1266. }
  1267. int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
  1268. {
  1269. int res;
  1270. if (instance->engine->red_init) {
  1271. ao2_lock(instance);
  1272. res = instance->engine->red_init(instance, buffer_time, payloads, generations);
  1273. ao2_unlock(instance);
  1274. } else {
  1275. res = -1;
  1276. }
  1277. return res;
  1278. }
  1279. int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
  1280. {
  1281. int res;
  1282. if (instance->engine->red_buffer) {
  1283. ao2_lock(instance);
  1284. res = instance->engine->red_buffer(instance, frame);
  1285. ao2_unlock(instance);
  1286. } else {
  1287. res = -1;
  1288. }
  1289. return res;
  1290. }
  1291. int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
  1292. {
  1293. int res;
  1294. if (instance->engine->get_stat) {
  1295. ao2_lock(instance);
  1296. res = instance->engine->get_stat(instance, stats, stat);
  1297. ao2_unlock(instance);
  1298. } else {
  1299. res = -1;
  1300. }
  1301. return res;
  1302. }
  1303. char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
  1304. {
  1305. struct ast_rtp_instance_stats stats = { 0, };
  1306. enum ast_rtp_instance_stat stat;
  1307. /* Determine what statistics we will need to retrieve based on field passed in */
  1308. if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
  1309. stat = AST_RTP_INSTANCE_STAT_ALL;
  1310. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
  1311. stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
  1312. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
  1313. stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
  1314. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
  1315. stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
  1316. } else {
  1317. return NULL;
  1318. }
  1319. /* Attempt to actually retrieve the statistics we need to generate the quality string */
  1320. if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
  1321. return NULL;
  1322. }
  1323. /* Now actually fill the buffer with the good information */
  1324. if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
  1325. snprintf(buf, size, "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
  1326. stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
  1327. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
  1328. snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
  1329. 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));
  1330. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
  1331. snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
  1332. 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));
  1333. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
  1334. snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
  1335. }
  1336. return buf;
  1337. }
  1338. void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
  1339. {
  1340. char quality_buf[AST_MAX_USER_FIELD];
  1341. char *quality;
  1342. struct ast_channel *bridge;
  1343. bridge = ast_channel_bridge_peer(chan);
  1344. if (bridge) {
  1345. ast_channel_lock_both(chan, bridge);
  1346. ast_channel_stage_snapshot(bridge);
  1347. } else {
  1348. ast_channel_lock(chan);
  1349. }
  1350. ast_channel_stage_snapshot(chan);
  1351. quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY,
  1352. quality_buf, sizeof(quality_buf));
  1353. if (quality) {
  1354. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
  1355. if (bridge) {
  1356. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
  1357. }
  1358. }
  1359. quality = ast_rtp_instance_get_quality(instance,
  1360. AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf));
  1361. if (quality) {
  1362. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
  1363. if (bridge) {
  1364. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
  1365. }
  1366. }
  1367. quality = ast_rtp_instance_get_quality(instance,
  1368. AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf));
  1369. if (quality) {
  1370. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
  1371. if (bridge) {
  1372. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
  1373. }
  1374. }
  1375. quality = ast_rtp_instance_get_quality(instance,
  1376. AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf));
  1377. if (quality) {
  1378. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
  1379. if (bridge) {
  1380. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
  1381. }
  1382. }
  1383. ast_channel_stage_snapshot_done(chan);
  1384. ast_channel_unlock(chan);
  1385. if (bridge) {
  1386. ast_channel_stage_snapshot_done(bridge);
  1387. ast_channel_unlock(bridge);
  1388. ast_channel_unref(bridge);
  1389. }
  1390. }
  1391. int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
  1392. {
  1393. int res;
  1394. if (instance->engine->set_read_format) {
  1395. ao2_lock(instance);
  1396. res = instance->engine->set_read_format(instance, format);
  1397. ao2_unlock(instance);
  1398. } else {
  1399. res = -1;
  1400. }
  1401. return res;
  1402. }
  1403. int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
  1404. {
  1405. int res;
  1406. if (instance->engine->set_read_format) {
  1407. ao2_lock(instance);
  1408. res = instance->engine->set_write_format(instance, format);
  1409. ao2_unlock(instance);
  1410. } else {
  1411. res = -1;
  1412. }
  1413. return res;
  1414. }
  1415. /* XXX Nothing calls this */
  1416. int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
  1417. {
  1418. struct ast_rtp_glue *glue;
  1419. struct ast_rtp_instance *peer_instance = NULL;
  1420. int res = -1;
  1421. if (!instance->engine->make_compatible) {
  1422. return -1;
  1423. }
  1424. ast_channel_lock(peer);
  1425. if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
  1426. ast_channel_unlock(peer);
  1427. return -1;
  1428. }
  1429. glue->get_rtp_info(peer, &peer_instance);
  1430. if (!peer_instance) {
  1431. ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
  1432. ast_channel_unlock(peer);
  1433. return -1;
  1434. }
  1435. if (peer_instance->engine != instance->engine) {
  1436. ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
  1437. ast_channel_unlock(peer);
  1438. ao2_ref(peer_instance, -1);
  1439. return -1;
  1440. }
  1441. /*
  1442. * XXX Good thing nothing calls this function because we would need
  1443. * deadlock avoidance to get the two instance locks.
  1444. */
  1445. res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
  1446. ast_channel_unlock(peer);
  1447. ao2_ref(peer_instance, -1);
  1448. peer_instance = NULL;
  1449. return res;
  1450. }
  1451. 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)
  1452. {
  1453. if (instance->engine->available_formats) {
  1454. ao2_lock(instance);
  1455. instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
  1456. ao2_unlock(instance);
  1457. if (ast_format_cap_count(result)) {
  1458. return;
  1459. }
  1460. }
  1461. ast_translate_available_formats(to_endpoint, to_asterisk, result);
  1462. }
  1463. int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
  1464. {
  1465. int res;
  1466. if (instance->engine->activate) {
  1467. ao2_lock(instance);
  1468. res = instance->engine->activate(instance);
  1469. ao2_unlock(instance);
  1470. } else {
  1471. res = 0;
  1472. }
  1473. return res;
  1474. }
  1475. void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
  1476. struct ast_sockaddr *suggestion,
  1477. const char *username)
  1478. {
  1479. if (instance->engine->stun_request) {
  1480. instance->engine->stun_request(instance, suggestion, username);
  1481. }
  1482. }
  1483. void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
  1484. {
  1485. instance->timeout = timeout;
  1486. }
  1487. void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
  1488. {
  1489. instance->holdtimeout = timeout;
  1490. }
  1491. void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
  1492. {
  1493. instance->keepalive = interval;
  1494. }
  1495. int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
  1496. {
  1497. return instance->timeout;
  1498. }
  1499. int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
  1500. {
  1501. return instance->holdtimeout;
  1502. }
  1503. int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
  1504. {
  1505. return instance->keepalive;
  1506. }
  1507. struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
  1508. {
  1509. return instance->engine;
  1510. }
  1511. struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
  1512. {
  1513. return instance->glue;
  1514. }
  1515. int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
  1516. {
  1517. if (res_srtp || res_srtp_policy) {
  1518. return -1;
  1519. }
  1520. if (!srtp_res || !policy_res) {
  1521. return -1;
  1522. }
  1523. res_srtp = srtp_res;
  1524. res_srtp_policy = policy_res;
  1525. return 0;
  1526. }
  1527. void ast_rtp_engine_unregister_srtp(void)
  1528. {
  1529. res_srtp = NULL;
  1530. res_srtp_policy = NULL;
  1531. }
  1532. int ast_rtp_engine_srtp_is_registered(void)
  1533. {
  1534. return res_srtp && res_srtp_policy;
  1535. }
  1536. int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy, int rtcp)
  1537. {
  1538. int res = 0;
  1539. struct ast_srtp **srtp;
  1540. if (!res_srtp) {
  1541. return -1;
  1542. }
  1543. srtp = rtcp ? &instance->rtcp_srtp : &instance->srtp;
  1544. if (!*srtp) {
  1545. res = res_srtp->create(srtp, instance, remote_policy);
  1546. } else {
  1547. res = res_srtp->replace(srtp, instance, remote_policy);
  1548. }
  1549. if (!res) {
  1550. res = res_srtp->add_stream(*srtp, local_policy);
  1551. }
  1552. return res;
  1553. }
  1554. struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance, int rtcp)
  1555. {
  1556. if (rtcp && instance->rtcp_srtp) {
  1557. return instance->rtcp_srtp;
  1558. } else {
  1559. return instance->srtp;
  1560. }
  1561. }
  1562. int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
  1563. {
  1564. int res;
  1565. if (instance->engine->sendcng) {
  1566. ao2_lock(instance);
  1567. res = instance->engine->sendcng(instance, level);
  1568. ao2_unlock(instance);
  1569. } else {
  1570. res = -1;
  1571. }
  1572. return res;
  1573. }
  1574. static void rtp_ice_wrap_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
  1575. {
  1576. ao2_lock(instance);
  1577. instance->engine->ice->set_authentication(instance, ufrag, password);
  1578. ao2_unlock(instance);
  1579. }
  1580. static void rtp_ice_wrap_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
  1581. {
  1582. ao2_lock(instance);
  1583. instance->engine->ice->add_remote_candidate(instance, candidate);
  1584. ao2_unlock(instance);
  1585. }
  1586. static void rtp_ice_wrap_start(struct ast_rtp_instance *instance)
  1587. {
  1588. ao2_lock(instance);
  1589. instance->engine->ice->start(instance);
  1590. ao2_unlock(instance);
  1591. }
  1592. static void rtp_ice_wrap_stop(struct ast_rtp_instance *instance)
  1593. {
  1594. ao2_lock(instance);
  1595. instance->engine->ice->stop(instance);
  1596. ao2_unlock(instance);
  1597. }
  1598. static const char *rtp_ice_wrap_get_ufrag(struct ast_rtp_instance *instance)
  1599. {
  1600. const char *ufrag;
  1601. ao2_lock(instance);
  1602. ufrag = instance->engine->ice->get_ufrag(instance);
  1603. ao2_unlock(instance);
  1604. return ufrag;
  1605. }
  1606. static const char *rtp_ice_wrap_get_password(struct ast_rtp_instance *instance)
  1607. {
  1608. const char *password;
  1609. ao2_lock(instance);
  1610. password = instance->engine->ice->get_password(instance);
  1611. ao2_unlock(instance);
  1612. return password;
  1613. }
  1614. static struct ao2_container *rtp_ice_wrap_get_local_candidates(struct ast_rtp_instance *instance)
  1615. {
  1616. struct ao2_container *local_candidates;
  1617. ao2_lock(instance);
  1618. local_candidates = instance->engine->ice->get_local_candidates(instance);
  1619. ao2_unlock(instance);
  1620. return local_candidates;
  1621. }
  1622. static void rtp_ice_wrap_ice_lite(struct ast_rtp_instance *instance)
  1623. {
  1624. ao2_lock(instance);
  1625. instance->engine->ice->ice_lite(instance);
  1626. ao2_unlock(instance);
  1627. }
  1628. static void rtp_ice_wrap_set_role(struct ast_rtp_instance *instance,
  1629. enum ast_rtp_ice_role role)
  1630. {
  1631. ao2_lock(instance);
  1632. instance->engine->ice->set_role(instance, role);
  1633. ao2_unlock(instance);
  1634. }
  1635. static void rtp_ice_wrap_turn_request(struct ast_rtp_instance *instance,
  1636. enum ast_rtp_ice_component_type component, enum ast_transport transport,
  1637. const char *server, unsigned int port, const char *username, const char *password)
  1638. {
  1639. ao2_lock(instance);
  1640. instance->engine->ice->turn_request(instance, component, transport, server, port,
  1641. username, password);
  1642. ao2_unlock(instance);
  1643. }
  1644. static void rtp_ice_wrap_change_components(struct ast_rtp_instance *instance,
  1645. int num_components)
  1646. {
  1647. ao2_lock(instance);
  1648. instance->engine->ice->change_components(instance, num_components);
  1649. ao2_unlock(instance);
  1650. }
  1651. static struct ast_rtp_engine_ice rtp_ice_wrappers = {
  1652. .set_authentication = rtp_ice_wrap_set_authentication,
  1653. .add_remote_candidate = rtp_ice_wrap_add_remote_candidate,
  1654. .start = rtp_ice_wrap_start,
  1655. .stop = rtp_ice_wrap_stop,
  1656. .get_ufrag = rtp_ice_wrap_get_ufrag,
  1657. .get_password = rtp_ice_wrap_get_password,
  1658. .get_local_candidates = rtp_ice_wrap_get_local_candidates,
  1659. .ice_lite = rtp_ice_wrap_ice_lite,
  1660. .set_role = rtp_ice_wrap_set_role,
  1661. .turn_request = rtp_ice_wrap_turn_request,
  1662. .change_components = rtp_ice_wrap_change_components,
  1663. };
  1664. struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
  1665. {
  1666. if (instance->engine->ice) {
  1667. return &rtp_ice_wrappers;
  1668. }
  1669. /* ICE not available */
  1670. return NULL;
  1671. }
  1672. static int rtp_dtls_wrap_set_configuration(struct ast_rtp_instance *instance,
  1673. const struct ast_rtp_dtls_cfg *dtls_cfg)
  1674. {
  1675. int set_configuration;
  1676. ao2_lock(instance);
  1677. set_configuration = instance->engine->dtls->set_configuration(instance, dtls_cfg);
  1678. ao2_unlock(instance);
  1679. return set_configuration;
  1680. }
  1681. static int rtp_dtls_wrap_active(struct ast_rtp_instance *instance)
  1682. {
  1683. int active;
  1684. ao2_lock(instance);
  1685. active = instance->engine->dtls->active(instance);
  1686. ao2_unlock(instance);
  1687. return active;
  1688. }
  1689. static void rtp_dtls_wrap_stop(struct ast_rtp_instance *instance)
  1690. {
  1691. ao2_lock(instance);
  1692. instance->engine->dtls->stop(instance);
  1693. ao2_unlock(instance);
  1694. }
  1695. static void rtp_dtls_wrap_reset(struct ast_rtp_instance *instance)
  1696. {
  1697. ao2_lock(instance);
  1698. instance->engine->dtls->reset(instance);
  1699. ao2_unlock(instance);
  1700. }
  1701. static enum ast_rtp_dtls_connection rtp_dtls_wrap_get_connection(struct ast_rtp_instance *instance)
  1702. {
  1703. enum ast_rtp_dtls_connection get_connection;
  1704. ao2_lock(instance);
  1705. get_connection = instance->engine->dtls->get_connection(instance);
  1706. ao2_unlock(instance);
  1707. return get_connection;
  1708. }
  1709. static enum ast_rtp_dtls_setup rtp_dtls_wrap_get_setup(struct ast_rtp_instance *instance)
  1710. {
  1711. enum ast_rtp_dtls_setup get_setup;
  1712. ao2_lock(instance);
  1713. get_setup = instance->engine->dtls->get_setup(instance);
  1714. ao2_unlock(instance);
  1715. return get_setup;
  1716. }
  1717. static void rtp_dtls_wrap_set_setup(struct ast_rtp_instance *instance,
  1718. enum ast_rtp_dtls_setup setup)
  1719. {
  1720. ao2_lock(instance);
  1721. instance->engine->dtls->set_setup(instance, setup);
  1722. ao2_unlock(instance);
  1723. }
  1724. static void rtp_dtls_wrap_set_fingerprint(struct ast_rtp_instance *instance,
  1725. enum ast_rtp_dtls_hash hash, const char *fingerprint)
  1726. {
  1727. ao2_lock(instance);
  1728. instance->engine->dtls->set_fingerprint(instance, hash, fingerprint);
  1729. ao2_unlock(instance);
  1730. }
  1731. static enum ast_rtp_dtls_hash rtp_dtls_wrap_get_fingerprint_hash(struct ast_rtp_instance *instance)
  1732. {
  1733. enum ast_rtp_dtls_hash get_fingerprint_hash;
  1734. ao2_lock(instance);
  1735. get_fingerprint_hash = instance->engine->dtls->get_fingerprint_hash(instance);
  1736. ao2_unlock(instance);
  1737. return get_fingerprint_hash;
  1738. }
  1739. static const char *rtp_dtls_wrap_get_fingerprint(struct ast_rtp_instance *instance)
  1740. {
  1741. const char *get_fingerprint;
  1742. ao2_lock(instance);
  1743. get_fingerprint = instance->engine->dtls->get_fingerprint(instance);
  1744. ao2_unlock(instance);
  1745. return get_fingerprint;
  1746. }
  1747. static struct ast_rtp_engine_dtls rtp_dtls_wrappers = {
  1748. .set_configuration = rtp_dtls_wrap_set_configuration,
  1749. .active = rtp_dtls_wrap_active,
  1750. .stop = rtp_dtls_wrap_stop,
  1751. .reset = rtp_dtls_wrap_reset,
  1752. .get_connection = rtp_dtls_wrap_get_connection,
  1753. .get_setup = rtp_dtls_wrap_get_setup,
  1754. .set_setup = rtp_dtls_wrap_set_setup,
  1755. .set_fingerprint = rtp_dtls_wrap_set_fingerprint,
  1756. .get_fingerprint_hash = rtp_dtls_wrap_get_fingerprint_hash,
  1757. .get_fingerprint = rtp_dtls_wrap_get_fingerprint,
  1758. };
  1759. struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
  1760. {
  1761. if (instance->engine->dtls) {
  1762. return &rtp_dtls_wrappers;
  1763. }
  1764. /* DTLS not available */
  1765. return NULL;
  1766. }
  1767. int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
  1768. {
  1769. if (!strcasecmp(name, "dtlsenable")) {
  1770. dtls_cfg->enabled = ast_true(value) ? 1 : 0;
  1771. } else if (!strcasecmp(name, "dtlsverify")) {
  1772. if (!strcasecmp(value, "yes")) {
  1773. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT | AST_RTP_DTLS_VERIFY_CERTIFICATE;
  1774. } else if (!strcasecmp(value, "fingerprint")) {
  1775. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
  1776. } else if (!strcasecmp(value, "certificate")) {
  1777. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_CERTIFICATE;
  1778. } else if (!strcasecmp(value, "no")) {
  1779. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_NONE;
  1780. } else {
  1781. return -1;
  1782. }
  1783. } else if (!strcasecmp(name, "dtlsrekey")) {
  1784. if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
  1785. return -1;
  1786. }
  1787. } else if (!strcasecmp(name, "dtlscertfile")) {
  1788. if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
  1789. ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
  1790. return -1;
  1791. }
  1792. ast_free(dtls_cfg->certfile);
  1793. dtls_cfg->certfile = ast_strdup(value);
  1794. } else if (!strcasecmp(name, "dtlsprivatekey")) {
  1795. if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
  1796. ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
  1797. return -1;
  1798. }
  1799. ast_free(dtls_cfg->pvtfile);
  1800. dtls_cfg->pvtfile = ast_strdup(value);
  1801. } else if (!strcasecmp(name, "dtlscipher")) {
  1802. ast_free(dtls_cfg->cipher);
  1803. dtls_cfg->cipher = ast_strdup(value);
  1804. } else if (!strcasecmp(name, "dtlscafile")) {
  1805. if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
  1806. ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
  1807. return -1;
  1808. }
  1809. ast_free(dtls_cfg->cafile);
  1810. dtls_cfg->cafile = ast_strdup(value);
  1811. } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
  1812. if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
  1813. ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
  1814. return -1;
  1815. }
  1816. ast_free(dtls_cfg->capath);
  1817. dtls_cfg->capath = ast_strdup(value);
  1818. } else if (!strcasecmp(name, "dtlssetup")) {
  1819. if (!strcasecmp(value, "active")) {
  1820. dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
  1821. } else if (!strcasecmp(value, "passive")) {
  1822. dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
  1823. } else if (!strcasecmp(value, "actpass")) {
  1824. dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
  1825. }
  1826. } else if (!strcasecmp(name, "dtlsfingerprint")) {
  1827. if (!strcasecmp(value, "sha-256")) {
  1828. dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA256;
  1829. } else if (!strcasecmp(value, "sha-1")) {
  1830. dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA1;
  1831. }
  1832. } else {
  1833. return -1;
  1834. }
  1835. return 0;
  1836. }
  1837. void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
  1838. {
  1839. ast_rtp_dtls_cfg_free(dst_cfg); /* Prevent a double-call leaking memory via ast_strdup */
  1840. dst_cfg->enabled = src_cfg->enabled;
  1841. dst_cfg->verify = src_cfg->verify;
  1842. dst_cfg->rekey = src_cfg->rekey;
  1843. dst_cfg->suite = src_cfg->suite;
  1844. dst_cfg->hash = src_cfg->hash;
  1845. dst_cfg->certfile = ast_strdup(src_cfg->certfile);
  1846. dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
  1847. dst_cfg->cipher = ast_strdup(src_cfg->cipher);
  1848. dst_cfg->cafile = ast_strdup(src_cfg->cafile);
  1849. dst_cfg->capath = ast_strdup(src_cfg->capath);
  1850. dst_cfg->default_setup = src_cfg->default_setup;
  1851. }
  1852. void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
  1853. {
  1854. ast_free(dtls_cfg->certfile);
  1855. dtls_cfg->certfile = NULL;
  1856. ast_free(dtls_cfg->pvtfile);
  1857. dtls_cfg->pvtfile = NULL;
  1858. ast_free(dtls_cfg->cipher);
  1859. dtls_cfg->cipher = NULL;
  1860. ast_free(dtls_cfg->cafile);
  1861. dtls_cfg->cafile = NULL;
  1862. ast_free(dtls_cfg->capath);
  1863. dtls_cfg->capath = NULL;
  1864. }
  1865. /*! \internal
  1866. * \brief Small helper routine that cleans up entry i in
  1867. * \c ast_rtp_mime_types.
  1868. */
  1869. static void rtp_engine_mime_type_cleanup(int i)
  1870. {
  1871. ao2_cleanup(ast_rtp_mime_types[i].payload_type.format);
  1872. memset(&ast_rtp_mime_types[i], 0, sizeof(struct ast_rtp_mime_type));
  1873. }
  1874. static void set_next_mime_type(struct ast_format *format, int rtp_code, const char *type, const char *subtype, unsigned int sample_rate)
  1875. {
  1876. int x;
  1877. ast_rwlock_wrlock(&mime_types_lock);
  1878. x = mime_types_len;
  1879. if (ARRAY_LEN(ast_rtp_mime_types) <= x) {
  1880. ast_rwlock_unlock(&mime_types_lock);
  1881. return;
  1882. }
  1883. /* Make sure any previous value in ast_rtp_mime_types is cleaned up */
  1884. memset(&ast_rtp_mime_types[x], 0, sizeof(struct ast_rtp_mime_type));
  1885. if (format) {
  1886. ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
  1887. ast_rtp_mime_types[x].payload_type.format = ao2_bump(format);
  1888. } else {
  1889. ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
  1890. }
  1891. ast_copy_string(ast_rtp_mime_types[x].type, type, sizeof(ast_rtp_mime_types[x].type));
  1892. ast_copy_string(ast_rtp_mime_types[x].subtype, subtype, sizeof(ast_rtp_mime_types[x].subtype));
  1893. ast_rtp_mime_types[x].sample_rate = sample_rate;
  1894. mime_types_len++;
  1895. ast_rwlock_unlock(&mime_types_lock);
  1896. }
  1897. static void add_static_payload(int map, struct ast_format *format, int rtp_code)
  1898. {
  1899. int x;
  1900. struct ast_rtp_payload_type *type;
  1901. /*
  1902. * ARRAY_LEN's result is cast to an int so 'map' is not autocast to a size_t,
  1903. * which if negative would cause an assertion.
  1904. */
  1905. ast_assert(map < (int)ARRAY_LEN(static_RTP_PT));
  1906. ast_rwlock_wrlock(&static_RTP_PT_lock);
  1907. if (map < 0) {
  1908. /* find next available dynamic payload slot */
  1909. for (x = AST_RTP_PT_FIRST_DYNAMIC; x < AST_RTP_MAX_PT; ++x) {
  1910. if (!static_RTP_PT[x]) {
  1911. map = x;
  1912. break;
  1913. }
  1914. }
  1915. /* http://www.iana.org/assignments/rtp-parameters
  1916. * RFC 3551, Section 3: "[...] applications which need to define more
  1917. * than 32 dynamic payload types MAY bind codes below 96, in which case
  1918. * it is RECOMMENDED that unassigned payload type numbers be used
  1919. * first". Updated by RFC 5761, Section 4: "[...] values in the range
  1920. * 64-95 MUST NOT be used [to avoid conflicts with RTCP]". Summaries:
  1921. * https://tools.ietf.org/html/draft-roach-mmusic-unified-plan#section-3.2.1.2
  1922. * https://tools.ietf.org/html/draft-wu-avtcore-dynamic-pt-usage#section-3
  1923. */
  1924. if (map < 0) {
  1925. for (x = MAX(ast_option_rtpptdynamic, 35); x <= AST_RTP_PT_LAST_REASSIGN; ++x) {
  1926. if (!static_RTP_PT[x]) {
  1927. map = x;
  1928. break;
  1929. }
  1930. }
  1931. }
  1932. /* Yet, reusing mappings below 35 is not supported in Asterisk because
  1933. * when Compact Headers are activated, no rtpmap is send for those below
  1934. * 35. If you want to use 35 and below
  1935. * A) do not use Compact Headers,
  1936. * B) remove that code in chan_sip/res_pjsip, or
  1937. * C) add a flag that this RTP Payload Type got reassigned dynamically
  1938. * and requires a rtpmap even with Compact Headers enabled.
  1939. */
  1940. if (map < 0) {
  1941. for (x = MAX(ast_option_rtpptdynamic, 20); x < 35; ++x) {
  1942. if (!static_RTP_PT[x]) {
  1943. map = x;
  1944. break;
  1945. }
  1946. }
  1947. }
  1948. if (map < 0) {
  1949. for (x = MAX(ast_option_rtpptdynamic, 0); x < 20; ++x) {
  1950. if (!static_RTP_PT[x]) {
  1951. map = x;
  1952. break;
  1953. }
  1954. }
  1955. }
  1956. if (map < 0) {
  1957. if (format) {
  1958. ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n",
  1959. ast_format_get_name(format));
  1960. } else {
  1961. ast_log(LOG_WARNING, "No Dynamic RTP mapping available for RTP code %d\n",
  1962. rtp_code);
  1963. }
  1964. ast_rwlock_unlock(&static_RTP_PT_lock);
  1965. return;
  1966. }
  1967. }
  1968. type = ast_rtp_engine_alloc_payload_type();
  1969. if (type) {
  1970. if (format) {
  1971. ao2_ref(format, +1);
  1972. type->format = format;
  1973. type->asterisk_format = 1;
  1974. } else {
  1975. type->rtp_code = rtp_code;
  1976. }
  1977. type->payload = map;
  1978. ao2_cleanup(static_RTP_PT[map]);
  1979. static_RTP_PT[map] = type;
  1980. }
  1981. ast_rwlock_unlock(&static_RTP_PT_lock);
  1982. }
  1983. int ast_rtp_engine_load_format(struct ast_format *format)
  1984. {
  1985. char *codec_name = ast_strdupa(ast_format_get_codec_name(format));
  1986. codec_name = ast_str_to_upper(codec_name);
  1987. set_next_mime_type(format,
  1988. 0,
  1989. ast_codec_media_type2str(ast_format_get_type(format)),
  1990. codec_name,
  1991. ast_format_get_sample_rate(format));
  1992. add_static_payload(-1, format, 0);
  1993. return 0;
  1994. }
  1995. int ast_rtp_engine_unload_format(struct ast_format *format)
  1996. {
  1997. int x;
  1998. int y = 0;
  1999. ast_rwlock_wrlock(&static_RTP_PT_lock);
  2000. /* remove everything pertaining to this format id from the lists */
  2001. for (x = 0; x < AST_RTP_MAX_PT; x++) {
  2002. if (static_RTP_PT[x]
  2003. && ast_format_cmp(static_RTP_PT[x]->format, format) == AST_FORMAT_CMP_EQUAL) {
  2004. ao2_ref(static_RTP_PT[x], -1);
  2005. static_RTP_PT[x] = NULL;
  2006. }
  2007. }
  2008. ast_rwlock_unlock(&static_RTP_PT_lock);
  2009. ast_rwlock_wrlock(&mime_types_lock);
  2010. /* rebuild the list skipping the items matching this id */
  2011. for (x = 0; x < mime_types_len; x++) {
  2012. if (ast_format_cmp(ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
  2013. rtp_engine_mime_type_cleanup(x);
  2014. continue;
  2015. }
  2016. if (x != y) {
  2017. ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
  2018. }
  2019. y++;
  2020. }
  2021. mime_types_len = y;
  2022. ast_rwlock_unlock(&mime_types_lock);
  2023. return 0;
  2024. }
  2025. /*!
  2026. * \internal
  2027. * \brief \ref stasis message payload for RTCP messages
  2028. */
  2029. struct rtcp_message_payload {
  2030. struct ast_channel_snapshot *snapshot; /*< The channel snapshot, if available */
  2031. struct ast_rtp_rtcp_report *report; /*< The RTCP report */
  2032. struct ast_json *blob; /*< Extra JSON data to publish */
  2033. };
  2034. static void rtcp_message_payload_dtor(void *obj)
  2035. {
  2036. struct rtcp_message_payload *payload = obj;
  2037. ao2_cleanup(payload->report);
  2038. ao2_cleanup(payload->snapshot);
  2039. ast_json_unref(payload->blob);
  2040. }
  2041. static struct ast_manager_event_blob *rtcp_report_to_ami(struct stasis_message *msg)
  2042. {
  2043. struct rtcp_message_payload *payload = stasis_message_data(msg);
  2044. RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
  2045. RAII_VAR(struct ast_str *, packet_string, ast_str_create(512), ast_free);
  2046. unsigned int ssrc = payload->report->ssrc;
  2047. unsigned int type = payload->report->type;
  2048. unsigned int report_count = payload->report->reception_report_count;
  2049. int i;
  2050. if (!packet_string) {
  2051. return NULL;
  2052. }
  2053. if (payload->snapshot) {
  2054. channel_string = ast_manager_build_channel_state_string(payload->snapshot);
  2055. if (!channel_string) {
  2056. return NULL;
  2057. }
  2058. }
  2059. if (payload->blob) {
  2060. /* Optional data */
  2061. struct ast_json *to = ast_json_object_get(payload->blob, "to");
  2062. struct ast_json *from = ast_json_object_get(payload->blob, "from");
  2063. struct ast_json *rtt = ast_json_object_get(payload->blob, "rtt");
  2064. if (to) {
  2065. ast_str_append(&packet_string, 0, "To: %s\r\n", ast_json_string_get(to));
  2066. }
  2067. if (from) {
  2068. ast_str_append(&packet_string, 0, "From: %s\r\n", ast_json_string_get(from));
  2069. }
  2070. if (rtt) {
  2071. ast_str_append(&packet_string, 0, "RTT: %4.4f\r\n", ast_json_real_get(rtt));
  2072. }
  2073. }
  2074. ast_str_append(&packet_string, 0, "SSRC: 0x%.8x\r\n", ssrc);
  2075. ast_str_append(&packet_string, 0, "PT: %u(%s)\r\n", type, type== AST_RTP_RTCP_SR ? "SR" : "RR");
  2076. ast_str_append(&packet_string, 0, "ReportCount: %u\r\n", report_count);
  2077. if (type == AST_RTP_RTCP_SR) {
  2078. ast_str_append(&packet_string, 0, "SentNTP: %lu.%06lu\r\n",
  2079. (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec,
  2080. (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
  2081. ast_str_append(&packet_string, 0, "SentRTP: %u\r\n",
  2082. payload->report->sender_information.rtp_timestamp);
  2083. ast_str_append(&packet_string, 0, "SentPackets: %u\r\n",
  2084. payload->report->sender_information.packet_count);
  2085. ast_str_append(&packet_string, 0, "SentOctets: %u\r\n",
  2086. payload->report->sender_information.octet_count);
  2087. }
  2088. for (i = 0; i < report_count; i++) {
  2089. RAII_VAR(struct ast_str *, report_string, NULL, ast_free);
  2090. if (!payload->report->report_block[i]) {
  2091. break;
  2092. }
  2093. report_string = ast_str_create(256);
  2094. if (!report_string) {
  2095. return NULL;
  2096. }
  2097. ast_str_append(&report_string, 0, "Report%dSourceSSRC: 0x%.8x\r\n",
  2098. i, payload->report->report_block[i]->source_ssrc);
  2099. ast_str_append(&report_string, 0, "Report%dFractionLost: %d\r\n",
  2100. i, payload->report->report_block[i]->lost_count.fraction);
  2101. ast_str_append(&report_string, 0, "Report%dCumulativeLost: %u\r\n",
  2102. i, payload->report->report_block[i]->lost_count.packets);
  2103. ast_str_append(&report_string, 0, "Report%dHighestSequence: %u\r\n",
  2104. i, payload->report->report_block[i]->highest_seq_no & 0xffff);
  2105. ast_str_append(&report_string, 0, "Report%dSequenceNumberCycles: %u\r\n",
  2106. i, payload->report->report_block[i]->highest_seq_no >> 16);
  2107. ast_str_append(&report_string, 0, "Report%dIAJitter: %u\r\n",
  2108. i, payload->report->report_block[i]->ia_jitter);
  2109. ast_str_append(&report_string, 0, "Report%dLSR: %u\r\n",
  2110. i, payload->report->report_block[i]->lsr);
  2111. ast_str_append(&report_string, 0, "Report%dDLSR: %4.4f\r\n",
  2112. i, ((double)payload->report->report_block[i]->dlsr) / 65536);
  2113. ast_str_append(&packet_string, 0, "%s", ast_str_buffer(report_string));
  2114. }
  2115. return ast_manager_event_blob_create(EVENT_FLAG_REPORTING,
  2116. stasis_message_type(msg) == ast_rtp_rtcp_received_type() ? "RTCPReceived" : "RTCPSent",
  2117. "%s%s",
  2118. AS_OR(channel_string, ""),
  2119. ast_str_buffer(packet_string));
  2120. }
  2121. static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
  2122. const struct stasis_message_sanitizer *sanitize)
  2123. {
  2124. struct rtcp_message_payload *payload = stasis_message_data(msg);
  2125. struct ast_json *json_rtcp_report = NULL;
  2126. struct ast_json *json_rtcp_report_blocks;
  2127. struct ast_json *json_rtcp_sender_info = NULL;
  2128. struct ast_json *json_channel = NULL;
  2129. int i;
  2130. json_rtcp_report_blocks = ast_json_array_create();
  2131. if (!json_rtcp_report_blocks) {
  2132. return NULL;
  2133. }
  2134. for (i = 0; i < payload->report->reception_report_count && payload->report->report_block[i]; i++) {
  2135. struct ast_json *json_report_block;
  2136. char str_lsr[32];
  2137. snprintf(str_lsr, sizeof(str_lsr), "%u", payload->report->report_block[i]->lsr);
  2138. json_report_block = ast_json_pack("{s: I, s: i, s: i, s: i, s: i, s: s, s: i}",
  2139. "source_ssrc", (ast_json_int_t)payload->report->report_block[i]->source_ssrc,
  2140. "fraction_lost", payload->report->report_block[i]->lost_count.fraction,
  2141. "packets_lost", payload->report->report_block[i]->lost_count.packets,
  2142. "highest_seq_no", payload->report->report_block[i]->highest_seq_no,
  2143. "ia_jitter", payload->report->report_block[i]->ia_jitter,
  2144. "lsr", str_lsr,
  2145. "dlsr", payload->report->report_block[i]->dlsr);
  2146. if (!json_report_block
  2147. || ast_json_array_append(json_rtcp_report_blocks, json_report_block)) {
  2148. ast_json_unref(json_rtcp_report_blocks);
  2149. return NULL;
  2150. }
  2151. }
  2152. if (payload->report->type == AST_RTP_RTCP_SR) {
  2153. char sec[32];
  2154. char usec[32];
  2155. snprintf(sec, sizeof(sec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec);
  2156. snprintf(usec, sizeof(usec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
  2157. json_rtcp_sender_info = ast_json_pack("{s: s, s: s, s: i, s: i, s: i}",
  2158. "ntp_timestamp_sec", sec,
  2159. "ntp_timestamp_usec", usec,
  2160. "rtp_timestamp", payload->report->sender_information.rtp_timestamp,
  2161. "packets", payload->report->sender_information.packet_count,
  2162. "octets", payload->report->sender_information.octet_count);
  2163. if (!json_rtcp_sender_info) {
  2164. ast_json_unref(json_rtcp_report_blocks);
  2165. return NULL;
  2166. }
  2167. }
  2168. json_rtcp_report = ast_json_pack("{s: I, s: i, s: i, s: o, s: o}",
  2169. "ssrc", (ast_json_int_t)payload->report->ssrc,
  2170. "type", payload->report->type,
  2171. "report_count", payload->report->reception_report_count,
  2172. "sender_information", json_rtcp_sender_info ?: ast_json_null(),
  2173. "report_blocks", json_rtcp_report_blocks);
  2174. if (!json_rtcp_report) {
  2175. return NULL;
  2176. }
  2177. if (payload->snapshot) {
  2178. json_channel = ast_channel_snapshot_to_json(payload->snapshot, sanitize);
  2179. if (!json_channel) {
  2180. ast_json_unref(json_rtcp_report);
  2181. return NULL;
  2182. }
  2183. }
  2184. return ast_json_pack("{s: o, s: o, s: o}",
  2185. "channel", payload->snapshot ? json_channel : ast_json_null(),
  2186. "rtcp_report", json_rtcp_report,
  2187. "blob", ast_json_deep_copy(payload->blob) ?: ast_json_null());
  2188. }
  2189. static void rtp_rtcp_report_dtor(void *obj)
  2190. {
  2191. int i;
  2192. struct ast_rtp_rtcp_report *rtcp_report = obj;
  2193. for (i = 0; i < rtcp_report->reception_report_count; i++) {
  2194. ast_free(rtcp_report->report_block[i]);
  2195. }
  2196. }
  2197. struct ast_rtp_rtcp_report *ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
  2198. {
  2199. struct ast_rtp_rtcp_report *rtcp_report;
  2200. /* Size of object is sizeof the report + the number of report_blocks * sizeof pointer */
  2201. rtcp_report = ao2_alloc((sizeof(*rtcp_report) + report_blocks * sizeof(struct ast_rtp_rtcp_report_block *)),
  2202. rtp_rtcp_report_dtor);
  2203. return rtcp_report;
  2204. }
  2205. void ast_rtp_publish_rtcp_message(struct ast_rtp_instance *rtp,
  2206. struct stasis_message_type *message_type,
  2207. struct ast_rtp_rtcp_report *report,
  2208. struct ast_json *blob)
  2209. {
  2210. RAII_VAR(struct rtcp_message_payload *, payload, NULL, ao2_cleanup);
  2211. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  2212. if (!message_type) {
  2213. return;
  2214. }
  2215. payload = ao2_alloc(sizeof(*payload), rtcp_message_payload_dtor);
  2216. if (!payload || !report) {
  2217. return;
  2218. }
  2219. if (!ast_strlen_zero(rtp->channel_uniqueid)) {
  2220. payload->snapshot = ast_channel_snapshot_get_latest(rtp->channel_uniqueid);
  2221. }
  2222. if (blob) {
  2223. payload->blob = blob;
  2224. ast_json_ref(blob);
  2225. }
  2226. ao2_ref(report, +1);
  2227. payload->report = report;
  2228. message = stasis_message_create(message_type, payload);
  2229. if (!message) {
  2230. return;
  2231. }
  2232. stasis_publish(ast_rtp_topic(), message);
  2233. }
  2234. /*!
  2235. * @{ \brief Define RTCP/RTP message types.
  2236. */
  2237. STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_sent_type,
  2238. .to_ami = rtcp_report_to_ami,
  2239. .to_json = rtcp_report_to_json,);
  2240. STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_received_type,
  2241. .to_ami = rtcp_report_to_ami,
  2242. .to_json = rtcp_report_to_json,);
  2243. /*! @} */
  2244. struct stasis_topic *ast_rtp_topic(void)
  2245. {
  2246. return rtp_topic;
  2247. }
  2248. static void rtp_engine_shutdown(void)
  2249. {
  2250. int x;
  2251. ao2_cleanup(rtp_topic);
  2252. rtp_topic = NULL;
  2253. STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_received_type);
  2254. STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_sent_type);
  2255. ast_rwlock_wrlock(&static_RTP_PT_lock);
  2256. for (x = 0; x < AST_RTP_MAX_PT; x++) {
  2257. ao2_cleanup(static_RTP_PT[x]);
  2258. static_RTP_PT[x] = NULL;
  2259. }
  2260. ast_rwlock_unlock(&static_RTP_PT_lock);
  2261. ast_rwlock_wrlock(&mime_types_lock);
  2262. for (x = 0; x < mime_types_len; x++) {
  2263. if (ast_rtp_mime_types[x].payload_type.format) {
  2264. rtp_engine_mime_type_cleanup(x);
  2265. }
  2266. }
  2267. mime_types_len = 0;
  2268. ast_rwlock_unlock(&mime_types_lock);
  2269. }
  2270. int ast_rtp_engine_init(void)
  2271. {
  2272. ast_rwlock_init(&mime_types_lock);
  2273. ast_rwlock_init(&static_RTP_PT_lock);
  2274. rtp_topic = stasis_topic_create("rtp_topic");
  2275. if (!rtp_topic) {
  2276. return -1;
  2277. }
  2278. STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_sent_type);
  2279. STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_received_type);
  2280. ast_register_cleanup(rtp_engine_shutdown);
  2281. /* Define all the RTP mime types available */
  2282. set_next_mime_type(ast_format_g723, 0, "audio", "G723", 8000);
  2283. set_next_mime_type(ast_format_gsm, 0, "audio", "GSM", 8000);
  2284. set_next_mime_type(ast_format_ulaw, 0, "audio", "PCMU", 8000);
  2285. set_next_mime_type(ast_format_ulaw, 0, "audio", "G711U", 8000);
  2286. set_next_mime_type(ast_format_alaw, 0, "audio", "PCMA", 8000);
  2287. set_next_mime_type(ast_format_alaw, 0, "audio", "G711A", 8000);
  2288. set_next_mime_type(ast_format_g726, 0, "audio", "G726-32", 8000);
  2289. set_next_mime_type(ast_format_adpcm, 0, "audio", "DVI4", 8000);
  2290. set_next_mime_type(ast_format_slin, 0, "audio", "L16", 8000);
  2291. set_next_mime_type(ast_format_slin16, 0, "audio", "L16", 16000);
  2292. set_next_mime_type(ast_format_slin16, 0, "audio", "L16-256", 16000);
  2293. set_next_mime_type(ast_format_lpc10, 0, "audio", "LPC", 8000);
  2294. set_next_mime_type(ast_format_g729, 0, "audio", "G729", 8000);
  2295. set_next_mime_type(ast_format_g729, 0, "audio", "G729A", 8000);
  2296. set_next_mime_type(ast_format_g729, 0, "audio", "G.729", 8000);
  2297. set_next_mime_type(ast_format_speex, 0, "audio", "speex", 8000);
  2298. set_next_mime_type(ast_format_speex16, 0, "audio", "speex", 16000);
  2299. set_next_mime_type(ast_format_speex32, 0, "audio", "speex", 32000);
  2300. set_next_mime_type(ast_format_ilbc, 0, "audio", "iLBC", 8000);
  2301. /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
  2302. set_next_mime_type(ast_format_g722, 0, "audio", "G722", 8000);
  2303. set_next_mime_type(ast_format_g726_aal2, 0, "audio", "AAL2-G726-32", 8000);
  2304. set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
  2305. set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
  2306. set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
  2307. set_next_mime_type(ast_format_jpeg, 0, "video", "JPEG", 90000);
  2308. set_next_mime_type(ast_format_png, 0, "video", "PNG", 90000);
  2309. set_next_mime_type(ast_format_h261, 0, "video", "H261", 90000);
  2310. set_next_mime_type(ast_format_h263, 0, "video", "H263", 90000);
  2311. set_next_mime_type(ast_format_h263p, 0, "video", "h263-1998", 90000);
  2312. set_next_mime_type(ast_format_h264, 0, "video", "H264", 90000);
  2313. set_next_mime_type(ast_format_mp4, 0, "video", "MP4V-ES", 90000);
  2314. set_next_mime_type(ast_format_t140_red, 0, "text", "RED", 1000);
  2315. set_next_mime_type(ast_format_t140, 0, "text", "T140", 1000);
  2316. set_next_mime_type(ast_format_siren7, 0, "audio", "G7221", 16000);
  2317. set_next_mime_type(ast_format_siren14, 0, "audio", "G7221", 32000);
  2318. set_next_mime_type(ast_format_g719, 0, "audio", "G719", 48000);
  2319. /* Opus, VP8, and VP9 */
  2320. set_next_mime_type(ast_format_opus, 0, "audio", "opus", 48000);
  2321. set_next_mime_type(ast_format_vp8, 0, "video", "VP8", 90000);
  2322. set_next_mime_type(ast_format_vp9, 0, "video", "VP9", 90000);
  2323. /* Define the static rtp payload mappings */
  2324. add_static_payload(0, ast_format_ulaw, 0);
  2325. #ifdef USE_DEPRECATED_G726
  2326. add_static_payload(2, ast_format_g726, 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
  2327. #endif
  2328. add_static_payload(3, ast_format_gsm, 0);
  2329. add_static_payload(4, ast_format_g723, 0);
  2330. add_static_payload(5, ast_format_adpcm, 0);/* 8 kHz */
  2331. add_static_payload(6, ast_format_adpcm, 0); /* 16 kHz */
  2332. add_static_payload(7, ast_format_lpc10, 0);
  2333. add_static_payload(8, ast_format_alaw, 0);
  2334. add_static_payload(9, ast_format_g722, 0);
  2335. add_static_payload(10, ast_format_slin, 0); /* 2 channels */
  2336. add_static_payload(11, ast_format_slin, 0); /* 1 channel */
  2337. add_static_payload(13, NULL, AST_RTP_CN);
  2338. add_static_payload(16, ast_format_adpcm, 0); /* 11.025 kHz */
  2339. add_static_payload(17, ast_format_adpcm, 0); /* 22.050 kHz */
  2340. add_static_payload(18, ast_format_g729, 0);
  2341. add_static_payload(19, NULL, AST_RTP_CN); /* Also used for CN */
  2342. add_static_payload(26, ast_format_jpeg, 0);
  2343. add_static_payload(31, ast_format_h261, 0);
  2344. add_static_payload(34, ast_format_h263, 0);
  2345. add_static_payload(97, ast_format_ilbc, 0);
  2346. add_static_payload(99, ast_format_h264, 0);
  2347. add_static_payload(101, NULL, AST_RTP_DTMF);
  2348. add_static_payload(102, ast_format_siren7, 0);
  2349. add_static_payload(103, ast_format_h263p, 0);
  2350. add_static_payload(104, ast_format_mp4, 0);
  2351. add_static_payload(105, ast_format_t140_red, 0); /* Real time text chat (with redundancy encoding) */
  2352. add_static_payload(106, ast_format_t140, 0); /* Real time text chat */
  2353. add_static_payload(108, ast_format_vp9, 0);
  2354. add_static_payload(110, ast_format_speex, 0);
  2355. add_static_payload(111, ast_format_g726, 0);
  2356. add_static_payload(112, ast_format_g726_aal2, 0);
  2357. add_static_payload(115, ast_format_siren14, 0);
  2358. add_static_payload(116, ast_format_g719, 0);
  2359. add_static_payload(117, ast_format_speex16, 0);
  2360. add_static_payload(118, ast_format_slin16, 0); /* 16 Khz signed linear */
  2361. add_static_payload(119, ast_format_speex32, 0);
  2362. add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */
  2363. /* Opus and VP8 */
  2364. add_static_payload(100, ast_format_vp8, 0);
  2365. add_static_payload(107, ast_format_opus, 0);
  2366. return 0;
  2367. }
  2368. time_t ast_rtp_instance_get_last_tx(const struct ast_rtp_instance *rtp)
  2369. {
  2370. return rtp->last_tx;
  2371. }
  2372. void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time)
  2373. {
  2374. rtp->last_tx = time;
  2375. }
  2376. time_t ast_rtp_instance_get_last_rx(const struct ast_rtp_instance *rtp)
  2377. {
  2378. return rtp->last_rx;
  2379. }
  2380. void ast_rtp_instance_set_last_rx(struct ast_rtp_instance *rtp, time_t time)
  2381. {
  2382. rtp->last_rx = time;
  2383. }