VLCStreamOutput.m 12 KB

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