VLCStreamSession.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*****************************************************************************
  2. * VLCStreamSession.m: VLCKit.framework VLCStreamSession implementation
  3. *****************************************************************************
  4. * Copyright (C) 2008 Pierre d'Herbemont
  5. * Copyright (C) 2008 the VideoLAN team
  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
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 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 General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, 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 (readwrite) BOOL isComplete;
  28. @end
  29. @implementation VLCStreamSession
  30. @synthesize media=originalMedia;
  31. @synthesize streamOutput;
  32. @synthesize isComplete;
  33. - (id)init
  34. {
  35. if( self = [super init] )
  36. {
  37. reattemptedConnections = 0;
  38. [self addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
  39. self.isComplete = NO;
  40. }
  41. return self;
  42. }
  43. - (void)dealloc
  44. {
  45. [self removeObserver:self forKeyPath:@"state"];
  46. [super dealloc];
  47. }
  48. + (id)streamSession
  49. {
  50. return [[[self alloc] init] autorelease];
  51. }
  52. - (void)startStreaming
  53. {
  54. self.isComplete = NO;
  55. [self play];
  56. }
  57. - (void)stopStreaming
  58. {
  59. self.isComplete = YES;
  60. [super stop];
  61. }
  62. - (BOOL)play
  63. {
  64. NSString * libvlcArgs;
  65. if( self.drawable )
  66. libvlcArgs = [NSString stringWithFormat:@"#duplicate{dst=display,dst=\"%@\"}",[streamOutput representedLibVLCOptions]];
  67. else
  68. libvlcArgs = [streamOutput representedLibVLCOptions];
  69. if( libvlcArgs )
  70. {
  71. [super setMedia: [VLCMedia mediaWithMedia:originalMedia andLibVLCOptions:
  72. [NSDictionary dictionaryWithObject: libvlcArgs forKey: @"sout"]]];
  73. }
  74. else
  75. {
  76. [super setMedia: self.media];
  77. }
  78. [super play];
  79. return YES;
  80. }
  81. + (NSSet *)keyPathsForValuesAffectingDescription
  82. {
  83. return [NSSet setWithObjects:@"isCompleted", @"state", nil];
  84. }
  85. - (NSString *)description
  86. {
  87. if([self isComplete])
  88. return @"Done.";
  89. else if([self state] == VLCMediaPlayerStateError)
  90. return @"Error while Converting. Open Console.app to diagnose.";
  91. else
  92. return @"Converting...";
  93. }
  94. + (NSSet *)keyPathsForValuesAffectingEncounteredError
  95. {
  96. return [NSSet setWithObjects:@"state", nil];
  97. }
  98. - (BOOL)encounteredError;
  99. {
  100. return ([self state] == VLCMediaPlayerStateError);
  101. }
  102. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  103. {
  104. if([keyPath isEqualToString:@"state"])
  105. {
  106. if( (([self position] == 1.0 || [self state] == VLCMediaPlayerStateEnded || ([self state] == VLCMediaPlayerStateStopped && self.media)) ||
  107. [self encounteredError] ) && ![super.media subitems] )
  108. {
  109. self.isComplete = YES;
  110. return;
  111. }
  112. if( reattemptedConnections > 4 )
  113. return;
  114. /* Our media has in fact gained subitems, let's change our playing media */
  115. if( [[super.media subitems] count] > 0 )
  116. {
  117. [self stop];
  118. self.media = [[super.media subitems] mediaAtIndex:0];
  119. [self play];
  120. reattemptedConnections++;
  121. }
  122. return;
  123. }
  124. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  125. }
  126. @end