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"
31 /** Private structure.
36 struct mlt_producer_s parent;
40 /** Forward references to static methods.
43 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int track );
45 /** Constructor for the tractor.
48 mlt_tractor mlt_tractor_init( )
50 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
53 mlt_producer producer = &this->parent;
54 if ( mlt_producer_init( producer, this ) == 0 )
56 mlt_properties_set( mlt_producer_properties( producer ), "resource", "<tractor>" );
57 mlt_properties_set( mlt_producer_properties( producer ), "mlt_type", "mlt_producer" );
58 mlt_properties_set( mlt_producer_properties( producer ), "mlt_service", "tractor" );
60 producer->get_frame = producer_get_frame;
61 producer->close = ( mlt_destructor )mlt_tractor_close;
62 producer->close_object = this;
73 /** Get the service object associated to the tractor.
76 mlt_service mlt_tractor_service( mlt_tractor this )
78 return mlt_producer_service( &this->parent );
81 /** Get the producer object associated to the tractor.
84 mlt_producer mlt_tractor_producer( mlt_tractor this )
86 return this != NULL ? &this->parent : NULL;
89 /** Get the properties object associated to the tractor.
92 mlt_properties mlt_tractor_properties( mlt_tractor this )
94 return mlt_producer_properties( &this->parent );
97 /** Connect the tractor.
100 int mlt_tractor_connect( mlt_tractor this, mlt_service producer )
102 int ret = mlt_service_connect_producer( mlt_tractor_service( this ), producer, 0 );
104 // This is the producer we're going to connect to
106 this->producer = producer;
111 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
113 mlt_properties properties = mlt_frame_properties( this );
114 mlt_frame frame = mlt_frame_pop_service( this );
115 mlt_properties_inherit( mlt_frame_properties( frame ), properties );
116 mlt_frame_get_image( frame, buffer, format, width, height, writable );
117 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
118 mlt_properties_set_int( properties, "width", *width );
119 mlt_properties_set_int( properties, "height", *height );
123 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
125 mlt_properties properties = mlt_frame_properties( this );
126 mlt_frame frame = mlt_frame_pop_audio( this );
127 mlt_properties_inherit( mlt_frame_properties( frame ), properties );
128 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
129 mlt_properties_set_data( properties, "audio", *buffer, 0, NULL, NULL );
130 mlt_properties_set_int( properties, "frequency", *frequency );
131 mlt_properties_set_int( properties, "channels", *channels );
135 /** Get the next frame.
138 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
140 mlt_tractor this = parent->child;
142 // We only respond to the first track requests
143 if ( track == 0 && this->producer != NULL )
147 mlt_frame temp = NULL;
150 // Get the properties of the parent producer
151 mlt_properties properties = mlt_producer_properties( parent );
153 // Try to obtain the multitrack associated to the tractor
154 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
156 // Or a specific producer
157 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
159 // If we don't have one, we're in trouble...
160 if ( multitrack != NULL )
162 // Used to garbage collect all frames
165 // Get the id of the tractor
166 char *id = mlt_properties_get( properties, "_unique_id" );
168 // Will be used to store the frame properties object
169 mlt_properties frame_properties = NULL;
171 // We'll store audio and video frames to use here
172 mlt_frame audio = NULL;
173 mlt_frame video = NULL;
175 // Get the multitrack's producer
176 mlt_producer target = mlt_multitrack_producer( multitrack );
177 mlt_producer_seek( target, mlt_producer_frame( parent ) );
178 mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
180 // We will create one frame and attach everything to it
181 *frame = mlt_frame_init( );
183 // Get the properties of the frame
184 frame_properties = mlt_frame_properties( *frame );
186 // Loop through each of the tracks we're harvesting
187 for ( i = 0; !done; i ++ )
189 // Get a frame from the producer
190 mlt_service_get_frame( this->producer, &temp, i );
192 // Check for last track
193 done = mlt_properties_get_int( mlt_frame_properties( temp ), "last_track" );
195 // We store all frames with a destructor on the output frame
196 sprintf( label, "_%s_%d", id, count ++ );
197 mlt_properties_set_data( frame_properties, label, temp, 0, ( mlt_destructor )mlt_frame_close, NULL );
199 // Pick up first video and audio frames
200 if ( !done && !mlt_frame_is_test_audio( temp ) && !( mlt_properties_get_int( mlt_frame_properties( temp ), "hide" ) & 2 ) )
202 if ( !done && !mlt_frame_is_test_card( temp ) && !( mlt_properties_get_int( mlt_frame_properties( temp ), "hide" ) & 1 ) )
206 // Now stack callbacks
209 mlt_frame_push_audio( *frame, audio );
210 ( *frame )->get_audio = producer_get_audio;
211 mlt_properties_inherit( mlt_frame_properties( *frame ), mlt_frame_properties( audio ) );
216 mlt_frame_push_service( *frame, video );
217 mlt_frame_push_service( *frame, producer_get_image );
218 mlt_properties_inherit( mlt_frame_properties( *frame ), mlt_frame_properties( video ) );
221 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", audio == NULL );
222 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", video == NULL );
224 else if ( producer != NULL )
226 mlt_producer_seek( producer, mlt_producer_frame( parent ) );
227 mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
228 mlt_service_get_frame( this->producer, frame, track );
232 fprintf( stderr, "tractor without a multitrack!!\n" );
233 mlt_service_get_frame( this->producer, frame, track );
236 // Prepare the next frame
237 mlt_producer_prepare_next( parent );
239 // Indicate our found status
244 // Generate a test card
245 *frame = mlt_frame_init( );
250 /** Close the tractor.
253 void mlt_tractor_close( mlt_tractor this )
255 if ( this != NULL && mlt_properties_dec_ref( mlt_tractor_properties( this ) ) <= 0 )
257 this->parent.close = NULL;
258 mlt_producer_close( &this->parent );