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 10, 2024
1 parent cf8564f commit f1ed5f9
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 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 All @@ -45,6 +46,12 @@
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',
'end-of-candidates', 'ice-options')
ICE_ATTRS_RM += ('extmap', 'extmap-allow-mixed', 'msid',
'msid-semantic', 'mid', 'group', 'rtcp-fb')

try:
strtypes = (str, unicode)
except NameError:
Expand Down Expand Up @@ -217,6 +224,64 @@ def sdp_sect_fin(self, sdp_bc, sect):
sect.addHeader('a', F'setup:{self.dtls_mode}')
sect.addHeader('a', F'fingerprint:{self.dtls_fingerprint}')

class subcommand_ice(subcommand):
name = 'ICE'
ice_lufrag: str
ice_lpwd: str
ice_candidates: list
i_mod = subcommand.mod_2_i_mod['ice_lite']

def __init__(self):
super().__init__(f'{self.i_mod} S')

def handle_results(self, results, ur):
if ex:=super().handle_results(results, ur): return ex
ice_res = results[0].split()
if len(ice_res) < 3:
return RtpProxyError(f'RTPProxy errored: {self.name}: {results=}')
self.ice_lufrag, self.ice_lpwd = ice_res[:2]
self.ice_candidates = [unquote(r[2:]) for r in ice_res[2:]]
ur.sdp_sect_fins.append(self.sdp_sect_fin)
return None

def sdp_sect_fin(self, sdp_bc, sect):
self._sdp_attrs_rm(sdp_bc, sect, ICE_ATTRS_RM)
sect.addHeader('a', 'rtcp-mux')
sect.addHeader('a', 'ice-lite')
sect.addHeader('a', F'ice-ufrag:{self.ice_lufrag}')
sect.addHeader('a', F'ice-pwd:{self.ice_lpwd}')
for cand in self.ice_candidates:
sect.addHeader('a', F'candidate:{cand}')
sect.m_header.transport = 'UDP/TLS/RTP/SAVP'

class subcommand_deice(subcommand):
name = 'deICE'
i_mod = subcommand.mod_2_i_mod['ice_lite']

def __init__(self, sdp_bc, sect):
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')
super().__init__(f'{self.i_mod} A {adict["ice-ufrag"]} {adict["ice-pwd"]}')
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
self.append(f'{self.i_mod} C {cand}')

def handle_results(self, results, ur):
if ex:=super().handle_results(results, ur): return ex
if any(true for r in results if r != '0'):
return RtpProxyError(f'RTPProxy errored: {self.name}: {results=}')
ur.sdp_sect_fins.append(self.sdp_sect_fin)
return None

def sdp_sect_fin(self, sdp_bc, sect):
self._sdp_attrs_rm(sdp_bc, sect, ICE_ATTRS_RM)

class subcommand_dedtls(subcommand):
name = 'deDTLS'
i_mod = subcommand.mod_2_i_mod['dtls_gw']
Expand Down Expand Up @@ -276,6 +341,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 @@ -416,8 +482,12 @@ def _on_sdp_change(self, rtpps, sdp_body, result_callback, en_excpt):
up.index = si
if otherside.gateway_dtls == 'dtls':
up.subcommands.append(subcommand_dtls())
if otherside.deice:
up.subcommands.append(subcommand_ice())
if self.gateway_dtls == 'dtls' and sect.m_header.transport in DTLS_TRANSPORTS:
up.subcommands.append(subcommand_dedtls(sdp_bc, sect))
if self.deice:
up.subcommands.append(subcommand_deice(sdp_bc, sect))
up.result_callback = partial(self._sdp_change_finish, sdp_body, sect, sects, result_callback)
self.update(up)
return
Expand Down

0 comments on commit f1ed5f9

Please sign in to comment.