AudioController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Jellyfin.Api.Helpers;
  5. using Jellyfin.Api.Models.StreamingDtos;
  6. using MediaBrowser.Controller.MediaEncoding;
  7. using MediaBrowser.Model.Dlna;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Mvc;
  10. namespace Jellyfin.Api.Controllers
  11. {
  12. /// <summary>
  13. /// The audio controller.
  14. /// </summary>
  15. // TODO: In order to authenticate this in the future, Dlna playback will require updating
  16. public class AudioController : BaseJellyfinApiController
  17. {
  18. private readonly AudioHelper _audioHelper;
  19. private readonly TranscodingJobType _transcodingJobType = TranscodingJobType.Progressive;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="AudioController"/> class.
  22. /// </summary>
  23. /// <param name="audioHelper">Instance of <see cref="AudioHelper"/>.</param>
  24. public AudioController(AudioHelper audioHelper)
  25. {
  26. _audioHelper = audioHelper;
  27. }
  28. /// <summary>
  29. /// Gets an audio stream.
  30. /// </summary>
  31. /// <param name="itemId">The item id.</param>
  32. /// <param name="container">The audio container.</param>
  33. /// <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>
  34. /// <param name="params">The streaming parameters.</param>
  35. /// <param name="tag">The tag.</param>
  36. /// <param name="deviceProfileId">Optional. The dlna device profile id to utilize.</param>
  37. /// <param name="playSessionId">The play session id.</param>
  38. /// <param name="segmentContainer">The segment container.</param>
  39. /// <param name="segmentLength">The segment lenght.</param>
  40. /// <param name="minSegments">The minimum number of segments.</param>
  41. /// <param name="mediaSourceId">The media version id, if playing an alternate version.</param>
  42. /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</param>
  43. /// <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>
  44. /// <param name="enableAutoStreamCopy">Whether or not to allow automatic stream copy if requested values match the original source. Defaults to true.</param>
  45. /// <param name="allowVideoStreamCopy">Whether or not to allow copying of the video stream url.</param>
  46. /// <param name="allowAudioStreamCopy">Whether or not to allow copying of the audio stream url.</param>
  47. /// <param name="breakOnNonKeyFrames">Optional. Whether to break on non key frames.</param>
  48. /// <param name="audioSampleRate">Optional. Specify a specific audio sample rate, e.g. 44100.</param>
  49. /// <param name="maxAudioBitDepth">Optional. The maximum audio bit depth.</param>
  50. /// <param name="audioBitRate">Optional. Specify an audio bitrate to encode to, e.g. 128000. If omitted this will be left to encoder defaults.</param>
  51. /// <param name="audioChannels">Optional. Specify a specific number of audio channels to encode to, e.g. 2.</param>
  52. /// <param name="maxAudioChannels">Optional. Specify a maximum number of audio channels to encode to, e.g. 2.</param>
  53. /// <param name="profile">Optional. Specify a specific an encoder profile (varies by encoder), e.g. main, baseline, high.</param>
  54. /// <param name="level">Optional. Specify a level for the encoder profile (varies by encoder), e.g. 3, 3.1.</param>
  55. /// <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>
  56. /// <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>
  57. /// <param name="copyTimestamps">Whether or not to copy timestamps when transcoding with an offset. Defaults to false.</param>
  58. /// <param name="startTimeTicks">Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms.</param>
  59. /// <param name="width">Optional. The fixed horizontal resolution of the encoded video.</param>
  60. /// <param name="height">Optional. The fixed vertical resolution of the encoded video.</param>
  61. /// <param name="videoBitRate">Optional. Specify a video bitrate to encode to, e.g. 500000. If omitted this will be left to encoder defaults.</param>
  62. /// <param name="subtitleStreamIndex">Optional. The index of the subtitle stream to use. If omitted no subtitles will be used.</param>
  63. /// <param name="subtitleMethod">Optional. Specify the subtitle delivery method.</param>
  64. /// <param name="maxRefFrames">Optional.</param>
  65. /// <param name="maxVideoBitDepth">Optional. The maximum video bit depth.</param>
  66. /// <param name="requireAvc">Optional. Whether to require avc.</param>
  67. /// <param name="deInterlace">Optional. Whether to deinterlace the video.</param>
  68. /// <param name="requireNonAnamorphic">Optional. Whether to require a non anamporphic stream.</param>
  69. /// <param name="transcodingMaxAudioChannels">Optional. The maximum number of audio channels to transcode.</param>
  70. /// <param name="cpuCoreLimit">Optional. The limit of how many cpu cores to use.</param>
  71. /// <param name="liveStreamId">The live stream id.</param>
  72. /// <param name="enableMpegtsM2TsMode">Optional. Whether to enable the MpegtsM2Ts mode.</param>
  73. /// <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>
  74. /// <param name="subtitleCodec">Optional. Specify a subtitle codec to encode to.</param>
  75. /// <param name="transcodingReasons">Optional. The transcoding reason.</param>
  76. /// <param name="audioStreamIndex">Optional. The index of the audio stream to use. If omitted the first audio stream will be used.</param>
  77. /// <param name="videoStreamIndex">Optional. The index of the video stream to use. If omitted the first video stream will be used.</param>
  78. /// <param name="context">Optional. The <see cref="EncodingContext"/>.</param>
  79. /// <param name="streamOptions">Optional. The streaming options.</param>
  80. /// <response code="200">Audio stream returned.</response>
  81. /// <returns>A <see cref="FileResult"/> containing the audio file.</returns>
  82. [HttpGet("{itemId}/stream.{container}", Name = "GetAudioStreamByContainer")]
  83. [HttpGet("{itemId}/stream", Name = "GetAudioStream")]
  84. [HttpHead("{itemId}/stream.{container}", Name = "HeadAudioStreamByContainer")]
  85. [HttpHead("{itemId}/stream", Name = "HeadAudioStream")]
  86. [ProducesResponseType(StatusCodes.Status200OK)]
  87. public async Task<ActionResult> GetAudioStream(
  88. [FromRoute] Guid itemId,
  89. [FromRoute] 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? transcodingReasons,
  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 ?? true,
  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 ?? true,
  167. StartTimeTicks = startTimeTicks,
  168. Width = width,
  169. Height = height,
  170. VideoBitRate = videoBitRate,
  171. SubtitleStreamIndex = subtitleStreamIndex,
  172. SubtitleMethod = subtitleMethod,
  173. MaxRefFrames = maxRefFrames,
  174. MaxVideoBitDepth = maxVideoBitDepth,
  175. RequireAvc = requireAvc ?? true,
  176. DeInterlace = deInterlace ?? true,
  177. RequireNonAnamorphic = requireNonAnamorphic ?? true,
  178. TranscodingMaxAudioChannels = transcodingMaxAudioChannels,
  179. CpuCoreLimit = cpuCoreLimit,
  180. LiveStreamId = liveStreamId,
  181. EnableMpegtsM2TsMode = enableMpegtsM2TsMode ?? true,
  182. VideoCodec = videoCodec,
  183. SubtitleCodec = subtitleCodec,
  184. TranscodeReasons = transcodingReasons,
  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. }
  193. }