C# 語音聊天系統

ssihc0發表於2011-03-04
以前寫過一個點對點的語音對講,說下思路
傳送端 音訊採集 -- 音訊編碼 -- 網路傳輸
接受端 網路接收 -- 音訊解碼 -- 音訊輸出

需要要的話聯絡我,編碼我直接用網上找的一個 LumiSoft 音訊編碼用的G.711

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using LumiSoft.Media.Wave;
using LumiSoft.Media.Wave.Native;
using LumiSoft.Net.Media.Codec.Audio;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace testAudio
{
    public partial class Form1 : Form
    {
        private WaveIn m_waveIn = null;
        private WaveOut m_waveOut = null;
        
        private Socket socket = null;
        private IPEndPoint localEP = null;
        private IPEndPoint remoteEP = null;

        private AudioCodec m_audioCodec = null;

        private bool m_bAlive = true;

        public Form1()
        {
            InitializeComponent();
            
            m_audioCodec = new G711_alaw();

            localEP = new IPEndPoint(IPAddress.Any, 15888);
            remoteEP = new IPEndPoint(IPAddress.Parse("192.168.0.225"), 15888);
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Bind(localEP);

            m_waveOut = new WaveOut(WaveOut.Devices[0], 8000, 16, 1);
            
            Thread recvThread = new Thread(new ThreadStart(OnRecv));
            recvThread.Start();
        }

        private void OnRecv()
        {
            byte [] buf = new byte[2000];
            
            while(m_bAlive)
            {
                IPEndPoint ep = new IPEndPoint(IPAddress.Any,0);
                EndPoint sender = (EndPoint)ep;
                int nReceved = socket.ReceiveFrom(buf, ref sender);
                byte[] sourceBuf = new byte[nReceved];
                for (int i = 0; i < nReceved; i++)
                {
                    sourceBuf[i] = buf[i];
                }

                if (nReceved <= 0)
                {
                    Thread.Sleep(1);
                }
                else
                {
                    byte[] decodeData = m_audioCodec.Decode(sourceBuf, 0, sourceBuf.Length);
                    if (m_waveOut != null)
                    {
                        m_waveOut.Play(decodeData, 0, decodeData.Length);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (m_waveIn == null)
            {
                m_waveIn = new WaveIn(WaveIn.Devices[0], 8000, 16, 1, 500);
                m_waveIn.BufferFull += new BufferFullHandler(m_waveIn_BufferFull);
                m_waveIn.Start();
            }
            else
            {
                m_waveIn.Dispose();
                m_waveIn = null;
            }
        }

        private void m_waveIn_BufferFull(byte[] buf)
        {
            try
            {
                if(m_audioCodec != null)
                {
                    byte[] encodeData = m_audioCodec.Encode(buf, 0, buf.Length);
                    socket.SendTo(encodeData, remoteEP);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }
}

相關文章