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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. { 0, 0, 0, 0 }
  38. };
  39. static void show_help(struct videohub_data *data) {
  40. printf("\n");
  41. printf(" Usage: " PROGRAM_NAME " --host <host> [..commands..]\n");
  42. printf("\n");
  43. printf("Main options:\n");
  44. printf(" -h --host <hostname> | Set device hostname.\n");
  45. printf(" -p --port <port_number> | Set device port (default: 9990).\n");
  46. printf("\n");
  47. printf("Misc options:\n");
  48. printf(" -d --debug | Increase logging verbosity.\n");
  49. printf(" -q --quiet | Suppress warnings.\n");
  50. printf(" -H --help | Show help screen.\n");
  51. printf(" -V --version | Show program version.\n");
  52. printf("\n");
  53. printf("Commands:\n");
  54. printf(" -i --info | Show device info (default command).\n");
  55. printf(" -m --monitor | Show real time monitor for config changes.\n");
  56. printf("\n");
  57. }
  58. static void parse_options(struct videohub_data *data, int argc, char **argv) {
  59. int j, err = 0;
  60. // Set defaults
  61. data->dev_port = "9990";
  62. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  63. if (j == '?') // Invalid parameter
  64. exit(EXIT_FAILURE);
  65. switch (j) {
  66. case 'h': // --host
  67. data->dev_host = optarg;
  68. break;
  69. case 'p': // --port
  70. data->dev_port = optarg;
  71. break;
  72. case 'd': // --debug
  73. debug++;
  74. if (debug)
  75. quiet = 0; // Disable quiet
  76. break;
  77. case 'q': // --quiet
  78. quiet = !quiet;
  79. break;
  80. case 'i': // --info
  81. show_info = 1;
  82. break;
  83. case 'm': // --monitor
  84. show_monitor = 1;
  85. break;
  86. case 'H': // --help
  87. show_help(data);
  88. exit(EXIT_SUCCESS);
  89. case 'V': // --version
  90. // program_id is already printed on startup, just exit.
  91. exit(EXIT_SUCCESS);
  92. }
  93. }
  94. if (!data->dev_host || !strtoul(data->dev_port, NULL, 10))
  95. err = 1;
  96. if (err) {
  97. show_help(data);
  98. if (!data->dev_host)
  99. fprintf(stderr, "ERROR: host is not set. Use --host option.\n");
  100. exit(EXIT_FAILURE);
  101. }
  102. d("Device address: %s:%s\n", data->dev_host, data->dev_port);
  103. }
  104. static void print_device_desc(struct device_desc *d) {
  105. printf("\n");
  106. printf("Protocol version: %s\n", d->protocol_ver);
  107. printf("Model name: %s\n", d->model_name);
  108. printf("Unique ID: %s\n", d->unique_id);
  109. printf("Video inputs: %u\n", d->num_video_inputs);
  110. printf("Video processing units: %u\n", d->num_video_processing_units);
  111. printf("Video outputs: %u\n", d->num_video_outputs);
  112. printf("Video monitoring outputs: %u\n", d->num_video_monitoring_outputs);
  113. printf("Serial ports: %u\n", d->num_serial_ports);
  114. }
  115. static void printf_line(int len) {
  116. int i;
  117. for (i = 0; i < len; i++)
  118. printf("-");
  119. printf("\n");
  120. }
  121. static void print_device_settings(struct videohub_data *d) {
  122. unsigned int i;
  123. printf("\n");
  124. printf_line(71);
  125. printf("| i# | %-25s | o# | x | %-25s |\n", "Input name", "Output name");
  126. printf_line(71);
  127. for(i = 0; i < MIN(d->device.num_video_outputs, ARRAY_SIZE(d->outputs)); i++) {
  128. printf("| %2d | %-25s | %2d | %c | %-25s |\n",
  129. i + 1,
  130. d->inputs[d->outputs[i].routed_to].name,
  131. i + 1,
  132. d->outputs[i].locked ? (d->outputs[i].locked_other ? 'L' : 'O') : ' ',
  133. d->outputs[i].name
  134. );
  135. }
  136. printf_line(71);
  137. }
  138. static int read_device_command_stream(struct videohub_data *d) {
  139. int ret, ncommands = 0;
  140. char buf[8192 + 1];
  141. memset(buf, 0, sizeof(buf));
  142. while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
  143. ncommands += parse_text_buffer(d, buf);
  144. memset(buf, 0, sizeof(buf));
  145. }
  146. return ncommands;
  147. }
  148. int main(int argc, char **argv) {
  149. struct videohub_data *data = &maindata;
  150. printf("%s\n", program_id);
  151. parse_options(data, argc, argv);
  152. set_log_io_errors(0);
  153. data->dev_fd = connect_client(SOCK_STREAM, data->dev_host, data->dev_port);
  154. if (data->dev_fd < 0)
  155. exit(EXIT_FAILURE);
  156. read_device_command_stream(data);
  157. if (!strlen(data->device.protocol_ver) || !strlen(data->device.model_name))
  158. die("The device does not respond correctly. Is it Videohub?");
  159. if (strstr(data->device.protocol_ver, "2.") != data->device.protocol_ver)
  160. die("Device protocol is %s but this program supports 2.x only.\n",
  161. data->device.protocol_ver);
  162. if (!data->device.dev_present) {
  163. if (data->device.needs_fw_update) {
  164. die("Device reports that it needs firmware update.");
  165. }
  166. die("Device reports that it's not present.");
  167. }
  168. if (data->device.num_video_inputs > ARRAY_SIZE(data->inputs))
  169. die("The device supports %d inputs. Recompile the program with more MAX_INPUTS (currently %d)",
  170. data->device.num_video_inputs, MAX_INPUTS);
  171. if (data->device.num_video_outputs > ARRAY_SIZE(data->outputs))
  172. die("The device supports %d outputs. Recompile the program with more MAX_OUTPUTS (currently %d)\n",
  173. data->device.num_video_outputs, MAX_OUTPUTS);
  174. if (show_monitor) {
  175. while (1) {
  176. printf("\e[2J\e[H"); // Clear screen
  177. printf("%s\n", program_id);
  178. print_device_desc(&data->device);
  179. print_device_settings(data);
  180. fflush(stdout);
  181. do {
  182. sleep(1);
  183. } while (read_device_command_stream(data) == 0);
  184. }
  185. } else if (show_info) {
  186. print_device_desc(&data->device);
  187. print_device_settings(data);
  188. fflush(stdout);
  189. }
  190. shutdown_fd(&data->dev_fd);
  191. return 0;
  192. }