I'm trying to implement a program that reads a stream of MP3 from a Radio server and plays it. I'm doing this because I want to build a radio using a ESP8266, and I want to understand exactly how the flow of retrieving the stream from the web server goes so I can understand why it is currently not working so well..
Anyway, right now I have a small HTTP-Client C program that issues a GET request to a radio station server and outputs the response to a file.
This is the part that reads from the socket:
while((recived_len = recv(sock, BUF, BUFSIZ-1, 0)) > 0)
{
BUF[recived_len] = '\0';
printf("%s, BUF);
}
This loop goes forever, writing the data to STDOUT, and I then redirect it to a file. The problem is that for some reason the file is defective, the MP3 player doesn't play it.
If I do the same thing with curl
:
$ curl http://kanliveicy.media.kan.org.il/icy/kanbet_mp3 > curl_stream.mp3
// wait for a while
^C
It goes great - the MP3 player plays it well.
I did try to inspect the file that is outputted from my program. I used this MP3 parser to parse the file, and it seems that there is a lot of resyncing, i.e. stepping through the calculated frame size doesn't put you in the next MP3 header, and some of the headers show incorrect values, such as layer: 1
, etc.
What could be the reason for this malfunctioning? What is the difference between my simple program and curl
? Of course there are plenty of differences lol but I mean - what else is there other than just reading from the TCP socket?
Hint: I did notice that the download speed is significantly different: curl
downloads the stream much faster than my client application. Could this be related?
Love some help!