First draft of event handling
[melted] / src / framework / mlt_service.c
1 /*
2 * mlt_service.c -- interface for all service classes
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 #include "mlt_service.h"
23 #include "mlt_frame.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 /** IMPORTANT NOTES
29
30 The base service implements a null frame producing service - as such,
31 it is functional without extension and will produce test cards frames
32 and PAL sized audio frames.
33
34 PLEASE DO NOT CHANGE THIS BEHAVIOUR!!! OVERRIDE THE METHODS THAT
35 CONTROL THIS IN EXTENDING CLASSES.
36 */
37
38 /** Private service definition.
39 */
40
41 typedef struct
42 {
43 int size;
44 int count;
45 mlt_service *in;
46 mlt_service out;
47 }
48 mlt_service_base;
49
50 /** Private methods
51 */
52
53 static void mlt_service_disconnect( mlt_service this );
54 static void mlt_service_connect( mlt_service this, mlt_service that );
55 static int service_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
56 static void mlt_service_property_changed( mlt_listener, mlt_properties owner, mlt_service this, void **args );
57
58 /** Constructor
59 */
60
61 int mlt_service_init( mlt_service this, void *child )
62 {
63 int error = 0;
64
65 // Initialise everything to NULL
66 memset( this, 0, sizeof( struct mlt_service_s ) );
67
68 // Assign the child
69 this->child = child;
70
71 // Generate local space
72 this->local = calloc( sizeof( mlt_service_base ), 1 );
73
74 // Associate the methods
75 this->get_frame = service_get_frame;
76
77 // Initialise the properties
78 error = mlt_properties_init( &this->parent, this );
79 if ( error == 0 )
80 {
81 this->parent.close = ( mlt_destructor )mlt_service_close;
82 this->parent.close_object = this;
83
84 mlt_events_init( &this->parent );
85 mlt_events_register( &this->parent, "property-changed", ( mlt_transmitter )mlt_service_property_changed );
86 }
87
88 return error;
89 }
90
91 static void mlt_service_property_changed( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
92 {
93 if ( listener != NULL )
94 listener( owner, this, ( char * )args[ 0 ] );
95 }
96
97 /** Connect a producer service.
98 Returns: > 0 warning, == 0 success, < 0 serious error
99 1 = this service does not accept input
100 2 = the producer is invalid
101 3 = the producer is already registered with this consumer
102 */
103
104 int mlt_service_connect_producer( mlt_service this, mlt_service producer, int index )
105 {
106 int i = 0;
107
108 // Get the service base
109 mlt_service_base *base = this->local;
110
111 // Check if the producer is already registered with this service
112 for ( i = 0; i < base->count; i ++ )
113 if ( base->in[ i ] == producer )
114 return 3;
115
116 // Allocate space
117 if ( index >= base->size )
118 {
119 int new_size = base->size + index + 10;
120 base->in = realloc( base->in, new_size * sizeof( mlt_service ) );
121 if ( base->in != NULL )
122 {
123 for ( i = base->size; i < new_size; i ++ )
124 base->in[ i ] = NULL;
125 base->size = new_size;
126 }
127 }
128
129 // If we have space, assign the input
130 if ( base->in != NULL && index >= 0 && index < base->size )
131 {
132 // Get the current service
133 mlt_service current = base->in[ index ];
134
135 // Increment the reference count on this producer
136 if ( producer != NULL )
137 mlt_properties_inc_ref( mlt_service_properties( producer ) );
138
139 // Now we disconnect the producer service from its consumer
140 mlt_service_disconnect( producer );
141
142 // Add the service to index specified
143 base->in[ index ] = producer;
144
145 // Determine the number of active tracks
146 if ( index >= base->count )
147 base->count = index + 1;
148
149 // Now we connect the producer to its connected consumer
150 mlt_service_connect( producer, this );
151
152 // Close the current service
153 mlt_service_close( current );
154
155 // Inform caller that all went well
156 return 0;
157 }
158 else
159 {
160 return -1;
161 }
162 }
163
164 /** Disconnect this service from its consumer.
165 */
166
167 static void mlt_service_disconnect( mlt_service this )
168 {
169 if ( this != NULL )
170 {
171 // Get the service base
172 mlt_service_base *base = this->local;
173
174 // Disconnect
175 base->out = NULL;
176 }
177 }
178
179 /** Obtain the consumer this service is connected to.
180 */
181
182 mlt_service mlt_service_consumer( mlt_service this )
183 {
184 // Get the service base
185 mlt_service_base *base = this->local;
186
187 // Return the connected consumer
188 return base->out;
189 }
190
191 /** Obtain the producer this service is connected to.
192 */
193
194 mlt_service mlt_service_producer( mlt_service this )
195 {
196 // Get the service base
197 mlt_service_base *base = this->local;
198
199 // Return the connected producer
200 return base->count > 0 ? base->in[ base->count - 1 ] : NULL;
201 }
202
203 /** Associate this service to the consumer.
204 */
205
206 static void mlt_service_connect( mlt_service this, mlt_service that )
207 {
208 if ( this != NULL )
209 {
210 // Get the service base
211 mlt_service_base *base = this->local;
212
213 // There's a bit more required here...
214 base->out = that;
215 }
216 }
217
218 /** Get the first connected producer service.
219 */
220
221 mlt_service mlt_service_get_producer( mlt_service this )
222 {
223 mlt_service producer = NULL;
224
225 // Get the service base
226 mlt_service_base *base = this->local;
227
228 if ( base->in != NULL )
229 producer = base->in[ 0 ];
230
231 return producer;
232 }
233
234 /** Default implementation of get_frame.
235 */
236
237 static int service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
238 {
239 mlt_service_base *base = this->local;
240 if ( index < base->count )
241 {
242 mlt_service producer = base->in[ index ];
243 if ( producer != NULL )
244 return mlt_service_get_frame( producer, frame, index );
245 }
246 *frame = mlt_frame_init( );
247 return 0;
248 }
249
250 /** Return the properties object.
251 */
252
253 mlt_properties mlt_service_properties( mlt_service self )
254 {
255 return self != NULL ? &self->parent : NULL;
256 }
257
258 /** Obtain a frame.
259 */
260
261 int mlt_service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
262 {
263 if ( this != NULL )
264 return this->get_frame( this, frame, index );
265 *frame = mlt_frame_init( );
266 return 0;
267 }
268
269 /** Close the service.
270 */
271
272 void mlt_service_close( mlt_service this )
273 {
274 if ( this != NULL && mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
275 {
276 if ( this->close != NULL )
277 {
278 this->close( this->close_object );
279 }
280 else
281 {
282 mlt_service_base *base = this->local;
283 int i = 0;
284 for ( i = 0; i < base->count; i ++ )
285 if ( base->in[ i ] != NULL )
286 mlt_service_close( base->in[ i ] );
287 free( base->in );
288 free( base );
289 this->parent.close = NULL;
290 mlt_properties_close( &this->parent );
291 }
292 }
293 }
294