VLCMediaDiscoverer.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*****************************************************************************
  2. * VLCMediaDiscoverer.m: VLCKit.framework VLCMediaDiscoverer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2014-2015 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. }
  43. @end
  44. @implementation VLCMediaDiscoverer
  45. @synthesize libraryInstance = _privateLibrary;
  46. + (NSArray *)availableMediaDiscoverer
  47. {
  48. return @[];
  49. }
  50. + (NSArray *)availableMediaDiscovererForCategoryType:(VLCMediaDiscovererCategoryType)categoryType
  51. {
  52. libvlc_media_discoverer_description_t **discoverers;
  53. ssize_t numberOfDiscoverers = libvlc_media_discoverer_list_get([VLCLibrary sharedInstance], (libvlc_media_discoverer_category_t)categoryType, &discoverers);
  54. if (numberOfDiscoverers == 0) {
  55. libvlc_media_discoverer_list_release(discoverers, numberOfDiscoverers);
  56. return @[];
  57. }
  58. NSMutableArray *mutArray = [NSMutableArray arrayWithCapacity:numberOfDiscoverers];
  59. for (unsigned u = 0; u < numberOfDiscoverers; u++) {
  60. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  61. discoverers[u]->psz_name ? [NSString stringWithUTF8String:discoverers[u]->psz_name] : @"",
  62. VLCMediaDiscovererName,
  63. discoverers[u]->psz_longname ? [NSString stringWithUTF8String:discoverers[u]->psz_longname] : @"",
  64. VLCMediaDiscovererLongName,
  65. @(discoverers[u]->i_cat),
  66. VLCMediaDiscovererCategory,
  67. nil];
  68. [mutArray addObject:dict];
  69. }
  70. libvlc_media_discoverer_list_release(discoverers, numberOfDiscoverers);
  71. return [mutArray copy];
  72. }
  73. - (instancetype)initWithName:(NSString *)aServiceName
  74. {
  75. return [self initWithName:aServiceName libraryInstance:nil];
  76. }
  77. - (instancetype)initWithName:(NSString *)aServiceName libraryInstance:(VLCLibrary *)libraryInstance
  78. {
  79. if (self = [super init]) {
  80. _localizedName = nil;
  81. _discoveredMedia = nil;
  82. if (libraryInstance != nil) {
  83. _privateLibrary = libraryInstance;
  84. } else {
  85. _privateLibrary = [VLCLibrary sharedLibrary];
  86. }
  87. libvlc_retain([_privateLibrary instance]);
  88. _mdis = libvlc_media_discoverer_new([_privateLibrary instance],
  89. [aServiceName UTF8String]);
  90. if (_mdis == NULL) {
  91. VKLog(@"media discovery initialization failed, maybe no such module?");
  92. return NULL;
  93. }
  94. }
  95. return self;
  96. }
  97. - (void)dealloc
  98. {
  99. if (_mdis) {
  100. if (libvlc_media_discoverer_is_running(_mdis))
  101. libvlc_media_discoverer_stop(_mdis);
  102. libvlc_media_discoverer_release(_mdis);
  103. }
  104. [[VLCEventManager sharedManager] cancelCallToObject:self];
  105. libvlc_release(_privateLibrary.instance);
  106. }
  107. - (int)startDiscoverer
  108. {
  109. int returnValue = libvlc_media_discoverer_start(_mdis);
  110. if (returnValue == -1) {
  111. VKLog(@"media discovery start failed");
  112. return returnValue;
  113. }
  114. libvlc_media_list_t *p_mlist = libvlc_media_discoverer_media_list(_mdis);
  115. VLCMediaList *ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  116. libvlc_media_list_release(p_mlist);
  117. _discoveredMedia = ret;
  118. return returnValue;
  119. }
  120. - (void)stopDiscoverer
  121. {
  122. if ([NSThread isMainThread]) {
  123. [self performSelectorInBackground:@selector(stopDiscoverer) withObject:nil];
  124. return;
  125. }
  126. libvlc_media_discoverer_stop(_mdis);
  127. }
  128. - (VLCMediaList *)discoveredMedia
  129. {
  130. return _discoveredMedia;
  131. }
  132. - (NSString *)localizedName
  133. {
  134. #pragma clang diagnostic push
  135. #pragma clang diagnostic ignored "-Wdeprecated"
  136. if (_localizedName)
  137. return _localizedName;
  138. char *name = libvlc_media_discoverer_localized_name(_mdis);
  139. if (name) {
  140. _localizedName = @(name);
  141. free(name);
  142. }
  143. return _localizedName;
  144. #pragma clang diagnostic pop
  145. }
  146. - (BOOL)isRunning
  147. {
  148. return libvlc_media_discoverer_is_running(_mdis);;
  149. }
  150. @end