From d30fb765a81fba6ed5aa5006029d0a171ae3ab0d Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Thu, 15 Mar 2018 15:02:46 +0100 Subject: [PATCH] Control: report stats for audio --- src/audio/audio.cpp | 3 ++- src/control_socket.cpp | 5 +++++ src/control_socket.h | 1 + src/rtp/audio_decoders.cpp | 35 +++++++++++++++++++++++++++++++---- src/rtp/audio_decoders.h | 4 +++- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 4603485ec..8746b840e 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -69,6 +69,7 @@ #include "audio/jack.h" #include "audio/utils.h" #include "compat/platform_semaphore.h" +#include "compat/platform_semaphore.h" #include "debug.h" #include "../export.h" // not audio/export.h #include "host.h" @@ -669,7 +670,7 @@ static void *audio_receiver_thread(void *arg) assert(dec_state != NULL); cp->decoder_state = dec_state; dec_state->enabled = true; - dec_state->pbuf_data.decoder = (struct state_audio_decoder *) audio_decoder_init(s->audio_channel_map, s->audio_scale, s->requested_encryption, (audio_playback_ctl_t) audio_playback_ctl, s->audio_playback_device); + dec_state->pbuf_data.decoder = (struct state_audio_decoder *) audio_decoder_init(s->audio_channel_map, s->audio_scale, s->requested_encryption, (audio_playback_ctl_t) audio_playback_ctl, s->audio_playback_device, &s->audio_receiver_module); audio_decoder_set_volume(dec_state->pbuf_data.decoder, s->muted ? 0.0 : s->volume); assert(dec_state->pbuf_data.decoder != NULL); cp->decoder_state_deleter = audio_decoder_state_deleter; diff --git a/src/control_socket.cpp b/src/control_socket.cpp index 25bc2fdee..705d242fc 100644 --- a/src/control_socket.cpp +++ b/src/control_socket.cpp @@ -899,3 +899,8 @@ void control_report_event(struct control_state *s, const std::string &report_lin control_report_stats_event(s, "event " + report_line + "\r\n"); } +bool control_stats_enabled(struct control_state *s) +{ + return s && s->stats_on; +} + diff --git a/src/control_socket.h b/src/control_socket.h index d2edad2d2..49762dfd7 100644 --- a/src/control_socket.h +++ b/src/control_socket.h @@ -62,6 +62,7 @@ void control_start(struct control_state *state); void control_done(struct control_state *s); void control_report_stats(struct control_state *state, const std::string & stat_line); void control_report_event(struct control_state *state, const std::string & event_line); +bool control_stats_enabled(struct control_state *state); #endif // control_socket_h_ diff --git a/src/rtp/audio_decoders.cpp b/src/rtp/audio_decoders.cpp index 16b993add..89b6d942b 100644 --- a/src/rtp/audio_decoders.cpp +++ b/src/rtp/audio_decoders.cpp @@ -43,9 +43,11 @@ #include "config_win32.h" #endif // HAVE_CONFIG_H +#include "control_socket.h" #include "debug.h" #include "host.h" #include "lib_common.h" +#include "module.h" #include "perf.h" #include "tv.h" #include "rtp/rtp.h" @@ -64,9 +66,12 @@ #include "utils/worker.h" #include -#include -#include -#include +#include +#include +#include +#include + +using std::ostringstream; #define AUDIO_DECODER_MAGIC 0x12ab332bu @@ -113,6 +118,8 @@ struct state_audio_decoder { audio_playback_ctl_t audio_playback_ctl_func; void *audio_playback_state; + + struct control_state *control; }; static int validate_mapping(struct channel_map *map); @@ -166,7 +173,7 @@ static void compute_scale(struct scale_data *scale_data, float vol_avg, int samp } } -void *audio_decoder_init(char *audio_channel_map, const char *audio_scale, const char *encryption, audio_playback_ctl_t c, void *p_state) +void *audio_decoder_init(char *audio_channel_map, const char *audio_scale, const char *encryption, audio_playback_ctl_t c, void *p_state, struct module *parent) { struct state_audio_decoder *s; bool scale_auto = false; @@ -185,6 +192,8 @@ void *audio_decoder_init(char *audio_channel_map, const char *audio_scale, const s->audio_decompress = NULL; + s->control = (struct control_state *) get_module(get_root_module(parent), "control"); + if (encryption) { s->dec_funcs = static_cast(load_library("openssl_decrypt", LIBRARY_CLASS_UNDEFINED, OPENSSL_DECRYPT_ABI_VERSION)); @@ -599,6 +608,24 @@ int decode_audio_frame(struct coded_data *cdata, void *pbuf_data, struct pbuf_st decoder->decoded.append(decompressed); + if (control_stats_enabled(decoder->control)) { + double rms, peak; + rms = calculate_rms(&decompressed, 0, &peak); + double rms_dbfs0 = 20 * log(rms) / log(10); + double peak_dbfs0 = 20 * log(peak) / log(10); + double rms_dbfs1; + double peak_dbfs1; + if (decompressed.get_channel_count() == 1) { + rms_dbfs1 = rms_dbfs0; + peak_dbfs1 = peak_dbfs0; + } else { + rms = calculate_rms(&decompressed, 1, &peak); + rms_dbfs1 = 20 * log(rms) / log(10); + peak_dbfs1 = 20 * log(peak) / log(10); + } + control_report_stats(decoder->control, static_cast(ostringstream() << "ARECV volrms0 " << rms_dbfs0 << " volpeak0 " << peak_dbfs0 << " volrms1 " << rms_dbfs1 << " volpeak1 " << peak_dbfs1).str()); + } + double seconds; struct timeval t; diff --git a/src/rtp/audio_decoders.h b/src/rtp/audio_decoders.h index b33615488..689933579 100644 --- a/src/rtp/audio_decoders.h +++ b/src/rtp/audio_decoders.h @@ -42,13 +42,15 @@ extern "C" { #endif struct coded_data; +struct module; typedef bool (*audio_playback_ctl_t)(void *state, int request, void *data, size_t *len); int decode_audio_frame(struct coded_data *cdata, void *data, struct pbuf_stats *stats); int decode_audio_frame_mulaw(struct coded_data *cdata, void *data, struct pbuf_stats *stats); void *audio_decoder_init(char *audio_channel_map, const char *audio_scale, - const char *encryption, audio_playback_ctl_t c, void *ap_state); + const char *encryption, audio_playback_ctl_t c, void *ap_state, + struct module *parent); void audio_decoder_destroy(void *state); void audio_decoder_set_volume(void *state, double val);