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