forked from aricatamoy/svcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVCam.cpp
342 lines (276 loc) · 10.5 KB
/
SVCam.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// SVCam.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "SVCam.h"
//////////////////////////////////////////////////////////////////////////
// CSimpleVirtualCamFilter is the source filter which masquerades as a capture device
//////////////////////////////////////////////////////////////////////////
CUnknown * WINAPI CSimpleVirtualCamFilter::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr)
{
ASSERT(phr);
CUnknown *punk = new CSimpleVirtualCamFilter(lpunk, phr);
return punk;
}
CSimpleVirtualCamFilter::CSimpleVirtualCamFilter(LPUNKNOWN lpunk, HRESULT *phr) :
CSource(NAME("Simple Virtual Cam"), lpunk, CLSID_SimpleVirtualCamFilter)
{
ASSERT(phr);
CAutoLock cAutoLock(&m_cStateLock);
// Create the one and only output pin
m_paStreams = (CSourceStream **) new CSimpleVirtualCamFilterStream*[1];
m_paStreams[0] = new CSimpleVirtualCamFilterStream(phr, this, L"Simple Virtual Cam");
}
HRESULT CSimpleVirtualCamFilter::QueryInterface(REFIID riid, void **ppv)
{
//Forward request for IAMStreamConfig & IKsPropertySet to the pin
if (riid == _uuidof(IAMStreamConfig) || riid == _uuidof(IKsPropertySet))
return m_paStreams[0]->QueryInterface(riid, ppv);
else
return CSource::QueryInterface(riid, ppv);
}
//////////////////////////////////////////////////////////////////////////
// CKCamStream is the one and only output pin of CKCam which handles
// all the stuff.
//////////////////////////////////////////////////////////////////////////
CSimpleVirtualCamFilterStream::CSimpleVirtualCamFilterStream(HRESULT *phr, CSimpleVirtualCamFilter *pParent, LPCWSTR pPinName) :
CSourceStream(NAME("Simple Virtual Cam"), phr, pParent, pPinName), m_pParent(pParent)
{
// Set the default media type as 640x480x24@30
GetMediaType(8, &m_mt);
}
CSimpleVirtualCamFilterStream::~CSimpleVirtualCamFilterStream()
{
}
ULONG CSimpleVirtualCamFilterStream::Release(){
return GetOwner()->Release();
}
ULONG CSimpleVirtualCamFilterStream::AddRef(){
return GetOwner()->AddRef();
}
HRESULT CSimpleVirtualCamFilterStream::QueryInterface(REFIID riid, void **ppv)
{
// Standard OLE stuff
if (riid == _uuidof(IAMStreamConfig))
*ppv = (IAMStreamConfig*)this;
else if (riid == _uuidof(IKsPropertySet))
*ppv = (IKsPropertySet*)this;
else
return CSourceStream::QueryInterface(riid, ppv);
AddRef();
return S_OK;
}
///////////////////////////////////////////////////////////
// This is where the magic happens!
///////////////////////////////////////////////////////////
HRESULT CSimpleVirtualCamFilterStream::FillBuffer(IMediaSample *pms)
{
REFERENCE_TIME rtNow;
REFERENCE_TIME avgFrameTime = ((VIDEOINFOHEADER*)m_mt.pbFormat)->AvgTimePerFrame;
rtNow = m_rtLastTime;
m_rtLastTime += avgFrameTime;
pms->SetTime(&rtNow, &m_rtLastTime);
pms->SetSyncPoint(TRUE);
BYTE *pData;
long lDataLen;
pms->GetPointer(&pData);
lDataLen = pms->GetSize();
for (int i = 0; i < lDataLen; ++i)
pData[i] = rand();
return NOERROR;
}
//
// Notify
// Ignore quality management messages sent from the downstream filter
STDMETHODIMP CSimpleVirtualCamFilterStream::Notify(IBaseFilter * pSender, Quality q)
{
return E_NOTIMPL;
} // Notify
//////////////////////////////////////////////////////////////////////////
// This is called when the output format has been negotiated
//////////////////////////////////////////////////////////////////////////
HRESULT CSimpleVirtualCamFilterStream::SetMediaType(const CMediaType *pmt)
{
DECLARE_PTR(VIDEOINFOHEADER, pvi, pmt->Format());
HRESULT hr = CSourceStream::SetMediaType(pmt);
return hr;
}
// See Directshow help topic for IAMStreamConfig for details on this method
HRESULT CSimpleVirtualCamFilterStream::GetMediaType(int iPosition, CMediaType *pmt)
{
if (iPosition < 0) return E_INVALIDARG;
if (iPosition > 8) return VFW_S_NO_MORE_ITEMS;
if (iPosition == 0)
{
*pmt = m_mt;
return S_OK;
}
DECLARE_PTR(VIDEOINFOHEADER, pvi, pmt->AllocFormatBuffer(sizeof(VIDEOINFOHEADER)));
ZeroMemory(pvi, sizeof(VIDEOINFOHEADER));
pvi->bmiHeader.biCompression = BI_RGB;
pvi->bmiHeader.biBitCount = 24;
pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pvi->bmiHeader.biWidth = 80 * iPosition;
pvi->bmiHeader.biHeight = 60 * iPosition;
pvi->bmiHeader.biPlanes = 1;
pvi->bmiHeader.biSizeImage = GetBitmapSize(&pvi->bmiHeader);
pvi->bmiHeader.biClrImportant = 0;
pvi->AvgTimePerFrame = 1000000;
SetRectEmpty(&(pvi->rcSource)); // we want the whole image area rendered.
SetRectEmpty(&(pvi->rcTarget)); // no particular destination rectangle
pmt->SetType(&MEDIATYPE_Video);
pmt->SetFormatType(&FORMAT_VideoInfo);
pmt->SetTemporalCompression(FALSE);
// Work out the GUID for the subtype from the header info.
const GUID SubTypeGUID = GetBitmapSubtype(&pvi->bmiHeader);
pmt->SetSubtype(&SubTypeGUID);
pmt->SetSampleSize(pvi->bmiHeader.biSizeImage);
return NOERROR;
} // GetMediaType
// This method is called to see if a given output format is supported
HRESULT CSimpleVirtualCamFilterStream::CheckMediaType(const CMediaType *pMediaType)
{
VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *)(pMediaType->Format());
if (*pMediaType != m_mt)
return E_INVALIDARG;
return S_OK;
} // CheckMediaType
// This method is called after the pins are connected to allocate buffers to stream data
HRESULT CSimpleVirtualCamFilterStream::DecideBufferSize(IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *pProperties)
{
CAutoLock cAutoLock(m_pFilter->pStateLock());
HRESULT hr = NOERROR;
VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *)m_mt.Format();
pProperties->cBuffers = 1;
pProperties->cbBuffer = pvi->bmiHeader.biSizeImage;
ALLOCATOR_PROPERTIES Actual;
hr = pAlloc->SetProperties(pProperties, &Actual);
if (FAILED(hr)) return hr;
if (Actual.cbBuffer < pProperties->cbBuffer) return E_FAIL;
return NOERROR;
} // DecideBufferSize
// Called when graph is run
HRESULT CSimpleVirtualCamFilterStream::OnThreadCreate()
{
m_rtLastTime = 0;
return NOERROR;
} // OnThreadCreate
//////////////////////////////////////////////////////////////////////////
// IAMStreamConfig
//////////////////////////////////////////////////////////////////////////
HRESULT STDMETHODCALLTYPE CSimpleVirtualCamFilterStream::SetFormat(AM_MEDIA_TYPE *pmt)
{
DECLARE_PTR(VIDEOINFOHEADER, pvi, m_mt.pbFormat);
m_mt = *pmt;
IPin* pin;
ConnectedTo(&pin);
if (pin)
{
IFilterGraph *pGraph = m_pParent->GetGraph();
pGraph->Reconnect(this);
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE CSimpleVirtualCamFilterStream::GetFormat(AM_MEDIA_TYPE **ppmt)
{
*ppmt = CreateMediaType(&m_mt);
return S_OK;
}
HRESULT STDMETHODCALLTYPE CSimpleVirtualCamFilterStream::GetNumberOfCapabilities(int *piCount, int *piSize)
{
*piCount = 8;
*piSize = sizeof(VIDEO_STREAM_CONFIG_CAPS);
return S_OK;
}
HRESULT STDMETHODCALLTYPE CSimpleVirtualCamFilterStream::GetStreamCaps(int iIndex, AM_MEDIA_TYPE **pmt, BYTE *pSCC)
{
*pmt = CreateMediaType(&m_mt);
DECLARE_PTR(VIDEOINFOHEADER, pvi, (*pmt)->pbFormat);
if (iIndex == 0) iIndex = 4;
pvi->bmiHeader.biCompression = BI_RGB;
pvi->bmiHeader.biBitCount = 24;
pvi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pvi->bmiHeader.biWidth = 80 * iIndex;
pvi->bmiHeader.biHeight = 60 * iIndex;
pvi->bmiHeader.biPlanes = 1;
pvi->bmiHeader.biSizeImage = GetBitmapSize(&pvi->bmiHeader);
pvi->bmiHeader.biClrImportant = 0;
SetRectEmpty(&(pvi->rcSource)); // we want the whole image area rendered.
SetRectEmpty(&(pvi->rcTarget)); // no particular destination rectangle
(*pmt)->majortype = MEDIATYPE_Video;
(*pmt)->subtype = MEDIASUBTYPE_RGB24;
(*pmt)->formattype = FORMAT_VideoInfo;
(*pmt)->bTemporalCompression = FALSE;
(*pmt)->bFixedSizeSamples = FALSE;
(*pmt)->lSampleSize = pvi->bmiHeader.biSizeImage;
(*pmt)->cbFormat = sizeof(VIDEOINFOHEADER);
DECLARE_PTR(VIDEO_STREAM_CONFIG_CAPS, pvscc, pSCC);
pvscc->guid = FORMAT_VideoInfo;
pvscc->VideoStandard = AnalogVideo_None;
pvscc->InputSize.cx = 640;
pvscc->InputSize.cy = 480;
pvscc->MinCroppingSize.cx = 80;
pvscc->MinCroppingSize.cy = 60;
pvscc->MaxCroppingSize.cx = 640;
pvscc->MaxCroppingSize.cy = 480;
pvscc->CropGranularityX = 80;
pvscc->CropGranularityY = 60;
pvscc->CropAlignX = 0;
pvscc->CropAlignY = 0;
pvscc->MinOutputSize.cx = 80;
pvscc->MinOutputSize.cy = 60;
pvscc->MaxOutputSize.cx = 640;
pvscc->MaxOutputSize.cy = 480;
pvscc->OutputGranularityX = 0;
pvscc->OutputGranularityY = 0;
pvscc->StretchTapsX = 0;
pvscc->StretchTapsY = 0;
pvscc->ShrinkTapsX = 0;
pvscc->ShrinkTapsY = 0;
pvscc->MinFrameInterval = 200000; //50 fps
pvscc->MaxFrameInterval = 50000000; // 0.2 fps
pvscc->MinBitsPerSecond = (80 * 60 * 3 * 8) / 5;
pvscc->MaxBitsPerSecond = 640 * 480 * 3 * 8 * 50;
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
// IKsPropertySet
//////////////////////////////////////////////////////////////////////////
HRESULT CSimpleVirtualCamFilterStream::Set(REFGUID guidPropSet, DWORD dwID, void *pInstanceData,
DWORD cbInstanceData, void *pPropData, DWORD cbPropData)
{// Set: Cannot set any properties.
return E_NOTIMPL;
}
// Get: Return the pin category (our only property).
HRESULT CSimpleVirtualCamFilterStream::Get(
REFGUID guidPropSet, // Which property set.
DWORD dwPropID, // Which property in that set.
void *pInstanceData, // Instance data (ignore).
DWORD cbInstanceData, // Size of the instance data (ignore).
void *pPropData, // Buffer to receive the property data.
DWORD cbPropData, // Size of the buffer.
DWORD *pcbReturned // Return the size of the property.
)
{
if (guidPropSet != AMPROPSETID_Pin) return E_PROP_SET_UNSUPPORTED;
if (dwPropID != AMPROPERTY_PIN_CATEGORY) return E_PROP_ID_UNSUPPORTED;
if (pPropData == NULL && pcbReturned == NULL) return E_POINTER;
if (pcbReturned) *pcbReturned = sizeof(GUID);
if (pPropData == NULL) return S_OK; // Caller just wants to know the size.
if (cbPropData < sizeof(GUID)) return E_UNEXPECTED;// The buffer is too small.
*(GUID *)pPropData = PIN_CATEGORY_CAPTURE;
return S_OK;
}
// QuerySupported: Query whether the pin supports the specified property.
HRESULT CSimpleVirtualCamFilterStream::QuerySupported(REFGUID guidPropSet, DWORD dwPropID, DWORD *pTypeSupport)
{
if (guidPropSet != AMPROPSETID_Pin) return E_PROP_SET_UNSUPPORTED;
if (dwPropID != AMPROPERTY_PIN_CATEGORY) return E_PROP_ID_UNSUPPORTED;
// We support getting this property, but not setting it.
if (pTypeSupport) *pTypeSupport = KSPROPERTY_SUPPORT_GET;
return S_OK;
}
// Set misc flag that this is truly a live source
ULONG CSimpleVirtualCamFilterStream::GetMiscFlags(void)
{
return AM_FILTER_MISC_FLAGS_IS_SOURCE;
}