2
0

AudioController.cs 21 KB

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