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>
39 #include "miracle_unit.h"
40 #include "miracle_log.h"
41 #include "miracle_local.h"
43 #include <framework/mlt.h>
45 /* Forward references */
46 static void miracle_unit_status_communicate( miracle_unit
);
48 /** Allocate a new DV transmission unit.
50 \return A new miracle_unit handle.
53 miracle_unit
miracle_unit_init( int index
, char *constructor
)
55 miracle_unit
this = NULL
;
56 mlt_consumer consumer
= NULL
;
58 char *id
= strdup( constructor
);
59 char *arg
= strchr( id
, ':' );
64 consumer
= mlt_factory_consumer( id
, arg
);
66 if ( consumer
!= NULL
)
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
) );
86 /** Communicate the current status to all threads waiting on the notifier.
89 static void miracle_unit_status_communicate( miracle_unit unit
)
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
;
98 if ( root_dir
!= NULL
&& notifier
!= NULL
)
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 &&
106 status.out == 0 ) ) */
107 valerie_notifier_put( notifier
, &status
);
112 /** Set the notifier info
115 void miracle_unit_set_notifier( miracle_unit
this, valerie_notifier notifier
, char *root_dir
)
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
);
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
);
126 miracle_unit_status_communicate( this );
129 /** Create or locate a producer for the file specified.
132 static mlt_producer
create_producer( miracle_unit unit
, char *file
)
134 // Get the unit properties
135 mlt_properties properties
= unit
->producers
;
137 // Check if we already have loaded this file
138 mlt_producer result
= mlt_properties_get_data( properties
, file
, NULL
);
140 if ( result
== NULL
)
142 // 1st Line preferences
143 if ( strstr( file
, ".inigo" ) )
145 char *args
[ 2 ] = { file
, NULL
};
146 result
= mlt_factory_producer( "inigo", args
);
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
);
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
);
171 // 3rd line fallbacks
172 if ( result
== NULL
)
173 result
= mlt_factory_producer( "ffmpeg", file
);
175 // Now store the result
176 mlt_properties_set_data( properties
, file
, result
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
182 /** Update the generation count.
185 static void update_generation( miracle_unit unit
)
187 mlt_properties properties
= unit
->properties
;
188 int generation
= mlt_properties_get_int( properties
, "generation" );
189 mlt_properties_set_int( properties
, "generation", ++ generation
);
192 /** Wipe all clips on the playlist for this unit.
195 static void clear_unit( miracle_unit unit
)
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
);
201 mlt_playlist_clear( playlist
);
202 mlt_producer_seek( producer
, 0 );
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( );
209 update_generation( unit
);
212 /** Generate a report on all loaded clips.
215 void miracle_unit_report_list( miracle_unit unit
, valerie_response response
)
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
);
222 valerie_response_printf( response
, 1024, "%d\n", generation
);
224 for ( i
= 0; i
< mlt_playlist_count( playlist
); i
++ )
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",
238 /** Load a clip into the unit clearing existing play list.
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)
247 valerie_error_code
miracle_unit_load( miracle_unit unit
, char *clip
, int64_t in
, int64_t out
, int flush
)
249 // Have to clear the unit first
252 // Now try to create an producer
253 mlt_producer instance
= create_producer( unit
, clip
);
255 if ( instance
!= NULL
)
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
);
265 return valerie_invalid_file
;
268 valerie_error_code
miracle_unit_insert( miracle_unit unit
, char *clip
, int index
, int64_t in
, int64_t out
)
270 mlt_producer instance
= create_producer( unit
, clip
);
272 if ( instance
!= NULL
)
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
);
283 return valerie_invalid_file
;
286 valerie_error_code
miracle_unit_remove( miracle_unit unit
, int index
)
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
);
297 valerie_error_code
miracle_unit_clean( miracle_unit unit
)
300 miracle_log( LOG_DEBUG
, "Cleaned playlist" );
304 valerie_error_code
miracle_unit_move( miracle_unit unit
, int src
, int dest
)
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
);
315 /** Add a clip to the unit play list.
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)
324 valerie_error_code
miracle_unit_append( miracle_unit unit
, char *clip
, int64_t in
, int64_t out
)
326 mlt_producer instance
= create_producer( unit
, clip
);
328 if ( instance
!= NULL
)
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
);
339 return valerie_invalid_file
;
342 /** Start playing the clip.
344 Start a dv-pump and commence dv1394 transmission.
347 \param unit A miracle_unit handle.
348 \param speed An integer that specifies the playback rate as a
349 percentage multiplied by 100.
352 void miracle_unit_play( miracle_unit_t
*unit
, int speed
)
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
);
363 Terminates the dv_pump and halts dv1394 transmission.
365 \param unit A miracle_unit handle.
368 void miracle_unit_terminate( miracle_unit unit
)
372 /** Query the status of unit playback.
374 \param unit A miracle_unit handle.
375 \return 1 if the unit is not playing, 0 if playing.
378 int miracle_unit_has_terminated( miracle_unit unit
)
383 /** Transfer the currently loaded clip to another unit
386 int miracle_unit_transfer( miracle_unit dest_unit
, miracle_unit src_unit
)
391 /** Determine if unit is offline.
394 int miracle_unit_is_offline( miracle_unit unit
)
399 /** Obtain the status for a given unit
402 int miracle_unit_get_status( miracle_unit unit
, valerie_status status
)
404 int error
= unit
== NULL
;
406 memset( status
, 0, sizeof( valerie_status_t
) );
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
);
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
);
419 if ( info
.resource
!= NULL
&& strcmp( info
.resource
, "" ) )
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;
437 status
->generation
= mlt_properties_get_int( properties
, "generation" );
439 if ( !strcmp( status
->clip
, "" ) )
440 status
->status
= unit_not_loaded
;
441 else if ( status
->speed
== 0 )
442 status
->status
= unit_paused
;
444 status
->status
= unit_playing
;
448 status
->status
= unit_undefined
;
451 status
->unit
= mlt_properties_get_int( unit
->properties
, "unit" );
456 /** Change position in the playlist.
459 void miracle_unit_change_position( miracle_unit unit
, int clip
, int64_t position
)
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
;
471 else if ( clip
>= mlt_playlist_count( playlist
) )
473 clip
= mlt_playlist_count( playlist
) - 1;
474 position
= 999999999999;
477 if ( mlt_playlist_get_clip_info( playlist
, &info
, clip
) == 0 )
479 int64_t frame_start
= info
.start
;
480 int64_t frame_offset
= position
;
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
;
489 mlt_producer_seek( producer
, frame_start
+ frame_offset
- info
.frame_in
);
492 miracle_unit_status_communicate( unit
);
495 /** Get the index of the current clip.
498 int miracle_unit_get_current_clip( miracle_unit unit
)
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
);
506 /** Set a clip's in point
509 int miracle_unit_set_clip_in( miracle_unit unit
, int index
, int64_t position
)
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
);
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 );
527 /** Set a clip's out point.
530 int miracle_unit_set_clip_out( miracle_unit unit
, int index
, int64_t position
)
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
);
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 );
549 /** Step by specified position.
552 void miracle_unit_step( miracle_unit unit
, int64_t offset
)
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
);
561 /** Set the unit's clip mode regarding in and out points.
564 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
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 );
572 /** Get the unit's clip mode regarding in and out points.
575 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
577 //dv_player player = miracle_unit_get_dv_player( unit );
578 //return dv_player_get_clip_mode( player );
581 /** Set the unit's clip mode regarding eof handling.
584 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
586 //dv_player player = miracle_unit_get_dv_player( unit );
587 //dv_player_set_eof_action( player, action );
588 //miracle_unit_status_communicate( unit );
591 /** Get the unit's clip mode regarding eof handling.
594 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
596 //dv_player player = miracle_unit_get_dv_player( unit );
597 //return dv_player_get_eof_action( player );
600 int miracle_unit_set( miracle_unit unit
, char *name_value
)
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
);
607 char *miracle_unit_get( miracle_unit unit
, char *name
)
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
);
617 \param unit A miracle_unit handle.
620 void miracle_unit_close( miracle_unit unit
)
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
);
630 miracle_log( LOG_DEBUG
, "... unit closed." );