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