BaseStreamingService.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.Implementations.HttpServer;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.Drawing;
  8. using MediaBrowser.Model.Dto;
  9. using MediaBrowser.Model.Entities;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Api.Playback
  19. {
  20. /// <summary>
  21. /// Class BaseStreamingService
  22. /// </summary>
  23. public abstract class BaseStreamingService : BaseRestService
  24. {
  25. /// <summary>
  26. /// Gets or sets the application paths.
  27. /// </summary>
  28. /// <value>The application paths.</value>
  29. protected IServerApplicationPaths ApplicationPaths { get; set; }
  30. /// <summary>
  31. /// Gets or sets the user manager.
  32. /// </summary>
  33. /// <value>The user manager.</value>
  34. protected IUserManager UserManager { get; set; }
  35. /// <summary>
  36. /// Gets the server kernel.
  37. /// </summary>
  38. /// <value>The server kernel.</value>
  39. protected Kernel ServerKernel
  40. {
  41. get { return Kernel as Kernel; }
  42. }
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="BaseStreamingService" /> class.
  45. /// </summary>
  46. /// <param name="appPaths">The app paths.</param>
  47. /// <param name="userManager">The user manager.</param>
  48. protected BaseStreamingService(IServerApplicationPaths appPaths, IUserManager userManager)
  49. {
  50. ApplicationPaths = appPaths;
  51. UserManager = userManager;
  52. }
  53. /// <summary>
  54. /// Gets the command line arguments.
  55. /// </summary>
  56. /// <param name="outputPath">The output path.</param>
  57. /// <param name="state">The state.</param>
  58. /// <returns>System.String.</returns>
  59. protected abstract string GetCommandLineArguments(string outputPath, StreamState state);
  60. /// <summary>
  61. /// Gets the type of the transcoding job.
  62. /// </summary>
  63. /// <value>The type of the transcoding job.</value>
  64. protected abstract TranscodingJobType TranscodingJobType { get; }
  65. /// <summary>
  66. /// Gets the output file extension.
  67. /// </summary>
  68. /// <param name="state">The state.</param>
  69. /// <returns>System.String.</returns>
  70. protected virtual string GetOutputFileExtension(StreamState state)
  71. {
  72. return Path.GetExtension(state.Url);
  73. }
  74. /// <summary>
  75. /// Gets the output file path.
  76. /// </summary>
  77. /// <param name="state">The state.</param>
  78. /// <returns>System.String.</returns>
  79. protected string GetOutputFilePath(StreamState state)
  80. {
  81. var folder = ApplicationPaths.FFMpegStreamCachePath;
  82. return Path.Combine(folder, GetCommandLineArguments("dummy\\dummy", state).GetMD5() + GetOutputFileExtension(state).ToLower());
  83. }
  84. /// <summary>
  85. /// The fast seek offset seconds
  86. /// </summary>
  87. private const int FastSeekOffsetSeconds = 1;
  88. /// <summary>
  89. /// Gets the fast seek command line parameter.
  90. /// </summary>
  91. /// <param name="request">The request.</param>
  92. /// <returns>System.String.</returns>
  93. /// <value>The fast seek command line parameter.</value>
  94. protected string GetFastSeekCommandLineParameter(StreamRequest request)
  95. {
  96. var time = request.StartTimeTicks;
  97. if (time.HasValue)
  98. {
  99. var seconds = TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds;
  100. if (seconds > 0)
  101. {
  102. return string.Format("-ss {0}", seconds);
  103. }
  104. }
  105. return string.Empty;
  106. }
  107. /// <summary>
  108. /// Gets the slow seek command line parameter.
  109. /// </summary>
  110. /// <param name="request">The request.</param>
  111. /// <returns>System.String.</returns>
  112. /// <value>The slow seek command line parameter.</value>
  113. protected string GetSlowSeekCommandLineParameter(StreamRequest request)
  114. {
  115. var time = request.StartTimeTicks;
  116. if (time.HasValue)
  117. {
  118. if (TimeSpan.FromTicks(time.Value).TotalSeconds - FastSeekOffsetSeconds > 0)
  119. {
  120. return string.Format(" -ss {0}", FastSeekOffsetSeconds);
  121. }
  122. }
  123. return string.Empty;
  124. }
  125. /// <summary>
  126. /// Gets the map args.
  127. /// </summary>
  128. /// <param name="state">The state.</param>
  129. /// <returns>System.String.</returns>
  130. protected virtual string GetMapArgs(StreamState state)
  131. {
  132. var args = string.Empty;
  133. if (state.VideoStream != null)
  134. {
  135. args += string.Format("-map 0:{0}", state.VideoStream.Index);
  136. }
  137. else
  138. {
  139. args += "-map -0:v";
  140. }
  141. if (state.AudioStream != null)
  142. {
  143. args += string.Format(" -map 0:{0}", state.AudioStream.Index);
  144. }
  145. else
  146. {
  147. args += " -map -0:a";
  148. }
  149. if (state.SubtitleStream == null)
  150. {
  151. args += " -map -0:s";
  152. }
  153. return args;
  154. }
  155. /// <summary>
  156. /// Determines which stream will be used for playback
  157. /// </summary>
  158. /// <param name="allStream">All stream.</param>
  159. /// <param name="desiredIndex">Index of the desired.</param>
  160. /// <param name="type">The type.</param>
  161. /// <param name="returnFirstIfNoIndex">if set to <c>true</c> [return first if no index].</param>
  162. /// <returns>MediaStream.</returns>
  163. private MediaStream GetMediaStream(IEnumerable<MediaStream> allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true)
  164. {
  165. var streams = allStream.Where(s => s.Type == type).ToList();
  166. if (desiredIndex.HasValue)
  167. {
  168. var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value);
  169. if (stream != null)
  170. {
  171. return stream;
  172. }
  173. }
  174. // Just return the first one
  175. return returnFirstIfNoIndex ? streams.FirstOrDefault() : null;
  176. }
  177. /// <summary>
  178. /// If we're going to put a fixed size on the command line, this will calculate it
  179. /// </summary>
  180. /// <param name="state">The state.</param>
  181. /// <param name="outputVideoCodec">The output video codec.</param>
  182. /// <returns>System.String.</returns>
  183. protected string GetOutputSizeParam(StreamState state, string outputVideoCodec)
  184. {
  185. // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
  186. var assSubtitleParam = string.Empty;
  187. var request = state.Request;
  188. if (state.SubtitleStream != null)
  189. {
  190. if (state.SubtitleStream.Codec.IndexOf("srt", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("subrip", StringComparison.OrdinalIgnoreCase) != -1)
  191. {
  192. assSubtitleParam = GetTextSubtitleParam((Video)state.Item, state.SubtitleStream, request.StartTimeTicks);
  193. }
  194. }
  195. // If fixed dimensions were supplied
  196. if (request.Width.HasValue && request.Height.HasValue)
  197. {
  198. return string.Format(" -vf \"scale={0}:{1}{2}\"", request.Width.Value, request.Height.Value, assSubtitleParam);
  199. }
  200. var isH264Output = outputVideoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase);
  201. // If a fixed width was requested
  202. if (request.Width.HasValue)
  203. {
  204. return isH264Output ?
  205. string.Format(" -vf \"scale={0}:trunc(ow/a/2)*2{1}\"", request.Width.Value, assSubtitleParam) :
  206. string.Format(" -vf \"scale={0}:-1{1}\"", request.Width.Value, assSubtitleParam);
  207. }
  208. // If a max width was requested
  209. if (request.MaxWidth.HasValue && !request.MaxHeight.HasValue)
  210. {
  211. return isH264Output ?
  212. string.Format(" -vf \"scale=min(iw\\,{0}):trunc(ow/a/2)*2{1}\"", request.MaxWidth.Value, assSubtitleParam) :
  213. string.Format(" -vf \"scale=min(iw\\,{0}):-1{1}\"", request.MaxWidth.Value, assSubtitleParam);
  214. }
  215. // Need to perform calculations manually
  216. // Try to account for bad media info
  217. var currentHeight = state.VideoStream.Height ?? request.MaxHeight ?? request.Height ?? 0;
  218. var currentWidth = state.VideoStream.Width ?? request.MaxWidth ?? request.Width ?? 0;
  219. var outputSize = DrawingUtils.Resize(currentWidth, currentHeight, request.Width, request.Height, request.MaxWidth, request.MaxHeight);
  220. // If we're encoding with libx264, it can't handle odd numbered widths or heights, so we'll have to fix that
  221. if (isH264Output)
  222. {
  223. return string.Format(" -vf \"scale=trunc({0}/2)*2:trunc({1}/2)*2{2}\"", outputSize.Width, outputSize.Height, assSubtitleParam);
  224. }
  225. // Otherwise use -vf scale since ffmpeg will ensure internally that the aspect ratio is preserved
  226. return string.Format(" -vf \"scale={0}:-1{1}\"", Convert.ToInt32(outputSize.Width), assSubtitleParam);
  227. }
  228. /// <summary>
  229. /// Gets the text subtitle param.
  230. /// </summary>
  231. /// <param name="video">The video.</param>
  232. /// <param name="subtitleStream">The subtitle stream.</param>
  233. /// <param name="startTimeTicks">The start time ticks.</param>
  234. /// <returns>System.String.</returns>
  235. protected string GetTextSubtitleParam(Video video, MediaStream subtitleStream, long? startTimeTicks)
  236. {
  237. var path = subtitleStream.IsExternal ? GetConvertedAssPath(video, subtitleStream) : GetExtractedAssPath(video, subtitleStream);
  238. if (string.IsNullOrEmpty(path))
  239. {
  240. return string.Empty;
  241. }
  242. var param = string.Format(",ass={0}", path);
  243. if (startTimeTicks.HasValue)
  244. {
  245. var seconds = Convert.ToInt32(TimeSpan.FromTicks(startTimeTicks.Value).TotalSeconds);
  246. param += string.Format(",setpts=PTS-{0}/TB", seconds);
  247. }
  248. return param;
  249. }
  250. /// <summary>
  251. /// Gets the extracted ass path.
  252. /// </summary>
  253. /// <param name="video">The video.</param>
  254. /// <param name="subtitleStream">The subtitle stream.</param>
  255. /// <returns>System.String.</returns>
  256. private string GetExtractedAssPath(Video video, MediaStream subtitleStream)
  257. {
  258. var path = ServerKernel.FFMpegManager.GetSubtitleCachePath(video, subtitleStream.Index, ".ass");
  259. if (!File.Exists(path))
  260. {
  261. var success = ServerKernel.FFMpegManager.ExtractTextSubtitle(video, subtitleStream.Index, path, CancellationToken.None).Result;
  262. if (!success)
  263. {
  264. return null;
  265. }
  266. }
  267. return path;
  268. }
  269. /// <summary>
  270. /// Gets the converted ass path.
  271. /// </summary>
  272. /// <param name="video">The video.</param>
  273. /// <param name="subtitleStream">The subtitle stream.</param>
  274. /// <returns>System.String.</returns>
  275. private string GetConvertedAssPath(Video video, MediaStream subtitleStream)
  276. {
  277. var path = ServerKernel.FFMpegManager.GetSubtitleCachePath(video, subtitleStream.Index, ".ass");
  278. if (!File.Exists(path))
  279. {
  280. var success = ServerKernel.FFMpegManager.ConvertTextSubtitle(subtitleStream, path, CancellationToken.None).Result;
  281. if (!success)
  282. {
  283. return null;
  284. }
  285. }
  286. return path;
  287. }
  288. /// <summary>
  289. /// Gets the internal graphical subtitle param.
  290. /// </summary>
  291. /// <param name="state">The state.</param>
  292. /// <param name="outputVideoCodec">The output video codec.</param>
  293. /// <returns>System.String.</returns>
  294. protected string GetInternalGraphicalSubtitleParam(StreamState state, string outputVideoCodec)
  295. {
  296. var outputSizeParam = string.Empty;
  297. var request = state.Request;
  298. // Add resolution params, if specified
  299. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  300. {
  301. outputSizeParam = GetOutputSizeParam(state, outputVideoCodec).TrimEnd('"');
  302. outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
  303. }
  304. return string.Format(" -filter_complex \"[0:{0}]format=yuva444p,lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:0] [sub] overlay{1}\"", state.SubtitleStream.Index, outputSizeParam);
  305. }
  306. /// <summary>
  307. /// Gets the number of audio channels to specify on the command line
  308. /// </summary>
  309. /// <param name="request">The request.</param>
  310. /// <param name="audioStream">The audio stream.</param>
  311. /// <returns>System.Nullable{System.Int32}.</returns>
  312. protected int? GetNumAudioChannelsParam(StreamRequest request, MediaStream audioStream)
  313. {
  314. if (audioStream.Channels > 2 && request.AudioCodec.HasValue)
  315. {
  316. if (request.AudioCodec.Value == AudioCodecs.Aac)
  317. {
  318. // libvo_aacenc currently only supports two channel output
  319. return 2;
  320. }
  321. if (request.AudioCodec.Value == AudioCodecs.Wma)
  322. {
  323. // wmav2 currently only supports two channel output
  324. return 2;
  325. }
  326. }
  327. return request.AudioChannels;
  328. }
  329. /// <summary>
  330. /// Determines whether the specified stream is H264.
  331. /// </summary>
  332. /// <param name="stream">The stream.</param>
  333. /// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
  334. protected bool IsH264(MediaStream stream)
  335. {
  336. return stream.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
  337. stream.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
  338. }
  339. /// <summary>
  340. /// Gets the name of the output audio codec
  341. /// </summary>
  342. /// <param name="request">The request.</param>
  343. /// <returns>System.String.</returns>
  344. protected string GetAudioCodec(StreamRequest request)
  345. {
  346. var codec = request.AudioCodec;
  347. if (codec.HasValue)
  348. {
  349. if (codec == AudioCodecs.Aac)
  350. {
  351. return "libvo_aacenc";
  352. }
  353. if (codec == AudioCodecs.Mp3)
  354. {
  355. return "libmp3lame";
  356. }
  357. if (codec == AudioCodecs.Vorbis)
  358. {
  359. return "libvorbis";
  360. }
  361. if (codec == AudioCodecs.Wma)
  362. {
  363. return "wmav2";
  364. }
  365. }
  366. return "copy";
  367. }
  368. /// <summary>
  369. /// Gets the name of the output video codec
  370. /// </summary>
  371. /// <param name="request">The request.</param>
  372. /// <returns>System.String.</returns>
  373. protected string GetVideoCodec(StreamRequest request)
  374. {
  375. var codec = request.VideoCodec;
  376. if (codec.HasValue)
  377. {
  378. if (codec == VideoCodecs.H264)
  379. {
  380. return "libx264";
  381. }
  382. if (codec == VideoCodecs.Vpx)
  383. {
  384. return "libvpx";
  385. }
  386. if (codec == VideoCodecs.Wmv)
  387. {
  388. return "wmv2";
  389. }
  390. if (codec == VideoCodecs.Theora)
  391. {
  392. return "libtheora";
  393. }
  394. }
  395. return "copy";
  396. }
  397. /// <summary>
  398. /// Gets the input argument.
  399. /// </summary>
  400. /// <param name="item">The item.</param>
  401. /// <param name="isoMount">The iso mount.</param>
  402. /// <returns>System.String.</returns>
  403. protected string GetInputArgument(BaseItem item, IIsoMount isoMount)
  404. {
  405. return isoMount == null ?
  406. ServerKernel.FFMpegManager.GetInputArgument(item) :
  407. ServerKernel.FFMpegManager.GetInputArgument(item as Video, isoMount);
  408. }
  409. /// <summary>
  410. /// Starts the FFMPEG.
  411. /// </summary>
  412. /// <param name="state">The state.</param>
  413. /// <param name="outputPath">The output path.</param>
  414. /// <returns>Task.</returns>
  415. protected async Task StartFFMpeg(StreamState state, string outputPath)
  416. {
  417. var video = state.Item as Video;
  418. //if (video != null && video.VideoType == VideoType.Iso &&
  419. // video.IsoType.HasValue && Kernel.IsoManager.CanMount(video.Path))
  420. //{
  421. // IsoMount = await Kernel.IsoManager.Mount(video.Path, CancellationToken.None).ConfigureAwait(false);
  422. //}
  423. var process = new Process
  424. {
  425. StartInfo = new ProcessStartInfo
  426. {
  427. CreateNoWindow = true,
  428. UseShellExecute = false,
  429. // Must consume both stdout and stderr or deadlocks may occur
  430. RedirectStandardOutput = true,
  431. RedirectStandardError = true,
  432. FileName = ServerKernel.FFMpegManager.FFMpegPath,
  433. WorkingDirectory = Path.GetDirectoryName(ServerKernel.FFMpegManager.FFMpegPath),
  434. Arguments = GetCommandLineArguments(outputPath, state),
  435. WindowStyle = ProcessWindowStyle.Hidden,
  436. ErrorDialog = false
  437. },
  438. EnableRaisingEvents = true
  439. };
  440. Plugin.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process);
  441. //Logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments);
  442. var logFilePath = Path.Combine(Kernel.ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt");
  443. // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
  444. state.LogFileStream = new FileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
  445. process.Exited += (sender, args) => OnFFMpegProcessExited(process, state);
  446. try
  447. {
  448. process.Start();
  449. }
  450. catch (Win32Exception ex)
  451. {
  452. Logger.ErrorException("Error starting ffmpeg", ex);
  453. Plugin.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);
  454. state.LogFileStream.Dispose();
  455. throw;
  456. }
  457. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  458. process.BeginOutputReadLine();
  459. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  460. process.StandardError.BaseStream.CopyToAsync(state.LogFileStream);
  461. // Wait for the file to exist before proceeeding
  462. while (!File.Exists(outputPath))
  463. {
  464. await Task.Delay(100).ConfigureAwait(false);
  465. }
  466. }
  467. /// <summary>
  468. /// Processes the exited.
  469. /// </summary>
  470. /// <param name="process">The process.</param>
  471. /// <param name="state">The state.</param>
  472. protected void OnFFMpegProcessExited(Process process, StreamState state)
  473. {
  474. if (state.IsoMount != null)
  475. {
  476. state.IsoMount.Dispose();
  477. state.IsoMount = null;
  478. }
  479. var outputFilePath = GetOutputFilePath(state);
  480. state.LogFileStream.Dispose();
  481. int? exitCode = null;
  482. try
  483. {
  484. exitCode = process.ExitCode;
  485. Logger.Info("FFMpeg exited with code {0} for {1}", exitCode.Value, outputFilePath);
  486. }
  487. catch
  488. {
  489. Logger.Info("FFMpeg exited with an error for {0}", outputFilePath);
  490. }
  491. process.Dispose();
  492. Plugin.Instance.OnTranscodingFinished(outputFilePath, TranscodingJobType);
  493. if (!exitCode.HasValue || exitCode.Value != 0)
  494. {
  495. Logger.Info("Deleting partial stream file(s) {0}", outputFilePath);
  496. try
  497. {
  498. DeletePartialStreamFiles(outputFilePath);
  499. }
  500. catch (IOException ex)
  501. {
  502. Logger.ErrorException("Error deleting partial stream file(s) {0}", ex, outputFilePath);
  503. }
  504. }
  505. else
  506. {
  507. Logger.Info("FFMpeg completed and exited normally for {0}", outputFilePath);
  508. }
  509. }
  510. /// <summary>
  511. /// Deletes the partial stream files.
  512. /// </summary>
  513. /// <param name="outputFilePath">The output file path.</param>
  514. protected abstract void DeletePartialStreamFiles(string outputFilePath);
  515. /// <summary>
  516. /// Gets the state.
  517. /// </summary>
  518. /// <param name="request">The request.</param>
  519. /// <returns>StreamState.</returns>
  520. protected StreamState GetState(StreamRequest request)
  521. {
  522. var item = DtoBuilder.GetItemByClientId(request.Id, UserManager);
  523. var media = (IHasMediaStreams)item;
  524. var url = Request.PathInfo;
  525. if (!request.AudioCodec.HasValue)
  526. {
  527. request.AudioCodec = InferAudioCodec(url);
  528. }
  529. if (!request.VideoCodec.HasValue)
  530. {
  531. request.VideoCodec = InferVideoCodec(url);
  532. }
  533. return new StreamState
  534. {
  535. Item = item,
  536. Request = request,
  537. AudioStream = GetMediaStream(media.MediaStreams, request.AudioStreamIndex, MediaStreamType.Audio, true),
  538. VideoStream = GetMediaStream(media.MediaStreams, request.VideoStreamIndex, MediaStreamType.Video, true),
  539. SubtitleStream = GetMediaStream(media.MediaStreams, request.SubtitleStreamIndex, MediaStreamType.Subtitle, false),
  540. Url = url
  541. };
  542. }
  543. /// <summary>
  544. /// Infers the audio codec based on the url
  545. /// </summary>
  546. /// <param name="url">The URL.</param>
  547. /// <returns>System.Nullable{AudioCodecs}.</returns>
  548. private AudioCodecs? InferAudioCodec(string url)
  549. {
  550. var ext = Path.GetExtension(url);
  551. if (string.Equals(ext, ".mp3", StringComparison.OrdinalIgnoreCase))
  552. {
  553. return AudioCodecs.Mp3;
  554. }
  555. if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
  556. {
  557. return AudioCodecs.Aac;
  558. }
  559. if (string.Equals(ext, ".wam", StringComparison.OrdinalIgnoreCase))
  560. {
  561. return AudioCodecs.Wma;
  562. }
  563. return null;
  564. }
  565. /// <summary>
  566. /// Infers the video codec.
  567. /// </summary>
  568. /// <param name="url">The URL.</param>
  569. /// <returns>System.Nullable{VideoCodecs}.</returns>
  570. private VideoCodecs? InferVideoCodec(string url)
  571. {
  572. var ext = Path.GetExtension(url);
  573. if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
  574. {
  575. return VideoCodecs.Wmv;
  576. }
  577. if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
  578. {
  579. return VideoCodecs.Vpx;
  580. }
  581. if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase))
  582. {
  583. return VideoCodecs.Theora;
  584. }
  585. return null;
  586. }
  587. }
  588. }