~stanislav-fomin/minidv2avi/trunk

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
// DVToolsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "process.h"
#include "minidv2avi.h"
#include "DShow.h"
#include "ToolTab.h"
#include "DVToolsDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


void UrlThread(void *ptr)
{
	ShellExecute(NULL, "open", (char *)ptr, NULL, NULL, SW_SHOWNORMAL);
}


/////////////////////////////////////////////////////////////////////////////
// CDVToolsDlg dialog

#define IDC_TAB_CHANGE 0x100

enum {TAB_ALL=-1, TAB_NONE=0};

#define XL 25
#define XR 75

static struct CtrlProperties {int id; int dx, dw, dy, dh; int tabMask;} ctrlProperties[] = 
{
	{IDC_VIDEO,		  0,100,  0,100,	TAB_ALL},
	{IDC_PICTURE,	 XR, XR,100,100,	TAB_ALL},
	{IDC_VSRC_L,	 XL, XL,100,100,	TAB_ALL},
	{IDC_VSRC,		 XL, XR,100,100,	TAB_ALL},
	{IDC_VSRC_SEL,	 XR, XR,100,100,	TAB_ALL},
	{IDC_CAPTURE,	 XR, XR,100,100,	TAB_ALL},
	{IDCANCEL,		 XR, XR,100,100,	TAB_ALL},
	{IDC_COUNTER,	 XR, XR,100,100,	TAB_ALL},
	{IDC_STATUS,	 XL, XR,100,100,	TAB_ALL},
	{IDC_STATUS2,	 XR, XR,100,100,	TAB_ALL},
	{IDC_STATUS3,	 XR, XR,100,100,	TAB_ALL}
};

#define NControls (sizeof ctrlProperties/sizeof (struct CtrlProperties))

bool CaptureFilenameExtractBase(CString &file)
{
	int pos = file.Find(".");
	if (pos>=0) file = file.Mid(0, pos);
	return ! file.IsEmpty();
}


CDVToolsDlg::CDVToolsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDVToolsDlg::IDD, pParent) 
{
	//{{AFX_DATA_INIT(CDVToolsDlg)
	//}}AFX_DATA_INIT

	m_hIcon      = (HICON)LoadImage(AfxGetResourceHandle(), 
	                         MAKEINTRESOURCE(IDR_MAINFRAME), 
							 IMAGE_ICON,
							 0, 0, LR_DEFAULTSIZE);
	m_hIconSmall = (HICON)LoadImage(AfxGetResourceHandle(), 
	                         MAKEINTRESOURCE(IDR_MAINFRAME), 
							 IMAGE_ICON,
							 16, 16, 0);

	m_originalRect.right = 0;
	m_minWidth = m_minHeight = 1;
	m_exitOnFinish = 0;
}

CDVToolsDlg::~CDVToolsDlg()
{
}

void CDVToolsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDVToolsDlg)
	DDX_Control(pDX, IDC_VIDEO, m_video);
	DDX_Control(pDX, IDC_STATUS, m_status);
	DDX_Control(pDX, IDC_CAPTURE, m_capture_btn);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDVToolsDlg, CDialog)
	//{{AFX_MSG_MAP(CDVToolsDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_SIZE()
	ON_WM_GETMINMAXINFO()
	ON_WM_CLOSE()
	ON_WM_CTLCOLOR()
	ON_WM_MOVE()
	ON_BN_CLICKED(IDC_CAPTURE, OnCapture)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_DV_TIMECHANGE, OnDVTimeChange)
	ON_BN_CLICKED(IDREW, &CDVToolsDlg::OnBnClickedRew)
	ON_BN_CLICKED(IDCANCEL, &CDVToolsDlg::OnBnClickedCancel)
	ON_STN_CLICKED(IDC_STATUS, &CDVToolsDlg::OnStnClickedStatus)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDVToolsDlg message handlers

BOOL CDVToolsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	GetClientRect(&m_originalRect);
	GetWindowRect(&m_lastRect);

	m_minWidth = m_lastRect.right - m_lastRect.left;
	m_minHeight = m_lastRect.bottom - m_lastRect.top;
	int pgNo = 0;
	CString tmp;
	RECT rc = {0,0,0,0};

	m_VSRCname = "Microsoft DV Camera and VCR"; 

	return TRUE;  
}

void CDVToolsDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	CDialog::OnSysCommand(nID, lParam);
}

void CDVToolsDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDVToolsDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDVToolsDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnGetMinMaxInfo(lpMMI);
	lpMMI->ptMinTrackSize.x = m_minWidth;
	lpMMI->ptMinTrackSize.y = m_minHeight;
}

void CDVToolsDlg::OnMove(int x, int y) 
{
	CDialog::OnMove(x, y);
	
	if (!IsIconic() && !IsZoomed())	GetWindowRect(&m_lastRect);
}



HBRUSH CDVToolsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	if (pWnd->GetDlgCtrlID() == IDC_VIDEO) {
		hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
	}
	
	return hbr;
}

void CDVToolsDlg::OnCancel() 
{
	InitVideo();
}

void CDVToolsDlg::OnClose() 
{
	m_video.Destroy();
	CDialog::OnCancel();
}

void CDVToolsDlg::OnOK() 
{
}

void CDVToolsDlg::Exception2Status(CException *e)
{
	TCHAR buf[1024];
	e->GetErrorMessage(buf, sizeof buf);
	CString tmp;
	tmp.Format("Error: %s", buf);
	m_status.SetWindowText(tmp);
}

void CDVToolsDlg::InitVideo()
{
	m_exitOnFinish = 0;

	KillTimer(1);
	m_status.SetWindowText("Wait a sec! Warming up..");

	TRY {
		CString filename;
		m_video.BuildCapturing(m_VSRCname);
		SetTimer(1, 200, NULL);
	}
	CATCH_ALL(e) {
		Exception2Status(e);
	}
	END_CATCH_ALL;
}


void CDVToolsDlg::OnCapture() 
{
	if (m_video.GetState() == CDV::CapturePaused) {
		TRY {
			m_video.StartCapturing();
		}
		CATCH_ALL(e) {
			InitVideo();
			Exception2Status(e);
		}
		END_CATCH_ALL;
	}
	else if (m_video.GetState() == CDV::Capturing) {
		TRY {
			m_video.StopCapturing();
		}
		CATCH_ALL(e) {
			InitVideo();
			Exception2Status(e);
		}
		END_CATCH_ALL;
	}
	else {
		TRY {
			InitVideo();
			m_video.StartCapturing();
		}
		CATCH_ALL(e) {
			InitVideo();
			Exception2Status(e);
		}
		END_CATCH_ALL;
	}

	CString capture_button_text("START"); 
	CString tmp;

	if (m_video.GetState() == CDV::Capturing) {
		capture_button_text = "PAUSE";
	}

	m_capture_btn.GetWindowText(tmp);
	if (tmp != capture_button_text) m_capture_btn.SetWindowText(capture_button_text);
}

void CDVToolsDlg::OnTimer(UINT nIDEvent) 
{
	if (m_video.GetState() == CDV::Finished && m_exitOnFinish) {
		OnClose();
	}

	DrawStatus();
	CDialog::OnTimer(nIDEvent);
}



void CDVToolsDlg::DrawStatus() 
{
	SetThreadExecutionState(ES_DISPLAY_REQUIRED);

	CString txt, tmp;

	struct tm * pltm;
	pltm = localtime(&m_tape_time);
	char buf[100] = "";
	strftime(buf, sizeof buf, "CAMERA TIME: %Y-%m-%d %H:%M:%S", pltm);
	txt += buf;
	txt += "    ";

	switch (m_video.GetState()) {
	case CDV::Capturing: {
		txt += "     Capturing.    ";

		REFERENCE_TIME t = m_video.GetTime();
		if (t >= 0) 
		{
			t /= 1000000;
			int ss = (int)(t % 10); t /= 10;
			int s = (int)(t % 60); t /= 60;
			int m = (int)(t % 60); t /= 60;
			tmp.Format("CAPTURED: %d:%02d:%02d.%01d", (int)t, m, s, ss);
			txt += "    " + tmp;
		}


		tmp.Format(" QUEUE: %i/%i DROPPED: %i ", 
						m_video.m_queue->m_load, 
						m_video.m_queue->m_queueSize,
						m_video.GetDropped());
		
		txt += "    " + tmp;
		break;
	}
	case CDV::CapturePaused: txt += "Paused. ";
		break;
	case CDV::Finished: txt += "Finished. ";
		break;
	}


	m_status.GetWindowText(tmp);
	if (tmp != txt) m_status.SetWindowText(txt);
}


LRESULT CDVToolsDlg::OnDVTimeChange(WPARAM, LPARAM lParam)
{
	m_tape_time = lParam;
	return 0;
}



void CDVToolsDlg::OnBnClickedRew()
{
	TRY {
		OnCancel();
		m_video.Rew();
	}
	CATCH_ALL(e) {
		InitVideo();
		Exception2Status(e);
	}
	END_CATCH_ALL;
}


void CDVToolsDlg::OnBnClickedCancel()
{
	OnCancel();
}

void CDVToolsDlg::OnStnClickedStatus()
{
	// TODO: Add your control notification handler code here
}