/// <summary> /// 对讲开启 /// </summary> public static void AudioPaly() { Tools.AudioIsEnd = true; AudioThread = new Thread(Playaudio) { IsBackground = true }; AudioThread.Start(); AudioSendThread = new Thread(SendAudio) { IsBackground = true }; AudioSendThread.Start(); RecorderThread = new Thread(Recorder.StartRecording) { IsBackground = true }; RecorderThread.Start(); void Playaudio() { while (Tools.AudioIsEnd) { if (Tools.audioQueue.Count > 0) { Tools.audioQueue.TryDequeue(out byte[] AudioByte); if (AudioByte.Length > 10000) { continue; } byte[] G711data = AudioByte.Skip(4).ToArray(); byte[] PCMbuffer = Decode(G711data, 0, G711data.Length); AddDataToBufferedWaveProvider(PCMbuffer, 0, PCMbuffer.Length); } else { Thread.Sleep(5); } } } void SendAudio() { index = 0; time = 1000000000000000; while (Tools.AudioIsEnd) { if (Tools.RecorderQueue.Count > 1) { Tools.RecorderQueue.TryDequeue(out byte[] AudioByte); Tools.RecorderQueue.TryDequeue(out byte[] AudioByte2); try { PCM = AudioByte.Concat(AudioByte2).ToArray(); for (int i = 0; i < 5; i++) { G711A = new byte[324]; G711A = his.Concat(Recorder.Encode(PCM.Skip(i * 640).Take(640).ToArray(), 0, 640)).ToArray(); byte[] RtpBuffer = RTP.Encode(new RTPBody() { state = new byte[] { 48, 49, 99, 100 }, Vpxc = 129, MPT = 134, index = index, hSimNumber = Extension.ToBCD("013304521781"), chanle = 6, type = 51, time = split(time), length = 324, data = G711A }); index += 1; time += 40; AsynSend(Connect.Audioclient, RtpBuffer); Thread.Sleep(20); } byte[] split(long times) { int i = 0; List <byte> list = new List <byte>(); string str = times.ToString(); while (i < 16) { list.Add(byte.Parse(str.Substring(i, 2))); i += 2; } return(list.ToArray()); } } catch { } } else { Thread.Sleep(5); } } } }
private void button2_Click(object sender, EventArgs e) { // Hemos pulsado boton conectar // Guardamos el usuario del chat if (richTextBox2.Text == "") { richTextBox2.Text = "Anonymous"; richTextBox2.Enabled = false; username = "******"; } else { username = richTextBox2.Text; richTextBox2.Enabled = false; } button2.Enabled = false; button3.Enabled = true; richTextBox1.Enabled = true; button1.Enabled = true; listBox1.Items.Clear(); listBox1.Items.Add(String.Format("*** Bienvenido {0} a la sala de chat ***", username)); // Inicializamos los canales de comunicación try { // Transmision de video videoserver = new UdpClient(); videoremote = new IPEndPoint(multicast, videoport); // Implementando mi clase RTP video = new RTP("video1", videoserver, multicast, videoremote); // Timer para los paq/s timer1 = new Timer(); timer1.Tick += new EventHandler(analyzeVideo); // Sacamos parametros de la TX timer1.Interval = 1000; // Cada 1000 ms timer1.Start(); // Transmision audio audioserver = new UdpClient(); audioremote = new IPEndPoint(multicast, audioport); // Implementacion mi clase RTP audio = new RTP("audio1", audioserver, multicast, audioremote); // Iniciamos el audio InitAudioCapture(); audiosender = new Thread(new ThreadStart(SendAudio)); // Timer para los paq/s timer2 = new Timer(); timer2.Tick += new EventHandler(analyzeAudio); // Sacamos parametros de la TX timer2.Interval = 1000; // Cada 1000 ms timer2.Start(); // Enviar chat chat1server = new UdpClient(); chat1remote = new IPEndPoint(multicast, chat1port); chat1server.JoinMulticastGroup(multicast); // Recibir chat chat2server = new UdpClient(chat2port); chat2remote = null; chat2server.JoinMulticastGroup(multicast); checkBox4.Checked = true; Thread t = new Thread(this.ChatThread); t.Start(); CheckForIllegalCrossThreadCalls = false; } catch (Exception excp) { MessageBox.Show(excp.ToString()); } finally { checkBox1.Checked = true; } }