VLCStreamOutput.m 9.0 KB

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