| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel.DataAnnotations; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Net.Http; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Jellyfin.Api.Attributes; |
| | | 10 | | using Jellyfin.Api.Extensions; |
| | | 11 | | using Jellyfin.Api.Helpers; |
| | | 12 | | using Jellyfin.Api.ModelBinders; |
| | | 13 | | using Jellyfin.Extensions; |
| | | 14 | | using MediaBrowser.Common.Api; |
| | | 15 | | using MediaBrowser.Common.Configuration; |
| | | 16 | | using MediaBrowser.Common.Net; |
| | | 17 | | using MediaBrowser.Controller.Configuration; |
| | | 18 | | using MediaBrowser.Controller.Dto; |
| | | 19 | | using MediaBrowser.Controller.Entities; |
| | | 20 | | using MediaBrowser.Controller.Library; |
| | | 21 | | using MediaBrowser.Controller.MediaEncoding; |
| | | 22 | | using MediaBrowser.Controller.Streaming; |
| | | 23 | | using MediaBrowser.Model.Dlna; |
| | | 24 | | using MediaBrowser.Model.Dto; |
| | | 25 | | using MediaBrowser.Model.Entities; |
| | | 26 | | using MediaBrowser.Model.MediaInfo; |
| | | 27 | | using MediaBrowser.Model.Net; |
| | | 28 | | using MediaBrowser.Model.Querying; |
| | | 29 | | using Microsoft.AspNetCore.Authorization; |
| | | 30 | | using Microsoft.AspNetCore.Http; |
| | | 31 | | using Microsoft.AspNetCore.Mvc; |
| | | 32 | | |
| | | 33 | | namespace Jellyfin.Api.Controllers; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The videos controller. |
| | | 37 | | /// </summary> |
| | | 38 | | public class VideosController : BaseJellyfinApiController |
| | | 39 | | { |
| | | 40 | | private readonly ILibraryManager _libraryManager; |
| | | 41 | | private readonly IUserManager _userManager; |
| | | 42 | | private readonly IDtoService _dtoService; |
| | | 43 | | private readonly IMediaSourceManager _mediaSourceManager; |
| | | 44 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | | 45 | | private readonly IMediaEncoder _mediaEncoder; |
| | | 46 | | private readonly ITranscodeManager _transcodeManager; |
| | | 47 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 48 | | private readonly EncodingHelper _encodingHelper; |
| | | 49 | | |
| | | 50 | | private readonly TranscodingJobType _transcodingJobType = TranscodingJobType.Progressive; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Initializes a new instance of the <see cref="VideosController"/> class. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | | 56 | | /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param> |
| | | 57 | | /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param> |
| | | 58 | | /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param> |
| | | 59 | | /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</p |
| | | 60 | | /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param> |
| | | 61 | | /// <param name="transcodeManager">Instance of the <see cref="ITranscodeManager"/> interface.</param> |
| | | 62 | | /// <param name="httpClientFactory">Instance of the <see cref="IHttpClientFactory"/> interface.</param> |
| | | 63 | | /// <param name="encodingHelper">Instance of <see cref="EncodingHelper"/>.</param> |
| | 0 | 64 | | public VideosController( |
| | 0 | 65 | | ILibraryManager libraryManager, |
| | 0 | 66 | | IUserManager userManager, |
| | 0 | 67 | | IDtoService dtoService, |
| | 0 | 68 | | IMediaSourceManager mediaSourceManager, |
| | 0 | 69 | | IServerConfigurationManager serverConfigurationManager, |
| | 0 | 70 | | IMediaEncoder mediaEncoder, |
| | 0 | 71 | | ITranscodeManager transcodeManager, |
| | 0 | 72 | | IHttpClientFactory httpClientFactory, |
| | 0 | 73 | | EncodingHelper encodingHelper) |
| | | 74 | | { |
| | 0 | 75 | | _libraryManager = libraryManager; |
| | 0 | 76 | | _userManager = userManager; |
| | 0 | 77 | | _dtoService = dtoService; |
| | 0 | 78 | | _mediaSourceManager = mediaSourceManager; |
| | 0 | 79 | | _serverConfigurationManager = serverConfigurationManager; |
| | 0 | 80 | | _mediaEncoder = mediaEncoder; |
| | 0 | 81 | | _transcodeManager = transcodeManager; |
| | 0 | 82 | | _httpClientFactory = httpClientFactory; |
| | 0 | 83 | | _encodingHelper = encodingHelper; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets additional parts for a video. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="itemId">The item id.</param> |
| | | 90 | | /// <param name="userId">Optional. Filter by user id, and attach user data.</param> |
| | | 91 | | /// <response code="200">Additional parts returned.</response> |
| | | 92 | | /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the parts.</returns> |
| | | 93 | | [HttpGet("{itemId}/AdditionalParts")] |
| | | 94 | | [Authorize] |
| | | 95 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 96 | | public ActionResult<QueryResult<BaseItemDto>> GetAdditionalPart([FromRoute, Required] Guid itemId, [FromQuery] Guid? |
| | | 97 | | { |
| | 0 | 98 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 0 | 99 | | var user = userId.IsNullOrEmpty() |
| | 0 | 100 | | ? null |
| | 0 | 101 | | : _userManager.GetUserById(userId.Value); |
| | | 102 | | |
| | 0 | 103 | | var item = itemId.IsEmpty() |
| | 0 | 104 | | ? (userId.IsNullOrEmpty() |
| | 0 | 105 | | ? _libraryManager.RootFolder |
| | 0 | 106 | | : _libraryManager.GetUserRootFolder()) |
| | 0 | 107 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 0 | 108 | | if (item is null) |
| | | 109 | | { |
| | 0 | 110 | | return NotFound(); |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | var dtoOptions = new DtoOptions(); |
| | 0 | 114 | | dtoOptions = dtoOptions.AddClientFields(User); |
| | | 115 | | |
| | | 116 | | BaseItemDto[] items; |
| | 0 | 117 | | if (item is Video video) |
| | | 118 | | { |
| | 0 | 119 | | items = video.GetAdditionalParts() |
| | 0 | 120 | | .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, video)) |
| | 0 | 121 | | .ToArray(); |
| | | 122 | | } |
| | | 123 | | else |
| | | 124 | | { |
| | 0 | 125 | | items = Array.Empty<BaseItemDto>(); |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | var result = new QueryResult<BaseItemDto>(items); |
| | 0 | 129 | | return result; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Removes alternate video sources. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="itemId">The item id.</param> |
| | | 136 | | /// <response code="204">Alternate sources deleted.</response> |
| | | 137 | | /// <response code="404">Video not found.</response> |
| | | 138 | | /// <returns>A <see cref="NoContentResult"/> indicating success, or a <see cref="NotFoundResult"/> if the video does |
| | | 139 | | [HttpDelete("{itemId}/AlternateSources")] |
| | | 140 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 141 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 142 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 143 | | public async Task<ActionResult> DeleteAlternateSources([FromRoute, Required] Guid itemId) |
| | | 144 | | { |
| | | 145 | | var item = _libraryManager.GetItemById<Video>(itemId, User.GetUserId()); |
| | | 146 | | if (item is null) |
| | | 147 | | { |
| | | 148 | | return NotFound(); |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | if (item.LinkedAlternateVersions.Length == 0) |
| | | 152 | | { |
| | | 153 | | item = _libraryManager.GetItemById<Video>(Guid.Parse(item.PrimaryVersionId)); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | if (item is null) |
| | | 157 | | { |
| | | 158 | | return NotFound(); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | foreach (var link in item.GetLinkedAlternateVersions()) |
| | | 162 | | { |
| | | 163 | | link.SetPrimaryVersionId(null); |
| | | 164 | | link.LinkedAlternateVersions = Array.Empty<LinkedChild>(); |
| | | 165 | | |
| | | 166 | | await link.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | item.LinkedAlternateVersions = Array.Empty<LinkedChild>(); |
| | | 170 | | item.SetPrimaryVersionId(null); |
| | | 171 | | await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false); |
| | | 172 | | |
| | | 173 | | return NoContent(); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Merges videos into a single record. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <param name="ids">Item id list. This allows multiple, comma delimited.</param> |
| | | 180 | | /// <response code="204">Videos merged.</response> |
| | | 181 | | /// <response code="400">Supply at least 2 video ids.</response> |
| | | 182 | | /// <returns>A <see cref="NoContentResult"/> indicating success, or a <see cref="BadRequestResult"/> if less than tw |
| | | 183 | | [HttpPost("MergeVersions")] |
| | | 184 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 185 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 186 | | [ProducesResponseType(StatusCodes.Status400BadRequest)] |
| | | 187 | | public async Task<ActionResult> MergeVersions([FromQuery, Required, ModelBinder(typeof(CommaDelimitedCollectionModel |
| | | 188 | | { |
| | | 189 | | var userId = User.GetUserId(); |
| | | 190 | | var items = ids |
| | | 191 | | .Select(i => _libraryManager.GetItemById<BaseItem>(i, userId)) |
| | | 192 | | .OfType<Video>() |
| | | 193 | | .OrderBy(i => i.Id) |
| | | 194 | | .ToList(); |
| | | 195 | | |
| | | 196 | | if (items.Count < 2) |
| | | 197 | | { |
| | | 198 | | return BadRequest("Please supply at least two videos to merge."); |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | var primaryVersion = items.FirstOrDefault(i => i.MediaSourceCount > 1 && string.IsNullOrEmpty(i.PrimaryVersionId |
| | | 202 | | if (primaryVersion is null) |
| | | 203 | | { |
| | | 204 | | primaryVersion = items |
| | | 205 | | .OrderBy(i => |
| | | 206 | | { |
| | | 207 | | if (i.Video3DFormat.HasValue || i.VideoType != VideoType.VideoFile) |
| | | 208 | | { |
| | | 209 | | return 1; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | return 0; |
| | | 213 | | }) |
| | | 214 | | .ThenByDescending(i => i.GetDefaultVideoStream()?.Width ?? 0) |
| | | 215 | | .First(); |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | var alternateVersionsOfPrimary = primaryVersion.LinkedAlternateVersions.ToList(); |
| | | 219 | | |
| | | 220 | | foreach (var item in items.Where(i => !i.Id.Equals(primaryVersion.Id))) |
| | | 221 | | { |
| | | 222 | | item.SetPrimaryVersionId(primaryVersion.Id.ToString("N", CultureInfo.InvariantCulture)); |
| | | 223 | | |
| | | 224 | | await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false |
| | | 225 | | |
| | | 226 | | if (!alternateVersionsOfPrimary.Any(i => string.Equals(i.Path, item.Path, StringComparison.OrdinalIgnoreCase |
| | | 227 | | { |
| | | 228 | | alternateVersionsOfPrimary.Add(new LinkedChild |
| | | 229 | | { |
| | | 230 | | Path = item.Path, |
| | | 231 | | ItemId = item.Id |
| | | 232 | | }); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | foreach (var linkedItem in item.LinkedAlternateVersions) |
| | | 236 | | { |
| | | 237 | | if (!alternateVersionsOfPrimary.Any(i => string.Equals(i.Path, linkedItem.Path, StringComparison.Ordinal |
| | | 238 | | { |
| | | 239 | | alternateVersionsOfPrimary.Add(linkedItem); |
| | | 240 | | } |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | if (item.LinkedAlternateVersions.Length > 0) |
| | | 244 | | { |
| | | 245 | | item.LinkedAlternateVersions = Array.Empty<LinkedChild>(); |
| | | 246 | | await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(f |
| | | 247 | | } |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | primaryVersion.LinkedAlternateVersions = alternateVersionsOfPrimary.ToArray(); |
| | | 251 | | await primaryVersion.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait |
| | | 252 | | return NoContent(); |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Gets a video stream. |
| | | 257 | | /// </summary> |
| | | 258 | | /// <param name="itemId">The item id.</param> |
| | | 259 | | /// <param name="container">The video container. Possible values are: ts, webm, asf, wmv, ogv, mp4, m4v, mkv, mpeg, |
| | | 260 | | /// <param name="static">Optional. If true, the original file will be streamed statically without any encoding. Use |
| | | 261 | | /// <param name="params">The streaming parameters.</param> |
| | | 262 | | /// <param name="tag">The tag.</param> |
| | | 263 | | /// <param name="deviceProfileId">Optional. The dlna device profile id to utilize.</param> |
| | | 264 | | /// <param name="playSessionId">The play session id.</param> |
| | | 265 | | /// <param name="segmentContainer">The segment container.</param> |
| | | 266 | | /// <param name="segmentLength">The segment length.</param> |
| | | 267 | | /// <param name="minSegments">The minimum number of segments.</param> |
| | | 268 | | /// <param name="mediaSourceId">The media version id, if playing an alternate version.</param> |
| | | 269 | | /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</par |
| | | 270 | | /// <param name="audioCodec">Optional. Specify an audio codec to encode to, e.g. mp3. If omitted the server will aut |
| | | 271 | | /// <param name="enableAutoStreamCopy">Whether or not to allow automatic stream copy if requested values match the o |
| | | 272 | | /// <param name="allowVideoStreamCopy">Whether or not to allow copying of the video stream url.</param> |
| | | 273 | | /// <param name="allowAudioStreamCopy">Whether or not to allow copying of the audio stream url.</param> |
| | | 274 | | /// <param name="breakOnNonKeyFrames">Optional. Whether to break on non key frames.</param> |
| | | 275 | | /// <param name="audioSampleRate">Optional. Specify a specific audio sample rate, e.g. 44100.</param> |
| | | 276 | | /// <param name="maxAudioBitDepth">Optional. The maximum audio bit depth.</param> |
| | | 277 | | /// <param name="audioBitRate">Optional. Specify an audio bitrate to encode to, e.g. 128000. If omitted this will be |
| | | 278 | | /// <param name="audioChannels">Optional. Specify a specific number of audio channels to encode to, e.g. 2.</param> |
| | | 279 | | /// <param name="maxAudioChannels">Optional. Specify a maximum number of audio channels to encode to, e.g. 2.</param |
| | | 280 | | /// <param name="profile">Optional. Specify a specific an encoder profile (varies by encoder), e.g. main, baseline, |
| | | 281 | | /// <param name="level">Optional. Specify a level for the encoder profile (varies by encoder), e.g. 3, 3.1.</param> |
| | | 282 | | /// <param name="framerate">Optional. A specific video framerate to encode to, e.g. 23.976. Generally this should be |
| | | 283 | | /// <param name="maxFramerate">Optional. A specific maximum video framerate to encode to, e.g. 23.976. Generally thi |
| | | 284 | | /// <param name="copyTimestamps">Whether or not to copy timestamps when transcoding with an offset. Defaults to fals |
| | | 285 | | /// <param name="startTimeTicks">Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms.</param> |
| | | 286 | | /// <param name="width">Optional. The fixed horizontal resolution of the encoded video.</param> |
| | | 287 | | /// <param name="height">Optional. The fixed vertical resolution of the encoded video.</param> |
| | | 288 | | /// <param name="maxWidth">Optional. The maximum horizontal resolution of the encoded video.</param> |
| | | 289 | | /// <param name="maxHeight">Optional. The maximum vertical resolution of the encoded video.</param> |
| | | 290 | | /// <param name="videoBitRate">Optional. Specify a video bitrate to encode to, e.g. 500000. If omitted this will be |
| | | 291 | | /// <param name="subtitleStreamIndex">Optional. The index of the subtitle stream to use. If omitted no subtitles wil |
| | | 292 | | /// <param name="subtitleMethod">Optional. Specify the subtitle delivery method.</param> |
| | | 293 | | /// <param name="maxRefFrames">Optional.</param> |
| | | 294 | | /// <param name="maxVideoBitDepth">Optional. The maximum video bit depth.</param> |
| | | 295 | | /// <param name="requireAvc">Optional. Whether to require avc.</param> |
| | | 296 | | /// <param name="deInterlace">Optional. Whether to deinterlace the video.</param> |
| | | 297 | | /// <param name="requireNonAnamorphic">Optional. Whether to require a non anamorphic stream.</param> |
| | | 298 | | /// <param name="transcodingMaxAudioChannels">Optional. The maximum number of audio channels to transcode.</param> |
| | | 299 | | /// <param name="cpuCoreLimit">Optional. The limit of how many cpu cores to use.</param> |
| | | 300 | | /// <param name="liveStreamId">The live stream id.</param> |
| | | 301 | | /// <param name="enableMpegtsM2TsMode">Optional. Whether to enable the MpegtsM2Ts mode.</param> |
| | | 302 | | /// <param name="videoCodec">Optional. Specify a video codec to encode to, e.g. h264. If omitted the server will aut |
| | | 303 | | /// <param name="subtitleCodec">Optional. Specify a subtitle codec to encode to.</param> |
| | | 304 | | /// <param name="transcodeReasons">Optional. The transcoding reason.</param> |
| | | 305 | | /// <param name="audioStreamIndex">Optional. The index of the audio stream to use. If omitted the first audio stream |
| | | 306 | | /// <param name="videoStreamIndex">Optional. The index of the video stream to use. If omitted the first video stream |
| | | 307 | | /// <param name="context">Optional. The <see cref="EncodingContext"/>.</param> |
| | | 308 | | /// <param name="streamOptions">Optional. The streaming options.</param> |
| | | 309 | | /// <param name="enableAudioVbrEncoding">Optional. Whether to enable Audio Encoding.</param> |
| | | 310 | | /// <response code="200">Video stream returned.</response> |
| | | 311 | | /// <returns>A <see cref="FileResult"/> containing the audio file.</returns> |
| | | 312 | | [HttpGet("{itemId}/stream")] |
| | | 313 | | [HttpHead("{itemId}/stream", Name = "HeadVideoStream")] |
| | | 314 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 315 | | [ProducesVideoFile] |
| | | 316 | | public async Task<ActionResult> GetVideoStream( |
| | | 317 | | [FromRoute, Required] Guid itemId, |
| | | 318 | | [FromQuery] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string? container, |
| | | 319 | | [FromQuery] bool? @static, |
| | | 320 | | [FromQuery] string? @params, |
| | | 321 | | [FromQuery] string? tag, |
| | | 322 | | [FromQuery, ParameterObsolete] string? deviceProfileId, |
| | | 323 | | [FromQuery] string? playSessionId, |
| | | 324 | | [FromQuery] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string? segmentContainer, |
| | | 325 | | [FromQuery] int? segmentLength, |
| | | 326 | | [FromQuery] int? minSegments, |
| | | 327 | | [FromQuery] string? mediaSourceId, |
| | | 328 | | [FromQuery] string? deviceId, |
| | | 329 | | [FromQuery] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string? audioCodec, |
| | | 330 | | [FromQuery] bool? enableAutoStreamCopy, |
| | | 331 | | [FromQuery] bool? allowVideoStreamCopy, |
| | | 332 | | [FromQuery] bool? allowAudioStreamCopy, |
| | | 333 | | [FromQuery] bool? breakOnNonKeyFrames, |
| | | 334 | | [FromQuery] int? audioSampleRate, |
| | | 335 | | [FromQuery] int? maxAudioBitDepth, |
| | | 336 | | [FromQuery] int? audioBitRate, |
| | | 337 | | [FromQuery] int? audioChannels, |
| | | 338 | | [FromQuery] int? maxAudioChannels, |
| | | 339 | | [FromQuery] string? profile, |
| | | 340 | | [FromQuery] [RegularExpression(EncodingHelper.LevelValidationRegex)] string? level, |
| | | 341 | | [FromQuery] float? framerate, |
| | | 342 | | [FromQuery] float? maxFramerate, |
| | | 343 | | [FromQuery] bool? copyTimestamps, |
| | | 344 | | [FromQuery] long? startTimeTicks, |
| | | 345 | | [FromQuery] int? width, |
| | | 346 | | [FromQuery] int? height, |
| | | 347 | | [FromQuery] int? maxWidth, |
| | | 348 | | [FromQuery] int? maxHeight, |
| | | 349 | | [FromQuery] int? videoBitRate, |
| | | 350 | | [FromQuery] int? subtitleStreamIndex, |
| | | 351 | | [FromQuery] SubtitleDeliveryMethod? subtitleMethod, |
| | | 352 | | [FromQuery] int? maxRefFrames, |
| | | 353 | | [FromQuery] int? maxVideoBitDepth, |
| | | 354 | | [FromQuery] bool? requireAvc, |
| | | 355 | | [FromQuery] bool? deInterlace, |
| | | 356 | | [FromQuery] bool? requireNonAnamorphic, |
| | | 357 | | [FromQuery] int? transcodingMaxAudioChannels, |
| | | 358 | | [FromQuery] int? cpuCoreLimit, |
| | | 359 | | [FromQuery] string? liveStreamId, |
| | | 360 | | [FromQuery] bool? enableMpegtsM2TsMode, |
| | | 361 | | [FromQuery] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string? videoCodec, |
| | | 362 | | [FromQuery] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string? subtitleCodec, |
| | | 363 | | [FromQuery] string? transcodeReasons, |
| | | 364 | | [FromQuery] int? audioStreamIndex, |
| | | 365 | | [FromQuery] int? videoStreamIndex, |
| | | 366 | | [FromQuery] EncodingContext? context, |
| | | 367 | | [FromQuery] Dictionary<string, string> streamOptions, |
| | | 368 | | [FromQuery] bool enableAudioVbrEncoding = true) |
| | | 369 | | { |
| | | 370 | | var isHeadRequest = Request.Method == System.Net.WebRequestMethods.Http.Head; |
| | | 371 | | // CTS lifecycle is managed internally. |
| | | 372 | | var cancellationTokenSource = new CancellationTokenSource(); |
| | | 373 | | var streamingRequest = new VideoRequestDto |
| | | 374 | | { |
| | | 375 | | Id = itemId, |
| | | 376 | | Container = container, |
| | | 377 | | Static = @static ?? false, |
| | | 378 | | Params = @params, |
| | | 379 | | Tag = tag, |
| | | 380 | | PlaySessionId = playSessionId, |
| | | 381 | | SegmentContainer = segmentContainer, |
| | | 382 | | SegmentLength = segmentLength, |
| | | 383 | | MinSegments = minSegments, |
| | | 384 | | MediaSourceId = mediaSourceId, |
| | | 385 | | DeviceId = deviceId, |
| | | 386 | | AudioCodec = audioCodec, |
| | | 387 | | EnableAutoStreamCopy = enableAutoStreamCopy ?? true, |
| | | 388 | | AllowAudioStreamCopy = allowAudioStreamCopy ?? true, |
| | | 389 | | AllowVideoStreamCopy = allowVideoStreamCopy ?? true, |
| | | 390 | | BreakOnNonKeyFrames = breakOnNonKeyFrames ?? false, |
| | | 391 | | AudioSampleRate = audioSampleRate, |
| | | 392 | | MaxAudioChannels = maxAudioChannels, |
| | | 393 | | AudioBitRate = audioBitRate, |
| | | 394 | | MaxAudioBitDepth = maxAudioBitDepth, |
| | | 395 | | AudioChannels = audioChannels, |
| | | 396 | | Profile = profile, |
| | | 397 | | Level = level, |
| | | 398 | | Framerate = framerate, |
| | | 399 | | MaxFramerate = maxFramerate, |
| | | 400 | | CopyTimestamps = copyTimestamps ?? false, |
| | | 401 | | StartTimeTicks = startTimeTicks, |
| | | 402 | | Width = width, |
| | | 403 | | Height = height, |
| | | 404 | | MaxWidth = maxWidth, |
| | | 405 | | MaxHeight = maxHeight, |
| | | 406 | | VideoBitRate = videoBitRate, |
| | | 407 | | SubtitleStreamIndex = subtitleStreamIndex, |
| | | 408 | | SubtitleMethod = subtitleMethod ?? SubtitleDeliveryMethod.Encode, |
| | | 409 | | MaxRefFrames = maxRefFrames, |
| | | 410 | | MaxVideoBitDepth = maxVideoBitDepth, |
| | | 411 | | RequireAvc = requireAvc ?? false, |
| | | 412 | | DeInterlace = deInterlace ?? false, |
| | | 413 | | RequireNonAnamorphic = requireNonAnamorphic ?? false, |
| | | 414 | | TranscodingMaxAudioChannels = transcodingMaxAudioChannels, |
| | | 415 | | CpuCoreLimit = cpuCoreLimit, |
| | | 416 | | LiveStreamId = liveStreamId, |
| | | 417 | | EnableMpegtsM2TsMode = enableMpegtsM2TsMode ?? false, |
| | | 418 | | VideoCodec = videoCodec, |
| | | 419 | | SubtitleCodec = subtitleCodec, |
| | | 420 | | TranscodeReasons = transcodeReasons, |
| | | 421 | | AudioStreamIndex = audioStreamIndex, |
| | | 422 | | VideoStreamIndex = videoStreamIndex, |
| | | 423 | | Context = context ?? EncodingContext.Streaming, |
| | | 424 | | StreamOptions = streamOptions, |
| | | 425 | | EnableAudioVbrEncoding = enableAudioVbrEncoding |
| | | 426 | | }; |
| | | 427 | | |
| | | 428 | | var state = await StreamingHelpers.GetStreamingState( |
| | | 429 | | streamingRequest, |
| | | 430 | | HttpContext, |
| | | 431 | | _mediaSourceManager, |
| | | 432 | | _userManager, |
| | | 433 | | _libraryManager, |
| | | 434 | | _serverConfigurationManager, |
| | | 435 | | _mediaEncoder, |
| | | 436 | | _encodingHelper, |
| | | 437 | | _transcodeManager, |
| | | 438 | | _transcodingJobType, |
| | | 439 | | cancellationTokenSource.Token) |
| | | 440 | | .ConfigureAwait(false); |
| | | 441 | | |
| | | 442 | | if (@static.HasValue && @static.Value && state.DirectStreamProvider is not null) |
| | | 443 | | { |
| | | 444 | | var liveStreamInfo = _mediaSourceManager.GetLiveStreamInfo(streamingRequest.LiveStreamId); |
| | | 445 | | if (liveStreamInfo is null) |
| | | 446 | | { |
| | | 447 | | return NotFound(); |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | var liveStream = new ProgressiveFileStream(liveStreamInfo.GetStream()); |
| | | 451 | | // TODO (moved from MediaBrowser.Api): Don't hardcode contentType |
| | | 452 | | return File(liveStream, MimeTypes.GetMimeType("file.ts")); |
| | | 453 | | } |
| | | 454 | | |
| | | 455 | | // Static remote stream |
| | | 456 | | if (@static.HasValue && @static.Value && state.InputProtocol == MediaProtocol.Http) |
| | | 457 | | { |
| | | 458 | | var httpClient = _httpClientFactory.CreateClient(NamedClient.Default); |
| | | 459 | | return await FileStreamResponseHelpers.GetStaticRemoteStreamResult(state, httpClient, HttpContext).Configure |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | if (@static.HasValue && @static.Value && state.InputProtocol != MediaProtocol.File) |
| | | 463 | | { |
| | | 464 | | return BadRequest($"Input protocol {state.InputProtocol} cannot be streamed statically"); |
| | | 465 | | } |
| | | 466 | | |
| | | 467 | | // Static stream |
| | | 468 | | if (@static.HasValue && @static.Value && !(state.MediaSource.VideoType == VideoType.BluRay || state.MediaSource. |
| | | 469 | | { |
| | | 470 | | var contentType = state.GetMimeType("." + state.OutputContainer, false) ?? state.GetMimeType(state.MediaPath |
| | | 471 | | |
| | | 472 | | if (state.MediaSource.IsInfiniteStream) |
| | | 473 | | { |
| | | 474 | | var liveStream = new ProgressiveFileStream(state.MediaPath, null, _transcodeManager); |
| | | 475 | | return File(liveStream, contentType); |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | return FileStreamResponseHelpers.GetStaticFileResult( |
| | | 479 | | state.MediaPath, |
| | | 480 | | contentType); |
| | | 481 | | } |
| | | 482 | | |
| | | 483 | | // Need to start ffmpeg (because media can't be returned directly) |
| | | 484 | | var encodingOptions = _serverConfigurationManager.GetEncodingOptions(); |
| | | 485 | | var ffmpegCommandLineArguments = _encodingHelper.GetProgressiveVideoFullCommandLine(state, encodingOptions, Enco |
| | | 486 | | return await FileStreamResponseHelpers.GetTranscodedFile( |
| | | 487 | | state, |
| | | 488 | | isHeadRequest, |
| | | 489 | | HttpContext, |
| | | 490 | | _transcodeManager, |
| | | 491 | | ffmpegCommandLineArguments, |
| | | 492 | | _transcodingJobType, |
| | | 493 | | cancellationTokenSource).ConfigureAwait(false); |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | /// <summary> |
| | | 497 | | /// Gets a video stream. |
| | | 498 | | /// </summary> |
| | | 499 | | /// <param name="itemId">The item id.</param> |
| | | 500 | | /// <param name="container">The video container. Possible values are: ts, webm, asf, wmv, ogv, mp4, m4v, mkv, mpeg, |
| | | 501 | | /// <param name="static">Optional. If true, the original file will be streamed statically without any encoding. Use |
| | | 502 | | /// <param name="params">The streaming parameters.</param> |
| | | 503 | | /// <param name="tag">The tag.</param> |
| | | 504 | | /// <param name="deviceProfileId">Optional. The dlna device profile id to utilize.</param> |
| | | 505 | | /// <param name="playSessionId">The play session id.</param> |
| | | 506 | | /// <param name="segmentContainer">The segment container.</param> |
| | | 507 | | /// <param name="segmentLength">The segment length.</param> |
| | | 508 | | /// <param name="minSegments">The minimum number of segments.</param> |
| | | 509 | | /// <param name="mediaSourceId">The media version id, if playing an alternate version.</param> |
| | | 510 | | /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</par |
| | | 511 | | /// <param name="audioCodec">Optional. Specify an audio codec to encode to, e.g. mp3. If omitted the server will aut |
| | | 512 | | /// <param name="enableAutoStreamCopy">Whether or not to allow automatic stream copy if requested values match the o |
| | | 513 | | /// <param name="allowVideoStreamCopy">Whether or not to allow copying of the video stream url.</param> |
| | | 514 | | /// <param name="allowAudioStreamCopy">Whether or not to allow copying of the audio stream url.</param> |
| | | 515 | | /// <param name="breakOnNonKeyFrames">Optional. Whether to break on non key frames.</param> |
| | | 516 | | /// <param name="audioSampleRate">Optional. Specify a specific audio sample rate, e.g. 44100.</param> |
| | | 517 | | /// <param name="maxAudioBitDepth">Optional. The maximum audio bit depth.</param> |
| | | 518 | | /// <param name="audioBitRate">Optional. Specify an audio bitrate to encode to, e.g. 128000. If omitted this will be |
| | | 519 | | /// <param name="audioChannels">Optional. Specify a specific number of audio channels to encode to, e.g. 2.</param> |
| | | 520 | | /// <param name="maxAudioChannels">Optional. Specify a maximum number of audio channels to encode to, e.g. 2.</param |
| | | 521 | | /// <param name="profile">Optional. Specify a specific an encoder profile (varies by encoder), e.g. main, baseline, |
| | | 522 | | /// <param name="level">Optional. Specify a level for the encoder profile (varies by encoder), e.g. 3, 3.1.</param> |
| | | 523 | | /// <param name="framerate">Optional. A specific video framerate to encode to, e.g. 23.976. Generally this should be |
| | | 524 | | /// <param name="maxFramerate">Optional. A specific maximum video framerate to encode to, e.g. 23.976. Generally thi |
| | | 525 | | /// <param name="copyTimestamps">Whether or not to copy timestamps when transcoding with an offset. Defaults to fals |
| | | 526 | | /// <param name="startTimeTicks">Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms.</param> |
| | | 527 | | /// <param name="width">Optional. The fixed horizontal resolution of the encoded video.</param> |
| | | 528 | | /// <param name="height">Optional. The fixed vertical resolution of the encoded video.</param> |
| | | 529 | | /// <param name="maxWidth">Optional. The maximum horizontal resolution of the encoded video.</param> |
| | | 530 | | /// <param name="maxHeight">Optional. The maximum vertical resolution of the encoded video.</param> |
| | | 531 | | /// <param name="videoBitRate">Optional. Specify a video bitrate to encode to, e.g. 500000. If omitted this will be |
| | | 532 | | /// <param name="subtitleStreamIndex">Optional. The index of the subtitle stream to use. If omitted no subtitles wil |
| | | 533 | | /// <param name="subtitleMethod">Optional. Specify the subtitle delivery method.</param> |
| | | 534 | | /// <param name="maxRefFrames">Optional.</param> |
| | | 535 | | /// <param name="maxVideoBitDepth">Optional. The maximum video bit depth.</param> |
| | | 536 | | /// <param name="requireAvc">Optional. Whether to require avc.</param> |
| | | 537 | | /// <param name="deInterlace">Optional. Whether to deinterlace the video.</param> |
| | | 538 | | /// <param name="requireNonAnamorphic">Optional. Whether to require a non anamorphic stream.</param> |
| | | 539 | | /// <param name="transcodingMaxAudioChannels">Optional. The maximum number of audio channels to transcode.</param> |
| | | 540 | | /// <param name="cpuCoreLimit">Optional. The limit of how many cpu cores to use.</param> |
| | | 541 | | /// <param name="liveStreamId">The live stream id.</param> |
| | | 542 | | /// <param name="enableMpegtsM2TsMode">Optional. Whether to enable the MpegtsM2Ts mode.</param> |
| | | 543 | | /// <param name="videoCodec">Optional. Specify a video codec to encode to, e.g. h264. If omitted the server will aut |
| | | 544 | | /// <param name="subtitleCodec">Optional. Specify a subtitle codec to encode to.</param> |
| | | 545 | | /// <param name="transcodeReasons">Optional. The transcoding reason.</param> |
| | | 546 | | /// <param name="audioStreamIndex">Optional. The index of the audio stream to use. If omitted the first audio stream |
| | | 547 | | /// <param name="videoStreamIndex">Optional. The index of the video stream to use. If omitted the first video stream |
| | | 548 | | /// <param name="context">Optional. The <see cref="EncodingContext"/>.</param> |
| | | 549 | | /// <param name="streamOptions">Optional. The streaming options.</param> |
| | | 550 | | /// <param name="enableAudioVbrEncoding">Optional. Whether to enable Audio Encoding.</param> |
| | | 551 | | /// <response code="200">Video stream returned.</response> |
| | | 552 | | /// <returns>A <see cref="FileResult"/> containing the audio file.</returns> |
| | | 553 | | [HttpGet("{itemId}/stream.{container}")] |
| | | 554 | | [HttpHead("{itemId}/stream.{container}", Name = "HeadVideoStreamByContainer")] |
| | | 555 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 556 | | [ProducesVideoFile] |
| | | 557 | | public Task<ActionResult> GetVideoStreamByContainer( |
| | | 558 | | [FromRoute, Required] Guid itemId, |
| | | 559 | | [FromRoute, Required] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string container, |
| | | 560 | | [FromQuery] bool? @static, |
| | | 561 | | [FromQuery] string? @params, |
| | | 562 | | [FromQuery] string? tag, |
| | | 563 | | [FromQuery] string? deviceProfileId, |
| | | 564 | | [FromQuery] string? playSessionId, |
| | | 565 | | [FromQuery] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string? segmentContainer, |
| | | 566 | | [FromQuery] int? segmentLength, |
| | | 567 | | [FromQuery] int? minSegments, |
| | | 568 | | [FromQuery] string? mediaSourceId, |
| | | 569 | | [FromQuery] string? deviceId, |
| | | 570 | | [FromQuery] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string? audioCodec, |
| | | 571 | | [FromQuery] bool? enableAutoStreamCopy, |
| | | 572 | | [FromQuery] bool? allowVideoStreamCopy, |
| | | 573 | | [FromQuery] bool? allowAudioStreamCopy, |
| | | 574 | | [FromQuery] bool? breakOnNonKeyFrames, |
| | | 575 | | [FromQuery] int? audioSampleRate, |
| | | 576 | | [FromQuery] int? maxAudioBitDepth, |
| | | 577 | | [FromQuery] int? audioBitRate, |
| | | 578 | | [FromQuery] int? audioChannels, |
| | | 579 | | [FromQuery] int? maxAudioChannels, |
| | | 580 | | [FromQuery] string? profile, |
| | | 581 | | [FromQuery] [RegularExpression(EncodingHelper.LevelValidationRegex)] string? level, |
| | | 582 | | [FromQuery] float? framerate, |
| | | 583 | | [FromQuery] float? maxFramerate, |
| | | 584 | | [FromQuery] bool? copyTimestamps, |
| | | 585 | | [FromQuery] long? startTimeTicks, |
| | | 586 | | [FromQuery] int? width, |
| | | 587 | | [FromQuery] int? height, |
| | | 588 | | [FromQuery] int? maxWidth, |
| | | 589 | | [FromQuery] int? maxHeight, |
| | | 590 | | [FromQuery] int? videoBitRate, |
| | | 591 | | [FromQuery] int? subtitleStreamIndex, |
| | | 592 | | [FromQuery] SubtitleDeliveryMethod? subtitleMethod, |
| | | 593 | | [FromQuery] int? maxRefFrames, |
| | | 594 | | [FromQuery] int? maxVideoBitDepth, |
| | | 595 | | [FromQuery] bool? requireAvc, |
| | | 596 | | [FromQuery] bool? deInterlace, |
| | | 597 | | [FromQuery] bool? requireNonAnamorphic, |
| | | 598 | | [FromQuery] int? transcodingMaxAudioChannels, |
| | | 599 | | [FromQuery] int? cpuCoreLimit, |
| | | 600 | | [FromQuery] string? liveStreamId, |
| | | 601 | | [FromQuery] bool? enableMpegtsM2TsMode, |
| | | 602 | | [FromQuery] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string? videoCodec, |
| | | 603 | | [FromQuery] [RegularExpression(EncodingHelper.ContainerValidationRegex)] string? subtitleCodec, |
| | | 604 | | [FromQuery] string? transcodeReasons, |
| | | 605 | | [FromQuery] int? audioStreamIndex, |
| | | 606 | | [FromQuery] int? videoStreamIndex, |
| | | 607 | | [FromQuery] EncodingContext? context, |
| | | 608 | | [FromQuery] Dictionary<string, string> streamOptions, |
| | | 609 | | [FromQuery] bool enableAudioVbrEncoding = true) |
| | | 610 | | { |
| | 0 | 611 | | return GetVideoStream( |
| | 0 | 612 | | itemId, |
| | 0 | 613 | | container, |
| | 0 | 614 | | @static, |
| | 0 | 615 | | @params, |
| | 0 | 616 | | tag, |
| | 0 | 617 | | deviceProfileId, |
| | 0 | 618 | | playSessionId, |
| | 0 | 619 | | segmentContainer, |
| | 0 | 620 | | segmentLength, |
| | 0 | 621 | | minSegments, |
| | 0 | 622 | | mediaSourceId, |
| | 0 | 623 | | deviceId, |
| | 0 | 624 | | audioCodec, |
| | 0 | 625 | | enableAutoStreamCopy, |
| | 0 | 626 | | allowVideoStreamCopy, |
| | 0 | 627 | | allowAudioStreamCopy, |
| | 0 | 628 | | breakOnNonKeyFrames, |
| | 0 | 629 | | audioSampleRate, |
| | 0 | 630 | | maxAudioBitDepth, |
| | 0 | 631 | | audioBitRate, |
| | 0 | 632 | | audioChannels, |
| | 0 | 633 | | maxAudioChannels, |
| | 0 | 634 | | profile, |
| | 0 | 635 | | level, |
| | 0 | 636 | | framerate, |
| | 0 | 637 | | maxFramerate, |
| | 0 | 638 | | copyTimestamps, |
| | 0 | 639 | | startTimeTicks, |
| | 0 | 640 | | width, |
| | 0 | 641 | | height, |
| | 0 | 642 | | maxWidth, |
| | 0 | 643 | | maxHeight, |
| | 0 | 644 | | videoBitRate, |
| | 0 | 645 | | subtitleStreamIndex, |
| | 0 | 646 | | subtitleMethod, |
| | 0 | 647 | | maxRefFrames, |
| | 0 | 648 | | maxVideoBitDepth, |
| | 0 | 649 | | requireAvc, |
| | 0 | 650 | | deInterlace, |
| | 0 | 651 | | requireNonAnamorphic, |
| | 0 | 652 | | transcodingMaxAudioChannels, |
| | 0 | 653 | | cpuCoreLimit, |
| | 0 | 654 | | liveStreamId, |
| | 0 | 655 | | enableMpegtsM2TsMode, |
| | 0 | 656 | | videoCodec, |
| | 0 | 657 | | subtitleCodec, |
| | 0 | 658 | | transcodeReasons, |
| | 0 | 659 | | audioStreamIndex, |
| | 0 | 660 | | videoStreamIndex, |
| | 0 | 661 | | context, |
| | 0 | 662 | | streamOptions, |
| | 0 | 663 | | enableAudioVbrEncoding); |
| | | 664 | | } |
| | | 665 | | } |