Exemple #1
0
        private void AudioSourceDataAvailable(object sender, DataAvailableEventArgs e)
        {
            try
            {
                lock (_obj)
                {
                    if (_bTalking && _avstream != null)
                    {
                        byte[] bSrc = e.RawData;
                        int totBytes = bSrc.Length;

                        if (!_audioSource.RecordingFormat.Equals(_waveFormat))
                        {
                            var ws = new TalkHelperStream(bSrc, totBytes, _audioSource.RecordingFormat);
                            var helpStm = new WaveFormatConversionStream(_waveFormat, ws);
                            totBytes = helpStm.Read(bSrc, 0, 25000);
                            ws.Close();
                            ws.Dispose();
                            helpStm.Close();
                            helpStm.Dispose();
                        }
                        var enc = new byte[totBytes / 2];
                        ALawEncoder.ALawEncode(bSrc, totBytes, enc);

                        try {
                            _avstream.Write(enc, 0, enc.Length);
                            _avstream.Flush();
                        }
                        catch (SocketException)
                        {
                            StopTalk();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("",ex);//MainForm.LogExceptionToFile(ex);
                StopTalk();
            }
        }
Exemple #2
0
        private void DirectStreamListener()
        {
            try
            {
                var data = new byte[PacketSize];
                if (_stream != null)
                {
                    while (!_stopEvent.WaitOne(0, false))
                    {

                        if (DataAvailable != null)
                        {
                            int recbytesize = _stream.Read(data, 0, PacketSize);
                            if (recbytesize > 0)
                            {
                                if (_sampleChannel != null)
                                {
                                    _waveProvider.AddSamples(data, 0, recbytesize);

                                    var sampleBuffer = new float[recbytesize];
                                    _sampleChannel.Read(sampleBuffer, 0, recbytesize);

                                    if (Listening && WaveOutProvider != null)
                                    {
                                        WaveOutProvider.AddSamples(data, 0, recbytesize);
                                    }
                                    var da = new DataAvailableEventArgs((byte[]) data.Clone());
                                    DataAvailable(this, da);
                                }

                            }
                            else
                            {
                                break;
                            }

                            if (_stopEvent.WaitOne(Interval, false))
                                break;
                        }

                    }
                }

                if (AudioFinished != null)
                    AudioFinished(this, ReasonToFinishPlaying.StoppedByUser);
            }
            catch (Exception e)
            {
                if (AudioSourceError!=null)
                    AudioSourceError(this, new AudioSourceErrorEventArgs(e.Message));
                Log.Error("", e);//MainForm.LogExceptionToFile(e);
            }
            if (_stream != null)
            {
                _stream.Close();
                _stream = null;
            }
        }
Exemple #3
0
        private void SoundCallback(Sound soundData)
        {
            if (DataAvailable == null || _needsSetup) return;

            if (_sampleChannel != null)
            {
                var samples = new byte[soundData.SamplesSize];
                Marshal.Copy(soundData.SamplesData, samples, 0, (int)soundData.SamplesSize);

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

                var sampleBuffer = new float[samples.Length];
                _sampleChannel.Read(sampleBuffer, 0, samples.Length);

                if (Listening && WaveOutProvider != null)
                {
                    WaveOutProvider.AddSamples(samples, 0, samples.Length);
                }
                var da = new DataAvailableEventArgs((byte[])samples.Clone());
                if (DataAvailable != null)
                    DataAvailable(this, da);
            }
        }
Exemple #4
0
        private void WebStreamListener()
        {
            try
            {
                var data = new byte[6400];
                if (_socket != null)
                {
                    while (!stopEvent.WaitOne(0, false))
                    {
                        if (DataAvailable != null)
                        {
                            int recbytesize = _socket.Receive(data, 0, 6400,SocketFlags.None);

                            if (_sampleChannel != null)
                            {
                                _waveProvider.AddSamples(data, 0, recbytesize);

                                var sampleBuffer = new float[recbytesize];
                                _sampleChannel.Read(sampleBuffer, 0, recbytesize);

                                if (Listening && WaveOutProvider != null)
                                {
                                    WaveOutProvider.AddSamples(data, 0, recbytesize);
                                }
                                var da = new DataAvailableEventArgs((byte[])data.Clone());
                                DataAvailable(this, da);
                            }
                        }
                        else
                        {
                            break;
                        }
                        // need to stop ?
                        if (stopEvent.WaitOne(0, false))
                            break;
                    }
                }

                if (AudioFinished != null)
                    AudioFinished(this, ReasonToFinishPlaying.StoppedByUser);
            }
            catch (Exception e)
            {
                if (AudioSourceError!=null)
                    AudioSourceError(this, new AudioSourceErrorEventArgs(e.Message));
                Log.Error("",e);//MainForm.LogExceptionToFile(e);
            }
            if (_socket != null)
            {
                _socket.Close();
                _socket = null;
            }
        }
Exemple #5
0
 void WaveInDataAvailable(object sender, WaveInEventArgs e)
 {
     _isrunning = true;
     if (DataAvailable != null)
     {
         //forces processing of volume level without piping it out
         if (_sampleChannel != null)
         {
             var sampleBuffer = new float[e.BytesRecorded];
             _sampleChannel.Read(sampleBuffer, 0, e.BytesRecorded);
         }
         if (Listening && WaveOutProvider != null)
         {
             WaveOutProvider.AddSamples(e.Buffer, 0, e.Buffer.Length);
         }
         var da = new DataAvailableEventArgs((byte[])e.Buffer.Clone());
         DataAvailable(this, da);
     }
 }
Exemple #6
0
        private void SpyServerListener()
        {
            HttpWebRequest request = null;
            WebResponse response = null;
            Stream stream = null;

            var data = new byte[3200];
            try
            {
                request = (HttpWebRequest)WebRequest.Create(_source);
                request.Timeout = 10000;
                request.ReadWriteTimeout = 5000;
                response = request.GetResponse();
                stream = response.GetResponseStream();

                if (stream != null)
                {
                    stream.ReadTimeout = 5000;
                    while (!_stopEvent.WaitOne(0, false))
                    {
                        if (DataAvailable != null)
                        {
                            int recbytesize = stream.Read(data, 0, 3200);
                            if (recbytesize==0)
                                throw new Exception("lost stream");

                            byte[] dec;
                            ALawDecoder.ALawDecode(data, recbytesize, out dec);

                            if (_sampleChannel != null)
                            {
                                _waveProvider.AddSamples(dec, 0, dec.Length);

                                var sampleBuffer = new float[dec.Length];
                                _sampleChannel.Read(sampleBuffer, 0, dec.Length);

                                if (Listening && WaveOutProvider != null)
                                {
                                    WaveOutProvider.AddSamples(dec, 0, dec.Length);
                                }
                                var da = new DataAvailableEventArgs((byte[])dec.Clone());
                                DataAvailable(this, da);
                            }
                        }
                        else
                        {
                            break;
                        }
                        // need to stop ?
                        if (_stopEvent.WaitOne(0, false))
                            break;
                    }
                }

                if (AudioFinished != null)
                    AudioFinished(this, ReasonToFinishPlaying.StoppedByUser);
            }
            catch (Exception e)
            {

                if (AudioSourceError!=null)
                    AudioSourceError(this, new AudioSourceErrorEventArgs(e.Message));
                Log.Error("",e);//MainForm.LogExceptionToFile(e);
            }
            if (stream != null)
            {
                try
                {
                    stream.Close();
                }
                catch
                {

                }
                stream = null;
            }
        }
Exemple #7
0
        public void AddSamples(byte[] samples)
        {
            if (DataAvailable != null)
            {
                if (samples.Length > 0)
                {
                    var da = new DataAvailableEventArgs((byte[]) samples.Clone());
                    DataAvailable(this, da);

                }
            }
        }
Exemple #8
0
        private void FfmpegListener()
        {
            AudioFileReader afr = null;
            Program.WriterMutex.WaitOne();
            try
            {
                afr = new AudioFileReader();
                afr.Open(_source);
            }
            catch (Exception ex)
            {
                Log.Error("",ex);//MainForm.LogExceptionToFile(ex);
            }
            Program.WriterMutex.ReleaseMutex();
            if (afr == null || !afr.IsOpen)
            {
                if (AudioFinished!=null)
                    AudioFinished(this, ReasonToFinishPlaying.AudioSourceError);
                return;
            }

            RecordingFormat = new WaveFormat(afr.SampleRate, 16, afr.Channels);
            _waveProvider = new BufferedWaveProvider(RecordingFormat) { DiscardOnBufferOverflow = true };

            _sampleChannel = new SampleChannel(_waveProvider);
            _sampleChannel.PreVolumeMeter += SampleChannelPreVolumeMeter;

            byte[] data;
            int mult = afr.BitsPerSample/8;
            double btrg = Convert.ToDouble(afr.SampleRate*mult*afr.Channels);
            DateTime lastPacket = DateTime.Now;
            bool realTime = _source.IndexOf("://") != -1;

            try
            {
                DateTime req = DateTime.Now;
                while (!_stopEvent.WaitOne(0, false))
                {
                    data = afr.ReadAudioFrame();
                    if (data.Length>0)
                    {
                        lastPacket = DateTime.Now;
                        if (DataAvailable != null)
                        {
                            //forces processing of volume level without piping it out
                            _waveProvider.AddSamples(data, 0, data.Length);

                            var sampleBuffer = new float[data.Length];
                            _sampleChannel.Read(sampleBuffer, 0, data.Length);

                            if (WaveOutProvider!=null && Listening)
                            {
                                WaveOutProvider.AddSamples(data, 0, data.Length);
                            }
                            var da = new DataAvailableEventArgs((byte[]) data.Clone());
                            DataAvailable(this, da);
                        }

                        if (realTime)
                        {
                            if (_stopEvent.WaitOne(10, false))
                                break;
                        }
                        else
                        {
                            double f = (data.Length/btrg)*1000;
                            if (f > 0)
                            {
                                var span = DateTime.Now.Subtract(req);
                                var msec = Convert.ToInt32(f - (int) span.TotalMilliseconds);
                                if ((msec > 0) && (_stopEvent.WaitOne(msec, false)))
                                    break;
                                req = DateTime.Now;
                            }
                        }
                    }
                    else
                    {
                        if ((DateTime.Now - lastPacket).TotalMilliseconds > 5000)
                        {
                            afr.Close();
                            Stop();
                            throw new Exception("Audio source timeout");
                        }
                        if (_stopEvent.WaitOne(30, false))
                            break;
                    }

                }

                if (AudioFinished != null)
                    AudioFinished(this, ReasonToFinishPlaying.StoppedByUser);
            }
            catch (Exception e)
            {
                if (AudioSourceError!=null)
                    AudioSourceError(this, new AudioSourceErrorEventArgs(e.Message));
                Log.Error("",e);//MainForm.LogExceptionToFile(e);
            }
        }
Exemple #9
0
        private void AudioSourceDataAvailable(object sender, DataAvailableEventArgs e)
        {
            try
            {
                lock (_obj)
                {
                    if (_bTalking && _avstream != null)
                    {
                        byte[] bSrc = e.RawData;
                        int totBytes = bSrc.Length;

                        if (!_audioSource.RecordingFormat.Equals(_waveFormat))
                        {
                            var ws = new TalkHelperStream(bSrc, totBytes, _audioSource.RecordingFormat);
                            var helpStm = new WaveFormatConversionStream(_waveFormat, ws);
                            totBytes = helpStm.Read(bSrc, 0, 25000);

                            ws.Close();
                            ws.Dispose();
                            helpStm.Close();
                            helpStm.Dispose();
                        }

                        if (_needsencodeinit)
                        {
                            _enc.EncodeInit(BitConverter.ToInt16(e.RawData, 0), BitConverter.ToInt16(e.RawData, 2));
                            _needsencodeinit = false;
                        }

                        var buff = new byte[25000];
                        int c;
                        unsafe
                        {
                            fixed (byte* src = bSrc)
                            {
                                fixed (byte* dst = buff)
                                {
                                    c = (int)_enc.EncodeFoscam(src, totBytes, dst);
                                }
                            }
                        }
                        Buffer.BlockCopy(buff,0,_talkBuffer,_talkDatalen,c);
                        _talkDatalen += c;

                        var dtms = (int) (DateTime.Now - _dt).TotalMilliseconds;
                        int i = 0;
                        int j = 0;
                        try
                        {
                            while (j + 160 < _talkDatalen)
                            {
                                //need to write out in 160 byte packets for 40ms
                                byte[] cmd = SInit(TalkData, MoIPAvFlag);

                                cmd = AddNext(cmd, dtms + (i*40));
                                cmd = AddNext(cmd, _seq);
                                cmd = AddNext(cmd, (int) (DateTime.Now - _dt).TotalSeconds);
                                cmd = AddNext(cmd, (byte) 0x0);
                                cmd = AddNext(cmd, 160);

                                var pkt = new byte[160];
                                Buffer.BlockCopy(_talkBuffer, j, pkt, 0, 160);
                                cmd = AddNext(cmd, pkt, 160);
                                Encode(ref cmd);

                                _avstream.Write(cmd, 0, cmd.Length);
                                j += 160;
                                _seq++;
                                i++;
                            }
                            if (j < _talkDatalen)
                            {
                                Buffer.BlockCopy(_talkBuffer, j, _talkBuffer, 0, _talkDatalen-j);
                                _talkDatalen = _talkDatalen - j;
                            }
                        }
                        catch (SocketException)
                        {
                            StopTalk(true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("",ex);//MainForm.LogExceptionToFile(ex);
                StopTalk(true);
            }
        }
Exemple #10
0
        private void AudioSourceDataAvailable(object sender, DataAvailableEventArgs e)
        {
            try
            {
                lock (_obj)
                {
                    if (_bTalking && _avstream != null)
                    {
                        byte[] bSrc = e.RawData;
                        int totBytes = bSrc.Length;

                        if (!_audioSource.RecordingFormat.Equals(_waveFormat))
                        {
                            using (var helper = new TalkHelperStream(bSrc, totBytes, _audioSource.RecordingFormat))
                            {
                                using (var helpStm = new WaveFormatConversionStream(_waveFormat, helper))
                                {
                                    totBytes = helpStm.Read(bSrc, 0, 25000);
                                }
                            }
                        }
                        var enc = _muLawCodec.Encode(bSrc, 0, totBytes);
                        ALawEncoder.ALawEncode(bSrc, totBytes, enc);

                        Buffer.BlockCopy(enc, 0, _talkBuffer, _talkDatalen, enc.Length);
                        _talkDatalen += enc.Length;

                        int j = 0;
                        try
                        {
                            while (j + 240 < _talkDatalen)
                            {
                                //need to write out in 240 byte packets
                                var pkt = new byte[240];
                                Buffer.BlockCopy(_talkBuffer, j, pkt, 0, 240);

                               // _avstream.Write(_hdr, 0, _hdr.Length);
                                _avstream.Write(pkt, 0, 240);
                                j += 240;
                            }
                            if (j < _talkDatalen)
                            {
                                Buffer.BlockCopy(_talkBuffer, j, _talkBuffer, 0, _talkDatalen - j);
                                _talkDatalen = _talkDatalen - j;
                            }
                        }
                        catch (SocketException)
                        {
                            StopTalk();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("",ex);//MainForm.LogExceptionToFile(ex);
                StopTalk();
            }
        }