UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
controlport_stats.cpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2022 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 /* HAVE_CONFIG_H */
43
44#include <memory>
45#include <string_view>
46
47#include "debug.h"
48#include "module.h"
49#include "control_socket.h"
50#include "audio/audio_filter.h"
51#include "audio/types.h"
52#include "audio/utils.h"
53#include "lib_common.h"
54
70
71static void usage(){
72 printf("controlport_stats:\n");
73 printf("Reporting of audio volume via control port\n");
74}
75
76static af_result_code init(struct module *parent, const char *cfg, void **state){
77 auto s = std::make_unique<state_controlport_stats>(parent);
78
79 if(strcmp(cfg, "help") == 0){
80 usage();
81 return AF_HELP_SHOWN;
82 }
83
84 *state = s.release();
85 return AF_OK;
86};
87
89 int in_bps, int in_ch_count, int in_sample_rate)
90{
91 auto s = static_cast<state_controlport_stats *>(state);
92
93 s->bps = in_bps;
94 s->ch_count = in_ch_count;
95 s->sample_rate = in_sample_rate;
96
97 return AF_OK;
98}
99
100static void done(void *state){
101 auto s = static_cast<state_controlport_stats *>(state);
102
103 delete s;
104}
105
106static void get_configured(void *state,
107 int *bps, int *ch_count, int *sample_rate)
108{
109 auto s = static_cast<state_controlport_stats *>(state);
110
111 if(bps) *bps = s->bps;
112 if(ch_count) *ch_count = s->ch_count;
113 if(sample_rate) *sample_rate = s->sample_rate;
114}
115
117 auto s = static_cast<state_controlport_stats *>(state);
118
119 auto f = *frame;
120
121 if(f->bps != s->bps || f->ch_count != s->ch_count){
122 if(configure(state, f->bps, f->ch_count, f->sample_rate) != AF_OK){
123 return AF_MISCONFIGURED;
124 }
125 }
126
127 if(!control_stats_enabled(s->control))
128 return AF_OK;
129
130 std::string report = "ASEND";
131 for(int i = 0; i < f->ch_count; i++){
132 double rms, peak;
133 rms = calculate_rms(f, i, &peak);
134 double rms_dbfs0 = 20 * log(rms) / log(10);
135 double peak_dbfs0 = 20 * log(peak) / log(10);
136 report += " volrms" + std::to_string(i) + " " + std::to_string(rms_dbfs0);
137 report += " volpeak" + std::to_string(i) + " " + std::to_string(peak_dbfs0);
138 }
139 control_report_stats(s->control, report.c_str());
140
141 return AF_OK;
142}
143
145 .name = "controlport_stats",
146 .init = init,
147 .done = done,
148 .configure = configure,
149 .get_configured_in = get_configured,
150 .get_configured_out = get_configured,
151 .filter = filter,
152};
153
void ** state
int int int sample_rate
struct video_frame * frame
This file contains definition of some common audio types.
#define AUDIO_FILTER_ABI_VERSION
af_result_code
@ AF_MISCONFIGURED
@ AF_HELP_SHOWN
@ AF_OK
bool control_stats_enabled(struct control_state *s)
void control_report_stats(struct control_state *s, const std::string &report_line)
static const struct audio_filter_info audio_filter_controlport_stats
static af_result_code filter(void *state, struct audio_frame **frame)
static void usage()
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 configure(void *state, int in_bps, int in_ch_count, int in_sample_rate)
static void done(void *state)
struct module * get_module(struct module *root, const char *const_path)
Definition module.c:264
struct module * get_root_module(struct module *node)
Definition module.c:203
@ MODULE_CLASS_DATA
Definition module.h:86
@ LIBRARY_CLASS_AUDIO_FILTER
Definition lib_common.h:85
#define REGISTER_MODULE(name, info, lclass, abi)
Registers module to global modules' registry.
Definition lib_common.h:144
const char * parent
Definition settings.cpp:198
struct used to comunicate with audio capture/playback device
Definition types.h:99
state_controlport_stats(struct module *mod)
double calculate_rms(audio_frame2 *frame, int channel, double *peak)
Calculates mean and peak RMS from audio samples.
Definition utils.cpp:150