VLCStreamSession.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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)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:originalMedia andLibVLCOptions:
  67. [NSDictionary dictionaryWithObject: libvlcArgs forKey: @"sout"]]];
  68. }
  69. else
  70. {
  71. [super setMedia: self.media];
  72. }
  73. [super play];
  74. }
  75. + (NSSet *)keyPathsForValuesAffectingDescription
  76. {
  77. return [NSSet setWithObjects:@"isCompleted", @"state", nil];
  78. }
  79. - (NSString *)description
  80. {
  81. if([self isComplete])
  82. return @"Done.";
  83. else if([self state] == VLCMediaPlayerStateError)
  84. return @"Error while Converting. Open Console.app to diagnose.";
  85. else
  86. return @"Converting...";
  87. }
  88. + (NSSet *)keyPathsForValuesAffectingEncounteredError
  89. {
  90. return [NSSet setWithObjects:@"state", nil];
  91. }
  92. - (BOOL)encounteredError;
  93. {
  94. return ([self state] == VLCMediaPlayerStateError);
  95. }
  96. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  97. {
  98. if([keyPath isEqualToString:@"state"])
  99. {
  100. if( (([self position] == 1.0 || [self state] == VLCMediaPlayerStateEnded || ([self state] == VLCMediaPlayerStateStopped && self.media)) ||
  101. [self encounteredError] ) && ![super.media subitems] )
  102. {
  103. self.isComplete = YES;
  104. return;
  105. }
  106. if( reattemptedConnections > 4 )
  107. return;
  108. /* Our media has in fact gained subitems, let's change our playing media */
  109. if( [[super.media subitems] count] > 0 )
  110. {
  111. [self stop];
  112. self.media = [[super.media subitems] mediaAtIndex:0];
  113. [self play];
  114. reattemptedConnections++;
  115. }
  116. return;
  117. }
  118. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  119. }
  120. @end