/src/rtpproxy/src/rtpp_analyzer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2015 Sippy Software, Inc., http://www.sippysoft.com |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
15 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
18 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 | | * SUCH DAMAGE. |
25 | | * |
26 | | */ |
27 | | |
28 | | #include <sys/types.h> |
29 | | #include <netinet/in.h> |
30 | | #include <stddef.h> |
31 | | #include <stdlib.h> |
32 | | #include <string.h> |
33 | | |
34 | | #include "rtpp_ssrc.h" |
35 | | #include "rtpa_stats.h" |
36 | | #include "rtpp_types.h" |
37 | | #include "rtpp_refcnt.h" |
38 | | #include "rtpp_log_obj.h" |
39 | | #include "rtp.h" |
40 | | #include "rtpp_time.h" |
41 | | #include "rtp_packet.h" |
42 | | #include "rtp_analyze.h" |
43 | | #include "rtpp_analyzer.h" |
44 | | #include "rtpp_analyzer_fin.h" |
45 | | #include "rtpp_mallocs.h" |
46 | | |
47 | | struct rtpp_analyzer_priv { |
48 | | struct rtpp_analyzer pub; |
49 | | struct rtpp_session_stat rstat; |
50 | | uint32_t pecount; |
51 | | uint32_t aecount; |
52 | | struct rtpp_log *log; |
53 | | }; |
54 | | |
55 | | static enum update_rtpp_stats_rval rtpp_analyzer_update(struct rtpp_analyzer *, |
56 | | struct rtp_packet *); |
57 | | static void rtpp_analyzer_get_stats(struct rtpp_analyzer *, |
58 | | struct rtpa_stats *); |
59 | | static int rtpp_analyzer_get_jstats(struct rtpp_analyzer *, |
60 | | struct rtpa_stats_jitter *); |
61 | | static void rtpp_analyzer_dtor(struct rtpp_analyzer_priv *); |
62 | | |
63 | | DEFINE_SMETHODS(rtpp_analyzer, |
64 | | .update = &rtpp_analyzer_update, |
65 | | .get_stats = &rtpp_analyzer_get_stats, |
66 | | .get_jstats = &rtpp_analyzer_get_jstats, |
67 | | ); |
68 | | |
69 | | struct rtpp_analyzer * |
70 | | rtpp_analyzer_ctor(struct rtpp_log *log) |
71 | 1 | { |
72 | 1 | struct rtpp_analyzer_priv *pvt; |
73 | 1 | struct rtpp_analyzer *rap; |
74 | | |
75 | 1 | pvt = rtpp_rzmalloc(sizeof(struct rtpp_analyzer_priv), PVT_RCOFFS(pvt)); |
76 | 1 | if (pvt == NULL) { |
77 | 0 | return (NULL); |
78 | 0 | } |
79 | 1 | rap = &pvt->pub; |
80 | 1 | if (rtpp_stats_init(&pvt->rstat) != 0) { |
81 | 0 | goto e0; |
82 | 0 | } |
83 | 1 | pvt->log = log; |
84 | 1 | RTPP_OBJ_INCREF(log); |
85 | 1 | PUBINST_FININIT(&pvt->pub, pvt, rtpp_analyzer_dtor); |
86 | 1 | return (rap); |
87 | 0 | e0: |
88 | 0 | RTPP_OBJ_DECREF(&(pvt->pub)); |
89 | 0 | free(pvt); |
90 | 0 | return (NULL); |
91 | 1 | } |
92 | | |
93 | | static enum update_rtpp_stats_rval |
94 | | rtpp_analyzer_update(struct rtpp_analyzer *rap, struct rtp_packet *pkt) |
95 | 490 | { |
96 | 490 | struct rtpp_analyzer_priv *pvt; |
97 | 490 | enum update_rtpp_stats_rval rval; |
98 | | |
99 | 490 | PUB2PVT(rap, pvt); |
100 | 490 | if (rtp_packet_parse(pkt) != RTP_PARSER_OK) { |
101 | 85 | pvt->pecount++; |
102 | 85 | return (UPDATE_ERR); |
103 | 85 | } |
104 | 405 | rval = update_rtpp_stats(pvt->log, &(pvt->rstat), &(pkt->data.header), |
105 | 405 | pkt->parsed, pkt->rtime.mono); |
106 | 405 | if (rval == UPDATE_ERR) { |
107 | 19 | pvt->aecount++; |
108 | 19 | } |
109 | 405 | pvt->rstat.last.pt = pkt->data.header.pt; |
110 | 405 | return (rval); |
111 | 490 | } |
112 | | |
113 | | static void |
114 | | rtpp_analyzer_get_stats(struct rtpp_analyzer *rap, struct rtpa_stats *rsp) |
115 | 0 | { |
116 | 0 | struct rtpp_session_stat ostat; |
117 | 0 | struct rtpp_analyzer_priv *pvt; |
118 | |
|
119 | 0 | PUB2PVT(rap, pvt); |
120 | 0 | rsp->pecount = pvt->pecount; |
121 | 0 | rsp->aecount = pvt->aecount; |
122 | 0 | memset(&ostat, '\0', sizeof(ostat)); |
123 | 0 | update_rtpp_totals(&(pvt->rstat), &ostat); |
124 | 0 | rsp->psent = ostat.psent; |
125 | 0 | rsp->precvd = ostat.precvd; |
126 | 0 | rsp->pdups = ostat.duplicates; |
127 | 0 | rsp->ssrc_changes = pvt->rstat.ssrc_changes; |
128 | 0 | rsp->last_ssrc = pvt->rstat.last.ssrc; |
129 | 0 | rsp->plost = ostat.psent - ostat.precvd; |
130 | 0 | if (pvt->rstat.last.pt != PT_UNKN) { |
131 | 0 | rsp->last_pt = pvt->rstat.last.pt; |
132 | 0 | } else { |
133 | 0 | rsp->last_pt = -1; |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | | static int |
138 | | rtpp_analyzer_get_jstats(struct rtpp_analyzer *rap, |
139 | | struct rtpa_stats_jitter *jrsp) |
140 | 0 | { |
141 | 0 | struct rtpp_analyzer_priv *pvt; |
142 | 0 | int rval; |
143 | |
|
144 | 0 | PUB2PVT(rap, pvt); |
145 | 0 | rval = get_jitter_stats(pvt->rstat.jdata, jrsp, pvt->log); |
146 | 0 | return (rval); |
147 | 0 | } |
148 | | |
149 | | static void |
150 | | rtpp_analyzer_dtor(struct rtpp_analyzer_priv *pvt) |
151 | 0 | { |
152 | |
|
153 | 0 | rtpp_analyzer_fin(&(pvt->pub)); |
154 | 0 | rtpp_stats_destroy(&pvt->rstat); |
155 | 0 | RTPP_OBJ_DECREF(pvt->log); |
156 | 0 | free(pvt); |
157 | 0 | } |