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

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 "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("Video input/output configuration:\n");
  80. printf(" --vi-name <in_X> <name> | Set video input port X name.\n");
  81. printf(" --vo-name <out_X> <name> | Set video output port X name.\n");
  82. printf("\n");
  83. printf(" --vo-route <out_X> <in_Y> | Connect video output X to video input 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. // Check environment
  99. data->dev_host = getenv("VIDEOHUB_HOST");
  100. data->dev_port = getenv("VIDEOHUB_PORT");
  101. // Set defaults
  102. if (!data->dev_port)
  103. data->dev_port = "9990";
  104. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  105. if (j == '?') // Invalid parameter
  106. exit(EXIT_FAILURE);
  107. switch (j) {
  108. case 'h': // --host
  109. data->dev_host = optarg;
  110. break;
  111. case 'p': // --port
  112. data->dev_port = optarg;
  113. break;
  114. case 'd': // --debug
  115. debug++;
  116. if (debug)
  117. quiet = 0; // Disable quiet
  118. break;
  119. case 'q': // --quiet
  120. quiet = !quiet;
  121. break;
  122. case 'i': // --info
  123. show_info = 1;
  124. break;
  125. case 'm': // --monitor
  126. show_monitor = 1;
  127. break;
  128. case 901: show_list |= action_list_device; break; // --list-device
  129. case 902: show_list |= action_list_vinputs; break; // --list-vinputs
  130. case 903: show_list |= action_list_voutputs; break; // --list-voutputs
  131. case 1001: // --vi-name
  132. case 1002: // --vo-name
  133. case 1011: // --vi-route
  134. case 1012: // --vo-route
  135. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  136. die("No more than %u commands are supported.", num_parsed_cmds);
  137. if (optind == argc || argv[optind - 1][0] == '-' || argv[optind][0] == '-') {
  138. fprintf(stderr, "%s: option '%s' requires two arguments\n", argv[0], argv[optind - 2]);
  139. exit(EXIT_FAILURE);
  140. }
  141. switch (j) {
  142. case 1001: c->cmd = CMD_INPUT_LABELS; break; // --vi-name
  143. case 1002: c->cmd = CMD_OUTPUT_LABELS; break; // --vo-name
  144. case 1011: c->cmd = CMD_VIDEO_OUTPUT_ROUTING; break; // --vo-route
  145. }
  146. c->param1 = argv[optind - 1];
  147. c->param2 = argv[optind];
  148. c->param1 = argv[optind - 1];
  149. c->param2 = argv[optind];
  150. c = &parsed_cmds.entry[++num_parsed_cmds];
  151. break;
  152. case 1021: // --vo-lock
  153. case 1022: // --vo-unlock
  154. if (num_parsed_cmds == ARRAY_SIZE(parsed_cmds.entry))
  155. die("No more than %u commands are supported.", num_parsed_cmds);
  156. c->cmd = CMD_VIDEO_OUTPUT_LOCKS;
  157. c->param1 = argv[optind - 1];
  158. c->do_lock = (j == 1021);
  159. c = &parsed_cmds.entry[++num_parsed_cmds];
  160. break;
  161. case 'H': // --help
  162. show_help(data);
  163. exit(EXIT_SUCCESS);
  164. case 'V': // --version
  165. printf("%s\n", program_id);
  166. exit(EXIT_SUCCESS);
  167. }
  168. }
  169. if (!data->dev_host || !strtoul(data->dev_port, NULL, 10))
  170. err = 1;
  171. if (err) {
  172. show_help(data);
  173. if (!data->dev_host)
  174. fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
  175. exit(EXIT_FAILURE);
  176. }
  177. d("Device address: %s:%s\n", data->dev_host, data->dev_port);
  178. }
  179. static int read_device_command_stream(struct videohub_data *d) {
  180. int ret, ncommands = 0;
  181. char buf[8192 + 1];
  182. memset(buf, 0, sizeof(buf));
  183. while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
  184. ncommands += parse_text_buffer(d, buf);
  185. memset(buf, 0, sizeof(buf));
  186. }
  187. return ncommands;
  188. }
  189. int main(int argc, char **argv) {
  190. struct videohub_data *data = &maindata;
  191. parse_options(data, argc, argv);
  192. set_log_io_errors(0);
  193. data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
  194. if (data->dev_fd < 0)
  195. exit(EXIT_FAILURE);
  196. read_device_command_stream(data);
  197. if (!strlen(data->device.protocol_ver) || !strlen(data->device.model_name))
  198. die("The device does not respond correctly. Is it Videohub?");
  199. if (strstr(data->device.protocol_ver, "2.") != data->device.protocol_ver)
  200. die("Device protocol is %s but this program supports 2.x only.\n",
  201. data->device.protocol_ver);
  202. if (!data->device.dev_present) {
  203. if (data->device.needs_fw_update) {
  204. die("Device reports that it needs firmware update.");
  205. }
  206. die("Device reports that it's not present.");
  207. }
  208. if (data->device.num_video_inputs > ARRAY_SIZE(data->inputs))
  209. die("The device supports %d inputs. Recompile the program with more MAX_INPUTS (currently %d)",
  210. data->device.num_video_inputs, MAX_INPUTS);
  211. if (data->device.num_video_outputs > ARRAY_SIZE(data->outputs))
  212. die("The device supports %d outputs. Recompile the program with more MAX_OUTPUTS (currently %d)\n",
  213. data->device.num_video_outputs, MAX_OUTPUTS);
  214. if (num_parsed_cmds) {
  215. unsigned int i;
  216. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  217. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  218. if (!ve->param1)
  219. continue;
  220. prepare_cmd_entry(data, &parsed_cmds.entry[i]);
  221. }
  222. //print_device_settings(data);
  223. for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
  224. char cmd_buffer[1024];
  225. struct vcmd_entry *ve = &parsed_cmds.entry[i];
  226. if (!ve->param1)
  227. continue;
  228. format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
  229. if (strlen(cmd_buffer)) {
  230. printf("%s", cmd_buffer);
  231. fdwrite(data->dev_fd, cmd_buffer, strlen(cmd_buffer));
  232. }
  233. }
  234. //usleep(100000);
  235. //read_device_command_stream(data);
  236. //print_device_settings(data);
  237. } else if (show_monitor) {
  238. while (1) {
  239. int sleeps = 0;
  240. printf("\e[2J\e[H"); // Clear screen
  241. time_t now = time(NULL);
  242. struct tm *tm = localtime(&now);
  243. printf("Last update: %s\n", asctime(tm));
  244. print_device_info(data);
  245. print_device_video_inputs(data);
  246. print_device_video_outputs(data);
  247. fflush(stdout);
  248. do {
  249. usleep(500000);
  250. if (++sleeps >= 20) {
  251. char *ping_cmd = "PING:\n\n";
  252. fdwrite(data->dev_fd, ping_cmd, strlen(ping_cmd));
  253. }
  254. } while (read_device_command_stream(data) == 0);
  255. }
  256. } else if (show_list) {
  257. if (show_list & action_list_device) print_device_info(data);
  258. if (show_list & action_list_vinputs) print_device_video_inputs(data);
  259. if (show_list & action_list_voutputs) print_device_video_outputs(data);
  260. fflush(stdout);
  261. } else if (show_info) {
  262. print_device_info(data);
  263. print_device_video_inputs(data);
  264. print_device_video_outputs(data);
  265. fflush(stdout);
  266. }
  267. shutdown_fd(&data->dev_fd);
  268. return 0;
  269. }