VLCMediaDiscoverer.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/libvlc.h>
  31. #include <vlc/libvlc_media_discoverer.h>
  32. @interface VLCMediaDiscoverer ()
  33. {
  34. NSString *_localizedName;
  35. VLCMediaList *_discoveredMedia;
  36. void *_mdis;
  37. VLCLibrary *_privateLibrary;
  38. }
  39. @end
  40. @implementation VLCMediaDiscoverer
  41. @synthesize libraryInstance = _privateLibrary;
  42. + (NSArray *)availableMediaDiscoverer
  43. {
  44. return @[];
  45. }
  46. - (instancetype)initWithName:(NSString *)aServiceName
  47. {
  48. return [self initWithName:aServiceName libraryInstance:nil];
  49. }
  50. - (instancetype)initWithName:(NSString *)aServiceName libraryInstance:(VLCLibrary *)libraryInstance
  51. {
  52. if (self = [super init]) {
  53. _localizedName = nil;
  54. _discoveredMedia = nil;
  55. if (libraryInstance != nil) {
  56. _privateLibrary = libraryInstance;
  57. } else {
  58. _privateLibrary = [VLCLibrary sharedLibrary];
  59. }
  60. libvlc_retain([_privateLibrary instance]);
  61. _mdis = libvlc_media_discoverer_new([_privateLibrary instance],
  62. [aServiceName UTF8String]);
  63. if (_mdis == NULL) {
  64. VKLog(@"media discovery initialization failed, maybe no such module?");
  65. return NULL;
  66. }
  67. }
  68. return self;
  69. }
  70. - (void)dealloc
  71. {
  72. if (_mdis) {
  73. if (libvlc_media_discoverer_is_running(_mdis))
  74. libvlc_media_discoverer_stop(_mdis);
  75. libvlc_media_discoverer_release(_mdis);
  76. }
  77. [[VLCEventManager sharedManager] cancelCallToObject:self];
  78. libvlc_release(_privateLibrary.instance);
  79. }
  80. - (int)startDiscoverer
  81. {
  82. int returnValue = libvlc_media_discoverer_start(_mdis);
  83. if (returnValue == -1) {
  84. VKLog(@"media discovery start failed");
  85. return returnValue;
  86. }
  87. libvlc_media_list_t *p_mlist = libvlc_media_discoverer_media_list(_mdis);
  88. VLCMediaList *ret = [VLCMediaList mediaListWithLibVLCMediaList:p_mlist];
  89. libvlc_media_list_release(p_mlist);
  90. _discoveredMedia = ret;
  91. return returnValue;
  92. }
  93. - (void)stopDiscoverer
  94. {
  95. if ([NSThread isMainThread]) {
  96. [self performSelectorInBackground:@selector(stopDiscoverer) withObject:nil];
  97. return;
  98. }
  99. libvlc_media_discoverer_stop(_mdis);
  100. }
  101. - (VLCMediaList *)discoveredMedia
  102. {
  103. return _discoveredMedia;
  104. }
  105. - (NSString *)localizedName
  106. {
  107. if (_localizedName)
  108. return _localizedName;
  109. char *name = libvlc_media_discoverer_localized_name(_mdis);
  110. if (name) {
  111. _localizedName = @(name);
  112. free(name);
  113. }
  114. return _localizedName;
  115. }
  116. - (BOOL)isRunning
  117. {
  118. return libvlc_media_discoverer_is_running(_mdis);;
  119. }
  120. @end