X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fframework%2Fmlt_repository.c;h=e5e5e79388458f3d31fd46f22edafb8d9fcf51a6;hb=8db690decb304a447407e30ea11f535ea2256dd7;hp=9e0b1a879e9f98e272a03eb7e15a573583b2abe2;hpb=ec7f7d58e000242e918c2ae2cebd172bffe6e9d4;p=mlt diff --git a/src/framework/mlt_repository.c b/src/framework/mlt_repository.c index 9e0b1a87..e5e5e793 100644 --- a/src/framework/mlt_repository.c +++ b/src/framework/mlt_repository.c @@ -1,198 +1,506 @@ -/* - * repository.c -- provides a map between service and shared objects - * Copyright (C) 2003-2004 Ushodaya Enterprises Limited - * Author: Charles Yates +/** + * \file mlt_repository.c + * \brief provides a map between service and shared objects + * \see mlt_repository_s * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * Copyright (C) 2003-2009 Ushodaya Enterprises Limited + * \author Charles Yates + * \author Dan Dennedy * - * This program is distributed in the hope that it will be useful, + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "mlt_repository.h" #include "mlt_properties.h" +#include "mlt_tokeniser.h" +#include "mlt_log.h" +#include "mlt_factory.h" #include #include #include #include +#include +#include +#include + +/** the default subdirectory of the datadir for holding presets */ +#define PRESETS_DIR "/presets" + +/** \brief Repository class + * + * The Repository is a collection of plugin modules and their services and service metadata. + * + * \extends mlt_properties_s + * \properties \p language a cached list of user locales + */ struct mlt_repository_s { - struct mlt_properties_s parent; + struct mlt_properties_s parent; /// a list of object files + mlt_properties consumers; /// a list of entry points for consumers + mlt_properties filters; /// a list of entry points for filters + mlt_properties producers; /// a list of entry points for producers + mlt_properties transitions; /// a list of entry points for transitions }; -static char *construct_full_file( char *output, char *prefix, char *file ) -{ - strcpy( output, prefix ); - if ( prefix[ strlen( prefix ) - 1 ] != '/' ) - strcat( output, "/" ); - strcat( output, file ); - return output; -} +/** Construct a new repository. + * + * \public \memberof mlt_repository_s + * \param directory the full path of a directory from which to read modules + * \return a new repository or NULL if failed + */ -static char *chomp( char *input ) +mlt_repository mlt_repository_init( const char *directory ) { - if ( input[ strlen( input ) - 1 ] == '\n' ) - input[ strlen( input ) - 1 ] = '\0'; - return input; + // Safety check + if ( directory == NULL || strcmp( directory, "" ) == 0 ) + return NULL; + + // Construct the repository + mlt_repository self = calloc( 1, sizeof( struct mlt_repository_s )); + mlt_properties_init( &self->parent, self ); + self->consumers = mlt_properties_new(); + self->filters = mlt_properties_new(); + self->producers = mlt_properties_new(); + self->transitions = mlt_properties_new(); + + // Get the directory list + mlt_properties dir = mlt_properties_new(); + int count = mlt_properties_dir_list( dir, directory, NULL, 0 ); + int i; + + // Iterate over files + for ( i = 0; i < count; i++ ) + { + int flags = RTLD_NOW; + const char *object_name = mlt_properties_get_value( dir, i); + + // Very temporary hack to allow the quicktime plugins to work + // TODO: extend repository to allow this to be used on a case by case basis + if ( strstr( object_name, "libmltkino" ) ) + flags |= RTLD_GLOBAL; + + // Open the shared object + void *object = dlopen( object_name, flags ); + if ( object != NULL ) + { + // Get the registration function + mlt_repository_callback symbol_ptr = dlsym( object, "mlt_register" ); + + // Call the registration function + if ( symbol_ptr != NULL ) + { + symbol_ptr( self ); + + // Register the object file for closure + mlt_properties_set_data( &self->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL ); + } + else + { + dlclose( object ); + } + } + else if ( strstr( object_name, "libmlt" ) ) + { + mlt_log( NULL, MLT_LOG_WARNING, "%s: failed to dlopen %s\n (%s)\n", __FUNCTION__, object_name, dlerror() ); + } + } + + mlt_properties_close( dir ); + + return self; } -static mlt_properties construct_object( char *prefix, char *id ) +/** Create a properties list for a service holding a function pointer to its constructor function. + * + * \private \memberof mlt_repository_s + * \param symbol a pointer to a function that can create the service. + * \return a properties list + */ + +static mlt_properties new_service( void *symbol ) { - mlt_properties output = mlt_properties_new( ); - mlt_properties_set( output, "prefix", prefix ); - mlt_properties_set( output, "id", id ); - return output; + mlt_properties properties = mlt_properties_new(); + mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL ); + return properties; } -static mlt_properties construct_service( mlt_properties object, char *id ) +/** Register a service with the repository. + * + * Typically, this is invoked by a module within its mlt_register(). + * + * \public \memberof mlt_repository_s + * \param self a repository + * \param service_type a service class + * \param service the name of a service + * \param symbol a pointer to a function to create the service + */ + +void mlt_repository_register( mlt_repository self, mlt_service_type service_type, const char *service, mlt_register_callback symbol ) { - mlt_properties output = mlt_properties_new( ); - mlt_properties_set_data( output, "object", object, 0, NULL, NULL ); - mlt_properties_set( output, "id", id ); - return output; + // Add the entry point to the corresponding service list + switch ( service_type ) + { + case consumer_type: + mlt_properties_set_data( self->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL ); + break; + case filter_type: + mlt_properties_set_data( self->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL ); + break; + case producer_type: + mlt_properties_set_data( self->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL ); + break; + case transition_type: + mlt_properties_set_data( self->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL ); + break; + default: + break; + } } -static void *construct_instance( mlt_properties service_properties, char *symbol, void *input ) -{ - // Extract the service - char *service = mlt_properties_get( service_properties, "id" ); +/** Get the repository properties for particular service class. + * + * \private \memberof mlt_repository_s + * \param self a repository + * \param type a service class + * \param service the name of a service + * \return a properties list or NULL if error + */ - // Get the object properties - void *object_properties = mlt_properties_get_data( service_properties, "object", NULL ); +static mlt_properties get_service_properties( mlt_repository self, mlt_service_type type, const char *service ) +{ + mlt_properties service_properties = NULL; - // Get the dlopen'd object - void *object = mlt_properties_get_data( object_properties, "dlopen", NULL ); + // Get the entry point from the corresponding service list + switch ( type ) + { + case consumer_type: + service_properties = mlt_properties_get_data( self->consumers, service, NULL ); + break; + case filter_type: + service_properties = mlt_properties_get_data( self->filters, service, NULL ); + break; + case producer_type: + service_properties = mlt_properties_get_data( self->producers, service, NULL ); + break; + case transition_type: + service_properties = mlt_properties_get_data( self->transitions, service, NULL ); + break; + default: + break; + } + return service_properties; +} - // Get the dlsym'd symbol - void *( *symbol_ptr )( char *, void * ) = mlt_properties_get_data( object_properties, symbol, NULL ); +/** Construct a new instance of a service. + * + * \public \memberof mlt_repository_s + * \param self a repository + * \param profile a \p mlt_profile to give the service + * \param type a service class + * \param service the name of the service + * \param input an optional argument to the service constructor + */ - // Check that we have object and open if we don't - if ( object == NULL ) +void *mlt_repository_create( mlt_repository self, mlt_profile profile, mlt_service_type type, const char *service, const void *input ) +{ + mlt_properties properties = get_service_properties( self, type, service ); + if ( properties != NULL ) { - char full_file[ 512 ]; + mlt_register_callback symbol_ptr = mlt_properties_get_data( properties, "symbol", NULL ); - // Get the prefix and id of the shared object - char *prefix = mlt_properties_get( object_properties, "prefix" ); - char *file = mlt_properties_get( object_properties, "id" ); + // Construct the service + return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL; + } + return NULL; +} - // Construct the full file - construct_full_file( full_file, prefix, file ); +/** Destroy a repository and free its resources. + * + * \public \memberof mlt_repository_s + * \param self a repository + */ - // Open the shared object - object = dlopen( full_file, RTLD_NOW | RTLD_GLOBAL ); - if ( object == NULL ) - fprintf( stderr, "Failed to load plugin: %s\n", dlerror() ); +void mlt_repository_close( mlt_repository self ) +{ + mlt_properties_close( self->consumers ); + mlt_properties_close( self->filters ); + mlt_properties_close( self->producers ); + mlt_properties_close( self->transitions ); + mlt_properties_close( &self->parent ); + free( self ); +} - // Set it on the properties - mlt_properties_set_data( object_properties, "dlopen", object, 0, ( mlt_destructor )dlclose, NULL ); - } +/** Get the list of registered consumers. + * + * \public \memberof mlt_repository_s + * \param self a repository + * \return a properties list containing all of the consumers + */ - // Now check if we have this symbol pointer - if ( object != NULL && symbol_ptr == NULL ) - { - // Construct it now - symbol_ptr = dlsym( object, symbol ); +mlt_properties mlt_repository_consumers( mlt_repository self ) +{ + return self->consumers; +} - // Set it on the properties - mlt_properties_set_data( object_properties, "dlsym", symbol_ptr, 0, NULL, NULL ); - } +/** Get the list of registered filters. + * + * \public \memberof mlt_repository_s + * \param self a repository + * \return a properties list of all of the filters + */ + +mlt_properties mlt_repository_filters( mlt_repository self ) +{ + return self->filters; +} - // Construct the service - return symbol_ptr != NULL ? symbol_ptr( service, input ) : NULL; +/** Get the list of registered producers. + * + * \public \memberof mlt_repository_s + * \param self a repository + * \return a properties list of all of the producers + */ + +mlt_properties mlt_repository_producers( mlt_repository self ) +{ + return self->producers; } -mlt_repository mlt_repository_init( mlt_properties object_list, char *prefix, char *data, char *symbol ) +/** Get the list of registered transitions. + * + * \public \memberof mlt_repository_s + * \param self a repository + * \return a properties list of all of the transitions + */ + +mlt_properties mlt_repository_transitions( mlt_repository self ) { - char full_file[ 512 ]; - FILE *file; + return self->transitions; +} - // Construct the repository - mlt_repository this = calloc( sizeof( struct mlt_repository_s ), 1 ); - mlt_properties_init( &this->parent, this ); +/** Register the metadata for a service. + * + * IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties + * that you supply! + * + * \public \memberof mlt_repository_s + * \param self a repository + * \param type a service class + * \param service the name of a service + * \param callback the pointer to a function that can supply metadata + * \param callback_data an opaque user data pointer to be supplied on the callback + */ - // Add the symbol to THIS repository properties. - mlt_properties_set( &this->parent, "_symbol", symbol ); +void mlt_repository_register_metadata( mlt_repository self, mlt_service_type type, const char *service, mlt_metadata_callback callback, void *callback_data ) +{ + mlt_properties service_properties = get_service_properties( self, type, service ); + mlt_properties_set_data( service_properties, "metadata_cb", callback, 0, NULL, NULL ); + mlt_properties_set_data( service_properties, "metadata_cb_data", callback_data, 0, NULL, NULL ); +} - // Construct full file - construct_full_file( full_file, prefix, data ); +/** Get the metadata about a service. + * + * Returns NULL if service or its metadata are unavailable. + * + * \public \memberof mlt_repository_s + * \param self a repository + * \param type a service class + * \param service the name of a service + * \return the service metadata as a structured properties list + */ - // Open the file - file = fopen( full_file, "r" ); +mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service ) +{ + mlt_properties metadata = NULL; + mlt_properties properties = get_service_properties( self, type, service ); - // Parse the contents - if ( file != NULL ) + // If this is a valid service + if ( properties ) { - char full[ 512 ]; - char service[ 256 ]; - char object[ 256 ]; - - while( fgets( full, 512, file ) ) + // Lookup cached metadata + metadata = mlt_properties_get_data( properties, "metadata", NULL ); + if ( ! metadata ) { - chomp( full ); + // Not cached, so get the registered metadata callback function + mlt_metadata_callback callback = mlt_properties_get_data( properties, "metadata_cb", NULL ); - if ( full[ 0 ] != '#' && full[ 0 ] != '\0' && sscanf( full, "%s %s", service, object ) == 2 ) + // If a metadata callback function is registered + if ( callback ) { - // Get the object properties first - mlt_properties object_properties = mlt_properties_get_data( object_list, object, NULL ); + // Fetch the callback data arg + void *data = mlt_properties_get_data( properties, "metadata_cb_data", NULL ); - // If their are no properties, create them now - if ( object_properties == NULL ) - { - // Construct the object - object_properties = construct_object( prefix, object ); + // Fetch the metadata through the callback + metadata = callback( type, service, data ); - // Add it to the object list - mlt_properties_set_data( object_list, object, object_properties, 0, ( mlt_destructor )mlt_properties_close, NULL ); - } + // Cache the metadata + if ( metadata ) + // Include dellocation and serialisation + mlt_properties_set_data( properties, "metadata", metadata, 0, ( mlt_destructor )mlt_properties_close, ( mlt_serialiser )mlt_properties_serialise_yaml ); + } + } + } + return metadata; +} - // Now construct a property for the service - mlt_properties service_properties = construct_service( object_properties, service ); +/** Try to determine the locale from some commonly used environment variables. + * + * \private \memberof mlt_repository_s + * \return a string containing the locale id or NULL if unknown + */ + +static char *getenv_locale() +{ + char *s = getenv( "LANGUAGE" ); + if ( s && s[0] ) + return s; + s = getenv( "LC_ALL" ); + if ( s && s[0] ) + return s; + s = getenv( "LC_MESSAGES" ); + if ( s && s[0] ) + return s; + s = getenv( "LANG" ); + if ( s && s[0] ) + return s; + return NULL; +} - // Add it to the repository - mlt_properties_set_data( &this->parent, service, service_properties, 0, ( mlt_destructor )mlt_properties_close, NULL ); +/** Return a list of user-preferred language codes taken from environment variables. + * + * A module should use this to locate a localized YAML Tiny file from which to build + * its metadata strucutured properties. + * + * \public \memberof mlt_repository_s + * \param self a repository + * \return a properties list that is a list (not a map) of locales, defaults to "en" if not + * overridden by environment variables, in order: LANGUAGE, LC_ALL, LC_MESSAGES, LANG + */ + +mlt_properties mlt_repository_languages( mlt_repository self ) +{ + mlt_properties languages = mlt_properties_get_data( &self->parent, "languages", NULL ); + if ( languages ) + return languages; + + languages = mlt_properties_new(); + char *locale = getenv_locale(); + if ( locale ) + { + locale = strdup( locale ); + mlt_tokeniser tokeniser = mlt_tokeniser_init(); + int count = mlt_tokeniser_parse_new( tokeniser, locale, ":" ); + if ( count ) + { + int i; + for ( i = 0; i < count; i++ ) + { + char *locale = mlt_tokeniser_get_string( tokeniser, i ); + if ( strcmp( locale, "C" ) == 0 || strcmp( locale, "POSIX" ) == 0 ) + locale = "en"; + else if ( strlen( locale ) > 2 ) + locale[2] = 0; + char string[21]; + snprintf( string, sizeof(string), "%d", i ); + mlt_properties_set( languages, string, locale ); } } - - // Close the file - fclose( file ); + else + { + mlt_properties_set( languages, "0", "en" ); + } + free( locale ); + mlt_tokeniser_close( tokeniser ); } - - return this; + else + { + mlt_properties_set( languages, "0", "en" ); + } + mlt_properties_set_data( &self->parent, "languages", languages, 0, ( mlt_destructor )mlt_properties_close, NULL ); + return languages; } -void *mlt_repository_fetch( mlt_repository this, char *service, void *input ) +static void list_presets( mlt_properties properties, const char *path, const char *dirname ) { - // Get the service properties - mlt_properties service_properties = mlt_properties_get_data( &this->parent, service, NULL ); + DIR *dir = opendir( dirname ); - // If the service exists - if ( service_properties != NULL ) + if ( dir ) { - // Get the symbol that is used to generate this service - char *symbol = mlt_properties_get( &this->parent, "_symbol" ); + struct dirent *de = readdir( dir ); + char fullname[ PATH_MAX ]; - // Now get an instance of the service - return construct_instance( service_properties, symbol, input ); - } + while ( de != NULL ) + { + if ( de->d_name[0] != '.' && de->d_name[strlen( de->d_name ) - 1] != '~' ) + { + struct stat info; - return NULL; + snprintf( fullname, sizeof(fullname), "%s/%s", dirname, de->d_name ); + stat( fullname, &info ); + if ( S_ISDIR( info.st_mode ) ) + { + // recurse into subdirectories + char sub[ PATH_MAX ]; + if ( path ) + snprintf( sub, sizeof(sub), "%s/%s", path, de->d_name ); + else + strncpy( sub, de->d_name, sizeof(sub) ); + list_presets( properties, sub, fullname ); + } + else + { + // load the preset + mlt_properties preset = mlt_properties_load( fullname ); + if ( preset && mlt_properties_count( preset ) ) + { + snprintf( fullname, 1024, "%s/%s", path, de->d_name ); + mlt_properties_set_data( properties, fullname, preset, 0, (mlt_destructor) mlt_properties_close, NULL ); + } + } + } + de = readdir( dir ); + } + closedir( dir ); + } } -void mlt_repository_close( mlt_repository this ) +/** Get the list of presets. + * + * \public \memberof mlt_repository_s + * \return a properties list of all the presets + */ + +mlt_properties mlt_repository_presets( ) { - mlt_properties_close( &this->parent ); - free( this ); -} + mlt_properties result = mlt_properties_new(); + char *path = getenv( "MLT_PRESETS_PATH" ); + if ( path ) + { + path = strdup( path ); + } + else + { + path = malloc( strlen( mlt_environment( "MLT_DATA" ) ) + strlen( PRESETS_DIR ) + 1 ); + strcpy( path, mlt_environment( "MLT_DATA" ) ); + strcat( path, PRESETS_DIR ); + } + list_presets( result, NULL, path ); + free( path ); + return result; +}