-
Notifications
You must be signed in to change notification settings - Fork 1
/
CapturePreviewDlg.cpp
423 lines (345 loc) · 11.9 KB
/
CapturePreviewDlg.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
/* -LICENSE-START-
** Copyright (c) 2013 Blackmagic Design
**
** Permission is hereby granted, free of charge, to any person or organization
** obtaining a copy of the software and accompanying documentation covered by
** this license (the "Software") to use, reproduce, display, distribute,
** execute, and transmit the Software, and to prepare derivative works of the
** Software, and to permit third-parties to whom the Software is furnished to
** do so, all subject to the following:
**
** The copyright notices in the Software and this entire statement, including
** the above license grant, this restriction and the following disclaimer,
** must be included in all copies of the Software, in whole or in part, and
** all derivative works of the Software, unless such copies or derivative
** works are solely in the form of machine-executable object code generated by
** a source language processor.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
** -LICENSE-END-
*/
// CapturePreviewDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CapturePreview.h"
#include "CapturePreviewDlg.h"
#include "DeckLinkDevice.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace std;
CCapturePreviewDlg::CCapturePreviewDlg(CWnd* pParent)
: CDialog(CCapturePreviewDlg::IDD, pParent), m_deckLinkDiscovery(NULL),
m_selectedDevice(NULL)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCapturePreviewDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_INPUT_DEVICE_COMBO, m_deviceListCombo);
DDX_Control(pDX, IDC_AUTODETECT_FORMAT_CHECK, m_applyDetectedInputModeCheckbox);
DDX_Control(pDX, IDC_INPUT_MODE_COMBO, m_modeListCombo);
DDX_Control(pDX, IDC_START_STOP_BUTTON, m_startStopButton);
DDX_Control(pDX, IDC_INVALID_INPUT_STATIC, m_invalidInputLabel);
DDX_Control(pDX, IDC_VITC_TC_F1_STATIC, m_vitcTcF1);
DDX_Control(pDX, IDC_VITC_UB_F1_STATIC, m_vitcUbF1);
DDX_Control(pDX, IDC_VITC_TC_F2_STATIC, m_vitcTcF2);
DDX_Control(pDX, IDC_VITC_UB_F2__STATIC, m_vitcUbF2);
DDX_Control(pDX, IDC_RP188_VITC1_TC_STATIC, m_rp188Vitc1Tc);
DDX_Control(pDX, IDC_RP188_VITC1_UB_STATIC, m_rp188Vitc1Ub);
DDX_Control(pDX, IDC_RP188_VITC2_TC_STATIC, m_rp188Vitc2Tc);
DDX_Control(pDX, IDC_RP188_VITC2_TC__STATIC, m_rp188Vitc2Ub);
DDX_Control(pDX, IDC_RP188_LTC_TC_STATIC, m_rp188LtcTc);
DDX_Control(pDX, IDC_RP188_LTC_UB_STATIC, m_rp188LtcUb);
DDX_Control(pDX, IDC_PREVIEW_BOX, m_previewBox);
}
BEGIN_MESSAGE_MAP(CCapturePreviewDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
// UI element messages
ON_BN_CLICKED(IDC_START_STOP_BUTTON, &CCapturePreviewDlg::OnStartStopBnClicked)
ON_CBN_SELCHANGE(IDC_INPUT_DEVICE_COMBO, &CCapturePreviewDlg::OnNewDeviceSelected)
// Custom messages
ON_MESSAGE(WM_REFRESH_INPUT_STREAM_DATA_MESSAGE, &CCapturePreviewDlg::OnRefreshInputStreamData)
ON_MESSAGE(WM_SELECT_VIDEO_MODE_MESSAGE, &CCapturePreviewDlg::OnSelectVideoMode)
ON_MESSAGE(WM_ADD_DEVICE_MESSAGE, &CCapturePreviewDlg::OnAddDevice)
ON_MESSAGE(WM_REMOVE_DEVICE_MESSAGE, &CCapturePreviewDlg::OnRemoveDevice)
ON_MESSAGE(WM_ERROR_RESTARTING_CAPTURE_MESSAGE, &CCapturePreviewDlg::OnErrorRestartingCapture)
END_MESSAGE_MAP()
void CCapturePreviewDlg::OnStartStopBnClicked()
{
if (m_selectedDevice == NULL)
return;
if (m_selectedDevice->IsCapturing())
StopCapture();
else
StartCapture();
}
void CCapturePreviewDlg::OnNewDeviceSelected()
{
int selectedDeviceIndex;
selectedDeviceIndex = m_deviceListCombo.GetCurSel();
if (selectedDeviceIndex < 0)
return;
m_selectedDevice = (DeckLinkDevice*) m_deviceListCombo.GetItemDataPtr(selectedDeviceIndex);
// Update the video mode popup menu
RefreshVideoModeList();
// Enable the interface
EnableInterface(true);
if (m_selectedDevice->SupportsFormatDetection())
m_applyDetectedInputModeCheckbox.SetCheck(BST_CHECKED);
}
void CCapturePreviewDlg::OnClose()
{
// Stop the capture
StopCapture();
// Release all DeckLinkDevice instances
while(m_deviceListCombo.GetCount() > 0)
{
DeckLinkDevice* device = (DeckLinkDevice*)m_deviceListCombo.GetItemDataPtr(0);
device->Release();
m_deviceListCombo.DeleteString(0);
}
if (m_previewWindow != NULL)
{
delete m_previewWindow;
m_previewWindow = NULL;
}
// Release DeckLink discovery instance
if (m_deckLinkDiscovery != NULL)
{
m_deckLinkDiscovery->Disable();
m_deckLinkDiscovery->Release();
m_deckLinkDiscovery = NULL;
}
CDialog::OnClose();
}
void CCapturePreviewDlg::ShowErrorMessage(TCHAR* msg, TCHAR* title)
{
MessageBox(msg, title);
}
void CCapturePreviewDlg::RefreshVideoModeList()
{
vector<CString> modeNames;
unsigned int modeIndex = 0;
// Clear the menu
m_modeListCombo.ResetContent();
// Get the mode names
m_selectedDevice->GetDisplayModeNames(modeNames);
// Add them to the menu
while (modeIndex < modeNames.size())
m_modeListCombo.AddString(modeNames[modeIndex++]);
m_modeListCombo.SetCurSel(0);
}
void CCapturePreviewDlg::StartCapture()
{
bool applyDetectedInputMode = (m_applyDetectedInputModeCheckbox.GetCheck() == BST_CHECKED);
if (m_selectedDevice &&
m_selectedDevice->StartCapture(m_modeListCombo.GetCurSel(), m_previewWindow, applyDetectedInputMode))
{
// Update UI
m_startStopButton.SetWindowText(_T("Stop capture"));
EnableInterface(false);
}
}
void CCapturePreviewDlg::StopCapture()
{
if (m_selectedDevice)
m_selectedDevice->StopCapture();
// Update UI
m_startStopButton.SetWindowText(_T("Start capture"));
EnableInterface(true);
m_invalidInputLabel.ShowWindow(SW_HIDE);
}
void CCapturePreviewDlg::EnableInterface(bool enabled)
{
m_deviceListCombo.EnableWindow((enabled) ? TRUE : FALSE);
m_modeListCombo.EnableWindow((enabled) ? TRUE : FALSE);
if (enabled)
{
if (m_selectedDevice && m_selectedDevice->SupportsFormatDetection())
{
m_applyDetectedInputModeCheckbox.EnableWindow(TRUE);
}
else
{
m_applyDetectedInputModeCheckbox.EnableWindow(FALSE);
m_applyDetectedInputModeCheckbox.SetCheck(BST_UNCHECKED);
}
}
else
m_applyDetectedInputModeCheckbox.EnableWindow(FALSE);
}
BOOL CCapturePreviewDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, FALSE);
// Empty popup menus
m_deviceListCombo.ResetContent();
m_modeListCombo.ResetContent();
// Disable the interface
m_startStopButton.EnableWindow(FALSE);
EnableInterface(false);
// Create and initialise preview and DeckLink device discovery objects
m_previewWindow = new PreviewWindow();
if (m_previewWindow->init(&m_previewBox) == false)
{
ShowErrorMessage(_T("This application was unable to initialise the preview window"), _T("Error"));
goto bail;
}
m_deckLinkDiscovery = new DeckLinkDeviceDiscovery(this);
if (! m_deckLinkDiscovery->Enable())
ShowErrorMessage(_T("Please install the Blackmagic Desktop Video drivers to use the features of this application."), _T("This application requires the Desktop Video drivers installed."));
bail:
return TRUE;
}
void CCapturePreviewDlg::AddDevice(IDeckLink* deckLink)
{
int deviceIndex;
DeckLinkDevice* newDevice = new DeckLinkDevice(this, deckLink);
// Initialise new DeckLinkDevice object
if (!newDevice->Init())
{
newDevice->Release();
return;
}
// Add this DeckLink device to the device list
deviceIndex = m_deviceListCombo.AddString(newDevice->GetDeviceName());
if (deviceIndex < 0)
return;
m_deviceListCombo.SetItemDataPtr(deviceIndex, newDevice);
if (m_deviceListCombo.GetCount() == 1)
{
// We have added our first item, refresh and enable UI
m_deviceListCombo.SetCurSel(0);
OnNewDeviceSelected();
m_startStopButton.EnableWindow(TRUE);
EnableInterface(true);
}
}
void CCapturePreviewDlg::RemoveDevice(IDeckLink* deckLink)
{
int deviceIndex = -1;
DeckLinkDevice* deviceToRemove = NULL;
// Find the combo box entry to remove (there may be multiple entries with the same name, but each
// will have a different data pointer).
for (deviceIndex = 0; deviceIndex < m_deviceListCombo.GetCount(); ++deviceIndex)
{
deviceToRemove = (DeckLinkDevice*)m_deviceListCombo.GetItemDataPtr(deviceIndex);
if (deviceToRemove->DeckLinkInstance() == deckLink)
break;
}
if (deviceToRemove == NULL)
return;
// Stop capturing before removal
if (deviceToRemove->IsCapturing())
deviceToRemove->StopCapture();
// Remove device from list
m_deviceListCombo.DeleteString(deviceIndex);
// Refresh UI
m_startStopButton.SetWindowText(_T("Start capture"));
// Check how many devices are left
if (m_deviceListCombo.GetCount() == 0)
{
// We have removed the last device, disable the interface.
m_startStopButton.EnableWindow(FALSE);
EnableInterface(false);
m_selectedDevice = NULL;
}
else if (m_selectedDevice == deviceToRemove)
{
// The device that was removed was the one selected in the UI.
// Select the first available device in the list and reset the UI.
m_deviceListCombo.SetCurSel(0);
OnNewDeviceSelected();
m_startStopButton.EnableWindow(TRUE);
m_invalidInputLabel.ShowWindow(SW_HIDE);
}
// Release DeckLinkDevice instance
deviceToRemove->Release();
}
void CCapturePreviewDlg::UpdateAncillaryData(AncillaryDataStruct& ancillaryData)
{
// Copy ancillary data under protection of critsec object
m_critSec.Lock();
m_ancillaryData = ancillaryData;
m_critSec.Unlock();
}
LRESULT CCapturePreviewDlg::OnRefreshInputStreamData(WPARAM wParam, LPARAM lParam)
{
// Update the UI under protection of critsec object
m_critSec.Lock();
m_vitcTcF1.SetWindowText(m_ancillaryData.vitcF1Timecode);
m_vitcUbF1.SetWindowText(m_ancillaryData.vitcF1UserBits);
m_vitcTcF2.SetWindowText(m_ancillaryData.vitcF2Timecode);
m_vitcUbF2.SetWindowText(m_ancillaryData.vitcF2UserBits);
m_rp188Vitc1Tc.SetWindowText(m_ancillaryData.rp188vitc1Timecode);
m_rp188Vitc1Ub.SetWindowText(m_ancillaryData.rp188vitc1UserBits);
m_rp188Vitc2Tc.SetWindowText(m_ancillaryData.rp188vitc2Timecode);
m_rp188Vitc2Ub.SetWindowText(m_ancillaryData.rp188vitc2UserBits);
m_rp188LtcTc.SetWindowText(m_ancillaryData.rp188ltcTimecode);
m_rp188LtcUb.SetWindowText(m_ancillaryData.rp188ltcUserBits);
m_critSec.Unlock();
m_invalidInputLabel.ShowWindow((wParam) ? SW_SHOW : SW_HIDE);
return 0;
}
LRESULT CCapturePreviewDlg::OnSelectVideoMode(WPARAM wParam, LPARAM lParam)
{
// A new video mode was selected by the user
m_modeListCombo.SetCurSel((unsigned int) wParam);
return 0;
}
LRESULT CCapturePreviewDlg::OnAddDevice(WPARAM wParam, LPARAM lParam)
{
// A new device has been connected
AddDevice((IDeckLink*)wParam);
return 0;
}
LRESULT CCapturePreviewDlg::OnRemoveDevice(WPARAM wParam, LPARAM lParam)
{
// An existing device has been disconnected
RemoveDevice((IDeckLink*)wParam);
return 0;
}
LRESULT CCapturePreviewDlg::OnErrorRestartingCapture(WPARAM wParam, LPARAM lParam)
{
// A change in the input video mode was detected, but the capture could not be restarted.
StopCapture();
ShowErrorMessage(_T("This application was unable to apply the detected input video mode."), _T("Error restarting the capture."));
return 0;
}
void CCapturePreviewDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCapturePreviewDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}