- / g711.c
- //
- #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; /** stream type */
- unsigned char marker:1; /** when send the first framer,set it */
- /** 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),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");
- /**
- 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[12]= %x/n",sendbuf[12]);
- printf("**********************************/n");
- usleep(19000);
- memset(sendbuf,0,1500);//清空sendbuf;此时会将上次的时间戳清空,因此需要ts_current来保存上次的时间戳值
- }
- 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