-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeckcontrol.cpp
303 lines (269 loc) · 8.25 KB
/
deckcontrol.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
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "DeckLinkAPI.h"
#include "common.h"
#define SAFE_RELEASE(p) { if ( (p) ) { (p)->Release(); (p) = 0; } }
enum DeckCommand {
NO_COMMAND = -1,
GET_CURRENT_STATE,
SET_STANDBY,
PLAY,
STOP,
TOGGLE_PLAY_STOP,
EJECT,
GO_TO_TIMECODE,
FAST_FORWARD,
REWIND,
STEP_FORWARD,
STEP_BACK,
JOG,
SHUTTLE,
GET_TIMECODE,
CRASH_RECORD_START,
CRASH_RECORD_STOP
};
struct DeckCommandMapping
{
/**
* Name of the command, as specified on the command line
*/
const char *commandString;
/**
* Corresponding of enum DeckCommand
*/
DeckCommand commandNum;
/**
* Number of additional parameters required for command
*/
int numParameters;
};
static const DeckCommandMapping commandTable [] = {
{ "getcurrentstate", GET_CURRENT_STATE, 0 },
{ "play", PLAY, 0 },
{ "stop", STOP, 0 },
{ "toggleplaystop", TOGGLE_PLAY_STOP, 0 },
{ "eject", EJECT, 0 },
{ "gototimecode", GO_TO_TIMECODE, 1 },
{ "fastforward", FAST_FORWARD, 0 },
{ "rewind", REWIND, 0 },
{ "stepforward", STEP_FORWARD, 0 },
{ "stepback", STEP_BACK, 0 },
{ "jog", JOG, 1 },
{ "shuttle", SHUTTLE, 1 },
{ "gettimecode", GET_TIMECODE, 0 },
{ "crashrecordstart", CRASH_RECORD_START, 0 },
{ "crashrecordstop", CRASH_RECORD_STOP, 0 },
{ NULL }
};
class StatusCallback : public IDeckLinkDeckControlStatusCallback
{
private:
pthread_mutex_t *m_mutex;
pthread_cond_t *m_cond;
bool m_connected;
public:
StatusCallback(pthread_mutex_t *mutex, pthread_cond_t *cond) :
m_mutex(mutex),
m_cond(cond),
m_connected(false) {}
virtual HRESULT TimecodeUpdate (BMDTimecodeBCD currentTimecode) { return E_NOTIMPL; }
virtual HRESULT VTRControlStateChanged (BMDDeckControlVTRControlState newState, BMDDeckControlError error) { return E_NOTIMPL; }
virtual HRESULT DeckControlEventReceived (BMDDeckControlEvent event, BMDDeckControlError error) { return E_NOTIMPL; }
virtual HRESULT DeckControlStatusChanged (BMDDeckControlStatusFlags flags, uint32_t mask)
{
pthread_mutex_lock(m_mutex);
if ((mask & bmdDeckControlStatusDeckConnected) && (flags & bmdDeckControlStatusDeckConnected))
{
m_connected = true;
pthread_cond_signal(m_cond);
}
pthread_mutex_unlock(m_mutex);
return S_OK;
}
HRESULT QueryInterface (REFIID iid, LPVOID *ppv) { return E_NOINTERFACE; }
ULONG AddRef () { return 1; }
ULONG Release () { return 1; }
bool isConnected() { return m_connected; }
};
int main(int argc, char *argv[])
{
int err = 0;
DeckCommand cmd = NO_COMMAND;
const char *cmdarg;
const DeckCommandMapping *tmpTable;
IDeckLinkIterator *deckLinkIterator = NULL;
IDeckLink *deckLink = NULL;
IDeckLinkDeckControl *deckControl = NULL;
BMDDeckControlError deckError = bmdDeckControlNoError;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
StatusCallback callback(&mutex, &cond);
// Parse command line
if (argc > 1)
{
tmpTable = commandTable;
while (tmpTable->commandString)
{
if (!strcmp(argv[1], tmpTable->commandString) &&
argc - 2 >= tmpTable->numParameters)
{
cmd = tmpTable->commandNum;
if (argc > 2)
cmdarg = argv[2];
else
cmdarg = "0";
printf("Issued command '%s'\n", tmpTable->commandString);
}
tmpTable++;
}
}
// Print usage if command line is invalid
if (cmd == NO_COMMAND)
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, "%s <command> [parameter1] [parameter2] ...\n\n", argv[0]);
fprintf(stderr, "Commands:\n");
tmpTable = commandTable;
while (tmpTable->commandString)
{
printf(" %s", tmpTable->commandString);
for (int i=1; i<=tmpTable->numParameters; i++)
printf(" [parameter %d]", i);
printf("\n");
tmpTable++;
}
err = -1;
goto bail;
}
// Try to load decklink driver and open the first card
deckLinkIterator = CreateDeckLinkIteratorInstance();
if (!deckLinkIterator)
{
fprintf(stderr, "This application requires the DeckLink drivers installed\n");
err = -2;
goto bail;
}
if (deckLinkIterator->Next(&deckLink) != S_OK)
{
fprintf(stderr, "Could not detect a DeckLink card\n");
err = -4;
goto bail;
}
if (deckLink->QueryInterface(IID_IDeckLinkDeckControl, (void **)&deckControl) != S_OK)
{
fprintf(stderr, "Could not obtain the DeckControl interface\n");
err = -8;
goto bail;
}
// Connect to deck
deckControl->SetCallback(&callback);
pthread_mutex_lock(&mutex);
if (deckControl->Open(30000, 1001, true, &deckError) == S_OK)
{
while (!callback.isConnected())
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
else
{
pthread_mutex_unlock(&mutex);
fprintf(stderr, "Could not open serial port (%s)\n", ERR_TO_STR(deckError));
err = -16;
goto bail;
}
// Send the command
switch(cmd)
{
case GET_CURRENT_STATE:
BMDDeckControlMode mode;
BMDDeckControlVTRControlState vtrControlState;
BMDDeckControlStatusFlags flags;
deckControl->GetCurrentState(&mode, &vtrControlState, &flags);
printf("VTR control state: %s\n", STATE_TO_STR(vtrControlState));
break;
case PLAY:
deckControl->Play(&deckError);
break;
case STOP:
deckControl->Stop(&deckError);
break;
case TOGGLE_PLAY_STOP:
deckControl->TogglePlayStop(&deckError);
break;
case EJECT:
deckControl->Eject(&deckError);
break;
case GO_TO_TIMECODE:
int tc[4];
cmdarg = strtok(argv[2], ":");
for (int i=0; i<4; i++)
{
if (!cmdarg) break;
tc[i] = atoi(cmdarg);
if (tc[i] > 59 || tc[i] < 0)
tc[i] = 0;
cmdarg = strtok(NULL, ":");
}
deckControl->GoToTimecode(MAKE_TC_BCD(tc[0] / 10, tc[0] % 10,
tc[1] / 10, tc[1] % 10,
tc[2] / 10, tc[2] % 10,
tc[3] / 10, tc[3] % 10),
&deckError);
break;
case FAST_FORWARD:
deckControl->FastForward(atoi(cmdarg), &deckError);
break;
case REWIND:
deckControl->Rewind(atoi(cmdarg), &deckError);
break;
case STEP_FORWARD:
deckControl->StepForward(&deckError);
break;
case STEP_BACK:
deckControl->StepBack(&deckError);
break;
case JOG:
deckControl->Jog(atof(argv[2]), &deckError);
break;
case SHUTTLE:
deckControl->Shuttle(atof(argv[2]), &deckError);
break;
case GET_TIMECODE:
IDeckLinkTimecode *currentTimecode;
uint8_t hours, minutes, seconds, frames;
deckControl->GetTimecode(¤tTimecode, &deckError);
currentTimecode->GetComponents(&hours, &minutes, &seconds, &frames);
printf("TC=%02"PRIu8":%02"PRIu8":%02"PRIu8":%02"PRIu8"\n",
hours, minutes, seconds, frames);
SAFE_RELEASE(currentTimecode);
break;
case CRASH_RECORD_START:
deckControl->CrashRecordStart(&deckError);
break;
case CRASH_RECORD_STOP:
deckControl->CrashRecordStop(&deckError);
break;
}
if (deckError == bmdDeckControlNoError)
{
printf("Command sucessfully issued\n");
}
{
printf("Error sending command (%s)\n", ERR_TO_STR(deckError));
}
deckControl->Close(false);
bail:
if (deckControl)
deckControl->SetCallback(NULL);
SAFE_RELEASE(deckControl);
SAFE_RELEASE(deckLink);
SAFE_RELEASE(deckLinkIterator);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
return err;
}