Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
i am trying to send audio to a AXIS M1054 Network Camera. When i start my application, it after sending 6 packets to the camera shows an error:"An established connection was aborted by the software in your host machine"

here's the code


C#
namespace sound_stream
        {
            public partial class Form1 : Form
            {
                public static IWaveIn waveSource = null;
                public Socket cameraAudioSocket = null;
                public IPEndPoint remoteEndPoint = null;
                public string camera_address = "192.168.1.22";
                public int camera_port = 81;
                public byte[] allData = null;
                private MemoryStream mstream = new MemoryStream();
                
                public Form1()
                {
                    InitializeComponent();
                }

                private void start_Click(object sender, EventArgs e)
                {
                    try
                    {
                        start.Enabled = false;
                        stop.Enabled = true;
                        waveSource = new WaveIn();
                        waveSource.WaveFormat = new WaveFormat(8000, 1);
                        CameraSetup();
                        if (cameraAudioSocket == null)
                        {
                            cameraAudioSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                            IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(camera_address), camera_port);
                            cameraAudioSocket.Connect(remoteEndPoint);

                        }

                        waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
                        waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);
                       
                        waveSource.StartRecording();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(null, "Error: " + ex.ToString(), "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                }
              
                void waveSource_DataAvailable(object sender, WaveInEventArgs e)
                {
                    try
                    {
                           byte[] encoded = G711.Encode_uLaw(e.Buffer, 0, e.BytesRecorded);
                           cameraAudioSocket.Send(encode,encoded.Length, SocketFlags.None);
                                             
                    }
                    catch (SocketException er)
                    {
                        status.Text += er.Message;
                        //MessageBox.Show(null, "Error: " + er.ToString(), "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                }
                void CameraSetup()
                {
                    try
                    {
                        // Create the audio socket as a streaming, internetwork (IP) socket using the TCP protocol.
                        cameraAudioSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        
                        // Establish a remote endpoint at the Camera
                        IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(camera_address), camera_port);
                        
                        // Connect the audio socket to the remote endpoint
                        cameraAudioSocket.Connect(remoteEndPoint);

                        // Create the HTTP POST command header in order to initiate the transfer of audio data
                        string httpPostString =
                        "POST /axis-cgi/audio/transmit.cgi HTTP/1.0" + Environment.NewLine
                        + "Content-Type: audio/basic" + Environment.NewLine
                        + "Content-Length: 9999999" + Environment.NewLine
                        + "Connection: Keep-Alive" + Environment.NewLine
                        + "Cache-Control: no-cache" + Environment.NewLine
                        + "Authorization: root pass" + Environment.NewLine + Environment.NewLine;
                        

                        // Convert the data into byte data
                        byte[] httpPostData = Encoding.Unicode.GetBytes(httpPostString);

                        // Send the byte data through the socket to the Camera. A return value of -1 indicates failure.
                        if (cameraAudioSocket.Send(httpPostData) == -1)
                            status.Text += "Error sending initial packet";
                    }
                    catch (SocketException err)
                    {
                        status.Text += err.Message;
                        //MessageBox.Show(null, "Error: " + err.ToString(), "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                    
                }
                
                void waveSource_RecordingStopped(object sender, EventArgs e)
                {
                    if (waveSource != null)
                    {
                        waveSource.Dispose();
                        waveSource = null;
                    }

                }

                private void stop_Click_1(object sender, EventArgs e)
                {
                    stop.Enabled = false;
                    waveSource.StopRecording();
                    start.Enabled = true;
                    cameraAudioSocket.Close();
                }

                private void clear_Click(object sender, EventArgs e)
                {
                    status.Text = null;
                }

            }
        }


Exception details:

System.Net.Sockets.SocketException was caught
Message="An established connection was aborted by the software in your host machine"
Source="System"
ErrorCode=10053
NativeErrorCode=10053
StackTrace:
at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 size, SocketFlags socketFlags)
at sound_stream.Form1.waveSource_DataAvailable(Object sender, WaveInEventArgs e) in C:\Users\abc\Documents\Visual Studio 2008\Projects\sound\sound stream\Form1.cs:line 78
InnerException:

I posted the question on stackoverflow Here's the Link and someone suggested to us HttpClient or Webclient so can i use them to send audio and if yes then how??
please help.
Posted
Updated 9-Jul-14 22:50pm
v4
Comments
Bernhard Hiller 10-Jul-14 9:21am    
"send audio to a ... camera" - eh? What are you trying to do? What do you expect a camera to do with audio data?
pawany16 11-Jul-14 0:51am    
It's a Ip network camera and support's duplex audio

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900