VLCMediaDiscoverer.m 5.62 KB
Newer Older
1
/*****************************************************************************
2
 * VLCMediaDiscoverer.m: VLCKit.framework VLCMediaDiscoverer implementation
3 4
 *****************************************************************************
 * Copyright (C) 2007 Pierre d'Herbemont
5 6
 * Copyright (C) 2014-2015 Felix Paul Kühne
 * 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 47
    VLCLibrary *_privateLibrary;
}
48 49
@end

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

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

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

    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];
}

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

- (instancetype)initWithName:(NSString *)aServiceName libraryInstance:(VLCLibrary *)libraryInstance
91
{
92
    if (self = [super init]) {
93 94
        _localizedName = nil;
        _discoveredMedia = nil;
95

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

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

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

- (void)dealloc
{
116 117 118 119 120
    if (_mdis) {
        if (libvlc_media_discoverer_is_running(_mdis))
            libvlc_media_discoverer_stop(_mdis);
        libvlc_media_discoverer_release(_mdis);
    }
121

122 123
    [[VLCEventManager sharedManager] cancelCallToObject:self];

124
    libvlc_release(_privateLibrary.instance);
125 126
}

127 128 129
- (int)startDiscoverer
{
    int returnValue = libvlc_media_discoverer_start(_mdis);
130
    if (returnValue == -1) {
131
        VKLog(@"media discovery start failed");
132 133
        return returnValue;
    }
134

135 136 137 138 139 140
    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;

141 142 143 144 145
    return returnValue;
}

- (void)stopDiscoverer
{
146 147 148 149 150
    if ([NSThread isMainThread]) {
        [self performSelectorInBackground:@selector(stopDiscoverer) withObject:nil];
        return;
    }

151 152 153
    libvlc_media_discoverer_stop(_mdis);
}

154
- (VLCMediaList *)discoveredMedia
155
{
156
    return _discoveredMedia;
157 158 159 160
}

- (NSString *)localizedName
{
161 162
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
163 164
    if (_localizedName)
        return _localizedName;
165

166
    char *name = libvlc_media_discoverer_localized_name(_mdis);
167
    if (name) {
168
        _localizedName = @(name);
169
        free(name);
170
    }
171
    return _localizedName;
172
#pragma clang diagnostic pop
173
}
174 175 176

- (BOOL)isRunning
{
177
    return libvlc_media_discoverer_is_running(_mdis);;
178 179
}

180
@end