chan_multicast_rtp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, 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 Multicast RTP Paging Channel
  25. *
  26. * \ingroup channel_drivers
  27. */
  28. /*** MODULEINFO
  29. <support_level>deprecated</support_level>
  30. <defaultenabled>no</defaultenabled>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include <fcntl.h>
  35. #include <sys/signal.h>
  36. #include "asterisk/lock.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/config.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/pbx.h"
  41. #include "asterisk/sched.h"
  42. #include "asterisk/io.h"
  43. #include "asterisk/acl.h"
  44. #include "asterisk/callerid.h"
  45. #include "asterisk/file.h"
  46. #include "asterisk/cli.h"
  47. #include "asterisk/app.h"
  48. #include "asterisk/rtp_engine.h"
  49. #include "asterisk/causes.h"
  50. static const char tdesc[] = "Multicast RTP Paging Channel Driver";
  51. /* Forward declarations */
  52. 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);
  53. static int multicast_rtp_call(struct ast_channel *ast, const char *dest, int timeout);
  54. static int multicast_rtp_hangup(struct ast_channel *ast);
  55. static struct ast_frame *multicast_rtp_read(struct ast_channel *ast);
  56. static int multicast_rtp_write(struct ast_channel *ast, struct ast_frame *f);
  57. /* Channel driver declaration */
  58. static struct ast_channel_tech multicast_rtp_tech = {
  59. .type = "MulticastRTP",
  60. .description = tdesc,
  61. .requester = multicast_rtp_request,
  62. .call = multicast_rtp_call,
  63. .hangup = multicast_rtp_hangup,
  64. .read = multicast_rtp_read,
  65. .write = multicast_rtp_write,
  66. };
  67. /*! \brief Function called when we should read a frame from the channel */
  68. static struct ast_frame *multicast_rtp_read(struct ast_channel *ast)
  69. {
  70. return &ast_null_frame;
  71. }
  72. /*! \brief Function called when we should write a frame to the channel */
  73. static int multicast_rtp_write(struct ast_channel *ast, struct ast_frame *f)
  74. {
  75. struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
  76. return ast_rtp_instance_write(instance, f);
  77. }
  78. /*! \brief Function called when we should actually call the destination */
  79. static int multicast_rtp_call(struct ast_channel *ast, const char *dest, int timeout)
  80. {
  81. struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
  82. ast_queue_control(ast, AST_CONTROL_ANSWER);
  83. return ast_rtp_instance_activate(instance);
  84. }
  85. /*! \brief Function called when we should hang the channel up */
  86. static int multicast_rtp_hangup(struct ast_channel *ast)
  87. {
  88. struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
  89. ast_rtp_instance_destroy(instance);
  90. ast_channel_tech_pvt_set(ast, NULL);
  91. return 0;
  92. }
  93. /*! \brief Function called when we should prepare to call the destination */
  94. 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)
  95. {
  96. char *tmp = ast_strdupa(data), *multicast_type = tmp, *destination, *control;
  97. struct ast_rtp_instance *instance;
  98. struct ast_sockaddr control_address;
  99. struct ast_sockaddr destination_address;
  100. struct ast_channel *chan;
  101. struct ast_format_cap *caps = NULL;
  102. struct ast_format *fmt = NULL;
  103. fmt = ast_format_cap_get_format(cap, 0);
  104. ast_sockaddr_setnull(&control_address);
  105. /* If no type was given we can't do anything */
  106. if (ast_strlen_zero(multicast_type)) {
  107. goto failure;
  108. }
  109. if (!(destination = strchr(tmp, '/'))) {
  110. goto failure;
  111. }
  112. *destination++ = '\0';
  113. if ((control = strchr(destination, '/'))) {
  114. *control++ = '\0';
  115. if (!ast_sockaddr_parse(&control_address, control,
  116. PARSE_PORT_REQUIRE)) {
  117. goto failure;
  118. }
  119. }
  120. if (!ast_sockaddr_parse(&destination_address, destination,
  121. PARSE_PORT_REQUIRE)) {
  122. goto failure;
  123. }
  124. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  125. if (!caps) {
  126. goto failure;
  127. }
  128. if (!(instance = ast_rtp_instance_new("multicast", NULL, &control_address, multicast_type))) {
  129. goto failure;
  130. }
  131. if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids, requestor, 0, "MulticastRTP/%p", instance))) {
  132. ast_rtp_instance_destroy(instance);
  133. goto failure;
  134. }
  135. ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
  136. ast_rtp_instance_set_remote_address(instance, &destination_address);
  137. ast_channel_tech_set(chan, &multicast_rtp_tech);
  138. ast_format_cap_append(caps, fmt, 0);
  139. ast_channel_nativeformats_set(chan, caps);
  140. ast_channel_set_writeformat(chan, fmt);
  141. ast_channel_set_rawwriteformat(chan, fmt);
  142. ast_channel_set_readformat(chan, fmt);
  143. ast_channel_set_rawreadformat(chan, fmt);
  144. ast_channel_tech_pvt_set(chan, instance);
  145. ast_channel_unlock(chan);
  146. ao2_ref(fmt, -1);
  147. ao2_ref(caps, -1);
  148. return chan;
  149. failure:
  150. ao2_cleanup(fmt);
  151. ao2_cleanup(caps);
  152. *cause = AST_CAUSE_FAILURE;
  153. return NULL;
  154. }
  155. /*! \brief Function called when our module is loaded */
  156. static int load_module(void)
  157. {
  158. if (!(multicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  159. return AST_MODULE_LOAD_DECLINE;
  160. }
  161. ast_format_cap_append_by_type(multicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
  162. if (ast_channel_register(&multicast_rtp_tech)) {
  163. ast_log(LOG_ERROR, "Unable to register channel class 'MulticastRTP'\n");
  164. ao2_ref(multicast_rtp_tech.capabilities, -1);
  165. multicast_rtp_tech.capabilities = NULL;
  166. return AST_MODULE_LOAD_DECLINE;
  167. }
  168. return AST_MODULE_LOAD_SUCCESS;
  169. }
  170. /*! \brief Function called when our module is unloaded */
  171. static int unload_module(void)
  172. {
  173. ast_channel_unregister(&multicast_rtp_tech);
  174. ao2_ref(multicast_rtp_tech.capabilities, -1);
  175. multicast_rtp_tech.capabilities = NULL;
  176. return 0;
  177. }
  178. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Multicast RTP Paging Channel (use chan_rtp instead)",
  179. .support_level = AST_MODULE_SUPPORT_DEPRECATED,
  180. .load = load_module,
  181. .unload = unload_module,
  182. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  183. );