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
);
56 static void mlt_service_property_changed( mlt_listener
, mlt_properties owner
, mlt_service
this, void **args
);
61 int mlt_service_init( mlt_service
this, void *child
)
65 // Initialise everything to NULL
66 memset( this, 0, sizeof( struct mlt_service_s
) );
71 // Generate local space
72 this->local
= calloc( sizeof( mlt_service_base
), 1 );
74 // Associate the methods
75 this->get_frame
= service_get_frame
;
77 // Initialise the properties
78 error
= mlt_properties_init( &this->parent
, this );
81 this->parent
.close
= ( mlt_destructor
)mlt_service_close
;
82 this->parent
.close_object
= this;
84 mlt_events_init( &this->parent
);
85 mlt_events_register( &this->parent
, "property-changed", ( mlt_transmitter
)mlt_service_property_changed
);
91 static void mlt_service_property_changed( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
93 if ( listener
!= NULL
)
94 listener( owner
, this, ( char * )args
[ 0 ] );
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
104 int mlt_service_connect_producer( mlt_service
this, mlt_service producer
, int index
)
108 // Get the service base
109 mlt_service_base
*base
= this->local
;
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
)
117 if ( index
>= base
->size
)
119 int new_size
= base
->size
+ index
+ 10;
120 base
->in
= realloc( base
->in
, new_size
* sizeof( mlt_service
) );
121 if ( base
->in
!= NULL
)
123 for ( i
= base
->size
; i
< new_size
; i
++ )
124 base
->in
[ i
] = NULL
;
125 base
->size
= new_size
;
129 // If we have space, assign the input
130 if ( base
->in
!= NULL
&& index
>= 0 && index
< base
->size
)
132 // Get the current service
133 mlt_service current
= base
->in
[ index
];
135 // Increment the reference count on this producer
136 if ( producer
!= NULL
)
137 mlt_properties_inc_ref( mlt_service_properties( producer
) );
139 // Now we disconnect the producer service from its consumer
140 mlt_service_disconnect( producer
);
142 // Add the service to index specified
143 base
->in
[ index
] = producer
;
145 // Determine the number of active tracks
146 if ( index
>= base
->count
)
147 base
->count
= index
+ 1;
149 // Now we connect the producer to its connected consumer
150 mlt_service_connect( producer
, this );
152 // Close the current service
153 mlt_service_close( current
);
155 // Inform caller that all went well
164 /** Disconnect this service from its consumer.
167 static void mlt_service_disconnect( mlt_service
this )
171 // Get the service base
172 mlt_service_base
*base
= this->local
;
179 /** Obtain the consumer this service is connected to.
182 mlt_service
mlt_service_consumer( mlt_service
this )
184 // Get the service base
185 mlt_service_base
*base
= this->local
;
187 // Return the connected consumer
191 /** Obtain the producer this service is connected to.
194 mlt_service
mlt_service_producer( mlt_service
this )
196 // Get the service base
197 mlt_service_base
*base
= this->local
;
199 // Return the connected producer
200 return base
->count
> 0 ? base
->in
[ base
->count
- 1 ] : NULL
;
203 /** Associate this service to the consumer.
206 static void mlt_service_connect( mlt_service
this, mlt_service that
)
210 // Get the service base
211 mlt_service_base
*base
= this->local
;
213 // There's a bit more required here...
218 /** Get the first connected producer service.
221 mlt_service
mlt_service_get_producer( mlt_service
this )
223 mlt_service producer
= NULL
;
225 // Get the service base
226 mlt_service_base
*base
= this->local
;
228 if ( base
->in
!= NULL
)
229 producer
= base
->in
[ 0 ];
234 /** Default implementation of get_frame.
237 static int service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
239 mlt_service_base
*base
= this->local
;
240 if ( index
< base
->count
)
242 mlt_service producer
= base
->in
[ index
];
243 if ( producer
!= NULL
)
244 return mlt_service_get_frame( producer
, frame
, index
);
246 *frame
= mlt_frame_init( );
250 /** Return the properties object.
253 mlt_properties
mlt_service_properties( mlt_service self
)
255 return self
!= NULL ?
&self
->parent
: NULL
;
261 int mlt_service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
264 return this->get_frame( this, frame
, index
);
265 *frame
= mlt_frame_init( );
269 /** Close the service.
272 void mlt_service_close( mlt_service
this )
274 if ( this != NULL
&& mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
276 if ( this->close
!= NULL
)
278 this->close( this->close_object
);
282 mlt_service_base
*base
= this->local
;
284 for ( i
= 0; i
< base
->count
; i
++ )
285 if ( base
->in
[ i
] != NULL
)
286 mlt_service_close( base
->in
[ i
] );
289 this->parent
.close
= NULL
;
290 mlt_properties_close( &this->parent
);