BaseProgressiveStreamingService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Model.Dto;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. namespace MediaBrowser.Api.Playback.Progressive
  8. {
  9. /// <summary>
  10. /// Class BaseProgressiveStreamingService
  11. /// </summary>
  12. public abstract class BaseProgressiveStreamingService : BaseStreamingService
  13. {
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="BaseProgressiveStreamingService" /> class.
  16. /// </summary>
  17. /// <param name="appPaths">The app paths.</param>
  18. protected BaseProgressiveStreamingService(IServerApplicationPaths appPaths)
  19. : base(appPaths)
  20. {
  21. }
  22. /// <summary>
  23. /// Gets the output file extension.
  24. /// </summary>
  25. /// <param name="state">The state.</param>
  26. /// <returns>System.String.</returns>
  27. protected override string GetOutputFileExtension(StreamState state)
  28. {
  29. var ext = base.GetOutputFileExtension(state);
  30. if (!string.IsNullOrEmpty(ext))
  31. {
  32. return ext;
  33. }
  34. // Try to infer based on the desired video codec
  35. if (state.Request.VideoCodec.HasValue)
  36. {
  37. var video = state.Item as Video;
  38. if (video != null)
  39. {
  40. switch (state.Request.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) && !Plugin.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. if (!File.Exists(outputPath))
  111. {
  112. await StartFFMpeg(state, outputPath).ConfigureAwait(false);
  113. }
  114. else
  115. {
  116. Plugin.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. }