UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
audio_decoders.cpp
Go to the documentation of this file.
1
6/*
7 * Copyright (c) 2014 Fundació i2CAT, Internet I Innovació Digital a Catalunya
8 * Copyright (c) 2012-2024 CESNET
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, is permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * 3. Neither the name of CESNET nor the names of its contributors may be
23 * used to endorse or promote products derived from this software without
24 * specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
28 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
30 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
37 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
41
42#include <algorithm> // for min
43#include <atomic> // for atomic_uint64_t
44#include <cassert> // for assert
45#include <chrono> // for steady_clock, duration_cast, ope...
46#include <cmath> // for log
47#include <cstdlib> // for strtoll, atof, free, realloc
48#include <cstring> // for memcpy, strchr, strlen, memset
49#include <iomanip> // for setprecision
50#include <iostream> // for basic_ostream, operator<<, clog
51#include <map> // for map
52#include <string> // for char_traits, allocator, operator+
53#include <utility> // for pair, move, swap
54#include <vector> // for vector
55
56#include "audio/audio_playback.h" // for AUDIO_PLAYBACK_CTL_QUERY_FORMAT
57#include "audio/codec.h" // for get_audio_codec_to_tag, audio_co...
58#include "audio/resampler.hpp" // for audio_frame2_resampler
59#include "audio/types.h" // for audio_frame2, audio_desc, AC_PCM
60#include "audio/utils.h" // for channel_map, calculate_rms, mux_...
61#include "compat/htonl.h" // for ntohl, sockaddr_storage
62#include "compat/strings.h" // for strcasecmp
63#include "control_socket.h"
64#include "crypto/openssl_decrypt.h" // for openssl_decrypt_info, OPENSSL_DE...
65#include "crypto/openssl_encrypt.h" // for openssl_mode
66#include "debug.h"
67#include "host.h"
68#include "lib_common.h"
69#include "messaging.h" // for new_response, msg_universal, che...
70#include "module.h"
71#include "rtp/fec.h" // for fec
72#include "rtp/pbuf.h" // for pbuf_audio_data, coded_data
73#include "rtp/rtp.h" // for RTP_MAX_PACKET_LEN
74#include "rtp/rtp_types.h" // for BUFNUM_BITS, audio_payload_hdr_t
75#include "tv.h"
76#include "types.h" // for fec_desc, fec_type
77#include "ug_runtime_error.hpp"
78#include "utils/color_out.h"
79#include "utils/macros.h"
81#include "utils/worker.h"
82
83using std::chrono::duration_cast;
84using std::chrono::seconds;
85using std::chrono::steady_clock;
86using std::fixed;
87using std::hex;
88using std::map;
89using std::ostringstream;
90using std::pair;
91using std::setprecision;
92using std::string;
93using std::to_string;
94using std::vector;
95
96#define AUDIO_DECODER_MAGIC 0x12ab332bu
97#define MOD_NAME "[audio dec.] "
98
99struct scale_data {
100 double vol_avg = 1.0;
101 int samples = 0;
102
103 double scale = 1.0;
104};
105
107private:
108 unsigned long int last_bufnum = -1;
109 int64_t played = 0;
110 int64_t missed = 0;
111
112 steady_clock::time_point t_last = steady_clock::now();
113
114 void print() const {
116 << SUNDERLINE("Audio dec stats")
117 << " (cumulative): " << SBOLD(played) << " played / "
118 << SBOLD(played + missed) << " total audio frames\n";
119 }
120
121public:
125
126 void update(unsigned long int bufnum) {
127 if (last_bufnum != static_cast<unsigned long int>(-1)) {
128 if ((last_bufnum + 1) % (1U<<BUFNUM_BITS) == bufnum) {
129 played += 1;
130 } else {
131 unsigned long int diff = (bufnum - last_bufnum + 1 + (1U<<BUFNUM_BITS)) % (1U<<BUFNUM_BITS);
132 if (diff >= (1U<<BUFNUM_BITS) / 2) {
133 diff -= (1U<<BUFNUM_BITS) / 2;
134 }
135 missed += diff;
136 }
137 }
138 last_bufnum = bufnum;
139
140 auto now = steady_clock::now();
141 if (duration_cast<seconds>(steady_clock::now() - t_last).count() > CUMULATIVE_REPORTS_INTERVAL) {
142 print();
143 t_last = now;
144 }
145 }
146};
147
149 uint32_t magic;
150 struct module mod;
151
152 struct timeval t0;
153
155
156 unsigned int channel_remapping:1;
158
159 vector<struct scale_data> scale = vector<scale_data>(1);
161
163
164 struct audio_desc saved_desc; // from network
166
168
171
172 bool muted;
173
175
178
182
184
186 std::atomic_uint64_t req_resample_to{0}; // hi 32 - numerator; lo 32 - denominator
187};
188
189constexpr double VOL_UP = 1.1;
190constexpr double VOL_DOWN = 1.0/1.1;
191
192static void compute_scale(struct scale_data *scale_data, double vol_avg, int samples, int sample_rate)
193{
194 scale_data->vol_avg = scale_data->vol_avg * (scale_data->samples / ((double) scale_data->samples + samples)) +
195 vol_avg * (samples / ((double) scale_data->samples + samples));
196 scale_data->samples += samples;
197
198 if (scale_data->samples > sample_rate * 6) {
199 double ratio = 1.0;
200
201 if (scale_data->vol_avg > 0.25 || (scale_data->vol_avg > 0.05 && scale_data->scale > 1.0)) {
202 ratio = VOL_DOWN;
203 } else if (scale_data->vol_avg < 0.20 && scale_data->scale < 1.0) {
204 ratio = VOL_UP;
205 }
206
207 scale_data->scale *= ratio;
208 scale_data->vol_avg *= ratio;
209
210 MSG(VERBOSE,
211 "Audio scale adjusted to: %f (average volume was %f)\n",
213
215 }
216}
217
219{
220 auto *s = static_cast<struct state_audio_decoder *>(m->priv_data);
221
222 while (auto *msg = reinterpret_cast<msg_universal *>(check_message(m))) {
223 struct response *r = nullptr;
224 if (strstr(msg->text, MSG_UNIVERSAL_TAG_AUDIO_DECODER) == msg->text) {
225 s->req_resample_to = strtoull(msg->text + strlen(MSG_UNIVERSAL_TAG_AUDIO_DECODER), nullptr, 0);
226 LOG(LOG_LEVEL_VERBOSE) << MOD_NAME << "Resampling sound to: " << (s->req_resample_to >> ADEC_CH_RATE_SHIFT) << "/" << (s->req_resample_to & ((1LLU << ADEC_CH_RATE_SHIFT) - 1)) << "\n";
227 r = new_response(RESPONSE_OK, nullptr);
228 } else {
229 r = new_response(RESPONSE_NOT_FOUND, nullptr);
230 }
231 free_message(reinterpret_cast<message *>(msg), r);
232 }
233}
234
235ADD_TO_PARAM("soft-resample", "* soft-resample=<num>/<den>\n"
236 " Resample to specified sampling rate, eg. 12288128/256 for 48000.5 Hz\n");
237
238void *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)
239{
240 struct state_audio_decoder *s = NULL;
241 bool scale_auto = false;
242 double scale_factor = 1.0;
243 char *tmp = nullptr;
244
245 assert(audio_scale != NULL);
246
247 try {
248 s = new struct state_audio_decoder();
249 } catch (ug_runtime_error &e) {
250 log_msg(LOG_LEVEL_ERROR, MOD_NAME "%s\n", e.what());
251 goto error;
252 }
253
256 s->audio_playback_state = p_state;
257
260 s->mod.priv_data = s;
263
264 if (const char *val = get_commandline_param("soft-resample")) {
265 assert(strchr(val, '/') != nullptr);
266 s->req_resample_to = strtoll(val, NULL, 0) << 32LLU | strtoll(strchr(val, '/') + 1, NULL, 0);
267 }
268
269 gettimeofday(&s->t0, NULL);
271
272 s->audio_decompress = NULL;
273
274 s->control = (struct control_state *) get_module(get_root_module(parent), "control");
275
276 if (strlen(encryption) > 0) {
277 s->dec_funcs = static_cast<const struct openssl_decrypt_info *>(load_library("openssl_decrypt",
279 if (!s->dec_funcs) {
280 log_msg(LOG_LEVEL_ERROR, "This UltraGrid version was build "
281 "without OpenSSL support!\n");
282 delete s;
283 return NULL;
284 }
285 if (s->dec_funcs->init(&s->decrypt, encryption) != 0) {
286 log_msg(LOG_LEVEL_ERROR, "Unable to create decompress!\n");
287 delete s;
288 return NULL;
289 }
290 }
291
292 if(audio_channel_map) {
293 if (!parse_channel_map_cfg(&s->channel_map, audio_channel_map)) {
294 goto error;
295 }
296 s->channel_remapping = true;
297 } else {
298 s->channel_remapping = false;
299 s->channel_map.map = NULL;
300 s->channel_map.sizes = NULL;
301 s->channel_map.size = 0;
302 }
303
304 if(strcasecmp(audio_scale, "mixauto") == 0) {
305 if(s->channel_remapping) {
306 log_msg(LOG_LEVEL_WARNING, MOD_NAME "Channel remapping detected - automatically scaling audio levels. Use \"--audio-scale none\" to disable.\n");
307 scale_auto = true;
308 } else {
309 scale_auto = false;
310 }
311 } else if(strcasecmp(audio_scale, "auto") == 0) {
312 scale_auto = true;
313 } else if(strcasecmp(audio_scale, "none") == 0) {
314 scale_auto = false;
315 scale_factor = 1.0;
316 } else {
317 scale_auto = false;
318 scale_factor = atof(audio_scale);
319 if(scale_factor <= 0.0) {
320 log_msg(LOG_LEVEL_ERROR, "Invalid audio scaling factor!\n");
321 goto error;
322 }
323 }
324
325 s->fixed_scale = scale_auto ? false : true;
326 s->scale.at(0).scale = scale_factor;
327
328 return s;
329
330error:
331 free(tmp);
332 if (s) {
334 }
335 return NULL;
336}
337
339{
340 struct state_audio_decoder *s = (struct state_audio_decoder *) state;
341
342 assert(s != NULL);
343 assert(s->magic == AUDIO_DECODER_MAGIC);
344
347
348 if (s->dec_funcs) {
349 s->dec_funcs->destroy(s->decrypt);
350 }
351
352 delete s->fec_state;
353 module_done(&s->mod);
354 delete s;
355}
356
357bool parse_audio_hdr(uint32_t *hdr, struct audio_desc *desc)
358{
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;
362
363 uint32_t audio_tag = ntohl(hdr[4]);
364 desc->codec = get_audio_codec_to_tag(audio_tag);
365
366 return true;
367}
368
376
377static void *adec_compute_and_print_stats(void *arg) {
378 auto d = (struct adec_stats_processing_data*) arg;
379 string loss;
380 if (d->bytes_received < d->bytes_expected) {
381 loss = " (" + to_string(d->bytes_expected - d->bytes_received) + " lost)";
382 }
383 log_msg(LOG_LEVEL_INFO, "[Audio decoder] Received %ld/%ld B%s, "
384 "decoded %d samples in %.2f sec.\n",
385 d->bytes_received,
386 d->bytes_expected,
387 loss.c_str(),
388 d->frame.get_sample_count(),
389 d->seconds);
390
391 char volume[STR_LEN];
392 char *vol_start = volume;
393 for (int i = 0; i < d->frame.get_channel_count(); ++i) {
394 double rms = 0.0;
395 double peak = 0.0;
396 rms = calculate_rms(&d->frame, i, &peak);
397 format_audio_channel_volume(i, rms, peak,
399 &vol_start, volume + sizeof volume);
400 }
401
402 log_msg(LOG_LEVEL_INFO, "[Audio decoder] Volume: %s dBFS RMS/peak%s\n",
403 volume, d->muted_receiver ? TBOLD(TRED(" (muted)")) : "");
404
405 delete d;
406
407 return NULL;
408}
409
410
411ADD_TO_PARAM("audio-dec-format", "* audio-dec-format=<fmt>|help\n"
412 " Forces specified format playback format.\n");
417static 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)
418{
419 if(decoder->saved_desc.ch_count == input_channels &&
420 decoder->saved_desc.bps == bps &&
421 decoder->saved_desc.sample_rate == sample_rate &&
422 decoder->saved_audio_tag == audio_tag) {
423 return true;
424 }
425
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,
429
430 if(decoder->channel_remapping && decoder->channel_map.size > input_channels){
431 log_msg(LOG_LEVEL_ERROR, MOD_NAME "Audio channel map references channels with idx higher than ch. count!\n");
432 }
433
434 int output_channels = decoder->channel_remapping ?
435 decoder->channel_map.max_output + 1: input_channels;
436 audio_desc device_desc = audio_desc{bps, sample_rate, output_channels, AC_PCM};
437 if (const char *fmt = get_commandline_param("audio-dec-format")) {
438 if (int ret = parse_audio_format(fmt, &device_desc)) {
439 exit_uv(ret > 0 ? 0 : 1);
440 return false;
441 }
442 }
443 size_t len = sizeof device_desc;
444 if (!decoder->audio_playback_ctl_func(decoder->audio_playback_state, AUDIO_PLAYBACK_CTL_QUERY_FORMAT, &device_desc, &len)) {
445 log_msg(LOG_LEVEL_ERROR, "Unable to query audio desc!\n");
446 return false;
447 }
448
449 s->buffer.bps = device_desc.bps;
450 s->buffer.ch_count = device_desc.ch_count;
451 s->buffer.sample_rate = device_desc.sample_rate;
452
453 if(!decoder->fixed_scale) {
454 decoder->scale.clear();
455 int scale_count = decoder->channel_remapping ? decoder->channel_map.max_output + 1: decoder->saved_desc.ch_count;
456 decoder->scale.resize(scale_count);
457 }
458 decoder->saved_desc.ch_count = input_channels;
459 decoder->saved_desc.bps = bps;
461 decoder->saved_audio_tag = audio_tag;
462 audio_codec_t audio_codec = get_audio_codec_to_tag(audio_tag);
463
464 received_frame.init(input_channels, audio_codec, bps, sample_rate);
465 decoder->decoded.init(input_channels, AC_PCM,
466 device_desc.bps, device_desc.sample_rate);
467 decoder->decoded.reserve(device_desc.bps * device_desc.sample_rate * 6);
468
470 if(!decoder->audio_decompress) {
471 log_msg(LOG_LEVEL_FATAL, "Unable to create audio decompress!\n");
472 exit_uv(1);
473 return false;
474 }
475
476 decoder->resample_remainder = {};
477 return true;
478}
479
480static 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)
481{
482 struct state_audio_decoder *decoder = s->decoder;
483 fec_desc fec_desc { FEC_RS, fec_params >> 19U, (fec_params >> 6U) & 0x1FFFU, fec_params & 0x3F };
484
485 if (decoder->fec_state == NULL || decoder->fec_state_desc.k != fec_desc.k || decoder->fec_state_desc.m != fec_desc.m || decoder->fec_state_desc.c != fec_desc.c) {
486 delete decoder->fec_state;
488 if (decoder->fec_state == nullptr) {
489 LOG(LOG_LEVEL_ERROR) << MOD_NAME << "Cannot initialize FEC decoder!\n";
490 return false;
491 }
492 decoder->fec_state_desc = fec_desc;
493 }
494
496
497 int channel = 0;
498 for (auto & c : fec_data) {
499 char *out = nullptr;
500 int out_len = 0;
501 if (decoder->fec_state->decode(c.first.data(), c.first.size(), &out, &out_len, c.second)) {
502 assert(out_len >= (int) sizeof(audio_payload_hdr_t));
503 if (!desc) {
504 uint32_t quant_sample_rate = 0;
505 uint32_t audio_tag = 0;
506
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);
511
512 desc.bps = (quant_sample_rate >> 26) / 8;
513 desc.sample_rate = quant_sample_rate & 0x07FFFFFFU;
514 desc.ch_count = fec_data.size();
515 desc.codec = get_audio_codec_to_tag(audio_tag);
516 if (!desc.codec) {
517 auto flags = std::clog.flags();
518 LOG(LOG_LEVEL_ERROR) << MOD_NAME << "Wrong AudioTag 0x" << hex << audio_tag << "\n";
519 std::clog.flags(flags);
520 }
521
522 if (!audio_decoder_reconfigure(decoder, s, received_frame, desc.ch_count, desc.bps, desc.sample_rate, audio_tag)) {
523 return false;
524 }
525 }
526 received_frame.replace(channel, 0, out + sizeof(audio_payload_hdr_t), out_len - sizeof(audio_payload_hdr_t));
527 }
528 channel += 1;
529 }
530
531 return true;
532}
533
534int decode_audio_frame(struct coded_data *cdata, void *pbuf_data, struct pbuf_stats *)
535{
536 struct pbuf_audio_data *s = (struct pbuf_audio_data *) pbuf_data;
537 struct state_audio_decoder *decoder = s->decoder;
538
539 int input_channels = 0;
540
541 bool first = true;
542 int bufnum = 0;
543
544 if(!cdata) {
545 return false;
546 }
547
548 if (!cdata->data->m) {
549 // skip frame without m-bit, we cannot determine number of channels
550 // (it is maximal substream number + 1 in packet with m-bit)
551 return false;
552 }
553
554 DEBUG_TIMER_START(audio_decode);
555 audio_frame2 received_frame;
556 received_frame.init(decoder->saved_desc.ch_count,
558 decoder->saved_desc.bps,
559 decoder->saved_desc.sample_rate);
560 received_frame.set_timestamp(cdata->data->ts);
561 vector<pair<vector<char>, map<int, int>>> fec_data;
562 uint32_t fec_params = 0;
563
564 while (cdata != NULL) {
565 char *data;
566 // for definition see rtp_callbacks.h
567 uint32_t *audio_hdr = (uint32_t *)(void *) cdata->data->data;
568 const int pt = cdata->data->pt;
569 enum openssl_mode crypto_mode;
570
571 if (PT_AUDIO_IS_ENCRYPTED(pt)) {
572 if(!decoder->decrypt) {
573 log_msg(LOG_LEVEL_WARNING, "Receiving encrypted audio data but "
574 "no decryption key entered!\n");
575 return false;
576 }
577 } else if (PT_IS_AUDIO(pt) && !PT_AUDIO_IS_ENCRYPTED(pt)) {
578 if(decoder->decrypt) {
579 log_msg(LOG_LEVEL_WARNING, "Receiving unencrypted audio data "
580 "while expecting encrypted.\n");
581 return false;
582 }
583 } else {
584 if (pt == PT_Unassign_Type95) {
585 log_msg_once(LOG_LEVEL_WARNING, to_fourcc('U', 'V', 'P', 'T'), MOD_NAME "Unassigned PT 95 received, ignoring.\n");
586 } else {
587 log_msg(LOG_LEVEL_WARNING, "Unknown audio packet type: %d\n", pt);
588 }
589 return false;
590 }
591
592 unsigned int length;
593 char plaintext[RTP_MAX_PACKET_LEN]; // plaintext will be actually shorter
594 size_t main_hdr_len = PT_AUDIO_HAS_FEC(pt) ? sizeof(fec_payload_hdr_t) : sizeof(audio_payload_hdr_t);
595 if (PT_AUDIO_IS_ENCRYPTED(pt)) {
596 uint32_t encryption_hdr = ntohl(*(uint32_t *)(void *) (cdata->data->data + main_hdr_len));
597 crypto_mode = (enum openssl_mode) (encryption_hdr >> 24);
598 if (crypto_mode == MODE_AES128_NONE || crypto_mode > MODE_AES128_MAX) {
599 log_msg(LOG_LEVEL_WARNING, "Unknown cipher mode: %d\n", (int) crypto_mode);
600 return false;
601 }
602 char *ciphertext = cdata->data->data + sizeof(crypto_payload_hdr_t) +
603 main_hdr_len;
604 int ciphertext_len = cdata->data->data_len - main_hdr_len -
605 sizeof(crypto_payload_hdr_t);
606
607 if((length = decoder->dec_funcs->decrypt(decoder->decrypt,
608 ciphertext, ciphertext_len,
609 (char *) audio_hdr, sizeof(audio_payload_hdr_t),
610 plaintext, crypto_mode)) == 0) {
611 return false;
612 }
613 data = plaintext;
614 } else {
615 assert(PT_IS_AUDIO(pt));
616 length = cdata->data->data_len - main_hdr_len;
617 data = cdata->data->data + main_hdr_len;
618 }
619
620 /* we receive last channel first (with m bit, last packet) */
621 /* thus can be set only with m-bit packet */
622 if(cdata->data->m) {
623 input_channels = ((ntohl(audio_hdr[0]) >> 22) & 0x3ff) + 1;
624 }
625
626 // we have:
627 // 1) last packet, then we have just set total channels
628 // 2) not last, but the last one was processed at first
629 assert(input_channels > 0);
630
631 int channel = (ntohl(audio_hdr[0]) >> 22) & 0x3ff;
632 bufnum = ntohl(audio_hdr[0]) & ((1U<<BUFNUM_BITS) - 1U);
633 int sample_rate = ntohl(audio_hdr[3]) & 0x3fffff;
634 unsigned int offset = ntohl(audio_hdr[1]);
635 unsigned int buffer_len = ntohl(audio_hdr[2]);
636 //fprintf(stderr, "%d-%d-%d ", length, bufnum, channel);
637
638 if (packet_counter_get_channels(decoder->packet_counter) != input_channels) {
640 decoder->packet_counter = packet_counter_init(input_channels);
641 }
642
643 if (PT_AUDIO_HAS_FEC(pt)) {
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);
649 } else {
650 int bps = (ntohl(audio_hdr[3]) >> 26) / 8;
651 uint32_t audio_tag = ntohl(audio_hdr[4]);
652
653 if (!audio_decoder_reconfigure(decoder, s, received_frame, input_channels, bps, sample_rate, audio_tag)) {
654 return false;
655 }
656
657 received_frame.replace(channel, offset, data, length);
658
659 /* buffer size same for every packet of the frame */
662 received_frame.resize(channel, buffer_len);
663 }
664
665 packet_counter_register_packet(decoder->packet_counter, channel, bufnum, offset, length);
666
667 if (first) {
668 memcpy(&s->source, ((char *) cdata->data) + RTP_MAX_PACKET_LEN, sizeof(struct sockaddr_storage));
669 first = false;
670 }
671
672 cdata = cdata->nxt;
673 }
674
675 decoder->summary.update(bufnum);
676
677 if (fec_params != 0) {
678 if (!audio_fec_decode(s, fec_data, fec_params, received_frame)) {
679 return false;
680 }
681 }
682
683 s->frame_size = received_frame.get_data_len();
684 audio_frame2 decompressed = audio_codec_decompress(decoder->audio_decompress, &received_frame);
685 if (!decompressed) {
686 return false;
687 }
688
689 // Perform a variable rate resample if any output device has requested it
690 if (decoder->req_resample_to != 0 || s->buffer.sample_rate != decompressed.get_sample_rate()) {
691 int resampler_bps = decoder->resampler.align_bps(decompressed.get_bps());
692 if (resampler_bps <= 0) {
693 return false;
694 }
695 if (resampler_bps != decompressed.get_bps()) {
696 decompressed.change_bps(resampler_bps);
697 }
698 if (decoder->req_resample_to != 0) {
699 auto [ret, remainder] = decompressed.resample_fake(decoder->resampler, decoder->req_resample_to >> ADEC_CH_RATE_SHIFT, decoder->req_resample_to & ((1LLU << ADEC_CH_RATE_SHIFT) - 1));
700 if (!ret) {
701 LOG(LOG_LEVEL_INFO) << MOD_NAME << "You may try to set different sampling on sender.\n";
702 return false;
703 }
704 decoder->resample_remainder = std::move(remainder);
705 } else {
706 if (!decompressed.resample(decoder->resampler, s->buffer.sample_rate)) {
707 LOG(LOG_LEVEL_INFO) << MOD_NAME << "You may try to set different sampling on sender.\n";
708 return false;
709 }
710 }
711 }
712
713 if (decompressed.get_bps() != s->buffer.bps) {
714 decompressed.change_bps(s->buffer.bps);
715 }
716
717 size_t new_data_len = s->buffer.data_len + decompressed.get_data_len(0) * s->buffer.ch_count;
718 if ((size_t) s->buffer.max_size < new_data_len) {
719 s->buffer.max_size = new_data_len;
720 s->buffer.data = (char *) realloc(s->buffer.data, new_data_len);
721 }
722
723 memset(s->buffer.data + s->buffer.data_len, 0, new_data_len - s->buffer.data_len);
724
725 if (!decoder->muted) {
726 // there is a mapping for channel
727 for(int channel = 0; channel < decompressed.get_channel_count(); ++channel) {
728 if(decoder->channel_remapping) {
729 if(channel < decoder->channel_map.size) {
730 for(int i = 0; i < decoder->channel_map.sizes[channel]; ++i) {
731 int new_position = decoder->channel_map.map[channel][i];
732 if (new_position >= s->buffer.ch_count)
733 continue;
735 decompressed.get_data(channel),
736 decompressed.get_bps(), decompressed.get_data_len(channel),
737 s->buffer.ch_count, new_position,
738 decoder->scale.at(decoder->fixed_scale ? 0 : new_position).scale);
739 }
740 }
741 } else {
742 if (channel >= s->buffer.ch_count)
743 continue;
744 mux_and_mix_channel(s->buffer.data + s->buffer.data_len, decompressed.get_data(channel),
745 decompressed.get_bps(),
746 decompressed.get_data_len(channel), s->buffer.ch_count, channel,
747 decoder->scale.at(decoder->fixed_scale ? 0 : input_channels).scale);
748 }
749 }
750 }
751 s->buffer.data_len = new_data_len;
752 if (s->buffer.timestamp == -1) {
753 s->buffer.timestamp = decompressed.get_timestamp();
754 }
755
756 decoder->decoded.append(decompressed);
757
758 if (control_stats_enabled(decoder->control)) {
759 std::string report = "ARECV";
760 int num_ch = std::min(decompressed.get_channel_count(), control_audio_ch_report_count(decoder->control));
761 for(int i = 0; i < num_ch; i++){
762 double rms, peak;
763 rms = calculate_rms(&decompressed, i, &peak);
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);
768 }
769
770 control_report_stats(decoder->control, report);
771 }
772
773 double seconds;
774 struct timeval t;
775
776 gettimeofday(&t, 0);
777 seconds = tv_diff(t, decoder->t0);
778 if(seconds > 5.0) {
779 auto d = new adec_stats_processing_data;
780 d->frame.init(decoder->decoded.get_channel_count(), decoder->decoded.get_codec(), decoder->decoded.get_bps(), decoder->decoded.get_sample_rate());
783 d->frame.reserve(decoder->decoded.get_bps() * decoder->decoded.get_sample_rate() * 6);
784
785 std::swap(d->frame, decoder->decoded);
786 d->seconds = seconds;
787 d->bytes_received = packet_counter_get_total_bytes(decoder->packet_counter);
788 d->bytes_expected = packet_counter_get_all_bytes(decoder->packet_counter);
789 d->muted_receiver = decoder->muted;
790
792
793 decoder->t0 = t;
795 }
796
797 DEBUG_TIMER_START(audio_decode_compute_autoscale);
798 if(!decoder->fixed_scale) {
799 int output_channels = decoder->channel_remapping ?
800 decoder->channel_map.max_output + 1: input_channels;
801 for(int i = 0; i <= decoder->channel_map.max_output; ++i) {
802 if (decoder->channel_map.contributors[i] <= 1) {
803 continue;
804 }
805 double avg = get_avg_volume(s->buffer.data, s->buffer.bps,
806 s->buffer.data_len / output_channels / s->buffer.bps, output_channels, i);
807 compute_scale(&decoder->scale.at(i), avg,
808 s->buffer.data_len / output_channels / s->buffer.bps, s->buffer.sample_rate);
809 }
810 }
811 DEBUG_TIMER_STOP(audio_decode_compute_autoscale);
812 DEBUG_TIMER_STOP(audio_decode);
813
814 return true;
815}
816/*
817 * Second version that uses external audio configuration,
818 * now it uses a struct state_audio_decoder instead an audio_frame2.
819 * It does multi-channel handling.
820 */
821int decode_audio_frame_mulaw(struct coded_data *cdata, void *data, struct pbuf_stats *)
822{
823 struct pbuf_audio_data *s = (struct pbuf_audio_data *) data;
824 struct state_audio_decoder *audio = s->decoder;
825
826 //struct state_audio_decoder *audio = (struct state_audio_decoder *)data;
827
828 if(!cdata) return false;
829
830 audio_frame2 received_frame;
831 received_frame.init(audio->saved_desc.ch_count, audio->saved_desc.codec,
832 audio->saved_desc.bps, audio->saved_desc.sample_rate);
833
834 // Check-if-there-is-only-one-channel optimization.
835 if (received_frame.get_channel_count() == 1) {
836 //char *to = audio->received_frame->data[0];
837 while (cdata != NULL) {
838 // Get the data to copy into the received_frame.
839 char *from = cdata->data->data;
840
841 received_frame.append(0, from, cdata->data->data_len);
842
843 cdata = cdata->nxt;
844 }
845 } else { // Multi-channel case.
846
847 /*
848 * Unoptimized version of the multi-channel handling.
849 * TODO: Optimize it! It's a matrix transpose.
850 * Take a look at http://stackoverflow.com/questions/1777901/array-interleaving-problem
851 */
852
853 while (cdata != NULL) {
854 // Check that the amount of data on cdata->data->data is congruent with 0 modulus audio->received_frame->ch_count.
855 if (cdata->data->data_len % received_frame.get_channel_count() != 0) {
856 // printf something?
857 return false;
858 }
859
860 char *from = cdata->data->data;
861
862 // For each group of samples.
863 for (int g = 0 ; g < (cdata->data->data_len / received_frame.get_channel_count()) ; g++) {
864 // Iterate throught each channel.
865 for (int ch = 0 ; ch < received_frame.get_channel_count(); ch ++) {
866
867 received_frame.append(ch, from, cdata->data->data_len);
868
869 from += received_frame.get_bps();
870 }
871 }
872
873 cdata = cdata->nxt;
874 }
875
876 }
877
878 return true;
879}
880
882{
883 auto s = (struct state_audio_decoder *) state;
884 for (auto & i : s->scale) {
885 i.scale = val;
886 }
887 s->muted = val == 0.0;
888}
889
void ** state
int void * val
struct video_desc desc
int int int sample_rate
const char * fmt
int void size_t * len
const char unsigned int flags
struct audio_frame ** audio
This file contains definition of some common audio types.
audio_codec_t
Definition types.h:51
@ AC_PCM
Definition types.h:53
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 *)
constexpr double VOL_UP
#define MOD_NAME
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
int align_bps(int orig)
void change_bps(int new_bps)
Definition types.cpp:313
void resize(int channel, size_t len)
Definition types.cpp:183
char * get_data(int channel)
Definition types.cpp:218
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.
Definition types.cpp:115
audio_codec_t get_codec() const
Definition types.cpp:213
void reserve(size_t len)
Definition types.cpp:161
int get_channel_count() const
Definition types.cpp:260
bool resample(audio_frame2_resampler &resampler_state, int new_sample_rate)
Definition types.cpp:361
int64_t get_timestamp() const
Definition types.cpp:340
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,...
Definition types.cpp:152
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
Definition types.cpp:342
void set_timestamp(int64_t ts)
Definition types.cpp:335
size_t get_data_len(int channel) const
Definition types.cpp:228
int get_sample_rate() const
Definition types.cpp:275
int get_bps() const
Definition types.cpp:200
void append(audio_frame2 const &frame)
Definition types.cpp:125
audio_codec_t get_audio_codec_to_tag(uint32_t tag)
Definition codec.cpp:460
audio_frame2 audio_codec_decompress(struct audio_codec_state *s, audio_frame2 *frame)
Definition codec.cpp:299
const char * get_name_to_audio_codec(audio_codec_t codec)
Definition codec.cpp:450
struct audio_codec_state * audio_codec_reconfigure(struct audio_codec_state *old, audio_codec_t audio_codec, audio_codec_direction_t direction)
Definition codec.cpp:225
void audio_codec_done(struct audio_codec_state *s)
Definition codec.cpp:372
@ AUDIO_DECODER
Definition codec.h:47
#define TERM_BOLD
Definition color_out.h:48
#define SBOLD(x)
Definition color_out.h:80
#define SUNDERLINE(x)
Definition color_out.h:81
#define TRED(x)
Definition color_out.h:75
#define TBOLD(x)
Definition color_out.h:70
#define TERM_FG_MAGENTA
Definition color_out.h:54
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
Definition cuda_dxt.cu:139
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
void log_msg_once(int level, uint32_t id, const char *msg,...)
Definition debug.cpp:137
#define DEBUG_TIMER_START(name)
Definition debug.h:324
#define LOG_LEVEL_NOTICE
information that may be interesting
Definition debug.h:61
#define LOG_LEVEL_INFO
normal reporting
Definition debug.h:62
#define DEBUG_TIMER_STOP(name)
Definition debug.h:325
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define LOG_LEVEL_VERBOSE
Definition debug.h:63
#define MSG(l, fmt,...)
Definition debug.h:96
#define LOG(level)
Definition debug.h:313
#define LOG_LEVEL_FATAL
errors that prevent UG run
Definition debug.h:58
#define CUMULATIVE_REPORTS_INTERVAL
Definition debug.h:110
#define LOG_LEVEL_WARNING
less severe errors
Definition debug.h:60
void module_init_default(struct module *module_data)
Definition module.c:48
void module_register(struct module *module_data, struct module *parent)
Definition module.c:82
struct module * get_module(struct module *root, const char *const_path)
Definition module.c:264
void module_done(struct module *module_data)
Definition module.c:93
struct module * get_root_module(struct module *node)
Definition module.c:203
@ MODULE_CLASS_DECODER
Definition module.h:95
int count
Definition hd-rum.c:44
void exit_uv(int status)
Definition host.cpp:625
const char * get_commandline_param(const char *key)
Definition host.cpp:894
#define ADD_TO_PARAM(param, doc)
Definition host.h:203
const void * load_library(const char *name, enum library_class cls, int abi_version)
@ LIBRARY_CLASS_UNDEFINED
Definition lib_common.h:74
#define to_fourcc(a, b, c, d)
Creates FourCC word.
Definition macros.h:114
@ STR_LEN
"standard" string length placeholder
Definition macros.h:134
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_NOT_FOUND
Definition messaging.h:61
#define OPENSSL_DECRYPT_ABI_VERSION
openssl_mode
only GCM provides autenticity, other modes only check integrity (CRC)
@ MODE_AES128_NONE
@ MODE_AES128_MAX
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
Definition rtp.h:61
#define PT_AUDIO_IS_ENCRYPTED(pt)
Definition rtp_types.h:166
#define BUFNUM_BITS
Definition rtp_types.h:171
uint32_t audio_payload_hdr_t[5]
Definition rtp_types.h:129
uint32_t fec_payload_hdr_t[5]
Definition rtp_types.h:152
#define PT_AUDIO_HAS_FEC(pt)
RFC 2550 hdr for audio - 16b MBZ, 16b Frag_off.
Definition rtp_types.h:165
#define PT_IS_AUDIO(pt)
Definition rtp_types.h:167
#define PT_Unassign_Type95
Definition rtp_types.h:77
uint32_t crypto_payload_hdr_t[1]
Definition rtp_types.h:161
codec_t first
Definition sdl2.c:423
const char * parent
Definition settings.cpp:198
int sample_rate
Definition types.h:78
int ch_count
Definition types.h:79
int bps
Definition types.h:77
int sample_rate
Definition types.h:101
int64_t timestamp
Definition types.h:109
char * data
Definition types.h:102
int ch_count
Definition types.h:104
int data_len
Definition types.h:103
int bps
Definition types.h:100
int max_size
Definition types.h:105
int * contributors
Definition utils.h:172
int max_output
Definition utils.h:174
int * sizes
Definition utils.h:171
int size
Definition utils.h:173
int ** map
Definition utils.h:170
rtp_packet * data
Definition pbuf.h:75
struct coded_data * nxt
Definition pbuf.h:72
unsigned int k
Definition types.h:209
unsigned int m
Definition types.h:209
unsigned int c
Definition types.h:209
Definition fec.h:51
static fec * create_from_desc(struct fec_desc) noexcept
Definition fec.cpp:107
virtual bool decode(char *in, int in_len, char **out, int *out_len, const std::map< int, int > &)=0
enum module_class cls
Definition module.h:115
void * priv_data
Definition module.h:126
notify_t new_message
if set, notifies module that new message is in queue, receiver lock is hold during the call
Definition module.h:119
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
Definition pbuf.h:100
size_t frame_size
currently decoded audio frame size (used similarly as vcodec_state::max_frame_size to allow caller ad...
Definition pbuf.h:104
struct state_audio_decoder * decoder
Definition pbuf.h:101
audio_frame buffer
Definition pbuf.h:99
steady_clock::time_point t_last
void update(unsigned long int 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)
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)
Definition tv.c:97
This file contains some common data types.
@ FEC_RS
Definition types.h:204
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)
Definition utils.cpp:402
bool parse_channel_map_cfg(struct channel_map *channel_map, const char *cfg)
Definition utils.cpp:716
double get_avg_volume(char *data, int bps, int sample_count, int stream_channels, int pos_in_stream)
Definition utils.cpp:461
double calculate_rms(audio_frame2 *frame, int channel, double *peak)
Calculates mean and peak RMS from audio samples.
Definition utils.cpp:150
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....
Definition utils.cpp:828
int parse_audio_format(const char *str, struct audio_desc *ret)
Definition utils.cpp:655
void task_run_async_detached(runnable_t task, void *data)
Runs task asynchronously in a detached state.
Definition worker.cpp:275