chan_rtp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009 - 2014, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. * Andreas 'MacBrody' Brodmann <andreas.brodmann@gmail.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \author Joshua Colp <jcolp@digium.com>
  22. * \author Andreas 'MacBrody' Broadmann <andreas.brodmann@gmail.com>
  23. *
  24. * \brief RTP (Multicast and Unicast) Media Channel
  25. *
  26. * \ingroup channel_drivers
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/channel.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/acl.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/rtp_engine.h"
  39. #include "asterisk/causes.h"
  40. #include "asterisk/format_cache.h"
  41. /* Forward declarations */
  42. static struct ast_channel *multicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
  43. static struct ast_channel *unicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
  44. static int rtp_call(struct ast_channel *ast, const char *dest, int timeout);
  45. static int rtp_hangup(struct ast_channel *ast);
  46. static struct ast_frame *rtp_read(struct ast_channel *ast);
  47. static int rtp_write(struct ast_channel *ast, struct ast_frame *f);
  48. /* Multicast channel driver declaration */
  49. static struct ast_channel_tech multicast_rtp_tech = {
  50. .type = "MulticastRTP",
  51. .description = "Multicast RTP Paging Channel Driver",
  52. .requester = multicast_rtp_request,
  53. .call = rtp_call,
  54. .hangup = rtp_hangup,
  55. .read = rtp_read,
  56. .write = rtp_write,
  57. };
  58. /* Unicast channel driver declaration */
  59. static struct ast_channel_tech unicast_rtp_tech = {
  60. .type = "UnicastRTP",
  61. .description = "Unicast RTP Media Channel Driver",
  62. .requester = unicast_rtp_request,
  63. .call = rtp_call,
  64. .hangup = rtp_hangup,
  65. .read = rtp_read,
  66. .write = rtp_write,
  67. };
  68. /*! \brief Function called when we should read a frame from the channel */
  69. static struct ast_frame *rtp_read(struct ast_channel *ast)
  70. {
  71. struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
  72. int fdno = ast_channel_fdno(ast);
  73. switch (fdno) {
  74. case 0:
  75. return ast_rtp_instance_read(instance, 0);
  76. default:
  77. return &ast_null_frame;
  78. }
  79. }
  80. /*! \brief Function called when we should write a frame to the channel */
  81. static int rtp_write(struct ast_channel *ast, struct ast_frame *f)
  82. {
  83. struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
  84. return ast_rtp_instance_write(instance, f);
  85. }
  86. /*! \brief Function called when we should actually call the destination */
  87. static int rtp_call(struct ast_channel *ast, const char *dest, int timeout)
  88. {
  89. struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
  90. ast_queue_control(ast, AST_CONTROL_ANSWER);
  91. return ast_rtp_instance_activate(instance);
  92. }
  93. /*! \brief Function called when we should hang the channel up */
  94. static int rtp_hangup(struct ast_channel *ast)
  95. {
  96. struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
  97. ast_rtp_instance_destroy(instance);
  98. ast_channel_tech_pvt_set(ast, NULL);
  99. return 0;
  100. }
  101. /*! \brief Function called when we should prepare to call the multicast destination */
  102. static struct ast_channel *multicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  103. {
  104. char *parse;
  105. struct ast_rtp_instance *instance;
  106. struct ast_sockaddr control_address;
  107. struct ast_sockaddr destination_address;
  108. struct ast_channel *chan;
  109. struct ast_format_cap *caps = NULL;
  110. struct ast_format *fmt = NULL;
  111. AST_DECLARE_APP_ARGS(args,
  112. AST_APP_ARG(type);
  113. AST_APP_ARG(destination);
  114. AST_APP_ARG(control);
  115. );
  116. if (ast_strlen_zero(data)) {
  117. ast_log(LOG_ERROR, "A multicast type and destination must be given to the 'MulticastRTP' channel\n");
  118. goto failure;
  119. }
  120. parse = ast_strdupa(data);
  121. AST_NONSTANDARD_APP_ARGS(args, parse, '/');
  122. fmt = ast_format_cap_get_format(cap, 0);
  123. ast_sockaddr_setnull(&control_address);
  124. if (!ast_strlen_zero(args.control) &&
  125. !ast_sockaddr_parse(&control_address, args.control, PARSE_PORT_REQUIRE)) {
  126. ast_log(LOG_ERROR, "Control address '%s' could not be parsed\n", args.control);
  127. goto failure;
  128. }
  129. if (!ast_sockaddr_parse(&destination_address, args.destination,
  130. PARSE_PORT_REQUIRE)) {
  131. ast_log(LOG_ERROR, "Destination address '%s' could not be parsed\n", args.destination);
  132. goto failure;
  133. }
  134. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  135. if (!caps) {
  136. goto failure;
  137. }
  138. if (!(instance = ast_rtp_instance_new("multicast", NULL, &control_address, args.type))) {
  139. ast_log(LOG_ERROR, "Could not create RTP instance for sending media to '%s'\n", args.destination);
  140. goto failure;
  141. }
  142. if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids, requestor, 0, "MulticastRTP/%p", instance))) {
  143. ast_rtp_instance_destroy(instance);
  144. goto failure;
  145. }
  146. ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
  147. ast_rtp_instance_set_remote_address(instance, &destination_address);
  148. ast_channel_tech_set(chan, &multicast_rtp_tech);
  149. ast_format_cap_append(caps, fmt, 0);
  150. ast_channel_nativeformats_set(chan, caps);
  151. ast_channel_set_writeformat(chan, fmt);
  152. ast_channel_set_rawwriteformat(chan, fmt);
  153. ast_channel_set_readformat(chan, fmt);
  154. ast_channel_set_rawreadformat(chan, fmt);
  155. ast_channel_tech_pvt_set(chan, instance);
  156. ast_channel_unlock(chan);
  157. ao2_ref(fmt, -1);
  158. ao2_ref(caps, -1);
  159. return chan;
  160. failure:
  161. ao2_cleanup(fmt);
  162. ao2_cleanup(caps);
  163. *cause = AST_CAUSE_FAILURE;
  164. return NULL;
  165. }
  166. /*! \brief Function called when we should prepare to call the unicast destination */
  167. static struct ast_channel *unicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  168. {
  169. char *parse;
  170. struct ast_rtp_instance *instance;
  171. struct ast_sockaddr address;
  172. struct ast_sockaddr local_address;
  173. struct ast_channel *chan;
  174. struct ast_format_cap *caps = NULL;
  175. struct ast_format *fmt = NULL;
  176. AST_DECLARE_APP_ARGS(args,
  177. AST_APP_ARG(destination);
  178. AST_APP_ARG(engine);
  179. AST_APP_ARG(format);
  180. );
  181. if (ast_strlen_zero(data)) {
  182. goto failure;
  183. }
  184. parse = ast_strdupa(data);
  185. AST_NONSTANDARD_APP_ARGS(args, parse, '/');
  186. if (!ast_strlen_zero(args.format)) {
  187. fmt = ast_format_cache_get(args.format);
  188. } else {
  189. fmt = ast_format_cap_get_format(cap, 0);
  190. }
  191. if (!fmt) {
  192. ast_log(LOG_ERROR, "No format specified for sending RTP to '%s'\n", args.destination);
  193. goto failure;
  194. }
  195. if (!ast_sockaddr_parse(&address, args.destination,
  196. PARSE_PORT_REQUIRE)) {
  197. ast_log(LOG_ERROR, "Destination '%s' could not be parsed\n", args.destination);
  198. goto failure;
  199. }
  200. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  201. if (!caps) {
  202. goto failure;
  203. }
  204. ast_ouraddrfor(&address, &local_address);
  205. if (!(instance = ast_rtp_instance_new(args.engine, NULL, &local_address, NULL))) {
  206. ast_log(LOG_ERROR, "Could not create RTP instance for sending media to '%s'\n", args.destination);
  207. goto failure;
  208. }
  209. if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids, requestor, 0, "UnicastRTP/%s-%p", args.destination, instance))) {
  210. ast_rtp_instance_destroy(instance);
  211. goto failure;
  212. }
  213. ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
  214. ast_rtp_instance_set_remote_address(instance, &address);
  215. ast_channel_set_fd(chan, 0, ast_rtp_instance_fd(instance, 0));
  216. ast_channel_tech_set(chan, &unicast_rtp_tech);
  217. ast_format_cap_append(caps, fmt, 0);
  218. ast_channel_nativeformats_set(chan, caps);
  219. ast_channel_set_writeformat(chan, fmt);
  220. ast_channel_set_rawwriteformat(chan, fmt);
  221. ast_channel_set_readformat(chan, fmt);
  222. ast_channel_set_rawreadformat(chan, fmt);
  223. ast_channel_tech_pvt_set(chan, instance);
  224. pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_ADDRESS", ast_sockaddr_stringify_addr(&local_address));
  225. ast_rtp_instance_get_local_address(instance, &local_address);
  226. pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_PORT", ast_sockaddr_stringify_port(&local_address));
  227. ast_channel_unlock(chan);
  228. ao2_ref(fmt, -1);
  229. ao2_ref(caps, -1);
  230. return chan;
  231. failure:
  232. ao2_cleanup(fmt);
  233. ao2_cleanup(caps);
  234. *cause = AST_CAUSE_FAILURE;
  235. return NULL;
  236. }
  237. /*! \brief Function called when our module is unloaded */
  238. static int unload_module(void)
  239. {
  240. ast_channel_unregister(&multicast_rtp_tech);
  241. ao2_cleanup(multicast_rtp_tech.capabilities);
  242. multicast_rtp_tech.capabilities = NULL;
  243. ast_channel_unregister(&unicast_rtp_tech);
  244. ao2_cleanup(unicast_rtp_tech.capabilities);
  245. unicast_rtp_tech.capabilities = NULL;
  246. return 0;
  247. }
  248. /*! \brief Function called when our module is loaded */
  249. static int load_module(void)
  250. {
  251. if (!(multicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  252. return AST_MODULE_LOAD_DECLINE;
  253. }
  254. ast_format_cap_append_by_type(multicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
  255. if (ast_channel_register(&multicast_rtp_tech)) {
  256. ast_log(LOG_ERROR, "Unable to register channel class 'MulticastRTP'\n");
  257. unload_module();
  258. return AST_MODULE_LOAD_DECLINE;
  259. }
  260. if (!(unicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  261. unload_module();
  262. return AST_MODULE_LOAD_DECLINE;
  263. }
  264. ast_format_cap_append_by_type(unicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
  265. if (ast_channel_register(&unicast_rtp_tech)) {
  266. ast_log(LOG_ERROR, "Unable to register channel class 'UnicastRTP'\n");
  267. unload_module();
  268. return AST_MODULE_LOAD_DECLINE;
  269. }
  270. return AST_MODULE_LOAD_SUCCESS;
  271. }
  272. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RTP Media Channel",
  273. .support_level = AST_MODULE_SUPPORT_CORE,
  274. .load = load_module,
  275. .unload = unload_module,
  276. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  277. );