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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_cmd2(int argc, char **argv, enum vcmd vcmd) {
  107. struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
  108. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  109. die("No more than %u commands are supported.", num_parsed_cmds);
  110. if (optind == argc || argv[optind - 1][0] == '-' || argv[optind][0] == '-') {
  111. fprintf(stderr, "%s: option '%s' requires two arguments\n", argv[0], argv[optind - 2]);
  112. exit(EXIT_FAILURE);
  113. }
  114. c->cmd = &videohub_commands[vcmd];
  115. c->param1 = argv[optind - 1];
  116. c->param2 = argv[optind];
  117. num_parsed_cmds++;
  118. }
  119. static void parse_cmd1(int argc, char **argv, enum vcmd vcmd, bool do_lock) {
  120. struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
  121. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  122. die("No more than %u commands are supported.", num_parsed_cmds);
  123. c->cmd = &videohub_commands[vcmd];
  124. c->param1 = argv[optind - 1];
  125. c->do_lock = do_lock;
  126. num_parsed_cmds++;
  127. }
  128. static void parse_options(struct videohub_data *data, int argc, char **argv) {
  129. int j, err = 0;
  130. // Check environment
  131. data->dev_host = getenv("VIDEOHUB_HOST");
  132. data->dev_port = getenv("VIDEOHUB_PORT");
  133. // Set defaults
  134. if (!data->dev_port)
  135. data->dev_port = "9990";
  136. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  137. if (j == '?') // Invalid parameter
  138. exit(EXIT_FAILURE);
  139. switch (j) {
  140. case 'h': // --host
  141. data->dev_host = optarg;
  142. break;
  143. case 'p': // --port
  144. data->dev_port = optarg;
  145. break;
  146. case 'T': { // --test-input
  147. struct stat st;
  148. FILE *f;
  149. if (stat(optarg, &st) != 0)
  150. die("Can't stat %s: %s", optarg, strerror(errno));
  151. f = fopen(optarg, "r");
  152. if (!f)
  153. die("Can't open %s: %s", optarg, strerror(errno));
  154. test_data = xzalloc(st.st_size);
  155. if (fread(test_data, st.st_size, 1, f) < 1)
  156. die("Can't read from %s: %s", optarg, strerror(errno));
  157. fclose(f);
  158. data->dev_host = "sdi-matrix";
  159. break;
  160. }
  161. case 'd': // --debug
  162. debug++;
  163. if (debug)
  164. quiet = 0; // Disable quiet
  165. break;
  166. case 'q': // --quiet
  167. quiet = !quiet;
  168. break;
  169. case 'i': // --info
  170. show_info = 1;
  171. break;
  172. case 'm': // --monitor
  173. show_monitor = 1;
  174. break;
  175. case 'b': // --backup
  176. show_backup = 1;
  177. break;
  178. case 901: show_list |= action_list_device; break; // --list-device
  179. case 902: show_list |= action_list_vinputs; break; // --list-vinputs
  180. case 903: show_list |= action_list_voutputs; break; // --list-voutputs
  181. case 1001: parse_cmd2(argc, argv, CMD_INPUT_LABELS); break; // --vi-name
  182. case 1002: parse_cmd2(argc, argv, CMD_OUTPUT_LABELS); break; // --vo-name
  183. case 1011: parse_cmd2(argc, argv, CMD_VIDEO_OUTPUT_ROUTING); break; // --vo-route
  184. case 1021: parse_cmd1(argc, argv, CMD_VIDEO_OUTPUT_LOCKS, true); break; // --vo-lock
  185. case 1022: parse_cmd1(argc, argv, CMD_VIDEO_OUTPUT_LOCKS, false); break; // --vo-unlock
  186. case 'H': // --help
  187. show_help(data);
  188. exit(EXIT_SUCCESS);
  189. case 'V': // --version
  190. printf("%s\n", program_id);
  191. exit(EXIT_SUCCESS);
  192. }
  193. }
  194. if (!data->dev_host || !strtoul(data->dev_port, NULL, 10))
  195. err = 1;
  196. if (err) {
  197. show_help(data);
  198. if (!data->dev_host)
  199. fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
  200. exit(EXIT_FAILURE);
  201. }
  202. d("Device address: %s:%s\n", data->dev_host, data->dev_port);
  203. }
  204. static void check_number_of_ports(struct port_set *p) {
  205. if (p->num > ARRAY_SIZE(p->port))
  206. die("The device supports %d ports. Increase MAX_PORTS (%lu) and recompile the program.",
  207. p->num, ARRAY_SIZE(p->port));
  208. }
  209. static void print_device_full(struct videohub_data *d) {
  210. print_device_info(d);
  211. print_device_video_inputs(d);
  212. print_device_video_outputs(d);
  213. fflush(stdout);
  214. }
  215. static int read_device_command_stream(struct videohub_data *d) {
  216. int ret, ncommands = 0;
  217. char buf[8192 + 1];
  218. if (test_data)
  219. return 0;
  220. memset(buf, 0, sizeof(buf));
  221. while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
  222. ncommands += parse_text_buffer(d, buf);
  223. memset(buf, 0, sizeof(buf));
  224. }
  225. return ncommands;
  226. }
  227. static void send_device_command(struct videohub_data *d, char *cmd_buffer) {
  228. if (!test_data)
  229. fdwrite(d->dev_fd, cmd_buffer, strlen(cmd_buffer));
  230. else
  231. parse_text_buffer(d, cmd_buffer);
  232. }
  233. int main(int argc, char **argv) {
  234. struct videohub_data *data = &maindata;
  235. parse_options(data, argc, argv);
  236. set_log_io_errors(0);
  237. if (!test_data) {
  238. data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
  239. if (data->dev_fd < 0)
  240. exit(EXIT_FAILURE);
  241. }
  242. read_device_command_stream(data);
  243. if (test_data)
  244. parse_text_buffer(data, test_data);
  245. if (!strlen(data->device.protocol_ver) || !strlen(data->device.model_name))
  246. die("The device does not respond correctly. Is it Videohub?");
  247. if (strstr(data->device.protocol_ver, "2.") != data->device.protocol_ver)
  248. die("Device protocol is %s but this program supports 2.x only.\n",
  249. data->device.protocol_ver);
  250. if (!data->device.dev_present) {
  251. if (data->device.needs_fw_update) {
  252. die("Device reports that it needs firmware update.");
  253. }
  254. die("Device reports that it is not present.");
  255. }
  256. check_number_of_ports(&data->inputs);
  257. check_number_of_ports(&data->outputs);
  258. if (num_parsed_cmds) {
  259. unsigned int i;
  260. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  261. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  262. if (!ve->param1)
  263. continue;
  264. prepare_cmd_entry(data, &parsed_cmds.entry[i]);
  265. }
  266. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  267. char cmd_buffer[1024];
  268. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  269. if (!ve->param1)
  270. continue;
  271. format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
  272. if (strlen(cmd_buffer)) {
  273. show_cmd(data, ve);
  274. send_device_command(data, cmd_buffer);
  275. read_device_command_stream(data);
  276. }
  277. }
  278. // Show the result after commands
  279. if (test_data)
  280. print_device_full(data);
  281. } else if (show_monitor) {
  282. while (1) {
  283. int sleeps = 0;
  284. printf("\e[2J\e[H"); // Clear screen
  285. time_t now = time(NULL);
  286. struct tm *tm = localtime(&now);
  287. printf("Last update: %s\n", asctime(tm));
  288. print_device_info(data);
  289. print_device_video_inputs(data);
  290. print_device_video_outputs(data);
  291. fflush(stdout);
  292. do {
  293. usleep(500000);
  294. if (++sleeps >= 20)
  295. send_device_command(data, "PING:\n\n");
  296. } while (read_device_command_stream(data) == 0);
  297. }
  298. } else if (show_list) {
  299. if (show_list & action_list_device) print_device_info(data);
  300. if (show_list & action_list_vinputs) print_device_video_inputs(data);
  301. if (show_list & action_list_voutputs) print_device_video_outputs(data);
  302. fflush(stdout);
  303. } else if (show_backup) {
  304. print_device_backup(data);
  305. } else if (show_info) {
  306. print_device_full(data);
  307. }
  308. shutdown_fd(&data->dev_fd);
  309. free(test_data);
  310. return 0;
  311. }