summaryrefslogtreecommitdiff
path: root/orkaudio/CapturePort.cpp
blob: 044520ffb91560420ee69cae461518e295fb98f6 (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
/*
 * 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 "CapturePort.h"
#include "Utils.h"
#include "ImmediateProcessing.h"
#include "LogManager.h"
#include "Reporting.h"
#include "ConfigManager.h"

CapturePort::CapturePort(CStdString& Id)
{
	m_Id = Id;
	m_vadBelowThresholdSec = 0.0;
	m_vadUp = false;
	m_capturing = false;
}

CStdString CapturePort::ToString()
{
	CStdString ret;
	return ret;
}

void CapturePort::AddAudioChunk(AudioChunkRef chunkRef)
{
	time_t now = time(NULL);

	if(CONFIG.m_audioSegmentation)
	{
		if (m_audioTapeRef.get())
		{
			if ((now - m_audioTapeRef->m_beginDate) >= CONFIG.m_audioSegmentDuration)
			{
				// signal current tape stop event
				CaptureEventRef eventRef(new CaptureEvent);
				eventRef->m_type = CaptureEvent::EtStop;
				eventRef->m_timestamp = now;
				AddCaptureEvent(eventRef);

				// create new tape
				m_audioTapeRef.reset(new AudioTape(m_Id));

				// signal new tape start event
				eventRef.reset(new CaptureEvent);
				eventRef->m_type = CaptureEvent::EtStart;
				eventRef->m_timestamp = now;
				AddCaptureEvent(eventRef);
			}
		}	
		else
		{
			// create new tape
			m_audioTapeRef.reset(new AudioTape(m_Id));

			// signal new tape start event
			CaptureEventRef eventRef(new CaptureEvent);
			eventRef->m_type = CaptureEvent::EtStart;
			eventRef->m_timestamp = now;
			AddCaptureEvent(eventRef);
		}
	}
	else if (CONFIG.m_vad)
	{
		if(chunkRef->GetEncoding() == PcmAudio)
		{
			if(m_vadUp)
			{
				// There is an ongoing capture
				if (chunkRef->ComputeRmsDb() < CONFIG.m_vadLowThresholdDb)
				{
					// Level has gone below low threshold, increase holdon counter
					m_vadBelowThresholdSec += chunkRef->GetDurationSec();
				}
				else
				{
					// Level has gone above low threshold, reset holdon counter
					m_vadBelowThresholdSec = 0.0;
				}

				if (m_vadBelowThresholdSec > CONFIG.m_vadHoldOnSec)
				{
					// no activity detected for more than hold on time
					m_vadUp = false;

					// signal current tape stop event
					CaptureEventRef eventRef(new CaptureEvent);
					eventRef->m_type = CaptureEvent::EtStop;
					eventRef->m_timestamp = now;
					AddCaptureEvent(eventRef);
				}
			}
			else
			{
				// No capture is taking place yet
				if (chunkRef->ComputeRmsDb() > CONFIG.m_vadHighThresholdDb)
				{
					// Voice detected, start a new capture
					m_vadBelowThresholdSec = 0.0;
					m_vadUp = true;

					// create new tape
					m_audioTapeRef.reset(new AudioTape(m_Id));

					// signal new tape start event
					CaptureEventRef eventRef(new CaptureEvent);
					eventRef->m_type = CaptureEvent::EtStart;
					eventRef->m_timestamp = now;
					AddCaptureEvent(eventRef);
				}
			}
		}
		else
		{
			LOG4CXX_ERROR(LOG.portLog, CStdString("Voice activity detection cannot be used on non PCM audio"));
		}
	}

	if (m_audioTapeRef.get() && m_capturing)
	{
		m_audioTapeRef->AddAudioChunk(chunkRef);

		// Signal to immediate processing thread that tape has new stuff
		ImmediateProcessing::GetInstance()->AddAudioTape(m_audioTapeRef);
	}
}

void CapturePort::AddCaptureEvent(CaptureEventRef eventRef)
{
	AudioTapeRef audioTapeRef = m_audioTapeRef;

	// First of all, handle tape start
	if (eventRef->m_type == CaptureEvent::EtStart)
	{
		m_capturing = true;
		if (audioTapeRef.get())
		{
			audioTapeRef->SetShouldStop();	// force stop of previous tape
		}
		audioTapeRef.reset(new AudioTape(m_Id));	// Create a new tape
		audioTapeRef->AddCaptureEvent(eventRef, false);
		//Reporting::GetInstance()->AddAudioTape(audioTapeRef);
		m_audioTapeRef = audioTapeRef;
		LOG4CXX_INFO(LOG.portLog, "#" + m_Id + ": start");
	}

	if (!audioTapeRef.get())
	{
		LOG4CXX_WARN(LOG.portLog, "#" + m_Id + ": received unexpected capture event:" 
			+ CaptureEvent::EventTypeToString(eventRef->m_type));
	}
	else
	{
		// Ok, at this point, we know we have a valid audio tape
		switch(eventRef->m_type)
		{
		case CaptureEvent::EtStop:

			m_capturing = false;
			LOG4CXX_INFO(LOG.portLog, "#" + m_Id + ": stop");
			audioTapeRef->AddCaptureEvent(eventRef, true);

			if (m_audioTapeRef->GetAudioFileRef().get())
			{
				// Notify immediate processing that tape has stopped
				ImmediateProcessing::GetInstance()->AddAudioTape(m_audioTapeRef);
				// Reporting needs to send a stop message
				Reporting::GetInstance()->AddAudioTape(audioTapeRef);
			}
			else
			{
				// Received a stop but there is no valid audio file associated with the tape
				LOG4CXX_WARN(LOG.portLog, "#" + m_Id + ": no audio reported between last start and stop");
			}
			break;
		case CaptureEvent::EtDirection:
		case CaptureEvent::EtRemoteParty:
		case CaptureEvent::EtLocalParty:
		case CaptureEvent::EtLocalEntryPoint:
		default:
			audioTapeRef->AddCaptureEvent(eventRef, false);
		}
	}
}


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

void CapturePorts::Initialize()
{
	m_ports.clear();
}

CapturePortRef CapturePorts::GetPort(CStdString & portId)
{
	std::map<CStdString, CapturePortRef>::iterator pair;

	pair = m_ports.find(portId);

	if (pair == m_ports.end())
	{
		CapturePortRef nullPortRef;
		return nullPortRef;
	}
	else
	{
		return pair->second;
	}
}

CapturePortRef CapturePorts::AddAndReturnPort(CStdString & portId)
{
	//MutexGuard mutexGuard(m_mutex);		// To make sure a channel cannot be created twice

	CapturePortRef portRef = GetPort(portId);
	if (portRef.get() == NULL)
	{
		// The port does not already exist, create it.
		CapturePortRef newPortRef(new CapturePort(portId));
		m_ports.insert(std::make_pair(portId, newPortRef));
		return newPortRef;
	}
	else
	{
		return portRef;
	}
}