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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "version.h"
  20. #include "libfuncs/libfuncs.h"
  21. int debug;
  22. int quiet;
  23. static struct videohub_data maindata;
  24. static int show_info = 1;
  25. static int show_monitor = 0;
  26. static const char *program_id = PROGRAM_NAME " Version: " VERSION " Git: " GIT_VER;
  27. static const char short_options[] = "h:p:qdHVim";
  28. static const struct option long_options[] = {
  29. { "host", required_argument, NULL, 'h' },
  30. { "port", required_argument, NULL, 'p' },
  31. { "quiet", no_argument, NULL, 'q' },
  32. { "debug", no_argument, NULL, 'd' },
  33. { "help", no_argument, NULL, 'H' },
  34. { "version", no_argument, NULL, 'V' },
  35. { "info", no_argument, NULL, 'i' },
  36. { "monitor", no_argument, NULL, 'm' },
  37. { "vi-name", required_argument, NULL, 1001 },
  38. { "vo-name", required_argument, NULL, 1002 },
  39. { "vo-route", required_argument, NULL, 1011 },
  40. { "vo-lock", required_argument, NULL, 1021 },
  41. { "vo-unlock", required_argument, NULL, 1022 },
  42. { 0, 0, 0, 0 }
  43. };
  44. static void show_help(struct videohub_data *data) {
  45. printf("\n");
  46. printf(" Usage: " PROGRAM_NAME " --host <host> [..commands..]\n");
  47. printf("\n");
  48. printf("Main options:\n");
  49. printf(" -h --host <hostname> | Set device hostname.\n");
  50. printf(" -p --port <port_number> | Set device port (default: 9990).\n");
  51. printf("\n");
  52. printf("Misc options:\n");
  53. printf(" -d --debug | Increase logging verbosity.\n");
  54. printf(" -q --quiet | Suppress warnings.\n");
  55. printf(" -H --help | Show help screen.\n");
  56. printf(" -V --version | Show program version.\n");
  57. printf("\n");
  58. printf("Commands:\n");
  59. printf(" -i --info | Show device info (default command).\n");
  60. printf(" -m --monitor | Show real time monitor for config changes.\n");
  61. printf("\n");
  62. printf("Configuration:\n");
  63. printf(" --vi-name <in_X> <name> | Set input <name> to input port X.\n");
  64. printf(" --vo-name <out_X> <name> | Set output <name> to output port X.\n");
  65. printf("\n");
  66. printf(" --vo-route <out_X> <in_Y> | Connect output port X to input port Y.\n");
  67. printf("\n");
  68. printf(" --vo-lock <out_X> | Lock output port X.\n");
  69. printf(" --vo-unlock <out_X> | Unlock output port X.\n");
  70. printf("\n");
  71. printf(" NOTE: For <in_X/out_X/in_Y> you may use port number or port name.\n");
  72. printf("\n");
  73. }
  74. static int num_parsed_cmds = 0;
  75. static struct run_cmds {
  76. struct vcmd_entry entry[MAX_RUN_CMDS];
  77. } parsed_cmds;
  78. static void parse_options(struct videohub_data *data, int argc, char **argv) {
  79. int j, err = 0;
  80. struct vcmd_entry *c = &parsed_cmds.entry[0];
  81. // Set defaults
  82. data->dev_port = "9990";
  83. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  84. if (j == '?') // Invalid parameter
  85. exit(EXIT_FAILURE);
  86. switch (j) {
  87. case 'h': // --host
  88. data->dev_host = optarg;
  89. break;
  90. case 'p': // --port
  91. data->dev_port = optarg;
  92. break;
  93. case 'd': // --debug
  94. debug++;
  95. if (debug)
  96. quiet = 0; // Disable quiet
  97. break;
  98. case 'q': // --quiet
  99. quiet = !quiet;
  100. break;
  101. case 'i': // --info
  102. show_info = 1;
  103. break;
  104. case 'm': // --monitor
  105. show_monitor = 1;
  106. break;
  107. case 1001: // --vi-name
  108. case 1002: // --vo-name
  109. case 1011: // --vi-route
  110. case 1012: // --vo-route
  111. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  112. die("No more than %u commands are supported.", num_parsed_cmds);
  113. if (optind == argc || argv[optind - 1][0] == '-' || argv[optind][0] == '-') {
  114. fprintf(stderr, "%s: option '%s' requires two arguments\n", argv[0], argv[optind - 2]);
  115. exit(EXIT_FAILURE);
  116. }
  117. switch (j) {
  118. case 1001: c->cmd = CMD_INPUT_LABELS; break; // --vi-name
  119. case 1002: c->cmd = CMD_OUTPUT_LABELS; break; // --vo-name
  120. case 1011: c->cmd = CMD_VIDEO_OUTPUT_ROUTING; break; // --vo-route
  121. }
  122. c->param1 = argv[optind - 1];
  123. c->param2 = argv[optind];
  124. c->param1 = argv[optind - 1];
  125. c->param2 = argv[optind];
  126. c = &parsed_cmds.entry[++num_parsed_cmds];
  127. break;
  128. case 1021: // --vo-lock
  129. case 1022: // --vo-unlock
  130. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  131. die("No more than %u commands are supported.", num_parsed_cmds);
  132. c->cmd = CMD_VIDEO_OUTPUT_LOCKS;
  133. c->param1 = argv[optind - 1];
  134. c->do_lock = (j == 1021);
  135. c = &parsed_cmds.entry[++num_parsed_cmds];
  136. break;
  137. case 'H': // --help
  138. show_help(data);
  139. exit(EXIT_SUCCESS);
  140. case 'V': // --version
  141. // program_id is already printed on startup, just exit.
  142. exit(EXIT_SUCCESS);
  143. }
  144. }
  145. if (!data->dev_host || !strtoul(data->dev_port, NULL, 10))
  146. err = 1;
  147. if (err) {
  148. show_help(data);
  149. if (!data->dev_host)
  150. fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
  151. exit(EXIT_FAILURE);
  152. }
  153. d("Device address: %s:%s\n", data->dev_host, data->dev_port);
  154. }
  155. static void print_device_desc(struct device_desc *d) {
  156. printf("\n");
  157. printf("Protocol version: %s\n", d->protocol_ver);
  158. printf("Model name: %s\n", d->model_name);
  159. printf("Unique ID: %s\n", d->unique_id);
  160. printf("Video inputs: %u\n", d->num_video_inputs);
  161. printf("Video processing units: %u\n", d->num_video_processing_units);
  162. printf("Video outputs: %u\n", d->num_video_outputs);
  163. printf("Video monitoring outputs: %u\n", d->num_video_monitoring_outputs);
  164. printf("Serial ports: %u\n", d->num_serial_ports);
  165. }
  166. static void printf_line(int len) {
  167. int i;
  168. for (i = 0; i < len; i++)
  169. printf("-");
  170. printf("\n");
  171. }
  172. static void print_device_settings(struct videohub_data *d) {
  173. unsigned int i;
  174. printf("\n");
  175. printf_line(71);
  176. printf("| i# | %-25s | o# | x | %-25s |\n", "Input name", "Output name");
  177. printf_line(71);
  178. for(i = 0; i < MIN(d->device.num_video_outputs, ARRAY_SIZE(d->outputs)); i++) {
  179. printf("| %2d | %-25s | %2d | %c | %-25s |\n",
  180. i + 1,
  181. d->inputs[d->outputs[i].routed_to].name,
  182. i + 1,
  183. d->outputs[i].locked ? (d->outputs[i].locked_other ? 'L' : 'O') : ' ',
  184. d->outputs[i].name
  185. );
  186. }
  187. printf_line(71);
  188. }
  189. static int read_device_command_stream(struct videohub_data *d) {
  190. int ret, ncommands = 0;
  191. char buf[8192 + 1];
  192. memset(buf, 0, sizeof(buf));
  193. while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
  194. ncommands += parse_text_buffer(d, buf);
  195. memset(buf, 0, sizeof(buf));
  196. }
  197. return ncommands;
  198. }
  199. int main(int argc, char **argv) {
  200. struct videohub_data *data = &maindata;
  201. printf("%s\n", program_id);
  202. parse_options(data, argc, argv);
  203. set_log_io_errors(0);
  204. data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
  205. if (data->dev_fd < 0)
  206. exit(EXIT_FAILURE);
  207. read_device_command_stream(data);
  208. if (!strlen(data->device.protocol_ver) || !strlen(data->device.model_name))
  209. die("The device does not respond correctly. Is it Videohub?");
  210. if (strstr(data->device.protocol_ver, "2.") != data->device.protocol_ver)
  211. die("Device protocol is %s but this program supports 2.x only.\n",
  212. data->device.protocol_ver);
  213. if (!data->device.dev_present) {
  214. if (data->device.needs_fw_update) {
  215. die("Device reports that it needs firmware update.");
  216. }
  217. die("Device reports that it's not present.");
  218. }
  219. if (data->device.num_video_inputs > ARRAY_SIZE(data->inputs))
  220. die("The device supports %d inputs. Recompile the program with more MAX_INPUTS (currently %d)",
  221. data->device.num_video_inputs, MAX_INPUTS);
  222. if (data->device.num_video_outputs > ARRAY_SIZE(data->outputs))
  223. die("The device supports %d outputs. Recompile the program with more MAX_OUTPUTS (currently %d)\n",
  224. data->device.num_video_outputs, MAX_OUTPUTS);
  225. if (num_parsed_cmds) {
  226. unsigned int i;
  227. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  228. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  229. if (!ve->param1)
  230. continue;
  231. prepare_cmd_entry(data, &parsed_cmds.entry[i]);
  232. }
  233. //print_device_settings(data);
  234. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  235. char cmd_buffer[1024];
  236. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  237. if (!ve->param1)
  238. continue;
  239. format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
  240. if (strlen(cmd_buffer)) {
  241. printf("%s", cmd_buffer);
  242. fdwrite(data->dev_fd, cmd_buffer, strlen(cmd_buffer));
  243. }
  244. }
  245. //usleep(100000);
  246. //read_device_command_stream(data);
  247. //print_device_settings(data);
  248. } else if (show_monitor) {
  249. while (1) {
  250. printf("\e[2J\e[H"); // Clear screen
  251. printf("%s\n", program_id);
  252. print_device_desc(&data->device);
  253. print_device_settings(data);
  254. fflush(stdout);
  255. do {
  256. sleep(1);
  257. } while (read_device_command_stream(data) == 0);
  258. }
  259. } else if (show_info) {
  260. print_device_desc(&data->device);
  261. print_device_settings(data);
  262. fflush(stdout);
  263. }
  264. shutdown_fd(&data->dev_fd);
  265. return 0;
  266. }