G711:8000HZ,8位,20ms一包数据 ,50包每秒
源代码:
#include <string.h>
#include <memory.h>
#include <netdb.h>
#include <time.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h> //close()
#define DEST_PORT 8888
#define DEST_IP "192.168.1.18"
typedef struct
{
unsigned char csrc_len:4;
unsigned char extension:1;
unsigned char padding:1;
unsigned char version:2;
unsigned char payload:7;
unsigned char marker:1;
unsigned short seq_no;
unsigned long timestamp;
unsigned long ssrc;
} RTP_FIXED_HEADER;
int main(int argc, char* argv[])
{
FILE *file_g711 = NULL;
int ret;
int M_bit;
M_bit = 1;
char sendbuf[1500];
memset(sendbuf,0,1500);
unsigned short seq_num = 0;
RTP_FIXED_HEADER *rtp_hdr;
int socket1;
struct sockaddr_in server;
int len = sizeof(server);
float framerate = 25;
unsigned int timestamp_increse = 0,ts_current = 0;
timestamp_increse = 160;
server.sin_family = AF_INET;
server.sin_port = htons(DEST_PORT);
server.sin_addr.s_addr = inet_addr(DEST_IP);
socket1 = socket(AF_INET,SOCK_DGRAM,0);
connect(socket1, (struct sockaddr *)&server, len) ;//申请UDP套接字
file_g711 = fopen("test.g711","rb");
if(file_g711 == NULL)
{
printf("fopen error!\n");
}
printf("g711 open successfully!\n");
while((ret = fread(&sendbuf[12],sizeof(char),160,file_g711)) > 0)
{
rtp_hdr =(RTP_FIXED_HEADER*)&sendbuf[0];
//设置RTP HEADER,
rtp_hdr->payload = 0; //负载类型号,
rtp_hdr->version = 2; //版本号,此版本固定为2
if(1 == M_bit)
{
rtp_hdr->marker = 1; //标志位,由具体协议规定其值。
M_bit = 0;
printf("M_bit = 1\n");
}
else
{
rtp_hdr->marker = 0; //标志位,由具体协议规定其值。
}
rtp_hdr->ssrc = htonl(10); //随机指定为10,并且在本RTP会话中全局唯一
rtp_hdr->seq_no = htons(seq_num ++);
printf("\n\n%x\n\n",sendbuf[1]);
ts_current = ts_current+timestamp_increse;
rtp_hdr->timestamp=htonl(ts_current);
printf("calloc\n");
//free(Buf);
printf("read data size is %d\n",ret);
printf("sizeof(sendbuf) = %d",sizeof(sendbuf));
ret = send( socket1, sendbuf, ret+12, 0 );//发送rtp包
printf("\n\n%d\n\n",ret);
if(ret<0)
{
perror("send");
break;
}
printf("**********************************\n");
printf("ret = %d\n",ret);
printf("rtp_hdr->payload = %x\n",rtp_hdr->payload);
printf("rtp_hdr->version = %x\n",rtp_hdr->version);
printf("rtp_hdr->marker = %x\n",rtp_hdr->marker);
printf("rtp_hdr->ssrc = %x\n",rtp_hdr->ssrc);
printf("rtp_hdr->seq_no = %x\n",rtp_hdr->seq_no);
printf("rtp_hdr->timestamp = %x\n",rtp_hdr->timestamp);
printf("sendbuf[0]= %x\n",sendbuf[0]);
printf("sendbuf[12]= %x\n",sendbuf[12]);
printf("**********************************\n");
usleep(19000);
memset(sendbuf,0,1500);
}
fclose(file_g711);
printf("end !!!!!!!!!!!!!!!!!!\n");
return 0;
}
sdp文件:
m=audio 8888 RTP/AVP 0
a=rtpmap:8 pcmu/8000/1
a=ptime:20
c=IN IP4 192.168.1.18