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