AudioController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Threading.Tasks;
  5. using Jellyfin.Api.Attributes;
  6. using Jellyfin.Api.Helpers;
  7. using Jellyfin.Api.Models.StreamingDtos;
  8. using MediaBrowser.Controller.MediaEncoding;
  9. using MediaBrowser.Controller.Streaming;
  10. using MediaBrowser.Model.Dlna;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Mvc;
  13. namespace Jellyfin.Api.Controllers;
  14. /// <summary>
  15. /// The audio controller.
  16. /// </summary>
  17. public class AudioController : BaseJellyfinApiController
  18. {
  19. private readonly AudioHelper _audioHelper;
  20. private readonly TranscodingJobType _transcodingJobType = TranscodingJobType.Progressive;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="AudioController"/> class.
  23. /// </summary>
  24. /// <param name="audioHelper">Instance of <see cref="AudioHelper"/>.</param>
  25. public AudioController(AudioHelper audioHelper)
  26. {
  27. _audioHelper = audioHelper;
  28. }
  29. /// <summary>
  30. /// Gets an audio stream.
  31. /// </summary>
  32. /// <param name="itemId">The item id.</param>
  33. /// <param name="container">The audio container.</param>
  34. /// <param name="static">Optional. If true, the original file will be streamed statically without any encoding. Use either no url extension or the original file extension. true/false.</param>
  35. /// <param name="params">The streaming parameters.</param>
  36. /// <param name="tag">The tag.</param>
  37. /// <param name="deviceProfileId">Optional. The dlna device profile id to utilize.</param>
  38. /// <param name="playSessionId">The play session id.</param>
  39. /// <param name="segmentContainer">The segment container.</param>
  40. /// <param name="segmentLength">The segment length.</param>
  41. /// <param name="minSegments">The minimum number of segments.</param>
  42. /// <param name="mediaSourceId">The media version id, if playing an alternate version.</param>
  43. /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</param>
  44. /// <param name="audioCodec">Optional. Specify an audio codec to encode to, e.g. mp3. If omitted the server will auto-select using the url's extension.</param>
  45. /// <param name="enableAutoStreamCopy">Whether or not to allow automatic stream copy if requested values match the original source. Defaults to true.</param>
  46. /// <param name="allowVideoStreamCopy">Whether or not to allow copying of the video stream url.</param>
  47. /// <param name="allowAudioStreamCopy">Whether or not to allow copying of the audio stream url.</param>
  48. /// <param name="breakOnNonKeyFrames">Optional. Whether to break on non key frames.</param>
  49. /// <param name="audioSampleRate">Optional. Specify a specific audio sample rate, e.g. 44100.</param>
  50. /// <param name="maxAudioBitDepth">Optional. The maximum audio bit depth.</param>
  51. /// <param name="audioBitRate">Optional. Specify an audio bitrate to encode to, e.g. 128000. If omitted this will be left to encoder defaults.</param>
  52. /// <param name="audioChannels">Optional. Specify a specific number of audio channels to encode to, e.g. 2.</param>
  53. /// <param name="maxAudioChannels">Optional. Specify a maximum number of audio channels to encode to, e.g. 2.</param>
  54. /// <param name="profile">Optional. Specify a specific an encoder profile (varies by encoder), e.g. main, baseline, high.</param>
  55. /// <param name="level">Optional. Specify a level for the encoder profile (varies by encoder), e.g. 3, 3.1.</param>
  56. /// <param name="framerate">Optional. A specific video framerate to encode to, e.g. 23.976. Generally this should be omitted unless the device has specific requirements.</param>
  57. /// <param name="maxFramerate">Optional. A specific maximum video framerate to encode to, e.g. 23.976. Generally this should be omitted unless the device has specific requirements.</param>
  58. /// <param name="copyTimestamps">Whether or not to copy timestamps when transcoding with an offset. Defaults to false.</param>
  59. /// <param name="startTimeTicks">Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms.</param>
  60. /// <param name="width">Optional. The fixed horizontal resolution of the encoded video.</param>
  61. /// <param name="height">Optional. The fixed vertical resolution of the encoded video.</param>
  62. /// <param name="videoBitRate">Optional. Specify a video bitrate to encode to, e.g. 500000. If omitted this will be left to encoder defaults.</param>
  63. /// <param name="subtitleStreamIndex">Optional. The index of the subtitle stream to use. If omitted no subtitles will be used.</param>
  64. /// <param name="subtitleMethod">Optional. Specify the subtitle delivery method.</param>
  65. /// <param name="maxRefFrames">Optional.</param>
  66. /// <param name="maxVideoBitDepth">Optional. The maximum video bit depth.</param>
  67. /// <param name="requireAvc">Optional. Whether to require avc.</param>
  68. /// <param name="deInterlace">Optional. Whether to deinterlace the video.</param>
  69. /// <param name="requireNonAnamorphic">Optional. Whether to require a non anamorphic stream.</param>
  70. /// <param name="transcodingMaxAudioChannels">Optional. The maximum number of audio channels to transcode.</param>
  71. /// <param name="cpuCoreLimit">Optional. The limit of how many cpu cores to use.</param>
  72. /// <param name="liveStreamId">The live stream id.</param>
  73. /// <param name="enableMpegtsM2TsMode">Optional. Whether to enable the MpegtsM2Ts mode.</param>
  74. /// <param name="videoCodec">Optional. Specify a video codec to encode to, e.g. h264. If omitted the server will auto-select using the url's extension.</param>
  75. /// <param name="subtitleCodec">Optional. Specify a subtitle codec to encode to.</param>
  76. /// <param name="transcodeReasons">Optional. The transcoding reason.</param>
  77. /// <param name="audioStreamIndex">Optional. The index of the audio stream to use. If omitted the first audio stream will be used.</param>
  78. /// <param name="videoStreamIndex">Optional. The index of the video stream to use. If omitted the first video stream will be used.</param>
  79. /// <param name="context">Optional. The <see cref="EncodingContext"/>.</param>
  80. /// <param name="streamOptions">Optional. The streaming options.</param>
  81. /// <param name="enableAudioVbrEncoding">Optional. Whether to enable Audio Encoding.</param>
  82. /// <response code="200">Audio stream returned.</response>
  83. /// <returns>A <see cref="FileResult"/> containing the audio file.</returns>
  84. [HttpGet("{itemId}/stream", Name = "GetAudioStream")]
  85. [HttpHead("{itemId}/stream", Name = "HeadAudioStream")]
  86. [ProducesResponseType(StatusCodes.Status200OK)]
  87. [ProducesAudioFile]
  88. public async Task<ActionResult> GetAudioStream(
  89. [FromRoute, Required] Guid itemId,
  90. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? container,
  91. [FromQuery] bool? @static,
  92. [FromQuery] string? @params,
  93. [FromQuery] string? tag,
  94. [FromQuery, ParameterObsolete] string? deviceProfileId,
  95. [FromQuery] string? playSessionId,
  96. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? segmentContainer,
  97. [FromQuery] int? segmentLength,
  98. [FromQuery] int? minSegments,
  99. [FromQuery] string? mediaSourceId,
  100. [FromQuery] string? deviceId,
  101. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? audioCodec,
  102. [FromQuery] bool? enableAutoStreamCopy,
  103. [FromQuery] bool? allowVideoStreamCopy,
  104. [FromQuery] bool? allowAudioStreamCopy,
  105. [FromQuery] bool? breakOnNonKeyFrames,
  106. [FromQuery] int? audioSampleRate,
  107. [FromQuery] int? maxAudioBitDepth,
  108. [FromQuery] int? audioBitRate,
  109. [FromQuery] int? audioChannels,
  110. [FromQuery] int? maxAudioChannels,
  111. [FromQuery] string? profile,
  112. [FromQuery] string? level,
  113. [FromQuery] float? framerate,
  114. [FromQuery] float? maxFramerate,
  115. [FromQuery] bool? copyTimestamps,
  116. [FromQuery] long? startTimeTicks,
  117. [FromQuery] int? width,
  118. [FromQuery] int? height,
  119. [FromQuery] int? videoBitRate,
  120. [FromQuery] int? subtitleStreamIndex,
  121. [FromQuery] SubtitleDeliveryMethod? subtitleMethod,
  122. [FromQuery] int? maxRefFrames,
  123. [FromQuery] int? maxVideoBitDepth,
  124. [FromQuery] bool? requireAvc,
  125. [FromQuery] bool? deInterlace,
  126. [FromQuery] bool? requireNonAnamorphic,
  127. [FromQuery] int? transcodingMaxAudioChannels,
  128. [FromQuery] int? cpuCoreLimit,
  129. [FromQuery] string? liveStreamId,
  130. [FromQuery] bool? enableMpegtsM2TsMode,
  131. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? videoCodec,
  132. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? subtitleCodec,
  133. [FromQuery] string? transcodeReasons,
  134. [FromQuery] int? audioStreamIndex,
  135. [FromQuery] int? videoStreamIndex,
  136. [FromQuery] EncodingContext? context,
  137. [FromQuery] Dictionary<string, string>? streamOptions,
  138. [FromQuery] bool enableAudioVbrEncoding = true)
  139. {
  140. StreamingRequestDto streamingRequest = new StreamingRequestDto
  141. {
  142. Id = itemId,
  143. Container = container,
  144. Static = @static ?? false,
  145. Params = @params,
  146. Tag = tag,
  147. PlaySessionId = playSessionId,
  148. SegmentContainer = segmentContainer,
  149. SegmentLength = segmentLength,
  150. MinSegments = minSegments,
  151. MediaSourceId = mediaSourceId,
  152. DeviceId = deviceId,
  153. AudioCodec = audioCodec,
  154. EnableAutoStreamCopy = enableAutoStreamCopy ?? true,
  155. AllowAudioStreamCopy = allowAudioStreamCopy ?? true,
  156. AllowVideoStreamCopy = allowVideoStreamCopy ?? true,
  157. BreakOnNonKeyFrames = breakOnNonKeyFrames ?? false,
  158. AudioSampleRate = audioSampleRate,
  159. MaxAudioChannels = maxAudioChannels,
  160. AudioBitRate = audioBitRate,
  161. MaxAudioBitDepth = maxAudioBitDepth,
  162. AudioChannels = audioChannels,
  163. Profile = profile,
  164. Level = level,
  165. Framerate = framerate,
  166. MaxFramerate = maxFramerate,
  167. CopyTimestamps = copyTimestamps ?? false,
  168. StartTimeTicks = startTimeTicks,
  169. Width = width,
  170. Height = height,
  171. VideoBitRate = videoBitRate,
  172. SubtitleStreamIndex = subtitleStreamIndex,
  173. SubtitleMethod = subtitleMethod ?? SubtitleDeliveryMethod.Encode,
  174. MaxRefFrames = maxRefFrames,
  175. MaxVideoBitDepth = maxVideoBitDepth,
  176. RequireAvc = requireAvc ?? false,
  177. DeInterlace = deInterlace ?? false,
  178. RequireNonAnamorphic = requireNonAnamorphic ?? false,
  179. TranscodingMaxAudioChannels = transcodingMaxAudioChannels,
  180. CpuCoreLimit = cpuCoreLimit,
  181. LiveStreamId = liveStreamId,
  182. EnableMpegtsM2TsMode = enableMpegtsM2TsMode ?? false,
  183. VideoCodec = videoCodec,
  184. SubtitleCodec = subtitleCodec,
  185. TranscodeReasons = transcodeReasons,
  186. AudioStreamIndex = audioStreamIndex,
  187. VideoStreamIndex = videoStreamIndex,
  188. Context = context ?? EncodingContext.Static,
  189. StreamOptions = streamOptions,
  190. EnableAudioVbrEncoding = enableAudioVbrEncoding
  191. };
  192. return await _audioHelper.GetAudioStream(_transcodingJobType, streamingRequest).ConfigureAwait(false);
  193. }
  194. /// <summary>
  195. /// Gets an audio stream.
  196. /// </summary>
  197. /// <param name="itemId">The item id.</param>
  198. /// <param name="container">The audio container.</param>
  199. /// <param name="static">Optional. If true, the original file will be streamed statically without any encoding. Use either no url extension or the original file extension. true/false.</param>
  200. /// <param name="params">The streaming parameters.</param>
  201. /// <param name="tag">The tag.</param>
  202. /// <param name="deviceProfileId">Optional. The dlna device profile id to utilize.</param>
  203. /// <param name="playSessionId">The play session id.</param>
  204. /// <param name="segmentContainer">The segment container.</param>
  205. /// <param name="segmentLength">The segment length.</param>
  206. /// <param name="minSegments">The minimum number of segments.</param>
  207. /// <param name="mediaSourceId">The media version id, if playing an alternate version.</param>
  208. /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</param>
  209. /// <param name="audioCodec">Optional. Specify an audio codec to encode to, e.g. mp3. If omitted the server will auto-select using the url's extension.</param>
  210. /// <param name="enableAutoStreamCopy">Whether or not to allow automatic stream copy if requested values match the original source. Defaults to true.</param>
  211. /// <param name="allowVideoStreamCopy">Whether or not to allow copying of the video stream url.</param>
  212. /// <param name="allowAudioStreamCopy">Whether or not to allow copying of the audio stream url.</param>
  213. /// <param name="breakOnNonKeyFrames">Optional. Whether to break on non key frames.</param>
  214. /// <param name="audioSampleRate">Optional. Specify a specific audio sample rate, e.g. 44100.</param>
  215. /// <param name="maxAudioBitDepth">Optional. The maximum audio bit depth.</param>
  216. /// <param name="audioBitRate">Optional. Specify an audio bitrate to encode to, e.g. 128000. If omitted this will be left to encoder defaults.</param>
  217. /// <param name="audioChannels">Optional. Specify a specific number of audio channels to encode to, e.g. 2.</param>
  218. /// <param name="maxAudioChannels">Optional. Specify a maximum number of audio channels to encode to, e.g. 2.</param>
  219. /// <param name="profile">Optional. Specify a specific an encoder profile (varies by encoder), e.g. main, baseline, high.</param>
  220. /// <param name="level">Optional. Specify a level for the encoder profile (varies by encoder), e.g. 3, 3.1.</param>
  221. /// <param name="framerate">Optional. A specific video framerate to encode to, e.g. 23.976. Generally this should be omitted unless the device has specific requirements.</param>
  222. /// <param name="maxFramerate">Optional. A specific maximum video framerate to encode to, e.g. 23.976. Generally this should be omitted unless the device has specific requirements.</param>
  223. /// <param name="copyTimestamps">Whether or not to copy timestamps when transcoding with an offset. Defaults to false.</param>
  224. /// <param name="startTimeTicks">Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms.</param>
  225. /// <param name="width">Optional. The fixed horizontal resolution of the encoded video.</param>
  226. /// <param name="height">Optional. The fixed vertical resolution of the encoded video.</param>
  227. /// <param name="videoBitRate">Optional. Specify a video bitrate to encode to, e.g. 500000. If omitted this will be left to encoder defaults.</param>
  228. /// <param name="subtitleStreamIndex">Optional. The index of the subtitle stream to use. If omitted no subtitles will be used.</param>
  229. /// <param name="subtitleMethod">Optional. Specify the subtitle delivery method.</param>
  230. /// <param name="maxRefFrames">Optional.</param>
  231. /// <param name="maxVideoBitDepth">Optional. The maximum video bit depth.</param>
  232. /// <param name="requireAvc">Optional. Whether to require avc.</param>
  233. /// <param name="deInterlace">Optional. Whether to deinterlace the video.</param>
  234. /// <param name="requireNonAnamorphic">Optional. Whether to require a non anamporphic stream.</param>
  235. /// <param name="transcodingMaxAudioChannels">Optional. The maximum number of audio channels to transcode.</param>
  236. /// <param name="cpuCoreLimit">Optional. The limit of how many cpu cores to use.</param>
  237. /// <param name="liveStreamId">The live stream id.</param>
  238. /// <param name="enableMpegtsM2TsMode">Optional. Whether to enable the MpegtsM2Ts mode.</param>
  239. /// <param name="videoCodec">Optional. Specify a video codec to encode to, e.g. h264. If omitted the server will auto-select using the url's extension.</param>
  240. /// <param name="subtitleCodec">Optional. Specify a subtitle codec to encode to.</param>
  241. /// <param name="transcodeReasons">Optional. The transcoding reason.</param>
  242. /// <param name="audioStreamIndex">Optional. The index of the audio stream to use. If omitted the first audio stream will be used.</param>
  243. /// <param name="videoStreamIndex">Optional. The index of the video stream to use. If omitted the first video stream will be used.</param>
  244. /// <param name="context">Optional. The <see cref="EncodingContext"/>.</param>
  245. /// <param name="streamOptions">Optional. The streaming options.</param>
  246. /// <param name="enableAudioVbrEncoding">Optional. Whether to enable Audio Encoding.</param>
  247. /// <response code="200">Audio stream returned.</response>
  248. /// <returns>A <see cref="FileResult"/> containing the audio file.</returns>
  249. [HttpGet("{itemId}/stream.{container}", Name = "GetAudioStreamByContainer")]
  250. [HttpHead("{itemId}/stream.{container}", Name = "HeadAudioStreamByContainer")]
  251. [ProducesResponseType(StatusCodes.Status200OK)]
  252. [ProducesAudioFile]
  253. public async Task<ActionResult> GetAudioStreamByContainer(
  254. [FromRoute, Required] Guid itemId,
  255. [FromRoute, Required] string container,
  256. [FromQuery] bool? @static,
  257. [FromQuery] string? @params,
  258. [FromQuery] string? tag,
  259. [FromQuery, ParameterObsolete] string? deviceProfileId,
  260. [FromQuery] string? playSessionId,
  261. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? segmentContainer,
  262. [FromQuery] int? segmentLength,
  263. [FromQuery] int? minSegments,
  264. [FromQuery] string? mediaSourceId,
  265. [FromQuery] string? deviceId,
  266. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? audioCodec,
  267. [FromQuery] bool? enableAutoStreamCopy,
  268. [FromQuery] bool? allowVideoStreamCopy,
  269. [FromQuery] bool? allowAudioStreamCopy,
  270. [FromQuery] bool? breakOnNonKeyFrames,
  271. [FromQuery] int? audioSampleRate,
  272. [FromQuery] int? maxAudioBitDepth,
  273. [FromQuery] int? audioBitRate,
  274. [FromQuery] int? audioChannels,
  275. [FromQuery] int? maxAudioChannels,
  276. [FromQuery] string? profile,
  277. [FromQuery] string? level,
  278. [FromQuery] float? framerate,
  279. [FromQuery] float? maxFramerate,
  280. [FromQuery] bool? copyTimestamps,
  281. [FromQuery] long? startTimeTicks,
  282. [FromQuery] int? width,
  283. [FromQuery] int? height,
  284. [FromQuery] int? videoBitRate,
  285. [FromQuery] int? subtitleStreamIndex,
  286. [FromQuery] SubtitleDeliveryMethod? subtitleMethod,
  287. [FromQuery] int? maxRefFrames,
  288. [FromQuery] int? maxVideoBitDepth,
  289. [FromQuery] bool? requireAvc,
  290. [FromQuery] bool? deInterlace,
  291. [FromQuery] bool? requireNonAnamorphic,
  292. [FromQuery] int? transcodingMaxAudioChannels,
  293. [FromQuery] int? cpuCoreLimit,
  294. [FromQuery] string? liveStreamId,
  295. [FromQuery] bool? enableMpegtsM2TsMode,
  296. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? videoCodec,
  297. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? subtitleCodec,
  298. [FromQuery] string? transcodeReasons,
  299. [FromQuery] int? audioStreamIndex,
  300. [FromQuery] int? videoStreamIndex,
  301. [FromQuery] EncodingContext? context,
  302. [FromQuery] Dictionary<string, string>? streamOptions,
  303. [FromQuery] bool enableAudioVbrEncoding = true)
  304. {
  305. StreamingRequestDto streamingRequest = new StreamingRequestDto
  306. {
  307. Id = itemId,
  308. Container = container,
  309. Static = @static ?? false,
  310. Params = @params,
  311. Tag = tag,
  312. PlaySessionId = playSessionId,
  313. SegmentContainer = segmentContainer,
  314. SegmentLength = segmentLength,
  315. MinSegments = minSegments,
  316. MediaSourceId = mediaSourceId,
  317. DeviceId = deviceId,
  318. AudioCodec = audioCodec,
  319. EnableAutoStreamCopy = enableAutoStreamCopy ?? true,
  320. AllowAudioStreamCopy = allowAudioStreamCopy ?? true,
  321. AllowVideoStreamCopy = allowVideoStreamCopy ?? true,
  322. BreakOnNonKeyFrames = breakOnNonKeyFrames ?? false,
  323. AudioSampleRate = audioSampleRate,
  324. MaxAudioChannels = maxAudioChannels,
  325. AudioBitRate = audioBitRate,
  326. MaxAudioBitDepth = maxAudioBitDepth,
  327. AudioChannels = audioChannels,
  328. Profile = profile,
  329. Level = level,
  330. Framerate = framerate,
  331. MaxFramerate = maxFramerate,
  332. CopyTimestamps = copyTimestamps ?? false,
  333. StartTimeTicks = startTimeTicks,
  334. Width = width,
  335. Height = height,
  336. VideoBitRate = videoBitRate,
  337. SubtitleStreamIndex = subtitleStreamIndex,
  338. SubtitleMethod = subtitleMethod ?? SubtitleDeliveryMethod.Encode,
  339. MaxRefFrames = maxRefFrames,
  340. MaxVideoBitDepth = maxVideoBitDepth,
  341. RequireAvc = requireAvc ?? false,
  342. DeInterlace = deInterlace ?? false,
  343. RequireNonAnamorphic = requireNonAnamorphic ?? false,
  344. TranscodingMaxAudioChannels = transcodingMaxAudioChannels,
  345. CpuCoreLimit = cpuCoreLimit,
  346. LiveStreamId = liveStreamId,
  347. EnableMpegtsM2TsMode = enableMpegtsM2TsMode ?? false,
  348. VideoCodec = videoCodec,
  349. SubtitleCodec = subtitleCodec,
  350. TranscodeReasons = transcodeReasons,
  351. AudioStreamIndex = audioStreamIndex,
  352. VideoStreamIndex = videoStreamIndex,
  353. Context = context ?? EncodingContext.Static,
  354. StreamOptions = streamOptions,
  355. EnableAudioVbrEncoding = enableAudioVbrEncoding
  356. };
  357. return await _audioHelper.GetAudioStream(_transcodingJobType, streamingRequest).ConfigureAwait(false);
  358. }
  359. }