UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
audio_capture.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2015-2023 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#ifdef HAVE_CONFIG_H
39#include "config.h"
40#include "config_unix.h"
41#include "config_win32.h"
42#endif
43
44#include <assert.h>
45#include <stdio.h>
46#include <string.h>
47#include <stdlib.h>
48
49#include "debug.h"
50#include "host.h"
51
52#include "audio/audio_capture.h"
53#include "audio/capture/sdi.h"
54#include "audio/types.h"
55
56/* vidcap flags */
57#include "video_capture.h"
58
59#include "lib_common.h"
60
62 char name[128];
64 void *state;
65};
66
67int audio_capture_init(struct module *parent, const char *driver, const char *cfg, struct state_audio_capture **state)
68{
69 assert(cfg != NULL);
70 struct state_audio_capture *s = calloc(1, sizeof(struct state_audio_capture));
71 assert(s != NULL);
72
74
75 if (s->funcs == NULL) {
76 log_msg(LOG_LEVEL_ERROR, "Unknown audio capture driver: %s\n", driver);
77 goto error;
78 }
79
80 strncpy(s->name, driver, sizeof s->name - 1);
81
82 s->state = s->funcs->init(parent, cfg);
83
84 if(!s->state) {
85 log_msg(LOG_LEVEL_ERROR, "Error initializing audio capture.\n");
86 goto error;
87 }
88
89 if(s->state == INIT_NOERR) {
90 free(s);
91 return 1;
92 }
93
94 *state = s;
95 return 0;
96
97error:
98 free(s);
99 return -1;
100}
101
103{
104 struct state_audio_capture *device = NULL;
105 int ret = audio_capture_init(NULL, "none", "", &device);
106 if (ret != 0) {
107 log_msg(LOG_LEVEL_ERROR, "Unable to initialize null audio capture: %d\n", ret);
108 }
109 return device;
110}
111
113{
114 printf("Available audio capture devices:\n");
116}
117
119{
120 if(s) {
121 s->funcs->done(s->state);
122
123 free(s);
124 }
125}
126
128{
129 if(s) {
130 return s->funcs->read(s->state);
131 } else {
132 return NULL;
133 }
134}
135
140unsigned int audio_capture_get_vidcap_flags(const char *const_device_name)
141{
142 char *tmp = strdup(const_device_name);
143 assert(tmp != NULL);
144 char *save_ptr = NULL;
145 char *device_name = strtok_r(tmp, ":", &save_ptr);
146 assert(device_name != NULL);
147 unsigned int ret;
148
149 if(strcasecmp(device_name, "embedded") == 0) {
151 } else if(strcasecmp(device_name, "AESEBU") == 0) {
153 } else if(strcasecmp(device_name, "analog") == 0) {
155 } else {
156 ret = 0u;
157 }
158
159 free(tmp);
160 return ret;
161}
162
168int audio_capture_get_vidcap_index(const char *const_device_name)
169{
170 char *tmp = strdup(const_device_name);
171 assert(tmp != NULL);
172 char *save_ptr = NULL;
173 unsigned int ret;
174
175 strtok_r(tmp, ":", &save_ptr);
176 char *vidcap_index_str = strtok_r(NULL, ":", &save_ptr);
177
178 if (vidcap_index_str == NULL) {
179 ret = -1;
180 } else {
181 ret = atoi(vidcap_index_str);
182 }
183
184 free(tmp);
185 return ret;
186}
187
189{
190 return s->name;
191}
192
194{
195 return s->state;
196}
197
void ** state
This file contains definition of some common audio types.
int audio_capture_init(struct module *parent, const char *driver, const char *cfg, struct state_audio_capture **state)
struct state_audio_capture * audio_capture_init_null_device()
unsigned int audio_capture_get_vidcap_flags(const char *const_device_name)
void audio_capture_done(struct state_audio_capture *s)
void * audio_capture_get_state_pointer(struct state_audio_capture *s)
int audio_capture_get_vidcap_index(const char *const_device_name)
const char * audio_capture_get_driver_name(struct state_audio_capture *s)
struct audio_frame * audio_capture_read(struct state_audio_capture *s)
void audio_capture_print_help(bool full)
#define AUDIO_CAPTURE_ABI_VERSION
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define INIT_NOERR
Definition host.h:73
void list_modules(enum library_class cls, int abi_version, bool full)
const void * load_library(const char *name, enum library_class cls, int abi_version)
@ LIBRARY_CLASS_AUDIO_CAPTURE
Definition lib_common.h:76
const char * parent
Definition settings.cpp:198
struct audio_frame *(* read)(void *state)
void *(* init)(struct module *parent, const char *cfg)
void(* done)(void *state)
struct used to comunicate with audio capture/playback device
Definition types.h:99
const struct audio_capture_info * funcs
#define VIDCAP_FLAG_AUDIO_AESEBU
AES/EBU audio.
#define VIDCAP_FLAG_AUDIO_ANALOG
(balanced) analog audio
#define VIDCAP_FLAG_AUDIO_EMBEDDED
HD-SDI embedded audio.