rtp_engine.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  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. /* Drop our engine reference */
  178. ast_module_unref(instance->engine->mod);
  179. ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
  180. }
  181. int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
  182. {
  183. ao2_ref(instance, -1);
  184. return 0;
  185. }
  186. struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
  187. struct ast_sched_context *sched, const struct ast_sockaddr *sa,
  188. void *data)
  189. {
  190. struct ast_sockaddr address = {{0,}};
  191. struct ast_rtp_instance *instance = NULL;
  192. struct ast_rtp_engine *engine = NULL;
  193. AST_RWLIST_RDLOCK(&engines);
  194. /* If an engine name was specified try to use it or otherwise use the first one registered */
  195. if (!ast_strlen_zero(engine_name)) {
  196. AST_RWLIST_TRAVERSE(&engines, engine, entry) {
  197. if (!strcmp(engine->name, engine_name)) {
  198. break;
  199. }
  200. }
  201. } else {
  202. engine = AST_RWLIST_FIRST(&engines);
  203. }
  204. /* If no engine was actually found bail out now */
  205. if (!engine) {
  206. ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
  207. AST_RWLIST_UNLOCK(&engines);
  208. return NULL;
  209. }
  210. /* Bump up the reference count before we return so the module can not be unloaded */
  211. ast_module_ref(engine->mod);
  212. AST_RWLIST_UNLOCK(&engines);
  213. /* Allocate a new RTP instance */
  214. if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
  215. ast_module_unref(engine->mod);
  216. return NULL;
  217. }
  218. instance->engine = engine;
  219. ast_sockaddr_copy(&instance->local_address, sa);
  220. ast_sockaddr_copy(&address, sa);
  221. ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
  222. /* And pass it off to the engine to setup */
  223. if (instance->engine->new(instance, sched, &address, data)) {
  224. ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
  225. ao2_ref(instance, -1);
  226. return NULL;
  227. }
  228. ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
  229. return instance;
  230. }
  231. void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
  232. {
  233. instance->data = data;
  234. }
  235. void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
  236. {
  237. return instance->data;
  238. }
  239. int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
  240. {
  241. return instance->engine->write(instance, frame);
  242. }
  243. struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
  244. {
  245. return instance->engine->read(instance, rtcp);
  246. }
  247. int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
  248. const struct ast_sockaddr *address)
  249. {
  250. ast_sockaddr_copy(&instance->local_address, address);
  251. return 0;
  252. }
  253. int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
  254. const struct ast_sockaddr *address)
  255. {
  256. ast_sockaddr_copy(&instance->remote_address, address);
  257. /* moo */
  258. if (instance->engine->remote_address_set) {
  259. instance->engine->remote_address_set(instance, &instance->remote_address);
  260. }
  261. return 0;
  262. }
  263. int ast_rtp_instance_set_alt_remote_address(struct ast_rtp_instance *instance,
  264. const struct ast_sockaddr *address)
  265. {
  266. ast_sockaddr_copy(&instance->alt_remote_address, address);
  267. /* oink */
  268. if (instance->engine->alt_remote_address_set) {
  269. instance->engine->alt_remote_address_set(instance, &instance->alt_remote_address);
  270. }
  271. return 0;
  272. }
  273. int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
  274. struct ast_sockaddr *address)
  275. {
  276. if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
  277. ast_sockaddr_copy(address, &instance->local_address);
  278. return 1;
  279. }
  280. return 0;
  281. }
  282. void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
  283. struct ast_sockaddr *address)
  284. {
  285. ast_sockaddr_copy(address, &instance->local_address);
  286. }
  287. int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
  288. struct ast_sockaddr *address)
  289. {
  290. if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
  291. ast_sockaddr_copy(address, &instance->remote_address);
  292. return 1;
  293. }
  294. return 0;
  295. }
  296. void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
  297. struct ast_sockaddr *address)
  298. {
  299. ast_sockaddr_copy(address, &instance->remote_address);
  300. }
  301. void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
  302. {
  303. if (instance->engine->extended_prop_set) {
  304. instance->engine->extended_prop_set(instance, property, value);
  305. }
  306. }
  307. void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
  308. {
  309. if (instance->engine->extended_prop_get) {
  310. return instance->engine->extended_prop_get(instance, property);
  311. }
  312. return NULL;
  313. }
  314. void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
  315. {
  316. instance->properties[property] = value;
  317. if (instance->engine->prop_set) {
  318. instance->engine->prop_set(instance, property, value);
  319. }
  320. }
  321. int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
  322. {
  323. return instance->properties[property];
  324. }
  325. struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
  326. {
  327. return &instance->codecs;
  328. }
  329. void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
  330. {
  331. int i;
  332. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  333. codecs->payloads[i].asterisk_format = 0;
  334. codecs->payloads[i].rtp_code = 0;
  335. ast_format_clear(&codecs->payloads[i].format);
  336. if (instance && instance->engine && instance->engine->payload_set) {
  337. instance->engine->payload_set(instance, i, 0, NULL, 0);
  338. }
  339. }
  340. }
  341. void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
  342. {
  343. int i;
  344. ast_rwlock_rdlock(&static_RTP_PT_lock);
  345. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  346. if (static_RTP_PT[i].rtp_code || static_RTP_PT[i].asterisk_format) {
  347. codecs->payloads[i].asterisk_format = static_RTP_PT[i].asterisk_format;
  348. codecs->payloads[i].rtp_code = static_RTP_PT[i].rtp_code;
  349. ast_format_copy(&codecs->payloads[i].format, &static_RTP_PT[i].format);
  350. if (instance && instance->engine && instance->engine->payload_set) {
  351. instance->engine->payload_set(instance, i, codecs->payloads[i].asterisk_format, &codecs->payloads[i].format, codecs->payloads[i].rtp_code);
  352. }
  353. }
  354. }
  355. ast_rwlock_unlock(&static_RTP_PT_lock);
  356. }
  357. void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
  358. {
  359. int i;
  360. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  361. if (src->payloads[i].rtp_code || src->payloads[i].asterisk_format) {
  362. ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
  363. dest->payloads[i].asterisk_format = src->payloads[i].asterisk_format;
  364. dest->payloads[i].rtp_code = src->payloads[i].rtp_code;
  365. ast_format_copy(&dest->payloads[i].format, &src->payloads[i].format);
  366. if (instance && instance->engine && instance->engine->payload_set) {
  367. instance->engine->payload_set(instance, i, dest->payloads[i].asterisk_format, &dest->payloads[i].format, dest->payloads[i].rtp_code);
  368. }
  369. }
  370. }
  371. }
  372. void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
  373. {
  374. ast_rwlock_rdlock(&static_RTP_PT_lock);
  375. if (payload < 0 || payload >= AST_RTP_MAX_PT || (!static_RTP_PT[payload].rtp_code && !static_RTP_PT[payload].asterisk_format)) {
  376. ast_rwlock_unlock(&static_RTP_PT_lock);
  377. return;
  378. }
  379. codecs->payloads[payload].asterisk_format = static_RTP_PT[payload].asterisk_format;
  380. codecs->payloads[payload].rtp_code = static_RTP_PT[payload].rtp_code;
  381. ast_format_copy(&codecs->payloads[payload].format, &static_RTP_PT[payload].format);
  382. ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
  383. if (instance && instance->engine && instance->engine->payload_set) {
  384. instance->engine->payload_set(instance, payload, codecs->payloads[payload].asterisk_format, &codecs->payloads[payload].format, codecs->payloads[payload].rtp_code);
  385. }
  386. ast_rwlock_unlock(&static_RTP_PT_lock);
  387. }
  388. int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
  389. char *mimetype, char *mimesubtype,
  390. enum ast_rtp_options options,
  391. unsigned int sample_rate)
  392. {
  393. unsigned int i;
  394. int found = 0;
  395. if (pt < 0 || pt >= AST_RTP_MAX_PT)
  396. return -1; /* bogus payload type */
  397. ast_rwlock_rdlock(&mime_types_lock);
  398. for (i = 0; i < mime_types_len; ++i) {
  399. const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
  400. if (strcasecmp(mimesubtype, t->subtype)) {
  401. continue;
  402. }
  403. if (strcasecmp(mimetype, t->type)) {
  404. continue;
  405. }
  406. /* if both sample rates have been supplied, and they don't match,
  407. * then this not a match; if one has not been supplied, then the
  408. * rates are not compared */
  409. if (sample_rate && t->sample_rate &&
  410. (sample_rate != t->sample_rate)) {
  411. continue;
  412. }
  413. found = 1;
  414. codecs->payloads[pt] = t->payload_type;
  415. if ((t->payload_type.format.id == AST_FORMAT_G726) && t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
  416. ast_format_set(&codecs->payloads[pt].format, AST_FORMAT_G726_AAL2, 0);
  417. }
  418. if (instance && instance->engine && instance->engine->payload_set) {
  419. instance->engine->payload_set(instance, pt, codecs->payloads[i].asterisk_format, &codecs->payloads[i].format, codecs->payloads[i].rtp_code);
  420. }
  421. break;
  422. }
  423. ast_rwlock_unlock(&mime_types_lock);
  424. return (found ? 0 : -2);
  425. }
  426. 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)
  427. {
  428. return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
  429. }
  430. void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
  431. {
  432. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  433. return;
  434. }
  435. ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
  436. codecs->payloads[payload].asterisk_format = 0;
  437. codecs->payloads[payload].rtp_code = 0;
  438. ast_format_clear(&codecs->payloads[payload].format);
  439. if (instance && instance->engine && instance->engine->payload_set) {
  440. instance->engine->payload_set(instance, payload, 0, NULL, 0);
  441. }
  442. }
  443. struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
  444. {
  445. struct ast_rtp_payload_type result = { .asterisk_format = 0, };
  446. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  447. return result;
  448. }
  449. result.asterisk_format = codecs->payloads[payload].asterisk_format;
  450. result.rtp_code = codecs->payloads[payload].rtp_code;
  451. ast_format_copy(&result.format, &codecs->payloads[payload].format);
  452. if (!result.rtp_code && !result.asterisk_format) {
  453. ast_rwlock_rdlock(&static_RTP_PT_lock);
  454. result = static_RTP_PT[payload];
  455. ast_rwlock_unlock(&static_RTP_PT_lock);
  456. }
  457. return result;
  458. }
  459. struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
  460. {
  461. if (payload < 0 || payload >= AST_RTP_MAX_PT) {
  462. return NULL;
  463. }
  464. if (!codecs->payloads[payload].asterisk_format) {
  465. return NULL;
  466. }
  467. return &codecs->payloads[payload].format;
  468. }
  469. void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
  470. {
  471. int i;
  472. ast_format_cap_remove_all(astformats);
  473. *nonastformats = 0;
  474. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  475. if (codecs->payloads[i].rtp_code || codecs->payloads[i].asterisk_format) {
  476. ast_debug(1, "Incorporating payload %d on %p\n", i, codecs);
  477. }
  478. if (codecs->payloads[i].asterisk_format) {
  479. ast_format_cap_add(astformats, &codecs->payloads[i].format);
  480. } else {
  481. *nonastformats |= codecs->payloads[i].rtp_code;
  482. }
  483. }
  484. }
  485. int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
  486. {
  487. int i;
  488. int res = -1;
  489. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  490. if (codecs->payloads[i].asterisk_format && asterisk_format && format &&
  491. (ast_format_cmp(format, &codecs->payloads[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  492. return i;
  493. } else if (!codecs->payloads[i].asterisk_format && !asterisk_format &&
  494. (codecs->payloads[i].rtp_code == code)) {
  495. return i;
  496. }
  497. }
  498. ast_rwlock_rdlock(&static_RTP_PT_lock);
  499. for (i = 0; i < AST_RTP_MAX_PT; i++) {
  500. if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
  501. (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  502. res = i;
  503. break;
  504. } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
  505. (static_RTP_PT[i].rtp_code == code)) {
  506. res = i;
  507. break;
  508. }
  509. }
  510. ast_rwlock_unlock(&static_RTP_PT_lock);
  511. return res;
  512. }
  513. const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
  514. {
  515. int i;
  516. const char *res = "";
  517. ast_rwlock_rdlock(&mime_types_lock);
  518. for (i = 0; i < mime_types_len; i++) {
  519. if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
  520. (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  521. if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
  522. res = "G726-32";
  523. break;
  524. } else {
  525. res = ast_rtp_mime_types[i].subtype;
  526. break;
  527. }
  528. } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
  529. ast_rtp_mime_types[i].payload_type.rtp_code == code) {
  530. res = ast_rtp_mime_types[i].subtype;
  531. break;
  532. }
  533. }
  534. ast_rwlock_unlock(&mime_types_lock);
  535. return res;
  536. }
  537. unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
  538. {
  539. unsigned int i;
  540. unsigned int res = 0;
  541. ast_rwlock_rdlock(&mime_types_lock);
  542. for (i = 0; i < mime_types_len; ++i) {
  543. if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
  544. (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
  545. res = ast_rtp_mime_types[i].sample_rate;
  546. break;
  547. } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
  548. ast_rtp_mime_types[i].payload_type.rtp_code == code) {
  549. res = ast_rtp_mime_types[i].sample_rate;
  550. break;
  551. }
  552. }
  553. ast_rwlock_unlock(&mime_types_lock);
  554. return res;
  555. }
  556. 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)
  557. {
  558. int found = 0;
  559. const char *name;
  560. if (!buf) {
  561. return NULL;
  562. }
  563. if (asterisk_format) {
  564. struct ast_format tmp_fmt;
  565. ast_format_cap_iter_start(ast_format_capability);
  566. while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
  567. name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
  568. ast_str_append(&buf, 0, "%s|", name);
  569. found = 1;
  570. }
  571. ast_format_cap_iter_end(ast_format_capability);
  572. } else {
  573. int x;
  574. ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
  575. for (x = 1; x < AST_RTP_MAX; x <<= 1) {
  576. if (rtp_capability & x) {
  577. name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
  578. ast_str_append(&buf, 0, "%s|", name);
  579. found = 1;
  580. }
  581. }
  582. }
  583. ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
  584. return ast_str_buffer(buf);
  585. }
  586. void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
  587. {
  588. codecs->pref = *prefs;
  589. if (instance && instance->engine->packetization_set) {
  590. instance->engine->packetization_set(instance, &instance->codecs.pref);
  591. }
  592. }
  593. int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
  594. {
  595. return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
  596. }
  597. int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
  598. {
  599. return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
  600. }
  601. int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
  602. {
  603. return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
  604. }
  605. int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
  606. {
  607. return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
  608. }
  609. enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
  610. {
  611. return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
  612. }
  613. void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
  614. {
  615. if (instance->engine->update_source) {
  616. instance->engine->update_source(instance);
  617. }
  618. }
  619. void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
  620. {
  621. if (instance->engine->change_source) {
  622. instance->engine->change_source(instance);
  623. }
  624. }
  625. int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
  626. {
  627. return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
  628. }
  629. void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
  630. {
  631. if (instance->engine->stop) {
  632. instance->engine->stop(instance);
  633. }
  634. }
  635. int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
  636. {
  637. return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
  638. }
  639. struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
  640. {
  641. struct ast_rtp_glue *glue = NULL;
  642. AST_RWLIST_RDLOCK(&glues);
  643. AST_RWLIST_TRAVERSE(&glues, glue, entry) {
  644. if (!strcasecmp(glue->type, type)) {
  645. break;
  646. }
  647. }
  648. AST_RWLIST_UNLOCK(&glues);
  649. return glue;
  650. }
  651. 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)
  652. {
  653. enum ast_bridge_result res = AST_BRIDGE_FAILED;
  654. struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
  655. struct ast_frame *fr = NULL;
  656. struct timeval start;
  657. /* Start locally bridging both instances */
  658. if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
  659. ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c0->name, c1->name);
  660. ast_channel_unlock(c0);
  661. ast_channel_unlock(c1);
  662. return AST_BRIDGE_FAILED_NOWARN;
  663. }
  664. if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
  665. ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c1->name, c0->name);
  666. if (instance0->engine->local_bridge) {
  667. instance0->engine->local_bridge(instance0, NULL);
  668. }
  669. ast_channel_unlock(c0);
  670. ast_channel_unlock(c1);
  671. return AST_BRIDGE_FAILED_NOWARN;
  672. }
  673. ast_channel_unlock(c0);
  674. ast_channel_unlock(c1);
  675. instance0->bridged = instance1;
  676. instance1->bridged = instance0;
  677. ast_poll_channel_add(c0, c1);
  678. /* Hop into a loop waiting for a frame from either channel */
  679. cs[0] = c0;
  680. cs[1] = c1;
  681. cs[2] = NULL;
  682. start = ast_tvnow();
  683. for (;;) {
  684. int ms;
  685. /* If the underlying formats have changed force this bridge to break */
  686. if ((ast_format_cmp(&c0->rawreadformat, &c1->rawwriteformat) == AST_FORMAT_CMP_NOT_EQUAL) ||
  687. (ast_format_cmp(&c1->rawreadformat, &c0->rawwriteformat) == AST_FORMAT_CMP_NOT_EQUAL)) {
  688. ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
  689. res = AST_BRIDGE_FAILED_NOWARN;
  690. break;
  691. }
  692. /* Check if anything changed */
  693. if ((c0->tech_pvt != pvt0) ||
  694. (c1->tech_pvt != pvt1) ||
  695. (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
  696. (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks) ||
  697. (!ast_framehook_list_is_empty(c0->framehooks) || !ast_framehook_list_is_empty(c1->framehooks))) {
  698. ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
  699. /* 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 */
  700. if ((c0->masq || c0->masqr) && (fr = ast_read(c0))) {
  701. ast_frfree(fr);
  702. }
  703. if ((c1->masq || c1->masqr) && (fr = ast_read(c1))) {
  704. ast_frfree(fr);
  705. }
  706. res = AST_BRIDGE_RETRY;
  707. break;
  708. }
  709. /* Wait on a channel to feed us a frame */
  710. ms = ast_remaining_ms(start, timeoutms);
  711. if (!(who = ast_waitfor_n(cs, 2, &ms))) {
  712. if (!ms) {
  713. res = AST_BRIDGE_RETRY;
  714. break;
  715. }
  716. ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
  717. if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
  718. break;
  719. }
  720. continue;
  721. }
  722. /* Read in frame from channel */
  723. fr = ast_read(who);
  724. other = (who == c0) ? c1 : c0;
  725. /* Depending on the frame we may need to break out of our bridge */
  726. if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
  727. ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
  728. ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
  729. /* Record received frame and who */
  730. *fo = fr;
  731. *rc = who;
  732. ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
  733. res = AST_BRIDGE_COMPLETE;
  734. break;
  735. } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
  736. if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
  737. (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
  738. (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
  739. (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
  740. (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
  741. (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
  742. /* If we are going on hold, then break callback mode and P2P bridging */
  743. if (fr->subclass.integer == AST_CONTROL_HOLD) {
  744. if (instance0->engine->local_bridge) {
  745. instance0->engine->local_bridge(instance0, NULL);
  746. }
  747. if (instance1->engine->local_bridge) {
  748. instance1->engine->local_bridge(instance1, NULL);
  749. }
  750. instance0->bridged = NULL;
  751. instance1->bridged = NULL;
  752. } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
  753. if (instance0->engine->local_bridge) {
  754. instance0->engine->local_bridge(instance0, instance1);
  755. }
  756. if (instance1->engine->local_bridge) {
  757. instance1->engine->local_bridge(instance1, instance0);
  758. }
  759. instance0->bridged = instance1;
  760. instance1->bridged = instance0;
  761. }
  762. /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
  763. if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
  764. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  765. }
  766. ast_frfree(fr);
  767. } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
  768. if (ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
  769. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  770. }
  771. ast_frfree(fr);
  772. } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
  773. if (ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
  774. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  775. }
  776. ast_frfree(fr);
  777. } else {
  778. *fo = fr;
  779. *rc = who;
  780. ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, who->name);
  781. res = AST_BRIDGE_COMPLETE;
  782. break;
  783. }
  784. } else {
  785. if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
  786. (fr->frametype == AST_FRAME_DTMF_END) ||
  787. (fr->frametype == AST_FRAME_VOICE) ||
  788. (fr->frametype == AST_FRAME_VIDEO) ||
  789. (fr->frametype == AST_FRAME_IMAGE) ||
  790. (fr->frametype == AST_FRAME_HTML) ||
  791. (fr->frametype == AST_FRAME_MODEM) ||
  792. (fr->frametype == AST_FRAME_TEXT)) {
  793. ast_write(other, fr);
  794. }
  795. ast_frfree(fr);
  796. }
  797. /* Swap priority */
  798. cs[2] = cs[0];
  799. cs[0] = cs[1];
  800. cs[1] = cs[2];
  801. }
  802. /* Stop locally bridging both instances */
  803. if (instance0->engine->local_bridge) {
  804. instance0->engine->local_bridge(instance0, NULL);
  805. }
  806. if (instance1->engine->local_bridge) {
  807. instance1->engine->local_bridge(instance1, NULL);
  808. }
  809. instance0->bridged = NULL;
  810. instance1->bridged = NULL;
  811. ast_poll_channel_del(c0, c1);
  812. return res;
  813. }
  814. static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0,
  815. struct ast_channel *c1,
  816. struct ast_rtp_instance *instance0,
  817. struct ast_rtp_instance *instance1,
  818. struct ast_rtp_instance *vinstance0,
  819. struct ast_rtp_instance *vinstance1,
  820. struct ast_rtp_instance *tinstance0,
  821. struct ast_rtp_instance *tinstance1,
  822. struct ast_rtp_glue *glue0,
  823. struct ast_rtp_glue *glue1,
  824. struct ast_format_cap *cap0,
  825. struct ast_format_cap *cap1,
  826. int timeoutms,
  827. int flags,
  828. struct ast_frame **fo,
  829. struct ast_channel **rc,
  830. void *pvt0,
  831. void *pvt1)
  832. {
  833. enum ast_bridge_result res = AST_BRIDGE_FAILED;
  834. struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
  835. struct ast_format_cap *oldcap0 = ast_format_cap_dup(cap0);
  836. struct ast_format_cap *oldcap1 = ast_format_cap_dup(cap1);
  837. struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
  838. struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
  839. struct ast_frame *fr = NULL;
  840. struct timeval start;
  841. if (!oldcap0 || !oldcap1) {
  842. ast_channel_unlock(c0);
  843. ast_channel_unlock(c1);
  844. goto remote_bridge_cleanup;
  845. }
  846. /* Test the first channel */
  847. if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0))) {
  848. ast_rtp_instance_get_remote_address(instance1, &ac1);
  849. if (vinstance1) {
  850. ast_rtp_instance_get_remote_address(vinstance1, &vac1);
  851. }
  852. if (tinstance1) {
  853. ast_rtp_instance_get_remote_address(tinstance1, &tac1);
  854. }
  855. } else {
  856. ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
  857. }
  858. /* Test the second channel */
  859. if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0))) {
  860. ast_rtp_instance_get_remote_address(instance0, &ac0);
  861. if (vinstance0) {
  862. ast_rtp_instance_get_remote_address(instance0, &vac0);
  863. }
  864. if (tinstance0) {
  865. ast_rtp_instance_get_remote_address(instance0, &tac0);
  866. }
  867. } else {
  868. ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
  869. }
  870. ast_channel_unlock(c0);
  871. ast_channel_unlock(c1);
  872. instance0->bridged = instance1;
  873. instance1->bridged = instance0;
  874. ast_poll_channel_add(c0, c1);
  875. /* Go into a loop handling any stray frames that may come in */
  876. cs[0] = c0;
  877. cs[1] = c1;
  878. cs[2] = NULL;
  879. start = ast_tvnow();
  880. for (;;) {
  881. int ms;
  882. /* Check if anything changed */
  883. if ((c0->tech_pvt != pvt0) ||
  884. (c1->tech_pvt != pvt1) ||
  885. (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
  886. (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks) ||
  887. (!ast_framehook_list_is_empty(c0->framehooks) || !ast_framehook_list_is_empty(c1->framehooks))) {
  888. ast_debug(1, "Oooh, something is weird, backing out\n");
  889. res = AST_BRIDGE_RETRY;
  890. break;
  891. }
  892. /* Check if they have changed their address */
  893. ast_rtp_instance_get_remote_address(instance1, &t1);
  894. if (vinstance1) {
  895. ast_rtp_instance_get_remote_address(vinstance1, &vt1);
  896. }
  897. if (tinstance1) {
  898. ast_rtp_instance_get_remote_address(tinstance1, &tt1);
  899. }
  900. if (glue1->get_codec) {
  901. ast_format_cap_remove_all(cap1);
  902. glue1->get_codec(c1, cap1);
  903. }
  904. ast_rtp_instance_get_remote_address(instance0, &t0);
  905. if (vinstance0) {
  906. ast_rtp_instance_get_remote_address(vinstance0, &vt0);
  907. }
  908. if (tinstance0) {
  909. ast_rtp_instance_get_remote_address(tinstance0, &tt0);
  910. }
  911. if (glue0->get_codec) {
  912. ast_format_cap_remove_all(cap0);
  913. glue0->get_codec(c0, cap0);
  914. }
  915. if ((ast_sockaddr_cmp(&t1, &ac1)) ||
  916. (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
  917. (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
  918. (!ast_format_cap_identical(cap1, oldcap1))) {
  919. char tmp_buf[512] = { 0, };
  920. ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
  921. c1->name, ast_sockaddr_stringify(&t1),
  922. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
  923. ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
  924. c1->name, ast_sockaddr_stringify(&vt1),
  925. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
  926. ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
  927. c1->name, ast_sockaddr_stringify(&tt1),
  928. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
  929. ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
  930. c1->name, ast_sockaddr_stringify(&ac1),
  931. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
  932. ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
  933. c1->name, ast_sockaddr_stringify(&vac1),
  934. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
  935. ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
  936. c1->name, ast_sockaddr_stringify(&tac1),
  937. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
  938. if (glue0->update_peer(c0,
  939. ast_sockaddr_isnull(&t1) ? NULL : instance1,
  940. ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
  941. ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
  942. cap1, 0)) {
  943. ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
  944. }
  945. ast_sockaddr_copy(&ac1, &t1);
  946. ast_sockaddr_copy(&vac1, &vt1);
  947. ast_sockaddr_copy(&tac1, &tt1);
  948. ast_format_cap_copy(oldcap1, cap1);
  949. }
  950. if ((ast_sockaddr_cmp(&t0, &ac0)) ||
  951. (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
  952. (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
  953. (!ast_format_cap_identical(cap0, oldcap0))) {
  954. char tmp_buf[512] = { 0, };
  955. ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
  956. c0->name, ast_sockaddr_stringify(&t0),
  957. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap0));
  958. ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
  959. c0->name, ast_sockaddr_stringify(&ac0),
  960. ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap0));
  961. if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
  962. vt0.len ? vinstance0 : NULL,
  963. tt0.len ? tinstance0 : NULL,
  964. cap0, 0)) {
  965. ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
  966. }
  967. ast_sockaddr_copy(&ac0, &t0);
  968. ast_sockaddr_copy(&vac0, &vt0);
  969. ast_sockaddr_copy(&tac0, &tt0);
  970. ast_format_cap_copy(oldcap0, cap0);
  971. }
  972. ms = ast_remaining_ms(start, timeoutms);
  973. /* Wait for frame to come in on the channels */
  974. if (!(who = ast_waitfor_n(cs, 2, &ms))) {
  975. if (!ms) {
  976. res = AST_BRIDGE_RETRY;
  977. break;
  978. }
  979. ast_debug(1, "Ooh, empty read...\n");
  980. if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
  981. break;
  982. }
  983. continue;
  984. }
  985. fr = ast_read(who);
  986. other = (who == c0) ? c1 : c0;
  987. if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
  988. (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
  989. ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
  990. /* Break out of bridge */
  991. *fo = fr;
  992. *rc = who;
  993. ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
  994. res = AST_BRIDGE_COMPLETE;
  995. break;
  996. } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
  997. if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
  998. (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
  999. (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
  1000. (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
  1001. (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
  1002. (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
  1003. if (fr->subclass.integer == AST_CONTROL_HOLD) {
  1004. /* If we someone went on hold we want the other side to reinvite back to us */
  1005. if (who == c0) {
  1006. glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
  1007. } else {
  1008. glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
  1009. }
  1010. } else if (fr->subclass.integer == AST_CONTROL_UNHOLD ||
  1011. fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER) {
  1012. /* If they went off hold they should go back to being direct, or if we have
  1013. * been told to force a peer update, go ahead and do it. */
  1014. if (who == c0) {
  1015. glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
  1016. } else {
  1017. glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
  1018. }
  1019. }
  1020. /* Update local address information */
  1021. ast_rtp_instance_get_remote_address(instance0, &t0);
  1022. ast_sockaddr_copy(&ac0, &t0);
  1023. ast_rtp_instance_get_remote_address(instance1, &t1);
  1024. ast_sockaddr_copy(&ac1, &t1);
  1025. /* Update codec information */
  1026. if (glue0->get_codec && c0->tech_pvt) {
  1027. ast_format_cap_remove_all(cap0);
  1028. ast_format_cap_remove_all(oldcap0);
  1029. glue0->get_codec(c0, cap0);
  1030. ast_format_cap_append(oldcap0, cap0);
  1031. }
  1032. if (glue1->get_codec && c1->tech_pvt) {
  1033. ast_format_cap_remove_all(cap1);
  1034. ast_format_cap_remove_all(oldcap1);
  1035. glue1->get_codec(c1, cap1);
  1036. ast_format_cap_append(oldcap1, cap1);
  1037. }
  1038. /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
  1039. if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
  1040. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  1041. }
  1042. ast_frfree(fr);
  1043. } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
  1044. if (ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
  1045. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  1046. }
  1047. ast_frfree(fr);
  1048. } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
  1049. if (ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
  1050. ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
  1051. }
  1052. ast_frfree(fr);
  1053. } else {
  1054. *fo = fr;
  1055. *rc = who;
  1056. ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, who->name);
  1057. res = AST_BRIDGE_COMPLETE;
  1058. goto remote_bridge_cleanup;
  1059. }
  1060. } else {
  1061. if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
  1062. (fr->frametype == AST_FRAME_DTMF_END) ||
  1063. (fr->frametype == AST_FRAME_VOICE) ||
  1064. (fr->frametype == AST_FRAME_VIDEO) ||
  1065. (fr->frametype == AST_FRAME_IMAGE) ||
  1066. (fr->frametype == AST_FRAME_HTML) ||
  1067. (fr->frametype == AST_FRAME_MODEM) ||
  1068. (fr->frametype == AST_FRAME_TEXT)) {
  1069. ast_write(other, fr);
  1070. }
  1071. ast_frfree(fr);
  1072. }
  1073. /* Swap priority */
  1074. cs[2] = cs[0];
  1075. cs[0] = cs[1];
  1076. cs[1] = cs[2];
  1077. }
  1078. if (ast_test_flag(c0, AST_FLAG_ZOMBIE)) {
  1079. ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", c0->name);
  1080. } else if (c0->tech_pvt != pvt0) {
  1081. ast_debug(1, "Channel c0->'%s' pvt changed, in bridge with c1->'%s'\n", c0->name, c1->name);
  1082. } else if (glue0 != ast_rtp_instance_get_glue(c0->tech->type)) {
  1083. ast_debug(1, "Channel c0->'%s' technology changed, in bridge with c1->'%s'\n", c0->name, c1->name);
  1084. } else if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
  1085. ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
  1086. }
  1087. if (ast_test_flag(c1, AST_FLAG_ZOMBIE)) {
  1088. ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", c1->name);
  1089. } else if (c1->tech_pvt != pvt1) {
  1090. ast_debug(1, "Channel c1->'%s' pvt changed, in bridge with c0->'%s'\n", c1->name, c0->name);
  1091. } else if (glue1 != ast_rtp_instance_get_glue(c1->tech->type)) {
  1092. ast_debug(1, "Channel c1->'%s' technology changed, in bridge with c0->'%s'\n", c1->name, c0->name);
  1093. } else if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
  1094. ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
  1095. }
  1096. instance0->bridged = NULL;
  1097. instance1->bridged = NULL;
  1098. ast_poll_channel_del(c0, c1);
  1099. remote_bridge_cleanup:
  1100. ast_format_cap_destroy(oldcap0);
  1101. ast_format_cap_destroy(oldcap1);
  1102. return res;
  1103. }
  1104. /*!
  1105. * \brief Conditionally unref an rtp instance
  1106. */
  1107. static void unref_instance_cond(struct ast_rtp_instance **instance)
  1108. {
  1109. if (*instance) {
  1110. ao2_ref(*instance, -1);
  1111. *instance = NULL;
  1112. }
  1113. }
  1114. 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)
  1115. {
  1116. struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
  1117. *vinstance0 = NULL, *vinstance1 = NULL,
  1118. *tinstance0 = NULL, *tinstance1 = NULL;
  1119. struct ast_rtp_glue *glue0, *glue1;
  1120. struct ast_sockaddr addr1 = { {0, }, }, addr2 = { {0, }, };
  1121. enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1122. enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1123. enum ast_bridge_result res = AST_BRIDGE_FAILED;
  1124. enum ast_rtp_dtmf_mode dmode;
  1125. struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
  1126. struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
  1127. int unlock_chans = 1;
  1128. if (!cap0 || !cap1) {
  1129. unlock_chans = 0;
  1130. goto done;
  1131. }
  1132. /* Lock both channels so we can look for the glue that binds them together */
  1133. ast_channel_lock(c0);
  1134. while (ast_channel_trylock(c1)) {
  1135. ast_channel_unlock(c0);
  1136. usleep(1);
  1137. ast_channel_lock(c0);
  1138. }
  1139. /* Ensure neither channel got hungup during lock avoidance */
  1140. if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
  1141. ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
  1142. goto done;
  1143. }
  1144. /* Grab glue that binds each channel to something using the RTP engine */
  1145. if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
  1146. ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
  1147. goto done;
  1148. }
  1149. audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
  1150. video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
  1151. audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
  1152. video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
  1153. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  1154. 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)) {
  1155. audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1156. }
  1157. 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)) {
  1158. audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1159. }
  1160. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  1161. if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
  1162. res = AST_BRIDGE_FAILED_NOWARN;
  1163. goto done;
  1164. }
  1165. /* If address families differ, force a local bridge */
  1166. ast_rtp_instance_get_remote_address(instance0, &addr1);
  1167. ast_rtp_instance_get_remote_address(instance1, &addr2);
  1168. if (addr1.ss.ss_family != addr2.ss.ss_family ||
  1169. (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
  1170. audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
  1171. audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
  1172. }
  1173. /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
  1174. dmode = ast_rtp_instance_dtmf_mode_get(instance0);
  1175. if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && dmode) {
  1176. res = AST_BRIDGE_FAILED_NOWARN;
  1177. goto done;
  1178. }
  1179. dmode = ast_rtp_instance_dtmf_mode_get(instance1);
  1180. if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && dmode) {
  1181. res = AST_BRIDGE_FAILED_NOWARN;
  1182. goto done;
  1183. }
  1184. /* 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 */
  1185. 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)))) {
  1186. res = AST_BRIDGE_FAILED_NOWARN;
  1187. goto done;
  1188. }
  1189. /* Make sure that codecs match */
  1190. if (glue0->get_codec){
  1191. glue0->get_codec(c0, cap0);
  1192. }
  1193. if (glue1->get_codec) {
  1194. glue1->get_codec(c1, cap1);
  1195. }
  1196. if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
  1197. char tmp0[256] = { 0, };
  1198. char tmp1[256] = { 0, };
  1199. ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
  1200. ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
  1201. ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
  1202. res = AST_BRIDGE_FAILED_NOWARN;
  1203. goto done;
  1204. }
  1205. instance0->glue = glue0;
  1206. instance1->glue = glue1;
  1207. instance0->chan = c0;
  1208. instance1->chan = c1;
  1209. /* Depending on the end result for bridging either do a local bridge or remote bridge */
  1210. if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
  1211. ast_verb(3, "Locally bridging %s and %s\n", c0->name, c1->name);
  1212. res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, c0->tech_pvt, c1->tech_pvt);
  1213. } else {
  1214. ast_verb(3, "Remotely bridging %s and %s\n", c0->name, c1->name);
  1215. res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
  1216. tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
  1217. fo, rc, c0->tech_pvt, c1->tech_pvt);
  1218. }
  1219. instance0->glue = NULL;
  1220. instance1->glue = NULL;
  1221. instance0->chan = NULL;
  1222. instance1->chan = NULL;
  1223. unlock_chans = 0;
  1224. done:
  1225. if (unlock_chans) {
  1226. ast_channel_unlock(c0);
  1227. ast_channel_unlock(c1);
  1228. }
  1229. ast_format_cap_destroy(cap1);
  1230. ast_format_cap_destroy(cap0);
  1231. unref_instance_cond(&instance0);
  1232. unref_instance_cond(&instance1);
  1233. unref_instance_cond(&vinstance0);
  1234. unref_instance_cond(&vinstance1);
  1235. unref_instance_cond(&tinstance0);
  1236. unref_instance_cond(&tinstance1);
  1237. return res;
  1238. }
  1239. struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
  1240. {
  1241. return instance->bridged;
  1242. }
  1243. void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
  1244. {
  1245. struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
  1246. *vinstance0 = NULL, *vinstance1 = NULL,
  1247. *tinstance0 = NULL, *tinstance1 = NULL;
  1248. struct ast_rtp_glue *glue0, *glue1;
  1249. enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1250. enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1251. struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
  1252. struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
  1253. int res = 0;
  1254. /* Lock both channels so we can look for the glue that binds them together */
  1255. ast_channel_lock(c0);
  1256. while (ast_channel_trylock(c1)) {
  1257. ast_channel_unlock(c0);
  1258. usleep(1);
  1259. ast_channel_lock(c0);
  1260. }
  1261. if (!cap1 || !cap0) {
  1262. goto done;
  1263. }
  1264. /* Grab glue that binds each channel to something using the RTP engine */
  1265. if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
  1266. ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
  1267. goto done;
  1268. }
  1269. audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
  1270. video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
  1271. audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
  1272. video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
  1273. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  1274. 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)) {
  1275. audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1276. }
  1277. 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)) {
  1278. audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1279. }
  1280. 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) {
  1281. glue0->get_codec(c0, cap0);
  1282. }
  1283. 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) {
  1284. glue1->get_codec(c1, cap1);
  1285. }
  1286. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  1287. if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
  1288. goto done;
  1289. }
  1290. /* Make sure we have matching codecs */
  1291. if (!ast_format_cap_has_joint(cap0, cap1)) {
  1292. goto done;
  1293. }
  1294. ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
  1295. if (vinstance0 && vinstance1) {
  1296. ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
  1297. }
  1298. if (tinstance0 && tinstance1) {
  1299. ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
  1300. }
  1301. if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
  1302. ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
  1303. }
  1304. res = 0;
  1305. done:
  1306. ast_channel_unlock(c0);
  1307. ast_channel_unlock(c1);
  1308. ast_format_cap_destroy(cap0);
  1309. ast_format_cap_destroy(cap1);
  1310. unref_instance_cond(&instance0);
  1311. unref_instance_cond(&instance1);
  1312. unref_instance_cond(&vinstance0);
  1313. unref_instance_cond(&vinstance1);
  1314. unref_instance_cond(&tinstance0);
  1315. unref_instance_cond(&tinstance1);
  1316. if (!res) {
  1317. ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
  1318. }
  1319. }
  1320. int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
  1321. {
  1322. struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
  1323. *vinstance0 = NULL, *vinstance1 = NULL,
  1324. *tinstance0 = NULL, *tinstance1 = NULL;
  1325. struct ast_rtp_glue *glue0, *glue1;
  1326. enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1327. enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1328. struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
  1329. struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
  1330. int res = 0;
  1331. /* If there is no second channel just immediately bail out, we are of no use in that scenario */
  1332. if (!c1) {
  1333. ast_format_cap_destroy(cap0);
  1334. ast_format_cap_destroy(cap1);
  1335. return -1;
  1336. }
  1337. /* Lock both channels so we can look for the glue that binds them together */
  1338. ast_channel_lock(c0);
  1339. while (ast_channel_trylock(c1)) {
  1340. ast_channel_unlock(c0);
  1341. usleep(1);
  1342. ast_channel_lock(c0);
  1343. }
  1344. if (!cap1 || !cap0) {
  1345. goto done;
  1346. }
  1347. /* Grab glue that binds each channel to something using the RTP engine */
  1348. if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
  1349. ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
  1350. goto done;
  1351. }
  1352. audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
  1353. video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
  1354. audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
  1355. video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
  1356. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  1357. 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)) {
  1358. audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
  1359. }
  1360. 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)) {
  1361. audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
  1362. }
  1363. 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) {
  1364. glue0->get_codec(c0, cap0);
  1365. }
  1366. 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) {
  1367. glue1->get_codec(c1, cap1);
  1368. }
  1369. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  1370. if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
  1371. goto done;
  1372. }
  1373. /* Make sure we have matching codecs */
  1374. if (!ast_format_cap_has_joint(cap0, cap1)) {
  1375. goto done;
  1376. }
  1377. /* Bridge media early */
  1378. if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
  1379. ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
  1380. }
  1381. res = 0;
  1382. done:
  1383. ast_channel_unlock(c0);
  1384. ast_channel_unlock(c1);
  1385. ast_format_cap_destroy(cap0);
  1386. ast_format_cap_destroy(cap1);
  1387. unref_instance_cond(&instance0);
  1388. unref_instance_cond(&instance1);
  1389. unref_instance_cond(&vinstance0);
  1390. unref_instance_cond(&vinstance1);
  1391. unref_instance_cond(&tinstance0);
  1392. unref_instance_cond(&tinstance1);
  1393. if (!res) {
  1394. ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
  1395. }
  1396. return res;
  1397. }
  1398. int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
  1399. {
  1400. return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
  1401. }
  1402. int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
  1403. {
  1404. return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
  1405. }
  1406. int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
  1407. {
  1408. return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
  1409. }
  1410. char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
  1411. {
  1412. struct ast_rtp_instance_stats stats = { 0, };
  1413. enum ast_rtp_instance_stat stat;
  1414. /* Determine what statistics we will need to retrieve based on field passed in */
  1415. if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
  1416. stat = AST_RTP_INSTANCE_STAT_ALL;
  1417. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
  1418. stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
  1419. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
  1420. stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
  1421. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
  1422. stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
  1423. } else {
  1424. return NULL;
  1425. }
  1426. /* Attempt to actually retrieve the statistics we need to generate the quality string */
  1427. if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
  1428. return NULL;
  1429. }
  1430. /* Now actually fill the buffer with the good information */
  1431. if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
  1432. snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
  1433. stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
  1434. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
  1435. snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
  1436. 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));
  1437. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
  1438. snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
  1439. 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));
  1440. } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
  1441. snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
  1442. }
  1443. return buf;
  1444. }
  1445. void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
  1446. {
  1447. char quality_buf[AST_MAX_USER_FIELD], *quality;
  1448. struct ast_channel *bridge = ast_bridged_channel(chan);
  1449. if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
  1450. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
  1451. if (bridge) {
  1452. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
  1453. }
  1454. }
  1455. if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
  1456. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
  1457. if (bridge) {
  1458. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
  1459. }
  1460. }
  1461. if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
  1462. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
  1463. if (bridge) {
  1464. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
  1465. }
  1466. }
  1467. if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
  1468. pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
  1469. if (bridge) {
  1470. pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
  1471. }
  1472. }
  1473. }
  1474. int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
  1475. {
  1476. return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
  1477. }
  1478. int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
  1479. {
  1480. return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
  1481. }
  1482. int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
  1483. {
  1484. struct ast_rtp_glue *glue;
  1485. struct ast_rtp_instance *peer_instance = NULL;
  1486. int res = -1;
  1487. if (!instance->engine->make_compatible) {
  1488. return -1;
  1489. }
  1490. ast_channel_lock(peer);
  1491. if (!(glue = ast_rtp_instance_get_glue(peer->tech->type))) {
  1492. ast_channel_unlock(peer);
  1493. return -1;
  1494. }
  1495. glue->get_rtp_info(peer, &peer_instance);
  1496. if (!peer_instance || peer_instance->engine != instance->engine) {
  1497. ast_channel_unlock(peer);
  1498. ao2_ref(peer_instance, -1);
  1499. peer_instance = NULL;
  1500. return -1;
  1501. }
  1502. res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
  1503. ast_channel_unlock(peer);
  1504. ao2_ref(peer_instance, -1);
  1505. peer_instance = NULL;
  1506. return res;
  1507. }
  1508. 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)
  1509. {
  1510. if (instance->engine->available_formats) {
  1511. instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
  1512. if (!ast_format_cap_is_empty(result)) {
  1513. return;
  1514. }
  1515. }
  1516. ast_translate_available_formats(to_endpoint, to_asterisk, result);
  1517. }
  1518. int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
  1519. {
  1520. return instance->engine->activate ? instance->engine->activate(instance) : 0;
  1521. }
  1522. void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
  1523. struct ast_sockaddr *suggestion,
  1524. const char *username)
  1525. {
  1526. if (instance->engine->stun_request) {
  1527. instance->engine->stun_request(instance, suggestion, username);
  1528. }
  1529. }
  1530. void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
  1531. {
  1532. instance->timeout = timeout;
  1533. }
  1534. void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
  1535. {
  1536. instance->holdtimeout = timeout;
  1537. }
  1538. void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
  1539. {
  1540. instance->keepalive = interval;
  1541. }
  1542. int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
  1543. {
  1544. return instance->timeout;
  1545. }
  1546. int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
  1547. {
  1548. return instance->holdtimeout;
  1549. }
  1550. int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
  1551. {
  1552. return instance->keepalive;
  1553. }
  1554. struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
  1555. {
  1556. return instance->engine;
  1557. }
  1558. struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
  1559. {
  1560. return instance->glue;
  1561. }
  1562. struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
  1563. {
  1564. return instance->chan;
  1565. }
  1566. int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
  1567. {
  1568. if (res_srtp || res_srtp_policy) {
  1569. return -1;
  1570. }
  1571. if (!srtp_res || !policy_res) {
  1572. return -1;
  1573. }
  1574. res_srtp = srtp_res;
  1575. res_srtp_policy = policy_res;
  1576. return 0;
  1577. }
  1578. void ast_rtp_engine_unregister_srtp(void)
  1579. {
  1580. res_srtp = NULL;
  1581. res_srtp_policy = NULL;
  1582. }
  1583. int ast_rtp_engine_srtp_is_registered(void)
  1584. {
  1585. return res_srtp && res_srtp_policy;
  1586. }
  1587. int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
  1588. {
  1589. int res = 0;
  1590. if (!res_srtp) {
  1591. return -1;
  1592. }
  1593. if (!instance->srtp) {
  1594. res = res_srtp->create(&instance->srtp, instance, remote_policy);
  1595. } else {
  1596. res = res_srtp->replace(&instance->srtp, instance, remote_policy);
  1597. }
  1598. if (!res) {
  1599. res = res_srtp->add_stream(instance->srtp, local_policy);
  1600. }
  1601. return res;
  1602. }
  1603. struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
  1604. {
  1605. return instance->srtp;
  1606. }
  1607. int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
  1608. {
  1609. if (instance->engine->sendcng) {
  1610. return instance->engine->sendcng(instance, level);
  1611. }
  1612. return -1;
  1613. }
  1614. static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
  1615. {
  1616. int x = mime_types_len;
  1617. if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
  1618. return;
  1619. }
  1620. ast_rwlock_wrlock(&mime_types_lock);
  1621. if (format) {
  1622. ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
  1623. ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
  1624. } else {
  1625. ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
  1626. }
  1627. ast_rtp_mime_types[x].type = type;
  1628. ast_rtp_mime_types[x].subtype = subtype;
  1629. ast_rtp_mime_types[x].sample_rate = sample_rate;
  1630. mime_types_len++;
  1631. ast_rwlock_unlock(&mime_types_lock);
  1632. }
  1633. static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
  1634. {
  1635. int x;
  1636. ast_rwlock_wrlock(&static_RTP_PT_lock);
  1637. if (map < 0) {
  1638. /* find next available dynamic payload slot */
  1639. for (x = 96; x < 127; x++) {
  1640. if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
  1641. map = x;
  1642. break;
  1643. }
  1644. }
  1645. }
  1646. if (map < 0) {
  1647. ast_log(LOG_WARNING, "No Dynamic RTP mapping avaliable for format %s\n" ,ast_getformatname(format));
  1648. ast_rwlock_unlock(&static_RTP_PT_lock);
  1649. return;
  1650. }
  1651. if (format) {
  1652. static_RTP_PT[map].asterisk_format = 1;
  1653. ast_format_copy(&static_RTP_PT[map].format, format);
  1654. } else {
  1655. static_RTP_PT[map].rtp_code = rtp_code;
  1656. }
  1657. ast_rwlock_unlock(&static_RTP_PT_lock);
  1658. }
  1659. int ast_rtp_engine_load_format(const struct ast_format *format)
  1660. {
  1661. switch (format->id) {
  1662. case AST_FORMAT_SILK:
  1663. set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
  1664. add_static_payload(-1, format, 0);
  1665. break;
  1666. case AST_FORMAT_CELT:
  1667. set_next_mime_type(format, 0, "audio", "CELT", ast_format_rate(format));
  1668. add_static_payload(-1, format, 0);
  1669. break;
  1670. default:
  1671. break;
  1672. }
  1673. return 0;
  1674. }
  1675. int ast_rtp_engine_unload_format(const struct ast_format *format)
  1676. {
  1677. int x;
  1678. int y = 0;
  1679. ast_rwlock_wrlock(&static_RTP_PT_lock);
  1680. /* remove everything pertaining to this format id from the lists */
  1681. for (x = 0; x < AST_RTP_MAX_PT; x++) {
  1682. if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
  1683. memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
  1684. }
  1685. }
  1686. ast_rwlock_unlock(&static_RTP_PT_lock);
  1687. ast_rwlock_wrlock(&mime_types_lock);
  1688. /* rebuild the list skipping the items matching this id */
  1689. for (x = 0; x < mime_types_len; x++) {
  1690. if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
  1691. continue;
  1692. }
  1693. ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
  1694. y++;
  1695. }
  1696. mime_types_len = y;
  1697. ast_rwlock_unlock(&mime_types_lock);
  1698. return 0;
  1699. }
  1700. int ast_rtp_engine_init()
  1701. {
  1702. struct ast_format tmpfmt;
  1703. ast_rwlock_init(&mime_types_lock);
  1704. ast_rwlock_init(&static_RTP_PT_lock);
  1705. /* Define all the RTP mime types available */
  1706. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
  1707. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
  1708. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
  1709. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
  1710. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
  1711. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
  1712. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
  1713. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
  1714. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
  1715. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
  1716. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16-256", 16000);
  1717. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
  1718. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
  1719. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
  1720. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
  1721. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
  1722. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0, "audio", "speex", 16000);
  1723. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0, "audio", "speex", 32000);
  1724. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
  1725. /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
  1726. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
  1727. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
  1728. set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
  1729. set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
  1730. set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
  1731. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
  1732. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
  1733. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
  1734. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
  1735. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "h263-1998", 90000);
  1736. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
  1737. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
  1738. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
  1739. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
  1740. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
  1741. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
  1742. set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
  1743. /* Define the static rtp payload mappings */
  1744. add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
  1745. #ifdef USE_DEPRECATED_G726
  1746. 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... */
  1747. #endif
  1748. add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
  1749. add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
  1750. add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
  1751. add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
  1752. add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
  1753. add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
  1754. add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
  1755. add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
  1756. add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
  1757. add_static_payload(13, NULL, AST_RTP_CN);
  1758. add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
  1759. add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
  1760. add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
  1761. add_static_payload(19, NULL, AST_RTP_CN); /* Also used for CN */
  1762. add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
  1763. add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
  1764. add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
  1765. add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
  1766. add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
  1767. add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
  1768. add_static_payload(101, NULL, AST_RTP_DTMF);
  1769. add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
  1770. add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
  1771. add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
  1772. add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0); /* Real time text chat (with redundancy encoding) */
  1773. add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0); /* Real time text chat */
  1774. add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
  1775. add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
  1776. add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
  1777. add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
  1778. add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
  1779. add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
  1780. add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
  1781. add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
  1782. add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */
  1783. return 0;
  1784. }