/// <summary> /// Does the preparation to start a stream /// </summary> internal static async Task <StreamContext> StartOriginalFileStreamingAsync(EndPointSettings client, StreamItem newStreamItem) { if (STREAM_ITEMS.TryAdd(client.ClientId, newStreamItem)) { await newStreamItem.BusyLock.WaitAsync(); try { IMediaAccessor mediaAccessor = ServiceRegistration.Get <IMediaAccessor>(); List <IResourceAccessor> resources = new List <IResourceAccessor>(); foreach (var res in newStreamItem.TranscoderObject.Metadata.FilePaths) { var path = ResourcePath.Deserialize(res.Value); if (mediaAccessor.LocalResourceProviders.TryGetValue(path.BasePathSegment.ProviderId, out var resourceProvider) && resourceProvider is IBaseResourceProvider baseProvider && baseProvider.TryCreateResourceAccessor(path.BasePathSegment.Path, out var accessor)) { using (accessor) { if (accessor is IFileSystemResourceAccessor) { newStreamItem.TranscoderObject.StartStreaming(); newStreamItem.StreamContext = await MediaConverter.GetFileStreamAsync(path); return(newStreamItem.StreamContext); } } } } } finally { newStreamItem.BusyLock.Release(); } } return(null); }
/// <summary> /// Returns a DLNA media item based on the given client and request /// </summary> /// <returns>Returns the requested DLNA media item otherwise null</returns> internal static StreamItem GetNewStreamItem(EndPointSettings client, Uri uri) { if (STREAM_ITEMS.TryGetValue(client.ClientId, out var currentStreamItem)) { return(currentStreamItem); } currentStreamItem = new StreamItem(uri.Host); Guid mediaItemGuid = Guid.Empty; DlnaMediaItem dlnaItem = null; int channel = 0; if (DlnaResourceAccessUtils.ParseMediaItem(uri, out mediaItemGuid)) { if (mediaItemGuid == Guid.Empty) { throw new InvalidOperationException(string.Format("Illegal request syntax. Correct syntax is '{0}'", DlnaResourceAccessUtils.SYNTAX)); } if (!client.DlnaMediaItems.TryGetValue(mediaItemGuid, out dlnaItem)) { // Attempt to grab the media item from the database. MediaItem item = MediaLibraryHelper.GetMediaItem(mediaItemGuid); if (item == null) { throw new Exception(string.Format("Media item '{0}' not found.", mediaItemGuid)); } dlnaItem = client.GetDlnaItem(item); } if (dlnaItem == null) { throw new Exception(string.Format("DLNA media item '{0}' not found.", mediaItemGuid)); } } else if (DlnaResourceAccessUtils.ParseTVChannel(uri, out channel)) { dlnaItem = client.GetLiveDlnaItem(channel); if (dlnaItem == null) { throw new Exception(string.Format("DLNA TV channel '{0}' was never tuned.", channel)); } } else if (DlnaResourceAccessUtils.ParseRadioChannel(uri, out channel)) { dlnaItem = client.GetLiveDlnaItem(channel); if (dlnaItem == null) { throw new Exception(string.Format("DLNA radio channel '{0}' was never tuned.", channel)); } } currentStreamItem.RequestedMediaItem = mediaItemGuid; currentStreamItem.TranscoderObject = dlnaItem; currentStreamItem.Title = dlnaItem?.MediaItemTitle; currentStreamItem.LiveChannelId = channel > 0 ? channel : 0; return(currentStreamItem); }
/// <summary> /// Does the preparation to start a transcode stream /// </summary> internal static async Task <TranscodeContext> StartTranscodeStreamingAsync(EndPointSettings client, double startTime, double lengthTime, StreamItem newStreamItem) { if (STREAM_ITEMS.TryAdd(client.ClientId, newStreamItem)) { await newStreamItem.BusyLock.WaitAsync(); try { if (newStreamItem?.TranscoderObject?.TranscodingParameter == null) { STREAM_ITEMS.TryRemove(client.ClientId, out _); return(null); } if (!newStreamItem.TranscoderObject.StartTrancoding()) { Logger.Debug("StreamControl: Transcoding busy for mediaitem {0}", newStreamItem.RequestedMediaItem); return(null); } if (newStreamItem.IsLive) { newStreamItem.StreamContext = await MediaConverter.GetLiveStreamAsync(client.ClientId.ToString(), newStreamItem.TranscoderObject.TranscodingParameter, newStreamItem.LiveChannelId, true); } else { newStreamItem.StreamContext = await MediaConverter.GetMediaStreamAsync(client.ClientId.ToString(), newStreamItem.TranscoderObject.TranscodingParameter, startTime, lengthTime, true); } if (newStreamItem.StreamContext is TranscodeContext context) { context.UpdateStreamUse(true); return(context); } else if (newStreamItem.StreamContext != null) { //We want a transcoded stream newStreamItem.StreamContext.Dispose(); newStreamItem.StreamContext = null; } return(null); } finally { newStreamItem.BusyLock.Release(); } } return(null); }