private void startSDPlay() { if (DeviceID == null) { return; } DeviceListener.Device d = deviceListener.Find(DeviceID); panel2.BackColor = SystemColors.Control; panel3.BackColor = SystemColors.Control; ThreadParam ap = new ThreadParam(); ap.filename = "product_test.mp3"; ap.id = DeviceID; ap.volume = 85; label14.Text = ap.filename; label9.Text = ap.filename; SDPlayThreadHandle = new Thread(SDFilePlayThread); SDPlayThreadHandle.IsBackground = true; SDPlayThreadHandle.Start(ap); }
private void button3_Click(object sender, EventArgs e) { if (comboBox1.SelectedItem != null) { DeviceItem item = (DeviceItem)comboBox1.SelectedItem; DeviceListener.Device d = deviceListener.Find(item.id); if (d != null) { int ret = deviceListener.SDCardGetInfo(d, out string disk_size, out string free_size); if (ret == 0) { label4.Text = disk_size; label5.Text = free_size; } } } }
private void FilePlayThread(object obj) { IPAddress localaddr = IPAddress.Parse("0.0.0.0"); TcpListener tcpserver = new TcpListener(localaddr, 0); tcpserver.Start(); IPEndPoint localep = (IPEndPoint)tcpserver.LocalEndpoint; WorkProcessHandler h = new WorkProcessHandler(FilePlayProcess); ThreadParam ap = (ThreadParam)obj; DeviceListener.Device d = deviceListener.Find(ap.id); if (d == null) { return; } int ret = deviceListener.FilePlayStart(d, "0.0.0.0", localep.Port, ap.streamtype, ap.volume, null); if (ret != 0) { return; } TcpClient tc = null; try { long starttime = DateTime.Now.Ticks; while ((DateTime.Now.Ticks - starttime) < 3000 * 10000)//3000ms { if (tcpserver.Pending()) { tc = tcpserver.AcceptTcpClient(); break; } } if (tc != null) { FileStream fs = File.OpenRead(ap.filename); if (fs != null) { bool eof = false; long writebytes = 0; byte[] fbs = new byte[1024]; NetworkStream tcs = tc.GetStream(); while (!eof) { if (tcs.CanWrite) { int readbytes = fs.Read(fbs, 0, fbs.Length); tcs.Write(fbs, 0, readbytes); writebytes += readbytes; if (readbytes < fbs.Length) { eof = true; } int process = (int)(100 * writebytes / fs.Length); this.BeginInvoke(h, d, ap.filename, false, process); } else { Thread.Sleep(10); } } } tc.Close(); } Thread.Sleep(500); deviceListener.FilePlayStop(d); this.BeginInvoke(h, d, ap.filename, true, 0); dataThread = null; } catch (ThreadAbortException abortException) { //this.BeginInvoke(h, ap.filename, true, 0); if (tc != null) { tc.Close(); } dataThread = null; } }
private void IntercomThread(object obj) { int sequenceNumber = 0; long timestamp = 0; short[] inputbuffer = new short[2048]; DeviceListener.Device d = deviceListener.Find(DeviceID); UdpClient udpserver; try { udpserver = new UdpClient(9999); } catch (Exception ex) { MessageBox.Show("port is in use"); return; } int port = 9999; DelegateShowWave h = new DelegateShowWave(ShowWave); int ret; ret = deviceListener.IntercomStart(d, "0.0.0.0", port, "g711-u", "rtp", 100, 20, "mic", "disable"); 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(DeviceID.ToString()) == false) { return; } dev_ip = ipep.Address.ToString(); dev_port = ipep.Port; 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; 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 = g711.g711Decode_ulaw(packet.Payload); this.Invoke(h, pcmbuf1); } } } } } catch (ThreadAbortException abortException) { deviceListener.IntercomStop(d); udpserver.Close(); } }
private void button9_Click(object sender, EventArgs e) { DeviceListener.Device d = deviceListener.Find(DeviceID); if (d == null) { panel2.BackColor = SystemColors.ControlDark; label5.Text = string.Format("DEVICE ID({0:D8} IS NOT EXITS)", d.id); return; } panel2.BackColor = SystemColors.Control; int okpack = 0; int faultpack = 0; UdpClient uc = new UdpClient(); byte[] cmd = new byte[] { 0xff, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x01, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x18, 0x15 }; //IPAddress ipaddr = IPAddress.Parse(d.peerip); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(d.peerip), 65276); IPEndPoint rep = new IPEndPoint(IPAddress.Any, 0); uc.Client.ReceiveTimeout = 1000; try { for (int i = 0; i < 1000; i++) { uc.Send(cmd, cmd.Length, ep); byte[] rb = uc.Receive(ref rep); if (rb != null) { if (rep.Address.ToString().Equals(d.peerip)) { okpack++; } else { faultpack++; } } else { faultpack++; } label5.Text = string.Format("PING SEND={0:D}, RECV={1:D}, DISCARD:{2:D}", i + 1, okpack, faultpack); if (faultpack > 4) { break; } Application.DoEvents(); } } catch (Exception ex) { } if (okpack == 1000) { panel2.BackColor = Color.LimeGreen; PlaySuccessNotify(); } else { panel2.BackColor = Color.Red; PlayFaultNotify(); } }
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(); } }