videohub 1.0.0

Blackmagic Videohub Control Protocol Codec
Documentation
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
// Basic Video Hub Parser.

use crate::helpers::*;
use crate::model::*;
use bytes::BytesMut;
use nom::{
    branch::alt,
    bytes::streaming::{tag, tag_no_case, take_until},
    character::streaming::{multispace0, space1},
    error::{Error, ErrorKind, ParseError},
    sequence::{preceded, terminated, tuple},
    Err, IResult,
};

const COLON: &[u8] = b":";

/// Parse one "Key: Value" line to (key, value) tuple
fn parse_kv_line(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
    let (i, (k, _, _, v, _)) = tuple((
        take_until(COLON),
        tag(COLON),
        space1,
        take_until_newline,
        any_newline,
    ))(i)?;
    Ok((i, (k.trim_ascii(), v.trim_ascii_end())))
}

/// Parse the body of a Preamble block after its header
fn parse_preamble_body(i: &[u8]) -> IResult<&[u8], VideohubMessage> {
    let (i, (_, _, ver, _)) = tuple((
        tag_no_case(b"Version"),
        tag(COLON),
        take_until_newline,
        any_newline,
    ))(i)?;
    let version = String::from_utf8_lossy(ver.trim_ascii()).to_string();
    Ok((i, VideohubMessage::Preamble(Preamble { version })))
}

/// Parse the body of DeviceInfo block after its header
fn parse_device_body(mut i: &[u8]) -> IResult<&[u8], VideohubMessage> {
    let mut di = DeviceInfo::default();
    while let Ok((i2, (k, v))) = parse_kv_line(i) {
        let lk = k.to_ascii_lowercase();
        match &lk[..] {
            b"device present" => {
                di.present = Some(match v {
                    b"true" => Present::Yes,
                    b"false" => Present::No,
                    b"needs_update" => Present::NeedsUpdate,
                    _ => return Err(Err::Error(Error::from_error_kind(i, ErrorKind::Tag))),
                })
            }
            b"model name" => di.model_name = Some(String::from_utf8_lossy(v).to_string()),
            b"friendly name" => di.unique_id = Some(String::from_utf8_lossy(v).to_string()),
            b"unique id" => di.unique_id = Some(String::from_utf8_lossy(v).to_string()),
            b"video inputs" => di.video_inputs = Some(parse_u32(v)?.1),
            b"video processing units" => di.video_processing_units = Some(parse_u32(v)?.1),
            b"video outputs" => di.video_outputs = Some(parse_u32(v)?.1),
            b"video monitoring outputs" => di.video_monitoring_outputs = Some(parse_u32(v)?.1),
            b"serial ports" => di.serial_ports = Some(parse_u32(v)?.1),
            _ => {
                let mut unknown = di.unknown_fields.unwrap_or_else(|| Vec::new());
                unknown.push(UnknownKVPair {
                    key: String::from_utf8_lossy(k).to_string(),
                    value: String::from_utf8_lossy(v).to_string(),
                });
                di.unknown_fields = Some(unknown);
            }
        }
        i = i2;
    }
    Ok((i, VideohubMessage::DeviceInfo(di)))
}

/// Parse generic "ID Name Here" label lines
fn parse_label_body<'a>(
    mut i: &'a [u8],
    ctor: fn(Vec<Label>) -> VideohubMessage,
) -> IResult<&'a [u8], VideohubMessage> {
    let mut out = Vec::new();
    while let Ok((i2, (id, _, nm, _))) =
        tuple((parse_u32, space1, take_until_newline, any_newline))(i)
    {
        out.push(Label {
            id,
            name: String::from_utf8_lossy(nm.trim_ascii()).to_string(),
        });
        i = i2;
    }
    Ok((i, ctor(out)))
}

/// Parse generic "to from" route lines
fn parse_route_body<'a>(
    mut i: &'a [u8],
    ctor: fn(Vec<Route>) -> VideohubMessage,
) -> IResult<&'a [u8], VideohubMessage> {
    let mut out = Vec::new();
    while let Ok((i2, (t, _, f, _))) = tuple((parse_u32, space1, parse_u32, any_newline))(i) {
        out.push(Route {
            from_input: f,
            to_output: t,
        });
        i = i2;
    }
    Ok((i, ctor(out)))
}

/// Parse generic "ID [O/L/U]" lines
fn parse_lock_body<'a>(
    mut i: &'a [u8],
    ctor: fn(Vec<Lock>) -> VideohubMessage,
) -> IResult<&'a [u8], VideohubMessage> {
    let mut out = Vec::new();
    while let Ok((i2, (id, _, s, _))) =
        tuple((parse_u32, space1, take_until_newline, any_newline))(i)
    {
        let state = match s.trim_ascii_end() {
            b"O" | b"o" => LockState::Owned,
            b"L" | b"l" => LockState::Locked,
            b"U" | b"u" => LockState::Unlocked,
            _ => return Err(Err::Error(Error::from_error_kind(i, ErrorKind::Tag))),
        };
        out.push(Lock { id, state });
        i = i2;
    }
    Ok((i, ctor(out)))
}

/// Parse generic "status" lines
fn parse_hw_body<'a>(
    mut i: &'a [u8],
    ctor: fn(Vec<HardwarePort>) -> VideohubMessage,
) -> IResult<&'a [u8], VideohubMessage> {
    let mut out = Vec::new();
    while let Ok((i2, (id, _, hw_type, _))) =
        tuple((parse_u32, space1, take_until_newline, any_newline))(i)
    {
        let tp = hw_type.trim_ascii_end();
        let lp = tp.to_ascii_lowercase();
        let port_type = match &lp[..] {
            b"one" => HardwarePortType::None,
            b"bnc" => HardwarePortType::BNC,
            b"optical" => HardwarePortType::Optical,
            b"thunderbolt" => HardwarePortType::Thunderbolt,
            b"rs422" => HardwarePortType::RS422,
            _ => HardwarePortType::Other(String::from_utf8_lossy(tp).to_string()),
        };
        out.push(HardwarePort { id, port_type });
        i = i2;
    }
    Ok((i, ctor(out)))
}

/// Parse generic Key-Value lines
fn parse_kv_body<'a>(
    mut i: &'a [u8],
    ctor: fn(Vec<(&'a [u8], &'a [u8])>) -> VideohubMessage,
) -> IResult<&'a [u8], VideohubMessage> {
    let mut out = Vec::new();
    while let Ok((i2, (k, v))) = parse_kv_line(i) {
        out.push((k, v));
        i = i2;
    }
    Ok((i, ctor(out)))
}

impl VideohubMessage {
    /// Parse one block including its trailing blank-line
    pub fn parse_single_block(i: &[u8]) -> IResult<&[u8], VideohubMessage> {
        let (i, header) = preceded(multispace0, terminated(take_until_newline, any_newline))(i)?;
        let (i, body) = alt((any_newline, take_until_empty_line))(i)?;
        let trimmed_header = header.trim_ascii_end();
        let screaming_header = trimmed_header.to_ascii_uppercase();
        let (_, msg) = match &screaming_header[..] {
            b"PROTOCOL PREAMBLE:" => parse_preamble_body(body)?,
            b"VIDEOHUB DEVICE:" => parse_device_body(body)?,

            b"INPUT LABELS:" => parse_label_body(body, VideohubMessage::InputLabels)?,
            b"OUTPUT LABELS:" => parse_label_body(body, VideohubMessage::OutputLabels)?,
            b"MONITOR OUTPUT LABELS:" => {
                parse_label_body(body, VideohubMessage::MonitorOutputLabels)?
            }
            b"SERIAL PORT LABELS:" => parse_label_body(body, VideohubMessage::SerialPortLabels)?,
            b"FRAME LABELS:" => parse_label_body(body, VideohubMessage::FrameLabels)?,

            b"VIDEO OUTPUT ROUTING:" => {
                parse_route_body(body, VideohubMessage::VideoOutputRouting)?
            }
            b"VIDEO MONITORING OUTPUT ROUTING:" => {
                parse_route_body(body, VideohubMessage::VideoMonitoringOutputRouting)?
            }
            b"SERIAL PORT ROUTING:" => parse_route_body(body, VideohubMessage::SerialPortRouting)?,
            b"PROCESSING UNIT ROUTING:" => {
                parse_route_body(body, VideohubMessage::ProcessingUnitRouting)?
            }
            b"FRAME BUFFER ROUTING:" => {
                parse_route_body(body, VideohubMessage::FrameBufferRouting)?
            }

            b"VIDEO OUTPUT LOCKS:" => parse_lock_body(body, VideohubMessage::VideoOutputLocks)?,
            b"MONITORING OUTPUT LOCKS:" => {
                parse_lock_body(body, VideohubMessage::MonitoringOutputLocks)?
            }
            b"SERIAL PORT LOCKS:" => parse_lock_body(body, VideohubMessage::SerialPortLocks)?,
            b"PROCESSING UNIT LOCKS:" => {
                parse_lock_body(body, VideohubMessage::ProcessingUnitLocks)?
            }
            b"FRAME BUFFER LOCKS:" => parse_lock_body(body, VideohubMessage::FrameBufferLocks)?,

            b"VIDEO INPUT STATUS:" => parse_hw_body(body, VideohubMessage::VideoInputStatus)?,
            b"VIDEO OUTPUT STATUS:" => parse_hw_body(body, VideohubMessage::VideoOutputStatus)?,
            b"SERIAL PORT STATUS:" => parse_hw_body(body, VideohubMessage::SerialPortStatus)?,

            b"ALARM STATUS:" => parse_kv_body(body, |vals| {
                VideohubMessage::AlarmStatus(
                    vals.iter()
                        .map(|t| Alarm {
                            name: String::from_utf8_lossy(t.0.trim_ascii()).to_string(),
                            status: String::from_utf8_lossy(t.1.trim_ascii()).to_string(),
                        })
                        .collect(),
                )
            })?,
            b"CONFIGURATION:" => parse_kv_body(body, |vals| {
                VideohubMessage::Configuration(
                    vals.iter()
                        .map(|t| Setting {
                            setting: String::from_utf8_lossy(t.0.trim_ascii()).to_string(),
                            value: String::from_utf8_lossy(t.1.trim_ascii()).to_string(),
                        })
                        .collect(),
                )
            })?,

            b"ACK" => (i, VideohubMessage::ACK),
            b"NAK" => (i, VideohubMessage::ACK),
            b"PING:" => (i, VideohubMessage::Ping),
            b"END PRELUDE:" => (i, VideohubMessage::EndPrelude),

            _ => (
                b"".as_slice(),
                VideohubMessage::UnknownMessage(
                    BytesMut::from(trimmed_header),
                    BytesMut::from(body),
                ),
            ),
        };
        Ok((i, msg))
    }

    /// Parse an entire Videohub conversation of multiple messages.
    pub fn parse_all_blocks(input: &[u8]) -> IResult<&[u8], Vec<VideohubMessage>> {
        let mut i = input;
        let mut messages = Vec::new();
        loop {
            let (ni, message) = Self::parse_single_block(i)?;
            messages.push(message);
            if ni.is_empty() {
                return Ok((ni, messages));
            }
            i = ni;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const BMD_EXAMPLE: &[u8] = include_bytes!("./bmd_example.txt");
    const BMD_CLEANSWITCH: &[u8] = include_bytes!("./bmd_cleanswitch_12x12.txt");

    #[test]
    fn parse_only_preamble() {
        let buf = b"PROTOCOL PREAMBLE:\r\nVersion: 2.4\r\n\r\n";
        let (rem, msg) = VideohubMessage::parse_single_block(buf).expect("should parse preamble");
        // no leftover
        assert!(rem.is_empty(), "remaining = {:?}", rem);
        match msg {
            VideohubMessage::Preamble(p) => assert_eq!(p.version, "2.4"),
            _ => panic!("expected Preamble, got {:?}", msg),
        }
    }

    #[test]
    fn parse_single_line() {
        let buf = b"PING:\n\n";
        let (rem, msg) = VideohubMessage::parse_single_block(buf).expect("should parse preamble");
        // no leftover
        assert!(rem.is_empty(), "remaining = {:?}", rem);
        assert_eq!(msg, VideohubMessage::Ping);
    }

    #[test]
    fn parse_only_deviceinfo() {
        let buf = b"VIDEOHUB DEVICE:\r\n\
                    Device present: true\r\n\
                    Model name: foobar\r\n\
                    Video inputs: 3\r\n\r\n";
        let (rem, msg) = VideohubMessage::parse_single_block(buf).expect("should parse device");
        assert!(rem.is_empty(), "remaining = {:?}", rem);
        let lower = buf.to_ascii_lowercase();
        let (rem2, msg2) = VideohubMessage::parse_single_block(&lower[..])
            .expect("should parse lower-case device");
        assert!(rem2.is_empty(), "remaining = {:?}", rem);
        assert_eq!(msg, msg2, "parsing should not depend on case");

        match msg {
            VideohubMessage::DeviceInfo(d) => {
                assert!(matches!(d.present, Some(Present::Yes)));
                assert_eq!(d.model_name.as_deref(), Some("foobar"));
                assert_eq!(d.video_inputs, Some(3));
            }
            _ => panic!("expected DeviceInfo, got {:?}", msg),
        }
    }

    #[test]
    fn parse_only_input_labels() {
        let buf = b"INPUT LABELS:\r\n0 a\r\n1  b \r\n\r\n";
        let (rem, msg) =
            VideohubMessage::parse_single_block(buf).expect("should parse input labels");
        assert!(rem.is_empty(), "remaining = {:?}", rem);
        let lower = buf.to_ascii_lowercase();
        let (rem2, msg2) = VideohubMessage::parse_single_block(&lower[..])
            .expect("should parse lower-case input labels");
        assert!(rem2.is_empty(), "remaining = {:?}", rem);
        assert_eq!(msg, msg2, "parsing should not depend on case");

        match msg {
            VideohubMessage::InputLabels(v) => {
                assert_eq!(v.len(), 2);
                assert_eq!(v[0].id, 0);
                assert_eq!(&v[0].name, "a");
                assert_eq!(v[1].id, 1);
                assert_eq!(&v[1].name, "b");
            }
            _ => panic!("expected InputLabels, got {:?}", msg),
        }
    }

    #[test]
    fn parse_only_output_labels() {
        let buf = b"OUTPUT LABELS:\n5 X\n\n";
        let (rem, msg) =
            VideohubMessage::parse_single_block(buf).expect("should parse output labels");
        assert!(rem.is_empty(), "remaining = {:?}", rem);
        match msg {
            VideohubMessage::OutputLabels(v) => {
                assert_eq!(v.len(), 1);
                assert_eq!(v[0].id, 5);
                assert_eq!(&v[0].name, "X");
            }
            _ => panic!("expected OutputLabels, got {:?}", msg),
        }
    }

    #[test]
    fn parse_partial() {
        let mut buf: Vec<u8> = Vec::from(b"INPUT ");
        let r = VideohubMessage::parse_single_block(&buf);
        assert!(r.is_err());

        buf.extend_from_slice(b"LABELS:\n0 A");
        let r = VideohubMessage::parse_single_block(&buf);
        assert!(r.is_err());

        buf.extend_from_slice(b"\n\nOUTPUT LABELS:\n");
        let (rem, partial) = VideohubMessage::parse_single_block(&buf).unwrap();
        assert_eq!(
            partial,
            VideohubMessage::InputLabels(vec![Label {
                id: 0,
                name: String::from("A"),
            }])
        );
        assert_eq!(rem, b"OUTPUT LABELS:\n");
    }

    #[test]
    fn parse_multiple_sections() {
        let buf = b"PROTOCOL PREAMBLE:\nVersion:2.4\n\nINPUT LABELS:\n0 A\n\n";
        let (rem, v) = VideohubMessage::parse_all_blocks(buf).expect("should parse two sections");
        assert!(rem.is_empty(), "remaining = {:?}", rem);
        assert_eq!(v.len(), 2);
        matches!(v[0], VideohubMessage::Preamble(_));
        matches!(v[1], VideohubMessage::InputLabels(_));
    }

    #[test]
    fn parse_bmd_example() {
        let (rem, msgs) = VideohubMessage::parse_all_blocks(BMD_EXAMPLE).unwrap();
        assert!(rem.is_empty(), "remaining = {:?}", rem);
        assert_eq!(msgs.len(), 4);

        match &msgs[0] {
            VideohubMessage::Preamble(p) => assert_eq!(p.version, "2.4"),
            _ => panic!("expected Preamble"),
        }
        match &msgs[1] {
            VideohubMessage::DeviceInfo(d) => assert!(matches!(d.present, Some(Present::Yes))),
            _ => panic!("expected DeviceInfo"),
        }
        match &msgs[2] {
            VideohubMessage::InputLabels(v) => {
                assert_eq!(v[0].id, 0);
                assert_eq!(&v[0].name, "Camera 1");
            }
            _ => panic!("expected InputLabels"),
        }
        match &msgs[3] {
            VideohubMessage::OutputLabels(v) => {
                assert_eq!(v[0].id, 0);
                assert_eq!(&v[0].name, "Main Monitor 1");
            }
            _ => panic!("expected OutputLabels"),
        }
    }
    #[test]
    fn parse_bmd_example_but_lowercase() {
        let lower_example = BMD_EXAMPLE.to_ascii_lowercase();
        let (rem, msgs) = VideohubMessage::parse_all_blocks(&lower_example[..]).unwrap();
        assert!(rem.is_empty(), "remaining = {:?}", rem);
        assert_eq!(msgs.len(), 4);

        match &msgs[0] {
            VideohubMessage::Preamble(p) => assert_eq!(p.version, "2.4"),
            _ => panic!("expected Preamble"),
        }
        match &msgs[1] {
            VideohubMessage::DeviceInfo(d) => assert!(matches!(d.present, Some(Present::Yes))),
            _ => panic!("expected DeviceInfo"),
        }
        match &msgs[2] {
            VideohubMessage::InputLabels(v) => {
                assert_eq!(v[0].id, 0);
                assert_eq!(&v[0].name, "camera 1");
            }
            _ => panic!("expected InputLabels"),
        }
        match &msgs[3] {
            VideohubMessage::OutputLabels(v) => {
                assert_eq!(v[0].id, 0);
                assert_eq!(&v[0].name, "main monitor 1");
            }
            _ => panic!("expected OutputLabels"),
        }
    }

    #[test]
    fn parse_bmd_cleanswitch() {
        let (rem, msgs) = VideohubMessage::parse_all_blocks(BMD_CLEANSWITCH).unwrap();
        assert!(rem.is_empty(), "remaining = {:?}", rem);
        assert_eq!(msgs.len(), 8);

        match &msgs[0] {
            VideohubMessage::Preamble(p) => assert_eq!(p.version, "2.8"),
            _ => panic!("expected Preamble"),
        }
        match &msgs[1] {
            VideohubMessage::DeviceInfo(d) => assert!(matches!(d.present, Some(Present::Yes))),
            _ => panic!("expected DeviceInfo"),
        }
        match &msgs[2] {
            VideohubMessage::InputLabels(v) => {
                assert_eq!(v[0].id, 0);
                assert_eq!(&v[0].name, "HyperDeck 1");
                assert_eq!(v.len(), 12);
            }
            _ => panic!("expected InputLabels"),
        }
        match &msgs[3] {
            VideohubMessage::OutputLabels(v) => {
                assert_eq!(v[0].id, 0);
                assert_eq!(&v[0].name, "Teranex AV 1");
                assert_eq!(v.len(), 12);
            }
            _ => panic!("expected OutputLabels"),
        }
        match &msgs[4] {
            VideohubMessage::VideoOutputLocks(v) => {
                assert_eq!(v[0].id, 0);
                assert_eq!(v[0].state, LockState::Unlocked);
                assert_eq!(v.len(), 12);
            }
            _ => panic!("expected VideoOutputLocks"),
        }
        match &msgs[5] {
            VideohubMessage::VideoOutputRouting(v) => {
                assert_eq!(v[0].to_output, 0);
                assert_eq!(v[0].from_input, 6);
                assert_eq!(v.len(), 12);
            }
            _ => panic!("expected VideoOutputLocks"),
        }
        match &msgs[6] {
            VideohubMessage::Configuration(v) => {
                assert_eq!(&v[0].setting, "Take Mode");
                assert_eq!(&v[0].value, "false");
            }
            _ => panic!("expected VideoOutputLocks"),
        }
        assert_eq!(&msgs[7], &VideohubMessage::EndPrelude);
    }
}