UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
audio_playback.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2015-2021 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 <stdio.h>
45#include <string.h>
46#include <stdlib.h>
47
49#include "audio/playback/sdi.h"
50#include "audio/types.h"
51#include "debug.h"
52#include "host.h"
53#include "lib_common.h"
54#include "tv.h"
55#include "video_display.h" /* flags */
56
58 char name[128];
60 void *state;
61
62 struct timeval t0;
63 long long int samples_played;
64};
65
66void audio_playback_help(bool full)
67{
68 printf("Available audio playback devices:\n");
70}
71
72int audio_playback_init(const char *device, const char *cfg, struct state_audio_playback **state)
73{
74 struct state_audio_playback *s;
75
76 s = calloc(1, sizeof(struct state_audio_playback));
77 gettimeofday(&s->t0, NULL);
79
80 if (s->funcs == NULL) {
81 log_msg(LOG_LEVEL_ERROR, "Unknown audio playback driver: %s\n", device);
82 goto error;
83 }
84
85 strncpy(s->name, device, sizeof s->name - 1);
86 s->state = s->funcs->init(cfg);
87
88 if(!s->state) {
89 log_msg(LOG_LEVEL_ERROR, "Error initializing audio playback.\n");
90 goto error;
91 }
92
93 if (s->state == INIT_NOERR) {
94 free(s);
95 return 1;
96 }
97
98 *state = s;
99 return 0;
100
101error:
102 free(s);
103 return -1;
104}
105
107{
108 struct state_audio_playback *device = NULL;
109 int ret = audio_playback_init("none", "", &device);
110 if (ret != 0) {
111 log_msg(LOG_LEVEL_ERROR, "Unable to initialize null audio playback: %d\n", ret);
112 }
113
114 return device;
115}
116
118{
119 if (!s) {
120 return;
121 }
122
123 if (s->samples_played > 0) {
124 struct timeval t1;
125 gettimeofday(&t1, NULL);
126
127 log_msg(LOG_LEVEL_INFO, "Played %lld audio samples in %f seconds (%f samples per second).\n",
128 s->samples_played, tv_diff(t1, s->t0),
129 s->samples_played / tv_diff(t1, s->t0));
130 }
131
132 s->funcs->done(s->state);
133 free(s);
134}
135
137{
138 if(!s)
139 return 0u;
140
141 if (strcasecmp(s->name, "embedded") == 0) {
143 } else if (strcasecmp(s->name, "AESEBU") == 0) {
145 } else if (strcasecmp(s->name, "analog") == 0) {
147 } else {
148 return 0u;
149 }
150}
151
153{
154 s->samples_played += frame->data_len / frame->ch_count / frame->bps;
155 s->funcs->write(s->state, frame);
156}
157
158bool audio_playback_ctl(struct state_audio_playback *s, int request, void *data, size_t *len)
159{
160 return s->funcs->ctl(s->state, request, data, len);
161}
162
164 int sample_rate)
165{
166 return s->funcs->reconfigure(s->state, (struct audio_desc){quant_samples / 8, sample_rate, channels, AC_PCM});
167}
168
170{
171 return s->state;
172}
173
174/* vim: set expandtab sw=8: */
175
void ** state
int int int sample_rate
int int channels
struct video_frame * frame
int void size_t * len
int quant_samples
This file contains definition of some common audio types.
bool audio_playback_ctl(struct state_audio_playback *s, int request, void *data, size_t *len)
void * audio_playback_get_state_pointer(struct state_audio_playback *s)
unsigned int audio_playback_get_display_flags(struct state_audio_playback *s)
bool audio_playback_reconfigure(struct state_audio_playback *s, int quant_samples, int channels, int sample_rate)
int audio_playback_init(const char *device, const char *cfg, struct state_audio_playback **state)
struct state_audio_playback * audio_playback_init_null_device(void)
void audio_playback_put_frame(struct state_audio_playback *s, const struct audio_frame *frame)
void audio_playback_help(bool full)
void audio_playback_done(struct state_audio_playback *s)
#define AUDIO_PLAYBACK_ABI_VERSION
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 DISPLAY_FLAG_AUDIO_ANALOG
#define DISPLAY_FLAG_AUDIO_AESEBU
#define DISPLAY_FLAG_AUDIO_EMBEDDED
#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_PLAYBACK
Definition lib_common.h:77
struct used to comunicate with audio capture/playback device
Definition types.h:99
bool(* reconfigure)(void *state, struct audio_desc)
void *(* init)(const char *cfg)
void(* done)(void *state)
void(* write)(void *state, const struct audio_frame *frame)
bool(* ctl)(void *state, int request, void *data, size_t *len)
const struct audio_playback_info * funcs
long long int samples_played
double tv_diff(struct timeval curr_time, struct timeval prev_time)
Definition tv.c:97