BaseProgressiveStreamingService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using MediaBrowser.Api.Images;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Entities.Audio;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Model.Dto;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Threading.Tasks;
  13. using MediaBrowser.Model.Entities;
  14. namespace MediaBrowser.Api.Playback.Progressive
  15. {
  16. /// <summary>
  17. /// Class BaseProgressiveStreamingService
  18. /// </summary>
  19. public abstract class BaseProgressiveStreamingService : BaseStreamingService
  20. {
  21. protected BaseProgressiveStreamingService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager) :
  22. base(appPaths, userManager, libraryManager, isoManager)
  23. {
  24. }
  25. /// <summary>
  26. /// Gets the output file extension.
  27. /// </summary>
  28. /// <param name="state">The state.</param>
  29. /// <returns>System.String.</returns>
  30. protected override string GetOutputFileExtension(StreamState state)
  31. {
  32. var ext = base.GetOutputFileExtension(state);
  33. if (!string.IsNullOrEmpty(ext))
  34. {
  35. return ext;
  36. }
  37. var videoRequest = state.Request as VideoStreamRequest;
  38. // Try to infer based on the desired video codec
  39. if (videoRequest != null && videoRequest.VideoCodec.HasValue)
  40. {
  41. var video = state.Item as Video;
  42. if (video != null)
  43. {
  44. switch (videoRequest.VideoCodec.Value)
  45. {
  46. case VideoCodecs.H264:
  47. return ".ts";
  48. case VideoCodecs.Theora:
  49. return ".ogv";
  50. case VideoCodecs.Vpx:
  51. return ".webm";
  52. case VideoCodecs.Wmv:
  53. return ".asf";
  54. }
  55. }
  56. }
  57. // Try to infer based on the desired audio codec
  58. if (state.Request.AudioCodec.HasValue)
  59. {
  60. var audio = state.Item as Audio;
  61. if (audio != null)
  62. {
  63. switch (state.Request.AudioCodec.Value)
  64. {
  65. case AudioCodecs.Aac:
  66. return ".aac";
  67. case AudioCodecs.Mp3:
  68. return ".mp3";
  69. case AudioCodecs.Vorbis:
  70. return ".ogg";
  71. case AudioCodecs.Wma:
  72. return ".wma";
  73. }
  74. }
  75. }
  76. return null;
  77. }
  78. /// <summary>
  79. /// Adds the dlna headers.
  80. /// </summary>
  81. /// <param name="state">The state.</param>
  82. /// <param name="responseHeaders">The response headers.</param>
  83. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  84. private void AddDlnaHeaders(StreamState state, IDictionary<string, string> responseHeaders)
  85. {
  86. var timeSeek = RequestContext.GetHeader("TimeSeekRange.dlna.org");
  87. if (!string.IsNullOrEmpty(timeSeek))
  88. {
  89. ResultFactory.ThrowError(406, "Time seek not supported during encoding.", responseHeaders);
  90. return;
  91. }
  92. var transferMode = RequestContext.GetHeader("transferMode.dlna.org");
  93. responseHeaders["transferMode.dlna.org"] = string.IsNullOrEmpty(transferMode) ? "Streaming" : transferMode;
  94. var contentFeatures = string.Empty;
  95. var extension = GetOutputFileExtension(state);
  96. if (string.Equals(extension, ".mp3", StringComparison.OrdinalIgnoreCase))
  97. {
  98. contentFeatures = "DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
  99. }
  100. else if (string.Equals(extension, ".aac", StringComparison.OrdinalIgnoreCase))
  101. {
  102. contentFeatures = "DLNA.ORG_PN=AAC_ISO;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
  103. }
  104. else if (string.Equals(extension, ".wma", StringComparison.OrdinalIgnoreCase))
  105. {
  106. contentFeatures = "DLNA.ORG_PN=WMABASE;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
  107. }
  108. else if (string.Equals(extension, ".avi", StringComparison.OrdinalIgnoreCase))
  109. {
  110. contentFeatures = "DLNA.ORG_PN=AVI;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
  111. }
  112. else if (string.Equals(extension, ".mp4", StringComparison.OrdinalIgnoreCase))
  113. {
  114. contentFeatures = "DLNA.ORG_PN=MPEG4_P2_SP_AAC;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
  115. }
  116. else if (string.Equals(extension, ".mpeg", StringComparison.OrdinalIgnoreCase))
  117. {
  118. contentFeatures = "DLNA.ORG_PN=MPEG_PS_PAL;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
  119. }
  120. else if (string.Equals(extension, ".wmv", StringComparison.OrdinalIgnoreCase))
  121. {
  122. contentFeatures = "DLNA.ORG_PN=WMVHIGH_BASE;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
  123. }
  124. else if (string.Equals(extension, ".asf", StringComparison.OrdinalIgnoreCase))
  125. {
  126. contentFeatures = "DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000";
  127. }
  128. else if (string.Equals(extension, ".mkv", StringComparison.OrdinalIgnoreCase))
  129. {
  130. contentFeatures = "DLNA.ORG_OP=01;DLNA.ORG_CI=0";
  131. }
  132. if (!string.IsNullOrEmpty(contentFeatures))
  133. {
  134. responseHeaders["ContentFeatures.DLNA.ORG"] = contentFeatures;
  135. }
  136. }
  137. /// <summary>
  138. /// Gets the type of the transcoding job.
  139. /// </summary>
  140. /// <value>The type of the transcoding job.</value>
  141. protected override TranscodingJobType TranscodingJobType
  142. {
  143. get { return TranscodingJobType.Progressive; }
  144. }
  145. /// <summary>
  146. /// Processes the request.
  147. /// </summary>
  148. /// <param name="request">The request.</param>
  149. /// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
  150. /// <returns>Task.</returns>
  151. protected object ProcessRequest(StreamRequest request, bool isHeadRequest)
  152. {
  153. var state = GetState(request);
  154. if (request.AlbumArt)
  155. {
  156. return GetAlbumArtResponse(state);
  157. }
  158. var responseHeaders = new Dictionary<string, string>();
  159. AddDlnaHeaders(state, responseHeaders);
  160. if (request.Static)
  161. {
  162. return ResultFactory.GetStaticFileResult(RequestContext, state.Item.Path, responseHeaders, isHeadRequest);
  163. }
  164. var outputPath = GetOutputFilePath(state);
  165. if (File.Exists(outputPath) && !ApiEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
  166. {
  167. return ResultFactory.GetStaticFileResult(RequestContext, outputPath, responseHeaders, isHeadRequest);
  168. }
  169. return GetStreamResult(state, responseHeaders, isHeadRequest).Result;
  170. }
  171. /// <summary>
  172. /// Gets the album art response.
  173. /// </summary>
  174. /// <param name="state">The state.</param>
  175. /// <returns>System.Object.</returns>
  176. private object GetAlbumArtResponse(StreamState state)
  177. {
  178. var request = new GetItemImage
  179. {
  180. MaxWidth = 800,
  181. MaxHeight = 800,
  182. Type = ImageType.Primary,
  183. Id = state.Item.Id.ToString()
  184. };
  185. // Try and find some image to return
  186. if (!state.Item.HasImage(ImageType.Primary))
  187. {
  188. if (state.Item.HasImage(ImageType.Backdrop))
  189. {
  190. request.Type = ImageType.Backdrop;
  191. }
  192. else if (state.Item.HasImage(ImageType.Thumb))
  193. {
  194. request.Type = ImageType.Thumb;
  195. }
  196. else if (state.Item.HasImage(ImageType.Logo))
  197. {
  198. request.Type = ImageType.Logo;
  199. }
  200. }
  201. return new ImageService(UserManager, LibraryManager)
  202. {
  203. Logger = Logger,
  204. RequestContext = RequestContext,
  205. ResultFactory = ResultFactory
  206. }.Get(request);
  207. }
  208. /// <summary>
  209. /// Gets the stream result.
  210. /// </summary>
  211. /// <param name="state">The state.</param>
  212. /// <param name="responseHeaders">The response headers.</param>
  213. /// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
  214. /// <returns>Task{System.Object}.</returns>
  215. private async Task<object> GetStreamResult(StreamState state, IDictionary<string, string> responseHeaders, bool isHeadRequest)
  216. {
  217. // Use the command line args with a dummy playlist path
  218. var outputPath = GetOutputFilePath(state);
  219. var contentType = MimeTypes.GetMimeType(outputPath);
  220. // Headers only
  221. if (isHeadRequest)
  222. {
  223. responseHeaders["Accept-Ranges"] = "none";
  224. return ResultFactory.GetResult(null, contentType, responseHeaders);
  225. }
  226. if (!File.Exists(outputPath))
  227. {
  228. await StartFFMpeg(state, outputPath).ConfigureAwait(false);
  229. }
  230. else
  231. {
  232. ApiEntryPoint.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
  233. }
  234. var result = new ProgressiveStreamWriter(outputPath, state, Logger);
  235. result.Options["Accept-Ranges"] = "none";
  236. result.Options["Content-Type"] = contentType;
  237. // Add the response headers to the result object
  238. foreach (var item in responseHeaders)
  239. {
  240. result.Options[item.Key] = item.Value;
  241. }
  242. return result;
  243. }
  244. /// <summary>
  245. /// Deletes the partial stream files.
  246. /// </summary>
  247. /// <param name="outputFilePath">The output file path.</param>
  248. protected override void DeletePartialStreamFiles(string outputFilePath)
  249. {
  250. File.Delete(outputFilePath);
  251. }
  252. }
  253. }