src/framework/mlt_consumer.c src/framework/mlt_consumer.h
[melted] / src / inigo / inigo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sched.h>
5
6 #include <framework/mlt.h>
7
8 #include "io.h"
9
10 static void transport_action( mlt_producer producer, char *value )
11 {
12 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
13 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
14 mlt_consumer consumer = mlt_properties_get_data( properties, "transport_consumer", NULL );
15
16 mlt_properties_set_int( properties, "stats_off", 0 );
17
18 if ( strlen( value ) == 1 )
19 {
20 switch( value[ 0 ] )
21 {
22 case 'q':
23 mlt_properties_set_int( properties, "done", 1 );
24 break;
25 case '0':
26 mlt_producer_set_speed( producer, 1 );
27 mlt_producer_seek( producer, 0 );
28 break;
29 case '1':
30 mlt_producer_set_speed( producer, -10 );
31 break;
32 case '2':
33 mlt_producer_set_speed( producer, -5 );
34 break;
35 case '3':
36 mlt_producer_set_speed( producer, -2 );
37 break;
38 case '4':
39 mlt_producer_set_speed( producer, -1 );
40 break;
41 case '5':
42 mlt_producer_set_speed( producer, 0 );
43 break;
44 case '6':
45 case ' ':
46 mlt_producer_set_speed( producer, 1 );
47 break;
48 case '7':
49 mlt_producer_set_speed( producer, 2 );
50 break;
51 case '8':
52 mlt_producer_set_speed( producer, 5 );
53 break;
54 case '9':
55 mlt_producer_set_speed( producer, 10 );
56 break;
57 case 'd':
58 if ( multitrack != NULL )
59 {
60 int i = 0;
61 mlt_position last = -1;
62 fprintf( stderr, "\n" );
63 for ( i = 0; 1; i ++ )
64 {
65 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_start, i );
66 if ( time == last )
67 break;
68 last = time;
69 fprintf( stderr, "%d: %d\n", i, time );
70 }
71 }
72 break;
73
74 case 'g':
75 if ( multitrack != NULL )
76 {
77 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 0 );
78 mlt_producer_seek( producer, time );
79 }
80 break;
81 case 'H':
82 if ( producer != NULL )
83 {
84 mlt_position position = mlt_producer_position( producer );
85 mlt_producer_seek( producer, position - ( mlt_producer_get_fps( producer ) * 60 ) );
86 }
87 break;
88 case 'h':
89 if ( producer != NULL )
90 {
91 mlt_position position = mlt_producer_position( producer );
92 mlt_producer_set_speed( producer, 0 );
93 mlt_producer_seek( producer, position - 1 );
94 }
95 break;
96 case 'j':
97 if ( multitrack != NULL )
98 {
99 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, 1 );
100 mlt_producer_seek( producer, time );
101 }
102 break;
103 case 'k':
104 if ( multitrack != NULL )
105 {
106 mlt_position time = mlt_multitrack_clip( multitrack, mlt_whence_relative_current, -1 );
107 mlt_producer_seek( producer, time );
108 }
109 break;
110 case 'l':
111 if ( producer != NULL )
112 {
113 mlt_position position = mlt_producer_position( producer );
114 mlt_producer_set_speed( producer, 0 );
115 mlt_producer_seek( producer, position + 1 );
116 }
117 break;
118 case 'L':
119 if ( producer != NULL )
120 {
121 mlt_position position = mlt_producer_position( producer );
122 mlt_producer_seek( producer, position + ( mlt_producer_get_fps( producer ) * 60 ) );
123 }
124 break;
125 }
126
127 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "refresh", 1 );
128 }
129
130 mlt_properties_set_int( properties, "stats_off", 0 );
131 }
132
133 static mlt_consumer create_consumer( char *id, mlt_producer producer )
134 {
135 char *arg = id != NULL ? strchr( id, ':' ) : NULL;
136 if ( arg != NULL )
137 *arg ++ = '\0';
138 mlt_consumer consumer = mlt_factory_consumer( id, arg );
139 if ( consumer != NULL )
140 {
141 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
142 mlt_properties_set_data( properties, "transport_callback", transport_action, 0, NULL, NULL );
143 mlt_properties_set_data( properties, "transport_producer", producer, 0, NULL, NULL );
144 mlt_properties_set_data( MLT_PRODUCER_PROPERTIES( producer ), "transport_consumer", consumer, 0, NULL, NULL );
145 }
146 return consumer;
147 }
148
149 static void transport( mlt_producer producer, mlt_consumer consumer )
150 {
151 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
152 int silent = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "silent" );
153 struct timespec tm = { 0, 40000 };
154
155 if ( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
156 {
157 if ( !silent )
158 {
159 term_init( );
160
161 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
162 fprintf( stderr, "|1=-10| |2= -5| |3= -2| |4= -1| |5= 0| |6= 1| |7= 2| |8= 5| |9= 10|\n" );
163 fprintf( stderr, "+-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+\n" );
164
165 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
166 fprintf( stderr, "| H = back 1 minute, L = forward 1 minute |\n" );
167 fprintf( stderr, "| h = previous frame, l = next frame |\n" );
168 fprintf( stderr, "| g = start of clip, j = next clip, k = previous clip |\n" );
169 fprintf( stderr, "| 0 = restart, q = quit, space = play |\n" );
170 fprintf( stderr, "+---------------------------------------------------------------------+\n" );
171 }
172
173 while( mlt_properties_get_int( properties, "done" ) == 0 && !mlt_consumer_is_stopped( consumer ) )
174 {
175 int value = silent ? -1 : term_read( );
176
177 if ( value != -1 )
178 transport_action( producer, ( char * )&value );
179
180 if ( !silent && mlt_properties_get_int( properties, "stats_off" ) == 0 )
181 fprintf( stderr, "Current Position: %10d\r", mlt_producer_position( producer ) );
182
183 if ( silent )
184 nanosleep( &tm, NULL );
185 }
186
187 if ( !silent )
188 fprintf( stderr, "\n" );
189 }
190 }
191
192 int main( int argc, char **argv )
193 {
194 int i;
195 mlt_consumer consumer = NULL;
196 mlt_producer inigo = NULL;
197 FILE *store = NULL;
198 char *name = NULL;
199 struct sched_param scp;
200
201 // Use realtime scheduling if possible
202 memset( &scp, '\0', sizeof( scp ) );
203 scp.sched_priority = sched_get_priority_max( SCHED_FIFO ) - 1;
204 #ifndef __DARWIN__
205 sched_setscheduler( 0, SCHED_FIFO, &scp );
206 #endif
207
208 // Construct the factory
209 mlt_factory_init( NULL );
210
211 // Check for serialisation switch first
212 for ( i = 1; i < argc; i ++ )
213 {
214 if ( !strcmp( argv[ i ], "-serialise" ) )
215 {
216 name = argv[ ++ i ];
217 if ( strstr( name, ".inigo" ) )
218 store = fopen( name, "w" );
219 }
220 }
221
222 // Get inigo producer
223 if ( argc > 1 )
224 inigo = mlt_factory_producer( "inigo", &argv[ 1 ] );
225
226 if ( argc > 1 && inigo != NULL && mlt_producer_get_length( inigo ) > 0 )
227 {
228 // Get inigo's properties
229 mlt_properties inigo_props = MLT_PRODUCER_PROPERTIES( inigo );
230
231 // Get the last group
232 mlt_properties group = mlt_properties_get_data( inigo_props, "group", 0 );
233
234 // Parse the arguments
235 for ( i = 1; i < argc; i ++ )
236 {
237 if ( !strcmp( argv[ i ], "-consumer" ) )
238 {
239 consumer = create_consumer( argv[ ++ i ], inigo );
240 while ( argv[ i + 1 ] != NULL && strstr( argv[ i + 1 ], "=" ) )
241 mlt_properties_parse( group, argv[ ++ i ] );
242 }
243 else if ( !strcmp( argv[ i ], "-serialise" ) )
244 {
245 i ++;
246 }
247 else
248 {
249 if ( store != NULL )
250 fprintf( store, "%s\n", argv[ i ] );
251
252 i ++;
253
254 while ( argv[ i ] != NULL && argv[ i ][ 0 ] != '-' )
255 {
256 if ( store != NULL )
257 fprintf( store, "%s\n", argv[ i ] );
258 i += 1;
259 }
260
261 i --;
262 }
263 }
264
265 // If we have no consumer, default to sdl
266 if ( store == NULL && consumer == NULL )
267 consumer = create_consumer( NULL, inigo );
268
269 if ( consumer != NULL && store == NULL )
270 {
271 // Apply group settings
272 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
273 mlt_properties_inherit( properties, group );
274
275 // Connect consumer to inigo
276 mlt_consumer_connect( consumer, MLT_PRODUCER_SERVICE( inigo ) );
277
278 // Start the consumer
279 mlt_consumer_start( consumer );
280
281 // Transport functionality
282 transport( inigo, consumer );
283
284 // Stop the consumer
285 mlt_consumer_stop( consumer );
286 }
287 else if ( store != NULL )
288 {
289 fprintf( stderr, "Project saved as %s.\n", name );
290 fclose( store );
291 }
292 }
293 else
294 {
295 fprintf( stderr, "Usage: inigo [ -group [ name=value ]* ]\n"
296 " [ -consumer id[:arg] [ name=value ]* ]\n"
297 " [ -filter filter[:arg] [ name=value ] * ]\n"
298 " [ -attach filter[:arg] [ name=value ] * ]\n"
299 " [ -mix length [ -mixer transition ]* ]\n"
300 " [ -transition id[:arg] [ name=value ] * ]\n"
301 " [ -blank frames ]\n"
302 " [ -track ]\n"
303 " [ -split relative-frame ]\n"
304 " [ -join clips ]\n"
305 " [ -repeat times ]\n"
306 " [ producer [ name=value ] * ]+\n" );
307 }
308
309 // Close the consumer
310 if ( consumer != NULL )
311 mlt_consumer_close( consumer );
312
313 // Close the producer
314 if ( inigo != NULL )
315 mlt_producer_close( inigo );
316
317 // Close the factory
318 mlt_factory_close( );
319
320 return 0;
321 }