[vlc-devel] [PATCH 8/9] lua: Add support for services_advertisement

Hugo Beauzée-Luyssen hugo at beauzee.fr
Wed Sep 2 15:47:30 CEST 2020


---
 modules/lua/Makefile.am |   3 +-
 modules/lua/extension.c |   1 +
 modules/lua/intf.c      |   1 +
 modules/lua/libs.h      |   1 +
 modules/lua/libs/sa.c   | 116 ++++++++++++++++++++++++++++++++++++++++
 share/lua/README.txt    |  18 +++++++
 6 files changed, 139 insertions(+), 1 deletion(-)
 create mode 100644 modules/lua/libs/sa.c

diff --git a/modules/lua/Makefile.am b/modules/lua/Makefile.am
index 39852e5116..89ae1d2597 100644
--- a/modules/lua/Makefile.am
+++ b/modules/lua/Makefile.am
@@ -32,7 +32,8 @@ liblua_plugin_la_SOURCES = \
 	lua/libs/io.c \
 	lua/libs/errno.c \
 	lua/libs/rand.c \
-	lua/libs/renderers.c
+	lua/libs/renderers.c \
+	lua/libs/sa.c
 
 if HAVE_WIN32
 liblua_plugin_la_SOURCES += lua/libs/win.c
diff --git a/modules/lua/extension.c b/modules/lua/extension.c
index 998ba5a0cd..78a69d0dad 100644
--- a/modules/lua/extension.c
+++ b/modules/lua/extension.c
@@ -835,6 +835,7 @@ static lua_State* GetLuaState( extensions_manager_t *p_mgr,
         luaopen_errno( L );
         luaopen_rand( L );
         luaopen_rd( L );
+        luaopen_sa( L );
 #if defined(_WIN32) && !VLC_WINSTORE_APP
         luaopen_win( L );
 #endif
diff --git a/modules/lua/intf.c b/modules/lua/intf.c
index 409432e8ec..78958df363 100644
--- a/modules/lua/intf.c
+++ b/modules/lua/intf.c
@@ -272,6 +272,7 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
     luaopen_errno( L );
     luaopen_rand( L );
     luaopen_rd( L );
+    luaopen_sa( L );
 #if defined(_WIN32) && !VLC_WINSTORE_APP
     luaopen_win( L );
 #endif
diff --git a/modules/lua/libs.h b/modules/lua/libs.h
index ba8cbb2496..430057925a 100644
--- a/modules/lua/libs.h
+++ b/modules/lua/libs.h
@@ -50,6 +50,7 @@ void luaopen_vlcio( lua_State *L );
 void luaopen_errno( lua_State *L );
 void luaopen_rand( lua_State *L );
 void luaopen_rd( lua_State *L );
+void luaopen_sa( lua_State *L );
 #ifdef _WIN32
 void luaopen_win( lua_State *L );
 #endif
diff --git a/modules/lua/libs/sa.c b/modules/lua/libs/sa.c
new file mode 100644
index 0000000000..71a9d82a47
--- /dev/null
+++ b/modules/lua/libs/sa.c
@@ -0,0 +1,116 @@
+/*****************************************************************************
+ * sa.c: Services advertisement related functions
+ *****************************************************************************
+ * Copyright (C) 2020 the VideoLAN team
+ *
+ * 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.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_services_advertisement.h>
+
+#include "../vlc.h"
+#include "../libs.h"
+
+static int vlclua_sa_entry_announce( lua_State* L )
+{
+    services_advertisement_entry_t** entry = luaL_checkudata( L, 1, "services_advertisement" );
+    vlc_object_t* obj = vlclua_get_this( L );
+    bool res = vlc_sa_Announce( vlc_object_instance( obj ), *entry ) == VLC_SUCCESS;
+    lua_pushboolean( L, res );
+    return 1;
+}
+
+static int vlclua_sa_entry_conceal( lua_State* L )
+{
+    services_advertisement_entry_t** entry = luaL_checkudata( L, 1, "services_advertisement" );
+    vlc_object_t* obj = vlclua_get_this( L );
+    bool res = vlc_sa_Conceal( vlc_object_instance( obj ), *entry ) == VLC_SUCCESS;
+    lua_pushboolean( L, res );
+    return 1;
+}
+
+static int vlclua_sa_entry_add_txt( lua_State* L )
+{
+    services_advertisement_entry_t** entry = luaL_checkudata( L, 1, "services_advertisement" );
+    const char* txt = luaL_checkstring( L, 2 );
+    lua_pushboolean( L, vlc_sa_entry_Add_TXT( *entry, txt ) );
+    return 1;
+}
+
+static int vlclua_sa_entry_release( lua_State* L )
+{
+    services_advertisement_entry_t** entry = luaL_checkudata( L, 1, "services_advertisement" );
+    vlc_object_t* obj = vlclua_get_this( L );
+    vlc_sa_Conceal( vlc_object_instance( obj ), *entry );
+    vlc_sa_entry_Release( *entry );
+    return 0;
+}
+
+static const luaL_Reg vlclua_sa_entry_reg[] = {
+    { "announce", vlclua_sa_entry_announce },
+    { "conceal", vlclua_sa_entry_conceal },
+    { "add_txt", vlclua_sa_entry_add_txt },
+    { NULL, NULL }
+};
+
+static int vlclua_sa_new_entry( lua_State* L )
+{
+    const char* name = luaL_checkstring( L, 1 );
+    const char* type = luaL_checkstring( L, 2 );
+    lua_Integer port = luaL_checkinteger( L, 3 );
+    services_advertisement_entry_t* entry = vlc_sa_entry_New( name, type, port );
+    if ( !entry )
+    {
+        lua_pushnil( L );
+        return 1;
+    }
+    services_advertisement_entry_t** lua_entry = lua_newuserdata( L, sizeof( *lua_entry ) );
+    if ( !lua_entry )
+    {
+        vlc_sa_entry_Release( entry );
+        lua_pushnil( L );
+        return 1;
+    }
+    *lua_entry = entry;
+    if ( luaL_newmetatable( L, "services_advertisement" ) )
+    {
+        lua_newtable( L );
+        luaL_register( L, NULL, vlclua_sa_entry_reg );
+        lua_setfield( L, -2, "__index" );
+        lua_pushcfunction( L, vlclua_sa_entry_release );
+        lua_setfield( L, -2, "__gc" );
+    }
+    lua_setmetatable( L, -2 );
+
+    return 1;
+}
+
+static const luaL_Reg vlclua_sa_reg[] = {
+    { "new_entry", vlclua_sa_new_entry },
+
+    { NULL, NULL }
+};
+
+void luaopen_sa( lua_State *L )
+{
+    lua_newtable( L );
+    luaL_register( L, NULL, vlclua_sa_reg );
+    lua_setfield( L, -2, "sa" );
+}
diff --git a/share/lua/README.txt b/share/lua/README.txt
index b9072bb454..853806ad8c 100644
--- a/share/lua/README.txt
+++ b/share/lua/README.txt
@@ -423,6 +423,24 @@ d:set_encodedby(): the item's EncodedBy (OPTIONAL, meta data)
 d:set_arturl(): the item's ArtURL (OPTIONAL, meta data)
 d:set_trackid(): the item's TrackID (OPTIONAL, meta data)
 
+Services Advertisement
+------------------
+Scripts can use the following Services Advertisement function:
+
+vlc.sa.new_entry( name, type, port )
+  Which returns a service advertisement entry object or nil on error.
+
+The service advertisement entry objects have the following members:
+  :announce()
+    Announce the entry on the network.
+  :add_txt( record )
+    Add a TXT record for the entry
+  :conceal()
+    Conceals the entry and hide it from the network
+
+When the service advertisement object is garbage collected, it will 
+automatically be concealed.
+
 Stream
 ------
 stream( url ): Instantiate a stream object for specific url.
-- 
2.20.1



More information about the vlc-devel mailing list