简体   繁体   English

从 C 中的音频 stream 服务器读取

[英]Reading from an Audio stream server in C

I'm trying to implement a program that reads a stream of MP3 from a Radio server and plays it.我正在尝试实现一个程序,该程序从 Radio 服务器读取 MP3 的 stream 并播放它。 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..我这样做是因为我想使用 ESP8266 构建收音机,并且我想准确了解从 web 服务器检索 stream 的流程是如何进行的,所以我可以理解为什么它目前不能正常工作。

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.无论如何,现在我有一个小型 HTTP-Client C 程序,它向广播电台服务器发出 GET 请求并将响应输出到文件。

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.这个循环一直持续下去,将数据写入 STDOUT,然后我将其重定向到一个文件。 The problem is that for some reason the file is defective, the MP3 player doesn't play it.问题是由于某种原因文件有缺陷,MP3 播放器无法播放。

If I do the same thing with curl :如果我对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.效果很好 - MP3 播放器播放得很好。

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 resync ing, ie 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.我使用这个MP3 解析器来解析文件,似乎有很多resync ing,即逐步计算的帧大小不会让你进入下一个 MP3 header,并且一些标题显示不正确的值,例如作为layer: 1等。

What could be the reason for this malfunctioning?这种故障的原因可能是什么? What is the difference between my simple program and curl ?我的简单程序和curl什么区别? Of course there are plenty of differences lol but I mean - what else is there other than just reading from the TCP socket?当然有很多不同之处,哈哈,但我的意思是——除了从 TCP 插座读取之外,还有什么?

Hint: I did notice that the download speed is significantly different: curl downloads the stream much faster than my client application.提示:我确实注意到下载速度明显不同: curl下载 stream 比我的客户端应用程序快得多。 Could this be related?这可能有关系吗?

Love some help!喜欢一些帮助!

I believe that your code will truncate every received buffer when it contains a NULL byte.相信当您的代码包含NULL 字节时,您的代码将截断每个接收到的缓冲区。

Just compare curl's raw output with the one you receive, and you should be able to see the difference.只需将 curl 的原始 output 与您收到的进行比较,您应该就能看到差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM