音频直播服务是叫做 LANmic 无线话筒 的安卓程序。
访问http://192.168.1.8:8080 就能播放了。可以网页播放,vlc,ffmpeg,
那么我sdl能不能播放呢?LANmic 提供了wav编码,可以直接pcm播放。
经过搜索,发现搜到的文章都是一大抄,各种转,都是一样的,sdl本地文件播放,让人头大,无奈只好自己分析方法了。
经过抓包,我发现访问http://192.168.1.8:8080 返回的是http头,后续就是riff wavefmt 搞过wav格式的都知道,这个wav头了
那么我们把http头跳过,读取后面的wav头以及数据,就可以播放了。
经过调试,代码如下
#include <stdio.h>
#include <tchar.h>
#include "SDL.h"
#include "sockutil.h"
#pragma comment(lib, "sdl2.lib")
#pragma comment(lib, "SDL2main.lib")
Uint32 audio_len;//音频数据大小
Uint8 *audio_pos;//指向音频数据的指针
/**回调函数(由系统调用)
* 函数声明:typedef void (SDLCALL * SDL_AudioCallback)
* (void *userdata, Uint8 * stream, int len);
* This function is called when the audio device needs more data.
*
* userdata: An application-specific parameter saved in the SDL_AudioSpec structure(SDL_AudioSpec结构中的用户自定义数据,一般情况下可以不用)
* stream: A pointer to the audio data buffer.(该指针指向需要填充的音频缓冲区)
* len: The length of that buffer in bytes.(音频缓冲区的大小,以字节为单位)
*
* Once the callback returns, the buffer will no longer be valid.
* Stereo samples are stored in a LRLRLR ordering.
*
* You can choose to avoid callbacks and use SDL_QueueAudio() instead, if
* you like. Just open your audio device with a NULL callback.
*/
void fill_audio(void *userdata, Uint8 *stream, int len)
{
//SDL2中必须首先使用SDL_memset()将stream中的数据设置为0
SDL_memset(stream, 0, len);
if (audio_len == 0) /* Only play if we have data left */
{
return;
}
len = (len > audio_len ? audio_len : len); /* Mix as much data as possible */
/**
* 函数声明:extern DECLSPEC void SDLCALL
* SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume);
* This takes two audio buffers of the playing audio format and mixes
* them, performing addition, volume adjustment, and overflow clipping.
* The volume ranges from 0 - 128, and should be set to ::SDL_MIX_MAXVOLUME
* for full audio volume. Note this does not change hardware volume.
* This is provided for convenience -- you can mix your own audio data.
*
* #define SDL_MIX_MAXVOLUME 128
*/
SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
audio_pos += len;
audio_len -= len;
}
int play()
{
//初始化SDL
if (SDL_Init(SDL_INIT_AUDIO))
{
printf("Could not initialize SDL - %s\n", SDL_GetError());
return -1;
}
//SDL_AudioSpec初始化
SDL_AudioSpec wanted_spec;
wanted_spec.freq = 44100; //音频数据的采样率(常用的有48000,44100等)
wanted_spec.format = AUDIO_S16SYS;//音频数据的格式
wanted_spec.channels = 1; //声道数(例如单声道取值为1,立体声取值为2)
wanted_spec.silence = 0; //设置静音的值
wanted_spec.samples = 1024; //音频缓冲区中的采样个数(要求必须是2的n次方)
wanted_spec.callback = fill_audio;//填充音频缓冲区的回调函数
//打开音频
if (SDL_OpenAudio(&wanted_spec, NULL) < 0)
{
printf("can't open audio.\n");
return -1;
}
//注释掉的是文件播放.
//FILE *fp_pcm = fopen("aa.wav", "rb");
//if (fp_pcm == NULL)
//{
// printf("cannot open this file\n");
// return -1;
//}
SOCK_INIT
char target[255]= "192.168.1.8";
// Connect to server.
SOCKET c = SockLib::ConnectTarget(target,8080);
if ( c == INVALID_SOCKET ) {
printf("Connected to server %s err \n", target );
return 0;
}
printf("Connected to server %s \n", target );
std::string httpheader="" ;
int ct=0;
while(1)
{
char buf[1025]="";
int ret=recv(c,buf,1,0);
if(ret<0) return 1;
httpheader +=buf[0];
ct+=ret;
int i=IsHTTPRequestComplete(httpheader.c_str(),ct); //有http结束标志,说明接收完.
if (i>0)
{
break;
}
}
//跳过http头,后面就是RIFF的wav数据了。
Sleep(1000);
int pcm_buffer_size = 4096;//每次读取4096字节的数据,同时也是音频帧大小
char *pcm_buffer = (char *)malloc(pcm_buffer_size);
int data_count = 0;
//播放音频数据
SDL_PauseAudio(0);
while (true)
{
//int ret = fread(pcm_buffer, 1, pcm_buffer_size, fp_pcm);
bool ret = SockLib::RecvBuff( c, (BYTE*)pcm_buffer, pcm_buffer_size );
if (ret ==false)
{
//这里有可能是会有剩余音频数据的,不知道这样改对不对?
audio_pos = (Uint8 *)pcm_buffer;
audio_len = ret;
while (audio_len > 0)
{
SDL_Delay(1);
}
//退出
break;
//循环播放
// fseek(fp_pcm, 0, SEEK_SET);
// fread(pcm_buffer, 1, pcm_buffer_size, fp_pcm);
data_count = 0;
}
printf("Now Playing %10d Bytes data.\n", data_count);
data_count += pcm_buffer_size;
audio_pos = (Uint8 *)pcm_buffer;
//Audio buffer length
audio_len = pcm_buffer_size;
while (audio_len > 0)//Wait until finish
{
SDL_Delay(1);
}
}
free(pcm_buffer);
// fclose(fp_pcm);
SDL_Quit();
return -1;
}
int _tmain(int argc, _TCHAR* argv[])
{
while(1)
{
play();
}
return 0;
}
来源:oschina
链接:https://my.oschina.net/u/2397441/blog/1928236