[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]);