BaseProgressiveStreamingService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.Dto;
  8. using System.IO;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Api.Playback.Progressive
  11. {
  12. /// <summary>
  13. /// Class BaseProgressiveStreamingService
  14. /// </summary>
  15. public abstract class BaseProgressiveStreamingService : BaseStreamingService
  16. {
  17. protected BaseProgressiveStreamingService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager) :
  18. base(appPaths, userManager, libraryManager, isoManager)
  19. {
  20. }
  21. /// <summary>
  22. /// Gets the output file extension.
  23. /// </summary>
  24. /// <param name="state">The state.</param>
  25. /// <returns>System.String.</returns>
  26. protected override string GetOutputFileExtension(StreamState state)
  27. {
  28. var ext = base.GetOutputFileExtension(state);
  29. if (!string.IsNullOrEmpty(ext))
  30. {
  31. return ext;
  32. }
  33. var videoRequest = state.Request as VideoStreamRequest;
  34. // Try to infer based on the desired video codec
  35. if (videoRequest != null && videoRequest.VideoCodec.HasValue)
  36. {
  37. var video = state.Item as Video;
  38. if (video != null)
  39. {
  40. switch (videoRequest.VideoCodec.Value)
  41. {
  42. case VideoCodecs.H264:
  43. return ".ts";
  44. case VideoCodecs.Theora:
  45. return ".ogv";
  46. case VideoCodecs.Vpx:
  47. return ".webm";
  48. case VideoCodecs.Wmv:
  49. return ".asf";
  50. }
  51. }
  52. }
  53. // Try to infer based on the desired audio codec
  54. if (state.Request.AudioCodec.HasValue)
  55. {
  56. var audio = state.Item as Audio;
  57. if (audio != null)
  58. {
  59. switch (state.Request.AudioCodec.Value)
  60. {
  61. case AudioCodecs.Aac:
  62. return ".aac";
  63. case AudioCodecs.Mp3:
  64. return ".mp3";
  65. case AudioCodecs.Vorbis:
  66. return ".ogg";
  67. case AudioCodecs.Wma:
  68. return ".wma";
  69. }
  70. }
  71. }
  72. return null;
  73. }
  74. /// <summary>
  75. /// Gets the type of the transcoding job.
  76. /// </summary>
  77. /// <value>The type of the transcoding job.</value>
  78. protected override TranscodingJobType TranscodingJobType
  79. {
  80. get { return TranscodingJobType.Progressive; }
  81. }
  82. /// <summary>
  83. /// Processes the request.
  84. /// </summary>
  85. /// <param name="request">The request.</param>
  86. /// <returns>Task.</returns>
  87. protected object ProcessRequest(StreamRequest request)
  88. {
  89. var state = GetState(request);
  90. if (request.Static)
  91. {
  92. return ToStaticFileResult(state.Item.Path);
  93. }
  94. var outputPath = GetOutputFilePath(state);
  95. if (File.Exists(outputPath) && !ApiEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
  96. {
  97. return ToStaticFileResult(outputPath);
  98. }
  99. return GetStreamResult(state).Result;
  100. }
  101. /// <summary>
  102. /// Gets the stream result.
  103. /// </summary>
  104. /// <param name="state">The state.</param>
  105. /// <returns>Task{System.Object}.</returns>
  106. private async Task<ProgressiveStreamWriter> GetStreamResult(StreamState state)
  107. {
  108. // Use the command line args with a dummy playlist path
  109. var outputPath = GetOutputFilePath(state);
  110. Response.ContentType = MimeTypes.GetMimeType(outputPath);
  111. if (!File.Exists(outputPath))
  112. {
  113. await StartFFMpeg(state, outputPath).ConfigureAwait(false);
  114. }
  115. else
  116. {
  117. ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
  118. }
  119. return new ProgressiveStreamWriter(outputPath, state, Logger);
  120. }
  121. /// <summary>
  122. /// Deletes the partial stream files.
  123. /// </summary>
  124. /// <param name="outputFilePath">The output file path.</param>
  125. protected override void DeletePartialStreamFiles(string outputFilePath)
  126. {
  127. File.Delete(outputFilePath);
  128. }
  129. }
  130. }