res_hep_rtcp: Add module that sends RTCP information to a Homer Server
[asterisk/asterisk.git] / res / res_hep_rtcp.c
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 /*!
20  * \file
21  * \brief RTCP logging with Homer
22  *
23  * \author Matt Jordan <mjordan@digium.com>
24  *
25  */
26
27 /*** MODULEINFO
28         <depend>res_hep</depend>
29         <defaultenabled>no</defaultenabled>
30         <support_level>extended</support_level>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <pjsip.h>
38
39 #include "asterisk/res_hep.h"
40 #include "asterisk/module.h"
41 #include "asterisk/netsock2.h"
42 #include "asterisk/stasis.h"
43 #include "asterisk/rtp_engine.h"
44 #include "asterisk/json.h"
45 #include "asterisk/config.h"
46
47 static struct stasis_subscription *stasis_rtp_subscription;
48
49 static void rtcp_message_handler(struct stasis_message *message)
50 {
51
52         RAII_VAR(struct ast_json *, json_payload, NULL, ast_json_unref);
53         RAII_VAR(char *,  payload, NULL, ast_json_free);
54         struct ast_json *json_blob;
55         struct ast_json *json_channel;
56         struct ast_json *json_rtcp;
57         struct hepv3_capture_info *capture_info;
58         struct ast_json *from;
59         struct ast_json *to;
60         struct timeval current_time = ast_tvnow();
61
62         json_payload = stasis_message_to_json(message, NULL);
63         if (!json_payload) {
64                 return;
65         }
66
67         json_blob = ast_json_object_get(json_payload, "blob");
68         if (!json_blob) {
69                 return;
70         }
71
72         json_channel = ast_json_object_get(json_payload, "channel");
73         if (!json_channel) {
74                 return;
75         }
76
77         json_rtcp = ast_json_object_get(json_payload, "rtcp_report");
78         if (!json_rtcp) {
79                 return;
80         }
81
82         from = ast_json_object_get(json_blob, "from");
83         to = ast_json_object_get(json_blob, "to");
84         if (!from || !to) {
85                 return;
86         }
87
88         payload = ast_json_dump_string(json_rtcp);
89         if (ast_strlen_zero(payload)) {
90                 return;
91         }
92
93         capture_info = hepv3_create_capture_info(payload, strlen(payload));
94         if (!capture_info) {
95                 return;
96         }
97         ast_sockaddr_parse(&capture_info->src_addr, ast_json_string_get(from), PARSE_PORT_REQUIRE);
98         ast_sockaddr_parse(&capture_info->dst_addr, ast_json_string_get(to), PARSE_PORT_REQUIRE);
99
100         capture_info->uuid = ast_strdup(ast_json_string_get(ast_json_object_get(json_channel, "name")));
101         if (!capture_info->uuid) {
102                 ao2_ref(capture_info, -1);
103                 return;
104         }
105         capture_info->capture_time = current_time;
106         capture_info->capture_type = HEPV3_CAPTURE_TYPE_RTCP;
107         capture_info->zipped = 0;
108
109         hepv3_send_packet(capture_info);
110 }
111
112 static void rtp_topic_handler(void *data, struct stasis_subscription *sub, struct stasis_message *message)
113 {
114         struct stasis_message_type *message_type = stasis_message_type(message);
115
116         if ((message_type == ast_rtp_rtcp_sent_type()) ||
117                 (message_type == ast_rtp_rtcp_received_type())) {
118                 rtcp_message_handler(message);
119         }
120 }
121
122 static int load_module(void)
123 {
124
125         stasis_rtp_subscription = stasis_subscribe(ast_rtp_topic(),
126                 rtp_topic_handler, NULL);
127         if (!stasis_rtp_subscription) {
128                 return AST_MODULE_LOAD_FAILURE;
129         }
130
131         return AST_MODULE_LOAD_SUCCESS;
132 }
133
134 static int unload_module(void)
135 {
136         if (stasis_rtp_subscription) {
137                 stasis_rtp_subscription = stasis_unsubscribe(stasis_rtp_subscription);
138         }
139
140         return 0;
141 }
142
143 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RTCP HEPv3 Logger",
144         .load = load_module,
145         .unload = unload_module,
146         .load_pri = AST_MODPRI_DEFAULT,
147         );