videohubctrl can be used to control Blackmagic Design Videohub SDI router device over the network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

videohubctrl.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Blackmagic Design Videohub control application
  3. * Copyright (C) 2014 Unix Solutions Ltd.
  4. * Written by Georgi Chorbadzhiyski
  5. *
  6. * Released under MIT license.
  7. * See LICENSE-MIT.txt for license terms.
  8. *
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <getopt.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include "data.h"
  20. #include "cmd.h"
  21. #include "net.h"
  22. #include "util.h"
  23. #include "display.h"
  24. #include "version.h"
  25. #include "libfuncs/libfuncs.h"
  26. int debug;
  27. int quiet;
  28. static struct videohub_data maindata;
  29. static int show_info = 1;
  30. static int show_monitor = 0;
  31. static int show_backup = 0;
  32. static int show_list = 0;
  33. static char *test_data;
  34. enum list_actions {
  35. action_list_device = (1 << 0),
  36. action_list_vinputs = (1 << 1),
  37. action_list_voutputs = (1 << 2),
  38. };
  39. static const char *program_id = PROGRAM_NAME " Version: " VERSION " Git: " GIT_VER;
  40. static const char short_options[] = "h:p:T:qdHVimb";
  41. static const struct option long_options[] = {
  42. { "host", required_argument, NULL, 'h' },
  43. { "port", required_argument, NULL, 'p' },
  44. { "test-input", required_argument, NULL, 'T' },
  45. { "quiet", no_argument, NULL, 'q' },
  46. { "debug", no_argument, NULL, 'd' },
  47. { "help", no_argument, NULL, 'H' },
  48. { "version", no_argument, NULL, 'V' },
  49. { "info", no_argument, NULL, 'i' },
  50. { "monitor", no_argument, NULL, 'm' },
  51. { "backup", no_argument, NULL, 'b' },
  52. { "list-device", no_argument, NULL, 901 },
  53. { "list-vinputs", no_argument, NULL, 902 },
  54. { "list-voutputs", no_argument, NULL, 903 },
  55. { "vi-name", required_argument, NULL, 1001 },
  56. { "vo-name", required_argument, NULL, 1002 },
  57. { "vo-route", required_argument, NULL, 1011 },
  58. { "vo-lock", required_argument, NULL, 1021 },
  59. { "vo-unlock", required_argument, NULL, 1022 },
  60. { 0, 0, 0, 0 }
  61. };
  62. static void show_help(struct videohub_data *data) {
  63. printf("%s\n", program_id);
  64. printf("\n");
  65. printf(" Usage: " PROGRAM_NAME " --host <host> [..commands..]\n");
  66. printf("\n");
  67. printf("Main options:\n");
  68. printf(" -h --host <host> | Set device host name.\n");
  69. printf(" -p --port <port_number> | Set device port (default: 9990).\n");
  70. printf("\n");
  71. printf("Commands:\n");
  72. printf(" -i --info | Show full device info (default command).\n");
  73. printf(" . This command is shows the equivalent of\n");
  74. printf(" . running all --list-XXX commands.\n");
  75. printf(" -m --monitor | Display real-time config changes monitor.\n");
  76. printf(" -b --backup | Show the command line that will restore\n");
  77. printf(" . the device to the current configuration.\n");
  78. printf("\n");
  79. printf(" --list-device | Display device info.\n");
  80. printf(" --list-vinputs | List device video inputs.\n");
  81. printf(" --list-voutputs | List device video outputs.\n");
  82. printf("\n");
  83. printf("Video input/output configuration:\n");
  84. printf(" --vi-name <in_X> <name> | Set video input port X name.\n");
  85. printf(" --vo-name <out_X> <name> | Set video output port X name.\n");
  86. printf("\n");
  87. printf(" --vo-route <out_X> <in_Y> | Connect video output X to video input Y\n");
  88. printf("\n");
  89. printf(" --vo-lock <out_X> | Lock output port X.\n");
  90. printf(" --vo-unlock <out_X> | Unlock output port X.\n");
  91. printf("\n");
  92. printf(" For <in_X/out_X/in_Y> you may use port number or port name.\n");
  93. printf("\n");
  94. printf("Misc options:\n");
  95. printf(" -T --test-input <file> | Read commands from <file>.\n");
  96. printf(" -d --debug | Increase logging verbosity.\n");
  97. printf(" -q --quiet | Suppress warnings.\n");
  98. printf(" -H --help | Show help screen.\n");
  99. printf(" -V --version | Show program version.\n");
  100. printf("\n");
  101. }
  102. static int num_parsed_cmds = 0;
  103. static struct run_cmds {
  104. struct vcmd_entry entry[MAX_RUN_CMDS];
  105. } parsed_cmds;
  106. static void parse_options(struct videohub_data *data, int argc, char **argv) {
  107. int j, err = 0;
  108. struct vcmd_entry *c = &parsed_cmds.entry[0];
  109. // Check environment
  110. data->dev_host = getenv("VIDEOHUB_HOST");
  111. data->dev_port = getenv("VIDEOHUB_PORT");
  112. // Set defaults
  113. if (!data->dev_port)
  114. data->dev_port = "9990";
  115. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  116. if (j == '?') // Invalid parameter
  117. exit(EXIT_FAILURE);
  118. switch (j) {
  119. case 'h': // --host
  120. data->dev_host = optarg;
  121. break;
  122. case 'p': // --port
  123. data->dev_port = optarg;
  124. break;
  125. case 'T': { // --test-input
  126. struct stat st;
  127. FILE *f;
  128. if (stat(optarg, &st) != 0)
  129. die("Can't stat %s: %s", optarg, strerror(errno));
  130. f = fopen(optarg, "r");
  131. if (!f)
  132. die("Can't open %s: %s", optarg, strerror(errno));
  133. test_data = xzalloc(st.st_size);
  134. if (fread(test_data, st.st_size, 1, f) < 1)
  135. die("Can't read from %s: %s", optarg, strerror(errno));
  136. fclose(f);
  137. data->dev_host = "sdi-matrix";
  138. break;
  139. }
  140. case 'd': // --debug
  141. debug++;
  142. if (debug)
  143. quiet = 0; // Disable quiet
  144. break;
  145. case 'q': // --quiet
  146. quiet = !quiet;
  147. break;
  148. case 'i': // --info
  149. show_info = 1;
  150. break;
  151. case 'm': // --monitor
  152. show_monitor = 1;
  153. break;
  154. case 'b': // --backup
  155. show_backup = 1;
  156. break;
  157. case 901: show_list |= action_list_device; break; // --list-device
  158. case 902: show_list |= action_list_vinputs; break; // --list-vinputs
  159. case 903: show_list |= action_list_voutputs; break; // --list-voutputs
  160. case 1001: // --vi-name
  161. case 1002: // --vo-name
  162. case 1011: // --vi-route
  163. case 1012: // --vo-route
  164. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  165. die("No more than %u commands are supported.", num_parsed_cmds);
  166. if (optind == argc || argv[optind - 1][0] == '-' || argv[optind][0] == '-') {
  167. fprintf(stderr, "%s: option '%s' requires two arguments\n", argv[0], argv[optind - 2]);
  168. exit(EXIT_FAILURE);
  169. }
  170. switch (j) {
  171. case 1001: c->cmd = &videohub_commands[CMD_INPUT_LABELS]; break; // --vi-name
  172. case 1002: c->cmd = &videohub_commands[CMD_OUTPUT_LABELS]; break; // --vo-name
  173. case 1011: c->cmd = &videohub_commands[CMD_VIDEO_OUTPUT_ROUTING]; break; // --vo-route
  174. }
  175. c->param1 = argv[optind - 1];
  176. c->param2 = argv[optind];
  177. c->param1 = argv[optind - 1];
  178. c->param2 = argv[optind];
  179. c = &parsed_cmds.entry[++num_parsed_cmds];
  180. break;
  181. case 1021: // --vo-lock
  182. case 1022: // --vo-unlock
  183. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  184. die("No more than %u commands are supported.", num_parsed_cmds);
  185. c->cmd = &videohub_commands[CMD_VIDEO_OUTPUT_LOCKS];
  186. c->param1 = argv[optind - 1];
  187. c->do_lock = (j == 1021);
  188. c = &parsed_cmds.entry[++num_parsed_cmds];
  189. break;
  190. case 'H': // --help
  191. show_help(data);
  192. exit(EXIT_SUCCESS);
  193. case 'V': // --version
  194. printf("%s\n", program_id);
  195. exit(EXIT_SUCCESS);
  196. }
  197. }
  198. if (!data->dev_host || !strtoul(data->dev_port, NULL, 10))
  199. err = 1;
  200. if (err) {
  201. show_help(data);
  202. if (!data->dev_host)
  203. fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
  204. exit(EXIT_FAILURE);
  205. }
  206. d("Device address: %s:%s\n", data->dev_host, data->dev_port);
  207. }
  208. static void check_number_of_ports(struct port_set *p) {
  209. if (p->num > ARRAY_SIZE(p->port))
  210. die("The device supports %d ports. Increase MAX_PORTS (%lu) and recompile the program.",
  211. p->num, ARRAY_SIZE(p->port));
  212. }
  213. static void print_device_full(struct videohub_data *d) {
  214. print_device_info(d);
  215. print_device_video_inputs(d);
  216. print_device_video_outputs(d);
  217. fflush(stdout);
  218. }
  219. static int read_device_command_stream(struct videohub_data *d) {
  220. int ret, ncommands = 0;
  221. char buf[8192 + 1];
  222. if (test_data)
  223. return 0;
  224. memset(buf, 0, sizeof(buf));
  225. while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
  226. ncommands += parse_text_buffer(d, buf);
  227. memset(buf, 0, sizeof(buf));
  228. }
  229. return ncommands;
  230. }
  231. static void send_device_command(struct videohub_data *d, char *cmd_buffer) {
  232. if (!test_data)
  233. fdwrite(d->dev_fd, cmd_buffer, strlen(cmd_buffer));
  234. else
  235. parse_text_buffer(d, cmd_buffer);
  236. }
  237. int main(int argc, char **argv) {
  238. struct videohub_data *data = &maindata;
  239. parse_options(data, argc, argv);
  240. set_log_io_errors(0);
  241. if (!test_data) {
  242. data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
  243. if (data->dev_fd < 0)
  244. exit(EXIT_FAILURE);
  245. }
  246. read_device_command_stream(data);
  247. if (test_data)
  248. parse_text_buffer(data, test_data);
  249. if (!strlen(data->device.protocol_ver) || !strlen(data->device.model_name))
  250. die("The device does not respond correctly. Is it Videohub?");
  251. if (strstr(data->device.protocol_ver, "2.") != data->device.protocol_ver)
  252. die("Device protocol is %s but this program supports 2.x only.\n",
  253. data->device.protocol_ver);
  254. if (!data->device.dev_present) {
  255. if (data->device.needs_fw_update) {
  256. die("Device reports that it needs firmware update.");
  257. }
  258. die("Device reports that it is not present.");
  259. }
  260. check_number_of_ports(&data->inputs);
  261. check_number_of_ports(&data->outputs);
  262. if (num_parsed_cmds) {
  263. unsigned int i;
  264. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  265. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  266. if (!ve->param1)
  267. continue;
  268. prepare_cmd_entry(data, &parsed_cmds.entry[i]);
  269. }
  270. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  271. char cmd_buffer[1024];
  272. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  273. if (!ve->param1)
  274. continue;
  275. format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
  276. if (strlen(cmd_buffer)) {
  277. show_cmd(data, ve);
  278. send_device_command(data, cmd_buffer);
  279. read_device_command_stream(data);
  280. }
  281. }
  282. // Show the result after commands
  283. if (test_data)
  284. print_device_full(data);
  285. } else if (show_monitor) {
  286. while (1) {
  287. int sleeps = 0;
  288. printf("\e[2J\e[H"); // Clear screen
  289. time_t now = time(NULL);
  290. struct tm *tm = localtime(&now);
  291. printf("Last update: %s\n", asctime(tm));
  292. print_device_info(data);
  293. print_device_video_inputs(data);
  294. print_device_video_outputs(data);
  295. fflush(stdout);
  296. do {
  297. usleep(500000);
  298. if (++sleeps >= 20)
  299. send_device_command(data, "PING:\n\n");
  300. } while (read_device_command_stream(data) == 0);
  301. }
  302. } else if (show_list) {
  303. if (show_list & action_list_device) print_device_info(data);
  304. if (show_list & action_list_vinputs) print_device_video_inputs(data);
  305. if (show_list & action_list_voutputs) print_device_video_outputs(data);
  306. fflush(stdout);
  307. } else if (show_backup) {
  308. print_device_backup(data);
  309. } else if (show_info) {
  310. print_device_full(data);
  311. }
  312. shutdown_fd(&data->dev_fd);
  313. free(test_data);
  314. return 0;
  315. }