VLCStreamOutput.m 12 KB

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