1 /* SPDX-License-Identifier: GPL-3.0-or-later
2  * Copyright © 2016-2018 The TokTok team.
3  * Copyright © 2013-2015 Tox project.
4  */
5 #ifndef C_TOXCORE_TOXAV_AUDIO_H
6 #define C_TOXCORE_TOXAV_AUDIO_H
7 
8 #include "toxav.h"
9 
10 #include "../toxcore/logger.h"
11 #include "../toxcore/util.h"
12 #include "rtp.h"
13 
14 #include <opus.h>
15 #include <pthread.h>
16 
17 #define AUDIO_JITTERBUFFER_COUNT 3
18 #define AUDIO_MAX_SAMPLE_RATE 48000
19 #define AUDIO_MAX_CHANNEL_COUNT 2
20 
21 #define AUDIO_START_SAMPLE_RATE 48000
22 #define AUDIO_START_BITRATE 48000
23 #define AUDIO_START_CHANNEL_COUNT 2
24 #define AUDIO_OPUS_PACKET_LOSS_PERC 10
25 #define AUDIO_OPUS_COMPLEXITY 10
26 
27 #define AUDIO_DECODER_START_SAMPLE_RATE 48000
28 #define AUDIO_DECODER_START_CHANNEL_COUNT 1
29 
30 #define AUDIO_MAX_FRAME_DURATION_MS 120
31 
32 // ((sampling_rate_in_hz * frame_duration_in_ms) / 1000) * 2 // because PCM16 needs 2 bytes for 1 sample
33 // These are per frame and per channel.
34 #define AUDIO_MAX_BUFFER_SIZE_PCM16 ((AUDIO_MAX_SAMPLE_RATE * AUDIO_MAX_FRAME_DURATION_MS) / 1000)
35 #define AUDIO_MAX_BUFFER_SIZE_BYTES (AUDIO_MAX_BUFFER_SIZE_PCM16 * 2)
36 
37 typedef struct ACSession_s {
38     Mono_Time *mono_time;
39     const Logger *log;
40 
41     /* encoding */
42     OpusEncoder *encoder;
43     int32_t le_sample_rate; /* Last encoder sample rate */
44     int32_t le_channel_count; /* Last encoder channel count */
45     int32_t le_bit_rate; /* Last encoder bit rate */
46 
47     /* decoding */
48     OpusDecoder *decoder;
49     int32_t lp_channel_count; /* Last packet channel count */
50     int32_t lp_sampling_rate; /* Last packet sample rate */
51     int32_t lp_frame_duration; /* Last packet frame duration */
52     int32_t ld_sample_rate; /* Last decoder sample rate */
53     int32_t ld_channel_count; /* Last decoder channel count */
54     uint64_t ldrts; /* Last decoder reconfiguration time stamp */
55     void *j_buf;
56 
57     pthread_mutex_t queue_mutex[1];
58 
59     ToxAV *av;
60     uint32_t friend_number;
61     /* Audio frame receive callback */
62     toxav_audio_receive_frame_cb *acb;
63     void *acb_user_data;
64 } ACSession;
65 
66 ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
67                   toxav_audio_receive_frame_cb *cb, void *cb_data);
68 void ac_kill(ACSession *ac);
69 void ac_iterate(ACSession *ac);
70 int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg);
71 int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels);
72 
73 #endif // C_TOXCORE_TOXAV_AUDIO_H
74