res_rtp_multicast.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. /*!
  20. * \file
  21. *
  22. * \brief Multicast RTP Engine
  23. *
  24. * \author Joshua Colp <jcolp@digium.com>
  25. * \author Andreas 'MacBrody' Brodmann <andreas.brodmann@gmail.com>
  26. *
  27. * \ingroup rtp_engines
  28. */
  29. /*** MODULEINFO
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include <sys/time.h>
  35. #include <signal.h>
  36. #include <fcntl.h>
  37. #include <math.h>
  38. #include "asterisk/pbx.h"
  39. #include "asterisk/frame.h"
  40. #include "asterisk/channel.h"
  41. #include "asterisk/acl.h"
  42. #include "asterisk/config.h"
  43. #include "asterisk/lock.h"
  44. #include "asterisk/utils.h"
  45. #include "asterisk/cli.h"
  46. #include "asterisk/manager.h"
  47. #include "asterisk/unaligned.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/rtp_engine.h"
  50. /*! Command value used for Linksys paging to indicate we are starting */
  51. #define LINKSYS_MCAST_STARTCMD 6
  52. /*! Command value used for Linksys paging to indicate we are stopping */
  53. #define LINKSYS_MCAST_STOPCMD 7
  54. /*! \brief Type of paging to do */
  55. enum multicast_type {
  56. /*! Simple multicast enabled client/receiver paging like Snom and Barix uses */
  57. MULTICAST_TYPE_BASIC = 0,
  58. /*! More advanced Linksys type paging which requires a start and stop packet */
  59. MULTICAST_TYPE_LINKSYS,
  60. };
  61. /*! \brief Structure for a Linksys control packet */
  62. struct multicast_control_packet {
  63. /*! Unique identifier for the control packet */
  64. uint32_t unique_id;
  65. /*! Actual command in the control packet */
  66. uint32_t command;
  67. /*! IP address for the RTP */
  68. uint32_t ip;
  69. /*! Port for the RTP */
  70. uint32_t port;
  71. };
  72. /*! \brief Structure for a multicast paging instance */
  73. struct multicast_rtp {
  74. /*! TYpe of multicast paging this instance is doing */
  75. enum multicast_type type;
  76. /*! Socket used for sending the audio on */
  77. int socket;
  78. /*! Synchronization source value, used when creating/sending the RTP packet */
  79. unsigned int ssrc;
  80. /*! Sequence number, used when creating/sending the RTP packet */
  81. uint16_t seqno;
  82. };
  83. /* Forward Declarations */
  84. static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data);
  85. static int multicast_rtp_activate(struct ast_rtp_instance *instance);
  86. static int multicast_rtp_destroy(struct ast_rtp_instance *instance);
  87. static int multicast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
  88. static struct ast_frame *multicast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
  89. /* RTP Engine Declaration */
  90. static struct ast_rtp_engine multicast_rtp_engine = {
  91. .name = "multicast",
  92. .new = multicast_rtp_new,
  93. .activate = multicast_rtp_activate,
  94. .destroy = multicast_rtp_destroy,
  95. .write = multicast_rtp_write,
  96. .read = multicast_rtp_read,
  97. };
  98. /*! \brief Function called to create a new multicast instance */
  99. static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data)
  100. {
  101. struct multicast_rtp *multicast;
  102. const char *type = data;
  103. if (!(multicast = ast_calloc(1, sizeof(*multicast)))) {
  104. return -1;
  105. }
  106. if (!strcasecmp(type, "basic")) {
  107. multicast->type = MULTICAST_TYPE_BASIC;
  108. } else if (!strcasecmp(type, "linksys")) {
  109. multicast->type = MULTICAST_TYPE_LINKSYS;
  110. } else {
  111. ast_free(multicast);
  112. return -1;
  113. }
  114. if ((multicast->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  115. ast_free(multicast);
  116. return -1;
  117. }
  118. multicast->ssrc = ast_random();
  119. ast_rtp_instance_set_data(instance, multicast);
  120. return 0;
  121. }
  122. /*! \brief Helper function which populates a control packet with useful information and sends it */
  123. static int multicast_send_control_packet(struct ast_rtp_instance *instance, struct multicast_rtp *multicast, int command)
  124. {
  125. struct multicast_control_packet control_packet = { .unique_id = htonl((u_long)time(NULL)),
  126. .command = htonl(command),
  127. };
  128. struct ast_sockaddr control_address, remote_address;
  129. ast_rtp_instance_get_local_address(instance, &control_address);
  130. ast_rtp_instance_get_remote_address(instance, &remote_address);
  131. /* Ensure the user of us have given us both the control address and destination address */
  132. if (ast_sockaddr_isnull(&control_address) ||
  133. ast_sockaddr_isnull(&remote_address)) {
  134. return -1;
  135. }
  136. /* The protocol only supports IPv4. */
  137. if (ast_sockaddr_is_ipv6(&remote_address)) {
  138. ast_log(LOG_WARNING, "Cannot send control packet for IPv6 "
  139. "remote address.\n");
  140. return -1;
  141. }
  142. control_packet.ip = htonl(ast_sockaddr_ipv4(&remote_address));
  143. control_packet.port = htonl(ast_sockaddr_port(&remote_address));
  144. /* Based on a recommendation by Brian West who did the FreeSWITCH implementation we send control packets twice */
  145. ast_sendto(multicast->socket, &control_packet, sizeof(control_packet), 0, &control_address);
  146. ast_sendto(multicast->socket, &control_packet, sizeof(control_packet), 0, &control_address);
  147. return 0;
  148. }
  149. /*! \brief Function called to indicate that audio is now going to flow */
  150. static int multicast_rtp_activate(struct ast_rtp_instance *instance)
  151. {
  152. struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance);
  153. if (multicast->type != MULTICAST_TYPE_LINKSYS) {
  154. return 0;
  155. }
  156. return multicast_send_control_packet(instance, multicast, LINKSYS_MCAST_STARTCMD);
  157. }
  158. /*! \brief Function called to destroy a multicast instance */
  159. static int multicast_rtp_destroy(struct ast_rtp_instance *instance)
  160. {
  161. struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance);
  162. if (multicast->type == MULTICAST_TYPE_LINKSYS) {
  163. multicast_send_control_packet(instance, multicast, LINKSYS_MCAST_STOPCMD);
  164. }
  165. close(multicast->socket);
  166. ast_free(multicast);
  167. return 0;
  168. }
  169. /*! \brief Function called to broadcast some audio on a multicast instance */
  170. static int multicast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
  171. {
  172. struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance);
  173. struct ast_frame *f = frame;
  174. struct ast_sockaddr remote_address;
  175. int hdrlen = 12, res = 0, codec;
  176. unsigned char *rtpheader;
  177. /* We only accept audio, nothing else */
  178. if (frame->frametype != AST_FRAME_VOICE) {
  179. return 0;
  180. }
  181. /* Grab the actual payload number for when we create the RTP packet */
  182. if ((codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 1, &frame->subclass.format, 0)) < 0) {
  183. return -1;
  184. }
  185. /* If we do not have space to construct an RTP header duplicate the frame so we get some */
  186. if (frame->offset < hdrlen) {
  187. f = ast_frdup(frame);
  188. }
  189. /* Construct an RTP header for our packet */
  190. rtpheader = (unsigned char *)(f->data.ptr - hdrlen);
  191. put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (multicast->seqno)));
  192. put_unaligned_uint32(rtpheader + 4, htonl(f->ts * 8));
  193. put_unaligned_uint32(rtpheader + 8, htonl(multicast->ssrc));
  194. /* Increment sequence number and wrap to 0 if it overflows 16 bits. */
  195. multicast->seqno = 0xFFFF & (multicast->seqno + 1);
  196. /* Finally send it out to the eager phones listening for us */
  197. ast_rtp_instance_get_remote_address(instance, &remote_address);
  198. if (ast_sendto(multicast->socket, (void *) rtpheader, f->datalen + hdrlen, 0, &remote_address) < 0) {
  199. ast_log(LOG_ERROR, "Multicast RTP Transmission error to %s: %s\n",
  200. ast_sockaddr_stringify(&remote_address),
  201. strerror(errno));
  202. res = -1;
  203. }
  204. /* If we were forced to duplicate the frame free the new one */
  205. if (frame != f) {
  206. ast_frfree(f);
  207. }
  208. return res;
  209. }
  210. /*! \brief Function called to read from a multicast instance */
  211. static struct ast_frame *multicast_rtp_read(struct ast_rtp_instance *instance, int rtcp)
  212. {
  213. return &ast_null_frame;
  214. }
  215. static int load_module(void)
  216. {
  217. if (ast_rtp_engine_register(&multicast_rtp_engine)) {
  218. return AST_MODULE_LOAD_DECLINE;
  219. }
  220. return AST_MODULE_LOAD_SUCCESS;
  221. }
  222. static int unload_module(void)
  223. {
  224. ast_rtp_engine_unregister(&multicast_rtp_engine);
  225. return 0;
  226. }
  227. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Multicast RTP Engine",
  228. .load = load_module,
  229. .unload = unload_module,
  230. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  231. );