VLCStreamSession.m 3.95 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * VLCStreamSession.m: VLCKit.framework VLCStreamSession implementation
 *****************************************************************************
 * Copyright (C) 2008 Pierre d'Herbemont
5
 * Copyright (C) 2008 VLC authors and VideoLAN
6 7 8 9
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
24 25 26 27

#import "VLCStreamSession.h"
#import "VLCLibVLCBridging.h"

28
@interface VLCStreamSession ()
29 30 31 32

@property (nonatomic, readwrite) BOOL isComplete;
@property (nonatomic, readwrite) NSUInteger reattemptedConnections;

33 34
@end

35
@implementation VLCStreamSession
36

James Dumay's avatar
James Dumay committed
37
- (instancetype)init
38 39 40 41 42 43 44 45 46 47 48 49
{
    if( self = [super init] )
    {
        [self addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"state"];
}
50

James Dumay's avatar
James Dumay committed
51
+ (instancetype)streamSession
52
{
53
    return [[self alloc] init];
54 55 56
}


57
- (void)startStreaming
58
{
59
    self.isComplete = NO;
60 61 62
    [self play];
}

63 64 65 66 67 68 69
- (void)stopStreaming
{
    self.isComplete = YES;
    [super stop];
}

- (BOOL)play
70 71 72
{
    NSString * libvlcArgs;
    if( self.drawable )
73
        libvlcArgs = [NSString stringWithFormat:@"#duplicate{dst=display,dst=\"%@\"}",[_streamOutput representedLibVLCOptions]];
74
    else
75
        libvlcArgs = [_streamOutput representedLibVLCOptions];
76 77
    if( libvlcArgs )
    {
James Dumay's avatar
James Dumay committed
78
        [super setMedia:[VLCMedia mediaWithMedia:self.media
79 80 81
                                andLibVLCOptions:@{
                                    @"sout" : libvlcArgs
                                }]];
82 83 84
    }
    else
    {
85
        [super setMedia: self.media];
86 87
    }
    [super play];
88
    return YES;
89 90 91 92 93 94 95 96 97
}

+ (NSSet *)keyPathsForValuesAffectingDescription
{
    return [NSSet setWithObjects:@"isCompleted", @"state", nil];
}

- (NSString *)description
{
98
    if ([self isComplete])
99
        return @"Done.";
100 101

    if ([self state] == VLCMediaPlayerStateError)
102
        return @"Error while Converting. Open Console.app to diagnose.";
103 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 118 119 120 121 122 123 124 125 126

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"state"])
    {
        if( (([self position] == 1.0 || [self state] == VLCMediaPlayerStateEnded || ([self state] == VLCMediaPlayerStateStopped && self.media)) ||
            [self encounteredError] ) && ![super.media subitems] )
        {
            self.isComplete = YES;
            return;
        }
127
        if( _reattemptedConnections > 4 )
128 129 130 131 132 133 134 135
            return;

        /* Our media has in fact gained subitems, let's change our playing media */
        if( [[super.media subitems] count] > 0 )
        {
            [self stop];
            self.media = [[super.media subitems] mediaAtIndex:0];
            [self play];
136
            _reattemptedConnections++;
137 138 139 140 141 142
        }
        return;
    }
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

143
@end