adding the rock thrower...
[melted] / src / framework / mlt_tractor.c
1 /*
2 * mlt_tractor.c -- tractor service class
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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 #include "config.h"
22
23 #include "mlt_tractor.h"
24 #include "mlt_frame.h"
25 #include "mlt_multitrack.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 /** Private structure.
32 */
33
34 struct mlt_tractor_s
35 {
36 struct mlt_producer_s parent;
37 mlt_service producer;
38 };
39
40 /** Forward references to static methods.
41 */
42
43 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int track );
44 static void producer_close( mlt_producer this );
45
46 /** Constructor for the tractor.
47
48 TODO: thread this service...
49 */
50
51 mlt_tractor mlt_tractor_init( )
52 {
53 mlt_tractor this = calloc( sizeof( struct mlt_tractor_s ), 1 );
54 if ( this != NULL )
55 {
56 mlt_producer producer = &this->parent;
57 if ( mlt_producer_init( producer, this ) == 0 )
58 {
59 mlt_properties_set( mlt_producer_properties( producer ), "resource", "<tractor>" );
60 mlt_properties_set( mlt_producer_properties( producer ), "mlt_type", "mlt_producer" );
61 mlt_properties_set( mlt_producer_properties( producer ), "mlt_service", "tractor" );
62
63 producer->get_frame = producer_get_frame;
64 producer->close = producer_close;
65 }
66 else
67 {
68 free( this );
69 this = NULL;
70 }
71 }
72 return this;
73 }
74
75 /** Get the service object associated to the tractor.
76 */
77
78 mlt_service mlt_tractor_service( mlt_tractor this )
79 {
80 return mlt_producer_service( &this->parent );
81 }
82
83 /** Get the producer object associated to the tractor.
84 */
85
86 mlt_producer mlt_tractor_producer( mlt_tractor this )
87 {
88 return &this->parent;
89 }
90
91 /** Get the properties object associated to the tractor.
92 */
93
94 mlt_properties mlt_tractor_properties( mlt_tractor this )
95 {
96 return mlt_producer_properties( &this->parent );
97 }
98
99 /** Connect the tractor.
100 */
101
102 int mlt_tractor_connect( mlt_tractor this, mlt_service producer )
103 {
104 int ret = mlt_service_connect_producer( mlt_tractor_service( this ), producer, 0 );
105
106 // This is the producer we're going to connect to
107 if ( ret == 0 )
108 this->producer = producer;
109
110 return ret;
111 }
112
113 /** Get the next frame.
114
115 TODO: This should be reading a pump being populated by the thread...
116 */
117
118 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int track )
119 {
120 mlt_tractor this = parent->child;
121
122 // We only respond to the first track requests
123 if ( track == 0 && this->producer != NULL )
124 {
125 int i = 0;
126 int looking = 1;
127 int done = 0;
128 mlt_frame temp = NULL;
129 mlt_frame store[ 10 ];
130 int count = 0;
131
132 // Get the properties of the parent producer
133 mlt_properties properties = mlt_producer_properties( parent );
134
135 // Try to obtain the multitrack associated to the tractor
136 mlt_multitrack multitrack = mlt_properties_get_data( properties, "multitrack", NULL );
137
138 // Or a specific producer
139 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
140
141 // If we don't have one, we're in trouble...
142 if ( multitrack != NULL )
143 {
144 mlt_producer target = mlt_multitrack_producer( multitrack );
145 mlt_producer_seek( target, mlt_producer_frame( parent ) );
146 mlt_producer_set_speed( target, mlt_producer_get_speed( parent ) );
147
148 // Loop through each of the tracks we're harvesting
149 for ( i = 0; !done; i ++ )
150 {
151 // Get a frame from the producer
152 mlt_service_get_frame( this->producer, &temp, i );
153
154 // Check for last track
155 done = mlt_properties_get_int( mlt_frame_properties( temp ), "last_track" );
156
157 // Handle the frame
158 if ( done && looking )
159 {
160 // Use this as output if we don't have one already
161 *frame = temp;
162 }
163 else if ( ( !mlt_frame_is_test_card( temp ) || !mlt_frame_is_test_audio( temp ) ) && looking &&
164 mlt_producer_frame( parent ) == mlt_frame_get_position( temp ) )
165 {
166 *frame = temp;
167 looking = 0;
168 }
169 else
170 {
171 // We store all other frames for now
172 store[ count ++ ] = temp;
173 }
174 }
175
176 // Now place all the unused frames on to the properties (will be destroyed automatically)
177 while ( count -- )
178 {
179 mlt_properties frame_properties = mlt_frame_properties( *frame );
180 char label[ 30 ];
181 sprintf( label, "tractor_%d", count );
182 while ( mlt_properties_get_data( frame_properties, label, NULL ) != NULL )
183 strcat( label, "+" );
184 mlt_properties_set_data( frame_properties, label, store[ count ], 0, ( mlt_destructor )mlt_frame_close, NULL );
185 }
186 }
187 else if ( producer != NULL )
188 {
189 mlt_producer_seek( producer, mlt_producer_frame( parent ) );
190 mlt_producer_set_speed( producer, mlt_producer_get_speed( parent ) );
191 mlt_service_get_frame( this->producer, frame, track );
192 }
193 else
194 {
195 fprintf( stderr, "tractor without a multitrack!!\n" );
196 mlt_service_get_frame( this->producer, frame, track );
197 }
198
199 // Prepare the next frame
200 mlt_producer_prepare_next( parent );
201
202 // Indicate our found status
203 return 0;
204 }
205 else
206 {
207 // Generate a test card
208 *frame = mlt_frame_init( );
209 return 0;
210 }
211 }
212
213 /** Close the tractor.
214 */
215
216 void mlt_tractor_close( mlt_tractor this )
217 {
218 this->parent.close = NULL;
219 mlt_producer_close( &this->parent );
220 free( this );
221 }
222
223 /** Close the producer.
224 */
225
226 static void producer_close( mlt_producer this )
227 {
228 mlt_tractor_close( this->child );
229 }
230