AudioController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. {
  14. /// <summary>
  15. /// The audio controller.
  16. /// </summary>
  17. // TODO: In order to authenticate this in the future, Dlna playback will require updating
  18. public class AudioController : BaseJellyfinApiController
  19. {
  20. private readonly AudioHelper _audioHelper;
  21. private readonly TranscodingJobType _transcodingJobType = TranscodingJobType.Progressive;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="AudioController"/> class.
  24. /// </summary>
  25. /// <param name="audioHelper">Instance of <see cref="AudioHelper"/>.</param>
  26. public AudioController(AudioHelper audioHelper)
  27. {
  28. _audioHelper = audioHelper;
  29. }
  30. /// <summary>
  31. /// Gets an audio stream.
  32. /// </summary>
  33. /// <param name="itemId">The item id.</param>
  34. /// <param name="container">The audio container.</param>
  35. /// <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>
  36. /// <param name="params">The streaming parameters.</param>
  37. /// <param name="tag">The tag.</param>
  38. /// <param name="deviceProfileId">Optional. The dlna device profile id to utilize.</param>
  39. /// <param name="playSessionId">The play session id.</param>
  40. /// <param name="segmentContainer">The segment container.</param>
  41. /// <param name="segmentLength">The segment lenght.</param>
  42. /// <param name="minSegments">The minimum number of segments.</param>
  43. /// <param name="mediaSourceId">The media version id, if playing an alternate version.</param>
  44. /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</param>
  45. /// <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>
  46. /// <param name="enableAutoStreamCopy">Whether or not to allow automatic stream copy if requested values match the original source. Defaults to true.</param>
  47. /// <param name="allowVideoStreamCopy">Whether or not to allow copying of the video stream url.</param>
  48. /// <param name="allowAudioStreamCopy">Whether or not to allow copying of the audio stream url.</param>
  49. /// <param name="breakOnNonKeyFrames">Optional. Whether to break on non key frames.</param>
  50. /// <param name="audioSampleRate">Optional. Specify a specific audio sample rate, e.g. 44100.</param>
  51. /// <param name="maxAudioBitDepth">Optional. The maximum audio bit depth.</param>
  52. /// <param name="audioBitRate">Optional. Specify an audio bitrate to encode to, e.g. 128000. If omitted this will be left to encoder defaults.</param>
  53. /// <param name="audioChannels">Optional. Specify a specific number of audio channels to encode to, e.g. 2.</param>
  54. /// <param name="maxAudioChannels">Optional. Specify a maximum number of audio channels to encode to, e.g. 2.</param>
  55. /// <param name="profile">Optional. Specify a specific an encoder profile (varies by encoder), e.g. main, baseline, high.</param>
  56. /// <param name="level">Optional. Specify a level for the encoder profile (varies by encoder), e.g. 3, 3.1.</param>
  57. /// <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>
  58. /// <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>
  59. /// <param name="copyTimestamps">Whether or not to copy timestamps when transcoding with an offset. Defaults to false.</param>
  60. /// <param name="startTimeTicks">Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms.</param>
  61. /// <param name="width">Optional. The fixed horizontal resolution of the encoded video.</param>
  62. /// <param name="height">Optional. The fixed vertical resolution of the encoded video.</param>
  63. /// <param name="videoBitRate">Optional. Specify a video bitrate to encode to, e.g. 500000. If omitted this will be left to encoder defaults.</param>
  64. /// <param name="subtitleStreamIndex">Optional. The index of the subtitle stream to use. If omitted no subtitles will be used.</param>
  65. /// <param name="subtitleMethod">Optional. Specify the subtitle delivery method.</param>
  66. /// <param name="maxRefFrames">Optional.</param>
  67. /// <param name="maxVideoBitDepth">Optional. The maximum video bit depth.</param>
  68. /// <param name="requireAvc">Optional. Whether to require avc.</param>
  69. /// <param name="deInterlace">Optional. Whether to deinterlace the video.</param>
  70. /// <param name="requireNonAnamorphic">Optional. Whether to require a non anamporphic stream.</param>
  71. /// <param name="transcodingMaxAudioChannels">Optional. The maximum number of audio channels to transcode.</param>
  72. /// <param name="cpuCoreLimit">Optional. The limit of how many cpu cores to use.</param>
  73. /// <param name="liveStreamId">The live stream id.</param>
  74. /// <param name="enableMpegtsM2TsMode">Optional. Whether to enable the MpegtsM2Ts mode.</param>
  75. /// <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, vpx, wmv.</param>
  76. /// <param name="subtitleCodec">Optional. Specify a subtitle codec to encode to.</param>
  77. /// <param name="transcodingReasons">Optional. The transcoding reason.</param>
  78. /// <param name="audioStreamIndex">Optional. The index of the audio stream to use. If omitted the first audio stream will be used.</param>
  79. /// <param name="videoStreamIndex">Optional. The index of the video stream to use. If omitted the first video stream will be used.</param>
  80. /// <param name="context">Optional. The <see cref="EncodingContext"/>.</param>
  81. /// <param name="streamOptions">Optional. The streaming options.</param>
  82. /// <response code="200">Audio stream returned.</response>
  83. /// <returns>A <see cref="FileResult"/> containing the audio file.</returns>
  84. [HttpGet("{itemId}/stream.{container:required}", Name = "GetAudioStreamByContainer")]
  85. [HttpGet("{itemId}/stream", Name = "GetAudioStream")]
  86. [HttpHead("{itemId}/stream.{container:required}", Name = "HeadAudioStreamByContainer")]
  87. [HttpHead("{itemId}/stream", Name = "HeadAudioStream")]
  88. [ProducesResponseType(StatusCodes.Status200OK)]
  89. [ProducesAudioFile]
  90. public async Task<ActionResult> GetAudioStream(
  91. [FromRoute, Required] Guid itemId,
  92. [FromRoute] string? container,
  93. [FromQuery] bool? @static,
  94. [FromQuery] string? @params,
  95. [FromQuery] string? tag,
  96. [FromQuery] string? deviceProfileId,
  97. [FromQuery] string? playSessionId,
  98. [FromQuery] string? segmentContainer,
  99. [FromQuery] int? segmentLength,
  100. [FromQuery] int? minSegments,
  101. [FromQuery] string? mediaSourceId,
  102. [FromQuery] string? deviceId,
  103. [FromQuery] string? audioCodec,
  104. [FromQuery] bool? enableAutoStreamCopy,
  105. [FromQuery] bool? allowVideoStreamCopy,
  106. [FromQuery] bool? allowAudioStreamCopy,
  107. [FromQuery] bool? breakOnNonKeyFrames,
  108. [FromQuery] int? audioSampleRate,
  109. [FromQuery] int? maxAudioBitDepth,
  110. [FromQuery] int? audioBitRate,
  111. [FromQuery] int? audioChannels,
  112. [FromQuery] int? maxAudioChannels,
  113. [FromQuery] string? profile,
  114. [FromQuery] string? level,
  115. [FromQuery] float? framerate,
  116. [FromQuery] float? maxFramerate,
  117. [FromQuery] bool? copyTimestamps,
  118. [FromQuery] long? startTimeTicks,
  119. [FromQuery] int? width,
  120. [FromQuery] int? height,
  121. [FromQuery] int? videoBitRate,
  122. [FromQuery] int? subtitleStreamIndex,
  123. [FromQuery] SubtitleDeliveryMethod subtitleMethod,
  124. [FromQuery] int? maxRefFrames,
  125. [FromQuery] int? maxVideoBitDepth,
  126. [FromQuery] bool? requireAvc,
  127. [FromQuery] bool? deInterlace,
  128. [FromQuery] bool? requireNonAnamorphic,
  129. [FromQuery] int? transcodingMaxAudioChannels,
  130. [FromQuery] int? cpuCoreLimit,
  131. [FromQuery] string? liveStreamId,
  132. [FromQuery] bool? enableMpegtsM2TsMode,
  133. [FromQuery] string? videoCodec,
  134. [FromQuery] string? subtitleCodec,
  135. [FromQuery] string? transcodingReasons,
  136. [FromQuery] int? audioStreamIndex,
  137. [FromQuery] int? videoStreamIndex,
  138. [FromQuery] EncodingContext? context,
  139. [FromQuery] Dictionary<string, string>? streamOptions)
  140. {
  141. StreamingRequestDto streamingRequest = new StreamingRequestDto
  142. {
  143. Id = itemId,
  144. Container = container,
  145. Static = @static ?? true,
  146. Params = @params,
  147. Tag = tag,
  148. DeviceProfileId = deviceProfileId,
  149. PlaySessionId = playSessionId,
  150. SegmentContainer = segmentContainer,
  151. SegmentLength = segmentLength,
  152. MinSegments = minSegments,
  153. MediaSourceId = mediaSourceId,
  154. DeviceId = deviceId,
  155. AudioCodec = audioCodec,
  156. EnableAutoStreamCopy = enableAutoStreamCopy ?? true,
  157. AllowAudioStreamCopy = allowAudioStreamCopy ?? true,
  158. AllowVideoStreamCopy = allowVideoStreamCopy ?? true,
  159. BreakOnNonKeyFrames = breakOnNonKeyFrames ?? false,
  160. AudioSampleRate = audioSampleRate,
  161. MaxAudioChannels = maxAudioChannels,
  162. AudioBitRate = audioBitRate,
  163. MaxAudioBitDepth = maxAudioBitDepth,
  164. AudioChannels = audioChannels,
  165. Profile = profile,
  166. Level = level,
  167. Framerate = framerate,
  168. MaxFramerate = maxFramerate,
  169. CopyTimestamps = copyTimestamps ?? true,
  170. StartTimeTicks = startTimeTicks,
  171. Width = width,
  172. Height = height,
  173. VideoBitRate = videoBitRate,
  174. SubtitleStreamIndex = subtitleStreamIndex,
  175. SubtitleMethod = subtitleMethod,
  176. MaxRefFrames = maxRefFrames,
  177. MaxVideoBitDepth = maxVideoBitDepth,
  178. RequireAvc = requireAvc ?? true,
  179. DeInterlace = deInterlace ?? true,
  180. RequireNonAnamorphic = requireNonAnamorphic ?? true,
  181. TranscodingMaxAudioChannels = transcodingMaxAudioChannels,
  182. CpuCoreLimit = cpuCoreLimit,
  183. LiveStreamId = liveStreamId,
  184. EnableMpegtsM2TsMode = enableMpegtsM2TsMode ?? true,
  185. VideoCodec = videoCodec,
  186. SubtitleCodec = subtitleCodec,
  187. TranscodeReasons = transcodingReasons,
  188. AudioStreamIndex = audioStreamIndex,
  189. VideoStreamIndex = videoStreamIndex,
  190. Context = context ?? EncodingContext.Static,
  191. StreamOptions = streamOptions
  192. };
  193. return await _audioHelper.GetAudioStream(_transcodingJobType, streamingRequest).ConfigureAwait(false);
  194. }
  195. }
  196. }