-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathamcap.cpp
4661 lines (3997 loc) · 154 KB
/
amcap.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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// File: AMCap.cpp
//
// Desc: Audio/Video Capture sample for DirectShow
//
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
#include <atlbase.h>
#include <commdlg.h>
#include <dbt.h>
#include <fcntl.h>
#include <io.h>
#include <mmreg.h>
#include <msacm.h>
#include <stdio.h>
#include <windows.h>
// atl
#include <atlbase.h>
#include <atlstr.h>
//strmbasd
#include <streams.h>
//project
#include "amcap.h"
#include "crossbar.h"
#include "rev.h"
#include "SampleCGB.h"
#include "status.h"
#include "stdafx.h"
//------------------------------------------------------------------------------
// Macros
//------------------------------------------------------------------------------
#define ABS(x) (((x) > 0) ? (x) : -(x))
// An application can advertise the existence of its filter graph
// by registering the graph with a global Running Object Table (ROT).
// The GraphEdit application can detect and remotely view the running
// filter graph, allowing you to 'spy' on the graph with GraphEdit.
//
// To enable registration in this sample, define REGISTER_FILTERGRAPH.
//
#ifdef DEBUG
#define REGISTER_FILTERGRAPH
#endif
//------------------------------------------------------------------------------
// Global data
//------------------------------------------------------------------------------
HINSTANCE ghInstApp=0;
HACCEL ghAccel=0;
HFONT ghfontApp=0;
TEXTMETRIC gtm={0};
TCHAR gszAppName[]=TEXT("AMCap");
DWORD ghwndStyle = (WS_OVERLAPPEDWINDOW|WS_SYSMENU|WS_VISIBLE);
HWND ghwndApp=0, ghwndStatus=0;
HMENU ghmenuApp=0;
BOOL bIsZoomed;
HDEVNOTIFY ghDevNotify=0;
PUnregisterDeviceNotification gpUnregisterDeviceNotification=0;
PRegisterDeviceNotification gpRegisterDeviceNotification=0;
DWORD g_dwGraphRegister=0;
struct _capstuff
{
TCHAR szCaptureFile[_MAX_PATH];
WORD wCapFileSize; // size in Meg
ISampleCaptureGraphBuilder *pBuilder;
IVideoWindow *pVW;
IMediaEventEx *pME;
IAMDroppedFrames *pDF;
IAMVideoCompression *pVC;
IAMVfwCaptureDialogs *pDlg;
IAMStreamConfig *pASC; // for audio cap
IAMStreamConfig *pVSC; // for video cap
IBaseFilter *pRender;
IBaseFilter *pVCap, *pACap;
IGraphBuilder *pFg;
IFileSinkFilter *pSink;
IConfigAviMux *pConfigAviMux;
int iMasterStream;
BOOL fCaptureGraphBuilt;
BOOL fPreviewGraphBuilt;
BOOL fCapturing;
BOOL fPreviewing;
BOOL fMPEG2;
BOOL fCapAudio;
BOOL fCapCC;
BOOL fCCAvail;
BOOL fCapAudioIsRelevant;
bool fDeviceMenuPopulated;
IMoniker *rgpmVideoMenu[10];
IMoniker *rgpmAudioMenu[10];
IMoniker *pmVideo;
IMoniker *pmAudio;
double FrameRate;
int CaptureWidth;
int CaptureHeight;
BOOL fWantPreview;
long lCapStartTime;
long lCapStopTime;
WCHAR wachFriendlyName[120];
BOOL fUseTimeLimit;
BOOL fUseFrameRate;
DWORD dwTimeLimit;
int iFormatDialogPos;
int iSourceDialogPos;
int iDisplayDialogPos;
int iVCapDialogPos;
int iVCrossbarDialogPos;
int iTVTunerDialogPos;
int iACapDialogPos;
int iACrossbarDialogPos;
int iTVAudioDialogPos;
int iVCapCapturePinDialogPos;
int iVCapPreviewPinDialogPos;
int iACapCapturePinDialogPos;
long lDroppedBase;
long lNotBase;
BOOL fPreviewFaked;
CCrossbar *pCrossbar;
int iVideoInputMenuPos;
LONG NumberOfVideoInputs;
HMENU hMenuPopup;
int iNumVCapDevices;
} gcap;
// implements IAMCopyCaptureFileProgress
//
class CProgress : public CUnknown, public IAMCopyCaptureFileProgress
{
public:
CProgress(TCHAR *pName, LPUNKNOWN pUnk, HRESULT *phr) :
CUnknown(pName, pUnk, phr)
{
};
~CProgress()
{
};
STDMETHODIMP_(ULONG) AddRef()
{
return 1;
};
STDMETHODIMP_(ULONG) Release()
{
return 0;
};
STDMETHODIMP QueryInterface(REFIID iid, void **p)
{
CheckPointer(p, E_POINTER);
if(iid == IID_IAMCopyCaptureFileProgress)
{
return GetInterface((IAMCopyCaptureFileProgress *)this, p);
}
else
{
return E_NOINTERFACE;
}
};
STDMETHODIMP Progress(int i)
{
TCHAR tach[80];
wsprintf(tach, TEXT("Save File Progress: %d%%\0"), i);
statusUpdateStatus(ghwndStatus, tach);
return S_OK;
};
};
CComModule _Module;
//------------------------------------------------------------------------------
// Function Prototypes
//------------------------------------------------------------------------------
typedef LONG(PASCAL *LPWNDPROC)(HWND, UINT, WPARAM, LPARAM); // pointer to a window procedure
LONG WINAPI AppWndProc(HWND hwnd, UINT uiMessage, WPARAM wParam, LPARAM lParam);
LONG PASCAL AppCommand(HWND hwnd, unsigned msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void ErrMsg(LPTSTR sz,...);
BOOL SetCaptureFile(HWND hWnd);
BOOL SaveCaptureFile(HWND hWnd);
BOOL AllocCaptureFile(HWND hWnd);
int DoDialog(HWND hwndParent, int DialogID, DLGPROC fnDialog, long lParam);
int FAR PASCAL AllocCapFileProc(HWND hDlg, UINT Message, UINT wParam, LONG lParam);
int FAR PASCAL FrameRateProc(HWND hDlg, UINT Message, UINT wParam, LONG lParam);
int FAR PASCAL TimeLimitProc(HWND hDlg, UINT Message, UINT wParam, LONG lParam);
int FAR PASCAL PressAKeyProc(HWND hDlg, UINT Message, UINT wParam, LONG lParam);
void TearDownGraph(void);
BOOL BuildCaptureGraph();
BOOL BuildPreviewGraph();
void UpdateStatus(BOOL fAllStats);
void AddDevicesToMenu();
void ChooseDevices(TCHAR *szVideo, TCHAR *szAudio);
void ChooseDevices(IMoniker *pmVideo, IMoniker *pmAudio);
void ChooseFrameRate();
void ReadVideoPin();
BOOL InitCapFilters();
void FreeCapFilters();
BOOL StopPreview();
BOOL StartPreview();
BOOL StopCapture();
void FullScreen();
void ResizeChild(int sw, int sh);
DWORDLONG GetSize(LPCTSTR tach);
void MakeMenuOptions();
void OnClose();
// Adds/removes a DirectShow filter graph from the Running Object Table,
// allowing GraphEdit to "spy" on a remote filter graph if enabled.
HRESULT AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister);
void RemoveGraphFromRot(DWORD pdwRegister);
//------------------------------------------------------------------------------
// Name: SetAppCaption()
// Desc: Set the caption to be the application name followed by the capture file
//------------------------------------------------------------------------------
void SetAppCaption()
{
TCHAR tach[_MAX_PATH + 80];
lstrcpyn(tach, gszAppName, NUMELMS(tach));
if(gcap.szCaptureFile[0] != 0)
{
lstrcat(tach, TEXT(" - "));
lstrcat(tach, gcap.szCaptureFile);
}
SetWindowText(ghwndApp, tach);
}
//print help
void usage() {
wprintf(L"Usage: amcap [-v]\n-v, --version\tprint version\n");
}
//parse arguments
bool parse_arguments() {
LPWSTR *argv;
int argc;
argv = CommandLineToArgvW(GetCommandLine(), &argc);
if (argv == NULL)
{
wprintf(L"Unable to parse command line\n");
return false;
}
if (argc > 1) {
CString s(argv[1]);
if (!s.Compare(L"-v") || !s.Compare(L"--version")) {
wprintf(L"%s %s\n", TEXT(VER_DATE), TEXT(VER));
} else {
wprintf(L"Invalid argument.\n\n");
usage();
}
return false;
}
LocalFree(argv);
return true;
}
/*----------------------------------------------------------------------------*\
| AppInit( hInst, hPrev) |
| |
| Description: |
| This is called when the application is first loaded into |
| memory. It performs all initialization that doesn't need to be done |
| once per instance. |
| |
| Arguments: |
| hInstance instance handle of current instance |
| hPrev instance handle of previous instance |
| |
| Returns: |
| TRUE if successful, FALSE if not |
| |
\*----------------------------------------------------------------------------*/
BOOL AppInit(HINSTANCE hInst, HINSTANCE hPrev, int sw)
{
WNDCLASS cls;
HDC hdc;
const DWORD dwExStyle = 0;
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
DbgInitialise(hInst);
/* Save instance handle for DialogBoxs */
ghInstApp = hInst;
ghAccel = LoadAccelerators(hInst, MAKEINTATOM(ID_APP));
if(!hPrev)
{
/*
* Register a class for the main application window
*/
cls.hCursor = LoadCursor(NULL,IDC_ARROW);
cls.hIcon = LoadIcon(hInst, TEXT("AMCapIcon"));
cls.lpszMenuName = MAKEINTATOM(ID_APP);
cls.lpszClassName = MAKEINTATOM(ID_APP);
cls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
cls.hInstance = hInst;
cls.style = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
cls.lpfnWndProc = (WNDPROC) AppWndProc;
cls.cbWndExtra = 0;
cls.cbClsExtra = 0;
if(!RegisterClass(&cls))
return FALSE;
}
// Is this necessary?
ghfontApp = (HFONT)GetStockObject(ANSI_VAR_FONT);
hdc = GetDC(NULL);
SelectObject(hdc, ghfontApp);
GetTextMetrics(hdc, >m);
ReleaseDC(NULL, hdc);
ghwndApp=CreateWindowEx(dwExStyle,
MAKEINTATOM(ID_APP), // Class name
gszAppName, // Caption
// Style bits
ghwndStyle,
CW_USEDEFAULT, 0, // Position
320,300, // Size
(HWND)NULL, // Parent window (no parent)
(HMENU)NULL, // use class menu
hInst, // handle to window instance
(LPSTR)NULL // no params to pass on
);
// create the status bar
statusInit(hInst, hPrev);
ghwndStatus = CreateWindowEx(0,
szStatusClass,
NULL,
WS_CHILD|WS_BORDER|WS_VISIBLE|WS_CLIPSIBLINGS,
0, 0,
0, 0,
ghwndApp,
NULL,
hInst,
NULL);
if(ghwndStatus == NULL)
return(FALSE);
// Read the capture file name from win.ini
GetProfileString(TEXT("annie"), TEXT("CaptureFile"), TEXT(""),
gcap.szCaptureFile,
sizeof(gcap.szCaptureFile));
// Read the list of devices to use from win.ini
ZeroMemory(gcap.rgpmAudioMenu, sizeof(gcap.rgpmAudioMenu));
ZeroMemory(gcap.rgpmVideoMenu, sizeof(gcap.rgpmVideoMenu));
gcap.pmVideo = 0;
gcap.pmAudio = 0;
gcap.fMPEG2 = FALSE;
TCHAR szVideoDisplayName[1024], szAudioDisplayName[1024];
*szAudioDisplayName = *szVideoDisplayName = 0; // null terminate
GetProfileString(TEXT("annie"), TEXT("VideoDevice2"), TEXT(""),
szVideoDisplayName, NUMELMS(szVideoDisplayName));
GetProfileString(TEXT("annie"), TEXT("AudioDevice2"), TEXT(""),
szAudioDisplayName, NUMELMS(szAudioDisplayName));
gcap.fDeviceMenuPopulated = false;
AddDevicesToMenu();
// do we want audio?
gcap.fCapAudio = GetProfileInt(TEXT("annie"), TEXT("CaptureAudio"), TRUE);
gcap.fCapCC = GetProfileInt(TEXT("annie"), TEXT("CaptureCC"), FALSE);
// do we want preview?
gcap.fWantPreview = GetProfileInt(TEXT("annie"), TEXT("WantPreview"), FALSE);
// which stream should be the master? NONE(-1) means nothing special happens
// AUDIO(1) means the video frame rate is changed before written out to keep
// the movie in sync when the audio and video capture crystals are not the
// same (and therefore drift out of sync after a few minutes). VIDEO(0)
// means the audio sample rate is changed before written out
gcap.iMasterStream = GetProfileInt(TEXT("annie"), TEXT("MasterStream"), 1);
// get the frame rate from win.ini before making the graph
gcap.fUseFrameRate = GetProfileInt(TEXT("annie"), TEXT("UseFrameRate"), 1);
int units_per_frame = GetProfileInt(TEXT("annie"), TEXT("FrameRate"), 666667); // 15fps
gcap.FrameRate = 10000000. / units_per_frame;
gcap.FrameRate = (int)(gcap.FrameRate * 100) / 100.;
gcap.CaptureWidth = GetProfileInt(TEXT("annie"), TEXT("CaptureWidth"), 640);
gcap.CaptureHeight = GetProfileInt(TEXT("annie"), TEXT("CaptureHeight"), 360);
// reasonable default
if(gcap.FrameRate <= 0.)
gcap.FrameRate = 15.0;
gcap.fUseTimeLimit = GetProfileInt(TEXT("annie"), TEXT("UseTimeLimit"), 0);
gcap.dwTimeLimit = GetProfileInt(TEXT("annie"), TEXT("TimeLimit"), 0);
// Instantiate the capture filters we need to do the menu items.
// This will start previewing, if desired
//
// Make these the official devices we're using
ChooseDevices(szVideoDisplayName, szAudioDisplayName);
// Register for device add/remove notifications
DEV_BROADCAST_DEVICEINTERFACE filterData;
ZeroMemory(&filterData, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
filterData.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
filterData.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filterData.dbcc_classguid = AM_KSCATEGORY_CAPTURE;
gpUnregisterDeviceNotification = NULL;
gpRegisterDeviceNotification = NULL;
// dynload device removal APIs
{
HMODULE hmodUser = GetModuleHandle(TEXT("user32.dll"));
ASSERT(hmodUser); // we link to user32
gpUnregisterDeviceNotification = (PUnregisterDeviceNotification)
GetProcAddress(hmodUser, "UnregisterDeviceNotification");
// m_pRegisterDeviceNotification is prototyped differently in unicode
gpRegisterDeviceNotification = (PRegisterDeviceNotification)
GetProcAddress(hmodUser,
#ifdef UNICODE
"RegisterDeviceNotificationW"
#else
"RegisterDeviceNotificationA"
#endif
);
// failures expected on older platforms.
ASSERT(gpRegisterDeviceNotification && gpUnregisterDeviceNotification ||
!gpRegisterDeviceNotification && !gpUnregisterDeviceNotification);
}
ghDevNotify = NULL;
if(gpRegisterDeviceNotification)
{
ghDevNotify = gpRegisterDeviceNotification(ghwndApp, &filterData, DEVICE_NOTIFY_WINDOW_HANDLE);
ASSERT(ghDevNotify != NULL);
}
SetAppCaption();
ShowWindow(ghwndApp,sw);
return TRUE;
}
void IMonRelease(IMoniker *&pm)
{
if(pm)
{
pm->Release();
pm = 0;
}
}
/*----------------------------------------------------------------------------*\
| WinMain( hInst, hPrev, lpszCmdLine, cmdShow ) |
| |
| Description: |
| The main procedure for the App. After initializing, it just goes |
| into a message-processing loop until it gets a WM_QUIT message |
| (meaning the app was closed). |
| |
| Arguments: |
| hInst instance handle of this instance of the app |
| hPrev instance handle of previous instance, NULL if first |
| szCmdLine ->null-terminated command line |
| cmdShow specifies how the window is initially displayed |
| |
| Returns: |
| The exit code as specified in the WM_QUIT message. |
| |
\*----------------------------------------------------------------------------*/
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
MSG msg;
if (!parse_arguments()) return 0;
/* Call initialization procedure */
if(!AppInit(hInst,hPrev,sw))
return FALSE;
/*
* Polling messages from event queue
*/
for(;;)
{
while(PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break; // Leave the PeekMessage while() loop
if(TranslateAccelerator(ghwndApp, ghAccel, &msg))
continue;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT)
break; // Leave the for() loop
WaitMessage();
}
// Reached on WM_QUIT message
CoUninitialize();
return ((int) msg.wParam);
}
/*----------------------------------------------------------------------------*\
| AppWndProc( hwnd, uiMessage, wParam, lParam ) |
| |
| Description: |
| The window proc for the app's main (tiled) window. This processes all |
| of the parent window's messages. |
| |
| Arguments: |
| hwnd window handle for the window |
| msg message number |
| wParam message-dependent |
| lParam message-dependent |
| |
| Returns: |
| 0 if processed, nonzero if ignored |
| |
\*----------------------------------------------------------------------------*/
LONG WINAPI AppWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rc;
int cxBorder, cyBorder, cy;
switch(msg)
{
case WM_CREATE:
break;
case WM_COMMAND:
return AppCommand(hwnd,msg,wParam,lParam);
case WM_INITMENU:
// we can start capture if not capturing already
EnableMenuItem((HMENU)wParam, MENU_START_CAP,
(!gcap.fCapturing) ? MF_ENABLED : MF_GRAYED);
// we can stop capture if it's currently capturing
EnableMenuItem((HMENU)wParam, MENU_STOP_CAP,
(gcap.fCapturing) ? MF_ENABLED : MF_GRAYED);
// We can bring up a dialog if the graph is stopped
EnableMenuItem((HMENU)wParam, MENU_DIALOG0, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOG1, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOG2, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOG3, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOG4, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOG5, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOG6, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOG7, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOG8, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOG9, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOGA, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOGB, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOGC, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOGD, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOGE, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_DIALOGF, !gcap.fCapturing ?
MF_ENABLED : MF_GRAYED);
// toggles capturing audio or not - can't be capturing right now
// and we must have an audio capture device created
EnableMenuItem((HMENU)wParam, MENU_CAP_AUDIO,
(!gcap.fCapturing && gcap.pACap) ? MF_ENABLED : MF_GRAYED);
// are we capturing audio?
CheckMenuItem((HMENU)wParam, MENU_CAP_AUDIO,
(gcap.fCapAudio) ? MF_CHECKED : MF_UNCHECKED);
// are we doing closed captioning?
CheckMenuItem((HMENU)wParam, MENU_CAP_CC,
(gcap.fCapCC) ? MF_CHECKED : MF_UNCHECKED);
EnableMenuItem((HMENU)wParam, MENU_CAP_CC,
(gcap.fCCAvail) ? MF_ENABLED : MF_GRAYED);
// change audio formats when not capturing
EnableMenuItem((HMENU)wParam, MENU_AUDIOFORMAT, (gcap.fCapAudio &&
!gcap.fCapturing) ? MF_ENABLED : MF_GRAYED);
// change frame rate when not capturing, and only if the video
// filter captures a VIDEOINFO type format
EnableMenuItem((HMENU)wParam, MENU_FRAMERATE,
(!gcap.fCapturing && gcap.fCapAudioIsRelevant) ?
MF_ENABLED : MF_GRAYED);
// change time limit when not capturing
EnableMenuItem((HMENU)wParam, MENU_TIMELIMIT,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
// change capture file name when not capturing
EnableMenuItem((HMENU)wParam, MENU_SET_CAP_FILE,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
// pre-allocate capture file when not capturing
EnableMenuItem((HMENU)wParam, MENU_ALLOC_CAP_FILE,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
// can save capture file when not capturing
EnableMenuItem((HMENU)wParam, MENU_SAVE_CAP_FILE,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
// do we want preview?
CheckMenuItem((HMENU)wParam, MENU_PREVIEW,
(gcap.fWantPreview) ? MF_CHECKED : MF_UNCHECKED);
// can toggle preview if not capturing
EnableMenuItem((HMENU)wParam, MENU_PREVIEW,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
// MPEG2 enabled ?
CheckMenuItem((HMENU)wParam, MENU_MPEG2,
(gcap.fMPEG2) ? MF_CHECKED : MF_UNCHECKED);
// can toggle MPEG2 if not capturing
EnableMenuItem((HMENU)wParam, MENU_MPEG2,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
// which is the master stream? Not applicable unless we're also
// capturing audio
EnableMenuItem((HMENU)wParam, MENU_NOMASTER,
(!gcap.fCapturing && gcap.fCapAudio) ? MF_ENABLED : MF_GRAYED);
CheckMenuItem((HMENU)wParam, MENU_NOMASTER,
(gcap.iMasterStream == -1) ? MF_CHECKED : MF_UNCHECKED);
EnableMenuItem((HMENU)wParam, MENU_AUDIOMASTER,
(!gcap.fCapturing && gcap.fCapAudio) ? MF_ENABLED : MF_GRAYED);
CheckMenuItem((HMENU)wParam, MENU_AUDIOMASTER,
(gcap.iMasterStream == 1) ? MF_CHECKED : MF_UNCHECKED);
EnableMenuItem((HMENU)wParam, MENU_VIDEOMASTER,
(!gcap.fCapturing && gcap.fCapAudio) ? MF_ENABLED : MF_GRAYED);
CheckMenuItem((HMENU)wParam, MENU_VIDEOMASTER,
(gcap.iMasterStream == 0) ? MF_CHECKED : MF_UNCHECKED);
// can't select a new capture device when capturing
EnableMenuItem((HMENU)wParam, MENU_VDEVICE0,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_VDEVICE1,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_VDEVICE2,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_VDEVICE3,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_VDEVICE4,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_VDEVICE5,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_VDEVICE6,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_VDEVICE7,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_VDEVICE8,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_VDEVICE9,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE0,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE1,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE2,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE3,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE4,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE5,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE6,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE7,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE8,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
EnableMenuItem((HMENU)wParam, MENU_ADEVICE9,
!gcap.fCapturing ? MF_ENABLED : MF_GRAYED);
break;
case WM_INITMENUPOPUP:
if(GetSubMenu(GetMenu(ghwndApp), 1) == (HMENU)wParam)
{
AddDevicesToMenu();
}
break;
//
// We're out of here!
//
case WM_DESTROY:
DbgTerminate();
IMonRelease(gcap.pmVideo);
IMonRelease(gcap.pmAudio);
{
for(int i = 0; i < NUMELMS(gcap.rgpmVideoMenu); i++)
{
IMonRelease(gcap.rgpmVideoMenu[i]);
}
for(int i = 0; i < NUMELMS(gcap.rgpmAudioMenu); i++)
{
IMonRelease(gcap.rgpmAudioMenu[i]);
}
}
PostQuitMessage(0);
break;
case WM_CLOSE:
OnClose();
break;
case WM_ENDSESSION:
if(wParam || (lParam & ENDSESSION_LOGOFF))
{
OnClose();
}
break;
case WM_ERASEBKGND:
break;
// ESC will stop capture
case WM_KEYDOWN:
if((GetAsyncKeyState(VK_ESCAPE) & 0x01) && gcap.fCapturing)
{
StopCapture();
if(gcap.fWantPreview)
{
BuildPreviewGraph();
StartPreview();
}
}
switch(LOWORD( wParam ))
{
case VK_SPACE:
SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(MENU_PREVIEW, 0), 0);
break;
case VK_ESCAPE:
FullScreen();
break;
}
break;
case WM_LBUTTONDBLCLK:
FullScreen();
break;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
// nothing to do
EndPaint(hwnd,&ps);
break;
case WM_TIMER:
// update our status bar with #captured, #dropped
// if we've stopped capturing, don't do it anymore. Some WM_TIMER
// messages may come late, after we've destroyed the graph and
// we'll get invalid numbers.
if(gcap.fCapturing)
UpdateStatus(FALSE);
// is our time limit up?
if(gcap.fUseTimeLimit)
{
if((timeGetTime() - gcap.lCapStartTime) / 1000 >=
gcap.dwTimeLimit)
{
StopCapture();
if(gcap.fWantPreview)
{
BuildPreviewGraph();
StartPreview();
}
}
}
break;
case WM_SIZE:
// make the preview window fit inside our window, taking up
// all of our client area except for the status window at the
// bottom
GetClientRect(ghwndApp, &rc);
cxBorder = GetSystemMetrics(SM_CXBORDER);
cyBorder = GetSystemMetrics(SM_CYBORDER);
cy = statusGetHeight() + cyBorder;
if (GetWindowLong(ghwndStatus, GWL_STYLE) & WS_VISIBLE)
{
MoveWindow(ghwndStatus, -cxBorder, rc.bottom - cy,
rc.right + (2 * cxBorder), cy + cyBorder, TRUE);
rc.bottom -= cy;
}
// this is the video renderer window showing the preview
ResizeChild(rc.right, rc.bottom);
break;
case WM_FGNOTIFY:
// uh-oh, something went wrong while capturing - the filtergraph
// will send us events like EC_COMPLETE, EC_USERABORT and the one
// we care about, EC_ERRORABORT.
if(gcap.pME)
{
LONG event;
LONG_PTR l1, l2;
HRESULT hrAbort = S_OK;
BOOL bAbort = FALSE;
while(gcap.pME->GetEvent(&event, &l1, &l2, 0) == S_OK)
{
gcap.pME->FreeEventParams(event, l1, l2);
if(event == EC_ERRORABORT)
{
StopCapture();
bAbort = TRUE;
hrAbort = static_cast<HRESULT>(l1);
continue;
}
else if(event == EC_DEVICE_LOST)
{
// Check if we have lost a capture filter being used.
// lParam2 of EC_DEVICE_LOST event == 1 indicates device added
// == 0 indicates device removed
if(l2 == 0)
{
IBaseFilter *pf;
IUnknown *punk = (IUnknown *) l1;
if(S_OK == punk->QueryInterface(IID_IBaseFilter, (void **) &pf))
{
if(::IsEqualObject(gcap.pVCap, pf))
{
pf->Release();
bAbort = FALSE;
StopCapture();
TCHAR szError[100];
_tcscpy(szError,
TEXT("Stopping Capture (Device Lost). Select New Capture Device\0"));
ErrMsg(szError);
break;
}
pf->Release();
}
}
}
} // end while
if(bAbort)
{
if(gcap.fWantPreview)
{
BuildPreviewGraph();
StartPreview();
}
TCHAR szError[100];
wsprintf(szError, TEXT("ERROR during capture, error code=%08x\0"), hrAbort);
ErrMsg(szError);
}
}
break;
case WM_DEVICECHANGE:
// We are interested in only device arrival & removal events
if(DBT_DEVICEARRIVAL != wParam && DBT_DEVICEREMOVECOMPLETE != wParam)
break;
PDEV_BROADCAST_HDR pdbh = (PDEV_BROADCAST_HDR) lParam;
if(pdbh->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE)
{
break;
}
PDEV_BROADCAST_DEVICEINTERFACE pdbi = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;
// Check for capture devices.
if(pdbi->dbcc_classguid != AM_KSCATEGORY_CAPTURE)
{
break;
}
// Check for device arrival/removal.
if(DBT_DEVICEARRIVAL == wParam || DBT_DEVICEREMOVECOMPLETE == wParam)
{
gcap.fDeviceMenuPopulated = false;
}
break;
}
return (LONG) DefWindowProc(hwnd,msg,wParam,lParam);
}
// Make a graph builder object we can use for capture graph building
//
BOOL MakeBuilder()
{
// we have one already
if(gcap.pBuilder)
return TRUE;
gcap.pBuilder = new ISampleCaptureGraphBuilder( );
if( NULL == gcap.pBuilder )
{
return FALSE;
}
return TRUE;
}
// Make a graph object we can use for capture graph building
//
BOOL MakeGraph()
{
// we have one already
if(gcap.pFg)
return TRUE;
HRESULT hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC,
IID_IGraphBuilder, (LPVOID *)&gcap.pFg);
return (hr == NOERROR) ? TRUE : FALSE;
}
// make sure the preview window inside our window is as big as the
// dimensions of captured video, or some capture cards won't show a preview.
// (Also, it helps people tell what size video they're capturing)
// We will resize our app's window big enough so that once the status bar
// is positioned at the bottom there will be enough room for the preview
// window to be w x h
//
int gnRecurse = 0;
void ResizeWindow(int w, int h)
{
RECT rcW, rcC;
int xExtra, yExtra;
int cyBorder = GetSystemMetrics(SM_CYBORDER);
gnRecurse++;
GetWindowRect(ghwndApp, &rcW);
GetClientRect(ghwndApp, &rcC);
xExtra = rcW.right - rcW.left - rcC.right;
yExtra = rcW.bottom - rcW.top - rcC.bottom + cyBorder + statusGetHeight();
rcC.right = w;
rcC.bottom = h;
SetWindowPos(ghwndApp, NULL, 0, 0, rcC.right + xExtra,
rcC.bottom + yExtra, SWP_NOZORDER | SWP_NOMOVE);
// we may need to recurse once. But more than that means the window cannot
// be made the size we want, trying will just stack fault.
//
if(gnRecurse == 1 && ((rcC.right + xExtra != rcW.right - rcW.left && w > GetSystemMetrics(SM_CXMIN)) ||
(rcC.bottom + yExtra != rcW.bottom - rcW.top)))
ResizeWindow(w,h);
gnRecurse--;
ResizeChild(rcC.right + xExtra, rcC.bottom + yExtra);
}
void ResizeChild(int sw, int sh)
{
int nw = 0, nh = 0, w = 0, h = 0, l = 0, t = 0;