4,356
社区成员
发帖
与我相关
我的任务
分享
while(d->read_started || d->write_started)
{
if (d->pcmfd>=0){
if (d->read_started){
struct timeval timeout;
fd_set read_fds;
audio_buf_info info;
if (rm==NULL) rm=allocb(bsize,0);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO( &read_fds );
FD_SET( d->pcmfd, &read_fds );
if( select( d->pcmfd + 1, &read_fds, NULL, NULL, &timeout ) == -1 ) {
}
if (FD_ISSET( d->pcmfd, &read_fds ) && ioctl( d->pcmfd, SNDCTL_DSP_GETISPACE, &info ) != -1)
{
if (info.bytes>=bsize)
{
err=read(d->pcmfd,rm->b_wptr,bsize);
if (err<0){
ms_warning("Fail to read %i bytes from soundcard: %s",
bsize,strerror(errno));
}else{
rm->b_wptr+=err;
ms_mutex_lock(&d->mutex);
putq(&d->rq,rm);
ms_mutex_unlock(&d->mutex);
rm=NULL;
}
}
else
{
timeout.tv_sec = 0;
timeout.tv_usec = 5000;
select(0, 0, NULL, NULL, &timeout );
}
}
else
{
timeout.tv_sec = 0;
timeout.tv_usec = 5000;
select(0, 0, NULL, NULL, &timeout );
}
}else {
int sz = read(d->pcmfd,rtmpbuff,bsize);
if( sz!=bsize) ms_warning("sound device read returned %i !",sz);
}
if (d->write_started){
audio_buf_info info;
if( ms_bufferizer_get_avail(d->bufferizer)>=bsize && ioctl( d->pcmfd, SNDCTL_DSP_GETOSPACE, &info ) == 0 ) {
if( info.fragstotal - info.fragments > 15 ) {
static int c=0;
/* drop the fragment if the buffer starts to fill up */
/* we got too much data: I prefer to empty the incoming buffer */
while (ms_bufferizer_get_avail(d->bufferizer)>bsize*4){
ms_mutex_lock(&d->mutex);
err=ms_bufferizer_read(d->bufferizer,wtmpbuff,bsize);
err=ms_bufferizer_read(d->bufferizer,wtmpbuff,bsize);
err=ms_bufferizer_read(d->bufferizer,wtmpbuff,bsize);
err=ms_bufferizer_read(d->bufferizer,wtmpbuff,bsize);
ms_mutex_unlock(&d->mutex);
c=c+err*4;
ms_warning("drop fragment when buffer gets too much data (%i - discarded:%i)", info.fragstotal - info.fragments, c);
if (err==0)
break;
}
}else {
ms_mutex_lock(&d->mutex);
err=ms_bufferizer_read(d->bufferizer,wtmpbuff,bsize);
ms_mutex_unlock(&d->mutex);
err=write(d->pcmfd,wtmpbuff,bsize);
if (err<0){
ms_warning("Fail to write %i bytes from soundcard: %s",
bsize,strerror(errno));
}
}
}
}else {
int sz;
memset(wtmpbuff,0,bsize);
sz = write(d->pcmfd,wtmpbuff,bsize);
if( sz!=bsize) ms_warning("sound device write returned %i !",sz);
}
}else usleep(20000);
}
err=write(d->pcmfd,wtmpbuff,bsize);