More miracle mods
[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, ".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 static void clear_unit( miracle_unit unit )
188 {
189 mlt_properties properties = unit->properties;
190 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
191 mlt_producer producer = mlt_playlist_producer( playlist );
192
193 mlt_playlist_clear( playlist );
194 mlt_producer_seek( producer, 0 );
195
196 if ( unit->old_producers != NULL )
197 mlt_properties_close( unit->old_producers );
198 unit->old_producers = unit->producers;
199 unit->producers = mlt_properties_new( );
200
201 update_generation( unit );
202 }
203
204 /** Generate a report on all loaded clips.
205 */
206
207 void miracle_unit_report_list( miracle_unit unit, valerie_response response )
208 {
209 int i;
210 mlt_properties properties = unit->properties;
211 int generation = mlt_properties_get_int( properties, "generation" );
212 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
213
214 valerie_response_printf( response, 1024, "%d\n", generation );
215
216 for ( i = 0; i < mlt_playlist_count( playlist ); i ++ )
217 {
218 mlt_playlist_clip_info info;
219 mlt_playlist_get_clip_info( playlist , &info, i );
220 valerie_response_printf( response, 10240, "%d \"%s\" %e %e %e %e %.2f\n",
221 i, info.resource, info.in, info.out, info.playtime, info.length, info.fps );
222 }
223 }
224
225 /** Load a clip into the unit clearing existing play list.
226
227 \todo error handling
228 \param unit A miracle_unit handle.
229 \param clip The absolute file name of the clip to load.
230 \param in The starting frame (-1 for 0)
231 \param out The ending frame (-1 for maximum)
232 */
233
234 valerie_error_code miracle_unit_load( miracle_unit unit, char *clip, double in, double out, int flush )
235 {
236 // Have to clear the unit first
237 clear_unit( unit );
238
239 // Now try to create an producer
240 mlt_producer instance = create_producer( unit, clip );
241
242 if ( instance != NULL )
243 {
244 mlt_properties properties = unit->properties;
245 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
246 mlt_playlist_append_io( playlist, instance, in, out );
247 miracle_log( LOG_DEBUG, "loaded clip %s", clip );
248 miracle_unit_status_communicate( unit );
249 return valerie_ok;
250 }
251
252 return valerie_invalid_file;
253 }
254
255 valerie_error_code miracle_unit_insert( miracle_unit unit, char *clip, int index, double in, double out )
256 {
257 mlt_producer instance = create_producer( unit, clip );
258
259 if ( instance != NULL )
260 {
261 mlt_properties properties = unit->properties;
262 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
263 mlt_playlist_insert( playlist, instance, index, in, out );
264 miracle_log( LOG_DEBUG, "inserted clip %s at %d", clip, index );
265 miracle_unit_status_communicate( unit );
266 return valerie_ok;
267 }
268
269 return valerie_invalid_file;
270 }
271
272 valerie_error_code miracle_unit_remove( miracle_unit unit, int index )
273 {
274 mlt_properties properties = unit->properties;
275 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
276 mlt_playlist_remove( playlist, index );
277 miracle_log( LOG_DEBUG, "removed clip at %d", index );
278 miracle_unit_status_communicate( unit );
279 return valerie_ok;
280 }
281
282 valerie_error_code miracle_unit_clean( miracle_unit unit )
283 {
284 clear_unit( unit );
285 miracle_log( LOG_DEBUG, "Cleaned playlist" );
286 return valerie_ok;
287 }
288
289 valerie_error_code miracle_unit_move( miracle_unit unit, int src, int dest )
290 {
291 mlt_properties properties = unit->properties;
292 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
293 mlt_playlist_move( playlist, src, dest );
294 miracle_log( LOG_DEBUG, "moved clip %d to %d", src, dest );
295 miracle_unit_status_communicate( unit );
296 return valerie_ok;
297 }
298
299 /** Add a clip to the unit play list.
300
301 \todo error handling
302 \param unit A miracle_unit handle.
303 \param clip The absolute file name of the clip to load.
304 \param in The starting frame (-1 for 0)
305 \param out The ending frame (-1 for maximum)
306 */
307
308 valerie_error_code miracle_unit_append( miracle_unit unit, char *clip, double in, double out )
309 {
310 mlt_producer instance = create_producer( unit, clip );
311
312 if ( instance != NULL )
313 {
314 mlt_properties properties = unit->properties;
315 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
316 mlt_playlist_append_io( playlist, instance, in, out );
317 miracle_log( LOG_DEBUG, "appended clip %s", clip );
318 miracle_unit_status_communicate( unit );
319 return valerie_ok;
320 }
321
322 return valerie_invalid_file;
323 }
324
325 /** Start playing the clip.
326
327 Start a dv-pump and commence dv1394 transmission.
328
329 \todo error handling
330 \param unit A miracle_unit handle.
331 \param speed An integer that specifies the playback rate as a
332 percentage multiplied by 100.
333 */
334
335 void miracle_unit_play( miracle_unit_t *unit, int speed )
336 {
337 mlt_properties properties = unit->properties;
338 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
339 mlt_producer producer = mlt_playlist_producer( playlist );
340 mlt_producer_set_speed( producer, ( double )speed / 1000 );
341 miracle_unit_status_communicate( unit );
342 }
343
344 /** Stop playback.
345
346 Terminates the dv_pump and halts dv1394 transmission.
347
348 \param unit A miracle_unit handle.
349 */
350
351 void miracle_unit_terminate( miracle_unit unit )
352 {
353 }
354
355 /** Query the status of unit playback.
356
357 \param unit A miracle_unit handle.
358 \return 1 if the unit is not playing, 0 if playing.
359 */
360
361 int miracle_unit_has_terminated( miracle_unit unit )
362 {
363 return 0;
364 }
365
366 /** Transfer the currently loaded clip to another unit
367 */
368
369 int miracle_unit_transfer( miracle_unit dest_unit, miracle_unit src_unit )
370 {
371 return 0;
372 }
373
374 /** Determine if unit is offline.
375 */
376
377 int miracle_unit_is_offline( miracle_unit unit )
378 {
379 return 0;
380 }
381
382 /** Obtain the status for a given unit
383 */
384
385 int miracle_unit_get_status( miracle_unit unit, valerie_status status )
386 {
387 int error = unit == NULL;
388
389 memset( status, 0, sizeof( valerie_status_t ) );
390
391 if ( !error )
392 {
393 mlt_properties properties = unit->properties;
394 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
395 mlt_producer producer = mlt_playlist_producer( playlist );
396 mlt_producer clip = mlt_playlist_current( playlist );
397
398 mlt_playlist_clip_info info;
399 int clip_index = mlt_playlist_current_clip( playlist );
400 mlt_playlist_get_clip_info( playlist, &info, clip_index );
401
402 if ( info.resource != NULL && strcmp( info.resource, "" ) )
403 {
404 strncpy( status->clip, info.resource, sizeof( status->clip ) );
405 status->speed = (int)( mlt_producer_get_speed( producer ) * 1000.0 );
406 status->fps = mlt_producer_get_fps( producer );
407 status->in = info.in;
408 status->out = info.in + info.playtime;
409 status->position = mlt_producer_position( clip );
410 status->length = mlt_producer_get_length( clip );
411 status->seek_flag = 1;
412 }
413
414 if ( !strcmp( status->clip, "" ) )
415 status->status = unit_not_loaded;
416 else if ( status->speed == 0 )
417 status->status = unit_paused;
418 else
419 status->status = unit_playing;
420 }
421 else
422 {
423 status->status = unit_undefined;
424 }
425
426 status->unit = mlt_properties_get_int( unit->properties, "unit" );
427
428 return error;
429 }
430
431 /** Change position in the playlist.
432 */
433
434 void miracle_unit_change_position( miracle_unit unit, int clip, double position )
435 {
436 miracle_unit_status_communicate( unit );
437 }
438
439 /** Get the index of the current clip.
440 */
441
442 int miracle_unit_get_current_clip( miracle_unit unit )
443 {
444 mlt_properties properties = unit->properties;
445 mlt_playlist playlist = mlt_properties_get_data( properties, "playlist", NULL );
446 int clip_index = mlt_playlist_current_clip( playlist );
447 return clip_index;
448 }
449
450 /** Set a clip's in point
451 */
452
453 int miracle_unit_set_clip_in( miracle_unit unit, int index, double position )
454 {
455 int error = 0;
456 return error;
457 }
458
459 /** Set a clip's out point.
460 */
461
462 int miracle_unit_set_clip_out( miracle_unit unit, int index, double position )
463 {
464 int error = 0;
465 return error;
466 }
467
468 /** Step by specified position.
469 */
470
471 void miracle_unit_step( miracle_unit unit, double offset )
472 {
473 }
474
475 /** Set the unit's clip mode regarding in and out points.
476 */
477
478 //void miracle_unit_set_mode( miracle_unit unit, dv_player_clip_mode mode )
479 //{
480 //dv_player player = miracle_unit_get_dv_player( unit );
481 //if ( player != NULL )
482 //dv_player_set_clip_mode( player, mode );
483 //miracle_unit_status_communicate( unit );
484 //}
485
486 /** Get the unit's clip mode regarding in and out points.
487 */
488
489 //dv_player_clip_mode miracle_unit_get_mode( miracle_unit unit )
490 //{
491 //dv_player player = miracle_unit_get_dv_player( unit );
492 //return dv_player_get_clip_mode( player );
493 //}
494
495 /** Set the unit's clip mode regarding eof handling.
496 */
497
498 //void miracle_unit_set_eof_action( miracle_unit unit, dv_player_eof_action action )
499 //{
500 //dv_player player = miracle_unit_get_dv_player( unit );
501 //dv_player_set_eof_action( player, action );
502 //miracle_unit_status_communicate( unit );
503 //}
504
505 /** Get the unit's clip mode regarding eof handling.
506 */
507
508 //dv_player_eof_action miracle_unit_get_eof_action( miracle_unit unit )
509 //{
510 //dv_player player = miracle_unit_get_dv_player( unit );
511 //return dv_player_get_eof_action( player );
512 //}
513
514 /** Release the unit
515
516 \todo error handling
517 \param unit A miracle_unit handle.
518 */
519
520 void miracle_unit_close( miracle_unit unit )
521 {
522 if ( unit != NULL )
523 {
524 miracle_log( LOG_DEBUG, "closing unit..." );
525 if ( unit->old_producers != NULL )
526 mlt_properties_close( unit->old_producers );
527 mlt_properties_close( unit->properties );
528 mlt_properties_close( unit->producers );
529 free( unit );
530 miracle_log( LOG_DEBUG, "... unit closed." );
531 }
532 }
533