mlt_producer.c, mlt_playlist.h, mlt_field.h, mlt_playlist.c, mlt_tractor.c, mlt_events.c:
[melted] / src / framework / mlt_field.c
1 /**
2 * \file mlt_field.c
3 * \brief a field for planting multiple transitions and filters
4 *
5 * Copyright (C) 2003-2008 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include "mlt_field.h"
24 #include "mlt_service.h"
25 #include "mlt_filter.h"
26 #include "mlt_transition.h"
27 #include "mlt_multitrack.h"
28 #include "mlt_tractor.h"
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 /** \brief Field class
34 *
35 */
36
37 struct mlt_field_s
38 {
39 /// This is the producer we're connected to
40 mlt_service producer;
41
42 /// Multitrack
43 mlt_multitrack multitrack;
44
45 /// Tractor
46 mlt_tractor tractor;
47 };
48
49 /** Construct a field, mulitrack, and tractor.
50 *
51 * \public \memberof mlt_field_s
52 * \return a new field
53 */
54
55 mlt_field mlt_field_init( )
56 {
57 // Initialise the field
58 mlt_field this = calloc( sizeof( struct mlt_field_s ), 1 );
59
60 // Initialise it
61 if ( this != NULL )
62 {
63 // Construct a multitrack
64 this->multitrack = mlt_multitrack_init( );
65
66 // Construct a tractor
67 this->tractor = mlt_tractor_init( );
68
69 // The first plant will be connected to the mulitrack
70 this->producer = MLT_MULTITRACK_SERVICE( this->multitrack );
71
72 // Connect the tractor to the multitrack
73 mlt_tractor_connect( this->tractor, this->producer );
74 }
75
76 // Return this
77 return this;
78 }
79
80 /** Construct a field and initialize with supplied multitrack and tractor.
81 *
82 * \public \memberof mlt_field_s
83 * \param multitrack a multitrack
84 * \param tractor a tractor
85 * \return a new field
86 */
87
88 mlt_field mlt_field_new( mlt_multitrack multitrack, mlt_tractor tractor )
89 {
90 // Initialise the field
91 mlt_field this = calloc( sizeof( struct mlt_field_s ), 1 );
92
93 // Initialise it
94 if ( this != NULL )
95 {
96 // Construct a multitrack
97 this->multitrack = multitrack;
98
99 // Construct a tractor
100 this->tractor = tractor;
101
102 // The first plant will be connected to the mulitrack
103 this->producer = MLT_MULTITRACK_SERVICE( this->multitrack );
104
105 // Connect the tractor to the multitrack
106 mlt_tractor_connect( this->tractor, this->producer );
107 }
108
109 // Return this
110 return this;
111 }
112
113 /** Get the service associated to this field.
114 *
115 * \public \memberof mlt_field_s
116 * \param this a field
117 * \return the tractor as a service
118 */
119
120 mlt_service mlt_field_service( mlt_field this )
121 {
122 return MLT_TRACTOR_SERVICE( this->tractor );
123 }
124
125 /** Get the multitrack.
126 *
127 * \public \memberof mlt_field_s
128 * \param this a field
129 * \return the multitrack
130 */
131
132 mlt_multitrack mlt_field_multitrack( mlt_field this )
133 {
134 return this != NULL ? this->multitrack : NULL;
135 }
136
137 /** Get the tractor.
138 *
139 * \public \memberof mlt_field_s
140 * \param this a field
141 * \return the tractor
142 */
143
144 mlt_tractor mlt_field_tractor( mlt_field this )
145 {
146 return this != NULL ? this->tractor : NULL;
147 }
148
149 /** Get the properties associated to this field.
150 *
151 * \public \memberof mlt_field_s
152 * \param this a field
153 * \return a properties list
154 */
155
156 mlt_properties mlt_field_properties( mlt_field this )
157 {
158 return MLT_SERVICE_PROPERTIES( mlt_field_service( this ) );
159 }
160
161 /** Plant a filter.
162 *
163 * \public \memberof mlt_field_s
164 * \param this a field
165 * \param that a filter
166 * \param track the track index
167 * \return true if there was an error
168 */
169
170 int mlt_field_plant_filter( mlt_field this, mlt_filter that, int track )
171 {
172 // Connect the filter to the last producer
173 int result = mlt_filter_connect( that, this->producer, track );
174
175 // If sucessful, then we'll use this for connecting in the future
176 if ( result == 0 )
177 {
178 // This is now the new producer
179 this->producer = MLT_FILTER_SERVICE( that );
180
181 // Reconnect tractor to new producer
182 mlt_tractor_connect( this->tractor, this->producer );
183
184 // Fire an event
185 mlt_events_fire( mlt_field_properties( this ), "service-changed", NULL );
186 }
187
188 return result;
189 }
190
191 /** Plant a transition.
192 *
193 * \public \memberof mlt_field_s
194 * \param this a field
195 * \param that a transition
196 * \param a_track input A's track index
197 * \param b_track input B's track index
198 * \return true if there was an error
199 */
200
201 int mlt_field_plant_transition( mlt_field this, mlt_transition that, int a_track, int b_track )
202 {
203 // Connect the transition to the last producer
204 int result = mlt_transition_connect( that, this->producer, a_track, b_track );
205
206 // If sucessful, then we'll use this for connecting in the future
207 if ( result == 0 )
208 {
209 // This is now the new producer
210 this->producer = MLT_TRANSITION_SERVICE( that );
211
212 // Reconnect tractor to new producer
213 mlt_tractor_connect( this->tractor, this->producer );
214
215 // Fire an event
216 mlt_events_fire( mlt_field_properties( this ), "service-changed", NULL );
217 }
218
219 return 0;
220 }
221
222 /** Close the field.
223 *
224 * \public \memberof mlt_field_s
225 * \param this a field
226 */
227
228 void mlt_field_close( mlt_field this )
229 {
230 if ( this != NULL && mlt_properties_dec_ref( mlt_field_properties( this ) ) <= 0 )
231 {
232 //mlt_tractor_close( this->tractor );
233 //mlt_multitrack_close( this->multitrack );
234 free( this );
235 }
236 }
237
238 /** Remove a filter or transition from the field.
239 *
240 * \public \memberof mlt_field_s
241 * \param self a field
242 * \param service the filter or transition to remove
243 */
244
245 void mlt_field_disconnect_service( mlt_field self, mlt_service service )
246 {
247 mlt_service p = mlt_service_producer( service );
248 mlt_service c = mlt_service_consumer( service);
249 int i;
250 switch ( mlt_service_identify(c) )
251 {
252 case filter_type:
253 i = mlt_filter_get_track( MLT_FILTER(c) );
254 mlt_service_connect_producer( c, p, i );
255 break;
256 case transition_type:
257 i = mlt_transition_get_a_track ( MLT_TRANSITION(c) );
258 mlt_service_connect_producer( c, p, i );
259 MLT_TRANSITION(c)->producer = p;
260 break;
261 case tractor_type:
262 self->producer = p;
263 mlt_tractor_connect( MLT_TRACTOR(c), p );
264 default:
265 break;
266 }
267 mlt_events_fire( mlt_field_properties( self ), "service-changed", NULL );
268 }