dcd0742fef7c14728da16616f6596e367205f177
[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
57 /** Constructor
58 */
59
60 int mlt_service_init( mlt_service this, void *child )
61 {
62 // Initialise everything to NULL
63 memset( this, 0, sizeof( struct mlt_service_s ) );
64
65 // Assign the child
66 this->child = child;
67
68 // Generate local space
69 this->local = calloc( sizeof( mlt_service_base ), 1 );
70
71 // Associate the methods
72 this->get_frame = service_get_frame;
73
74 // Initialise the properties
75 return mlt_properties_init( &this->parent, this );
76 }
77
78 /** Connect a producer service.
79 Returns: > 0 warning, == 0 success, < 0 serious error
80 1 = this service does not accept input
81 2 = the producer is invalid
82 3 = the producer is already registered with this consumer
83 */
84
85 int mlt_service_connect_producer( mlt_service this, mlt_service producer, int index )
86 {
87 int i = 0;
88
89 // Get the service base
90 mlt_service_base *base = this->local;
91
92 // Check if the producer is already registered with this service
93 for ( i = 0; i < base->count; i ++ )
94 if ( base->in[ i ] == producer )
95 return 3;
96
97 // Allocate space
98 if ( index >= base->size )
99 {
100 int new_size = base->size + index + 10;
101 base->in = realloc( base->in, new_size * sizeof( mlt_service ) );
102 if ( base->in != NULL )
103 {
104 for ( i = base->size; i < new_size; i ++ )
105 base->in[ i ] = NULL;
106 base->size = new_size;
107 }
108 }
109
110 // If we have space, assign the input
111 if ( base->in != NULL && index >= 0 && index < base->size )
112 {
113 // Now we disconnect the producer service from its consumer
114 mlt_service_disconnect( producer );
115
116 // Add the service to index specified
117 base->in[ index ] = producer;
118
119 // Determine the number of active tracks
120 if ( index >= base->count )
121 base->count = index + 1;
122
123 // Now we connect the producer to its connected consumer
124 mlt_service_connect( producer, this );
125
126 // Inform caller that all went well
127 return 0;
128 }
129 else
130 {
131 return -1;
132 }
133 }
134
135 /** Disconnect this service from its consumer.
136 */
137
138 static void mlt_service_disconnect( mlt_service this )
139 {
140 // Get the service base
141 mlt_service_base *base = this->local;
142
143 // There's a bit more required here...
144 base->out = NULL;
145 }
146
147 /** Associate this service to the its consumer.
148 */
149
150 static void mlt_service_connect( mlt_service this, mlt_service that )
151 {
152 // Get the service base
153 mlt_service_base *base = this->local;
154
155 // There's a bit more required here...
156 base->out = that;
157 }
158
159 /** Get the first connected producer service.
160 */
161
162 mlt_service mlt_service_get_producer( mlt_service this )
163 {
164 mlt_service producer = NULL;
165
166 // Get the service base
167 mlt_service_base *base = this->local;
168
169 if ( base->in != NULL )
170 producer = base->in[ 0 ];
171
172 return producer;
173 }
174
175 /** Default implementation of get_frame.
176 */
177
178 static int service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
179 {
180 mlt_service_base *base = this->local;
181 if ( index < base->count )
182 {
183 mlt_service producer = base->in[ index ];
184 if ( producer != NULL )
185 return mlt_service_get_frame( producer, frame, index );
186 }
187 *frame = mlt_frame_init( );
188 return 0;
189 }
190
191 /** Return the properties object.
192 */
193
194 mlt_properties mlt_service_properties( mlt_service self )
195 {
196 return &self->parent;
197 }
198
199 /** Obtain a frame.
200 */
201
202 int mlt_service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
203 {
204 return this->get_frame( this, frame, index );
205 }
206
207 /** Close the service.
208 */
209
210 void mlt_service_close( mlt_service this )
211 {
212 mlt_service_base *base = this->local;
213 free( base->in );
214 free( base );
215 mlt_properties_close( &this->parent );
216 }
217