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 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "data.h"
  16. #include "cmd.h"
  17. #include "net.h"
  18. #include "util.h"
  19. #include "display.h"
  20. #include "version.h"
  21. #include "libfuncs/libfuncs.h"
  22. int debug;
  23. int quiet;
  24. static struct videohub_data maindata;
  25. static int show_info = 1;
  26. static int show_monitor = 0;
  27. static int show_backup = 0;
  28. static int show_list = 0;
  29. enum list_actions {
  30. action_list_device = (1 << 0),
  31. action_list_vinputs = (1 << 1),
  32. action_list_voutputs = (1 << 2),
  33. };
  34. static const char *program_id = PROGRAM_NAME " Version: " VERSION " Git: " GIT_VER;
  35. static const char short_options[] = "h:p:qdHVimb";
  36. static const struct option long_options[] = {
  37. { "host", required_argument, NULL, 'h' },
  38. { "port", required_argument, NULL, 'p' },
  39. { "quiet", no_argument, NULL, 'q' },
  40. { "debug", no_argument, NULL, 'd' },
  41. { "help", no_argument, NULL, 'H' },
  42. { "version", no_argument, NULL, 'V' },
  43. { "info", no_argument, NULL, 'i' },
  44. { "monitor", no_argument, NULL, 'm' },
  45. { "backup", no_argument, NULL, 'b' },
  46. { "list-device", no_argument, NULL, 901 },
  47. { "list-vinputs", no_argument, NULL, 902 },
  48. { "list-voutputs", no_argument, NULL, 903 },
  49. { "vi-name", required_argument, NULL, 1001 },
  50. { "vo-name", required_argument, NULL, 1002 },
  51. { "vo-route", required_argument, NULL, 1011 },
  52. { "vo-lock", required_argument, NULL, 1021 },
  53. { "vo-unlock", required_argument, NULL, 1022 },
  54. { 0, 0, 0, 0 }
  55. };
  56. static void show_help(struct videohub_data *data) {
  57. printf("%s\n", program_id);
  58. printf("\n");
  59. printf(" Usage: " PROGRAM_NAME " --host <host> [..commands..]\n");
  60. printf("\n");
  61. printf("Main options:\n");
  62. printf(" -h --host <hostname> | Set device hostname.\n");
  63. printf(" -p --port <port_number> | Set device port (default: 9990).\n");
  64. printf("\n");
  65. printf("Misc options:\n");
  66. printf(" -d --debug | Increase logging verbosity.\n");
  67. printf(" -q --quiet | Suppress warnings.\n");
  68. printf(" -H --help | Show help screen.\n");
  69. printf(" -V --version | Show program version.\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 equivallent 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(" NOTE: For <in_X/out_X/in_Y> you may use port number or port name.\n");
  93. printf("\n");
  94. }
  95. static int num_parsed_cmds = 0;
  96. static struct run_cmds {
  97. struct vcmd_entry entry[MAX_RUN_CMDS];
  98. } parsed_cmds;
  99. static void parse_options(struct videohub_data *data, int argc, char **argv) {
  100. int j, err = 0;
  101. struct vcmd_entry *c = &parsed_cmds.entry[0];
  102. // Check environment
  103. data->dev_host = getenv("VIDEOHUB_HOST");
  104. data->dev_port = getenv("VIDEOHUB_PORT");
  105. // Set defaults
  106. if (!data->dev_port)
  107. data->dev_port = "9990";
  108. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  109. if (j == '?') // Invalid parameter
  110. exit(EXIT_FAILURE);
  111. switch (j) {
  112. case 'h': // --host
  113. data->dev_host = optarg;
  114. break;
  115. case 'p': // --port
  116. data->dev_port = optarg;
  117. break;
  118. case 'd': // --debug
  119. debug++;
  120. if (debug)
  121. quiet = 0; // Disable quiet
  122. break;
  123. case 'q': // --quiet
  124. quiet = !quiet;
  125. break;
  126. case 'i': // --info
  127. show_info = 1;
  128. break;
  129. case 'm': // --monitor
  130. show_monitor = 1;
  131. break;
  132. case 'b': // --backup
  133. show_backup = 1;
  134. break;
  135. case 901: show_list |= action_list_device; break; // --list-device
  136. case 902: show_list |= action_list_vinputs; break; // --list-vinputs
  137. case 903: show_list |= action_list_voutputs; break; // --list-voutputs
  138. case 1001: // --vi-name
  139. case 1002: // --vo-name
  140. case 1011: // --vi-route
  141. case 1012: // --vo-route
  142. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  143. die("No more than %u commands are supported.", num_parsed_cmds);
  144. if (optind == argc || argv[optind - 1][0] == '-' || argv[optind][0] == '-') {
  145. fprintf(stderr, "%s: option '%s' requires two arguments\n", argv[0], argv[optind - 2]);
  146. exit(EXIT_FAILURE);
  147. }
  148. switch (j) {
  149. case 1001: c->cmd = CMD_INPUT_LABELS; break; // --vi-name
  150. case 1002: c->cmd = CMD_OUTPUT_LABELS; break; // --vo-name
  151. case 1011: c->cmd = CMD_VIDEO_OUTPUT_ROUTING; break; // --vo-route
  152. }
  153. c->param1 = argv[optind - 1];
  154. c->param2 = argv[optind];
  155. c->param1 = argv[optind - 1];
  156. c->param2 = argv[optind];
  157. c = &parsed_cmds.entry[++num_parsed_cmds];
  158. break;
  159. case 1021: // --vo-lock
  160. case 1022: // --vo-unlock
  161. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  162. die("No more than %u commands are supported.", num_parsed_cmds);
  163. c->cmd = CMD_VIDEO_OUTPUT_LOCKS;
  164. c->param1 = argv[optind - 1];
  165. c->do_lock = (j == 1021);
  166. c = &parsed_cmds.entry[++num_parsed_cmds];
  167. break;
  168. case 'H': // --help
  169. show_help(data);
  170. exit(EXIT_SUCCESS);
  171. case 'V': // --version
  172. printf("%s\n", program_id);
  173. exit(EXIT_SUCCESS);
  174. }
  175. }
  176. if (!data->dev_host || !strtoul(data->dev_port, NULL, 10))
  177. err = 1;
  178. if (err) {
  179. show_help(data);
  180. if (!data->dev_host)
  181. fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
  182. exit(EXIT_FAILURE);
  183. }
  184. d("Device address: %s:%s\n", data->dev_host, data->dev_port);
  185. }
  186. static int read_device_command_stream(struct videohub_data *d) {
  187. int ret, ncommands = 0;
  188. char buf[8192 + 1];
  189. memset(buf, 0, sizeof(buf));
  190. while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
  191. ncommands += parse_text_buffer(d, buf);
  192. memset(buf, 0, sizeof(buf));
  193. }
  194. return ncommands;
  195. }
  196. int main(int argc, char **argv) {
  197. struct videohub_data *data = &maindata;
  198. parse_options(data, argc, argv);
  199. set_log_io_errors(0);
  200. data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
  201. if (data->dev_fd < 0)
  202. exit(EXIT_FAILURE);
  203. read_device_command_stream(data);
  204. if (!strlen(data->device.protocol_ver) || !strlen(data->device.model_name))
  205. die("The device does not respond correctly. Is it Videohub?");
  206. if (strstr(data->device.protocol_ver, "2.") != data->device.protocol_ver)
  207. die("Device protocol is %s but this program supports 2.x only.\n",
  208. data->device.protocol_ver);
  209. if (!data->device.dev_present) {
  210. if (data->device.needs_fw_update) {
  211. die("Device reports that it needs firmware update.");
  212. }
  213. die("Device reports that it's not present.");
  214. }
  215. if (data->device.num_video_inputs > ARRAY_SIZE(data->inputs))
  216. die("The device supports %d inputs. Recompile the program with more MAX_INPUTS (currently %d)",
  217. data->device.num_video_inputs, MAX_INPUTS);
  218. if (data->device.num_video_outputs > ARRAY_SIZE(data->outputs))
  219. die("The device supports %d outputs. Recompile the program with more MAX_OUTPUTS (currently %d)\n",
  220. data->device.num_video_outputs, MAX_OUTPUTS);
  221. if (num_parsed_cmds) {
  222. unsigned int i;
  223. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  224. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  225. if (!ve->param1)
  226. continue;
  227. prepare_cmd_entry(data, &parsed_cmds.entry[i]);
  228. }
  229. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  230. char cmd_buffer[1024];
  231. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  232. if (!ve->param1)
  233. continue;
  234. format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
  235. if (strlen(cmd_buffer)) {
  236. show_cmd(data, ve);
  237. fdwrite(data->dev_fd, cmd_buffer, strlen(cmd_buffer));
  238. read_device_command_stream(data);
  239. }
  240. }
  241. } else if (show_monitor) {
  242. while (1) {
  243. int sleeps = 0;
  244. printf("\e[2J\e[H"); // Clear screen
  245. time_t now = time(NULL);
  246. struct tm *tm = localtime(&now);
  247. printf("Last update: %s\n", asctime(tm));
  248. print_device_info(data);
  249. print_device_video_inputs(data);
  250. print_device_video_outputs(data);
  251. fflush(stdout);
  252. do {
  253. usleep(500000);
  254. if (++sleeps >= 20) {
  255. char *ping_cmd = "PING:\n\n";
  256. fdwrite(data->dev_fd, ping_cmd, strlen(ping_cmd));
  257. }
  258. } while (read_device_command_stream(data) == 0);
  259. }
  260. } else if (show_list) {
  261. if (show_list & action_list_device) print_device_info(data);
  262. if (show_list & action_list_vinputs) print_device_video_inputs(data);
  263. if (show_list & action_list_voutputs) print_device_video_outputs(data);
  264. fflush(stdout);
  265. } else if (show_backup) {
  266. print_device_backup(data);
  267. } else if (show_info) {
  268. print_device_info(data);
  269. print_device_video_inputs(data);
  270. print_device_video_outputs(data);
  271. fflush(stdout);
  272. }
  273. shutdown_fd(&data->dev_fd);
  274. return 0;
  275. }