BaseProgressiveStreamingService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // Try to infer based on the desired video codec
  34. if (state.Request.VideoCodec.HasValue)
  35. {
  36. var video = state.Item as Video;
  37. if (video != null)
  38. {
  39. switch (state.Request.VideoCodec.Value)
  40. {
  41. case VideoCodecs.H264:
  42. return ".ts";
  43. case VideoCodecs.Theora:
  44. return ".ogv";
  45. case VideoCodecs.Vpx:
  46. return ".webm";
  47. case VideoCodecs.Wmv:
  48. return ".asf";
  49. }
  50. }
  51. }
  52. // Try to infer based on the desired audio codec
  53. if (state.Request.AudioCodec.HasValue)
  54. {
  55. var audio = state.Item as Audio;
  56. if (audio != null)
  57. {
  58. switch (state.Request.AudioCodec.Value)
  59. {
  60. case AudioCodecs.Aac:
  61. return ".aac";
  62. case AudioCodecs.Mp3:
  63. return ".mp3";
  64. case AudioCodecs.Vorbis:
  65. return ".ogg";
  66. case AudioCodecs.Wma:
  67. return ".wma";
  68. }
  69. }
  70. }
  71. return null;
  72. }
  73. /// <summary>
  74. /// Gets the type of the transcoding job.
  75. /// </summary>
  76. /// <value>The type of the transcoding job.</value>
  77. protected override TranscodingJobType TranscodingJobType
  78. {
  79. get { return TranscodingJobType.Progressive; }
  80. }
  81. /// <summary>
  82. /// Processes the request.
  83. /// </summary>
  84. /// <param name="request">The request.</param>
  85. /// <returns>Task.</returns>
  86. protected object ProcessRequest(StreamRequest request)
  87. {
  88. var state = GetState(request);
  89. if (request.Static)
  90. {
  91. return ToStaticFileResult(state.Item.Path);
  92. }
  93. var outputPath = GetOutputFilePath(state);
  94. if (File.Exists(outputPath) && !ServerEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
  95. {
  96. return ToStaticFileResult(outputPath);
  97. }
  98. return GetStreamResult(state).Result;
  99. }
  100. /// <summary>
  101. /// Gets the stream result.
  102. /// </summary>
  103. /// <param name="state">The state.</param>
  104. /// <returns>Task{System.Object}.</returns>
  105. private async Task<ProgressiveStreamWriter> GetStreamResult(StreamState state)
  106. {
  107. // Use the command line args with a dummy playlist path
  108. var outputPath = GetOutputFilePath(state);
  109. Response.ContentType = MimeTypes.GetMimeType(outputPath);
  110. if (!File.Exists(outputPath))
  111. {
  112. await StartFFMpeg(state, outputPath).ConfigureAwait(false);
  113. }
  114. else
  115. {
  116. ServerEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
  117. }
  118. return new ProgressiveStreamWriter
  119. {
  120. Path = outputPath,
  121. State = state
  122. };
  123. }
  124. /// <summary>
  125. /// Deletes the partial stream files.
  126. /// </summary>
  127. /// <param name="outputFilePath">The output file path.</param>
  128. protected override void DeletePartialStreamFiles(string outputFilePath)
  129. {
  130. File.Delete(outputFilePath);
  131. }
  132. }
  133. }