VLCMediaDiscoverer.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Felix Paul Kühne <fkuehne # videolan dot org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation; either version 2.1 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with this program; if not, write to the Free Software Foundation,
  24. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25. *****************************************************************************/
  26. #import "VLCMediaDiscoverer.h"
  27. #import "VLCLibrary.h"
  28. #import "VLCLibVLCBridging.h"
  29. #import "VLCEventManager.h"
  30. #include <vlc/vlc.h>
  31. #include <vlc/libvlc.h>
  32. #include <vlc/libvlc_media_discoverer.h>
  33. NSString *const VLCMediaDiscovererName = @"VLCMediaDiscovererName";
  34. NSString *const VLCMediaDiscovererLongName = @"VLCMediaDiscovererLongName";
  35. NSString *const VLCMediaDiscovererCategory = @"VLCMediaDiscovererCategory";
  36. @interface VLCMediaDiscoverer ()
  37. {
  38. NSString *_localizedName;
  39. VLCMediaList *_discoveredMedia;
  40. libvlc_media_discoverer_t *_mdis;
  41. VLCLibrary *_privateLibrary;
  42. dispatch_queue_t _libVLCBackgroundQueue;
  43. }
  44. @end
  45. @implementation VLCMediaDiscoverer
  46. @synthesize libraryInstance = _privateLibrary;
  47. + (NSArray *)availableMediaDiscoverer
  48. {
  49. return @[];
  50. }
  51. + (NSArray *)availableMediaDiscovererForCategoryType:(VLCMediaDiscovererCategoryType)categoryType
  52. {
  53. libvlc_media_discoverer_description_t **discoverers;
  54. ssize_t numberOfDiscoverers = libvlc_media_discoverer_list_get([VLCLibrary sharedInstance], (libvlc_media_discoverer_category_t)categoryType, &discoverers);
  55. if (numberOfDiscoverers == 0) {
  56. libvlc_media_discoverer_list_release(discoverers, numberOfDiscoverers);
  57. return @[];
  58. }
  59. NSMutableArray *mutArray = [NSMutableArray arrayWithCapacity:numberOfDiscoverers];
  60. for (unsigned u = 0; u < numberOfDiscoverers; u++) {
  61. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  62. discoverers[u]->psz_name ? [NSString stringWithUTF8String:discoverers[u]->psz_name] : @"",
  63. VLCMediaDiscovererName,
  64. discoverers[u]->psz_longname ? [NSString stringWithUTF8String:discoverers[u]->psz_longname] : @"",
  65. VLCMediaDiscovererLongName,
  66. @(discoverers[u]->i_cat),
  67. VLCMediaDiscovererCategory,
  68. nil];
  69. [mutArray addObject:dict];
  70. }
  71. libvlc_media_discoverer_list_release(discoverers, numberOfDiscoverers);
  72. return [mutArray copy];
  73. }
  74. - (instancetype)initWithName:(NSString *)aServiceName
  75. {
  76. return [self initWithName:aServiceName libraryInstance:nil];
  77. }
  78. - (instancetype)initWithName:(NSString *)aServiceName libraryInstance:(VLCLibrary *)libraryInstance
  79. {
  80. if (self = [super init]) {
  81. _localizedName = nil;
  82. _discoveredMedia = nil;
  83. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  84. if (libraryInstance != nil) {
  85. _privateLibrary = libraryInstance;
  86. } else {
  87. _privateLibrary = [VLCLibrary sharedLibrary];
  88. }
  89. libvlc_retain([_privateLibrary instance]);
  90. _mdis = libvlc_media_discoverer_new([_privateLibrary instance],
  91. [aServiceName UTF8String]);
  92. if (_mdis == NULL) {
  93. VKLog(@"media discovery initialization failed, maybe no such module?");
  94. return NULL;
  95. }
  96. }
  97. return self;
  98. }
  99. - (void)dealloc
  100. {
  101. [[VLCEventManager sharedManager] cancelCallToObject:self];
  102. _discoveredMedia = nil;
  103. if (_mdis) {
  104. if (libvlc_media_discoverer_is_running(_mdis))
  105. libvlc_media_discoverer_stop(_mdis);
  106. libvlc_media_discoverer_release(_mdis);
  107. }
  108. libvlc_release(_privateLibrary.instance);
  109. }
  110. - (int)startDiscoverer
  111. {
  112. int returnValue = libvlc_media_discoverer_start(_mdis);
  113. if (returnValue == -1) {
  114. VKLog(@"media discovery start failed");
  115. return returnValue;
  116. }
  117. libvlc_media_list_t *p_mlist = libvlc_media_discoverer_media_list(_mdis);
  118. VLCMediaList *ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  119. libvlc_media_list_release(p_mlist);
  120. _discoveredMedia = ret;
  121. return returnValue;
  122. }
  123. - (void)stopDiscoverer
  124. {
  125. if (![self isRunning]) {
  126. return;
  127. }
  128. dispatch_async(_libVLCBackgroundQueue, ^{
  129. libvlc_media_discoverer_stop(_mdis);
  130. });
  131. }
  132. - (VLCMediaList *)discoveredMedia
  133. {
  134. return _discoveredMedia;
  135. }
  136. - (NSString *)localizedName
  137. {
  138. #pragma clang diagnostic push
  139. #pragma clang diagnostic ignored "-Wdeprecated"
  140. if (_localizedName)
  141. return _localizedName;
  142. char *name = libvlc_media_discoverer_localized_name(_mdis);
  143. if (name) {
  144. _localizedName = @(name);
  145. free(name);
  146. }
  147. return _localizedName;
  148. #pragma clang diagnostic pop
  149. }
  150. - (BOOL)isRunning
  151. {
  152. return libvlc_media_discoverer_is_running(_mdis);;
  153. }
  154. @end