mlt_field.[hc]: added mlt_field_disconnect_service
[melted] / src / framework / mlt_field.c
1 /*
2 * mlt_field.c -- A field for planting multiple transitions and filters
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "mlt_field.h"
22 #include "mlt_service.h"
23 #include "mlt_filter.h"
24 #include "mlt_transition.h"
25 #include "mlt_multitrack.h"
26 #include "mlt_tractor.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30
31 /** Private structures.
32 */
33
34 struct mlt_field_s
35 {
36 // This is the producer we're connected to
37 mlt_service producer;
38
39 // Multitrack
40 mlt_multitrack multitrack;
41
42 // Tractor
43 mlt_tractor tractor;
44 };
45
46 /** Constructor.
47
48 We construct a multitrack and a tractor here.
49 */
50
51 mlt_field mlt_field_init( )
52 {
53 // Initialise the field
54 mlt_field this = calloc( sizeof( struct mlt_field_s ), 1 );
55
56 // Initialise it
57 if ( this != NULL )
58 {
59 // Construct a multitrack
60 this->multitrack = mlt_multitrack_init( );
61
62 // Construct a tractor
63 this->tractor = mlt_tractor_init( );
64
65 // The first plant will be connected to the mulitrack
66 this->producer = MLT_MULTITRACK_SERVICE( this->multitrack );
67
68 // Connect the tractor to the multitrack
69 mlt_tractor_connect( this->tractor, this->producer );
70 }
71
72 // Return this
73 return this;
74 }
75
76 mlt_field mlt_field_new( mlt_multitrack multitrack, mlt_tractor tractor )
77 {
78 // Initialise the field
79 mlt_field this = calloc( sizeof( struct mlt_field_s ), 1 );
80
81 // Initialise it
82 if ( this != NULL )
83 {
84 // Construct a multitrack
85 this->multitrack = multitrack;
86
87 // Construct a tractor
88 this->tractor = tractor;
89
90 // The first plant will be connected to the mulitrack
91 this->producer = MLT_MULTITRACK_SERVICE( this->multitrack );
92
93 // Connect the tractor to the multitrack
94 mlt_tractor_connect( this->tractor, this->producer );
95 }
96
97 // Return this
98 return this;
99 }
100
101 /** Get the service associated to this field.
102 */
103
104 mlt_service mlt_field_service( mlt_field this )
105 {
106 return MLT_TRACTOR_SERVICE( this->tractor );
107 }
108
109 /** Get the multi track.
110 */
111
112 mlt_multitrack mlt_field_multitrack( mlt_field this )
113 {
114 return this != NULL ? this->multitrack : NULL;
115 }
116
117 /** Get the tractor.
118 */
119
120 mlt_tractor mlt_field_tractor( mlt_field this )
121 {
122 return this != NULL ? this->tractor : NULL;
123 }
124
125 /** Get the properties associated to this field.
126 */
127
128 mlt_properties mlt_field_properties( mlt_field this )
129 {
130 return MLT_SERVICE_PROPERTIES( mlt_field_service( this ) );
131 }
132
133 /** Plant a filter.
134 */
135
136 int mlt_field_plant_filter( mlt_field this, mlt_filter that, int track )
137 {
138 // Connect the filter to the last producer
139 int result = mlt_filter_connect( that, this->producer, track );
140
141 // If sucessful, then we'll use this for connecting in the future
142 if ( result == 0 )
143 {
144 // This is now the new producer
145 this->producer = MLT_FILTER_SERVICE( that );
146
147 // Reconnect tractor to new producer
148 mlt_tractor_connect( this->tractor, this->producer );
149
150 // Fire an event
151 mlt_events_fire( mlt_field_properties( this ), "service-changed", NULL );
152 }
153
154 return result;
155 }
156
157 /** Plant a transition.
158 */
159
160 int mlt_field_plant_transition( mlt_field this, mlt_transition that, int a_track, int b_track )
161 {
162 // Connect the transition to the last producer
163 int result = mlt_transition_connect( that, this->producer, a_track, b_track );
164
165 // If sucessful, then we'll use this for connecting in the future
166 if ( result == 0 )
167 {
168 // This is now the new producer
169 this->producer = MLT_TRANSITION_SERVICE( that );
170
171 // Reconnect tractor to new producer
172 mlt_tractor_connect( this->tractor, this->producer );
173
174 // Fire an event
175 mlt_events_fire( mlt_field_properties( this ), "service-changed", NULL );
176 }
177
178 return 0;
179 }
180
181 /** Close the field.
182 */
183
184 void mlt_field_close( mlt_field this )
185 {
186 if ( this != NULL && mlt_properties_dec_ref( mlt_field_properties( this ) ) <= 0 )
187 {
188 //mlt_tractor_close( this->tractor );
189 //mlt_multitrack_close( this->multitrack );
190 free( this );
191 }
192 }
193
194 void mlt_field_disconnect_service( mlt_field self, mlt_service service )
195 {
196 mlt_service p = mlt_service_producer( service );
197 mlt_service c = mlt_service_consumer( service);
198 int i;
199 switch ( mlt_service_identify(c) )
200 {
201 case filter_type:
202 i = mlt_filter_get_track( MLT_FILTER(c) );
203 mlt_service_connect_producer( c, p, i );
204 break;
205 case transition_type:
206 i = mlt_transition_get_a_track ( MLT_TRANSITION(c) );
207 mlt_service_connect_producer( c, p, i );
208 break;
209 case tractor_type:
210 self->producer = p;
211 mlt_tractor_connect( MLT_TRACTOR(c), p );
212 default:
213 break;
214 }
215 mlt_events_fire( mlt_field_properties( self ), "service-changed", NULL );
216 }