如何将从麦克风录制的音频数据发布到摄像机 [英] How Do I Post Audio Data Which Is Being Recorded From Microphone To Camera

查看:122
本文介绍了如何将从麦克风录制的音频数据发布到摄像机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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







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;
                }

            }
        }





异常详情:



System.Net.Sockets.SocketException被捕获

消息=已建立的连接被主机中的软件中止

Source =System

ErrorCode = 10053

NativeErrorCode = 10053

StackTrace:

at System。 System.SNet.Sockets.Socket.Send(Byte []缓冲区,Int32大小,SocketFlags) socketFlags)

at the sound_stream.Form1.waveSource_DataAvailable(Object sender,WaveInEventArgs e)in C:\ Users\abc\Documents\Visual Studio 2008 \Projects \ _sound \ _sound stream \Form1.cs:第78行

InnerException:



我在stackoverflow上发布了这个问题这是链接,有人向我们建议HttpClient或Webclient,所以我可以使用它们发送音频,如果是,那么如何?

请帮忙。



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.

推荐答案

这篇关于如何将从麦克风录制的音频数据发布到摄像机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆