Commit 9d24ab8b authored by Soomin Lee's avatar Soomin Lee

VLCRendererDiscoverer: Track discovered renderer items

Creating a new instance of `VLCRendererItem` each time we got an event
from libvlc led to duplication and issues with item deletions. Thus,
this patch checks before creating a new instance, if a `VLCRendererItem`
exist with a `libvlc_renderer_item_t *`.
parent 25a85cd2
......@@ -27,6 +27,7 @@
#import "VLCStreamOutput.h"
#endif
#import "VLCMediaPlayer.h"
#import "VLCRendererItem.h"
/**
* Bridges functionality between libvlc and VLCMediaList implementation.
......@@ -146,6 +147,27 @@
- (id)initWithMediaPlayer:(VLCMediaPlayer *)mediaPlayer;
@end
/**
* Bridges functionality between libvlc and VLCRendererItem implementation.
*/
@interface VLCRendererItem (VLCRendererItemBridging)
/**
* Initializer method to create an VLCRendererItem with an `libvlc_renderer_item_t *`.
*
* \param renderer item.
* \note This initializer is not meant to be used externally.
* \return An instance of `VLCRendererItem`, can be nil.
*/
- (instancetype)initWithRendererItem:(void *)item;
/**
* Returns a `libvlc_renderer_item_t *` renderer item.
* \return Renderer item.
*/
- (void *)libVLCRendererItem;
@end
/**
* TODO: Documentation
*/
......
/*****************************************************************************
* VLCRendererItem+Init.h
*****************************************************************************
* Copyright © 2018 VLC authors, VideoLAN
* Copyright © 2018 Videolabs
*
* Authors: Soomin Lee<bubu@mikan.io>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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"
/**
* VLCRendererItem internal category in order to hide the C part to the users
*/
@interface VLCRendererItem (Internal)
/**
* Initializer method to create an VLCRendererItem with an libvlc_renderer_item_t *
*
* \param renderer item
* \note This initializer is not meant to be used externally
* \return An instance of `VLCRendererItem`, can be nil
*/
- (instancetype _Nullable)initWithCItem:(libvlc_renderer_item_t * _Nonnull)item;
/**
* Returns the C renderer item
* \return Renderer item
*/
- (libvlc_renderer_item_t * _Nonnull)rendererItem;
@end
......@@ -24,16 +24,18 @@
@class VLCRendererItem;
@class VLCRendererDiscoverer;
NS_ASSUME_NONNULL_BEGIN
/**
* Renderer Discoverer delegate protocol
* Allows to be notified upon discovery/changes of an renderer item
*/
@protocol VLCRendererDiscovererDelegate <NSObject>
- (void)rendererDiscovererItemAdded:(VLCRendererDiscoverer * _Nonnull)rendererDiscoverer
item:(VLCRendererItem * _Nonnull)item;
- (void)rendererDiscovererItemDeleted:(VLCRendererDiscoverer * _Nonnull)rendererDiscoverer
item:(VLCRendererItem * _Nonnull)item;
- (void)rendererDiscovererItemAdded:(VLCRendererDiscoverer *)rendererDiscoverer
item:(VLCRendererItem *)item;
- (void)rendererDiscovererItemDeleted:(VLCRendererDiscoverer *)rendererDiscoverer
item:(VLCRendererItem *)item;
@end
......@@ -45,12 +47,12 @@
/**
* Name of the renderer discoverer
*/
@property (nonatomic, readonly, copy) NSString * _Nonnull name;
@property (nonatomic, readonly, copy) NSString *name;
/**
* Long name of the renderer discoverer
*/
@property (nonatomic, readonly, copy) NSString * _Nonnull longName;
@property (nonatomic, readonly, copy) NSString *longName;
/**
* Instanciates an object that holds information about a renderer discoverer
......@@ -58,7 +60,7 @@
* \param longName Long name of the renderer discoverer
* \return A new `VLCRendererDiscovererDescription` object, only if there were no errors
*/
- (instancetype _Nonnull)initWithName:(NSString * _Nonnull)name longName:(NSString *_Nonnull)longName;
- (instancetype)initWithName:(NSString *)name longName:(NSString *)longName;
@end
......@@ -70,7 +72,12 @@
/**
* Name of the renderer discoverer
*/
@property (nonatomic, readonly, copy) NSString * _Nonnull name;
@property (nonatomic, readonly, copy) NSString *name;
/**
* Renderers of the discoverer
*/
@property (nonatomic, readonly, copy) NSArray<VLCRendererItem *> *renderers;
/**
* Receiver's delegate
......@@ -84,7 +91,14 @@
* \param name Name of the renderer discoverer
* \return A new `VLCRendererDiscoverer` object, only if there were no errors
*/
- (instancetype _Nullable)initWithName:(NSString * _Nonnull)name;
- (instancetype _Nullable)initWithName:(NSString *)name;
/**
* Returns discovered renderers
* \return discovered renderers
*/
- (NSArray<VLCRendererItem *> *)renderers;
/**
* Start the renderer discoverer
......@@ -107,3 +121,5 @@
+ (NSArray<VLCRendererDiscovererDescription *> * _Nullable)list;
@end
NS_ASSUME_NONNULL_END
......@@ -21,6 +21,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
NS_ASSUME_NONNULL_BEGIN
typedef NS_OPTIONS(NSInteger, VLCRendererPlay) {
/** The renderer can render audio */
VLCRendererPlaysAudio = 1 << 0,
......@@ -36,17 +38,17 @@ typedef NS_OPTIONS(NSInteger, VLCRendererPlay) {
/**
* Name of the renderer item
*/
@property (nonatomic, readonly, copy) NSString * _Nonnull name;
@property (nonatomic, readonly, copy) NSString *name;
/**
* For now, the type can only be "chromecast" ("upnp", "airplay" may come later)
*/
@property (nonatomic, readonly, copy) NSString * _Nonnull type;
@property (nonatomic, readonly, copy) NSString *type;
/**
* IconURI of the renderer item
*/
@property (nonatomic, readonly, copy) NSString * _Nonnull iconURI;
@property (nonatomic, readonly, copy) NSString *iconURI;
/**
* Flags of the renderer item
......@@ -60,3 +62,5 @@ typedef NS_OPTIONS(NSInteger, VLCRendererPlay) {
- (instancetype _Nullable)init NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END
......@@ -378,7 +378,6 @@
8D2CE648203DCC48004BB7F6 /* VLCRendererDiscoverer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = VLCRendererDiscoverer.m; path = Sources/VLCRendererDiscoverer.m; sourceTree = "<group>"; };
8D2CE64E203EEA47004BB7F6 /* VLCRendererItem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = VLCRendererItem.h; path = Headers/Public/VLCRendererItem.h; sourceTree = "<group>"; };
8D2CE64F203EEA47004BB7F6 /* VLCRendererItem.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = VLCRendererItem.m; path = Sources/VLCRendererItem.m; sourceTree = "<group>"; };
8D2CE653203F206F004BB7F6 /* VLCRendererItem+Init.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "VLCRendererItem+Init.h"; path = "Headers/Internal/VLCRendererItem+Init.h"; sourceTree = "<group>"; };
AA747D9E0F9514B9006C5449 /* MobilePrefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MobilePrefix.pch; path = Headers/PCH/MobilePrefix.pch; sourceTree = SOURCE_ROOT; };
AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
D2AAC07E0554694100DB518D /* libMobileVLCKit.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libMobileVLCKit.a; sourceTree = BUILT_PRODUCTS_DIR; };
......@@ -585,7 +584,6 @@
7D803EC41C8F2AEF00864A9C /* VLCiOSLegacyDialogProvider.h */,
7D34F5551C909DF6008A39F0 /* VLCCustomDialogProvider.h */,
7DB683D91C9961BA000C70BE /* VLCHelperCode.h */,
8D2CE653203F206F004BB7F6 /* VLCRendererItem+Init.h */,
);
name = Internal;
sourceTree = "<group>";
......
......@@ -30,7 +30,6 @@
#import "VLCMediaPlayer.h"
#import "VLCEventManager.h"
#import "VLCLibVLCBridging.h"
#import "VLCRendererItem+Init.h"
#if !TARGET_OS_IPHONE
# import "VLCVideoView.h"
#endif
......@@ -1290,7 +1289,7 @@ static void HandleMediaPlayerSnapshot(const libvlc_event_t * event, void * self)
- (BOOL)setRendererItem:(VLCRendererItem *)item
{
return libvlc_media_player_set_renderer(_playerInstance, item.rendererItem) == 0;
return libvlc_media_player_set_renderer(_playerInstance, item.libVLCRendererItem) == 0;
}
@end
......
......@@ -24,11 +24,12 @@
#import "VLCRendererDiscoverer.h"
#import "VLCLibrary.h"
#import "VLCEventManager.h"
#import "VLCRendererItem+Init.h"
#import "VLCLibVLCBridging.h"
@interface VLCRendererDiscoverer()
{
libvlc_renderer_discoverer_t *_rendererDiscoverer;
NSMutableArray<VLCRendererItem *> *_rendererItems;
}
@end
......@@ -39,7 +40,7 @@ static void HandleRendererDiscovererItemAdded(const libvlc_event_t *event, void
@autoreleasepool {
[[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
withMethod:@selector(itemAdded:)
withArgumentAsObject:[[VLCRendererItem alloc] initWithCItem:
withArgumentAsObject:[NSValue valueWithPointer:
event->u.renderer_discoverer_item_added.item]];
}
}
......@@ -49,8 +50,8 @@ static void HandleRendererDiscovererItemDeleted(const libvlc_event_t *event, voi
@autoreleasepool {
[[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
withMethod:@selector(itemDeleted:)
withArgumentAsObject:[[VLCRendererItem alloc] initWithCItem:
event->u.renderer_discoverer_item_deleted.item]];
withArgumentAsObject:[NSValue valueWithPointer:
event->u.renderer_discoverer_item_deleted.item]];
}
}
......@@ -90,6 +91,7 @@ static void HandleRendererDiscovererItemDeleted(const libvlc_event_t *event, voi
return nil;
}
_rendererItems = [[NSMutableArray alloc] init];
libvlc_event_manager_t *p_em = libvlc_renderer_discoverer_event_manager(_rendererDiscoverer);
if (p_em) {
......@@ -98,7 +100,6 @@ static void HandleRendererDiscovererItemDeleted(const libvlc_event_t *event, voi
libvlc_event_attach(p_em, libvlc_RendererDiscovererItemDeleted,
HandleRendererDiscovererItemDeleted, (__bridge void *)(self));
}
}
return self;
}
......@@ -153,16 +154,47 @@ static void HandleRendererDiscovererItemDeleted(const libvlc_event_t *event, voi
return [list copy];
}
- (VLCRendererItem *)discoveredItemsContainItem:(libvlc_renderer_item_t *)item
{
for (VLCRendererItem *rendererItem in _rendererItems) {
BOOL hasSameName = !strcmp(libvlc_renderer_item_name(rendererItem.libVLCRendererItem), libvlc_renderer_item_name(item));
BOOL hasSameType = !strcmp(libvlc_renderer_item_type(rendererItem.libVLCRendererItem), libvlc_renderer_item_type(item));
if (hasSameName && hasSameType) {
return rendererItem;
}
}
return nil;
}
- (NSArray<VLCRendererItem *> *)renderers
{
return [_rendererItems copy];
}
#pragma mark - Handling libvlc event callbacks
- (void)itemAdded:(VLCRendererItem *)item
- (void)itemAdded:(NSValue *)item
{
[_delegate rendererDiscovererItemAdded:self item:item];
libvlc_renderer_item_t *renderer_item = item.pointerValue;
VLCRendererItem *rendererItem = [self discoveredItemsContainItem:renderer_item];
if (!rendererItem) {
rendererItem = [[VLCRendererItem alloc] initWithRendererItem:renderer_item];
[_rendererItems addObject:rendererItem];
[_delegate rendererDiscovererItemAdded:self item:rendererItem];
}
}
- (void)itemDeleted:(VLCRendererItem *)item
- (void)itemDeleted:(NSValue *)item
{
[_delegate rendererDiscovererItemDeleted:self item:item];
libvlc_renderer_item_t *renderer_item = item.pointerValue;
VLCRendererItem *rendererItem = [self discoveredItemsContainItem:renderer_item];
if (rendererItem) {
[_rendererItems removeObject:rendererItem];
[_delegate rendererDiscovererItemDeleted:self item:rendererItem];
}
}
@end
......@@ -21,7 +21,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#import "VLCRendererItem+Init.h"
#import "VLCRendererItem.h"
@interface VLCRendererItem()
{
......@@ -40,9 +40,9 @@
@end
@implementation VLCRendererItem (Internal)
@implementation VLCRendererItem (VLCRendererItemBridging)
- (instancetype)initWithCItem:(libvlc_renderer_item_t *)item
- (instancetype)initWithRendererItem:(void *)item
{
self = [super init];
if (self) {
......@@ -67,7 +67,7 @@
return self;
}
- (libvlc_renderer_item_t *)rendererItem
- (void *)libVLCRendererItem
{
return _item;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment