VLCMediaDiscoverer.m 5.75 KB
Newer Older
1
/*****************************************************************************
2
 * VLCMediaDiscoverer.m: VLCKit.framework VLCMediaDiscoverer implementation
3 4
 *****************************************************************************
 * Copyright (C) 2007 Pierre d'Herbemont
5
 * Copyright (C) 2014-2017 Felix Paul Kühne
6
 * Copyright (C) 2007, 2015 VLC authors and VideoLAN
7 8 9
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
10
 *          Felix Paul Kühne <fkuehne # videolan dot org>
11
 *
12 13 14
 * 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
15 16 17 18
 * (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
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
22 23 24
 * 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.
25 26
 *****************************************************************************/

27
#import "VLCMediaDiscoverer.h"
28
#import "VLCLibrary.h"
29
#import "VLCLibVLCBridging.h"
30
#import "VLCEventManager.h"
31

32
#include <vlc/vlc.h>
33
#include <vlc/libvlc.h>
34
#include <vlc/libvlc_media_discoverer.h>
35

36 37 38 39
NSString *const VLCMediaDiscovererName = @"VLCMediaDiscovererName";
NSString *const VLCMediaDiscovererLongName = @"VLCMediaDiscovererLongName";
NSString *const VLCMediaDiscovererCategory = @"VLCMediaDiscovererCategory";

40 41
@interface VLCMediaDiscoverer ()
{
42 43
    NSString *_localizedName;
    VLCMediaList *_discoveredMedia;
44
    libvlc_media_discoverer_t *_mdis;
45

46
    VLCLibrary *_privateLibrary;
47
    dispatch_queue_t _libVLCBackgroundQueue;
48
}
49 50
@end

51
@implementation VLCMediaDiscoverer
52 53
@synthesize libraryInstance = _privateLibrary;

54 55
+ (NSArray *)availableMediaDiscoverer
{
56
    return @[];
57 58
}

59 60
+ (NSArray *)availableMediaDiscovererForCategoryType:(VLCMediaDiscovererCategoryType)categoryType
{
61
    libvlc_media_discoverer_description_t **discoverers;
62
    ssize_t numberOfDiscoverers = libvlc_media_discoverer_list_get([VLCLibrary sharedInstance], (libvlc_media_discoverer_category_t)categoryType, &discoverers);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

    if (numberOfDiscoverers == 0) {
        libvlc_media_discoverer_list_release(discoverers, numberOfDiscoverers);
        return @[];
    }

    NSMutableArray *mutArray = [NSMutableArray arrayWithCapacity:numberOfDiscoverers];
    for (unsigned u = 0; u < numberOfDiscoverers; u++) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              discoverers[u]->psz_name ? [NSString stringWithUTF8String:discoverers[u]->psz_name] : @"",
                              VLCMediaDiscovererName,
                              discoverers[u]->psz_longname ? [NSString stringWithUTF8String:discoverers[u]->psz_longname] : @"",
                              VLCMediaDiscovererLongName,
                              @(discoverers[u]->i_cat),
                              VLCMediaDiscovererCategory,
                              nil];
        [mutArray addObject:dict];
    }

    libvlc_media_discoverer_list_release(discoverers, numberOfDiscoverers);
    return [mutArray copy];
}

86
- (instancetype)initWithName:(NSString *)aServiceName
87 88 89 90 91
{
    return [self initWithName:aServiceName libraryInstance:nil];
}

- (instancetype)initWithName:(NSString *)aServiceName libraryInstance:(VLCLibrary *)libraryInstance
92
{
93
    if (self = [super init]) {
94 95
        _localizedName = nil;
        _discoveredMedia = nil;
96
        _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
97

98 99 100 101 102
        if (libraryInstance != nil) {
            _privateLibrary = libraryInstance;
        } else {
            _privateLibrary = [VLCLibrary sharedLibrary];
        }
103
        libvlc_retain([_privateLibrary instance]);
104

105
        _mdis = libvlc_media_discoverer_new([_privateLibrary instance],
106 107 108 109 110 111
                                            [aServiceName UTF8String]);

        if (_mdis == NULL) {
            VKLog(@"media discovery initialization failed, maybe no such module?");
            return NULL;
        }
112 113 114 115 116 117
    }
    return self;
}

- (void)dealloc
{
118 119
    [[VLCEventManager sharedManager] cancelCallToObject:self];

120 121
    _discoveredMedia = nil;

122
    if (_mdis) {
123 124 125
        if (libvlc_media_discoverer_is_running(_mdis))
            libvlc_media_discoverer_stop(_mdis);
        libvlc_media_discoverer_release(_mdis);
126
    }
127

128
    libvlc_release(_privateLibrary.instance);
129 130
}

131 132 133
- (int)startDiscoverer
{
    int returnValue = libvlc_media_discoverer_start(_mdis);
134
    if (returnValue == -1) {
135
        VKLog(@"media discovery start failed");
136 137
        return returnValue;
    }
138

139 140 141 142 143 144
    libvlc_media_list_t *p_mlist = libvlc_media_discoverer_media_list(_mdis);
    VLCMediaList *ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
    libvlc_media_list_release(p_mlist);

    _discoveredMedia = ret;

145 146 147 148 149
    return returnValue;
}

- (void)stopDiscoverer
{
150
    if (![self isRunning]) {
151 152 153
        return;
    }

154 155 156
    dispatch_async(_libVLCBackgroundQueue, ^{
        libvlc_media_discoverer_stop(_mdis);
    });
157 158
}

159
- (VLCMediaList *)discoveredMedia
160
{
161
    return _discoveredMedia;
162 163 164 165
}

- (NSString *)localizedName
{
166 167
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
168 169
    if (_localizedName)
        return _localizedName;
170

171
    char *name = libvlc_media_discoverer_localized_name(_mdis);
172
    if (name) {
173
        _localizedName = @(name);
174
        free(name);
175
    }
176
    return _localizedName;
177
#pragma clang diagnostic pop
178
}
179 180 181

- (BOOL)isRunning
{
182
    return libvlc_media_discoverer_is_running(_mdis);;
183 184
}

185
@end