UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
switcher.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2014-2024 CESNET z.s.p.o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <assert.h>
39#include <errno.h>
40#include <inttypes.h>
41#include <limits.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45
46#include "audio/types.h"
47#include "debug.h"
48#include "lib_common.h"
49#include "module.h"
50#include "utils/color_out.h"
51#include "utils/macros.h"
52#include "utils/text.h"
53#include "video.h"
54#include "video_capture.h"
55
56#define MOD_NAME "[switcher] "
57
58/* prototypes of functions defined in this module */
59static void show_help(void);
60static void vidcap_switcher_done(void *state);
61
62static void show_help()
63{
64 char desc[] =
65 TBOLD("switcher") " capture allow switching between given "
66 "video devices optionally with audio "
67 "(see below or wiki for usage).\n\n"
68 "Switching can be done by using keys 1-N or "
69 "via a control socket.\n\n";
71
72 color_printf("Usage:\n");
73 color_printf("\t" TBOLD(TRED("-t switcher")) "[opts] " TBOLD(
74 "-t <dev1> -t "
75 "<dev2> [-t ...]") "\n\n");
76
77 color_printf("options:\n");
79 "<devN>") " - a configuration of device to be switched\n");
81 "select=<N>") " - specifies initially selected device idx\n");
83 "excl_init") " - devices will be initialized after switching to "
84 "and\n\t\tdeinitialized after switching to another\n");
86 "fallback") " - in case that capture doesn't return a frame (in "
87 "time),\n\t\tcapture from next available "
88 "device(s)\n\n");
89
90 color_printf(TBOLD("Audio") " and other " TBOLD(
91 "positional") " options should precede its respective device.\n");
92 color_printf("Eg. in:\n" TBOLD(
93 " uv -t switcher -s embedded -F flip -t testcard -s "
94 "analog -t decklink") "\n");
95 color_printf("flip & embedded aplies to testcard, analog to decklink.\n");
97 "See "
98 "also: <" TUNDERLINE("https://github.com/CESNET/UltraGrid/wiki/"
99 "Video-Switcher#audio") ">\n\n");
100}
101
103 struct module mod;
104 struct vidcap **devices;
105 char (*device_names)[128];
106 unsigned int devices_cnt;
107
108 unsigned int selected_device;
109
113};
114
115
116static void vidcap_switcher_probe(struct device_info **available_cards, int *count, void (**deleter)(void *))
117{
118 *deleter = free;
119 *available_cards = NULL;
120 *count = 0;
121}
122
124 for (unsigned int i = 0U; i < MIN(s->devices_cnt, 10); ++i) {
125 struct msg_universal *m = (struct msg_universal *) new_message(sizeof(struct msg_universal));
126 snprintf(m->text, sizeof m->text, "map %d capture.data %d#switch to video input %d", i + 1, i, i + 1);
127 struct response *r = send_message_sync(get_root_module(&s->mod), "keycontrol", (struct message *) m, 100, SEND_MESSAGE_FLAG_QUIET | SEND_MESSAGE_FLAG_NO_STORE);
129 log_msg(LOG_LEVEL_ERROR, "Cannot register keyboard control for video switcher (error %d)!\n", response_get_status(r));
130 }
131 free_response(r);
132 }
133}
134
135static int
136parse_fmt(struct vidcap_switcher_state *s, char *cfg)
137{
138 assert(cfg != NULL);
139 char *save_ptr = NULL;
140 char *item = NULL;
141 while ((item = strtok_r(cfg, ":", &save_ptr))) {
142 if (strcmp(item, "help") == 0) {
143 show_help();
144 return VIDCAP_INIT_NOERR;
145 }
146 if (strcmp(item, "excl_init") == 0) {
147 s->excl_init = true;
148 } else if (strcmp(item, "fallback") == 0) {
149 s->fallback = true;
150 } else if (strncasecmp(item, "select=", strlen("select=")) ==
151 0) {
152 char *val_s = item + strlen("select=");
153 char *endptr = NULL;
154 errno = 0;
155 long val = strtol(val_s, &endptr, 0);
156 if (errno != 0 || *val_s == '\0' || *endptr != '\0' ||
157 val < 0 || (uintmax_t) val > UINT_MAX) {
159 MOD_NAME "Wrong value: %s\n", val_s);
160 return VIDCAP_INIT_FAIL;
161 }
162 s->selected_device = val;
163 } else {
164 MSG(ERROR, "Unknown initialization option: %s\n", item);
165 show_help();
166 return VIDCAP_INIT_FAIL;
167 }
168 cfg = NULL;
169 }
170 if (s->excl_init && s->fallback) {
171 MSG(ERROR, "Options \"excl_init\" and \"fallback\" are "
172 "mutualy incompatible!\n");
173 return VIDCAP_INIT_FAIL;
174 }
175
176 return 0;
177}
178
179static int
181{
182 verbose_msg("vidcap_switcher_init\n");
183
184 struct vidcap_switcher_state *s = calloc(1, sizeof *s);
185 if (s == NULL) {
186 MSG(ERROR, "Unable to allocate switcher capture state\n");
187 return VIDCAP_INIT_FAIL;
188 }
189
190 const char *cfg_c = vidcap_params_get_fmt(params);
191 if (cfg_c && strcmp(cfg_c, "") != 0) {
192 char *cfg = strdup(cfg_c);
193 const int rc = parse_fmt(s, cfg);
194 free(cfg);
195 if (rc != 0) {
196 free(s);
197 return rc;
198 }
199 }
200
204 s->devices_cnt = 0;
205 struct vidcap_params *tmp = params;
206 while((tmp = vidcap_params_get_next(tmp))) {
207 if (vidcap_params_get_driver(tmp) == NULL) {
208 break;
209 }
210 s->devices_cnt++;
211 }
212
213 if (s->selected_device >= s->devices_cnt) {
214 MSG(ERROR, "Error: device #%d not available!\n",
215 s->selected_device);
216 goto error;
217 }
218
219 s->devices = calloc(s->devices_cnt, sizeof(struct vidcap *));
220 s->device_names = calloc(s->devices_cnt, sizeof s->device_names[0]);
221 tmp = params;
222 for (unsigned int i = 0; i < s->devices_cnt; ++i) {
223 tmp = vidcap_params_get_next(tmp);
224
227 }
228
229 if (!s->excl_init || i == s->selected_device) {
230 int ret = initialize_video_capture(&s->mod, tmp, &s->devices[i]);
231 if(ret != 0) {
232 MSG(ERROR,
233 "Unable to initialize device %d (%s:%s).\n",
236 goto error;
237 }
238 }
239 snprintf(s->device_names[i], sizeof s->device_names[i],
240 "%s%s%s", vidcap_params_get_driver(tmp),
241 strlen(vidcap_params_get_fmt(tmp)) > 0 ? ":" : "",
243 }
244
245 s->params = params;
246
248
249 *state = s;
250 return VIDCAP_INIT_OK;
251
252error:
254 return VIDCAP_INIT_FAIL;
255}
256
257static void
259{
261 if (s == NULL) {
262 return;
263 }
264
265 if (s->devices != NULL) {
266 for (unsigned int i = 0U; i < s->devices_cnt; ++i) {
267 if (s->devices[i] != NULL) {
268 vidcap_done(s->devices[i]);
269 }
270 }
271 }
272 module_done(&s->mod);
273 free(s->devices);
274 free(s->device_names);
275 free(s);
276}
277
278static struct video_frame *
280{
282 struct audio_frame *audio_frame = NULL;
283 struct video_frame *frame = NULL;
284
285 struct message *msg;
286 while ((msg = check_message(&s->mod))) {
287 struct msg_universal *msg_univ = (struct msg_universal *) msg;
288 char *endptr = NULL;
289 errno = 0;
290 long val = strtol(msg_univ->text, &endptr, 0);
291 if (errno != 0 || val < 0 || (uintmax_t) val > UINT_MAX || msg_univ->text[0] == '\0' || *endptr != '\0') {
292 log_msg(LOG_LEVEL_ERROR, "[switcher] Cannot switch to device %s. Wrong value.\n", msg_univ->text);
294 continue;
295 }
296 unsigned int new_selected_device = val;
297 struct response *r;
298
299 if (new_selected_device < s->devices_cnt){
300 MSG(NOTICE,
301 "Switched from device %d to device %d (%s).\n",
302 s->selected_device + 1, new_selected_device + 1,
303 s->device_names[new_selected_device]);
304 if (s->excl_init) {
306 s->devices[s->selected_device] = NULL;
307 int ret = initialize_video_capture(&s->mod,
308 vidcap_params_get_nth((struct vidcap_params *) s->params, new_selected_device + 1),
309 &s->devices[new_selected_device]);
310 assert(ret == 0);
311 }
312
313 s->selected_device = new_selected_device;
314 r = new_response(RESPONSE_OK, NULL);
315 } else {
316 log_msg(LOG_LEVEL_ERROR, "[switcher] Cannot switch to device %u. Device out of bounds (total devices %u).\n",
317 new_selected_device + 1, s->devices_cnt);
319 }
320 free_message(msg, r);
321 }
322
325
326 if (frame || !s->fallback) {
327 return frame;
328 }
329 // if frame was not returned but we have a fallback behavior, try also other devices
330 for (unsigned int i = (s->selected_device + 1U) % s->devices_cnt;
331 i != s->selected_device;
332 i = (s->selected_device + 1U) % s->devices_cnt) {
335 if (frame != NULL) {
338 "Frame timed out, using fallback device #%d\n",
339 i);
340 break;
341 }
342 }
343 return frame;
344}
345
353
355
void ** state
int void * val
struct video_desc desc
struct video_frame * frame
struct audio_frame ** audio
int void(** deleter)(void *))
This file contains definition of some common audio types.
struct audio_frame audio_frame
struct used to comunicate with audio capture/playback device
int color_printf(const char *format,...)
#define TUNDERLINE(x)
Definition color_out.h:71
#define TRED(x)
Definition color_out.h:75
#define TBOLD(x)
Definition color_out.h:70
static void new_message(struct module *m)
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_INFO
normal reporting
Definition debug.h:62
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define MSG(l, fmt,...)
Definition debug.h:96
#define verbose_msg(...)
Definition debug.h:90
void module_init_default(struct module *module_data)
Definition module.c:48
void module_register(struct module *module_data, struct module *parent)
Definition module.c:82
void module_done(struct module *module_data)
Definition module.c:93
struct module * get_root_module(struct module *node)
Definition module.c:203
@ MODULE_CLASS_DATA
Definition module.h:86
#define VIDCAP_INIT_NOERR
state not initialized, other action performed (typically help)
#define VIDCAP_INIT_OK
initialization successful
struct video_frame * vidcap_grab(struct vidcap *state, struct audio_frame **audio)
Grabs video frame. This function may block for a short period if waiting for incoming frame....
int initialize_video_capture(struct module *parent, struct vidcap_params *param, struct vidcap **state)
Initializes video capture.
void vidcap_done(struct vidcap *state)
Destroys vidap state.
#define VIDEO_CAPTURE_ABI_VERSION
#define VIDCAP_INIT_FAIL
error ocured
int count
Definition hd-rum.c:44
static struct @19 params[100]
@ LIBRARY_CLASS_VIDEO_CAPTURE
Definition lib_common.h:78
#define REGISTER_MODULE(name, info, lclass, abi)
Registers module to global modules' registry.
Definition lib_common.h:144
#define MIN(a, b)
Definition macros.h:63
void free_message(struct message *msg, struct response *r)
struct response * new_response(int status, const char *text)
struct response * send_message_sync(struct module *root, const char *const_path, struct message *msg, int timeout_ms, int flags)
Sends message without waiting for response.
int response_get_status(struct response *r)
struct message * check_message(struct module *mod)
void free_response(struct response *r)
#define SEND_MESSAGE_FLAG_QUIET
do not print error messages on console (recv not found/queue full)
Definition messaging.h:171
#define RESPONSE_OK
Definition messaging.h:57
#define SEND_MESSAGE_FLAG_NO_STORE
if receiver doesn't exist, doesn't store it and retur 404 instead
Definition messaging.h:172
#define RESPONSE_BAD_REQUEST
Definition messaging.h:60
struct msghdr msg
struct used to comunicate with audio capture/playback device
Definition types.h:99
Definition hd-rum.c:27
enum module_class cls
Definition module.h:115
struct message m
Definition messaging.h:156
char text[8192]
Definition messaging.h:157
struct module mod
Definition switcher.c:103
unsigned int selected_device
Definition switcher.c:108
unsigned int devices_cnt
Definition switcher.c:106
struct vidcap_params * params
Definition switcher.c:110
char(* device_names)[128]
Definition switcher.c:105
struct vidcap ** devices
Definition switcher.c:104
This struct represents video capture state.
Struct video_frame represents a video frame and contains video description.
Definition types.h:305
static void vidcap_switcher_probe(struct device_info **available_cards, int *count, void(**deleter)(void *))
Definition switcher.c:116
static const struct video_capture_info vidcap_switcher_info
Definition switcher.c:346
static int parse_fmt(struct vidcap_switcher_state *s, char *cfg)
Definition switcher.c:136
#define MOD_NAME
Definition switcher.c:56
static struct video_frame * vidcap_switcher_grab(void *state, struct audio_frame **audio)
Definition switcher.c:279
static void vidcap_switcher_done(void *state)
Definition switcher.c:258
static void show_help(void)
Definition switcher.c:62
static void vidcap_switcher_register_keyboard_ctl(struct vidcap_switcher_state *s)
Definition switcher.c:123
static int vidcap_switcher_init(struct vidcap_params *params, void **state)
Definition switcher.c:180
char * wrap_paragraph(char *text)
Definition text.c:204
This is an umbrella header for video functions.
struct module * vidcap_params_get_parent(const struct vidcap_params *params)
struct vidcap_params * vidcap_params_get_next(const struct vidcap_params *curr)
const char * vidcap_params_get_driver(const struct vidcap_params *params)
void vidcap_params_set_flags(struct vidcap_params *params, unsigned int flags)
const char * vidcap_params_get_fmt(const struct vidcap_params *params)
unsigned int vidcap_params_get_flags(const struct vidcap_params *params)
struct vidcap_params * vidcap_params_get_nth(struct vidcap_params *curr, int index)