def createAnswer(streams, offer, **kwargs): '''Create an answer SDP for the remote offer SDP using local (streams) list of media Stream objects.''' s = SDP() s.v = '0' for a in "iep": if a in kwargs: s[a] = kwargs[a] s.o = SDP.originator() s.s = '-' s.t = offer.t s.m = [] streams = list(streams) # so that original list is not modified for your in offer.m: # for each m= line in offer my, i = None, 0 # answered stream while i < len(streams): if streams[ i].media == your.media: # match the first stream in streams my = streams[i].dup() # found, hence del streams[ i] # remove from streams so that we don't match again for another m= found = [] for fy in your.fmt: # all offered formats, find the matching pairs for fm in my.fmt: # the preference order is from offer, hence do for fy, then for fm. try: fmpt, fypt = int(fm.pt), int( fy.pt) # try using numeric payload type except: fmpt = fypt = -1 if 0<=fmpt<32 and 0<=fypt<32 and fmpt == fypt \ or fmpt<0 and fypt<0 and fm.pt == fy.pt \ or str(fm.name).lower() == str(fy.name).lower() and fm.rate == fy.rate and fm.count == fy.count: # we don't match the params found.append((fy, fm)) break if found: # we found some matching formats, put them in my.fmt = map(lambda x: x[0], found) # use remote's fy including fy.pt else: my.fmt = [ format(pt=0) ] # no match in formats, but matched media, must put a format with payload type 0 my.port = 0 # and reset the port. break else: i = i + 1 if not my: # did not match the stream, must put a stream with port = 0 my = SDP.media(str(your)) my.port = 0 s.m.append(my) # append it to our media valid = False for my in s.m: # check if any valid matching stream is present with valid port if my.port != 0: valid = True break return valid and s or None # if no valid matching stream found, return None
def createOffer(streams, previous=None, **kwargs): '''Create an offer SDP using local (streams) list of media Stream objects. If a previous offer/answer SDP is specified then it creates a modified offer. Additionally, the optional keyword arguments such as e and p can be specified.''' s = SDP() s.v = '0' for a in "iep": # add optioanl e and p headers if present if a in kwargs: s[a] = kwargs[a] s.o = SDP.originator(previous and str(previous.o) or None) if previous: s.o.version = s.o.version + 1 s.s = '-' s.t = ['0 0'] # because t= can appear multiple times, it is a list. s.m = streams return s
def createAnswer(streams, offer, **kwargs): '''Create an answer SDP for the remote offer SDP using local (streams) list of media Stream objects.''' s = SDP() s.v = '0' for a in "iep": if a in kwargs: s[a] = kwargs[a] s.o = SDP.originator() s.s = '-' s.t = offer.t s.m = [] streams = list(streams) # so that original list is not modified for your in offer.m: # for each m= line in offer my, i = None, 0 # answered stream while i < len(streams): if streams[i].media == your.media: # match the first stream in streams my = streams[i].dup() # found, hence del streams[i] # remove from streams so that we don't match again for another m= found = [] for fy in your.fmt: # all offered formats, find the matching pairs for fm in my.fmt:# the preference order is from offer, hence do for fy, then for fm. try: fmpt, fypt = int(fm.pt), int(fy.pt) # try using numeric payload type except: fmpt = fypt = -1 if 0<=fmpt<32 and 0<=fypt<32 and fmpt == fypt \ or fmpt<0 and fypt<0 and fm.pt == fy.pt \ or str(fm.name).lower() == str(fy.name).lower() and fm.rate == fy.rate and fm.count == fy.count: # we don't match the params found.append((fy, fm)); break if found: # we found some matching formats, put them in my.fmt = map(lambda x: x[0], found) # use remote's fy including fy.pt else: my.fmt = [format(pt=0)] # no match in formats, but matched media, must put a format with payload type 0 my.port = 0 # and reset the port. break else: i = i + 1 if not my: # did not match the stream, must put a stream with port = 0 my = SDP.media(str(your)) my.port = 0 s.m.append(my) # append it to our media valid = False for my in s.m: # check if any valid matching stream is present with valid port if my.port != 0: valid = True break return valid and s or None # if no valid matching stream found, return None