2

我正在开发一个项目,该项目需要实现从服务器上的网络摄像头到远程客户端上的小程序的实时视频流。

我有一个代码,用于通过服务器上的 JMF 从网络摄像头捕获实时流,并在 rtp 上部署流和一个从 rtp 地址接收流然后将其播放到其中的小程序。

但问题是它只能在我自己的电脑(服务器)中工作并且不能在本地网络上工作(它加载小程序但不运行视频播放器)。

服务器代码:

public class Streaming_Server {

    /**
     * @param args the command line arguments
     */
    private static String PORT = "10000";
    private static InetAddress addr;
    static final Format[] FORMATS = { new VideoFormat("rgb") };
    static final ContentDescriptor CONTENT_DESCRIPTOR = new ContentDescriptor("raw.rtp");

    public static void main(String[] args) throws IOException, NoDataSourceException, NoProcessorException, CannotRealizeException, NoDataSinkException {
        // TODO code application logic here
        try
        {
            addr = InetAddress.getLocalHost();
            String ipAddr = addr.getHostAddress();
            String hostname = addr.getHostName();
            System.out.println("Your IP is " + ipAddr);
            System.out.println("Your Host Name is " + hostname);
        }
        catch (UnknownHostException e){}


        CaptureDeviceInfo webcamInfooo = new CaptureDeviceInfo("Camera", new MediaLocator("vfw://0"), null);
        MediaLocator camDeviceMediaLocator = webcamInfooo.getLocator();
        DataSource source = Manager.createDataSource(camDeviceMediaLocator);
        Processor mediaProcessor = Manager.createRealizedProcessor(new ProcessorModel(source, FORMATS, CONTENT_DESCRIPTOR));
        //MediaLocator outputMediaLocator = new MediaLocator("rtp://" + addr.getHostAddress() + ":" + PORT + "/video");
        MediaLocator outputMediaLocator = new MediaLocator("rtp://192.168.1.4:20000/video");
        DataSink dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(), outputMediaLocator);

        mediaProcessor.start();
        dataSink.open();
        dataSink.start();
    }
}

客户代码:

public class Streaming_Client extends Applet implements ControllerListener {

    /**
     * Initialization method that will be called after the applet is loaded into
     * the browser.
     */
    Player player = null;
    private MediaLocator mediaLocator;
    private static String PORT = "10000";
    private static InetAddress addr;

    public void init() {
        // TODO start asynchronous download of heavy resources
        //mediaLocator = new MediaLocator("rtp:/192.168.1.4:10000/video");
        mediaLocator = new MediaLocator("rtp:/192.168.1.4:20000/video");
        setLayout(new BorderLayout());
        try {
            player = Manager.createPlayer(mediaLocator);
            player.addControllerListener(this);
        } catch (IOException | NoPlayerException ex) {
            Logger.getLogger(Streaming_Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void start(){
        player.start();
    }

    public void stop(){
        player.stop();
        player.deallocate();
    }

    public void destroy(){
        player.close();
    }
    // TODO overwrite start(), stop() and destroy() methods

    @Override
    public void controllerUpdate(ControllerEvent ce) {
        if ((ce instanceof RealizeCompleteEvent))
        {
        Component comp;
        if ((comp = this.player.getVisualComponent()) != null)
            add("Center", comp);
        if ((comp = this.player.getControlPanelComponent()) != null)
            add("South", comp);
        validate();
        }
    }
}
4

1 回答 1

3

经过一番研究,我发现服务器中的 MediaLocator 采用目标 IP而不是源 IP。

这是一件烦人的事情,因为你必须知道你在哪里流式传输。

所以,在这个编辑之后..

CaptureDeviceInfo webcamInfooo = new CaptureDeviceInfo("Camera", new MediaLocator("vfw://0"), null);
MediaLocator camDeviceMediaLocator = webcamInfooo.getLocator();
DataSource source = Manager.createDataSource(camDeviceMediaLocator);
Processor mediaProcessor = Manager.createRealizedProcessor(new ProcessorModel(source, FORMATS, CONTENT_DESCRIPTOR));
//MediaLocator outputMediaLocator = new MediaLocator("rtp://" + addr.getHostAddress() + ":" + PORT + "/video");
MediaLocator outputMediaLocator = new MediaLocator("rtp://192.168.1.6:20000/video");
DataSink dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(), outputMediaLocator);

我只能通过Intranet从 192.168.1.4 流式传输实时视频捕获到 192.168.1.6 。

但直到现在我还找不到如何通过互联网流式传输,因为我必须分配远程 pc ip。

于 2012-09-23T18:35:07.237 回答