VLCStreamOutput.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*****************************************************************************
  2. * VLCStreamOutput.m: VLCKit.framework VLCStreamOutput implementation
  3. *****************************************************************************
  4. * Copyright (C) 2008 Pierre d'Herbemont
  5. * Copyright (C) 2008 VLC authors and VideoLAN
  6. * Copyright (C) 2012 Brendon Justin
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Brendon Justin <brendonjustin # gmail.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation; either version 2.1 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with this program; if not, write to the Free Software Foundation,
  24. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25. *****************************************************************************/
  26. #import "VLCStreamOutput.h"
  27. #import "VLCLibVLCBridging.h"
  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. + (id)rtpBroadcastStreamOutput
  64. {
  65. return [self rtpBroadcastStreamOutputWithSAPAnnounce:@"Helloworld!"];
  66. }
  67. + (id)ipodStreamOutputWithFilePath:(NSString *)filePath
  68. {
  69. return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
  70. [NSDictionary dictionaryWithObjectsAndKeys:
  71. @"h264", @"videoCodec",
  72. @"1024", @"videoBitrate", // max by Apple: 1.5 mbps
  73. @"mp4a", @"audioCodec",
  74. @"128", @"audioBitrate", // max by Apple: 160 kbps
  75. @"2", @"channels",
  76. @"640", @"width", // max by Apple: do.
  77. @"480", @"canvasHeight", // max by Apple: do.
  78. @"Yes", @"audio-sync",
  79. nil
  80. ], @"transcodingOptions",
  81. [NSDictionary dictionaryWithObjectsAndKeys:
  82. @"mp4", @"muxer",
  83. @"file", @"access",
  84. [[filePath copy] autorelease], @"destination",
  85. nil
  86. ], @"outputOptions",
  87. nil
  88. ]
  89. ];
  90. }
  91. + (id)mpeg4StreamOutputWithFilePath:(NSString *)filePath
  92. {
  93. return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
  94. [NSDictionary dictionaryWithObjectsAndKeys:
  95. @"mp4v", @"videoCodec",
  96. @"1024", @"videoBitrate",
  97. @"mp4a", @"audioCodec",
  98. @"192", @"audioBitrate",
  99. nil
  100. ], @"transcodingOptions",
  101. [NSDictionary dictionaryWithObjectsAndKeys:
  102. @"mp4", @"muxer",
  103. @"file", @"access",
  104. [[filePath copy] autorelease], @"destination", nil
  105. ], @"outputOptions",
  106. nil
  107. ]
  108. ];
  109. }
  110. + (id)streamOutputWithFilePath:(NSString *)filePath
  111. {
  112. return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
  113. [NSDictionary dictionaryWithObjectsAndKeys:
  114. @"ps", @"muxer",
  115. @"file", @"access",
  116. [[filePath copy] autorelease], @"destination", nil
  117. ], @"outputOptions",
  118. nil
  119. ]
  120. ];
  121. }
  122. + (id)mpeg2StreamOutputWithFilePath:(NSString *)filePath;
  123. {
  124. return [self streamOutputWithOptionDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
  125. [NSDictionary dictionaryWithObjectsAndKeys:
  126. @"mp2v", @"videoCodec",
  127. @"1024", @"videoBitrate",
  128. @"mpga", @"audioCodec",
  129. @"128", @"audioBitrate",
  130. @"Yes", @"audio-sync",
  131. nil
  132. ], @"transcodingOptions",
  133. [NSDictionary dictionaryWithObjectsAndKeys:
  134. @"ps", @"muxer",
  135. @"file", @"access",
  136. [[filePath copy] autorelease], @"destination", nil
  137. ], @"outputOptions",
  138. nil
  139. ]
  140. ];
  141. }
  142. @end
  143. @implementation VLCStreamOutput (LibVLCBridge)
  144. - (NSString *)representedLibVLCOptions
  145. {
  146. NSString * representedOptions;
  147. NSMutableArray * subOptions = [NSMutableArray array];
  148. NSMutableArray * optionsAsArray = [NSMutableArray array];
  149. NSDictionary * transcodingOptions = [options objectForKey:@"transcodingOptions"];
  150. if( transcodingOptions )
  151. {
  152. NSString * videoCodec = [transcodingOptions objectForKey:@"videoCodec"];
  153. NSString * audioCodec = [transcodingOptions objectForKey:@"audioCodec"];
  154. NSString * subtitleCodec = [transcodingOptions objectForKey:@"subtitleCodec"];
  155. NSString * videoBitrate = [transcodingOptions objectForKey:@"videoBitrate"];
  156. NSString * audioBitrate = [transcodingOptions objectForKey:@"audioBitrate"];
  157. NSString * channels = [transcodingOptions objectForKey:@"channels"];
  158. NSString * height = [transcodingOptions objectForKey:@"height"];
  159. NSString * canvasHeight = [transcodingOptions objectForKey:@"canvasHeight"];
  160. NSString * width = [transcodingOptions objectForKey:@"width"];
  161. NSString * audioSync = [transcodingOptions objectForKey:@"audioSync"];
  162. NSString * videoEncoder = [transcodingOptions objectForKey:@"videoEncoder"];
  163. NSString * subtitleEncoder = [transcodingOptions objectForKey:@"subtitleEncoder"];
  164. NSString * subtitleOverlay = [transcodingOptions objectForKey:@"subtitleOverlay"];
  165. if( videoEncoder ) [subOptions addObject:[NSString stringWithFormat:@"venc=%@", videoEncoder]];
  166. if( videoCodec ) [subOptions addObject:[NSString stringWithFormat:@"vcodec=%@", videoCodec]];
  167. if( videoBitrate ) [subOptions addObject:[NSString stringWithFormat:@"vb=%@", videoBitrate]];
  168. if( width ) [subOptions addObject:[NSString stringWithFormat:@"width=%@", width]];
  169. if( height ) [subOptions addObject:[NSString stringWithFormat:@"height=%@", height]];
  170. if( canvasHeight ) [subOptions addObject:[NSString stringWithFormat:@"canvas-height=%@", canvasHeight]];
  171. if( audioCodec ) [subOptions addObject:[NSString stringWithFormat:@"acodec=%@", audioCodec]];
  172. if( audioBitrate ) [subOptions addObject:[NSString stringWithFormat:@"ab=%@", audioBitrate]];
  173. if( channels ) [subOptions addObject:[NSString stringWithFormat:@"channels=%@", channels]];
  174. if( audioSync ) [subOptions addObject:@"audioSync"];
  175. if( subtitleCodec ) [subOptions addObject:[NSString stringWithFormat:@"scodec=%@", subtitleCodec]];
  176. if( subtitleEncoder ) [subOptions addObject:[NSString stringWithFormat:@"senc=%@", subtitleEncoder]];
  177. if( subtitleOverlay ) [subOptions addObject:@"soverlay"];
  178. [optionsAsArray addObject: [NSString stringWithFormat:@"#transcode{%@}", [subOptions componentsJoinedByString:@","]]];
  179. [subOptions removeAllObjects];
  180. }
  181. NSDictionary * outputOptions = [options objectForKey:@"outputOptions"];
  182. if( outputOptions )
  183. {
  184. NSString * muxer = [outputOptions objectForKey:@"muxer"];
  185. NSString * destination = [outputOptions objectForKey:@"destination"];
  186. NSString * url = [outputOptions objectForKey:@"url"];
  187. NSString * access = [outputOptions objectForKey:@"access"];
  188. if( muxer ) [subOptions addObject:[NSString stringWithFormat:@"mux=%@", muxer]];
  189. if( destination ) [subOptions addObject:[NSString stringWithFormat:@"dst=\"%@\"", [destination stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]];
  190. if( url ) [subOptions addObject:[NSString stringWithFormat:@"url=\"%@\"", [url stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]];
  191. if( access ) [subOptions addObject:[NSString stringWithFormat:@"access=%@", access]];
  192. NSString *std = [NSString stringWithFormat:@"std{%@}", [subOptions componentsJoinedByString:@","]];
  193. if ( !transcodingOptions )
  194. std = [NSString stringWithFormat:@"#%@", std];
  195. [optionsAsArray addObject:std];
  196. [subOptions removeAllObjects];
  197. }
  198. NSDictionary * rtpOptions = [options objectForKey:@"rtpOptions"];
  199. if( rtpOptions )
  200. {
  201. NSString * muxer = [rtpOptions objectForKey:@"muxer"];
  202. NSString * destination = [rtpOptions objectForKey:@"destination"];
  203. NSString * sdp = [rtpOptions objectForKey:@"sdp"];
  204. NSString * name = [rtpOptions objectForKey:@"name"];
  205. NSString * sap = [rtpOptions objectForKey:@"sap"];
  206. if( muxer ) [subOptions addObject:[NSString stringWithFormat:@"muxer=%@", muxer]];
  207. if( destination ) [subOptions addObject:[NSString stringWithFormat:@"dst=%@", destination]];
  208. if( sdp ) [subOptions addObject:[NSString stringWithFormat:@"sdp=%@", sdp]];
  209. if( sap ) [subOptions addObject:@"sap"];
  210. if( name ) [subOptions addObject:[NSString stringWithFormat:@"name=\"%@\"", name]];
  211. NSString *rtp = [NSString stringWithFormat:@"#rtp{%@}", [subOptions componentsJoinedByString:@","]];
  212. if ( !transcodingOptions )
  213. rtp = [NSString stringWithFormat:@"#%@", rtp];
  214. [optionsAsArray addObject:rtp];
  215. [subOptions removeAllObjects];
  216. }
  217. representedOptions = [optionsAsArray componentsJoinedByString:@":"];
  218. return representedOptions;
  219. }
  220. @end