< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.AudioController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/AudioController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 3
Coverable lines: 3
Total lines: 367
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/AudioController.cs

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