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 /** Create or locate a producer for the file specified.
133 static mlt_producer
create_producer( miracle_unit unit
, char *file
)
135 // Get the unit properties
136 mlt_properties properties
= unit
->producers
;
138 // Check if we already have loaded this file
139 mlt_producer result
= mlt_properties_get_data( properties
, file
, NULL
);
141 if ( result
== NULL
)
143 // 1st Line preferences
144 if ( strstr( file
, ".inigo" ) )
146 char *args
[ 2 ] = { file
, NULL
};
147 result
= mlt_factory_producer( "inigo", args
);
149 else if ( strstr( file
, ".mpg" ) )
150 result
= mlt_factory_producer( "mcmpeg", file
);
151 else if ( strstr( file
, ".mpeg" ) )
152 result
= mlt_factory_producer( "mcmpeg", file
);
153 else if ( strstr( file
, ".dv" ) )
154 result
= mlt_factory_producer( "mcdv", file
);
155 else if ( strstr( file
, ".dif" ) )
156 result
= mlt_factory_producer( "mcdv", file
);
157 else if ( strstr( file
, ".jpg" ) )
158 result
= mlt_factory_producer( "pixbuf", file
);
159 else if ( strstr( file
, ".JPG" ) )
160 result
= mlt_factory_producer( "pixbuf", file
);
161 else if ( strstr( file
, ".jpeg" ) )
162 result
= mlt_factory_producer( "pixbuf", file
);
163 else if ( strstr( file
, ".png" ) )
164 result
= mlt_factory_producer( "pixbuf", file
);
165 else if ( strstr( file
, ".tga" ) )
166 result
= mlt_factory_producer( "pixbuf", file
);
167 else if ( strstr( file
, ".txt" ) )
168 result
= mlt_factory_producer( "pango", file
);
170 // 2nd Line fallbacks
171 if ( result
== NULL
&& strstr( file
, ".dv" ) )
172 result
= mlt_factory_producer( "libdv", file
);
173 else if ( result
== NULL
&& strstr( file
, ".dif" ) )
174 result
= mlt_factory_producer( "libdv", file
);
176 // 3rd line fallbacks
177 if ( result
== NULL
)
178 result
= mlt_factory_producer( "ffmpeg", file
);
180 // Now store the result
181 mlt_properties_set_data( properties
, file
, result
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
187 /** Update the generation count.
190 static void update_generation( miracle_unit unit
)
192 mlt_properties properties
= unit
->properties
;
193 int generation
= mlt_properties_get_int( properties
, "generation" );
194 mlt_properties_set_int( properties
, "generation", ++ generation
);
197 /** Wipe all clips on the playlist for this unit.
200 static void clear_unit( miracle_unit unit
)
202 mlt_properties properties
= unit
->properties
;
203 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
204 mlt_producer producer
= mlt_playlist_producer( playlist
);
206 mlt_playlist_clear( playlist
);
207 mlt_producer_seek( producer
, 0 );
209 if ( unit
->old_producers
!= NULL
)
210 mlt_properties_close( unit
->old_producers
);
211 unit
->old_producers
= unit
->producers
;
212 unit
->producers
= mlt_properties_new( );
214 update_generation( unit
);
217 /** Generate a report on all loaded clips.
220 void miracle_unit_report_list( miracle_unit unit
, valerie_response response
)
223 mlt_properties properties
= unit
->properties
;
224 int generation
= mlt_properties_get_int( properties
, "generation" );
225 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
227 valerie_response_printf( response
, 1024, "%d\n", generation
);
229 for ( i
= 0; i
< mlt_playlist_count( playlist
); i
++ )
231 mlt_playlist_clip_info info
;
232 mlt_playlist_get_clip_info( playlist
, &info
, i
);
233 valerie_response_printf( response
, 10240, "%d \"%s\" %lld %lld %lld %lld %.2f\n",
243 /** Load a clip into the unit clearing existing play list.
246 \param unit A miracle_unit handle.
247 \param clip The absolute file name of the clip to load.
248 \param in The starting frame (-1 for 0)
249 \param out The ending frame (-1 for maximum)
252 valerie_error_code
miracle_unit_load( miracle_unit unit
, char *clip
, int64_t in
, int64_t out
, int flush
)
254 // Have to clear the unit first
257 // Now try to create an producer
258 mlt_producer instance
= create_producer( unit
, clip
);
260 if ( instance
!= NULL
)
262 mlt_properties properties
= unit
->properties
;
263 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
264 mlt_playlist_append_io( playlist
, instance
, in
, out
);
265 miracle_log( LOG_DEBUG
, "loaded clip %s", clip
);
266 miracle_unit_status_communicate( unit
);
270 return valerie_invalid_file
;
273 valerie_error_code
miracle_unit_insert( miracle_unit unit
, char *clip
, int index
, int64_t in
, int64_t out
)
275 mlt_producer instance
= create_producer( unit
, clip
);
277 if ( instance
!= NULL
)
279 mlt_properties properties
= unit
->properties
;
280 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
281 mlt_playlist_insert( playlist
, instance
, index
, in
, out
);
282 miracle_log( LOG_DEBUG
, "inserted clip %s at %d", clip
, index
);
283 update_generation( unit
);
284 miracle_unit_status_communicate( unit
);
288 return valerie_invalid_file
;
291 valerie_error_code
miracle_unit_remove( miracle_unit unit
, int index
)
293 mlt_properties properties
= unit
->properties
;
294 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
295 mlt_playlist_remove( playlist
, index
);
296 miracle_log( LOG_DEBUG
, "removed clip at %d", index
);
297 update_generation( unit
);
298 miracle_unit_status_communicate( unit
);
302 valerie_error_code
miracle_unit_clean( miracle_unit unit
)
305 miracle_log( LOG_DEBUG
, "Cleaned playlist" );
306 miracle_unit_status_communicate( unit
);
310 valerie_error_code
miracle_unit_move( miracle_unit unit
, int src
, int dest
)
312 mlt_properties properties
= unit
->properties
;
313 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
314 mlt_playlist_move( playlist
, src
, dest
);
315 miracle_log( LOG_DEBUG
, "moved clip %d to %d", src
, dest
);
316 update_generation( unit
);
317 miracle_unit_status_communicate( unit
);
321 /** Add a clip to the unit play list.
324 \param unit A miracle_unit handle.
325 \param clip The absolute file name of the clip to load.
326 \param in The starting frame (-1 for 0)
327 \param out The ending frame (-1 for maximum)
330 valerie_error_code
miracle_unit_append( miracle_unit unit
, char *clip
, int64_t in
, int64_t out
)
332 mlt_producer instance
= create_producer( unit
, clip
);
334 if ( instance
!= NULL
)
336 mlt_properties properties
= unit
->properties
;
337 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
338 mlt_playlist_append_io( playlist
, instance
, in
, out
);
339 miracle_log( LOG_DEBUG
, "appended clip %s", clip
);
340 update_generation( unit
);
341 miracle_unit_status_communicate( unit
);
345 return valerie_invalid_file
;
348 /** Start playing the unit.
351 \param unit A miracle_unit handle.
352 \param speed An integer that specifies the playback rate as a
353 percentage multiplied by 100.
356 void miracle_unit_play( miracle_unit_t
*unit
, int speed
)
358 mlt_properties properties
= unit
->properties
;
359 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
360 mlt_producer producer
= mlt_playlist_producer( playlist
);
361 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
362 mlt_producer_set_speed( producer
, ( double )speed
/ 1000 );
363 mlt_consumer_start( consumer
);
364 miracle_unit_status_communicate( unit
);
369 Terminates the dv_pump and halts dv1394 transmission.
371 \param unit A miracle_unit handle.
374 void miracle_unit_terminate( miracle_unit unit
)
376 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
377 mlt_consumer_stop( consumer
);
378 miracle_unit_status_communicate( unit
);
381 /** Query the status of unit playback.
383 \param unit A miracle_unit handle.
384 \return 1 if the unit is not playing, 0 if playing.
387 int miracle_unit_has_terminated( miracle_unit unit
)
389 mlt_consumer consumer
= mlt_properties_get_data( unit
->properties
, "consumer", NULL
);
390 return mlt_consumer_is_stopped( consumer
);
393 /** Transfer the currently loaded clip to another unit
396 int miracle_unit_transfer( miracle_unit dest_unit
, miracle_unit src_unit
)
401 /** Determine if unit is offline.
404 int miracle_unit_is_offline( miracle_unit unit
)
409 /** Obtain the status for a given unit
412 int miracle_unit_get_status( miracle_unit unit
, valerie_status status
)
414 int error
= unit
== NULL
;
416 memset( status
, 0, sizeof( valerie_status_t
) );
420 mlt_properties properties
= unit
->properties
;
421 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
422 mlt_producer producer
= mlt_playlist_producer( playlist
);
423 mlt_producer clip
= mlt_playlist_current( playlist
);
425 mlt_playlist_clip_info info
;
426 int clip_index
= mlt_playlist_current_clip( playlist
);
427 mlt_playlist_get_clip_info( playlist
, &info
, clip_index
);
429 if ( info
.resource
!= NULL
&& strcmp( info
.resource
, "" ) )
431 strncpy( status
->clip
, info
.resource
, sizeof( status
->clip
) );
432 status
->speed
= (int)( mlt_producer_get_speed( producer
) * 1000.0 );
433 status
->fps
= mlt_producer_get_fps( producer
);
434 status
->in
= info
.frame_in
;
435 status
->out
= info
.frame_out
;
436 status
->position
= mlt_producer_position( clip
);
437 status
->length
= mlt_producer_get_length( clip
);
438 strncpy( status
->tail_clip
, info
.resource
, sizeof( status
->tail_clip
) );
439 status
->tail_in
= info
.frame_in
;
440 status
->tail_out
= info
.frame_out
;
441 status
->tail_position
= mlt_producer_position( clip
);
442 status
->tail_length
= mlt_producer_get_length( clip
);
443 status
->clip_index
= mlt_playlist_current_clip( playlist
);
444 status
->seek_flag
= 1;
447 status
->generation
= mlt_properties_get_int( properties
, "generation" );
449 if ( miracle_unit_has_terminated( unit
) )
450 status
->status
= unit_stopped
;
451 else if ( !strcmp( status
->clip
, "" ) )
452 status
->status
= unit_not_loaded
;
453 else if ( status
->speed
== 0 )
454 status
->status
= unit_paused
;
456 status
->status
= unit_playing
;
460 status
->status
= unit_undefined
;
463 status
->unit
= mlt_properties_get_int( unit
->properties
, "unit" );
468 /** Change position in the playlist.
471 void miracle_unit_change_position( miracle_unit unit
, int clip
, int64_t position
)
473 mlt_properties properties
= unit
->properties
;
474 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
475 mlt_producer producer
= mlt_playlist_producer( playlist
);
476 mlt_playlist_clip_info info
;
483 else if ( clip
>= mlt_playlist_count( playlist
) )
485 clip
= mlt_playlist_count( playlist
) - 1;
489 if ( mlt_playlist_get_clip_info( playlist
, &info
, clip
) == 0 )
491 int64_t frame_start
= info
.start
;
492 int64_t frame_offset
= position
;
494 if ( frame_offset
< 0 )
495 frame_offset
= info
.frame_out
;
496 if ( frame_offset
< info
.frame_in
)
497 frame_offset
= info
.frame_in
;
498 if ( frame_offset
>= info
.frame_out
)
499 frame_offset
= info
.frame_out
;
501 mlt_producer_seek( producer
, frame_start
+ frame_offset
- info
.frame_in
);
504 miracle_unit_status_communicate( unit
);
507 /** Get the index of the current clip.
510 int miracle_unit_get_current_clip( miracle_unit unit
)
512 mlt_properties properties
= unit
->properties
;
513 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
514 int clip_index
= mlt_playlist_current_clip( playlist
);
518 /** Set a clip's in point
521 int miracle_unit_set_clip_in( miracle_unit unit
, int index
, int64_t position
)
523 mlt_properties properties
= unit
->properties
;
524 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
525 mlt_playlist_clip_info info
;
526 int error
= mlt_playlist_get_clip_info( playlist
, &info
, index
);
530 miracle_unit_play( unit
, 0 );
531 error
= mlt_playlist_resize_clip( playlist
, index
, position
, info
.frame_out
);
532 update_generation( unit
);
533 miracle_unit_change_position( unit
, index
, 0 );
539 /** Set a clip's out point.
542 int miracle_unit_set_clip_out( miracle_unit unit
, int index
, int64_t position
)
544 mlt_properties properties
= unit
->properties
;
545 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
546 mlt_playlist_clip_info info
;
547 int error
= mlt_playlist_get_clip_info( playlist
, &info
, index
);
551 miracle_unit_play( unit
, 0 );
552 error
= mlt_playlist_resize_clip( playlist
, index
, info
.frame_in
, position
);
553 update_generation( unit
);
554 miracle_unit_status_communicate( unit
);
555 miracle_unit_change_position( unit
, index
, -1 );
561 /** Step by specified position.
564 void miracle_unit_step( miracle_unit unit
, int64_t offset
)
566 mlt_properties properties
= unit
->properties
;
567 mlt_playlist playlist
= mlt_properties_get_data( properties
, "playlist", NULL
);
568 mlt_producer producer
= mlt_playlist_producer( playlist
);
569 mlt_position position
= mlt_producer_frame( producer
);
570 mlt_producer_seek( producer
, position
+ offset
);
573 /** Set the unit's clip mode regarding in and out points.
576 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
578 //dv_player player = miracle_unit_get_dv_player( unit );
579 //if ( player != NULL )
580 //dv_player_set_clip_mode( player, mode );
581 //miracle_unit_status_communicate( unit );
584 /** Get the unit's clip mode regarding in and out points.
587 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
589 //dv_player player = miracle_unit_get_dv_player( unit );
590 //return dv_player_get_clip_mode( player );
593 /** Set the unit's clip mode regarding eof handling.
596 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
598 //dv_player player = miracle_unit_get_dv_player( unit );
599 //dv_player_set_eof_action( player, action );
600 //miracle_unit_status_communicate( unit );
603 /** Get the unit's clip mode regarding eof handling.
606 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
608 //dv_player player = miracle_unit_get_dv_player( unit );
609 //return dv_player_get_eof_action( player );
612 int miracle_unit_set( miracle_unit unit
, char *name_value
)
614 mlt_playlist playlist
= mlt_properties_get_data( unit
->properties
, "playlist", NULL
);
615 mlt_properties properties
= mlt_playlist_properties( playlist
);
616 return mlt_properties_parse( properties
, name_value
);
619 char *miracle_unit_get( miracle_unit unit
, char *name
)
621 mlt_playlist playlist
= mlt_properties_get_data( unit
->properties
, "playlist", NULL
);
622 mlt_properties properties
= mlt_playlist_properties( playlist
);
623 return mlt_properties_get( properties
, name
);
629 \param unit A miracle_unit handle.
632 void miracle_unit_close( miracle_unit unit
)
636 miracle_log( LOG_DEBUG
, "closing unit..." );
637 if ( unit
->old_producers
!= NULL
)
638 mlt_properties_close( unit
->old_producers
);
639 mlt_properties_close( unit
->properties
);
640 mlt_properties_close( unit
->producers
);
642 miracle_log( LOG_DEBUG
, "... unit closed." );