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

📄 avtransmitter.java

📁 jtapi for telephone
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**
     * Use the RTPManager API to create sessions for each media
     * track of the processor.
     */
    protected void createTransmitter() throws MediaException
    {
        try
        {
            console.logEntry();
           /* PushBufferDataSource pbds = (PushBufferDataSource) dataOutput;
            PushBufferStream pbss[] = pbds.getStreams();*/
            rtpMgrs = new RTPManager[1];
            //used by mobility
            sessionAddresses = new SessionAddress[1];
            SessionAddress localAddr, destAddr;
            InetAddress remoteAddress;
            
            
            console.debug("data sources - " + 1);
            int port = 0;
            int i = 0;
            
            try
            {
                remoteAddress = InetAddress.getByName(ipAddress);
            }
            catch (UnknownHostException ex)
            {
                console.error("Failed to resolve remote address", ex);
                throw new MediaException("Failed to resolve remote address",
                ex);
            }
            // port = findPortForFormat(pbss[i].getFormat().getEncoding());
            if (port == -1)
            {
                console.error("failed to find a format's port");
                throw new MediaException(
                "Internal error! AVTransmitter failed to find a"
                + " format's corresponding port");
            }
            
            localAddr = new SessionAddress(mediaManCallback.getLocalHost(),((Integer)ports.get(0)).intValue() );
            destAddr = new SessionAddress(remoteAddress,  ((Integer)ports.get(0)).intValue());
            rtpMgrs[i] =  mediaManCallback.getRtpManager(localAddr);
            if (rtpMgrs[i] == null)
            {
                rtpMgrs[i] = RTPManager.newInstance();
                mediaManCallback.putRtpManager(localAddr, rtpMgrs[i]);
                
            }
            
            
            
            try
            {
                rtpMgrs[i].initialize(localAddr);
                console.debug("Just bond to port" + localAddr.getDataPort());
                rtpMgrs[i].addTarget(destAddr);
                sessionAddresses[i] = destAddr;
            }
            catch (InvalidSessionAddressException ex)
            {
                //port was occupied
                if (console.isDebugEnabled())
                {
                    console.debug("Couldn't bind to local ports "
                    + localAddr.getDataPort() + ", " +
                    localAddr.getControlPort()
                    + " @ " +
                    localAddr.getControlHostAddress()
                    + ".\n Exception message was: " +
                    ex.getMessage()
                    + " Will try another pair!");
                }
                
            }
            catch (IOException ex)
            {
                //we should just try to notify user and continue with other tracks
                console.error(
                "Failed to initialize an RTPManager for address pair:\n"
                + "Local address:" + localAddr.toString()
                + " data port:" + localAddr.getDataPort()
                + " control port:" + localAddr.getControlPort() +
                "\n"
                + "Dest  address:" + destAddr
                + " data port:" + destAddr.getDataPort()
                + " control port:" + destAddr.getControlPort(),
                ex);
                mediaManCallback.fireNonFatalMediaError(new
                MediaException(
                "Failed to initialize an RTPManager for address pair:\n"
                + "Local address:" + localAddr.toString()
                + " data port:" + localAddr.getDataPort()
                + " control port:" + localAddr.getControlPort() +
                "\n"
                + "Dest  address:" + destAddr
                + " data port:" + destAddr.getDataPort()
                + " control port:" + destAddr.getControlPort(),
                ex));
                
            }
        }
        
        finally
        {
            console.logExit();
        }
    }
    
    
    
    protected int findPortForFormat(String format)
    {
        try
        {
            console.logEntry();
            for (int i = 0; i < formatSets.size(); i++)
            {
                ArrayList currentSet = (ArrayList) formatSets.get(i);
                for (int j = 0; j < currentSet.size(); j++)
                {
                    if ( ( (String) currentSet.get(j)).equals(format))
                    {
                        return ( (Integer) ports.get(i)).intValue();
                    }
                }
            }
            return -1;
        }
        finally
        {
            console.logExit();
        }
    }
    
    
    
    protected int findFirstMatchingFormat(Format[] hayStack, ArrayList needles)
    {
        try
        {
            console.logEntry();
            if (hayStack == null || needles == null)
            {
                return -1;
            }
            for (int j = 0; j < needles.size(); j++)
            {
                ArrayList currentSet = (ArrayList) needles.get(j);
                for (int k = 0; k < currentSet.size(); k++)
                {
                    for (int i = 0; i < hayStack.length; i++)
                    {
                        if (hayStack[i].getEncoding().equals(
                        currentSet.get(k)))
                        {
                            return i;
                        }
                    }
                }
            }
            return -1;
        }
        finally
        {
            console.logExit();
        }
    }
    
    /****************************************************************
     * Convenience methods to handle processor's state changes.
     ****************************************************************/
    protected Integer stateLock = new Integer(0);
    protected boolean failed = false;
    Integer getStateLock()
    {
        return stateLock;
    }
    
    void setFailed()
    {
        failed = true;
    }
    
    protected synchronized boolean waitForState(Processor p, int state)
    {
        
        p.addControllerListener(new StateListener());
        failed = false;
        // Call the required method on the processor
        if (state == Processor.Configured)
        {
            p.configure();
        }
        else if (state == Processor.Realized)
        {
            p.realize();
        }
        // Wait until we get an event that confirms the
        // success of the method, or a failure event.
        // See StateListener inner class
        while (p.getState() < state && !failed)
        {
            synchronized (getStateLock())
            {
                try
                {
                    getStateLock().wait();
                }
                catch (InterruptedException ie)
                {
                    return false;
                }
            }
        }
        if (failed)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    /****************************************************************
     * Inner Classes
     ****************************************************************/
    class StateListener
    implements ControllerListener
    {
        public void controllerUpdate(ControllerEvent ce)
        {
            try
            {
                console.logEntry();
                console.debug(ce.toString());
                // If there was an error during configure or
                // realize, the processor will be closed
                if (ce instanceof ControllerClosedEvent)
                {
                    setFailed();
                    // All controller events, send a notification
                    // to the waiting thread in waitForState method.
                }
                if (ce instanceof ControllerEvent)
                {
                    synchronized (getStateLock())
                    {
                        getStateLock().notifyAll();
                    }
                }
                //stop the stream at the end of the audio file
                if (ce instanceof EndOfMediaEvent)
                {
                    try
                    {
                        sendStream.stop();
                        processor.stop();
                        
                    }catch(Exception ex)
                    {
                        console.debug(ex.toString());
                    }
                }
            }
            finally
            {
                console.logExit();
            }
        }
    }
    
    public void play(Processor p) throws MediaException
    {
        console.logEntry();
        this.configureProcessor(p);
        processor.start();
        PushBufferDataSource pbds = (PushBufferDataSource) dataOutput;
        PushBufferStream pbss[] = pbds.getStreams();
        int i =0;
        try
        {
            sendStream = rtpMgrs[i].createSendStream(dataOutput, i);
            sendStream.start();
            if (console.isDebugEnabled())
            {
                console.debug("Started transmitting track " + i
                + " encoded as " +
                pbss[i].getFormat().getEncoding()
                + " @ [" + ipAddress + "]");
            }
        }
        catch (Exception ex)
        {
            console.error("Session " + i +
            " failed to start transmitting.");
            throw new MediaException(
            "Session " + i + " failed to start transmitting.");
        }
        console.logExit();
    }
    
    public void stopPlaying()
    {
        try
        {
            sendStream.stop();
        }
        catch(java.io.IOException ex)
        {
            console.debug(ex.toString());
        }
    }
}

⌨️ 快捷键说明

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