示例#1
0
        public StreamDecoder()
        {
            _hikDecoder    = new HikM4Decoder();
            _ffmpegDecoder = new FfmpegDecoder();

            _hikDecoder.VideoFrameEvent += HikDecoder_VideoFrameEvent;
        }
示例#2
0
        private void initDecoder()
        {
            _ffmpegDecoder  = new FfmpegDecoder();
            _hikm4Decoder   = new HikM4Decoder();
            _univiewDecoder = new UniviewDecoder();

            _hikm4Decoder.VideoFrameEvent   += hikm4Decoder_VideoFrameEvent;
            _univiewDecoder.VideoFrameEvent += univiewDecoder_VideoFrameEvent;
        }
示例#3
0
文件: Program.cs 项目: randoms/cscore
        static void Main(string[] args)
        {
            const string DefaultStream =
                @"http://stream.srg-ssr.ch/m/rsj/aacp_96";

            Console.WriteLine("01 - File");
            Console.WriteLine("02 - Stream");
            Console.WriteLine("99 - Test-Stream");

            int choice;

            do
            {
                Int32.TryParse(Console.ReadLine(), out choice);
            } while (choice != 1 && choice != 2 && choice != 99);

            Stream stream = null;
            string url    = null;

            if (choice == 1)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                stream = File.OpenRead(openFileDialog.FileName);
            }
            else if (choice == 2)
            {
                Console.WriteLine("Enter a stream url:");
                url = Console.ReadLine();
            }
            else
            {
                url = DefaultStream;
            }

            //we could also easily pass the filename as url
            //but since we want to test the decoding of System.IO.Stream, we
            //pass a FileStream as argument.
            FfmpegDecoder ffmpegDecoder = stream == null
                ? new FfmpegDecoder(url)
                : new FfmpegDecoder(stream);

            using (ffmpegDecoder)
                using (var wasapiOut = new WasapiOut())
                {
                    wasapiOut.Initialize(ffmpegDecoder);
                    wasapiOut.Play();

                    Console.ReadKey();
                }
        }
示例#4
0
        private IWaveSource GetCodec(string filename)
        {
            IWaveSource waveSource       = null;
            bool        useFfmpegDecoder = true;

            // FfmpegDecoder doesn't support WMA lossless. If Windows Media Foundation is available,
            // we can use MediaFoundationDecoder for WMA files, which supports WMA lossless.
            if (this.supportsWindowsMediaFoundation && Path.GetExtension(filename).ToLower().Equals(FileFormats.WMA))
            {
                try
                {
                    waveSource       = new MediaFoundationDecoder(filename);
                    useFfmpegDecoder = false;
                }
                catch (Exception)
                {
                }
            }

            if (useFfmpegDecoder)
            {
                // waveSource = new FfmpegDecoder(this.filename);

                // On some systems, files with special characters (e.g. "æ", "ø") can't be opened by FfmpegDecoder.
                // This exception is thrown: avformat_open_input returned 0xfffffffe: No such file or directory.
                // StackTrace: at CSCore.Ffmpeg.FfmpegCalls.AvformatOpenInput(AVFormatContext** formatContext, String url)
                // This issue can't be reproduced for now, so we're using a stream as it works in all cases.
                // See: https://github.com/digimezzo/Dopamine/issues/746
                // And: https://github.com/filoe/cscore/issues/344
                this.audioStream = File.OpenRead(filename);
                waveSource       = new FfmpegDecoder(this.audioStream);
            }

            // If the SampleRate < 32000, make it 32000. The Equalizer's maximum frequency is 16000Hz.
            // The sample rate has to be bigger than 2 * frequency.
            if (waveSource.WaveFormat.SampleRate < 32000)
            {
                waveSource = waveSource.ChangeSampleRate(32000);
            }

            return(waveSource
                   .ToSampleSource()
                   .AppendSource(this.Create10BandEqualizer, out this.equalizer)
                   .ToWaveSource());
        }
示例#5
0
        private IWaveSource GetCodec(string filename)
        {
            IWaveSource waveSource       = null;
            bool        useFfmpegDecoder = true;

            if (Path.GetExtension(filename).ToLower().Equals(FileFormats.WMA))
            {
                try
                {
                    waveSource       = new MediaFoundationDecoder(filename);
                    useFfmpegDecoder = false;
                }
                catch (Exception)
                {
                }
            }

            if (useFfmpegDecoder)
            {
                // FfmpegDecoder constructor which uses string as parameter throws exception
                // "Exception: avformat_open_input returned 0xffffffea: Invalid argument."
                // when the file name contains special characters.
                // See: https://github.com/filoe/cscore/issues/298
                // Workaround: use the constructor which uses stream as parameter.
                // IWaveSource waveSource = new FfmpegDecoder(this.filename);
                this.audioStream = File.OpenRead(filename);
                waveSource       = new FfmpegDecoder(this.audioStream);
            }

            // If the SampleRate < 32000, make it 32000. The Equalizer's maximum frequency is 16000Hz.
            // The sample rate has to be bigger than 2 * frequency.
            if (waveSource.WaveFormat.SampleRate < 32000)
            {
                waveSource = waveSource.ChangeSampleRate(32000);
            }

            return(waveSource
                   .ToSampleSource()
                   .AppendSource(this.Create10BandEqualizer, out this.equalizer)
                   .ToWaveSource());
        }
示例#6
0
        private IWaveSource GetCodec(string filename)
        {
            IWaveSource waveSource;

            try
            {
                if (System.IO.Path.GetExtension(this.filename.ToLower()) == FileFormats.MP3)
                {
                    // For MP3's, we force usage of MediaFoundationDecoder. CSCore uses DmoMp3Decoder
                    // by default. DmoMp3Decoder however is very slow at playing MP3's from a NAS.
                    // So we're using MediaFoundationDecoder until DmoMp3Decoder has improved.
                    waveSource = new MediaFoundationDecoder(this.filename);
                }
                else
                {
                    // Other file formats are using the default decoders
                    waveSource = CodecFactory.Instance.GetCodec(this.filename);
                }
            }
            catch (Exception)
            {
                waveSource = new FfmpegDecoder(filename); //try ffmpeg as last option
            }

            // If the SampleRate < 32000, make it 32000. The Equalizer's maximum frequency is 16000Hz.
            // The samplerate has to be bigger than 2 * frequency.
            if (waveSource.WaveFormat.SampleRate < 32000)
            {
                waveSource = waveSource.ChangeSampleRate(32000);
            }

            return(waveSource
                   .ToSampleSource()
                   .AppendSource(this.Create10BandEqualizer, out this.equalizer)
                   .ToWaveSource());
        }
示例#7
0
        private IWaveSource GetCodec(string filename)
        {
            IWaveSource waveSource       = null;
            bool        useFfmpegDecoder = true;

            // FfmpegDecoder doesn't support WMA lossless. If Windows Media Foundation is available,
            // we can use MediaFoundationDecoder for WMA files, which supports WMA lossless.
            if (this.supportsWindowsMediaFoundation && Path.GetExtension(filename).ToLower().Equals(FileFormats.WMA))
            {
                try
                {
                    waveSource       = new MediaFoundationDecoder(filename);
                    useFfmpegDecoder = false;
                }
                catch (Exception)
                {
                }
            }

            if (useFfmpegDecoder)
            {
                waveSource = new FfmpegDecoder(this.filename);
            }

            // If the SampleRate < 32000, make it 32000. The Equalizer's maximum frequency is 16000Hz.
            // The sample rate has to be bigger than 2 * frequency.
            if (waveSource.WaveFormat.SampleRate < 32000)
            {
                waveSource = waveSource.ChangeSampleRate(32000);
            }

            return(waveSource
                   .ToSampleSource()
                   .AppendSource(this.Create10BandEqualizer, out this.equalizer)
                   .ToWaveSource());
        }
示例#8
0
 public void Dispose()
 {
     _decoder?.Dispose();
     _decoder = null;
 }
示例#9
0
 public FfmpegStreamDecoder(Constants.AVCodecID codecId, int width, int height)
 {
     _decoder = new FfmpegDecoder();
     _decoder.Init(codecId, width, height);
 }
        void run()
        {
            FfmpegDecoder decoder = new FfmpegDecoder();
            int           index   = 0;

            int wait = 0;

            while (!_disposeEvent.WaitOne(wait))
            {
                wait = 1;
                if (_records.Length > 0)
                {
                    bool     reposed = false;
                    DateTime startRealTime;
                    DateTime startPlayTime;
                    lock (_playTimeLockObj)
                    {
                        startRealTime = _startRealTime;
                        startPlayTime = _startPlayTime;
                        reposed       = _reposed;
                        _reposed      = false;
                    }

                    if (_playing || reposed)
                    {
                        DateTime curPlayTime = startPlayTime + new TimeSpan((long)Math.Round((DateTime.Now - startRealTime).Ticks * _speed));
                        if (curPlayTime > EndTime)
                        {
                            curPlayTime = EndTime;
                        }

                        Record <FfmpegPackage> record = null;
                        if (reposed)
                        {
                            for (index = 0; index < _records.Length; index++)
                            {
                                if (_records[index].Time >= curPlayTime)
                                {
                                    break;
                                }
                            }
                            if (index < _records.Length)
                            {
                                record = _records[index];
                            }
                        }
                        else
                        {
                            int next = index + 1;
                            if (next < _records.Length && _records[next].Time < curPlayTime)
                            {
                                index  = next;
                                record = _records[index];
                            }
                        }

                        if (record != null)
                        {
                            _currentTime = record.Time;
                            decoder.Init(record.Package.CodecID, record.Package.Width, record.Package.Height);
                            var frame = decoder.Decode(record.Package.Type, record.Package.Pts, record.Package.Data);
                            if (frame != null)
                            {
                                var videoFrame = new VideoFrame()
                                {
                                    Width     = frame.Width,
                                    Height    = frame.Height,
                                    Timestamp = (int)frame.Pts,
                                    Data      = frame.Data,
                                };
                                fireVideoFrameEvent(videoFrame);
                            }
                            wait = 0;
                        }
                    }
                }
            }
        }