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>
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.
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.
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.
22 #include "mlt_service.h"
23 #include "mlt_frame.h"
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.
34 PLEASE DO NOT CHANGE THIS BEHAVIOUR!!! OVERRIDE THE METHODS THAT
35 CONTROL THIS IN EXTENDING CLASSES.
38 /** Private service definition.
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
);
60 int mlt_service_init( mlt_service
this, void *child
)
64 // Initialise everything to NULL
65 memset( this, 0, sizeof( struct mlt_service_s
) );
70 // Generate local space
71 this->local
= calloc( sizeof( mlt_service_base
), 1 );
73 // Associate the methods
74 this->get_frame
= service_get_frame
;
76 // Initialise the properties
77 error
= mlt_properties_init( &this->parent
, this );
80 this->parent
.close
= ( mlt_destructor
)mlt_service_close
;
81 this->parent
.close_object
= this;
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
94 int mlt_service_connect_producer( mlt_service
this, mlt_service producer
, int index
)
98 // Get the service base
99 mlt_service_base
*base
= this->local
;
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
)
107 if ( index
>= base
->size
)
109 int new_size
= base
->size
+ index
+ 10;
110 base
->in
= realloc( base
->in
, new_size
* sizeof( mlt_service
) );
111 if ( base
->in
!= NULL
)
113 for ( i
= base
->size
; i
< new_size
; i
++ )
114 base
->in
[ i
] = NULL
;
115 base
->size
= new_size
;
119 // If we have space, assign the input
120 if ( base
->in
!= NULL
&& index
>= 0 && index
< base
->size
)
122 // Get the current service
123 mlt_service current
= base
->in
[ index
];
125 // Increment the reference count on this producer
126 if ( producer
!= NULL
)
127 mlt_properties_inc_ref( mlt_service_properties( producer
) );
129 // Now we disconnect the producer service from its consumer
130 mlt_service_disconnect( producer
);
132 // Add the service to index specified
133 base
->in
[ index
] = producer
;
135 // Determine the number of active tracks
136 if ( index
>= base
->count
)
137 base
->count
= index
+ 1;
139 // Now we connect the producer to its connected consumer
140 mlt_service_connect( producer
, this );
142 // Close the current service
143 mlt_service_close( current
);
145 // Inform caller that all went well
154 /** Disconnect this service from its consumer.
157 static void mlt_service_disconnect( mlt_service
this )
161 // Get the service base
162 mlt_service_base
*base
= this->local
;
169 /** Obtain the consumer this service is connected to.
172 mlt_service
mlt_service_consumer( mlt_service
this )
174 // Get the service base
175 mlt_service_base
*base
= this->local
;
177 // Return the connected consumer
181 /** Obtain the producer this service is connected to.
184 mlt_service
mlt_service_producer( mlt_service
this )
186 // Get the service base
187 mlt_service_base
*base
= this->local
;
189 // Return the connected producer
190 return base
->count
> 0 ? base
->in
[ base
->count
- 1 ] : NULL
;
193 /** Associate this service to the consumer.
196 static void mlt_service_connect( mlt_service
this, mlt_service that
)
200 // Get the service base
201 mlt_service_base
*base
= this->local
;
203 // There's a bit more required here...
208 /** Get the first connected producer service.
211 mlt_service
mlt_service_get_producer( mlt_service
this )
213 mlt_service producer
= NULL
;
215 // Get the service base
216 mlt_service_base
*base
= this->local
;
218 if ( base
->in
!= NULL
)
219 producer
= base
->in
[ 0 ];
224 /** Default implementation of get_frame.
227 static int service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
229 mlt_service_base
*base
= this->local
;
230 if ( index
< base
->count
)
232 mlt_service producer
= base
->in
[ index
];
233 if ( producer
!= NULL
)
234 return mlt_service_get_frame( producer
, frame
, index
);
236 *frame
= mlt_frame_init( );
240 /** Return the properties object.
243 mlt_properties
mlt_service_properties( mlt_service self
)
245 return self
!= NULL ?
&self
->parent
: NULL
;
251 int mlt_service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
254 return this->get_frame( this, frame
, index
);
255 *frame
= mlt_frame_init( );
259 /** Close the service.
262 void mlt_service_close( mlt_service
this )
264 if ( this != NULL
&& mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
266 if ( this->close
!= NULL
)
268 this->close( this->close_object
);
272 mlt_service_base
*base
= this->local
;
274 for ( i
= 0; i
< base
->count
; i
++ )
275 if ( base
->in
[ i
] != NULL
)
276 mlt_service_close( base
->in
[ i
] );
279 this->parent
.close
= NULL
;
280 mlt_properties_close( &this->parent
);