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

📄 traandrec.java

📁 java语音聊天系统 分为服务器端 客户端 连接之后 既可以相互通信 包括语音 摄像头
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

            ipAddr = InetAddress.getByName(ipAddress);

            if (isMulticastAddress == false) {
                audioRTPMgr.initialize(new RTPSocketAdapter(audioDataSock,
                        audioCtrlSock,
                        ipAddr, destAudioDataPort,
                        destAudioCtrlPort));
            } else {
                audioRTPMgr.initialize(new RTPSocketAdapter(ipAddr,
                        destAudioDataPort, ttl));
            }

            if (audioDataOutput != null) {
                sendStream = audioRTPMgr.createSendStream(audioDataOutput, 0);
                sendStream.start();
                System.err.println("  - Start audio transmit ");
            }
        } catch (Exception e) {
            return e.getMessage();
        }

        return null;
    }

    public String createVideoRTPMgrs() {

        InetAddress ipAddr;
        SendStream sendStream;

        try {
            if(videoRTPMgr == null){
                videoRTPMgr = RTPManager.newInstance();
            }
            videoRTPMgr.addSessionListener(this);
            videoRTPMgr.addReceiveStreamListener(this);

            ipAddr = InetAddress.getByName(ipAddress);

            if (isMulticastAddress == false) {
                videoRTPMgr.initialize(new RTPSocketAdapter(videoDataSock,
                        videoCtrlSock, ipAddr, destVideoDataPort,
                        destVideoCtrlPort));
            } else {
                videoRTPMgr.initialize(new RTPSocketAdapter(ipAddr,
                        destVideoDataPort, ttl));
            }

            if (videoDataOutput != null) {
                sendStream = videoRTPMgr.createSendStream(videoDataOutput, 0);
                sendStream.start();
                System.err.println("  - Start video transmit ");
            }
        } catch (Exception e) {
            return e.getMessage();
        }

        return null;
    }

    public synchronized void update(SessionEvent evt) {
        if (evt instanceof NewParticipantEvent) {
            Participant p = ((NewParticipantEvent) evt).getParticipant();
            System.err.println("  - A new participant had just joined: " +
                               p.getCNAME());
        }
    }

    public synchronized void update(ReceiveStreamEvent evt) {

        RTPManager mgr = (RTPManager) evt.getSource();
        Participant participant = evt.getParticipant(); // could be null.
        ReceiveStream stream = evt.getReceiveStream(); // could be null.

        if (evt instanceof RemotePayloadChangeEvent) {
            System.err.println("  - Received an RTP PayloadChangeEvent.");
            System.err.println("Sorry, cannot handle payload change.");
            System.exit(0);
        }

        else if (evt instanceof NewReceiveStreamEvent) {
            try {
                stream = ((NewReceiveStreamEvent) evt).getReceiveStream();
                DataSource ds = stream.getDataSource();

                // Find out the formats.
                RTPControl ctl = (RTPControl) ds.getControl(
                        "javax.media.rtp.RTPControl");
                if (ctl != null) {
                    System.err.println("  - Recevied new RTP stream: " +
                                       ctl.getFormat());
                } else {
                    System.err.println("  - Recevied new RTP stream");
                }

                if (participant == null) {
                    System.err.println(
                            "      The sender of this stream had yet to be identified.");
                } else {
                    System.err.println("      The stream comes from: " +
                                       participant.getCNAME());
                }

                Player p = Manager.createPlayer(ds);
                if (p == null) {
                    return;
                }

                p.addControllerListener(this);
                p.realize();
                if (isMulticastAddress &&
                    (ctl.getFormat() instanceof VideoFormat)) {
                    PlayerWindow pw = new PlayerWindow(p, stream); //建新窗口
                    playerWindows.addElement(pw);
                }

                // Notify intialize() that a new stream had arrived.
                synchronized (dataSync) {
                    dataReceived = true;
                    dataSync.notifyAll();
                }

            } catch (Exception e) {
                System.err.println("NewReceiveStreamEvent exception " +
                                   e.getMessage());
                return;
            }
        }

        else if (evt instanceof StreamMappedEvent) {
            if (stream != null && stream.getDataSource() != null) {
                DataSource ds = stream.getDataSource();
                // Find out the formats.
                RTPControl ctl = (RTPControl) ds.getControl(
                        "javax.media.rtp.RTPControl");
                System.err.println("  - The previously unidentified stream ");
                if (ctl != null) {
                    System.err.println("      " + ctl.getFormat());
                }
                System.err.println("      had now been identified as sent by: " +
                                   participant.getCNAME());
            }
        }

        else if (evt instanceof ByeEvent) {

            System.err.println("  - Got \"bye\" from: " + participant.getCNAME());
            PlayerWindow pw = find(stream);
            if (pw != null) {
                pw.close();
                playerWindows.removeElement(pw);
            }
        }
    }

    public synchronized void controllerUpdate(ControllerEvent ce) {

        Player p = (Player) ce.getSourceController();

        if (p == null) {
            return;
        }

        if (ce instanceof RealizeCompleteEvent) {
            if (isMulticastAddress) {
                PlayerWindow pw = find(p);
                if (pw != null) {
                    pw.initialize();
                    pw.setVisible(true);
                    pw.setResizable(false);
                }
            } else {
                if (p.getVisualComponent() != null) {
                    chatFrame.videoPanel.add(new PlayerPanel(p));
                }
            }
            p.start();
        }

        if (ce instanceof ControllerErrorEvent) {
            p.removeControllerListener(this);
            PlayerWindow pw = find(p);
            if (pw != null) {
                pw.close();
                playerWindows.removeElement(pw);
            }
            System.err.println("Receiver internal error: " + ce);
        }

    }

    public boolean isDone() {
        return playerWindows.size() == 0; //判断窗口数目是否为零
    }

    PlayerWindow find(Player p) {
        for (int i = 0; i < playerWindows.size(); i++) {
            PlayerWindow pw = (PlayerWindow) playerWindows.elementAt(i);
            if (pw.player == p) {
                return pw;
            }
        }
        return null;
    }

    PlayerWindow find(ReceiveStream strm) {
        for (int i = 0; i < playerWindows.size(); i++) {
            PlayerWindow pw = (PlayerWindow) playerWindows.elementAt(i);
            if (pw.stream == strm) {
                return pw;
            }
        }
        return null;
    }

    class PlayerPanel extends JPanel {

        Component vc, cc;

        PlayerPanel(Player p) {
            setLayout(new BorderLayout());
            if ((vc = p.getVisualComponent()) != null) {
                add("Center", vc);
            }
        }

        public Dimension getPreferredSize() {
            int w = 0, h = 0;
            if (isMulticastAddress) {
                if (vc != null) {
                    Dimension size = vc.getPreferredSize();
                    w = size.width;
                    h = size.height;
                }
                if (cc != null) {
                    Dimension size = cc.getPreferredSize();
                    if (w == 0) {
                        w = size.width;
                    }
                    h += size.height;
                }
                if (w < 160) {
                    w = 160;
                }
                return new Dimension(w, h);
            } else {
                return new Dimension(227, 186);
            }
        }
    }


    class PlayerWindow extends JFrame {

        Player player;
        ReceiveStream stream;

        PlayerWindow(Player p, ReceiveStream strm) {
            player = p;
            stream = strm;
        }

        public void initialize() {
            add(new PlayerPanel(player));
        }

        public void close() {
            player.close();
            setVisible(false);
            dispose();
        }

        public void addNotify() {
            super.addNotify();
            pack();
        }
    }


    Format checkForVideoSizes(Format original, Format supported) {

        int width, height;
        Dimension size = ((VideoFormat) original).getSize();
        Format jpegFmt = new Format(VideoFormat.JPEG_RTP);
        Format h263Fmt = new Format(VideoFormat.H263_RTP);

        if (supported.matches(jpegFmt)) {
            // For JPEG, make sure width and height are divisible by 8.
            width = (size.width % 8 == 0 ? size.width :
                     (int) (size.width / 8) * 8);
            height = (size.height % 8 == 0 ? size.height :
                      (int) (size.height / 8) * 8);
        } else if (supported.matches(h263Fmt)) {
            // For H.263, we only support some specific sizes.
            if (size.width < 128) {
                width = 128;
                height = 96;
            } else if (size.width < 176) {
                width = 176;
                height = 144;
            } else {
                width = 352;
                height = 288;
            }
        } else {
            // We don't know this particular format.  We'll just
            // leave it alone then.
            return supported;
        }

        return (new VideoFormat(null,
                                new Dimension(width, height),
                                Format.NOT_SPECIFIED,
                                null,
                                Format.NOT_SPECIFIED)).intersects(supported);
    }

    // Setting the encoding quality to the specified value on the JPEG encoder.
    // 0.5 is a good default.
    void setJPEGQuality(Player p, float val) {

        Control cs[] = p.getControls();
        QualityControl qc = null;
        VideoFormat jpegFmt = new VideoFormat(VideoFormat.JPEG);

        // Loop through the controls to find the Quality control for
        // the JPEG encoder.
        for (int i = 0; i < cs.length; i++) {

            if (cs[i] instanceof QualityControl && cs[i] instanceof Owned) {
                Object owner = ((Owned) cs[i]).getOwner();

                // Check to see if the owner is a Codec.
                // Then check for the output format.
                if (owner instanceof Codec) {
                    Format fmts[] = ((Codec) owner).getSupportedOutputFormats(null);
                    for (int j = 0; j < fmts.length; j++) {
                        if (fmts[j].matches(jpegFmt)) {
                            qc = (QualityControl) cs[i];
                            qc.setQuality(val);
                            System.err.println("- Setting quality to " +
                                               val + " on " + qc);
                            break;
                        }
                    }
                }
                if (qc != null) {
                    break;
                }
            }
        }
    }

    public Integer stateLock = new Integer(0);
    public boolean failed = false;

    Integer getStateLock() {
        return stateLock;
    }

    void setFailed() {
        failed = true;
    }

    public 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();
        }

        while (p.getState() < state && !failed) {
            synchronized (getStateLock()) {
                try {
                    getStateLock().wait();
                } catch (InterruptedException ie) {
                    return false;
                }
            }
        }

        if (failed) {
            return false;
        } else {
            return true;
        }
    }


    class StateListener implements ControllerListener {

        public void controllerUpdate(ControllerEvent ce) {

            // 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();
                }
            }
        }
    }


    public String getLocalAddress() {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException ex) {
        }
        return addr.getHostAddress();
    }

}

⌨️ 快捷键说明

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