Skip to content

Commit

Permalink
Add some basic support for offering ice-lite using rtpproxy's
Browse files Browse the repository at this point in the history
module with the same name.
  • Loading branch information
sobomax committed Jul 4, 2024
1 parent c2d73d6 commit 052c20c
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions sippy/Rtp_proxy_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from hashlib import md5
from random import random
from time import time
from urllib.parse import unquote
try:
from _thread import get_ident
except ImportError:
Expand Down Expand Up @@ -104,11 +105,17 @@ class update_result():
sendonly = None
dtls_mode = None
dtls_fingerprint = None
ice_lufrag = None
ice_lpwd = None
ice_candidates = None

DTLS_TRANSPORTS = ('UDP/TLS/RTP/SAVP', 'UDP/TLS/RTP/SAVPF')
DTLS_ATTRS = ('setup', 'fingerprint', 'rtcp', 'ssrc')
DTLS_ATTRS_RM = tuple([a for a in DTLS_ATTRS if a != 'ssrc'])

ICE_ATTRS = ('ice-ufrag', 'ice-pwd')
ICE_ATTRS_RM = ICE_ATTRS + ('candidate', 'ice-options', 'rtcp-mux')

class _rtpps_side(object):
session_exists = False
codecs = None
Expand All @@ -118,6 +125,7 @@ class _rtpps_side(object):
oh_remote = None
repacketize = None
gateway_dtls = 'pass'
deice = False
soft_repacketize = False
after_sdp_change = None
needs_new_port = False
Expand Down Expand Up @@ -194,11 +202,16 @@ def update_result(self, result, up):
return
ur = update_result()
if len(t0) > 1:
subc_res = t0[1].lstrip()
if subc_res == '-1':
subc_ress = t0[1].split('&&')
dtls_res = subc_ress.pop(0).strip()
if dtls_res == '-1':
up.result_callback(None, up.rtpps, *up.callback_parameters)
return
ur.dtls_mode, ur.dtls_fingerprint = subc_res.split(' ', 1)
ur.dtls_mode, ur.dtls_fingerprint = dtls_res.split(' ', 1)
if len(subc_ress) > 0:
ice_res = subc_ress.pop(0).strip().split()
ur.ice_lufrag, ur.ice_lpwd = ice_res[:2]
ur.ice_candidates = [unquote(r[2:]) for r in ice_res[2:]]
ur.rtpproxy_port = int(t1[0])
if ur.rtpproxy_port == 0:
up.result_callback(None, up.rtpps, *up.callback_parameters)
Expand Down Expand Up @@ -297,6 +310,8 @@ def _on_sdp_change(self, rtpps, sdp_body, result_callback, en_excpt):
if otherside.gateway_dtls == 'dtls':
up.subcommands.append('M4:1 S')
otherside.transports[si] = 'UDP/TLS/RTP/SAVP'
if otherside.deice:
up.subcommands.append('M5:1 S')
if self.gateway_dtls == 'dtls' and sect.m_header.transport in DTLS_TRANSPORTS:
adict = dict([(x.name, x.value) for x in sect.a_headers
if x.name in DTLS_ATTRS])
Expand All @@ -318,6 +333,20 @@ def _on_sdp_change(self, rtpps, sdp_body, result_callback, en_excpt):
up.subcommands.append(subcommand)
if otherside.gateway_dtls == 'rtp':
otherside.transports[si] = 'RTP/AVP'
if self.deice:
adict = dict([(x.name, x.value) for x in sect.a_headers
if x.name in ICE_ATTRS])
for rattr in ('ice-ufrag', 'ice-pwd'):
if rattr not in adict:
raise SdpParseError(f'Missing ICE {rattr} parameter')
subcommand = f'M5:1 A {adict["ice-ufrag"]} {adict["ice-pwd"]}'
up.subcommands.append(subcommand)
for cand in (x.value for x in sect.a_headers
if x.name == 'candidate'):
pts = cand.split(None, 4)
if pts[2].lower() != 'udp':
continue
up.subcommands.append(f'M5:1 C {cand}')
up.callback_parameters = (sdp_body, sect, sects, result_callback)
self.update(up)
return
Expand All @@ -335,11 +364,23 @@ def _sdp_change_finish(self, ur, rtpps, sdp_body, sect, sects, result_callback):
for dtls_hdr in [x for x in sect.a_headers
if x.name in DTLS_ATTRS_RM]:
sect.a_headers.remove(dtls_hdr)
if self.deice:
for ice_hdr in [x for x in sect.a_headers if x.name in ICE_ATTRS_RM]:
sect.a_headers.remove(ice_hdr)
if ur.dtls_fingerprint:
for dtls_hdr in [x for x in sect.a_headers if x.name in DTLS_ATTRS_RM]:
sect.a_headers.remove(dtls_hdr)
sect.addHeader('a', F'setup:{ur.dtls_mode}')
sect.addHeader('a', F'fingerprint:{ur.dtls_fingerprint}')
if ur.ice_lufrag:
for ice_hdr in [x for x in sect.a_headers if x.name in ICE_ATTRS_RM]:
sect.a_headers.remove(ice_hdr)
sect.addHeader('a', 'rtcp-mux');
sect.addHeader('a', 'ice-lite')
sect.addHeader('a', F'ice-ufrag:{ur.ice_lufrag}')
sect.addHeader('a', F'ice-pwd:{ur.ice_lpwd}')
for cand in ur.ice_candidates:
sect.addHeader('a', F'candidate:{cand}')
sect.c_header.atype = ur.family
sect.c_header.addr = ur.rtpproxy_address
if sect.m_header.port != 0:
Expand Down

0 comments on commit 052c20c

Please sign in to comment.