summaryrefslogtreecommitdiff
path: root/orkaudio/audiocaptureplugins/voip/Rtp.cpp
blob: 49d08158f2160f0112d893e98ca1c2163e973203 (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
/*
 * 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 "Rtp.h"
#include "AudioCapturePlugin.h"
#include "AudioCapturePluginCommon.h"
#include "PacketHeaderDefs.h"
#include "assert.h"

extern "C"
{
#include "g711.h"
}
extern AudioChunkCallBackFunction g_audioChunkCallBack;


void RtpPacketInfo::ToString(CStdString& string)
{
	char sourceIp[16];
	ACE_OS::inet_ntop(AF_INET, (void*)&m_sourceIp, sourceIp, sizeof(sourceIp));
	char destIp[16];
	ACE_OS::inet_ntop(AF_INET, (void*)&m_destIp, destIp, sizeof(destIp));
	string.Format("%s,%d %s,%d seq:%u ts:%u len:%d type:%x", sourceIp, m_sourcePort, destIp, m_destPort, m_seqNum, m_timestamp, m_payloadSize, m_payloadType);
}

//==============================================================================

RtpRingBuffer::RtpRingBuffer()
{
	m_writePtr = m_buffer;
	m_readPtr = m_buffer;
	m_bufferEnd = m_buffer + NUM_SAMPLES_CIRCULAR_BUFFER;
	m_writeTimestamp = 0;
	m_readTimestamp = 0;
	m_log = Logger::getLogger("rtpringbuffer");
	m_shippedSamples = 0;
}


void RtpRingBuffer::AddRtpPacket(RtpPacketInfoRef& rtpInfo)
{
	unsigned int rtpEndTimestamp = rtpInfo->m_timestamp + rtpInfo->m_payloadSize;

	if(m_writeTimestamp == 0)
	{
		// First RTP packet of the session
		LOG4CXX_DEBUG(m_log, m_capturePort + " first packet");
		m_writeTimestamp = rtpInfo->m_timestamp;
		m_readTimestamp = m_writeTimestamp;
		StoreRtpPacket(rtpInfo);
	}
	else if (rtpInfo->m_timestamp >= m_readTimestamp)	// drop packets that are older than last shipment
	{
		if( (int)(rtpEndTimestamp - m_writeTimestamp) <= (int)FreeSpace())
		{
			// RTP packet fits into current buffer
			StoreRtpPacket(rtpInfo);

			if(UsedSpace() > NUM_SAMPLES_TRIGGER)
			{
				// We have enough stuff, make a shipment
				CreateShipment(0);
			}
		}
		else
		{
			// RTP packet does not fit into current buffer
			// work out how much silence we need to add to the current buffer when shipping
			size_t silenceSize = rtpInfo->m_timestamp - m_writeTimestamp;
			CreateShipment(silenceSize);

			// reset buffer
			m_writePtr = m_buffer;
			m_readPtr = m_buffer;
			m_writeTimestamp = rtpInfo->m_timestamp;
			m_readTimestamp = m_writeTimestamp;

			// Store new packet
			StoreRtpPacket(rtpInfo);
		}
	}
	else
	{
		LOG4CXX_DEBUG(m_log, m_capturePort + " packet too old, dropped");
	}
	CStdString debug;
	debug.Format("free:%u used:%u wr:%x rd:%x wrts:%u rdts:%d", FreeSpace(), UsedSpace(), m_writePtr, m_readPtr, m_writeTimestamp, m_readTimestamp);
	LOG4CXX_DEBUG(m_log, debug);
}

// Writes to the internal buffer without any size verification
void RtpRingBuffer::StoreRtpPacket(RtpPacketInfoRef& rtpInfo)
{
	CStdString debug;

	// 1. Silence from write pointer until end of RTP packet
	unsigned int endRtpTimestamp = rtpInfo->m_timestamp + rtpInfo->m_payloadSize;
	if (endRtpTimestamp > m_writeTimestamp)
	{
		for(int i=0; i<(endRtpTimestamp - m_writeTimestamp); i++)
		{
			*m_writePtr = 0;
			m_writePtr++;
			if(m_writePtr >= m_bufferEnd)
			{
				m_writePtr = m_buffer;
			}
		}
		int silenceSize = endRtpTimestamp - m_writeTimestamp;
		m_writeTimestamp = endRtpTimestamp;
		debug.Format("Zeroed %d samples, wr:%x wrts:%u", silenceSize, m_writePtr, m_writeTimestamp);
		LOG4CXX_DEBUG(m_log, debug);
	}

	// 2. Mix in the latest samples from this RTP packet
	unsigned int timestampDelta = m_writeTimestamp - rtpInfo->m_timestamp;
	ASSERT(timestampDelta>=0);
	short* tempWritePtr = CicularPointerSubtractOffset(m_writePtr, timestampDelta);
	unsigned char* payload = rtpInfo->m_payload;

	for(int i=0; i<rtpInfo->m_payloadSize ; i++)
	{
		if(rtpInfo->m_payloadType == RTP_PT_PCMA)
		{
			*tempWritePtr += (short)alaw2linear(payload[i]);
		}
		else if(rtpInfo->m_payloadType == RTP_PT_PCMU)
		{
			*tempWritePtr += (short)ulaw2linear(payload[i]);
		}
		tempWritePtr++;
		if(tempWritePtr >= m_bufferEnd)
		{
			tempWritePtr = m_buffer;
		}
	}
	debug.Format("Copied %d samples, tmpwr:%x", rtpInfo->m_payloadSize, tempWritePtr);
	LOG4CXX_DEBUG(m_log, debug);
}

short* RtpRingBuffer::CircularPointerAddOffset(short *ptr, size_t offset)
{
	if((ptr + offset) >= m_bufferEnd)
	{
		return m_buffer + offset - (m_bufferEnd-ptr);
	}
	else
	{
		return ptr + offset;
	}
}

short* RtpRingBuffer::CicularPointerSubtractOffset(short *ptr, size_t offset)
{
	if((ptr-offset) < m_buffer)
	{
		return m_bufferEnd - offset + (ptr-m_buffer);
	}
	else
	{
		return ptr - offset;
	}
}

void RtpRingBuffer::CreateShipment(size_t silenceSize)
{
	// 1. ship from readPtr until stop pointer or until end of buffer if wrapped
	bool bufferWrapped = false;
	short* stopPtr = NULL;
	short* wrappedStopPtr = NULL;
	if (silenceSize)
	{
		// There is additional silence to ship, do not take holdoff into account
		stopPtr = m_writePtr;
	}
	else
	{
		stopPtr = CicularPointerSubtractOffset(m_writePtr, NUM_SAMPLES_SHIPMENT_HOLDOFF);
	}

	if (stopPtr < m_readPtr)
	{
		wrappedStopPtr = stopPtr;
		stopPtr = m_bufferEnd;
		bufferWrapped = true;
	}
	size_t shortSize = stopPtr-m_readPtr;
	size_t byteSize = shortSize*2;
	AudioChunkRef chunk(new AudioChunk());
	AudioChunkDetails details;
	details.m_encoding = PcmAudio;
	chunk->SetBuffer((void*)m_readPtr, byteSize, details);
	g_audioChunkCallBack(chunk, m_capturePort);
	m_shippedSamples += shortSize;
	m_readPtr = CircularPointerAddOffset(m_readPtr ,shortSize);
	m_readTimestamp += shortSize;

	CStdString debug;
	debug.Format("Ship %d samples, rd:%x rdts:%u", shortSize, m_readPtr, m_readTimestamp);
	LOG4CXX_DEBUG(m_log, debug);


	// 2. ship from beginning of buffer until stop ptr
	if(bufferWrapped) 
	{
		shortSize = wrappedStopPtr - m_buffer;
		byteSize = shortSize*2;
		chunk.reset(new AudioChunk());
		AudioChunkDetails details;
		details.m_encoding = PcmAudio;
		chunk->SetBuffer((void*)m_buffer, byteSize, details);
		g_audioChunkCallBack(chunk, m_capturePort);
		m_shippedSamples += shortSize;
		m_readPtr = CircularPointerAddOffset(m_readPtr ,shortSize);
		m_readTimestamp += shortSize;
		debug.Format("Ship wrapped %d samples, rd:%x rdts:%u", shortSize, m_readPtr, m_readTimestamp);
		LOG4CXX_DEBUG(m_log, debug);
	}

	// 3. ship silence
	if (silenceSize)
	{
		byteSize = silenceSize*2;
		char* silenceBuffer = (char*)calloc(byteSize, 1);
		if (silenceBuffer)
		{
			AudioChunkRef chunk(new AudioChunk());
			AudioChunkDetails details;
			details.m_encoding = PcmAudio;
			chunk->SetBuffer((void*)silenceBuffer, byteSize, details);
			g_audioChunkCallBack(chunk, m_capturePort);
			m_shippedSamples += silenceSize;
			m_readPtr = CircularPointerAddOffset(m_readPtr ,silenceSize);
			m_readTimestamp += silenceSize;
		}
		debug.Format("Ship %d silence samples, rd:%x rdts:%u", silenceSize, m_readPtr, m_readTimestamp);
		LOG4CXX_DEBUG(m_log, debug);
	}
}


unsigned int RtpRingBuffer::UsedSpace()
{
	if(m_writePtr >= m_readPtr)
	{
		return m_writePtr - m_readPtr;
	}
	return NUM_SAMPLES_CIRCULAR_BUFFER + m_writePtr - m_readPtr;
}


unsigned int RtpRingBuffer::FreeSpace()
{
	return NUM_SAMPLES_CIRCULAR_BUFFER - UsedSpace();
}

void RtpRingBuffer::SetCapturePort(CStdString& port)
{
	m_capturePort = port;
}