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