001// Display RTSP streaming of video
002// (c) 2011 Virgoptrex
003// This code is distributed in the hope that it will be useful,
004// but WITHOUT ANY WARRANTY; without even the implied warranty of
005// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
006// Leave Credits intact
007
008package ca.bc.webarts.tools;// replace this with your package
009import java.awt.BorderLayout;
010import java.awt.Dimension;
011
012import javax.swing.JFrame;
013import javax.swing.SwingUtilities;
014
015// import org.gstreamer.Caps;
016import org.gstreamer.Element;
017import org.gstreamer.ElementFactory;
018import org.gstreamer.Gst;
019import org.gstreamer.Pad;
020import org.gstreamer.PadDirection;
021import org.gstreamer.Pipeline;
022import org.gstreamer.swing.VideoComponent;
023
024/**
025 * A Simple videotest example.
026 */
027public class VideoGSTPlayer
028{
029  private static Pipeline pipe;
030
031
032  public VideoGSTPlayer()
033  {
034  }
035
036
037  public static void main(String[] args)
038  {
039    // Quartz is abysmally slow at scaling video for some reason, so turn it off.
040    System.setProperty("apple.awt.graphics.UseQuartz", "false");
041
042    args = Gst.init("SwingVideoTest", args);
043
044    pipe = new Pipeline("pipeline");
045    /*
046    final Element videosrc = ElementFactory.make("videotestsrc", "source");
047    final Element videofilter = ElementFactory.make("capsfilter", "flt");
048    videofilter.setCaps(Caps.fromString("video/x-raw-yuv, width=720, height=576"
049            + ", bpp=32, depth=32, framerate=25/1"));
050    */
051
052    pipe.getBus().connect(new Bus.ERROR()
053    {
054      public void errorMessage(GstObject source, int code, String message)
055      {
056        System.out.println("Error occurred: " + message);
057        Gst.quit();
058      }
059    } );
060    pipe.getBus().connect(new Bus.STATE_CHANGED()
061    {
062      public void stateChanged(GstObject source, State old, State current, State pending)
063      {
064        if (source == pipe)
065        {
066          System.out.println("Pipeline state changed from " + old + " to " + current);
067        }
068      }
069    } );
070    pipe.getBus().connect(new Bus.EOS()
071    {
072      public void endOfStream(GstObject source)
073      {
074        System.out.println("Finished playing file");
075        Gst.quit();
076      }
077    } );
078
079    pipe.getBus().connect(new Bus.TAG()
080    {
081      public void tagsFound(GstObject source, TagList tagList)
082      {
083        for (String tag : tagList.getTagNames())
084        {
085          System.out.println("Found tag " + tag + " = " + tagList.getValue(tag, 0));
086        }
087      }
088    } );
089
090    final Element source = ElementFactory.make("rtspsrc", "Source");
091    final Element demux = ElementFactory.make("rtpmp4vdepay", "Depay");
092    final Element decoder = ElementFactory.make("ffdec_mpeg4", "Decoder");
093    final Element colorspace = ElementFactory.make("ffmpegcolorspace", "Colorspace");
094    // final Element sink = ElementFactory.make ("autovideosink", "Output");
095
096    SwingUtilities.invokeLater(new Runnable()
097    {
098
099      public void run()
100      {
101        // Create the video component and link it in
102        VideoComponent videoComponent = new VideoComponent();
103        Element videosink = videoComponent.getElement();
104
105        source.connect(new Element.PAD_ADDED()
106        {
107          public void padAdded(Element element, Pad pad)
108          {
109            pad.link(demux.getStaticPad("sink"));
110          }
111        } );
112
113        Pad p = new Pad(null, PadDirection.SRC);
114        source.addPad(p);
115
116        source.set("location", "rtsp://<user>:<pass>@<ip>/mpeg4/1/media.amp");        // replace this with your source
117
118        pipe.addMany(source, demux, decoder, colorspace, videosink);
119        Element.linkMany(demux, decoder, colorspace, videosink);
120
121        // Now create a JFrame to display the video output
122        JFrame frame = new JFrame("Swing Video Test");
123        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
124        frame.add(videoComponent, BorderLayout.CENTER);
125        videoComponent.setPreferredSize(new Dimension(720, 576));
126        frame.pack();
127        frame.setVisible(true);
128
129        // Start the pipeline processing
130        pipe.play();
131      }
132    } );
133  }
134}