Search in sources :

Example 6 with Codec

use of org.red5.app.sip.codecs.Codec in project bigbluebutton by bigbluebutton.

the class SdpUtils method makeAttributeNegotiation.

/**
     * Here we make the negotiation of all attributes besides "rtpmap" (
     * these are negotiated on "makeMediaPayloadsNegotiation" method).
     * 
     * @param newSdp
     * @param localMedia
     * @param remoteAttribute
     */
private static void makeAttributeNegotiation(SessionDescriptor newSdp, MediaDescriptor localMedia, AttributeField remoteAttribute) {
    try {
        if (remoteAttribute.getAttributeName().equals(Codec.ATTRIBUTE_RTPMAP)) {
            log.info("\"rtpmap\" attributes were already negotiated.");
        } else if (!isPayloadRelatedAttribute(remoteAttribute)) {
            // We do nothing with attributes that are not payload 
            // related, like: "ptime", "direction", etc.
            // For now, we consider that they don't demand negotiation.                
            log.info("Attribute is not payload related. Do not negotiate it...");
        } else {
            String payloadId = getPayloadIdFromAttribute(remoteAttribute);
            if ("".equals(payloadId)) {
                log.error("Payload id not found on attribute: Name = [" + remoteAttribute.getAttributeName() + "], Value = [" + remoteAttribute.getAttributeValue() + "].");
            } else if (findAttributeByPayloadId(Codec.ATTRIBUTE_RTPMAP, payloadId, newSdp.getMediaDescriptor(localMedia.getMedia().getMedia())) != null) {
                // We must be sure this attribute is related with a payload 
                // already present on newSdp.                    
                //                    log.debug("Payload " + payloadId + " present on newSdp.");
                AttributeField localAttribute = findAttributeByPayloadId(remoteAttribute.getAttributeName(), payloadId, localMedia);
                Codec sipCodec = CodecFactory.getInstance().getSIPAudioCodec(Integer.valueOf(payloadId));
                if (sipCodec != null) {
                    String localAttibuteValue = "";
                    if (localAttribute != null) {
                        localAttibuteValue = localAttribute.getAttributeValue();
                    } else {
                        log.info("Attribute not found on local media.");
                    }
                    String attributeValueResult = sipCodec.codecNegotiateAttribute(remoteAttribute.getAttributeName(), localAttibuteValue, remoteAttribute.getAttributeValue());
                    if ((attributeValueResult != null) && (!"".equals(attributeValueResult))) {
                        AttributeField af = new AttributeField(remoteAttribute.getAttributeName(), attributeValueResult);
                        MediaDescriptor md = newSdp.getMediaDescriptor(localMedia.getMedia().getMedia());
                        md.addAttribute(af);
                    }
                } else {
                    log.warn("Codec not found!");
                }
            }
        }
    } catch (Exception exception) {
        log.error("Failure creating initial SDP: " + exception.toString());
    }
}
Also used : Codec(org.red5.app.sip.codecs.Codec) AttributeField(org.zoolu.sdp.AttributeField) MediaDescriptor(org.zoolu.sdp.MediaDescriptor)

Example 7 with Codec

use of org.red5.app.sip.codecs.Codec in project bigbluebutton by bigbluebutton.

the class SdpUtils method createInitialSdp.

/**
     * 
     * @param userName
     * @param viaAddress
     * 
     * @return Return the initial local SDP.
     */
public static SessionDescriptor createInitialSdp(String userName, String viaAddress, int audioPort, int videoPort, String audioCodecsPrecedence) {
    SessionDescriptor initialDescriptor = null;
    try {
        //            log.debug("userName = [" + userName + "], viaAddress = [" + viaAddress + 
        //                    "], audioPort = [" + audioPort + "], videoPort = [" + videoPort + 
        //                    "], audioCodecsPrecedence = [" + audioCodecsPrecedence + "]." );
        int audioCodecsNumber = CodecFactory.getInstance().getAvailableAudioCodecsCount();
        int videoCodecsNumber = CodecFactory.getInstance().getAvailableVideoCodecsCount();
        if ((audioCodecsNumber == 0) && (videoCodecsNumber == 0)) {
            log.debug("audioCodecsNumber = [" + audioCodecsNumber + "], videoCodecsNumber = [" + videoCodecsNumber + "].");
            return null;
        }
        initialDescriptor = new SessionDescriptor(userName, viaAddress);
        if (initialDescriptor == null) {
            log.error("Error instantiating the initialDescriptor!");
            return null;
        }
        if (audioCodecsNumber > 0) {
            Codec[] audioCodecs;
            Vector audioAttributes = new Vector();
            if (audioCodecsPrecedence.isEmpty()) {
                audioCodecs = CodecFactory.getInstance().getAvailableAudioCodecs();
            } else {
                audioCodecs = CodecFactory.getInstance().getAvailableAudioCodecsWithPrecedence(audioCodecsPrecedence);
            }
            for (int audioIndex = 0; audioIndex < audioCodecsNumber; audioIndex++) {
                String payloadId = String.valueOf(audioCodecs[audioIndex].getCodecId());
                String rtpmapParamValue = payloadId;
                rtpmapParamValue += " " + audioCodecs[audioIndex].getCodecName();
                rtpmapParamValue += "/" + audioCodecs[audioIndex].getSampleRate() + "/1";
                //                    log.debug("Adding rtpmap for payload [" + payloadId + 
                //                            "] with value = [" + rtpmapParamValue + "]." );
                audioAttributes.add(new AttributeField(Codec.ATTRIBUTE_RTPMAP, rtpmapParamValue));
                String[] codecMediaAttributes = audioCodecs[audioIndex].getCodecMediaAttributes();
                if (codecMediaAttributes != null) {
                    for (int attribIndex = 0; attribIndex < codecMediaAttributes.length; attribIndex++) {
                        //                            log.debug("Adding audio media attribute [" + 
                        //                                    codecMediaAttributes[attribIndex] + "]." );
                        AttributeField newAttribute = parseAttributeField(codecMediaAttributes[attribIndex]);
                        if (newAttribute != null) {
                            audioAttributes.add(newAttribute);
                        }
                    }
                } else {
                    log.warn("Audio codec has no especific media attributes.");
                }
            }
            // Calculate the format list to be used on MediaDescriptor creation.
            String formatList = getFormatList(audioAttributes);
            for (Enumeration attributesEnum = audioAttributes.elements(); attributesEnum.hasMoreElements(); ) {
                AttributeField audioAttribute = (AttributeField) attributesEnum.nextElement();
                if (initialDescriptor.getMediaDescriptor(Codec.MEDIA_TYPE_AUDIO) == null) {
                    //                        log.debug("Creating audio media descriptor." );
                    MediaField mf = new MediaField(Codec.MEDIA_TYPE_AUDIO, audioPort, 0, "RTP/AVP", formatList);
                    initialDescriptor.addMedia(mf, audioAttribute);
                } else {
                    //                        log.debug("Just adding attribute.");
                    initialDescriptor.getMediaDescriptor(Codec.MEDIA_TYPE_AUDIO).addAttribute(audioAttribute);
                }
            }
            String[] commonAudioMediaAttributes = CodecFactory.getInstance().getCommonAudioMediaAttributes();
            if (commonAudioMediaAttributes != null) {
                for (int attribIndex = 0; attribIndex < commonAudioMediaAttributes.length; attribIndex++) {
                    //                        log.debug("Adding common audio media attribute [" + commonAudioMediaAttributes[attribIndex] + "].");
                    AttributeField newAttribute = parseAttributeField(commonAudioMediaAttributes[attribIndex]);
                    if (newAttribute != null) {
                        initialDescriptor.getMediaDescriptor(Codec.MEDIA_TYPE_AUDIO).addAttribute(newAttribute);
                    }
                }
            } else {
                log.debug("No common audio media attributes.");
            }
        }
        if (videoCodecsNumber > 0) {
            Codec[] videoCodecs = CodecFactory.getInstance().getAvailableVideoCodecs();
            Vector videoAttributes = new Vector();
            for (int videoIndex = 0; videoIndex < audioCodecsNumber; videoIndex++) {
                String payloadId = String.valueOf(videoCodecs[videoIndex].getCodecId());
                String rtpmapParamValue = payloadId;
                rtpmapParamValue += " " + videoCodecs[videoIndex].getCodecName();
                rtpmapParamValue += "/" + videoCodecs[videoIndex].getSampleRate() + "/1";
                //                    log.debug("Adding rtpmap for payload [" + payloadId + "] with value = [" + rtpmapParamValue + "].");
                videoAttributes.add(new AttributeField(Codec.ATTRIBUTE_RTPMAP, rtpmapParamValue));
                String[] codecMediaAttributes = videoCodecs[videoIndex].getCodecMediaAttributes();
                if (codecMediaAttributes != null) {
                    for (int attribIndex = 0; attribIndex < codecMediaAttributes.length; attribIndex++) {
                        //                            log.debug("Adding video media attribute [" + codecMediaAttributes[attribIndex] + "].");
                        AttributeField newAttribute = parseAttributeField(codecMediaAttributes[attribIndex]);
                        if (newAttribute != null) {
                            videoAttributes.add(newAttribute);
                        }
                    }
                } else {
                    log.info("Video codec has no especific media attributes.");
                }
            }
            // Calculate the format list to be used on MediaDescriptor creation.
            String formatList = getFormatList(videoAttributes);
            for (Enumeration attributesEnum = videoAttributes.elements(); attributesEnum.hasMoreElements(); ) {
                AttributeField videoAttribute = (AttributeField) attributesEnum.nextElement();
                if (initialDescriptor.getMediaDescriptor(Codec.MEDIA_TYPE_VIDEO) == null) {
                    MediaField mf = new MediaField(Codec.MEDIA_TYPE_VIDEO, audioPort, 0, "RTP/AVP", formatList);
                    initialDescriptor.addMedia(mf, videoAttribute);
                } else {
                    initialDescriptor.getMediaDescriptor(Codec.MEDIA_TYPE_VIDEO).addAttribute(videoAttribute);
                }
            }
            String[] commonVideoMediaAttributes = CodecFactory.getInstance().getCommonAudioMediaAttributes();
            if (commonVideoMediaAttributes != null) {
                for (int attribIndex = 0; attribIndex < commonVideoMediaAttributes.length; attribIndex++) {
                    //                        log.debug("Adding common video media attribute [" + commonVideoMediaAttributes[attribIndex] + "]." );
                    AttributeField newAttribute = parseAttributeField(commonVideoMediaAttributes[attribIndex]);
                    if (newAttribute != null) {
                        initialDescriptor.getMediaDescriptor(Codec.MEDIA_TYPE_VIDEO).addAttribute(newAttribute);
                    }
                }
            } else {
                log.info("No common video media attributes.");
            }
        }
    } catch (Exception exception) {
        log.error("Failure creating initial SDP: " + exception.toString());
    }
    return initialDescriptor;
}
Also used : Codec(org.red5.app.sip.codecs.Codec) Enumeration(java.util.Enumeration) SessionDescriptor(org.zoolu.sdp.SessionDescriptor) AttributeField(org.zoolu.sdp.AttributeField) MediaField(org.zoolu.sdp.MediaField) Vector(java.util.Vector)

Aggregations

Codec (org.red5.app.sip.codecs.Codec)6 AttributeField (org.zoolu.sdp.AttributeField)4 MediaDescriptor (org.zoolu.sdp.MediaDescriptor)4 Enumeration (java.util.Enumeration)2 Vector (java.util.Vector)2 MediaField (org.zoolu.sdp.MediaField)2 SessionDescriptor (org.zoolu.sdp.SessionDescriptor)2 IoBuffer (org.apache.mina.core.buffer.IoBuffer)1 IBroadcastStream (org.red5.server.api.stream.IBroadcastStream)1 IStreamListener (org.red5.server.api.stream.IStreamListener)1 IStreamPacket (org.red5.server.api.stream.IStreamPacket)1 AudioData (org.red5.server.net.rtmp.event.AudioData)1