AudioController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 a audio codec to encode to, e.g. mp3. If omitted the server will auto-select using the url's extension. Options: aac, mp3, vorbis, wma.</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. Options: h265, h264, mpeg4, theora, vp8, vp9, vpx (deprecated), wmv.</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. /// <response code="200">Audio stream returned.</response>
  82. /// <returns>A <see cref="FileResult"/> containing the audio file.</returns>
  83. [HttpGet("{itemId}/stream", Name = "GetAudioStream")]
  84. [HttpHead("{itemId}/stream", Name = "HeadAudioStream")]
  85. [ProducesResponseType(StatusCodes.Status200OK)]
  86. [ProducesAudioFile]
  87. public async Task<ActionResult> GetAudioStream(
  88. [FromRoute, Required] Guid itemId,
  89. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? container,
  90. [FromQuery] bool? @static,
  91. [FromQuery] string? @params,
  92. [FromQuery] string? tag,
  93. [FromQuery, ParameterObsolete] string? deviceProfileId,
  94. [FromQuery] string? playSessionId,
  95. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? segmentContainer,
  96. [FromQuery] int? segmentLength,
  97. [FromQuery] int? minSegments,
  98. [FromQuery] string? mediaSourceId,
  99. [FromQuery] string? deviceId,
  100. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? audioCodec,
  101. [FromQuery] bool? enableAutoStreamCopy,
  102. [FromQuery] bool? allowVideoStreamCopy,
  103. [FromQuery] bool? allowAudioStreamCopy,
  104. [FromQuery] bool? breakOnNonKeyFrames,
  105. [FromQuery] int? audioSampleRate,
  106. [FromQuery] int? maxAudioBitDepth,
  107. [FromQuery] int? audioBitRate,
  108. [FromQuery] int? audioChannels,
  109. [FromQuery] int? maxAudioChannels,
  110. [FromQuery] string? profile,
  111. [FromQuery] string? level,
  112. [FromQuery] float? framerate,
  113. [FromQuery] float? maxFramerate,
  114. [FromQuery] bool? copyTimestamps,
  115. [FromQuery] long? startTimeTicks,
  116. [FromQuery] int? width,
  117. [FromQuery] int? height,
  118. [FromQuery] int? videoBitRate,
  119. [FromQuery] int? subtitleStreamIndex,
  120. [FromQuery] SubtitleDeliveryMethod? subtitleMethod,
  121. [FromQuery] int? maxRefFrames,
  122. [FromQuery] int? maxVideoBitDepth,
  123. [FromQuery] bool? requireAvc,
  124. [FromQuery] bool? deInterlace,
  125. [FromQuery] bool? requireNonAnamorphic,
  126. [FromQuery] int? transcodingMaxAudioChannels,
  127. [FromQuery] int? cpuCoreLimit,
  128. [FromQuery] string? liveStreamId,
  129. [FromQuery] bool? enableMpegtsM2TsMode,
  130. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? videoCodec,
  131. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? subtitleCodec,
  132. [FromQuery] string? transcodeReasons,
  133. [FromQuery] int? audioStreamIndex,
  134. [FromQuery] int? videoStreamIndex,
  135. [FromQuery] EncodingContext? context,
  136. [FromQuery] Dictionary<string, string>? streamOptions)
  137. {
  138. StreamingRequestDto streamingRequest = new StreamingRequestDto
  139. {
  140. Id = itemId,
  141. Container = container,
  142. Static = @static ?? false,
  143. Params = @params,
  144. Tag = tag,
  145. PlaySessionId = playSessionId,
  146. SegmentContainer = segmentContainer,
  147. SegmentLength = segmentLength,
  148. MinSegments = minSegments,
  149. MediaSourceId = mediaSourceId,
  150. DeviceId = deviceId,
  151. AudioCodec = audioCodec,
  152. EnableAutoStreamCopy = enableAutoStreamCopy ?? true,
  153. AllowAudioStreamCopy = allowAudioStreamCopy ?? true,
  154. AllowVideoStreamCopy = allowVideoStreamCopy ?? true,
  155. BreakOnNonKeyFrames = breakOnNonKeyFrames ?? false,
  156. AudioSampleRate = audioSampleRate,
  157. MaxAudioChannels = maxAudioChannels,
  158. AudioBitRate = audioBitRate,
  159. MaxAudioBitDepth = maxAudioBitDepth,
  160. AudioChannels = audioChannels,
  161. Profile = profile,
  162. Level = level,
  163. Framerate = framerate,
  164. MaxFramerate = maxFramerate,
  165. CopyTimestamps = copyTimestamps ?? false,
  166. StartTimeTicks = startTimeTicks,
  167. Width = width,
  168. Height = height,
  169. VideoBitRate = videoBitRate,
  170. SubtitleStreamIndex = subtitleStreamIndex,
  171. SubtitleMethod = subtitleMethod ?? SubtitleDeliveryMethod.Encode,
  172. MaxRefFrames = maxRefFrames,
  173. MaxVideoBitDepth = maxVideoBitDepth,
  174. RequireAvc = requireAvc ?? false,
  175. DeInterlace = deInterlace ?? false,
  176. RequireNonAnamorphic = requireNonAnamorphic ?? false,
  177. TranscodingMaxAudioChannels = transcodingMaxAudioChannels,
  178. CpuCoreLimit = cpuCoreLimit,
  179. LiveStreamId = liveStreamId,
  180. EnableMpegtsM2TsMode = enableMpegtsM2TsMode ?? false,
  181. VideoCodec = videoCodec,
  182. SubtitleCodec = subtitleCodec,
  183. TranscodeReasons = transcodeReasons,
  184. AudioStreamIndex = audioStreamIndex,
  185. VideoStreamIndex = videoStreamIndex,
  186. Context = context ?? EncodingContext.Static,
  187. StreamOptions = streamOptions
  188. };
  189. return await _audioHelper.GetAudioStream(_transcodingJobType, streamingRequest).ConfigureAwait(false);
  190. }
  191. /// <summary>
  192. /// Gets an audio stream.
  193. /// </summary>
  194. /// <param name="itemId">The item id.</param>
  195. /// <param name="container">The audio container.</param>
  196. /// <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>
  197. /// <param name="params">The streaming parameters.</param>
  198. /// <param name="tag">The tag.</param>
  199. /// <param name="deviceProfileId">Optional. The dlna device profile id to utilize.</param>
  200. /// <param name="playSessionId">The play session id.</param>
  201. /// <param name="segmentContainer">The segment container.</param>
  202. /// <param name="segmentLength">The segment length.</param>
  203. /// <param name="minSegments">The minimum number of segments.</param>
  204. /// <param name="mediaSourceId">The media version id, if playing an alternate version.</param>
  205. /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</param>
  206. /// <param name="audioCodec">Optional. Specify a audio codec to encode to, e.g. mp3. If omitted the server will auto-select using the url's extension. Options: aac, mp3, vorbis, wma.</param>
  207. /// <param name="enableAutoStreamCopy">Whether or not to allow automatic stream copy if requested values match the original source. Defaults to true.</param>
  208. /// <param name="allowVideoStreamCopy">Whether or not to allow copying of the video stream url.</param>
  209. /// <param name="allowAudioStreamCopy">Whether or not to allow copying of the audio stream url.</param>
  210. /// <param name="breakOnNonKeyFrames">Optional. Whether to break on non key frames.</param>
  211. /// <param name="audioSampleRate">Optional. Specify a specific audio sample rate, e.g. 44100.</param>
  212. /// <param name="maxAudioBitDepth">Optional. The maximum audio bit depth.</param>
  213. /// <param name="audioBitRate">Optional. Specify an audio bitrate to encode to, e.g. 128000. If omitted this will be left to encoder defaults.</param>
  214. /// <param name="audioChannels">Optional. Specify a specific number of audio channels to encode to, e.g. 2.</param>
  215. /// <param name="maxAudioChannels">Optional. Specify a maximum number of audio channels to encode to, e.g. 2.</param>
  216. /// <param name="profile">Optional. Specify a specific an encoder profile (varies by encoder), e.g. main, baseline, high.</param>
  217. /// <param name="level">Optional. Specify a level for the encoder profile (varies by encoder), e.g. 3, 3.1.</param>
  218. /// <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>
  219. /// <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>
  220. /// <param name="copyTimestamps">Whether or not to copy timestamps when transcoding with an offset. Defaults to false.</param>
  221. /// <param name="startTimeTicks">Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms.</param>
  222. /// <param name="width">Optional. The fixed horizontal resolution of the encoded video.</param>
  223. /// <param name="height">Optional. The fixed vertical resolution of the encoded video.</param>
  224. /// <param name="videoBitRate">Optional. Specify a video bitrate to encode to, e.g. 500000. If omitted this will be left to encoder defaults.</param>
  225. /// <param name="subtitleStreamIndex">Optional. The index of the subtitle stream to use. If omitted no subtitles will be used.</param>
  226. /// <param name="subtitleMethod">Optional. Specify the subtitle delivery method.</param>
  227. /// <param name="maxRefFrames">Optional.</param>
  228. /// <param name="maxVideoBitDepth">Optional. The maximum video bit depth.</param>
  229. /// <param name="requireAvc">Optional. Whether to require avc.</param>
  230. /// <param name="deInterlace">Optional. Whether to deinterlace the video.</param>
  231. /// <param name="requireNonAnamorphic">Optional. Whether to require a non anamporphic stream.</param>
  232. /// <param name="transcodingMaxAudioChannels">Optional. The maximum number of audio channels to transcode.</param>
  233. /// <param name="cpuCoreLimit">Optional. The limit of how many cpu cores to use.</param>
  234. /// <param name="liveStreamId">The live stream id.</param>
  235. /// <param name="enableMpegtsM2TsMode">Optional. Whether to enable the MpegtsM2Ts mode.</param>
  236. /// <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. Options: h265, h264, mpeg4, theora, vp8, vp9, vpx (deprecated), wmv.</param>
  237. /// <param name="subtitleCodec">Optional. Specify a subtitle codec to encode to.</param>
  238. /// <param name="transcodeReasons">Optional. The transcoding reason.</param>
  239. /// <param name="audioStreamIndex">Optional. The index of the audio stream to use. If omitted the first audio stream will be used.</param>
  240. /// <param name="videoStreamIndex">Optional. The index of the video stream to use. If omitted the first video stream will be used.</param>
  241. /// <param name="context">Optional. The <see cref="EncodingContext"/>.</param>
  242. /// <param name="streamOptions">Optional. The streaming options.</param>
  243. /// <response code="200">Audio stream returned.</response>
  244. /// <returns>A <see cref="FileResult"/> containing the audio file.</returns>
  245. [HttpGet("{itemId}/stream.{container}", Name = "GetAudioStreamByContainer")]
  246. [HttpHead("{itemId}/stream.{container}", Name = "HeadAudioStreamByContainer")]
  247. [ProducesResponseType(StatusCodes.Status200OK)]
  248. [ProducesAudioFile]
  249. public async Task<ActionResult> GetAudioStreamByContainer(
  250. [FromRoute, Required] Guid itemId,
  251. [FromRoute, Required] string container,
  252. [FromQuery] bool? @static,
  253. [FromQuery] string? @params,
  254. [FromQuery] string? tag,
  255. [FromQuery, ParameterObsolete] string? deviceProfileId,
  256. [FromQuery] string? playSessionId,
  257. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? segmentContainer,
  258. [FromQuery] int? segmentLength,
  259. [FromQuery] int? minSegments,
  260. [FromQuery] string? mediaSourceId,
  261. [FromQuery] string? deviceId,
  262. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? audioCodec,
  263. [FromQuery] bool? enableAutoStreamCopy,
  264. [FromQuery] bool? allowVideoStreamCopy,
  265. [FromQuery] bool? allowAudioStreamCopy,
  266. [FromQuery] bool? breakOnNonKeyFrames,
  267. [FromQuery] int? audioSampleRate,
  268. [FromQuery] int? maxAudioBitDepth,
  269. [FromQuery] int? audioBitRate,
  270. [FromQuery] int? audioChannels,
  271. [FromQuery] int? maxAudioChannels,
  272. [FromQuery] string? profile,
  273. [FromQuery] string? level,
  274. [FromQuery] float? framerate,
  275. [FromQuery] float? maxFramerate,
  276. [FromQuery] bool? copyTimestamps,
  277. [FromQuery] long? startTimeTicks,
  278. [FromQuery] int? width,
  279. [FromQuery] int? height,
  280. [FromQuery] int? videoBitRate,
  281. [FromQuery] int? subtitleStreamIndex,
  282. [FromQuery] SubtitleDeliveryMethod? subtitleMethod,
  283. [FromQuery] int? maxRefFrames,
  284. [FromQuery] int? maxVideoBitDepth,
  285. [FromQuery] bool? requireAvc,
  286. [FromQuery] bool? deInterlace,
  287. [FromQuery] bool? requireNonAnamorphic,
  288. [FromQuery] int? transcodingMaxAudioChannels,
  289. [FromQuery] int? cpuCoreLimit,
  290. [FromQuery] string? liveStreamId,
  291. [FromQuery] bool? enableMpegtsM2TsMode,
  292. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? videoCodec,
  293. [FromQuery] [RegularExpression(EncodingHelper.ValidationRegex)] string? subtitleCodec,
  294. [FromQuery] string? transcodeReasons,
  295. [FromQuery] int? audioStreamIndex,
  296. [FromQuery] int? videoStreamIndex,
  297. [FromQuery] EncodingContext? context,
  298. [FromQuery] Dictionary<string, string>? streamOptions)
  299. {
  300. StreamingRequestDto streamingRequest = new StreamingRequestDto
  301. {
  302. Id = itemId,
  303. Container = container,
  304. Static = @static ?? false,
  305. Params = @params,
  306. Tag = tag,
  307. PlaySessionId = playSessionId,
  308. SegmentContainer = segmentContainer,
  309. SegmentLength = segmentLength,
  310. MinSegments = minSegments,
  311. MediaSourceId = mediaSourceId,
  312. DeviceId = deviceId,
  313. AudioCodec = audioCodec,
  314. EnableAutoStreamCopy = enableAutoStreamCopy ?? true,
  315. AllowAudioStreamCopy = allowAudioStreamCopy ?? true,
  316. AllowVideoStreamCopy = allowVideoStreamCopy ?? true,
  317. BreakOnNonKeyFrames = breakOnNonKeyFrames ?? false,
  318. AudioSampleRate = audioSampleRate,
  319. MaxAudioChannels = maxAudioChannels,
  320. AudioBitRate = audioBitRate,
  321. MaxAudioBitDepth = maxAudioBitDepth,
  322. AudioChannels = audioChannels,
  323. Profile = profile,
  324. Level = level,
  325. Framerate = framerate,
  326. MaxFramerate = maxFramerate,
  327. CopyTimestamps = copyTimestamps ?? false,
  328. StartTimeTicks = startTimeTicks,
  329. Width = width,
  330. Height = height,
  331. VideoBitRate = videoBitRate,
  332. SubtitleStreamIndex = subtitleStreamIndex,
  333. SubtitleMethod = subtitleMethod ?? SubtitleDeliveryMethod.Encode,
  334. MaxRefFrames = maxRefFrames,
  335. MaxVideoBitDepth = maxVideoBitDepth,
  336. RequireAvc = requireAvc ?? false,
  337. DeInterlace = deInterlace ?? false,
  338. RequireNonAnamorphic = requireNonAnamorphic ?? false,
  339. TranscodingMaxAudioChannels = transcodingMaxAudioChannels,
  340. CpuCoreLimit = cpuCoreLimit,
  341. LiveStreamId = liveStreamId,
  342. EnableMpegtsM2TsMode = enableMpegtsM2TsMode ?? false,
  343. VideoCodec = videoCodec,
  344. SubtitleCodec = subtitleCodec,
  345. TranscodeReasons = transcodeReasons,
  346. AudioStreamIndex = audioStreamIndex,
  347. VideoStreamIndex = videoStreamIndex,
  348. Context = context ?? EncodingContext.Static,
  349. StreamOptions = streamOptions
  350. };
  351. return await _audioHelper.GetAudioStream(_transcodingJobType, streamingRequest).ConfigureAwait(false);
  352. }
  353. }