]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCStreamSession.m
VLCKit.framework: Documentation updates and whitespace cleanup.
[vlc] / projects / macosx / framework / Sources / VLCStreamSession.m
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
25 #import "VLCStreamSession.h"
26 #import "VLCLibVLCBridging.h"
27
28 @interface VLCStreamSession ()
29 @property (readwrite) BOOL isComplete;
30 @end
31
32 @implementation VLCStreamSession
33 @synthesize media=originalMedia;
34 @synthesize streamOutput;
35 @synthesize isComplete;
36
37 - (id)init
38 {
39     if( self = [super init] )
40     {
41         reattemptedConnections = 0;
42         [self addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
43         self.isComplete = NO;
44     }
45     return self;
46 }
47
48 - (void)dealloc
49 {
50     [self removeObserver:self forKeyPath:@"state"];
51     [super dealloc];
52 }
53
54 + (id)streamSession
55 {
56     return [[[self alloc] init] autorelease];
57 }
58
59
60 - (void)startStreaming;
61 {
62     self.isComplete = NO;
63     [self play];
64 }
65
66 - (void)play;
67 {
68     NSString * libvlcArgs;
69     if( self.drawable )
70         libvlcArgs = [NSString stringWithFormat:@"duplicate{dst=display,dst=\"%@\"}",[streamOutput representedLibVLCOptions]];
71     else
72         libvlcArgs = [streamOutput representedLibVLCOptions];
73
74     if( libvlcArgs )
75     {
76         [super setMedia: [VLCMedia mediaWithMedia:originalMedia andLibVLCOptions:
77                                 [NSDictionary dictionaryWithObject: libvlcArgs forKey: @"sout"]]];
78     }
79     else
80     {
81         [super setMedia: self.media];
82     }
83     [super play];
84 }
85
86 + (NSSet *)keyPathsForValuesAffectingDescription
87 {
88     return [NSSet setWithObjects:@"isCompleted", @"state", nil];
89 }
90
91 - (NSString *)description
92 {
93     if([self isComplete])
94         return @"Done.";
95     else if([self state] == VLCMediaPlayerStateError)
96         return @"Error while Converting. Open Console.app to diagnose.";
97     else
98         return @"Converting...";
99 }
100
101 + (NSSet *)keyPathsForValuesAffectingEncounteredError
102 {
103     return [NSSet setWithObjects:@"state", nil];
104 }
105
106 - (BOOL)encounteredError;
107 {
108     return ([self state] == VLCMediaPlayerStateError);
109 }
110
111 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
112 {
113     if([keyPath isEqualToString:@"state"])
114     {
115         if( (([self position] == 1.0 || [self state] == VLCMediaPlayerStateEnded || ([self state] == VLCMediaPlayerStateStopped && self.media)) ||
116             [self encounteredError] ) && ![super.media subitems] )
117         {
118             self.isComplete = YES;
119             return;
120         }
121         if( reattemptedConnections > 4 )
122             return;
123
124         /* Our media has in fact gained subitems, let's change our playing media */
125         if( [[super.media subitems] count] > 0 )
126         {
127             [self stop];
128             self.media = [[super.media subitems] mediaAtIndex:0];
129             [self play];
130             reattemptedConnections++;
131         }
132         return;
133     }
134     [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
135 }
136
137 @end