关于从PC传过来的MP3文件流如何一帧一帧解码呢?
本人所使用的是关于网上开源的Mp3公共libmad来解码,当从PC端传过来的每一帧依次保存为一个mp3文件,调用此解码就一切正常。但是不知道如何从一个文件解码转为一帧去解码播放声音?求高人指点,小弟在此先谢过。。。。
以下是一个传入给编码器的函数接口,
//mp3解码输入回调函数
static enum mad_flow input(void *data, struct mad_stream *stream)
{
struct buffer *pbuffer = data;
int ret;
if(mp3_statue.bmp3dec == 2)
return MAD_FLOW_STOP;
if(mp3_statue.bmp3dec == 3)
{
sound_device_play_stop(pbuffer-> hdev);
sem_post(&sem_pause);
sem_wait(&sem_thread);
sound_device_play_start(pbuffer-> hdev);
}
if(pbuffer-> in_file == 0 )
return MAD_FLOW_STOP;
if(stream-> next_frame)
{
int unused = pbuffer-> in_buf + pbuffer-> inlen - stream-> next_frame;
memmove(pbuffer-> in_buf, stream-> next_frame, unused);
pbuffer-> inlen = unused;
}
ret = readdata(pbuffer-> in_buf + pbuffer-> inlen,pbuffer-> buf_size - pbuffer-> inlen,pbuffer-> in_file);
if( 0 > ret )
{
return MAD_FLOW_STOP;
}
pbuffer-> inlen += ret;
if(pbuffer-> inlen == 0)
return MAD_FLOW_STOP;
mad_stream_buffer(stream, pbuffer-> in_buf, pbuffer-> inlen);
return MAD_FLOW_CONTINUE;
}
struct buffer {
int in_file; //mp3文件句柄
int hdev; //dev句柄
unsigned char * in_buf; //mp3数据
int buf_size; //in_buf的长度
int inlen; //in_buf中数据长度
unsigned char * out_buf; //pcm数据
int out_buf_len; //单个PCM buf长度
int outpos; //输出buf数据偏移量
};
static struct buffer buffer; //记录输入输出缓冲buf信息
static int readdata(char * lpbuf,int maxlen,int file)
{
int ret;
struct pollfd ufds;
reread:
ufds.fd = file;
ufds.events = POLLIN;
ret = poll(&ufds, 1, 500);
if(ret == 0)
{
printf( "read file time out\n ");
return -1;
}
else if(ret < 0)
{
perror( "read file error ");
return -1;
}
ret = read(file,lpbuf,maxlen);
if(ret == 0)
{
lseek(file,0,SEEK_SET);
goto reread;
}
else if(ret < 0)
{
perror( "read file error ");
printf( "%x\n ",file);
}
return ret;
}
------解决思路----------------------
看TCPMP开源的代码可以直接播放网络媒体
------解决思路----------------------
你这个是解码对应的标准mp3文件,mp3文件有自己的信息头,那段代码就是先判断MP3的信息头然后才一步步的解码,网络传递过来的被分开了吧,你应该把接收的数据插到解码数据段的地方