summaryrefslogtreecommitdiff
path: root/orkaudio/audiocaptureplugins/voip/SipSession.cpp
blob: 591175cba7e82d313523330215eb99ca3405fdcf (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

#define _WINSOCKAPI_		// prevents the inclusion of winsock.h

#include "Utils.h"
#include "SipSession.h"
#include "AudioCapturePlugin.h"
#include "AudioCapturePluginCommon.h"
#include <list>
#include "VoIpConfig.h"
#include "ace/OS_NS_arpa_inet.h"

extern AudioChunkCallBackFunction g_audioChunkCallBack;
extern CaptureEventCallBackFunction g_captureEventCallBack;

extern VoIpConfigTopObjectRef g_VoIpConfigTopObjectRef;
#define DLLCONFIG g_VoIpConfigTopObjectRef.get()->m_config


SipSession::SipSession()
{
	m_lastUpdated = time(NULL);
	m_log = Logger::getLogger("sipsession");
	m_invitorIp.s_addr = 0;
	m_invitorTcpPort = 0;
	m_inviteeIp.s_addr = 0;
	m_inviteeTcpPort = 0;
	m_direction = CaptureEvent::DirUnkn;
	m_protocol = ProtocolEnum::ProtUnkn;
	m_numRtpPackets = 0;
	m_started = false;
}

void SipSession::Stop()
{
	LOG4CXX_DEBUG(m_log, m_capturePort + " Session stop");
	CaptureEventRef stopEvent(new CaptureEvent);
	stopEvent->m_type = CaptureEvent::EtStop;
	stopEvent->m_timestamp = time(NULL);
	g_captureEventCallBack(stopEvent, m_capturePort);
}

void SipSession::Start()
{
	m_started = true;
	LOG4CXX_DEBUG(m_log, m_capturePort + " " + ProtocolToString(m_protocol) + " Session start");
	m_rtpRingBuffer.SetCapturePort(m_capturePort);
	CaptureEventRef startEvent(new CaptureEvent);
	startEvent->m_type = CaptureEvent::EtStart;
	startEvent->m_timestamp = time(NULL);
	g_captureEventCallBack(startEvent, m_capturePort);
}

void SipSession::ProcessMetadataSipIncoming()
{
	m_remoteParty = m_invite->m_from;
	m_localParty = m_invite->m_to;
	m_direction = CaptureEvent::DirIn;
	m_capturePort.Format("%s,%d", ACE_OS::inet_ntoa(m_inviteeIp), m_inviteeTcpPort);
}

void SipSession::ProcessMetadataSipOutgoing()
{
	m_remoteParty = m_invite->m_to;
	m_localParty = m_invite->m_from;
	m_direction = CaptureEvent::DirOut;
	m_capturePort.Format("%s,%d", ACE_OS::inet_ntoa(m_invitorIp), m_invitorTcpPort);
}

void SipSession::ProcessMetadataRawRtp(RtpPacketInfoRef& rtpPacket)
{
	bool sourceIsLocal = true;

	if(DLLCONFIG.IsMediaGateway(rtpPacket->m_sourceIp))
	{
		if(DLLCONFIG.IsMediaGateway(rtpPacket->m_destIp))
		{
			// media gateway to media gateway
			sourceIsLocal = false;
		}
		else if (DLLCONFIG.IsPartOfLan(rtpPacket->m_destIp))
		{
			// Media gateway to internal
			sourceIsLocal = false;
		}
		else
		{
			// Media gateway to external
			sourceIsLocal = true;
		}
	}
	else if (DLLCONFIG.IsPartOfLan(rtpPacket->m_sourceIp))
	{
		// source address is internal
		sourceIsLocal = true;
	}
	else
	{
		// source address is external
		sourceIsLocal = false;
	}

	if(sourceIsLocal)
	{
		m_localParty.Format("%s", ACE_OS::inet_ntoa(rtpPacket->m_sourceIp));
		m_remoteParty.Format("%s", ACE_OS::inet_ntoa(rtpPacket->m_destIp));
		m_capturePort.Format("%s,%d", ACE_OS::inet_ntoa(rtpPacket->m_sourceIp), rtpPacket->m_sourcePort);
	}
	else
	{
		m_localParty.Format("%s", ACE_OS::inet_ntoa(rtpPacket->m_destIp));
		m_remoteParty.Format("%s", ACE_OS::inet_ntoa(rtpPacket->m_sourceIp));
		m_capturePort.Format("%s,%d", ACE_OS::inet_ntoa(rtpPacket->m_destIp), rtpPacket->m_destPort);
	}
}

void SipSession::ProcessMetadataSip(RtpPacketInfoRef& rtpPacket)
{
	bool done = false;

	// work out invitee IP address
	if(rtpPacket->m_sourceIp.s_addr == m_invitorIp.s_addr)
	{
		m_inviteeIp = rtpPacket->m_destIp;
		m_inviteeTcpPort = rtpPacket->m_destPort;
		m_invitorTcpPort = rtpPacket->m_sourcePort;
	}
	else if(rtpPacket->m_destIp.s_addr == m_invitorIp.s_addr)
	{
		m_inviteeIp = rtpPacket->m_sourceIp;
		m_inviteeTcpPort = rtpPacket->m_sourcePort;
		m_invitorTcpPort = rtpPacket->m_destPort;
	}
	else
	{
		LOG4CXX_DEBUG(m_log, m_ipAndPort + " alien RTP packet");
	}

	// work out capture port and direction
	if(DLLCONFIG.IsMediaGateway(m_invitorIp))
	{
		if(DLLCONFIG.IsMediaGateway(m_inviteeIp))
		{
			// dismiss
		}
		if(DLLCONFIG.IsPartOfLan(m_inviteeIp))
		{
			ProcessMetadataSipIncoming();
		}	
	}
	else if (DLLCONFIG.IsPartOfLan(m_invitorIp))
	{
		ProcessMetadataSipOutgoing();
	}
	else
	{
		// SIP invitor IP address is an outside IP address
		if(DLLCONFIG.IsMediaGateway(m_inviteeIp))
		{
			// dismiss
		}
		else if(DLLCONFIG.IsPartOfLan(m_inviteeIp))
		{
			ProcessMetadataSipIncoming();
		}
		else
		{
			// SIP invitee IP address is an outside IP address
			ProcessMetadataSipOutgoing();
		}
	}
}

void SipSession::ReportMetadata()
{
	// report Local party
	CaptureEventRef event(new CaptureEvent());
	event->m_type = CaptureEvent::EtLocalParty;
	event->m_value = m_localParty;
	g_captureEventCallBack(event, m_capturePort);

	// Report remote party
	event.reset(new CaptureEvent());
	event->m_type = CaptureEvent::EtRemoteParty;
	event->m_value = m_remoteParty;
	g_captureEventCallBack(event, m_capturePort);

	// report direction
	event.reset(new CaptureEvent());
	event->m_type = CaptureEvent::EtDirection;
	event->m_value = CaptureEvent::DirectionToString(m_direction);
	g_captureEventCallBack(event, m_capturePort);
}


void SipSession::AddRtpPacket(RtpPacketInfoRef& rtpPacket)
{
	// if first RTP packet, start session
	if(m_lastRtpPacket.get() == NULL)
	{
		if(m_protocol == ProtRawRtp)
		{
			ProcessMetadataRawRtp(rtpPacket);
		}
		else if(m_protocol == ProtSip)
		{
			ProcessMetadataSip(rtpPacket);
			Start();
			ReportMetadata();
		}
	}
	m_lastRtpPacket = rtpPacket;
	m_numRtpPackets++;

	if(m_protocol == ProtRawRtp && m_numRtpPackets == 50)
	{
		// We've got 50 RTP packets, this should be a "real" raw RTP session, not a leftover from a SIP session
		Start();
		ReportMetadata();
	}

	// send audio buffer
	//AudioChunkRef chunkRef(new AudioChunk);
	//chunkRef->SetBuffer(rtpPacket->m_payload, rtpPacket->m_payloadSize, AudioChunk::AlawAudio);
	//g_audioChunkCallBack(chunkRef, m_ipAndPort);
	
	if(m_started)
	{
		m_rtpRingBuffer.AddRtpPacket(rtpPacket);
		m_lastUpdated = time(NULL);
	}
}


void SipSession::ReportSipInvite(SipInviteInfoRef& invite)
{
	m_invite = invite;
	m_invitorIp = invite->m_fromIp;
}

int SipSession::ProtocolToEnum(CStdString& protocol)
{
	int protocolEnum = ProtUnkn;
	if(protocol.CompareNoCase(PROT_RAW_RTP) == 0)
	{
		protocolEnum = ProtRawRtp;
	}
	else if (protocol.CompareNoCase(PROT_SIP) == 0)
	{
		protocolEnum = ProtSip;
	}
	return protocolEnum;
}

CStdString SipSession::ProtocolToString(int protocolEnum)
{
	CStdString protocolString;
	switch (protocolEnum)
	{
	case ProtRawRtp:
		protocolString = PROT_RAW_RTP;
		break;
	case ProtSip:
		protocolString = PROT_SIP;
		break;
	default:
		protocolString = PROT_UNKN;
	}
	return protocolString;
}

//=====================================================================
SipSessions::SipSessions()
{
	m_log = Logger::getLogger("sipsessions");
}


void SipSessions::ReportSipInvite(SipInviteInfoRef& invite)
{
	CStdString key = CStdString(ACE_OS::inet_ntoa(invite->m_fromIp)) + "," + invite->m_fromRtpPort;
	std::map<CStdString, SipSessionRef>::iterator pair;
	
	pair = m_byIpAndPort.find(key);

	if (pair != m_byIpAndPort.end())
	{
		// A session exists ont the same IP+port, stop old session
		SipSessionRef session = pair->second;
		Stop(session);
	}
	// create new session and insert into both maps
	SipSessionRef session(new SipSession());
	session->m_ipAndPort = key;
	session->m_protocol = SipSession::ProtSip;
	session->ReportSipInvite(invite);
	m_byCallId.insert(std::make_pair(invite->m_callId, session));
	m_byIpAndPort.insert(std::make_pair(key, session));
}

void SipSessions::ReportSipBye(SipByeInfo bye)
{
	std::map<CStdString, SipSessionRef>::iterator pair;
	pair = m_byCallId.find(bye.m_callId);

	if (pair != m_byCallId.end())
	{
		// Session found: stop it
		SipSessionRef session = pair->second;
		Stop(session);
	}
}

void SipSessions::Stop(SipSessionRef& session)
{
	session->Stop();
	m_byIpAndPort.erase(session->m_ipAndPort);
	if(session->m_invite.get() != NULL)
	{
		m_byCallId.erase(session->m_invite->m_callId);
	}
}


void SipSessions::ReportRtpPacket(RtpPacketInfoRef& rtpPacket)
{
	// Does a session exist with this source Ip+Port
	SipSessionRef session;
	CStdString port = IntToString(rtpPacket->m_sourcePort);
	CStdString ipAndPort = CStdString(ACE_OS::inet_ntoa(rtpPacket->m_sourceIp)) + "," + port;
	std::map<CStdString, SipSessionRef>::iterator pair;

	pair = m_byIpAndPort.find(ipAndPort);
	if (pair != m_byIpAndPort.end())
	{
		session = pair->second;
	}	
	else
	{
		// Does a session exist with this destination Ip+Port
		port = IntToString(rtpPacket->m_destPort);
		ipAndPort = CStdString(ACE_OS::inet_ntoa(rtpPacket->m_destIp)) + "," + port;
		pair = m_byIpAndPort.find(ipAndPort);
		if (pair != m_byIpAndPort.end())
		{
			session = pair->second;
		}
		else
		{
			// create new Raw RTP session and insert into IP+Port map
			SipSessionRef session(new SipSession());
			session->m_protocol = SipSession::ProtRawRtp;
			session->m_ipAndPort = ipAndPort;
			m_byIpAndPort.insert(std::make_pair(ipAndPort, session));
		}
	}
	if (!session.get() == NULL)
	{
		// Found a session give it the RTP packet info
		session->AddRtpPacket(rtpPacket);
	}
}

void SipSessions::Hoover(time_t now)
{
	CStdString numSessions = IntToString(m_byIpAndPort.size());
	LOG4CXX_DEBUG(m_log, "Hoover - check " + numSessions + " sessions time:" + IntToString(now));

	// Go round the sessions and find inactive ones
	std::map<CStdString, SipSessionRef>::iterator pair;
	std::list<SipSessionRef> toDismiss;

	for(pair = m_byIpAndPort.begin(); pair != m_byIpAndPort.end(); pair++)
	{
		SipSessionRef session = pair->second;
		if((now - session->m_lastUpdated) > 10)
		{
			toDismiss.push_back(session);
		}
	}

	// discard inactive sessions
	for (std::list<SipSessionRef>::iterator it = toDismiss.begin(); it != toDismiss.end() ; it++)
	{
		SipSessionRef session = *it;
		LOG4CXX_DEBUG(m_log, session->m_ipAndPort + " Expired");
		Stop(session);
	}
}