rtp_engine.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373
  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. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <math.h>
  30. #include "asterisk/channel.h"
  31. #include "asterisk/frame.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/rtp_engine.h"
  34. #include "asterisk/manager.h"
  35. #include "asterisk/options.h"
  36. #include "asterisk/astobj2.h"
  37. #include "asterisk/pbx.h"
  38. #include "asterisk/translate.h"
  39. #include "asterisk/netsock2.h"
  40. #include "asterisk/_private.h"
  41. #include "asterisk/framehook.h"
  42. struct ast_srtp_res *res_srtp = NULL;
  43. struct ast_srtp_policy_res *res_srtp_policy = NULL;
  44. /*! Structure that represents an RTP session (instance) */
  45. struct ast_rtp_instance {
  46. /*! Engine that is handling this RTP instance */
  47. struct ast_rtp_engine *engine;
  48. /*! Data unique to the RTP engine */
  49. void *data;
  50. /*! RTP properties that have been set and their value */
  51. int properties[AST_RTP_PROPERTY_MAX];
  52. /*! Address that we are expecting RTP to come in to */
  53. struct ast_sockaddr local_address;
  54. /*! Address that we are sending RTP to */
  55. struct ast_sockaddr remote_address;
  56. /*! Alternate address that we are receiving RTP from */
  57. struct ast_sockaddr alt_remote_address;
  58. /*! Instance that we are bridged to if doing remote or local bridging */
  59. struct ast_rtp_instance *bridged;
  60. /*! Payload and packetization information */
  61. struct ast_rtp_codecs codecs;
  62. /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
  63. int timeout;
  64. /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
  65. int holdtimeout;
  66. /*! RTP keepalive interval */
  67. int keepalive;
  68. /*! Glue currently in use */
  69. struct ast_rtp_glue *glue;
  70. /*! Channel associated with the instance */
  71. struct ast_channel *chan;
  72. /*! SRTP info associated with the instance */
  73. struct ast_srtp *srtp;
  74. };
  75. /*! List of RTP engines that are currently registered */
  76. static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
  77. /*! List of RTP glues */
  78. static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
  79. /*! The following array defines the MIME Media type (and subtype) for each
  80. of our codecs, or RTP-specific data type. */
  81. static struct ast_rtp_mime_type {
  82. struct ast_rtp_payload_type payload_type;
  83. char *type;
  84. char *subtype;
  85. unsigned int sample_rate;
  86. } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
  87. static ast_rwlock_t mime_types_lock;
  88. static int mime_types_len = 0;
  89. /*!
  90. * \brief Mapping between Asterisk codecs and rtp payload types
  91. *
  92. * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
  93. * also, our own choices for dynamic payload types. This is our master
  94. * table for transmission
  95. *
  96. * See http://www.iana.org/assignments/rtp-parameters for a list of
  97. * assigned values
  98. */
  99. static struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT];
  100. static ast_rwlock_t static_RTP_PT_lock;
  101. int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
  102. {
  103. struct ast_rtp_engine *current_engine;
  104. /* Perform a sanity check on the engine structure to make sure it has the basics */
  105. if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
  106. ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
  107. return -1;
  108. }
  109. /* Link owner module to the RTP engine for reference counting purposes */
  110. engine->mod = module;
  111. AST_RWLIST_WRLOCK(&engines);
  112. /* Ensure that no two modules with the same name are registered at the same time */
  113. AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
  114. if (!strcmp(current_engine->name, engine->name)) {
  115. ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
  116. AST_RWLIST_UNLOCK(&engines);
  117. return -1;
  118. }
  119. }
  120. /* The engine survived our critique. Off to the list it goes to be used */
  121. AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
  122. AST_RWLIST_UNLOCK(&engines);
  123. ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
  124. return 0;
  125. }
  126. int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
  127. {
  128. struct ast_rtp_engine *current_engine = NULL;
  129. AST_RWLIST_WRLOCK(&engines);
  130. if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
  131. ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
  132. }
  133. AST_RWLIST_UNLOCK(&engines);
  134. return current_engine ? 0 : -1;
  135. }
  136. int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
  137. {
  138. struct ast_rtp_glue *current_glue = NULL;
  139. if (ast_strlen_zero(glue->type)) {
  140. return -1;
  141. }
  142. glue->mod = module;
  143. AST_RWLIST_WRLOCK(&glues);
  144. AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
  145. if (!strcasecmp(current_glue->type, glue->type)) {
  146. ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
  147. AST_RWLIST_UNLOCK(&glues);
  148. return -1;
  149. }
  150. }
  151. AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
  152. AST_RWLIST_UNLOCK(&glues);
  153. ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
  154. return 0;
  155. }
  156. int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
  157. {
  158. struct ast_rtp_glue *current_glue = NULL;
  159. AST_RWLIST_WRLOCK(&glues);
  160. if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
  161. ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
  162. }
  163. AST_RWLIST_UNLOCK(&glues);
  164. return current_glue ? 0 : -1;
  165. }
  166. static void instance_destructor(void *obj)
  167. {
  168. struct ast_rtp_instance *instance = obj;
  169. /* Pass us off to the engine to destroy */
  170. if (instance->data && instance->engine->destroy(instance)) {
  171. ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
  172. return;
  173. }
  174. if (instance->srtp) {
  175. res_srtp->destroy(instance->srtp);
  176. }
  177. ast_rtp_codecs_payloads_destroy(&instance->codecs);
  178. /* Drop our engine reference */
  179. ast_module_unref(instance->engine->mod);
  180. ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
  181. }
  182. int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
  183. {
  184. ao2_ref(instance, -1);
  185. return 0;
  186. }
  187. struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
  188. struct ast_sched_context *sched, const struct ast_sockaddr *sa,
  189. void *data)
  190. {
  191. struct ast_sockaddr address = {{0,}};
  192. struct ast_rtp_instance *instance = NULL;
  193. struct ast_rtp_engine *engine = NULL;
  194. AST_RWLIST_RDLOCK(&engines);
  195. /* If an engine name was specified try to use it or otherwise use the first one registered */
  196. if (!ast_strlen_zero(engine_name)) {
  197. AST_RWLIST_TRAVERSE(&engines, engine, entry) {
  198. if (!strcmp(engine->name, engine_name)) {
  199. break;
  200. }
  201. }
  202. } else {
  203. engine = AST_RWLIST_FIRST(&engines);
  204. }
  205. /* If no engine was actually found bail out now */
  206. if (!engine) {
  207. ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
  208. AST_RWLIST_UNLOCK(&engines);
  209. return NULL;
  210. }
  211. /* Bump up the reference count before we return so the module can not be unloaded */
  212. ast_module_ref(engine->mod);
  213. AST_RWLIST_UNLOCK(&engines);
  214. /* Allocate a new RTP instance */
  215. if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
  216. ast_module_unref(engine->mod);
  217. return NULL;
  218. }
  219. instance->engine = engine;
  220. ast_sockaddr_copy(&instance->local_address, sa);
  221. ast_sockaddr_copy(&address, sa);
  222. if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
  223. ao2_ref(instance, -1);
  224. return NULL;
  225. }
  226. ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
  227. /* And pass it off to the engine to setup */
  228. if (instance->engine->new(instance, sched, &address, data)) {
  229. ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
  230. ao2_ref(instance, -1);
  231. return NULL;
  232. }
  233. ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
  234. return instance;
  235. }
  236. void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
  237. {
  238. instance->data = data;
  239. }
  240. void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
  241. {
  242. return instance->data;
  243. }
  244. int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
  245. {
  246. return instance->engine->write(instance, frame);
  247. }
  248. struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
  249. {
  250. return instance->engine->read(instance, rtcp);
  251. }
  252. int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
  253. const struct ast_sockaddr *address)
  254. {
  255. ast_sockaddr_copy(&instance->local_address, address);
  256. return 0;
  257. }
  258. int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
  259. const struct ast_sockaddr *address)
  260. {
  261. ast_sockaddr_copy(&instance->remote_address, address);
  262. /* moo */
  263. if (instance->engine->remote_address_set) {
  264. instance->engine->remote_address_set(instance, &instance->remote_address);
  265. }
  266. return 0;
  267. }
  268. int ast_rtp_instance_set_alt_remote_address(struct ast_rtp_instance *instance,
  269. const struct ast_sockaddr *address)
  270. {
  271. ast_sockaddr_copy(&instance->alt_remote_address, address);
  272. /* oink */
  273. if (instance->engine->alt_remote_address_set) {
  274. instance->engine->alt_remote_address_set(instance, &instance->alt_remote_address);
  275. }
  276. return 0;
  277. }
  278. int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
  279. struct ast_sockaddr *address)
  280. {
  281. if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
  282. ast_sockaddr_copy(address, &instance->local_address);
  283. return 1;
  284. }
  285. return 0;
  286. }
  287. void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
  288. struct ast_sockaddr *address)
  289. {
  290. ast_sockaddr_copy(address, &instance->local_address);
  291. }
  292. int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
  293. struct ast_sockaddr *address)
  294. {
  295. if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
  296. ast_sockaddr_copy(address, &instance->remote_address);
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
  302. struct ast_sockaddr *address)
  303. {
  304. ast_sockaddr_copy(address, &instance->remote_address);
  305. }
  306. void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
  307. {
  308. if (instance->engine->extended_prop_set) {
  309. instance->engine->extended_prop_set(instance, property, value);
  310. }
  311. }
  312. void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
  313. {
  314. if (instance->engine->extended_prop_get) {
  315. return instance->engine->extended_prop_get(instance, property);
  316. }
  317. return NULL;
  318. }
  319. void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
  320. {
  321. instance->properties[property] = value;
  322. if (instance->engine->prop_set) {
  323. instance->engine->prop_set(instance, property, value);
  324. }
  325. }
  326. int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
  327. {
  328. return instance->properties[property];
  329. }
  330. struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
  331. {
  332. return &instance->codecs;
  333. }
  334. static int rtp_payload_type_hash(const void *obj, const int flags)
  335. {
  336. const struct ast_rtp_payload_type *type = obj;
  337. const int *payload = obj;
  338. return (flags & OBJ_KEY) ? *payload : type->payload;
  339. }
  340. static int rtp_payload_type_cmp(void *obj, void *arg, int flags)
  341. {
  342. struct ast_rtp_payload_type *type1 = obj, *type2 = arg;
  343. const int *payload = arg;
  344. return (type1->payload == (OBJ_KEY ? *payload : type2->payload)) ? CMP_MATCH | CMP_STOP : 0;
  345. }
  346. int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
  347. {
  348. if (!(codecs->payloads = ao2_container_alloc(AST_RTP_MAX_PT, rtp_payload_type_hash, rtp_payload_type_cmp))) {
  349. return -1;
  350. }
  351. return 0;
  352. }
  353. void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
  354. {
  355. ao2_cleanup(codecs->payloads);
  356. }
  357. void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
  358. {
  359. ast_rtp_codecs_payloads_destroy(codecs);
  360. if (instance && instance->engine && instance->engine->payload_set) {
  361. int i;
  362. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  363. instance->engine->payload_set(instance, i, 0, NULL, 0);
  364. }
  365. }
  366. ast_rtp_codecs_payloads_initialize(codecs);
  367. }
  368. void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
  369. {
  370. int i;
  371. ast_rwlock_rdlock(&static_RTP_PT_lock);
  372. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  373. if (static_RTP_PT[i].rtp_code || static_RTP_PT[i].asterisk_format) {
  374. struct ast_rtp_payload_type *type;
  375. if (!(type = ao2_alloc(sizeof(*type), NULL))) {
  376. /* Unfortunately if this occurs the payloads container will not contain all possible default payloads
  377. * but we err on the side of doing what we can in the hopes that the extreme memory conditions which
  378. * caused this to occur will go away.
  379. */
  380. continue;
  381. }
  382. type->payload = i;
  383. type->asterisk_format = static_RTP_PT[i].asterisk_format;
  384. type->rtp_code = static_RTP_PT[i].rtp_code;
  385. ast_format_copy(&type->format, &static_RTP_PT[i].format);
  386. ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
  387. if (instance && instance->engine && instance->engine->payload_set) {
  388. instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
  389. }
  390. ao2_ref(type, -1);
  391. }
  392. }
  393. ast_rwlock_unlock(&static_RTP_PT_lock);
  394. }
  395. void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
  396. {
  397. int i;
  398. struct ast_rtp_payload_type *type;
  399. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  400. struct ast_rtp_payload_type *new_type;
  401. if (!(type = ao2_find(src->payloads, &i, OBJ_KEY | OBJ_NOLOCK))) {
  402. continue;
  403. }
  404. if (!(new_type = ao2_alloc(sizeof(*new_type), NULL))) {
  405. continue;
  406. }
  407. ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
  408. new_type->payload = i;
  409. *new_type = *type;
  410. ao2_link_flags(dest->payloads, new_type, OBJ_NOLOCK);
  411. ao2_ref(new_type, -1);
  412. if (instance && instance->engine && instance->engine->payload_set) {
  413. instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
  414. }
  415. ao2_ref(type, -1);
  416. }
  417. }
  418. void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
  419. {
  420. struct ast_rtp_payload_type *type;
  421. ast_rwlock_rdlock(&static_RTP_PT_lock);
  422. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  423. ast_rwlock_unlock(&static_RTP_PT_lock);
  424. return;
  425. }
  426. if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
  427. if (!(type = ao2_alloc(sizeof(*type), NULL))) {
  428. ast_rwlock_unlock(&static_RTP_PT_lock);
  429. return;
  430. }
  431. type->payload = payload;
  432. ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
  433. }
  434. type->asterisk_format = static_RTP_PT[payload].asterisk_format;
  435. type->rtp_code = static_RTP_PT[payload].rtp_code;
  436. type->payload = payload;
  437. ast_format_copy(&type->format, &static_RTP_PT[payload].format);
  438. ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
  439. if (instance && instance->engine && instance->engine->payload_set) {
  440. instance->engine->payload_set(instance, payload, type->asterisk_format, &type->format, type->rtp_code);
  441. }
  442. ao2_ref(type, -1);
  443. ast_rwlock_unlock(&static_RTP_PT_lock);
  444. }
  445. int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
  446. char *mimetype, char *mimesubtype,
  447. enum ast_rtp_options options,
  448. unsigned int sample_rate)
  449. {
  450. unsigned int i;
  451. int found = 0;
  452. if (pt < 0 || pt >= AST_RTP_MAX_PT)
  453. return -1; /* bogus payload type */
  454. ast_rwlock_rdlock(&mime_types_lock);
  455. for (i = 0; i < mime_types_len; ++i) {
  456. const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
  457. struct ast_rtp_payload_type *type;
  458. if (strcasecmp(mimesubtype, t->subtype)) {
  459. continue;
  460. }
  461. if (strcasecmp(mimetype, t->type)) {
  462. continue;
  463. }
  464. /* if both sample rates have been supplied, and they don't match,
  465. * then this not a match; if one has not been supplied, then the
  466. * rates are not compared */
  467. if (sample_rate && t->sample_rate &&
  468. (sample_rate != t->sample_rate)) {
  469. continue;
  470. }
  471. found = 1;
  472. if (!(type = ao2_find(codecs->payloads, &pt, OBJ_KEY | OBJ_NOLOCK))) {
  473. if (!(type = ao2_alloc(sizeof(*type), NULL))) {
  474. continue;
  475. }
  476. type->payload = pt;
  477. ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
  478. }
  479. *type = t->payload_type;
  480. type->payload = pt;
  481. if ((t->payload_type.format.id == AST_FORMAT_G726) && t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
  482. ast_format_set(&type->format, AST_FORMAT_G726_AAL2, 0);
  483. }
  484. if (instance && instance->engine && instance->engine->payload_set) {
  485. instance->engine->payload_set(instance, pt, type->asterisk_format, &type->format, type->rtp_code);
  486. }
  487. ao2_ref(type, -1);
  488. break;
  489. }
  490. ast_rwlock_unlock(&mime_types_lock);
  491. return (found ? 0 : -2);
  492. }
  493. 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)
  494. {
  495. return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
  496. }
  497. void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
  498. {
  499. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  500. return;
  501. }
  502. ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
  503. ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK | OBJ_NODATA | OBJ_UNLINK);
  504. if (instance && instance->engine && instance->engine->payload_set) {
  505. instance->engine->payload_set(instance, payload, 0, NULL, 0);
  506. }
  507. }
  508. struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
  509. {
  510. struct ast_rtp_payload_type result = { .asterisk_format = 0, }, *type;
  511. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  512. return result;
  513. }
  514. if ((type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
  515. result = *type;
  516. ao2_ref(type, -1);
  517. }
  518. if (!result.rtp_code && !result.asterisk_format) {
  519. ast_rwlock_rdlock(&static_RTP_PT_lock);
  520. result = static_RTP_PT[payload];
  521. ast_rwlock_unlock(&static_RTP_PT_lock);
  522. }
  523. return result;
  524. }
  525. struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
  526. {
  527. struct ast_rtp_payload_type *type;
  528. struct ast_format *format;
  529. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  530. return NULL;
  531. }
  532. if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
  533. return NULL;
  534. }
  535. format = type->asterisk_format ? &type->format : NULL;
  536. ao2_ref(type, -1);
  537. return format;
  538. }
  539. static int rtp_payload_type_add_ast(void *obj, void *arg, int flags)
  540. {
  541. struct ast_rtp_payload_type *type = obj;
  542. struct ast_format_cap *astformats = arg;
  543. if (type->asterisk_format) {
  544. ast_format_cap_add(astformats, &type->format);
  545. }
  546. return 0;
  547. }
  548. static int rtp_payload_type_add_nonast(void *obj, void *arg, int flags)
  549. {
  550. struct ast_rtp_payload_type *type = obj;
  551. int *nonastformats = arg;
  552. if (!type->asterisk_format) {
  553. *nonastformats |= type->rtp_code;
  554. }
  555. return 0;
  556. }
  557. void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
  558. {
  559. ast_format_cap_remove_all(astformats);
  560. *nonastformats = 0;
  561. ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_ast, astformats);
  562. ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_nonast, nonastformats);
  563. }
  564. static int rtp_payload_type_find_format(void *obj, void *arg, int flags)
  565. {
  566. struct ast_rtp_payload_type *type = obj;
  567. struct ast_format *format = arg;
  568. return (type->asterisk_format && (ast_format_cmp(&type->format, format) != AST_FORMAT_CMP_NOT_EQUAL)) ? CMP_MATCH | CMP_STOP : 0;
  569. }
  570. static int rtp_payload_type_find_nonast_format(void *obj, void *arg, int flags)
  571. {
  572. struct ast_rtp_payload_type *type = obj;
  573. int *rtp_code = arg;
  574. return ((!type->asterisk_format && (type->rtp_code == *rtp_code)) ? CMP_MATCH | CMP_STOP : 0);
  575. }
  576. int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
  577. {
  578. struct ast_rtp_payload_type *type;
  579. int i, res = -1;
  580. if (asterisk_format && format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_format, (void*)format))) {
  581. res = type->payload;
  582. ao2_ref(type, -1);
  583. return res;
  584. } else if (!asterisk_format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_nonast_format, (void*)&code))) {
  585. res = type->payload;
  586. ao2_ref(type, -1);
  587. return res;
  588. }
  589. ast_rwlock_rdlock(&static_RTP_PT_lock);
  590. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  591. if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
  592. (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  593. res = i;
  594. break;
  595. } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
  596. (static_RTP_PT[i].rtp_code == code)) {
  597. res = i;
  598. break;
  599. }
  600. }
  601. ast_rwlock_unlock(&static_RTP_PT_lock);
  602. return res;
  603. }
  604. int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
  605. {
  606. struct ast_rtp_payload_type *type;
  607. int res = -1;
  608. /* Search the payload type in the codecs passed */
  609. if ((type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY)))
  610. {
  611. res = type->payload;
  612. ao2_ref(type, -1);
  613. return res;
  614. }
  615. return res;
  616. }
  617. const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
  618. {
  619. int i;
  620. const char *res = "";
  621. ast_rwlock_rdlock(&mime_types_lock);
  622. for (i = 0; i < mime_types_len; i++) {
  623. if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
  624. (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  625. if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
  626. res = "G726-32";
  627. break;
  628. } else {
  629. res = ast_rtp_mime_types[i].subtype;
  630. break;
  631. }
  632. } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
  633. ast_rtp_mime_types[i].payload_type.rtp_code == code) {
  634. res = ast_rtp_mime_types[i].subtype;
  635. break;
  636. }
  637. }
  638. ast_rwlock_unlock(&mime_types_lock);
  639. return res;
  640. }
  641. unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
  642. {
  643. unsigned int i;
  644. unsigned int res = 0;
  645. ast_rwlock_rdlock(&mime_types_lock);
  646. for (i = 0; i < mime_types_len; ++i) {
  647. if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
  648. (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  649. res = ast_rtp_mime_types[i].sample_rate;
  650. break;
  651. } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
  652. ast_rtp_mime_types[i].payload_type.rtp_code == code) {
  653. res = ast_rtp_mime_types[i].sample_rate;
  654. break;
  655. }
  656. }
  657. ast_rwlock_unlock(&mime_types_lock);
  658. return res;
  659. }
  660. 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)
  661. {
  662. int found = 0;
  663. const char *name;
  664. if (!buf) {
  665. return NULL;
  666. }
  667. if (asterisk_format) {
  668. struct ast_format tmp_fmt;
  669. ast_format_cap_iter_start(ast_format_capability);
  670. while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
  671. name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
  672. ast_str_append(&buf, 0, "%s|", name);
  673. found = 1;
  674. }
  675. ast_format_cap_iter_end(ast_format_capability);
  676. } else {
  677. int x;
  678. ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
  679. for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
  680. if (rtp_capability & x) {
  681. name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
  682. ast_str_append(&buf, 0, "%s|", name);
  683. found = 1;
  684. }
  685. }
  686. }
  687. ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
  688. return ast_str_buffer(buf);
  689. }
  690. void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
  691. {
  692. codecs->pref = *prefs;
  693. if (instance && instance->engine->packetization_set) {
  694. instance->engine->packetization_set(instance, &instance->codecs.pref);
  695. }
  696. }
  697. int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
  698. {
  699. return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
  700. }
  701. int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
  702. {
  703. return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
  704. }
  705. int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
  706. {
  707. return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
  708. }
  709. int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
  710. {
  711. return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
  712. }
  713. enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
  714. {
  715. return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
  716. }
  717. void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
  718. {
  719. if (instance->engine->update_source) {
  720. instance->engine->update_source(instance);
  721. }
  722. }
  723. void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
  724. {
  725. if (instance->engine->change_source) {
  726. instance->engine->change_source(instance);
  727. }
  728. }
  729. int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
  730. {
  731. return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
  732. }
  733. void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
  734. {
  735. if (instance->engine->stop) {
  736. instance->engine->stop(instance);
  737. }
  738. }
  739. int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
  740. {
  741. return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
  742. }
  743. struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
  744. {
  745. struct ast_rtp_glue *glue = NULL;
  746. AST_RWLIST_RDLOCK(&glues);
  747. AST_RWLIST_TRAVERSE(&glues, glue, entry) {
  748. if (!strcasecmp(glue->type, type)) {
  749. break;
  750. }
  751. }
  752. AST_RWLIST_UNLOCK(&glues);
  753. return glue;
  754. }
  755. static enum ast_bridge_result local_bridge_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1, int timeoutms, int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
  756. {
  757. enum ast_bridge_result res = AST_BRIDGE_FAILED;
  758. struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
  759. struct ast_frame *fr = NULL;
  760. struct timeval start;
  761. /* Start locally bridging both instances */
  762. if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
  763. ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c0), ast_channel_name(c1));
  764. ast_channel_unlock(c0);
  765. ast_channel_unlock(c1);
  766. return AST_BRIDGE_FAILED_NOWARN;
  767. }
  768. if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
  769. ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c1), ast_channel_name(c0));
  770. if (instance0->engine->local_bridge) {
  771. instance0->engine->local_bridge(instance0, NULL);
  772. }
  773. ast_channel_unlock(c0);
  774. ast_channel_unlock(c1);
  775. return AST_BRIDGE_FAILED_NOWARN;
  776. }
  777. ast_channel_unlock(c0);
  778. ast_channel_unlock(c1);
  779. instance0->bridged = instance1;
  780. instance1->bridged = instance0;
  781. ast_poll_channel_add(c0, c1);
  782. /* Hop into a loop waiting for a frame from either channel */
  783. cs[0] = c0;
  784. cs[1] = c1;
  785. cs[2] = NULL;
  786. start = ast_tvnow();
  787. for (;;) {
  788. int ms;
  789. /* If the underlying formats have changed force this bridge to break */
  790. if ((ast_format_cmp(ast_channel_rawreadformat(c0), ast_channel_rawwriteformat(c1)) == AST_FORMAT_CMP_NOT_EQUAL) ||
  791. (ast_format_cmp(ast_channel_rawreadformat(c1), ast_channel_rawwriteformat(c0)) == AST_FORMAT_CMP_NOT_EQUAL)) {
  792. ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
  793. res = AST_BRIDGE_FAILED_NOWARN;
  794. break;
  795. }
  796. /* Check if anything changed */
  797. if ((ast_channel_tech_pvt(c0) != pvt0) ||
  798. (ast_channel_tech_pvt(c1) != pvt1) ||
  799. (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
  800. (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
  801. (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
  802. ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
  803. /* If a masquerade needs to happen we have to try to read in a frame so that it actually happens. Without this we risk being called again and going into a loop */
  804. if ((ast_channel_masq(c0) || ast_channel_masqr(c0)) && (fr = ast_read(c0))) {
  805. ast_frfree(fr);
  806. }
  807. if ((ast_channel_masq(c1) || ast_channel_masqr(c1)) && (fr = ast_read(c1))) {
  808. ast_frfree(fr);
  809. }
  810. res = AST_BRIDGE_RETRY;
  811. break;
  812. }
  813. /* Wait on a channel to feed us a frame */
  814. ms = ast_remaining_ms(start, timeoutms);
  815. if (!(who = ast_waitfor_n(cs, 2, &ms))) {
  816. if (!ms) {
  817. res = AST_BRIDGE_RETRY;
  818. break;
  819. }
  820. ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
  821. if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
  822. break;
  823. }
  824. continue;
  825. }
  826. /* Read in frame from channel */
  827. fr = ast_read(who);
  828. other = (who == c0) ? c1 : c0;
  829. /* Depending on the frame we may need to break out of our bridge */
  830. if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
  831. ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
  832. ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
  833. /* Record received frame and who */
  834. *fo = fr;
  835. *rc = who;
  836. ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
  837. res = AST_BRIDGE_COMPLETE;
  838. break;
  839. } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
  840. if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
  841. (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
  842. (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
  843. (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
  844. (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
  845. (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
  846. /* If we are going on hold, then break callback mode and P2P bridging */
  847. if (fr->subclass.integer == AST_CONTROL_HOLD) {
  848. if (instance0->engine->local_bridge) {
  849. instance0->engine->local_bridge(instance0, NULL);
  850. }
  851. if (instance1->engine->local_bridge) {
  852. instance1->engine->local_bridge(instance1, NULL);
  853. }
  854. instance0->bridged = NULL;
  855. instance1->bridged = NULL;
  856. } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
  857. if (instance0->engine->local_bridge) {
  858. instance0->engine->local_bridge(instance0, instance1);
  859. }
  860. if (instance1->engine->local_bridge) {
  861. instance1->engine->local_bridge(instance1, instance0);
  862. }
  863. instance0->bridged = instance1;
  864. instance1->bridged = instance0;
  865. }
  866. /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
  867. if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
  868. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  869. }
  870. ast_frfree(fr);
  871. } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
  872. if (ast_channel_connected_line_sub(who, other, fr, 1) &&
  873. ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
  874. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  875. }
  876. ast_frfree(fr);
  877. } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
  878. if (ast_channel_redirecting_sub(who, other, fr, 1) &&
  879. ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
  880. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  881. }
  882. ast_frfree(fr);
  883. } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
  884. ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
  885. ast_frfree(fr);
  886. } else {
  887. *fo = fr;
  888. *rc = who;
  889. ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
  890. res = AST_BRIDGE_COMPLETE;
  891. break;
  892. }
  893. } else {
  894. if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
  895. (fr->frametype == AST_FRAME_DTMF_END) ||
  896. (fr->frametype == AST_FRAME_VOICE) ||
  897. (fr->frametype == AST_FRAME_VIDEO) ||
  898. (fr->frametype == AST_FRAME_IMAGE) ||
  899. (fr->frametype == AST_FRAME_HTML) ||
  900. (fr->frametype == AST_FRAME_MODEM) ||
  901. (fr->frametype == AST_FRAME_TEXT)) {
  902. ast_write(other, fr);
  903. }
  904. ast_frfree(fr);
  905. }
  906. /* Swap priority */
  907. cs[2] = cs[0];
  908. cs[0] = cs[1];
  909. cs[1] = cs[2];
  910. }
  911. /* Stop locally bridging both instances */
  912. if (instance0->engine->local_bridge) {
  913. instance0->engine->local_bridge(instance0, NULL);
  914. }
  915. if (instance1->engine->local_bridge) {
  916. instance1->engine->local_bridge(instance1, NULL);
  917. }
  918. instance0->bridged = NULL;
  919. instance1->bridged = NULL;
  920. ast_poll_channel_del(c0, c1);
  921. return res;
  922. }
  923. static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0,
  924. struct ast_channel *c1,
  925. struct ast_rtp_instance *instance0,
  926. struct ast_rtp_instance *instance1,
  927. struct ast_rtp_instance *vinstance0,
  928. struct ast_rtp_instance *vinstance1,
  929. struct ast_rtp_instance *tinstance0,
  930. struct ast_rtp_instance *tinstance1,
  931. struct ast_rtp_glue *glue0,
  932. struct ast_rtp_glue *glue1,
  933. struct ast_format_cap *cap0,
  934. struct ast_format_cap *cap1,
  935. int timeoutms,
  936. int flags,
  937. struct ast_frame **fo,
  938. struct ast_channel **rc,
  939. void *pvt0,
  940. void *pvt1)
  941. {
  942. enum ast_bridge_result res = AST_BRIDGE_FAILED;
  943. struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
  944. struct ast_format_cap *oldcap0 = ast_format_cap_dup(cap0);
  945. struct ast_format_cap *oldcap1 = ast_format_cap_dup(cap1);
  946. struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
  947. struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
  948. struct ast_frame *fr = NULL;
  949. struct timeval start;
  950. if (!oldcap0 || !oldcap1) {
  951. ast_channel_unlock(c0);
  952. ast_channel_unlock(c1);
  953. goto remote_bridge_cleanup;
  954. }
  955. /* Test the first channel */
  956. if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0))) {
  957. ast_rtp_instance_get_remote_address(instance1, &ac1);
  958. if (vinstance1) {
  959. ast_rtp_instance_get_remote_address(vinstance1, &vac1);
  960. }
  961. if (tinstance1) {
  962. ast_rtp_instance_get_remote_address(tinstance1, &tac1);
  963. }
  964. } else {
  965. ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
  966. }
  967. /* Test the second channel */
  968. if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0))) {
  969. ast_rtp_instance_get_remote_address(instance0, &ac0);
  970. if (vinstance0) {
  971. ast_rtp_instance_get_remote_address(instance0, &vac0);
  972. }
  973. if (tinstance0) {
  974. ast_rtp_instance_get_remote_address(instance0, &tac0);
  975. }
  976. } else {
  977. ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
  978. }
  979. ast_channel_unlock(c0);
  980. ast_channel_unlock(c1);
  981. instance0->bridged = instance1;
  982. instance1->bridged = instance0;
  983. ast_poll_channel_add(c0, c1);
  984. /* Go into a loop handling any stray frames that may come in */
  985. cs[0] = c0;
  986. cs[1] = c1;
  987. cs[2] = NULL;
  988. start = ast_tvnow();
  989. for (;;) {
  990. int ms;
  991. /* Check if anything changed */
  992. if ((ast_channel_tech_pvt(c0) != pvt0) ||
  993. (ast_channel_tech_pvt(c1) != pvt1) ||
  994. (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
  995. (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
  996. (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
  997. ast_debug(1, "Oooh, something is weird, backing out\n");
  998. res = AST_BRIDGE_RETRY;
  999. break;
  1000. }
  1001. /* Check if they have changed their address */
  1002. ast_rtp_instance_get_remote_address(instance1, &t1);
  1003. if (vinstance1) {
  1004. ast_rtp_instance_get_remote_address(vinstance1, &vt1);
  1005. }
  1006. if (tinstance1) {
  1007. ast_rtp_instance_get_remote_address(tinstance1, &tt1);
  1008. }
  1009. ast_channel_lock(c1);
  1010. if (glue1->get_codec && ast_channel_tech_pvt(c1)) {
  1011. ast_format_cap_remove_all(cap1);
  1012. glue1->get_codec(c1, cap1);
  1013. }
  1014. ast_channel_unlock(c1);
  1015. ast_rtp_instance_get_remote_address(instance0, &t0);
  1016. if (vinstance0) {
  1017. ast_rtp_instance_get_remote_address(vinstance0, &vt0);
  1018. }
  1019. if (tinstance0) {
  1020. ast_rtp_instance_get_remote_address(tinstance0, &tt0);
  1021. }
  1022. ast_channel_lock(c0);
  1023. if (glue0->get_codec && ast_channel_tech_pvt(c0)) {
  1024. ast_format_cap_remove_all(cap0);
  1025. glue0->get_codec(c0, cap0);
  1026. }
  1027. ast_channel_unlock(c0);
  1028. if ((ast_sockaddr_cmp(&t1, &ac1)) ||
  1029. (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
  1030. (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
  1031. (!ast_format_cap_identical(cap1, oldcap1))) {
  1032. char tmp_buf[512] = { 0, };
  1033. ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
  1034. ast_channel_name(c1), ast_sockaddr_stringify(&t1),
  1035. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
  1036. ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
  1037. ast_channel_name(c1), ast_sockaddr_stringify(&vt1),
  1038. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
  1039. ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
  1040. ast_channel_name(c1), ast_sockaddr_stringify(&tt1),
  1041. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
  1042. ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
  1043. ast_channel_name(c1), ast_sockaddr_stringify(&ac1),
  1044. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
  1045. ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
  1046. ast_channel_name(c1), ast_sockaddr_stringify(&vac1),
  1047. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
  1048. ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
  1049. ast_channel_name(c1), ast_sockaddr_stringify(&tac1),
  1050. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
  1051. if (glue0->update_peer(c0,
  1052. ast_sockaddr_isnull(&t1) ? NULL : instance1,
  1053. ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
  1054. ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
  1055. cap1, 0)) {
  1056. ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
  1057. }
  1058. ast_sockaddr_copy(&ac1, &t1);
  1059. ast_sockaddr_copy(&vac1, &vt1);
  1060. ast_sockaddr_copy(&tac1, &tt1);
  1061. ast_format_cap_copy(oldcap1, cap1);
  1062. }
  1063. if ((ast_sockaddr_cmp(&t0, &ac0)) ||
  1064. (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
  1065. (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
  1066. (!ast_format_cap_identical(cap0, oldcap0))) {
  1067. char tmp_buf[512] = { 0, };
  1068. ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
  1069. ast_channel_name(c0), ast_sockaddr_stringify(&t0),
  1070. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap0));
  1071. ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
  1072. ast_channel_name(c0), ast_sockaddr_stringify(&ac0),
  1073. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap0));
  1074. if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
  1075. vt0.len ? vinstance0 : NULL,
  1076. tt0.len ? tinstance0 : NULL,
  1077. cap0, 0)) {
  1078. ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
  1079. }
  1080. ast_sockaddr_copy(&ac0, &t0);
  1081. ast_sockaddr_copy(&vac0, &vt0);
  1082. ast_sockaddr_copy(&tac0, &tt0);
  1083. ast_format_cap_copy(oldcap0, cap0);
  1084. }
  1085. ms = ast_remaining_ms(start, timeoutms);
  1086. /* Wait for frame to come in on the channels */
  1087. if (!(who = ast_waitfor_n(cs, 2, &ms))) {
  1088. if (!ms) {
  1089. res = AST_BRIDGE_RETRY;
  1090. break;
  1091. }
  1092. ast_debug(1, "Ooh, empty read...\n");
  1093. if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
  1094. break;
  1095. }
  1096. continue;
  1097. }
  1098. fr = ast_read(who);
  1099. other = (who == c0) ? c1 : c0;
  1100. if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
  1101. (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
  1102. ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
  1103. /* Break out of bridge */
  1104. *fo = fr;
  1105. *rc = who;
  1106. ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
  1107. res = AST_BRIDGE_COMPLETE;
  1108. break;
  1109. } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
  1110. if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
  1111. (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
  1112. (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
  1113. (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
  1114. (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
  1115. (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
  1116. if (fr->subclass.integer == AST_CONTROL_HOLD) {
  1117. /* If we someone went on hold we want the other side to reinvite back to us */
  1118. if (who == c0) {
  1119. glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
  1120. } else {
  1121. glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
  1122. }
  1123. } else if (fr->subclass.integer == AST_CONTROL_UNHOLD ||
  1124. fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER) {
  1125. /* If they went off hold they should go back to being direct, or if we have
  1126. * been told to force a peer update, go ahead and do it. */
  1127. if (who == c0) {
  1128. glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
  1129. } else {
  1130. glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
  1131. }
  1132. }
  1133. /* Update local address information */
  1134. ast_rtp_instance_get_remote_address(instance0, &t0);
  1135. ast_sockaddr_copy(&ac0, &t0);
  1136. ast_rtp_instance_get_remote_address(instance1, &t1);
  1137. ast_sockaddr_copy(&ac1, &t1);
  1138. /* Update codec information */
  1139. ast_channel_lock(c0);
  1140. if (glue0->get_codec && ast_channel_tech_pvt(c0)) {
  1141. ast_format_cap_remove_all(cap0);
  1142. ast_format_cap_remove_all(oldcap0);
  1143. glue0->get_codec(c0, cap0);
  1144. ast_format_cap_append(oldcap0, cap0);
  1145. }
  1146. ast_channel_unlock(c0);
  1147. ast_channel_lock(c1);
  1148. if (glue1->get_codec && ast_channel_tech_pvt(c1)) {
  1149. ast_format_cap_remove_all(cap1);
  1150. ast_format_cap_remove_all(oldcap1);
  1151. glue1->get_codec(c1, cap1);
  1152. ast_format_cap_append(oldcap1, cap1);
  1153. }
  1154. ast_channel_unlock(c1);
  1155. /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
  1156. if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
  1157. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  1158. }
  1159. ast_frfree(fr);
  1160. } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
  1161. if (ast_channel_connected_line_sub(who, other, fr, 1) &&
  1162. ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
  1163. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  1164. }
  1165. ast_frfree(fr);
  1166. } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
  1167. if (ast_channel_redirecting_sub(who, other, fr, 1) &&
  1168. ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
  1169. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  1170. }
  1171. ast_frfree(fr);
  1172. } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
  1173. ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
  1174. ast_frfree(fr);
  1175. } else {
  1176. *fo = fr;
  1177. *rc = who;
  1178. ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
  1179. res = AST_BRIDGE_COMPLETE;
  1180. break;
  1181. }
  1182. } else {
  1183. if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
  1184. (fr->frametype == AST_FRAME_DTMF_END) ||
  1185. (fr->frametype == AST_FRAME_VOICE) ||
  1186. (fr->frametype == AST_FRAME_VIDEO) ||
  1187. (fr->frametype == AST_FRAME_IMAGE) ||
  1188. (fr->frametype == AST_FRAME_HTML) ||
  1189. (fr->frametype == AST_FRAME_MODEM) ||
  1190. (fr->frametype == AST_FRAME_TEXT)) {
  1191. ast_write(other, fr);
  1192. }
  1193. ast_frfree(fr);
  1194. }
  1195. /* Swap priority */
  1196. cs[2] = cs[0];
  1197. cs[0] = cs[1];
  1198. cs[1] = cs[2];
  1199. }
  1200. if (ast_test_flag(ast_channel_flags(c0), AST_FLAG_ZOMBIE)) {
  1201. ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c0));
  1202. } else if (ast_channel_tech_pvt(c0) != pvt0) {
  1203. ast_debug(1, "Channel c0->'%s' pvt changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
  1204. } else if (glue0 != ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) {
  1205. ast_debug(1, "Channel c0->'%s' technology changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
  1206. } else if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
  1207. ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c0));
  1208. }
  1209. if (ast_test_flag(ast_channel_flags(c1), AST_FLAG_ZOMBIE)) {
  1210. ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c1));
  1211. } else if (ast_channel_tech_pvt(c1) != pvt1) {
  1212. ast_debug(1, "Channel c1->'%s' pvt changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
  1213. } else if (glue1 != ast_rtp_instance_get_glue(ast_channel_tech(c1)->type)) {
  1214. ast_debug(1, "Channel c1->'%s' technology changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
  1215. } else if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
  1216. ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c1));
  1217. }
  1218. instance0->bridged = NULL;
  1219. instance1->bridged = NULL;
  1220. ast_poll_channel_del(c0, c1);
  1221. remote_bridge_cleanup:
  1222. ast_format_cap_destroy(oldcap0);
  1223. ast_format_cap_destroy(oldcap1);
  1224. return res;
  1225. }
  1226. /*!
  1227. * \brief Conditionally unref an rtp instance
  1228. */
  1229. static void unref_instance_cond(struct ast_rtp_instance **instance)
  1230. {
  1231. if (*instance) {
  1232. ao2_ref(*instance, -1);
  1233. *instance = NULL;
  1234. }
  1235. }
  1236. enum ast_bridge_result ast_rtp_instance_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
  1237. {
  1238. struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
  1239. *vinstance0 = NULL, *vinstance1 = NULL,
  1240. *tinstance0 = NULL, *tinstance1 = NULL;
  1241. struct ast_rtp_glue *glue0, *glue1;
  1242. struct ast_sockaddr addr1 = { {0, }, }, addr2 = { {0, }, };
  1243. enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1244. enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1245. enum ast_bridge_result res = AST_BRIDGE_FAILED;
  1246. enum ast_rtp_dtmf_mode dmode;
  1247. struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
  1248. struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
  1249. int unlock_chans = 1;
  1250. int read_ptime0, read_ptime1, write_ptime0, write_ptime1;
  1251. if (!cap0 || !cap1) {
  1252. unlock_chans = 0;
  1253. goto done;
  1254. }
  1255. /* Lock both channels so we can look for the glue that binds them together */
  1256. ast_channel_lock(c0);
  1257. while (ast_channel_trylock(c1)) {
  1258. ast_channel_unlock(c0);
  1259. usleep(1);
  1260. ast_channel_lock(c0);
  1261. }
  1262. /* Ensure neither channel got hungup during lock avoidance */
  1263. if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
  1264. ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
  1265. goto done;
  1266. }
  1267. /* Grab glue that binds each channel to something using the RTP engine */
  1268. if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
  1269. ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
  1270. goto done;
  1271. }
  1272. audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
  1273. video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
  1274. audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
  1275. video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
  1276. /* If the channels are of the same technology, they might have limitations on remote bridging */
  1277. if (ast_channel_tech(c0) == ast_channel_tech(c1)) {
  1278. if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
  1279. if (glue0->allow_rtp_remote && !(glue0->allow_rtp_remote(c0, c1))) {
  1280. /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
  1281. audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
  1282. }
  1283. }
  1284. if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
  1285. if (glue0->allow_vrtp_remote && !(glue0->allow_vrtp_remote(c0, c1))) {
  1286. /* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
  1287. video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
  1288. }
  1289. }
  1290. }
  1291. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  1292. 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)) {
  1293. audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1294. }
  1295. 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)) {
  1296. audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1297. }
  1298. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  1299. if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
  1300. res = AST_BRIDGE_FAILED_NOWARN;
  1301. goto done;
  1302. }
  1303. /* If address families differ, force a local bridge */
  1304. ast_rtp_instance_get_remote_address(instance0, &addr1);
  1305. ast_rtp_instance_get_remote_address(instance1, &addr2);
  1306. if (addr1.ss.ss_family != addr2.ss.ss_family ||
  1307. (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
  1308. audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
  1309. audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
  1310. }
  1311. /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
  1312. dmode = ast_rtp_instance_dtmf_mode_get(instance0);
  1313. if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && dmode) {
  1314. res = AST_BRIDGE_FAILED_NOWARN;
  1315. goto done;
  1316. }
  1317. dmode = ast_rtp_instance_dtmf_mode_get(instance1);
  1318. if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && dmode) {
  1319. res = AST_BRIDGE_FAILED_NOWARN;
  1320. goto done;
  1321. }
  1322. /* If we have gotten to a local bridge make sure that both sides have the same local bridge callback and that they are DTMF compatible */
  1323. if ((audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) && ((instance0->engine->local_bridge != instance1->engine->local_bridge) || (instance0->engine->dtmf_compatible && !instance0->engine->dtmf_compatible(c0, instance0, c1, instance1)))) {
  1324. res = AST_BRIDGE_FAILED_NOWARN;
  1325. goto done;
  1326. }
  1327. /* Make sure that codecs match */
  1328. if (glue0->get_codec){
  1329. glue0->get_codec(c0, cap0);
  1330. }
  1331. if (glue1->get_codec) {
  1332. glue1->get_codec(c1, cap1);
  1333. }
  1334. if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
  1335. char tmp0[256] = { 0, };
  1336. char tmp1[256] = { 0, };
  1337. ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
  1338. ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
  1339. ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
  1340. res = AST_BRIDGE_FAILED_NOWARN;
  1341. goto done;
  1342. }
  1343. read_ptime0 = (ast_codec_pref_getsize(&instance0->codecs.pref, ast_channel_rawreadformat(c0))).cur_ms;
  1344. read_ptime1 = (ast_codec_pref_getsize(&instance1->codecs.pref, ast_channel_rawreadformat(c1))).cur_ms;
  1345. write_ptime0 = (ast_codec_pref_getsize(&instance0->codecs.pref, ast_channel_rawwriteformat(c0))).cur_ms;
  1346. write_ptime1 = (ast_codec_pref_getsize(&instance1->codecs.pref, ast_channel_rawwriteformat(c1))).cur_ms;
  1347. if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
  1348. ast_debug(1, "Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
  1349. read_ptime0, write_ptime1, read_ptime1, write_ptime0);
  1350. res = AST_BRIDGE_FAILED_NOWARN;
  1351. goto done;
  1352. }
  1353. instance0->glue = glue0;
  1354. instance1->glue = glue1;
  1355. instance0->chan = c0;
  1356. instance1->chan = c1;
  1357. /* Depending on the end result for bridging either do a local bridge or remote bridge */
  1358. if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
  1359. ast_verb(3, "Locally bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
  1360. res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
  1361. } else {
  1362. ast_verb(3, "Remotely bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
  1363. res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
  1364. tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
  1365. fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
  1366. }
  1367. instance0->glue = NULL;
  1368. instance1->glue = NULL;
  1369. instance0->chan = NULL;
  1370. instance1->chan = NULL;
  1371. unlock_chans = 0;
  1372. done:
  1373. if (unlock_chans) {
  1374. ast_channel_unlock(c0);
  1375. ast_channel_unlock(c1);
  1376. }
  1377. ast_format_cap_destroy(cap1);
  1378. ast_format_cap_destroy(cap0);
  1379. unref_instance_cond(&instance0);
  1380. unref_instance_cond(&instance1);
  1381. unref_instance_cond(&vinstance0);
  1382. unref_instance_cond(&vinstance1);
  1383. unref_instance_cond(&tinstance0);
  1384. unref_instance_cond(&tinstance1);
  1385. return res;
  1386. }
  1387. struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
  1388. {
  1389. return instance->bridged;
  1390. }
  1391. void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
  1392. {
  1393. struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
  1394. *vinstance_dst = NULL, *vinstance_src = NULL,
  1395. *tinstance_dst = NULL, *tinstance_src = NULL;
  1396. struct ast_rtp_glue *glue_dst, *glue_src;
  1397. enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
  1398. enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
  1399. struct ast_format_cap *cap_dst = ast_format_cap_alloc_nolock();
  1400. struct ast_format_cap *cap_src = ast_format_cap_alloc_nolock();
  1401. /* Lock both channels so we can look for the glue that binds them together */
  1402. ast_channel_lock_both(c_dst, c_src);
  1403. if (!cap_src || !cap_dst) {
  1404. goto done;
  1405. }
  1406. /* Grab glue that binds each channel to something using the RTP engine */
  1407. 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))) {
  1408. ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
  1409. goto done;
  1410. }
  1411. audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
  1412. video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
  1413. audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
  1414. video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
  1415. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  1416. 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)) {
  1417. audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
  1418. }
  1419. 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)) {
  1420. audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
  1421. }
  1422. 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) {
  1423. glue_dst->get_codec(c_dst, cap_dst);
  1424. }
  1425. 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) {
  1426. glue_src->get_codec(c_src, cap_src);
  1427. }
  1428. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  1429. if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
  1430. goto done;
  1431. }
  1432. /* Make sure we have matching codecs */
  1433. if (!ast_format_cap_has_joint(cap_dst, cap_src)) {
  1434. goto done;
  1435. }
  1436. ast_rtp_codecs_payloads_copy(&instance_src->codecs, &instance_dst->codecs, instance_dst);
  1437. if (vinstance_dst && vinstance_src) {
  1438. ast_rtp_codecs_payloads_copy(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
  1439. }
  1440. if (tinstance_dst && tinstance_src) {
  1441. ast_rtp_codecs_payloads_copy(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
  1442. }
  1443. if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
  1444. ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
  1445. ast_channel_name(c_dst), ast_channel_name(c_src));
  1446. } else {
  1447. ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
  1448. ast_channel_name(c_dst), ast_channel_name(c_src));
  1449. }
  1450. done:
  1451. ast_channel_unlock(c_dst);
  1452. ast_channel_unlock(c_src);
  1453. ast_format_cap_destroy(cap_dst);
  1454. ast_format_cap_destroy(cap_src);
  1455. unref_instance_cond(&instance_dst);
  1456. unref_instance_cond(&instance_src);
  1457. unref_instance_cond(&vinstance_dst);
  1458. unref_instance_cond(&vinstance_src);
  1459. unref_instance_cond(&tinstance_dst);
  1460. unref_instance_cond(&tinstance_src);
  1461. }
  1462. int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
  1463. {
  1464. struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
  1465. *vinstance0 = NULL, *vinstance1 = NULL,
  1466. *tinstance0 = NULL, *tinstance1 = NULL;
  1467. struct ast_rtp_glue *glue0, *glue1;
  1468. enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1469. enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1470. struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
  1471. struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
  1472. int res = 0;
  1473. /* If there is no second channel just immediately bail out, we are of no use in that scenario */
  1474. if (!c1) {
  1475. ast_format_cap_destroy(cap0);
  1476. ast_format_cap_destroy(cap1);
  1477. return -1;
  1478. }
  1479. /* Lock both channels so we can look for the glue that binds them together */
  1480. ast_channel_lock(c0);
  1481. while (ast_channel_trylock(c1)) {
  1482. ast_channel_unlock(c0);
  1483. usleep(1);
  1484. ast_channel_lock(c0);
  1485. }
  1486. if (!cap1 || !cap0) {
  1487. goto done;
  1488. }
  1489. /* Grab glue that binds each channel to something using the RTP engine */
  1490. if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
  1491. ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
  1492. goto done;
  1493. }
  1494. audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
  1495. video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
  1496. audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
  1497. video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
  1498. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  1499. 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)) {
  1500. audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1501. }
  1502. 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)) {
  1503. audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1504. }
  1505. 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) {
  1506. glue0->get_codec(c0, cap0);
  1507. }
  1508. 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) {
  1509. glue1->get_codec(c1, cap1);
  1510. }
  1511. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  1512. if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
  1513. goto done;
  1514. }
  1515. /* Make sure we have matching codecs */
  1516. if (!ast_format_cap_has_joint(cap0, cap1)) {
  1517. goto done;
  1518. }
  1519. /* Bridge media early */
  1520. if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
  1521. ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
  1522. }
  1523. res = 0;
  1524. done:
  1525. ast_channel_unlock(c0);
  1526. ast_channel_unlock(c1);
  1527. ast_format_cap_destroy(cap0);
  1528. ast_format_cap_destroy(cap1);
  1529. unref_instance_cond(&instance0);
  1530. unref_instance_cond(&instance1);
  1531. unref_instance_cond(&vinstance0);
  1532. unref_instance_cond(&vinstance1);
  1533. unref_instance_cond(&tinstance0);
  1534. unref_instance_cond(&tinstance1);
  1535. if (!res) {
  1536. ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
  1537. }
  1538. return res;
  1539. }
  1540. int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
  1541. {
  1542. return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
  1543. }
  1544. int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
  1545. {
  1546. return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
  1547. }
  1548. int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
  1549. {
  1550. return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
  1551. }
  1552. char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
  1553. {
  1554. struct ast_rtp_instance_stats stats = { 0, };
  1555. enum ast_rtp_instance_stat stat;
  1556. /* Determine what statistics we will need to retrieve based on field passed in */
  1557. if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
  1558. stat = AST_RTP_INSTANCE_STAT_ALL;
  1559. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
  1560. stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
  1561. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
  1562. stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
  1563. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
  1564. stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
  1565. } else {
  1566. return NULL;
  1567. }
  1568. /* Attempt to actually retrieve the statistics we need to generate the quality string */
  1569. if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
  1570. return NULL;
  1571. }
  1572. /* Now actually fill the buffer with the good information */
  1573. if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
  1574. snprintf(buf, size, "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
  1575. stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
  1576. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
  1577. snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
  1578. 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));
  1579. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
  1580. snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
  1581. 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));
  1582. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
  1583. snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
  1584. }
  1585. return buf;
  1586. }
  1587. void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
  1588. {
  1589. char quality_buf[AST_MAX_USER_FIELD], *quality;
  1590. struct ast_channel *bridge = ast_bridged_channel(chan);
  1591. if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
  1592. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
  1593. if (bridge) {
  1594. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
  1595. }
  1596. }
  1597. if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
  1598. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
  1599. if (bridge) {
  1600. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
  1601. }
  1602. }
  1603. if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
  1604. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
  1605. if (bridge) {
  1606. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
  1607. }
  1608. }
  1609. if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
  1610. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
  1611. if (bridge) {
  1612. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
  1613. }
  1614. }
  1615. }
  1616. int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
  1617. {
  1618. return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
  1619. }
  1620. int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
  1621. {
  1622. return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
  1623. }
  1624. int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
  1625. {
  1626. struct ast_rtp_glue *glue;
  1627. struct ast_rtp_instance *peer_instance = NULL;
  1628. int res = -1;
  1629. if (!instance->engine->make_compatible) {
  1630. return -1;
  1631. }
  1632. ast_channel_lock(peer);
  1633. if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
  1634. ast_channel_unlock(peer);
  1635. return -1;
  1636. }
  1637. glue->get_rtp_info(peer, &peer_instance);
  1638. if (!peer_instance) {
  1639. ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
  1640. ast_channel_unlock(peer);
  1641. return -1;
  1642. }
  1643. if (peer_instance->engine != instance->engine) {
  1644. ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
  1645. ast_channel_unlock(peer);
  1646. ao2_ref(peer_instance, -1);
  1647. return -1;
  1648. }
  1649. res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
  1650. ast_channel_unlock(peer);
  1651. ao2_ref(peer_instance, -1);
  1652. peer_instance = NULL;
  1653. return res;
  1654. }
  1655. 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)
  1656. {
  1657. if (instance->engine->available_formats) {
  1658. instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
  1659. if (!ast_format_cap_is_empty(result)) {
  1660. return;
  1661. }
  1662. }
  1663. ast_translate_available_formats(to_endpoint, to_asterisk, result);
  1664. }
  1665. int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
  1666. {
  1667. return instance->engine->activate ? instance->engine->activate(instance) : 0;
  1668. }
  1669. void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
  1670. struct ast_sockaddr *suggestion,
  1671. const char *username)
  1672. {
  1673. if (instance->engine->stun_request) {
  1674. instance->engine->stun_request(instance, suggestion, username);
  1675. }
  1676. }
  1677. void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
  1678. {
  1679. instance->timeout = timeout;
  1680. }
  1681. void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
  1682. {
  1683. instance->holdtimeout = timeout;
  1684. }
  1685. void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
  1686. {
  1687. instance->keepalive = interval;
  1688. }
  1689. int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
  1690. {
  1691. return instance->timeout;
  1692. }
  1693. int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
  1694. {
  1695. return instance->holdtimeout;
  1696. }
  1697. int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
  1698. {
  1699. return instance->keepalive;
  1700. }
  1701. struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
  1702. {
  1703. return instance->engine;
  1704. }
  1705. struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
  1706. {
  1707. return instance->glue;
  1708. }
  1709. struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
  1710. {
  1711. return instance->chan;
  1712. }
  1713. int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
  1714. {
  1715. if (res_srtp || res_srtp_policy) {
  1716. return -1;
  1717. }
  1718. if (!srtp_res || !policy_res) {
  1719. return -1;
  1720. }
  1721. res_srtp = srtp_res;
  1722. res_srtp_policy = policy_res;
  1723. return 0;
  1724. }
  1725. void ast_rtp_engine_unregister_srtp(void)
  1726. {
  1727. res_srtp = NULL;
  1728. res_srtp_policy = NULL;
  1729. }
  1730. int ast_rtp_engine_srtp_is_registered(void)
  1731. {
  1732. return res_srtp && res_srtp_policy;
  1733. }
  1734. int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
  1735. {
  1736. int res = 0;
  1737. if (!res_srtp) {
  1738. return -1;
  1739. }
  1740. if (!instance->srtp) {
  1741. res = res_srtp->create(&instance->srtp, instance, remote_policy);
  1742. } else {
  1743. res = res_srtp->replace(&instance->srtp, instance, remote_policy);
  1744. }
  1745. if (!res) {
  1746. res = res_srtp->add_stream(instance->srtp, local_policy);
  1747. }
  1748. return res;
  1749. }
  1750. struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
  1751. {
  1752. return instance->srtp;
  1753. }
  1754. int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
  1755. {
  1756. if (instance->engine->sendcng) {
  1757. return instance->engine->sendcng(instance, level);
  1758. }
  1759. return -1;
  1760. }
  1761. struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
  1762. {
  1763. return instance->engine->ice;
  1764. }
  1765. struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
  1766. {
  1767. return instance->engine->dtls;
  1768. }
  1769. int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
  1770. {
  1771. if (!strcasecmp(name, "dtlsenable")) {
  1772. dtls_cfg->enabled = ast_true(value) ? 1 : 0;
  1773. } else if (!strcasecmp(name, "dtlsverify")) {
  1774. if (!strcasecmp(value, "yes")) {
  1775. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT | AST_RTP_DTLS_VERIFY_CERTIFICATE;
  1776. } else if (!strcasecmp(value, "fingerprint")) {
  1777. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
  1778. } else if (!strcasecmp(value, "certificate")) {
  1779. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_CERTIFICATE;
  1780. } else if (!strcasecmp(value, "no")) {
  1781. dtls_cfg->verify = AST_RTP_DTLS_VERIFY_NONE;
  1782. } else {
  1783. return -1;
  1784. }
  1785. } else if (!strcasecmp(name, "dtlsrekey")) {
  1786. if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
  1787. return -1;
  1788. }
  1789. } else if (!strcasecmp(name, "dtlscertfile")) {
  1790. ast_free(dtls_cfg->certfile);
  1791. dtls_cfg->certfile = ast_strdup(value);
  1792. } else if (!strcasecmp(name, "dtlsprivatekey")) {
  1793. ast_free(dtls_cfg->pvtfile);
  1794. dtls_cfg->pvtfile = ast_strdup(value);
  1795. } else if (!strcasecmp(name, "dtlscipher")) {
  1796. ast_free(dtls_cfg->cipher);
  1797. dtls_cfg->cipher = ast_strdup(value);
  1798. } else if (!strcasecmp(name, "dtlscafile")) {
  1799. ast_free(dtls_cfg->cafile);
  1800. dtls_cfg->cafile = ast_strdup(value);
  1801. } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
  1802. ast_free(dtls_cfg->capath);
  1803. dtls_cfg->capath = ast_strdup(value);
  1804. } else if (!strcasecmp(name, "dtlssetup")) {
  1805. if (!strcasecmp(value, "active")) {
  1806. dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
  1807. } else if (!strcasecmp(value, "passive")) {
  1808. dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
  1809. } else if (!strcasecmp(value, "actpass")) {
  1810. dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
  1811. }
  1812. } else if (!strcasecmp(name, "dtlsfingerprint")) {
  1813. if (!strcasecmp(value, "sha-256")) {
  1814. dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA256;
  1815. } else if (!strcasecmp(value, "sha-1")) {
  1816. dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA1;
  1817. }
  1818. } else {
  1819. return -1;
  1820. }
  1821. return 0;
  1822. }
  1823. void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
  1824. {
  1825. dst_cfg->enabled = src_cfg->enabled;
  1826. dst_cfg->verify = src_cfg->verify;
  1827. dst_cfg->rekey = src_cfg->rekey;
  1828. dst_cfg->suite = src_cfg->suite;
  1829. dst_cfg->hash = src_cfg->hash;
  1830. dst_cfg->certfile = ast_strdup(src_cfg->certfile);
  1831. dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
  1832. dst_cfg->cipher = ast_strdup(src_cfg->cipher);
  1833. dst_cfg->cafile = ast_strdup(src_cfg->cafile);
  1834. dst_cfg->capath = ast_strdup(src_cfg->capath);
  1835. dst_cfg->default_setup = src_cfg->default_setup;
  1836. }
  1837. void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
  1838. {
  1839. ast_free(dtls_cfg->certfile);
  1840. ast_free(dtls_cfg->pvtfile);
  1841. ast_free(dtls_cfg->cipher);
  1842. ast_free(dtls_cfg->cafile);
  1843. ast_free(dtls_cfg->capath);
  1844. }
  1845. static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
  1846. {
  1847. int x = mime_types_len;
  1848. if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
  1849. return;
  1850. }
  1851. ast_rwlock_wrlock(&mime_types_lock);
  1852. if (format) {
  1853. ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
  1854. ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
  1855. } else {
  1856. ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
  1857. }
  1858. ast_rtp_mime_types[x].type = type;
  1859. ast_rtp_mime_types[x].subtype = subtype;
  1860. ast_rtp_mime_types[x].sample_rate = sample_rate;
  1861. mime_types_len++;
  1862. ast_rwlock_unlock(&mime_types_lock);
  1863. }
  1864. static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
  1865. {
  1866. int x;
  1867. ast_rwlock_wrlock(&static_RTP_PT_lock);
  1868. if (map < 0) {
  1869. /* find next available dynamic payload slot */
  1870. for (x = 96; x < 127; x++) {
  1871. if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
  1872. map = x;
  1873. break;
  1874. }
  1875. }
  1876. }
  1877. if (map < 0) {
  1878. ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n" ,ast_getformatname(format));
  1879. ast_rwlock_unlock(&static_RTP_PT_lock);
  1880. return;
  1881. }
  1882. if (format) {
  1883. static_RTP_PT[map].asterisk_format = 1;
  1884. ast_format_copy(&static_RTP_PT[map].format, format);
  1885. } else {
  1886. static_RTP_PT[map].rtp_code = rtp_code;
  1887. }
  1888. ast_rwlock_unlock(&static_RTP_PT_lock);
  1889. }
  1890. int ast_rtp_engine_load_format(const struct ast_format *format)
  1891. {
  1892. switch (format->id) {
  1893. case AST_FORMAT_SILK:
  1894. set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
  1895. add_static_payload(-1, format, 0);
  1896. break;
  1897. case AST_FORMAT_CELT:
  1898. set_next_mime_type(format, 0, "audio", "CELT", ast_format_rate(format));
  1899. add_static_payload(-1, format, 0);
  1900. break;
  1901. default:
  1902. break;
  1903. }
  1904. return 0;
  1905. }
  1906. int ast_rtp_engine_unload_format(const struct ast_format *format)
  1907. {
  1908. int x;
  1909. int y = 0;
  1910. ast_rwlock_wrlock(&static_RTP_PT_lock);
  1911. /* remove everything pertaining to this format id from the lists */
  1912. for (x = 0; x < AST_RTP_MAX_PT; x++) {
  1913. if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
  1914. memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
  1915. }
  1916. }
  1917. ast_rwlock_unlock(&static_RTP_PT_lock);
  1918. ast_rwlock_wrlock(&mime_types_lock);
  1919. /* rebuild the list skipping the items matching this id */
  1920. for (x = 0; x < mime_types_len; x++) {
  1921. if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
  1922. continue;
  1923. }
  1924. ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
  1925. y++;
  1926. }
  1927. mime_types_len = y;
  1928. ast_rwlock_unlock(&mime_types_lock);
  1929. return 0;
  1930. }
  1931. int ast_rtp_engine_init()
  1932. {
  1933. struct ast_format tmpfmt;
  1934. ast_rwlock_init(&mime_types_lock);
  1935. ast_rwlock_init(&static_RTP_PT_lock);
  1936. /* Define all the RTP mime types available */
  1937. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
  1938. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
  1939. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
  1940. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
  1941. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
  1942. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
  1943. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
  1944. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
  1945. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
  1946. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
  1947. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16-256", 16000);
  1948. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
  1949. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
  1950. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
  1951. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
  1952. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
  1953. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0, "audio", "speex", 16000);
  1954. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0, "audio", "speex", 32000);
  1955. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
  1956. /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
  1957. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
  1958. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
  1959. set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
  1960. set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
  1961. set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
  1962. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
  1963. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
  1964. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
  1965. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
  1966. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "H263-1998", 90000);
  1967. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
  1968. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
  1969. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
  1970. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
  1971. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
  1972. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
  1973. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
  1974. /* Define the static rtp payload mappings */
  1975. add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
  1976. #ifdef USE_DEPRECATED_G726
  1977. add_static_payload(2, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
  1978. #endif
  1979. add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
  1980. add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
  1981. add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
  1982. add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
  1983. add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
  1984. add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
  1985. add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
  1986. add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
  1987. add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
  1988. add_static_payload(13, NULL, AST_RTP_CN);
  1989. add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
  1990. add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
  1991. add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
  1992. add_static_payload(19, NULL, AST_RTP_CN); /* Also used for CN */
  1993. add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
  1994. add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
  1995. add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
  1996. add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
  1997. add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
  1998. add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
  1999. add_static_payload(101, NULL, AST_RTP_DTMF);
  2000. add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
  2001. add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
  2002. add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
  2003. add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0); /* Real time text chat (with redundancy encoding) */
  2004. add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0); /* Real time text chat */
  2005. add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
  2006. add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
  2007. add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
  2008. add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
  2009. add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
  2010. add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
  2011. add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
  2012. add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
  2013. add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */
  2014. return 0;
  2015. }