BaseStreamingService.cs 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Devices;
  4. using MediaBrowser.Controller.Dlna;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.MediaEncoding;
  7. using MediaBrowser.Model.Dlna;
  8. using MediaBrowser.Model.Dto;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Extensions;
  11. using MediaBrowser.Model.IO;
  12. using MediaBrowser.Model.MediaInfo;
  13. using MediaBrowser.Model.Serialization;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Globalization;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using MediaBrowser.Common.Net;
  23. using MediaBrowser.Controller;
  24. using MediaBrowser.Controller.Net;
  25. using MediaBrowser.Model.Diagnostics;
  26. namespace MediaBrowser.Api.Playback
  27. {
  28. /// <summary>
  29. /// Class BaseStreamingService
  30. /// </summary>
  31. public abstract class BaseStreamingService : BaseApiService
  32. {
  33. /// <summary>
  34. /// Gets or sets the application paths.
  35. /// </summary>
  36. /// <value>The application paths.</value>
  37. protected IServerConfigurationManager ServerConfigurationManager { get; private set; }
  38. /// <summary>
  39. /// Gets or sets the user manager.
  40. /// </summary>
  41. /// <value>The user manager.</value>
  42. protected IUserManager UserManager { get; private set; }
  43. /// <summary>
  44. /// Gets or sets the library manager.
  45. /// </summary>
  46. /// <value>The library manager.</value>
  47. protected ILibraryManager LibraryManager { get; private set; }
  48. /// <summary>
  49. /// Gets or sets the iso manager.
  50. /// </summary>
  51. /// <value>The iso manager.</value>
  52. protected IIsoManager IsoManager { get; private set; }
  53. /// <summary>
  54. /// Gets or sets the media encoder.
  55. /// </summary>
  56. /// <value>The media encoder.</value>
  57. protected IMediaEncoder MediaEncoder { get; private set; }
  58. protected IFileSystem FileSystem { get; private set; }
  59. protected IDlnaManager DlnaManager { get; private set; }
  60. protected IDeviceManager DeviceManager { get; private set; }
  61. protected ISubtitleEncoder SubtitleEncoder { get; private set; }
  62. protected IMediaSourceManager MediaSourceManager { get; private set; }
  63. protected IZipClient ZipClient { get; private set; }
  64. protected IJsonSerializer JsonSerializer { get; private set; }
  65. public static IServerApplicationHost AppHost;
  66. public static IHttpClient HttpClient;
  67. protected IAuthorizationContext AuthorizationContext { get; private set; }
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
  70. /// </summary>
  71. protected BaseStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext)
  72. {
  73. JsonSerializer = jsonSerializer;
  74. AuthorizationContext = authorizationContext;
  75. ZipClient = zipClient;
  76. MediaSourceManager = mediaSourceManager;
  77. DeviceManager = deviceManager;
  78. SubtitleEncoder = subtitleEncoder;
  79. DlnaManager = dlnaManager;
  80. FileSystem = fileSystem;
  81. ServerConfigurationManager = serverConfig;
  82. UserManager = userManager;
  83. LibraryManager = libraryManager;
  84. IsoManager = isoManager;
  85. MediaEncoder = mediaEncoder;
  86. }
  87. /// <summary>
  88. /// Gets the command line arguments.
  89. /// </summary>
  90. /// <param name="outputPath">The output path.</param>
  91. /// <param name="state">The state.</param>
  92. /// <param name="isEncoding">if set to <c>true</c> [is encoding].</param>
  93. /// <returns>System.String.</returns>
  94. protected abstract string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding);
  95. /// <summary>
  96. /// Gets the type of the transcoding job.
  97. /// </summary>
  98. /// <value>The type of the transcoding job.</value>
  99. protected abstract TranscodingJobType TranscodingJobType { get; }
  100. /// <summary>
  101. /// Gets the output file extension.
  102. /// </summary>
  103. /// <param name="state">The state.</param>
  104. /// <returns>System.String.</returns>
  105. protected virtual string GetOutputFileExtension(StreamState state)
  106. {
  107. return Path.GetExtension(state.RequestedUrl);
  108. }
  109. /// <summary>
  110. /// Gets the output file path.
  111. /// </summary>
  112. /// <param name="state">The state.</param>
  113. /// <returns>System.String.</returns>
  114. private string GetOutputFilePath(StreamState state)
  115. {
  116. var folder = ServerConfigurationManager.ApplicationPaths.TranscodingTempPath;
  117. var outputFileExtension = GetOutputFileExtension(state);
  118. var data = GetCommandLineArguments("dummy\\dummy", state, false);
  119. data += "-" + (state.Request.DeviceId ?? string.Empty);
  120. data += "-" + (state.Request.PlaySessionId ?? string.Empty);
  121. var dataHash = data.GetMD5().ToString("N");
  122. if (EnableOutputInSubFolder)
  123. {
  124. return Path.Combine(folder, dataHash, dataHash + (outputFileExtension ?? string.Empty).ToLower());
  125. }
  126. return Path.Combine(folder, dataHash + (outputFileExtension ?? string.Empty).ToLower());
  127. }
  128. protected virtual bool EnableOutputInSubFolder
  129. {
  130. get { return false; }
  131. }
  132. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  133. /// <summary>
  134. /// Gets the fast seek command line parameter.
  135. /// </summary>
  136. /// <param name="request">The request.</param>
  137. /// <returns>System.String.</returns>
  138. /// <value>The fast seek command line parameter.</value>
  139. protected string GetFastSeekCommandLineParameter(StreamRequest request)
  140. {
  141. var time = request.StartTimeTicks ?? 0;
  142. if (time > 0)
  143. {
  144. return string.Format("-ss {0}", MediaEncoder.GetTimeParameter(time));
  145. }
  146. return string.Empty;
  147. }
  148. /// <summary>
  149. /// Gets the map args.
  150. /// </summary>
  151. /// <param name="state">The state.</param>
  152. /// <returns>System.String.</returns>
  153. protected virtual string GetMapArgs(StreamState state)
  154. {
  155. // If we don't have known media info
  156. // If input is video, use -sn to drop subtitles
  157. // Otherwise just return empty
  158. if (state.VideoStream == null && state.AudioStream == null)
  159. {
  160. return state.IsInputVideo ? "-sn" : string.Empty;
  161. }
  162. // We have media info, but we don't know the stream indexes
  163. if (state.VideoStream != null && state.VideoStream.Index == -1)
  164. {
  165. return "-sn";
  166. }
  167. // We have media info, but we don't know the stream indexes
  168. if (state.AudioStream != null && state.AudioStream.Index == -1)
  169. {
  170. return state.IsInputVideo ? "-sn" : string.Empty;
  171. }
  172. var args = string.Empty;
  173. if (state.VideoStream != null)
  174. {
  175. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  176. }
  177. else
  178. {
  179. // No known video stream
  180. args += "-vn";
  181. }
  182. if (state.AudioStream != null)
  183. {
  184. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  185. }
  186. else
  187. {
  188. args += " -map -0:a";
  189. }
  190. if (state.SubtitleStream == null || state.VideoRequest.SubtitleMethod == SubtitleDeliveryMethod.Hls)
  191. {
  192. args += " -map -0:s";
  193. }
  194. else if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
  195. {
  196. args += " -map 1:0 -sn";
  197. }
  198. return args;
  199. }
  200. /// <summary>
  201. /// Determines which stream will be used for playback
  202. /// </summary>
  203. /// <param name="allStream">All stream.</param>
  204. /// <param name="desiredIndex">Index of the desired.</param>
  205. /// <param name="type">The type.</param>
  206. /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param>
  207. /// <returns>MediaStream.</returns>
  208. private MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true)
  209. {
  210. var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList();
  211. if (desiredIndex.HasValue)
  212. {
  213. var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value);
  214. if (stream != null)
  215. {
  216. return stream;
  217. }
  218. }
  219. if (type == MediaStreamType.Video)
  220. {
  221. streams = streams.Where(i => !string.Equals(i.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)).ToList();
  222. }
  223. if (returnFirstIfNoIndex && type == MediaStreamType.Audio)
  224. {
  225. return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ??
  226. streams.FirstOrDefault();
  227. }
  228. // Just return the first one
  229. return returnFirstIfNoIndex ? streams.FirstOrDefault() : null;
  230. }
  231. /// <summary>
  232. /// Gets the number of threads.
  233. /// </summary>
  234. /// <returns>System.Int32.</returns>
  235. protected int GetNumberOfThreads(StreamState state, bool isWebm)
  236. {
  237. var threads = ApiEntryPoint.Instance.GetEncodingOptions().EncodingThreadCount;
  238. if (isWebm)
  239. {
  240. // Recommended per docs
  241. return Math.Max(Environment.ProcessorCount - 1, 2);
  242. }
  243. // Automatic
  244. if (threads == -1)
  245. {
  246. return 0;
  247. }
  248. return threads;
  249. }
  250. protected string GetH264Encoder(StreamState state)
  251. {
  252. var defaultEncoder = "libx264";
  253. // Only use alternative encoders for video files.
  254. // When using concat with folder rips, if the mfx session fails to initialize, ffmpeg will be stuck retrying and will not exit gracefully
  255. // Since transcoding of folder rips is expiremental anyway, it's not worth adding additional variables such as this.
  256. if (state.VideoType == VideoType.VideoFile)
  257. {
  258. var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions();
  259. var hwType = encodingOptions.HardwareAccelerationType;
  260. if (string.Equals(hwType, "qsv", StringComparison.OrdinalIgnoreCase) ||
  261. string.Equals(hwType, "h264_qsv", StringComparison.OrdinalIgnoreCase))
  262. {
  263. return GetAvailableEncoder("h264_qsv", defaultEncoder);
  264. }
  265. if (string.Equals(hwType, "nvenc", StringComparison.OrdinalIgnoreCase))
  266. {
  267. return GetAvailableEncoder("h264_nvenc", defaultEncoder);
  268. }
  269. if (string.Equals(hwType, "h264_omx", StringComparison.OrdinalIgnoreCase))
  270. {
  271. return GetAvailableEncoder("h264_omx", defaultEncoder);
  272. }
  273. if (string.Equals(hwType, "vaapi", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(encodingOptions.VaapiDevice))
  274. {
  275. if (IsVaapiSupported(state))
  276. {
  277. return GetAvailableEncoder("h264_vaapi", defaultEncoder);
  278. }
  279. }
  280. }
  281. return defaultEncoder;
  282. }
  283. private bool IsVaapiSupported(StreamState state)
  284. {
  285. var videoStream = state.VideoStream;
  286. if (videoStream != null)
  287. {
  288. // vaapi will throw an error with this input
  289. // [vaapi @ 0x7faed8000960] No VAAPI support for codec mpeg4 profile -99.
  290. if (string.Equals(videoStream.Codec, "mpeg4", StringComparison.OrdinalIgnoreCase))
  291. {
  292. if (videoStream.Level == -99 || videoStream.Level == 15)
  293. {
  294. return false;
  295. }
  296. }
  297. }
  298. return true;
  299. }
  300. private string GetAvailableEncoder(string preferredEncoder, string defaultEncoder)
  301. {
  302. if (MediaEncoder.SupportsEncoder(preferredEncoder))
  303. {
  304. return preferredEncoder;
  305. }
  306. return defaultEncoder;
  307. }
  308. protected virtual string GetDefaultH264Preset()
  309. {
  310. return "superfast";
  311. }
  312. /// <summary>
  313. /// Gets the video bitrate to specify on the command line
  314. /// </summary>
  315. /// <param name="state">The state.</param>
  316. /// <param name="videoEncoder">The video codec.</param>
  317. /// <returns>System.String.</returns>
  318. protected string GetVideoQualityParam(StreamState state, string videoEncoder)
  319. {
  320. var param = string.Empty;
  321. var isVc1 = state.VideoStream != null &&
  322. string.Equals(state.VideoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase);
  323. var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions();
  324. if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase))
  325. {
  326. if (!string.IsNullOrWhiteSpace(encodingOptions.H264Preset))
  327. {
  328. param += "-preset " + encodingOptions.H264Preset;
  329. }
  330. else
  331. {
  332. param += "-preset " + GetDefaultH264Preset();
  333. }
  334. if (encodingOptions.H264Crf >= 0 && encodingOptions.H264Crf <= 51)
  335. {
  336. param += " -crf " + encodingOptions.H264Crf.ToString(CultureInfo.InvariantCulture);
  337. }
  338. else
  339. {
  340. param += " -crf 23";
  341. }
  342. }
  343. else if (string.Equals(videoEncoder, "libx265", StringComparison.OrdinalIgnoreCase))
  344. {
  345. param += "-preset fast";
  346. param += " -crf 28";
  347. }
  348. // h264 (h264_qsv)
  349. else if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase))
  350. {
  351. param += "-preset 7 -look_ahead 0";
  352. }
  353. // h264 (h264_nvenc)
  354. else if (string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase))
  355. {
  356. param += "-preset default";
  357. }
  358. // webm
  359. else if (string.Equals(videoEncoder, "libvpx", StringComparison.OrdinalIgnoreCase))
  360. {
  361. // Values 0-3, 0 being highest quality but slower
  362. var profileScore = 0;
  363. string crf;
  364. var qmin = "0";
  365. var qmax = "50";
  366. crf = "10";
  367. if (isVc1)
  368. {
  369. profileScore++;
  370. }
  371. // Max of 2
  372. profileScore = Math.Min(profileScore, 2);
  373. // http://www.webmproject.org/docs/encoder-parameters/
  374. param += string.Format("-speed 16 -quality good -profile:v {0} -slices 8 -crf {1} -qmin {2} -qmax {3}",
  375. profileScore.ToString(UsCulture),
  376. crf,
  377. qmin,
  378. qmax);
  379. }
  380. else if (string.Equals(videoEncoder, "mpeg4", StringComparison.OrdinalIgnoreCase))
  381. {
  382. param += "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  383. }
  384. // asf/wmv
  385. else if (string.Equals(videoEncoder, "wmv2", StringComparison.OrdinalIgnoreCase))
  386. {
  387. param += "-qmin 2";
  388. }
  389. else if (string.Equals(videoEncoder, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  390. {
  391. param += "-mbd 2";
  392. }
  393. param += GetVideoBitrateParam(state, videoEncoder);
  394. var framerate = GetFramerateParam(state);
  395. if (framerate.HasValue)
  396. {
  397. param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture));
  398. }
  399. if (!string.IsNullOrEmpty(state.OutputVideoSync))
  400. {
  401. param += " -vsync " + state.OutputVideoSync;
  402. }
  403. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  404. {
  405. if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase) &&
  406. !string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  407. {
  408. // not supported by h264_omx
  409. param += " -profile:v " + state.VideoRequest.Profile;
  410. }
  411. }
  412. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  413. {
  414. var level = NormalizeTranscodingLevel(state.OutputVideoCodec, state.VideoRequest.Level);
  415. // h264_qsv and h264_nvenc expect levels to be expressed as a decimal. libx264 supports decimal and non-decimal format
  416. // also needed for libx264 due to https://trac.ffmpeg.org/ticket/3307
  417. if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) ||
  418. string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) ||
  419. string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase))
  420. {
  421. switch (level)
  422. {
  423. case "30":
  424. param += " -level 3.0";
  425. break;
  426. case "31":
  427. param += " -level 3.1";
  428. break;
  429. case "32":
  430. param += " -level 3.2";
  431. break;
  432. case "40":
  433. param += " -level 4.0";
  434. break;
  435. case "41":
  436. param += " -level 4.1";
  437. break;
  438. case "42":
  439. param += " -level 4.2";
  440. break;
  441. case "50":
  442. param += " -level 5.0";
  443. break;
  444. case "51":
  445. param += " -level 5.1";
  446. break;
  447. case "52":
  448. param += " -level 5.2";
  449. break;
  450. default:
  451. param += " -level " + level;
  452. break;
  453. }
  454. }
  455. else if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase))
  456. {
  457. param += " -level " + level;
  458. }
  459. }
  460. if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase))
  461. {
  462. param += " -x264opts:0 subme=0:rc_lookahead=10:me_range=4:me=dia:no_chroma_me:8x8dct=0:partitions=none";
  463. }
  464. if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase) &&
  465. !string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) &&
  466. !string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  467. {
  468. param = "-pix_fmt yuv420p " + param;
  469. }
  470. return param;
  471. }
  472. private string NormalizeTranscodingLevel(string videoCodec, string level)
  473. {
  474. double requestLevel;
  475. // Clients may direct play higher than level 41, but there's no reason to transcode higher
  476. if (double.TryParse(level, NumberStyles.Any, UsCulture, out requestLevel))
  477. {
  478. if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
  479. {
  480. if (requestLevel > 41)
  481. {
  482. return "41";
  483. }
  484. }
  485. }
  486. return level;
  487. }
  488. protected string GetAudioFilterParam(StreamState state, bool isHls)
  489. {
  490. var volParam = string.Empty;
  491. var audioSampleRate = string.Empty;
  492. var channels = state.OutputAudioChannels;
  493. // Boost volume to 200% when downsampling from 6ch to 2ch
  494. if (channels.HasValue && channels.Value <= 2)
  495. {
  496. if (state.AudioStream != null && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5 && !ApiEntryPoint.Instance.GetEncodingOptions().DownMixAudioBoost.Equals(1))
  497. {
  498. volParam = ",volume=" + ApiEntryPoint.Instance.GetEncodingOptions().DownMixAudioBoost.ToString(UsCulture);
  499. }
  500. }
  501. if (state.OutputAudioSampleRate.HasValue)
  502. {
  503. audioSampleRate = state.OutputAudioSampleRate.Value + ":";
  504. }
  505. var adelay = isHls ? "adelay=1," : string.Empty;
  506. var pts = string.Empty;
  507. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.VideoRequest.SubtitleMethod == SubtitleDeliveryMethod.Encode && !state.VideoRequest.CopyTimestamps)
  508. {
  509. var seconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds;
  510. pts = string.Format(",asetpts=PTS-{0}/TB", Math.Round(seconds).ToString(UsCulture));
  511. }
  512. return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
  513. adelay,
  514. audioSampleRate,
  515. volParam,
  516. pts,
  517. state.OutputAudioSync);
  518. }
  519. /// <summary>
  520. /// If we're going to put a fixed size on the command line, this will calculate it
  521. /// </summary>
  522. /// <param name="state">The state.</param>
  523. /// <param name="outputVideoCodec">The output video codec.</param>
  524. /// <param name="allowTimeStampCopy">if set to <c>true</c> [allow time stamp copy].</param>
  525. /// <returns>System.String.</returns>
  526. protected string GetOutputSizeParam(StreamState state,
  527. string outputVideoCodec,
  528. bool allowTimeStampCopy = true)
  529. {
  530. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  531. var request = state.VideoRequest;
  532. var filters = new List<string>();
  533. if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  534. {
  535. filters.Add("format=nv12|vaapi");
  536. filters.Add("hwupload");
  537. }
  538. else if (state.DeInterlace && !string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  539. {
  540. filters.Add("yadif=0:-1:0");
  541. }
  542. if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  543. {
  544. // Work around vaapi's reduced scaling features
  545. var scaler = "scale_vaapi";
  546. // Given the input dimensions (inputWidth, inputHeight), determine the output dimensions
  547. // (outputWidth, outputHeight). The user may request precise output dimensions or maximum
  548. // output dimensions. Output dimensions are guaranteed to be even.
  549. decimal inputWidth = Convert.ToDecimal(state.VideoStream.Width);
  550. decimal inputHeight = Convert.ToDecimal(state.VideoStream.Height);
  551. decimal outputWidth = request.Width.HasValue ? Convert.ToDecimal(request.Width.Value) : inputWidth;
  552. decimal outputHeight = request.Height.HasValue ? Convert.ToDecimal(request.Height.Value) : inputHeight;
  553. decimal maximumWidth = request.MaxWidth.HasValue ? Convert.ToDecimal(request.MaxWidth.Value) : outputWidth;
  554. decimal maximumHeight = request.MaxHeight.HasValue ? Convert.ToDecimal(request.MaxHeight.Value) : outputHeight;
  555. if (outputWidth > maximumWidth || outputHeight > maximumHeight)
  556. {
  557. var scale = Math.Min(maximumWidth / outputWidth, maximumHeight / outputHeight);
  558. outputWidth = Math.Min(maximumWidth, Math.Truncate(outputWidth * scale));
  559. outputHeight = Math.Min(maximumHeight, Math.Truncate(outputHeight * scale));
  560. }
  561. outputWidth = 2 * Math.Truncate(outputWidth / 2);
  562. outputHeight = 2 * Math.Truncate(outputHeight / 2);
  563. if (outputWidth != inputWidth || outputHeight != inputHeight)
  564. {
  565. filters.Add(string.Format("{0}=w={1}:h={2}", scaler, outputWidth.ToString(UsCulture), outputHeight.ToString(UsCulture)));
  566. }
  567. }
  568. else
  569. {
  570. // If fixed dimensions were supplied
  571. if (request.Width.HasValue && request.Height.HasValue)
  572. {
  573. var widthParam = request.Width.Value.ToString(UsCulture);
  574. var heightParam = request.Height.Value.ToString(UsCulture);
  575. filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", widthParam, heightParam));
  576. }
  577. // If Max dimensions were supplied, for width selects lowest even number between input width and width req size and selects lowest even number from in width*display aspect and requested size
  578. else if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
  579. {
  580. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  581. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  582. filters.Add(string.Format("scale=trunc(min(max(iw\\,ih*dar)\\,min({0}\\,{1}*dar))/2)*2:trunc(min(max(iw/dar\\,ih)\\,min({0}/dar\\,{1}))/2)*2", maxWidthParam, maxHeightParam));
  583. }
  584. // If a fixed width was requested
  585. else if (request.Width.HasValue)
  586. {
  587. var widthParam = request.Width.Value.ToString(UsCulture);
  588. filters.Add(string.Format("scale={0}:trunc(ow/a/2)*2", widthParam));
  589. }
  590. // If a fixed height was requested
  591. else if (request.Height.HasValue)
  592. {
  593. var heightParam = request.Height.Value.ToString(UsCulture);
  594. filters.Add(string.Format("scale=trunc(oh*a/2)*2:{0}", heightParam));
  595. }
  596. // If a max width was requested
  597. else if (request.MaxWidth.HasValue)
  598. {
  599. var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
  600. filters.Add(string.Format("scale=trunc(min(max(iw\\,ih*dar)\\,{0})/2)*2:trunc(ow/dar/2)*2", maxWidthParam));
  601. }
  602. // If a max height was requested
  603. else if (request.MaxHeight.HasValue)
  604. {
  605. var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
  606. filters.Add(string.Format("scale=trunc(oh*a/2)*2:min(max(iw/dar\\,ih)\\,{0})", maxHeightParam));
  607. }
  608. }
  609. var output = string.Empty;
  610. if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.VideoRequest.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  611. {
  612. var subParam = GetTextSubtitleParam(state);
  613. filters.Add(subParam);
  614. if (allowTimeStampCopy)
  615. {
  616. output += " -copyts";
  617. }
  618. }
  619. if (filters.Count > 0)
  620. {
  621. output += string.Format(" -vf \"{0}\"", string.Join(",", filters.ToArray()));
  622. }
  623. return output;
  624. }
  625. /// <summary>
  626. /// Gets the text subtitle param.
  627. /// </summary>
  628. /// <param name="state">The state.</param>
  629. /// <returns>System.String.</returns>
  630. protected string GetTextSubtitleParam(StreamState state)
  631. {
  632. var seconds = Math.Round(TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds);
  633. var setPtsParam = state.VideoRequest.CopyTimestamps
  634. ? string.Empty
  635. : string.Format(",setpts=PTS -{0}/TB", seconds.ToString(UsCulture));
  636. if (state.SubtitleStream.IsExternal)
  637. {
  638. var subtitlePath = state.SubtitleStream.Path;
  639. var charsetParam = string.Empty;
  640. if (!string.IsNullOrEmpty(state.SubtitleStream.Language))
  641. {
  642. var charenc = SubtitleEncoder.GetSubtitleFileCharacterSet(subtitlePath, state.SubtitleStream.Language, state.MediaSource.Protocol, CancellationToken.None).Result;
  643. if (!string.IsNullOrEmpty(charenc))
  644. {
  645. charsetParam = ":charenc=" + charenc;
  646. }
  647. }
  648. // TODO: Perhaps also use original_size=1920x800 ??
  649. return string.Format("subtitles=filename='{0}'{1}{2}",
  650. MediaEncoder.EscapeSubtitleFilterPath(subtitlePath),
  651. charsetParam,
  652. setPtsParam);
  653. }
  654. var mediaPath = state.MediaPath ?? string.Empty;
  655. return string.Format("subtitles='{0}:si={1}'{2}",
  656. MediaEncoder.EscapeSubtitleFilterPath(mediaPath),
  657. state.InternalSubtitleStreamOffset.ToString(UsCulture),
  658. setPtsParam);
  659. }
  660. /// <summary>
  661. /// Gets the internal graphical subtitle param.
  662. /// </summary>
  663. /// <param name="state">The state.</param>
  664. /// <param name="outputVideoCodec">The output video codec.</param>
  665. /// <returns>System.String.</returns>
  666. protected string GetGraphicalSubtitleParam(StreamState state, string outputVideoCodec)
  667. {
  668. var outputSizeParam = string.Empty;
  669. var request = state.VideoRequest;
  670. // Add resolution params, if specified
  671. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  672. {
  673. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec).TrimEnd('"');
  674. if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
  675. {
  676. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("format", StringComparison.OrdinalIgnoreCase));
  677. }
  678. else
  679. {
  680. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  681. }
  682. }
  683. if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase) && outputSizeParam.Length == 0)
  684. {
  685. outputSizeParam = ",format=nv12|vaapi,hwupload";
  686. }
  687. var videoSizeParam = string.Empty;
  688. if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
  689. {
  690. videoSizeParam = string.Format("scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture));
  691. }
  692. var mapPrefix = state.SubtitleStream.IsExternal ?
  693. 1 :
  694. 0;
  695. var subtitleStreamIndex = state.SubtitleStream.IsExternal
  696. ? 0
  697. : state.SubtitleStream.Index;
  698. return string.Format(" -filter_complex \"[{0}:{1}]{4}[sub] ; [0:{2}] [sub] overlay{3}\"",
  699. mapPrefix.ToString(UsCulture),
  700. subtitleStreamIndex.ToString(UsCulture),
  701. state.VideoStream.Index.ToString(UsCulture),
  702. outputSizeParam,
  703. videoSizeParam);
  704. }
  705. /// <summary>
  706. /// Gets the probe size argument.
  707. /// </summary>
  708. /// <param name="state">The state.</param>
  709. /// <returns>System.String.</returns>
  710. private string GetProbeSizeArgument(StreamState state)
  711. {
  712. if (state.PlayableStreamFileNames.Count > 0)
  713. {
  714. return MediaEncoder.GetProbeSizeAndAnalyzeDurationArgument(state.PlayableStreamFileNames.ToArray(), state.InputProtocol);
  715. }
  716. return MediaEncoder.GetProbeSizeAndAnalyzeDurationArgument(new[] { state.MediaPath }, state.InputProtocol);
  717. }
  718. /// <summary>
  719. /// Gets the number of audio channels to specify on the command line
  720. /// </summary>
  721. /// <param name="request">The request.</param>
  722. /// <param name="audioStream">The audio stream.</param>
  723. /// <param name="outputAudioCodec">The output audio codec.</param>
  724. /// <returns>System.Nullable{System.Int32}.</returns>
  725. private int? GetNumAudioChannelsParam(StreamRequest request, MediaStream audioStream, string outputAudioCodec)
  726. {
  727. var inputChannels = audioStream == null
  728. ? null
  729. : audioStream.Channels;
  730. if (inputChannels <= 0)
  731. {
  732. inputChannels = null;
  733. }
  734. int? transcoderChannelLimit = null;
  735. var codec = outputAudioCodec ?? string.Empty;
  736. if (codec.IndexOf("wma", StringComparison.OrdinalIgnoreCase) != -1)
  737. {
  738. // wmav2 currently only supports two channel output
  739. transcoderChannelLimit = 2;
  740. }
  741. else if (codec.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
  742. {
  743. // libmp3lame currently only supports two channel output
  744. transcoderChannelLimit = 2;
  745. }
  746. else
  747. {
  748. // If we don't have any media info then limit it to 6 to prevent encoding errors due to asking for too many channels
  749. transcoderChannelLimit = 6;
  750. }
  751. var isTranscodingAudio = !string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase);
  752. int? resultChannels = null;
  753. if (isTranscodingAudio)
  754. {
  755. resultChannels = request.TranscodingMaxAudioChannels;
  756. }
  757. resultChannels = resultChannels ?? request.MaxAudioChannels ?? request.AudioChannels;
  758. if (inputChannels.HasValue)
  759. {
  760. resultChannels = resultChannels.HasValue
  761. ? Math.Min(resultChannels.Value, inputChannels.Value)
  762. : inputChannels.Value;
  763. }
  764. if (isTranscodingAudio && transcoderChannelLimit.HasValue)
  765. {
  766. resultChannels = resultChannels.HasValue
  767. ? Math.Min(resultChannels.Value, transcoderChannelLimit.Value)
  768. : transcoderChannelLimit.Value;
  769. }
  770. return resultChannels ?? request.AudioChannels;
  771. }
  772. /// <summary>
  773. /// Determines whether the specified stream is H264.
  774. /// </summary>
  775. /// <param name="stream">The stream.</param>
  776. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  777. protected bool IsH264(MediaStream stream)
  778. {
  779. var codec = stream.Codec ?? string.Empty;
  780. return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  781. codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  782. }
  783. /// <summary>
  784. /// Gets the audio encoder.
  785. /// </summary>
  786. /// <param name="state">The state.</param>
  787. /// <returns>System.String.</returns>
  788. protected string GetAudioEncoder(StreamState state)
  789. {
  790. var codec = state.OutputAudioCodec;
  791. if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase))
  792. {
  793. return "aac -strict experimental";
  794. }
  795. if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase))
  796. {
  797. return "libmp3lame";
  798. }
  799. if (string.Equals(codec, "vorbis", StringComparison.OrdinalIgnoreCase))
  800. {
  801. return "libvorbis";
  802. }
  803. if (string.Equals(codec, "wma", StringComparison.OrdinalIgnoreCase))
  804. {
  805. return "wmav2";
  806. }
  807. return codec.ToLower();
  808. }
  809. /// <summary>
  810. /// Gets the name of the output video codec
  811. /// </summary>
  812. /// <param name="state">The state.</param>
  813. /// <returns>System.String.</returns>
  814. protected string GetVideoEncoder(StreamState state)
  815. {
  816. var codec = state.OutputVideoCodec;
  817. if (!string.IsNullOrEmpty(codec))
  818. {
  819. if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase))
  820. {
  821. return GetH264Encoder(state);
  822. }
  823. if (string.Equals(codec, "vpx", StringComparison.OrdinalIgnoreCase))
  824. {
  825. return "libvpx";
  826. }
  827. if (string.Equals(codec, "wmv", StringComparison.OrdinalIgnoreCase))
  828. {
  829. return "wmv2";
  830. }
  831. if (string.Equals(codec, "theora", StringComparison.OrdinalIgnoreCase))
  832. {
  833. return "libtheora";
  834. }
  835. return codec.ToLower();
  836. }
  837. return "copy";
  838. }
  839. /// <summary>
  840. /// Gets the name of the output video codec
  841. /// </summary>
  842. /// <param name="state">The state.</param>
  843. /// <returns>System.String.</returns>
  844. protected string GetVideoDecoder(StreamState state)
  845. {
  846. if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  847. {
  848. return null;
  849. }
  850. // Only use alternative encoders for video files.
  851. // When using concat with folder rips, if the mfx session fails to initialize, ffmpeg will be stuck retrying and will not exit gracefully
  852. // Since transcoding of folder rips is expiremental anyway, it's not worth adding additional variables such as this.
  853. if (state.VideoType != VideoType.VideoFile)
  854. {
  855. return null;
  856. }
  857. if (state.VideoStream != null && !string.IsNullOrWhiteSpace(state.VideoStream.Codec))
  858. {
  859. if (string.Equals(ApiEntryPoint.Instance.GetEncodingOptions().HardwareAccelerationType, "qsv", StringComparison.OrdinalIgnoreCase))
  860. {
  861. switch (state.MediaSource.VideoStream.Codec.ToLower())
  862. {
  863. case "avc":
  864. case "h264":
  865. if (MediaEncoder.SupportsDecoder("h264_qsv"))
  866. {
  867. // Seeing stalls and failures with decoding. Not worth it compared to encoding.
  868. return "-c:v h264_qsv ";
  869. }
  870. break;
  871. case "mpeg2video":
  872. if (MediaEncoder.SupportsDecoder("mpeg2_qsv"))
  873. {
  874. return "-c:v mpeg2_qsv ";
  875. }
  876. break;
  877. case "vc1":
  878. if (MediaEncoder.SupportsDecoder("vc1_qsv"))
  879. {
  880. return "-c:v vc1_qsv ";
  881. }
  882. break;
  883. }
  884. }
  885. }
  886. // leave blank so ffmpeg will decide
  887. return null;
  888. }
  889. /// <summary>
  890. /// Gets the input argument.
  891. /// </summary>
  892. /// <param name="state">The state.</param>
  893. /// <returns>System.String.</returns>
  894. protected string GetInputArgument(StreamState state)
  895. {
  896. var arg = string.Format("-i {0}", GetInputPathArgument(state));
  897. if (state.SubtitleStream != null && state.VideoRequest.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  898. {
  899. if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
  900. {
  901. if (state.VideoStream != null && state.VideoStream.Width.HasValue)
  902. {
  903. // This is hacky but not sure how to get the exact subtitle resolution
  904. double height = state.VideoStream.Width.Value;
  905. height /= 16;
  906. height *= 9;
  907. arg += string.Format(" -canvas_size {0}:{1}", state.VideoStream.Width.Value.ToString(CultureInfo.InvariantCulture), Convert.ToInt32(height).ToString(CultureInfo.InvariantCulture));
  908. }
  909. var subtitlePath = state.SubtitleStream.Path;
  910. if (string.Equals(Path.GetExtension(subtitlePath), ".sub", StringComparison.OrdinalIgnoreCase))
  911. {
  912. var idxFile = Path.ChangeExtension(subtitlePath, ".idx");
  913. if (FileSystem.FileExists(idxFile))
  914. {
  915. subtitlePath = idxFile;
  916. }
  917. }
  918. arg += " -i \"" + subtitlePath + "\"";
  919. }
  920. }
  921. if (state.VideoRequest != null)
  922. {
  923. var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions();
  924. if (GetVideoEncoder(state).IndexOf("vaapi", StringComparison.OrdinalIgnoreCase) != -1)
  925. {
  926. var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.VideoRequest.SubtitleMethod == SubtitleDeliveryMethod.Encode;
  927. var hwOutputFormat = "vaapi";
  928. if (hasGraphicalSubs)
  929. {
  930. hwOutputFormat = "yuv420p";
  931. }
  932. arg = "-hwaccel vaapi -hwaccel_output_format " + hwOutputFormat + " -vaapi_device " + encodingOptions.VaapiDevice + " " + arg;
  933. }
  934. }
  935. return arg.Trim();
  936. }
  937. private string GetInputPathArgument(StreamState state)
  938. {
  939. var protocol = state.InputProtocol;
  940. var mediaPath = state.MediaPath ?? string.Empty;
  941. var inputPath = new[] { mediaPath };
  942. if (state.IsInputVideo)
  943. {
  944. if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
  945. {
  946. inputPath = MediaEncoderHelpers.GetInputArgument(FileSystem, mediaPath, state.InputProtocol, state.IsoMount, state.PlayableStreamFileNames);
  947. }
  948. }
  949. return MediaEncoder.GetInputArgument(inputPath, protocol);
  950. }
  951. private async Task AcquireResources(StreamState state, CancellationTokenSource cancellationTokenSource)
  952. {
  953. if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
  954. {
  955. state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationTokenSource.Token).ConfigureAwait(false);
  956. }
  957. if (state.MediaSource.RequiresOpening && string.IsNullOrWhiteSpace(state.Request.LiveStreamId))
  958. {
  959. var liveStreamResponse = await MediaSourceManager.OpenLiveStream(new LiveStreamRequest
  960. {
  961. OpenToken = state.MediaSource.OpenToken
  962. }, false, cancellationTokenSource.Token).ConfigureAwait(false);
  963. AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, state.VideoRequest, state.RequestedUrl);
  964. if (state.VideoRequest != null)
  965. {
  966. TryStreamCopy(state, state.VideoRequest);
  967. }
  968. }
  969. if (state.MediaSource.BufferMs.HasValue)
  970. {
  971. await Task.Delay(state.MediaSource.BufferMs.Value, cancellationTokenSource.Token).ConfigureAwait(false);
  972. }
  973. }
  974. /// <summary>
  975. /// Starts the FFMPEG.
  976. /// </summary>
  977. /// <param name="state">The state.</param>
  978. /// <param name="outputPath">The output path.</param>
  979. /// <param name="cancellationTokenSource">The cancellation token source.</param>
  980. /// <param name="workingDirectory">The working directory.</param>
  981. /// <returns>Task.</returns>
  982. protected async Task<TranscodingJob> StartFfMpeg(StreamState state,
  983. string outputPath,
  984. CancellationTokenSource cancellationTokenSource,
  985. string workingDirectory = null)
  986. {
  987. FileSystem.CreateDirectory(Path.GetDirectoryName(outputPath));
  988. await AcquireResources(state, cancellationTokenSource).ConfigureAwait(false);
  989. if (state.VideoRequest != null && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  990. {
  991. var auth = AuthorizationContext.GetAuthorizationInfo(Request);
  992. if (!string.IsNullOrWhiteSpace(auth.UserId))
  993. {
  994. var user = UserManager.GetUserById(auth.UserId);
  995. if (!user.Policy.EnableVideoPlaybackTranscoding)
  996. {
  997. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType, state);
  998. throw new ArgumentException("User does not have access to video transcoding");
  999. }
  1000. }
  1001. }
  1002. var transcodingId = Guid.NewGuid().ToString("N");
  1003. var commandLineArgs = GetCommandLineArguments(outputPath, state, true);
  1004. var process = ApiEntryPoint.Instance.ProcessFactory.Create(new ProcessOptions
  1005. {
  1006. CreateNoWindow = true,
  1007. UseShellExecute = false,
  1008. // Must consume both stdout and stderr or deadlocks may occur
  1009. //RedirectStandardOutput = true,
  1010. RedirectStandardError = true,
  1011. RedirectStandardInput = true,
  1012. FileName = MediaEncoder.EncoderPath,
  1013. Arguments = commandLineArgs,
  1014. IsHidden = true,
  1015. ErrorDialog = false,
  1016. EnableRaisingEvents = true,
  1017. WorkingDirectory = !string.IsNullOrWhiteSpace(workingDirectory) ? workingDirectory : null
  1018. });
  1019. var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath,
  1020. state.Request.PlaySessionId,
  1021. state.MediaSource.LiveStreamId,
  1022. transcodingId,
  1023. TranscodingJobType,
  1024. process,
  1025. state.Request.DeviceId,
  1026. state,
  1027. cancellationTokenSource);
  1028. var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments;
  1029. Logger.Info(commandLineLogMessage);
  1030. var logFilePrefix = "ffmpeg-transcode";
  1031. if (state.VideoRequest != null && string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) && string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase))
  1032. {
  1033. logFilePrefix = "ffmpeg-directstream";
  1034. }
  1035. else if (state.VideoRequest != null && string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  1036. {
  1037. logFilePrefix = "ffmpeg-remux";
  1038. }
  1039. var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, logFilePrefix + "-" + Guid.NewGuid() + ".txt");
  1040. FileSystem.CreateDirectory(Path.GetDirectoryName(logFilePath));
  1041. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  1042. state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true);
  1043. var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(Request.AbsoluteUri + Environment.NewLine + Environment.NewLine + JsonSerializer.SerializeToString(state.MediaSource) + Environment.NewLine + Environment.NewLine + commandLineLogMessage + Environment.NewLine + Environment.NewLine);
  1044. await state.LogFileStream.WriteAsync(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length, cancellationTokenSource.Token).ConfigureAwait(false);
  1045. process.Exited += (sender, args) => OnFfMpegProcessExited(process, transcodingJob, state);
  1046. try
  1047. {
  1048. process.Start();
  1049. }
  1050. catch (Exception ex)
  1051. {
  1052. Logger.ErrorException("Error starting ffmpeg", ex);
  1053. ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType, state);
  1054. throw;
  1055. }
  1056. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  1057. //process.BeginOutputReadLine();
  1058. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  1059. var task = Task.Run(() => StartStreamingLog(transcodingJob, state, process.StandardError.BaseStream, state.LogFileStream));
  1060. // Wait for the file to exist before proceeeding
  1061. while (!FileSystem.FileExists(state.WaitForPath ?? outputPath) && !transcodingJob.HasExited)
  1062. {
  1063. await Task.Delay(100, cancellationTokenSource.Token).ConfigureAwait(false);
  1064. }
  1065. if (state.IsInputVideo && transcodingJob.Type == TranscodingJobType.Progressive && !transcodingJob.HasExited)
  1066. {
  1067. await Task.Delay(1000, cancellationTokenSource.Token).ConfigureAwait(false);
  1068. if (state.ReadInputAtNativeFramerate && !transcodingJob.HasExited)
  1069. {
  1070. await Task.Delay(1500, cancellationTokenSource.Token).ConfigureAwait(false);
  1071. }
  1072. }
  1073. if (!transcodingJob.HasExited)
  1074. {
  1075. StartThrottler(state, transcodingJob);
  1076. }
  1077. ReportUsage(state);
  1078. return transcodingJob;
  1079. }
  1080. private void StartThrottler(StreamState state, TranscodingJob transcodingJob)
  1081. {
  1082. if (EnableThrottling(state))
  1083. {
  1084. transcodingJob.TranscodingThrottler = state.TranscodingThrottler = new TranscodingThrottler(transcodingJob, Logger, ServerConfigurationManager, ApiEntryPoint.Instance.TimerFactory, FileSystem);
  1085. state.TranscodingThrottler.Start();
  1086. }
  1087. }
  1088. private bool EnableThrottling(StreamState state)
  1089. {
  1090. return false;
  1091. // do not use throttling with hardware encoders
  1092. return state.InputProtocol == MediaProtocol.File &&
  1093. state.RunTimeTicks.HasValue &&
  1094. state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks &&
  1095. state.IsInputVideo &&
  1096. state.VideoType == VideoType.VideoFile &&
  1097. !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) &&
  1098. string.Equals(GetVideoEncoder(state), "libx264", StringComparison.OrdinalIgnoreCase);
  1099. }
  1100. private async Task StartStreamingLog(TranscodingJob transcodingJob, StreamState state, Stream source, Stream target)
  1101. {
  1102. try
  1103. {
  1104. using (var reader = new StreamReader(source))
  1105. {
  1106. while (!reader.EndOfStream)
  1107. {
  1108. var line = await reader.ReadLineAsync().ConfigureAwait(false);
  1109. ParseLogLine(line, transcodingJob, state);
  1110. var bytes = Encoding.UTF8.GetBytes(Environment.NewLine + line);
  1111. await target.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  1112. await target.FlushAsync().ConfigureAwait(false);
  1113. }
  1114. }
  1115. }
  1116. catch (ObjectDisposedException)
  1117. {
  1118. // Don't spam the log. This doesn't seem to throw in windows, but sometimes under linux
  1119. }
  1120. catch (Exception ex)
  1121. {
  1122. Logger.ErrorException("Error reading ffmpeg log", ex);
  1123. }
  1124. }
  1125. private void ParseLogLine(string line, TranscodingJob transcodingJob, StreamState state)
  1126. {
  1127. float? framerate = null;
  1128. double? percent = null;
  1129. TimeSpan? transcodingPosition = null;
  1130. long? bytesTranscoded = null;
  1131. int? bitRate = null;
  1132. var parts = line.Split(' ');
  1133. var totalMs = state.RunTimeTicks.HasValue
  1134. ? TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalMilliseconds
  1135. : 0;
  1136. var startMs = state.Request.StartTimeTicks.HasValue
  1137. ? TimeSpan.FromTicks(state.Request.StartTimeTicks.Value).TotalMilliseconds
  1138. : 0;
  1139. for (var i = 0; i < parts.Length; i++)
  1140. {
  1141. var part = parts[i];
  1142. if (string.Equals(part, "fps=", StringComparison.OrdinalIgnoreCase) &&
  1143. (i + 1 < parts.Length))
  1144. {
  1145. var rate = parts[i + 1];
  1146. float val;
  1147. if (float.TryParse(rate, NumberStyles.Any, UsCulture, out val))
  1148. {
  1149. framerate = val;
  1150. }
  1151. }
  1152. else if (state.RunTimeTicks.HasValue &&
  1153. part.StartsWith("time=", StringComparison.OrdinalIgnoreCase))
  1154. {
  1155. var time = part.Split(new[] { '=' }, 2).Last();
  1156. TimeSpan val;
  1157. if (TimeSpan.TryParse(time, UsCulture, out val))
  1158. {
  1159. var currentMs = startMs + val.TotalMilliseconds;
  1160. var percentVal = currentMs / totalMs;
  1161. percent = 100 * percentVal;
  1162. transcodingPosition = val;
  1163. }
  1164. }
  1165. else if (part.StartsWith("size=", StringComparison.OrdinalIgnoreCase))
  1166. {
  1167. var size = part.Split(new[] { '=' }, 2).Last();
  1168. int? scale = null;
  1169. if (size.IndexOf("kb", StringComparison.OrdinalIgnoreCase) != -1)
  1170. {
  1171. scale = 1024;
  1172. size = size.Replace("kb", string.Empty, StringComparison.OrdinalIgnoreCase);
  1173. }
  1174. if (scale.HasValue)
  1175. {
  1176. long val;
  1177. if (long.TryParse(size, NumberStyles.Any, UsCulture, out val))
  1178. {
  1179. bytesTranscoded = val * scale.Value;
  1180. }
  1181. }
  1182. }
  1183. else if (part.StartsWith("bitrate=", StringComparison.OrdinalIgnoreCase))
  1184. {
  1185. var rate = part.Split(new[] { '=' }, 2).Last();
  1186. int? scale = null;
  1187. if (rate.IndexOf("kbits/s", StringComparison.OrdinalIgnoreCase) != -1)
  1188. {
  1189. scale = 1024;
  1190. rate = rate.Replace("kbits/s", string.Empty, StringComparison.OrdinalIgnoreCase);
  1191. }
  1192. if (scale.HasValue)
  1193. {
  1194. float val;
  1195. if (float.TryParse(rate, NumberStyles.Any, UsCulture, out val))
  1196. {
  1197. bitRate = (int)Math.Ceiling(val * scale.Value);
  1198. }
  1199. }
  1200. }
  1201. }
  1202. if (framerate.HasValue || percent.HasValue)
  1203. {
  1204. ApiEntryPoint.Instance.ReportTranscodingProgress(transcodingJob, state, transcodingPosition, framerate, percent, bytesTranscoded, bitRate);
  1205. }
  1206. }
  1207. private int? GetVideoBitrateParamValue(VideoStreamRequest request, MediaStream videoStream, string outputVideoCodec)
  1208. {
  1209. var bitrate = request.VideoBitRate;
  1210. if (videoStream != null)
  1211. {
  1212. var isUpscaling = request.Height.HasValue && videoStream.Height.HasValue &&
  1213. request.Height.Value > videoStream.Height.Value;
  1214. if (request.Width.HasValue && videoStream.Width.HasValue &&
  1215. request.Width.Value > videoStream.Width.Value)
  1216. {
  1217. isUpscaling = true;
  1218. }
  1219. // Don't allow bitrate increases unless upscaling
  1220. if (!isUpscaling)
  1221. {
  1222. if (bitrate.HasValue && videoStream.BitRate.HasValue)
  1223. {
  1224. bitrate = Math.Min(bitrate.Value, videoStream.BitRate.Value);
  1225. }
  1226. }
  1227. }
  1228. if (bitrate.HasValue)
  1229. {
  1230. var inputVideoCodec = videoStream == null ? null : videoStream.Codec;
  1231. bitrate = ResolutionNormalizer.ScaleBitrate(bitrate.Value, inputVideoCodec, outputVideoCodec);
  1232. // If a max bitrate was requested, don't let the scaled bitrate exceed it
  1233. if (request.VideoBitRate.HasValue)
  1234. {
  1235. bitrate = Math.Min(bitrate.Value, request.VideoBitRate.Value);
  1236. }
  1237. }
  1238. return bitrate;
  1239. }
  1240. protected string GetVideoBitrateParam(StreamState state, string videoCodec)
  1241. {
  1242. var bitrate = state.OutputVideoBitrate;
  1243. if (bitrate.HasValue)
  1244. {
  1245. if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
  1246. {
  1247. // With vpx when crf is used, b:v becomes a max rate
  1248. // https://trac.ffmpeg.org/wiki/vpxEncodingGuide. But higher bitrate source files -b:v causes judder so limite the bitrate but dont allow it to "saturate" the bitrate. So dont contrain it down just up.
  1249. return string.Format(" -maxrate:v {0} -bufsize:v ({0}*2) -b:v {0}", bitrate.Value.ToString(UsCulture));
  1250. }
  1251. if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  1252. {
  1253. return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
  1254. }
  1255. if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
  1256. {
  1257. // h264
  1258. return string.Format(" -maxrate {0} -bufsize {1}",
  1259. bitrate.Value.ToString(UsCulture),
  1260. (bitrate.Value * 2).ToString(UsCulture));
  1261. }
  1262. // h264
  1263. return string.Format(" -b:v {0} -maxrate {0} -bufsize {1}",
  1264. bitrate.Value.ToString(UsCulture),
  1265. (bitrate.Value * 2).ToString(UsCulture));
  1266. }
  1267. return string.Empty;
  1268. }
  1269. private int? GetAudioBitrateParam(StreamRequest request, MediaStream audioStream)
  1270. {
  1271. if (request.AudioBitRate.HasValue)
  1272. {
  1273. // Make sure we don't request a bitrate higher than the source
  1274. var currentBitrate = audioStream == null ? request.AudioBitRate.Value : audioStream.BitRate ?? request.AudioBitRate.Value;
  1275. // Don't encode any higher than this
  1276. return Math.Min(384000, request.AudioBitRate.Value);
  1277. //return Math.Min(currentBitrate, request.AudioBitRate.Value);
  1278. }
  1279. return null;
  1280. }
  1281. /// <summary>
  1282. /// Gets the user agent param.
  1283. /// </summary>
  1284. /// <param name="state">The state.</param>
  1285. /// <returns>System.String.</returns>
  1286. private string GetUserAgentParam(StreamState state)
  1287. {
  1288. string useragent = null;
  1289. state.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent);
  1290. if (!string.IsNullOrWhiteSpace(useragent))
  1291. {
  1292. return "-user-agent \"" + useragent + "\"";
  1293. }
  1294. return string.Empty;
  1295. }
  1296. /// <summary>
  1297. /// Processes the exited.
  1298. /// </summary>
  1299. /// <param name="process">The process.</param>
  1300. /// <param name="job">The job.</param>
  1301. /// <param name="state">The state.</param>
  1302. private void OnFfMpegProcessExited(IProcess process, TranscodingJob job, StreamState state)
  1303. {
  1304. if (job != null)
  1305. {
  1306. job.HasExited = true;
  1307. }
  1308. Logger.Debug("Disposing stream resources");
  1309. state.Dispose();
  1310. try
  1311. {
  1312. Logger.Info("FFMpeg exited with code {0}", process.ExitCode);
  1313. }
  1314. catch
  1315. {
  1316. Logger.Error("FFMpeg exited with an error.");
  1317. }
  1318. // This causes on exited to be called twice:
  1319. //try
  1320. //{
  1321. // // Dispose the process
  1322. // process.Dispose();
  1323. //}
  1324. //catch (Exception ex)
  1325. //{
  1326. // Logger.ErrorException("Error disposing ffmpeg.", ex);
  1327. //}
  1328. }
  1329. protected double? GetFramerateParam(StreamState state)
  1330. {
  1331. if (state.VideoRequest != null)
  1332. {
  1333. if (state.VideoRequest.Framerate.HasValue)
  1334. {
  1335. return state.VideoRequest.Framerate.Value;
  1336. }
  1337. var maxrate = state.VideoRequest.MaxFramerate;
  1338. if (maxrate.HasValue && state.VideoStream != null)
  1339. {
  1340. var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
  1341. if (contentRate.HasValue && contentRate.Value > maxrate.Value)
  1342. {
  1343. return maxrate;
  1344. }
  1345. }
  1346. }
  1347. return null;
  1348. }
  1349. /// <summary>
  1350. /// Parses the parameters.
  1351. /// </summary>
  1352. /// <param name="request">The request.</param>
  1353. private void ParseParams(StreamRequest request)
  1354. {
  1355. var vals = request.Params.Split(';');
  1356. var videoRequest = request as VideoStreamRequest;
  1357. for (var i = 0; i < vals.Length; i++)
  1358. {
  1359. var val = vals[i];
  1360. if (string.IsNullOrWhiteSpace(val))
  1361. {
  1362. continue;
  1363. }
  1364. if (i == 0)
  1365. {
  1366. request.DeviceProfileId = val;
  1367. }
  1368. else if (i == 1)
  1369. {
  1370. request.DeviceId = val;
  1371. }
  1372. else if (i == 2)
  1373. {
  1374. request.MediaSourceId = val;
  1375. }
  1376. else if (i == 3)
  1377. {
  1378. request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  1379. }
  1380. else if (i == 4)
  1381. {
  1382. if (videoRequest != null)
  1383. {
  1384. videoRequest.VideoCodec = val;
  1385. }
  1386. }
  1387. else if (i == 5)
  1388. {
  1389. request.AudioCodec = val;
  1390. }
  1391. else if (i == 6)
  1392. {
  1393. if (videoRequest != null)
  1394. {
  1395. videoRequest.AudioStreamIndex = int.Parse(val, UsCulture);
  1396. }
  1397. }
  1398. else if (i == 7)
  1399. {
  1400. if (videoRequest != null)
  1401. {
  1402. videoRequest.SubtitleStreamIndex = int.Parse(val, UsCulture);
  1403. }
  1404. }
  1405. else if (i == 8)
  1406. {
  1407. if (videoRequest != null)
  1408. {
  1409. videoRequest.VideoBitRate = int.Parse(val, UsCulture);
  1410. }
  1411. }
  1412. else if (i == 9)
  1413. {
  1414. request.AudioBitRate = int.Parse(val, UsCulture);
  1415. }
  1416. else if (i == 10)
  1417. {
  1418. request.MaxAudioChannels = int.Parse(val, UsCulture);
  1419. }
  1420. else if (i == 11)
  1421. {
  1422. if (videoRequest != null)
  1423. {
  1424. videoRequest.MaxFramerate = float.Parse(val, UsCulture);
  1425. }
  1426. }
  1427. else if (i == 12)
  1428. {
  1429. if (videoRequest != null)
  1430. {
  1431. videoRequest.MaxWidth = int.Parse(val, UsCulture);
  1432. }
  1433. }
  1434. else if (i == 13)
  1435. {
  1436. if (videoRequest != null)
  1437. {
  1438. videoRequest.MaxHeight = int.Parse(val, UsCulture);
  1439. }
  1440. }
  1441. else if (i == 14)
  1442. {
  1443. request.StartTimeTicks = long.Parse(val, UsCulture);
  1444. }
  1445. else if (i == 15)
  1446. {
  1447. if (videoRequest != null)
  1448. {
  1449. videoRequest.Level = val;
  1450. }
  1451. }
  1452. else if (i == 16)
  1453. {
  1454. if (videoRequest != null)
  1455. {
  1456. videoRequest.MaxRefFrames = int.Parse(val, UsCulture);
  1457. }
  1458. }
  1459. else if (i == 17)
  1460. {
  1461. if (videoRequest != null)
  1462. {
  1463. videoRequest.MaxVideoBitDepth = int.Parse(val, UsCulture);
  1464. }
  1465. }
  1466. else if (i == 18)
  1467. {
  1468. if (videoRequest != null)
  1469. {
  1470. videoRequest.Profile = val;
  1471. }
  1472. }
  1473. else if (i == 19)
  1474. {
  1475. // cabac no longer used
  1476. }
  1477. else if (i == 20)
  1478. {
  1479. request.PlaySessionId = val;
  1480. }
  1481. else if (i == 21)
  1482. {
  1483. // api_key
  1484. }
  1485. else if (i == 22)
  1486. {
  1487. request.LiveStreamId = val;
  1488. }
  1489. else if (i == 23)
  1490. {
  1491. // Duplicating ItemId because of MediaMonkey
  1492. }
  1493. else if (i == 24)
  1494. {
  1495. if (videoRequest != null)
  1496. {
  1497. videoRequest.CopyTimestamps = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  1498. }
  1499. }
  1500. else if (i == 25)
  1501. {
  1502. if (!string.IsNullOrWhiteSpace(val) && videoRequest != null)
  1503. {
  1504. SubtitleDeliveryMethod method;
  1505. if (Enum.TryParse(val, out method))
  1506. {
  1507. videoRequest.SubtitleMethod = method;
  1508. }
  1509. }
  1510. }
  1511. else if (i == 26)
  1512. {
  1513. request.TranscodingMaxAudioChannels = int.Parse(val, UsCulture);
  1514. }
  1515. else if (i == 27)
  1516. {
  1517. if (videoRequest != null)
  1518. {
  1519. videoRequest.EnableSubtitlesInManifest = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  1520. }
  1521. }
  1522. else if (i == 28)
  1523. {
  1524. request.Tag = val;
  1525. }
  1526. else if (i == 29)
  1527. {
  1528. if (videoRequest != null)
  1529. {
  1530. videoRequest.RequireAvc = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
  1531. }
  1532. }
  1533. }
  1534. }
  1535. /// <summary>
  1536. /// Parses the dlna headers.
  1537. /// </summary>
  1538. /// <param name="request">The request.</param>
  1539. private void ParseDlnaHeaders(StreamRequest request)
  1540. {
  1541. if (!request.StartTimeTicks.HasValue)
  1542. {
  1543. var timeSeek = GetHeader("TimeSeekRange.dlna.org");
  1544. request.StartTimeTicks = ParseTimeSeekHeader(timeSeek);
  1545. }
  1546. }
  1547. /// <summary>
  1548. /// Parses the time seek header.
  1549. /// </summary>
  1550. private long? ParseTimeSeekHeader(string value)
  1551. {
  1552. if (string.IsNullOrWhiteSpace(value))
  1553. {
  1554. return null;
  1555. }
  1556. if (value.IndexOf("npt=", StringComparison.OrdinalIgnoreCase) != 0)
  1557. {
  1558. throw new ArgumentException("Invalid timeseek header");
  1559. }
  1560. value = value.Substring(4).Split(new[] { '-' }, 2)[0];
  1561. if (value.IndexOf(':') == -1)
  1562. {
  1563. // Parses npt times in the format of '417.33'
  1564. double seconds;
  1565. if (double.TryParse(value, NumberStyles.Any, UsCulture, out seconds))
  1566. {
  1567. return TimeSpan.FromSeconds(seconds).Ticks;
  1568. }
  1569. throw new ArgumentException("Invalid timeseek header");
  1570. }
  1571. // Parses npt times in the format of '10:19:25.7'
  1572. var tokens = value.Split(new[] { ':' }, 3);
  1573. double secondsSum = 0;
  1574. var timeFactor = 3600;
  1575. foreach (var time in tokens)
  1576. {
  1577. double digit;
  1578. if (double.TryParse(time, NumberStyles.Any, UsCulture, out digit))
  1579. {
  1580. secondsSum += digit * timeFactor;
  1581. }
  1582. else
  1583. {
  1584. throw new ArgumentException("Invalid timeseek header");
  1585. }
  1586. timeFactor /= 60;
  1587. }
  1588. return TimeSpan.FromSeconds(secondsSum).Ticks;
  1589. }
  1590. /// <summary>
  1591. /// Gets the state.
  1592. /// </summary>
  1593. /// <param name="request">The request.</param>
  1594. /// <param name="cancellationToken">The cancellation token.</param>
  1595. /// <returns>StreamState.</returns>
  1596. protected async Task<StreamState> GetState(StreamRequest request, CancellationToken cancellationToken)
  1597. {
  1598. ParseDlnaHeaders(request);
  1599. if (!string.IsNullOrWhiteSpace(request.Params))
  1600. {
  1601. ParseParams(request);
  1602. }
  1603. var url = Request.PathInfo;
  1604. if (string.IsNullOrEmpty(request.AudioCodec))
  1605. {
  1606. request.AudioCodec = InferAudioCodec(url);
  1607. }
  1608. var state = new StreamState(MediaSourceManager, Logger)
  1609. {
  1610. Request = request,
  1611. RequestedUrl = url,
  1612. UserAgent = Request.UserAgent
  1613. };
  1614. //if ((Request.UserAgent ?? string.Empty).IndexOf("iphone", StringComparison.OrdinalIgnoreCase) != -1 ||
  1615. // (Request.UserAgent ?? string.Empty).IndexOf("ipad", StringComparison.OrdinalIgnoreCase) != -1 ||
  1616. // (Request.UserAgent ?? string.Empty).IndexOf("ipod", StringComparison.OrdinalIgnoreCase) != -1)
  1617. //{
  1618. // state.SegmentLength = 6;
  1619. //}
  1620. if (state.VideoRequest != null)
  1621. {
  1622. if (!string.IsNullOrWhiteSpace(state.VideoRequest.VideoCodec))
  1623. {
  1624. state.SupportedVideoCodecs = state.VideoRequest.VideoCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
  1625. state.VideoRequest.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault();
  1626. }
  1627. }
  1628. if (!string.IsNullOrWhiteSpace(request.AudioCodec))
  1629. {
  1630. state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
  1631. state.Request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault(i => MediaEncoder.CanEncodeToAudioCodec(i))
  1632. ?? state.SupportedAudioCodecs.FirstOrDefault();
  1633. }
  1634. var item = LibraryManager.GetItemById(request.Id);
  1635. state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  1636. MediaSourceInfo mediaSource = null;
  1637. if (string.IsNullOrWhiteSpace(request.LiveStreamId))
  1638. {
  1639. TranscodingJob currentJob = !string.IsNullOrWhiteSpace(request.PlaySessionId) ?
  1640. ApiEntryPoint.Instance.GetTranscodingJob(request.PlaySessionId)
  1641. : null;
  1642. if (currentJob != null)
  1643. {
  1644. mediaSource = currentJob.MediaSource;
  1645. }
  1646. if (mediaSource == null)
  1647. {
  1648. var mediaSources = (await MediaSourceManager.GetPlayackMediaSources(request.Id, null, false, new[] { MediaType.Audio, MediaType.Video }, cancellationToken).ConfigureAwait(false)).ToList();
  1649. mediaSource = string.IsNullOrEmpty(request.MediaSourceId)
  1650. ? mediaSources.First()
  1651. : mediaSources.FirstOrDefault(i => string.Equals(i.Id, request.MediaSourceId));
  1652. if (mediaSource == null && string.Equals(request.Id, request.MediaSourceId, StringComparison.OrdinalIgnoreCase))
  1653. {
  1654. mediaSource = mediaSources.First();
  1655. }
  1656. }
  1657. }
  1658. else
  1659. {
  1660. var liveStreamInfo = await MediaSourceManager.GetLiveStreamWithDirectStreamProvider(request.LiveStreamId, cancellationToken).ConfigureAwait(false);
  1661. mediaSource = liveStreamInfo.Item1;
  1662. state.DirectStreamProvider = liveStreamInfo.Item2;
  1663. }
  1664. var videoRequest = request as VideoStreamRequest;
  1665. AttachMediaSourceInfo(state, mediaSource, videoRequest, url);
  1666. var container = Path.GetExtension(state.RequestedUrl);
  1667. if (string.IsNullOrEmpty(container))
  1668. {
  1669. container = request.Static ?
  1670. state.InputContainer :
  1671. (Path.GetExtension(GetOutputFilePath(state)) ?? string.Empty).TrimStart('.');
  1672. }
  1673. state.OutputContainer = (container ?? string.Empty).TrimStart('.');
  1674. state.OutputAudioBitrate = GetAudioBitrateParam(state.Request, state.AudioStream);
  1675. state.OutputAudioSampleRate = request.AudioSampleRate;
  1676. state.OutputAudioCodec = state.Request.AudioCodec;
  1677. state.OutputAudioChannels = GetNumAudioChannelsParam(state.Request, state.AudioStream, state.OutputAudioCodec);
  1678. if (videoRequest != null)
  1679. {
  1680. state.OutputVideoCodec = state.VideoRequest.VideoCodec;
  1681. state.OutputVideoBitrate = GetVideoBitrateParamValue(state.VideoRequest, state.VideoStream, state.OutputVideoCodec);
  1682. if (videoRequest != null)
  1683. {
  1684. TryStreamCopy(state, videoRequest);
  1685. }
  1686. if (state.OutputVideoBitrate.HasValue && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
  1687. {
  1688. var resolution = ResolutionNormalizer.Normalize(
  1689. state.VideoStream == null ? (int?)null : state.VideoStream.BitRate,
  1690. state.OutputVideoBitrate.Value,
  1691. state.VideoStream == null ? null : state.VideoStream.Codec,
  1692. state.OutputVideoCodec,
  1693. videoRequest.MaxWidth,
  1694. videoRequest.MaxHeight);
  1695. videoRequest.MaxWidth = resolution.MaxWidth;
  1696. videoRequest.MaxHeight = resolution.MaxHeight;
  1697. }
  1698. ApplyDeviceProfileSettings(state);
  1699. }
  1700. else
  1701. {
  1702. ApplyDeviceProfileSettings(state);
  1703. }
  1704. state.OutputFilePath = GetOutputFilePath(state);
  1705. return state;
  1706. }
  1707. private void TryStreamCopy(StreamState state, VideoStreamRequest videoRequest)
  1708. {
  1709. if (state.VideoStream != null && CanStreamCopyVideo(state))
  1710. {
  1711. state.OutputVideoCodec = "copy";
  1712. }
  1713. else
  1714. {
  1715. // If the user doesn't have access to transcoding, then force stream copy, regardless of whether it will be compatible or not
  1716. var auth = AuthorizationContext.GetAuthorizationInfo(Request);
  1717. if (!string.IsNullOrWhiteSpace(auth.UserId))
  1718. {
  1719. var user = UserManager.GetUserById(auth.UserId);
  1720. if (!user.Policy.EnableVideoPlaybackTranscoding)
  1721. {
  1722. state.OutputVideoCodec = "copy";
  1723. }
  1724. }
  1725. }
  1726. if (state.AudioStream != null && CanStreamCopyAudio(state, state.SupportedAudioCodecs))
  1727. {
  1728. state.OutputAudioCodec = "copy";
  1729. }
  1730. else
  1731. {
  1732. // If the user doesn't have access to transcoding, then force stream copy, regardless of whether it will be compatible or not
  1733. var auth = AuthorizationContext.GetAuthorizationInfo(Request);
  1734. if (!string.IsNullOrWhiteSpace(auth.UserId))
  1735. {
  1736. var user = UserManager.GetUserById(auth.UserId);
  1737. if (!user.Policy.EnableAudioPlaybackTranscoding)
  1738. {
  1739. state.OutputAudioCodec = "copy";
  1740. }
  1741. }
  1742. }
  1743. }
  1744. private void AttachMediaSourceInfo(StreamState state,
  1745. MediaSourceInfo mediaSource,
  1746. VideoStreamRequest videoRequest,
  1747. string requestedUrl)
  1748. {
  1749. state.MediaPath = mediaSource.Path;
  1750. state.InputProtocol = mediaSource.Protocol;
  1751. state.InputContainer = mediaSource.Container;
  1752. state.InputFileSize = mediaSource.Size;
  1753. state.InputBitrate = mediaSource.Bitrate;
  1754. state.RunTimeTicks = mediaSource.RunTimeTicks;
  1755. state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders;
  1756. if (mediaSource.VideoType.HasValue)
  1757. {
  1758. state.VideoType = mediaSource.VideoType.Value;
  1759. }
  1760. state.IsoType = mediaSource.IsoType;
  1761. state.PlayableStreamFileNames = mediaSource.PlayableStreamFileNames.ToList();
  1762. if (mediaSource.Timestamp.HasValue)
  1763. {
  1764. state.InputTimestamp = mediaSource.Timestamp.Value;
  1765. }
  1766. state.InputProtocol = mediaSource.Protocol;
  1767. state.MediaPath = mediaSource.Path;
  1768. state.RunTimeTicks = mediaSource.RunTimeTicks;
  1769. state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders;
  1770. state.InputBitrate = mediaSource.Bitrate;
  1771. state.InputFileSize = mediaSource.Size;
  1772. state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate;
  1773. if (state.ReadInputAtNativeFramerate ||
  1774. mediaSource.Protocol == MediaProtocol.File && string.Equals(mediaSource.Container, "wtv", StringComparison.OrdinalIgnoreCase))
  1775. {
  1776. state.OutputAudioSync = "1000";
  1777. state.InputVideoSync = "-1";
  1778. state.InputAudioSync = "1";
  1779. }
  1780. if (string.Equals(mediaSource.Container, "wma", StringComparison.OrdinalIgnoreCase))
  1781. {
  1782. // Seeing some stuttering when transcoding wma to audio-only HLS
  1783. state.InputAudioSync = "1";
  1784. }
  1785. var mediaStreams = mediaSource.MediaStreams;
  1786. if (videoRequest != null)
  1787. {
  1788. if (string.IsNullOrEmpty(videoRequest.VideoCodec))
  1789. {
  1790. videoRequest.VideoCodec = InferVideoCodec(requestedUrl);
  1791. }
  1792. state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video);
  1793. state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Subtitle, false);
  1794. state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio);
  1795. if (state.SubtitleStream != null && !state.SubtitleStream.IsExternal)
  1796. {
  1797. state.InternalSubtitleStreamOffset = mediaStreams.Where(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal).ToList().IndexOf(state.SubtitleStream);
  1798. }
  1799. if (state.VideoStream != null && state.VideoStream.IsInterlaced)
  1800. {
  1801. state.DeInterlace = true;
  1802. }
  1803. EnforceResolutionLimit(state, videoRequest);
  1804. }
  1805. else
  1806. {
  1807. state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true);
  1808. }
  1809. state.MediaSource = mediaSource;
  1810. }
  1811. protected bool CanStreamCopyVideo(StreamState state)
  1812. {
  1813. var request = state.VideoRequest;
  1814. var videoStream = state.VideoStream;
  1815. if (videoStream.IsInterlaced)
  1816. {
  1817. return false;
  1818. }
  1819. if (videoStream.IsAnamorphic ?? false)
  1820. {
  1821. return false;
  1822. }
  1823. // Can't stream copy if we're burning in subtitles
  1824. if (request.SubtitleStreamIndex.HasValue)
  1825. {
  1826. if (request.SubtitleMethod == SubtitleDeliveryMethod.Encode)
  1827. {
  1828. return false;
  1829. }
  1830. }
  1831. if (string.Equals("h264", videoStream.Codec, StringComparison.OrdinalIgnoreCase))
  1832. {
  1833. if (videoStream.IsAVC.HasValue && !videoStream.IsAVC.Value && request.RequireAvc)
  1834. {
  1835. Logger.Debug("Cannot stream copy video. Stream is marked as not AVC");
  1836. return false;
  1837. }
  1838. }
  1839. // Source and target codecs must match
  1840. if (string.IsNullOrEmpty(videoStream.Codec) || !state.SupportedVideoCodecs.Contains(videoStream.Codec, StringComparer.OrdinalIgnoreCase))
  1841. {
  1842. return false;
  1843. }
  1844. // If client is requesting a specific video profile, it must match the source
  1845. if (!string.IsNullOrEmpty(request.Profile))
  1846. {
  1847. if (string.IsNullOrEmpty(videoStream.Profile))
  1848. {
  1849. //return false;
  1850. }
  1851. if (!string.IsNullOrEmpty(videoStream.Profile) && !string.Equals(request.Profile, videoStream.Profile, StringComparison.OrdinalIgnoreCase))
  1852. {
  1853. var currentScore = GetVideoProfileScore(videoStream.Profile);
  1854. var requestedScore = GetVideoProfileScore(request.Profile);
  1855. if (currentScore == -1 || currentScore > requestedScore)
  1856. {
  1857. return false;
  1858. }
  1859. }
  1860. }
  1861. // Video width must fall within requested value
  1862. if (request.MaxWidth.HasValue)
  1863. {
  1864. if (!videoStream.Width.HasValue || videoStream.Width.Value > request.MaxWidth.Value)
  1865. {
  1866. return false;
  1867. }
  1868. }
  1869. // Video height must fall within requested value
  1870. if (request.MaxHeight.HasValue)
  1871. {
  1872. if (!videoStream.Height.HasValue || videoStream.Height.Value > request.MaxHeight.Value)
  1873. {
  1874. return false;
  1875. }
  1876. }
  1877. // Video framerate must fall within requested value
  1878. var requestedFramerate = request.MaxFramerate ?? request.Framerate;
  1879. if (requestedFramerate.HasValue)
  1880. {
  1881. var videoFrameRate = videoStream.AverageFrameRate ?? videoStream.RealFrameRate;
  1882. if (!videoFrameRate.HasValue || videoFrameRate.Value > requestedFramerate.Value)
  1883. {
  1884. return false;
  1885. }
  1886. }
  1887. // Video bitrate must fall within requested value
  1888. if (request.VideoBitRate.HasValue)
  1889. {
  1890. if (!videoStream.BitRate.HasValue || videoStream.BitRate.Value > request.VideoBitRate.Value)
  1891. {
  1892. return false;
  1893. }
  1894. }
  1895. if (request.MaxVideoBitDepth.HasValue)
  1896. {
  1897. if (videoStream.BitDepth.HasValue && videoStream.BitDepth.Value > request.MaxVideoBitDepth.Value)
  1898. {
  1899. return false;
  1900. }
  1901. }
  1902. if (request.MaxRefFrames.HasValue)
  1903. {
  1904. if (videoStream.RefFrames.HasValue && videoStream.RefFrames.Value > request.MaxRefFrames.Value)
  1905. {
  1906. return false;
  1907. }
  1908. }
  1909. // If a specific level was requested, the source must match or be less than
  1910. if (!string.IsNullOrEmpty(request.Level))
  1911. {
  1912. double requestLevel;
  1913. if (double.TryParse(request.Level, NumberStyles.Any, UsCulture, out requestLevel))
  1914. {
  1915. if (!videoStream.Level.HasValue)
  1916. {
  1917. //return false;
  1918. }
  1919. if (videoStream.Level.HasValue && videoStream.Level.Value > requestLevel)
  1920. {
  1921. return false;
  1922. }
  1923. }
  1924. }
  1925. return request.EnableAutoStreamCopy;
  1926. }
  1927. private int GetVideoProfileScore(string profile)
  1928. {
  1929. var list = new List<string>
  1930. {
  1931. "Constrained Baseline",
  1932. "Baseline",
  1933. "Extended",
  1934. "Main",
  1935. "High",
  1936. "Progressive High",
  1937. "Constrained High"
  1938. };
  1939. return Array.FindIndex(list.ToArray(), t => string.Equals(t, profile, StringComparison.OrdinalIgnoreCase));
  1940. }
  1941. protected virtual bool CanStreamCopyAudio(StreamState state, List<string> supportedAudioCodecs)
  1942. {
  1943. var request = state.VideoRequest;
  1944. var audioStream = state.AudioStream;
  1945. // Source and target codecs must match
  1946. if (string.IsNullOrEmpty(audioStream.Codec) || !supportedAudioCodecs.Contains(audioStream.Codec, StringComparer.OrdinalIgnoreCase))
  1947. {
  1948. return false;
  1949. }
  1950. // Video bitrate must fall within requested value
  1951. if (request.AudioBitRate.HasValue)
  1952. {
  1953. if (!audioStream.BitRate.HasValue || audioStream.BitRate.Value <= 0)
  1954. {
  1955. return false;
  1956. }
  1957. if (audioStream.BitRate.Value > request.AudioBitRate.Value)
  1958. {
  1959. return false;
  1960. }
  1961. }
  1962. // Channels must fall within requested value
  1963. var channels = request.AudioChannels ?? request.MaxAudioChannels;
  1964. if (channels.HasValue)
  1965. {
  1966. if (!audioStream.Channels.HasValue || audioStream.Channels.Value <= 0)
  1967. {
  1968. return false;
  1969. }
  1970. if (audioStream.Channels.Value > channels.Value)
  1971. {
  1972. return false;
  1973. }
  1974. }
  1975. // Sample rate must fall within requested value
  1976. if (request.AudioSampleRate.HasValue)
  1977. {
  1978. if (!audioStream.SampleRate.HasValue || audioStream.SampleRate.Value <= 0)
  1979. {
  1980. return false;
  1981. }
  1982. if (audioStream.SampleRate.Value > request.AudioSampleRate.Value)
  1983. {
  1984. return false;
  1985. }
  1986. }
  1987. return request.EnableAutoStreamCopy;
  1988. }
  1989. private void ApplyDeviceProfileSettings(StreamState state)
  1990. {
  1991. var headers = Request.Headers.ToDictionary();
  1992. if (!string.IsNullOrWhiteSpace(state.Request.DeviceProfileId))
  1993. {
  1994. state.DeviceProfile = DlnaManager.GetProfile(state.Request.DeviceProfileId);
  1995. }
  1996. else
  1997. {
  1998. if (!string.IsNullOrWhiteSpace(state.Request.DeviceId))
  1999. {
  2000. var caps = DeviceManager.GetCapabilities(state.Request.DeviceId);
  2001. if (caps != null)
  2002. {
  2003. state.DeviceProfile = caps.DeviceProfile;
  2004. }
  2005. else
  2006. {
  2007. state.DeviceProfile = DlnaManager.GetProfile(headers);
  2008. }
  2009. }
  2010. }
  2011. var profile = state.DeviceProfile;
  2012. if (profile == null)
  2013. {
  2014. // Don't use settings from the default profile.
  2015. // Only use a specific profile if it was requested.
  2016. return;
  2017. }
  2018. var audioCodec = state.ActualOutputAudioCodec;
  2019. var videoCodec = state.ActualOutputVideoCodec;
  2020. var mediaProfile = state.VideoRequest == null ?
  2021. profile.GetAudioMediaProfile(state.OutputContainer, audioCodec, state.OutputAudioChannels, state.OutputAudioBitrate) :
  2022. profile.GetVideoMediaProfile(state.OutputContainer,
  2023. audioCodec,
  2024. videoCodec,
  2025. state.OutputWidth,
  2026. state.OutputHeight,
  2027. state.TargetVideoBitDepth,
  2028. state.OutputVideoBitrate,
  2029. state.TargetVideoProfile,
  2030. state.TargetVideoLevel,
  2031. state.TargetFramerate,
  2032. state.TargetPacketLength,
  2033. state.TargetTimestamp,
  2034. state.IsTargetAnamorphic,
  2035. state.TargetRefFrames,
  2036. state.TargetVideoStreamCount,
  2037. state.TargetAudioStreamCount,
  2038. state.TargetVideoCodecTag,
  2039. state.IsTargetAVC);
  2040. if (mediaProfile != null)
  2041. {
  2042. state.MimeType = mediaProfile.MimeType;
  2043. }
  2044. if (!state.Request.Static)
  2045. {
  2046. var transcodingProfile = state.VideoRequest == null ?
  2047. profile.GetAudioTranscodingProfile(state.OutputContainer, audioCodec) :
  2048. profile.GetVideoTranscodingProfile(state.OutputContainer, audioCodec, videoCodec);
  2049. if (transcodingProfile != null)
  2050. {
  2051. state.EstimateContentLength = transcodingProfile.EstimateContentLength;
  2052. state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
  2053. state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
  2054. if (state.VideoRequest != null)
  2055. {
  2056. state.VideoRequest.CopyTimestamps = transcodingProfile.CopyTimestamps;
  2057. state.VideoRequest.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
  2058. }
  2059. }
  2060. }
  2061. }
  2062. private async void ReportUsage(StreamState state)
  2063. {
  2064. try
  2065. {
  2066. await ReportUsageInternal(state).ConfigureAwait(false);
  2067. }
  2068. catch
  2069. {
  2070. }
  2071. }
  2072. private Task ReportUsageInternal(StreamState state)
  2073. {
  2074. if (!ServerConfigurationManager.Configuration.EnableAnonymousUsageReporting)
  2075. {
  2076. return Task.FromResult(true);
  2077. }
  2078. if (!MediaEncoder.IsDefaultEncoderPath)
  2079. {
  2080. return Task.FromResult(true);
  2081. }
  2082. return Task.FromResult(true);
  2083. //var dict = new Dictionary<string, string>();
  2084. //var outputAudio = GetAudioEncoder(state);
  2085. //if (!string.IsNullOrWhiteSpace(outputAudio))
  2086. //{
  2087. // dict["outputAudio"] = outputAudio;
  2088. //}
  2089. //var outputVideo = GetVideoEncoder(state);
  2090. //if (!string.IsNullOrWhiteSpace(outputVideo))
  2091. //{
  2092. // dict["outputVideo"] = outputVideo;
  2093. //}
  2094. //if (ServerConfigurationManager.Configuration.CodecsUsed.Contains(outputAudio ?? string.Empty, StringComparer.OrdinalIgnoreCase) &&
  2095. // ServerConfigurationManager.Configuration.CodecsUsed.Contains(outputVideo ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  2096. //{
  2097. // return Task.FromResult(true);
  2098. //}
  2099. //dict["id"] = AppHost.SystemId;
  2100. //dict["type"] = state.VideoRequest == null ? "Audio" : "Video";
  2101. //var audioStream = state.AudioStream;
  2102. //if (audioStream != null && !string.IsNullOrWhiteSpace(audioStream.Codec))
  2103. //{
  2104. // dict["inputAudio"] = audioStream.Codec;
  2105. //}
  2106. //var videoStream = state.VideoStream;
  2107. //if (videoStream != null && !string.IsNullOrWhiteSpace(videoStream.Codec))
  2108. //{
  2109. // dict["inputVideo"] = videoStream.Codec;
  2110. //}
  2111. //var cert = GetType().Assembly.GetModules().First().GetSignerCertificate();
  2112. //if (cert != null)
  2113. //{
  2114. // dict["assemblySig"] = cert.GetCertHashString();
  2115. // dict["certSubject"] = cert.Subject ?? string.Empty;
  2116. // dict["certIssuer"] = cert.Issuer ?? string.Empty;
  2117. //}
  2118. //else
  2119. //{
  2120. // return Task.FromResult(true);
  2121. //}
  2122. //if (state.SupportedAudioCodecs.Count > 0)
  2123. //{
  2124. // dict["supportedAudioCodecs"] = string.Join(",", state.SupportedAudioCodecs.ToArray());
  2125. //}
  2126. //var auth = AuthorizationContext.GetAuthorizationInfo(Request);
  2127. //dict["appName"] = auth.Client ?? string.Empty;
  2128. //dict["appVersion"] = auth.Version ?? string.Empty;
  2129. //dict["device"] = auth.Device ?? string.Empty;
  2130. //dict["deviceId"] = auth.DeviceId ?? string.Empty;
  2131. //dict["context"] = "streaming";
  2132. ////Logger.Info(JsonSerializer.SerializeToString(dict));
  2133. //if (!ServerConfigurationManager.Configuration.CodecsUsed.Contains(outputAudio ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  2134. //{
  2135. // var list = ServerConfigurationManager.Configuration.CodecsUsed.ToList();
  2136. // list.Add(outputAudio);
  2137. // ServerConfigurationManager.Configuration.CodecsUsed = list.ToArray();
  2138. //}
  2139. //if (!ServerConfigurationManager.Configuration.CodecsUsed.Contains(outputVideo ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  2140. //{
  2141. // var list = ServerConfigurationManager.Configuration.CodecsUsed.ToList();
  2142. // list.Add(outputVideo);
  2143. // ServerConfigurationManager.Configuration.CodecsUsed = list.ToArray();
  2144. //}
  2145. //ServerConfigurationManager.SaveConfiguration();
  2146. ////Logger.Info(JsonSerializer.SerializeToString(dict));
  2147. //var options = new HttpRequestOptions()
  2148. //{
  2149. // Url = "https://mb3admin.com/admin/service/transcoding/report",
  2150. // CancellationToken = CancellationToken.None,
  2151. // LogRequest = false,
  2152. // LogErrors = false,
  2153. // BufferContent = false
  2154. //};
  2155. //options.RequestContent = JsonSerializer.SerializeToString(dict);
  2156. //options.RequestContentType = "application/json";
  2157. //return HttpClient.Post(options);
  2158. }
  2159. /// <summary>
  2160. /// Adds the dlna headers.
  2161. /// </summary>
  2162. /// <param name="state">The state.</param>
  2163. /// <param name="responseHeaders">The response headers.</param>
  2164. /// <param name="isStaticallyStreamed">if set to <c>true</c> [is statically streamed].</param>
  2165. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  2166. protected void AddDlnaHeaders(StreamState state, IDictionary<string, string> responseHeaders, bool isStaticallyStreamed)
  2167. {
  2168. var profile = state.DeviceProfile;
  2169. var transferMode = GetHeader("transferMode.dlna.org");
  2170. responseHeaders["transferMode.dlna.org"] = string.IsNullOrEmpty(transferMode) ? "Streaming" : transferMode;
  2171. responseHeaders["realTimeInfo.dlna.org"] = "DLNA.ORG_TLAG=*";
  2172. if (string.Equals(GetHeader("getMediaInfo.sec"), "1", StringComparison.OrdinalIgnoreCase))
  2173. {
  2174. if (state.RunTimeTicks.HasValue)
  2175. {
  2176. var ms = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalMilliseconds;
  2177. responseHeaders["MediaInfo.sec"] = string.Format("SEC_Duration={0};", Convert.ToInt32(ms).ToString(CultureInfo.InvariantCulture));
  2178. }
  2179. }
  2180. if (state.RunTimeTicks.HasValue && !isStaticallyStreamed && profile != null)
  2181. {
  2182. AddTimeSeekResponseHeaders(state, responseHeaders);
  2183. }
  2184. if (profile == null)
  2185. {
  2186. profile = DlnaManager.GetDefaultProfile();
  2187. }
  2188. var audioCodec = state.ActualOutputAudioCodec;
  2189. if (state.VideoRequest == null)
  2190. {
  2191. responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile)
  2192. .BuildAudioHeader(
  2193. state.OutputContainer,
  2194. audioCodec,
  2195. state.OutputAudioBitrate,
  2196. state.OutputAudioSampleRate,
  2197. state.OutputAudioChannels,
  2198. isStaticallyStreamed,
  2199. state.RunTimeTicks,
  2200. state.TranscodeSeekInfo
  2201. );
  2202. }
  2203. else
  2204. {
  2205. var videoCodec = state.ActualOutputVideoCodec;
  2206. responseHeaders["contentFeatures.dlna.org"] = new ContentFeatureBuilder(profile)
  2207. .BuildVideoHeader(
  2208. state.OutputContainer,
  2209. videoCodec,
  2210. audioCodec,
  2211. state.OutputWidth,
  2212. state.OutputHeight,
  2213. state.TargetVideoBitDepth,
  2214. state.OutputVideoBitrate,
  2215. state.TargetTimestamp,
  2216. isStaticallyStreamed,
  2217. state.RunTimeTicks,
  2218. state.TargetVideoProfile,
  2219. state.TargetVideoLevel,
  2220. state.TargetFramerate,
  2221. state.TargetPacketLength,
  2222. state.TranscodeSeekInfo,
  2223. state.IsTargetAnamorphic,
  2224. state.TargetRefFrames,
  2225. state.TargetVideoStreamCount,
  2226. state.TargetAudioStreamCount,
  2227. state.TargetVideoCodecTag,
  2228. state.IsTargetAVC
  2229. ).FirstOrDefault() ?? string.Empty;
  2230. }
  2231. foreach (var item in responseHeaders)
  2232. {
  2233. Request.Response.AddHeader(item.Key, item.Value);
  2234. }
  2235. }
  2236. private void AddTimeSeekResponseHeaders(StreamState state, IDictionary<string, string> responseHeaders)
  2237. {
  2238. var runtimeSeconds = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds.ToString(UsCulture);
  2239. var startSeconds = TimeSpan.FromTicks(state.Request.StartTimeTicks ?? 0).TotalSeconds.ToString(UsCulture);
  2240. responseHeaders["TimeSeekRange.dlna.org"] = string.Format("npt={0}-{1}/{1}", startSeconds, runtimeSeconds);
  2241. responseHeaders["X-AvailableSeekRange"] = string.Format("1 npt={0}-{1}", startSeconds, runtimeSeconds);
  2242. }
  2243. /// <summary>
  2244. /// Enforces the resolution limit.
  2245. /// </summary>
  2246. /// <param name="state">The state.</param>
  2247. /// <param name="videoRequest">The video request.</param>
  2248. private void EnforceResolutionLimit(StreamState state, VideoStreamRequest videoRequest)
  2249. {
  2250. // Switch the incoming params to be ceilings rather than fixed values
  2251. videoRequest.MaxWidth = videoRequest.MaxWidth ?? videoRequest.Width;
  2252. videoRequest.MaxHeight = videoRequest.MaxHeight ?? videoRequest.Height;
  2253. videoRequest.Width = null;
  2254. videoRequest.Height = null;
  2255. }
  2256. protected string GetInputModifier(StreamState state, bool genPts = true)
  2257. {
  2258. var inputModifier = string.Empty;
  2259. var probeSize = GetProbeSizeArgument(state);
  2260. inputModifier += " " + probeSize;
  2261. inputModifier = inputModifier.Trim();
  2262. var userAgentParam = GetUserAgentParam(state);
  2263. if (!string.IsNullOrWhiteSpace(userAgentParam))
  2264. {
  2265. inputModifier += " " + userAgentParam;
  2266. }
  2267. inputModifier = inputModifier.Trim();
  2268. inputModifier += " " + GetFastSeekCommandLineParameter(state.Request);
  2269. inputModifier = inputModifier.Trim();
  2270. //inputModifier += " -fflags +genpts+ignidx+igndts";
  2271. if (state.VideoRequest != null && genPts)
  2272. {
  2273. inputModifier += " -fflags +genpts";
  2274. }
  2275. if (!string.IsNullOrEmpty(state.InputAudioSync))
  2276. {
  2277. inputModifier += " -async " + state.InputAudioSync;
  2278. }
  2279. if (!string.IsNullOrEmpty(state.InputVideoSync))
  2280. {
  2281. inputModifier += " -vsync " + state.InputVideoSync;
  2282. }
  2283. if (state.ReadInputAtNativeFramerate)
  2284. {
  2285. inputModifier += " -re";
  2286. }
  2287. var videoDecoder = GetVideoDecoder(state);
  2288. if (!string.IsNullOrWhiteSpace(videoDecoder))
  2289. {
  2290. inputModifier += " " + videoDecoder;
  2291. }
  2292. if (state.VideoRequest != null)
  2293. {
  2294. // Important: If this is ever re-enabled, make sure not to use it with wtv because it breaks seeking
  2295. if (string.Equals(state.OutputContainer, "mkv", StringComparison.OrdinalIgnoreCase) && state.VideoRequest.CopyTimestamps)
  2296. {
  2297. //inputModifier += " -noaccurate_seek";
  2298. }
  2299. foreach (var stream in state.MediaSource.MediaStreams)
  2300. {
  2301. if (!stream.IsExternal && stream.Type != MediaStreamType.Subtitle)
  2302. {
  2303. if (!string.IsNullOrWhiteSpace(stream.Codec) && stream.Index != -1)
  2304. {
  2305. inputModifier += " -codec:" + stream.Index.ToString(UsCulture) + " " + stream.Codec;
  2306. }
  2307. }
  2308. }
  2309. //var videoStream = state.VideoStream;
  2310. //if (videoStream != null && !string.IsNullOrWhiteSpace(videoStream.Codec))
  2311. //{
  2312. // inputModifier += " -codec:0 " + videoStream.Codec;
  2313. // var audioStream = state.AudioStream;
  2314. // if (audioStream != null && !string.IsNullOrWhiteSpace(audioStream.Codec))
  2315. // {
  2316. // inputModifier += " -codec:1 " + audioStream.Codec;
  2317. // }
  2318. //}
  2319. }
  2320. return inputModifier;
  2321. }
  2322. /// <summary>
  2323. /// Infers the audio codec based on the url
  2324. /// </summary>
  2325. /// <param name="url">The URL.</param>
  2326. /// <returns>System.Nullable{AudioCodecs}.</returns>
  2327. private string InferAudioCodec(string url)
  2328. {
  2329. var ext = Path.GetExtension(url);
  2330. if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
  2331. {
  2332. return "mp3";
  2333. }
  2334. if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
  2335. {
  2336. return "aac";
  2337. }
  2338. if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
  2339. {
  2340. return "wma";
  2341. }
  2342. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
  2343. {
  2344. return "vorbis";
  2345. }
  2346. if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase))
  2347. {
  2348. return "vorbis";
  2349. }
  2350. if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  2351. {
  2352. return "vorbis";
  2353. }
  2354. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  2355. {
  2356. return "vorbis";
  2357. }
  2358. if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
  2359. {
  2360. return "vorbis";
  2361. }
  2362. return "copy";
  2363. }
  2364. /// <summary>
  2365. /// Infers the video codec.
  2366. /// </summary>
  2367. /// <param name="url">The URL.</param>
  2368. /// <returns>System.Nullable{VideoCodecs}.</returns>
  2369. private string InferVideoCodec(string url)
  2370. {
  2371. var ext = Path.GetExtension(url);
  2372. if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
  2373. {
  2374. return "wmv";
  2375. }
  2376. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  2377. {
  2378. return "vpx";
  2379. }
  2380. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
  2381. {
  2382. return "theora";
  2383. }
  2384. if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
  2385. {
  2386. return "h264";
  2387. }
  2388. return "copy";
  2389. }
  2390. }
  2391. }