0

我需要在 C# 中创建一个 Icecast 客户端。这样做的主要目的是发送来自两个音频设备的传入音频,以便通过 Icecast 进行广播。

当我在寻找已经制定的解决方案时,我发现 Butt 正是我需要的,但我还需要分别向 Icecast 发送两个音频设备。

我已经从两个麦克风获取音频输入并将它们保存在一个文件夹中(每个麦克风一个音频文件)。现在我需要将两个麦克风分别广播到 Icecast。

这一切都是因为我需要像广播电台一样广播两个麦克风(每个麦克风一个电台)。

主要解决方案是这样的:

  • 麦克风 1 => 像 Source Micro1 一样广播到 Icecast => 像 micro1.mp3 格式一样保存音频(工作)。
  • 麦克风 2 => 像 Source Micro2 一样广播到 Icecast => 像 micro2.mp3 格式一样保存音频(工作)。

我需要知道如何广播到 icecast,我正在使用 NAudio 库来获取音频输入并保存它。

编辑:我正在从 C# 与 Icecast 通信,这是我的代码:

public static void commIcecast(string url)
    {
        WebClient client = new WebClient();

        client.Headers.Add("content-type", "audio/mpeg");
        client.Headers.Add("Authorization", "Basic c291cmNlOmhhY2ttZQ==");
        client.Headers.Add("ice-name", "This is my server name");
        client.Headers.Add("ice-url", "http://www.google.com");
        client.Headers.Add("ice-genre", "Rock");
        client.Headers.Add("ice-description", "This is my server description");
        client.Headers.Add("ice-audio-info", "ice-samplerate=44100;ice-bitrate=128;ice-channels=2");

        Stream data = client.OpenRead(url);
        StreamReader reader = new StreamReader(data);
        string s = reader.ReadToEnd();
        Console.WriteLine(s);
        data.Close();
        reader.Close();
    }

但我只是从 Icecast 服务器收到这个答案:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Icecast Streaming Media Server</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<h2>Icecast2 Status</h2>
<br><div class="roundcont">
<div class="roundtop"><img src="/corner_topleft.jpg" class="corner" style="display: none"></div>
<table border="0" width="100%" id="table1" cellspacing="0" cellpadding="4"><tr><td bgcolor="#656565">
<a class="nav" href="admin/">Administration</a><a class="nav" href="status.xsl">Server Status</a><a class="nav" href="server_version.xsl">Version</a>
</td></tr></table>
<div class="roundbottom"><img src="/corner_bottomleft.jpg" class="corner" style="display: none"></div>
</div>
<br><br>&nbsp;


<div class="poster">Support icecast development at <a class="nav" target="_blank" href="http://www.icecast.org">www.icecast.org</a>
</div>
</body>
</html>

我尝试发送“SOURCE /mp3test ICE/1.0”,但 Headers.Add 方法不允许我这样做。

编辑:我通过 tcp 将其发送到 Icecast,但我无法回复,我只需要知道是否以这种方式发送它,如果是现在,我将不得不将帖子移至 tcp 问题。我没有收到使用这种发送方法的 Icecast 服务器的任何响应。

            System.Net.IPAddress address = System.Net.IPAddress.Parse(url);

            socketServer = new TcpClient(url, port);
            NetworkStream networkStream = socketServer.GetStream();

            data = Encoding.ASCII.GetBytes("SOURCE /csharp ICE/1.0");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("content-type: audio/mpeg");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("Authorization: Basic c291cmNlOmhhY2ttZQ==");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("ice-name: lala");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("ice-url: localhost");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("ice-genre: Rock");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("ice-bitrate: 128");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("ice-private: 0");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("ice-public: 1");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("ice-description: This is my server description");
            networkStream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes("ice-audio-info: ice-samplerate=44100;ice-bitrate=128;ice-channels=2");
            networkStream.Write(data, 0, data.Length);

            StreamReader reader = new StreamReader(networkStream);
            Console.WriteLine(reader.ReadToEnd());

有了这个我可以连接到 Icecast 服务器,至少连接的客户端数量在 Icecast 的全局统计中增加了,但是连接它丢失了,我无法得到任何响应。

4

2 回答 2

1

首先,您需要使用编解码器对捕获的音频进行编码。MP3 很受欢迎并且得到很好的支持。aacPlus 得到很好的支持(但不如 MP3 那么多),并且在大多数情况下提供更好的压缩比。

我不相信 NAudio 支持编码(如果我错了,请纠正我)。您将需要查看诸如 FFMPEG(各种可用的编解码器)或 LAME(优质 MP3 编解码器)之类的东西。作为奖励,如果您使用 FFMPEG,它能够为您从 DirectShow 源捕获,因此您只需编写代码即可从 STDOUT 获取数据。(请注意,这里存在潜在的许可问题,包括库的源代码和编解码器的专利。)

现在您已经编码了音频数据,您需要实现 Icecast 源协议。为此,请在此处查看我的答案: https ://stackoverflow.com/a/9985297/362536

于 2012-11-24T08:35:04.700 回答
0

To solve my problem I used Edcast. This is the url https://code.google.com/p/edcast-reborn/

Basically I used two instances of edcast, each one pointing a different audio input and they were connecting to the Icecast server.

With this configuration you can see the two edcast instances and listen the different audio inputs by accessing the Icecast server.

Thank you all for your help.

于 2014-09-15T15:04:09.763 回答