⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediamanager.java

📁 java 开发的sip软电话 源码 jain sip
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        }
    }

    public void addMediaListener(MediaListener listener)
    {
        try {
            
            listeners.add(listener);
        }
        finally {
            
        }
    }

    InetAddress getLocalHost() throws MediaException
    {
        try {
            
            String hostAddress = Utils.getProperty(
                "net.java.mais.media.IP_ADDRESS");
            InetAddress localhost = null;
            if (hostAddress == null) {
                localhost = NetworkAddressManager.getLocalHost(false);
            }
            else {
                try {
                    localhost = InetAddress.getByName(hostAddress);
                }
                catch (UnknownHostException ex) {
                    throw new MediaException("Failed to create localhost!", ex);
                }
            }

            return localhost;
        }
        finally {
            
        }
    }

    public String generateSdpDescription() throws MediaException
    {
        try {
            
            checkIfStarted();
            try {
                SessionDescription sessDescr = sdpFactory.
                    createSessionDescription();
                //"v=0"
                
                audioPort =  String.valueOf((int) (10000 * Math.random()) + 9999);
                do{
                videoPort =  String.valueOf((int) (10000 * Math.random()) + 9999);                
                }while(audioPort==videoPort);                
                
                //videoPort = "22497";
                //audioPort = "16251";
                
                Version v = sdpFactory.createVersion(0);
                InetSocketAddress publicVideoAddress = NetworkAddressManager.getPublicAddressFor(Integer.parseInt(getVideoPort()));
                InetSocketAddress publicAudioAddress = NetworkAddressManager.getPublicAddressFor(Integer.parseInt(getAudioPort()));
                InetAddress publicIpAddress = publicAudioAddress.getAddress();
                String addrType = publicIpAddress instanceof Inet6Address ?
                    "IP6" : "IP4";

                //spaces in the user name mess everything up.
                //bug report - Alessandro Melzi
                Origin o = sdpFactory.createOrigin(
                    Utils.getProperty("user.name").replace(' ', '_'), 20109217, 2, "IN",
                    addrType, publicIpAddress.getHostAddress());
                //"s=-"
                SessionName s = sdpFactory.createSessionName("<Tudomais>");
                //c=
                Connection c = sdpFactory.createConnection(
                    "IN",
                    addrType,
                    publicIpAddress.getHostAddress());
                //"t=0 0"
                TimeDescription t = sdpFactory.createTimeDescription();
                Vector timeDescs = new Vector();
                timeDescs.add(t);
                //--------Audio media description
                //make sure preferred formats come first
                surfacePreferredEncodings(getReceivableAudioFormats());
                String[] formats = getReceivableAudioFormats();
                MediaDescription am = sdpFactory.createMediaDescription(
                    "audio", publicAudioAddress.getPort(), 1, "RTP/AVP",
                    formats);
                if (!isAudioTransmissionSupported()) {
                    am.setAttribute("recvonly", null);
                    //--------Video media description
                }else{
                	am.setAttribute("sendrecv", null);
                }
                
                am.setAttribute("rtmap:101","telephone-event/"+publicAudioAddress.getPort());
                
                
                surfacePreferredEncodings(getReceivableVideoFormats());
                //"m=video 22222 RTP/AVP 34";
                String[] vformats = getReceivableVideoFormats();
                MediaDescription vm = sdpFactory.createMediaDescription(
                    "video", publicVideoAddress.getPort(), 1, "RTP/AVP",
                    vformats);
                if (!isVideoTransmissionSupported()) {
                    vm.setAttribute("recvonly", null);
                }else{
                  	vm.setAttribute("sendrecv", null);
                }
                
                Vector mediaDescs = new Vector();

                //Add Video and media descriptions if the user has not requested
                //otherwise (feature request by Pradeep Cheetal)
                if( Utils.getProperty("net.java.mais.media.NO_AUDIO_DESCRIPTION_IN_SDP")== null
                   || !Utils.getProperty("net.java.mais.media.NO_AUDIO_DESCRIPTION_IN_SDP").equalsIgnoreCase("true"))
                    mediaDescs.add(am);
                if( Utils.getProperty("net.java.mais.media.NO_VIDEO_DESCRIPTION_IN_SDP")== null
                   || !Utils.getProperty("net.java.mais.media.NO_VIDEO_DESCRIPTION_IN_SDP").equalsIgnoreCase("true"))
                       mediaDescs.add(vm);

                sessDescr.setVersion(v);
                sessDescr.setOrigin(o);
                sessDescr.setConnection(c);
                sessDescr.setSessionName(s);
                sessDescr.setTimeDescriptions(timeDescs);
                if(mediaDescs.size() > 0)
                    sessDescr.setMediaDescriptions(mediaDescs);
                return sessDescr.toString();
            }
            catch (SdpException exc) {
                throw new MediaException(
                    "An SDP exception occurred while generating local sdp description",
                    exc);
            }
        }
        finally {
            
        }
    }

    public String generateHoldSdpDescription(boolean setAudio, boolean setVideo) throws MediaException
    {
        try {
            
            checkIfStarted();
            try {
                SessionDescription sessDescr = sdpFactory.
                    createSessionDescription();
                
                System.out.println("MIC: "+setAudio);
                System.out.println("CAM: "+setVideo);
                
                AVTransmitter at=(AVTransmitter)(avTransmitters.get(0));
                
                Version v = sdpFactory.createVersion(0);
                                
                InetSocketAddress publicVideoAddress = NetworkAddressManager.getPublicAddressFor(at.localPorts.size()>1?(Integer)at.localPorts.get(1):(Integer)at.localPorts.get(0));//at.findPortForLocal((Integer)at.localPorts.get(1)));
                InetSocketAddress publicAudioAddress = NetworkAddressManager.getPublicAddressFor((Integer)at.localPorts.get(0));//at.findPortForLocal((Integer)at.localPorts.get(0)));
                InetAddress publicIpAddress = publicAudioAddress.getAddress();
                String addrType = publicIpAddress instanceof Inet6Address ?
                    "IP6" : "IP4";

                //spaces in the user name mess everything up.
                //bug report - Alessandro Melzi
                Origin o = sdpFactory.createOrigin(
                    Utils.getProperty("user.name").replace(' ', '_'), 20109217, 3, "IN",
                    addrType, publicIpAddress.getHostAddress());
                //"s=-"
                SessionName s = sdpFactory.createSessionName("<Tudomais>");
                //c=
                Connection c = sdpFactory.createConnection(
                    "IN",
                    addrType,
                    false?"0.0.0.0":publicIpAddress.getHostAddress());
                //"t=0 0"
                TimeDescription t = sdpFactory.createTimeDescription();
                Vector timeDescs = new Vector();
                timeDescs.add(t);
                //--------Audio media description
                //make sure preferred formats come first
                surfacePreferredEncodings(getReceivableAudioFormats());
                String[] formats = getReceivableAudioFormats();
                MediaDescription am = sdpFactory.createMediaDescription(
                    "audio", publicAudioAddress.getPort(), 1, "RTP/AVP",
                    formats);
                if (!isAudioTransmissionSupported()) {
                    am.setAttribute("recvonly", null);
                    //--------Video media description
                }else{
                	am.setAttribute(setAudio?"sendonly":"sendrecv", null);
                }
                surfacePreferredEncodings(getReceivableVideoFormats());
                //"m=video 22222 RTP/AVP 34";
                String[] vformats = getReceivableVideoFormats();
                MediaDescription vm = sdpFactory.createMediaDescription(
                    "video", publicVideoAddress.getPort(), 1, "RTP/AVP",
                    vformats);
                if (!isVideoTransmissionSupported()) {
                    vm.setAttribute("recvonly", null);
                }else{
                  	vm.setAttribute(setVideo?"sendonly":"sendrecv", null);
                }
                
                Vector mediaDescs = new Vector();

                //Add Video and media descriptions if the user has not requested
                //otherwise (feature request by Pradeep Cheetal)
                if( Utils.getProperty("net.java.mais.media.NO_AUDIO_DESCRIPTION_IN_SDP")== null
                   || !Utils.getProperty("net.java.mais.media.NO_AUDIO_DESCRIPTION_IN_SDP").equalsIgnoreCase("true"))
                    mediaDescs.add(am);
                if( Utils.getProperty("net.java.mais.media.NO_VIDEO_DESCRIPTION_IN_SDP")== null
                   || !Utils.getProperty("net.java.mais.media.NO_VIDEO_DESCRIPTION_IN_SDP").equalsIgnoreCase("true"))
                       mediaDescs.add(vm);
             
                sessDescr.setVersion(v);
                sessDescr.setOrigin(o);
                sessDescr.setConnection(c);
                sessDescr.setSessionName(s);
                sessDescr.setTimeDescriptions(timeDescs);
                if(mediaDescs.size() > 0)
                    sessDescr.setMediaDescriptions(mediaDescs);
                return sessDescr.toString();
            }
            catch (SdpException exc) {
                throw new MediaException(
                    "An SDP exception occurred while generating local sdp description",
                    exc);
            }
        }
        finally {
            
        }
    }
    
    /* 
     * Changes for NAT Traversal Policy
     */
    
    String getAudioPort()
    {
        try {
            
            /*@TODO MULTI CHAMADAS */
            String audioPort;
            if(this.avTransmitters.size()>0){
            AVTransmitter at = (AVTransmitter)this.avTransmitters.get(0);
            audioPort = String.valueOf(at.ports.get(0));
            }else{
            audioPort = Utils.getProperty(
            "net.java.mais.media.AUDIO_PORT");            
            }
            //return audioPort == null ? "22224" : audioPort;
            return this.audioPort;
        }
        finally {
            
        }
    }

    String getVideoPort()
    {
        try {
            
            /*@TODO MULTI CHAMADAS */
            String videoPort;
            if(this.avTransmitters.size()>0){
                AVTransmitter at = (AVTransmitter)this.avTransmitters.get(0);
                if(at.ports.size()>1)
                videoPort = String.valueOf(at.ports.get(1));
            }else{
            videoPort = Utils.getProperty(
                "net.java.mais.media.VIDEO_PORT");
            }
            //return videoPort == null ? "22222" : videoPort;
            return this.videoPort;            
        }
        finally {
            
        }
    }

    protected void finalize()
    {
        try {
            
            try {
                if (avDataSource != null) {
                    avDataSource.disconnect();
                }
            }
            catch (Exception exc) {
            }
        }
        finally {
            
        }
    }

    public boolean isStarted()
    {
        return isStarted;
    }

    protected void checkIfStarted() throws MediaException
    {
        if (!isStarted()) {
            throw new MediaException(
                "The MediaManager had not been properly started! "
                + "Impossible to continue");
        }
    }

    protected boolean isAudioTransmissionSupported()
    {
        return transmittableAudioFormats.size() > 0;
    }

    protected boolean isVideoTransmissionSupported()
    {
        return transmittableVideoFormats.size() > 0;
    }

    protected boolean isMediaTransmittable(String media)
    {
        if (media.equalsIgnoreCase("video")
            && isVideoTransmissionSupported()) {
            return true;
        }
        else if (media.equalsIgnoreCase("audio")
                 && isAudioTransmissionSupported()) {
            return true;
        }
        else {
            return false;
        }
    }

    protected String[] getReceivableAudioFormats()
    {
        return receivableAudioFormats;
    }

    protected String[] getReceivableVideoFormats()
    {
        return receivableVideoFormats;
    }

    protected String findCorrespondingJmfFormat(String sdpFormatStr)
    {
        int sdpFormat = -1;
        try {
            sdpFormat = Integer.parseInt(sdpFormatStr);
        }
        catch (NumberFormatException ex) {
            return null;
        }
        //return AudioFormat.GSM_RTP;
        switch (sdpFormat) {
            case SdpConstants.PCMU:
                return AudioFormat.ULAW_RTP;
            case SdpConstants.GSM:
                return AudioFormat.GSM_RTP;
            case SdpConstants.G723:
                return AudioFormat.G723_RTP;
            case SdpConstants.DVI4_8000:
                return AudioFormat.DVI_RTP;
            case SdpConstants.DVI4_16000:
                return AudioFormat.DVI_RTP;
            case SdpConstants.PCMA:
                return AudioFormat.ALAW;
            case SdpConstants.G728:
                return AudioFormat.G728_RTP;
            case SdpConstants.G729:
                return AudioFormat.G729_RTP;
            case SdpConstants.H263:
                return VideoFormat.H263_RTP;
            case SdpConstants.JPEG:
                return VideoFormat.JPEG_RTP;
            case SdpConstants.H261:
                return VideoFormat.H261_RTP;
            default:
                return null;
        }
    }

    protected String findCorrespondingSdpFormat(String jmfFormat)
    {
        if (jmfFormat == null) {
            return null;
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -