summaryrefslogtreecommitdiff
path: root/orkbasecxx/CapturePluginProxy.cpp
blob: 67c8395366ca5e4cb4d9833c45679ef38de511c3 (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
/*
 * 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
 *
 */

#include "CapturePluginProxy.h"
#include "ace/OS_NS_dirent.h"
#include "ace/OS_NS_string.h"
#include "ace/Thread_Manager.h"
#include "ConfigManager.h"
#include "CapturePort.h"

CapturePluginProxy* CapturePluginProxy::m_singleton;	

CapturePluginProxy::CapturePluginProxy()
{
	m_configureFunction = NULL;
	m_registerCallBacksFunction = NULL;
	m_initializeFunction = NULL;
	m_runFunction = NULL;
	m_startCaptureFunction = NULL;
	m_stopCaptureFunction = NULL;

	m_loaded = false;
}

CapturePluginProxy* CapturePluginProxy::Singleton()
{
	return m_singleton;
}

bool CapturePluginProxy::Initialize()
{
	m_singleton = new CapturePluginProxy();
	return m_singleton->Init();
}

bool CapturePluginProxy::Init()
{
	// Get the desired capture plugin from the config file, or else, use the first dll encountered.
	CStdString pluginDirectory = CONFIG.m_capturePluginPath + "/";
	CStdString pluginPath;
	if (!CONFIG.m_capturePlugin.IsEmpty())
	{
		// A specific plugin was specified in the config file
		pluginPath = pluginDirectory + CONFIG.m_capturePlugin;
	}
	else
	{
		// No plugin specified, find the first one in the plugin directory
		ACE_DIR* dir = ACE_OS::opendir((PCSTR)pluginDirectory);
		if (!dir)
		{
			LOG4CXX_ERROR(LOG.rootLog, CStdString("Capture plugin directory could not be found:" + pluginDirectory));
		}
		else
		{
			dirent* dirEntry = NULL;
			bool found = false;
			bool done = false;
			while(!found && !done)
			{	
				dirEntry = ACE_OS::readdir(dir);
				if(dirEntry)
				{
					if (ACE_OS::strstr(dirEntry->d_name, ".dll"))
					{
						found = true;
						done = true;
						pluginPath = pluginDirectory + dirEntry->d_name;
					}
				}
				else
				{
					done = true;
				}
			}
			ACE_OS::closedir(dir);
		}
	}
	if (!pluginPath.IsEmpty())
	{
		m_dll.open((PCSTR)pluginPath);
		ACE_TCHAR* error = m_dll.error();
#ifndef WIN32
		char *errorstr;
#endif
		if(error)
		{
#ifndef WIN32
			errorstr = dlerror();
			CStdString errorcstds(errorstr);
			LOG4CXX_ERROR(LOG.rootLog, CStdString("Failed to load the following plugin: ") + pluginPath + " " + errorcstds);
#else
			LOG4CXX_ERROR(LOG.rootLog, CStdString("Failed to load the following plugin: ") + pluginPath);
#endif
		}
		else
		{
			// Ok, the dll has been successfully loaded
			LOG4CXX_INFO(LOG.rootLog, CStdString("Loaded plugin: ") + pluginPath);

			RegisterCallBacksFunction registerCallBacks;
			registerCallBacks = (RegisterCallBacksFunction)m_dll.symbol("RegisterCallBacks");
			registerCallBacks(AudioChunkCallBack, CaptureEventCallBack, OrkLogManager::Instance());

			m_configureFunction = (ConfigureFunction)m_dll.symbol("Configure");
			if (m_configureFunction)
			{
				ConfigManager::Instance()->AddConfigureFunction(m_configureFunction);

				m_initializeFunction = (InitializeFunction)m_dll.symbol("Initialize");
				if (m_initializeFunction)
				{
					m_initializeFunction();

					m_runFunction = (RunFunction)m_dll.symbol("Run");
					if (m_runFunction)
					{
						m_startCaptureFunction = (StartCaptureFunction)m_dll.symbol("StartCapture");
						if (m_startCaptureFunction)
						{
							m_stopCaptureFunction = (StopCaptureFunction)m_dll.symbol("StopCapture");
							if (m_stopCaptureFunction)
							{
								m_pauseCaptureFunction = (PauseCaptureFunction)m_dll.symbol("PauseCapture");
								if(m_pauseCaptureFunction)
								{
									m_setOnHoldFunction = (SetOnHoldFunction)m_dll.symbol("SetOnHold");
									if(m_setOnHoldFunction)
									{
										m_setOffHoldFunction = (SetOnHoldFunction)m_dll.symbol("SetOffHold");
										if(m_setOffHoldFunction)
										{
											m_loaded = true;
										}
										else
										{
											LOG4CXX_ERROR(LOG.rootLog, CStdString("Could not find SetOffHold function in ") + pluginPath);
										}
									}
									else
									{
										LOG4CXX_ERROR(LOG.rootLog, CStdString("Could not find SetOnHold function in ") + pluginPath);
									}
								}
								else
								{
									LOG4CXX_ERROR(LOG.rootLog, CStdString("Could not find PauseCapture function in ") + pluginPath);
								}
							}
							else
							{
								LOG4CXX_ERROR(LOG.rootLog, CStdString("Could not find StopCapture function in ") + pluginPath);
							}
						}
						else
						{
							LOG4CXX_ERROR(LOG.rootLog, CStdString("Could not find StartCapture function in ") + pluginPath);
						}
					}
					else
					{
						LOG4CXX_ERROR(LOG.rootLog, CStdString("Could not find Run function in ") + pluginPath);
					}
				}
				else
				{
					LOG4CXX_ERROR(LOG.rootLog, CStdString("Could not find Initialize function in ") + pluginPath);
				}
			}
			else
			{
				LOG4CXX_ERROR(LOG.rootLog, CStdString("Could not find Configure function in ") + pluginPath);
			}
		}
	}
	else
	{
		LOG4CXX_ERROR(LOG.rootLog, CStdString("Failed to find any capture plugin in: ") + pluginDirectory);
	}

	return m_loaded;
}

void CapturePluginProxy::Run()
{
	if (!ACE_Thread_Manager::instance()->spawn(ACE_THR_FUNC(m_runFunction)))
	{
		LOG4CXX_INFO(LOG.rootLog, CStdString("Failed to create capture thread"));
	}
}

void CapturePluginProxy::Shutdown()
{
	ShutdownFunction shutdownFunction = (ShutdownFunction)m_dll.symbol("Shutdown");
	if (shutdownFunction)
	{
		LOG4CXX_INFO(LOG.rootLog, CStdString("Shutting down"));
		shutdownFunction();
	}
	else
	{
		LOG4CXX_INFO(LOG.rootLog, CStdString("Could not find DLL Shutdown function"));
	}
}

void CapturePluginProxy::StartCapture(CStdString& party, CStdString& orkuid)
{
	if(m_loaded)
	{
		m_startCaptureFunction(party, orkuid);
	}
	else
	{
		throw(CStdString("StartCapture: Capture plugin not yet loaded"));
	}
}

void CapturePluginProxy::StopCapture(CStdString& party)
{
	if(m_loaded)
	{
		m_stopCaptureFunction(party);
	}
	else
	{
		throw(CStdString("StopCapture: Capture plugin not yet loaded"));
	}
}

void CapturePluginProxy::PauseCapture(CStdString& party, CStdString& orkuid)
{
	if(m_loaded)
	{
		m_pauseCaptureFunction(party, orkuid);
	}
	else
	{
		throw(CStdString("PauseCapture: Capture plugin not yet loaded"));
	}
}

void __CDECL__  CapturePluginProxy::AudioChunkCallBack(AudioChunkRef chunkRef, CStdString& capturePort)
{
	// find the right port and give it the audio chunk
	CapturePortRef portRef = CapturePortsSingleton::instance()->AddAndReturnPort(capturePort);
	portRef->AddAudioChunk(chunkRef);
}

void __CDECL__ CapturePluginProxy::CaptureEventCallBack(CaptureEventRef eventRef, CStdString& capturePort)
{
	if(CONFIG.m_vad || CONFIG.m_audioSegmentation)
	{
		if (eventRef->m_type == CaptureEvent::EtStart || eventRef->m_type == CaptureEvent::EtStop)
		{
			if(CONFIG.m_audioSegmentation == true)
			{
				// find the right port and give it the event
				// If this is EtStop, we clear the event cache
				CapturePortRef portRef = CapturePortsSingleton::instance()->AddAndReturnPort(capturePort);
				if(eventRef->m_type == CaptureEvent::EtStop)
				{
					portRef->ClearEventQueue();
				}
				portRef->AddCaptureEvent(eventRef);
			}
			else
			{
				LOG4CXX_ERROR(LOG.portLog, "#" + capturePort + ": received start or stop while in VAD mode");
			}
		}
		else
		{
			if(CONFIG.m_audioSegmentation == true)
			{
				// find the right port and give it the event
				CapturePortRef portRef = CapturePortsSingleton::instance()->AddAndReturnPort(capturePort);
				portRef->QueueCaptureEvent(eventRef);
				portRef->AddCaptureEvent(eventRef);
			}
		}
	}
	else
	{
		// find the right port and give it the event
		CapturePortRef portRef = CapturePortsSingleton::instance()->AddAndReturnPort(capturePort);
		portRef->AddCaptureEvent(eventRef);
	}
}