[vlc-devel] [PATCH 1/5] macosx: Add renderer discovery classes

Marvin Scholz epirat07 at gmail.com
Thu Jun 16 23:54:17 CEST 2016


This adds two wrapper classes:

- VLCRendererDiscovery
  Wraps libvlc’s vlc_renderer_discovery and allows a delegate
  to receive add and delete notifications for renderer items.

- VLCRendererItem
  Wraps libvlc’s vlc_renderer_item, is used by VLCRendererDiscovery
  to conveniently wrap the vlc_renderer_item to use them easier in
  Objective C.
---
 modules/gui/macosx/VLCRendererDiscovery.h | 127 ++++++++++++++++++++++++++
 modules/gui/macosx/VLCRendererDiscovery.m | 142 ++++++++++++++++++++++++++++++
 modules/gui/macosx/VLCRendererItem.h      |  91 +++++++++++++++++++
 modules/gui/macosx/VLCRendererItem.m      | 105 ++++++++++++++++++++++
 4 files changed, 465 insertions(+)
 create mode 100644 modules/gui/macosx/VLCRendererDiscovery.h
 create mode 100644 modules/gui/macosx/VLCRendererDiscovery.m
 create mode 100644 modules/gui/macosx/VLCRendererItem.h
 create mode 100644 modules/gui/macosx/VLCRendererItem.m

diff --git a/modules/gui/macosx/VLCRendererDiscovery.h b/modules/gui/macosx/VLCRendererDiscovery.h
new file mode 100644
index 0000000..a7f8f6a
--- /dev/null
+++ b/modules/gui/macosx/VLCRendererDiscovery.h
@@ -0,0 +1,127 @@
+/*****************************************************************************
+ * VLCRendererDiscovery.h: Wrapper class for vlc_renderer_discovery
+ *****************************************************************************
+ * Copyright (C) 2016 VLC authors and VideoLAN
+ * $Id$
+ *
+ * Authors: Marvin Scholz <epirat07 at gmail dot com>
+ *
+ * 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.
+ *****************************************************************************/
+
+#import <Foundation/Foundation.h>
+
+#import "VLCRendererItem.h"
+
+ at protocol VLCRendererDiscoveryDelegate;
+
+/**
+ \c VLCRendererDiscovery is a simple wrapper class for libvlc’s
+ \c vlc_renderer_discovery. It's initialized with the renderer name and
+ manages the underlying renderer discovery.
+ */
+ at interface VLCRendererDiscovery : NSObject
+
+/**
+ The delegate that is called when a \c VLCRendererItem is added or deleted
+ */
+ at property (weak) id<VLCRendererDiscoveryDelegate> delegate;
+
+/**
+ The name of the renderer discovery module
+ */
+ at property (readonly) NSString *name;
+
+/**
+ The longname of the renderer discovery module
+ */
+ at property (readonly) NSString *longName;
+
+/**
+ Array of \c VLCRendererItems that the module discovered
+ */
+ at property (readonly) NSMutableArray<VLCRendererItem*> *rendererItems;
+
+/**
+ Indicates if the discovery has been started
+ */
+ at property (readonly) bool discoveryStarted;
+
+/**
+ Initialize the class with a renderer name and (optional) longname retrieved with
+ the \c vlc_rd_get_names function.
+ 
+ \param name        Renderer name as C string
+ \param longname    Renderer longname as C string
+ 
+ \returns   Initialized class that already created the underlying
+            \c vlc_renderer_discovery structure or nil on failure.
+ */
+- (instancetype)initWithName:(const char*)name andLongname:(const char*)longname;
+
+/**
+ Starts the renderer discovery
+
+ \return YES if the renderer was successfully started, NO otherwise.
+
+ \sa -stopDiscovery
+ */
+- (bool)startDiscovery;
+
+/**
+ Stops the renderer discovery
+
+ \note Stopping an already stopped renderer discovery has no effect.
+
+ \sa -startDiscovery
+ */
+- (void)stopDiscovery;
+
+- (void)dealloc;
+
+/* This should never be called! */
+- (void)handleEvent:(const vlc_event_t *)event;
+
+ at end
+
+#pragma mark Delegate Protocol
+/**
+ \c VLCRendererDiscoveryDelegate protocol defines the required methods
+ to be implemented by a \c VLCRendererDiscovery delegate.
+ */
+ at protocol VLCRendererDiscoveryDelegate
+
+ at required
+/**
+ Invoked when a \c VLCRendererItem was added
+ 
+ \param item    The renderer item that was added
+ \param sender  The \c VLCRendererDiscovery object that added the Item.
+
+ \sa -removedRendererItem:from:
+ */
+- (void)addedRendererItem:(VLCRendererItem *)item from:(VLCRendererDiscovery *)sender;
+
+/**
+ Invoked when a \c VLCRendererItem was removed
+
+ \param item    The renderer item that was removed
+ \param sender  The \c VLCRendererDiscovery object that removed the Item.
+
+ \sa -addedRendererItem:from:
+ */
+- (void)removedRendererItem:(VLCRendererItem *)item from:(VLCRendererDiscovery *)sender;
+
+ at end
\ No newline at end of file
diff --git a/modules/gui/macosx/VLCRendererDiscovery.m b/modules/gui/macosx/VLCRendererDiscovery.m
new file mode 100644
index 0000000..502eb6a
--- /dev/null
+++ b/modules/gui/macosx/VLCRendererDiscovery.m
@@ -0,0 +1,142 @@
+/*****************************************************************************
+ * VLCRendererDiscovery.m: Wrapper class for vlc_renderer_discovery
+ *****************************************************************************
+ * Copyright (C) 2016 VLC authors and VideoLAN
+ * $Id$
+ *
+ * Authors: Marvin Scholz <epirat07 at gmail dot com>
+ *
+ * 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.
+ *****************************************************************************/
+
+#import "VLCRendererDiscovery.h"
+
+#import "intf.h"
+
+#include <vlc_common.h>
+#include <vlc_renderer_discovery.h>
+
+// C callback event handler function for vlc_event_manager
+static void renderer_event_received(const vlc_event_t *p_event, void *user_data)
+{
+    VLCRendererDiscovery *target = (__bridge VLCRendererDiscovery*)user_data;
+    [target handleEvent:p_event];
+}
+
+ at implementation VLCRendererDiscovery {
+    intf_thread_t          *p_intf;
+    vlc_renderer_discovery *p_rd;
+}
+
+- (instancetype)initWithName:(const char*)name andLongname:(const char*)longname
+{
+    self = [super init];
+
+    if (self) {
+        if (!name)
+            return nil;
+
+        // Create renderer object
+        p_intf = getIntf();
+        p_rd = vlc_rd_new(VLC_OBJECT(p_intf), name);
+
+        if (!p_rd) {
+            msg_Err(p_intf, "Could not create '%s' renderer discovery service", name);
+            return nil;
+        }
+        _name = toNSStr(name);
+        _longName = (!longname) ? nil : toNSStr(longname);
+        _discoveryStarted = false;
+    }
+    return self;
+}
+
+- (void)dealloc
+{
+    if (_discoveryStarted)
+        [self stopDiscovery];
+    if (p_rd != NULL)
+        vlc_rd_release(p_rd);
+}
+
+- (bool)startDiscovery
+{
+    msg_Dbg(p_intf, "Starting renderer discovery service %s", _name.UTF8String);
+    [self attachEventHandlers];
+    int ret = vlc_rd_start(p_rd);
+    if (ret == VLC_SUCCESS) {
+        _discoveryStarted = true;
+        return true;
+    } else {
+        msg_Err(p_intf, "Could not start '%s' renderer discovery", _name.UTF8String);
+        [self detachEventHandler];
+        return false;
+    }
+}
+
+- (void)stopDiscovery
+{
+    if (_discoveryStarted) {
+        [self detachEventHandler];
+        vlc_rd_stop(p_rd);
+        _discoveryStarted = false;
+    }
+}
+
+- (void)attachEventHandlers
+{
+    vlc_event_manager_t *em = vlc_rd_event_manager(p_rd);
+    vlc_event_attach(em, vlc_RendererDiscoveryItemAdded, renderer_event_received, (__bridge void *)self);
+    vlc_event_attach(em, vlc_RendererDiscoveryItemRemoved, renderer_event_received, (__bridge void *)self);
+}
+
+- (void)detachEventHandler
+{
+    vlc_event_manager_t *em = vlc_rd_event_manager(p_rd);
+    vlc_event_detach(em, vlc_RendererDiscoveryItemAdded, renderer_event_received, (__bridge void *)self);
+    vlc_event_detach(em, vlc_RendererDiscoveryItemRemoved, renderer_event_received, (__bridge void *)self);
+}
+
+- (void)handleEvent:(const vlc_event_t *)event
+{
+    if (event->type == vlc_RendererDiscoveryItemAdded) {
+        vlc_renderer_item *base_item =  event->u.renderer_discovery_item_added.p_new_item;
+        VLCRendererItem *item = [[VLCRendererItem alloc] initWithRendererItem:base_item];
+        [_rendererItems addObject:item];
+        if (_delegate)
+            [_delegate addedRendererItem:item from:self];
+        return;
+    }
+    if (event->type == vlc_RendererDiscoveryItemAdded) {
+        vlc_renderer_item *base_item =  event->u.renderer_discovery_item_removed.p_item;
+
+        VLCRendererItem *result_item = nil;
+        for (VLCRendererItem *i in _rendererItems) {
+            if ([i isBaseItemEqualTo:base_item])
+                result_item = i;
+        }
+        if (result_item) {
+            [_rendererItems removeObject:result_item];
+            if (_delegate)
+                [_delegate removedRendererItem:result_item from:self];
+        } else {
+            msg_Err(p_intf, "VLCRendererDiscovery could not find item to remove!");
+        }
+        return;
+    }
+    msg_Err(p_intf, "VLCRendererDiscovery received event of unhandled type");
+}
+
+ at end
diff --git a/modules/gui/macosx/VLCRendererItem.h b/modules/gui/macosx/VLCRendererItem.h
new file mode 100644
index 0000000..e8ccca3
--- /dev/null
+++ b/modules/gui/macosx/VLCRendererItem.h
@@ -0,0 +1,91 @@
+/*****************************************************************************
+ * VLCRendererItem.h: Wrapper class for vlc_renderer_item
+ *****************************************************************************
+ * Copyright (C) 2016 VLC authors and VideoLAN
+ * $Id$
+ *
+ * Authors: Marvin Scholz <epirat07 at gmail dot com>
+ *
+ * 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.
+ *****************************************************************************/
+
+#import <Foundation/Foundation.h>
+
+#include <vlc_common.h>
+#include <vlc_playlist.h>
+
+/**
+ \c VLCRendererItem is a simple wrapper class for libvlc’s
+ \c vlc_renderer_item. It's initialized with the renderer item and
+ manages it's lifetime.
+ */
+ at interface VLCRendererItem : NSObject
+
+/**
+ Initialize the object with a renderer item, typically received from
+ a \c vlc_renderer_discovery event.
+ */
+- (instancetype)initWithRendererItem:(vlc_renderer_item*)item;
+- (void)dealloc;
+
+/**
+ The name of the renderer item
+ */
+- (NSString*)name;
+
+/**
+ The iconURI of the renderer item
+ */
+- (NSURL*)iconURI;
+
+/**
+ Flags indicating capabilities of the renderer item
+ 
+ Compare it to:
+    \li \c VLC_RENDERER_CAN_AUDIO
+    \li \c VLC_RENDERER_CAN_VIDEO
+ */
+- (int)flags;
+
+/**
+ Checks if the Item’s sout string is equivalent to the given
+ sout string. If output is YES, it's checked if it's an
+ output sout as well.
+ 
+ \param sout    The sout c string to compare with
+ \param output  Indicates wether to check if sout is an output
+ 
+ \return YES if souts match the given sout and output, NO otherwise
+ */
+- (bool)isSoutEqualTo:(const char*)sout asOutput:(bool)output;
+
+/**
+ Compares the address of the internal \c vlc_renderer_item to the address
+ of the specified item.
+ 
+ \param item    Item to compare with
+ 
+ \return YES if items have the same address, NO otherwise
+ */
+- (bool)isBaseItemEqualTo:(vlc_renderer_item*)item;
+
+/**
+ Sets the passed playlist’s sout to the sout of the \c VLCRendererItem.
+ 
+ \param playlist The playlist for which to set the sout
+ */
+- (void)setSoutForPlaylist:(playlist_t*)playlist;
+
+ at end
diff --git a/modules/gui/macosx/VLCRendererItem.m b/modules/gui/macosx/VLCRendererItem.m
new file mode 100644
index 0000000..a89c2c3
--- /dev/null
+++ b/modules/gui/macosx/VLCRendererItem.m
@@ -0,0 +1,105 @@
+/*****************************************************************************
+ * VLCRendererItem.m: Wrapper class for vlc_renderer_item
+ *****************************************************************************
+ * Copyright (C) 2016 VLC authors and VideoLAN
+ * $Id$
+ *
+ * Authors: Marvin Scholz <epirat07 at gmail dot com>
+ *
+ * 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.
+ *****************************************************************************/
+
+#import "VLCRendererItem.h"
+#import "StringUtility.h"
+
+#include <vlc_common.h>
+#include <vlc_renderer_discovery.h>
+
+
+ at implementation VLCRendererItem {
+    vlc_renderer_item *renderer_item;
+}
+
+- (instancetype)initWithRendererItem:(vlc_renderer_item*)item
+{
+    self = [super init];
+    if (self) {
+        if (!item)
+            return nil;
+        renderer_item = vlc_renderer_item_hold(item);
+    }
+    return self;
+}
+
+- (void)dealloc
+{
+    vlc_renderer_item_release(renderer_item);
+}
+
+- (NSString*)name
+{
+    const char *name = vlc_renderer_item_name(renderer_item);
+    if (!name)
+        return nil;
+    return toNSStr(name);
+}
+
+- (NSURL*)iconURI
+{
+    const char *uri = vlc_renderer_item_icon_uri(renderer_item);
+    if (!uri)
+        return nil;
+    return [NSURL URLWithString:toNSStr(uri)];
+}
+
+- (int)flags
+{
+    return vlc_renderer_item_flags(renderer_item);
+}
+
+- (bool)isSoutEqualTo:(const char*)sout asOutput:(bool)output
+{
+    NSString *tmp_sout;
+    if (!sout)
+        sout = "";
+    const char *psz_out = vlc_renderer_item_sout(renderer_item);
+        if (likely(psz_out != NULL)) {
+            if (output) {
+                tmp_sout = [NSString stringWithFormat:@"#%s", psz_out];
+            } else {
+                tmp_sout = toNSStr(psz_out);
+            }
+        }
+    return (strcmp(tmp_sout.UTF8String, sout) == 0);
+}
+
+- (bool)isBaseItemEqualTo:(vlc_renderer_item*)item
+{
+    return renderer_item == item;
+}
+
+- (void)setSoutForPlaylist:(playlist_t*)playlist
+{
+    NSString *sout;
+    const char *item_sout = vlc_renderer_item_sout(renderer_item);
+
+    if (!playlist || !item_sout)
+        return;
+
+    sout = [[NSString alloc] initWithFormat:@"#%s", item_sout];
+    var_SetString(playlist , "sout", sout.UTF8String);
+}
+
+ at end
-- 
2.7.4 (Apple Git-66)



More information about the vlc-devel mailing list