/* receives messages from pipeline elements */
public static Bus bus = null;
/* send an EOS to this element to shutdown pipeline */
public static Element src = null;
public static void main(String[] args) {
/* initialize gstreamer library */
args = Gst.init("MultiSinkPipe", args);
/* create pipeline */
final Pipeline pipe = new Pipeline("MultiSinkPipe");
/* create all pipeline elements */
src = ElementFactory.make("filesrc", "filesrc0");
Element tee = ElementFactory.make("tee", "tee0");
Element h264q = ElementFactory.make("queue", "queue0");
Element decodebin = ElementFactory.make("decodebin", "decodebin0");
final Element enc = ElementFactory.make("x264enc", "x264enc0");
Element mux = ElementFactory.make("avmux_mp4", "avmux_mp40");
final Element sink = ElementFactory.make("filesink", "filesink0");
Element mpegq = ElementFactory.make("queue", "queue1");
final Element mpegsink = ElementFactory.make("filesink", "filesink1");
/* set all necessary pipeline attributes */
src.set("location", "D:\\backupx\\Twinkle Twinkle Little Star.mp4");
tee.set("silent", "false");
enc.set("bitrate", 4096);
sink.set("location", "D:\\movies\\new\\output.mp4");
mpegsink.set("location", "D:\\movies\\new\\output.mpg");
/* store all elements in the pipeline bin */
pipe.addMany(src, tee, h264q, decodebin, enc, mux, sink,
mpegq, mpegsink);
/* linking each element separately and check for errors */
if(!src.link(tee)) {
System.out.println("src-tee");
return;
}
if (!tee.link(h264q)) {
System.out.println("tee-h264q");
return;
}
if (!h264q.link(decodebin)) {
System.out.println("h264q-bin");
return;
}
/* need to connect a signal for dynamic pads */
decodebin.connect(new Element.PAD_ADDED() {
public void padAdded(Element e, final Pad p) {
p.link(enc.getStaticPad("sink"));
}
});
Pad np = new Pad(null, PadDirection.SRC);
decodebin.addPad(np);
if (!enc.link(mux)) {
System.out.println("Enc-Mux");
return;
}
if (!mux.link(sink)) {
System.out.println("Mux-Sink");
return;
}
if (!tee.link(mpegq)) {
System.out.println("tee-mpegq");
return;
}
if (!mpegq.link(mpegsink)) {
System.out.println("mpegq-mpegsink");
}
/* listen for EOS signal (to shut pipeline down smoothly) and error
* messages. */
bus = pipe.getBus();
bus.connect(new Bus.EOS() {
public void endOfStream(GstObject source) {
System.out.println("EOS");
pipe.setState(State.NULL);
Gst.quit();
}
});
bus.connect(new Bus.ERROR() {
public void errorMessage(GstObject arg0, int arg1, String arg2) {
System.out.println("Error: " + arg2 + " " + arg0);
Gst.quit();
}
});
/* display some detailed information about the pipeline */
//printPipeline(pipe);
/* start up pipeline. use timer thread for now */
pipe.setState(State.PLAYING);
System.out.println("Starting pipeline...");
while (pipe.getState() != State.PLAYING);
System.out.println("Pipeline started.");
new Thread(new TimerThread()).start();
Gst.main();
Gst.deinit();
}
/* Iterate through all the elements in the pipeline and display their
* caps. Useful for debugging and general information about the pipeline */
public static void printPipeline(Pipeline p) {
List<Element> elements = p.getElements();
if (elements.size() > 0) {
Iterator<Element> elemiter = elements.iterator();
Element e = null;
while (elemiter.hasNext()) {
e = (Element) elemiter.next();
List<Pad> pads = e.getPads();
if (pads.size() > 0) {
Iterator<Pad> paditer = pads.iterator();
Pad pad = null;
while (paditer.hasNext()) {
pad = (Pad) paditer.next();
System.out.print(e + " " + pad.getDirection());
System.out.println("\t" + pad.getCaps());
}
}
}
}
}
/* Used to time pipeline. A more appropriate method will be implemented later.