summaryrefslogtreecommitdiff
path: root/orkaudio/audiocaptureplugins/sounddevice/SoundDevice.cpp
blob: 4252f0d4c66a59157b3dc3b6adb3473f16725979 (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
/*
 * 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 "ace/OS_NS_unistd.h"
#include "ace/Singleton.h"
#include "ace/Min_Max.h"
#include "AudioCapturePlugin.h"
#include "AudioCapturePluginCommon.h"
#include "portaudio.h"
#include "Utils.h"
#include "SoundDeviceConfig.h"

extern AudioChunkCallBackFunction g_audioChunkCallBack;
extern CaptureEventCallBackFunction g_captureEventCallBack;
extern OrkLogManager* g_logManager;

SoundDeviceConfigTopObjectRef g_soundDeviceConfigTopObjectRef;
#define DLLCONFIG g_soundDeviceConfigTopObjectRef.get()->m_config


typedef struct 
{
	PaDeviceID deviceID;
	int channelCount;
	PortAudioStream* stream;
} DeviceUserData;

int portAudioCallBack(void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData)
{
	DeviceUserData *device = (DeviceUserData *)userData;
	short * inputSamples = (short *)inputBuffer;
	CStdString portName;

	if (device->channelCount == 2)
	{
		// stereo -> split into two different chunks
		short* rightBuffer = new short[DLLCONFIG.m_audioChunkSize];
		short* leftBuffer = new short[DLLCONFIG.m_audioChunkSize];

		for (int sampleId=0; sampleId<DLLCONFIG.m_audioChunkSize ; sampleId++)
		{
			rightBuffer[sampleId] = inputSamples[2*sampleId];
			leftBuffer[sampleId] = inputSamples[2*sampleId+1];
		}
		AudioChunkRef chunkRef(new AudioChunk);
		chunkRef->SetBuffer(rightBuffer, sizeof(short)*framesPerBuffer, AudioChunk::PcmAudio);
		portName.Format("port%d-%d", device->deviceID, 1);
		g_audioChunkCallBack(chunkRef, portName);

		chunkRef.reset(new AudioChunk);
		chunkRef->SetBuffer(leftBuffer, sizeof(short)*framesPerBuffer, AudioChunk::PcmAudio);
		portName.Format("port%d-%d", device->deviceID, 2);
		g_audioChunkCallBack(chunkRef, portName);

		delete rightBuffer;
		delete leftBuffer;
	}
	else
	{
		// mono
		AudioChunkRef chunkRef(new AudioChunk);
		chunkRef->SetBuffer(inputSamples, sizeof(short)*framesPerBuffer, AudioChunk::PcmAudio);
		portName.Format("port%d", device->deviceID);
		g_audioChunkCallBack(chunkRef, portName);
	}

	return 0;
}


class SoundDevice
{
public:
	SoundDevice();
	void Initialize();
	void Run();
	void StartCapture(CStdString& port);
	void StopCapture(CStdString& port);
private:
	DeviceUserData** m_devices;
	int m_deviceCount;
	PaStream* m_stream;
};

typedef ACE_Singleton<SoundDevice, ACE_Thread_Mutex> SoundDeviceSingleton;

SoundDevice::SoundDevice()
{
	m_deviceCount = 0;
	m_devices = NULL;
}

void Configure(DOMNode* node)
{
	if (node)
	{
		SoundDeviceConfigTopObjectRef soundDeviceConfigTopObjectRef(new SoundDeviceConfigTopObject);
		try
		{
			soundDeviceConfigTopObjectRef.get()->DeSerializeDom(node);
			g_soundDeviceConfigTopObjectRef = soundDeviceConfigTopObjectRef;
		}
		catch (CStdString& e)
		{
			LOG4CXX_WARN(g_logManager->rootLog, "SoundDevice.dll: " + e + " - using defaults");
		}
	}
	else
	{
		LOG4CXX_WARN(g_logManager->rootLog, "SoundDevice.dll: got empty DOM tree");
	}
}


void SoundDevice::Initialize()
{
	LOG4CXX_INFO(g_logManager->rootLog, "Initializing Sound Device plugin");

	// create a default config object in case it was not properly initialized by Configure
	if(!g_soundDeviceConfigTopObjectRef.get())
	{
		g_soundDeviceConfigTopObjectRef.reset(new SoundDeviceConfigTopObject);
	}


	PaError result = Pa_Initialize();
	if (result)
	{
		LOG4CXX_ERROR(g_logManager->rootLog, "Could not initialize Sound Device plugin");
	}

	m_deviceCount = Pa_CountDevices();
	m_devices = new DeviceUserData*[m_deviceCount];

	for( PaDeviceID deviceID=0; deviceID<m_deviceCount; deviceID++ )
	{
		const PaDeviceInfo* deviceInfo = Pa_GetDeviceInfo( deviceID );

		m_devices[deviceID] = new DeviceUserData;
		m_devices[deviceID]->deviceID = deviceID;
		m_devices[deviceID]->channelCount = ace_max(deviceInfo->maxInputChannels,2);
		m_devices[deviceID]->stream = NULL;
		CStdString deviceName(deviceInfo->name);

		if (deviceInfo->maxInputChannels > 0 && deviceName.Find("Primary") == -1)	// Primary audio device is a duplicate of one of the devices
		{
			CStdString capturePorts;
			if (deviceInfo->maxInputChannels > 1)
			{
				capturePorts.Format("port%d-1, port%d-2", deviceID, deviceID);	// stereo
			}
			else
			{
				capturePorts.Format("port%d-1", deviceID);	// mono
			}

			CStdString maxInputChannelsString = IntToString(deviceInfo->maxInputChannels);
			LOG4CXX_INFO(g_logManager->rootLog, "Ports:" + capturePorts + " Name:" + deviceName + " Channels:" + maxInputChannelsString);

			result =  Pa_OpenStream(	&m_devices[deviceID]->stream,
										deviceID,
										m_devices[deviceID]->channelCount,
										paInt16,
										NULL,
										paNoDevice ,
										0,
										paInt16,
										NULL,
										8000.0,
										DLLCONFIG.m_audioChunkSize,
										0,
										0,
										portAudioCallBack,
										(void*)m_devices[deviceID] );

			if (result)
			{
				CStdString deviceIdString = IntToString(deviceID);
				LOG4CXX_ERROR(g_logManager->rootLog, "Device:" + deviceIdString + CStdString(" Pa_OpenStream error:") + Pa_GetErrorText(result));
			}
		}
	}
}

void SoundDevice::Run()
{
	for( PaDeviceID deviceID=0; deviceID<m_deviceCount; deviceID++ )
	{
		if (m_devices[deviceID]->channelCount > 0 && m_devices[deviceID]->stream)
		{
			PaError result = Pa_StartStream(m_devices[deviceID]->stream);
			if (result)
			{
				CStdString deviceIdString = IntToString(deviceID);
				LOG4CXX_ERROR(g_logManager->rootLog, "Device:" + deviceIdString + CStdString(" Pa_StartStream error:") + Pa_GetErrorText(result));
			}
		}
	}
}

void SoundDevice::StartCapture(CStdString& port)
{
	CaptureEventRef startEvent(new CaptureEvent);
	startEvent->m_type = CaptureEvent::EtStart;
	startEvent->m_timestamp = time(NULL);
	g_captureEventCallBack(startEvent, port);
}

void SoundDevice::StopCapture(CStdString& port)
{
	CaptureEventRef stopEvent(new CaptureEvent);
	stopEvent->m_type = CaptureEvent::EtStop;
	stopEvent->m_timestamp = time(NULL);
	g_captureEventCallBack(stopEvent, port);
}

void __CDECL__ Initialize()
{
	SoundDeviceSingleton::instance()->Initialize();
}

void __CDECL__ Run()
{
	SoundDeviceSingleton::instance()->Run();
	for(;;)
	{
		// if this idle thread is missing, it will crash winmm
		ACE_OS::sleep(5);
	}
}

void __CDECL__ StartCapture(CStdString& capturePort)
{
	SoundDeviceSingleton::instance()->StartCapture(capturePort);
}

void __CDECL__ StopCapture(CStdString& capturePort)
{
	SoundDeviceSingleton::instance()->StopCapture(capturePort);
}