VLCStreamSession.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*****************************************************************************
  2. * VLCStreamSession.m: VLCKit.framework VLCStreamSession implementation
  3. *****************************************************************************
  4. * Copyright (C) 2008 Pierre d'Herbemont
  5. * Copyright (C) 2008 VLC authors and VideoLAN
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import "VLCStreamSession.h"
  25. #import "VLCLibVLCBridging.h"
  26. @interface VLCStreamSession ()
  27. @property (nonatomic, readwrite) BOOL isComplete;
  28. @property (nonatomic, readwrite) NSUInteger reattemptedConnections;
  29. @end
  30. @implementation VLCStreamSession
  31. - (instancetype)init
  32. {
  33. if( self = [super init] )
  34. {
  35. [self addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
  36. }
  37. return self;
  38. }
  39. - (void)dealloc
  40. {
  41. [self removeObserver:self forKeyPath:@"state"];
  42. }
  43. + (instancetype)streamSession
  44. {
  45. return [[self alloc] init];
  46. }
  47. - (void)startStreaming
  48. {
  49. self.isComplete = NO;
  50. [self play];
  51. }
  52. - (void)stopStreaming
  53. {
  54. self.isComplete = YES;
  55. [super stop];
  56. }
  57. - (void)play
  58. {
  59. NSString * libvlcArgs;
  60. if( self.drawable )
  61. libvlcArgs = [NSString stringWithFormat:@"#duplicate{dst=display,dst=\"%@\"}",[_streamOutput representedLibVLCOptions]];
  62. else
  63. libvlcArgs = [_streamOutput representedLibVLCOptions];
  64. if( libvlcArgs )
  65. {
  66. [super setMedia:[VLCMedia mediaWithMedia:self.media
  67. andLibVLCOptions:@{
  68. @"sout" : libvlcArgs
  69. }]];
  70. }
  71. else
  72. {
  73. [super setMedia: self.media];
  74. }
  75. [super play];
  76. }
  77. + (NSSet *)keyPathsForValuesAffectingDescription
  78. {
  79. return [NSSet setWithObjects:@"isCompleted", @"state", nil];
  80. }
  81. - (NSString *)description
  82. {
  83. if ([self isComplete])
  84. return @"Done.";
  85. if ([self state] == VLCMediaPlayerStateError)
  86. return @"Error while Converting. Open Console.app to diagnose.";
  87. return @"Converting...";
  88. }
  89. + (NSSet *)keyPathsForValuesAffectingEncounteredError
  90. {
  91. return [NSSet setWithObjects:@"state", nil];
  92. }
  93. - (BOOL)encounteredError;
  94. {
  95. return ([self state] == VLCMediaPlayerStateError);
  96. }
  97. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  98. {
  99. if([keyPath isEqualToString:@"state"])
  100. {
  101. if( (([self position] == 1.0 || [self state] == VLCMediaPlayerStateEnded || ([self state] == VLCMediaPlayerStateStopped && self.media)) ||
  102. [self encounteredError] ) && ![super.media subitems] )
  103. {
  104. self.isComplete = YES;
  105. return;
  106. }
  107. if( _reattemptedConnections > 4 )
  108. return;
  109. /* Our media has in fact gained subitems, let's change our playing media */
  110. if( [[super.media subitems] count] > 0 )
  111. {
  112. [self stop];
  113. self.media = [[super.media subitems] mediaAtIndex:0];
  114. [self play];
  115. _reattemptedConnections++;
  116. }
  117. return;
  118. }
  119. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  120. }
  121. @end