Skip to content

Commit c75491a

Browse files
committed
Add set_sender_codec
1 parent 60dd117 commit c75491a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

lib/ex_webrtc/peer_connection.ex

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule ExWebRTC.PeerConnection do
99

1010
require Logger
1111

12+
alias ExWebRTC.RTPCodecParameters
1213
alias __MODULE__.{Configuration, Demuxer, TWCCRecorder}
1314

1415
alias ExWebRTC.{
@@ -152,9 +153,58 @@ defmodule ExWebRTC.PeerConnection do
152153
GenServer.call(peer_connection, :get_configuration)
153154
end
154155

156+
@doc """
157+
Sets the codec that will be used for sending RTP packets.
158+
159+
`send_rtp/4` overrides some of the RTP packet fields.
160+
In particular, when multiple codecs are negotiated, `send_rtp/4` will use
161+
payload type of the most preffered by the remote side codec (i.e. the first
162+
one from the list of codecs in the remote description).
163+
164+
Use this function if you want to select, which codec (hence payload type)
165+
should be used for sending.
166+
167+
Although very unlikely, keep in mind that after renegotiation,
168+
the selected codec may no longer be supported by the remote side and you might
169+
need to call this function again, passing a new codec.
170+
171+
To check available codecs you can use `get_transceivers/1`:
172+
173+
```
174+
{:ok, pc} = PeerConnection.start_link()
175+
{:ok, rtp_sender} = PeerConnection.add_track(MediaStreamTrack.new(:video))
176+
177+
tr =
178+
pc
179+
|> PeerConnection.get_transceiver()
180+
|> Enum.find(fn tr -> tr.sender.id == rtp_sender.id end)
181+
182+
dbg(tr.codecs) # list of supported codecs both for sending and receiving
183+
184+
# e.g. always prefer h264 over vp8
185+
h264 = Enum.find(tr.codecs, fn codec -> codec.mime_type == "video/H264" end)
186+
vp8 = Enum.find(tr.codecs, fn codec -> codec.mime_type == "video/VP8" end)
187+
188+
:ok = PeerConnection.set_sender_codec(pc, rtp_sender.id, h264 || vp8)
189+
```
190+
"""
191+
@spec set_sender_codec(peer_connection(), RTPSender.id(), RTPCodecParameters.t()) ::
192+
:ok | {:error, term()}
193+
def set_sender_codec(peer_connection, sender_id, codec) do
194+
GenServer.call(peer_connection, {:set_sender_codec, sender_id, codec})
195+
end
196+
155197
@doc """
156198
Sends an RTP packet to the remote peer using the track specified by the `track_id`.
157199
200+
The following fields of the RTP packet will be overwritten by this function:
201+
* payload type
202+
* ssrc
203+
* rtp header extensions
204+
205+
If you negotiated multiple codecs (hence payload types) and you want to choose,
206+
which one should be used, see `set_sender_codec/3`.
207+
158208
Options:
159209
* `rtx?` - send the packet as if it was retransmitted (use SSRC and payload type specific to RTX)
160210
"""

0 commit comments

Comments
 (0)