VLCMediaDiscoverer.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. VLCMediaList *_discoveredMedia;
  39. libvlc_media_discoverer_t *_mdis;
  40. VLCLibrary *_privateLibrary;
  41. dispatch_queue_t _libVLCBackgroundQueue;
  42. }
  43. @end
  44. @implementation VLCMediaDiscoverer
  45. @synthesize libraryInstance = _privateLibrary;
  46. + (NSArray *)availableMediaDiscovererForCategoryType:(VLCMediaDiscovererCategoryType)categoryType
  47. {
  48. libvlc_media_discoverer_description_t **discoverers;
  49. ssize_t numberOfDiscoverers = libvlc_media_discoverer_list_get([VLCLibrary sharedInstance], (libvlc_media_discoverer_category_t)categoryType, &discoverers);
  50. if (numberOfDiscoverers == 0) {
  51. libvlc_media_discoverer_list_release(discoverers, numberOfDiscoverers);
  52. return @[];
  53. }
  54. NSMutableArray *mutArray = [NSMutableArray arrayWithCapacity:numberOfDiscoverers];
  55. for (unsigned u = 0; u < numberOfDiscoverers; u++) {
  56. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  57. discoverers[u]->psz_name ? [NSString stringWithUTF8String:discoverers[u]->psz_name] : @"",
  58. VLCMediaDiscovererName,
  59. discoverers[u]->psz_longname ? [NSString stringWithUTF8String:discoverers[u]->psz_longname] : @"",
  60. VLCMediaDiscovererLongName,
  61. @(discoverers[u]->i_cat),
  62. VLCMediaDiscovererCategory,
  63. nil];
  64. [mutArray addObject:dict];
  65. }
  66. libvlc_media_discoverer_list_release(discoverers, numberOfDiscoverers);
  67. return [mutArray copy];
  68. }
  69. - (instancetype)initWithName:(NSString *)aServiceName
  70. {
  71. return [self initWithName:aServiceName libraryInstance:nil];
  72. }
  73. - (instancetype)initWithName:(NSString *)aServiceName libraryInstance:(VLCLibrary *)libraryInstance
  74. {
  75. if (self = [super init]) {
  76. _discoveredMedia = nil;
  77. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  78. if (libraryInstance != nil) {
  79. _privateLibrary = libraryInstance;
  80. } else {
  81. _privateLibrary = [VLCLibrary sharedLibrary];
  82. }
  83. libvlc_retain([_privateLibrary instance]);
  84. _mdis = libvlc_media_discoverer_new([_privateLibrary instance],
  85. [aServiceName UTF8String]);
  86. if (_mdis == NULL) {
  87. VKLog(@"media discovery initialization failed, maybe no such module?");
  88. return NULL;
  89. }
  90. }
  91. return self;
  92. }
  93. - (void)dealloc
  94. {
  95. [[VLCEventManager sharedManager] cancelCallToObject:self];
  96. _discoveredMedia = nil;
  97. if (_mdis) {
  98. if (libvlc_media_discoverer_is_running(_mdis))
  99. libvlc_media_discoverer_stop(_mdis);
  100. libvlc_media_discoverer_release(_mdis);
  101. }
  102. libvlc_release(_privateLibrary.instance);
  103. }
  104. - (int)startDiscoverer
  105. {
  106. int returnValue = libvlc_media_discoverer_start(_mdis);
  107. if (returnValue == -1) {
  108. VKLog(@"media discovery start failed");
  109. return returnValue;
  110. }
  111. libvlc_media_list_t *p_mlist = libvlc_media_discoverer_media_list(_mdis);
  112. VLCMediaList *ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  113. libvlc_media_list_release(p_mlist);
  114. _discoveredMedia = ret;
  115. return returnValue;
  116. }
  117. - (void)stopDiscoverer
  118. {
  119. if (![self isRunning]) {
  120. return;
  121. }
  122. dispatch_async(_libVLCBackgroundQueue, ^{
  123. libvlc_media_discoverer_stop(_mdis);
  124. });
  125. }
  126. - (VLCMediaList *)discoveredMedia
  127. {
  128. return _discoveredMedia;
  129. }
  130. - (BOOL)isRunning
  131. {
  132. return libvlc_media_discoverer_is_running(_mdis);;
  133. }
  134. @end