event fix for playlist and consumer-stopped event
[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 mlt_event_inc_ref( this->list[ this->count ]->event );
207
208 mlt_properties_set( properties, "eof", "pause" );
209
210 mlt_producer_set_speed( producer, 0 );
211
212 this->count ++;
213
214 mlt_properties_inc_ref( properties );
215
216 return mlt_playlist_virtual_refresh( this );
217 }
218
219 /** Seek in the virtual playlist.
220 */
221
222 static mlt_service mlt_playlist_virtual_seek( mlt_playlist this )
223 {
224 // Default producer to blank
225 mlt_producer producer = NULL;
226
227 // Map playlist position to real producer in virtual playlist
228 mlt_position position = mlt_producer_frame( &this->parent );
229
230 mlt_position original = position;
231
232 // Total number of frames
233 int64_t total = 0;
234
235 // Get the properties
236 mlt_properties properties = mlt_playlist_properties( this );
237
238 // Get the eof handling
239 char *eof = mlt_properties_get( properties, "eof" );
240
241 // Index for the main loop
242 int i = 0;
243
244 // Loop for each producer until found
245 for ( i = 0; i < this->count; i ++ )
246 {
247 // Increment the total
248 total += this->list[ i ]->frame_count;
249
250 // Check if the position indicates that we have found the clip
251 if ( position < this->list[ i ]->frame_count )
252 {
253 // Found it, now break
254 producer = this->list[ i ]->producer;
255 break;
256 }
257 else
258 {
259 // Decrement position by length of this entry
260 position -= this->list[ i ]->frame_count;
261 }
262 }
263
264 // Seek in real producer to relative position
265 if ( producer != NULL )
266 {
267 position += this->list[ i ]->frame_in;
268 mlt_producer_seek( producer, position );
269 }
270 else if ( !strcmp( eof, "pause" ) && total > 0 )
271 {
272 playlist_entry *entry = this->list[ this->count - 1 ];
273 mlt_producer this_producer = mlt_playlist_producer( this );
274 mlt_producer_seek( this_producer, original - 1 );
275 producer = entry->producer;
276 mlt_producer_seek( producer, entry->frame_out );
277 mlt_producer_set_speed( this_producer, 0 );
278 mlt_producer_set_speed( producer, 0 );
279 }
280 else if ( !strcmp( eof, "loop" ) && total > 0 )
281 {
282 playlist_entry *entry = this->list[ 0 ];
283 mlt_producer this_producer = mlt_playlist_producer( this );
284 mlt_producer_seek( this_producer, 0 );
285 producer = entry->producer;
286 mlt_producer_seek( producer, entry->frame_in );
287 }
288 else
289 {
290 producer = &this->blank;
291 }
292
293 return mlt_producer_service( producer );
294 }
295
296 /** Invoked when a producer indicates that it has prematurely reached its end.
297 */
298
299 static mlt_producer mlt_playlist_virtual_set_out( mlt_playlist this )
300 {
301 // Default producer to blank
302 mlt_producer producer = &this->blank;
303
304 // Map playlist position to real producer in virtual playlist
305 mlt_position position = mlt_producer_frame( &this->parent );
306
307 // Loop through the virtual playlist
308 int i = 0;
309
310 for ( i = 0; i < this->count; i ++ )
311 {
312 if ( position < this->list[ i ]->frame_count )
313 {
314 // Found it, now break
315 producer = this->list[ i ]->producer;
316 break;
317 }
318 else
319 {
320 // Decrement position by length of this entry
321 position -= this->list[ i ]->frame_count;
322 }
323 }
324
325 // Seek in real producer to relative position
326 if ( i < this->count && this->list[ i ]->frame_out != position )
327 {
328 // Update the frame_count for the changed clip (hmmm)
329 this->list[ i ]->frame_out = position;
330 this->list[ i ]->frame_count = this->list[ i ]->frame_out - this->list[ i ]->frame_in + 1;
331
332 // Refresh the playlist
333 mlt_playlist_virtual_refresh( this );
334 }
335
336 return producer;
337 }
338
339 /** Obtain the current clips index.
340 */
341
342 int mlt_playlist_current_clip( mlt_playlist this )
343 {
344 // Map playlist position to real producer in virtual playlist
345 mlt_position position = mlt_producer_frame( &this->parent );
346
347 // Loop through the virtual playlist
348 int i = 0;
349
350 for ( i = 0; i < this->count; i ++ )
351 {
352 if ( position < this->list[ i ]->frame_count )
353 {
354 // Found it, now break
355 break;
356 }
357 else
358 {
359 // Decrement position by length of this entry
360 position -= this->list[ i ]->frame_count;
361 }
362 }
363
364 return i;
365 }
366
367 /** Obtain the current clips producer.
368 */
369
370 mlt_producer mlt_playlist_current( mlt_playlist this )
371 {
372 int i = mlt_playlist_current_clip( this );
373 if ( i < this->count )
374 return this->list[ i ]->producer;
375 else
376 return &this->blank;
377 }
378
379 /** Get the position which corresponds to the start of the next clip.
380 */
381
382 mlt_position mlt_playlist_clip( mlt_playlist this, mlt_whence whence, int index )
383 {
384 mlt_position position = 0;
385 int absolute_clip = index;
386 int i = 0;
387
388 // Determine the absolute clip
389 switch ( whence )
390 {
391 case mlt_whence_relative_start:
392 absolute_clip = index;
393 break;
394
395 case mlt_whence_relative_current:
396 absolute_clip = mlt_playlist_current_clip( this ) + index;
397 break;
398
399 case mlt_whence_relative_end:
400 absolute_clip = this->count - index;
401 break;
402 }
403
404 // Check that we're in a valid range
405 if ( absolute_clip < 0 )
406 absolute_clip = 0;
407 else if ( absolute_clip > this->count )
408 absolute_clip = this->count;
409
410 // Now determine the position
411 for ( i = 0; i < absolute_clip; i ++ )
412 position += this->list[ i ]->frame_count;
413
414 return position;
415 }
416
417 /** Get all the info about the clip specified.
418 */
419
420 int mlt_playlist_get_clip_info( mlt_playlist this, mlt_playlist_clip_info *info, int index )
421 {
422 int error = index < 0 || index >= this->count;
423 memset( info, 0, sizeof( mlt_playlist_clip_info ) );
424 if ( !error )
425 {
426 mlt_producer producer = this->list[ index ]->producer;
427 mlt_properties properties = mlt_producer_properties( producer );
428 info->clip = index;
429 info->producer = producer;
430 info->start = mlt_playlist_clip( this, mlt_whence_relative_start, index );
431 info->resource = mlt_properties_get( properties, "resource" );
432 info->frame_in = this->list[ index ]->frame_in;
433 info->frame_out = this->list[ index ]->frame_out;
434 info->frame_count = this->list[ index ]->frame_count;
435 info->length = mlt_producer_get_length( producer );
436 info->fps = mlt_producer_get_fps( producer );
437 info->event = this->list[ index ]->event;
438 }
439
440 // Determine the consuming filter service
441 if ( info->producer != NULL )
442 {
443 info->service = mlt_producer_service( info->producer );
444 while ( mlt_service_consumer( info->service ) != NULL )
445 info->service = mlt_service_consumer( info->service );
446 }
447
448 return error;
449 }
450
451 /** Get number of clips in the playlist.
452 */
453
454 int mlt_playlist_count( mlt_playlist this )
455 {
456 return this->count;
457 }
458
459 /** Clear the playlist.
460 */
461
462 int mlt_playlist_clear( mlt_playlist this )
463 {
464 int i;
465 for ( i = 0; i < this->count; i ++ )
466 {
467 mlt_event_close( this->list[ i ]->event );
468 mlt_producer_close( this->list[ i ]->producer );
469 }
470 this->count = 0;
471 mlt_properties_set_double( mlt_playlist_properties( this ), "first_fps", 0 );
472 return mlt_playlist_virtual_refresh( this );
473 }
474
475 /** Append a producer to the playlist.
476 */
477
478 int mlt_playlist_append( mlt_playlist this, mlt_producer producer )
479 {
480 // Append to virtual list
481 return mlt_playlist_virtual_append( this, producer, 0, mlt_producer_get_playtime( producer ) - 1 );
482 }
483
484 /** Append a producer to the playlist with in/out points.
485 */
486
487 int mlt_playlist_append_io( mlt_playlist this, mlt_producer producer, mlt_position in, mlt_position out )
488 {
489 // Append to virtual list
490 if ( in != -1 && out != -1 )
491 return mlt_playlist_virtual_append( this, producer, in, out );
492 else
493 return mlt_playlist_append( this, producer );
494 }
495
496 /** Append a blank to the playlist of a given length.
497 */
498
499 int mlt_playlist_blank( mlt_playlist this, mlt_position length )
500 {
501 // Append to the virtual list
502 return mlt_playlist_virtual_append( this, &this->blank, 0, length );
503 }
504
505 /** Insert a producer into the playlist.
506 */
507
508 int mlt_playlist_insert( mlt_playlist this, mlt_producer producer, int where, mlt_position in, mlt_position out )
509 {
510 // Append to end
511 mlt_events_block( mlt_playlist_properties( this ), this );
512 mlt_playlist_append_io( this, producer, in, out );
513
514 // Move to the position specified
515 mlt_playlist_move( this, this->count - 1, where );
516 mlt_events_unblock( mlt_playlist_properties( this ), this );
517
518 return mlt_playlist_virtual_refresh( this );
519 }
520
521 /** Remove an entry in the playlist.
522 */
523
524 int mlt_playlist_remove( mlt_playlist this, int where )
525 {
526 if ( this->count > 0 )
527 {
528 // We need to know the current clip and the position within the playlist
529 int current = mlt_playlist_current_clip( this );
530 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
531
532 // We need all the details about the clip we're removing
533 mlt_playlist_clip_info where_info;
534
535 // Loop variable
536 int i = 0;
537
538 // Make sure the clip to be removed is valid and correct if necessary
539 if ( where < 0 )
540 where = 0;
541 if ( where >= this->count )
542 where = this->count - 1;
543
544 // Get the clip info of the clip to be removed
545 mlt_playlist_get_clip_info( this, &where_info, where );
546
547 // Close the producer associated to the clip info
548 mlt_event_close( where_info.event );
549 mlt_producer_close( where_info.producer );
550
551 // Reorganise the list
552 for ( i = where + 1; i < this->count; i ++ )
553 this->list[ i - 1 ] = this->list[ i ];
554 this->count --;
555
556 // Correct position
557 if ( where == current )
558 mlt_producer_seek( mlt_playlist_producer( this ), where_info.start );
559 else if ( where < current && this->count > 0 )
560 mlt_producer_seek( mlt_playlist_producer( this ), position - where_info.frame_count );
561 else if ( this->count == 0 )
562 mlt_producer_seek( mlt_playlist_producer( this ), 0 );
563
564 // Refresh the playlist
565 mlt_playlist_virtual_refresh( this );
566 }
567
568 return 0;
569 }
570
571 /** Move an entry in the playlist.
572 */
573
574 int mlt_playlist_move( mlt_playlist this, int src, int dest )
575 {
576 int i;
577
578 /* We need to ensure that the requested indexes are valid and correct it as necessary */
579 if ( src < 0 )
580 src = 0;
581 if ( src >= this->count )
582 src = this->count - 1;
583
584 if ( dest < 0 )
585 dest = 0;
586 if ( dest >= this->count )
587 dest = this->count - 1;
588
589 if ( src != dest && this->count > 1 )
590 {
591 int current = mlt_playlist_current_clip( this );
592 mlt_position position = mlt_producer_position( mlt_playlist_producer( this ) );
593 playlist_entry *src_entry = NULL;
594
595 // We need all the details about the current clip
596 mlt_playlist_clip_info current_info;
597
598 mlt_playlist_get_clip_info( this, &current_info, current );
599 position -= current_info.start;
600
601 if ( current == src )
602 current = dest;
603 else if ( current > src && current < dest )
604 current ++;
605 else if ( current == dest )
606 current = src;
607
608 src_entry = this->list[ src ];
609 if ( src > dest )
610 {
611 for ( i = src; i > dest; i -- )
612 this->list[ i ] = this->list[ i - 1 ];
613 }
614 else
615 {
616 for ( i = src; i < dest; i ++ )
617 this->list[ i ] = this->list[ i + 1 ];
618 }
619 this->list[ dest ] = src_entry;
620
621 mlt_playlist_get_clip_info( this, &current_info, current );
622 mlt_producer_seek( mlt_playlist_producer( this ), current_info.start + position );
623 mlt_playlist_virtual_refresh( this );
624 }
625
626 return 0;
627 }
628
629 /** Resize the current clip.
630 */
631
632 int mlt_playlist_resize_clip( mlt_playlist this, int clip, mlt_position in, mlt_position out )
633 {
634 int error = clip < 0 || clip >= this->count;
635 if ( error == 0 )
636 {
637 playlist_entry *entry = this->list[ clip ];
638 mlt_producer producer = entry->producer;
639
640 if ( in <= -1 )
641 in = 0;
642 if ( out <= -1 || out >= mlt_producer_get_playtime( producer ) )
643 out = mlt_producer_get_playtime( producer ) - 1;
644
645 if ( out < in )
646 {
647 mlt_position t = in;
648 in = out;
649 out = t;
650 }
651
652 entry->frame_in = in;
653 entry->frame_out = out;
654 entry->frame_count = out - in + 1;
655 mlt_playlist_virtual_refresh( this );
656 }
657 return error;
658 }
659
660 /** Split a clip on the playlist at the given position.
661 */
662
663 int mlt_playlist_split( mlt_playlist this, int clip, mlt_position position )
664 {
665 int error = clip < 0 || clip >= this->count;
666 if ( error == 0 )
667 {
668 playlist_entry *entry = this->list[ clip ];
669 if ( position > 0 && position < entry->frame_count )
670 {
671 int in = entry->frame_in;
672 int out = entry->frame_out;
673 mlt_events_block( mlt_playlist_properties( this ), this );
674 mlt_playlist_resize_clip( this, clip, in, in + position );
675 mlt_events_unblock( mlt_playlist_properties( this ), this );
676 mlt_playlist_insert( this, entry->producer, clip + 1, in + position + 1, out );
677 }
678 else
679 {
680 error = 1;
681 }
682 }
683 return error;
684 }
685
686 /** Join 1 or more consecutive clips.
687 */
688
689 int mlt_playlist_join( mlt_playlist this, int clip, int count, int merge )
690 {
691 int error = clip < 0 || ( clip + 1 ) >= this->count;
692 if ( error == 0 )
693 {
694 int i = clip;
695 mlt_playlist new_clip = mlt_playlist_init( );
696 mlt_events_block( mlt_playlist_properties( this ), this );
697 if ( clip + count >= this->count )
698 count = this->count - clip;
699 for ( i = 0; i <= count; i ++ )
700 {
701 playlist_entry *entry = this->list[ clip ];
702 mlt_playlist_append_io( new_clip, entry->producer, entry->frame_in, entry->frame_out );
703 mlt_playlist_remove( this, clip );
704 }
705 mlt_events_unblock( mlt_playlist_properties( this ), this );
706 mlt_playlist_insert( this, mlt_playlist_producer( new_clip ), clip, 0, -1 );
707 mlt_playlist_close( new_clip );
708 }
709 return error;
710 }
711
712 /** Get the current frame.
713 */
714
715 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
716 {
717 // Get this mlt_playlist
718 mlt_playlist this = producer->child;
719
720 // Get the real producer
721 mlt_service real = mlt_playlist_virtual_seek( this );
722
723 // Get the frame
724 mlt_service_get_frame( real, frame, index );
725
726 // Check if we're at the end of the clip
727 mlt_properties properties = mlt_frame_properties( *frame );
728 if ( mlt_properties_get_int( properties, "end_of_clip" ) )
729 mlt_playlist_virtual_set_out( this );
730
731 // Check for notifier and call with appropriate argument
732 mlt_properties playlist_properties = mlt_producer_properties( producer );
733 void ( *notifier )( void * ) = mlt_properties_get_data( playlist_properties, "notifier", NULL );
734 if ( notifier != NULL )
735 {
736 void *argument = mlt_properties_get_data( playlist_properties, "notifier_arg", NULL );
737 notifier( argument );
738 }
739
740 // Update position on the frame we're creating
741 mlt_frame_set_position( *frame, mlt_producer_frame( producer ) );
742
743 // Position ourselves on the next frame
744 mlt_producer_prepare_next( producer );
745
746 return 0;
747 }
748
749 /** Close the playlist.
750 */
751
752 void mlt_playlist_close( mlt_playlist this )
753 {
754 if ( this != NULL && mlt_properties_dec_ref( mlt_playlist_properties( this ) ) <= 0 )
755 {
756 int i = 0;
757 this->parent.close = NULL;
758 for ( i = 0; i < this->count; i ++ )
759 {
760 mlt_event_close( this->list[ i ]->event );
761 mlt_producer_close( this->list[ i ]->producer );
762 free( this->list[ i ] );
763 }
764 mlt_producer_close( &this->blank );
765 mlt_producer_close( &this->parent );
766 free( this->list );
767 free( this );
768 }
769 }