]> git.sesse.net Git - mlt/blob - src/miracle/miracle_unit.c
miracle part 1
[mlt] / src / miracle / miracle_unit.c
1 /*
2  * miracle_unit.c -- DV Transmission Unit Implementation
3  * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <sys/poll.h>
30 #include <sys/types.h>
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <signal.h>
36
37 #include <sys/mman.h>
38
39 #include "miracle_unit.h"
40 #include "miracle_log.h"
41 #include "miracle_local.h"
42
43 #include <framework/mlt.h>
44
45 /* Forward references */
46 static void miracle_unit_status_communicate( miracle_unit );
47
48 /** Allocate a new DV transmission unit.
49
50     \return A new miracle_unit handle.
51 */
52
53 miracle_unit miracle_unit_init( int index, char *constructor )
54 {
55         miracle_unit this = NULL;
56         mlt_consumer consumer = NULL;
57
58         char *id = strdup( constructor );
59         char *arg = strchr( id, ':' );
60
61         if ( arg != NULL )
62                 *arg ++ = '\0';
63
64         consumer = mlt_factory_consumer( id, arg );
65
66         if ( consumer != NULL )
67         {
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 ) );
81         }
82
83         return this;
84 }
85
86 /** Communicate the current status to all threads waiting on the notifier.
87 */
88
89 static void miracle_unit_status_communicate( miracle_unit unit )
90 {
91         if ( unit != NULL )
92         {
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;
97
98                 if ( root_dir != NULL && notifier != NULL )
99                 {
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 && 
105                                                 status.in == 0 && 
106                                                 status.out == 0 ) ) */
107                                         valerie_notifier_put( notifier, &status );
108                 }
109         }
110 }
111
112 /** Set the notifier info
113 */
114
115 void miracle_unit_set_notifier( miracle_unit this, valerie_notifier notifier, char *root_dir )
116 {
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 );
120
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 );
125
126         miracle_unit_status_communicate( this );
127 }
128
129 /** Create or locate a producer for the file specified.
130 */
131
132 static mlt_producer create_producer( miracle_unit unit, char *file )
133 {
134         // Get the unit properties
135         mlt_properties properties = unit->producers;
136
137         // Check if we already have loaded this file
138         mlt_producer result = mlt_properties_get_data( properties, file, NULL );
139
140         if ( result == NULL )
141         {
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 );
159         
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 );
165         
166                 // 3rd line fallbacks 
167                 if ( result == NULL )
168                         result = mlt_factory_producer( "ffmpeg", file );
169
170                 // Now store the result
171                 mlt_properties_set_data( properties, file, result, 0, ( mlt_destructor )mlt_producer_close, NULL );
172         }
173
174         return result;
175 }
176
177 /** Update the generation count.
178 */
179
180 static void update_generation( miracle_unit unit )
181 {
182         mlt_properties properties = unit->properties;
183         int generation = mlt_properties_get_int( properties, "generation" );
184         mlt_properties_set_int( properties, "generation", ++ generation );
185 }
186
187 static void clear_unit( miracle_unit unit )
188 {
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 );
192
193         mlt_playlist_clear( playlist );
194         mlt_producer_seek( producer, 0 );
195
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( );
200
201         update_generation( unit );
202 }
203
204 /** Generate a report on all loaded clips.
205 */
206
207 void miracle_unit_report_list( miracle_unit unit, valerie_response response )
208 {
209         int i;
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 );
213
214         valerie_response_printf( response, 1024, "%d\n", generation );
215                 
216         for ( i = 0; i < mlt_playlist_count( playlist ); i ++ )
217         {
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 );
222         }
223 }
224
225 /** Load a clip into the unit clearing existing play list.
226
227     \todo error handling
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)
232 */
233
234 valerie_error_code miracle_unit_load( miracle_unit unit, char *clip, double in, double out, int flush )
235 {
236         // Have to clear the unit first
237         clear_unit( unit );
238
239         // Now try to create an producer
240         mlt_producer instance = create_producer( unit, clip );
241
242         if ( instance != NULL )
243         {
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 );
249                 return valerie_ok;
250         }
251
252         return valerie_invalid_file;
253 }
254
255 valerie_error_code miracle_unit_insert( miracle_unit unit, const char *clip, int index, double in, double out )
256 {
257         /*
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 )
261         {
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 );
266         }
267         return error;
268         */
269         return valerie_ok;
270 }
271
272 valerie_error_code miracle_unit_remove( miracle_unit unit, int index )
273 {
274         /*
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 )
278         {
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 );
283         }
284         return error;
285         */
286         return valerie_ok;
287 }
288
289 valerie_error_code miracle_unit_clean( miracle_unit unit )
290 {
291         clear_unit( unit );
292         miracle_log( LOG_DEBUG, "Cleaned playlist" );
293         return valerie_ok;
294 }
295
296 valerie_error_code miracle_unit_move( miracle_unit unit, int src, int dest )
297 {
298         /*
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 )
302         {
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 );
307         }
308         return error;
309         */
310         return valerie_ok;
311 }
312
313 /** Add a clip to the unit play list.
314
315     \todo error handling
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)
320 */
321
322 valerie_error_code miracle_unit_append( miracle_unit unit, char *clip, double in, double out )
323 {
324         mlt_producer instance = create_producer( unit, clip );
325
326         if ( instance != NULL )
327         {
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 );
333                 return valerie_ok;
334         }
335
336         return valerie_invalid_file;
337 }
338
339 /** Start playing the clip.
340
341     Start a dv-pump and commence dv1394 transmission.
342
343     \todo error handling
344     \param unit A miracle_unit handle.
345     \param speed An integer that specifies the playback rate as a
346                  percentage multiplied by 100.
347 */
348
349 void miracle_unit_play( miracle_unit_t *unit, int speed )
350 {
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 );
356 }
357
358 /** Stop playback.
359
360     Terminates the dv_pump and halts dv1394 transmission.
361
362     \param unit A miracle_unit handle.
363 */
364
365 void miracle_unit_terminate( miracle_unit unit )
366 {
367 }
368
369 /** Query the status of unit playback.
370
371     \param unit A miracle_unit handle.
372     \return 1 if the unit is not playing, 0 if playing.
373 */
374
375 int miracle_unit_has_terminated( miracle_unit unit )
376 {
377         return 0;
378 }
379
380 /** Transfer the currently loaded clip to another unit
381 */
382
383 int miracle_unit_transfer( miracle_unit dest_unit, miracle_unit src_unit )
384 {
385         return 0;
386 }
387
388 /** Determine if unit is offline.
389 */
390
391 int miracle_unit_is_offline( miracle_unit unit )
392 {
393         return 0;
394 }
395
396 /** Obtain the status for a given unit
397 */
398
399 int miracle_unit_get_status( miracle_unit unit, valerie_status status )
400 {
401         int error = unit == NULL;
402
403         memset( status, 0, sizeof( valerie_status_t ) );
404
405         if ( !error )
406         {
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 );
411
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 );
415
416                 if ( info.resource != NULL && strcmp( info.resource, "" ) )
417                 {
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;
426                 }
427
428                 if ( !strcmp( status->clip, "" ) )
429                         status->status = unit_not_loaded;
430                 else if ( status->speed == 0 )
431                         status->status = unit_paused;
432                 else
433                         status->status = unit_playing;
434         }
435         else
436         {
437                 status->status = unit_undefined;
438         }
439
440         status->unit = mlt_properties_get_int( unit->properties, "unit" );
441
442         return error;
443 }
444
445 /** Change position in the playlist.
446 */
447
448 void miracle_unit_change_position( miracle_unit unit, int clip, double position )
449 {
450         miracle_unit_status_communicate( unit );
451 }
452
453 /** Change speed.
454 */
455
456 void miracle_unit_change_speed( miracle_unit unit, int speed )
457 {
458 }
459
460 int     miracle_unit_get_current_clip( miracle_unit unit )
461 {
462         return 0;
463 }
464
465 /** Set a clip's in point
466 */
467
468 int miracle_unit_set_clip_in( miracle_unit unit, int index, double position )
469 {
470         int error = 0;
471         return error;
472 }
473
474 /** Set a clip's out point.
475 */
476
477 int miracle_unit_set_clip_out( miracle_unit unit, int index, double position )
478 {
479         int error = 0;
480         return error;
481 }
482
483 /** Step by specified position.
484 */
485
486 void miracle_unit_step( miracle_unit unit, double offset )
487 {
488 }
489
490 /** Set the unit's clip mode regarding in and out points.
491 */
492
493 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
494 //{
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 );
499 //}
500
501 /** Get the unit's clip mode regarding in and out points.
502 */
503
504 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
505 //{
506         //dv_player player = miracle_unit_get_dv_player( unit );
507         //return dv_player_get_clip_mode( player );
508 //}
509
510 /** Set the unit's clip mode regarding eof handling.
511 */
512
513 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
514 //{
515         //dv_player player = miracle_unit_get_dv_player( unit );
516         //dv_player_set_eof_action( player, action );
517         //miracle_unit_status_communicate( unit );
518 //}
519
520 /** Get the unit's clip mode regarding eof handling.
521 */
522
523 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
524 //{
525         //dv_player player = miracle_unit_get_dv_player( unit );
526         //return dv_player_get_eof_action( player );
527 //}
528
529 /** Release the unit
530
531     \todo error handling
532     \param unit A miracle_unit handle.
533 */
534
535 void miracle_unit_close( miracle_unit unit )
536 {
537         if ( unit != NULL )
538         {
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 );
544                 free( unit );
545                 miracle_log( LOG_DEBUG, "... unit closed." );
546         }
547 }
548