]> git.sesse.net Git - mlt/blob - src/framework/mlt_service.c
Mlt Ref Counts and Playlist split/join
[mlt] / 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
57 /** Constructor
58 */
59
60 int mlt_service_init( mlt_service this, void *child )
61 {
62         int error = 0;
63
64         // Initialise everything to NULL
65         memset( this, 0, sizeof( struct mlt_service_s ) );
66
67         // Assign the child
68         this->child = child;
69
70         // Generate local space
71         this->local = calloc( sizeof( mlt_service_base ), 1 );
72
73         // Associate the methods
74         this->get_frame = service_get_frame;
75         
76         // Initialise the properties
77         error = mlt_properties_init( &this->parent, this );
78         if ( error == 0 )
79         {
80                 this->parent.close = ( mlt_destructor )mlt_service_close;
81                 this->parent.close_object = this;
82         }
83
84         return error;
85 }
86
87 /** Connect a producer service.
88         Returns: > 0 warning, == 0 success, < 0 serious error
89                          1 = this service does not accept input
90                          2 = the producer is invalid
91                          3 = the producer is already registered with this consumer
92 */
93
94 int mlt_service_connect_producer( mlt_service this, mlt_service producer, int index )
95 {
96         int i = 0;
97
98         // Get the service base
99         mlt_service_base *base = this->local;
100
101         // Check if the producer is already registered with this service
102         for ( i = 0; i < base->count; i ++ )
103                 if ( base->in[ i ] == producer )
104                         return 3;
105
106         // Allocate space
107         if ( index >= base->size )
108         {
109                 int new_size = base->size + index + 10;
110                 base->in = realloc( base->in, new_size * sizeof( mlt_service ) );
111                 if ( base->in != NULL )
112                 {
113                         for ( i = base->size; i < new_size; i ++ )
114                                 base->in[ i ] = NULL;
115                         base->size = new_size;
116                 }
117         }
118
119         // If we have space, assign the input
120         if ( base->in != NULL && index >= 0 && index < base->size )
121         {
122                 // Get the current service
123                 mlt_service current = base->in[ index ];
124
125                 // Increment the reference count on this producer
126                 if ( producer != NULL )
127                         mlt_properties_inc_ref( mlt_service_properties( producer ) );
128
129                 // Now we disconnect the producer service from its consumer
130                 mlt_service_disconnect( producer );
131                 
132                 // Add the service to index specified
133                 base->in[ index ] = producer;
134                 
135                 // Determine the number of active tracks
136                 if ( index >= base->count )
137                         base->count = index + 1;
138
139                 // Now we connect the producer to its connected consumer
140                 mlt_service_connect( producer, this );
141
142                 // Close the current service
143                 mlt_service_close( current );
144
145                 // Inform caller that all went well
146                 return 0;
147         }
148         else
149         {
150                 return -1;
151         }
152 }
153
154 /** Disconnect this service from its consumer.
155 */
156
157 static void mlt_service_disconnect( mlt_service this )
158 {
159         if ( this != NULL )
160         {
161                 // Get the service base
162                 mlt_service_base *base = this->local;
163
164                 // Disconnect
165                 base->out = NULL;
166         }
167 }
168
169 /** Obtain the consumer this service is connected to.
170 */
171
172 mlt_service mlt_service_consumer( mlt_service this )
173 {
174         // Get the service base
175         mlt_service_base *base = this->local;
176
177         // Return the connected consumer
178         return base->out;
179 }
180
181 /** Obtain the producer this service is connected to.
182 */
183
184 mlt_service mlt_service_producer( mlt_service this )
185 {
186         // Get the service base
187         mlt_service_base *base = this->local;
188
189         // Return the connected producer
190         return base->count > 0 ? base->in[ base->count - 1 ] : NULL;
191 }
192
193 /** Associate this service to the consumer.
194 */
195
196 static void mlt_service_connect( mlt_service this, mlt_service that )
197 {
198         if ( this != NULL )
199         {
200                 // Get the service base
201                 mlt_service_base *base = this->local;
202
203                 // There's a bit more required here...
204                 base->out = that;
205         }
206 }
207
208 /** Get the first connected producer service.
209 */
210
211 mlt_service mlt_service_get_producer( mlt_service this )
212 {
213         mlt_service producer = NULL;
214
215         // Get the service base
216         mlt_service_base *base = this->local;
217
218         if ( base->in != NULL )
219                 producer = base->in[ 0 ];
220         
221         return producer;
222 }
223
224 /** Default implementation of get_frame.
225 */
226
227 static int service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
228 {
229         mlt_service_base *base = this->local;
230         if ( index < base->count )
231         {
232                 mlt_service producer = base->in[ index ];
233                 if ( producer != NULL )
234                         return mlt_service_get_frame( producer, frame, index );
235         }
236         *frame = mlt_frame_init( );
237         return 0;
238 }
239
240 /** Return the properties object.
241 */
242
243 mlt_properties mlt_service_properties( mlt_service self )
244 {
245         return self != NULL ? &self->parent : NULL;
246 }
247
248 /** Obtain a frame.
249 */
250
251 int mlt_service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
252 {
253         if ( this != NULL )
254                 return this->get_frame( this, frame, index );
255         *frame = mlt_frame_init( );
256         return 0;
257 }
258
259 /** Close the service.
260 */
261
262 void mlt_service_close( mlt_service this )
263 {
264         if ( this != NULL && mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
265         {
266                 if ( this->close != NULL )
267                 {
268                         this->close( this->close_object );
269                 }
270                 else
271                 {
272                         mlt_service_base *base = this->local;
273                         int i = 0;
274                         for ( i = 0; i < base->count; i ++ )
275                                 if ( base->in[ i ] != NULL )
276                                         mlt_service_close( base->in[ i ] );
277                         free( base->in );
278                         free( base );
279                         this->parent.close = NULL;
280                         mlt_properties_close( &this->parent );
281                 }
282         }
283 }
284