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