From b9cb11cb5062454b04fc04d3078039feff5b1662 Mon Sep 17 00:00:00 2001 From: sepfy Date: Mon, 18 Sep 2023 12:03:55 +0100 Subject: [PATCH] Support OPUS --- README.md | 3 ++- src/config.h | 2 ++ src/peer_connection.c | 7 +++++++ src/rtp.c | 15 +++++++++++---- src/rtp.h | 1 + src/sdp.c | 11 +++++++++++ src/sdp.h | 2 ++ 7 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 68ce4c0..b203585 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,9 @@ libpeer is a WebRTC implementation written in C, developed with BSD socket. The - H264 - G.711 PCM (A-law) - G.711 PCM (ยต-law) + - OPUS - DataChannel -- STUN +- STUN/TURN - Signaling ### Dependencies diff --git a/src/config.h b/src/config.h index bb464bb..f870a97 100644 --- a/src/config.h +++ b/src/config.h @@ -9,12 +9,14 @@ #define VIDEO_RB_DATA_LENGTH (CONFIG_MTU * 64) #define AUDIO_RB_DATA_LENGTH (CONFIG_MTU * 64) #define DATA_RB_DATA_LENGTH (SCTP_MTU * 128) +#define AUDIO_LATENCY 50 // ms #else #define HAVE_USRSCTP #define RSA_KEY_LENGTH 2048 #define VIDEO_RB_DATA_LENGTH (CONFIG_MTU * 256) #define AUDIO_RB_DATA_LENGTH (CONFIG_MTU * 256) #define DATA_RB_DATA_LENGTH (SCTP_MTU * 128) +#define AUDIO_LATENCY 20 // ms #endif // siganling diff --git a/src/peer_connection.c b/src/peer_connection.c index 003b393..cc7f50d 100644 --- a/src/peer_connection.c +++ b/src/peer_connection.c @@ -308,6 +308,13 @@ static void peer_connection_state_new(PeerConnection *pc) { sdp_append(&pc->local_sdp, "a=setup:actpass"); strcat(pc->local_sdp.content, description); break; + + case CODEC_OPUS: + sdp_append_opus(&pc->local_sdp); + sdp_append(&pc->local_sdp, "a=fingerprint:sha-256 %s", pc->dtls_srtp.local_fingerprint); + sdp_append(&pc->local_sdp, "a=setup:actpass"); + strcat(pc->local_sdp.content, description); + default: break; } diff --git a/src/rtp.c b/src/rtp.c index bb19256..3efbc3a 100644 --- a/src/rtp.c +++ b/src/rtp.c @@ -77,7 +77,7 @@ static int rtp_encoder_encode_h264_fu_a(RtpEncoder *rtp_encoder, uint8_t *buf, s rtp_packet->header.seq_number = htons(rtp_encoder->seq_number++); rtp_packet->header.timestamp = htonl(rtp_encoder->timestamp); rtp_packet->header.ssrc = htonl(rtp_encoder->ssrc); - rtp_encoder->timestamp += 90000/25; // 25 FPS. + rtp_encoder->timestamp += rtp_encoder->timestamp_increment; uint8_t type = buf[0] & 0x1f; uint8_t nri = (buf[0] & 0x60) >> 5; @@ -173,7 +173,7 @@ static int rtp_encoder_encode_generic(RtpEncoder *rtp_encoder, uint8_t *buf, siz rtp_header->markerbit = 0; rtp_header->type = rtp_encoder->type; rtp_header->seq_number = htons(rtp_encoder->seq_number++); - rtp_encoder->timestamp += size; // 8000 HZ. + rtp_encoder->timestamp += rtp_encoder->timestamp_increment; rtp_header->timestamp = htonl(rtp_encoder->timestamp); rtp_header->ssrc = htonl(rtp_encoder->ssrc); memcpy(rtp_encoder->buf + sizeof(RtpHeader), buf, size); @@ -195,16 +195,24 @@ void rtp_encoder_init(RtpEncoder *rtp_encoder, MediaCodec codec, RtpOnPacket on_ case CODEC_H264: rtp_encoder->type = PT_H264; rtp_encoder->ssrc = SSRC_H264; + rtp_encoder->timestamp_increment = 90000/30; // 25 FPS. rtp_encoder->encode_func = rtp_encoder_encode_h264; break; case CODEC_PCMA: rtp_encoder->type = PT_PCMA; rtp_encoder->ssrc = SSRC_PCMA; + rtp_encoder->timestamp_increment = AUDIO_LATENCY*8000/1000; rtp_encoder->encode_func = rtp_encoder_encode_generic; break; case CODEC_PCMU: rtp_encoder->type = PT_PCMU; rtp_encoder->ssrc = SSRC_PCMU; + rtp_encoder->timestamp_increment = AUDIO_LATENCY*8000/1000; + rtp_encoder->encode_func = rtp_encoder_encode_generic; + case CODEC_OPUS: + rtp_encoder->type = PT_OPUS; + rtp_encoder->ssrc = SSRC_OPUS; + rtp_encoder->timestamp_increment = AUDIO_LATENCY*48000/1000; rtp_encoder->encode_func = rtp_encoder_encode_generic; default: break; @@ -235,9 +243,8 @@ void rtp_decoder_init(RtpDecoder *rtp_decoder, MediaCodec codec, RtpOnPacket on_ rtp_decoder->decode_func = NULL; break; case CODEC_PCMA: - rtp_decoder->decode_func = rtp_decode_generic; - break; case CODEC_PCMU: + case CODEC_OPUS: rtp_decoder->decode_func = rtp_decode_generic; default: break; diff --git a/src/rtp.h b/src/rtp.h index c1a5697..e27de55 100644 --- a/src/rtp.h +++ b/src/rtp.h @@ -91,6 +91,7 @@ struct RtpEncoder { uint16_t seq_number; uint32_t ssrc; uint32_t timestamp; + uint32_t timestamp_increment; uint8_t buf[CONFIG_MTU + 1]; }; diff --git a/src/sdp.c b/src/sdp.c index cb647ab..2d2911d 100644 --- a/src/sdp.c +++ b/src/sdp.c @@ -65,6 +65,17 @@ void sdp_append_pcmu(Sdp *sdp) { sdp_append(sdp, "a=rtcp-mux"); } +void sdp_append_opus(Sdp *sdp) { + + sdp_append(sdp, "m=audio 9 UDP/TLS/RTP/SAVP 111"); + sdp_append(sdp, "a=rtpmap:111 opus/48000/2"); + sdp_append(sdp, "a=ssrc:6 cname:webrtc-opus"); + sdp_append(sdp, "a=sendrecv"); + sdp_append(sdp, "a=mid:audio"); + sdp_append(sdp, "c=IN IP4 0.0.0.0"); + sdp_append(sdp, "a=rtcp-mux"); +} + void sdp_append_datachannel(Sdp *sdp) { sdp_append(sdp, "m=application 50712 UDP/DTLS/SCTP webrtc-datachannel"); diff --git a/src/sdp.h b/src/sdp.h index a309832..2352fd6 100644 --- a/src/sdp.h +++ b/src/sdp.h @@ -18,6 +18,8 @@ void sdp_append_pcma(Sdp *sdp); void sdp_append_pcmu(Sdp *sdp); +void sdp_append_opus(Sdp *sdp); + void sdp_append_datachannel(Sdp *sdp); void sdp_create(Sdp *sdp, int b_video, int b_audio, int b_datachannel);