UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
switcher.c File Reference
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "audio/types.h"
#include "debug.h"
#include "lib_common.h"
#include "module.h"
#include "utils/color_out.h"
#include "utils/macros.h"
#include "utils/text.h"
#include "video.h"
#include "video_capture.h"
Include dependency graph for switcher.c:

Go to the source code of this file.

Classes

struct  vidcap_switcher_state
 

Macros

#define MOD_NAME   "[switcher] "
 

Functions

static int parse_fmt (struct vidcap_switcher_state *s, char *cfg)
 
 REGISTER_MODULE (switcher, &vidcap_switcher_info, LIBRARY_CLASS_VIDEO_CAPTURE, VIDEO_CAPTURE_ABI_VERSION)
 
static void show_help (void)
 
static void vidcap_switcher_done (void *state)
 
static struct video_frame * vidcap_switcher_grab (void *state, struct audio_frame **audio)
 
static int vidcap_switcher_init (struct vidcap_params *params, void **state)
 
static void vidcap_switcher_probe (struct device_info **available_cards, int *count, void(**deleter)(void *))
 
static void vidcap_switcher_register_keyboard_ctl (struct vidcap_switcher_state *s)
 

Variables

static const struct video_capture_info vidcap_switcher_info
 

Detailed Description

Author
Martin Pulec pulec.nosp@m.@ces.nosp@m.net.c.nosp@m.z

Definition in file switcher.c.

Macro Definition Documentation

◆ MOD_NAME

#define MOD_NAME   "[switcher] "

Definition at line 56 of file switcher.c.

Referenced by parse_fmt(), and vidcap_switcher_grab().

Function Documentation

◆ parse_fmt()

static int parse_fmt ( struct vidcap_switcher_state * s,
char * cfg )
static

Definition at line 136 of file switcher.c.

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}
int void * val
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define MSG(l, fmt,...)
Definition debug.h:96
#define VIDCAP_INIT_NOERR
state not initialized, other action performed (typically help)
#define VIDCAP_INIT_FAIL
error ocured
Definition hd-rum.c:27
unsigned int selected_device
Definition switcher.c:108
#define MOD_NAME
Definition switcher.c:56
static void show_help(void)
Definition switcher.c:62

References vidcap_switcher_state::excl_init, vidcap_switcher_state::fallback, LOG_LEVEL_ERROR, log_msg(), MOD_NAME, MSG, vidcap_switcher_state::selected_device, show_help(), val, VIDCAP_INIT_FAIL, and VIDCAP_INIT_NOERR.

Referenced by vidcap_switcher_init().

Here is the call graph for this function:

◆ REGISTER_MODULE()

REGISTER_MODULE ( switcher ,
& vidcap_switcher_info,
LIBRARY_CLASS_VIDEO_CAPTURE ,
VIDEO_CAPTURE_ABI_VERSION  )

◆ show_help()

static void show_help ( void )
static

Definition at line 62 of file switcher.c.

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}
struct video_desc desc
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
char * wrap_paragraph(char *text)
Definition text.c:204

References color_printf(), desc, TBOLD, TRED, TUNDERLINE, and wrap_paragraph().

Referenced by parse_fmt().

Here is the call graph for this function:

◆ vidcap_switcher_done()

static void vidcap_switcher_done ( void * state)
static

Definition at line 258 of file switcher.c.

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}
void ** state
void module_done(struct module *module_data)
Definition module.c:93
void vidcap_done(struct vidcap *state)
Destroys vidap state.
struct module mod
Definition switcher.c:103
unsigned int devices_cnt
Definition switcher.c:106
char(* device_names)[128]
Definition switcher.c:105
struct vidcap ** devices
Definition switcher.c:104

References vidcap_switcher_state::device_names, vidcap_switcher_state::devices, vidcap_switcher_state::devices_cnt, vidcap_switcher_state::mod, module_done(), state, and vidcap_done().

Referenced by vidcap_switcher_init().

Here is the call graph for this function:

◆ vidcap_switcher_grab()

static struct video_frame * vidcap_switcher_grab ( void * state,
struct audio_frame ** audio )
static

Definition at line 279 of file switcher.c.

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}
struct video_frame * frame
struct audio_frame ** audio
struct audio_frame audio_frame
struct used to comunicate with audio capture/playback device
#define LOG_LEVEL_INFO
normal reporting
Definition debug.h:62
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 free_message(struct message *msg, struct response *r)
struct response * new_response(int status, const char *text)
struct message * check_message(struct module *mod)
#define RESPONSE_OK
Definition messaging.h:57
#define RESPONSE_BAD_REQUEST
Definition messaging.h:60
struct msghdr msg
struct used to comunicate with audio capture/playback device
Definition types.h:99
char text[8192]
Definition messaging.h:157
struct vidcap_params * params
Definition switcher.c:110
Struct video_frame represents a video frame and contains video description.
Definition types.h:305
struct vidcap_params * vidcap_params_get_nth(struct vidcap_params *curr, int index)

References audio, check_message(), vidcap_switcher_state::device_names, vidcap_switcher_state::devices, vidcap_switcher_state::devices_cnt, vidcap_switcher_state::excl_init, vidcap_switcher_state::fallback, frame, free_message(), initialize_video_capture(), LOG_LEVEL_ERROR, LOG_LEVEL_INFO, log_msg(), vidcap_switcher_state::mod, MOD_NAME, MSG, msg, new_response(), vidcap_switcher_state::params, RESPONSE_BAD_REQUEST, RESPONSE_OK, vidcap_switcher_state::selected_device, state, msg_universal::text, val, vidcap_done(), vidcap_grab(), and vidcap_params_get_nth().

Here is the call graph for this function:

◆ vidcap_switcher_init()

static int vidcap_switcher_init ( struct vidcap_params * params,
void ** state )
static

Definition at line 180 of file switcher.c.

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}
#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
@ MODULE_CLASS_DATA
Definition module.h:86
#define VIDCAP_INIT_OK
initialization successful
static struct @19 params[100]
enum module_class cls
Definition module.h:115
This struct represents video capture state.
static int parse_fmt(struct vidcap_switcher_state *s, char *cfg)
Definition switcher.c:136
static void vidcap_switcher_done(void *state)
Definition switcher.c:258
static void vidcap_switcher_register_keyboard_ctl(struct vidcap_switcher_state *s)
Definition switcher.c:123
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)

References module::cls, vidcap_switcher_state::device_names, vidcap_switcher_state::devices, vidcap_switcher_state::devices_cnt, vidcap_switcher_state::excl_init, initialize_video_capture(), vidcap_switcher_state::mod, MODULE_CLASS_DATA, module_init_default(), module_register(), MSG, params, vidcap_switcher_state::params, parse_fmt(), vidcap_switcher_state::selected_device, state, verbose_msg, VIDCAP_INIT_FAIL, VIDCAP_INIT_OK, vidcap_params_get_driver(), vidcap_params_get_flags(), vidcap_params_get_fmt(), vidcap_params_get_next(), vidcap_params_get_parent(), vidcap_params_set_flags(), vidcap_switcher_done(), and vidcap_switcher_register_keyboard_ctl().

Here is the call graph for this function:

◆ vidcap_switcher_probe()

static void vidcap_switcher_probe ( struct device_info ** available_cards,
int * count,
void(** deleter )(void *) )
static

Definition at line 116 of file switcher.c.

117{
118 *deleter = free;
119 *available_cards = NULL;
120 *count = 0;
121}
int void(** deleter)(void *))
int count
Definition hd-rum.c:44

References count, and deleter.

◆ vidcap_switcher_register_keyboard_ctl()

static void vidcap_switcher_register_keyboard_ctl ( struct vidcap_switcher_state * s)
static

Definition at line 123 of file switcher.c.

123 {
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}
static void new_message(struct module *m)
struct module * get_root_module(struct module *node)
Definition module.c:203
#define MIN(a, b)
Definition macros.h:63
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)
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 SEND_MESSAGE_FLAG_NO_STORE
if receiver doesn't exist, doesn't store it and retur 404 instead
Definition messaging.h:172
struct message m
Definition messaging.h:156

References vidcap_switcher_state::devices_cnt, free_response(), get_root_module(), LOG_LEVEL_ERROR, log_msg(), msg_universal::m, MIN, vidcap_switcher_state::mod, new_message(), response_get_status(), RESPONSE_OK, SEND_MESSAGE_FLAG_NO_STORE, SEND_MESSAGE_FLAG_QUIET, and send_message_sync().

Referenced by vidcap_switcher_init().

Here is the call graph for this function:

Variable Documentation

◆ vidcap_switcher_info

const struct video_capture_info vidcap_switcher_info
static
Initial value:
= {
}
static void vidcap_switcher_probe(struct device_info **available_cards, int *count, void(**deleter)(void *))
Definition switcher.c:116
static struct video_frame * vidcap_switcher_grab(void *state, struct audio_frame **audio)
Definition switcher.c:279
static int vidcap_switcher_init(struct vidcap_params *params, void **state)
Definition switcher.c:180

Definition at line 346 of file switcher.c.