03db181596606a1b5e37a296d7448267f6cef0b0
[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         <support_level>extended</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_REGISTER_FILE()
35
36 #include "asterisk/res_hep.h"
37 #include "asterisk/module.h"
38 #include "asterisk/netsock2.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/stasis.h"
42 #include "asterisk/rtp_engine.h"
43 #include "asterisk/json.h"
44 #include "asterisk/config.h"
45
46 static struct stasis_subscription *stasis_rtp_subscription;
47
48 static char *assign_uuid(struct ast_json *json_channel)
49 {
50         const char *channel_name = ast_json_string_get(ast_json_object_get(json_channel, "name"));
51         enum hep_uuid_type uuid_type = hepv3_get_uuid_type();
52         char *uuid = NULL;
53
54         if (!channel_name) {
55                 return NULL;
56         }
57
58         if (uuid_type == HEP_UUID_TYPE_CALL_ID && ast_begins_with(channel_name, "PJSIP")) {
59                 struct ast_channel *chan = ast_channel_get_by_name(channel_name);
60                 char buf[128];
61
62                 if (chan && !ast_func_read(chan, "CHANNEL(pjsip,call-id)", buf, sizeof(buf))) {
63                         uuid = ast_strdup(buf);
64                 }
65
66                 ast_channel_cleanup(chan);
67         }
68
69         /* If we couldn't get the call-id or didn't want it, just use the channel name */
70         if (!uuid) {
71                 uuid = ast_strdup(channel_name);
72         }
73
74         return uuid;
75 }
76
77 static void rtcp_message_handler(struct stasis_message *message)
78 {
79
80         RAII_VAR(struct ast_json *, json_payload, NULL, ast_json_unref);
81         RAII_VAR(char *,  payload, NULL, ast_json_free);
82         struct ast_json *json_blob;
83         struct ast_json *json_channel;
84         struct ast_json *json_rtcp;
85         struct hepv3_capture_info *capture_info;
86         struct ast_json *from;
87         struct ast_json *to;
88         struct timeval current_time = ast_tvnow();
89
90         json_payload = stasis_message_to_json(message, NULL);
91         if (!json_payload) {
92                 return;
93         }
94
95         json_blob = ast_json_object_get(json_payload, "blob");
96         if (!json_blob) {
97                 return;
98         }
99
100         json_channel = ast_json_object_get(json_payload, "channel");
101         if (!json_channel) {
102                 return;
103         }
104
105         json_rtcp = ast_json_object_get(json_payload, "rtcp_report");
106         if (!json_rtcp) {
107                 return;
108         }
109
110         from = ast_json_object_get(json_blob, "from");
111         to = ast_json_object_get(json_blob, "to");
112         if (!from || !to) {
113                 return;
114         }
115
116         payload = ast_json_dump_string(json_rtcp);
117         if (ast_strlen_zero(payload)) {
118                 return;
119         }
120
121         capture_info = hepv3_create_capture_info(payload, strlen(payload));
122         if (!capture_info) {
123                 return;
124         }
125         ast_sockaddr_parse(&capture_info->src_addr, ast_json_string_get(from), PARSE_PORT_REQUIRE);
126         ast_sockaddr_parse(&capture_info->dst_addr, ast_json_string_get(to), PARSE_PORT_REQUIRE);
127
128         capture_info->uuid = assign_uuid(json_channel);
129         if (!capture_info->uuid) {
130                 ao2_ref(capture_info, -1);
131                 return;
132         }
133         capture_info->capture_time = current_time;
134         capture_info->capture_type = HEPV3_CAPTURE_TYPE_RTCP;
135         capture_info->zipped = 0;
136
137         hepv3_send_packet(capture_info);
138 }
139
140 static void rtp_topic_handler(void *data, struct stasis_subscription *sub, struct stasis_message *message)
141 {
142         struct stasis_message_type *message_type = stasis_message_type(message);
143
144         if ((message_type == ast_rtp_rtcp_sent_type()) ||
145                 (message_type == ast_rtp_rtcp_received_type())) {
146                 rtcp_message_handler(message);
147         }
148 }
149
150 static int load_module(void)
151 {
152         if (!ast_module_check("res_hep.so") || !hepv3_is_loaded()) {
153                 ast_log(AST_LOG_WARNING, "res_hep is not loaded or running; declining module load\n");
154                 return AST_MODULE_LOAD_DECLINE;
155         }
156
157         stasis_rtp_subscription = stasis_subscribe(ast_rtp_topic(),
158                 rtp_topic_handler, NULL);
159         if (!stasis_rtp_subscription) {
160                 return AST_MODULE_LOAD_FAILURE;
161         }
162
163         return AST_MODULE_LOAD_SUCCESS;
164 }
165
166 static int unload_module(void)
167 {
168         if (stasis_rtp_subscription) {
169                 stasis_rtp_subscription = stasis_unsubscribe_and_join(stasis_rtp_subscription);
170         }
171
172         return 0;
173 }
174
175 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RTCP HEPv3 Logger",
176         .load = load_module,
177         .unload = unload_module,
178         .load_pri = AST_MODPRI_DEFAULT,
179 );