VLCMediaDiscoverer.m 3.6 KB

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