Linux音视频同步问题
本人开发了一个视频采集播放系统,程序基本写好了,单音视频不能同步,哪位大虾帮一下忙回复 #1 buaalinux1 的帖子
这个是你的程序问题,具体现象描述清楚一点 终于有回应得了,先表示感谢,具体说来就是:声音和图像可以并发执行,但时间对不上号,此刻的声音不能和此刻的图像同步,具体程序如下,有会写同步的请帮忙给出答案,就是给点指点也好,大概该怎末做,说一下,不胜感激//---------play avi---------------
/*image thread*/
void *thread3()
{
int filesize;
char *bbuffer=NULL;
bbuffer = (char *)malloc (320*240*4);
char *dataBuf;
mem_file.pfilebuff=bbuffer;
dataBuf=(char*)malloc(320*3*240);
for(;;)
{
if(fread(&filesize,1,4,avfp) == 4)
{
printf("read filesize is : %d !\n",filesize);
}
if(fread(bbuffer,1,filesize,avfp) != filesize)
{
printf("play is over !\n");
break;
}
mem_file.base=0;
mem_file.filesize=filesize;
if(mem_file.filesize<=0)
continue;
if(!mem_file.pfilebuff)
continue;
cinfo.err=jpeg_std_error(&jerr.pub);
jerr.pub.error_exit=my_error_exit;
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_decompress(&cinfo);
goto clearend;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo,(FILE *)(&mem_file));
jpeg_read_header(&cinfo,TRUE);
jpeg_start_decompress(&cinfo);
if(dataBuf==NULL)
{
printf("JpegFile :\nOut of memory");
jpeg_destroy_decompress(&cinfo);
goto clearend;
}
row_stride=cinfo.output_width * cinfo.output_components;
width=cinfo.output_width;
height=cinfo.output_height;
buffer=(*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while(cinfo.output_scanline<cinfo.output_height)
{
jpeg_read_scanlines(&cinfo,buffer,1);
if (cinfo.out_color_components==3)
j_putRGBScanline(buffer[0],cinfo.output_width,dataBuf,cinfo.output_scanline-1);
else if(cinfo.out_color_components==1)
j_putGrayScanlineToRGB(buffer[0],cinfo.output_width,dataBuf,cinfo.output_scanline-1);
}
(void)jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
diff_width=fbdev.fb_width-width;
diff_height=fbdev.fb_height-height;
if(diff_width>0)
{
x= diff_width/2;
}
else
{
x=0;
width=fbdev.fb_width;
}
if(diff_height>0)
{
y=diff_height/2;
}
else
{
y=0;
height=fbdev.fb_height;
}
fbdev.fb_draw(&fbdev,dataBuf,0,0,320,240);
}
clearend:
free(dataBuf);
free(bbuffer);
}
/*sound thread*/
void *thread4()
{
int id;
int arg;
int status;
char buf[4096];
/* if((audiofd = open("/dev/sound/dsp",O_WRONLY)) < 0 )
{
printf("Can't open sound device!\n");
exit(1);
}
*/
/* if((fd = open("/tmp/audio.wav",O_RDWR)) < 0)
{
printf("Can't open output file!\n");
exit (1);
}
*/
arg = SIZE;
status = ioctl(audiofd,SOUND_PCM_WRITE_BITS,&arg);
if(status == -1)
printf("SOUND_PCM_WRITE_BITS ioctl failed\n");
if(arg != SIZE)
printf("unable to set sample size\n");
arg = CHANNELS;
status = ioctl(audiofd,SOUND_PCM_WRITE_CHANNELS,&arg);
if(status == -1)
printf("SOUND_PCM_WRITE_CHANNELS ioctl failed\n");
if(arg != CHANNELS)
printf("unable to set number of channels\n");
//arg = RATE;
arg =22050;
status = ioctl(audiofd,SOUND_PCM_WRITE_RATE,&arg);
if(status == -1)
printf("SOUND_PCM_WRITE_WRITE ioctl failed\n");
while(fread(buf,sizeof(buf),1,audiofp) > 0)
write(audiofd,buf,sizeof(buf));
}
void play_thread_create(void)
{
int temp;
memset(&thread, 0, sizeof(thread)); //comment1
/*创建线程*/
if((temp = pthread_create(&thread[0], NULL, thread3, NULL)) != 0) //comment2
printf(" sorry sir thread 3 failed !\n");
else
printf("my sir thread 3 is successful !\n");
if((temp = pthread_create(&thread[1], NULL, thread4, NULL)) != 0)//comment3
printf("sorry sir thread 4 failed !\n");
else
printf("my sir thread 4 is successful !\n");
}
void play_thread_wait(void)
{
/*等待线程结束*/
if(thread[0] !=0)
{
pthread_join(thread[0],NULL);
printf("thread 1 was over! \n");
}
if(thread[1] !=0)
{
pthread_join(thread[1],NULL);
printf("thread 2 was over !\n");
}
}
[ 本帖最后由 dreamice 于 2009-8-23 21:53 编辑 ]
回复 #3 buaalinux1 的帖子
应该是同步问题造成的,这个我也没什么经验 加个timeline然后声音的数据挂在timeline上,视频的也挂在timeline上
对比一下,就可以同步了,一般来说,是视频跟随声音的
图像处理速度慢的话,可以根据声音来跳帧 一段段的放,同不同步只能靠天了. 原帖由 T-bagwell 于 2009-8-24 11:19 发表 http://linux.chinaunix.net/bbs/images/common/back.gif
加个timeline
然后声音的数据挂在timeline上,视频的也挂在timeline上
对比一下,就可以同步了,一般来说,是视频跟随声音的
图像处理速度慢的话,可以根据声音来跳帧
Tbag兄,一般来说,音视频是同时存储在一起的,还是分开存储的?
我记得以前做的时候,是同时存储的,所以播放的时候不存在同步的问题。
页:
[1]