]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCStreamSession.m
macosx/framework: fixed MPEG2 exporting, updated iPod video profile (requires contrib...
[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)stopStreaming
67 {
68     self.isComplete = YES;
69     [super stop];
70 }
71
72 - (BOOL)play
73 {
74     NSString * libvlcArgs;
75     if( self.drawable )
76         libvlcArgs = [NSString stringWithFormat:@"duplicate{dst=display,dst=\"%@\"}",[streamOutput representedLibVLCOptions]];
77     else
78         libvlcArgs = [streamOutput representedLibVLCOptions];
79     if( libvlcArgs )
80     {
81         [super setMedia: [VLCMedia mediaWithMedia:originalMedia andLibVLCOptions:
82                                 [NSDictionary dictionaryWithObject: libvlcArgs forKey: @"sout"]]];
83     }
84     else
85     {
86         [super setMedia: self.media];
87     }
88     [super play];
89         return YES;
90 }
91
92 + (NSSet *)keyPathsForValuesAffectingDescription
93 {
94     return [NSSet setWithObjects:@"isCompleted", @"state", nil];
95 }
96
97 - (NSString *)description
98 {
99     if([self isComplete])
100         return @"Done.";
101     else if([self state] == VLCMediaPlayerStateError)
102         return @"Error while Converting. Open Console.app to diagnose.";
103     else
104         return @"Converting...";
105 }
106
107 + (NSSet *)keyPathsForValuesAffectingEncounteredError
108 {
109     return [NSSet setWithObjects:@"state", nil];
110 }
111
112 - (BOOL)encounteredError;
113 {
114     return ([self state] == VLCMediaPlayerStateError);
115 }
116
117 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
118 {
119     if([keyPath isEqualToString:@"state"])
120     {
121         if( (([self position] == 1.0 || [self state] == VLCMediaPlayerStateEnded || ([self state] == VLCMediaPlayerStateStopped && self.media)) ||
122             [self encounteredError] ) && ![super.media subitems] )
123         {
124             self.isComplete = YES;
125             return;
126         }
127         if( reattemptedConnections > 4 )
128             return;
129
130         /* Our media has in fact gained subitems, let's change our playing media */
131         if( [[super.media subitems] count] > 0 )
132         {
133             [self stop];
134             self.media = [[super.media subitems] mediaAtIndex:0];
135             [self play];
136             reattemptedConnections++;
137         }
138         return;
139     }
140     [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
141 }
142
143 @end