音頻直播服務是叫做 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;
}
程式運作如下