chan_multicast_rtp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <fcntl.h>
  34. #include <sys/signal.h>
  35. #include "asterisk/lock.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/config.h"
  38. #include "asterisk/module.h"
  39. #include "asterisk/pbx.h"
  40. #include "asterisk/sched.h"
  41. #include "asterisk/io.h"
  42. #include "asterisk/acl.h"
  43. #include "asterisk/callerid.h"
  44. #include "asterisk/file.h"
  45. #include "asterisk/cli.h"
  46. #include "asterisk/app.h"
  47. #include "asterisk/rtp_engine.h"
  48. #include "asterisk/causes.h"
  49. static const char tdesc[] = "Multicast RTP Paging Channel Driver";
  50. /* Forward declarations */
  51. static struct ast_channel *multicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause);
  52. static int multicast_rtp_call(struct ast_channel *ast, char *dest, int timeout);
  53. static int multicast_rtp_hangup(struct ast_channel *ast);
  54. static struct ast_frame *multicast_rtp_read(struct ast_channel *ast);
  55. static int multicast_rtp_write(struct ast_channel *ast, struct ast_frame *f);
  56. /* Channel driver declaration */
  57. static struct ast_channel_tech multicast_rtp_tech = {
  58. .type = "MulticastRTP",
  59. .description = tdesc,
  60. .requester = multicast_rtp_request,
  61. .call = multicast_rtp_call,
  62. .hangup = multicast_rtp_hangup,
  63. .read = multicast_rtp_read,
  64. .write = multicast_rtp_write,
  65. };
  66. /*! \brief Function called when we should read a frame from the channel */
  67. static struct ast_frame *multicast_rtp_read(struct ast_channel *ast)
  68. {
  69. return &ast_null_frame;
  70. }
  71. /*! \brief Function called when we should write a frame to the channel */
  72. static int multicast_rtp_write(struct ast_channel *ast, struct ast_frame *f)
  73. {
  74. struct ast_rtp_instance *instance = ast->tech_pvt;
  75. return ast_rtp_instance_write(instance, f);
  76. }
  77. /*! \brief Function called when we should actually call the destination */
  78. static int multicast_rtp_call(struct ast_channel *ast, char *dest, int timeout)
  79. {
  80. struct ast_rtp_instance *instance = ast->tech_pvt;
  81. ast_queue_control(ast, AST_CONTROL_ANSWER);
  82. return ast_rtp_instance_activate(instance);
  83. }
  84. /*! \brief Function called when we should hang the channel up */
  85. static int multicast_rtp_hangup(struct ast_channel *ast)
  86. {
  87. struct ast_rtp_instance *instance = ast->tech_pvt;
  88. ast_rtp_instance_destroy(instance);
  89. ast->tech_pvt = NULL;
  90. return 0;
  91. }
  92. /*! \brief Function called when we should prepare to call the destination */
  93. static struct ast_channel *multicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
  94. {
  95. char *tmp = ast_strdupa(data), *multicast_type = tmp, *destination, *control;
  96. struct ast_rtp_instance *instance;
  97. struct ast_sockaddr control_address;
  98. struct ast_sockaddr destination_address;
  99. struct ast_channel *chan;
  100. struct ast_format fmt;
  101. ast_best_codec(cap, &fmt);
  102. ast_sockaddr_setnull(&control_address);
  103. /* If no type was given we can't do anything */
  104. if (ast_strlen_zero(multicast_type)) {
  105. goto failure;
  106. }
  107. if (!(destination = strchr(tmp, '/'))) {
  108. goto failure;
  109. }
  110. *destination++ = '\0';
  111. if ((control = strchr(destination, '/'))) {
  112. *control++ = '\0';
  113. if (!ast_sockaddr_parse(&control_address, control,
  114. PARSE_PORT_REQUIRE)) {
  115. goto failure;
  116. }
  117. }
  118. if (!ast_sockaddr_parse(&destination_address, destination,
  119. PARSE_PORT_REQUIRE)) {
  120. goto failure;
  121. }
  122. if (!(instance = ast_rtp_instance_new("multicast", NULL, &control_address, multicast_type))) {
  123. goto failure;
  124. }
  125. if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", requestor ? requestor->linkedid : "", 0, "MulticastRTP/%p", instance))) {
  126. ast_rtp_instance_destroy(instance);
  127. goto failure;
  128. }
  129. ast_rtp_instance_set_remote_address(instance, &destination_address);
  130. chan->tech = &multicast_rtp_tech;
  131. ast_format_cap_add(chan->nativeformats, &fmt);
  132. ast_format_copy(&chan->writeformat, &fmt);
  133. ast_format_copy(&chan->rawwriteformat, &fmt);
  134. ast_format_copy(&chan->readformat, &fmt);
  135. ast_format_copy(&chan->rawreadformat, &fmt);
  136. chan->tech_pvt = instance;
  137. return chan;
  138. failure:
  139. *cause = AST_CAUSE_FAILURE;
  140. return NULL;
  141. }
  142. /*! \brief Function called when our module is loaded */
  143. static int load_module(void)
  144. {
  145. if (!(multicast_rtp_tech.capabilities = ast_format_cap_alloc())) {
  146. return AST_MODULE_LOAD_DECLINE;
  147. }
  148. ast_format_cap_add_all(multicast_rtp_tech.capabilities);
  149. if (ast_channel_register(&multicast_rtp_tech)) {
  150. ast_log(LOG_ERROR, "Unable to register channel class 'MulticastRTP'\n");
  151. return AST_MODULE_LOAD_DECLINE;
  152. }
  153. return AST_MODULE_LOAD_SUCCESS;
  154. }
  155. /*! \brief Function called when our module is unloaded */
  156. static int unload_module(void)
  157. {
  158. ast_channel_unregister(&multicast_rtp_tech);
  159. multicast_rtp_tech.capabilities = ast_format_cap_destroy(multicast_rtp_tech.capabilities);
  160. return 0;
  161. }
  162. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Multicast RTP Paging Channel",
  163. .load = load_module,
  164. .unload = unload_module,
  165. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  166. );