Run time type identification
[melted] / mlt++ / HOWTO
1 INTRODUCTION
2 ------------
3
4         This document provides a brief tutorial on the use of the mlt++ wrapper 
5         and bindings.
6
7
8 Hello World
9 -----------
10
11         The mlt++ wrapper is a c++ wrapper for the mlt C library. As such, it 
12         provides clean C++ access to the underlying library.
13
14         An example of use is as follows:
15
16         #include <time.h>
17         #include <mlt++/Mlt.h>
18         using namespace Mlt;
19
20         int main( void )
21         {
22                 Factory::init( );
23                 Producer p( "pango:" );
24                 p.set( "text", "Hello World" );
25                 Consumer c( "sdl" );
26                 c.connect( p );
27                 c.start( );
28                 struct timespec tm = { 1, 0 };
29                 while ( !c.is_stopped( ) )
30                         nanosleep( &tm, NULL );
31                 return 0;
32         }
33
34         This is a fairly typical example of use of mlt++ - create a 'producer' (an
35         object which produces 'frames'), create a 'consumer' (an object which consumes
36         frames), connect them together, start the consumer and wait until done (here
37         we just wait for the user to close the window).
38
39         In this case, we construct a window as a consumer using the 'sdl' consumer
40         (SDL is a standard portable library which provides platform independent
41         access to accelerated video display and audio) and use the 'pango' 
42         producer to generate frames with the words 'Hello World' (pango is a 
43         library from the gtk toolkit).
44
45         The main point of this example is to show that mlt uses existing libraries
46         to provide its functionality - this keeps the framework itself very small.
47
48         Note that mlt is designed to be housed in GUI or server type applications -
49         typically, applications don't wait around for the consumer to be stopped in
50         the manner shown.
51
52         TODO: Replace wait loop with an event.
53
54         So far, we've introduced the Producer and Consumer mlt classes. We'll cover
55         each of these in more detail later in the tutorial, but for now, we'll 
56         briefly cover the remaining classes.
57
58
59 Playlists
60 ---------
61
62         Another simple class is the Playlist - this is direct extension of Producer
63         and it allows you to maintain a list of producer objects.
64
65         As a simple example of the Playlist in action, we'll convert the example
66         above into an application which plays multiple video or audio files.
67
68         #include <time.h>
69         #include <mlt++/Mlt.h>
70         using namespace Mlt;
71
72         int main( int argc, char **argv )
73         {
74                 Factory::init( );
75                 Playlist list;
76                 for ( int i = 1; i < argc; i ++ )
77                 {
78                         Producer p( argv[i] );
79                         if ( p.is_valid( ) )
80                                 list.append( p );
81                 }
82                 Consumer c( "sdl" );
83                 c.connect( list );
84                 c.start( );
85                 struct timespec tm = { 1, 0 };
86                 while ( !c.is_stopped( ) )
87                         nanosleep( &tm, NULL );
88                 return 0;
89         }
90
91         Now you can run the program as:
92
93                 ./player *.avi *.mp3 *.jpg etc
94
95         In this case, we construct a playlist by simply appending producers to it.
96         Notice that although the scope of the Producer is limited to the inner 
97         for loop, we can safely add it to the playlist - this is due to the fact
98         that all mlt objects maintain reference counts and no object is really
99         destroyed until all the references are gone. In this case, when the list
100         object goes out of scope, all the producers we created will automatically
101         be destroyed.
102
103
104 Filters
105 -------
106
107         So far, we've shown how you can load and play media. We've given a brief
108         intro to the Playlist container, now it's time to start manipulating 
109         things...
110
111         For the next example, I'll add a 'watermark' to the video - a watermark
112         is used by broadcasters to brand the channel and normally consists of a 
113         logo of some sort. We'll just use some black text on a partially 
114         transparent red background.
115
116         #include <time.h>
117         #include <mlt++/Mlt.h>
118         using namespace Mlt;
119
120         int main( int argc, char **argv )
121         {
122                 Factory::init( );
123                 Playlist list;
124                 for ( int i = 1; i < argc; i ++ )
125                 {
126                         Producer p( argv[i] );
127                         if ( p.is_valid( ) )
128                                 list.append( p );
129                 }
130                 Filter f( "watermark", "pango:" );
131                 f.set( "producer.text", "MLT++" );
132                 f.set( "producer.fgcolour", "0x000000ff" );
133                 f.set( "producer.bgcolour", "0xff000080" );
134                 list.attach( f );
135                 Consumer c( "sdl" );
136                 c.connect( list );
137                 c.start( );
138                 struct timespec tm = { 1, 0 };
139                 while ( !c.is_stopped( ) )
140                         nanosleep( &tm, NULL );
141                 return 0;
142         }
143
144         Notice that the watermark filter reuses the 'pango' producer we showed in the
145         first example. In fact, you could use any producer here - if you wanted to
146         use a graphic or a video, you would just construct the filter with a full path
147         to that as the second argument.
148
149         We manipulate the filter using the set method - this method was also shown
150         in the first example. 
151
152         Finally, we attach the filter to the playlist. This ensure that all frames 
153         that are obtained from the playlist are watermarked. 
154
155
156 Tractor
157 -------
158
159         A tractor is an object that allows the manipulation of multiple video and audio
160         tracks. 
161
162         Stepping away from the player example we've been tinkering with for a minute,
163         let's assume we want to do something like dubbing a video with some audio. This
164         a very trivial thing to do:
165
166         Tractor *dub( char *video_file, char *audio_file )
167         {
168                 Tractor *tractor = new Tractor( );
169                 Producer video( video_file );
170                 Producer audio( audio_file );
171                 tractor->set_track( video, 0 );
172                 tractor->set_track( audio, 1 );
173                 return tractor;
174         }
175
176         That's all that needs to be done - you can now connect the returned object to a
177         consumer, or add it to a playlist, or even apply it as a track to another tractor.
178
179
180 Transition
181 ----------
182
183         Let's now assume we want to mix the audio between two tracks - to do this, we 
184         need to introduce the concept of a transition. A transition in mlt is a service
185         which combines frames from two producers to produce a new frame.
186
187         Tractor *mix( char *video_file, char *audio_file )
188         {
189                 Tractor *tractor = new Tractor( );
190                 Transition mix( "mix" );
191                 Producer video( video_file );
192                 Producer audio( audio_file );
193                 tractor.set_track( video, 0 );
194                 tractor.set_track( audio, 1 );
195                 tractor.field.plant_transition( mix, 0, 1 );
196                 return tractor;
197         }
198
199         The tractor returned will now mix the audio from the original video and the audio.
200
201
202 That's All Folks...
203 -------------------
204
205         And that, believe it or not, is a fairly complete summary of the classes you'll 
206         typically be interfacing with in mlt++. Obviously, there's a little more to it 
207         than this - a couple of intrisinc classes have been glossed over (notably, the 
208         Properties and Service base classes). The next section will cover all of the 
209         above, but in much more detail...
210
211
212 DIGGING DEEPER
213 --------------
214
215         The previous section was designed to give you a whistle stop tour through the major
216         framework classes. This section will take you through the scenic route.
217
218
219 Introducing Base Classes
220 ------------------------
221
222         Services in mlt are the collective noun for Producers, Filters, Transitions and 
223         Consumer. A Service is also the base class from which all of these classes 
224         extend. It provides the basic connectivity which has been shown throughout the
225         examples in the previous section.
226
227         Properties are the main way in which we communicate with the Services - 
228         essentially, it provides get/set methods for named values. All services extend
229         Properties.
230
231
232 Properties
233 ----------
234
235         Properties provide the general mechanism for communicating with Services - 
236         through the Properties interface, we are able to manipulate and serialise 
237         a services state.
238
239         For example, to dump all the properties to stdout, you can use something 
240         like:
241
242         void dump( Properties &properties )
243         {
244                 for ( int i = 0; i < properties.count( ); i ++ )
245                         cout << Properties.get_name( i ) << " = " << Properties.get( i ) << endl;
246         }
247
248         Note that the properties object handles type conversion, so the following
249         is acceptable:
250
251         properties.set( "hello", "10.5" );
252         int hello_int = properties.get_int( "hello" );
253         double hello_double = properties.get_double( "hello" );
254
255         A couple of convenience methods are provide to examine or serialise property
256         objects. 
257
258         For example:
259
260         properties.debug( );
261
262         will report all serialisable properties on stderr, in the form:
263
264         Object: [ ref=1, in=0, out=0, track=0, u=75, v=150, _unique_id=15, 
265         mlt_type=filter, mlt_service=sepia ]
266
267
268 Services
269 --------
270
271         Typically, all the services are constructed via the specific classes 
272         constructor. Often, you will receive Service objects rather than their
273         specific type. In order to access the extended classes interface, 
274         you will need to create a reference.
275
276         For example, given an arbitrary Service object, you can determine its
277         type by using the type method - this will return a 'service_type' which
278         has values of producer_type, filter_type etc. Alternatively, you can
279         create a wrapping object and check on its validity.
280
281         bool do_we_have_a_producer( Service &service )
282         {
283                 Producer producer( service );
284                 return producer.is_valid( );
285         }
286
287