alternative to assert
[gtkD.git] / gtkD / demos / gstreamer / gstreamer_helloworld.d
blob4c3f7fa45d813bdc4d0373727e21e5d5ba6683cc
1 /*
2 * This file is part of gtkD.
4 * gtkD is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * gtkD is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with gtkD; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * gstreamerD helloworld.
20 * by Joonas Kiviharju
24 //Phobos imports:
26 import std.stdio;
28 //gtkD imports:
30 import gtk.GtkD;
32 //gstreamerD imports:
34 import gstreamer.gstreamer;
36 import gobject.ObjectG;
37 import gstreamer.Element;
38 import gstreamer.Pipeline;
39 import gstreamer.ElementFactory;
40 import gstreamer.Pad;
41 import gstreamer.Message;
42 import gstreamer.Bus;
44 import gstreamerc.gstreamertypes;
45 import gstreamerc.gstreamer;
47 import gtkc.glib;
50 class GstHello
52 public:
54 bool busCall( Message msg )
56 debug(gstreamer) writefln("GstHello.busCall(msg) START.");
57 debug(gstreamer) scope(exit) writefln("GstHello.busCall(msg) END.");
59 switch( msg.type )
61 case GstMessageType.UNKNOWN:
62 writefln("Unknown message type.");
63 break;
64 case GstMessageType.EOS:
65 writefln("End-of-stream.");
66 GtkD.mainQuit();
67 break;
69 case GstMessageType.ERROR:
71 gchar *dbug;
72 GError *err;
73 msg.parseError(&err, &dbug);
74 //g_free (dbug);
75 writefln("Error: %s\n", err.message);
76 //g_error_free (err);
77 GtkD.mainQuit();
78 break;
80 default:
81 break;
84 return true;
87 this(char[] file)
90 // create elements
92 pipeline = new Pipeline("audio-player");
94 source = ElementFactory.make("filesrc", "file-source");
95 parser = ElementFactory.make("oggdemux", "ogg-parser");
96 decoder = ElementFactory.make("vorbisdec", "vorbis-decoder");
97 conv = ElementFactory.make("audioconvert", "converter");
98 sink = ElementFactory.make("alsasink", "alsa-output");
100 if( pipeline is null || source is null || parser is null || decoder is null || conv is null || sink is null )
102 writefln("One or more element could not be created");
103 if( pipeline is null ) writefln(" : no pipeline.");
104 if( source is null ) writefln(" : no source.");
105 if( parser is null ) writefln(" : no parser.");
106 if( decoder is null ) writefln(" : no decoder.");
107 if( conv is null ) writefln(" : no conv.");
108 if( sink is null ) writefln(" : no sink.");
110 throw new Exception("One or more gstreamerD elements could not be created.");
113 // set filename property on the file source. Also add a message handler.
115 source.location( file ); //You can also use this like a D property: source.location = file;
116 //Or you can also do: source.setProperty("location", file);
118 pipeline.getBus().addWatch( &busCall );
120 // put all elements in a bin
122 //shouldbe, but doesn't work yet:
123 //pipeline.addMany( source, parser, decoder, conv, sink );
124 //TODO, add variable number of arguments (...) support to GtkWrapper
125 pipeline.add( source );
126 pipeline.add( parser );
127 pipeline.add( decoder );
128 pipeline.add( conv );
129 pipeline.add( sink );
131 // link together - note that we cannot link the parser and
132 // decoder yet, because the parser uses dynamic pads. For that,
133 // we set a pad-added signal handler.
134 source.link( parser );
136 //shouldbe, but doesn't work yet:
137 //decoder.linkMany( conv, sink );
138 decoder.link( conv );
139 conv.link( sink );
140 parser.addOnPadAdded(&newPad);
142 // Now set to playing and iterate.
143 writefln("Setting to PLAYING.");
144 pipeline.setState( GstState.PLAYING );
145 writefln("Running.");
148 ~this()
150 pipeline.setState( GstState.NULL );
153 void newPad( Pad pad, Element element )
155 writefln("newPad callback called. START.");
156 Pad sinkpad;
158 // We can now link this pad with the audio decoder
159 writefln("Dynamic pad created, linking parser/decoder");
161 sinkpad = decoder.getPad("sink");
162 writefln("doing a gst_pad_link.");
163 pad.link( sinkpad );
164 writefln("Done. That was ok.");
168 protected:
170 Pipeline pipeline;
171 Element source, parser, decoder, conv, sink;
175 int main(char[][] args)
177 writefln("gstreamerD Hello World!");
179 uint major, minor, micro, nano;
181 writefln("Trying to init...");
183 //GtkD.init(args);
184 GStreamer.init(args);
186 // check input arguments
187 if (args.length != 2)
189 writefln("Usage: %s <Ogg/Vorbis filename>\n", args[0]);
190 return -1;
193 writefln("Checking version of GStreamer...");
194 GStreamer.versio(&major, &minor, &micro, &nano);
195 writefln("This program is linked against GStreamer ", major, ".", minor, ".", micro );
197 writefln( "The file is: ", args[1] );
198 GstHello gstHello = new GstHello( args[1] );
200 //We must use the gtkD mainloop to run gstreamerD apps.
201 GtkD.main();
203 return 0;