framework: remove global profile, rather share one mlt_profile across a service netwo...
[melted] / src / inigo / inigo.c
1 /*
2 * inigo.c -- MLT command line utility
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sched.h>
25
26 #include <framework/mlt.h>
27
28 #ifdef __DARWIN__
29 #include <SDL.h>
30 #endif
31
32 #include "io.h"
33
34 static void transport_action( mlt_producer producer, char *value )
35 {
36 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
37 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
38 mlt_consumer consumer = mlt_properties_get_data( properties, "transport_consumer", NULL );
39
40 mlt_properties_set_int( properties, "stats_off", 0 );
41
42 if ( strlen( value ) == 1 )
43 {
44 switch( value[ 0 ] )
45 {
46 case 'q':
47 mlt_properties_set_int( properties, "done", 1 );
48 break;
49 case '0':
50 mlt_producer_set_speed( producer, 1 );
51 mlt_producer_seek( producer, 0 );
52 break;
53 case '1':
54 mlt_producer_set_speed( producer, -10 );
55 break;
56 case '2':
57 mlt_producer_set_speed( producer, -5 );
58 break;
59 case '3':
60 mlt_producer_set_speed( producer, -2 );
61 break;
62 case '4':
63 mlt_producer_set_speed( producer, -1 );
64 break;
65 case '5':
66 mlt_producer_set_speed( producer, 0 );
67 break;
68 case '6':
69 case ' ':
70 mlt_producer_set_speed( producer, 1 );
71 break;
72 case '7':
73 mlt_producer_set_speed( producer, 2 );
74 break;
75 case '8':
76 mlt_producer_set_speed( producer, 5 );
77 break;
78 case '9':
79 mlt_producer_set_speed( producer, 10 );
80 break;
81 case 'd':
82 if ( multitrack != NULL )
83 {
84 int i = 0;
85 mlt_position last = -1;
86 fprintf( stderr, "\n" );
87 for ( i = 0; 1; i ++ )
88 {
89 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_start, i );
90 if ( time == last )
91 break;
92 last = time;
93 fprintf( stderr, "%d: %d\n", i, (int)time );
94 }
95 }
96 break;
97
98 case 'g':
99 if ( multitrack != NULL )
100 {
101 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 0 );
102 mlt_producer_seek( producer, time );
103 }
104 break;
105 case 'H':
106 if ( producer != NULL )
107 {
108 mlt_position position = mlt_producer_position( producer );
109 mlt_producer_seek( producer, position - ( mlt_producer_get_fps( producer ) * 60 ) );
110 }
111 break;
112 case 'h':
113 if ( producer != NULL )
114 {
115 mlt_position position = mlt_producer_position( producer );
116 mlt_producer_set_speed( producer, 0 );
117 mlt_producer_seek( producer, position - 1 );
118 }
119 break;
120 case 'j':
121 if ( multitrack != NULL )
122 {
123 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 1 );
124 mlt_producer_seek( producer, time );
125 }
126 break;
127 case 'k':
128 if ( multitrack != NULL )
129 {
130 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, -1 );
131 mlt_producer_seek( producer, time );
132 }
133 break;
134 case 'l':
135 if ( producer != NULL )
136 {
137 mlt_position position = mlt_producer_position( producer );
138 if ( mlt_producer_get_speed( producer ) != 0 )
139 mlt_producer_set_speed( producer, 0 );
140 else
141 mlt_producer_seek( producer, position + 1 );
142 }
143 break;
144 case 'L':
145 if ( producer != NULL )
146 {
147 mlt_position position = mlt_producer_position( producer );
148 mlt_producer_seek( producer, position + ( mlt_producer_get_fps( producer ) * 60 ) );
149 }
150 break;
151 }
152
153 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
154 }
155
156 mlt_properties_set_int( properties, "stats_off", 0 );
157 }
158
159 static mlt_consumer create_consumer( mlt_profile profile, char *id )
160 {
161 char *arg = id != NULL ? strchr( id, ':' ) : NULL;
162 if ( arg != NULL )
163 *arg ++ = '\0';
164 mlt_consumer consumer = mlt_factory_consumer( profile, id, arg );
165 if ( consumer != NULL )
166 {
167 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
168 mlt_properties_set_data( properties, "transport_callback", transport_action, 0, NULL, NULL );
169 }
170 return consumer;
171 }
172
173 #ifdef __DARWIN__
174
175 static void event_handling( mlt_producer producer, mlt_consumer consumer )
176 {
177 SDL_Event event;
178
179 while ( SDL_PollEvent( &event ) )
180 {
181 switch( event.type )
182 {
183 case SDL_QUIT:
184 mlt_properties_set_int( MLT_PRODUCER_PROPERTIES( consumer ), "done", 1 );
185 break;
186
187 case SDL_KEYDOWN:
188 if ( event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
189 {
190 char keyboard[ 2 ] = { event.key.keysym.unicode, 0 };
191 transport_action( producer, keyboard );
192 }
193 break;
194 }
195 }
196 }
197
198 #endif
199
200 static void transport( mlt_producer producer, mlt_consumer consumer )
201 {
202 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
203 int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
204 int progress = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "progress" );
205 struct timespec tm = { 0, 40000 };
206 int total_length = mlt_producer_get_length( producer );
207 int last_position = 0;
208
209 if ( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
210 {
211 if ( !silent && !progress )
212 {
213 term_init( );
214
215 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
216 fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5= 0| |6= 1| |7= 2| |8= 5| |9= 10|\n" );
217 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
218
219 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
220 fprintf( stderr, "| H = back 1 minute, L = forward 1 minute |\n" );
221 fprintf( stderr, "| h = previous frame, l = next frame |\n" );
222 fprintf( stderr, "| g = start of clip, j = next clip, k = previous clip |\n" );
223 fprintf( stderr, "| 0 = restart, q = quit, space = play |\n" );
224 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
225 }
226
227 while( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
228 {
229 int value = ( silent || progress )? -1 : term_read( );
230
231 if ( value != -1 )
232 {
233 char string[ 2 ] = { value, 0 };
234 transport_action( producer, string );
235 }
236
237 #ifdef __DARWIN__
238 event_handling( producer, consumer );
239 #endif
240
241 if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
242 {
243 if ( progress )
244 {
245 int current_position = mlt_producer_position( producer );
246 if ( current_position > last_position )
247 {
248 fprintf( stderr, "Current Frame: %10d, percentage: %10d\r",
249 current_position, 100 * current_position / total_length );
250 last_position = current_position;
251 }
252 }
253 else
254 {
255 fprintf( stderr, "Current Position: %10d\r", (int)mlt_producer_position( producer ) );
256 }
257 }
258
259 if ( silent )
260 nanosleep( &tm, NULL );
261 }
262
263 if ( !silent )
264 fprintf( stderr, "\n" );
265 }
266 }
267
268 int main( int argc, char **argv )
269 {
270 int i;
271 mlt_consumer consumer = NULL;
272 mlt_producer inigo = NULL;
273 FILE *store = NULL;
274 char *name = NULL;
275 struct sched_param scp;
276 mlt_profile profile = NULL;
277
278 // Use realtime scheduling if possible
279 memset( &scp, '\0', sizeof( scp ) );
280 scp.sched_priority = sched_get_priority_max( SCHED_FIFO ) - 1;
281 #ifndef __DARWIN__
282 sched_setscheduler( 0, SCHED_FIFO, &scp );
283 #endif
284
285 // Construct the factory
286 mlt_factory_init( NULL );
287
288 for ( i = 1; i < argc; i ++ )
289 {
290 // Check for serialisation switch
291 if ( !strcmp( argv[ i ], "-serialise" ) )
292 {
293 name = argv[ ++ i ];
294 if ( name != NULL && strstr( name, ".inigo" ) )
295 store = fopen( name, "w" );
296 else
297 {
298 if ( name == NULL || name[0] == '-' )
299 store = stdout;
300 name = NULL;
301 }
302 }
303 // Look for the profile option
304 else if ( !strcmp( argv[ i ], "-profile" ) )
305 {
306 const char *pname = argv[ ++ i ];
307 if ( pname && pname[0] != '-' )
308 profile = mlt_profile_init( pname );
309 }
310 }
311
312 // Create profile if not set explicitly
313 if ( profile == NULL )
314 profile = mlt_profile_init( NULL );
315
316 // Look for the consumer option
317 for ( i = 1; i < argc; i ++ )
318 {
319 if ( !strcmp( argv[ i ], "-consumer" ) )
320 {
321 consumer = create_consumer( profile, argv[ ++ i ] );
322 if ( consumer )
323 {
324 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
325 while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
326 mlt_properties_parse( properties, argv[ ++ i ] );
327 }
328 }
329 }
330
331 // If we have no consumer, default to sdl
332 if ( store == NULL && consumer == NULL )
333 consumer = create_consumer( profile, NULL );
334
335 // Get inigo producer
336 if ( argc > 1 )
337 inigo = mlt_factory_producer( profile, "inigo", &argv[ 1 ] );
338
339 // Set transport properties on consumer and produder
340 if ( consumer != NULL && inigo != NULL )
341 {
342 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES( consumer ), "transport_producer", inigo, 0, NULL, NULL );
343 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( inigo ), "transport_consumer", consumer, 0, NULL, NULL );
344 }
345
346 if ( argc > 1 && inigo != NULL && mlt_producer_get_length( inigo ) > 0 )
347 {
348 // Parse the arguments
349 for ( i = 1; i < argc; i ++ )
350 {
351 if ( !strcmp( argv[ i ], "-serialise" ) )
352 {
353 if ( store != stdout )
354 i ++;
355 }
356 else
357 {
358 if ( store != NULL )
359 fprintf( store, "%s\n", argv[ i ] );
360
361 i ++;
362
363 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
364 {
365 if ( store != NULL )
366 fprintf( store, "%s\n", argv[ i ] );
367 i += 1;
368 }
369
370 i --;
371 }
372 }
373
374 if ( consumer != NULL && store == NULL )
375 {
376 // Get inigo's properties
377 mlt_properties inigo_props = MLT_PRODUCER_PROPERTIES( inigo );
378
379 // Get the last group
380 mlt_properties group = mlt_properties_get_data( inigo_props, "group", 0 );
381
382 // Apply group settings
383 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
384 mlt_properties_inherit( properties, group );
385
386 // Connect consumer to inigo
387 mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE( inigo ) );
388
389 // Start the consumer
390 mlt_consumer_start( consumer );
391
392 // Transport functionality
393 transport( inigo, consumer );
394
395 // Stop the consumer
396 mlt_consumer_stop( consumer );
397 }
398 else if ( store != NULL && store != stdout && name != NULL )
399 {
400 fprintf( stderr, "Project saved as %s.\n", name );
401 fclose( store );
402 }
403 }
404 else
405 {
406 fprintf( stderr, "Usage: inigo [ -profile name ]\n"
407 " [ -serialise [ filename.inigo ] ]\n"
408 " [ -group [ name=value ]* ]\n"
409 " [ -consumer id[:arg] [ name=value ]* [ silent=1 ] [ progress=1 ] ]\n"
410 " [ -filter filter[:arg] [ name=value ]* ]\n"
411 " [ -attach filter[:arg] [ name=value ]* ]\n"
412 " [ -mix length [ -mixer transition ]* ]\n"
413 " [ -transition id[:arg] [ name=value ]* ]\n"
414 " [ -blank frames ]\n"
415 " [ -track ]\n"
416 " [ -split relative-frame ]\n"
417 " [ -join clips ]\n"
418 " [ -repeat times ]\n"
419 " [ producer [ name=value ]* ]+\n" );
420 }
421
422 // Close the consumer
423 if ( consumer != NULL )
424 mlt_consumer_close( consumer );
425
426 // Close the producer
427 if ( inigo != NULL )
428 mlt_producer_close( inigo );
429
430 // Close the factory
431 mlt_profile_close( profile );
432 mlt_factory_close( );
433
434 return 0;
435 }