]> git.sesse.net Git - mlt/blob - src/miracle/miracle_unit.c
westley/libxml2 mods, mcdv/mpeg release integration
[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 #include <limits.h>
37
38 #include <sys/mman.h>
39
40 #include "miracle_unit.h"
41 #include "miracle_log.h"
42 #include "miracle_local.h"
43
44 #include <framework/mlt.h>
45
46 /* Forward references */
47 static void miracle_unit_status_communicate( miracle_unit );
48
49 /** Allocate a new DV transmission unit.
50
51     \return A new miracle_unit handle.
52 */
53
54 miracle_unit miracle_unit_init( int index, char *constructor )
55 {
56         miracle_unit this = NULL;
57         mlt_consumer consumer = NULL;
58
59         char *id = strdup( constructor );
60         char *arg = strchr( id, ':' );
61
62         if ( arg != NULL )
63                 *arg ++ = '\0';
64
65         consumer = mlt_factory_consumer( id, arg );
66
67         if ( consumer != NULL )
68         {
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 ) );
82         }
83
84         return this;
85 }
86
87 /** Communicate the current status to all threads waiting on the notifier.
88 */
89
90 static void miracle_unit_status_communicate( miracle_unit unit )
91 {
92         if ( unit != NULL )
93         {
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;
98
99                 if ( root_dir != NULL && notifier != NULL )
100                 {
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 && 
106                                                 status.in == 0 && 
107                                                 status.out == 0 ) ) */
108                                         valerie_notifier_put( notifier, &status );
109                 }
110         }
111 }
112
113 /** Set the notifier info
114 */
115
116 void miracle_unit_set_notifier( miracle_unit this, valerie_notifier notifier, char *root_dir )
117 {
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 );
121
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 );
126
127         miracle_unit_status_communicate( this );
128 }
129
130 static mlt_producer create_producer( char *file )
131 {
132         mlt_producer result = NULL;
133
134         // 1st Line preferences
135         if ( strstr( file, ".inigo" ) )
136                 result = mlt_factory_producer( "inigo_file", file );
137         else if ( strstr( file, ".westley" ) )
138                 result = mlt_factory_producer( "westley", file );
139         else if ( strstr( file, ".mpg" ) )
140                 result = mlt_factory_producer( "mcmpeg", file );
141         else if ( strstr( file, ".mpeg" ) )
142                 result = mlt_factory_producer( "mcmpeg", file );
143         else if ( strstr( file, ".dv" ) )
144                 result = mlt_factory_producer( "mcdv", file );
145         else if ( strstr( file, ".dif" ) )
146                 result = mlt_factory_producer( "mcdv", file );
147         else if ( strstr( file, ".jpg" ) )
148                 result = mlt_factory_producer( "pixbuf", file );
149         else if ( strstr( file, ".JPG" ) )
150                 result = mlt_factory_producer( "pixbuf", file );
151         else if ( strstr( file, ".jpeg" ) )
152                 result = mlt_factory_producer( "pixbuf", file );
153         else if ( strstr( file, ".png" ) )
154                 result = mlt_factory_producer( "pixbuf", file );
155         else if ( strstr( file, ".tga" ) )
156                 result = mlt_factory_producer( "pixbuf", file );
157         else if ( strstr( file, ".txt" ) )
158                 result = mlt_factory_producer( "pango", file );
159         else if ( strstr( file, ".ogg" ) )
160                 result = mlt_factory_producer( "vorbis", file );
161
162         // 2nd Line fallbacks
163         if ( result == NULL && strstr( file, ".dv" ) )
164                 result = mlt_factory_producer( "libdv", file );
165         else if ( result == NULL && strstr( file, ".dif" ) )
166                 result = mlt_factory_producer( "libdv", file );
167
168         // 3rd line fallbacks 
169         if ( result == NULL )
170                 result = mlt_factory_producer( "avformat", file );
171
172         // 4th line fallbacks 
173         if ( result == NULL )
174                 result = mlt_factory_producer( "ffmpeg", file );
175
176         return result;
177 }
178
179 /** Create or locate a producer for the file specified.
180 */
181
182 static mlt_producer locate_producer( miracle_unit unit, char *file )
183 {
184         // Get the unit properties
185         mlt_properties properties = unit->producers;
186
187         // Check if we already have loaded this file
188         mlt_producer result = mlt_properties_get_data( properties, file, NULL );
189
190         if ( result == NULL )
191         {
192                 // Create the producer
193                 result = create_producer( file );
194
195                 // Now store the result
196                 if ( result != NULL )
197                         mlt_properties_set_data( properties, file, result, 0, ( mlt_destructor )mlt_producer_close, NULL );
198         }
199
200         return result;
201 }
202
203 /** Update the generation count.
204 */
205
206 static void update_generation( miracle_unit unit )
207 {
208         mlt_properties properties = unit->properties;
209         int generation = mlt_properties_get_int( properties, "generation" );
210         mlt_properties_set_int( properties, "generation", ++ generation );
211 }
212
213 /** Wipe all clips on the playlist for this unit.
214 */
215
216 static void clear_unit( miracle_unit unit )
217 {
218         mlt_properties properties = unit->properties;
219         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
220         mlt_producer producer = mlt_playlist_producer( playlist );
221
222         mlt_playlist_clear( playlist );
223         mlt_producer_seek( producer, 0 );
224
225         if ( unit->old_producers != NULL )
226                 mlt_properties_close( unit->old_producers );
227         unit->old_producers = unit->producers;
228         unit->producers = mlt_properties_new( );
229
230         update_generation( unit );
231 }
232
233 /** Generate a report on all loaded clips.
234 */
235
236 void miracle_unit_report_list( miracle_unit unit, valerie_response response )
237 {
238         int i;
239         mlt_properties properties = unit->properties;
240         int generation = mlt_properties_get_int( properties, "generation" );
241         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
242
243         valerie_response_printf( response, 1024, "%d\n", generation );
244                 
245         for ( i = 0; i < mlt_playlist_count( playlist ); i ++ )
246         {
247                 mlt_playlist_clip_info info;
248                 mlt_playlist_get_clip_info( playlist , &info, i );
249                 valerie_response_printf( response, 10240, "%d \"%s\" %lld %lld %lld %lld %.2f\n", 
250                                                                  i, info.resource, 
251                                                                  info.frame_in, 
252                                                                  info.frame_out,
253                                                                  info.frame_count, 
254                                                                  info.length, 
255                                                                  info.fps );
256         }
257 }
258
259 /** Load a clip into the unit clearing existing play list.
260
261     \todo error handling
262     \param unit A miracle_unit handle.
263     \param clip The absolute file name of the clip to load.
264     \param in   The starting frame (-1 for 0)
265         \param out  The ending frame (-1 for maximum)
266 */
267
268 valerie_error_code miracle_unit_load( miracle_unit unit, char *clip, int64_t in, int64_t out, int flush )
269 {
270         // Now try to create an producer
271         mlt_producer instance = create_producer( clip );
272
273         if ( instance != NULL )
274         {
275                 clear_unit( unit );
276                 mlt_properties properties = unit->properties;
277                 mlt_properties_set_data( unit->producers, clip, instance, 0, ( mlt_destructor )mlt_producer_close, NULL );
278                 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
279                 mlt_playlist_append_io( playlist, instance, in, out );
280                 miracle_log( LOG_DEBUG, "loaded clip %s", clip );
281                 miracle_unit_status_communicate( unit );
282                 return valerie_ok;
283         }
284
285         return valerie_invalid_file;
286 }
287
288 valerie_error_code miracle_unit_insert( miracle_unit unit, char *clip, int index, int64_t in, int64_t out )
289 {
290         mlt_producer instance = locate_producer( unit, clip );
291
292         if ( instance != NULL )
293         {
294                 mlt_properties properties = unit->properties;
295                 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
296                 mlt_playlist_insert( playlist, instance, index, in, out );
297                 miracle_log( LOG_DEBUG, "inserted clip %s at %d", clip, index );
298                 update_generation( unit );
299                 miracle_unit_status_communicate( unit );
300                 return valerie_ok;
301         }
302
303         return valerie_invalid_file;
304 }
305
306 valerie_error_code miracle_unit_remove( miracle_unit unit, int index )
307 {
308         mlt_properties properties = unit->properties;
309         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
310         mlt_playlist_remove( playlist, index );
311         miracle_log( LOG_DEBUG, "removed clip at %d", index );
312         update_generation( unit );
313         miracle_unit_status_communicate( unit );
314         return valerie_ok;
315 }
316
317 valerie_error_code miracle_unit_clean( miracle_unit unit )
318 {
319         clear_unit( unit );
320         miracle_log( LOG_DEBUG, "Cleaned playlist" );
321         miracle_unit_status_communicate( unit );
322         return valerie_ok;
323 }
324
325 valerie_error_code miracle_unit_move( miracle_unit unit, int src, int dest )
326 {
327         mlt_properties properties = unit->properties;
328         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
329         mlt_playlist_move( playlist, src, dest );
330         miracle_log( LOG_DEBUG, "moved clip %d to %d", src, dest );
331         update_generation( unit );
332         miracle_unit_status_communicate( unit );
333         return valerie_ok;
334 }
335
336 /** Add a clip to the unit play list.
337
338     \todo error handling
339     \param unit A miracle_unit handle.
340     \param clip The absolute file name of the clip to load.
341     \param in   The starting frame (-1 for 0)
342         \param out  The ending frame (-1 for maximum)
343 */
344
345 valerie_error_code miracle_unit_append( miracle_unit unit, char *clip, int64_t in, int64_t out )
346 {
347         mlt_producer instance = locate_producer( unit, clip );
348
349         if ( instance != NULL )
350         {
351                 mlt_properties properties = unit->properties;
352                 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
353                 mlt_playlist_append_io( playlist, instance, in, out );
354                 miracle_log( LOG_DEBUG, "appended clip %s", clip );
355                 update_generation( unit );
356                 miracle_unit_status_communicate( unit );
357                 return valerie_ok;
358         }
359
360         return valerie_invalid_file;
361 }
362
363 /** Start playing the unit.
364
365     \todo error handling
366     \param unit A miracle_unit handle.
367     \param speed An integer that specifies the playback rate as a
368                  percentage multiplied by 100.
369 */
370
371 void miracle_unit_play( miracle_unit_t *unit, int speed )
372 {
373         mlt_properties properties = unit->properties;
374         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
375         mlt_producer producer = mlt_playlist_producer( playlist );
376         mlt_consumer consumer = mlt_properties_get_data( unit->properties, "consumer", NULL );
377         mlt_producer_set_speed( producer, ( double )speed / 1000 );
378         mlt_consumer_start( consumer );
379         miracle_unit_status_communicate( unit );
380 }
381
382 /** Stop playback.
383
384     Terminates the dv_pump and halts dv1394 transmission.
385
386     \param unit A miracle_unit handle.
387 */
388
389 void miracle_unit_terminate( miracle_unit unit )
390 {
391         mlt_consumer consumer = mlt_properties_get_data( unit->properties, "consumer", NULL );
392         mlt_consumer_stop( consumer );
393         miracle_unit_status_communicate( unit );
394 }
395
396 /** Query the status of unit playback.
397
398     \param unit A miracle_unit handle.
399     \return 1 if the unit is not playing, 0 if playing.
400 */
401
402 int miracle_unit_has_terminated( miracle_unit unit )
403 {
404         mlt_consumer consumer = mlt_properties_get_data( unit->properties, "consumer", NULL );
405         return mlt_consumer_is_stopped( consumer );
406 }
407
408 /** Transfer the currently loaded clip to another unit
409 */
410
411 int miracle_unit_transfer( miracle_unit dest_unit, miracle_unit src_unit )
412 {
413         return 0;
414 }
415
416 /** Determine if unit is offline.
417 */
418
419 int miracle_unit_is_offline( miracle_unit unit )
420 {
421         return 0;
422 }
423
424 /** Obtain the status for a given unit
425 */
426
427 int miracle_unit_get_status( miracle_unit unit, valerie_status status )
428 {
429         int error = unit == NULL;
430
431         memset( status, 0, sizeof( valerie_status_t ) );
432
433         if ( !error )
434         {
435                 mlt_properties properties = unit->properties;
436                 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
437                 mlt_producer producer = mlt_playlist_producer( playlist );
438                 mlt_producer clip = mlt_playlist_current( playlist );
439
440                 mlt_playlist_clip_info info;
441                 int clip_index = mlt_playlist_current_clip( playlist );
442                 mlt_playlist_get_clip_info( playlist, &info, clip_index );
443
444                 if ( info.resource != NULL && strcmp( info.resource, "" ) )
445                 {
446                         strncpy( status->clip, info.resource, sizeof( status->clip ) );
447                         status->speed = (int)( mlt_producer_get_speed( producer ) * 1000.0 );
448                         status->fps = mlt_producer_get_fps( producer );
449                         status->in = info.frame_in;
450                         status->out = info.frame_out;
451                         status->position = mlt_producer_position( clip );
452                         status->length = mlt_producer_get_length( clip );
453                         strncpy( status->tail_clip, info.resource, sizeof( status->tail_clip ) );
454                         status->tail_in = info.frame_in;
455                         status->tail_out = info.frame_out;
456                         status->tail_position = mlt_producer_position( clip );
457                         status->tail_length = mlt_producer_get_length( clip );
458                         status->clip_index = mlt_playlist_current_clip( playlist );
459                         status->seek_flag = 1;
460                 }
461
462                 status->generation = mlt_properties_get_int( properties, "generation" );
463
464                 if ( miracle_unit_has_terminated( unit ) )
465                         status->status = unit_stopped;
466                 else if ( !strcmp( status->clip, "" ) )
467                         status->status = unit_not_loaded;
468                 else if ( status->speed == 0 )
469                         status->status = unit_paused;
470                 else
471                         status->status = unit_playing;
472         }
473         else
474         {
475                 status->status = unit_undefined;
476         }
477
478         status->unit = mlt_properties_get_int( unit->properties, "unit" );
479
480         return error;
481 }
482
483 /** Change position in the playlist.
484 */
485
486 void miracle_unit_change_position( miracle_unit unit, int clip, int64_t position )
487 {
488         mlt_properties properties = unit->properties;
489         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
490         mlt_producer producer = mlt_playlist_producer( playlist );
491         mlt_playlist_clip_info info;
492
493         if ( clip < 0 )
494         {
495                 clip = 0;
496                 position = 0;
497         }
498         else if ( clip >= mlt_playlist_count( playlist ) )
499         {
500                 clip = mlt_playlist_count( playlist ) - 1;
501                 position = LONG_MAX;
502         }
503
504         if ( mlt_playlist_get_clip_info( playlist, &info, clip ) == 0 )
505         {
506                 int64_t frame_start = info.start;
507                 int64_t frame_offset = position;
508
509                 if ( frame_offset < 0 )
510                         frame_offset = info.frame_out;
511                 if ( frame_offset < info.frame_in )
512                         frame_offset = info.frame_in;
513                 if ( frame_offset >= info.frame_out )
514                         frame_offset = info.frame_out;
515                 
516                 mlt_producer_seek( producer, frame_start + frame_offset - info.frame_in );
517         }
518
519         miracle_unit_status_communicate( unit );
520 }
521
522 /** Get the index of the current clip.
523 */
524
525 int     miracle_unit_get_current_clip( miracle_unit unit )
526 {
527         mlt_properties properties = unit->properties;
528         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
529         int clip_index = mlt_playlist_current_clip( playlist );
530         return clip_index;
531 }
532
533 /** Set a clip's in point
534 */
535
536 int miracle_unit_set_clip_in( miracle_unit unit, int index, int64_t position )
537 {
538         mlt_properties properties = unit->properties;
539         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
540         mlt_playlist_clip_info info;
541         int error = mlt_playlist_get_clip_info( playlist, &info, index );
542
543         if ( error == 0 )
544         {
545                 miracle_unit_play( unit, 0 );
546                 error = mlt_playlist_resize_clip( playlist, index, position, info.frame_out );
547                 update_generation( unit );
548                 miracle_unit_change_position( unit, index, 0 );
549         }
550
551         return error;
552 }
553
554 /** Set a clip's out point.
555 */
556
557 int miracle_unit_set_clip_out( miracle_unit unit, int index, int64_t position )
558 {
559         mlt_properties properties = unit->properties;
560         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
561         mlt_playlist_clip_info info;
562         int error = mlt_playlist_get_clip_info( playlist, &info, index );
563
564         if ( error == 0 )
565         {
566                 miracle_unit_play( unit, 0 );
567                 error = mlt_playlist_resize_clip( playlist, index, info.frame_in, position );
568                 update_generation( unit );
569                 miracle_unit_status_communicate( unit );
570                 miracle_unit_change_position( unit, index, -1 );
571         }
572
573         return error;
574 }
575
576 /** Step by specified position.
577 */
578
579 void miracle_unit_step( miracle_unit unit, int64_t offset )
580 {
581         mlt_properties properties = unit->properties;
582         mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
583         mlt_producer producer = mlt_playlist_producer( playlist );
584         mlt_position position = mlt_producer_frame( producer );
585         mlt_producer_seek( producer, position + offset );
586 }
587
588 /** Set the unit's clip mode regarding in and out points.
589 */
590
591 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
592 //{
593         //dv_player player = miracle_unit_get_dv_player( unit );
594         //if ( player != NULL )
595                 //dv_player_set_clip_mode( player, mode );
596         //miracle_unit_status_communicate( unit );
597 //}
598
599 /** Get the unit's clip mode regarding in and out points.
600 */
601
602 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
603 //{
604         //dv_player player = miracle_unit_get_dv_player( unit );
605         //return dv_player_get_clip_mode( player );
606 //}
607
608 /** Set the unit's clip mode regarding eof handling.
609 */
610
611 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
612 //{
613         //dv_player player = miracle_unit_get_dv_player( unit );
614         //dv_player_set_eof_action( player, action );
615         //miracle_unit_status_communicate( unit );
616 //}
617
618 /** Get the unit's clip mode regarding eof handling.
619 */
620
621 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
622 //{
623         //dv_player player = miracle_unit_get_dv_player( unit );
624         //return dv_player_get_eof_action( player );
625 //}
626
627 int miracle_unit_set( miracle_unit unit, char *name_value )
628 {
629         mlt_playlist playlist = mlt_properties_get_data( unit->properties, "playlist", NULL );
630         mlt_properties properties = mlt_playlist_properties( playlist );
631         return mlt_properties_parse( properties, name_value );
632 }
633
634 char *miracle_unit_get( miracle_unit unit, char *name )
635 {
636         mlt_playlist playlist = mlt_properties_get_data( unit->properties, "playlist", NULL );
637         mlt_properties properties = mlt_playlist_properties( playlist );
638         return mlt_properties_get( properties, name );
639 }
640
641 /** Release the unit
642
643     \todo error handling
644     \param unit A miracle_unit handle.
645 */
646
647 void miracle_unit_close( miracle_unit unit )
648 {
649         if ( unit != NULL )
650         {
651                 miracle_log( LOG_DEBUG, "closing unit..." );
652                 if ( unit->old_producers != NULL )
653                         mlt_properties_close( unit->old_producers );
654                 mlt_properties_close( unit->properties );
655                 mlt_properties_close( unit->producers );
656                 free( unit );
657                 miracle_log( LOG_DEBUG, "... unit closed." );
658         }
659 }
660