sd.c 13.5 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 <vlc_common.h>
37 38
#include <vlc_services_discovery.h>
#include <vlc_playlist.h>
39
#include <vlc_charset.h>
40

41 42 43
#include "../vlc.h"
#include "../libs.h"
#include "playlist.h"
44 45 46 47

/*****************************************************************************
 *
 *****************************************************************************/
48
static int vlclua_node_add_subitem( lua_State * );
49
static int vlclua_node_add_subnode( lua_State * );
50 51 52

static const luaL_Reg vlclua_node_reg[] = {
    { "add_subitem", vlclua_node_add_subitem },
53
    { "add_subnode", vlclua_node_add_subnode },
54 55 56
    { NULL, NULL }
};

57
static int vlclua_sd_get_services_names( lua_State *L )
58
{
59
    playlist_t *p_playlist = vlclua_get_playlist_internal( L );
60
    char **ppsz_longnames;
61
    char **ppsz_names = vlc_sd_GetNames( p_playlist, &ppsz_longnames, NULL );
62 63 64
    if( !ppsz_names )
        return 0;

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    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 );
    }
    free( ppsz_names );
    free( ppsz_longnames );
    return 1;
}

81
static int vlclua_sd_add( lua_State *L )
82 83 84 85 86 87 88
{
    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 );
}

89
static int vlclua_sd_remove( lua_State *L )
90 91 92 93 94 95 96
{
    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 );
}

97
static int vlclua_sd_is_loaded( lua_State *L )
98 99 100 101 102 103
{
    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;
}
104

105 106 107 108 109 110 111 112
static int vlclua_sd_add_node( lua_State *L )
{
    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
    if( lua_istable( L, -1 ) )
    {
        lua_getfield( L, -1, "title" );
        if( lua_isstring( L, -1 ) )
        {
113
            const char *psz_name = lua_tostring( L, -1 );
114
            input_item_t *p_input = input_item_NewWithType( "vlc://nop",
115
                                                            psz_name, 0, NULL, 0,
116
                                                            -1, ITEM_TYPE_NODE );
117 118
            lua_pop( L, 1 );

119
            if( p_input )
120
            {
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                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 );
                }
                services_discovery_AddItem( p_sd, p_input, NULL );
                input_item_t **udata = (input_item_t **)
                                       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" );
                }
                lua_setmetatable( L, -2 );
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
            }
        }
        else
            msg_Err( p_sd, "vlc.sd.add_node: the \"title\" parameter can't be empty" );
    }
    else
        msg_Err( p_sd, "Error parsing add_node arguments" );
    return 1;
}

static int vlclua_sd_add_item( lua_State *L )
{
    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
    if( lua_istable( L, -1 ) )
    {
157
        lua_getfield( L, -1, "path" );
158 159
        if( lua_isstring( L, -1 ) )
        {
Fabio Ritrovato's avatar
Fabio Ritrovato committed
160 161
            char **ppsz_options = NULL;
            int i_options = 0;
162
            const char *psz_path = lua_tostring( L, -1 );
Fabio Ritrovato's avatar
Fabio Ritrovato committed
163
            vlclua_read_options( p_sd, L, &i_options, &ppsz_options );
164
            input_item_t *p_input = input_item_NewExt( psz_path, psz_path,
Fabio Ritrovato's avatar
Fabio Ritrovato committed
165 166 167
                                                       i_options,
                                                       (const char **)ppsz_options,
                                                       VLC_INPUT_OPTION_TRUSTED, -1 );
168 169
            lua_pop( L, 1 );

170
            if( p_input )
171
            {
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
                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 ) )
                   input_item_SetDuration( p_input, (lua_tonumber( L, -1 )*1e6) );
                else if( !lua_isnil( L, -1 ) )
                    msg_Warn( p_sd, "Item duration should be a number (in seconds)." );
                lua_pop( L, 1 );
                services_discovery_AddItem( p_sd, p_input, NULL );
                input_item_t **udata = (input_item_t **)
                                       lua_newuserdata( L, sizeof( input_item_t * ) );
                *udata = p_input;
                if( luaL_newmetatable( L, "input_item_t" ) )
                {
                    lua_pushliteral( L, "none of your business" );
                    lua_setfield( L, -2, "__metatable" );
                }
                lua_setmetatable( L, -2 );
                vlc_gc_decref( p_input );
193
            }
Fabio Ritrovato's avatar
Fabio Ritrovato committed
194 195 196
            while( i_options > 0 )
                free( ppsz_options[--i_options] );
            free( ppsz_options );
197 198
        }
        else
199
            msg_Err( p_sd, "vlc.sd.add_item: the \"path\" parameter can't be empty" );
200 201 202 203 204 205 206 207 208
    }
    else
        msg_Err( p_sd, "Error parsing add_item arguments" );
    return 1;
}

static int vlclua_sd_remove_item( lua_State *L )
{
    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
209
    if( !lua_isnil( L, 1 ) )
210
    {
211
        input_item_t **pp_input = luaL_checkudata( L, 1, "input_item_t" );
212 213
        if( *pp_input )
            services_discovery_RemoveItem( p_sd, *pp_input );
214 215
        /* Make sure we won't try to remove it again */
        *pp_input = NULL;
216 217 218 219 220 221 222 223 224 225 226 227
    }
    return 1;
}

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" );
    if( *pp_node )
    {
        if( lua_istable( L, -1 ) )
        {
228
            lua_getfield( L, -1, "path" );
229 230
            if( lua_isstring( L, -1 ) )
            {
Fabio Ritrovato's avatar
Fabio Ritrovato committed
231 232
                char **ppsz_options = NULL;
                int i_options = 0;
233
                const char *psz_path = lua_tostring( L, -1 );
Fabio Ritrovato's avatar
Fabio Ritrovato committed
234
                vlclua_read_options( p_sd, L, &i_options, &ppsz_options );
235
                input_item_node_t *p_input_node = input_item_node_Create( *pp_node );
236
                input_item_t *p_input = input_item_NewExt( psz_path,
237
                                                           psz_path, i_options,
Fabio Ritrovato's avatar
Fabio Ritrovato committed
238 239
                                                           (const char **)ppsz_options,
                                                           VLC_INPUT_OPTION_TRUSTED, -1 );
240 241
                lua_pop( L, 1 );

242
                if( p_input )
243
                {
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                    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 );
                    lua_getfield( L, -1, "duration" );
                    if( lua_isnumber( L, -1 ) )
                        input_item_SetDuration( p_input, (lua_tonumber( L, -1 )*1e6) );
                    else if( !lua_isnil( L, -1 ) )
                        msg_Warn( p_sd, "Item duration should be a number (in seconds)." );
                    lua_pop( L, 1 );
                    input_item_node_AppendItem( p_input_node, p_input );
                    input_item_node_PostAndDelete( p_input_node );
                    input_item_t **udata = (input_item_t **)
                                           lua_newuserdata( L, sizeof( input_item_t * ) );
                    *udata = p_input;
                    if( luaL_newmetatable( L, "input_item_t" ) )
                    {
                        lua_pushliteral( L, "none of your business" );
                        lua_setfield( L, -2, "__metatable" );
                    }
                    lua_setmetatable( L, -2 );
                    vlc_gc_decref( p_input );
265
                }
Fabio Ritrovato's avatar
Fabio Ritrovato committed
266 267 268
                while( i_options > 0 )
                    free( ppsz_options[--i_options] );
                free( ppsz_options );
269 270
            }
            else
271
                msg_Err( p_sd, "node:add_subitem: the \"path\" parameter can't be empty" );
272 273 274 275 276 277 278
        }
        else
            msg_Err( p_sd, "Error parsing add_subitem arguments" );
    }
    return 1;
}

279
static int vlclua_node_add_subnode( lua_State *L )
280 281 282 283 284 285 286 287 288 289
{
    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" );
    if( *pp_node )
    {
        if( lua_istable( L, -1 ) )
        {
            lua_getfield( L, -1, "title" );
            if( lua_isstring( L, -1 ) )
            {
290
                const char *psz_name = lua_tostring( L, -1 );
291
                input_item_node_t *p_input_node = input_item_node_Create( *pp_node );
292
                input_item_t *p_input = input_item_NewWithType( "vlc://nop",
293
                                                                psz_name, 0, NULL, 0,
294
                                                                -1, ITEM_TYPE_NODE );
295 296
                lua_pop( L, 1 );

297
                if( p_input )
298
                {
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
                    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 );
                        input_item_SetArtURL( p_input, psz_value );
                        free( psz_value );
                    }
                    input_item_node_AppendItem( p_input_node, p_input );
                    input_item_node_PostAndDelete( p_input_node );
                    input_item_t **udata = (input_item_t **)
                                           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" );
                    }
                    lua_setmetatable( L, -2 );
320 321 322 323 324 325 326 327 328 329 330
                }
            }
            else
                msg_Err( p_sd, "node:add_node: the \"title\" parameter can't be empty" );
        }
        else
            msg_Err( p_sd, "Error parsing add_node arguments" );
    }
    return 1;
}

331 332 333 334 335 336 337 338
/*****************************************************************************
 *
 *****************************************************************************/
static const luaL_Reg vlclua_sd_reg[] = {
    { "get_services_names", vlclua_sd_get_services_names },
    { "add", vlclua_sd_add },
    { "remove", vlclua_sd_remove },
    { "is_loaded", vlclua_sd_is_loaded },
339 340 341
    { "add_node", vlclua_sd_add_node },
    { "add_item", vlclua_sd_add_item },
    { "remove_item", vlclua_sd_remove_item },
342 343 344 345 346 347 348 349 350
    { NULL, NULL }
};

void luaopen_sd( lua_State *L )
{
    lua_newtable( L );
    luaL_register( L, NULL, vlclua_sd_reg );
    lua_setfield( L, -2, "sd" );
}