2013-02-16 01:53:32 +04:00
|
|
|
#include "wf_directsound.h"
|
|
|
|
#include "wf_interface.h"
|
|
|
|
#include "wf_info.h"
|
|
|
|
#include "wf_rdpsnd.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <winpr/windows.h>
|
|
|
|
|
|
|
|
#define INITGUID
|
|
|
|
#include <initguid.h>
|
|
|
|
#include <objbase.h>
|
|
|
|
|
|
|
|
#define CINTERFACE 1
|
|
|
|
#include <mmsystem.h>
|
|
|
|
#include <dsound.h>
|
|
|
|
|
2014-09-12 19:38:12 +04:00
|
|
|
#include <freerdp/log.h>
|
|
|
|
#define TAG SERVER_TAG("windows")
|
|
|
|
|
2013-02-16 01:53:32 +04:00
|
|
|
IDirectSoundCapture8* cap;
|
|
|
|
IDirectSoundCaptureBuffer8* capBuf;
|
|
|
|
DSCBUFFERDESC dscbd;
|
|
|
|
DWORD lastPos;
|
|
|
|
wfPeerContext* latestPeer;
|
|
|
|
|
2013-02-19 01:01:37 +04:00
|
|
|
int wf_rdpsnd_set_latest_peer(wfPeerContext* peer)
|
2013-02-16 01:53:32 +04:00
|
|
|
{
|
|
|
|
latestPeer = peer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
int wf_directsound_activate(RdpsndServerContext* context)
|
2013-02-16 01:53:32 +04:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
2013-03-16 05:19:16 +04:00
|
|
|
wfInfo* wfi;
|
2013-02-16 01:53:32 +04:00
|
|
|
|
|
|
|
LPDIRECTSOUNDCAPTUREBUFFER pDSCB;
|
|
|
|
|
2013-03-16 05:19:16 +04:00
|
|
|
wfi = wf_info_get_instance();
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_DBG(TAG, "RDPSND (direct sound) Activated");
|
2013-02-16 01:53:32 +04:00
|
|
|
hr = DirectSoundCaptureCreate8(NULL, &cap, NULL);
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to create sound capture device");
|
2013-02-20 01:27:35 +04:00
|
|
|
return 1;
|
2013-02-16 01:53:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_INFO(TAG, "Created sound capture device");
|
2013-02-16 01:53:32 +04:00
|
|
|
dscbd.dwSize = sizeof(DSCBUFFERDESC);
|
|
|
|
dscbd.dwFlags = 0;
|
2013-03-16 05:19:16 +04:00
|
|
|
dscbd.dwBufferBytes = wfi->agreed_format->nAvgBytesPerSec;
|
2013-02-16 01:53:32 +04:00
|
|
|
dscbd.dwReserved = 0;
|
2013-03-16 05:19:16 +04:00
|
|
|
dscbd.lpwfxFormat = wfi->agreed_format;
|
2013-02-16 01:53:32 +04:00
|
|
|
dscbd.dwFXCount = 0;
|
|
|
|
dscbd.lpDSCFXDesc = NULL;
|
|
|
|
|
|
|
|
hr = cap->lpVtbl->CreateCaptureBuffer(cap, &dscbd, &pDSCB, NULL);
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to create capture buffer");
|
2013-02-16 01:53:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_INFO(TAG, "Created capture buffer");
|
2013-02-16 01:53:32 +04:00
|
|
|
hr = pDSCB->lpVtbl->QueryInterface(pDSCB, &IID_IDirectSoundCaptureBuffer8, (LPVOID*)&capBuf);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to QI capture buffer");
|
2013-02-16 01:53:32 +04:00
|
|
|
}
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_INFO(TAG, "Created IDirectSoundCaptureBuffer8");
|
|
|
|
pDSCB->lpVtbl->Release(pDSCB);
|
2013-02-16 01:53:32 +04:00
|
|
|
lastPos = 0;
|
|
|
|
|
|
|
|
CreateThread(NULL, 0, wf_rdpsnd_directsound_thread, latestPeer, 0, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI wf_rdpsnd_directsound_thread(LPVOID lpParam)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
DWORD beg = 0;
|
|
|
|
DWORD end = 0;
|
|
|
|
DWORD diff, rate;
|
|
|
|
wfPeerContext* context;
|
|
|
|
wfInfo* wfi;
|
|
|
|
|
|
|
|
VOID* pbCaptureData = NULL;
|
|
|
|
DWORD dwCaptureLength = 0;
|
|
|
|
VOID* pbCaptureData2 = NULL;
|
|
|
|
DWORD dwCaptureLength2 = 0;
|
|
|
|
VOID* pbPlayData = NULL;
|
|
|
|
DWORD dwReadPos = 0;
|
|
|
|
LONG lLockSize = 0;
|
|
|
|
|
|
|
|
wfi = wf_info_get_instance();
|
|
|
|
|
|
|
|
context = (wfPeerContext*)lpParam;
|
|
|
|
rate = 1000 / 24;
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_INFO(TAG, "Trying to start capture");
|
2013-02-16 01:53:32 +04:00
|
|
|
hr = capBuf->lpVtbl->Start(capBuf, DSCBSTART_LOOPING);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to start capture");
|
2013-02-16 01:53:32 +04:00
|
|
|
}
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_INFO(TAG, "Capture started");
|
2013-02-16 01:53:32 +04:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
|
|
|
|
end = GetTickCount();
|
|
|
|
diff = end - beg;
|
|
|
|
|
|
|
|
if (diff < rate)
|
|
|
|
{
|
|
|
|
Sleep(rate - diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
beg = GetTickCount();
|
|
|
|
|
|
|
|
if (wf_rdpsnd_lock() > 0)
|
|
|
|
{
|
|
|
|
//check for main exit condition
|
|
|
|
if (wfi->snd_stop == TRUE)
|
|
|
|
{
|
|
|
|
wf_rdpsnd_unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = capBuf->lpVtbl->GetCurrentPosition(capBuf, NULL, &dwReadPos);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to get read pos");
|
2013-02-16 01:53:32 +04:00
|
|
|
wf_rdpsnd_unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lLockSize = dwReadPos - lastPos;//dscbd.dwBufferBytes;
|
|
|
|
if (lLockSize < 0) lLockSize += dscbd.dwBufferBytes;
|
|
|
|
|
2014-09-12 19:38:12 +04:00
|
|
|
//WLog_DBG(TAG, "Last, read, lock = [%d, %d, %d]\n", lastPos, dwReadPos, lLockSize);
|
2013-02-16 01:53:32 +04:00
|
|
|
|
2014-09-12 19:38:12 +04:00
|
|
|
if (lLockSize == 0)
|
2013-02-16 01:53:32 +04:00
|
|
|
{
|
|
|
|
wf_rdpsnd_unlock();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
hr = capBuf->lpVtbl->Lock(capBuf, lastPos, lLockSize, &pbCaptureData, &dwCaptureLength, &pbCaptureData2, &dwCaptureLength2, 0L);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to lock sound capture buffer");
|
2013-02-16 01:53:32 +04:00
|
|
|
wf_rdpsnd_unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//fwrite(pbCaptureData, 1, dwCaptureLength, pFile);
|
|
|
|
//fwrite(pbCaptureData2, 1, dwCaptureLength2, pFile);
|
|
|
|
|
|
|
|
//FIXME: frames = bytes/(bytespersample * channels)
|
|
|
|
|
2014-06-05 02:16:10 +04:00
|
|
|
context->rdpsnd->SendSamples(context->rdpsnd, pbCaptureData, dwCaptureLength/4, (UINT16)(beg & 0xffff));
|
|
|
|
context->rdpsnd->SendSamples(context->rdpsnd, pbCaptureData2, dwCaptureLength2/4, (UINT16)(beg & 0xffff));
|
2013-02-16 01:53:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
hr = capBuf->lpVtbl->Unlock(capBuf, pbCaptureData, dwCaptureLength, pbCaptureData2, dwCaptureLength2);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to unlock sound capture buffer");
|
2013-02-16 01:53:32 +04:00
|
|
|
wf_rdpsnd_unlock();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO keep track of location in buffer
|
|
|
|
lastPos += dwCaptureLength;
|
|
|
|
lastPos %= dscbd.dwBufferBytes;
|
|
|
|
lastPos += dwCaptureLength2;
|
|
|
|
lastPos %= dscbd.dwBufferBytes;
|
|
|
|
|
|
|
|
wf_rdpsnd_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_INFO(TAG, "Trying to stop sound capture");
|
2013-02-16 01:53:32 +04:00
|
|
|
hr = capBuf->lpVtbl->Stop(capBuf);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2014-09-12 19:38:12 +04:00
|
|
|
WLog_ERR(TAG, "Failed to stop capture");
|
2013-02-16 01:53:32 +04:00
|
|
|
}
|
2014-09-12 19:38:12 +04:00
|
|
|
|
|
|
|
WLog_INFO(TAG, "Capture stopped");
|
2013-02-16 01:53:32 +04:00
|
|
|
capBuf->lpVtbl->Release(capBuf);
|
|
|
|
cap->lpVtbl->Release(cap);
|
|
|
|
|
|
|
|
lastPos = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|