83using std::chrono::duration_cast;
84using std::chrono::seconds;
85using std::chrono::steady_clock;
89using std::ostringstream;
91using std::setprecision;
96#define AUDIO_DECODER_MAGIC 0x12ab332bu
97#define MOD_NAME "[audio dec.] "
112 steady_clock::time_point
t_last = steady_clock::now();
117 <<
" (cumulative): " <<
SBOLD(
played) <<
" played / "
127 if (
last_bufnum !=
static_cast<unsigned long int>(-1)) {
140 auto now = steady_clock::now();
159 vector<struct scale_data>
scale = vector<scale_data>(1);
211 "Audio scale adjusted to: %f (average volume was %f)\n",
236 " Resample to specified sampling rate, eg. 12288128/256 for 48000.5 Hz\n");
241 bool scale_auto =
false;
242 double scale_factor = 1.0;
245 assert(audio_scale != NULL);
265 assert(strchr(
val,
'/') !=
nullptr);
269 gettimeofday(&s->
t0, NULL);
276 if (strlen(encryption) > 0) {
281 "without OpenSSL support!\n");
292 if(audio_channel_map) {
304 if(strcasecmp(audio_scale,
"mixauto") == 0) {
311 }
else if(strcasecmp(audio_scale,
"auto") == 0) {
313 }
else if(strcasecmp(audio_scale,
"none") == 0) {
318 scale_factor = atof(audio_scale);
319 if(scale_factor <= 0.0) {
326 s->
scale.at(0).scale = scale_factor;
359 desc->ch_count = ((ntohl(hdr[0]) >> 22) & 0x3ff) + 1;
360 desc->sample_rate = ntohl(hdr[3]) & 0x3fffff;
361 desc->bps = (ntohl(hdr[3]) >> 26) / 8;
363 uint32_t audio_tag = ntohl(hdr[4]);
380 if (d->bytes_received < d->bytes_expected) {
381 loss =
" (" + to_string(d->bytes_expected - d->bytes_received) +
" lost)";
384 "decoded %d samples in %.2f sec.\n",
388 d->frame.get_sample_count(),
392 char *vol_start = volume;
393 for (
int i = 0; i < d->frame.get_channel_count(); ++i) {
399 &vol_start, volume +
sizeof volume);
403 volume, d->muted_receiver ?
TBOLD(
TRED(
" (muted)")) :
"");
412 " Forces specified format playback format.\n");
426 log_msg(
LOG_LEVEL_NOTICE,
"New incoming audio format detected: %d Hz, %d channel%s, %d bits per sample, codec %s\n",
427 sample_rate, input_channels, input_channels == 1 ?
"":
"s", bps * 8,
443 size_t len =
sizeof device_desc;
454 decoder->
scale.clear();
456 decoder->
scale.resize(scale_count);
498 for (
auto & c : fec_data) {
501 if (decoder->
fec_state->
decode(c.first.data(), c.first.size(), &out, &out_len, c.second)) {
504 uint32_t quant_sample_rate = 0;
505 uint32_t audio_tag = 0;
507 memcpy(&quant_sample_rate, out + 3 *
sizeof(uint32_t),
sizeof(uint32_t));
508 memcpy(&audio_tag, out + 4 *
sizeof(uint32_t),
sizeof(uint32_t));
509 quant_sample_rate = ntohl(quant_sample_rate);
510 audio_tag = ntohl(audio_tag);
512 desc.bps = (quant_sample_rate >> 26) / 8;
513 desc.sample_rate = quant_sample_rate & 0x07FFFFFFU;
514 desc.ch_count = fec_data.size();
517 auto flags = std::clog.flags();
519 std::clog.flags(
flags);
539 int input_channels = 0;
548 if (!cdata->
data->m) {
561 vector<pair<vector<char>, map<int, int>>> fec_data;
562 uint32_t fec_params = 0;
564 while (cdata != NULL) {
567 uint32_t *audio_hdr = (uint32_t *)(
void *) cdata->
data->data;
568 const int pt = cdata->
data->pt;
574 "no decryption key entered!\n");
580 "while expecting encrypted.\n");
596 uint32_t encryption_hdr = ntohl(*(uint32_t *)(
void *) (cdata->
data->data + main_hdr_len));
597 crypto_mode = (
enum openssl_mode) (encryption_hdr >> 24);
604 int ciphertext_len = cdata->
data->data_len - main_hdr_len -
608 ciphertext, ciphertext_len,
610 plaintext, crypto_mode)) == 0) {
616 length = cdata->
data->data_len - main_hdr_len;
617 data = cdata->
data->data + main_hdr_len;
623 input_channels = ((ntohl(audio_hdr[0]) >> 22) & 0x3ff) + 1;
629 assert(input_channels > 0);
631 int channel = (ntohl(audio_hdr[0]) >> 22) & 0x3ff;
632 bufnum = ntohl(audio_hdr[0]) & ((1U<<
BUFNUM_BITS) - 1U);
634 unsigned int offset = ntohl(audio_hdr[1]);
635 unsigned int buffer_len = ntohl(audio_hdr[2]);
644 fec_data.resize(input_channels);
645 fec_data[channel].first.resize(buffer_len);
646 fec_params = ntohl(audio_hdr[3]);
647 fec_data[channel].second[
offset] = length;
648 memcpy(fec_data[channel].
first.data() +
offset, data, length);
650 int bps = (ntohl(audio_hdr[3]) >> 26) / 8;
651 uint32_t audio_tag = ntohl(audio_hdr[4]);
662 received_frame.
resize(channel, buffer_len);
677 if (fec_params != 0) {
692 if (resampler_bps <= 0) {
695 if (resampler_bps != decompressed.
get_bps()) {
725 if (!decoder->
muted) {
759 std::string report =
"ARECV";
761 for(
int i = 0; i < num_ch; i++){
764 double rms_dbfs0 = 20 * log(rms) / log(10);
765 double peak_dbfs0 = 20 * log(peak) / log(10);
766 report +=
" volrms" + std::to_string(i) +
" " + std::to_string(rms_dbfs0);
767 report +=
" volpeak" + std::to_string(i) +
" " + std::to_string(peak_dbfs0);
785 std::swap(d->frame, decoder->
decoded);
786 d->seconds = seconds;
789 d->muted_receiver = decoder->
muted;
828 if(!cdata)
return false;
837 while (cdata != NULL) {
839 char *from = cdata->
data->data;
841 received_frame.
append(0, from, cdata->
data->data_len);
853 while (cdata != NULL) {
860 char *from = cdata->
data->data;
867 received_frame.
append(ch, from, cdata->
data->data_len);
869 from += received_frame.
get_bps();
884 for (
auto & i : s->scale) {
887 s->muted =
val == 0.0;
const char unsigned int flags
struct audio_frame ** audio
This file contains definition of some common audio types.
void audio_decoder_destroy(void *state)
bool parse_audio_hdr(uint32_t *hdr, struct audio_desc *desc)
static bool audio_fec_decode(struct pbuf_audio_data *s, vector< pair< vector< char >, map< int, int > > > &fec_data, uint32_t fec_params, audio_frame2 &received_frame)
static void compute_scale(struct scale_data *scale_data, double vol_avg, int samples, int sample_rate)
#define AUDIO_DECODER_MAGIC
int decode_audio_frame_mulaw(struct coded_data *cdata, void *data, struct pbuf_stats *)
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)
int decode_audio_frame(struct coded_data *cdata, void *pbuf_data, struct pbuf_stats *)
static void * adec_compute_and_print_stats(void *arg)
void audio_decoder_set_volume(void *state, double val)
constexpr double VOL_DOWN
static bool audio_decoder_reconfigure(struct state_audio_decoder *decoder, struct pbuf_audio_data *s, audio_frame2 &received_frame, int input_channels, int bps, int sample_rate, uint32_t audio_tag)
static void audio_decoder_process_message(struct module *m)
#define ADEC_CH_RATE_SHIFT
#define MSG_UNIVERSAL_TAG_AUDIO_DECODER
bool(* audio_playback_ctl_t)(void *state, int request, void *data, size_t *len)
#define AUDIO_PLAYBACK_CTL_QUERY_FORMAT
void change_bps(int new_bps)
void resize(int channel, size_t len)
char * get_data(int channel)
void init(int nr_channels, audio_codec_t codec, int bps, int sample_rate)
Initializes audio_frame2 for use. If already initialized, data are dropped.
audio_codec_t get_codec() const
int get_channel_count() const
bool resample(audio_frame2_resampler &resampler_state, int new_sample_rate)
int64_t get_timestamp() const
void replace(int channel, size_t offset, const char *data, size_t length)
replaces portion of data of specified channel. If the size of the channel is not sufficient,...
std::tuple< bool, audio_frame2 > resample_fake(audio_frame2_resampler &resampler_state, int new_sample_rate_num, int new_sample_rate_den)
@ resamples to new sample rate while keeping nominal sample rate intact
void set_timestamp(int64_t ts)
size_t get_data_len(int channel) const
int get_sample_rate() const
void append(audio_frame2 const &frame)
audio_codec_t get_audio_codec_to_tag(uint32_t tag)
audio_frame2 audio_codec_decompress(struct audio_codec_state *s, audio_frame2 *frame)
const char * get_name_to_audio_codec(audio_codec_t codec)
struct audio_codec_state * audio_codec_reconfigure(struct audio_codec_state *old, audio_codec_t audio_codec, audio_codec_direction_t direction)
void audio_codec_done(struct audio_codec_state *s)
bool control_stats_enabled(struct control_state *s)
int control_audio_ch_report_count(struct control_state *state)
void control_report_stats(struct control_state *s, const std::string &report_line)
static __device__ const float offset
void log_msg(int level, const char *format,...)
void log_msg_once(int level, uint32_t id, const char *msg,...)
#define DEBUG_TIMER_START(name)
#define LOG_LEVEL_NOTICE
information that may be interesting
#define LOG_LEVEL_INFO
normal reporting
#define DEBUG_TIMER_STOP(name)
#define LOG_LEVEL_ERROR
general errors
#define LOG_LEVEL_VERBOSE
#define LOG_LEVEL_FATAL
errors that prevent UG run
#define CUMULATIVE_REPORTS_INTERVAL
#define LOG_LEVEL_WARNING
less severe errors
void module_init_default(struct module *module_data)
void module_register(struct module *module_data, struct module *parent)
struct module * get_module(struct module *root, const char *const_path)
void module_done(struct module *module_data)
struct module * get_root_module(struct module *node)
const char * get_commandline_param(const char *key)
#define ADD_TO_PARAM(param, doc)
const void * load_library(const char *name, enum library_class cls, int abi_version)
@ LIBRARY_CLASS_UNDEFINED
#define to_fourcc(a, b, c, d)
Creates FourCC word.
@ STR_LEN
"standard" string length placeholder
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_NOT_FOUND
#define OPENSSL_DECRYPT_ABI_VERSION
openssl_mode
only GCM provides autenticity, other modes only check integrity (CRC)
int packet_counter_get_all_bytes(struct packet_counter *state)
int packet_counter_get_channels(struct packet_counter *state)
void packet_counter_clear(struct packet_counter *state)
int packet_counter_get_total_bytes(struct packet_counter *state)
void packet_counter_register_packet(struct packet_counter *state, unsigned int substream_id, unsigned int bufnum, unsigned int offset, unsigned int len)
struct packet_counter * packet_counter_init(int num_substreams)
void packet_counter_destroy(struct packet_counter *state)
#define RTP_MAX_PACKET_LEN
#define PT_AUDIO_IS_ENCRYPTED(pt)
uint32_t audio_payload_hdr_t[5]
uint32_t fec_payload_hdr_t[5]
#define PT_AUDIO_HAS_FEC(pt)
RFC 2550 hdr for audio - 16b MBZ, 16b Frag_off.
#define PT_Unassign_Type95
uint32_t crypto_payload_hdr_t[1]
static fec * create_from_desc(struct fec_desc) noexcept
virtual bool decode(char *in, int in_len, char **out, int *out_len, const std::map< int, int > &)=0
notify_t new_message
if set, notifies module that new message is in queue, receiver lock is hold during the call
int(* init)(struct openssl_decrypt **state, const char *passphrase)
void(* destroy)(struct openssl_decrypt *state)
int(* decrypt)(struct openssl_decrypt *decrypt, const char *ciphertext, int ciphertext_len, const char *aad, int aad_len, char *plaintext, enum openssl_mode mode)
struct sockaddr_storage source
size_t frame_size
currently decoded audio frame size (used similarly as vcodec_state::max_frame_size to allow caller ad...
struct state_audio_decoder * decoder
~state_audio_decoder_summary()
steady_clock::time_point t_last
void update(unsigned long int bufnum)
unsigned long int last_bufnum
vector< struct scale_data > scale
contains scaling metadata if we want to perform audio scaling
std::atomic_uint64_t req_resample_to
struct control_state * control
struct audio_codec_state * audio_decompress
struct channel_map channel_map
audio_playback_ctl_t audio_playback_ctl_func
audio_frame2 resample_remainder
unsigned int channel_remapping
audio_frame2 decoded
buffer that keeps audio samples from last 5 seconds (for statistics)
void * audio_playback_state
struct packet_counter * packet_counter
struct openssl_decrypt * decrypt
struct state_audio_decoder_summary summary
const struct openssl_decrypt_info * dec_funcs
audio_frame2_resampler resampler
struct audio_desc saved_desc
static struct @51 loss[MAX_HISTORY]
double tv_diff(struct timeval curr_time, struct timeval prev_time)
This file contains some common data types.
void mux_and_mix_channel(char *out, const char *in, int bps, int in_len, int out_stream_channels, int pos_in_stream, double scale)
bool parse_channel_map_cfg(struct channel_map *channel_map, const char *cfg)
double get_avg_volume(char *data, int bps, int sample_count, int stream_channels, int pos_in_stream)
double calculate_rms(audio_frame2 *frame, int channel, double *peak)
Calculates mean and peak RMS from audio samples.
void format_audio_channel_volume(int chan_idx, double rms, double peak, const char *format_color, char **volume_start, char *volume_end)
format per channel incrementally volume string (eg. "[0]-18.00/-14.99, [1] -18.00/-14....
int parse_audio_format(const char *str, struct audio_desc *ret)
void task_run_async_detached(runnable_t task, void *data)
Runs task asynchronously in a detached state.