@@ -3,7 +3,7 @@ defmodule ExWebRTC.PeerConnection.Configuration do
33 PeerConnection configuration
44 """
55
6- alias ExWebRTC.RTPCodecParameters
6+ alias ExWebRTC . { RTPCodecParameters , SDPUtils }
77 alias ExSDP.Attribute . { Extmap , FMTP , RTCPFeedback }
88
99 @ default_audio_codecs [
@@ -48,8 +48,15 @@ defmodule ExWebRTC.PeerConnection.Configuration do
4848 }
4949 }
5050
51- @ mandatory_audio_rtp_hdr_exts Enum . map ( [ :mid ] , & Map . fetch! ( @ rtp_hdr_extensions , & 1 ) . ext )
52- @ mandatory_video_rtp_hdr_exts Enum . map ( [ :mid ] , & Map . fetch! ( @ rtp_hdr_extensions , & 1 ) . ext )
51+ @ mandatory_audio_rtp_hdr_exts Map . new ( [ :mid ] , fn ext_shortcut ->
52+ extmap = Map . fetch! ( @ rtp_hdr_extensions , ext_shortcut ) . ext
53+ { extmap . uri , extmap }
54+ end )
55+
56+ @ mandatory_video_rtp_hdr_exts Map . new ( [ :mid ] , fn ext_shortcut ->
57+ extmap = Map . fetch! ( @ rtp_hdr_extensions , ext_shortcut ) . ext
58+ { extmap . uri , extmap }
59+ end )
5360
5461 @ typedoc """
5562 Supported RTP header extensions.
@@ -113,8 +120,8 @@ defmodule ExWebRTC.PeerConnection.Configuration do
113120 ice_ip_filter: ( :inet . ip_address ( ) -> boolean ( ) ) ,
114121 audio_codecs: [ RTPCodecParameters . t ( ) ] ,
115122 video_codecs: [ RTPCodecParameters . t ( ) ] ,
116- audio_rtp_hdr_exts: [ Extmap . t ( ) ] ,
117- video_rtp_hdr_exts: [ Extmap . t ( ) ]
123+ audio_rtp_hdr_exts: % { ( uri :: String . t ( ) ) => Extmap . t ( ) } ,
124+ video_rtp_hdr_exts: % { ( uri :: String . t ( ) ) => Extmap . t ( ) }
118125 }
119126
120127 defstruct ice_servers: [ ] ,
@@ -183,8 +190,8 @@ defmodule ExWebRTC.PeerConnection.Configuration do
183190 def is_supported_rtp_hdr_extension ( config , rtp_hdr_extension , media_type ) do
184191 supported_uris =
185192 case media_type do
186- :audio -> Enum . map ( config . audio_rtp_hdr_exts , & & 1 . uri )
187- :video -> Enum . map ( config . video_rtp_hdr_exts , & & 1 . uri )
193+ :audio -> Map . keys ( config . audio_rtp_hdr_exts )
194+ :video -> Map . keys ( config . video_rtp_hdr_exts )
188195 end
189196
190197 rtp_hdr_extension . uri in supported_uris
@@ -194,34 +201,66 @@ defmodule ExWebRTC.PeerConnection.Configuration do
194201 @ spec is_supported_rtcp_fb ( t ( ) , RTCPFeedback . t ( ) ) :: boolean ( )
195202 def is_supported_rtcp_fb ( _config , _rtcp_fb ) , do: false
196203
204+ @ doc false
205+ @ spec update ( t ( ) , ExSDP . t ( ) ) :: t ( )
206+ def update ( config , sdp ) do
207+ extmaps = SDPUtils . get_extensions2 ( sdp )
208+
209+ { audio_exts , video_exts } =
210+ Enum . reduce ( extmaps , { config . audio_rtp_hdr_exts , config . video_rtp_hdr_exts } , fn extmap ,
211+ { audio_exts ,
212+ video_exts } ->
213+ audio_exts =
214+ if Map . has_key? ( audio_exts , extmap . uri ) do
215+ Map . put ( audio_exts , extmap . uri , extmap )
216+ else
217+ audio_exts
218+ end
219+
220+ video_exts =
221+ if Map . has_key? ( video_exts , extmap . uri ) do
222+ Map . put ( video_exts , extmap . uri , extmap )
223+ else
224+ video_exts
225+ end
226+
227+ { audio_exts , video_exts }
228+ end )
229+
230+ % { config | audio_rtp_hdr_exts: audio_exts , video_rtp_hdr_exts: video_exts }
231+ end
232+
197233 defp add_mandatory_rtp_hdr_extensions ( options ) do
198234 options
199- |> Keyword . update ( :audio_rtp_hdr_exts , [ ] , fn exts ->
200- exts ++ @ mandatory_audio_rtp_hdr_exts
235+ |> Keyword . update ( :audio_rtp_hdr_exts , % { } , fn exts ->
236+ Map . merge ( exts , @ mandatory_audio_rtp_hdr_exts )
201237 end )
202- |> Keyword . update ( :video_rtp_hdr_exts , [ ] , fn exts ->
203- exts ++ @ mandatory_video_rtp_hdr_exts
238+ |> Keyword . update ( :video_rtp_hdr_exts , % { } , fn exts ->
239+ Map . merge ( exts , @ mandatory_video_rtp_hdr_exts )
204240 end )
205241 end
206242
207243 defp resolve_rtp_hdr_extensions ( options ) do
208244 { audio_exts , video_exts } =
209245 Keyword . get ( options , :rtp_hdr_extensions , [ ] )
210- |> Enum . reduce ( { [ ] , [ ] } , fn ext , { audio_exts , video_exts } ->
246+ |> Enum . reduce ( { % { } , % { } } , fn ext , { audio_exts , video_exts } ->
211247 resolved_ext = Map . fetch! ( @ rtp_hdr_extensions , ext )
212248
213249 case resolved_ext . media_type do
214250 :audio ->
215- { [ resolved_ext . ext | audio_exts ] , video_exts }
251+ audio_exts = Map . put ( audio_exts , resolved_ext . ext . uri , resolved_ext . ext )
252+ { audio_exts , video_exts }
216253
217254 :all ->
218- { [ resolved_ext . ext | audio_exts ] , [ resolved_ext . ext | video_exts ] }
255+ audio_exts = Map . put ( audio_exts , resolved_ext . ext . uri , resolved_ext . ext )
256+ video_exts = Map . put ( video_exts , resolved_ext . ext . uri , resolved_ext . ext )
257+ { audio_exts , video_exts }
219258 end
220259 end )
221260
222261 options
223- |> Keyword . put ( :audio_rtp_hdr_exts , Enum . reverse ( audio_exts ) )
224- |> Keyword . put ( :video_rtp_hdr_exts , Enum . reverse ( video_exts ) )
262+ |> Keyword . put ( :audio_rtp_hdr_exts , audio_exts )
263+ |> Keyword . put ( :video_rtp_hdr_exts , video_exts )
225264 |> Keyword . delete ( :rtp_hdr_extensions )
226265 end
227266
0 commit comments