コード例 #1
0
ファイル: FFMPEGStream.cs プロジェクト: uzbekdev1/main
        private void FfmpegListener()
        {
            ReasonToFinishPlaying reasonToStop = ReasonToFinishPlaying.StoppedByUser;

            VideoFileReader vfr = null;

            Program.WriterMutex.WaitOne();
            try
            {
                vfr = new VideoFileReader();
                vfr.Open(_source);
            }
            catch (Exception ex)
            {
                Log.Error("", ex);//MainForm.LogExceptionToFile(ex);
            }
            Program.WriterMutex.ReleaseMutex();
            if (vfr == null || !vfr.IsOpen)
            {
                if (PlayingFinished != null)
                {
                    PlayingFinished(this, ReasonToFinishPlaying.VideoSourceError);
                }
                return;
            }
            bool hasaudio = false;

            if (vfr.Channels > 0)
            {
                hasaudio        = true;
                RecordingFormat = new WaveFormat(vfr.SampleRate, 16, vfr.Channels);

                WaveOutProvider = new BufferedWaveProvider(RecordingFormat)
                {
                    DiscardOnBufferOverflow = true
                };
                _waveProvider = new BufferedWaveProvider(RecordingFormat)
                {
                    DiscardOnBufferOverflow = true
                };


                _sampleChannel    = new SampleChannel(_waveProvider);
                _meteringProvider = new MeteringSampleProvider(_sampleChannel);
                _meteringProvider.StreamVolume += MeteringProviderStreamVolume;

                if (HasAudioStream != null)
                {
                    HasAudioStream(this, EventArgs.Empty);
                }
            }

            int interval = 1000 / ((vfr.FrameRate == 0) ? 25 : vfr.FrameRate);

            byte[] data;
            Bitmap frame;

            try
            {
                while (!_stopEvent.WaitOne(0, false))
                {
                    DateTime start = DateTime.Now;
                    frame = vfr.ReadVideoFrame();
                    if (frame == null)
                    {
                        reasonToStop = ReasonToFinishPlaying.EndOfStreamReached;
                        break;
                    }

                    if (NewFrame != null)
                    {
                        NewFrame(this, new NewFrameEventArgs(frame));
                    }
                    frame.Dispose();

                    if (hasaudio)
                    {
                        data = vfr.ReadAudioFrame();
                        if (DataAvailable != null)
                        {
                            _waveProvider.AddSamples(data, 0, data.Length);

                            if (Listening)
                            {
                                WaveOutProvider.AddSamples(data, 0, data.Length);
                            }

                            _mFramesReceived++;

                            //forces processing of volume level without piping it out
                            var sampleBuffer = new float[data.Length];

                            _meteringProvider.Read(sampleBuffer, 0, data.Length);
                            DataAvailable(this, new DataAvailableEventArgs((byte[])data.Clone()));
                        }
                    }

                    if (interval > 0)
                    {
                        // get frame extract duration
                        TimeSpan span = DateTime.Now.Subtract(start);

                        // miliseconds to sleep
                        int msec = interval - (int)span.TotalMilliseconds;

                        if ((msec > 0) && (_stopEvent.WaitOne(msec, false)))
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (VideoSourceError != null)
                {
                    VideoSourceError(this, new VideoSourceErrorEventArgs(e.Message));
                }
                Log.Error("", e);//MainForm.LogExceptionToFile(e);
                reasonToStop = ReasonToFinishPlaying.DeviceLost;
            }
            if (PlayingFinished != null)
            {
                PlayingFinished(this, reasonToStop);
            }
        }