VLCStreamOutput.m 8.9 KB

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