sd.c 13.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * sd.c: Services discovery related functions
 *****************************************************************************
4
 * Copyright (C) 2007-2008 the VideoLAN team
5 6 7
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea at videolan tod org>
8
 *          Fabio Ritrovato <sephiroth87 at videolan dot org>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * 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.
 *
 * This program 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.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifndef  _GNU_SOURCE
#   define  _GNU_SOURCE
#endif

32 33 34 35
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

36
#include <assert.h>
37
#include <math.h>
38
#include <vlc_common.h>
39 40
#include <vlc_services_discovery.h>
#include <vlc_playlist.h>
41
#include <vlc_charset.h>
42
#include <vlc_md5.h>
43

44 45
#include "../vlc.h"
#include "../libs.h"
46

47 48 49
static int vlclua_sd_delete_common( input_item_t **pp_item )
{
    assert(pp_item != NULL);
50

51 52 53
    input_item_t *p_item = *pp_item;
    if (p_item != NULL) /* item may be NULL if already removed earlier */
        input_item_Release( p_item );
54

55 56 57 58
    return 1;
}

static int vlclua_sd_remove_common( lua_State *L, input_item_t **pp_item )
59
{
60 61 62 63 64
    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );

    if (pp_item == NULL)
        return luaL_error( L, "expected item" );

65
    input_item_t *p_item = *pp_item;
66 67
    if (*pp_item == NULL)
        return luaL_error( L, "already removed item" );
68

69
    services_discovery_RemoveItem( p_sd, p_item );
70
    input_item_Release( p_item );
71
    /* Make sure we won't try to remove it again */
72 73 74 75
    *pp_item = NULL;
    return 1;
}

76 77 78 79 80 81 82 83 84 85

/*** Input item ***/

static int vlclua_sd_item_delete( lua_State *L )
{
    input_item_t **pp_item = luaL_checkudata( L, 1, "input_item_t" );

    return vlclua_sd_delete_common( pp_item );
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#define vlclua_item_luareg( a ) \
{ "set_" # a, vlclua_item_set_ ## a },

#define vlclua_item_meta( lowercase, normal ) \
static int vlclua_item_set_ ## lowercase ( lua_State *L )\
{\
    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );\
    input_item_t **pp_node = (input_item_t **)luaL_checkudata( L, 1, "input_item_t" );\
    if( *pp_node )\
    {\
        if( lua_isstring( L, -1 ) )\
        {\
            input_item_Set ## normal ( *pp_node, lua_tostring( L, -1 ) );\
        } else\
            msg_Err( p_sd, "Error parsing set_ " # lowercase " arguments" );\
    }\
    return 1;\
}

vlclua_item_meta(title, Title)
vlclua_item_meta(artist, Artist)
vlclua_item_meta(genre, Genre)
vlclua_item_meta(copyright, Copyright)
vlclua_item_meta(album, Album)
vlclua_item_meta(tracknum, TrackNum)
vlclua_item_meta(description, Description)
vlclua_item_meta(rating, Rating)
vlclua_item_meta(date, Date)
vlclua_item_meta(setting, Setting)
vlclua_item_meta(url, URL)
vlclua_item_meta(language, Language)
vlclua_item_meta(nowplaying, NowPlaying)
vlclua_item_meta(publisher, Publisher)
vlclua_item_meta(encodedby, EncodedBy)
vlclua_item_meta(arturl, ArtworkURL)
vlclua_item_meta(trackid, TrackID)
122
vlclua_item_meta(tracktotal, TrackTotal)
123 124 125 126 127
vlclua_item_meta(director, Director)
vlclua_item_meta(season, Season)
vlclua_item_meta(episode, Episode)
vlclua_item_meta(showname, ShowName)
vlclua_item_meta(actors, Actors)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

static const luaL_Reg vlclua_item_reg[] = {
    vlclua_item_luareg(title)
    vlclua_item_luareg(artist)
    vlclua_item_luareg(genre)
    vlclua_item_luareg(copyright)
    vlclua_item_luareg(album)
    vlclua_item_luareg(tracknum)
    vlclua_item_luareg(description)
    vlclua_item_luareg(rating)
    vlclua_item_luareg(date)
    vlclua_item_luareg(setting)
    vlclua_item_luareg(url)
    vlclua_item_luareg(language)
    vlclua_item_luareg(nowplaying)
    vlclua_item_luareg(publisher)
    vlclua_item_luareg(encodedby)
    vlclua_item_luareg(arturl)
    vlclua_item_luareg(trackid)
147
    vlclua_item_luareg(tracktotal)
148 149 150 151 152
    vlclua_item_luareg(director)
    vlclua_item_luareg(season)
    vlclua_item_luareg(episode)
    vlclua_item_luareg(showname)
    vlclua_item_luareg(actors)
153 154 155
    { NULL, NULL }
};

156 157 158 159 160 161 162 163
static input_item_t *vlclua_sd_create_item( services_discovery_t *p_sd,
                                            lua_State *L )
{
    if( !lua_istable( L, -1 ) )
    {
        msg_Err( p_sd, "Error: argument must be table" );
        return NULL;
    }
164

165 166 167
    lua_getfield( L, -1, "path" );
    if( !lua_isstring( L, -1 ) )
    {
168
        msg_Err( p_sd, "Error: \"%s\" parameter is required", "path" );
169 170
        return NULL;
    }
171

172 173 174 175 176 177 178 179 180
    const char *psz_path = lua_tostring( L, -1 );

    lua_getfield( L, -2, "title" );

    const char *psz_title = luaL_checkstring( L, -1 )
       ? luaL_checkstring( L, -1 )
       : psz_path;

    input_item_t *p_input = input_item_New( psz_path, psz_title );
181 182
    lua_pop( L, 2 );

183 184 185 186 187 188 189 190
    if( unlikely(p_input == NULL) )
        return NULL;

    /* The table must be at the top of the stack when calling
     * vlclua_read_options() */
    char **ppsz_options = NULL;
    int i_options = 0;

191
    lua_pushvalue( L, -1 );
192
    vlclua_read_options( p_sd, L, &i_options, &ppsz_options );
193
    lua_pop( L, 1 );
194 195 196 197 198 199 200 201 202 203 204 205 206 207

    input_item_AddOptions( p_input, i_options, (const char **)ppsz_options,
                           VLC_INPUT_OPTION_TRUSTED );
    while( i_options > 0 )
        free( ppsz_options[--i_options] );
    free( ppsz_options );

    vlclua_read_meta_data( p_sd, L, p_input );
    /* This one is to be tested... */
    vlclua_read_custom_meta_data( p_sd, L, p_input );
    /* The duration is given in seconds, convert to microseconds */

    lua_getfield( L, -1, "duration" );
    if( lua_isnumber( L, -1 ) )
208
        p_input->i_duration = llround(lua_tonumber( L, -1 ) * 1e6);
209 210 211 212 213 214 215
    else if( !lua_isnil( L, -1 ) )
        msg_Warn( p_sd, "Item duration should be a number (in seconds)." );
    lua_pop( L, 1 );

    /* string to build the input item uid */
    lua_getfield( L, -1, "uiddata" );
    if( lua_isstring( L, -1 ) )
216
    {
217 218
        char *s = strdup( luaL_checkstring( L, -1 ) );
        if ( s )
219
        {
220 221 222 223 224 225 226 227 228 229 230 231
            struct md5_s md5;
            InitMD5( &md5 );
            AddMD5( &md5, s, strlen( s ) );
            EndMD5( &md5 );
            free( s );
            s = psz_md5_hash( &md5 );
            if ( s )
                 input_item_AddInfo( p_input, "uid", "md5", "%s", s );
            free( s );
        }
    }
    lua_pop( L, 1 );
232

233 234
    input_item_t **udata = lua_newuserdata( L, sizeof( input_item_t * ) );
    *udata = p_input;
235

236 237 238 239 240
    if( luaL_newmetatable( L, "input_item_t" ) )
    {
        lua_newtable( L );
        luaL_register( L, NULL, vlclua_item_reg );
        lua_setfield( L, -2, "__index" );
241 242
        lua_pushcfunction( L, vlclua_sd_item_delete );
        lua_setfield( L, -2, "__gc" );
243 244 245 246
        lua_pushliteral( L, "none of your business" );
        lua_setfield( L, -2, "__metatable" );
    }
    lua_setmetatable( L, -2 );
247

248 249
    return p_input;
}
250 251


252 253
/*** Input item tree node ***/

254 255 256 257
static int vlclua_sd_node_delete( lua_State *L )
{
    input_item_t **pp_item = luaL_checkudata( L, 1, "node" );

258
    return vlclua_sd_delete_common( pp_item );
259 260
}

261 262 263 264 265 266 267 268 269
static int vlclua_sd_add_sub_common( services_discovery_t *p_sd,
                                     input_item_t **pp_node,
                                     input_item_t *p_input )
{
    if( *pp_node != NULL && p_input != NULL )
        services_discovery_AddSubItem( p_sd, *pp_node, p_input );
    return 1;
}

270 271 272 273
static int vlclua_node_add_subitem( lua_State *L )
{
    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
    input_item_t **pp_node = (input_item_t **)luaL_checkudata( L, 1, "node" );
274

275 276
    return vlclua_sd_add_sub_common( p_sd, pp_node,
                                     vlclua_sd_create_item( p_sd, L ) );
277
}
278

279 280
static const luaL_Reg vlclua_node_reg[];

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
static input_item_t *vlclua_sd_create_node( services_discovery_t *p_sd,
                                            lua_State *L )
{
    if( !lua_istable( L, -1 ) )
    {
        msg_Err( p_sd, "Error: argument must be table" );
        return NULL;
    }

    lua_getfield( L, -1, "title" );
    if( !lua_isstring( L, -1 ) )
    {
        msg_Err( p_sd, "Error: \"%s\" parameter is required", "title" );
        return NULL;
    }

    const char *psz_name = lua_tostring( L, -1 );
    input_item_t *p_input = input_item_NewExt( "vlc://nop", psz_name, -1,
                                               ITEM_TYPE_NODE,
                                               ITEM_NET_UNKNOWN );
    lua_pop( L, 1 );

    if( unlikely(p_input == NULL) )
        return NULL;

    lua_getfield( L, -1, "arturl" );
    if( lua_isstring( L, -1 ) && strcmp( lua_tostring( L, -1 ), "" ) )
    {
        char *psz_value = strdup( lua_tostring( L, -1 ) );
        EnsureUTF8( psz_value );
        msg_Dbg( p_sd, "ArtURL: %s", psz_value );
        /* TODO: ask for art download if not local file */
        input_item_SetArtURL( p_input, psz_value );
        free( psz_value );
    }
    lua_pop( L, 1 );

    input_item_t **udata = lua_newuserdata( L, sizeof( input_item_t * ) );
    *udata = p_input;
    if( luaL_newmetatable( L, "node" ) )
    {
        lua_newtable( L );
        luaL_register( L, NULL, vlclua_node_reg );
        lua_setfield( L, -2, "__index" );
325 326
        lua_pushcfunction( L, vlclua_sd_node_delete );
        lua_setfield( L, -2, "__gc" );
327 328 329 330 331 332
    }
    lua_setmetatable( L, -2 );

    return p_input;
}

333
static int vlclua_node_add_subnode( lua_State *L )
334 335
{
    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
336
    input_item_t **pp_node = (input_item_t **)luaL_checkudata( L, 1, "node" );
337

338 339
    return vlclua_sd_add_sub_common( p_sd, pp_node,
                                     vlclua_sd_create_node( p_sd, L ) );
340 341
}

342 343 344 345 346 347 348 349 350
static const luaL_Reg vlclua_node_reg[] = {
    { "add_subitem", vlclua_node_add_subitem },
    { "add_subnode", vlclua_node_add_subnode },
    { NULL, NULL }
};


/*** Services discovery instance ***/

351
static int vlclua_sd_add_common( services_discovery_t *p_sd,
352
                                 input_item_t *p_input )
353
{
354
    if( p_input != NULL )
355
        services_discovery_AddItem( p_sd, p_input );
356 357 358
    return 1;
}

359 360 361 362
static int vlclua_sd_add_item( lua_State *L )
{
    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );

363
    return vlclua_sd_add_common( p_sd, vlclua_sd_create_item( p_sd, L ) );
364 365
}

366 367 368
static int vlclua_sd_add_node( lua_State *L )
{
    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
369

370
    return vlclua_sd_add_common( p_sd, vlclua_sd_create_node( p_sd, L ) );
371 372
}

373 374
static int vlclua_sd_remove_item( lua_State *L )
{
375 376 377
    input_item_t **pp_input = luaL_checkudata( L, 1, "input_item_t" );

    return vlclua_sd_remove_common( L, pp_input );
378 379
}

380 381
static int vlclua_sd_remove_node( lua_State *L )
{
382 383 384
    input_item_t **pp_input = luaL_checkudata( L, 1, "node" );

    return vlclua_sd_remove_common( L, pp_input );
385 386
}

387 388 389 390 391 392 393
static const luaL_Reg vlclua_sd_sd_reg[] = {
    { "add_item", vlclua_sd_add_item },
    { "add_node", vlclua_sd_add_node },
    { "remove_item", vlclua_sd_remove_item },
    { "remove_node", vlclua_sd_remove_node },
    { NULL, NULL }
};
394

395
void luaopen_sd_sd( lua_State *L )
396
{
397 398 399 400
    lua_newtable( L );
    luaL_register( L, NULL, vlclua_sd_sd_reg );
    lua_setfield( L, -2, "sd" );
}
401 402


403
/*** SD management (for user interfaces) ***/
404

405 406 407 408 409 410 411
static int vlclua_sd_get_services_names( lua_State *L )
{
    playlist_t *p_playlist = vlclua_get_playlist_internal( L );
    char **ppsz_longnames;
    char **ppsz_names = vlc_sd_GetNames( p_playlist, &ppsz_longnames, NULL );
    if( !ppsz_names )
        return 0;
412

413 414 415 416 417 418 419 420 421 422
    char **ppsz_longname = ppsz_longnames;
    char **ppsz_name = ppsz_names;
    lua_settop( L, 0 );
    lua_newtable( L );
    for( ; *ppsz_name; ppsz_name++,ppsz_longname++ )
    {
        lua_pushstring( L, *ppsz_longname );
        lua_setfield( L, -2, *ppsz_name );
        free( *ppsz_name );
        free( *ppsz_longname );
423
    }
424 425
    free( ppsz_names );
    free( ppsz_longnames );
426 427 428
    return 1;
}

429
static int vlclua_sd_add( lua_State *L )
430
{
431 432 433 434
    const char *psz_sd = luaL_checkstring( L, 1 );
    playlist_t *p_playlist = vlclua_get_playlist_internal( L );
    int i_ret = playlist_ServicesDiscoveryAdd( p_playlist, psz_sd );
    return vlclua_push_ret( L, i_ret );
435 436
}

437 438 439 440 441 442 443
static int vlclua_sd_remove( lua_State *L )
{
    const char *psz_sd = luaL_checkstring( L, 1 );
    playlist_t *p_playlist = vlclua_get_playlist_internal( L );
    int i_ret = playlist_ServicesDiscoveryRemove( p_playlist, psz_sd );
    return vlclua_push_ret( L, i_ret );
}
444

445
static int vlclua_sd_is_loaded( lua_State *L )
446
{
447 448 449 450
    const char *psz_sd = luaL_checkstring( L, 1 );
    playlist_t *p_playlist = vlclua_get_playlist_internal( L );
    lua_pushboolean( L, playlist_IsServicesDiscoveryLoaded( p_playlist, psz_sd ));
    return 1;
451 452 453 454 455 456 457 458 459 460 461
}

static const luaL_Reg vlclua_sd_intf_reg[] = {
    { "get_services_names", vlclua_sd_get_services_names },
    { "add", vlclua_sd_add },
    { "remove", vlclua_sd_remove },
    { "is_loaded", vlclua_sd_is_loaded },
    { NULL, NULL }
};

void luaopen_sd_intf( lua_State *L )
462 463
{
    lua_newtable( L );
464
    luaL_register( L, NULL, vlclua_sd_intf_reg );
465 466
    lua_setfield( L, -2, "sd" );
}