Skip to content

Commit a94ff66

Browse files
committed
refactoring, increased bitrate, reduced buffers
1 parent 10595a1 commit a94ff66

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

janus/src/acap.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "uslibs/ring.h"
3939
#include "uslibs/threading.h"
4040

41+
#include "rtp.h"
4142
#include "logging.h"
4243

4344

@@ -49,24 +50,23 @@
4950
// - https://github.com/xiph/opus/blob/7b05f44/src/opus_demo.c#L368
5051
// #define _HZ_TO_FRAMES(_hz) (6 * (_hz) / 50) // 120ms
5152
#define _HZ_TO_FRAMES(_hz) ((_hz) / 50) // 20ms
52-
#define _HZ_TO_BUF16(_hz) (_HZ_TO_FRAMES(_hz) * 2) // One stereo frame = (16bit L) + (16bit R)
53+
#define _HZ_TO_BUF16(_hz) (_HZ_TO_FRAMES(_hz) * US_RTP_OPUS_CH) // ... * 2: One stereo frame = (16bit L) + (16bit R)
5354
#define _HZ_TO_BUF8(_hz) (_HZ_TO_BUF16(_hz) * sizeof(s16))
5455

5556
#define _MIN_PCM_HZ 8000
5657
#define _MAX_PCM_HZ 192000
5758
#define _MAX_BUF16 _HZ_TO_BUF16(_MAX_PCM_HZ)
5859
#define _MAX_BUF8 _HZ_TO_BUF8(_MAX_PCM_HZ)
59-
#define _ENCODER_INPUT_HZ 48000
6060

6161

6262
typedef struct {
6363
s16 data[_MAX_BUF16];
6464
} _pcm_buffer_s;
6565

6666
typedef struct {
67-
u8 data[_MAX_BUF8]; // Worst case
67+
u8 data[US_RTP_PAYLOAD_SIZE];
6868
uz used;
69-
u64 pts;
69+
u64 pts;
7070
} _enc_buffer_s;
7171

7272

@@ -117,7 +117,7 @@ us_acap_s *us_acap_init(const char *name, uint pcm_hz) {
117117

118118
SET_PARAM("Can't initialize PCM params", snd_pcm_hw_params_any);
119119
SET_PARAM("Can't set PCM access type", snd_pcm_hw_params_set_access, SND_PCM_ACCESS_RW_INTERLEAVED);
120-
SET_PARAM("Can't set PCM channels numbre", snd_pcm_hw_params_set_channels, 2);
120+
SET_PARAM("Can't set PCM channels number", snd_pcm_hw_params_set_channels, US_RTP_OPUS_CH);
121121
SET_PARAM("Can't set PCM sampling format", snd_pcm_hw_params_set_format, SND_PCM_FORMAT_S16_LE);
122122
SET_PARAM("Can't set PCM sampling rate", snd_pcm_hw_params_set_rate_near, &acap->pcm_hz, 0);
123123
if (acap->pcm_hz < _MIN_PCM_HZ || acap->pcm_hz > _MAX_PCM_HZ) {
@@ -132,8 +132,8 @@ us_acap_s *us_acap_init(const char *name, uint pcm_hz) {
132132
# undef SET_PARAM
133133
}
134134

135-
if (acap->pcm_hz != _ENCODER_INPUT_HZ) {
136-
acap->res = speex_resampler_init(2, acap->pcm_hz, _ENCODER_INPUT_HZ, SPEEX_RESAMPLER_QUALITY_DESKTOP, &err);
135+
if (acap->pcm_hz != US_RTP_OPUS_HZ) {
136+
acap->res = speex_resampler_init(US_RTP_OPUS_CH, acap->pcm_hz, US_RTP_OPUS_HZ, SPEEX_RESAMPLER_QUALITY_DESKTOP, &err);
137137
if (err < 0) {
138138
acap->res = NULL;
139139
_JLOG_PERROR_RES(err, "acap", "Can't create resampler");
@@ -143,9 +143,11 @@ us_acap_s *us_acap_init(const char *name, uint pcm_hz) {
143143

144144
{
145145
// OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY
146-
acap->enc = opus_encoder_create(_ENCODER_INPUT_HZ, 2, OPUS_APPLICATION_AUDIO, &err);
146+
acap->enc = opus_encoder_create(US_RTP_OPUS_HZ, US_RTP_OPUS_CH, OPUS_APPLICATION_AUDIO, &err);
147147
assert(err == 0);
148-
assert(!opus_encoder_ctl(acap->enc, OPUS_SET_BITRATE(48000)));
148+
// https://github.com/meetecho/janus-gateway/blob/3cdd6ff/src/plugins/janus_audiobridge.c#L2272
149+
// https://datatracker.ietf.org/doc/html/rfc7587#section-3.1.1
150+
assert(!opus_encoder_ctl(acap->enc, OPUS_SET_BITRATE(128000)));
149151
assert(!opus_encoder_ctl(acap->enc, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND)));
150152
assert(!opus_encoder_ctl(acap->enc, OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC)));
151153
// OPUS_SET_INBAND_FEC(1), OPUS_SET_PACKET_LOSS_PERC(10): see rtpa.c
@@ -258,13 +260,13 @@ static void *_encoder_thread(void *v_acap) {
258260

259261
s16 *in_ptr;
260262
if (acap->res != NULL) {
261-
assert(acap->pcm_hz != _ENCODER_INPUT_HZ);
263+
assert(acap->pcm_hz != US_RTP_OPUS_HZ);
262264
u32 in_count = acap->pcm_frames;
263-
u32 out_count = _HZ_TO_FRAMES(_ENCODER_INPUT_HZ);
265+
u32 out_count = _HZ_TO_FRAMES(US_RTP_OPUS_HZ);
264266
speex_resampler_process_interleaved_int(acap->res, in->data, &in_count, in_res, &out_count);
265267
in_ptr = in_res;
266268
} else {
267-
assert(acap->pcm_hz == _ENCODER_INPUT_HZ);
269+
assert(acap->pcm_hz == US_RTP_OPUS_HZ);
268270
in_ptr = in->data;
269271
}
270272

@@ -276,14 +278,14 @@ static void *_encoder_thread(void *v_acap) {
276278
}
277279
_enc_buffer_s *const out = acap->enc_ring->items[out_ri];
278280

279-
const int size = opus_encode(acap->enc, in_ptr, _HZ_TO_FRAMES(_ENCODER_INPUT_HZ), out->data, US_ARRAY_LEN(out->data));
281+
const int size = opus_encode(acap->enc, in_ptr, _HZ_TO_FRAMES(US_RTP_OPUS_HZ), out->data, US_ARRAY_LEN(out->data));
280282
us_ring_consumer_release(acap->pcm_ring, in_ri);
281283

282284
if (size >= 0) {
283285
out->used = size;
284286
out->pts = acap->pts;
285287
// https://datatracker.ietf.org/doc/html/rfc7587#section-4.2
286-
acap->pts += _HZ_TO_FRAMES(_ENCODER_INPUT_HZ);
288+
acap->pts += _HZ_TO_FRAMES(US_RTP_OPUS_HZ);
287289
} else {
288290
_JLOG_PERROR_OPUS(size, "acap", "Fatal: Can't encode PCM frame to OPUS");
289291
}

janus/src/rtp.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828
// https://stackoverflow.com/questions/47635545/why-webrtc-chose-rtp-max-packet-size-to-1200-bytes
2929
#define US_RTP_DATAGRAM_SIZE 1200
3030
#define US_RTP_HEADER_SIZE 12
31+
#define US_RTP_PAYLOAD_SIZE (US_RTP_DATAGRAM_SIZE - US_RTP_HEADER_SIZE)
3132

32-
#define US_RTP_VIDEO_PAYLOAD 96
33-
#define US_RTP_AUDIO_PAYLOAD 111
33+
#define US_RTP_H264_PAYLOAD 96
34+
#define US_RTP_OPUS_PAYLOAD 111
35+
36+
#define US_RTP_OPUS_HZ 48000
37+
#define US_RTP_OPUS_CH 2
3438

3539

3640
typedef struct {

janus/src/rtpa.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ us_rtpa_s *us_rtpa_init(us_rtp_callback_f callback) {
3333
us_rtpa_s *rtpa;
3434
US_CALLOC(rtpa, 1);
3535
rtpa->rtp = us_rtp_init();
36-
us_rtp_assign(rtpa->rtp, US_RTP_AUDIO_PAYLOAD, false);
36+
us_rtp_assign(rtpa->rtp, US_RTP_OPUS_PAYLOAD, false);
3737
rtpa->callback = callback;
3838
return rtpa;
3939
}
@@ -49,14 +49,16 @@ char *us_rtpa_make_sdp(us_rtpa_s *rtpa, bool mic) {
4949
US_ASPRINTF(sdp,
5050
"m=audio 1 RTP/SAVPF %u" RN
5151
"c=IN IP4 0.0.0.0" RN
52-
"a=rtpmap:%u OPUS/48000/2" RN
52+
"a=rtpmap:%u OPUS/%u/%u" RN
5353
"a=fmtp:%u sprop-stereo=1" RN // useinbandfec=1
5454
"a=rtcp-fb:%u nack" RN
5555
"a=rtcp-fb:%u nack pli" RN
5656
"a=rtcp-fb:%u goog-remb" RN
5757
"a=ssrc:%" PRIu32 " cname:ustreamer" RN
5858
"a=%s" RN,
59-
pl, pl, pl, pl, pl, pl,
59+
pl, pl,
60+
US_RTP_OPUS_HZ, US_RTP_OPUS_CH,
61+
pl, pl, pl, pl,
6062
rtpa->rtp->ssrc,
6163
(mic ? "sendrecv" : "sendonly")
6264
);

janus/src/rtpv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ us_rtpv_s *us_rtpv_init(us_rtp_callback_f callback) {
4545
us_rtpv_s *rtpv;
4646
US_CALLOC(rtpv, 1);
4747
rtpv->rtp = us_rtp_init();
48-
us_rtp_assign(rtpv->rtp, US_RTP_VIDEO_PAYLOAD, true);
48+
us_rtp_assign(rtpv->rtp, US_RTP_H264_PAYLOAD, true);
4949
rtpv->callback = callback;
5050
return rtpv;
5151
}

0 commit comments

Comments
 (0)