1/*****************************************************************************
2 * VLCRendererDiscovery.m: Wrapper class for vlc_renderer_discovery_t
3 *****************************************************************************
4 * Copyright (C) 2016 VLC authors and VideoLAN
5 * $Id: 198ca8ce4a6aeca571464f78e3e9db704fb6feec $
6 *
7 * Authors: Marvin Scholz <epirat07 at gmail dot com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
23
24#import "VLCRendererDiscovery.h"
25
26#import "VLCMain.h"
27
28#include <vlc_common.h>
29#include <vlc_renderer_discovery.h>
30
31@interface VLCRendererDiscovery ()
32{
33    intf_thread_t               *p_intf;
34    vlc_renderer_discovery_t    *p_rd;
35    BOOL                        _isRunning;
36}
37
38- (void)handleItemAdded:(vlc_renderer_item_t *)item;
39- (void)handleItemRemoved:(const vlc_renderer_item_t *)item;
40@end
41
42// C callback event handler functions
43static void renderer_event_item_added(vlc_renderer_discovery_t *rd,
44                                      vlc_renderer_item_t *item)
45{
46    VLCRendererDiscovery *target = (__bridge VLCRendererDiscovery*)rd->owner.sys;
47    [target handleItemAdded:item];
48}
49
50static void renderer_event_item_removed(vlc_renderer_discovery_t *rd,
51                                        vlc_renderer_item_t *item)
52{
53    VLCRendererDiscovery *target = (__bridge VLCRendererDiscovery*)rd->owner.sys;
54    [target handleItemRemoved:item];
55}
56
57@implementation VLCRendererDiscovery
58
59- (instancetype)initWithName:(const char*)name andLongname:(const char*)longname
60{
61    self = [super init];
62
63    if (self) {
64        if (!name)
65            [NSException raise:NSInvalidArgumentException
66                        format:@"name must not be nil"];
67        _name = [NSString stringWithUTF8String:name];
68        _longName = (!longname) ? nil : [NSString stringWithUTF8String:longname];
69        _rendererItems = [NSMutableArray array];
70    }
71    return self;
72}
73
74- (void)dealloc
75{
76    [self stopDiscovery];
77}
78
79- (bool)startDiscovery
80{
81    if (_isRunning) {
82        return YES;
83    }
84
85    struct vlc_renderer_discovery_owner owner =
86    {
87        (__bridge void *) self,
88        renderer_event_item_added,
89        renderer_event_item_removed,
90    };
91
92    p_intf = getIntf();
93
94    msg_Dbg(p_intf, "Starting renderer discovery service %s", _name.UTF8String);
95    // Create renderer object
96    p_rd = vlc_rd_new(VLC_OBJECT(p_intf), _name.UTF8String, &owner);
97
98    if (!p_rd) {
99        _isRunning = NO;
100        msg_Err(p_intf, "Could not create '%s' renderer discovery service", _name.UTF8String);
101        return false;
102    }
103
104    _isRunning = YES;
105    return true;
106}
107
108- (void)stopDiscovery
109{
110    if (p_rd != NULL) {
111        vlc_rd_release(p_rd);
112        p_rd = NULL;
113        _isRunning = NO;
114    }
115}
116
117- (void)handleItemAdded:(vlc_renderer_item_t *)base_item
118{
119    VLCRendererItem *item = [[VLCRendererItem alloc] initWithRendererItem:base_item];
120    [_rendererItems addObject:item];
121    if (_delegate)
122        [_delegate addedRendererItem:item from:self];
123}
124
125- (void)handleItemRemoved:(const vlc_renderer_item_t *)base_item
126{
127    VLCRendererItem *result_item = nil;
128    for (VLCRendererItem *item in _rendererItems) {
129        if (item.rendererItem == base_item) {
130            result_item = item;
131            break;
132        }
133    }
134    if (result_item) {
135        if (_delegate)
136            [_delegate removedRendererItem:result_item from:self];
137        [_rendererItems removeObject:result_item];
138    } else {
139        msg_Err(p_intf, "VLCRendererDiscovery could not find item to remove!");
140    }
141}
142
143@end
144