BaseProgressiveStreamingService.cs 5.0 KB

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