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