public void TalkRTPServerThread(object obj) { int sequenceNumber = 0; long timestamp = 0; short[] inputbuffer = new short[2048]; AudioPowerUpdate hander = new AudioPowerUpdate(AudioPowerUpdateHandler); ThreadParam ap = (ThreadParam)obj; DeviceListener.Device d = deviceListener.Find(ap.id); if (d == null) { return; } UdpClient udpserver = new UdpClient(9999); int port = 9999; bool normal_mode = ap.mode.Equals("normal"); int ret; if (normal_mode) { ret = deviceListener.IntercomStart(d, "0.0.0.0", port, "g711-u", "rtp", ap.volume, ap.gain, ap.inputsource, ap.aec); } else { ret = deviceListener.IntercomEmergencyStart(d, "0.0.0.0", port, "g711-u", "rtp", ap.volume, ap.gain, ap.inputsource, ap.aec); } if (ret != 0) { return; } IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0); udpserver.Client.ReceiveTimeout = 3000; byte[] bs = udpserver.Receive(ref ipep); if (bs == null) { return; } string dev_ip = null; int dev_port = 0; string str = System.Text.Encoding.Default.GetString(bs); if (str.Contains(ap.id.ToString()) == false) { return; } dev_ip = ipep.Address.ToString(); dev_port = ipep.Port; //GroovyCodecs.G711.uLaw.ULawEncoder g711encode = new GroovyCodecs.G711.uLaw.ULawEncoder(); //GroovyCodecs.G711.uLaw.ULawDecoder g711decode = new GroovyCodecs.G711.uLaw.ULawDecoder(); SoundCardDLL.SoundCardInit(8000); try { short[] micpack = new short[2048]; int micpack_length = 0; short[] speakerpack = new short[2048]; int speakerpack_length = 0; while (true) { micpack_length = 0; speakerpack_length = 0; if (SoundCardDLL.SoundCardWaitForInputData()) { while (true) { short[] pcmbuf = SoundCardDLL.SoundCardReadFrom(160); if (pcmbuf == null) { break; } if ((micpack_length + pcmbuf.Length) < 2048) { Array.Copy(pcmbuf, 0, micpack, micpack_length, pcmbuf.Length); micpack_length += pcmbuf.Length; } if (checkBox1.Checked) { Array.Clear(pcmbuf, 0, pcmbuf.Length); } //byte[] ba = g711encode.Process(pcmbuf); byte[] ba = g711.g711Encode_ulwa(pcmbuf); RtpPacket packet = new RtpPacket(RtpPayloadType.G711_uLaw, sequenceNumber, timestamp, ba, ba.Length); sequenceNumber++; timestamp += ba.Length; udpserver.Send(packet.ToArray(), packet.Length, dev_ip, dev_port); } } while (udpserver.Available > 0) { bs = udpserver.Receive(ref ipep); if (dev_ip.Equals(ipep.Address.ToString())) { RtpPacket packet = new RtpPacket(bs, bs.Length); if (packet.PayloadType == RtpPayloadType.G711_uLaw) { //short[] pcmbuf1 = g711decode.Process(packet.Payload); short[] pcmbuf1 = g711.g711Decode_ulaw(packet.Payload); SoundCardDLL.SoundCardWriteTo(pcmbuf1); if ((speakerpack_length + pcmbuf1.Length) < 2048) { Array.Copy(pcmbuf1, 0, speakerpack, speakerpack_length, pcmbuf1.Length); speakerpack_length += pcmbuf1.Length; } } } } double mic_db = 0; double speaker_db = 0; if (micpack_length > 0) { double a = 0; for (int i = 0; i < micpack_length; i++) { a += micpack[i] * micpack[i]; } mic_db = 20 * Math.Log10(a / (dbBase * micpack_length)); mic_db += 100; if (mic_db < 0) { mic_db = 0; } if (mic_db > 100) { mic_db = 100; } this.Invoke(hander, Convert.ToInt32(mic_db), -1); } if (speakerpack_length > 0) { double a = 0; for (int i = 0; i < speakerpack_length; i++) { a += speakerpack[i] * speakerpack[i]; } speaker_db = 20 * Math.Log10(a / (dbBase * speakerpack_length)); speaker_db += 100; if (speaker_db < 0) { speaker_db = 0; } if (speaker_db > 100) { speaker_db = 100; } this.Invoke(hander, -1, Convert.ToInt32(speaker_db)); } } } catch (ThreadAbortException abortException) { if (normal_mode) { deviceListener.IntercomStop(d); } else { deviceListener.IntercomEmergencyStop(d); } //udpserver.Close(); //SoundCard.SoundCardClose(); } finally { udpserver.Close(); SoundCardDLL.SoundCardClose(); } }