]> git.sesse.net Git - vlc/blob - projects/macosx/framework/Sources/VLCStreamOutput.m
VLCKit: fix a leak.
[vlc] / projects / macosx / framework / Sources / VLCStreamOutput.m
1 /*****************************************************************************
2  * VLCStreamOutput.m: VLCKit.framework VLCStreamOutput 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 "VLCStreamOutput.h"
26 #import "VLCLibVLCBridging.h"
27
28 @implementation VLCStreamOutput
29 - (id)initWithOptionDictionary:(NSDictionary *)dictionary
30 {
31     if( self = [super init] )
32     {
33         options = [[NSMutableDictionary dictionaryWithDictionary:dictionary] retain];
34     }
35     return self;
36 }
37 - (NSString *)description
38 {
39     return [self representedLibVLCOptions];
40 }
41 + (id)streamOutputWithOptionDictionary:(NSDictionary *)dictionary
42 {
43     return [[[self alloc] initWithOptionDictionary:dictionary] autorelease];
44 }
45 + (id)rtpBroadcastStreamOutputWithSAPAnnounce:(NSString *)announceName
46 {
47     NSString *name = [announceName copy];
48     id output = [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
49                                             [NSDictionary dictionaryWithObjectsAndKeys:
50                                                 @"ts", @"muxer",
51                                                 @"file", @"access",
52                                                 @"sap", @"sdp",
53                                                 name, @"name",
54                                                 @"239.255.1.1", @"destination", nil
55                                             ], @"rtpOptions",
56                                             nil
57                                             ]
58                                         ];
59     [name release];
60     return output;
61 }
62
63 + (id)rtpBroadcastStreamOutput
64 {
65     return [self rtpBroadcastStreamOutputWithSAPAnnounce:@"Helloworld!"];
66 }
67
68 + (id)ipodStreamOutputWithFilePath:(NSString *)filePath
69 {
70     return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
71                                             [NSDictionary dictionaryWithObjectsAndKeys:
72                                                 @"mp4v", @"videoCodec",
73                                                 @"1024",  @"videoBitrate",
74                                                 @"mp4a", @"audioCodec",
75                                                 @"192", @"audioBitrate",
76                                                 @"2",   @"channels",
77                                                 @"320", @"width",
78                                                 @"240", @"canvasHeight",
79                                                 @"Yes", @"audio-sync",
80                                                 nil
81                                             ], @"transcodingOptions",
82                                             [NSDictionary dictionaryWithObjectsAndKeys:
83                                                 @"mp4", @"muxer",
84                                                 @"file", @"access",
85                                                 [filePath copy], @"destination", nil
86                                             ], @"outputOptions",
87                                             nil
88                                             ]
89                                         ];
90 }
91
92 + (id)mpeg4StreamOutputWithFilePath:(NSString *)filePath
93 {
94     return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
95                                             [NSDictionary dictionaryWithObjectsAndKeys:
96                                                 @"mp4v", @"videoCodec",
97                                                 @"1024",  @"videoBitrate",
98                                                 @"mp4a", @"audioCodec",
99                                                 @"192", @"audioBitrate",
100                                                 nil
101                                             ], @"transcodingOptions",
102                                             [NSDictionary dictionaryWithObjectsAndKeys:
103                                                 @"mp4", @"muxer",
104                                                 @"file", @"access",
105                                                 [filePath copy], @"destination", nil
106                                             ], @"outputOptions",
107                                             nil
108                                             ]
109                                         ];
110 }
111
112 + (id)streamOutputWithFilePath:(NSString *)filePath
113 {
114     return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
115                                             [NSDictionary dictionaryWithObjectsAndKeys:
116                                                 @"ps", @"muxer",
117                                                 @"file", @"access",
118                                                 [filePath copy], @"destination", nil
119                                             ], @"outputOptions",
120                                             nil
121                                             ]
122                                         ];
123 }
124
125 + (id)mpeg2StreamOutputWithFilePath:(NSString *)filePath;
126 {
127     return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
128                                             [NSDictionary dictionaryWithObjectsAndKeys:
129                                                 @"mp2v", @"videoCodec",
130                                                 @"1024", @"videoBitrate",
131                                                 @"mp2a",   @"audioCodec",
132                                                 @"128",   @"audioBitrate",
133                                                 @"Yes",   @"audio-sync",
134                                                 nil
135                                             ], @"transcodingOptions",
136                                             [NSDictionary dictionaryWithObjectsAndKeys:
137                                                 @"mpeg", @"muxer",
138                                                 @"file", @"access",
139                                                 [filePath copy], @"destination", nil
140                                             ], @"outputOptions",
141                                             nil
142                                             ]
143                                         ];
144 }
145 @end
146
147 @implementation VLCStreamOutput (LibVLCBridge)
148 - (NSString *)representedLibVLCOptions
149 {
150     NSString * representedOptions;
151     NSMutableArray * subOptions = [NSMutableArray array];
152     NSMutableArray * optionsAsArray = [NSMutableArray array];
153     NSDictionary * transcodingOptions = [options objectForKey:@"transcodingOptions"];
154     if( transcodingOptions )
155     {
156         NSString * videoCodec = [transcodingOptions objectForKey:@"videoCodec"];
157         NSString * audioCodec = [transcodingOptions objectForKey:@"audioCodec"];
158         NSString * videoBitrate = [transcodingOptions objectForKey:@"videoBitrate"];
159         NSString * audioBitrate = [transcodingOptions objectForKey:@"audioBitrate"];
160         NSString * channels = [transcodingOptions objectForKey:@"channels"];
161         NSString * height = [transcodingOptions objectForKey:@"height"];
162         NSString * canvasHeight = [transcodingOptions objectForKey:@"canvasHeight"];
163         NSString * width = [transcodingOptions objectForKey:@"width"];
164         NSString * audioSync = [transcodingOptions objectForKey:@"audioSync"];
165         NSString * videoEncoder = [transcodingOptions objectForKey:@"videoEncoder"];
166         if( videoEncoder )   [subOptions addObject:[NSString stringWithFormat:@"venc=%@", videoEncoder]];
167         if( videoCodec )   [subOptions addObject:[NSString stringWithFormat:@"vcodec=%@", videoCodec]];
168         if( videoBitrate ) [subOptions addObject:[NSString stringWithFormat:@"vb=%@", videoBitrate]];
169         if( width ) [subOptions addObject:[NSString stringWithFormat:@"width=%@", width]];
170         if( height ) [subOptions addObject:[NSString stringWithFormat:@"height=%@", height]];
171         if( canvasHeight ) [subOptions addObject:[NSString stringWithFormat:@"canvas-height=%@", canvasHeight]];
172         if( audioCodec )   [subOptions addObject:[NSString stringWithFormat:@"acodec=%@", audioCodec]];
173         if( audioBitrate ) [subOptions addObject:[NSString stringWithFormat:@"ab=%@", audioBitrate]];
174         if( channels ) [subOptions addObject:[NSString stringWithFormat:@"channels=%@", channels]];
175         if( audioSync ) [subOptions addObject:[NSString stringWithFormat:@"audioSync", width]];
176         [optionsAsArray addObject: [NSString stringWithFormat:@"transcode{%@}", [subOptions componentsJoinedByString:@","]]];
177         [subOptions removeAllObjects];
178     }
179     
180     NSDictionary * outputOptions = [options objectForKey:@"outputOptions"];
181     if( outputOptions )
182     {
183         NSString * muxer = [outputOptions objectForKey:@"muxer"];
184         NSString * destination = [outputOptions objectForKey:@"destination"];
185         NSString * url = [outputOptions objectForKey:@"url"];
186         NSString * access = [outputOptions objectForKey:@"access"];
187         if( muxer )       [subOptions addObject:[NSString stringWithFormat:@"mux=%@", muxer]];
188         if( destination ) [subOptions addObject:[NSString stringWithFormat:@"dst=\"%@\"", [destination stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]];
189         if( url ) [subOptions addObject:[NSString stringWithFormat:@"url=\"%@\"", [url stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]];
190         if( access )      [subOptions addObject:[NSString stringWithFormat:@"access=%@", access]];
191         [optionsAsArray addObject:[NSString stringWithFormat:@"std{%@}", [subOptions componentsJoinedByString:@","]]];
192         [subOptions removeAllObjects];
193     }
194
195     NSDictionary * rtpOptions = [options objectForKey:@"rtpOptions"];
196     if( rtpOptions )
197     {
198         NSString * muxer = [rtpOptions objectForKey:@"muxer"];
199         NSString * destination = [rtpOptions objectForKey:@"destination"];
200         NSString * sdp = [rtpOptions objectForKey:@"sdp"];
201         NSString * name = [rtpOptions objectForKey:@"name"];
202         if( muxer )       [subOptions addObject:[NSString stringWithFormat:@"muxer=%@", muxer]];
203         if( destination ) [subOptions addObject:[NSString stringWithFormat:@"dst=%@", destination]];
204         if( sdp )      [subOptions addObject:[NSString stringWithFormat:@"sdp=%@", sdp]];
205         if( name )      [subOptions addObject:[NSString stringWithFormat:@"name=\"%@\"", name]];
206         [optionsAsArray addObject:[NSString stringWithFormat:@"rtp{%@}", [subOptions componentsJoinedByString:@","]]];
207         [subOptions removeAllObjects];
208     }
209     representedOptions = [optionsAsArray componentsJoinedByString:@":"];
210     return representedOptions;
211 }
212 @end