MacOSX/Framework: Add VLCStreamSession, VLCStreamOutput to allow streaming/exporting.
[vlc.git] / extras / MacOSX / Framework / Sources / VLCStreamOutput.m
1 //
2 //  VLCStreamOutput.m
3 //  VLCKit
4 //
5 //  Created by Pierre d'Herbemont on 1/12/08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "VLCStreamOutput.h"
10 #import "VLCLibVLCBridging.h"
11
12
13 @implementation VLCStreamOutput
14 - (id)initWithOptionDictionary:(NSDictionary *)dictionary
15 {
16     if( self = [super init] )
17     {
18         options = [NSMutableDictionary dictionaryWithDictionary:dictionary];
19     }
20     return self;
21 }
22 - (NSString *)description
23 {
24     return [self representedLibVLCOptions];
25 }
26 + (id)streamOutputWithOptionDictionary:(NSDictionary *)dictionary
27 {
28     return [[[self alloc] initWithOptionDictionary:dictionary] autorelease];
29 }
30 + (id)rtpBroadcastStreamOutputWithSAPAnnounce:(NSString *)announceName
31 {
32     return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
33                                             [NSDictionary dictionaryWithObjectsAndKeys:
34                                                 @"ts", @"muxer",
35                                                 @"file", @"access",
36                                                 @"sap", @"sdp",
37                                                 [announceName copy], @"name",
38                                                 @"239.255.1.1", @"destination", nil
39                                             ], @"rtpOptions",
40                                             nil
41                                             ]
42                                         ];
43 }
44
45 + (id)rtpBroadcastStreamOutput
46 {
47     return [self rtpBroadcastStreamOutputWithSAPAnnounce:@"Helloworld!"];
48 }
49
50 + (id)ipodStreamOutputWithFilePath:(NSString *)filePath
51 {
52     return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
53                                             [NSDictionary dictionaryWithObjectsAndKeys:
54                                                 @"x264", @"videoCodec",
55                                                 @"768",  @"videoBitrate",
56                                                 @"mp4a", @"audioCodec",
57                                                 @"128", @"audioBitrate",
58                                                 @"2",   @"channels",
59                                                 @"320", @"width",
60                                                 @"240", @"canvasHeight",
61                                                 @"Yes", @"audio-sync",
62                                                 nil
63                                             ], @"transcodingOptions",
64                                             [NSDictionary dictionaryWithObjectsAndKeys:
65                                                 @"mp4", @"muxer",
66                                                 @"file", @"access",
67                                                 [filePath copy], @"destination", nil
68                                             ], @"outputOptions",
69                                             nil
70                                             ]
71                                         ];
72 }
73 @end
74
75 @implementation VLCStreamOutput (LibVLCBridge)
76 - (NSString *)representedLibVLCOptions
77 {
78     NSString * representedOptions;
79     NSMutableArray * subOptions = [NSMutableArray array];
80     NSMutableArray * optionsAsArray = [NSMutableArray array];
81     NSDictionary * transcodingOptions = [options objectForKey:@"transcodingOptions"];
82     if( transcodingOptions )
83     {
84         NSString * videoCodec = [transcodingOptions objectForKey:@"videoCodec"];
85         NSString * audioCodec = [transcodingOptions objectForKey:@"audioCodec"];
86         NSString * videoBitrate = [transcodingOptions objectForKey:@"videoBitrate"];
87         NSString * audioBitrate = [transcodingOptions objectForKey:@"audioBitrate"];
88         NSString * channels = [transcodingOptions objectForKey:@"channels"];
89         NSString * height = [transcodingOptions objectForKey:@"height"];
90         NSString * canvasHeight = [transcodingOptions objectForKey:@"canvasHeight"];
91         NSString * width = [transcodingOptions objectForKey:@"width"];
92         NSString * audioSync = [transcodingOptions objectForKey:@"audioSync"];
93         if( videoCodec )   [subOptions addObject:[NSString stringWithFormat:@"vcodec=%@", videoCodec]];
94         if( videoBitrate ) [subOptions addObject:[NSString stringWithFormat:@"vb=%@", videoBitrate]];
95         if( width ) [subOptions addObject:[NSString stringWithFormat:@"width=%@", width]];
96         if( height ) [subOptions addObject:[NSString stringWithFormat:@"height=%@", height]];
97         if( canvasHeight ) [subOptions addObject:[NSString stringWithFormat:@"canvas-height=%@", canvasHeight]];
98         if( audioCodec )   [subOptions addObject:[NSString stringWithFormat:@"acodec=%@", audioCodec]];
99         if( audioBitrate ) [subOptions addObject:[NSString stringWithFormat:@"ab=%@", audioBitrate]];
100         if( channels ) [subOptions addObject:[NSString stringWithFormat:@"channels=%@", channels]];
101         if( audioSync ) [subOptions addObject:[NSString stringWithFormat:@"audioSync", width]];
102         [optionsAsArray addObject: [NSString stringWithFormat:@"transcode{%@}", [subOptions componentsJoinedByString:@","]]];
103         [subOptions removeAllObjects];
104     }
105     
106     NSDictionary * outputOptions = [options objectForKey:@"outputOptions"];
107     if( outputOptions )
108     {
109         NSString * muxer = [outputOptions objectForKey:@"muxer"];
110         NSString * destination = [outputOptions objectForKey:@"destination"];
111         NSString * url = [outputOptions objectForKey:@"url"];
112         NSString * access = [outputOptions objectForKey:@"access"];
113         if( muxer )       [subOptions addObject:[NSString stringWithFormat:@"muxer=%@", muxer]];
114         if( destination ) [subOptions addObject:[NSString stringWithFormat:@"dst=\"%@\"", [destination stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]];
115         if( url ) [subOptions addObject:[NSString stringWithFormat:@"url=\"%@\"", [url stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]];
116         if( access )      [subOptions addObject:[NSString stringWithFormat:@"access=%@", access]];
117         [optionsAsArray addObject:[NSString stringWithFormat:@"std{%@}", [subOptions componentsJoinedByString:@","]]];
118         [subOptions removeAllObjects];
119     }
120
121     NSDictionary * rtpOptions = [options objectForKey:@"rtpOptions"];
122     if( rtpOptions )
123     {
124         NSString * muxer = [rtpOptions objectForKey:@"muxer"];
125         NSString * destination = [rtpOptions objectForKey:@"destination"];
126         NSString * sdp = [rtpOptions objectForKey:@"sdp"];
127         NSString * name = [rtpOptions objectForKey:@"name"];
128         if( muxer )       [subOptions addObject:[NSString stringWithFormat:@"muxer=%@", muxer]];
129         if( destination ) [subOptions addObject:[NSString stringWithFormat:@"dst=%@", destination]];
130         if( sdp )      [subOptions addObject:[NSString stringWithFormat:@"sdp=%@", sdp]];
131         if( name )      [subOptions addObject:[NSString stringWithFormat:@"name=\"%@\"", name]];
132         [optionsAsArray addObject:[NSString stringWithFormat:@"rtp{%@}", [subOptions componentsJoinedByString:@","]]];
133         [subOptions removeAllObjects];
134     }
135     representedOptions = [optionsAsArray componentsJoinedByString:@":"];
136     return representedOptions;
137 }
138 @end