VLCRendererDiscovery.m 4.14 KB
Newer Older
1
/*****************************************************************************
2
 * VLCRendererDiscovery.m: Wrapper class for vlc_renderer_discovery_t
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *****************************************************************************
 * 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"

26
#import "VLCMain.h"
27 28 29 30 31 32

#include <vlc_common.h>
#include <vlc_renderer_discovery.h>

@interface VLCRendererDiscovery ()
{
33 34
    intf_thread_t               *p_intf;
    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 41
@end

42 43 44
// C callback event handler functions
static void renderer_event_item_added(vlc_renderer_discovery_t *rd,
                                      vlc_renderer_item_t *item)
45
{
46 47 48 49 50 51 52 53 54
    VLCRendererDiscovery *target = (__bridge VLCRendererDiscovery*)rd->owner.sys;
    [target handleItemAdded:item];
}

static void renderer_event_item_removed(vlc_renderer_discovery_t *rd,
                                        vlc_renderer_item_t *item)
{
    VLCRendererDiscovery *target = (__bridge VLCRendererDiscovery*)rd->owner.sys;
    [target handleItemRemoved:item];
55 56 57 58 59 60 61 62 63 64 65 66
}

@implementation VLCRendererDiscovery

- (instancetype)initWithName:(const char*)name andLongname:(const char*)longname
{
    self = [super init];

    if (self) {
        if (!name)
            [NSException raise:NSInvalidArgumentException
                        format:@"name must not be nil"];
67 68
        _name = [NSString stringWithUTF8String:name];
        _longName = (!longname) ? nil : [NSString stringWithUTF8String:longname];
69
        _rendererItems = [NSMutableArray array];
70 71 72 73 74 75
    }
    return self;
}

- (void)dealloc
{
76
    [self stopDiscovery];
77 78 79 80
}

- (bool)startDiscovery
{
81 82 83 84
    if (_isRunning) {
        return YES;
    }

85 86
    struct vlc_renderer_discovery_owner owner =
    {
Thomas Guillem's avatar
Thomas Guillem committed
87
        (__bridge void *) self,
88 89 90 91
        renderer_event_item_added,
        renderer_event_item_removed,
    };

92 93
    p_intf = getIntf();

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 101 102 103
        msg_Err(p_intf, "Could not create '%s' renderer discovery service", _name.UTF8String);
        return false;
    }

104
    _isRunning = YES;
105
    return true;
106 107 108 109
}

- (void)stopDiscovery
{
110 111 112
    if (p_rd != NULL) {
        vlc_rd_release(p_rd);
        p_rd = NULL;
113
        _isRunning = NO;
114 115 116
    }
}

117
- (void)handleItemAdded:(vlc_renderer_item_t *)base_item
118
{
119 120 121 122
    VLCRendererItem *item = [[VLCRendererItem alloc] initWithRendererItem:base_item];
    [_rendererItems addObject:item];
    if (_delegate)
        [_delegate addedRendererItem:item from:self];
123 124
}

125
- (void)handleItemRemoved:(const vlc_renderer_item_t *)base_item
126
{
127 128 129 130
    VLCRendererItem *result_item = nil;
    for (VLCRendererItem *item in _rendererItems) {
        if (item.rendererItem == base_item) {
            result_item = item;
131
            break;
132 133
        }
    }
134 135 136
    if (result_item) {
        if (_delegate)
            [_delegate removedRendererItem:result_item from:self];
137
        [_rendererItems removeObject:result_item];
138 139 140
    } else {
        msg_Err(p_intf, "VLCRendererDiscovery could not find item to remove!");
    }
141 142 143
}

@end