vicky
vicky

Reputation: 37

how to send mu-law (G.711) codec wav file on a network SIP phone and play there

I am struggling to send a mu-law (G.711) codec wav file as binary data over a network on a SIP phone and want to play this wav file on that SIP phone. Below is the code in C#. Can anybody tell me what I am doing wrong?

private void MediaStreamPump()
    {
        String strHostName;

        m_MediaStreamPumping = true;

        strHostName = Dns.GetHostName();
        IPHostEntry ipEntry = Dns.GetHostByName(strHostName);

        IPEndPoint endPt = new IPEndPoint(ipEntry.AddressList[0], 7078);

        Socket receiveSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


        receiveSock.Bind(endPt);

        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
        EndPoint senderRemote = (EndPoint)sender;
        IPAddress[] sendToEntry=Dns.GetHostAddresses(m_RemoteEndpointIp);
        IPEndPoint sendToIp=new IPEndPoint(ipEntry.AddressList[0],m_RemoteEndpointPort);
        EndPoint otherEndpoint = (EndPoint)sendToIp;
        receiveSock.ReceiveTimeout = 1000;


        FileStream fileStream = new FileStream("E:\\G711NM.wav", FileMode.Open, FileAccess.Read);

         br = new BinaryReader(fileStream);
         try
            {
             byte[] buf = new byte[512]; 
              int count=1;
             // 32k
             while (true)
             {
               var buf1 = new byte[512];
                count = br.Read(buf1, 0, buf1.Length);
               if (count > 0)
               {

                 receiveSock.ReceiveFrom(buf, SocketFlags.None, ref senderRemote);

                 receiveSock.SendTo(buf1, SocketFlags.None, senderRemote);

                }
                else
                 break;

               }
             }
            catch (Exception ex)
             {
             MessageBox.Show(ex.ToString());
             }

        receiveSock.Close();
        m_MediaStreamPumping = false;

     }

Upvotes: 2

Views: 2166

Answers (1)

devgeezer
devgeezer

Reputation: 4189

You might expect that your request is simple, but as you're about to find out, there is a bunch of protocol "glue" that you're missing.

What are you missing? There's the SIP handshaking protocol that you're missing, as well as the SDP that you're missing that specifies the RTP streaming info of the data (the part that you have a rough start on above.) All these protocol messages have to be formatted correctly to be interpreted by your phone: for reference here's the RTP header structure. The SIP/SDP messages establish information about the RTP endpoints including port numbers and codec selection that happen outside of the SIP traffic. The RTP formatting also includes breaking the media data up into sequenced packets that have a header (that indicates the sequence number) in addition to an ordinarily small binary payload of the media stream (i.e. the raw data from the WAV file.)

My recommendation is to pick up a packet-sniffer tool like wireshark, and examine the packet information of a softphone -or- hook an existing SIP phone into a hub where you can observe the SIP,SDP & RTP traffic. This will provide some insights into the formats and the back-and-forth messaging involved. You could also try configuring an open-source PBX like asterisk that would let you configure a network of soft phones (you could get by with a network of just one or two softphones).

I suggest checking codeplex for SIP projects that will let you skip most of the SIP/SDP drudgery. While I cannot recommend any one of these in specific, I did manage to use SIP.NET to interact with an asterisk server that I configured about 5 years ago - mostly as a proof of concept for a company I used to work for. I hope this hurdle doesn't diminish your enthusiasm, but you do have a fair way to go before you'll be hearing your wav file played over your SIP phone.

Upvotes: 1

Related Questions