Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Does the preparation to start a stream
        /// </summary>
        /// <param name="identifier">The unique string which identifies the stream Item</param>
        /// <param name="context">Transcoder context</param>
        internal static async Task <TranscodeContext> StartStreamingAsync(string identifier, double startTime)
        {
            if (!STREAM_ITEMS.TryGetValue(identifier, out StreamItem currentStreamItem))
            {
                return(null);
            }

            using (await currentStreamItem.RequestBusyLockAsync())
            {
                if (currentStreamItem.TranscoderObject == null)
                {
                    STREAM_ITEMS.TryRemove(identifier, out _);
                    return(null);
                }

                if (currentStreamItem.IsActive)
                {
                    currentStreamItem.TranscoderObject.StopStreaming();
                    if (currentStreamItem.StreamContext != null)
                    {
                        if (currentStreamItem.StreamContext is TranscodeContext transcodeContext)
                        {
                            transcodeContext.UpdateStreamUse(false);
                        }
                        currentStreamItem.StreamContext.Dispose();
                        currentStreamItem.StreamContext = null;
                    }
                }

                if (!currentStreamItem.TranscoderObject.StartTrancoding())
                {
                    Logger.Debug("StreamControl: Transcoding busy for mediaitem {0}", currentStreamItem.RequestedMediaItem.MediaItemId);
                    return(null);
                }

                currentStreamItem.TranscoderObject.StartStreaming();
                if (currentStreamItem.IsLive)
                {
                    currentStreamItem.StreamContext = await MediaConverter.GetLiveStreamAsync(identifier, currentStreamItem.TranscoderObject.TranscodingParameter, currentStreamItem.LiveChannelId, true);
                }
                else
                {
                    currentStreamItem.StreamContext = await MediaConverter.GetMediaStreamAsync(identifier, currentStreamItem.TranscoderObject.TranscodingParameter, startTime, 0, true);
                }

                if (currentStreamItem.StreamContext is TranscodeContext context)
                {
                    context.UpdateStreamUse(true);
                    currentStreamItem.TranscoderObject.SegmentDir = context.SegmentDir;
                    return(context);
                }
                else if (currentStreamItem.StreamContext != null)
                {
                    //We want a transcoded stream
                    currentStreamItem.StreamContext.Dispose();
                    currentStreamItem.StreamContext = null;
                }

                return(null);
            }
        }