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.0KB

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