Пример #1
0
        /// <summary>
        /// Reads all the blocks of the specified media type from the source url.
        /// </summary>
        /// <param name="mediaSource">The subtitles URL.</param>
        /// <param name="sourceType">Type of the source.</param>
        /// <param name="parent">The parent.</param>
        /// <returns>A buffer containing all the blocks.</returns>
        internal static MediaBlockBuffer LoadBlocks(string mediaSource, MediaType sourceType, ILoggingHandler parent)
        {
            if (string.IsNullOrWhiteSpace(mediaSource))
            {
                throw new ArgumentNullException(nameof(mediaSource));
            }

            using (var tempContainer = new MediaContainer(mediaSource, null, parent))
            {
                tempContainer.Initialize();

                // Skip reading and decoding unused blocks
                tempContainer.MediaOptions.IsAudioDisabled    = sourceType != MediaType.Audio;
                tempContainer.MediaOptions.IsVideoDisabled    = sourceType != MediaType.Video;
                tempContainer.MediaOptions.IsSubtitleDisabled = sourceType != MediaType.Subtitle;

                // Open the container
                tempContainer.Open();
                if (tempContainer.Components.Main == null || tempContainer.Components.MainMediaType != sourceType)
                {
                    throw new MediaContainerException($"Could not find a stream of type '{sourceType}' to load blocks from");
                }

                // read all the packets and decode them
                var outputFrames = new List <MediaFrame>(1024 * 8);
                while (true)
                {
                    tempContainer.Read();
                    var frames = tempContainer.Decode();
                    foreach (var frame in frames)
                    {
                        if (frame.MediaType != sourceType)
                        {
                            continue;
                        }

                        outputFrames.Add(frame);
                    }

                    if (frames.Count <= 0 && tempContainer.IsAtEndOfStream)
                    {
                        break;
                    }
                }

                // Build the result
                var result = new MediaBlockBuffer(outputFrames.Count, sourceType);
                foreach (var frame in outputFrames)
                {
                    result.Add(frame, tempContainer);
                }

                tempContainer.Close();
                return(result);
            }
        }
Пример #2
0
        private static ConfiguredTaskAwaitable RunReaderTask()
        {
            if (StartTime != 0)
            {
                var decodedFrames = Container.Seek(TimeSpan.FromSeconds(StartTime));
                HandleDecoding(decodedFrames);
            }

            return(Task.Run(() =>
            {
                while (ReadCancel == false)
                {
                    try
                    {
                        if (Container.IsAtEndOfStream == false && ReadCancel == false)
                        {
                            // check if the packet buuffer is too low
                            if (Container.Components.PacketBufferCount <= 24)
                            {
                                // buffer at least 60 packets
                                while (Container.Components.PacketBufferCount < 48 && Container.IsAtEndOfStream == false && ReadCancel == false)
                                {
                                    Container.Read();
                                }

                                ($"Buffer    "
                                 + $" | LEN: {Container.Components.PacketBufferLength / 1024d,9:0.00}K"
                                 + $" | CNT: {Container.Components.PacketBufferCount,12}" + $" | POS: {Container.StreamPosition / 2014d,10:0.00}K")
                                .Warn(typeof(Program));
                            }
                        }
                    }
                    catch { }
                    finally
                    {
                        DecodingDone.Wait();
                    }
                }

                $"Reader task finished".Warn(typeof(Program));
            }).ConfigureAwait(false));
        }