2 * miracle_unit.c -- DV Transmission Unit Implementation
3 * Copyright (C) 2002-2003 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.
27 #include <sys/ioctl.h>
30 #include <sys/types.h>
40 #include "miracle_unit.h"
41 #include "miracle_log.h"
42 #include "miracle_local.h"
44 #include <framework/mlt.h>
46 /* Forward references */
47 static void miracle_unit_status_communicate( miracle_unit
);
49 /** Allocate a new DV transmission unit.
51 \return A new miracle_unit handle.
54 miracle_unit
miracle_unit_init( int index
, char *constructor
)
56 miracle_unit
this = NULL
;
57 mlt_consumer consumer
= NULL
;
59 char *id
= strdup( constructor
);
60 char *arg
= strchr( id
, ':' );
65 consumer
= mlt_factory_consumer( id
, arg
);
67 if ( consumer
!= NULL
)
69 mlt_playlist playlist
= mlt_playlist_init( );
70 this = calloc( sizeof( miracle_unit_t
), 1 );
71 this->properties
= mlt_properties_new( );
72 this->producers
= mlt_properties_new( );
73 mlt_properties_init( this->properties
, this );
74 mlt_properties_set_int( this->properties
, "unit", index
);
75 mlt_properties_set_int( this->properties
, "generation", 0 );
76 mlt_properties_set( this->properties
, "constructor", constructor
);
77 mlt_properties_set( this->properties
, "id", id
);
78 mlt_properties_set( this->properties
, "arg", arg
);
79 mlt_properties_set_data( this->properties
, "consumer", consumer
, 0, ( mlt_destructor
)mlt_consumer_close
, NULL
);
80 mlt_properties_set_data( this->properties
, "playlist", playlist
, 0, ( mlt_destructor
)mlt_playlist_close
, NULL
);
81 mlt_consumer_connect( consumer
, mlt_playlist_service( playlist
) );
87 static char *strip_root( miracle_unit unit
, char *file
)
89 mlt_properties properties
= unit
->properties
;
90 char *root
= mlt_properties_get( properties
, "root" );
91 if ( file
!= NULL
&& root
!= NULL
)
93 int length
= strlen( root
);
94 if ( root
[ length
- 1 ] == '/' )
96 if ( !strncmp( file
, root
, length
) )
102 /** Communicate the current status to all threads waiting on the notifier.
105 static void miracle_unit_status_communicate( miracle_unit unit
)
109 mlt_properties properties
= unit
->properties
;
110 char *root_dir
= mlt_properties_get( properties
, "root" );
111 valerie_notifier notifier
= mlt_properties_get_data( properties
, "notifier", NULL
);
112 valerie_status_t status
;
114 if ( root_dir
!= NULL
&& notifier
!= NULL
)
116 if ( miracle_unit_get_status( unit
, &status
) == 0 )
117 /* if ( !( ( status.status == unit_playing || status.status == unit_paused ) &&
118 strcmp( status.clip, "" ) &&
119 !strcmp( status.tail_clip, "" ) &&
120 status.position == 0 &&
122 status.out == 0 ) ) */
123 valerie_notifier_put( notifier
, &status
);
128 /** Set the notifier info
131 void miracle_unit_set_notifier( miracle_unit
this, valerie_notifier notifier
, char *root_dir
)
133 mlt_properties properties
= this->properties
;
134 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
135 mlt_properties playlist_properties
= mlt_playlist_properties( playlist
);
137 mlt_properties_set( properties
, "root", root_dir
);
138 mlt_properties_set_data( properties
, "notifier", notifier
, 0, NULL
, NULL
);
139 mlt_properties_set_data( playlist_properties
, "notifier_arg", this, 0, NULL
, NULL
);
140 mlt_properties_set_data( playlist_properties
, "notifier", miracle_unit_status_communicate
, 0, NULL
, NULL
);
142 miracle_unit_status_communicate( this );
145 static mlt_producer
create_producer( char *file
)
147 return mlt_factory_producer( "fezzik", file
);
150 /** Create or locate a producer for the file specified.
153 static mlt_producer
locate_producer( miracle_unit unit
, char *file
)
155 // Get the unit properties
156 mlt_properties properties
= unit
->producers
;
158 // Check if we already have loaded this file
159 mlt_producer result
= mlt_properties_get_data( properties
, file
, NULL
);
161 if ( result
== NULL
)
163 // Create the producer
164 result
= create_producer( file
);
166 // Now store the result
167 if ( result
!= NULL
)
168 mlt_properties_set_data( properties
, file
, result
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
174 /** Update the generation count.
177 static void update_generation( miracle_unit unit
)
179 mlt_properties properties
= unit
->properties
;
180 int generation
= mlt_properties_get_int( properties
, "generation" );
181 mlt_properties_set_int( properties
, "generation", ++ generation
);
184 /** Wipe all clips on the playlist for this unit.
187 static void clear_unit( miracle_unit unit
)
189 mlt_properties properties
= unit
->properties
;
190 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
191 mlt_producer producer
= mlt_playlist_producer( playlist
);
193 mlt_playlist_clear( playlist
);
194 mlt_producer_seek( producer
, 0 );
196 if ( unit
->old_producers
!= NULL
)
197 mlt_properties_close( unit
->old_producers
);
198 unit
->old_producers
= unit
->producers
;
199 unit
->producers
= mlt_properties_new( );
201 update_generation( unit
);
204 /** Wipe all but the playing clip from the unit.
207 static void clean_unit( miracle_unit unit
)
209 mlt_properties properties
= unit
->properties
;
210 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
211 mlt_playlist_clip_info info
;
212 int current
= mlt_playlist_current_clip( playlist
);
213 mlt_producer producer
= mlt_playlist_producer( playlist
);
214 mlt_position position
= mlt_producer_frame( producer
);
215 double speed
= mlt_producer_get_speed( producer
);
216 mlt_playlist_get_clip_info( playlist
, &info
, current
);
218 if ( info
.resource
!= NULL
)
220 void *instance
= mlt_properties_get_data( unit
->producers
, info
.resource
, NULL
);
221 position
-= info
.start
;
222 mlt_properties_set_data( unit
->producers
, info
.resource
, instance
, 0, NULL
, NULL
);
224 mlt_playlist_append_io( playlist
, instance
, info
.frame_in
, info
.frame_out
);
225 mlt_properties_set_data( unit
->producers
, info
.resource
, instance
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
226 mlt_producer_seek( producer
, position
);
227 mlt_producer_set_speed( producer
, speed
);
230 update_generation( unit
);
233 /** Generate a report on all loaded clips.
236 void miracle_unit_report_list( miracle_unit unit
, valerie_response response
)
239 mlt_properties properties
= unit
->properties
;
240 int generation
= mlt_properties_get_int( properties
, "generation" );
241 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
243 valerie_response_printf( response
, 1024, "%d\n", generation
);
245 for ( i
= 0; i
< mlt_playlist_count( playlist
); i
++ )
247 mlt_playlist_clip_info info
;
248 mlt_playlist_get_clip_info( playlist
, &info
, i
);
249 valerie_response_printf( response
, 10240, "%d \"%s\" %d %d %d %d %.2f\n",
251 strip_root( unit
, info
.resource
),
258 valerie_response_printf( response
, 1024, "\n" );
261 /** Load a clip into the unit clearing existing play list.
264 \param unit A miracle_unit handle.
265 \param clip The absolute file name of the clip to load.
266 \param in The starting frame (-1 for 0)
267 \param out The ending frame (-1 for maximum)
270 valerie_error_code
miracle_unit_load( miracle_unit unit
, char *clip
, int32_t in
, int32_t out
, int flush
)
272 // Now try to create an producer
273 mlt_producer instance
= create_producer( clip
);
275 if ( instance
!= NULL
)
278 mlt_properties properties
= unit
->properties
;
279 mlt_properties_set_data( unit
->producers
, clip
, instance
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
280 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
281 mlt_playlist_append_io( playlist
, instance
, in
, out
);
282 miracle_log( LOG_DEBUG
, "loaded clip %s", clip
);
283 miracle_unit_status_communicate( unit
);
287 return valerie_invalid_file
;
290 valerie_error_code
miracle_unit_insert( miracle_unit unit
, char *clip
, int index
, int32_t in
, int32_t out
)
292 mlt_producer instance
= locate_producer( unit
, clip
);
294 if ( instance
!= NULL
)
296 mlt_properties properties
= unit
->properties
;
297 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
298 fprintf( stderr
, "inserting clip %s before %d\n", clip
, index
);
299 mlt_playlist_insert( playlist
, instance
, index
, in
, out
);
300 miracle_log( LOG_DEBUG
, "inserted clip %s at %d", clip
, index
);
301 update_generation( unit
);
302 miracle_unit_status_communicate( unit
);
306 return valerie_invalid_file
;
309 valerie_error_code
miracle_unit_remove( miracle_unit unit
, int index
)
311 mlt_properties properties
= unit
->properties
;
312 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
313 mlt_playlist_remove( playlist
, index
);
314 miracle_log( LOG_DEBUG
, "removed clip at %d", index
);
315 update_generation( unit
);
316 miracle_unit_status_communicate( unit
);
320 valerie_error_code
miracle_unit_clean( miracle_unit unit
)
323 miracle_log( LOG_DEBUG
, "Cleaned playlist" );
324 miracle_unit_status_communicate( unit
);
328 valerie_error_code
miracle_unit_clear( miracle_unit unit
)
331 miracle_log( LOG_DEBUG
, "Cleared playlist" );
332 miracle_unit_status_communicate( unit
);
336 valerie_error_code
miracle_unit_move( miracle_unit unit
, int src
, int dest
)
338 mlt_properties properties
= unit
->properties
;
339 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
340 mlt_playlist_move( playlist
, src
, dest
);
341 miracle_log( LOG_DEBUG
, "moved clip %d to %d", src
, dest
);
342 update_generation( unit
);
343 miracle_unit_status_communicate( unit
);
347 /** Add a clip to the unit play list.
350 \param unit A miracle_unit handle.
351 \param clip The absolute file name of the clip to load.
352 \param in The starting frame (-1 for 0)
353 \param out The ending frame (-1 for maximum)
356 valerie_error_code
miracle_unit_append( miracle_unit unit
, char *clip
, int32_t in
, int32_t out
)
358 mlt_producer instance
= locate_producer( unit
, clip
);
360 if ( instance
!= NULL
)
362 mlt_properties properties
= unit
->properties
;
363 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
364 mlt_playlist_append_io( playlist
, instance
, in
, out
);
365 miracle_log( LOG_DEBUG
, "appended clip %s", clip
);
366 update_generation( unit
);
367 miracle_unit_status_communicate( unit
);
371 return valerie_invalid_file
;
374 /** Start playing the unit.
377 \param unit A miracle_unit handle.
378 \param speed An integer that specifies the playback rate as a
379 percentage multiplied by 100.
382 void miracle_unit_play( miracle_unit_t
*unit
, int speed
)
384 mlt_properties properties
= unit
->properties
;
385 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
386 mlt_producer producer
= mlt_playlist_producer( playlist
);
387 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
388 mlt_producer_set_speed( producer
, ( double )speed
/ 1000 );
389 mlt_consumer_start( consumer
);
390 miracle_unit_status_communicate( unit
);
395 Terminates the dv_pump and halts dv1394 transmission.
397 \param unit A miracle_unit handle.
400 void miracle_unit_terminate( miracle_unit unit
)
402 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
403 mlt_playlist playlist
= mlt_properties_get_data( unit
->properties
, "playlist", NULL
);
404 mlt_producer producer
= mlt_playlist_producer( playlist
);
405 mlt_producer_set_speed( producer
, 0 );
406 mlt_consumer_stop( consumer
);
407 miracle_unit_status_communicate( unit
);
410 /** Query the status of unit playback.
412 \param unit A miracle_unit handle.
413 \return 1 if the unit is not playing, 0 if playing.
416 int miracle_unit_has_terminated( miracle_unit unit
)
418 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
419 return mlt_consumer_is_stopped( consumer
);
422 /** Transfer the currently loaded clip to another unit
425 int miracle_unit_transfer( miracle_unit dest_unit
, miracle_unit src_unit
)
428 mlt_properties dest_properties
= dest_unit
->properties
;
429 mlt_playlist dest_playlist
= mlt_properties_get_data( dest_properties
, "playlist", NULL
);
430 mlt_properties src_properties
= src_unit
->properties
;
431 mlt_playlist src_playlist
= mlt_properties_get_data( src_properties
, "playlist", NULL
);
433 for ( i
= 0; i
< mlt_playlist_count( src_playlist
); i
++ )
435 mlt_playlist_clip_info info
;
436 mlt_playlist_get_clip_info( src_playlist
, &info
, i
);
437 mlt_producer producer
= locate_producer( dest_unit
, info
.resource
);
438 if ( producer
!= NULL
)
440 mlt_playlist_append_io( dest_playlist
, producer
, info
.frame_in
, info
.frame_out
);
443 miracle_unit_change_position( dest_unit
, mlt_playlist_count( dest_playlist
) - 1, 0 );
444 clean_unit( dest_unit
);
452 /** Determine if unit is offline.
455 int miracle_unit_is_offline( miracle_unit unit
)
460 /** Obtain the status for a given unit
463 int miracle_unit_get_status( miracle_unit unit
, valerie_status status
)
465 int error
= unit
== NULL
;
467 memset( status
, 0, sizeof( valerie_status_t
) );
471 mlt_properties properties
= unit
->properties
;
472 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
473 mlt_producer producer
= mlt_playlist_producer( playlist
);
474 mlt_producer clip
= mlt_playlist_current( playlist
);
476 mlt_playlist_clip_info info
;
477 int clip_index
= mlt_playlist_current_clip( playlist
);
478 mlt_playlist_get_clip_info( playlist
, &info
, clip_index
);
480 if ( info
.resource
!= NULL
&& strcmp( info
.resource
, "" ) )
482 strncpy( status
->clip
, strip_root( unit
, info
.resource
), sizeof( status
->clip
) );
483 status
->speed
= (int)( mlt_producer_get_speed( producer
) * 1000.0 );
484 status
->fps
= mlt_producer_get_fps( producer
);
485 status
->in
= info
.frame_in
;
486 status
->out
= info
.frame_out
;
487 status
->position
= mlt_producer_position( clip
);
488 status
->length
= mlt_producer_get_length( clip
);
489 strncpy( status
->tail_clip
, strip_root( unit
, info
.resource
), sizeof( status
->tail_clip
) );
490 status
->tail_in
= info
.frame_in
;
491 status
->tail_out
= info
.frame_out
;
492 status
->tail_position
= mlt_producer_position( clip
);
493 status
->tail_length
= mlt_producer_get_length( clip
);
494 status
->clip_index
= mlt_playlist_current_clip( playlist
);
495 status
->seek_flag
= 1;
498 status
->generation
= mlt_properties_get_int( properties
, "generation" );
500 if ( miracle_unit_has_terminated( unit
) )
501 status
->status
= unit_stopped
;
502 else if ( !strcmp( status
->clip
, "" ) )
503 status
->status
= unit_not_loaded
;
504 else if ( status
->speed
== 0 )
505 status
->status
= unit_paused
;
507 status
->status
= unit_playing
;
511 status
->status
= unit_undefined
;
514 status
->unit
= mlt_properties_get_int( unit
->properties
, "unit" );
519 /** Change position in the playlist.
522 void miracle_unit_change_position( miracle_unit unit
, int clip
, int32_t position
)
524 mlt_properties properties
= unit
->properties
;
525 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
526 mlt_producer producer
= mlt_playlist_producer( playlist
);
527 mlt_playlist_clip_info info
;
534 else if ( clip
>= mlt_playlist_count( playlist
) )
536 clip
= mlt_playlist_count( playlist
) - 1;
540 if ( mlt_playlist_get_clip_info( playlist
, &info
, clip
) == 0 )
542 int32_t frame_start
= info
.start
;
543 int32_t frame_offset
= position
;
545 if ( frame_offset
< 0 )
546 frame_offset
= info
.frame_out
;
547 if ( frame_offset
< info
.frame_in
)
548 frame_offset
= info
.frame_in
;
549 if ( frame_offset
>= info
.frame_out
)
550 frame_offset
= info
.frame_out
;
552 mlt_producer_seek( producer
, frame_start
+ frame_offset
- info
.frame_in
);
555 miracle_unit_status_communicate( unit
);
558 /** Get the index of the current clip.
561 int miracle_unit_get_current_clip( miracle_unit unit
)
563 mlt_properties properties
= unit
->properties
;
564 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
565 int clip_index
= mlt_playlist_current_clip( playlist
);
569 /** Set a clip's in point
572 int miracle_unit_set_clip_in( miracle_unit unit
, int index
, int32_t position
)
574 mlt_properties properties
= unit
->properties
;
575 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
576 mlt_playlist_clip_info info
;
577 int error
= mlt_playlist_get_clip_info( playlist
, &info
, index
);
581 miracle_unit_play( unit
, 0 );
582 error
= mlt_playlist_resize_clip( playlist
, index
, position
, info
.frame_out
);
583 update_generation( unit
);
584 miracle_unit_change_position( unit
, index
, 0 );
590 /** Set a clip's out point.
593 int miracle_unit_set_clip_out( miracle_unit unit
, int index
, int32_t position
)
595 mlt_properties properties
= unit
->properties
;
596 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
597 mlt_playlist_clip_info info
;
598 int error
= mlt_playlist_get_clip_info( playlist
, &info
, index
);
602 miracle_unit_play( unit
, 0 );
603 error
= mlt_playlist_resize_clip( playlist
, index
, info
.frame_in
, position
);
604 update_generation( unit
);
605 miracle_unit_status_communicate( unit
);
606 miracle_unit_change_position( unit
, index
, -1 );
612 /** Step by specified position.
615 void miracle_unit_step( miracle_unit unit
, int32_t offset
)
617 mlt_properties properties
= unit
->properties
;
618 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
619 mlt_producer producer
= mlt_playlist_producer( playlist
);
620 mlt_position position
= mlt_producer_frame( producer
);
621 mlt_producer_seek( producer
, position
+ offset
);
624 /** Set the unit's clip mode regarding in and out points.
627 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
629 //dv_player player = miracle_unit_get_dv_player( unit );
630 //if ( player != NULL )
631 //dv_player_set_clip_mode( player, mode );
632 //miracle_unit_status_communicate( unit );
635 /** Get the unit's clip mode regarding in and out points.
638 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
640 //dv_player player = miracle_unit_get_dv_player( unit );
641 //return dv_player_get_clip_mode( player );
644 /** Set the unit's clip mode regarding eof handling.
647 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
649 //dv_player player = miracle_unit_get_dv_player( unit );
650 //dv_player_set_eof_action( player, action );
651 //miracle_unit_status_communicate( unit );
654 /** Get the unit's clip mode regarding eof handling.
657 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
659 //dv_player player = miracle_unit_get_dv_player( unit );
660 //return dv_player_get_eof_action( player );
663 int miracle_unit_set( miracle_unit unit
, char *name_value
)
665 mlt_properties properties
= NULL
;
667 if ( strncmp( name_value
, "consumer.", 9 ) )
669 mlt_playlist playlist
= mlt_properties_get_data( unit
->properties
, "playlist", NULL
);
670 properties
= mlt_playlist_properties( playlist
);
674 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
675 properties
= mlt_consumer_properties( consumer
);
679 return mlt_properties_parse( properties
, name_value
);
682 char *miracle_unit_get( miracle_unit unit
, char *name
)
684 mlt_playlist playlist
= mlt_properties_get_data( unit
->properties
, "playlist", NULL
);
685 mlt_properties properties
= mlt_playlist_properties( playlist
);
686 return mlt_properties_get( properties
, name
);
692 \param unit A miracle_unit handle.
695 void miracle_unit_close( miracle_unit unit
)
699 miracle_log( LOG_DEBUG
, "closing unit..." );
700 if ( unit
->old_producers
!= NULL
)
701 mlt_properties_close( unit
->old_producers
);
702 mlt_properties_close( unit
->properties
);
703 mlt_properties_close( unit
->producers
);
705 miracle_log( LOG_DEBUG
, "... unit closed." );