your way create your voice chat systems - fadi abdel-qader طريقك لعمل محادثة...

Upload: -

Post on 30-May-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    1/19

    Voice ChatBy Using Microsoft Visual C#

    http://www.enashir.com/blogs/fadi

    1

    http://www.enashir.com/blogs/fadihttp://www.enashir.com/blogs/fadi
  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    2/19

    FADI Abdel-qader Dot Net Networks & TCP/IP Programming

    Chapter 11Voice Over IP Programming

    A.The Concept & Requirements of VoiceCommunication Systems

    B.How to Create a Voice Chat Throw DotNet Using Unmanaged APIs Functions

    C.Testing UDP Multicasting, TCP andThinking in SCTP to Transfer Voice ThrowNetworks

    D. How to Create a Voice ConferenceSystem Using Microsoft Direct Play 9

    2

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    3/19

    :

    IP BitsByte Array Datagram

    PacketsPacketsByte Array FIFO First In First

    Out...

    MicrosoftTransport Layer Dot Net

    Windows XP UDPTCP Windows XP

    ...

    :of Voice Communication SystemsRequirementsThe

    TCP UDP...

    :Stream-1Delivered on Sequence2Voice Compression3

    31 KB\S.8.000 KHz

    - TCP UDP

    :TCP TCP

    Conference SystemMulticastMulticastingBroadcasting TCPUDP TCP Oriented

    ProtocolMulticast ConferenceSystemBroadcasting..

    UDP.

    :UDP Delivered on Sequence Fragmented PacketsEthernet Encapsulation

    1500KB Packet DatagramEncapsulation EthernetFragmented

    Fragments

    UDP ITU-International Telecommunications UnionH.323

    RTP RealIETF Internet Engineering Task Force

    Time Transport Protocol UDP StreamTCP UDP

    3

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    4/19

    IPStreamDelivered on Sequence

    MulticastingBroadcasting SCTPStream Control Transmission ProtocolWindows

    LinuxMicrosoft Windows

    .

    ::The Concept Of Voice Communication

    BitsSampling

    :

    1- Application LayerBits Audio Compression Teachings

    .

    2- Transport Layer UDP TCP

    Stream UDPStream

    Delivered on Sequence Fragmentation Buffer Fragments

    Ethernet Packet 1500 KB Packet

    UDPStreamH.323

    Stream TCP IP Multicasting

    Broadcasting

    3- Network Layer Packets UDPMulticastingBroadcasting

    4- Data Link Layer Ethernet Ethernet

    Frame 1500 KB

    5- Physical Layer Delay Bits

    .

    Bits Physical Layer Data LinkLayer Network Layer Packets

    Unicast

    Multicast Broadcast Packets

    4

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    5/19

    Packets Acknowledgment Packets

    Transport Layer

    TCP UDP

    Packets

    Packets.

    PacketsEthernet Wireless LANIP MulticastingUDP:

    :to Create a Voice Chat Throw Dot Net Using UnmanagedHow

    APIs Functions:

    DLL

    winmm.dll Multimedia Methods

    Byte Array Buffer Method:

    waveInGetNumDevs

    Sound Card.waveInAddBuffer BitsByte Array

    Buffer Method :

    waveInAddBuffer(IntPtr hwi, ref WaveHdr pwh, int cbwh)Reference

    Buffer Buffer

    waveInClosewaveInOpen.waveInPrepareHeader Buffer

    .waveInAddBuffer

    5

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    6/19

    waveInUnprepareHeader Buffer Buffer.waveInReset Pointer BufferwaveInStopwaveInStart

    .

    DllImport

    :

    [DllImport(winmm.dll)]publicstaticexternint waveInGetNumDevs();[DllImport(winmm.dll)]publicstaticexternint waveInAddBuffer(IntPtr hwi, refWaveHdr pwh, intcbwh);[DllImport(winmm.dll)]publicstaticexternint waveInClose(IntPtr hwi);[DllImport(winmm.dll)]publicstaticexternint waveInOpen(out IntPtr phwi, int uDeviceID,WaveFormat lpFormat, WaveDelegate dwCallback, int dwInstance, intdwFlags);[DllImport(winmm.dll)]publicstaticexternint waveInPrepareHeader(IntPtr hWaveIn, refWaveHdrlpWaveInHdr, int uSize);[DllImport(winmm.dll)]publicstaticexternint waveInUnprepareHeader(IntPtr hWaveIn, refWaveHdrlpWaveInHdr, int uSize);[DllImport(winmm.dll)]publicstaticexternint waveInReset(IntPtr hwi);

    [DllImport(winmm.dll)]publicstaticexternint waveInStart(IntPtr hwi);[DllImport(winmm.dll)]publicstaticexternint waveInStop(IntPtr hwi);

    Methods Byte Array Buffer:

    [DllImport(winmm.dll)]publicstaticexternint waveOutGetNumDevs();

    [DllImport(winmm.dll)]publicstaticexternint waveOutPrepareHeader(IntPtr hWaveOut, refWaveHdrlpWaveOutHdr, int uSize);[DllImport(winmm.dll)]publicstaticexternint waveOutUnprepareHeader(IntPtr hWaveOut, refWaveHdr lpWaveOutHdr, int uSize);[DllImport(winmm.dll)]publicstaticexternint waveOutWrite(IntPtr hWaveOut, refWaveHdrlpWaveOutHdr, int uSize);[DllImport(winmm.dll)]

    publicstaticexternint waveOutOpen(out IntPtr hWaveOut, int uDeviceID,WaveFormat lpFormat, WaveDelegate dwCallback, int dwInstance, intdwFlags);

    6

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    7/19

    [DllImport(winmm.dll)]publicstaticexternint waveOutReset(IntPtr hWaveOut);[DllImport(mmdll)]publicstaticexternint waveOutClose(IntPtr hWaveOut);[DllImport(mmdll)]publicstaticexternint waveOutPause(IntPtr hWaveOut);[DllImport(mmdll)]

    publicstaticexternint waveOutRestart(IntPtr hWaveOut);[DllImport(mmdll)]publicstaticexternint waveOutGetPosition(IntPtr hWaveOut, outint lpInfo,int uSize);[DllImport(mmdll)]publicstaticexternint waveOutSetVolume(IntPtr hWaveOut, int dwVolume);[DllImport(mmdll)]publicstaticexternint waveOutGetVolume(IntPtr hWaveOut, outintdwVolume);

    Buffer:

    using System;using System.Runtime.InteropServices;using System.Resources;using System.IO;

    publicclass Winmm{

    publicconst UInt32 SND_ASYNC = 1;publicconst UInt32 SND_MEMORY = 4;

    [DllImport("Winmm.dll")]publicstaticexternbool PlaySound(byte[] data, IntPtr hMod,

    UInt32 dwFlags);public Winmm(){}

    publicstaticvoid PlayWavResource(byte[] buffer){

    PlaySound(buffer, IntPtr.Zero, SND_ASYNC | SND_MEMORY);}

    }

    PlaySound Method Byte BufferMethod Socket :

    void Voice_Receiver(){

    UdpClient sock = new UdpClient(5020);sock.JoinMulticastGroup(IPAddress.Parse(multicast_IP.Text));IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);byte[] voice_Come = sock.Receive(refiep);

    Winmm.PlayWavResource(voice_Come);sock.Close();}

    7

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    8/19

    :Voice Chat System

    Buffer Classes MicrosoftWindows Sound Recorder Classes

    Voice Library Classes Classes :

    WaveIn Class MethodsByte Array

    WaveOut Clasas BufferWaveStream ClassStreamMemoryStream

    FifoStream StreamWaveNative Methods

    winmm.dl .

    UDPFull Diplex Unicast Voice Chat System

    :

    :

    ClassesVoice.dll References

    Using :

    using System.Net;

    using System.Net.Sockets;using System.Threading;using Voice;

    8

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    9/19

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    10/19

    //for Recorderif(m_RecBuffer == null || m_RecBuffer.Length < size)m_RecBuffer = newbyte[size];System.Runtime.InteropServices.Marshal.Copy(data, m_RecBuffer, 0, size);

    //Microphone ==> data ==> m_RecBuffer ==> m_Fifosocket.SendTo(m_RecBuffer, newIPEndPoint(IPAddress.Parse(Peer_IP.Text),5030));

    } Full DuplexTow Ports

    Port ...Port

    :privatevoid Start(){

    Stop();try{WaveFormat fmt = new WaveFormat(44100, 16, 2);m_Player = new WaveOutPlayer(-1, fmt, 16384, 3, newBufferFillEventHandler(Filler));m_Recorder = new WaveInRecorder(-1, fmt, 16384, 3, newBufferDoneEventHandler(Voice_Out));}catch{Stop();throw;

    }}

    :

    privatevoid Stop(){if(m_Player != null)

    try{m_Player.Dispose();

    }finally{m_Player = null;}

    if(m_Recorder != null)try{m_Recorder.Dispose();}finally{m_Recorder = null;

    10

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    11/19

    }m_Fifo.Flush();// clear all pending data

    } Voice BufferSocket:

    privatevoid Filler(IntPtr data, int size){

    if(m_PlayBuffer == null || m_PlayBuffer.Length < size)m_PlayBuffer = newbyte[size];if(m_Fifo.Length >= size)m_Fifo.Read(m_PlayBuffer, 0, size);elsefor (int i = 0; i < m_PlayBuffer.Length; i++)m_PlayBuffer[i] = 0;System.Runtime.InteropServices.Marshal.Copy(m_PlayBuffer, 0, data, size);

    // m_Fifo ==> m_PlayBuffer==> data ==> Speakers}

    :UDP and Thinking in SCTP to Transfer Voice Throw,Testing TCPNetworks:

    Multicast Half Duplex Voice Chat System Multicasting

    :

    11

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    12/19

    )( IP Multicasting :

    privatevoid Voice_Out(IntPtr data, int size)

    {//for Recorderif(m_RecBuffer == null || m_RecBuffer.Length < size)

    m_RecBuffer = newbyte[size];System.Runtime.InteropServices.Marshal.Copy(data, m_RecBuffer, 0, size);

    //Microphone ==> data ==> m_RecBuffer ==> m_Fifosocket.SendTo(m_RecBuffer, new IPEndPoint(IPAddress.Parse("224.0.1.7"),5020));}

    IP Multicast Group :

    privatevoid Voice_In(){UdpClient sock = new UdpClient(5000);sock.JoinMulticastGroup(IPAddress.Parse("224.0.1.7"));IPEndPoint iep = new IPEndPoint(IPAddress.Any,0);

    while (true){m_Fifo.Write(sock.Receive(refiep), 0,sock.Receive(refiep).Length);}

    }

    ...

    UDP Multicasting Packets

    UDP ...

    TCP Oriented Protocol

    Stream

    Fragmentation

    ...Delivered Out on Sequence Socket :

    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream ,ProtocolType.Tcp);

    Network Stream Class :

    privatevoid Voice_Out(IntPtr data, int size){//for Recorder

    12

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    13/19

    if(m_RecBuffer == null || m_RecBuffer.Length < size)m_RecBuffer = newbyte[size];System.Runtime.InteropServices.Marshal.Copy(data, m_RecBuffer, 0, size);

    //Microphone ==> data ==> m_RecBuffer ==> m_Fifosock.Connect(new IPEndPoint(IPAddress.Parse("10.0.0.10"),5020));NetworkStream ns = new NetworkStream (socket);ns.Write (m_RecBuffer,0,m_RecBuffer.Length);

    } Network Stream :

    privatevoid Voice_In(){

    Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream , ProtocolType.Tcp);sock.Bind(new IPEndPoint(IPAddress.Parse("10.0.0.10"),5020));NetworkStream ns = new NetworkStream (sock);while (true){byte[] buffer = newbyte [16384];ns.Read (buffer,0,16384);m_Fifo.Write(buffer, 0, buffer.Length); }

    }

    Multicasting Broadcasting... UDP

    TCPLong HornMicrosoft SCTP...

    TCP UDP Multicasting Fragmentation

    Delivered on Sequence Header :

    13

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    14/19

    :tHow to Create a Voice Conference System Using Microsof9Direct Play:

    Direct Play9MicrosoftDirectPlay Transport Protocol DirectX

    Network Games

    NovelIPXTCP/IP DirectPlay Transport Protocol:

    -Reliable delivery of messages-Sequential a delivery of messages-Send prioritization-Streaming SessionStream Data

    Microsoft SCTP TCP UDP

    Transport Layer Direct Play Classes :

    : Connect Classes: Address , Guid , Peer ClassesDirectPlayHash Code

    Guid Class Peer Class

    Socket TCP/IP :

    using Microsoft.DirectX.DirectPlay;

    .

    .

    .Address hostAddress = new Address();hostAddress.ServiceProvider = Address.ServiceProviderTcpIp;

    // Select TCP/IP service provider

    ApplicationDescription dpApp = new ApplicationDescription();appGuid = Guid.NewGuid(); // Create a GUID for the applicationdpApp.GuidApplication = appGuid; // Set the application GUID

    dpApp.SessionName = "My Session"; // Optional Session Name

    myPeer.Host(dpApp, hostAddress); // Begin hosting

    IPXTCP/IP IP Address :

    using Microsoft.DirectX.DirectPlay;ServiceProviderInfo[] mySPInfo;Peer myPeer;

    System.Windows.Forms.ListBox listBox1;ApplicationDescription myAppDesc;.

    14

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    15/19

    .myPeer = new Peer();hostAddress = new Address();peerAddress = new Address();

    // Set the service provider to TCP/IPpeerAddress.ServiceProvider = Address.ServiceProviderTcpIp;

    hostAddress.ServiceProvider = Address.ServiceProviderTcpIp;

    // Attach FindHostResponseEventHandler to receiveFindHostResponseMessagesmyPeer.FindHostResponse += newFindHostResponseEventHandler(myEnumeratedHosts);

    // Call FindHosts to start the enumerationmyPeer.FindHosts(myAppDesc, hostAddress, peerAddress, null, 10, 0, 0,

    FindHostsFlags.OkToQueryForAddressing);

    TCP/IP FindHosts Method Peer Class

    Connect Method Peer Class:

    using Microsoft.DirectX.DirectPlay;// Structure for FindHostResponseMessagespublicstructHostInfo{

    public ApplicationDescription appdesc;

    public Address deviceAddress;public Address senderAddress;

    }...Peer myPeer = new Peer();HostInfo hostinfo = new HostInfo();

    // The FindHostResponseEventHandlerpublicvoid myEnumeratedHosts(object o, FindHostResponseEventArgs args){

    hostinfo.appdesc = args.Message.ApplicationDescription;hostinfo.deviceAddress = args.Message.AddressDevice;hostinfo.senderAddress = args.Message.AddressSender;

    }

    // Attach the ConnectCompleteEventHandler to receiveConnectCompleteMessages

    myPeer.ConnectComplete += newConnectCompleteEventHandler(OnConnectComplete);

    15

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    16/19

    // Call connect passing the Host information returned in theFindHostResponse eventmyPeer.Connect(hostinfo.appdesc, hostinfo.deviceAddress,hostinfo.senderAddress, null, ConnectFlags.OkToQueryForAddressing);

    :Voice.DirectPlay.tXDirec.Microsoft:

    :

    Classes

    1- Classes Server Sessions:Server Class :

    Server server = new Voice.Server(peerObject);

    16

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    17/19

    Peer Object Peer Class Server Object Methods

    :

    StartSession Method SessionDescription:

    //set up session description for the voice server

    Voice.SessionDescription sessionDesc = new Voice.SessionDescription();sessionDesc.BufferAggressiveness = Voice.BufferAggressiveness.Default;sessionDesc.BufferQuality = Voice.BufferQuality.Default;sessionDesc.Flags = 0;sessionDesc.SessionType = type;sessionDesc.GuidCompressionType = compressionType;

    Peer ObjectMethodVoice Session VoiceSession TypeCompression Type :

    protectedvoid CreateVoiceSession(Peer dpp, Voice.SessionType type, GuidcompressionType)

    {try

    {//set up session description for the voice serverVoice.SessionDescription sessionDesc = new Voice.SessionDescription();sessionDesc.BufferAggressiveness = Voice.BufferAggressiveness.Default;sessionDesc.BufferQuality = Voice.BufferQuality.Default;sessionDesc.Flags = 0;sessionDesc.SessionType = type;

    sessionDesc.GuidCompressionType = compressionType;

    //start the sessiontry

    {server.StartSession(sessionDesc);mIsHost = true;mInSession = true;

    }catch(DirectXException dxe)

    {throw dxe;

    }

    17

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    18/19

  • 8/14/2019 Your Way Create Your Voice Chat Systems - FADI Abdel-Qader

    19/19

    int[] xmitTargets = newint[1];xmitTargets[0] = (int) PlayerID.AllPlayers;client.TransmitTargets = xmitTargets;

    //get sound device config to check for half-duplexsoundConfig = client.SoundDeviceConfig;mHalfDuplex = ((soundConfig.Flags & Voice.SoundConfigFlags.HalfDuplex)

    != 0);

    }catch(Exception e){throw e;}

    }}

    TCP UDP DirectPlay Direct Play

    Direct Play Voice Over IP SCTP

    Linux...

    Voice ChatTransport Layer

    ClassesDirectPlay Transport Protocol.

    TCP/IP:

    http://www.enashir.com/blogs/fadi

    -))C

    http://www.enashir.com/blogs/fadihttp://www.enashir.com/blogs/fadi