VLCStreamOutput.m 11 KB

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