2013-12-08 2 views
2

저는 2 명의 사용자가 TCP를 사용하여 화상 회의를 할 수있게 해주는 화상 회의 응용 프로그램 (C#으로 작성)을 만들려고합니다. 또한 사용자는 텍스트 채팅을 별도로 할 수 있습니다. 현재 작업중인 비디오 스트림이 있지만 아직 오디오가 작동하지 않습니다. 나는 마이크에 접근하는 방법, TCP를 사용하여 스트리밍하는 방법, 그리고 다른 사용자의 스피커에서 재생하는 방법을 확신하지 못한다.TCP를 통해 양방향 오디오를 스트리밍하려고합니까?

누군가가 샘플 코드를 가르쳐 줄 수 있다면 마이크에 액세스하는 방법이나 나를 도울 것이라고 생각하는 다른 사람에게 도움이된다면 도움이 될 것입니다.

참조 용으로 내 코드를 첨부하고 있습니다.

WEBCAM.cs

using System; 
using System.IO; 
using System.Linq; 
using System.Text; 
using WebCam_Capture; 
using System.Windows.Controls; 
using System.Collections.Generic; 
using System.Windows.Media.Imaging; 
using System.Net; 
using System.Net.Sockets; 
using System.Windows; 

namespace DuckTalk 
{ 

class WebCam 
{  
    const int TEXT_VIDEO_NUM = 45674; 
    private System.Windows.Controls.TextBox _hostIpAddressBox; 


    private WebCamCapture webcam; 
    private int FrameNumber = 30; 





    public void InitializeWebCam(ref System.Windows.Controls.TextBox hostIpAddressBox) 
    { 

     webcam = new WebCamCapture(); 
     webcam.FrameNumber = ((ulong)(0ul)); 
     webcam.TimeToCapture_milliseconds = FrameNumber; 
     webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured); 


     _hostIpAddressBox = hostIpAddressBox;    
    } 



    void webcam_ImageCaptured(object source, WebcamEventArgs e) 
    { 
     TcpClient connection = null; 
     NetworkStream stream = null; 
     byte[] imgBytes; 

     try 
     { 
      //Set up IPAddress 
      IPAddress ipAddress = IPAddress.Parse(_hostIpAddressBox.Text); 
      IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, TEXT_VIDEO_NUM); 

      //Connect to TCP 
      connection = new TcpClient(); 
      connection.Connect(ipLocalEndPoint); 

      // Get a client stream for reading and writing. 
      stream = connection.GetStream(); 

      //Send image as bytes 
      imgBytes = ImageByteConverter.ImageToBytes((System.Drawing.Bitmap)e.WebCamImage); 
      stream.Write(imgBytes, 0, imgBytes.Length); 
     } 
     catch (Exception error) 
     { 
      MessageBox.Show("ERROR: " + error.Message); 
     } 
     finally 
     { 
      // Close everything. 
      if (connection != null) 
       connection.Close(); 

      if (stream != null) 
       stream.Close(); 
     }    
    } 


    public void Start() 
    { 
     webcam.TimeToCapture_milliseconds = FrameNumber; 
     webcam.Start(0); 
    } 


    public void Stop() 
    { 
     webcam.Stop(); 
    } 

} 
} 

ImageByteConverter.cs

using System; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Drawing; 
using System.Collections.Generic; 
using System.Windows.Media.Imaging; 


namespace DuckTalk 
{ 
class ImageByteConverter 
{    
    public static byte[] ImageToBytes(System.Drawing.Bitmap bitmap) 
    { 
     byte[] byteArray; 

     using (MemoryStream stream = new MemoryStream()) 
     { 
      bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); 
      stream.Close(); 

      byteArray = stream.ToArray(); 
     } 

     return byteArray; 
    } 

    public static BitmapImage BytesToImage(byte[] imgBytes) 
    { 
     var image = new BitmapImage(); 

     image.BeginInit(); 
     image.StreamSource = new System.IO.MemoryStream(imgBytes); 
     image.EndInit(); 

     return image; 
    } 
} 
} 

Window1.xaml.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.ComponentModel; 
using System.Net; 
using System.Net.Sockets; 

namespace DuckTalk 
{ 
/// <summary> 
/// Interaction logic for Window1.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    const int TEXT_PORT_NUM = 45673; 
    const int TEXT_VIDEO_NUM = 45674; 
    WebCam webcam; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void mainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e) 
    { 
     webcam = new WebCam(); 
     webcam.InitializeWebCam(ref xaml_hostTextBox); 

     var _backgroundIMWorker = new BackgroundWorker(); 
     var _backgroundVidWorker = new BackgroundWorker(); 

     _backgroundIMWorker.WorkerReportsProgress = true; 
     _backgroundVidWorker.WorkerReportsProgress = true; 

     // Set up the Background Worker Events 
     _backgroundIMWorker.DoWork += new DoWorkEventHandler(keepListeningForInstantMessages); 
     _backgroundVidWorker.DoWork += new DoWorkEventHandler(keepListeningForVideoMessages); 

     // Run the Background Workers 
     _backgroundIMWorker.RunWorkerAsync(); 
     _backgroundVidWorker.RunWorkerAsync(); 
    } 

    /////////////////////////////////////////////////////////////////////////////////////////////// 
    // 
    // 
    // The next 2 functions take care of the instant messaging part of the program 
    // 
    // 
    // 
    /////////////////////////////////////////////////////////////////////////////////////////////// 
    private void keepListeningForInstantMessages(object sender, DoWorkEventArgs e) 
    { 
     Action<string> displayIncomingMessage = (incomingMsg) => 
     { 
      xaml_incomingTextBox.Text += "\n\nINCOMING MESSAGE: " + incomingMsg; 
      xaml_incomingTextScroll.ScrollToBottom(); 
     }; 

     Socket connection; 
     Byte[] data; 
     String msg; 

     // create the socket 
     Socket listenSocket = new Socket(AddressFamily.InterNetwork, 
             SocketType.Stream, 
             ProtocolType.Tcp); 

     // bind the listening socket to the port 
     IPEndPoint ep = new IPEndPoint(IPAddress.Any, TEXT_PORT_NUM); 
     listenSocket.Bind(ep); 

     while (true) 
     { 
      msg = ""; 
      data = new Byte[3000]; 

      // start listening 
      listenSocket.Listen(1); 

      //Received a connection 
      connection = listenSocket.Accept(); 

      //Get Data 
      connection.Receive(data); 

      //Get the message in string format 
      msg = System.Text.Encoding.Default.GetString(data); 
      msg = msg.Substring(0,msg.IndexOf((char)0)); 

      //Send message to the UI 
      xaml_incomingTextBox.Dispatcher.BeginInvoke(displayIncomingMessage, msg); 

      connection.Close(); 
     } 
    }//end of keepListeningForInstantMessages 

    void SendInstantMsg(object sender, RoutedEventArgs e) 
    { 
     TcpClient connection = null; 
     NetworkStream stream = null; 
     byte[] data; 

     xaml_incomingTextBox.Text += "\n\nOUTGOING MESSAGE: " + xaml_outgoingTextBox.Text; 

     try 
     { 
      //Set up IPAddress 
      IPAddress ipAddress = IPAddress.Parse(xaml_hostTextBox.Text); 
      IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, TEXT_PORT_NUM); 

      //Connect to TCP 
      connection = new TcpClient(); 
      connection.Connect(ipLocalEndPoint); 

      //Convert text to bytes 
      data = System.Text.Encoding.ASCII.GetBytes(xaml_outgoingTextBox.Text);        

      // Get a client stream for reading and writing. 
      stream = connection.GetStream(); 

      // Send the message to the connected TcpServer. 
      stream.Write(data, 0, data.Count()); 

      xaml_outgoingTextBox.Text = ""; 
     } 
     catch (Exception error) 
     { 
      MessageBox.Show("ERROR: " + error.Message); 
     } 
     finally 
     { 
      // Close everything. 
      if (connection != null) 
       connection.Close(); 

      if (stream != null) 
       stream.Close(); 
     } 
    }//end of SendInstantMsg 

    /////////////////////////////////////////////////////////////////////////////////////////////// 
    // 
    // 
    // The next 2 functions take care of the video part of the program 
    // 
    // 
    // 
    /////////////////////////////////////////////////////////////////////////////////////////////// 
    private void keepListeningForVideoMessages(object sender, DoWorkEventArgs e) 
    { 
     Action<Byte[]> displayIncomingVideo = (incomingImgBytes) => 
     { 
      xaml_incomingVideo.Source = ImageByteConverter.BytesToImage(incomingImgBytes); 
     }; 

     Socket connection; 
     Byte[] incomingBytes; 
     Byte[] data; 
     int offset; 
     int numOfBytesRecieved; 

     // create the socket 
     Socket listenSocket = new Socket(AddressFamily.InterNetwork, 
             SocketType.Stream, 
             ProtocolType.Tcp); 

     // bind the listening socket to the port 
     IPEndPoint ep = new IPEndPoint(IPAddress.Any, TEXT_VIDEO_NUM); 
     listenSocket.Bind(ep); 

     while (true) 
     { 
      offset = 0; 
      numOfBytesRecieved = -1; 
      incomingBytes = new Byte[300000]; 

      // start listening 
      listenSocket.Listen(1); 

      //Received a connection 
      connection = listenSocket.Accept(); 

      //Get all the data from the connection stream 
      while (numOfBytesRecieved != 0) 
      { 
       numOfBytesRecieved = connection.Receive(incomingBytes, offset, 10000, SocketFlags.None); 

       offset += numOfBytesRecieved; 
      } 

      data = new Byte[offset]; 
      Array.Copy(incomingBytes, data, offset); 

      //Send image to the UI 
      xaml_incomingTextBox.Dispatcher.BeginInvoke(displayIncomingVideo, data); 

      connection.Close(); 
     } 
    }//end of keepListeningForVideoMessages 


    private void SendVideoMsg(object sender, RoutedEventArgs e) 
    { 
     xaml_incomingVideo.Visibility = Visibility.Visible; 
     xaml_StopVideoButton.Visibility = Visibility.Visible; 
     xaml_SendTextButton.Visibility = Visibility.Hidden; 
     webcam.Start(); 
    } 


    private void StopVideoMsg(object sender, RoutedEventArgs e) 
    { 
     xaml_incomingVideo.Visibility = Visibility.Hidden; 
     xaml_StopVideoButton.Visibility = Visibility.Hidden; 
     xaml_SendTextButton.Visibility = Visibility.Visible; 
     webcam.Stop(); 
    } 

}//end of Class 
}//end of NameSpace 
+2

는'나는 C# 및, 나는 몇 가지 라이브러리를 검색하는 것이 좋습니다 그리고 media'을 사용하는 새로운 브랜드로 비교적 새로운 해요 당신을 위해 다리 대부분의 작업을 수행합니다 (하지만 무료로하지 않습니다). 그것을 처음부터 쓰는 것은 어려울 것입니다. –

답변

0
당신은 NAudio을 다운로드 시도해야

, 그것은 오픈 소스 오디오 라이브러리입니다. 한 PC에서 다른 PC로 UDP를 사용하여 오디오를 스트리밍하는 방법에 대한 데모가 제공됩니다. 그것은 NAudioDemo 앱의 "Network Chat"에 있습니다. 현재 사용중인

http://naudio.codeplex.com/

TCP 정말 오디오를 사용하지 않는 것이 좋습니다. 당신은 (나는 당신의 궁극적 인 목표는 프로젝트와 함께 무엇인지 확실하지 않다) 바퀴를 재발견하고 싶지 않은 경우,

또는 UDP 대신

http://www.onsip.com/about-voip/sip/udp-versus-tcp-for-voip

를 사용하여, 당신은 우리의 iConf.NET 화상 회의 SDK를 다운로드하려고 할 수 있습니다

http://avspeed.com/

관련 문제