2 * melted_unit_commands.c
3 * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
25 #include <sys/types.h>
33 #include "melted_unit.h"
34 #include "melted_commands.h"
35 #include "melted_log.h"
38 void get_fullname( command_argument cmd_arg
, char *fullname
, size_t len
, char *filename
)
40 char *service
= strchr( filename
, ':' );
42 if ( service
!= NULL
)
45 filename
= strchr( service
, ':' );
48 if ( strlen( cmd_arg
->root_dir
) && filename
[0] == '/' )
51 snprintf( fullname
, len
, "%s:%s%s", service
, cmd_arg
->root_dir
, filename
);
55 if ( strlen( cmd_arg
->root_dir
) && filename
[0] == '/' )
58 snprintf( fullname
, len
, "%s%s", cmd_arg
->root_dir
, filename
);
62 int melted_load( command_argument cmd_arg
)
64 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
65 char *filename
= (char*) cmd_arg
->argument
;
69 if ( filename
[0] == '!' )
74 get_fullname( cmd_arg
, fullname
, sizeof(fullname
), filename
);
77 return RESPONSE_INVALID_UNIT
;
80 int32_t in
= -1, out
= -1;
81 if ( mvcp_tokeniser_count( cmd_arg
->tokeniser
) == 5 )
83 in
= atol( mvcp_tokeniser_get_string( cmd_arg
->tokeniser
, 3 ) );
84 out
= atol( mvcp_tokeniser_get_string( cmd_arg
->tokeniser
, 4 ) );
86 if ( melted_unit_load( unit
, fullname
, in
, out
, flush
) != mvcp_ok
)
87 return RESPONSE_BAD_FILE
;
89 return RESPONSE_SUCCESS
;
92 int melted_list( command_argument cmd_arg
)
94 melted_unit unit
= melted_get_unit( cmd_arg
->unit
);
98 melted_unit_report_list( unit
, cmd_arg
->response
);
99 return RESPONSE_SUCCESS
;
102 return RESPONSE_INVALID_UNIT
;
105 static int parse_clip( command_argument cmd_arg
, int arg
)
107 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
108 int clip
= melted_unit_get_current_clip( unit
);
110 if ( mvcp_tokeniser_count( cmd_arg
->tokeniser
) > arg
)
112 char *token
= mvcp_tokeniser_get_string( cmd_arg
->tokeniser
, arg
);
113 if ( token
[ 0 ] == '+' )
114 clip
+= atoi( token
+ 1 );
115 else if ( token
[ 0 ] == '-' )
116 clip
-= atoi( token
+ 1 );
118 clip
= atoi( token
);
124 int melted_insert( command_argument cmd_arg
)
126 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
127 char *filename
= (char*) cmd_arg
->argument
;
130 get_fullname( cmd_arg
, fullname
, sizeof(fullname
), filename
);
133 return RESPONSE_INVALID_UNIT
;
136 long in
= -1, out
= -1;
137 int index
= parse_clip( cmd_arg
, 3 );
139 if ( mvcp_tokeniser_count( cmd_arg
->tokeniser
) == 6 )
141 in
= atoi( mvcp_tokeniser_get_string( cmd_arg
->tokeniser
, 4 ) );
142 out
= atoi( mvcp_tokeniser_get_string( cmd_arg
->tokeniser
, 5 ) );
145 switch( melted_unit_insert( unit
, fullname
, index
, in
, out
) )
148 return RESPONSE_SUCCESS
;
150 return RESPONSE_BAD_FILE
;
153 return RESPONSE_SUCCESS
;
156 int melted_remove( command_argument cmd_arg
)
158 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
161 return RESPONSE_INVALID_UNIT
;
164 int index
= parse_clip( cmd_arg
, 2 );
166 if ( melted_unit_remove( unit
, index
) != mvcp_ok
)
167 return RESPONSE_BAD_FILE
;
169 return RESPONSE_SUCCESS
;
172 int melted_clean( command_argument cmd_arg
)
174 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
177 return RESPONSE_INVALID_UNIT
;
180 if ( melted_unit_clean( unit
) != mvcp_ok
)
181 return RESPONSE_BAD_FILE
;
183 return RESPONSE_SUCCESS
;
186 int melted_wipe( command_argument cmd_arg
)
188 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
191 return RESPONSE_INVALID_UNIT
;
194 if ( melted_unit_wipe( unit
) != mvcp_ok
)
195 return RESPONSE_BAD_FILE
;
197 return RESPONSE_SUCCESS
;
200 int melted_clear( command_argument cmd_arg
)
202 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
205 return RESPONSE_INVALID_UNIT
;
208 if ( melted_unit_clear( unit
) != mvcp_ok
)
209 return RESPONSE_BAD_FILE
;
211 return RESPONSE_SUCCESS
;
214 int melted_move( command_argument cmd_arg
)
216 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
220 if ( mvcp_tokeniser_count( cmd_arg
->tokeniser
) > 2 )
222 int src
= parse_clip( cmd_arg
, 2 );
223 int dest
= parse_clip( cmd_arg
, 3 );
225 if ( melted_unit_move( unit
, src
, dest
) != mvcp_ok
)
226 return RESPONSE_BAD_FILE
;
230 return RESPONSE_MISSING_ARG
;
235 return RESPONSE_INVALID_UNIT
;
238 return RESPONSE_SUCCESS
;
241 int melted_append( command_argument cmd_arg
)
243 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
244 char *filename
= (char*) cmd_arg
->argument
;
247 get_fullname( cmd_arg
, fullname
, sizeof(fullname
), filename
);
250 return RESPONSE_INVALID_UNIT
;
253 int32_t in
= -1, out
= -1;
254 if ( mvcp_tokeniser_count( cmd_arg
->tokeniser
) == 5 )
256 in
= atol( mvcp_tokeniser_get_string( cmd_arg
->tokeniser
, 3 ) );
257 out
= atol( mvcp_tokeniser_get_string( cmd_arg
->tokeniser
, 4 ) );
259 switch ( melted_unit_append( unit
, fullname
, in
, out
) )
262 return RESPONSE_SUCCESS
;
264 return RESPONSE_BAD_FILE
;
267 return RESPONSE_SUCCESS
;
270 int melted_push( command_argument cmd_arg
, mlt_service service
)
272 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
274 return RESPONSE_INVALID_UNIT
;
275 if ( service
!= NULL
)
276 if ( melted_unit_append_service( unit
, service
) == mvcp_ok
)
277 return RESPONSE_SUCCESS
;
278 return RESPONSE_BAD_FILE
;
281 int melted_receive( command_argument cmd_arg
, char *doc
)
283 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
285 return RESPONSE_INVALID_UNIT
;
288 // Get the consumer's profile
289 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
290 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( consumer
) );
291 mlt_producer producer
= mlt_factory_producer( profile
, "xml-string", doc
);
292 if ( producer
!= NULL
)
294 if ( melted_unit_append_service( unit
, MLT_PRODUCER_SERVICE( producer
) ) == mvcp_ok
)
296 mlt_producer_close( producer
);
297 return RESPONSE_SUCCESS
;
299 mlt_producer_close( producer
);
302 return RESPONSE_BAD_FILE
;
305 int melted_play( command_argument cmd_arg
)
307 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
311 return RESPONSE_INVALID_UNIT
;
316 if ( mvcp_tokeniser_count( cmd_arg
->tokeniser
) == 3 )
317 speed
= atoi( mvcp_tokeniser_get_string( cmd_arg
->tokeniser
, 2 ) );
318 melted_unit_play( unit
, speed
);
321 return RESPONSE_SUCCESS
;
324 int melted_stop( command_argument cmd_arg
)
326 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
328 return RESPONSE_INVALID_UNIT
;
330 melted_unit_terminate( unit
);
331 return RESPONSE_SUCCESS
;
334 int melted_pause( command_argument cmd_arg
)
336 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
338 return RESPONSE_INVALID_UNIT
;
340 melted_unit_play( unit
, 0 );
341 return RESPONSE_SUCCESS
;
344 int melted_rewind( command_argument cmd_arg
)
346 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
348 return RESPONSE_INVALID_UNIT
;
349 else if ( melted_unit_has_terminated( unit
) )
350 melted_unit_change_position( unit
, 0, 0 );
352 melted_unit_play( unit
, -2000 );
353 return RESPONSE_SUCCESS
;
356 int melted_step( command_argument cmd_arg
)
358 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
361 return RESPONSE_INVALID_UNIT
;
364 melted_unit_play( unit
, 0 );
365 melted_unit_step( unit
, *(int*) cmd_arg
->argument
);
367 return RESPONSE_SUCCESS
;
370 int melted_goto( command_argument cmd_arg
)
372 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
373 int clip
= parse_clip( cmd_arg
, 3 );
375 if (unit
== NULL
|| melted_unit_is_offline(unit
))
376 return RESPONSE_INVALID_UNIT
;
378 melted_unit_change_position( unit
, clip
, *(int*) cmd_arg
->argument
);
379 return RESPONSE_SUCCESS
;
382 int melted_ff( command_argument cmd_arg
)
384 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
386 return RESPONSE_INVALID_UNIT
;
387 else if ( melted_unit_has_terminated( unit
) )
388 melted_unit_change_position( unit
, 0, 0 );
390 melted_unit_play( unit
, 2000 );
391 return RESPONSE_SUCCESS
;
394 int melted_set_in_point( command_argument cmd_arg
)
396 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
397 int clip
= parse_clip( cmd_arg
, 3 );
400 return RESPONSE_INVALID_UNIT
;
403 int position
= *(int *) cmd_arg
->argument
;
405 switch( melted_unit_set_clip_in( unit
, clip
, position
) )
408 return RESPONSE_BAD_FILE
;
410 return RESPONSE_OUT_OF_RANGE
;
413 return RESPONSE_SUCCESS
;
416 int melted_set_out_point( command_argument cmd_arg
)
418 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
419 int clip
= parse_clip( cmd_arg
, 3 );
422 return RESPONSE_INVALID_UNIT
;
425 int position
= *(int *) cmd_arg
->argument
;
427 switch( melted_unit_set_clip_out( unit
, clip
, position
) )
430 return RESPONSE_BAD_FILE
;
432 return RESPONSE_OUT_OF_RANGE
;
436 return RESPONSE_SUCCESS
;
439 int melted_get_unit_status( command_argument cmd_arg
)
441 mvcp_status_t status
;
442 int error
= melted_unit_get_status( melted_get_unit( cmd_arg
->unit
), &status
);
445 return RESPONSE_INVALID_UNIT
;
449 mvcp_response_printf( cmd_arg
->response
, sizeof( text
), mvcp_status_serialise( &status
, text
, sizeof( text
) ) );
450 return RESPONSE_SUCCESS_1
;
456 int melted_set_unit_property( command_argument cmd_arg
)
458 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
459 char *name_value
= (char*) cmd_arg
->argument
;
461 return RESPONSE_INVALID_UNIT
;
463 melted_unit_set( unit
, name_value
);
464 return RESPONSE_SUCCESS
;
467 int melted_get_unit_property( command_argument cmd_arg
)
469 melted_unit unit
= melted_get_unit(cmd_arg
->unit
);
470 char *name
= (char*) cmd_arg
->argument
;
473 return RESPONSE_INVALID_UNIT
;
477 char *value
= melted_unit_get( unit
, name
);
479 mvcp_response_printf( cmd_arg
->response
, 1024, "%s\n", value
);
481 return RESPONSE_SUCCESS
;
485 int melted_transfer( command_argument cmd_arg
)
487 melted_unit src_unit
= melted_get_unit(cmd_arg
->unit
);
488 int dest_unit_id
= -1;
489 char *string
= (char*) cmd_arg
->argument
;
490 if ( string
!= NULL
&& ( string
[ 0 ] == 'U' || string
[ 0 ] == 'u' ) && strlen( string
) > 1 )
491 dest_unit_id
= atoi( string
+ 1 );
493 if ( src_unit
!= NULL
&& dest_unit_id
!= -1 )
495 melted_unit dest_unit
= melted_get_unit( dest_unit_id
);
496 if ( dest_unit
!= NULL
&& !melted_unit_is_offline(dest_unit
) && dest_unit
!= src_unit
)
498 melted_unit_transfer( dest_unit
, src_unit
);
499 return RESPONSE_SUCCESS
;
502 return RESPONSE_INVALID_UNIT
;