Question

I need to create an Icecast client in C#. The main objective of this it's to send the incoming audio from two audio devices to be broadcast via Icecast.

When I was searching for some solution already made, I found Butt which was exactly what I needed but I also needed to send to the Icecast two audio devices individually.

I'm already getting the audio input from the two microphones and save them in a folder (one audio file for each microphone). Now I need to broadcast individually the two microphone to Icecast.

All this is because I need to broadcast the two microphones like a radio station (one station for each microphone).

The main solution it's something like this:

  • Microphone 1 => Broadcast to Icecast like Source Micro1 => Save audio like micro1.mp3 format (working).
  • Microphone 2 => Broadcast to Icecast like Source Micro2 => Save audio like micro2.mp3 format (working).

I need to know how can I broadcast to icecast, i'm using NAudio library to get the audio input and save it.

EDIT: I'm communicating with Icecast from C#, this is my code:

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

But i'm just reciving this answer from the Icecast server:

<!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>

I've tried to send the "SOURCE /mp3test ICE/1.0" but the Headers.Add method don't allow me to do that.

EDIT: I'm sending this via tcp to Icecast, but i can't recibe the response, i just need to know if this way to send it , if it's right now i'll have to move the post to tcp issues. I'm not reciving any response of the Icecast server using this method of sending.

            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());

With this i can connect to the Icecast server, at least the number of clients connected increase in the global statistics in Icecast, but then the connection it's lost and i can't get any response.

Was it helpful?

Solution 2

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.

OTHER TIPS

First, you need to encode your captured audio with a codec. MP3 is popular and well supported. aacPlus is well supported (but not quite as much as MP3), and gives a better compression ratio in most cases.

I do not believe NAudio supports encoding (please correct me if I am wrong). You will need to look at something like FFMPEG (wide variety of codecs available) or LAME (quality MP3 codec). As a bonus, if you go with FFMPEG, it is able to capture from DirectShow sources for you, so you only need to write code to get data from STDOUT. (Note that there are potential licensing issues here, both with the source code of the libraries and with patents on the codecs.)

Now that you have encoded audio data, you need to implement the Icecast source protocol. To do that, see my answer here: https://stackoverflow.com/a/9985297/362536

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top