File: cdr.c

package info (click to toggle)
rtpengine 13.5.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,676 kB
  • sloc: ansic: 86,764; perl: 59,422; python: 3,193; sh: 1,030; makefile: 693; asm: 211
file content (184 lines) | stat: -rw-r--r-- 6,103 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "cdr.h"

#include <inttypes.h>

#include "rtplib.h"
#include "call.h"
#include "poller.h"
#include "str.h"

#define CDRBUFREMAINDER cdrbufend-cdrbufcur

static const char * const __term_reason_texts[] = {
	[TIMEOUT] = "TIMEOUT",
	[REGULAR] = "REGULAR",
	[FORCED] = "FORCED",
	[SILENT_TIMEOUT] = "SILENT_TIMEOUT",
	[FINAL_TIMEOUT] = "FINAL_TIMEOUT",
	[OFFER_TIMEOUT] = "OFFER_TIMEOUT",
};
static const char * const __tag_type_texts[] = {
	[FROM_TAG] = "FROM_TAG",
	[TO_TAG] = "TO_TAG",
};
const char * get_tag_type_text(enum tag_type t) {
	return get_enum_array_text(__tag_type_texts, t, "UNKNOWN");
}
const char *get_opmode_text(enum ng_opmode m) {
	return get_enum_array_text(ng_command_strings, m, "other");
}

static const char * get_term_reason_text(enum termination_reason t) {
	return get_enum_array_text(__term_reason_texts, t, "UNKNOWN");
}

void cdr_update_entry(call_t * c) {
	struct call_monologue *ml;
	int64_t tim_result_duration;
	int cdrlinecnt = 0;
	g_autoptr(GString) cdr = g_string_new("");
	struct call_media *md;
	const rtp_payload_type *rtp_pt;
	struct packet_stream *ps=0;

	if (!IS_OWN_CALL(c))
		return;

	/* CDRs and statistics */
	if (_log_facility_cdr) {
		g_string_append_printf(cdr, "ci=%s, ",c->callid.s);
		g_string_append_printf(cdr, "created_from=" STR_FORMAT ", ", STR_FMT(&c->created_from));
		g_string_append_printf(cdr, "last_signal=%" PRId64 ", ", c->last_signal_us / 1000000L);
		g_string_append_printf(cdr, "tos=%u, ", (unsigned int)c->tos);
	}

	for (__auto_type l = c->monologues.head; l; l = l->next) {
		ml = l->data;

		if (!ml->terminated) {
			ml->terminated = rtpe_now;
			ml->term_reason = UNKNOWN;
		}

		tim_result_duration = ml->terminated - ml->started;

		if (_log_facility_cdr) {
			g_string_append_printf(cdr,
				"ml%i_start_time=%" PRId64 ".%06" PRId64 ", "
				"ml%i_end_time=%" PRId64 ".%06" PRId64 ", "
				"ml%i_duration=%" PRId64 ".%06" PRId64 ", "
				"ml%i_termination=%s, "
				"ml%i_local_tag=%s, "
				"ml%i_local_tag_type=%s, ",
				cdrlinecnt, ml->started / 1000000, ml->started % 1000000,
				cdrlinecnt, ml->terminated / 1000000, ml->terminated % 1000000,
				cdrlinecnt, tim_result_duration / 1000000, tim_result_duration % 1000000,
				cdrlinecnt, get_term_reason_text(ml->term_reason),
				cdrlinecnt, ml->tag.s,
				cdrlinecnt, get_tag_type_text(ml->tagtype));

			g_auto(GQueue) mls = G_QUEUE_INIT; /* to avoid duplications */
			for (int i = 0; i < ml->medias->len; i++)
			{
				struct call_media * media = ml->medias->pdata[i];
				if (!media)
					continue;

				for (__auto_type sub = media->media_subscriptions.head; sub; sub = sub->next)
				{
					struct media_subscription * ms = sub->data;
					if (!g_queue_find(&mls, ms->monologue)) {
						g_string_append_printf(cdr, "ml%i_remote_tag=%s, ", cdrlinecnt, ms->monologue->tag.s);
						g_queue_push_tail(&mls, ms->monologue);
					}
				}
			}
		}

		for (unsigned int i = 0; i < ml->medias->len; i++) {
			md = ml->medias->pdata[i];
			if (!md)
				continue;

			rtp_pt = __rtp_stats_codec(md);

			/* add PayloadType(codec) info in CDR logging */
			if (_log_facility_cdr && rtp_pt) {
				g_string_append_printf(cdr, "payload_type=%u, ", rtp_pt->payload_type);
			} else if (_log_facility_cdr && !rtp_pt) {
				g_string_append_printf(cdr, "payload_type=unknown, ");
			}

			for (__auto_type o = md->streams.head; o; o = o->next) {
				ps = o->data;

				if (PS_ISSET(ps, FALLBACK_RTCP))
					continue;

				char *addr = sockaddr_print_buf(&ps->endpoint.address);
				char *local_addr = sockaddr_print_buf(&ps->last_local_endpoint.address);

				if (_log_facility_cdr) {
				    const char* protocol = (!PS_ISSET(ps, RTP) && PS_ISSET(ps, RTCP)) ? "rtcp" : "rtp";

				    if(!PS_ISSET(ps, RTP) && PS_ISSET(ps, RTCP)) {
					g_string_append_printf(cdr,
						"ml%i_midx%u_%s_endpoint_ip=%s, "
						"ml%i_midx%u_%s_endpoint_port=%u, "
						"ml%i_midx%u_%s_local_relay_ip=%s, "
						"ml%i_midx%u_%s_local_relay_port=%u, "
						"ml%i_midx%u_%s_relayed_packets=%" PRIu64 ", "
						"ml%i_midx%u_%s_relayed_bytes=%" PRIu64 ", "
						"ml%i_midx%u_%s_relayed_errors=%" PRIu64 ", "
						"ml%i_midx%u_%s_last_packet=%" PRIu64 ", "
						"ml%i_midx%u_%s_in_tos_tclass=%" PRIu8 ", ",
						cdrlinecnt, md->index, protocol, addr,
						cdrlinecnt, md->index, protocol, ps->endpoint.port,
						cdrlinecnt, md->index, protocol, local_addr,
						cdrlinecnt, md->index, protocol, ps->last_local_endpoint.port,
						cdrlinecnt, md->index, protocol,
						atomic64_get_na(&ps->stats_in->packets),
						cdrlinecnt, md->index, protocol,
						atomic64_get_na(&ps->stats_in->bytes),
						cdrlinecnt, md->index, protocol,
						atomic64_get_na(&ps->stats_in->errors),
						cdrlinecnt, md->index, protocol,
						packet_stream_last_packet(ps) / 1000000L,
						cdrlinecnt, md->index, protocol,
						atomic_get_na(&ps->stats_in->tos));
				    } else {
					g_string_append_printf(cdr,
						"ml%i_midx%u_%s_endpoint_ip=%s, "
						"ml%i_midx%u_%s_endpoint_port=%u, "
						"ml%i_midx%u_%s_local_relay_ip=%s, "
						"ml%i_midx%u_%s_local_relay_port=%u, "
						"ml%i_midx%u_%s_relayed_packets=%" PRIu64 ", "
						"ml%i_midx%u_%s_relayed_bytes=%" PRIu64 ", "
						"ml%i_midx%u_%s_relayed_errors=%" PRIu64 ", "
						"ml%i_midx%u_%s_last_packet=%" PRIu64 ", "
						"ml%i_midx%u_%s_in_tos_tclass=%" PRIu8 ", ",
						cdrlinecnt, md->index, protocol, addr,
						cdrlinecnt, md->index, protocol, ps->endpoint.port,
						cdrlinecnt, md->index, protocol, local_addr,
						cdrlinecnt, md->index, protocol, ps->last_local_endpoint.port,
						cdrlinecnt, md->index, protocol,
						atomic64_get_na(&ps->stats_in->packets),
						cdrlinecnt, md->index, protocol,
						atomic64_get_na(&ps->stats_in->bytes),
						cdrlinecnt, md->index, protocol,
						atomic64_get_na(&ps->stats_in->errors),
						cdrlinecnt, md->index, protocol,
						packet_stream_last_packet(ps) / 1000000L,
						cdrlinecnt, md->index, protocol,
						atomic_get_na(&ps->stats_in->tos));
				    }
				}

			}
		}
		if (_log_facility_cdr)
		    ++cdrlinecnt;
	}
	/* log it */
	cdrlog(cdr->str);
}