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