2 * mlt_tractor.c -- tractor service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
23 #include "mlt_tractor.h"
24 #include "mlt_frame.h"
25 #include "mlt_multitrack.h"
26 #include "mlt_field.h"
32 /** Private structure.
37 struct mlt_producer_s parent;
41 /** Forward references to static methods.
44 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int track );
46 /** Constructor for the tractor.
49 mlt_tractor mlt_tractor_init( )
51 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
54 mlt_producer producer = &this->parent;
55 if ( mlt_producer_init( producer, this ) == 0 )
57 mlt_properties_set( mlt_producer_properties( producer ), "resource", "<tractor>" );
58 mlt_properties_set( mlt_producer_properties( producer ), "mlt_type", "mlt_producer" );
59 mlt_properties_set( mlt_producer_properties( producer ), "mlt_service", "tractor" );
61 producer->get_frame = producer_get_frame;
62 producer->close = ( mlt_destructor )mlt_tractor_close;
63 producer->close_object = this;
74 mlt_tractor mlt_tractor_new( )
76 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
79 mlt_producer producer = &this->parent;
80 if ( mlt_producer_init( producer, this ) == 0 )
82 mlt_multitrack multitrack = mlt_multitrack_init( );
83 mlt_field field = mlt_field_new( multitrack, this );
84 mlt_properties props = mlt_producer_properties( producer );
86 mlt_properties_set( props, "resource", "<tractor>" );
87 mlt_properties_set( props, "mlt_type", "mlt_producer" );
88 mlt_properties_set( props, "mlt_service", "tractor" );
89 mlt_properties_set_position( props, "in", 0 );
90 mlt_properties_set_position( props, "out", 0 );
91 mlt_properties_set_position( props, "length", 0 );
92 mlt_properties_set_data( props, "multitrack", multitrack, 0, ( mlt_destructor )mlt_multitrack_close, NULL );
93 mlt_properties_set_data( props, "field", field, 0, ( mlt_destructor )mlt_field_close, NULL );
95 producer->get_frame = producer_get_frame;
96 producer->close = ( mlt_destructor )mlt_tractor_close;
97 producer->close_object = this;
108 /** Get the service object associated to the tractor.
111 mlt_service mlt_tractor_service( mlt_tractor this )
113 return mlt_producer_service( &this->parent );
116 /** Get the producer object associated to the tractor.
119 mlt_producer mlt_tractor_producer( mlt_tractor this )
121 return this != NULL ? &this->parent : NULL;
124 /** Get the properties object associated to the tractor.
127 mlt_properties mlt_tractor_properties( mlt_tractor this )
129 return mlt_producer_properties( &this->parent );
132 /** Get the field this tractor is harvesting.
135 mlt_field mlt_tractor_field( mlt_tractor this )
137 return mlt_properties_get_data( mlt_tractor_properties( this ), "field", NULL );
140 /** Get the multitrack this tractor is pulling.
143 mlt_multitrack mlt_tractor_multitrack( mlt_tractor this )
145 return mlt_properties_get_data( mlt_tractor_properties( this ), "multitrack", NULL );
148 /** Ensure the tractors in/out points match the multitrack.
151 void mlt_tractor_refresh( mlt_tractor this )
153 mlt_multitrack multitrack = mlt_tractor_multitrack( this );
154 mlt_properties properties = mlt_multitrack_properties( multitrack );
155 mlt_properties self = mlt_tractor_properties( this );
156 mlt_multitrack_refresh( multitrack );
157 mlt_properties_set_position( self, "in", 0 );
158 mlt_properties_set_position( self, "out", mlt_properties_get_position( properties, "out" ) );
159 mlt_properties_set_position( self, "length", mlt_properties_get_position( properties, "length" ) );
162 /** Connect the tractor.
165 int mlt_tractor_connect( mlt_tractor this, mlt_service producer )
167 int ret = mlt_service_connect_producer( mlt_tractor_service( this ), producer, 0 );
169 // This is the producer we're going to connect to
171 this->producer = producer;
176 /** Set the producer for a specific track.
179 int mlt_tractor_set_track( mlt_tractor this, mlt_producer producer, int index )
181 int error = mlt_multitrack_connect( mlt_tractor_multitrack( this ), producer, index );
182 mlt_tractor_refresh( this );
186 /** Get the producer for a specific track.
189 mlt_producer mlt_tractor_get_track( mlt_tractor this, int index )
191 return mlt_multitrack_track( mlt_tractor_multitrack( this ), index );
194 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
196 mlt_properties properties = mlt_frame_properties( this );
197 mlt_frame frame = mlt_frame_pop_service( this );
198 mlt_properties_inherit( mlt_frame_properties( frame ), properties );
199 mlt_frame_get_image( frame, buffer, format, width, height, writable );
200 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
201 mlt_properties_set_int( properties, "width", *width );
202 mlt_properties_set_int( properties, "height", *height );
206 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
208 mlt_properties properties = mlt_frame_properties( this );
209 mlt_frame frame = mlt_frame_pop_audio( this );
210 mlt_properties_inherit( mlt_frame_properties( frame ), properties );
211 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
212 mlt_properties_set_data( properties, "audio", *buffer, 0, NULL, NULL );
213 mlt_properties_set_int( properties, "frequency", *frequency );
214 mlt_properties_set_int( properties, "channels", *channels );
218 /** Get the next frame.
221 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
223 mlt_tractor this = parent->child;
225 // We only respond to the first track requests
226 if ( track == 0 && this->producer != NULL )
230 mlt_frame temp = NULL;
233 // Get the properties of the parent producer
234 mlt_properties properties = mlt_producer_properties( parent );
236 // Try to obtain the multitrack associated to the tractor
237 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
239 // Or a specific producer
240 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
242 // If we don't have one, we're in trouble...
243 if ( multitrack != NULL )
245 // Used to garbage collect all frames
248 // Get the id of the tractor
249 char *id = mlt_properties_get( properties, "_unique_id" );
251 // Will be used to store the frame properties object
252 mlt_properties frame_properties = NULL;
254 // We'll store audio and video frames to use here
255 mlt_frame audio = NULL;
256 mlt_frame video = NULL;
258 // Get the multitrack's producer
259 mlt_producer target = mlt_multitrack_producer( multitrack );
260 mlt_producer_seek( target, mlt_producer_frame( parent ) );
261 mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
263 // We will create one frame and attach everything to it
264 *frame = mlt_frame_init( );
266 // Get the properties of the frame
267 frame_properties = mlt_frame_properties( *frame );
269 // Loop through each of the tracks we're harvesting
270 for ( i = 0; !done; i ++ )
272 // Get a frame from the producer
273 mlt_service_get_frame( this->producer, &temp, i );
275 // Check for last track
276 done = mlt_properties_get_int( mlt_frame_properties( temp ), "last_track" );
278 // We store all frames with a destructor on the output frame
279 sprintf( label, "_%s_%d", id, count ++ );
280 mlt_properties_set_data( frame_properties, label, temp, 0, ( mlt_destructor )mlt_frame_close, NULL );
282 // Pick up first video and audio frames
283 if ( !done && !mlt_frame_is_test_audio( temp ) && !( mlt_properties_get_int( mlt_frame_properties( temp ), "hide" ) & 2 ) )
285 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( mlt_frame_properties( temp ), "hide" ) & 1 ) )
289 // Now stack callbacks
292 mlt_frame_push_audio( *frame, audio );
293 ( *frame )->get_audio = producer_get_audio;
294 mlt_properties_inherit( mlt_frame_properties( *frame ), mlt_frame_properties( audio ) );
299 mlt_frame_push_service( *frame, video );
300 mlt_frame_push_service( *frame, producer_get_image );
301 mlt_properties_inherit( mlt_frame_properties( *frame ), mlt_frame_properties( video ) );
304 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", audio == NULL );
305 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", video == NULL );
307 else if ( producer != NULL )
309 mlt_producer_seek( producer, mlt_producer_frame( parent ) );
310 mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
311 mlt_service_get_frame( this->producer, frame, track );
315 fprintf( stderr, "tractor without a multitrack!!\n" );
316 mlt_service_get_frame( this->producer, frame, track );
319 // Prepare the next frame
320 mlt_producer_prepare_next( parent );
322 // Indicate our found status
327 // Generate a test card
328 *frame = mlt_frame_init( );
333 /** Close the tractor.
336 void mlt_tractor_close( mlt_tractor this )
338 if ( this != NULL && mlt_properties_dec_ref( mlt_tractor_properties( this ) ) <= 0 )
340 this->parent.close = NULL;
341 mlt_producer_close( &this->parent );