Skip to content

Commit 579773b

Browse files
authored
Add Opus payloader (#47)
1 parent d0fbb42 commit 579773b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

examples/send_from_file/example.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule Peer do
1818
PeerConnection,
1919
RTPCodecParameters,
2020
RTP.VP8Payloader,
21+
RTP.OpusPayloader,
2122
RTPTransceiver,
2223
SessionDescription
2324
}
@@ -159,7 +160,8 @@ defmodule Peer do
159160
# that's why you might hear short pauses in audio playback, when using this example
160161
Process.send_after(self(), :send_audio_packet, duration)
161162
# values set to 0 are handled by PeerConnection.set_rtp
162-
rtp_packet = ExRTP.Packet.new(packet, 0, 0, state.last_audio_timestamp, 0)
163+
rtp_packet = OpusPayloader.payload(packet)
164+
rtp_packet = %{rtp_packet | timestamp: state.last_audio_timestamp}
163165
PeerConnection.send_rtp(state.peer_connection, state.audio_track_id, rtp_packet)
164166

165167
# OggReader.next_packet/1 returns duration in ms

lib/ex_webrtc/rtp/opus_payloader.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule ExWebRTC.RTP.OpusPayloader do
2+
@moduledoc """
3+
Encapsulates Opus audio packet into an RTP packet.
4+
"""
5+
6+
@doc """
7+
Packs Opus packet into an RTP packet.
8+
9+
Fields from RTP header like ssrc, timestamp etc. are set to 0.
10+
"""
11+
@spec payload(binary()) :: ExRTP.Packet.t()
12+
def payload(packet) when packet != <<>> do
13+
ExRTP.Packet.new(packet, 0, 0, 0, 0)
14+
end
15+
end

0 commit comments

Comments
 (0)