VLCStreamSession.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // VLCStreamSession.m
  3. // VLCKit
  4. //
  5. // Created by Pierre d'Herbemont on 1/12/08.
  6. // Copyright 2008 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "VLCStreamSession.h"
  9. #import "VLCLibVLCBridging.h"
  10. @interface VLCStreamSession ()
  11. @property (readwrite) BOOL isComplete;
  12. @end
  13. @implementation VLCStreamSession
  14. @synthesize media=originalMedia;
  15. @synthesize streamOutput;
  16. @synthesize isComplete;
  17. - (id)init
  18. {
  19. if( self = [super init] )
  20. {
  21. reattemptedConnections = 0;
  22. [self addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
  23. self.isComplete = NO;
  24. }
  25. return self;
  26. }
  27. - (void)dealloc
  28. {
  29. [self removeObserver:self forKeyPath:@"state"];
  30. [super dealloc];
  31. }
  32. + (id)streamSession
  33. {
  34. return [[[self alloc] init] autorelease];
  35. }
  36. - (void)startStreaming;
  37. {
  38. self.isComplete = NO;
  39. [self play];
  40. }
  41. - (void)play;
  42. {
  43. NSString * libvlcArgs;
  44. if( self.drawable )
  45. libvlcArgs = [NSString stringWithFormat:@"duplicate{dst=display,dst=\"%@\"}",[streamOutput representedLibVLCOptions]];
  46. else
  47. libvlcArgs = [streamOutput representedLibVLCOptions];
  48. if( libvlcArgs )
  49. {
  50. [super setMedia: [VLCMedia mediaWithMedia:originalMedia andLibVLCOptions:
  51. [NSDictionary dictionaryWithObject: libvlcArgs forKey: @"sout"]]];
  52. }
  53. else
  54. {
  55. [super setMedia: self.media];
  56. }
  57. [super play];
  58. }
  59. + (NSSet *)keyPathsForValuesAffectingDescription
  60. {
  61. return [NSSet setWithObjects:@"isCompleted", @"state", nil];
  62. }
  63. - (NSString *)description
  64. {
  65. if([self isComplete])
  66. return @"Done.";
  67. else if([self state] == VLCMediaPlayerStateError)
  68. return @"Error while Converting. Open Console.app to diagnose.";
  69. else
  70. return @"Converting...";
  71. }
  72. + (NSSet *)keyPathsForValuesAffectingEncounteredError
  73. {
  74. return [NSSet setWithObjects:@"state", nil];
  75. }
  76. - (BOOL)encounteredError;
  77. {
  78. return ([self state] == VLCMediaPlayerStateError);
  79. }
  80. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  81. {
  82. if([keyPath isEqualToString:@"state"])
  83. {
  84. if( (([self position] == 1.0 || [self state] == VLCMediaPlayerStateEnded || ([self state] == VLCMediaPlayerStateStopped && self.media)) ||
  85. [self encounteredError] ) && ![super.media subitems] )
  86. {
  87. self.isComplete = YES;
  88. return;
  89. }
  90. if( reattemptedConnections > 4 )
  91. return;
  92. /* Our media has in fact gained subitems, let's change our playing media */
  93. if( [[super.media subitems] count] > 0 )
  94. {
  95. [self stop];
  96. self.media = [[super.media subitems] mediaAtIndex:0];
  97. [self play];
  98. reattemptedConnections++;
  99. }
  100. return;
  101. }
  102. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  103. }
  104. @end