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