VLCStreamOutput.m 11 KB

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