linux 如何解决音频传输延迟,请高手指教,下面是我的客户端程序
这是客户端程序,服务端程序也差不多,两端可以实现音频传输,但是延迟很大,大概有5秒,要怎样改程序才能尽量减少延迟?????????????????
/*#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
*/
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/soundcard.h>
#include <errno.h>
#define PORT 8888
#define SERVER_IP "192.168.1.105"
#define BUFSIZE 4096
#define RATE 8000
#define SIZE 16
#define CHANNELS 2
#define RDONLY 0
#define WRONLY 1
int read_fd; //读音频文件
int write_fd; //写音频文件
pthread_mutex_t mutex;
unsigned char write_buf[BUFSIZE];
unsigned char read_buf[BUFSIZE];
int SoundCapture(int Flag);
//接收声音
void *sound_listen(void *arg)
{
char recv_buf[BUFSIZE];
int *parg=(int *)arg;
int listen_fd=*parg;
printf("listen_fd=%d\n",listen_fd);
while(1)
{
memset(read_buf,0,1024);
int ret = recv(listen_fd, (void *)recv_buf, BUFSIZE, 0);
if(ret <= 0)
{
printf("recv sounddata error!\n");
//pthread_cancel(soundPthID2);
pthread_exit(NULL);
}
else
{
printf("recv sounddata success!\n");
}
write(write_fd, recv_buf, BUFSIZE);
//sleep(1);
}
return (void*)0;
}
//发送声音
void *sound_send(void *arg)
{
char send_buf[BUFSIZE];
int *parg=(int *)arg;
int send_fd=*parg;
printf("send_fd=%d\n",send_fd);
while(1)
{
memset(write_buf,0,1024);
read(read_fd, send_buf, BUFSIZE);
int ret = send(send_fd, (void *)send_buf, BUFSIZE, 0);
if(ret <= 0)
{
printf("send sounddata error!\n");
//pthread_cancel(soundPthID1);
pthread_exit(NULL);
}
else
printf("send sounddata success!\n");
//sleep(1);
}
return (void*)0;
}
int main(int argc,char *argv[])
{
int connect_fd;
int ret;
int i;
//int port;
int len;
static struct sockaddr_in srv_addr;
pthread_t tid_listen;
pthread_t tid_send;
//客户端运行需要给出具体的连接地址和端口
//if(argc!=3)
//{
// printf("Usage: %s server_ip_address port\n",argv[0]);
// return 1;
//}
//获得输入的端口
//port=atoi(argv[2]);
//创建套节字用于客户端的连接
connect_fd=socket(PF_INET,SOCK_STREAM,0);
if(connect_fd<0)
{
perror("cannot create communication socket");
return 1;
}
//填充关于服务器的套节字信息
memset(&srv_addr,0,sizeof(srv_addr));
srv_addr.sin_family=AF_INET;
//srv_addr.sin_addr.s_addr=inet_addr(argv[1]);
srv_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
//srv_addr.sin_port=htons(port);
srv_addr.sin_port=htons(PORT);
//连接指定的服务器
ret=connect(connect_fd,(struct sockaddr *)&srv_addr,sizeof(srv_addr));
if(ret==-1)
{
perror("cannot connect to the server");
close(connect_fd);
return 1;
}
read_fd = SoundCapture(RDO