Search in sources :

Example 1 with AttributeField

use of gov.nist.javax.sdp.fields.AttributeField in project jain-slee.diameter by RestComm.

the class SDPConverter method convertSDP.

/**
 * Returns the value of the Media-Component-Description[] AVP, of type Grouped.
 */
public static MediaComponentDescription[] convertSDP(GqProvider gqProvider, String sourceSDP, String destinationSDP) throws SdpException, SdpParseException {
    SdpFactory sdpFactory = SdpFactory.getInstance();
    SessionDescription sourceSessionDescription = sdpFactory.createSessionDescription(sourceSDP);
    SessionDescription destinationSessionDescription = sdpFactory.createSessionDescription(destinationSDP);
    Vector<MediaDescription> sourceMediaDescriptions = sourceSessionDescription.getMediaDescriptions(false);
    Vector<MediaDescription> destinationMediaDescriptions = destinationSessionDescription.getMediaDescriptions(false);
    Vector<AttributeField> attributes;
    Vector<BandwidthField> bandwidth;
    Vector<MediaComponentDescription> mediaComponentDescriptions = new Vector<MediaComponentDescription>();
    Vector<MediaSubComponent[]> mediaSubComponents = new Vector<MediaSubComponent[]>();
    GqAvpFactory avpFactory = gqProvider.getGqAvpFactory();
    MediaComponentDescription currDescription = null;
    Media currMedia;
    String mediaType;
    String codecLine;
    String mode;
    if (sourceMediaDescriptions == null)
        throw new SdpParseException(0, 0, "no source media found");
    if (destinationMediaDescriptions == null)
        throw new SdpParseException(0, 0, "no destination media found");
    String proto;
    int dataPort;
    int rtcpPort;
    int ports;
    int step;
    String dataAddress;
    String rtcpAddress;
    MediaSubComponent currSubComponent;
    IPFilterRule currFlowDescription;
    String[] rtcpValues;
    for (int i = 0; i < sourceMediaDescriptions.size(); i++) {
        attributes = sourceMediaDescriptions.get(i).getAttributes(false);
        bandwidth = sourceMediaDescriptions.get(i).getBandwidths(false);
        currMedia = sourceMediaDescriptions.get(i).getMedia();
        currDescription = avpFactory.createMediaComponentDescription();
        mediaType = currMedia.getMediaType().toLowerCase();
        if (mediaType.equals("audio"))
            currDescription.setMediaType(MediaType.AUDIO);
        else if (mediaType.equals("video"))
            currDescription.setMediaType(MediaType.VIDEO);
        else if (mediaType.equals("control"))
            currDescription.setMediaType(MediaType.CONTROL);
        else if (mediaType.equals("application"))
            currDescription.setMediaType(MediaType.APPLICATION);
        else if (mediaType.equals("message"))
            currDescription.setMediaType(MediaType.MESSAGE);
        else if (mediaType.equals("data"))
            currDescription.setMediaType(MediaType.DATA);
        else if (mediaType.equals("text"))
            currDescription.setMediaType(MediaType.TEXT);
        else
            currDescription.setMediaType(MediaType.OTHER);
        codecLine = "downlink" + System.getProperty("line.separator");
        codecLine = codecLine + "offer" + System.getProperty("line.separator");
        codecLine = codecLine + currMedia.toString() + System.getProperty("line.separator");
        mode = "";
        dataPort = currMedia.getMediaPort();
        rtcpPort = currMedia.getMediaPort() + 1;
        if (sourceMediaDescriptions.get(i).getConnection() != null)
            rtcpAddress = sourceMediaDescriptions.get(i).getConnection().getAddress();
        else
            rtcpAddress = sourceSessionDescription.getConnection().getAddress();
        dataAddress = rtcpAddress;
        proto = currMedia.getProtocol().toLowerCase();
        ports = 1;
        step = 2;
        if (currMedia.getPortCount() > 0)
            ports = currMedia.getPortCount();
        if (attributes != null)
            for (int j = 0; j < attributes.size(); j++) if (attributes.get(j).getName().equals("recvonly"))
                mode = "recvonly";
            else if (attributes.get(j).getName().equals("sendonly"))
                mode = "sendonly";
            else if (attributes.get(j).getName().equals("sendrecv"))
                mode = "sendrecv";
            else if (attributes.get(j).getName().equals("inactive"))
                mode = "inactive";
            else if (attributes.get(j).getName().equals("rtcp")) {
                rtcpValues = attributes.get(j).getValue().split(" ");
                rtcpPort = Integer.parseInt(rtcpValues[0]);
                if (rtcpPort != dataPort + 1)
                    step = 1;
                if (rtcpValues.length == 3)
                    rtcpAddress = rtcpValues[2];
            } else
                codecLine += attributes.get(j).toString() + System.getProperty("line.separator");
        if (proto.equals("rtp/avp")) {
            mediaSubComponents.add(new MediaSubComponent[ports * 2]);
            // 2 flows
            if (!mode.equals("recvonly")) {
                for (int j = 0; j < ports; j++) {
                    currSubComponent = avpFactory.createMediaSubComponent();
                    currSubComponent.setFlowNumber(1 + j * ports);
                    currFlowDescription = new IPFilterRule("permit in 17 from " + dataAddress + " " + (dataPort + j * step) + " to any");
                    currSubComponent.setFlowDescription(currFlowDescription);
                    mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
                }
            }
            for (int j = 0; j < ports; j++) {
                currSubComponent = avpFactory.createMediaSubComponent();
                currSubComponent.setFlowNumber(2 + j * ports);
                currSubComponent.setFlowUsage(FlowUsage.RTCP);
                currFlowDescription = new IPFilterRule("permit in 17 from " + rtcpAddress + " " + (rtcpPort + j * step) + " to any");
                currSubComponent.setFlowDescription(currFlowDescription);
                mediaSubComponents.get(i)[1 + j * ports] = currSubComponent;
            }
        } else if (proto.equals("udp")) {
            // 1 flow
            if (!mode.equals("recvonly")) {
                for (int j = 0; j < ports; j++) {
                    currSubComponent = avpFactory.createMediaSubComponent();
                    currSubComponent.setFlowNumber(1 + j * ports);
                    currFlowDescription = new IPFilterRule("permit in 17 from " + dataAddress + " " + (dataPort + j * step) + " to any");
                    currSubComponent.setFlowDescription(currFlowDescription);
                    mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
                }
            }
        } else if (proto.equals("rtp/avp-tcp") || proto.equals("tcp/rtp/avp")) {
            // 2 flows
            if (!mode.equals("recvonly")) {
                for (int j = 0; j < ports; j++) {
                    currSubComponent = avpFactory.createMediaSubComponent();
                    currSubComponent.setFlowNumber(1 + j * ports);
                    currFlowDescription = new IPFilterRule("permit in 6 from " + dataAddress + " " + (dataPort + j * step) + " to any");
                    currSubComponent.setFlowDescription(currFlowDescription);
                    mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
                }
            }
            for (int j = 0; j < ports; j++) {
                currSubComponent = avpFactory.createMediaSubComponent();
                currSubComponent.setFlowNumber(2 + j * ports);
                currSubComponent.setFlowUsage(FlowUsage.RTCP);
                currFlowDescription = new IPFilterRule("permit in 6 from " + rtcpAddress + " " + (rtcpPort + j * step) + " to any");
                currSubComponent.setFlowDescription(currFlowDescription);
                mediaSubComponents.get(i)[1 + j * ports] = currSubComponent;
            }
        } else if (proto.equals("tcp")) {
            // 1 flow
            if (!mode.equals("recvonly")) {
                for (int j = 0; j < ports; j++) {
                    currSubComponent = avpFactory.createMediaSubComponent();
                    currSubComponent.setFlowNumber(1 + j * ports);
                    currFlowDescription = new IPFilterRule("permit in 6 from " + dataAddress + " " + (dataPort + j * step) + " to any");
                    currSubComponent.setFlowDescription(currFlowDescription);
                    mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
                }
            }
        } else if (proto.equals("rtp/avp-dccp") || proto.equals("dccp/rtp/avp")) {
            // 2 flows
            if (!mode.equals("recvonly")) {
                for (int j = 0; j < ports; j++) {
                    currSubComponent = avpFactory.createMediaSubComponent();
                    currSubComponent.setFlowNumber(1 + j * ports);
                    currFlowDescription = new IPFilterRule("permit in 33 from " + dataAddress + " " + (dataPort + j * step) + " to any");
                    currSubComponent.setFlowDescription(currFlowDescription);
                    mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
                }
            }
            for (int j = 0; j < ports; j++) {
                currSubComponent = avpFactory.createMediaSubComponent();
                currSubComponent.setFlowNumber(2 + j * ports);
                currSubComponent.setFlowUsage(FlowUsage.RTCP);
                currFlowDescription = new IPFilterRule("permit in 33 from " + rtcpAddress + " " + (rtcpPort + j * step) + " to any");
                currSubComponent.setFlowDescription(currFlowDescription);
                mediaSubComponents.get(i)[1 + j * ports] = currSubComponent;
            }
        } else if (proto.equals("dccp")) {
            // 1 flow
            if (!mode.equals("recvonly")) {
                for (int j = 0; j < ports; j++) {
                    currSubComponent = avpFactory.createMediaSubComponent();
                    currSubComponent.setFlowNumber(1 + j * ports);
                    currFlowDescription = new IPFilterRule("permit in 33 from " + dataAddress + " " + (dataPort + j * step) + " to any");
                    currSubComponent.setFlowDescription(currFlowDescription);
                    mediaSubComponents.get(i)[0 + j * ports] = currSubComponent;
                }
            }
        } else
            throw new SdpParseException(0, 0, "protocol not supported " + proto);
        if (bandwidth != null)
            for (int j = 0; j < bandwidth.size(); j++) if (bandwidth.get(j).getType().equals("AS"))
                currDescription.setMaxRequestedBandwidthDL(bandwidth.get(j).getValue());
            else if (bandwidth.get(j).getType().equals("RR"))
                currDescription.setRRBandwidth(bandwidth.get(j).getValue());
            else if (bandwidth.get(j).getType().equals("RS"))
                currDescription.setRSBandwidth(bandwidth.get(j).getValue());
            else
                codecLine += bandwidth.get(j).toString() + System.getProperty("line.separator");
        currDescription.setCodecData(codecLine.getBytes());
        if (currMedia.getMediaPort() == 0)
            currDescription.setFlowStatus(FlowStatus.REMOVED);
        else if (mode.equals("recvonly"))
            currDescription.setFlowStatus(FlowStatus.ENABLED_DOWNLINK);
        else if (mode.equals("sendonly"))
            currDescription.setFlowStatus(FlowStatus.ENABLED_UPLINK);
        else if (mode.equals("inactive"))
            currDescription.setFlowStatus(FlowStatus.DISABLED);
        else
            currDescription.setFlowStatus(FlowStatus.ENABLED);
        currDescription.setMediaComponentNumber(i + 1);
        mediaComponentDescriptions.add(currDescription);
    }
    Boolean found = false;
    for (int i = 0; i < destinationMediaDescriptions.size(); i++) {
        found = false;
        currMedia = destinationMediaDescriptions.get(i).getMedia();
        mediaType = currMedia.getMediaType().toLowerCase();
        attributes = destinationMediaDescriptions.get(i).getAttributes(false);
        bandwidth = destinationMediaDescriptions.get(i).getBandwidths(false);
        codecLine = "uplink" + System.getProperty("line.separator");
        codecLine = codecLine + "answer" + System.getProperty("line.separator");
        codecLine = codecLine + currMedia.toString() + System.getProperty("line.separator");
        for (int j = 0; j < mediaComponentDescriptions.size(); j++) if (mediaComponentDescriptions.get(j).getMediaType().toString().toLowerCase().equals(mediaType)) {
            currDescription = mediaComponentDescriptions.get(j);
            found = true;
            break;
        }
        if (found) {
            dataPort = currMedia.getMediaPort();
            rtcpPort = currMedia.getMediaPort() + 1;
            if (destinationMediaDescriptions.get(i).getConnection() != null)
                rtcpAddress = destinationMediaDescriptions.get(i).getConnection().getAddress();
            else
                rtcpAddress = destinationSessionDescription.getConnection().getAddress();
            dataAddress = rtcpAddress;
            proto = currMedia.getProtocol().toLowerCase();
            ports = 1;
            step = 2;
            if (currMedia.getPortCount() > 0)
                ports = currMedia.getPortCount();
            mode = "";
            if (attributes != null)
                for (int j = 0; j < attributes.size(); j++) if (attributes.get(j).getName().equals("recvonly"))
                    mode = "recvonly";
                else if (attributes.get(j).getName().equals("sendonly"))
                    mode = "sendonly";
                else if (attributes.get(j).getName().equals("sendrecv"))
                    mode = "sendrecv";
                else if (attributes.get(j).getName().equals("inactive"))
                    mode = "inactive";
                else if (attributes.get(j).getName().equals("rtcp")) {
                    rtcpValues = attributes.get(j).getValue().split(" ");
                    rtcpPort = Integer.parseInt(rtcpValues[0]);
                    if (rtcpPort != dataPort + 1)
                        step = 1;
                    if (rtcpValues.length == 3)
                        rtcpAddress = rtcpValues[2];
                } else
                    codecLine += attributes.get(j).toString() + System.getProperty("line.separator");
            if (proto.equals("rtp/avp")) {
                // 2 flows
                if (!mode.equals("sendonly")) {
                    for (int j = 0; j < ports; j++) {
                        currFlowDescription = new IPFilterRule("permit out 17 from any to " + dataAddress + " " + (dataPort + j * step));
                        mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
                    }
                }
                for (int j = 0; j < ports; j++) {
                    currFlowDescription = new IPFilterRule("permit out 17 from any to " + rtcpAddress + " " + (rtcpPort + j * step));
                    mediaSubComponents.get(i)[1 + j * ports].setFlowDescription(currFlowDescription);
                }
            } else if (proto.equals("udp")) {
                // 1 flow
                if (!mode.equals("sendonly")) {
                    for (int j = 0; j < ports; j++) {
                        currFlowDescription = new IPFilterRule("permit out 17 from any to " + dataAddress + " " + (dataPort + j * step));
                        mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
                    }
                }
            } else if (proto.equals("rtp/avp-tcp") || proto.equals("tcp/rtp/avp")) {
                // 2 flows
                if (!mode.equals("sendonly")) {
                    for (int j = 0; j < ports; j++) {
                        currFlowDescription = new IPFilterRule("permit out 6 from any to " + dataAddress + " " + (dataPort + j * step));
                        mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
                    }
                }
                for (int j = 0; j < ports; j++) {
                    currFlowDescription = new IPFilterRule("permit out 6 from any to " + rtcpAddress + " " + (rtcpPort + j * step));
                    mediaSubComponents.get(i)[1 + j * ports].setFlowDescription(currFlowDescription);
                }
            } else if (proto.equals("tcp")) {
                // 1 flow
                if (!mode.equals("sendonly")) {
                    for (int j = 0; j < ports; j++) {
                        currFlowDescription = new IPFilterRule("permit out 6 from any to " + dataAddress + " " + (dataPort + j * step));
                        mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
                    }
                }
            } else if (proto.equals("rtp/avp-dccp") || proto.equals("dccp/rtp/avp")) {
                // 2 flows
                if (!mode.equals("sendonly")) {
                    for (int j = 0; j < ports; j++) {
                        currFlowDescription = new IPFilterRule("permit out 33 from any to " + dataAddress + " " + (dataPort + j * step));
                        mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
                    }
                }
                for (int j = 0; j < ports; j++) {
                    currFlowDescription = new IPFilterRule("permit out 33 from any to " + rtcpAddress + " " + (rtcpPort + j * step));
                    mediaSubComponents.get(i)[1 + j * ports].setFlowDescription(currFlowDescription);
                }
            } else if (proto.equals("dccp")) {
                // 1 flow
                if (!mode.equals("sendonly")) {
                    for (int j = 0; j < ports; j++) {
                        currFlowDescription = new IPFilterRule("permit out 33 from any to " + dataAddress + " " + (dataPort + j * step));
                        mediaSubComponents.get(i)[0 + j * ports].setFlowDescription(currFlowDescription);
                    }
                }
            } else
                throw new SdpParseException(0, 0, "protocol not supported " + proto);
            if (bandwidth != null)
                for (int j = 0; j < bandwidth.size(); j++) if (bandwidth.get(j).getType().equals("AS"))
                    currDescription.setMaxRequestedBandwidthUL(bandwidth.get(j).getValue());
                else if (bandwidth.get(j).getType().equals("RR")) {
                // do nothing
                } else if (bandwidth.get(j).getType().equals("RS")) {
                // do nothing
                } else
                    codecLine += bandwidth.get(j).toString() + System.getProperty("line.separator");
            currDescription.setMediaSubComponents(mediaSubComponents.get(i));
            currDescription.setCodecData(codecLine.getBytes());
        }
    }
    return mediaComponentDescriptions.toArray(new MediaComponentDescription[mediaComponentDescriptions.size()]);
}
Also used : SessionDescription(javax.sdp.SessionDescription) IPFilterRule(net.java.slee.resource.diameter.base.events.avp.IPFilterRule) MediaDescription(javax.sdp.MediaDescription) AttributeField(gov.nist.javax.sdp.fields.AttributeField) Media(javax.sdp.Media) GqAvpFactory(net.java.slee.resource.diameter.gq.GqAvpFactory) MediaComponentDescription(net.java.slee.resource.diameter.gq.events.avp.MediaComponentDescription) SdpParseException(javax.sdp.SdpParseException) MediaSubComponent(net.java.slee.resource.diameter.gq.events.avp.MediaSubComponent) BandwidthField(gov.nist.javax.sdp.fields.BandwidthField) SdpFactory(javax.sdp.SdpFactory) Vector(java.util.Vector)

Aggregations

AttributeField (gov.nist.javax.sdp.fields.AttributeField)1 BandwidthField (gov.nist.javax.sdp.fields.BandwidthField)1 Vector (java.util.Vector)1 Media (javax.sdp.Media)1 MediaDescription (javax.sdp.MediaDescription)1 SdpFactory (javax.sdp.SdpFactory)1 SdpParseException (javax.sdp.SdpParseException)1 SessionDescription (javax.sdp.SessionDescription)1 IPFilterRule (net.java.slee.resource.diameter.base.events.avp.IPFilterRule)1 GqAvpFactory (net.java.slee.resource.diameter.gq.GqAvpFactory)1 MediaComponentDescription (net.java.slee.resource.diameter.gq.events.avp.MediaComponentDescription)1 MediaSubComponent (net.java.slee.resource.diameter.gq.events.avp.MediaSubComponent)1