VLCStreamSession.m 3.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************
 * VLCStreamSession.m: VLCKit.framework VLCStreamSession implementation
 *****************************************************************************
 * Copyright (C) 2008 Pierre d'Herbemont
 * Copyright (C) 2008 the VideoLAN team
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *****************************************************************************/
24 25 26 27

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

28 29 30 31
@interface VLCStreamSession ()
@property (readwrite) BOOL isComplete;
@end

32 33 34
@implementation VLCStreamSession
@synthesize media=originalMedia;
@synthesize streamOutput;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
@synthesize isComplete;

- (id)init
{
    if( self = [super init] )
    {
        reattemptedConnections = 0;
        [self addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
        self.isComplete = NO;
    }
    return self;
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"state"];
    [super dealloc];
}
53 54 55 56 57 58 59 60 61

+ (id)streamSession
{
    return [[[self alloc] init] autorelease];
}


- (void)startStreaming;
{
62
    self.isComplete = NO;
63 64 65
    [self play];
}

Pierre's avatar
Pierre committed
66
- (BOOL)play;
67 68 69 70
{
    NSString * libvlcArgs;
    if( self.drawable )
        libvlcArgs = [NSString stringWithFormat:@"duplicate{dst=display,dst=\"%@\"}",[streamOutput representedLibVLCOptions]];
71 72 73 74 75 76 77
    else
        libvlcArgs = [streamOutput representedLibVLCOptions];

    if( libvlcArgs )
    {
        [super setMedia: [VLCMedia mediaWithMedia:originalMedia andLibVLCOptions:
                                [NSDictionary dictionaryWithObject: libvlcArgs forKey: @"sout"]]];
78 79 80
    }
    else
    {
81
        [super setMedia: self.media];
82 83
    }
    [super play];
Pierre's avatar
Pierre committed
84
	return YES;
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

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

- (NSString *)description
{
    if([self isComplete])
        return @"Done.";
    else if([self state] == VLCMediaPlayerStateError)
        return @"Error while Converting. Open Console.app to diagnose.";
    else
        return @"Converting...";
}

102
+ (NSSet *)keyPathsForValuesAffectingEncounteredError
103
{
104
    return [NSSet setWithObjects:@"state", nil];
105 106
}

107
- (BOOL)encounteredError;
108
{
109
    return ([self state] == VLCMediaPlayerStateError);
110
}
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

- (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;
        }
        if( reattemptedConnections > 4 )
            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];
            reattemptedConnections++;
        }
        return;
    }
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

138
@end