Search code examples
c#voipvoice

Real time voice chat over IP using C#


I am try to make a VOice chat over IP using C#. First I have connected two laptops. Then I have sent message between two laptops. Then I try to send vice message it is difficult. I counld get microphone voice input using Naudio. Then I should do sampling and put wave file into byte array and make data package and send it. In client side I should catch the data packet and conver it to sound. But the problem is I cant find any material to learn regarding code for to convert and send data package. I search Internet but I couldnt find anything that can I understand. SO please anyone help me to do this. I see many projects that people do but those are difficult to understand. I am beginer to about this sending data.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;``
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Tutorial7
{
   public partial class Form1 : Form
   {
       Socket sock;
       Socket acc;
       public Form1()
       {
           InitializeComponent();
       }

       Socket socket()
       {
           return new Socket(AddressFamily.InterNetwork, SocketType.Stream, 
           ProtocolType.Tcp);
       }

       private void button1_Click(object sender, EventArgs e)
       {
           List<NAudio.Wave.WaveInCapabilities> sources = new 
           List<NAudio.Wave.WaveInCapabilities>();

           for (int i = 0; i < NAudio.Wave.WaveIn.DeviceCount; i++)
           {
               sources.Add(NAudio.Wave.WaveIn.GetCapabilities(i));
           }

           sourceList.Items.Clear();

           foreach (var source in sources)
           {
               ListViewItem item = new ListViewItem(source.ProductName);
               item.SubItems.Add(new ListViewItem.ListViewSubItem(item, 
                   source.Channels.ToString()));
               sourceList.Items.Add(item);
           } 
       }

       NAudio.Wave.WaveIn sourceStream = null;
       NAudio.Wave.DirectSoundOut waveOut = null;
       NAudio.Wave.WaveFileWriter waveWriter = null;

       private void button2_Click(object sender, EventArgs e)
       {
           if (sourceList.SelectedItems.Count == 0) return;

           int deviceNumber = sourceList.SelectedItems[0].Index;

           sourceStream = new NAudio.Wave.WaveIn();
           sourceStream.DeviceNumber = deviceNumber;
           sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, 
           NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);

           NAudio.Wave.WaveInProvider waveIn = new 
           NAudio.Wave.WaveInProvider(sourceStream);
        }
    }
}

Solution

  • I think you can use System.Net.Sockets.NetworkStream Class:

    Have a look at: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.networkstream?redirectedfrom=MSDN&view=netframework-4.7.2

    And this is a similar question: get stream of a socket object in c#