Commit a77e2404 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

simplify service discoveries

By the way, the event management seems quite broken. Events may occur
before _Create returns (even before this commit)...
parent a3fc39d8
......@@ -50,7 +50,6 @@ struct services_discovery_t
vlc_event_manager_t event_manager; /* Accessed through Setters for non class function */
services_discovery_sys_t *p_sys;
void (*pf_run) ( services_discovery_t *);
};
......@@ -67,8 +66,6 @@ VLC_EXPORT( char **, __services_discovery_GetServicesNames, ( vlc_object_t * p_s
/* Creation of a service_discovery object */
VLC_EXPORT( services_discovery_t *, services_discovery_Create, ( vlc_object_t * p_super, const char * psz_service_name ) );
VLC_EXPORT( void, services_discovery_Destroy, ( services_discovery_t * p_this ) );
VLC_EXPORT( int, services_discovery_Start, ( services_discovery_t * p_this ) );
VLC_EXPORT( void, services_discovery_Stop, ( services_discovery_t * p_this ) );
/* Read info from discovery object */
VLC_EXPORT( char *, services_discovery_GetLocalizedName, ( services_discovery_t * p_this ) );
......
......@@ -59,13 +59,13 @@ struct udi_input_id_t
struct services_discovery_sys_t
{
vlc_thread_t thread;
LibHalContext *p_ctx;
DBusConnection *p_connection;
int i_devices_number;
struct udi_input_id_t **pp_devices;
};
static void Run ( services_discovery_t *p_intf );
static void *Run ( void * );
static int Open ( vlc_object_t * );
static void Close( vlc_object_t * );
......@@ -108,7 +108,6 @@ static int Open( vlc_object_t *p_this )
p_sys->i_devices_number = 0;
p_sys->pp_devices = NULL;
p_sd->pf_run = Run;
p_sd->p_sys = p_sys;
dbus_error_init( &dbus_error );
......@@ -133,23 +132,26 @@ static int Open( vlc_object_t *p_this )
if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
{
msg_Err( p_sd, "hal not available : %s", dbus_error.message );
dbus_error_free( &dbus_error );
free( p_sys );
return VLC_EGENERIC;
goto error;
}
if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
!libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
{
msg_Err( p_sd, "unable to add callback" );
dbus_error_free( &dbus_error );
free( p_sys );
return VLC_EGENERIC;
goto error;
}
if( vlc_clone( &p_sys->thread, Run, p_this, VLC_THREAD_PRIORITY_LOW ) )
goto error;
services_discovery_SetLocalizedName( p_sd, _("Devices") );
return VLC_SUCCESS;
error:
dbus_error_free( &dbus_error );
free( p_sys );
return VLC_EGENERIC;
}
/*****************************************************************************
......@@ -160,6 +162,9 @@ static void Close( vlc_object_t *p_this )
services_discovery_t *p_sd = ( services_discovery_t* )p_this;
services_discovery_sys_t *p_sys = p_sd->p_sys;
/*vlc_cancel( p_sys->thread );*/
vlc_object_kill( p_sd );
vlc_join( p_sys->thread, NULL );
dbus_connection_unref( p_sys->p_connection );
struct udi_input_id_t *p_udi_entry;
......@@ -295,11 +300,12 @@ static void ParseDevice( services_discovery_t *p_sd, const char *psz_device )
/*****************************************************************************
* Run: main HAL thread
*****************************************************************************/
static void Run( services_discovery_t *p_sd )
static void *Run( voidt *data )
{
int i, i_devices;
services_discovery_t *p_sd = data;
services_discovery_sys_t *p_sys = p_sd->p_sys;
char **devices;
services_discovery_sys_t *p_sys = p_sd->p_sys;
int i, i_devices;
int canc = vlc_savecancel();
/* parse existing devices first */
......@@ -321,6 +327,7 @@ static void Run( services_discovery_t *p_sd )
/* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
}
vlc_restorecancel (canc);
return NULL;
}
void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
......
......@@ -89,6 +89,7 @@ struct services_discovery_sys_t
char **ppsz_urls;
int i_urls;
vlc_thread_t thread;
vlc_mutex_t lock;
vlc_cond_t wait;
bool b_update;
......@@ -97,7 +98,7 @@ struct services_discovery_sys_t
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static void Run( services_discovery_t *p_intf );
static void *Run( void * );
static int UrlsChange( vlc_object_t *, char const *, vlc_value_t,
vlc_value_t, void * );
static void ParseUrls( services_discovery_t *p_sd, char *psz_urls );
......@@ -121,9 +122,7 @@ static int Open( vlc_object_t *p_this )
vlc_cond_init( &p_sys->wait );
p_sys->b_update = true;
p_sd->pf_run = Run;
p_sd->p_sys = p_sys;
/* Give us a name */
services_discovery_SetLocalizedName( p_sd, _("Podcasts") );
......@@ -131,6 +130,11 @@ static int Open( vlc_object_t *p_this )
var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
{
free (p_sys);
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
......@@ -143,6 +147,9 @@ static void Close( vlc_object_t *p_this )
services_discovery_sys_t *p_sys = p_sd->p_sys;
int i;
vlc_cancel (p_sys->thread);
vlc_join (p_sys->thread, NULL);
var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys );
vlc_cond_destroy( &p_sys->wait );
vlc_mutex_destroy( &p_sys->lock );
......@@ -165,8 +172,9 @@ static void Close( vlc_object_t *p_this )
/*****************************************************************************
* Run: main thread
*****************************************************************************/
static void Run( services_discovery_t *p_sd )
static void *Run( void *data )
{
services_discovery_t *p_sd = data;
services_discovery_sys_t *p_sys = p_sd->p_sys;
vlc_mutex_lock( &p_sys->lock );
......
......@@ -232,6 +232,8 @@ struct sap_announce_t
struct services_discovery_sys_t
{
vlc_thread_t thread;
/* Socket descriptors */
int i_fd;
int *pi_fd;
......@@ -261,7 +263,7 @@ struct demux_sys_t
/* Main functions */
static int Demux( demux_t *p_demux );
static int Control( demux_t *, int, va_list );
static void Run ( services_discovery_t *p_sd );
static void *Run ( void *p_sd );
/* Main parsing functions */
static int ParseConnection( vlc_object_t *p_obj, sdp_t *p_sdp );
......@@ -300,7 +302,6 @@ static int Open( vlc_object_t *p_this )
p_sys->i_timeout = var_CreateGetInteger( p_sd, "sap-timeout" );
p_sd->pf_run = Run;
p_sd->p_sys = p_sys;
p_sys->pi_fd = NULL;
......@@ -324,6 +325,12 @@ static int Open( vlc_object_t *p_this )
p_sys->i_announces = 0;
p_sys->pp_announces = NULL;
/* TODO: create sockets here, and fix racy sockets table */
if (vlc_clone (&p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
{
free (p_sys);
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
......@@ -431,9 +438,11 @@ static void Close( vlc_object_t *p_this )
{
services_discovery_t *p_sd = ( services_discovery_t* )p_this;
services_discovery_sys_t *p_sys = p_sd->p_sys;
int i;
vlc_cancel (p_sys->thread);
vlc_join (p_sys->thread, NULL);
for( i = p_sys->i_fd-1 ; i >= 0 ; i-- )
{
net_Close( p_sys->pi_fd[i] );
......@@ -476,8 +485,9 @@ static void CloseDemux( vlc_object_t *p_this )
*****************************************************************************/
#define MAX_SAP_BUFFER 5000
static void Run( services_discovery_t *p_sd )
static void *Run( void *data )
{
services_discovery_t *p_sd = data;
char *psz_addr;
int i;
int timeout = -1;
......@@ -554,7 +564,7 @@ static void Run( services_discovery_t *p_sd )
if( p_sd->p_sys->i_fd == 0 )
{
msg_Err( p_sd, "unable to listen on any address" );
return;
return NULL;
}
/* read SAP packets */
......@@ -631,6 +641,7 @@ static void Run( services_discovery_t *p_sd )
else if( timeout < 200 )
timeout = 200; /* Don't wakeup too fast. */
}
assert (0);
}
/**********************************************************************
......
......@@ -140,8 +140,12 @@ vlc_module_end();
* Local prototypes
*****************************************************************************/
static void Run( services_discovery_t *p_sd );
static void *Run( void * );
struct services_discovery_sys_t
{
vlc_thread_t thread;
enum type_e i_type;
};
/*****************************************************************************
* Open: initialize and create stuff
......@@ -150,8 +154,17 @@ static int Open( vlc_object_t *p_this, enum type_e i_type )
{
services_discovery_t *p_sd = ( services_discovery_t* )p_this;
services_discovery_SetLocalizedName( p_sd, _(p_items[i_type].psz_name) );
p_sd->pf_run = Run;
p_sd->p_sys = (void *)i_type;
p_sd->p_sys = malloc (sizeof (*(p_sd->p_sys)));
if (p_sd->p_sys == NULL)
return VLC_ENOMEM;
p_sd->p_sys->i_type = i_type;
if (vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
{
free (p_sd->p_sys);
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
......@@ -211,16 +224,19 @@ static void AddSubitemsOfShoutItemURL( services_discovery_t *p_sd,
/*****************************************************************************
* Run:
*****************************************************************************/
static void Run( services_discovery_t *p_sd )
static void *Run( void *data )
{
enum type_e i_type = (enum type_e)p_sd->p_sys;
services_discovery_t *p_sd = data;
services_discovery_sys_t *p_sys = p_sd->p_sys;
enum type_e i_type = p_sys->i_type;
int i, j;
int canc = vlc_savecancel();
if( !p_items[i_type].p_children )
{
AddSubitemsOfShoutItemURL( p_sd, &p_items[i_type], NULL );
return;
vlc_restorecancel(canc);
return NULL;
}
for( i = 0; p_items[i_type].p_children[i].psz_name; i++ )
{
......@@ -240,6 +256,7 @@ static void Run( services_discovery_t *p_sd )
}
}
vlc_restorecancel(canc);
return NULL;
}
/*****************************************************************************
......@@ -247,5 +264,9 @@ static void Run( services_discovery_t *p_sd )
*****************************************************************************/
static void Close( vlc_object_t *p_this )
{
VLC_UNUSED(p_this);
services_discovery_t *p_sd = (services_discovery_t *)p_this;
services_discovery_sys_t *p_sys = p_sd->p_sys;
vlc_join (p_sys->thread, NULL);
free (p_sys);
}
......@@ -200,8 +200,6 @@ libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
services_discovery_ended,
p_mdis );
services_discovery_Start( p_mdis->p_sd );
/* Here we go */
return p_mdis;
......
......@@ -316,8 +316,6 @@ services_discovery_GetLocalizedName
__services_discovery_GetServicesNames
services_discovery_RemoveItem
services_discovery_SetLocalizedName
services_discovery_Start
services_discovery_Stop
sout_AccessOutControl
sout_AccessOutDelete
sout_AccessOutNew
......
......@@ -31,8 +31,6 @@
#include "playlist_internal.h"
#include "../libvlc.h"
static void* RunSD( vlc_object_t *p_this );
/*
* Services discovery
* Basically you just listen to Service discovery event through the
......@@ -62,7 +60,6 @@ services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name
if( !p_sd )
return NULL;
p_sd->pf_run = NULL;
p_sd->psz_localized_name = NULL;
vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
......@@ -87,47 +84,14 @@ services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name
p_sd->b_die = false; /* FIXME */
vlc_object_attach( p_sd, p_super );
return p_sd;
}
/***********************************************************************
* Destroy
***********************************************************************/
void services_discovery_Destroy ( services_discovery_t * p_sd )
{
vlc_event_manager_fini( &p_sd->event_manager );
free( p_sd->psz_module );
free( p_sd->psz_localized_name );
vlc_object_detach( p_sd );
vlc_object_release( p_sd );
}
/***********************************************************************
* Start
***********************************************************************/
int services_discovery_Start ( services_discovery_t * p_sd )
{
vlc_event_t event = {
.type = vlc_ServicesDiscoveryStarted
};
vlc_event_send( &p_sd->event_manager, &event );
if ((p_sd->pf_run != NULL)
&& vlc_thread_create( p_sd, "services_discovery", RunSD,
VLC_THREAD_PRIORITY_LOW, false))
{
msg_Err( p_sd, "cannot create services discovery thread" );
vlc_object_release( p_sd );
return VLC_EGENERIC;
}
return VLC_SUCCESS;
return p_sd;
}
/***********************************************************************
* Stop
***********************************************************************/
static void ObjectKillChildrens( vlc_object_t *p_obj )
{
vlc_list_t *p_list;
......@@ -140,17 +104,26 @@ static void ObjectKillChildrens( vlc_object_t *p_obj )
vlc_list_release( p_list );
}
void services_discovery_Stop ( services_discovery_t * p_sd )
/***********************************************************************
* Destroy
***********************************************************************/
void services_discovery_Destroy ( services_discovery_t * p_sd )
{
vlc_event_t event = {
.type = vlc_ServicesDiscoveryEnded
};
ObjectKillChildrens( VLC_OBJECT(p_sd) );
if( p_sd->pf_run ) vlc_thread_join( p_sd );
vlc_event_send( &p_sd->event_manager, &event );
module_Unneed( p_sd, p_sd->p_module );
vlc_event_manager_fini( &p_sd->event_manager );
free( p_sd->psz_module );
free( p_sd->psz_localized_name );
vlc_object_release( p_sd );
}
/***********************************************************************
......@@ -209,16 +182,6 @@ services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_it
vlc_event_send( &p_sd->event_manager, &event );
}
/***********************************************************************
* RunSD (Private)
***********************************************************************/
static void* RunSD( vlc_object_t *p_this )
{
services_discovery_t *p_sd = (services_discovery_t *)p_this;
p_sd->pf_run( p_sd );
return NULL;
}
/*
* Playlist - Services discovery bridge
*/
......@@ -351,8 +314,6 @@ int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modu
playlist_sd_item_removed,
p_cat );
services_discovery_Start( p_sd );
/* Free in playlist_ServicesDiscoveryRemove */
p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
if( !p_sds )
......@@ -396,8 +357,6 @@ int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
return VLC_EGENERIC;
}
services_discovery_Stop( p_sds->p_sd );
vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
vlc_ServicesDiscoveryItemAdded,
playlist_sd_item_added,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment