First draft of event handling
[melted] / src / framework / mlt_playlist.c
1 /*
2 * mlt_playlist.c -- playlist service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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 #include "config.h"
22
23 #include "mlt_playlist.h"
24 #include "mlt_frame.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 /** Virtual playlist entry.
31 */
32
33 typedef struct
34 {
35 mlt_producer producer;
36 mlt_position frame_in;
37 mlt_position frame_out;
38 mlt_position frame_count;
39 mlt_event event;
40 }
41 playlist_entry;
42
43 /** Private definition.
44 */
45
46 struct mlt_playlist_s
47 {
48 struct mlt_producer_s parent;
49 struct mlt_producer_s blank;
50
51 int size;
52 int count;
53 playlist_entry **list;
54 };
55
56 /** Forward declarations
57 */
58
59 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
60
61 /** Constructor.
62 */
63
64 mlt_playlist mlt_playlist_init( )
65 {
66 mlt_playlist this = calloc( sizeof( struct mlt_playlist_s ), 1 );
67 if ( this != NULL )
68 {
69 mlt_producer producer = &this->parent;
70
71 // Construct the producer
72 mlt_producer_init( producer, this );
73
74 // Override the producer get_frame
75 producer->get_frame = producer_get_frame;
76
77 // Define the destructor
78 producer->close = ( mlt_destructor )mlt_playlist_close;
79 producer->close_object = this;
80
81 // Initialise blank
82 mlt_producer_init( &this->blank, NULL );
83 mlt_properties_set( mlt_producer_properties( &this->blank ), "mlt_service", "blank" );
84 mlt_properties_set( mlt_producer_properties( &this->blank ), "resource", "blank" );
85
86 // Indicate that this producer is a playlist
87 mlt_properties_set_data( mlt_playlist_properties( this ), "playlist", this, 0, NULL, NULL );
88
89 // Specify the eof condition
90 mlt_properties_set( mlt_playlist_properties( this ), "eof", "pause" );
91 mlt_properties_set( mlt_playlist_properties( this ), "resource", "<playlist>" );
92 mlt_properties_set( mlt_playlist_properties( this ), "mlt_type", "mlt_producer" );
93 mlt_properties_set_position( mlt_playlist_properties( this ), "in", 0 );
94 mlt_properties_set_position( mlt_playlist_properties( this ), "out", 0 );
95 mlt_properties_set_position( mlt_playlist_properties( this ), "length", 0 );
96
97 this->size = 10;
98 this->list = malloc( this->size * sizeof( playlist_entry * ) );
99 }
100
101 return this;
102 }
103
104 /** Get the producer associated to this playlist.
105 */
106
107 mlt_producer mlt_playlist_producer( mlt_playlist this )
108 {
109 return this != NULL ? &this->parent : NULL;
110 }
111
112 /** Get the service associated to this playlist.
113 */
114
115 mlt_service mlt_playlist_service( mlt_playlist this )
116 {
117 return mlt_producer_service( &this->parent );
118 }
119
120 /** Get the propertues associated to this playlist.
121 */
122
123 mlt_properties mlt_playlist_properties( mlt_playlist this )
124 {
125 return mlt_producer_properties( &this->parent );
126 }
127
128 /** Refresh the playlist after a clip has been changed.
129 */
130
131 static int mlt_playlist_virtual_refresh( mlt_playlist this )
132 {
133 // Obtain the properties
134 mlt_properties properties = mlt_playlist_properties( this );
135
136 // Get the fps of the first producer
137 double fps = mlt_properties_get_double( properties, "first_fps" );
138 int i = 0;
139 mlt_position frame_count = 0;
140
141 for ( i = 0; i < this->count; i ++ )
142 {
143 // Get the producer
144 mlt_producer producer = this->list[ i ]->producer;
145
146 // If fps is 0
147 if ( fps == 0 )
148 {
149 // Inherit it from the producer
150 fps = mlt_producer_get_fps( producer );
151 }
152 else if ( fps != mlt_properties_get_double( mlt_producer_properties( producer ), "fps" ) )
153 {
154 // Generate a warning for now - the following attempt to fix may fail
155 fprintf( stderr, "Warning: fps mismatch on playlist producer %d\n", this->count );
156
157 // It should be safe to impose fps on an image producer, but not necessarily safe for video
158 mlt_properties_set_double( mlt_producer_properties( producer ), "fps", fps );
159 }
160
161 // Update the frame_count for this clip
162 frame_count += this->list[ i ]->frame_count;
163 }
164
165 // Refresh all properties
166 mlt_properties_set_double( properties, "first_fps", fps );
167 mlt_properties_set_double( properties, "fps", fps == 0 ? 25 : fps );
168 mlt_events_block( properties, properties );
169 mlt_properties_set_position( properties, "length", frame_count );
170 mlt_events_unblock( properties, properties );
171 mlt_properties_set_position( properties, "out", frame_count - 1 );
172
173 return 0;
174 }
175
176 /** Listener for producers on the playlist.
177 */
178
179 static void mlt_playlist_listener( mlt_producer producer, mlt_playlist this )
180 {
181 mlt_playlist_virtual_refresh( this );
182 }
183
184 /** Append to the virtual playlist.
185 */
186
187 static int mlt_playlist_virtual_append( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
188 {
189 mlt_properties properties = mlt_producer_properties( producer );
190
191 // Check that we have room
192 if ( this->count >= this->size )
193 {
194 int i;
195 this->list = realloc( this->list, ( this->size + 10 ) * sizeof( playlist_entry * ) );
196 for ( i = this->size; i < this->size + 10; i ++ ) this->list[ i ] = NULL;
197 this->size += 10;
198 }
199
200 this->list[ this->count ] = calloc( sizeof( playlist_entry ), 1 );
201 this->list[ this->count ]->producer = producer;
202 this->list[ this->count ]->frame_in = in;
203 this->list[ this->count ]->frame_out = out;
204 this->list[ this->count ]->frame_count = out - in + 1;
205 this->list[ this->count ]->event = mlt_events_listen( properties, this, "producer-changed", ( mlt_listener )mlt_playlist_listener );
206
207 mlt_properties_set( properties, "eof", "pause" );
208
209 mlt_producer_set_speed( producer, 0 );
210
211 this->count ++;
212
213 mlt_properties_inc_ref( properties );
214
215 return mlt_playlist_virtual_refresh( this );
216 }
217
218 /** Seek in the virtual playlist.
219 */
220
221 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this )
222 {
223 // Default producer to blank
224 mlt_producer producer = NULL;
225
226 // Map playlist position to real producer in virtual playlist
227 mlt_position position = mlt_producer_frame( &this->parent );
228
229 mlt_position original = position;
230
231 // Total number of frames
232 int64_t total = 0;
233
234 // Get the properties
235 mlt_properties properties = mlt_playlist_properties( this );
236
237 // Get the eof handling
238 char *eof = mlt_properties_get( properties, "eof" );
239
240 // Index for the main loop
241 int i = 0;
242
243 // Loop for each producer until found
244 for ( i = 0; i < this->count; i ++ )
245 {
246 // Increment the total
247 total += this->list[ i ]->frame_count;
248
249 // Check if the position indicates that we have found the clip
250 if ( position < this->list[ i ]->frame_count )
251 {
252 // Found it, now break
253 producer = this->list[ i ]->producer;
254 break;
255 }
256 else
257 {
258 // Decrement position by length of this entry
259 position -= this->list[ i ]->frame_count;
260 }
261 }
262
263 // Seek in real producer to relative position
264 if ( producer != NULL )
265 {
266 position += this->list[ i ]->frame_in;
267 mlt_producer_seek( producer, position );
268 }
269 else if ( !strcmp( eof, "pause" ) && total > 0 )
270 {
271 playlist_entry *entry = this->list[ this->count - 1 ];
272 mlt_producer this_producer = mlt_playlist_producer( this );
273 mlt_producer_seek( this_producer, original - 1 );
274 producer = entry->producer;
275 mlt_producer_seek( producer, entry->frame_out );
276 mlt_producer_set_speed( this_producer, 0 );
277 mlt_producer_set_speed( producer, 0 );
278 }
279 else if ( !strcmp( eof, "loop" ) && total > 0 )
280 {
281 playlist_entry *entry = this->list[ 0 ];
282 mlt_producer this_producer = mlt_playlist_producer( this );
283 mlt_producer_seek( this_producer, 0 );
284 producer = entry->producer;
285 mlt_producer_seek( producer, entry->frame_in );
286 }
287 else
288 {
289 producer = &this->blank;
290 }
291
292 return mlt_producer_service( producer );
293 }
294
295 /** Invoked when a producer indicates that it has prematurely reached its end.
296 */
297
298 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
299 {
300 // Default producer to blank
301 mlt_producer producer = &this->blank;
302
303 // Map playlist position to real producer in virtual playlist
304 mlt_position position = mlt_producer_frame( &this->parent );
305
306 // Loop through the virtual playlist
307 int i = 0;
308
309 for ( i = 0; i < this->count; i ++ )
310 {
311 if ( position < this->list[ i ]->frame_count )
312 {
313 // Found it, now break
314 producer = this->list[ i ]->producer;
315 break;
316 }
317 else
318 {
319 // Decrement position by length of this entry
320 position -= this->list[ i ]->frame_count;
321 }
322 }
323
324 // Seek in real producer to relative position
325 if ( i < this->count && this->list[ i ]->frame_out != position )
326 {
327 // Update the frame_count for the changed clip (hmmm)
328 this->list[ i ]->frame_out = position;
329 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
330
331 // Refresh the playlist
332 mlt_playlist_virtual_refresh( this );
333 }
334
335 return producer;
336 }
337
338 /** Obtain the current clips index.
339 */
340
341 int mlt_playlist_current_clip( mlt_playlist this )
342 {
343 // Map playlist position to real producer in virtual playlist
344 mlt_position position = mlt_producer_frame( &this->parent );
345
346 // Loop through the virtual playlist
347 int i = 0;
348
349 for ( i = 0; i < this->count; i ++ )
350 {
351 if ( position < this->list[ i ]->frame_count )
352 {
353 // Found it, now break
354 break;
355 }
356 else
357 {
358 // Decrement position by length of this entry
359 position -= this->list[ i ]->frame_count;
360 }
361 }
362
363 return i;
364 }
365
366 /** Obtain the current clips producer.
367 */
368
369 mlt_producer mlt_playlist_current( mlt_playlist this )
370 {
371 int i = mlt_playlist_current_clip( this );
372 if ( i < this->count )
373 return this->list[ i ]->producer;
374 else
375 return &this->blank;
376 }
377
378 /** Get the position which corresponds to the start of the next clip.
379 */
380
381 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
382 {
383 mlt_position position = 0;
384 int absolute_clip = index;
385 int i = 0;
386
387 // Determine the absolute clip
388 switch ( whence )
389 {
390 case mlt_whence_relative_start:
391 absolute_clip = index;
392 break;
393
394 case mlt_whence_relative_current:
395 absolute_clip = mlt_playlist_current_clip( this ) + index;
396 break;
397
398 case mlt_whence_relative_end:
399 absolute_clip = this->count - index;
400 break;
401 }
402
403 // Check that we're in a valid range
404 if ( absolute_clip < 0 )
405 absolute_clip = 0;
406 else if ( absolute_clip > this->count )
407 absolute_clip = this->count;
408
409 // Now determine the position
410 for ( i = 0; i < absolute_clip; i ++ )
411 position += this->list[ i ]->frame_count;
412
413 return position;
414 }
415
416 /** Get all the info about the clip specified.
417 */
418
419 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
420 {
421 int error = index < 0 || index >= this->count;
422 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
423 if ( !error )
424 {
425 mlt_producer producer = this->list[ index ]->producer;
426 mlt_properties properties = mlt_producer_properties( producer );
427 info->clip = index;
428 info->producer = producer;
429 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
430 info->resource = mlt_properties_get( properties, "resource" );
431 info->frame_in = this->list[ index ]->frame_in;
432 info->frame_out = this->list[ index ]->frame_out;
433 info->frame_count = this->list[ index ]->frame_count;
434 info->length = mlt_producer_get_length( producer );
435 info->fps = mlt_producer_get_fps( producer );
436 info->event = this->list[ index ]->event;
437 }
438
439 // Determine the consuming filter service
440 if ( info->producer != NULL )
441 {
442 info->service = mlt_producer_service( info->producer );
443 while ( mlt_service_consumer( info->service ) != NULL )
444 info->service = mlt_service_consumer( info->service );
445 }
446
447 return error;
448 }
449
450 /** Get number of clips in the playlist.
451 */
452
453 int mlt_playlist_count( mlt_playlist this )
454 {
455 return this->count;
456 }
457
458 /** Clear the playlist.
459 */
460
461 int mlt_playlist_clear( mlt_playlist this )
462 {
463 int i;
464 for ( i = 0; i < this->count; i ++ )
465 {
466 mlt_event_close( this->list[ i ]->event );
467 mlt_producer_close( this->list[ i ]->producer );
468 }
469 this->count = 0;
470 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
471 return mlt_playlist_virtual_refresh( this );
472 }
473
474 /** Append a producer to the playlist.
475 */
476
477 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
478 {
479 // Append to virtual list
480 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
481 }
482
483 /** Append a producer to the playlist with in/out points.
484 */
485
486 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
487 {
488 // Append to virtual list
489 if ( in != -1 && out != -1 )
490 return mlt_playlist_virtual_append( this, producer, in, out );
491 else
492 return mlt_playlist_append( this, producer );
493 }
494
495 /** Append a blank to the playlist of a given length.
496 */
497
498 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
499 {
500 // Append to the virtual list
501 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
502 }
503
504 /** Insert a producer into the playlist.
505 */
506
507 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
508 {
509 // Append to end
510 mlt_events_block( mlt_playlist_properties( this ), this );
511 mlt_playlist_append_io( this, producer, in, out );
512
513 // Move to the position specified
514 mlt_playlist_move( this, this->count - 1, where );
515 mlt_events_unblock( mlt_playlist_properties( this ), this );
516
517 return mlt_playlist_virtual_refresh( this );
518 }
519
520 /** Remove an entry in the playlist.
521 */
522
523 int mlt_playlist_remove( mlt_playlist this, int where )
524 {
525 if ( this->count > 0 )
526 {
527 // We need to know the current clip and the position within the playlist
528 int current = mlt_playlist_current_clip( this );
529 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
530
531 // We need all the details about the clip we're removing
532 mlt_playlist_clip_info where_info;
533
534 // Loop variable
535 int i = 0;
536
537 // Make sure the clip to be removed is valid and correct if necessary
538 if ( where < 0 )
539 where = 0;
540 if ( where >= this->count )
541 where = this->count - 1;
542
543 // Get the clip info of the clip to be removed
544 mlt_playlist_get_clip_info( this, &where_info, where );
545
546 // Close the producer associated to the clip info
547 mlt_event_close( where_info.event );
548 mlt_producer_close( where_info.producer );
549
550 // Reorganise the list
551 for ( i = where + 1; i < this->count; i ++ )
552 this->list[ i - 1 ] = this->list[ i ];
553 this->count --;
554
555 // Correct position
556 if ( where == current )
557 mlt_producer_seek( mlt_playlist_producer( this ), where_info.start );
558 else if ( where < current && this->count > 0 )
559 mlt_producer_seek( mlt_playlist_producer( this ), position - where_info.frame_count );
560 else if ( this->count == 0 )
561 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
562
563 // Refresh the playlist
564 mlt_playlist_virtual_refresh( this );
565 }
566
567 return 0;
568 }
569
570 /** Move an entry in the playlist.
571 */
572
573 int mlt_playlist_move( mlt_playlist this, int src, int dest )
574 {
575 int i;
576
577 /* We need to ensure that the requested indexes are valid and correct it as necessary */
578 if ( src < 0 )
579 src = 0;
580 if ( src >= this->count )
581 src = this->count - 1;
582
583 if ( dest < 0 )
584 dest = 0;
585 if ( dest >= this->count )
586 dest = this->count - 1;
587
588 if ( src != dest && this->count > 1 )
589 {
590 int current = mlt_playlist_current_clip( this );
591 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
592 playlist_entry *src_entry = NULL;
593
594 // We need all the details about the current clip
595 mlt_playlist_clip_info current_info;
596
597 mlt_playlist_get_clip_info( this, &current_info, current );
598 position -= current_info.start;
599
600 if ( current == src )
601 current = dest;
602 else if ( current > src && current < dest )
603 current ++;
604 else if ( current == dest )
605 current = src;
606
607 src_entry = this->list[ src ];
608 if ( src > dest )
609 {
610 for ( i = src; i > dest; i -- )
611 this->list[ i ] = this->list[ i - 1 ];
612 }
613 else
614 {
615 for ( i = src; i < dest; i ++ )
616 this->list[ i ] = this->list[ i + 1 ];
617 }
618 this->list[ dest ] = src_entry;
619
620 mlt_playlist_get_clip_info( this, &current_info, current );
621 mlt_producer_seek( mlt_playlist_producer( this ), current_info.start + position );
622 mlt_playlist_virtual_refresh( this );
623 }
624
625 return 0;
626 }
627
628 /** Resize the current clip.
629 */
630
631 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
632 {
633 int error = clip < 0 || clip >= this->count;
634 if ( error == 0 )
635 {
636 playlist_entry *entry = this->list[ clip ];
637 mlt_producer producer = entry->producer;
638
639 if ( in <= -1 )
640 in = 0;
641 if ( out <= -1 || out >= mlt_producer_get_playtime( producer ) )
642 out = mlt_producer_get_playtime( producer ) - 1;
643
644 if ( out < in )
645 {
646 mlt_position t = in;
647 in = out;
648 out = t;
649 }
650
651 entry->frame_in = in;
652 entry->frame_out = out;
653 entry->frame_count = out - in + 1;
654 mlt_playlist_virtual_refresh( this );
655 }
656 return error;
657 }
658
659 /** Split a clip on the playlist at the given position.
660 */
661
662 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
663 {
664 int error = clip < 0 || clip >= this->count;
665 if ( error == 0 )
666 {
667 playlist_entry *entry = this->list[ clip ];
668 if ( position > 0 && position < entry->frame_count )
669 {
670 int in = entry->frame_in;
671 int out = entry->frame_out;
672 mlt_events_block( mlt_playlist_properties( this ), this );
673 mlt_playlist_resize_clip( this, clip, in, in + position );
674 mlt_events_unblock( mlt_playlist_properties( this ), this );
675 mlt_playlist_insert( this, entry->producer, clip + 1, in + position + 1, out );
676 }
677 else
678 {
679 error = 1;
680 }
681 }
682 return error;
683 }
684
685 /** Join 1 or more consecutive clips.
686 */
687
688 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
689 {
690 int error = clip < 0 || ( clip + 1 ) >= this->count;
691 if ( error == 0 )
692 {
693 int i = clip;
694 mlt_playlist new_clip = mlt_playlist_init( );
695 mlt_events_block( mlt_playlist_properties( this ), this );
696 if ( clip + count >= this->count )
697 count = this->count - clip;
698 for ( i = 0; i <= count; i ++ )
699 {
700 playlist_entry *entry = this->list[ clip ];
701 mlt_playlist_append_io( new_clip, entry->producer, entry->frame_in, entry->frame_out );
702 mlt_playlist_remove( this, clip );
703 }
704 mlt_events_unblock( mlt_playlist_properties( this ), this );
705 mlt_playlist_insert( this, mlt_playlist_producer( new_clip ), clip, 0, -1 );
706 mlt_playlist_close( new_clip );
707 }
708 return error;
709 }
710
711 /** Get the current frame.
712 */
713
714 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
715 {
716 // Get this mlt_playlist
717 mlt_playlist this = producer->child;
718
719 // Get the real producer
720 mlt_service real = mlt_playlist_virtual_seek( this );
721
722 // Get the frame
723 mlt_service_get_frame( real, frame, index );
724
725 // Check if we're at the end of the clip
726 mlt_properties properties = mlt_frame_properties( *frame );
727 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
728 mlt_playlist_virtual_set_out( this );
729
730 // Check for notifier and call with appropriate argument
731 mlt_properties playlist_properties = mlt_producer_properties( producer );
732 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
733 if ( notifier != NULL )
734 {
735 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
736 notifier( argument );
737 }
738
739 // Update position on the frame we're creating
740 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
741
742 // Position ourselves on the next frame
743 mlt_producer_prepare_next( producer );
744
745 return 0;
746 }
747
748 /** Close the playlist.
749 */
750
751 void mlt_playlist_close( mlt_playlist this )
752 {
753 if ( this != NULL && mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
754 {
755 int i = 0;
756 this->parent.close = NULL;
757 for ( i = 0; i < this->count; i ++ )
758 {
759 mlt_event_close( this->list[ i ]->event );
760 mlt_producer_close( this->list[ i ]->producer );
761 free( this->list[ i ] );
762 }
763 mlt_producer_close( &this->blank );
764 mlt_producer_close( &this->parent );
765 free( this->list );
766 free( this );
767 }
768 }