@@ -9,6 +9,7 @@ defmodule ExWebRTC.PeerConnection do
9
9
10
10
require Logger
11
11
12
+ alias ExWebRTC.RTPCodecParameters
12
13
alias __MODULE__ . { Configuration , Demuxer , TWCCRecorder }
13
14
14
15
alias ExWebRTC . {
@@ -152,9 +153,58 @@ defmodule ExWebRTC.PeerConnection do
152
153
GenServer . call ( peer_connection , :get_configuration )
153
154
end
154
155
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
+
155
197
@ doc """
156
198
Sends an RTP packet to the remote peer using the track specified by the `track_id`.
157
199
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
+
158
208
Options:
159
209
* `rtx?` - send the packet as if it was retransmitted (use SSRC and payload type specific to RTX)
160
210
"""
0 commit comments