Example #1
0
def content_to_sdpstream(content):
    if content.description is None:
        raise ValueError('Missing media description')
    if content.transport is None:
        raise ValueError('Missing media transport')
    media_stream = SDPMediaStream(str(content.description.media), 0, 'RTP/AVP')
    formats = []
    attributes = []
    for item in content.description.payloads:
        formats.append(item.id)
        attributes.append(SDPAttribute('rtpmap', '%d %s/%d' % (item.id, str(item.name), item.clockrate)))
        if item.maxptime:
            attributes.append(SDPAttribute('maxptime', str(item.maxptime)))
        if item.ptime:
            attributes.append(SDPAttribute('ptime', str(item.ptime)))
        if item.parameters:
            parameters_str = ';'.join(('%s=%s' % (p.name, p.value) for p in item.parameters))
            attributes.append(SDPAttribute('fmtp', '%d %s' % (item.id, str(parameters_str))))
    media_stream.formats = map(str, formats)
    media_stream.attributes = attributes  # set attributes so that _codec_list is generated
    if content.description.encryption:
        if content.description.encryption.required:
            media_stream.transport = 'RTP/SAVP'
        for crypto in content.description.encryption.cryptos:
            crypto_str = '%s %s %s' % (crypto.tag, crypto.crypto_suite, crypto.key_params)
            if crypto.session_params:
                crypto_str += ' %s' % crypto.session_params
            media_stream.attributes.append(SDPAttribute('crypto', str(crypto_str)))
    if isinstance(content.transport, jingle.IceUdpTransport):
        if content.transport.ufrag:
            media_stream.attributes.append(SDPAttribute('ice-ufrag', str(content.transport.ufrag)))
        if content.transport.password:
            media_stream.attributes.append(SDPAttribute('ice-pwd', str(content.transport.password)))
        for candidate in content.transport.candidates:
            if not ipv4_re.match(candidate.ip):
                continue
            candidate_str = '%s %d %s %d %s %d typ %s' % (candidate.foundation, candidate.component, candidate.protocol.upper(), candidate.priority, candidate.ip, candidate.port, candidate.typ)
            if candidate.related_addr and candidate.related_port:
                candidate_str += ' raddr %s rport %d' % (candidate.related_addr, candidate.related_port)
            media_stream.attributes.append(SDPAttribute('candidate', str(candidate_str)))
        if content.transport.remote_candidate:
            remote_candidate = content.transport.remote_candidate
            remote_candidates_str = '%d %s %d' % (remote_candidate.component, remote_candidate.ip, remote_candidate.port)
            media_stream.attributes.append(SDPAttribute('remote-candidates', str(remote_candidates_str)))
    elif isinstance(content.transport, jingle.RawUdpTransport):
        # Nothing to do here
        pass
    else:
        raise ValueError
    # Set the proper connection information, pick the first RTP candidate and use that
    try:
        candidate = next(c for c in content.transport.candidates if c.component == 1 and ipv4_re.match(c.ip))
    except StopIteration:
        raise ValueError
    media_stream.connection = SDPConnection(str(candidate.ip))
    media_stream.port = candidate.port

    return media_stream
Example #2
0
def content_to_sdpstream(content):
    if content.description is None or content.transport is None:
        raise ValueError
    media_stream = SDPMediaStream(str(content.description.media), 0, 'RTP/AVP')
    formats = []
    attributes = []
    for item in content.description.payloads:
        formats.append(item.id)
        attributes.append(SDPAttribute('rtpmap', '%d %s/%d' % (item.id, str(item.name), item.clockrate)))
        if item.maxptime:
            attributes.append(SDPAttribute('maxptime', str(item.maxptime)))
        if item.ptime:
            attributes.append(SDPAttribute('ptime', str(item.ptime)))
        if item.parameters:
            parameters_str = ';'.join(('%s=%s' % (p.name, p.value) for p in item.parameters))
            attributes.append(SDPAttribute('fmtp', '%d %s' % (item.id, str(parameters_str))))
    media_stream.formats = map(str, formats)
    media_stream.attributes = attributes  # set attributes so that _codec_list is generated
    if content.description.encryption:
        if content.description.encryption.required:
            media_stream.transport = 'RTP/SAVP'
        for crypto in content.description.encryption.cryptos:
            crypto_str = '%s %s %s' % (crypto.tag, crypto.crypto_suite, crypto.key_params)
            if crypto.session_params:
                crypto_str += ' %s' % crypto.session_params
            media_stream.attributes.append(SDPAttribute('crypto', str(crypto_str)))
    if isinstance(content.transport, jingle.IceUdpTransport):
        if content.transport.ufrag:
            media_stream.attributes.append(SDPAttribute('ice-ufrag', str(content.transport.ufrag)))
        if content.transport.password:
            media_stream.attributes.append(SDPAttribute('ice-pwd', str(content.transport.password)))
        for candidate in content.transport.candidates:
            if not ipv4_re.match(candidate.ip):
                continue
            candidate_str = '%s %d %s %d %s %d typ %s' % (candidate.foundation, candidate.component, candidate.protocol.upper(), candidate.priority, candidate.ip, candidate.port, candidate.typ)
            if candidate.related_addr and candidate.related_port:
                candidate_str += ' raddr %s rport %d' % (candidate.related_addr, candidate.related_port)
            media_stream.attributes.append(SDPAttribute('candidate', str(candidate_str)))
        if content.transport.remote_candidate:
            remote_candidate = content.transport.remote_candidate
            remote_candidates_str = '%d %s %d' % (remote_candidate.component, remote_candidate.ip, remote_candidate.port)
            media_stream.attributes.append(SDPAttribute('remote-candidates', str(remote_candidates_str)))
    elif isinstance(content.transport, jingle.RawUdpTransport):
        # Nothing to do here
        pass
    else:
        raise ValueError
    # Set the proper connection information, pick the first RTP candidate and use that
    try:
        candidate = next(c for c in content.transport.candidates if c.component == 1 and ipv4_re.match(c.ip))
    except StopIteration:
        raise ValueError
    media_stream.connection = SDPConnection(str(candidate.ip))
    media_stream.port = candidate.port

    return media_stream