Skip to content

Commit

Permalink
Support OPUS
Browse files Browse the repository at this point in the history
  • Loading branch information
sepfy committed Sep 18, 2023
1 parent 8ee8ae7 commit b9cb11c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/peer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 11 additions & 4 deletions src/rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/rtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

Expand Down
11 changes: 11 additions & 0 deletions src/sdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions src/sdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b9cb11c

Please sign in to comment.