res_hep_rtcp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2014, Digium, Inc.
  5. *
  6. * Matt Jordan <mjordan@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. /*!
  19. * \file
  20. * \brief RTCP logging with Homer
  21. *
  22. * \author Matt Jordan <mjordan@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>res_hep</depend>
  27. <support_level>extended</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/res_hep.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/netsock2.h"
  34. #include "asterisk/stasis.h"
  35. #include "asterisk/rtp_engine.h"
  36. #include "asterisk/json.h"
  37. #include "asterisk/config.h"
  38. static struct stasis_subscription *stasis_rtp_subscription;
  39. static void rtcp_message_handler(struct stasis_message *message)
  40. {
  41. RAII_VAR(struct ast_json *, json_payload, NULL, ast_json_unref);
  42. RAII_VAR(char *, payload, NULL, ast_json_free);
  43. struct ast_json *json_blob;
  44. struct ast_json *json_channel;
  45. struct ast_json *json_rtcp;
  46. struct hepv3_capture_info *capture_info;
  47. struct ast_json *from;
  48. struct ast_json *to;
  49. struct timeval current_time = ast_tvnow();
  50. json_payload = stasis_message_to_json(message, NULL);
  51. if (!json_payload) {
  52. return;
  53. }
  54. json_blob = ast_json_object_get(json_payload, "blob");
  55. if (!json_blob) {
  56. return;
  57. }
  58. json_channel = ast_json_object_get(json_payload, "channel");
  59. if (!json_channel) {
  60. return;
  61. }
  62. json_rtcp = ast_json_object_get(json_payload, "rtcp_report");
  63. if (!json_rtcp) {
  64. return;
  65. }
  66. from = ast_json_object_get(json_blob, "from");
  67. to = ast_json_object_get(json_blob, "to");
  68. if (!from || !to) {
  69. return;
  70. }
  71. payload = ast_json_dump_string(json_rtcp);
  72. if (ast_strlen_zero(payload)) {
  73. return;
  74. }
  75. capture_info = hepv3_create_capture_info(payload, strlen(payload));
  76. if (!capture_info) {
  77. return;
  78. }
  79. ast_sockaddr_parse(&capture_info->src_addr, ast_json_string_get(from), PARSE_PORT_REQUIRE);
  80. ast_sockaddr_parse(&capture_info->dst_addr, ast_json_string_get(to), PARSE_PORT_REQUIRE);
  81. capture_info->uuid = ast_strdup(ast_json_string_get(ast_json_object_get(json_channel, "name")));
  82. if (!capture_info->uuid) {
  83. ao2_ref(capture_info, -1);
  84. return;
  85. }
  86. capture_info->capture_time = current_time;
  87. capture_info->capture_type = HEPV3_CAPTURE_TYPE_RTCP;
  88. capture_info->zipped = 0;
  89. hepv3_send_packet(capture_info);
  90. }
  91. static void rtp_topic_handler(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  92. {
  93. struct stasis_message_type *message_type = stasis_message_type(message);
  94. if ((message_type == ast_rtp_rtcp_sent_type()) ||
  95. (message_type == ast_rtp_rtcp_received_type())) {
  96. rtcp_message_handler(message);
  97. }
  98. }
  99. static int load_module(void)
  100. {
  101. stasis_rtp_subscription = stasis_subscribe(ast_rtp_topic(),
  102. rtp_topic_handler, NULL);
  103. if (!stasis_rtp_subscription) {
  104. return AST_MODULE_LOAD_FAILURE;
  105. }
  106. return AST_MODULE_LOAD_SUCCESS;
  107. }
  108. static int unload_module(void)
  109. {
  110. if (stasis_rtp_subscription) {
  111. stasis_rtp_subscription = stasis_unsubscribe(stasis_rtp_subscription);
  112. }
  113. return 0;
  114. }
  115. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RTCP HEPv3 Logger",
  116. .load = load_module,
  117. .unload = unload_module,
  118. .load_pri = AST_MODPRI_DEFAULT,
  119. );