Miracle mods - clean working, test card fix, silence dv when not playing
[melted] / 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 static char *strip_root( miracle_unit unit, char *file )
88 {
89 mlt_properties properties = unit->properties;
90 char *root = mlt_properties_get( properties, "root" );
91 if ( file != NULL && root != NULL )
92 {
93 int length = strlen( root );
94 if ( root[ length - 1 ] == '/' )
95 length --;
96 if ( !strncmp( file, root, length ) )
97 file += length;
98 }
99 return file;
100 }
101
102 /** Communicate the current status to all threads waiting on the notifier.
103 */
104
105 static void miracle_unit_status_communicate( miracle_unit unit )
106 {
107 if ( unit != NULL )
108 {
109 mlt_properties properties = unit->properties;
110 char *root_dir = mlt_properties_get( properties, "root" );
111 valerie_notifier notifier = mlt_properties_get_data( properties, "notifier", NULL );
112 valerie_status_t status;
113
114 if ( root_dir != NULL && notifier != NULL )
115 {
116 if ( miracle_unit_get_status( unit, &status ) == 0 )
117 /* if ( !( ( status.status == unit_playing || status.status == unit_paused ) &&
118 strcmp( status.clip, "" ) &&
119 !strcmp( status.tail_clip, "" ) &&
120 status.position == 0 &&
121 status.in == 0 &&
122 status.out == 0 ) ) */
123 valerie_notifier_put( notifier, &status );
124 }
125 }
126 }
127
128 /** Set the notifier info
129 */
130
131 void miracle_unit_set_notifier( miracle_unit this, valerie_notifier notifier, char *root_dir )
132 {
133 mlt_properties properties = this->properties;
134 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
135 mlt_properties playlist_properties = mlt_playlist_properties( playlist );
136
137 mlt_properties_set( properties, "root", root_dir );
138 mlt_properties_set_data( properties, "notifier", notifier, 0, NULL, NULL );
139 mlt_properties_set_data( playlist_properties, "notifier_arg", this, 0, NULL, NULL );
140 mlt_properties_set_data( playlist_properties, "notifier", miracle_unit_status_communicate, 0, NULL, NULL );
141
142 miracle_unit_status_communicate( this );
143 }
144
145 static mlt_producer create_producer( char *file )
146 {
147 return mlt_factory_producer( "fezzik", file );
148 }
149
150 /** Create or locate a producer for the file specified.
151 */
152
153 static mlt_producer locate_producer( miracle_unit unit, char *file )
154 {
155 // Get the unit properties
156 mlt_properties properties = unit->producers;
157
158 // Check if we already have loaded this file
159 mlt_producer result = mlt_properties_get_data( properties, file, NULL );
160
161 if ( result == NULL )
162 {
163 // Create the producer
164 result = create_producer( file );
165
166 // Now store the result
167 if ( result != NULL )
168 mlt_properties_set_data( properties, file, result, 0, ( mlt_destructor )mlt_producer_close, NULL );
169 }
170
171 return result;
172 }
173
174 /** Update the generation count.
175 */
176
177 static void update_generation( miracle_unit unit )
178 {
179 mlt_properties properties = unit->properties;
180 int generation = mlt_properties_get_int( properties, "generation" );
181 mlt_properties_set_int( properties, "generation", ++ generation );
182 }
183
184 /** Wipe all clips on the playlist for this unit.
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 /** Wipe all but the playing clip from the unit.
205 */
206
207 static void clean_unit( miracle_unit unit )
208 {
209 mlt_properties properties = unit->properties;
210 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
211 mlt_playlist_clip_info info;
212 int current = mlt_playlist_current_clip( playlist );
213 mlt_producer producer = mlt_playlist_producer( playlist );
214 mlt_position position = mlt_producer_frame( producer );
215 double speed = mlt_producer_get_speed( producer );
216 mlt_playlist_get_clip_info( playlist, &info, current );
217
218 if ( info.resource != NULL )
219 {
220 void *instance = mlt_properties_get_data( unit->producers, info.resource, NULL );
221 position -= info.start;
222 mlt_properties_set_data( unit->producers, info.resource, instance, 0, NULL, NULL );
223 clear_unit( unit );
224 mlt_playlist_append_io( playlist, instance, info.frame_in, info.frame_out );
225 mlt_properties_set_data( unit->producers, info.resource, instance, 0, ( mlt_destructor )mlt_producer_close, NULL );
226 mlt_producer_seek( producer, position );
227 mlt_producer_set_speed( producer, speed );
228 }
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,
251 strip_root( unit, info.resource ),
252 info.frame_in,
253 info.frame_out,
254 info.frame_count,
255 info.length,
256 info.fps );
257 }
258 }
259
260 /** Load a clip into the unit clearing existing play list.
261
262 \todo error handling
263 \param unit A miracle_unit handle.
264 \param clip The absolute file name of the clip to load.
265 \param in The starting frame (-1 for 0)
266 \param out The ending frame (-1 for maximum)
267 */
268
269 valerie_error_code miracle_unit_load( miracle_unit unit, char *clip, int64_t in, int64_t out, int flush )
270 {
271 // Now try to create an producer
272 mlt_producer instance = create_producer( clip );
273
274 if ( instance != NULL )
275 {
276 clear_unit( unit );
277 mlt_properties properties = unit->properties;
278 mlt_properties_set_data( unit->producers, clip, instance, 0, ( mlt_destructor )mlt_producer_close, NULL );
279 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
280 mlt_playlist_append_io( playlist, instance, in, out );
281 miracle_log( LOG_DEBUG, "loaded clip %s", clip );
282 miracle_unit_status_communicate( unit );
283 return valerie_ok;
284 }
285
286 return valerie_invalid_file;
287 }
288
289 valerie_error_code miracle_unit_insert( miracle_unit unit, char *clip, int index, int64_t in, int64_t out )
290 {
291 mlt_producer instance = locate_producer( unit, clip );
292
293 if ( instance != NULL )
294 {
295 mlt_properties properties = unit->properties;
296 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
297 fprintf( stderr, "inserting clip %s before %d\n", clip, index );
298 mlt_playlist_insert( playlist, instance, index, in, out );
299 miracle_log( LOG_DEBUG, "inserted clip %s at %d", clip, index );
300 update_generation( unit );
301 miracle_unit_status_communicate( unit );
302 return valerie_ok;
303 }
304
305 return valerie_invalid_file;
306 }
307
308 valerie_error_code miracle_unit_remove( miracle_unit unit, int index )
309 {
310 mlt_properties properties = unit->properties;
311 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
312 mlt_playlist_remove( playlist, index );
313 miracle_log( LOG_DEBUG, "removed clip at %d", index );
314 update_generation( unit );
315 miracle_unit_status_communicate( unit );
316 return valerie_ok;
317 }
318
319 valerie_error_code miracle_unit_clean( miracle_unit unit )
320 {
321 clean_unit( unit );
322 miracle_log( LOG_DEBUG, "Cleaned playlist" );
323 miracle_unit_status_communicate( unit );
324 return valerie_ok;
325 }
326
327 valerie_error_code miracle_unit_move( miracle_unit unit, int src, int dest )
328 {
329 mlt_properties properties = unit->properties;
330 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
331 mlt_playlist_move( playlist, src, dest );
332 miracle_log( LOG_DEBUG, "moved clip %d to %d", src, dest );
333 update_generation( unit );
334 miracle_unit_status_communicate( unit );
335 return valerie_ok;
336 }
337
338 /** Add a clip to the unit play list.
339
340 \todo error handling
341 \param unit A miracle_unit handle.
342 \param clip The absolute file name of the clip to load.
343 \param in The starting frame (-1 for 0)
344 \param out The ending frame (-1 for maximum)
345 */
346
347 valerie_error_code miracle_unit_append( miracle_unit unit, char *clip, int64_t in, int64_t out )
348 {
349 mlt_producer instance = locate_producer( unit, clip );
350
351 if ( instance != NULL )
352 {
353 mlt_properties properties = unit->properties;
354 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
355 mlt_playlist_append_io( playlist, instance, in, out );
356 miracle_log( LOG_DEBUG, "appended clip %s", clip );
357 update_generation( unit );
358 miracle_unit_status_communicate( unit );
359 return valerie_ok;
360 }
361
362 return valerie_invalid_file;
363 }
364
365 /** Start playing the unit.
366
367 \todo error handling
368 \param unit A miracle_unit handle.
369 \param speed An integer that specifies the playback rate as a
370 percentage multiplied by 100.
371 */
372
373 void miracle_unit_play( miracle_unit_t *unit, int speed )
374 {
375 mlt_properties properties = unit->properties;
376 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
377 mlt_producer producer = mlt_playlist_producer( playlist );
378 mlt_consumer consumer = mlt_properties_get_data( unit->properties, "consumer", NULL );
379 mlt_producer_set_speed( producer, ( double )speed / 1000 );
380 mlt_consumer_start( consumer );
381 miracle_unit_status_communicate( unit );
382 }
383
384 /** Stop playback.
385
386 Terminates the dv_pump and halts dv1394 transmission.
387
388 \param unit A miracle_unit handle.
389 */
390
391 void miracle_unit_terminate( miracle_unit unit )
392 {
393 mlt_consumer consumer = mlt_properties_get_data( unit->properties, "consumer", NULL );
394 mlt_consumer_stop( consumer );
395 miracle_unit_status_communicate( unit );
396 }
397
398 /** Query the status of unit playback.
399
400 \param unit A miracle_unit handle.
401 \return 1 if the unit is not playing, 0 if playing.
402 */
403
404 int miracle_unit_has_terminated( miracle_unit unit )
405 {
406 mlt_consumer consumer = mlt_properties_get_data( unit->properties, "consumer", NULL );
407 return mlt_consumer_is_stopped( consumer );
408 }
409
410 /** Transfer the currently loaded clip to another unit
411 */
412
413 int miracle_unit_transfer( miracle_unit dest_unit, miracle_unit src_unit )
414 {
415 int i;
416 mlt_properties dest_properties = dest_unit->properties;
417 mlt_playlist dest_playlist = mlt_properties_get_data( dest_properties, "playlist", NULL );
418 mlt_properties src_properties = src_unit->properties;
419 mlt_playlist src_playlist = mlt_properties_get_data( src_properties, "playlist", NULL );
420
421 for ( i = 0; i < mlt_playlist_count( src_playlist ); i ++ )
422 {
423 mlt_playlist_clip_info info;
424 mlt_playlist_get_clip_info( src_playlist, &info, i );
425 mlt_producer producer = locate_producer( dest_unit, info.resource );
426 if ( producer != NULL )
427 {
428 mlt_playlist_append_io( dest_playlist, producer, info.frame_in, info.frame_out );
429 if ( i == 0 )
430 {
431 miracle_unit_change_position( dest_unit, mlt_playlist_count( dest_playlist ) - 1, 0 );
432 clean_unit( dest_unit );
433 }
434 }
435 }
436
437 return 0;
438 }
439
440 /** Determine if unit is offline.
441 */
442
443 int miracle_unit_is_offline( miracle_unit unit )
444 {
445 return 0;
446 }
447
448 /** Obtain the status for a given unit
449 */
450
451 int miracle_unit_get_status( miracle_unit unit, valerie_status status )
452 {
453 int error = unit == NULL;
454
455 memset( status, 0, sizeof( valerie_status_t ) );
456
457 if ( !error )
458 {
459 mlt_properties properties = unit->properties;
460 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
461 mlt_producer producer = mlt_playlist_producer( playlist );
462 mlt_producer clip = mlt_playlist_current( playlist );
463
464 mlt_playlist_clip_info info;
465 int clip_index = mlt_playlist_current_clip( playlist );
466 mlt_playlist_get_clip_info( playlist, &info, clip_index );
467
468 if ( info.resource != NULL && strcmp( info.resource, "" ) )
469 {
470 strncpy( status->clip, strip_root( unit, info.resource ), sizeof( status->clip ) );
471 status->speed = (int)( mlt_producer_get_speed( producer ) * 1000.0 );
472 status->fps = mlt_producer_get_fps( producer );
473 status->in = info.frame_in;
474 status->out = info.frame_out;
475 status->position = mlt_producer_position( clip );
476 status->length = mlt_producer_get_length( clip );
477 strncpy( status->tail_clip, strip_root( unit, info.resource ), sizeof( status->tail_clip ) );
478 status->tail_in = info.frame_in;
479 status->tail_out = info.frame_out;
480 status->tail_position = mlt_producer_position( clip );
481 status->tail_length = mlt_producer_get_length( clip );
482 status->clip_index = mlt_playlist_current_clip( playlist );
483 status->seek_flag = 1;
484 }
485
486 status->generation = mlt_properties_get_int( properties, "generation" );
487
488 if ( miracle_unit_has_terminated( unit ) )
489 status->status = unit_stopped;
490 else if ( !strcmp( status->clip, "" ) )
491 status->status = unit_not_loaded;
492 else if ( status->speed == 0 )
493 status->status = unit_paused;
494 else
495 status->status = unit_playing;
496 }
497 else
498 {
499 status->status = unit_undefined;
500 }
501
502 status->unit = mlt_properties_get_int( unit->properties, "unit" );
503
504 return error;
505 }
506
507 /** Change position in the playlist.
508 */
509
510 void miracle_unit_change_position( miracle_unit unit, int clip, int64_t position )
511 {
512 mlt_properties properties = unit->properties;
513 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
514 mlt_producer producer = mlt_playlist_producer( playlist );
515 mlt_playlist_clip_info info;
516
517 if ( clip < 0 )
518 {
519 clip = 0;
520 position = 0;
521 }
522 else if ( clip >= mlt_playlist_count( playlist ) )
523 {
524 clip = mlt_playlist_count( playlist ) - 1;
525 position = LONG_MAX;
526 }
527
528 if ( mlt_playlist_get_clip_info( playlist, &info, clip ) == 0 )
529 {
530 int64_t frame_start = info.start;
531 int64_t frame_offset = position;
532
533 if ( frame_offset < 0 )
534 frame_offset = info.frame_out;
535 if ( frame_offset < info.frame_in )
536 frame_offset = info.frame_in;
537 if ( frame_offset >= info.frame_out )
538 frame_offset = info.frame_out;
539
540 mlt_producer_seek( producer, frame_start + frame_offset - info.frame_in );
541 }
542
543 miracle_unit_status_communicate( unit );
544 }
545
546 /** Get the index of the current clip.
547 */
548
549 int miracle_unit_get_current_clip( miracle_unit unit )
550 {
551 mlt_properties properties = unit->properties;
552 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
553 int clip_index = mlt_playlist_current_clip( playlist );
554 return clip_index;
555 }
556
557 /** Set a clip's in point
558 */
559
560 int miracle_unit_set_clip_in( miracle_unit unit, int index, int64_t position )
561 {
562 mlt_properties properties = unit->properties;
563 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
564 mlt_playlist_clip_info info;
565 int error = mlt_playlist_get_clip_info( playlist, &info, index );
566
567 if ( error == 0 )
568 {
569 miracle_unit_play( unit, 0 );
570 error = mlt_playlist_resize_clip( playlist, index, position, info.frame_out );
571 update_generation( unit );
572 miracle_unit_change_position( unit, index, 0 );
573 }
574
575 return error;
576 }
577
578 /** Set a clip's out point.
579 */
580
581 int miracle_unit_set_clip_out( miracle_unit unit, int index, int64_t position )
582 {
583 mlt_properties properties = unit->properties;
584 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
585 mlt_playlist_clip_info info;
586 int error = mlt_playlist_get_clip_info( playlist, &info, index );
587
588 if ( error == 0 )
589 {
590 miracle_unit_play( unit, 0 );
591 error = mlt_playlist_resize_clip( playlist, index, info.frame_in, position );
592 update_generation( unit );
593 miracle_unit_status_communicate( unit );
594 miracle_unit_change_position( unit, index, -1 );
595 }
596
597 return error;
598 }
599
600 /** Step by specified position.
601 */
602
603 void miracle_unit_step( miracle_unit unit, int64_t offset )
604 {
605 mlt_properties properties = unit->properties;
606 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
607 mlt_producer producer = mlt_playlist_producer( playlist );
608 mlt_position position = mlt_producer_frame( producer );
609 mlt_producer_seek( producer, position + offset );
610 }
611
612 /** Set the unit's clip mode regarding in and out points.
613 */
614
615 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
616 //{
617 //dv_player player = miracle_unit_get_dv_player( unit );
618 //if ( player != NULL )
619 //dv_player_set_clip_mode( player, mode );
620 //miracle_unit_status_communicate( unit );
621 //}
622
623 /** Get the unit's clip mode regarding in and out points.
624 */
625
626 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
627 //{
628 //dv_player player = miracle_unit_get_dv_player( unit );
629 //return dv_player_get_clip_mode( player );
630 //}
631
632 /** Set the unit's clip mode regarding eof handling.
633 */
634
635 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
636 //{
637 //dv_player player = miracle_unit_get_dv_player( unit );
638 //dv_player_set_eof_action( player, action );
639 //miracle_unit_status_communicate( unit );
640 //}
641
642 /** Get the unit's clip mode regarding eof handling.
643 */
644
645 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
646 //{
647 //dv_player player = miracle_unit_get_dv_player( unit );
648 //return dv_player_get_eof_action( player );
649 //}
650
651 int miracle_unit_set( miracle_unit unit, char *name_value )
652 {
653 mlt_properties properties = NULL;
654
655 if ( strncmp( name_value, "consumer.", 9 ) )
656 {
657 mlt_playlist playlist = mlt_properties_get_data( unit->properties, "playlist", NULL );
658 properties = mlt_playlist_properties( playlist );
659 }
660 else
661 {
662 mlt_consumer consumer = mlt_properties_get_data( unit->properties, "consumer", NULL );
663 properties = mlt_consumer_properties( consumer );
664 name_value += 9;
665 }
666
667 return mlt_properties_parse( properties, name_value );
668 }
669
670 char *miracle_unit_get( miracle_unit unit, char *name )
671 {
672 mlt_playlist playlist = mlt_properties_get_data( unit->properties, "playlist", NULL );
673 mlt_properties properties = mlt_playlist_properties( playlist );
674 return mlt_properties_get( properties, name );
675 }
676
677 /** Release the unit
678
679 \todo error handling
680 \param unit A miracle_unit handle.
681 */
682
683 void miracle_unit_close( miracle_unit unit )
684 {
685 if ( unit != NULL )
686 {
687 miracle_log( LOG_DEBUG, "closing unit..." );
688 if ( unit->old_producers != NULL )
689 mlt_properties_close( unit->old_producers );
690 mlt_properties_close( unit->properties );
691 mlt_properties_close( unit->producers );
692 free( unit );
693 miracle_log( LOG_DEBUG, "... unit closed." );
694 }
695 }
696