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 /** Communicate the current status to all threads waiting on the notifier.
90 static void miracle_unit_status_communicate( miracle_unit unit
)
94 mlt_properties properties
= unit
->properties
;
95 char *root_dir
= mlt_properties_get( properties
, "root" );
96 valerie_notifier notifier
= mlt_properties_get_data( properties
, "notifier", NULL
);
97 valerie_status_t status
;
99 if ( root_dir
!= NULL
&& notifier
!= NULL
)
101 if ( miracle_unit_get_status( unit
, &status
) == 0 )
102 /* if ( !( ( status.status == unit_playing || status.status == unit_paused ) &&
103 strcmp( status.clip, "" ) &&
104 !strcmp( status.tail_clip, "" ) &&
105 status.position == 0 &&
107 status.out == 0 ) ) */
108 valerie_notifier_put( notifier
, &status
);
113 /** Set the notifier info
116 void miracle_unit_set_notifier( miracle_unit
this, valerie_notifier notifier
, char *root_dir
)
118 mlt_properties properties
= this->properties
;
119 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
120 mlt_properties playlist_properties
= mlt_playlist_properties( playlist
);
122 mlt_properties_set( properties
, "root", root_dir
);
123 mlt_properties_set_data( properties
, "notifier", notifier
, 0, NULL
, NULL
);
124 mlt_properties_set_data( playlist_properties
, "notifier_arg", this, 0, NULL
, NULL
);
125 mlt_properties_set_data( playlist_properties
, "notifier", miracle_unit_status_communicate
, 0, NULL
, NULL
);
127 miracle_unit_status_communicate( this );
130 static mlt_producer
create_producer( char *file
)
132 return mlt_factory_producer( "fezzik", file
);
135 /** Create or locate a producer for the file specified.
138 static mlt_producer
locate_producer( miracle_unit unit
, char *file
)
140 // Get the unit properties
141 mlt_properties properties
= unit
->producers
;
143 // Check if we already have loaded this file
144 mlt_producer result
= mlt_properties_get_data( properties
, file
, NULL
);
146 if ( result
== NULL
)
148 // Create the producer
149 result
= create_producer( file
);
151 // Now store the result
152 if ( result
!= NULL
)
153 mlt_properties_set_data( properties
, file
, result
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
159 /** Update the generation count.
162 static void update_generation( miracle_unit unit
)
164 mlt_properties properties
= unit
->properties
;
165 int generation
= mlt_properties_get_int( properties
, "generation" );
166 mlt_properties_set_int( properties
, "generation", ++ generation
);
169 /** Wipe all clips on the playlist for this unit.
172 static void clear_unit( miracle_unit unit
)
174 mlt_properties properties
= unit
->properties
;
175 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
176 mlt_producer producer
= mlt_playlist_producer( playlist
);
178 mlt_playlist_clear( playlist
);
179 mlt_producer_seek( producer
, 0 );
181 if ( unit
->old_producers
!= NULL
)
182 mlt_properties_close( unit
->old_producers
);
183 unit
->old_producers
= unit
->producers
;
184 unit
->producers
= mlt_properties_new( );
186 update_generation( unit
);
189 /** Generate a report on all loaded clips.
192 void miracle_unit_report_list( miracle_unit unit
, valerie_response response
)
195 mlt_properties properties
= unit
->properties
;
196 int generation
= mlt_properties_get_int( properties
, "generation" );
197 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
199 valerie_response_printf( response
, 1024, "%d\n", generation
);
201 for ( i
= 0; i
< mlt_playlist_count( playlist
); i
++ )
203 mlt_playlist_clip_info info
;
204 mlt_playlist_get_clip_info( playlist
, &info
, i
);
205 valerie_response_printf( response
, 10240, "%d \"%s\" %lld %lld %lld %lld %.2f\n",
215 /** Load a clip into the unit clearing existing play list.
218 \param unit A miracle_unit handle.
219 \param clip The absolute file name of the clip to load.
220 \param in The starting frame (-1 for 0)
221 \param out The ending frame (-1 for maximum)
224 valerie_error_code
miracle_unit_load( miracle_unit unit
, char *clip
, int64_t in
, int64_t out
, int flush
)
226 // Now try to create an producer
227 mlt_producer instance
= create_producer( clip
);
229 if ( instance
!= NULL
)
232 mlt_properties properties
= unit
->properties
;
233 mlt_properties_set_data( unit
->producers
, clip
, instance
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
234 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
235 mlt_playlist_append_io( playlist
, instance
, in
, out
);
236 miracle_log( LOG_DEBUG
, "loaded clip %s", clip
);
237 miracle_unit_status_communicate( unit
);
241 return valerie_invalid_file
;
244 valerie_error_code
miracle_unit_insert( miracle_unit unit
, char *clip
, int index
, int64_t in
, int64_t out
)
246 mlt_producer instance
= locate_producer( unit
, clip
);
248 if ( instance
!= NULL
)
250 mlt_properties properties
= unit
->properties
;
251 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
252 mlt_playlist_insert( playlist
, instance
, index
, in
, out
);
253 miracle_log( LOG_DEBUG
, "inserted clip %s at %d", clip
, index
);
254 update_generation( unit
);
255 miracle_unit_status_communicate( unit
);
259 return valerie_invalid_file
;
262 valerie_error_code
miracle_unit_remove( miracle_unit unit
, int index
)
264 mlt_properties properties
= unit
->properties
;
265 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
266 mlt_playlist_remove( playlist
, index
);
267 miracle_log( LOG_DEBUG
, "removed clip at %d", index
);
268 update_generation( unit
);
269 miracle_unit_status_communicate( unit
);
273 valerie_error_code
miracle_unit_clean( miracle_unit unit
)
276 miracle_log( LOG_DEBUG
, "Cleaned playlist" );
277 miracle_unit_status_communicate( unit
);
281 valerie_error_code
miracle_unit_move( miracle_unit unit
, int src
, int dest
)
283 mlt_properties properties
= unit
->properties
;
284 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
285 mlt_playlist_move( playlist
, src
, dest
);
286 miracle_log( LOG_DEBUG
, "moved clip %d to %d", src
, dest
);
287 update_generation( unit
);
288 miracle_unit_status_communicate( unit
);
292 /** Add a clip to the unit play list.
295 \param unit A miracle_unit handle.
296 \param clip The absolute file name of the clip to load.
297 \param in The starting frame (-1 for 0)
298 \param out The ending frame (-1 for maximum)
301 valerie_error_code
miracle_unit_append( miracle_unit unit
, char *clip
, int64_t in
, int64_t out
)
303 mlt_producer instance
= locate_producer( unit
, clip
);
305 if ( instance
!= NULL
)
307 mlt_properties properties
= unit
->properties
;
308 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
309 mlt_playlist_append_io( playlist
, instance
, in
, out
);
310 miracle_log( LOG_DEBUG
, "appended clip %s", clip
);
311 update_generation( unit
);
312 miracle_unit_status_communicate( unit
);
316 return valerie_invalid_file
;
319 /** Start playing the unit.
322 \param unit A miracle_unit handle.
323 \param speed An integer that specifies the playback rate as a
324 percentage multiplied by 100.
327 void miracle_unit_play( miracle_unit_t
*unit
, int speed
)
329 mlt_properties properties
= unit
->properties
;
330 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
331 mlt_producer producer
= mlt_playlist_producer( playlist
);
332 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
333 mlt_producer_set_speed( producer
, ( double )speed
/ 1000 );
334 mlt_consumer_start( consumer
);
335 miracle_unit_status_communicate( unit
);
340 Terminates the dv_pump and halts dv1394 transmission.
342 \param unit A miracle_unit handle.
345 void miracle_unit_terminate( miracle_unit unit
)
347 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
348 mlt_consumer_stop( consumer
);
349 miracle_unit_status_communicate( unit
);
352 /** Query the status of unit playback.
354 \param unit A miracle_unit handle.
355 \return 1 if the unit is not playing, 0 if playing.
358 int miracle_unit_has_terminated( miracle_unit unit
)
360 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
361 return mlt_consumer_is_stopped( consumer
);
364 /** Transfer the currently loaded clip to another unit
367 int miracle_unit_transfer( miracle_unit dest_unit
, miracle_unit src_unit
)
372 /** Determine if unit is offline.
375 int miracle_unit_is_offline( miracle_unit unit
)
380 /** Obtain the status for a given unit
383 int miracle_unit_get_status( miracle_unit unit
, valerie_status status
)
385 int error
= unit
== NULL
;
387 memset( status
, 0, sizeof( valerie_status_t
) );
391 mlt_properties properties
= unit
->properties
;
392 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
393 mlt_producer producer
= mlt_playlist_producer( playlist
);
394 mlt_producer clip
= mlt_playlist_current( playlist
);
396 mlt_playlist_clip_info info
;
397 int clip_index
= mlt_playlist_current_clip( playlist
);
398 mlt_playlist_get_clip_info( playlist
, &info
, clip_index
);
400 if ( info
.resource
!= NULL
&& strcmp( info
.resource
, "" ) )
402 strncpy( status
->clip
, info
.resource
, sizeof( status
->clip
) );
403 status
->speed
= (int)( mlt_producer_get_speed( producer
) * 1000.0 );
404 status
->fps
= mlt_producer_get_fps( producer
);
405 status
->in
= info
.frame_in
;
406 status
->out
= info
.frame_out
;
407 status
->position
= mlt_producer_position( clip
);
408 status
->length
= mlt_producer_get_length( clip
);
409 strncpy( status
->tail_clip
, info
.resource
, sizeof( status
->tail_clip
) );
410 status
->tail_in
= info
.frame_in
;
411 status
->tail_out
= info
.frame_out
;
412 status
->tail_position
= mlt_producer_position( clip
);
413 status
->tail_length
= mlt_producer_get_length( clip
);
414 status
->clip_index
= mlt_playlist_current_clip( playlist
);
415 status
->seek_flag
= 1;
418 status
->generation
= mlt_properties_get_int( properties
, "generation" );
420 if ( miracle_unit_has_terminated( unit
) )
421 status
->status
= unit_stopped
;
422 else if ( !strcmp( status
->clip
, "" ) )
423 status
->status
= unit_not_loaded
;
424 else if ( status
->speed
== 0 )
425 status
->status
= unit_paused
;
427 status
->status
= unit_playing
;
431 status
->status
= unit_undefined
;
434 status
->unit
= mlt_properties_get_int( unit
->properties
, "unit" );
439 /** Change position in the playlist.
442 void miracle_unit_change_position( miracle_unit unit
, int clip
, int64_t position
)
444 mlt_properties properties
= unit
->properties
;
445 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
446 mlt_producer producer
= mlt_playlist_producer( playlist
);
447 mlt_playlist_clip_info info
;
454 else if ( clip
>= mlt_playlist_count( playlist
) )
456 clip
= mlt_playlist_count( playlist
) - 1;
460 if ( mlt_playlist_get_clip_info( playlist
, &info
, clip
) == 0 )
462 int64_t frame_start
= info
.start
;
463 int64_t frame_offset
= position
;
465 if ( frame_offset
< 0 )
466 frame_offset
= info
.frame_out
;
467 if ( frame_offset
< info
.frame_in
)
468 frame_offset
= info
.frame_in
;
469 if ( frame_offset
>= info
.frame_out
)
470 frame_offset
= info
.frame_out
;
472 mlt_producer_seek( producer
, frame_start
+ frame_offset
- info
.frame_in
);
475 miracle_unit_status_communicate( unit
);
478 /** Get the index of the current clip.
481 int miracle_unit_get_current_clip( miracle_unit unit
)
483 mlt_properties properties
= unit
->properties
;
484 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
485 int clip_index
= mlt_playlist_current_clip( playlist
);
489 /** Set a clip's in point
492 int miracle_unit_set_clip_in( miracle_unit unit
, int index
, int64_t position
)
494 mlt_properties properties
= unit
->properties
;
495 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
496 mlt_playlist_clip_info info
;
497 int error
= mlt_playlist_get_clip_info( playlist
, &info
, index
);
501 miracle_unit_play( unit
, 0 );
502 error
= mlt_playlist_resize_clip( playlist
, index
, position
, info
.frame_out
);
503 update_generation( unit
);
504 miracle_unit_change_position( unit
, index
, 0 );
510 /** Set a clip's out point.
513 int miracle_unit_set_clip_out( miracle_unit unit
, int index
, int64_t position
)
515 mlt_properties properties
= unit
->properties
;
516 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
517 mlt_playlist_clip_info info
;
518 int error
= mlt_playlist_get_clip_info( playlist
, &info
, index
);
522 miracle_unit_play( unit
, 0 );
523 error
= mlt_playlist_resize_clip( playlist
, index
, info
.frame_in
, position
);
524 update_generation( unit
);
525 miracle_unit_status_communicate( unit
);
526 miracle_unit_change_position( unit
, index
, -1 );
532 /** Step by specified position.
535 void miracle_unit_step( miracle_unit unit
, int64_t offset
)
537 mlt_properties properties
= unit
->properties
;
538 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
539 mlt_producer producer
= mlt_playlist_producer( playlist
);
540 mlt_position position
= mlt_producer_frame( producer
);
541 mlt_producer_seek( producer
, position
+ offset
);
544 /** Set the unit's clip mode regarding in and out points.
547 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
549 //dv_player player = miracle_unit_get_dv_player( unit );
550 //if ( player != NULL )
551 //dv_player_set_clip_mode( player, mode );
552 //miracle_unit_status_communicate( unit );
555 /** Get the unit's clip mode regarding in and out points.
558 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
560 //dv_player player = miracle_unit_get_dv_player( unit );
561 //return dv_player_get_clip_mode( player );
564 /** Set the unit's clip mode regarding eof handling.
567 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
569 //dv_player player = miracle_unit_get_dv_player( unit );
570 //dv_player_set_eof_action( player, action );
571 //miracle_unit_status_communicate( unit );
574 /** Get the unit's clip mode regarding eof handling.
577 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
579 //dv_player player = miracle_unit_get_dv_player( unit );
580 //return dv_player_get_eof_action( player );
583 int miracle_unit_set( miracle_unit unit
, char *name_value
)
585 mlt_properties properties
= NULL
;
587 if ( strncmp( name_value
, "consumer.", 9 ) )
589 mlt_playlist playlist
= mlt_properties_get_data( unit
->properties
, "playlist", NULL
);
590 properties
= mlt_playlist_properties( playlist
);
594 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
595 properties
= mlt_consumer_properties( consumer
);
599 return mlt_properties_parse( properties
, name_value
);
602 char *miracle_unit_get( miracle_unit unit
, char *name
)
604 mlt_playlist playlist
= mlt_properties_get_data( unit
->properties
, "playlist", NULL
);
605 mlt_properties properties
= mlt_playlist_properties( playlist
);
606 return mlt_properties_get( properties
, name
);
612 \param unit A miracle_unit handle.
615 void miracle_unit_close( miracle_unit unit
)
619 miracle_log( LOG_DEBUG
, "closing unit..." );
620 if ( unit
->old_producers
!= NULL
)
621 mlt_properties_close( unit
->old_producers
);
622 mlt_properties_close( unit
->properties
);
623 mlt_properties_close( unit
->producers
);
625 miracle_log( LOG_DEBUG
, "... unit closed." );