首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > 多媒体 >

g.711音频文件用rtp发送解决思路

2012-02-17 
g.711音频文件用rtp发送[codeC/C++][/code]#include stdio.h#include stdlib.h#include string.h#i

g.711音频文件用rtp发送
[code=C/C++][/code]#include <stdio.h>

#include <stdlib.h>

#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

{

  /**//* byte 0 */

  unsigned char csrc_len:4; /**//* expect 0 */

  unsigned char extension:1; /**//* expect 1, see RTP_OP below */

  unsigned char padding:1; /**//* expect 0 */

  unsigned char version:2; /**//* expect 2 */

  /**//* byte 1 */

  unsigned char payload:7; /**//* RTP_PAYLOAD_RTSP */

  unsigned char marker:1; /**//* expect 1 */

  /**//* bytes 2, 3 */

  unsigned short seq_no;

  /**//* bytes 4-7 */

  unsigned long timestamp;

  /**//* bytes 8-11 */

  unsigned long ssrc; /**//* stream number is used here. */

} 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),512,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");

  /**

  unsigned char *Buf;

  if ((Buf = (unsigned char*)calloc (512 , sizeof(char))) == NULL)

  {

  printf (" Could not allocate Buf memory\n");

  }

  Buf = &sendbuf[12];

  */

  /**

  ret = fread(Buf,1,511,file_g711);

  if(ret<0)

  {

  printf("fread error\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[13]= %x\n",sendbuf[13]);

  printf("**********************************\n");



  usleep(500000);

  memset(sendbuf,0,1500);//清空sendbuf;此时会将上次的时间戳清空,因此需要ts_current来保存上次的时间戳值

  }



  fclose(file_g711);

  printf("end !!!!!!!!!!!!!!!!!!\n");

  return 0;

}


[解决办法]
不懂 帮顶
[解决办法]
用Audiochecke检测的结果也一样。前者是CDDA,后者是mpeg(要先转换成wav才能检测)。

热点排行