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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
//*@@@+++@@@@******************************************************************
//
// Microsoft Windows Media Foundation
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//*@@@---@@@@******************************************************************
//
#pragma once
#include "stdafx.h"
#include "common.h"
#include "multipinmfthelpers.h"
extern DeviceStreamState pinStateTransition[][4];
class CPinQueue;
class CPinState;
class CMultipinMft;
class CBasePin:
public IMFAttributes,
public IKsControl
{
public:
CBasePin( _In_ ULONG _id=0, _In_ CMultipinMft *parent=NULL);
virtual ~CBasePin() = 0;
virtual STDMETHODIMP_(DeviceStreamState) GetState();
virtual STDMETHODIMP_(DeviceStreamState) SetState( _In_ DeviceStreamState State);
//
//IUnknown Interface functions
//
STDMETHODIMP_(ULONG) AddRef(
void
)
{
return InterlockedIncrement(&m_nRefCount);
}
STDMETHODIMP_(ULONG) Release(
void
)
{
ULONG uCount = InterlockedDecrement(&m_nRefCount);
if (uCount == 0)
{
delete this;
}
return uCount;
}
STDMETHODIMP_(HRESULT) QueryInterface(
_In_ REFIID riid,
_Outptr_result_maybenull_ void **ppvObject
);
//
// IKsControl Interface functions
//
STDMETHOD(KsProperty)(
_In_reads_bytes_(ulPropertyLength) PKSPROPERTY pProperty,
_In_ ULONG ulPropertyLength,
_Inout_updates_bytes_(ulDataLength) LPVOID pPropertyData,
_In_ ULONG ulDataLength,
_Out_opt_ ULONG* pBytesReturned
)
{
if ( m_spIkscontrol!=nullptr )
{
return m_spIkscontrol->KsProperty(pProperty,
ulPropertyLength,
pPropertyData,
ulDataLength,
pBytesReturned);
}
else
{
return E_NOTIMPL;
}
}
virtual STDMETHODIMP FlushQueues(
)
{
return S_OK;
}
//
// NOOPs for this iteration..
//
STDMETHOD(KsMethod)(
_In_reads_bytes_(ulMethodLength) PKSMETHOD pMethod,
_In_ ULONG ulMethodLength,
_Inout_updates_bytes_(ulDataLength) LPVOID pMethodData,
_In_ ULONG ulDataLength,
_Out_opt_ ULONG* pBytesReturned
)
{
UNREFERENCED_PARAMETER(pBytesReturned);
UNREFERENCED_PARAMETER(ulDataLength);
UNREFERENCED_PARAMETER(pMethodData);
UNREFERENCED_PARAMETER(pMethod);
UNREFERENCED_PARAMETER(ulMethodLength);
return S_OK;
}
STDMETHOD(KsEvent)(
_In_reads_bytes_(ulEventLength) PKSEVENT pEvent,
_In_ ULONG ulEventLength,
_Inout_updates_bytes_opt_(ulDataLength) LPVOID pEventData,
_In_ ULONG ulDataLength,
_Out_opt_ ULONG* pBytesReturned
)
{
UNREFERENCED_PARAMETER(pBytesReturned);
UNREFERENCED_PARAMETER(ulDataLength);
UNREFERENCED_PARAMETER(pEventData);
UNREFERENCED_PARAMETER(pEvent);
UNREFERENCED_PARAMETER(ulEventLength);
return S_OK;
}
//
//IMFAttributes implementation
//
STDMETHOD(GetItem)(
_In_ REFGUID guidKey,
_Inout_opt_ PROPVARIANT* pValue
)
{
return m_spAttributes->GetItem(guidKey, pValue);
}
STDMETHOD(GetItemType)(
_In_ REFGUID guidKey,
_Out_ MF_ATTRIBUTE_TYPE* pType
)
{
return m_spAttributes->GetItemType(guidKey, pType);
}
STDMETHOD(CompareItem)(
_In_ REFGUID guidKey,
_In_ REFPROPVARIANT Value,
_Out_ BOOL* pbResult
)
{
return m_spAttributes->CompareItem(guidKey, Value, pbResult);
}
STDMETHOD(Compare)(
_In_ IMFAttributes* pTheirs,
_In_ MF_ATTRIBUTES_MATCH_TYPE MatchType,
_Out_ BOOL* pbResult
)
{
return m_spAttributes->Compare(pTheirs, MatchType, pbResult);
}
STDMETHOD(GetUINT32)(
_In_ REFGUID guidKey,
_Out_ UINT32* punValue
)
{
return m_spAttributes->GetUINT32(guidKey, punValue);
}
STDMETHOD(GetUINT64)(
_In_ REFGUID guidKey,
_Out_ UINT64* punValue
)
{
return m_spAttributes->GetUINT64(guidKey, punValue);
}
STDMETHOD(GetDouble)(
_In_ REFGUID guidKey,
_Out_ double* pfValue
)
{
return m_spAttributes->GetDouble(guidKey, pfValue);
}
STDMETHOD(GetGUID)(
_In_ REFGUID guidKey,
_Out_ GUID* pguidValue
)
{
return m_spAttributes->GetGUID(guidKey, pguidValue);
}
STDMETHOD(GetStringLength)(
_In_ REFGUID guidKey,
_Out_ UINT32* pcchLength
)
{
return m_spAttributes->GetStringLength(guidKey, pcchLength);
}
STDMETHOD(GetString)(
_In_ REFGUID guidKey,
_Out_writes_(cchBufSize) LPWSTR pwszValue,
_In_ UINT32 cchBufSize,
_Inout_opt_ UINT32* pcchLength
)
{
return m_spAttributes->GetString(guidKey, pwszValue, cchBufSize, pcchLength);
}
STDMETHOD(GetAllocatedString)(
_In_ REFGUID guidKey,
_Out_writes_(*pcchLength + 1) LPWSTR* ppwszValue,
_Inout_ UINT32* pcchLength
)
{
return m_spAttributes->GetAllocatedString(guidKey, ppwszValue, pcchLength);
}
STDMETHOD(GetBlobSize)(
_In_ REFGUID guidKey,
_Out_ UINT32* pcbBlobSize
)
{
return m_spAttributes->GetBlobSize(guidKey, pcbBlobSize);
}
STDMETHOD(GetBlob)(
_In_ REFGUID guidKey,
_Out_writes_(cbBufSize) UINT8* pBuf,
UINT32 cbBufSize,
_Inout_ UINT32* pcbBlobSize
)
{
return m_spAttributes->GetBlob(guidKey, pBuf, cbBufSize, pcbBlobSize);
}
STDMETHOD(GetAllocatedBlob)(
__RPC__in REFGUID guidKey,
__RPC__deref_out_ecount_full_opt(*pcbSize) UINT8** ppBuf,
__RPC__out UINT32* pcbSize
)
{
return m_spAttributes->GetAllocatedBlob(guidKey, ppBuf, pcbSize);
}
STDMETHOD(GetUnknown)(
__RPC__in REFGUID guidKey,
__RPC__in REFIID riid,
__RPC__deref_out_opt LPVOID *ppv
)
{
return m_spAttributes->GetUnknown(guidKey, riid, ppv);
}
STDMETHOD(SetItem)(
_In_ REFGUID guidKey,
_In_ REFPROPVARIANT Value
)
{
return m_spAttributes->SetItem(guidKey, Value);
}
STDMETHOD(DeleteItem)(
_In_ REFGUID guidKey
)
{
return m_spAttributes->DeleteItem(guidKey);
}
STDMETHOD(DeleteAllItems)()
{
return m_spAttributes->DeleteAllItems();
}
STDMETHOD(SetUINT32)(
_In_ REFGUID guidKey,
_In_ UINT32 unValue
)
{
return m_spAttributes->SetUINT32(guidKey, unValue);
}
STDMETHOD(SetUINT64)(
_In_ REFGUID guidKey,
_In_ UINT64 unValue
)
{
return m_spAttributes->SetUINT64(guidKey, unValue);
}
STDMETHOD(SetDouble)(
_In_ REFGUID guidKey,
_In_ double fValue
)
{
return m_spAttributes->SetDouble(guidKey, fValue);
}
STDMETHOD(SetGUID)(
_In_ REFGUID guidKey,
_In_ REFGUID guidValue
)
{
return m_spAttributes->SetGUID(guidKey, guidValue);
}
STDMETHOD(SetString)(
_In_ REFGUID guidKey,
_In_ LPCWSTR wszValue
)
{
return m_spAttributes->SetString(guidKey, wszValue);
}
STDMETHOD(SetBlob)(
_In_ REFGUID guidKey,
_In_reads_(cbBufSize) const UINT8* pBuf,
UINT32 cbBufSize
)
{
return m_spAttributes->SetBlob(guidKey, pBuf, cbBufSize);
}
STDMETHOD(SetUnknown)(
_In_ REFGUID guidKey,
_In_ IUnknown* pUnknown
)
{
return m_spAttributes->SetUnknown(guidKey, pUnknown);
}
STDMETHOD(LockStore)()
{
return m_spAttributes->LockStore();
}
STDMETHOD(UnlockStore)()
{
return m_spAttributes->UnlockStore();
}
STDMETHOD(GetCount)(
_Out_ UINT32* pcItems
)
{
return m_spAttributes->GetCount(pcItems);
}
STDMETHOD(GetItemByIndex)(
UINT32 unIndex,
_Out_ GUID* pguidKey,
_Inout_ PROPVARIANT* pValue
)
{
return m_spAttributes->GetItemByIndex(unIndex, pguidKey, pValue);
}
STDMETHOD(CopyAllItems)(
_In_ IMFAttributes* pDest
)
{
return m_spAttributes->CopyAllItems(pDest);
}
//
//Helper Functions
//
__requires_lock_held(m_lock)
__inline HRESULT Active()
{
return (m_state == DeviceStreamState_Run)?S_OK:E_FAIL;
}
__inline DWORD streamId()
{
return m_StreamId;
}
__inline VOID setMediaType(_In_opt_ IMFMediaType *pMediaType)
{
m_setMediaType = pMediaType;
}
__inline HRESULT getMediaType(_Outptr_opt_result_maybenull_ IMFMediaType **ppMediaType)
{
HRESULT hr = S_OK;
if (!ppMediaType)
return E_INVALIDARG;
if (m_setMediaType != nullptr)
{
hr = m_setMediaType.CopyTo(ppMediaType);
}
else
{
hr = MF_E_TRANSFORM_TYPE_NOT_SET;
}
return hr;
}
__inline STDMETHOD (getPinAttributes) (_In_ IMFAttributes **ppAttributes)
{
return QueryInterface( IID_PPV_ARGS(ppAttributes) );
}
STDMETHOD(AddMediaType)(
_Inout_ DWORD *pos,
_In_ IMFMediaType *pMediatype); /*Filling the media types data structure*/
STDMETHODIMP GetMediaTypeAt(
_In_ DWORD pos,
_Outptr_result_maybenull_ IMFMediaType **pMediaType); /* getting the data from the data structure*/
STDMETHOD_(BOOL, IsMediaTypeSupported)(
_In_ IMFMediaType *pMediaType,
_When_(ppIMFMediaTypeFull != nullptr, _Outptr_result_maybenull_)
IMFMediaType **ppIMFMediaTypeFull);
STDMETHODIMP GetOutputAvailableType(
_In_ DWORD dwTypeIndex,
_Out_opt_ IMFMediaType **ppType);
VOID SetD3DManager(_In_opt_ IUnknown* pManager);
VOID SetWorkQueue(_In_ DWORD dwQueueId)
{
m_dwWorkQueueId = dwQueueId;
}
protected:
//
//Inline helper functions
//
_inline CMultipinMft* Parent()
{
return m_Parent;
}
__inline HRESULT setAttributes(_In_ IMFAttributes* _pAttributes)
{
m_spAttributes = _pAttributes;
return S_OK;
}
__inline CCritSec& lock()
{
return m_lock;
}
IMFMediaTypeArray m_listOfMediaTypes;
ComPtr<IMFAttributes> m_spAttributes;
ComPtr<IKsControl> m_spIkscontrol;
DeviceStreamState m_state;
ComPtr<IUnknown> m_spDxgiManager;
DWORD m_dwWorkQueueId;
private:
ULONG m_StreamId; /*Device Stream Id*/
CCritSec m_lock; /*This is only used to change the reference count i.e. active users of this stream*/
ComPtr<IMFMediaType> m_setMediaType;
CMultipinMft* m_Parent;
ULONG m_nRefCount;
};
class CInPin: public CBasePin{
public:
CInPin( _In_opt_ IMFAttributes*, _In_ ULONG ulPinId = 0, _In_ CMultipinMft *pParent=NULL);
~CInPin();
STDMETHOD ( Init )(
_In_ IMFTransform *
);
STDMETHOD_( VOID, ConnectPin)(
_In_ CBasePin *
);
STDMETHOD (SendSample)(
_In_ IMFSample *
);
HRESULT GenerateMFMediaTypeListFromDevice(
_In_ UINT uiStreamId
);
STDMETHODIMP WaitForSetInputPinMediaChange(
);
//
//Corresponding IMFDeviceTransform functions for the Pin
//
HRESULT GetInputStreamPreferredState(
_Inout_ DeviceStreamState *value,
_Outptr_opt_result_maybenull_ IMFMediaType** ppMediaType
);
HRESULT SetInputStreamState(
_In_ IMFMediaType *pMediaType,
_In_ DeviceStreamState value,
_In_ DWORD dwFlags
);
virtual STDMETHODIMP FlushQueues()
{
return S_OK;
}
//
//Inline functions
//
__inline IMFMediaType* getPreferredMediaType()
{
return m_spPrefferedMediaType.Get();
}
__inline VOID setPreferredMediaType( _In_ IMFMediaType *pMediaType)
{
m_spPrefferedMediaType = pMediaType;
}
__inline DeviceStreamState setPreferredStreamState(_In_ DeviceStreamState streamState)
{
return (DeviceStreamState)InterlockedCompareExchange((LONG*)&m_preferredStreamState, (LONG)streamState, (LONG)m_preferredStreamState);
}
__inline DeviceStreamState getPreferredStreamState()
{
return m_preferredStreamState;
}
void ReleaseConnectedPins();
protected:
ComPtr<IMFTransform> m_spSourceTransform; /*Source Transform i.e. DevProxy*/
GUID m_stStreamType; /*GUID representing the GUID*/
ComPtr<CBasePin> m_outpin; //Only one output pin connected per input pin. There can be multiple pins connected and this could be a list
DeviceStreamState m_preferredStreamState;
ComPtr<IMFMediaType> m_spPrefferedMediaType;
HANDLE m_waitInputMediaTypeWaiter; /*Set when the input media type is changed*/
};
class COutPin: public CBasePin{
public:
COutPin( _In_ ULONG id = 0,
_In_opt_ CMultipinMft *pparent = NULL,
_In_ IKsControl* iksControl=NULL);
~COutPin();
STDMETHODIMP FlushQueues();
STDMETHODIMP AddPin(
_In_ DWORD pinId
);
virtual STDMETHODIMP AddSample(
_In_ IMFSample *pSample,
_In_ CBasePin *inPin
);
STDMETHODIMP GetOutputStreamInfo(
_Out_ MFT_OUTPUT_STREAM_INFO *pStreamInfo
);
STDMETHODIMP ChangeMediaTypeFromInpin(
_In_ IMFMediaType *pInMediatype,
_In_ IMFMediaType* pOutMediaType,
_In_ DeviceStreamState state );
STDMETHODIMP ProcessOutput (
_In_ DWORD dwFlags,
_Inout_ MFT_OUTPUT_DATA_BUFFER *pOutputSample,
_Out_ DWORD *pdwStatus
);
STDMETHODIMP KsProperty(
_In_reads_bytes_(ulPropertyLength) PKSPROPERTY pProperty,
_In_ ULONG ulPropertyLength,
_Inout_updates_bytes_(ulDataLength) LPVOID pPropertyData,
_In_ ULONG ulDataLength,
_Out_opt_ ULONG* pBytesReturned
);
STDMETHODIMP_(VOID) SetFirstSample(
_In_ BOOL
);
UINT32 GetMediatypeCount()
{
return (UINT32)m_listOfMediaTypes.size();
}
protected:
CPinQueue * m_queue; /* Queue where the sample will be stored*/
BOOL m_firstSample;
};
class CAsyncInPin: public CInPin {
public:
STDMETHODIMP FlushQueues();
STDMETHODIMP SendSample(
_In_ IMFSample *
);
STDMETHODIMP Invoke(
_In_ IMFAsyncResult *
);
STDMETHODIMP Init();
CAsyncInPin(
_In_opt_ IMFAttributes *pAttributes,
_In_ ULONG ulPinId,
_In_ CMultipinMft *pParent) : CInPin(pAttributes,
ulPinId, pParent)
, m_dwSamplesInFlight(0)
, m_hHandle(INVALID_HANDLE_VALUE)
, m_bFlushing(FALSE)
, m_asyncCallback(nullptr)
{
m_hHandle = CreateEvent(NULL, TRUE, TRUE, L"Async_Pin Event");
if (m_hHandle == nullptr)
throw bad_alloc();
Init();
}
~CAsyncInPin()
{
FlushQueues();
CloseHandle(m_hHandle);
m_hHandle = INVALID_HANDLE_VALUE;
}
ComPtr<CDMFTAsyncCallback<CAsyncInPin,&CAsyncInPin::Invoke> > m_asyncCallback; // Callback object
HANDLE m_hHandle; // Handles to keep flush state
DWORD m_dwSamplesInFlight; // Samples in flight i.e. waiting for the callback functions to be called
BOOL m_bFlushing;
////////////////////////////////////////////////////////////////////////////////////////
// End of Asynchronous callback definitions
////////////////////////////////////////////////////////////////////////////////////////
};
class CTranslateOutPin : public COutPin {
/*List of GUIDS to be translated*/
const GUID tranlateGUIDS[2] = {
MFVideoFormat_H264,
MFVideoFormat_MJPG
};
// @@@@README : This is what the compressed media types will be translated into
const GUID translatedGUID = MFVideoFormat_NV12; // Translating to NV12
public:
CTranslateOutPin(_In_ ULONG id = 0,
_In_opt_ CMultipinMft *pparent = NULL,
_In_ IKsControl* iksControl = NULL) : COutPin(id, pparent, iksControl)
{
SetUINT32(MF_SD_VIDEO_SPHERICAL, TRUE);
}
STDMETHOD(AddMediaType)(
_Inout_ DWORD *pos,
_In_ IMFMediaType *pMediatype);
STDMETHOD_(BOOL, IsMediaTypeSupported)(
_In_ IMFMediaType *pMediaType,
_When_(ppIMFMediaTypeFull != nullptr, _Outptr_result_maybenull_)
IMFMediaType **ppIMFMediaTypeFull);
protected:
map<IMFMediaType*, IMFMediaType*> m_TranslatedMediaTypes;
};
|