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, ".mpg" ) )
144 result = mlt_factory_producer( "mcmpeg", file );
145 else if ( strstr( file, ".mpeg" ) )
146 result = mlt_factory_producer( "mcmpeg", file );
147 else if ( strstr( file, ".dv" ) )
148 result = mlt_factory_producer( "mcdv", file );
149 else if ( strstr( file, ".dif" ) )
150 result = mlt_factory_producer( "mcdv", file );
151 else if ( strstr( file, ".jpg" ) )
152 result = mlt_factory_producer( "pixbuf", file );
153 else if ( strstr( file, ".JPG" ) )
154 result = mlt_factory_producer( "pixbuf", file );
155 else if ( strstr( file, ".jpeg" ) )
156 result = mlt_factory_producer( "pixbuf", file );
157 else if ( strstr( file, ".png" ) )
158 result = mlt_factory_producer( "pixbuf", file );
160 // 2nd Line fallbacks
161 if ( result == NULL && strstr( file, ".dv" ) )
162 result = mlt_factory_producer( "libdv", file );
163 else if ( result == NULL && strstr( file, ".dif" ) )
164 result = mlt_factory_producer( "libdv", file );
166 // 3rd line fallbacks
167 if ( result == NULL )
168 result = mlt_factory_producer( "ffmpeg", file );
170 // Now store the result
171 mlt_properties_set_data( properties, file, result, 0, ( mlt_destructor )mlt_producer_close, NULL );
177 /** Update the generation count.
180 static void update_generation( miracle_unit unit )
182 mlt_properties properties = unit->properties;
183 int generation = mlt_properties_get_int( properties, "generation" );
184 mlt_properties_set_int( properties, "generation", ++ generation );
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 /** Generate a report on all loaded clips.
207 void miracle_unit_report_list( miracle_unit unit, valerie_response response )
210 mlt_properties properties = unit->properties;
211 int generation = mlt_properties_get_int( properties, "generation" );
212 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
214 valerie_response_printf( response, 1024, "%d\n", generation );
216 for ( i = 0; i < mlt_playlist_count( playlist ); i ++ )
218 mlt_playlist_clip_info info;
219 mlt_playlist_get_clip_info( playlist , &info, i );
220 valerie_response_printf( response, 10240, "%d \"%s\" %e %e %e %e %.2f\n",
221 i, info.resource, info.in, info.out, info.playtime, info.length, info.fps );
225 /** Load a clip into the unit clearing existing play list.
228 \param unit A miracle_unit handle.
229 \param clip The absolute file name of the clip to load.
230 \param in The starting frame (-1 for 0)
231 \param out The ending frame (-1 for maximum)
234 valerie_error_code miracle_unit_load( miracle_unit unit, char *clip, double in, double out, int flush )
236 // Have to clear the unit first
239 // Now try to create an producer
240 mlt_producer instance = create_producer( unit, clip );
242 if ( instance != NULL )
244 mlt_properties properties = unit->properties;
245 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
246 mlt_playlist_append_io( playlist, instance, in, out );
247 miracle_log( LOG_DEBUG, "loaded clip %s", clip );
248 miracle_unit_status_communicate( unit );
252 return valerie_invalid_file;
255 valerie_error_code miracle_unit_insert( miracle_unit unit, const char *clip, int index, double in, double out )
258 dv_player player = miracle_unit_get_dv_player( unit );
259 valerie_error_code error = dv_player_get_error( player );
260 if ( error == dv_pump_ok )
262 error = dv_player_insert_file( player, (char*) clip, index, in, out );
263 dv1394d_log( LOG_DEBUG, "inserted clip %s", clip );
264 if ( unit->is_terminated )
265 miracle_unit_status_communicate( unit );
272 valerie_error_code miracle_unit_remove( miracle_unit unit, int index )
275 dv_player player = miracle_unit_get_dv_player( unit );
276 valerie_error_code error = dv_player_get_error( player );
277 if ( error == dv_pump_ok )
279 error = dv_player_remove_clip( player, index );
280 dv1394d_log( LOG_DEBUG, "removed clip %d", index );
281 if ( unit->is_terminated )
282 miracle_unit_status_communicate( unit );
289 valerie_error_code miracle_unit_clean( miracle_unit unit )
292 miracle_log( LOG_DEBUG, "Cleaned playlist" );
296 valerie_error_code miracle_unit_move( miracle_unit unit, int src, int dest )
299 dv_player player = miracle_unit_get_dv_player( unit );
300 valerie_error_code error = dv_player_get_error( player );
301 if ( error == dv_pump_ok )
303 error = dv_player_move_clip( player, src, dest );
304 dv1394d_log( LOG_DEBUG, "moved clip %d to %d", src, dest );
305 if ( unit->is_terminated )
306 miracle_unit_status_communicate( unit );
313 /** Add a clip to the unit play list.
316 \param unit A miracle_unit handle.
317 \param clip The absolute file name of the clip to load.
318 \param in The starting frame (-1 for 0)
319 \param out The ending frame (-1 for maximum)
322 valerie_error_code miracle_unit_append( miracle_unit unit, char *clip, double in, double out )
324 mlt_producer instance = create_producer( unit, clip );
326 if ( instance != NULL )
328 mlt_properties properties = unit->properties;
329 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
330 mlt_playlist_append_io( playlist, instance, in, out );
331 miracle_log( LOG_DEBUG, "appended clip %s", clip );
332 miracle_unit_status_communicate( unit );
336 return valerie_invalid_file;
339 /** Start playing the clip.
341 Start a dv-pump and commence dv1394 transmission.
344 \param unit A miracle_unit handle.
345 \param speed An integer that specifies the playback rate as a
346 percentage multiplied by 100.
349 void miracle_unit_play( miracle_unit_t *unit, int speed )
351 mlt_properties properties = unit->properties;
352 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
353 mlt_producer producer = mlt_playlist_producer( playlist );
354 mlt_producer_set_speed( producer, ( double )speed / 1000 );
355 miracle_unit_status_communicate( unit );
360 Terminates the dv_pump and halts dv1394 transmission.
362 \param unit A miracle_unit handle.
365 void miracle_unit_terminate( miracle_unit unit )
369 /** Query the status of unit playback.
371 \param unit A miracle_unit handle.
372 \return 1 if the unit is not playing, 0 if playing.
375 int miracle_unit_has_terminated( miracle_unit unit )
380 /** Transfer the currently loaded clip to another unit
383 int miracle_unit_transfer( miracle_unit dest_unit, miracle_unit src_unit )
388 /** Determine if unit is offline.
391 int miracle_unit_is_offline( miracle_unit unit )
396 /** Obtain the status for a given unit
399 int miracle_unit_get_status( miracle_unit unit, valerie_status status )
401 int error = unit == NULL;
403 memset( status, 0, sizeof( valerie_status_t ) );
407 mlt_properties properties = unit->properties;
408 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
409 mlt_producer producer = mlt_playlist_producer( playlist );
410 mlt_producer clip = mlt_playlist_current( playlist );
412 mlt_playlist_clip_info info;
413 int clip_index = mlt_playlist_current_clip( playlist );
414 mlt_playlist_get_clip_info( playlist, &info, clip_index );
416 if ( info.resource != NULL && strcmp( info.resource, "" ) )
418 strncpy( status->clip, info.resource, sizeof( status->clip ) );
419 status->speed = (int)( mlt_producer_get_speed( producer ) * 1000.0 );
420 status->fps = mlt_producer_get_fps( producer );
421 status->in = info.in;
422 status->out = info.in + info.playtime;
423 status->position = mlt_producer_position( clip );
424 status->length = mlt_producer_get_length( clip );
425 status->seek_flag = 1;
428 if ( !strcmp( status->clip, "" ) )
429 status->status = unit_not_loaded;
430 else if ( status->speed == 0 )
431 status->status = unit_paused;
433 status->status = unit_playing;
437 status->status = unit_undefined;
440 status->unit = mlt_properties_get_int( unit->properties, "unit" );
445 /** Change position in the playlist.
448 void miracle_unit_change_position( miracle_unit unit, int clip, double position )
450 miracle_unit_status_communicate( unit );
456 void miracle_unit_change_speed( miracle_unit unit, int speed )
460 int miracle_unit_get_current_clip( miracle_unit unit )
465 /** Set a clip's in point
468 int miracle_unit_set_clip_in( miracle_unit unit, int index, double position )
474 /** Set a clip's out point.
477 int miracle_unit_set_clip_out( miracle_unit unit, int index, double position )
483 /** Step by specified position.
486 void miracle_unit_step( miracle_unit unit, double offset )
490 /** Set the unit's clip mode regarding in and out points.
493 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
495 //dv_player player = miracle_unit_get_dv_player( unit );
496 //if ( player != NULL )
497 //dv_player_set_clip_mode( player, mode );
498 //miracle_unit_status_communicate( unit );
501 /** Get the unit's clip mode regarding in and out points.
504 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
506 //dv_player player = miracle_unit_get_dv_player( unit );
507 //return dv_player_get_clip_mode( player );
510 /** Set the unit's clip mode regarding eof handling.
513 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
515 //dv_player player = miracle_unit_get_dv_player( unit );
516 //dv_player_set_eof_action( player, action );
517 //miracle_unit_status_communicate( unit );
520 /** Get the unit's clip mode regarding eof handling.
523 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
525 //dv_player player = miracle_unit_get_dv_player( unit );
526 //return dv_player_get_eof_action( player );
532 \param unit A miracle_unit handle.
535 void miracle_unit_close( miracle_unit unit )
539 miracle_log( LOG_DEBUG, "closing unit..." );
540 if ( unit->old_producers != NULL )
541 mlt_properties_close( unit->old_producers );
542 mlt_properties_close( unit->properties );
543 mlt_properties_close( unit->producers );
545 miracle_log( LOG_DEBUG, "... unit closed." );