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

📄 avreceiver.java

📁 jtapi for telephone
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * Large portions of this software are based upon public domain software
 * https://sip-communicator.dev.java.net/
 *
 */
package net.sourceforge.gjtapi.raw.sipprovider.media;

import net.sourceforge.gjtapi.raw.sipprovider.common.Console;
/**
 * <p>Title: SIP COMMUNICATOR</p>
 * <p>Description:JAIN-SIP Audio/Video phone application</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Organisation: LSIIT laboratory (http://lsiit.u-strasbg.fr) </p>
 * <p>Network Research Team (http://www-r2.u-strasbg.fr))</p>
 * <p>Louis Pasteur University - Strasbourg - France</p>
 * @author Emil Ivov (http://www.emcho.com)
 * @version 1.1
 *
 */
import java.net.*;
import javax.media.*;
import javax.media.control.*;
import javax.media.protocol.*;
import javax.media.rtp.*;
import javax.media.rtp.event.*;

import java.util.Properties;
import java.util.*;
/**
 * AVReceiver to receive RTP transmission using the new RTP API.
 */
class AVReceiver    implements ReceiveStreamListener, SessionListener,
ControllerListener, SendStreamListener
{
    private static Console console = Console.getConsole(AVReceiver.class);
    private DataSource dataSource = null;
    private net.sourceforge.gjtapi.raw.sipprovider.media.MediaManager mediaManager;
    private String sessions[] = null;
    private RTPManager mgrs[] = null;
    boolean dataReceived = false;
    private Object dataSync = new Object();
    
    private int bindRetries = 3;
    private Properties sipProp;
    private Processor processor = null;
    public DataSource ds;
    protected ArrayList formatSets = null;
    public AVReceiver(String sessions[],Properties sipProp)
    {
        this.sessions = sessions;
        this.sipProp = new Properties() ;
        this.sipProp.putAll(sipProp);
        String retries = null;
        
        
        if((retries = sipProp.getProperty("net.java.sip.communicator.media.RECEIVER_BIND_RETRIES")) != null)
        try
        {
            bindRetries = Integer.valueOf(retries).intValue();
        }
        catch (NumberFormatException ex)
        {
            console.error(retries + " is not a valid number. ignoring property", ex);
        }
    }
    
    void setMediaManager(MediaManager mManager)
    {
        this.mediaManager = mManager;
    }
    
    protected boolean initialize()
    {
        try
        {
            console.logEntry();
            InetAddress ipAddr;
            SessionAddress localAddr = new SessionAddress();
            SessionAddress destAddr;
            mgrs = new RTPManager[sessions.length];
            SessionLabel session;
            // Open the RTP sessions.
            for (int i = 0; i < sessions.length; i++)
            {
                // Parse the session addresses.
                try
                {
                    session = new SessionLabel(sessions[i]);
                }
                catch (IllegalArgumentException e)
                {
                    console.error(
                    "Failed to parse the session address given: "
                    + sessions[i]);
                    console.logExit();
                    return false;
                }
                if (console.isDebugEnabled())
                {
                    console.debug(
                    " Start listening for RTP @ addr: "
                    + session.addr + " port: " + session.port
                    + " ttl: " + session.ttl);
                }
                //rtpmanager avec localost + port local
                mgrs[i] = mediaManager.getRtpManager(new SessionAddress(mediaManager.getLocalHost(),session.port));
                if(mgrs[i] == null)
                {
                    mgrs[i] = RTPManager.newInstance();
                    mediaManager.putRtpManager(new SessionAddress(mediaManager.getLocalHost(),session.port), mgrs[i]);
                }
                mgrs[i].addSessionListener(this);
                mgrs[i].addReceiveStreamListener(this);
                mgrs[i].addSendStreamListener(this);
                ipAddr = InetAddress.getByName(session.addr);
                int tries = 0;
                while (tries++ < bindRetries)
                {
                    if (ipAddr.isMulticastAddress())
                    {
                        // local and remote address pairs are identical:
                        localAddr = new SessionAddress(ipAddr,
                        session.port,
                        session.ttl);
                        destAddr = new SessionAddress(ipAddr,
                        session.port,
                        session.ttl);
                    }
                    else
                    {
                        localAddr = new SessionAddress(mediaManager.getLocalHost(),session.port);
                        destAddr = new SessionAddress(ipAddr, session.port);
                    }
                    try
                    {
                        mgrs[i].initialize(localAddr);
                    }
                    catch (Exception exc)
                    {
                        if (tries < bindRetries)
                        {
                            continue;
                        }
                        console.error(
                        "Could not initialize rtp manager!",exc);
                        return false;
                    }
                    // You can try out some other buffer size to see
                    // if you can get better smoothness.
                    BufferControl bc = (BufferControl) mgrs[i].getControl("javax.media.control.BufferControl");
                    if (bc != null)
                    {
                        bc.setBufferLength(350);
                    }
                    
                    mgrs[i].addTarget(destAddr);//ajout de l'address
                    break; //port retries
                } //port retries
            }
        }
        catch (Exception e)
        {
            console.error("Cannot create the RTP Session: ", e);
            console.logExit();
            return false;
        }
        console.logExit();
        return true;
    }
    
    public boolean isDone()
    {
        return false;
    }
    
    /**
     * Close the players and the session managers.
     */
    protected void close(String LocalAddress)
    {
        try
        {
            console.logEntry();
            // close the RTP session.
            for (int i = 0; i < mgrs.length; i++)
            {
                if (mgrs[i] != null)
                {
                    if (console.isDebugEnabled())
                    {
                        console.debug("Stopped mgr " +  (i + 1));
                    }
                    try
                    {
                        InetAddress inetAdd =  InetAddress.getByName(LocalAddress);
                        SessionAddress sessionAddress = new SessionAddress(inetAdd, Integer.parseInt( mediaManager.getAudioPort()));
                        mgrs[i].removeTarget(sessionAddress ,"bye");
                        mgrs[i].dispose();
                        mgrs[i] = null;
                    }
                    catch (java.net.UnknownHostException ex)
                    {
                        console.debug(ex.toString());
                    }
                    catch(javax.media.rtp.InvalidSessionAddressException ex)
                    {
                        console.debug(ex.toString());
                    }
                    
                }
            }
        }
        finally
        {
            console.logExit();
        }
    }
    protected void close( )
    {
        try
        {
            console.logEntry();
            // close the RTP session.
            for (int i = 0; i < mgrs.length; i++)
            {
                if (mgrs[i] != null)
                {
                    if (console.isDebugEnabled())
                    {
                        console.debug("Stopped mgr " + (i + 1));
                    }
                    mgrs[i].removeTargets("Closing session from AVReceiver");
                    mgrs[i].dispose();
                    mgrs[i] = null;
                }
            }
        }
        finally
        {
            console.logExit();
        }
    }
    /**
     * SessionListener.
     */
    public synchronized void update(SessionEvent evt)
    {
        try
        {
            console.logEntry();
            if (evt instanceof NewParticipantEvent)
            {
                Participant p = ( (NewParticipantEvent) evt).getParticipant();
                if (console.isDebugEnabled())
                {
                    console.debug("A new participant had just joined: "
                    + p.getCNAME());
                }
            }
            else
            {
                if (console.isDebugEnabled())
                {
                    console.debug(
                    "Received a the following JMF Session event - "
                    + "evt.getClass().getName()");
                }
            }
        }
        finally
        {
            console.logExit();
        }
    }
    
    /**
     * ReceiveStreamListener
     */
    public synchronized void update(ReceiveStreamEvent evt)
    {
        try
        {
            console.logEntry();
            
            Participant participant = evt.getParticipant(); // could be null.
            ReceiveStream stream = evt.getReceiveStream(); // could be null.
            
            if (evt instanceof NewReceiveStreamEvent)
            {
                try
                {
                    stream = evt.getReceiveStream();
                    ds = stream.getDataSource();
                    // Find out the formats.
                    RTPControl ctl = (RTPControl) ds.getControl("javax.media.rtp.RTPControl");
                    if (console.isDebugEnabled())
                    {
                        if (ctl != null)
                        {
                            console.debug("Recevied new RTP stream: "
                            + ctl.getFormat());
                        }
                        else
                        {
                            console.debug("Recevied new RTP stream");
                        }
                    }
                    
                    processor = Manager.createProcessor(ds);
                    this.configureProcessor(processor);
                    
                    processor.realize();
                    
                    
                }
                catch (Exception e)
                {
                    console.error("NewReceiveStreamEvent exception ", e);
                    return;
                }
            }
            else if (evt instanceof StreamMappedEvent)
            {
                if (stream != null && stream.getDataSource() != null)
                {
                    ds = stream.getDataSource();
                    // Find out the formats.
                    RTPControl ctl = (RTPControl) ds.getControl(
                    "javax.media.rtp.RTPControl");

⌨️ 快捷键说明

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