]> git.sesse.net Git - mlt/blob - src/miracle/miracle_unit.c
inigo usage message
[mlt] / src / miracle / miracle_unit.c
1 /*
2  * miracle_unit.c -- DV Transmission Unit Implementation
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 <stdio.h>
26 #include <unistd.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <sys/poll.h>
30 #include <sys/types.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <signal.h>
36
37 #include <sys/mman.h>
38
39 #include "miracle_unit.h"
40 #include "miracle_log.h"
41 #include "miracle_local.h"
42
43 #include <framework/mlt.h>
44
45 /* Forward references */
46 static void miracle_unit_status_communicate( miracle_unit );
47
48 /** Allocate a new DV transmission unit.
49
50     \return A new miracle_unit handle.
51 */
52
53 miracle_unit miracle_unit_init( int index, char *constructor )
54 {
55         miracle_unit this = NULL;
56         mlt_consumer consumer = NULL;
57
58         char *id = strdup( constructor );
59         char *arg = strchr( id, ':' );
60
61         if ( arg != NULL )
62                 *arg ++ = '\0';
63
64         consumer = mlt_factory_consumer( id, arg );
65
66         if ( consumer != NULL )
67         {
68                 mlt_playlist playlist = mlt_playlist_init( );
69                 this = calloc( sizeof( miracle_unit_t ), 1 );
70                 this->properties = mlt_properties_new( );
71                 this->producers = mlt_properties_new( );
72                 mlt_properties_init( this->properties, this );
73                 mlt_properties_set_int( this->properties, "unit", index );
74                 mlt_properties_set_int( this->properties, "generation", 0 );
75                 mlt_properties_set( this->properties, "constructor", constructor );
76                 mlt_properties_set( this->properties, "id", id );
77                 mlt_properties_set( this->properties, "arg", arg );
78                 mlt_properties_set_data( this->properties, "consumer", consumer, 0, ( mlt_destructor )mlt_consumer_close, NULL );
79                 mlt_properties_set_data( this->properties, "playlist", playlist, 0, ( mlt_destructor )mlt_playlist_close, NULL );
80                 mlt_consumer_connect( consumer, mlt_playlist_service( playlist ) );
81         }
82
83         return this;
84 }
85
86 /** Communicate the current status to all threads waiting on the notifier.
87 */
88
89 static void miracle_unit_status_communicate( miracle_unit unit )
90 {
91         if ( unit != NULL )
92         {
93                 mlt_properties properties = unit->properties;
94                 char *root_dir = mlt_properties_get( properties, "root" );
95                 valerie_notifier notifier = mlt_properties_get_data( properties, "notifier", NULL );
96                 valerie_status_t status;
97
98                 if ( root_dir != NULL && notifier != NULL )
99                 {
100                         if ( miracle_unit_get_status( unit, &status ) == 0 )
101                                 /* if ( !( ( status.status == unit_playing || status.status == unit_paused ) &&
102                                                 strcmp( status.clip, "" ) && 
103                                         !strcmp( status.tail_clip, "" ) && 
104                                                 status.position == 0 && 
105                                                 status.in == 0 && 
106                                                 status.out == 0 ) ) */
107                                         valerie_notifier_put( notifier, &status );
108                 }
109         }
110 }
111
112 /** Set the notifier info
113 */
114
115 void miracle_unit_set_notifier( miracle_unit this, valerie_notifier notifier, char *root_dir )
116 {
117         mlt_properties properties = this->properties;
118         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
119         mlt_properties playlist_properties = mlt_playlist_properties( playlist );
120
121         mlt_properties_set( properties, "root", root_dir );
122         mlt_properties_set_data( properties, "notifier", notifier, 0, NULL, NULL );
123         mlt_properties_set_data( playlist_properties, "notifier_arg", this, 0, NULL, NULL );
124         mlt_properties_set_data( playlist_properties, "notifier", miracle_unit_status_communicate, 0, NULL, NULL );
125
126         miracle_unit_status_communicate( this );
127 }
128
129 /** Create or locate a producer for the file specified.
130 */
131
132 static mlt_producer create_producer( miracle_unit unit, char *file )
133 {
134         // Get the unit properties
135         mlt_properties properties = unit->producers;
136
137         // Check if we already have loaded this file
138         mlt_producer result = mlt_properties_get_data( properties, file, NULL );
139
140         if ( result == NULL )
141         {
142                 // 1st Line preferences
143                 if ( strstr( file, ".inigo" ) )
144                 {
145                         char *args[ 2 ] = { file, NULL };
146                         result = mlt_factory_producer( "inigo", args );
147                 }
148                 else if ( strstr( file, ".mpg" ) )
149                         result = mlt_factory_producer( "mcmpeg", file );
150                 else if ( strstr( file, ".mpeg" ) )
151                         result = mlt_factory_producer( "mcmpeg", file );
152                 else if ( strstr( file, ".dv" ) )
153                         result = mlt_factory_producer( "mcdv", file );
154                 else if ( strstr( file, ".dif" ) )
155                         result = mlt_factory_producer( "mcdv", file );
156                 else if ( strstr( file, ".jpg" ) )
157                         result = mlt_factory_producer( "pixbuf", file );
158                 else if ( strstr( file, ".JPG" ) )
159                         result = mlt_factory_producer( "pixbuf", file );
160                 else if ( strstr( file, ".jpeg" ) )
161                         result = mlt_factory_producer( "pixbuf", file );
162                 else if ( strstr( file, ".png" ) )
163                         result = mlt_factory_producer( "pixbuf", file );
164         
165                 // 2nd Line fallbacks
166                 if ( result == NULL && strstr( file, ".dv" ) )
167                         result = mlt_factory_producer( "libdv", file );
168                 else if ( result == NULL && strstr( file, ".dif" ) )
169                         result = mlt_factory_producer( "libdv", file );
170         
171                 // 3rd line fallbacks 
172                 if ( result == NULL )
173                         result = mlt_factory_producer( "ffmpeg", file );
174
175                 // Now store the result
176                 mlt_properties_set_data( properties, file, result, 0, ( mlt_destructor )mlt_producer_close, NULL );
177         }
178
179         return result;
180 }
181
182 /** Update the generation count.
183 */
184
185 static void update_generation( miracle_unit unit )
186 {
187         mlt_properties properties = unit->properties;
188         int generation = mlt_properties_get_int( properties, "generation" );
189         mlt_properties_set_int( properties, "generation", ++ generation );
190 }
191
192 /** Wipe all clips on the playlist for this unit.
193 */
194
195 static void clear_unit( miracle_unit unit )
196 {
197         mlt_properties properties = unit->properties;
198         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
199         mlt_producer producer = mlt_playlist_producer( playlist );
200
201         mlt_playlist_clear( playlist );
202         mlt_producer_seek( producer, 0 );
203
204         if ( unit->old_producers != NULL )
205                 mlt_properties_close( unit->old_producers );
206         unit->old_producers = unit->producers;
207         unit->producers = mlt_properties_new( );
208
209         update_generation( unit );
210 }
211
212 /** Generate a report on all loaded clips.
213 */
214
215 void miracle_unit_report_list( miracle_unit unit, valerie_response response )
216 {
217         int i;
218         mlt_properties properties = unit->properties;
219         int generation = mlt_properties_get_int( properties, "generation" );
220         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
221
222         valerie_response_printf( response, 1024, "%d\n", generation );
223                 
224         for ( i = 0; i < mlt_playlist_count( playlist ); i ++ )
225         {
226                 mlt_playlist_clip_info info;
227                 mlt_playlist_get_clip_info( playlist , &info, i );
228                 valerie_response_printf( response, 10240, "%d \"%s\" %lld %lld %lld %lld %.2f\n", 
229                                                                  i, info.resource, 
230                                                                  info.frame_in, 
231                                                                  info.frame_out,
232                                                                  info.frame_count, 
233                                                                  info.length, 
234                                                                  info.fps );
235         }
236 }
237
238 /** Load a clip into the unit clearing existing play list.
239
240     \todo error handling
241     \param unit A miracle_unit handle.
242     \param clip The absolute file name of the clip to load.
243     \param in   The starting frame (-1 for 0)
244         \param out  The ending frame (-1 for maximum)
245 */
246
247 valerie_error_code miracle_unit_load( miracle_unit unit, char *clip, int64_t in, int64_t out, int flush )
248 {
249         // Have to clear the unit first
250         clear_unit( unit );
251
252         // Now try to create an producer
253         mlt_producer instance = create_producer( unit, clip );
254
255         if ( instance != NULL )
256         {
257                 mlt_properties properties = unit->properties;
258                 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
259                 mlt_playlist_append_io( playlist, instance, in, out );
260                 miracle_log( LOG_DEBUG, "loaded clip %s", clip );
261                 miracle_unit_status_communicate( unit );
262                 return valerie_ok;
263         }
264
265         return valerie_invalid_file;
266 }
267
268 valerie_error_code miracle_unit_insert( miracle_unit unit, char *clip, int index, int64_t in, int64_t out )
269 {
270         mlt_producer instance = create_producer( unit, clip );
271
272         if ( instance != NULL )
273         {
274                 mlt_properties properties = unit->properties;
275                 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
276                 mlt_playlist_insert( playlist, instance, index, in, out );
277                 miracle_log( LOG_DEBUG, "inserted clip %s at %d", clip, index );
278                 update_generation( unit );
279                 miracle_unit_status_communicate( unit );
280                 return valerie_ok;
281         }
282
283         return valerie_invalid_file;
284 }
285
286 valerie_error_code miracle_unit_remove( miracle_unit unit, int index )
287 {
288         mlt_properties properties = unit->properties;
289         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
290         mlt_playlist_remove( playlist, index );
291         miracle_log( LOG_DEBUG, "removed clip at %d", index );
292         update_generation( unit );
293         miracle_unit_status_communicate( unit );
294         return valerie_ok;
295 }
296
297 valerie_error_code miracle_unit_clean( miracle_unit unit )
298 {
299         clear_unit( unit );
300         miracle_log( LOG_DEBUG, "Cleaned playlist" );
301         return valerie_ok;
302 }
303
304 valerie_error_code miracle_unit_move( miracle_unit unit, int src, int dest )
305 {
306         mlt_properties properties = unit->properties;
307         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
308         mlt_playlist_move( playlist, src, dest );
309         miracle_log( LOG_DEBUG, "moved clip %d to %d", src, dest );
310         update_generation( unit );
311         miracle_unit_status_communicate( unit );
312         return valerie_ok;
313 }
314
315 /** Add a clip to the unit play list.
316
317     \todo error handling
318     \param unit A miracle_unit handle.
319     \param clip The absolute file name of the clip to load.
320     \param in   The starting frame (-1 for 0)
321         \param out  The ending frame (-1 for maximum)
322 */
323
324 valerie_error_code miracle_unit_append( miracle_unit unit, char *clip, int64_t in, int64_t out )
325 {
326         mlt_producer instance = create_producer( unit, clip );
327
328         if ( instance != NULL )
329         {
330                 mlt_properties properties = unit->properties;
331                 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
332                 mlt_playlist_append_io( playlist, instance, in, out );
333                 miracle_log( LOG_DEBUG, "appended clip %s", clip );
334                 update_generation( unit );
335                 miracle_unit_status_communicate( unit );
336                 return valerie_ok;
337         }
338
339         return valerie_invalid_file;
340 }
341
342 /** Start playing the clip.
343
344     Start a dv-pump and commence dv1394 transmission.
345
346     \todo error handling
347     \param unit A miracle_unit handle.
348     \param speed An integer that specifies the playback rate as a
349                  percentage multiplied by 100.
350 */
351
352 void miracle_unit_play( miracle_unit_t *unit, int speed )
353 {
354         mlt_properties properties = unit->properties;
355         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
356         mlt_producer producer = mlt_playlist_producer( playlist );
357         mlt_producer_set_speed( producer, ( double )speed / 1000 );
358         miracle_unit_status_communicate( unit );
359 }
360
361 /** Stop playback.
362
363     Terminates the dv_pump and halts dv1394 transmission.
364
365     \param unit A miracle_unit handle.
366 */
367
368 void miracle_unit_terminate( miracle_unit unit )
369 {
370 }
371
372 /** Query the status of unit playback.
373
374     \param unit A miracle_unit handle.
375     \return 1 if the unit is not playing, 0 if playing.
376 */
377
378 int miracle_unit_has_terminated( miracle_unit unit )
379 {
380         return 0;
381 }
382
383 /** Transfer the currently loaded clip to another unit
384 */
385
386 int miracle_unit_transfer( miracle_unit dest_unit, miracle_unit src_unit )
387 {
388         return 0;
389 }
390
391 /** Determine if unit is offline.
392 */
393
394 int miracle_unit_is_offline( miracle_unit unit )
395 {
396         return 0;
397 }
398
399 /** Obtain the status for a given unit
400 */
401
402 int miracle_unit_get_status( miracle_unit unit, valerie_status status )
403 {
404         int error = unit == NULL;
405
406         memset( status, 0, sizeof( valerie_status_t ) );
407
408         if ( !error )
409         {
410                 mlt_properties properties = unit->properties;
411                 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
412                 mlt_producer producer = mlt_playlist_producer( playlist );
413                 mlt_producer clip = mlt_playlist_current( playlist );
414
415                 mlt_playlist_clip_info info;
416                 int clip_index = mlt_playlist_current_clip( playlist );
417                 mlt_playlist_get_clip_info( playlist, &info, clip_index );
418
419                 if ( info.resource != NULL && strcmp( info.resource, "" ) )
420                 {
421                         strncpy( status->clip, info.resource, sizeof( status->clip ) );
422                         status->speed = (int)( mlt_producer_get_speed( producer ) * 1000.0 );
423                         status->fps = mlt_producer_get_fps( producer );
424                         status->in = info.frame_in;
425                         status->out = info.frame_out;
426                         status->position = mlt_producer_position( clip );
427                         status->length = mlt_producer_get_length( clip );
428                         strncpy( status->tail_clip, info.resource, sizeof( status->tail_clip ) );
429                         status->tail_in = info.frame_in;
430                         status->tail_out = info.frame_out;
431                         status->tail_position = mlt_producer_position( clip );
432                         status->tail_length = mlt_producer_get_length( clip );
433                         status->clip_index = mlt_playlist_current_clip( playlist );
434                         status->seek_flag = 1;
435                 }
436
437                 status->generation = mlt_properties_get_int( properties, "generation" );
438
439                 if ( !strcmp( status->clip, "" ) )
440                         status->status = unit_not_loaded;
441                 else if ( status->speed == 0 )
442                         status->status = unit_paused;
443                 else
444                         status->status = unit_playing;
445         }
446         else
447         {
448                 status->status = unit_undefined;
449         }
450
451         status->unit = mlt_properties_get_int( unit->properties, "unit" );
452
453         return error;
454 }
455
456 /** Change position in the playlist.
457 */
458
459 void miracle_unit_change_position( miracle_unit unit, int clip, int64_t position )
460 {
461         mlt_properties properties = unit->properties;
462         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
463         mlt_producer producer = mlt_playlist_producer( playlist );
464         mlt_playlist_clip_info info;
465
466         if ( clip < 0 )
467         {
468                 clip = 0;
469                 position = 0;
470         }
471         else if ( clip >= mlt_playlist_count( playlist ) )
472         {
473                 clip = mlt_playlist_count( playlist ) - 1;
474                 position = 999999999999;
475         }
476
477         if ( mlt_playlist_get_clip_info( playlist, &info, clip ) == 0 )
478         {
479                 int64_t frame_start = info.start;
480                 int64_t frame_offset = position;
481
482                 if ( frame_offset < 0 )
483                         frame_offset = info.frame_out;
484                 if ( frame_offset < info.frame_in )
485                         frame_offset = info.frame_in;
486                 if ( frame_offset >= info.frame_out )
487                         frame_offset = info.frame_out;
488                 
489                 mlt_producer_seek( producer, frame_start + frame_offset - info.frame_in );
490         }
491
492         miracle_unit_status_communicate( unit );
493 }
494
495 /** Get the index of the current clip.
496 */
497
498 int     miracle_unit_get_current_clip( miracle_unit unit )
499 {
500         mlt_properties properties = unit->properties;
501         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
502         int clip_index = mlt_playlist_current_clip( playlist );
503         return clip_index;
504 }
505
506 /** Set a clip's in point
507 */
508
509 int miracle_unit_set_clip_in( miracle_unit unit, int index, int64_t position )
510 {
511         mlt_properties properties = unit->properties;
512         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
513         mlt_playlist_clip_info info;
514         int error = mlt_playlist_get_clip_info( playlist, &info, index );
515
516         if ( error == 0 )
517         {
518                 miracle_unit_play( unit, 0 );
519                 error = mlt_playlist_resize_clip( playlist, index, position, info.frame_out );
520                 update_generation( unit );
521                 miracle_unit_change_position( unit, index, 0 );
522         }
523
524         return error;
525 }
526
527 /** Set a clip's out point.
528 */
529
530 int miracle_unit_set_clip_out( miracle_unit unit, int index, int64_t position )
531 {
532         mlt_properties properties = unit->properties;
533         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
534         mlt_playlist_clip_info info;
535         int error = mlt_playlist_get_clip_info( playlist, &info, index );
536
537         if ( error == 0 )
538         {
539                 miracle_unit_play( unit, 0 );
540                 error = mlt_playlist_resize_clip( playlist, index, info.frame_in, position );
541                 update_generation( unit );
542                 miracle_unit_status_communicate( unit );
543                 miracle_unit_change_position( unit, index, -1 );
544         }
545
546         return error;
547 }
548
549 /** Step by specified position.
550 */
551
552 void miracle_unit_step( miracle_unit unit, int64_t offset )
553 {
554         mlt_properties properties = unit->properties;
555         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
556         mlt_producer producer = mlt_playlist_producer( playlist );
557         mlt_position position = mlt_producer_frame( producer );
558         mlt_producer_seek( producer, position + offset );
559 }
560
561 /** Set the unit's clip mode regarding in and out points.
562 */
563
564 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
565 //{
566         //dv_player player = miracle_unit_get_dv_player( unit );
567         //if ( player != NULL )
568                 //dv_player_set_clip_mode( player, mode );
569         //miracle_unit_status_communicate( unit );
570 //}
571
572 /** Get the unit's clip mode regarding in and out points.
573 */
574
575 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
576 //{
577         //dv_player player = miracle_unit_get_dv_player( unit );
578         //return dv_player_get_clip_mode( player );
579 //}
580
581 /** Set the unit's clip mode regarding eof handling.
582 */
583
584 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
585 //{
586         //dv_player player = miracle_unit_get_dv_player( unit );
587         //dv_player_set_eof_action( player, action );
588         //miracle_unit_status_communicate( unit );
589 //}
590
591 /** Get the unit's clip mode regarding eof handling.
592 */
593
594 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
595 //{
596         //dv_player player = miracle_unit_get_dv_player( unit );
597         //return dv_player_get_eof_action( player );
598 //}
599
600 int miracle_unit_set( miracle_unit unit, char *name_value )
601 {
602         mlt_playlist playlist = mlt_properties_get_data( unit->properties, "playlist", NULL );
603         mlt_properties properties = mlt_playlist_properties( playlist );
604         return mlt_properties_parse( properties, name_value );
605 }
606
607 char *miracle_unit_get( miracle_unit unit, char *name )
608 {
609         mlt_playlist playlist = mlt_properties_get_data( unit->properties, "playlist", NULL );
610         mlt_properties properties = mlt_playlist_properties( playlist );
611         return mlt_properties_get( properties, name );
612 }
613
614 /** Release the unit
615
616     \todo error handling
617     \param unit A miracle_unit handle.
618 */
619
620 void miracle_unit_close( miracle_unit unit )
621 {
622         if ( unit != NULL )
623         {
624                 miracle_log( LOG_DEBUG, "closing unit..." );
625                 if ( unit->old_producers != NULL )
626                         mlt_properties_close( unit->old_producers );
627                 mlt_properties_close( unit->properties );
628                 mlt_properties_close( unit->producers );
629                 free( unit );
630                 miracle_log( LOG_DEBUG, "... unit closed." );
631         }
632 }
633