コード例 #1
0
        private void SoundCallback(Sound soundData)
        {
            if (DataAvailable == null || _needsSetup)
            {
                return;
            }

            var samples = new byte[soundData.SamplesSize];

            Marshal.Copy(soundData.SamplesData, samples, 0, (int)soundData.SamplesSize);

            _waveProvider.AddSamples(samples, 0, samples.Length);

            if (Listening && WaveOutProvider != null)
            {
                WaveOutProvider.AddSamples(samples, 0, samples.Length);
            }

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

            _meteringProvider.Read(sampleBuffer, 0, samples.Length);

            if (DataAvailable != null)
            {
                DataAvailable(this, new DataAvailableEventArgs((byte[])samples.Clone()));
            }
        }
コード例 #2
0
ファイル: AudioEngine.cs プロジェクト: redshiba/Toxy
        private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
        {
            short[] shorts = BytesToShorts(e.Buffer);

            if (OnMicDataAvailable != null)
            {
                OnMicDataAvailable(shorts, _waveSource.WaveFormat.SampleRate, _waveSource.WaveFormat.Channels);
            }

            _waveSourceProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
            _waveSourceMeter.Read(new float[e.BytesRecorded], 0, e.BytesRecorded);
        }
コード例 #3
0
ファイル: KinectStream.cs プロジェクト: LubyRuffy/sitw_core
        private void AudioThread()
        {
            while (_stopEvent != null && !_stopEvent.WaitOne(0, false))
            {
                var data = _audioStream.Read(_audioBuffer, 0, _audioBuffer.Length);
                if (DataAvailable != null)
                {
                    _waveProvider.AddSamples(_audioBuffer, 0, data);

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

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

                    _meteringProvider.Read(sampleBuffer, 0, data);
                    DataAvailable(this, new DataAvailableEventArgs((byte[])_audioBuffer.Clone()));
                }
            }
        }
コード例 #4
0
        public void WaveInDataAvailable(object sender, WaveInEventArgs e)
        {
            var sampleBuffer = new float[e.BytesRecorded];

            if (_meteringProvider != null)
            {
                _meteringProvider.Read(sampleBuffer, 0, e.BytesRecorded);

                var enc = new byte[e.Buffer.Length / 2];
                //可以控制是否对语音进行编码,编码之后Client才可以播放出声音
                if (FrmVoiceSpeaker.ifF2PressProsessing)
                {
                    ALawEncoder.ALawEncode(e.Buffer, enc);
                }
                try
                {
                    SendVarData(client, enc);
                }
                catch (SocketException se)
                {
                    logger.Error("发送语音流出现异常。Exception:{0}", se.Message);
                }
            }
        }
コード例 #5
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);
            }
        }
コード例 #6
0
 public int Read(float[] buffer, int offset, int count) => meter.Read(buffer, offset, count);
コード例 #7
0
        // Worker thread
        private void WorkerThread()
        {
            // buffer to read stream
            var buffer   = new byte[BufSize];
            var encoding = new ASCIIEncoding();

            while (!_stopEvent.WaitOne(0, false))
            {
                // reset reload event
                _reloadEvent.Reset();

                // HTTP web request
                HttpWebRequest request = null;
                // web responce
                WebResponse response = null;
                // stream for MJPEG downloading
                Stream stream = null;
                // boundary betweeen images (string and binary versions)
                byte[] boundary;

                try
                {
                    // create request
                    request = (HttpWebRequest)WebRequest.Create(_source);
                    // set user agent
                    if (_userAgent != null)
                    {
                        request.UserAgent = _userAgent;
                    }

                    // set proxy
                    if (_proxy != null)
                    {
                        request.Proxy = _proxy;
                    }

                    if (_usehttp10)
                    {
                        request.ProtocolVersion = HttpVersion.Version10;
                    }

                    // set timeout value for the request
                    request.Timeout           = _requestTimeout;
                    request.AllowAutoRedirect = true;

                    // set login and password
                    if ((_login != null) && (_password != null) && (_login != string.Empty))
                    {
                        request.Credentials = new NetworkCredential(_login, _password);
                    }
                    // set connection group name
                    if (_useSeparateConnectionGroup)
                    {
                        request.ConnectionGroupName = GetHashCode().ToString();
                    }
                    // get response
                    response = request.GetResponse();

                    // get response stream
                    stream             = response.GetResponseStream();
                    stream.ReadTimeout = _requestTimeout;

                    boundary = encoding.GetBytes("--myboundary");
                    byte[] sep = encoding.GetBytes("\r\n\r\n");

                    // loop

                    int startPacket = -1;
                    int endPacket   = -1;
                    int ttl         = 0;

                    bool hasaudio = false;

                    while ((!_stopEvent.WaitOne(0, false)) && (!_reloadEvent.WaitOne(0, false)))
                    {
                        int read;
                        if ((read = stream.Read(buffer, ttl, ReadSize)) == 0)
                        {
                            throw new ApplicationException();
                        }

                        ttl += read;

                        if (startPacket == -1)
                        {
                            startPacket = ByteArrayUtils.Find(buffer, boundary, 0, ttl);
                        }
                        else
                        {
                            if (endPacket == -1)
                            {
                                endPacket = ByteArrayUtils.Find(buffer, boundary, startPacket + boundary.Length, ttl - (startPacket + boundary.Length));
                            }
                        }



                        if (startPacket > -1 && endPacket > startPacket)
                        {
                            int br = ByteArrayUtils.Find(buffer, sep, startPacket, 100);

                            if (br != -1)
                            {
                                var arr = new byte[br];
                                Array.Copy(buffer, startPacket, arr, 0, br - startPacket);
                                string s = Encoding.ASCII.GetString(arr);
                                int    k = s.IndexOf("Content-type: ");
                                if (k != -1)
                                {
                                    s = s.Substring(k + 14);
                                    s = s.Substring(0, s.IndexOf("\r\n"));
                                    s = s.Trim();
                                }
                                switch (s)
                                {
                                case "image/jpeg":
                                    Bitmap bitmap;
                                    using (var ms = new MemoryStream(buffer, br + 4, endPacket - br - 8))
                                    {
                                        bitmap = (Bitmap)Image.FromStream(ms);
                                    }
                                    // notify client
                                    NewFrame(this, new NewFrameEventArgs(bitmap));
                                    // release the image
                                    bitmap.Dispose();
                                    bitmap = null;

                                    break;

                                case "audio/raw":
                                    if (!hasaudio)
                                    {
                                        hasaudio        = true;
                                        RecordingFormat = new WaveFormat(16000, 16, 1);

                                        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);
                                        }
                                    }

                                    if (DataAvailable != null)
                                    {
                                        int l    = endPacket - br - 8;
                                        var data = new byte[l];
                                        using (var ms = new MemoryStream(buffer, br + 4, l))
                                        {
                                            ms.Read(data, 0, l);
                                        }
                                        _waveProvider.AddSamples(data, 0, data.Length);

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

                                        //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()));
                                    }

                                    break;

                                case "alert/text":
                                    // code to handle alert notifications goes here
                                    if (AlertHandler != null)
                                    {
                                        int dl    = endPacket - br - 8;
                                        var data2 = new byte[dl];
                                        using (var ms = new MemoryStream(buffer, br + 4, dl))
                                        {
                                            ms.Read(data2, 0, dl);
                                        }
                                        string alerttype = Encoding.ASCII.GetString(data2);
                                        AlertHandler(this, new AlertEventArgs(alerttype));
                                    }
                                    break;
                                }
                            }

                            ttl -= endPacket;
                            Array.Copy(buffer, endPacket, buffer, 0, ttl);
                            startPacket = -1;
                            endPacket   = -1;
                        }
                    }
                }
                catch (ApplicationException)
                {
                    // do nothing for Application Exception, which we raised on our own
                    // wait for a while before the next try
                    Thread.Sleep(250);
                }
                catch (ThreadAbortException)
                {
                    break;
                }
                catch (Exception exception)
                {
                    // provide information to clients
                    if (VideoSourceError != null)
                    {
                        VideoSourceError(this, new VideoSourceErrorEventArgs(exception.Message));
                    }
                    // wait for a while before the next try
                    Thread.Sleep(250);
                }
                finally
                {
                    // abort request
                    if (request != null)
                    {
                        request.Abort();
                        request = null;
                    }
                    // close response stream
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                    // close response
                    if (response != null)
                    {
                        response.Close();
                        response = null;
                    }
                }

                // need to stop ?
                if (_stopEvent.WaitOne(0, false))
                {
                    break;
                }
            }

            if (PlayingFinished != null)
            {
                PlayingFinished(this, ReasonToFinishPlaying.StoppedByUser);
            }
        }