基于java的rtsp流媒体

基于java的rtsp流媒体,java,streaming,gstreamer,Java,Streaming,Gstreamer,任何人都可以帮助我学习如何使用gstreamer在java中实现RTSP流媒体服务器 RTSP构建过程服务器(代码用C编写,但请查看我的客户端代码,了解如何使用gstreamer java API。它应该相当直接) 我修改了URL中的代码 构建二进制文件后,只需运行它/rtsp,然后在终端中打开另一个选项卡,使用以下管道测试服务器 测试了解码管道。很好用 gst-launch -v rtspsrc location="rtsp://localhost:8554/test" name=demux

任何人都可以帮助我学习如何使用gstreamer在java中实现RTSP流媒体服务器


RTSP构建过程服务器(代码用C编写,但请查看我的客户端代码,了解如何使用gstreamer java API。它应该相当直接)

我修改了URL中的代码

构建二进制文件后,只需运行它
/rtsp
,然后在终端中打开另一个选项卡,使用以下管道测试服务器

测试了解码管道。很好用

gst-launch -v rtspsrc location="rtsp://localhost:8554/test" name=demux demux. ! queue ! rtph264depay ! ffdec_h264 ! ffmpegcolorspace ! autovideosink sync=false demux. ! queue ! rtppcmadepay  ! alawdec ! autoaudiosink
Java代码客户端

 // Display RTSP streaming of video
 // (c) 2011 enthusiasticgeek
 // This code is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 // Leave Credits intact

package video2; //replace this with your package
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

//import org.gstreamer.Caps;
import org.gstreamer.Element;
import org.gstreamer.ElementFactory;
import org.gstreamer.Gst;
import org.gstreamer.Pad;
import org.gstreamer.PadDirection;
import org.gstreamer.Pipeline;
import org.gstreamer.swing.VideoComponent;

/**
 * A Simple videotest example.
 */
public class Main {
    public Main() {
    }
    private static Pipeline pipe;
    public static void main(String[] args) {
    // Quartz is abysmally slow at scaling video for some reason, so turn it off.
    System.setProperty("apple.awt.graphics.UseQuartz", "false");

    args = Gst.init("SwingVideoTest", args);

    pipe = new Pipeline("pipeline");
    /*
    final Element videosrc = ElementFactory.make("videotestsrc", "source");
    final Element videofilter = ElementFactory.make("capsfilter", "flt");
    videofilter.setCaps(Caps.fromString("video/x-raw-yuv, width=720, height=576"
            + ", bpp=32, depth=32, framerate=25/1"));
    */

     pipe.getBus().connect(new Bus.ERROR() {
        public void errorMessage(GstObject source, int code, String message) {
            System.out.println("Error occurred: " + message);
            Gst.quit();
        }
    });
    pipe.getBus().connect(new Bus.STATE_CHANGED() {
        public void stateChanged(GstObject source, State old, State current, State pending) {
            if (source == pipe) {
                System.out.println("Pipeline state changed from " + old + " to " + current);
            }
        }
    });
    pipe.getBus().connect(new Bus.EOS() {
        public void endOfStream(GstObject source) {
            System.out.println("Finished playing file");
            Gst.quit();
        }
    });        

     pipe.getBus().connect(new Bus.TAG() {
        public void tagsFound(GstObject source, TagList tagList) {
            for (String tag : tagList.getTagNames()) {
                System.out.println("Found tag " + tag + " = "
                        + tagList.getValue(tag, 0));
            }
        }
    });

    final Element source = ElementFactory.make("rtspsrc", "Source");
    final Element demux = ElementFactory.make("rtpmp4vdepay", "Depay");
    final Element decoder=ElementFactory.make("ffdec_mpeg4", "Decoder");
    final Element colorspace = ElementFactory.make("ffmpegcolorspace",  "Colorspace");
    //final Element sink = ElementFactory.make ("autovideosink", "Output");

    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            // Create the video component and link it in
            VideoComponent videoComponent = new VideoComponent();
            Element videosink = videoComponent.getElement();

           source.connect(new Element.PAD_ADDED() {
           public void padAdded(Element element, Pad pad) {
            pad.link(demux.getStaticPad("sink"));
           }
            });

           Pad p = new Pad(null, PadDirection.SRC);
           source.addPad(p);

            source.set("location","rtsp://<user>:<pass>@<ip>/mpeg4/1/media.amp");  //replace this with your source

            pipe.addMany(source, demux, decoder, colorspace, videosink);
            Element.linkMany(demux, decoder, colorspace, videosink);

            // Now create a JFrame to display the video output
            JFrame frame = new JFrame("Swing Video Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(videoComponent, BorderLayout.CENTER);
            videoComponent.setPreferredSize(new Dimension(720, 576));
            frame.pack();
            frame.setVisible(true);

            // Start the pipeline processing
            pipe.play();
        }
    });
    }
}
//显示RTSP视频流
//(c)2011狂热极客
//分发此代码是为了希望它有用,
//但无任何保证;甚至没有任何关于
//适销性或适合某一特定目的
//保留信用证不变
包装视频2//把这个换成你的包裹
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入javax.swing.JFrame;
导入javax.swing.SwingUtilities;
//导入org.gstreamer.Caps;
导入org.gstreamer.Element;
导入org.gstreamer.ElementFactory;
导入org.gstreamer.Gst;
导入org.gstreamer.Pad;
导入org.gstreamer.PadDirection;
导入org.gstreamer.Pipeline;
导入org.gstreamer.swing.VideoComponent;
/**
*一个简单的视频测试示例。
*/
公共班机{
公用干管(){
}
专用静态管道;
公共静态void main(字符串[]args){
//由于某些原因,石英在缩放视频时速度非常慢,所以请将其关闭。
System.setProperty(“apple.awt.graphics.UseQuartz”、“false”);
args=Gst.init(“SwingVideoTest”,args);
管道=新管道(“管道”);
/*
最终元素videosrc=ElementFactory.make(“videotestsrc”,“source”);
最终元件videofilter=ElementFactory.make(“capsfilter”、“flt”);
videofilter.setCaps(Caps.fromString(“video/x-raw-yuv,宽度=720,高度=576”)
+“,bpp=32,深度=32,帧速率=25/1”);
*/
pipe.getBus().connect(new Bus.ERROR()){
公共无效错误消息(GstObject源代码、int代码、字符串消息){
System.out.println(“发生错误:+消息”);
Gst.quit();
}
});
pipe.getBus().connect(新的总线状态_已更改(){
public void stateChanged(GstObject源、状态旧、状态当前、状态挂起){
如果(源==管道){
System.out.println(“管道状态从“+old+”更改为“+current”);
}
}
});
pipe.getBus().connect(new Bus.EOS()){
public void endOfStream(GstObject源){
System.out.println(“完成播放文件”);
Gst.quit();
}
});        
pipe.getBus().connect(new Bus.TAG()){
public void标记查找(GstObject源、标记列表标记列表){
for(字符串标记:tagList.getTagNames()){
System.out.println(“找到的标记”+标记+”=“
+tagList.getValue(tag,0));
}
}
});
最终元素源=ElementFactory.make(“rtspsrc”,“源”);
最终元件demux=元件工厂制造(“rtpmp4vdepay”、“Depay”);
最终元素解码器=元素工厂制造(“ffdec_mpeg4”,“解码器”);
最终元素colorspace=ElementFactory.make(“ffmpegcolorspace”、“colorspace”);
//最终元素接收器=ElementFactory.make(“自动视频接收器”、“输出”);
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
//创建视频组件并将其链接到
VideoComponent VideoComponent=新的VideoComponent();
Element videosink=videoComponent.getElement();
source.connect(新元素.PAD_添加(){
已添加公共空白焊盘(图元、焊盘){
pad.link(demux.getStaticPad(“sink”));
}
});
Pad p=新Pad(null,PadDirection.SRC);
来源:addPad(p);
source.set(“location”,“rtsp://:@/mpeg4/1/media.amp”);//将其替换为您的源代码
pipe.addMany(源、解复用、解码器、色彩空间、视频接收器);
linkMany元素(解复用器、解码器、色彩空间、视频接收器);
//现在创建一个JFrame来显示视频输出
JFrame=新JFrame(“Swing视频测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(videoComponent,BorderLayout.CENTER);
videoComponent.setPreferredSize(新尺寸(720576));
frame.pack();
frame.setVisible(true);
//启动管道处理
吹奏;
}
});
}
}

这是一个客户端,而不是服务器。@user8709 ok我已更正为合并服务器。在我的参考链接之后,编写服务器代码对他/她来说应该是一项非常简单的任务。我之前误读了这个问题。我希望至少我现在没有得到负面的观点(在从非常有限的资源中编写客户端代码的所有努力之后):@AffairicicGeek我正在尝试您的RTSP客户端代码,但遇到了一些编译问题,参数不匹配,你能帮我看看
org.gstreamer
jar的jar版本/可下载链接吗。@Abhijeet 1。这段代码已经5年了,所以请参考当时的发行说明和当前版本,API很可能已经更改。2.您没有指定哪个函数导致参数不匹配的问题。我帮不了你,因为你那边几乎没有消息。我的建议是用当前的API修改上面的代码-应该不会太难。@Abhijeet如果您仍然坚持使用旧的API,请尝试从github编译标记1.4
gst-launch -v rtspsrc location="rtsp://localhost:8554/test" name=demux demux. ! queue ! rtph264depay ! ffdec_h264 ! ffmpegcolorspace ! autovideosink sync=false demux. ! queue ! rtppcmadepay  ! alawdec ! autoaudiosink
 // Display RTSP streaming of video
 // (c) 2011 enthusiasticgeek
 // This code is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 // Leave Credits intact

package video2; //replace this with your package
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

//import org.gstreamer.Caps;
import org.gstreamer.Element;
import org.gstreamer.ElementFactory;
import org.gstreamer.Gst;
import org.gstreamer.Pad;
import org.gstreamer.PadDirection;
import org.gstreamer.Pipeline;
import org.gstreamer.swing.VideoComponent;

/**
 * A Simple videotest example.
 */
public class Main {
    public Main() {
    }
    private static Pipeline pipe;
    public static void main(String[] args) {
    // Quartz is abysmally slow at scaling video for some reason, so turn it off.
    System.setProperty("apple.awt.graphics.UseQuartz", "false");

    args = Gst.init("SwingVideoTest", args);

    pipe = new Pipeline("pipeline");
    /*
    final Element videosrc = ElementFactory.make("videotestsrc", "source");
    final Element videofilter = ElementFactory.make("capsfilter", "flt");
    videofilter.setCaps(Caps.fromString("video/x-raw-yuv, width=720, height=576"
            + ", bpp=32, depth=32, framerate=25/1"));
    */

     pipe.getBus().connect(new Bus.ERROR() {
        public void errorMessage(GstObject source, int code, String message) {
            System.out.println("Error occurred: " + message);
            Gst.quit();
        }
    });
    pipe.getBus().connect(new Bus.STATE_CHANGED() {
        public void stateChanged(GstObject source, State old, State current, State pending) {
            if (source == pipe) {
                System.out.println("Pipeline state changed from " + old + " to " + current);
            }
        }
    });
    pipe.getBus().connect(new Bus.EOS() {
        public void endOfStream(GstObject source) {
            System.out.println("Finished playing file");
            Gst.quit();
        }
    });        

     pipe.getBus().connect(new Bus.TAG() {
        public void tagsFound(GstObject source, TagList tagList) {
            for (String tag : tagList.getTagNames()) {
                System.out.println("Found tag " + tag + " = "
                        + tagList.getValue(tag, 0));
            }
        }
    });

    final Element source = ElementFactory.make("rtspsrc", "Source");
    final Element demux = ElementFactory.make("rtpmp4vdepay", "Depay");
    final Element decoder=ElementFactory.make("ffdec_mpeg4", "Decoder");
    final Element colorspace = ElementFactory.make("ffmpegcolorspace",  "Colorspace");
    //final Element sink = ElementFactory.make ("autovideosink", "Output");

    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            // Create the video component and link it in
            VideoComponent videoComponent = new VideoComponent();
            Element videosink = videoComponent.getElement();

           source.connect(new Element.PAD_ADDED() {
           public void padAdded(Element element, Pad pad) {
            pad.link(demux.getStaticPad("sink"));
           }
            });

           Pad p = new Pad(null, PadDirection.SRC);
           source.addPad(p);

            source.set("location","rtsp://<user>:<pass>@<ip>/mpeg4/1/media.amp");  //replace this with your source

            pipe.addMany(source, demux, decoder, colorspace, videosink);
            Element.linkMany(demux, decoder, colorspace, videosink);

            // Now create a JFrame to display the video output
            JFrame frame = new JFrame("Swing Video Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(videoComponent, BorderLayout.CENTER);
            videoComponent.setPreferredSize(new Dimension(720, 576));
            frame.pack();
            frame.setVisible(true);

            // Start the pipeline processing
            pipe.play();
        }
    });
    }
}