]> git.sesse.net Git - mlt/blob - src/miracle/miracle_unit_commands.c
Initial revision
[mlt] / src / miracle / miracle_unit_commands.c
1 /*
2  * unit_commands.c
3  * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
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 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "dvunit.h"
33 #include "global_commands.h"
34 #include "dverror.h"
35 #include "dvframepool.h"
36 #include "log.h"
37
38 int dv1394d_load( command_argument cmd_arg )
39 {
40         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
41         char *filename = (char*) cmd_arg->argument;
42         char fullname[1024];
43         int flush = 1;
44
45         if ( filename[0] == '!' )
46         {
47                 flush = 0;
48                 filename ++;
49         }
50
51         if ( filename[0] == '/' )
52                 filename++;
53
54         snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
55         
56         if (unit == NULL)
57                 return RESPONSE_INVALID_UNIT;
58         else
59         {
60                 long in = -1, out = -1;
61                 if ( dv_tokeniser_count( cmd_arg->tokeniser ) == 5 )
62                 {
63                         in = atoi( dv_tokeniser_get_string( cmd_arg->tokeniser, 3 ) );
64                         out = atoi( dv_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
65                 }
66                 if ( dv_unit_load( unit, fullname, in, out, flush ) != dv_pump_ok )
67                         return RESPONSE_BAD_FILE;
68         }
69         return RESPONSE_SUCCESS;
70 }
71
72 int dv1394d_list( command_argument cmd_arg )
73 {
74         int i = 0;
75         dv_unit unit = dv1394d_get_unit( cmd_arg->unit );
76         dv_player player = dv_unit_get_dv_player( unit );
77         
78         if ( player != NULL )
79         {
80                 dv_response_printf( cmd_arg->response, 1024, "%d\n", player->generation );
81                 
82                 for ( i = 0; i < dv_player_get_clip_count( player ); i ++ )
83                 {
84                         dv_clip clip = dv_player_get_clip( player, i );
85                         
86                         dv_response_printf( cmd_arg->response, 10240,
87                                                                 "%d \"%s\" %d %d %d %d %.2f\n", 
88                                                                 i,
89                                                                 dv_clip_get_resource( clip, cmd_arg->root_dir ),
90                                                                 dv_clip_get_in( clip ),
91                                                                 ( !dv_clip_is_seekable( clip ) && clip->out_frame == -1 ? -1 : dv_clip_get_out( clip ) ),
92                                                                 dv_clip_get_max_frames( clip ),
93                                                                 ( !dv_clip_is_seekable( clip ) && clip->out_frame == -1 ? -1 : dv_player_get_length_of_clip( player, i ) ),
94                                                                 dv_clip_frames_per_second( clip ) );
95                 }
96         
97                 dv_response_printf( cmd_arg->response, 2, "\n" );
98                 
99                 return RESPONSE_SUCCESS;
100         }
101         return RESPONSE_INVALID_UNIT;
102 }
103
104 static int parse_clip( command_argument cmd_arg, int arg )
105 {
106         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
107         int clip = dv_unit_get_current_clip( unit );
108         
109         if ( dv_tokeniser_count( cmd_arg->tokeniser ) > arg )
110         {
111                 dv_player player = dv_unit_get_dv_player( unit );
112                 char *token = dv_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 );
117                 else
118                         clip = atoi( token );
119                 if ( clip < 0 )
120                         clip = 0;
121                 if ( clip >= player->size )
122                         clip = player->size - 1;
123         }
124         
125         return clip;
126 }
127
128 int dv1394d_insert( command_argument cmd_arg )
129 {
130         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
131         char *filename = (char*) cmd_arg->argument;
132         char fullname[1024];
133
134         if ( filename[0] == '/' )
135                 filename++;
136
137         snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
138         
139         if (unit == NULL)
140                 return RESPONSE_INVALID_UNIT;
141         else
142         {
143                 long in = -1, out = -1;
144                 int index = parse_clip( cmd_arg, 3 );
145                 
146                 if ( dv_tokeniser_count( cmd_arg->tokeniser ) == 6 )
147                 {
148                         in = atoi( dv_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
149                         out = atoi( dv_tokeniser_get_string( cmd_arg->tokeniser, 5 ) );
150                 }
151                 
152                 switch( dv_unit_insert( unit, fullname, index, in, out ) )
153                 {
154                         case dv_pump_ok:
155                                 return RESPONSE_SUCCESS;
156                         case dv_pump_too_many_files_open:
157                                 return RESPONSE_TOO_MANY_FILES;
158                         default:
159                                 return RESPONSE_BAD_FILE;
160                 }
161         }
162         return RESPONSE_SUCCESS;
163 }
164
165 int dv1394d_remove( command_argument cmd_arg )
166 {
167         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
168         
169         if (unit == NULL)
170                 return RESPONSE_INVALID_UNIT;
171         else
172         {
173                 int index = parse_clip( cmd_arg, 2 );
174                         
175                 if ( dv_unit_remove( unit, index ) != dv_pump_ok )
176                         return RESPONSE_BAD_FILE;
177         }
178         return RESPONSE_SUCCESS;
179 }
180
181 int dv1394d_clean( command_argument cmd_arg )
182 {
183         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
184         
185         if (unit == NULL)
186                 return RESPONSE_INVALID_UNIT;
187         else
188         {
189                 if ( dv_unit_clean( unit ) != dv_pump_ok )
190                         return RESPONSE_BAD_FILE;
191         }
192         return RESPONSE_SUCCESS;
193 }
194
195 int dv1394d_move( command_argument cmd_arg )
196 {
197         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
198         
199         if ( unit != NULL )
200         {
201                 if ( dv_tokeniser_count( cmd_arg->tokeniser ) > 2 )
202                 {
203                         int src = parse_clip( cmd_arg, 2 );
204                         int dest = parse_clip( cmd_arg, 3 );
205                         
206                         if ( dv_unit_move( unit, src, dest ) != dv_pump_ok )
207                                 return RESPONSE_BAD_FILE;
208                 }
209                 else
210                 {
211                         return RESPONSE_MISSING_ARG;
212                 }
213         }
214         else
215         {
216                 return RESPONSE_INVALID_UNIT;
217         }
218         
219         return RESPONSE_SUCCESS;
220 }
221
222 int dv1394d_append( command_argument cmd_arg )
223 {
224         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
225         char *filename = (char*) cmd_arg->argument;
226         char fullname[1024];
227
228         if ( filename[0] == '/' )
229                 filename++;
230         snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
231         
232         if (unit == NULL)
233                 return RESPONSE_INVALID_UNIT;
234         else
235         {
236                 long in = -1, out = -1;
237                 if ( dv_tokeniser_count( cmd_arg->tokeniser ) == 5 )
238                 {
239                         in = atoi( dv_tokeniser_get_string( cmd_arg->tokeniser, 3 ) );
240                         out = atoi( dv_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
241                 }
242                 switch ( dv_unit_append( unit, fullname, in, out ) )
243                 {
244                         case dv_pump_ok:
245                                 return RESPONSE_SUCCESS;
246                         case dv_pump_too_many_files_open:
247                                 return RESPONSE_TOO_MANY_FILES;
248                         default:
249                                 return RESPONSE_BAD_FILE;
250                 }
251         }
252         return RESPONSE_SUCCESS;
253 }
254
255 int dv1394d_play( command_argument cmd_arg )
256 {
257         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
258         
259         if (unit == NULL || dv_unit_is_offline(unit))
260                 return RESPONSE_INVALID_UNIT;
261         else
262         {
263                 int speed = 1000;
264                 if ( dv_tokeniser_count( cmd_arg->tokeniser ) == 3 )
265                         speed = atoi( dv_tokeniser_get_string( cmd_arg->tokeniser, 2 ) );
266                 dv_unit_play( unit, speed );
267         }
268         
269         return RESPONSE_SUCCESS;
270 }
271
272 int dv1394d_stop( command_argument cmd_arg )
273 {
274         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
275         
276         if (unit == NULL || dv_unit_is_offline(unit))
277                 return RESPONSE_INVALID_UNIT;
278         else
279                 dv_unit_terminate( unit );
280         
281         return RESPONSE_SUCCESS;
282 }
283
284 int dv1394d_pause( command_argument cmd_arg )
285 {
286         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
287         
288         if (unit == NULL || dv_unit_is_offline(unit))
289                 return RESPONSE_INVALID_UNIT;
290         else 
291                 dv_unit_play( unit, 0 );
292         
293         return RESPONSE_SUCCESS;
294 }
295
296 int dv1394d_rewind( command_argument cmd_arg )
297 {
298         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
299         
300         if (unit == NULL || dv_unit_is_offline(unit))
301                 return RESPONSE_INVALID_UNIT;
302         else
303                 dv_unit_change_speed( unit, -2000 );
304         
305         return RESPONSE_SUCCESS;
306 }
307
308 int dv1394d_step( command_argument cmd_arg )
309 {
310         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
311         
312         if (unit == NULL || dv_unit_is_offline(unit))
313                 return RESPONSE_INVALID_UNIT;
314         else
315         {
316                 dv_unit_play( unit, 0 );
317                 dv_unit_step( unit, *(int*) cmd_arg->argument );
318         }
319         
320         return RESPONSE_SUCCESS;
321 }
322
323 int dv1394d_goto( command_argument cmd_arg )
324 {
325         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
326         int clip = parse_clip( cmd_arg, 3 );
327         
328         if (unit == NULL || dv_unit_is_offline(unit))
329                 return RESPONSE_INVALID_UNIT;
330         else
331                 dv_unit_change_position( unit, clip, *(int*) cmd_arg->argument );
332         
333         return RESPONSE_SUCCESS;
334 }
335
336 int dv1394d_ff( command_argument cmd_arg )
337 {
338         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
339         
340         if (unit == NULL || dv_unit_is_offline(unit))
341                 return RESPONSE_INVALID_UNIT;
342         else
343                 dv_unit_change_speed( unit, 2000 );
344         
345         return RESPONSE_SUCCESS;
346 }
347
348 int dv1394d_set_in_point( command_argument cmd_arg )
349 {
350         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
351         int clip = parse_clip( cmd_arg, 3 );
352         
353         if (unit == NULL || dv_unit_is_offline(unit))
354                 return RESPONSE_INVALID_UNIT;
355         else
356         {
357                 int position = *(int *) cmd_arg->argument;
358
359                 switch( dv_unit_set_clip_in( unit, clip, position ) )
360                 {
361                         case -1:
362                                 return RESPONSE_BAD_FILE;
363                         case -2:
364                                 return RESPONSE_OUT_OF_RANGE;
365                 }
366         }
367         
368         return RESPONSE_SUCCESS;
369 }
370
371 int dv1394d_set_out_point( command_argument cmd_arg )
372 {
373         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
374         int clip = parse_clip( cmd_arg, 3 );
375         
376         if (unit == NULL || dv_unit_is_offline(unit))
377                 return RESPONSE_INVALID_UNIT;
378         else
379         {
380                 int position = *(int *) cmd_arg->argument;
381
382                 switch( dv_unit_set_clip_out( unit, clip, position ) )
383                 {
384                         case -1:
385                                 return RESPONSE_BAD_FILE;
386                         case -2:
387                                 return RESPONSE_OUT_OF_RANGE;
388                 }
389         }
390         
391         return RESPONSE_SUCCESS;
392 }
393
394 int dv1394d_get_unit_status( command_argument cmd_arg )
395 {
396         dv1394_status_t status;
397         int error = dv_unit_get_status( dv1394d_get_unit( cmd_arg->unit ), &status );
398
399         if ( error == -1 )
400                 return RESPONSE_INVALID_UNIT;
401         else
402         {
403                 char text[ 10240 ];
404
405                 dv_response_printf( cmd_arg->response, 
406                                                         sizeof( text ), 
407                                                         dv1394_status_serialise( &status, text, sizeof( text ) ) );
408
409                 return RESPONSE_SUCCESS_1;
410         }
411         
412         return 0;
413 }
414
415
416 int dv1394d_set_unit_property( command_argument cmd_arg )
417 {
418         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
419         
420         if (unit == NULL)
421                 return RESPONSE_INVALID_UNIT;
422         else
423         {
424                 char *key = (char*) cmd_arg->argument;
425                 char *value = NULL;
426
427                 value = strchr( key, '=' );
428                 if (value == NULL)
429                         return RESPONSE_OUT_OF_RANGE;
430                 value[0] = 0;
431                 value++;
432                 dv1394d_log( LOG_DEBUG, "USET %s = %s", key, value );
433                 if ( strncasecmp( key, "eof", 1024) == 0 )
434                 {
435                         if ( strncasecmp( value, "pause", 1024) == 0)
436                                 dv_unit_set_eof_action( unit, dv_player_pause );
437                         else if ( strncasecmp( value, "loop", 1024) == 0)
438                                 dv_unit_set_eof_action( unit, dv_player_loop );
439                         else if ( strncasecmp( value, "stop", 1024) == 0)
440                                 dv_unit_set_eof_action( unit, dv_player_terminate );
441                         else if ( strncasecmp( value, "clean", 1024) == 0)
442                                 dv_unit_set_eof_action( unit, dv_player_clean_loop );
443                         else
444                                 return RESPONSE_OUT_OF_RANGE;
445                 }
446                 else if ( strncasecmp( key, "points", 1024) == 0 )
447                 {
448                         if ( strncasecmp( value, "use", 1024) == 0)
449                                 dv_unit_set_mode( unit, dv_clip_mode_restricted );
450                         else if ( strncasecmp( value, "ignore", 1024) == 0)
451                                 dv_unit_set_mode( unit, dv_clip_mode_unrestricted );
452                         else
453                                 return RESPONSE_OUT_OF_RANGE;
454                 }
455                 else if ( strncasecmp( key, "syt_offset", 1024) == 0 )
456                 {
457                         dv_unit_set_syt_offset( unit, atoi( value ) );
458                 }
459                 else if ( strncasecmp( key, "cip_n", 1024) == 0 )
460                 {
461                         dv_unit_set_cip_n( unit, atoi( value ) );
462                 }
463                 else if ( strncasecmp( key, "cip_d", 1024) == 0 )
464                 {
465                         dv_unit_set_cip_d( unit, atoi( value ) );
466                 }
467                 else if ( strncasecmp( key, "size", 1024) == 0 )
468                 {
469                         dv_unit_set_buffer_size( unit, atoi( value ) );
470                 }
471                 else if ( strncasecmp( key, "n_frames", 1024) == 0 )
472                 {
473                         dv_unit_set_n_frames( unit, atoi( value ) );
474                 }               
475                 else if ( strncasecmp( key, "n_fill", 1024) == 0 )
476                 {
477                         dv_unit_set_n_fill( unit, atoi( value ) );
478                 }               
479                 else
480                         return RESPONSE_OUT_OF_RANGE;
481         }
482         
483         return RESPONSE_SUCCESS;
484 }
485
486 int dv1394d_get_unit_property( command_argument cmd_arg )
487 {
488         dv_unit unit = dv1394d_get_unit(cmd_arg->unit);
489         
490         if (unit == NULL)
491                 return RESPONSE_INVALID_UNIT;
492         else
493         {
494                 char *key = (char*) cmd_arg->argument;
495
496                 if ( strncasecmp( key, "eof", 1024) == 0 )
497                 {
498                         switch ( dv_unit_get_eof_action( unit ) )
499                         {
500                                 case dv_player_pause:
501                                         dv_response_write( cmd_arg->response, "pause", strlen("pause") );
502                                         break;
503                                 case dv_player_loop:
504                                         dv_response_write( cmd_arg->response, "loop", strlen("loop") );
505                                         break;
506                                 case dv_player_terminate:
507                                         dv_response_write( cmd_arg->response, "stop", strlen("stop") );
508                                         break;
509                                 case dv_player_clean_loop:
510                                         dv_response_write( cmd_arg->response, "clean", strlen("clean") );
511                                         break;
512                         }
513                         return RESPONSE_SUCCESS_1;
514                 }
515                 else if ( strncasecmp( key, "points", 1024) == 0 )
516                 {
517                         if ( dv_unit_get_mode( unit ) == dv_clip_mode_restricted )
518                                 dv_response_write( cmd_arg->response, "use", strlen("use") );
519                         else
520                                 dv_response_write( cmd_arg->response, "ignore", strlen("ignore") );
521                         return RESPONSE_SUCCESS_1;
522                 }
523                 else if ( strncasecmp( key, "syt_offset", 1024) == 0 )
524                 {
525                         dv_response_printf( cmd_arg->response, 1024, "%d\n",
526                                 dv_unit_get_syt_offset( unit ) );
527                         return RESPONSE_SUCCESS_1;
528                 }
529                 else if ( strncasecmp( key, "cip_n", 1024) == 0 )
530                 {
531                         dv_response_printf( cmd_arg->response, 1024, "%d\n",
532                                 dv_unit_get_cip_n( unit ) );
533                         return RESPONSE_SUCCESS_1;
534                 }
535                 else if ( strncasecmp( key, "cip_d", 1024) == 0 )
536                 {
537                         dv_response_printf( cmd_arg->response, 1024, "%d\n",
538                                 dv_unit_get_cip_d( unit ) );
539                         return RESPONSE_SUCCESS_1;
540                 }
541                 else if ( strncasecmp( key, "size", 1024) == 0 )
542                 {
543                         dv_response_printf( cmd_arg->response, 1024, "%d\n",
544                                 dv_unit_get_buffer_size( unit ) );
545                         return RESPONSE_SUCCESS_1;
546                 }
547                 else if ( strncasecmp( key, "n_frames", 1024) == 0 )
548                 {
549                         dv_response_printf( cmd_arg->response, 1024, "%d\n",
550                                 dv_unit_get_n_frames( unit ) );
551                         return RESPONSE_SUCCESS_1;
552                 }
553                 else if ( strncasecmp( key, "n_fill", 1024) == 0 )
554                 {
555                         dv_response_printf( cmd_arg->response, 1024, "%d\n",
556                                 dv_unit_get_n_fill( unit ) );
557                         return RESPONSE_SUCCESS_1;
558                 }
559                 else if ( strncasecmp( key, "all", 1024 ) == 0 )
560                 {
561                         switch ( dv_unit_get_eof_action( unit ) )
562                         {
563                                 case dv_player_pause:
564                                         dv_response_write( cmd_arg->response, "eof=pause\n", strlen("pause") );
565                                         break;
566                                 case dv_player_loop:
567                                         dv_response_write( cmd_arg->response, "eof=loop\n", strlen("loop") );
568                                         break;
569                                 case dv_player_terminate:
570                                         dv_response_write( cmd_arg->response, "eof=stop\n", strlen("stop") );
571                                         break;
572                                 case dv_player_clean_loop:
573                                         dv_response_write( cmd_arg->response, "eof=clean\n", strlen("clean") );
574                                         break;
575                         }
576                         if ( dv_unit_get_mode( unit ) == dv_clip_mode_restricted )
577                                 dv_response_write( cmd_arg->response, "points=use\n", strlen("use") );
578                         else
579                                 dv_response_write( cmd_arg->response, "points=ignore\n", strlen("ignore") );
580                         dv_response_printf( cmd_arg->response, 1024, "syt_offset=%d\n", dv_unit_get_syt_offset( unit ) );
581                         dv_response_printf( cmd_arg->response, 1024, "cip_n=%d\n", dv_unit_get_cip_n( unit ) );
582                         dv_response_printf( cmd_arg->response, 1024, "cip_d=%d\n", dv_unit_get_cip_d( unit ) );
583                         dv_response_printf( cmd_arg->response, 1024, "size=%d\n", dv_unit_get_buffer_size( unit ) );
584                         dv_response_printf( cmd_arg->response, 1024, "n_frames=%d\n", dv_unit_get_n_frames( unit ) );
585                         dv_response_printf( cmd_arg->response, 1024, "n_fill=%d\n", dv_unit_get_n_fill( unit ) );
586                 }
587         }
588         
589         return RESPONSE_SUCCESS;
590 }
591
592
593 int dv1394d_transfer( command_argument cmd_arg )
594 {
595         dv_unit src_unit = dv1394d_get_unit(cmd_arg->unit);
596         int dest_unit_id = -1;
597         char *string = (char*) cmd_arg->argument;
598         if ( string != NULL && ( string[ 0 ] == 'U' || string[ 0 ] == 'u' ) && strlen( string ) > 1 )
599                 dest_unit_id = atoi( string + 1 );
600         
601         if ( src_unit != NULL && dest_unit_id != -1 )
602         {
603                 dv_unit dest_unit = dv1394d_get_unit( dest_unit_id );
604                 if ( dest_unit != NULL && !dv_unit_is_offline(dest_unit) && dest_unit != src_unit )
605                 {
606                         dv_unit_transfer( dest_unit, src_unit );
607                         return RESPONSE_SUCCESS;
608                 }
609         }
610         
611         return RESPONSE_INVALID_UNIT;
612 }