| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#ifdef HAVE_CONFIG_H |
|---|
| 29 |
# include "config.h" |
|---|
| 30 |
#endif |
|---|
| 31 |
|
|---|
| 32 |
#include <vlc_common.h> |
|---|
| 33 |
#include <vlc_plugin.h> |
|---|
| 34 |
#include <vlc_playlist.h> |
|---|
| 35 |
#include <vlc_arrays.h> |
|---|
| 36 |
|
|---|
| 37 |
#include <avahi-client/client.h> |
|---|
| 38 |
#include <avahi-client/publish.h> |
|---|
| 39 |
#include <avahi-client/lookup.h> |
|---|
| 40 |
#include <avahi-common/thread-watch.h> |
|---|
| 41 |
#include <avahi-common/malloc.h> |
|---|
| 42 |
#include <avahi-common/error.h> |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
static int Open ( vlc_object_t * ); |
|---|
| 50 |
static void Close( vlc_object_t * ); |
|---|
| 51 |
|
|---|
| 52 |
vlc_module_begin(); |
|---|
| 53 |
set_shortname( "Bonjour" ); |
|---|
| 54 |
set_description( N_("Bonjour services") ); |
|---|
| 55 |
set_category( CAT_PLAYLIST ); |
|---|
| 56 |
set_subcategory( SUBCAT_PLAYLIST_SD ); |
|---|
| 57 |
set_capability( "services_discovery", 0 ); |
|---|
| 58 |
set_callbacks( Open, Close ); |
|---|
| 59 |
vlc_module_end(); |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
struct services_discovery_sys_t |
|---|
| 66 |
{ |
|---|
| 67 |
AvahiThreadedPoll *poll; |
|---|
| 68 |
AvahiClient *client; |
|---|
| 69 |
AvahiServiceBrowser *sb; |
|---|
| 70 |
vlc_dictionary_t services_name_to_input_item; |
|---|
| 71 |
}; |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
static void client_callback( AvahiClient *c, AvahiClientState state, |
|---|
| 81 |
void * userdata ) |
|---|
| 82 |
{ |
|---|
| 83 |
services_discovery_t *p_sd = ( services_discovery_t* )userdata; |
|---|
| 84 |
services_discovery_sys_t *p_sys = p_sd->p_sys; |
|---|
| 85 |
|
|---|
| 86 |
if( state == AVAHI_CLIENT_FAILURE && |
|---|
| 87 |
(avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) ) |
|---|
| 88 |
{ |
|---|
| 89 |
msg_Err( p_sd, "avahi client disconnected" ); |
|---|
| 90 |
avahi_threaded_poll_quit( p_sys->poll ); |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
static void resolve_callback( |
|---|
| 98 |
AvahiServiceResolver *r, |
|---|
| 99 |
AvahiIfIndex interface, |
|---|
| 100 |
AvahiProtocol protocol, |
|---|
| 101 |
AvahiResolverEvent event, |
|---|
| 102 |
const char *name, |
|---|
| 103 |
const char *type, |
|---|
| 104 |
const char *domain, |
|---|
| 105 |
const char *host_name, |
|---|
| 106 |
const AvahiAddress *address, |
|---|
| 107 |
uint16_t port, |
|---|
| 108 |
AvahiStringList *txt, |
|---|
| 109 |
AvahiLookupResultFlags flags, |
|---|
| 110 |
void* userdata ) |
|---|
| 111 |
{ |
|---|
| 112 |
services_discovery_t *p_sd = ( services_discovery_t* )userdata; |
|---|
| 113 |
services_discovery_sys_t *p_sys = p_sd->p_sys; |
|---|
| 114 |
|
|---|
| 115 |
VLC_UNUSED(interface); VLC_UNUSED(host_name); |
|---|
| 116 |
VLC_UNUSED(flags); |
|---|
| 117 |
|
|---|
| 118 |
if( event == AVAHI_RESOLVER_FAILURE ) |
|---|
| 119 |
{ |
|---|
| 120 |
msg_Err( p_sd, |
|---|
| 121 |
"failed to resolve service '%s' of type '%s' in domain '%s'", |
|---|
| 122 |
name, type, domain ); |
|---|
| 123 |
} |
|---|
| 124 |
else if( event == AVAHI_RESOLVER_FOUND ) |
|---|
| 125 |
{ |
|---|
| 126 |
char a[128]; |
|---|
| 127 |
char *psz_uri = NULL; |
|---|
| 128 |
char *psz_addr = NULL; |
|---|
| 129 |
AvahiStringList *asl = NULL; |
|---|
| 130 |
input_item_t *p_input = NULL; |
|---|
| 131 |
|
|---|
| 132 |
msg_Dbg( p_sd, "service '%s' of type '%s' in domain '%s'", |
|---|
| 133 |
name, type, domain ); |
|---|
| 134 |
|
|---|
| 135 |
avahi_address_snprint(a, (sizeof(a)/sizeof(a[0]))-1, address); |
|---|
| 136 |
if( protocol == AVAHI_PROTO_INET6 ) |
|---|
| 137 |
if( asprintf( &psz_addr, "[%s]", a ) == -1 ) |
|---|
| 138 |
return; |
|---|
| 139 |
|
|---|
| 140 |
if( txt != NULL ) |
|---|
| 141 |
asl = avahi_string_list_find( txt, "path" ); |
|---|
| 142 |
if( asl != NULL ) |
|---|
| 143 |
{ |
|---|
| 144 |
size_t size; |
|---|
| 145 |
char *key = NULL; |
|---|
| 146 |
char *value = NULL; |
|---|
| 147 |
if( avahi_string_list_get_pair( asl, &key, &value, &size ) == 0 && |
|---|
| 148 |
value != NULL ) |
|---|
| 149 |
{ |
|---|
| 150 |
if( asprintf( &psz_uri, "http://%s:%d%s", |
|---|
| 151 |
psz_addr != NULL ? psz_addr : a, port, value ) == -1 ) |
|---|
| 152 |
{ |
|---|
| 153 |
free( psz_addr ); |
|---|
| 154 |
return; |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
if( key != NULL ) |
|---|
| 158 |
avahi_free( (void *)key ); |
|---|
| 159 |
if( value != NULL ) |
|---|
| 160 |
avahi_free( (void *)value ); |
|---|
| 161 |
} |
|---|
| 162 |
else |
|---|
| 163 |
{ |
|---|
| 164 |
if( asprintf( &psz_uri, "http://%s:%d", |
|---|
| 165 |
psz_addr != NULL ? psz_addr : a, port ) == -1 ) |
|---|
| 166 |
{ |
|---|
| 167 |
free( psz_addr ); |
|---|
| 168 |
return; |
|---|
| 169 |
} |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
if( psz_addr != NULL ) |
|---|
| 173 |
free( (void *)psz_addr ); |
|---|
| 174 |
|
|---|
| 175 |
if( psz_uri != NULL ) |
|---|
| 176 |
{ |
|---|
| 177 |
p_input = input_item_NewExt( p_sd, psz_uri, name, 0, NULL, -1 ); |
|---|
| 178 |
free( (void *)psz_uri ); |
|---|
| 179 |
} |
|---|
| 180 |
if( p_input != NULL ) |
|---|
| 181 |
{ |
|---|
| 182 |
vlc_dictionary_insert( &p_sys->services_name_to_input_item, |
|---|
| 183 |
name, p_input ); |
|---|
| 184 |
services_discovery_AddItem( p_sd, p_input, NULL ); |
|---|
| 185 |
vlc_gc_decref( p_input ); |
|---|
| 186 |
} |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
avahi_service_resolver_free( r ); |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
static void browse_callback( |
|---|
| 196 |
AvahiServiceBrowser *b, |
|---|
| 197 |
AvahiIfIndex interface, |
|---|
| 198 |
AvahiProtocol protocol, |
|---|
| 199 |
AvahiBrowserEvent event, |
|---|
| 200 |
const char *name, |
|---|
| 201 |
const char *type, |
|---|
| 202 |
const char *domain, |
|---|
| 203 |
AvahiLookupResultFlags flags, |
|---|
| 204 |
void* userdata ) |
|---|
| 205 |
{ |
|---|
| 206 |
VLC_UNUSED(b); |
|---|
| 207 |
VLC_UNUSED(flags); |
|---|
| 208 |
services_discovery_t *p_sd = ( services_discovery_t* )userdata; |
|---|
| 209 |
services_discovery_sys_t *p_sys = p_sd->p_sys; |
|---|
| 210 |
if( event == AVAHI_BROWSER_NEW ) |
|---|
| 211 |
{ |
|---|
| 212 |
if( avahi_service_resolver_new( p_sys->client, interface, protocol, |
|---|
| 213 |
name, type, domain, AVAHI_PROTO_UNSPEC, |
|---|
| 214 |
0, |
|---|
| 215 |
resolve_callback, userdata ) == NULL ) |
|---|
| 216 |
{ |
|---|
| 217 |
msg_Err( p_sd, "failed to resolve service '%s': %s", name, |
|---|
| 218 |
avahi_strerror( avahi_client_errno( p_sys->client ) ) ); |
|---|
| 219 |
} |
|---|
| 220 |
} |
|---|
| 221 |
else if( name ) |
|---|
| 222 |
{ |
|---|
| 223 |
|
|---|
| 224 |
input_item_t *p_item; |
|---|
| 225 |
p_item = vlc_dictionary_value_for_key( |
|---|
| 226 |
&p_sys->services_name_to_input_item, |
|---|
| 227 |
name ); |
|---|
| 228 |
if( !p_item ) |
|---|
| 229 |
msg_Err( p_sd, "failed to find service '%s' in playlist", name ); |
|---|
| 230 |
else |
|---|
| 231 |
{ |
|---|
| 232 |
services_discovery_RemoveItem( p_sd, p_item ); |
|---|
| 233 |
vlc_dictionary_remove_value_for_key( |
|---|
| 234 |
&p_sys->services_name_to_input_item, |
|---|
| 235 |
name ); |
|---|
| 236 |
} |
|---|
| 237 |
} |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
static int Open( vlc_object_t *p_this ) |
|---|
| 244 |
{ |
|---|
| 245 |
services_discovery_t *p_sd = ( services_discovery_t* )p_this; |
|---|
| 246 |
services_discovery_sys_t *p_sys; |
|---|
| 247 |
int err; |
|---|
| 248 |
|
|---|
| 249 |
p_sd->p_sys = p_sys = (services_discovery_sys_t *)malloc( |
|---|
| 250 |
sizeof( services_discovery_sys_t ) ); |
|---|
| 251 |
|
|---|
| 252 |
if( !p_sys ) |
|---|
| 253 |
return VLC_ENOMEM; |
|---|
| 254 |
|
|---|
| 255 |
memset( p_sys, 0, sizeof(*p_sys) ); |
|---|
| 256 |
|
|---|
| 257 |
vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 ); |
|---|
| 258 |
|
|---|
| 259 |
p_sys->poll = avahi_threaded_poll_new(); |
|---|
| 260 |
if( p_sys->poll == NULL ) |
|---|
| 261 |
{ |
|---|
| 262 |
msg_Err( p_sd, "failed to create Avahi threaded poll" ); |
|---|
| 263 |
goto error; |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
p_sys->client = avahi_client_new( avahi_threaded_poll_get(p_sys->poll), |
|---|
| 267 |
0, client_callback, p_sd, &err ); |
|---|
| 268 |
if( p_sys->client == NULL ) |
|---|
| 269 |
{ |
|---|
| 270 |
msg_Err( p_sd, "failed to create avahi client: %s", |
|---|
| 271 |
avahi_strerror( err ) ); |
|---|
| 272 |
goto error; |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC, |
|---|
| 276 |
AVAHI_PROTO_UNSPEC, |
|---|
| 277 |
"_vlc-http._tcp", NULL, |
|---|
| 278 |
0, browse_callback, p_sd ); |
|---|
| 279 |
if( p_sys->sb == NULL ) |
|---|
| 280 |
{ |
|---|
| 281 |
msg_Err( p_sd, "failed to create avahi service browser" ); |
|---|
| 282 |
goto error; |
|---|
| 283 |
} |
|---|
| 284 |
|
|---|
| 285 |
services_discovery_SetLocalizedName( p_sd, _("Bonjour") ); |
|---|
| 286 |
|
|---|
| 287 |
return VLC_SUCCESS; |
|---|
| 288 |
|
|---|
| 289 |
error: |
|---|
| 290 |
if( p_sys->sb != NULL ) |
|---|
| 291 |
avahi_service_browser_free( p_sys->sb ); |
|---|
| 292 |
if( p_sys->client != NULL ) |
|---|
| 293 |
avahi_client_free( p_sys->client ); |
|---|
| 294 |
if( p_sys->poll != NULL ) |
|---|
| 295 |
avahi_threaded_poll_free( p_sys->poll ); |
|---|
| 296 |
|
|---|
| 297 |
vlc_dictionary_clear( &p_sys->services_name_to_input_item ); |
|---|
| 298 |
free( p_sys ); |
|---|
| 299 |
|
|---|
| 300 |
return VLC_EGENERIC; |
|---|
| 301 |
} |
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
static void Close( vlc_object_t *p_this ) |
|---|
| 307 |
{ |
|---|
| 308 |
services_discovery_t *p_sd = ( services_discovery_t* )p_this; |
|---|
| 309 |
services_discovery_sys_t *p_sys = p_sd->p_sys; |
|---|
| 310 |
|
|---|
| 311 |
avahi_service_browser_free( p_sys->sb ); |
|---|
| 312 |
avahi_client_free( p_sys->client ); |
|---|
| 313 |
avahi_threaded_poll_free( p_sys->poll ); |
|---|
| 314 |
|
|---|
| 315 |
vlc_dictionary_clear( &p_sys->services_name_to_input_item ); |
|---|
| 316 |
free( p_sys ); |
|---|
| 317 |
} |
|---|