UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
delay.cpp File Reference
#include <memory>
#include <string_view>
#include "debug.h"
#include "module.h"
#include "audio/audio_filter.h"
#include "audio/types.h"
#include "lib_common.h"
#include "utils/ring_buffer.h"
#include "utils/misc.h"
#include "utils/string_view_utils.hpp"
Include dependency graph for delay.cpp:

Go to the source code of this file.

Classes

struct  state_delay
 

Functions

static af_result_code configure (void *state, int in_bps, int in_ch_count, int in_sample_rate)
 
static void done (void *state)
 
static af_result_code filter (void *state, struct audio_frame **frame)
 
static void get_configured (void *state, int *bps, int *ch_count, int *sample_rate)
 
static af_result_code init (struct module *parent, const char *cfg, void **state)
 
static af_result_code parse_cfg (state_delay *s, std::string_view cfg)
 
 REGISTER_MODULE (delay, &audio_filter_delay, LIBRARY_CLASS_AUDIO_FILTER, AUDIO_FILTER_ABI_VERSION)
 
static void set_delay_size (state_delay *s, int size)
 
static void usage ()
 

Variables

static const struct audio_filter_info audio_filter_delay
 

Detailed Description

Author
Martin Piatka piatk.nosp@m.a@ce.nosp@m.snet..nosp@m.cz

Definition in file delay.cpp.

Function Documentation

◆ configure()

static af_result_code configure ( void * state,
int in_bps,
int in_ch_count,
int in_sample_rate )
static

Definition at line 125 of file delay.cpp.

127{
128 auto s = static_cast<state_delay *>(state);
129
130 s->bps = in_bps;
131 s->ch_count = in_ch_count;
132 s->sample_rate = in_sample_rate;
133
134 if(!s->frame_mode){
135 int samples = (s->sample_rate / 1000) * s->req_delay;
136 set_delay_size(s, samples * s->ch_count * s->bps);
137 }
138
139 return AF_OK;
140}
void ** state
@ AF_OK
static void set_delay_size(state_delay *s, int size)
Definition delay.cpp:112

References AF_OK, state_delay::bps, set_delay_size(), and state.

Referenced by filter().

Here is the call graph for this function:

◆ done()

static void done ( void * state)
static

Definition at line 142 of file delay.cpp.

142 {
143 auto s = static_cast<state_delay *>(state);
144
145 delete s;
146}

References state.

◆ filter()

static af_result_code filter ( void * state,
struct audio_frame ** frame )
static

Definition at line 158 of file delay.cpp.

158 {
159 auto s = static_cast<state_delay *>(state);
160
161 struct message *msg;
162 while ((msg = check_message(s->mod.get()))) {
163 const char *text = ((msg_universal *) msg)->text;
164 if(parse_cfg(s, text) != AF_OK){
166 continue;
167 }
168
169 if(!s->frame_mode){
170 int samples = (s->sample_rate / 1000) * s->req_delay;
171 set_delay_size(s, samples * s->ch_count * s->bps);
172 }
173
175 }
176
177 auto f = *frame;
178 if(s->frame_mode){
179 set_delay_size(s, f->data_len * s->req_delay);
180 }
181
182 if(f->bps != s->bps || f->ch_count != s->ch_count){
183 if(configure(state, f->bps, f->ch_count, f->sample_rate) != AF_OK){
184 return AF_MISCONFIGURED;
185 }
186 }
187
188 //If ring is null, delay is 0
189 if(!s->ring)
190 return AF_OK;
191
192 if(f->data_len <= s->delay_size){
193 ring_buffer_write(s->ring.get(), f->data, f->data_len);
194 ring_buffer_read(s->ring.get(), f->data, f->data_len);
195 } else {
196 int excess_size = f->data_len - s->delay_size;
197
198 //Write the last delay_size bytes to buffer
199 ring_buffer_write(s->ring.get(), f->data + excess_size, s->delay_size);
200
201 //Move the beggining of frame to the end and prepend delay_size bytes from buffer
202 memmove(f->data + s->delay_size, f->data, excess_size);
203 ring_buffer_read(s->ring.get(), f->data, s->delay_size);
204 }
205
206 return AF_OK;
207}
struct video_frame * frame
@ AF_MISCONFIGURED
static af_result_code parse_cfg(state_delay *s, std::string_view cfg)
Definition delay.cpp:84
static af_result_code configure(void *state, int in_bps, int in_ch_count, int in_sample_rate)
Definition delay.cpp:125
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
int ring_buffer_read(struct ring_buffer *ring, char *out, int max_len)
void ring_buffer_write(struct ring_buffer *ring, const char *in, int len)
struct msghdr msg

References AF_MISCONFIGURED, AF_OK, check_message(), configure(), frame, free_message(), msg, new_response(), parse_cfg(), RESPONSE_BAD_REQUEST, RESPONSE_OK, ring_buffer_read(), ring_buffer_write(), set_delay_size(), and state.

Here is the call graph for this function:

◆ get_configured()

static void get_configured ( void * state,
int * bps,
int * ch_count,
int * sample_rate )
static

Definition at line 148 of file delay.cpp.

150{
151 auto s = static_cast<state_delay *>(state);
152
153 if(bps) *bps = s->bps;
154 if(ch_count) *ch_count = s->ch_count;
155 if(sample_rate) *sample_rate = s->sample_rate;
156}
int int int sample_rate

References state_delay::bps, sample_rate, and state.

◆ init()

static af_result_code init ( struct module * parent,
const char * cfg,
void ** state )
static

Definition at line 102 of file delay.cpp.

102 {
103 auto s = std::make_unique<state_delay>(parent);
104
105 auto ret = parse_cfg(s.get(), cfg);
106 if(ret == AF_OK)
107 *state = s.release();
108
109 return ret;
110};
const char * parent
Definition settings.cpp:198

References AF_OK, parent, parse_cfg(), and state.

Here is the call graph for this function:

◆ parse_cfg()

static af_result_code parse_cfg ( state_delay * s,
std::string_view cfg )
static

Definition at line 84 of file delay.cpp.

84 {
85 auto tok = tokenize(cfg, ':');
86 if(tok.empty() || tok == "help"){
87 usage();
88 return AF_HELP_SHOWN;
89 }
90
91 if(!parse_num(tok, s->req_delay)){
92 log_msg(LOG_LEVEL_ERROR, "Failed to parse delay time\n");
93 usage();
94 return AF_FAILURE;
95 }
96
97 s->frame_mode = (tokenize(cfg, ':') == "frames");
98
99 return AF_OK;
100}
@ AF_FAILURE
@ AF_HELP_SHOWN
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
static void usage()
Definition delay.cpp:78
std::string_view tokenize(std::string_view &str, char delim, char quot)
Tokenizer for string_view.
bool parse_num(std::string_view sv, T &res, int base=10)
int req_delay
Definition delay.cpp:68
bool frame_mode
Definition delay.cpp:67

References AF_FAILURE, AF_HELP_SHOWN, AF_OK, state_delay::frame_mode, LOG_LEVEL_ERROR, log_msg(), parse_num(), state_delay::req_delay, tokenize(), and usage().

Referenced by filter(), and init().

Here is the call graph for this function:

◆ REGISTER_MODULE()

REGISTER_MODULE ( delay ,
& audio_filter_delay,
LIBRARY_CLASS_AUDIO_FILTER ,
AUDIO_FILTER_ABI_VERSION  )

◆ set_delay_size()

static void set_delay_size ( state_delay * s,
int size )
static

Definition at line 112 of file delay.cpp.

112 {
113 if(size == s->delay_size)
114 return;
115
116 if(size == 0){
117 s->ring.reset();
118 } else {
119 s->ring.reset(ring_buffer_init(size * 2));
120 ring_fill(s->ring.get(), 0, size);
121 }
122 s->delay_size = size;
123}
struct ring_buffer * ring_buffer_init(int size)
void ring_fill(struct ring_buffer *ring, int c, int size)
int delay_size
Definition delay.cpp:74
std::unique_ptr< ring_buffer_t, Ring_buf_deleter > ring
Definition delay.cpp:75

References state_delay::delay_size, state_delay::ring, ring_buffer_init(), and ring_fill().

Referenced by configure(), and filter().

Here is the call graph for this function:

◆ usage()

static void usage ( void )
static

Definition at line 78 of file delay.cpp.

78 {
79 printf("Delays audio:\n\n");
80 printf("delay usage:\n");
81 printf("\tdelay:<delay in milliseconds>\n\n");
82}

Referenced by parse_cfg().

Variable Documentation

◆ audio_filter_delay

const struct audio_filter_info audio_filter_delay
static
Initial value:
= {
.name = "delay",
.init = init,
.done = done,
.configure = configure,
.get_configured_in = get_configured,
.get_configured_out = get_configured,
.filter = filter,
}
static af_result_code filter(void *state, struct audio_frame **frame)
Definition delay.cpp:158
static void get_configured(void *state, int *bps, int *ch_count, int *sample_rate)
Definition delay.cpp:148
static af_result_code init(struct module *parent, const char *cfg, void **state)
Definition delay.cpp:102
static void done(void *state)
Definition delay.cpp:142

Definition at line 209 of file delay.cpp.

209 {
210 .name = "delay",
211 .init = init,
212 .done = done,
213 .configure = configure,
214 .get_configured_in = get_configured,
215 .get_configured_out = get_configured,
216 .filter = filter,
217};