g711 RTP传输

  1. / g711.c  
  2. //  
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7.   
  8. #include   
  9. #include   
  10. #include   
  11. #include   
  12. #include   
  13. #include   
  14. #include     //close()  
  15. #define DEST_PORT  8888  
  16. #define DEST_IP "192.168.1.18"  
  17.   
  18.   
  19. typedef struct  
  20. {  
  21.     /** byte 0 */  
  22.     unsigned char csrc_len:4;        /** expect 0 */  
  23.     unsigned char extension:1;       /** expect 1, see RTP_OP below */  
  24.     unsigned char padding:1;         /** expect 0 */  
  25.     unsigned char version:2;         /** expect 2 */  
  26.     /** byte 1 */  
  27.     unsigned char payload:7;         /** stream type */  
  28.     unsigned char marker:1;          /** when send the first framer,set it */  
  29.     /** bytes 2, 3 */  
  30.     unsigned short seq_no;  
  31.     /** bytes 4-7 */  
  32.     unsigned  long timestamp;  
  33.     /** bytes 8-11 */  
  34.     unsigned long ssrc;              /** stream number is used here. */  
  35. } RTP_FIXED_HEADER;  
  36.   
  37.   
  38. int main(int argc, char* argv[])  
  39. {  
  40.     FILE *file_g711 = NULL;  
  41.     int ret;  
  42.     int M_bit;  
  43.     M_bit = 1;  
  44.   
  45.     char sendbuf[1500];  
  46.     memset(sendbuf,0,1500);  
  47.     unsigned short seq_num = 0;  
  48.     RTP_FIXED_HEADER        *rtp_hdr;  
  49.   
  50.     int    socket1;  
  51.     struct sockaddr_in server;  
  52.     int len = sizeof(server);  
  53.     float framerate = 25;  
  54.     unsigned int timestamp_increse = 0,ts_current = 0;  
  55.     timestamp_increse = 160;  
  56.   
  57.     server.sin_family = AF_INET;  
  58.     server.sin_port = htons(DEST_PORT);  
  59.     server.sin_addr.s_addr = inet_addr(DEST_IP);  
  60.     socket1 = socket(AF_INET,SOCK_DGRAM,0);  
  61.     connect(socket1, (struct sockaddr *)&server, len) ;//申请UDP套接字  
  62.   
  63.     file_g711 = fopen("test.g711","rb");  
  64.     if(file_g711 == NULL)  
  65.     {  
  66.         printf("fopen error!/n");  
  67.     }  
  68.     printf("g711 open successfully!/n");  
  69.   
  70.   
  71.     while((ret = fread(&sendbuf[12],sizeof(char),160,file_g711)) > 0)  
  72.     {  
  73.     rtp_hdr =(RTP_FIXED_HEADER*)&sendbuf[0];  
  74.     //设置RTP HEADER,  
  75.     rtp_hdr->payload     = 0;  //负载类型号,  
  76.     rtp_hdr->version     = 2;  //版本号,此版本固定为2  
  77.     if(1 == M_bit)  
  78.     {  
  79.             rtp_hdr->marker    = 1;   //标志位,由具体协议规定其值。  
  80.             M_bit = 0;  
  81.         printf("M_bit = 1/n");  
  82.     }  
  83.     else  
  84.     {  
  85.         rtp_hdr->marker    = 0;   //标志位,由具体协议规定其值。  
  86.     }  
  87.   
  88.         rtp_hdr->ssrc        = htonl(10);    //随机指定为10,并且在本RTP会话中全局唯一  
  89.   
  90.         rtp_hdr->seq_no = htons(seq_num ++);  
  91.         printf("/n/n%x/n/n",sendbuf[1]);  
  92.         ts_current = ts_current+timestamp_increse;  
  93.         rtp_hdr->timestamp=htonl(ts_current);  
  94.         printf("calloc/n");  
  95.         /** 
  96.         unsigned char *Buf; 
  97.         if ((Buf = (unsigned char*)calloc (512 , sizeof(char))) == NULL) 
  98.         { 
  99.             printf (" Could not allocate Buf memory/n"); 
  100.         } 
  101.         Buf = &sendbuf[12]; 
  102.         */  
  103.         /** 
  104.         ret = fread(Buf,1,511,file_g711); 
  105.         if(ret<0) 
  106.         { 
  107.             printf("fread error/n"); 
  108.         }*/  
  109.         //free(Buf);  
  110.         printf("read data size is %d/n",ret);  
  111.         printf("sizeof(sendbuf) = %d",sizeof(sendbuf));  
  112.   
  113.         ret = send( socket1, sendbuf, ret+12, 0 );//发送rtp包  
  114.         printf("/n/n%d/n/n",ret);  
  115.         if(ret<0)  
  116.         {  
  117.             perror("send");  
  118.             break;  
  119.         }  
  120.         printf("**********************************/n");  
  121.         printf("ret = %d/n",ret);  
  122.         printf("rtp_hdr->payload = %x/n",rtp_hdr->payload);  
  123.         printf("rtp_hdr->version = %x/n",rtp_hdr->version);  
  124.         printf("rtp_hdr->marker = %x/n",rtp_hdr->marker);  
  125.         printf("rtp_hdr->ssrc = %x/n",rtp_hdr->ssrc);  
  126.         printf("rtp_hdr->seq_no = %x/n",rtp_hdr->seq_no);  
  127.         printf("rtp_hdr->timestamp = %x/n",rtp_hdr->timestamp);  
  128.         printf("sendbuf[0]= %x/n",sendbuf[0]);  
  129.         printf("sendbuf[12]= %x/n",sendbuf[12]);  
  130.         printf("**********************************/n");  
  131.   
  132.         usleep(19000);  
  133.         memset(sendbuf,0,1500);//清空sendbuf;此时会将上次的时间戳清空,因此需要ts_current来保存上次的时间戳值  
  134.     }  
  135.     fclose(file_g711);  
  136.     printf("end !!!!!!!!!!!!!!!!!!/n");  
  137.     return 0;  
  138. }

  139. SDP文件内容: 
  1. m=audio 8888 RTP/AVP 0  
  2. a=rtpmap:8 pcmu/8000/1  
  3. a=ptime:20  
  4. c=IN IP4 192.168.1.18


你可能感兴趣的:(网络开发)