rtptst.cpp
资源名称:h323.zip [点击查看]
上传用户:hnnddl
上传日期:2007-01-06
资源大小:3580k
文件大小:11k
源码类别:
IP电话/视频会议
开发平台:
WINDOWS
- /*
- * $Revision: 1.1 $
- * $Date: 1997/08/15 15:45:39 $
- */
- ////////////////////////////////////////////////////////////////
- // Copyright (c) 1996 Lucent Technologies //
- // All Rights Reserved //
- // //
- // THIS IS UNPUBLISHED //
- // PROPRIETARY SOURCE //
- // CODE OF Lucent Technologies //
- // AND elemedia //
- // //
- // The copyright notice above does not evidence any //
- // actual or intended publication of such source code//
- ////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////
- // File : rtptst.cpp //
- // //
- // Test the RTP library. //
- // Bidirectional RTP pipe. //
- // Read from local and write into remote and vice versa.. //
- // Treats packets from remote side as being conformat to the //
- // RTP protocol, and the local side as raw UDP packets. //
- // //
- // --------------------------------------------- //
- // REMOTE RTPTST.EXE LOCAL //
- // END END //
- // --------------------------------------------- //
- // RTP -----> Deframe RTP -----> UDP packets //
- // Packets //
- // //
- // RTP //
- // Packets <------Frame RTP <-----UDP packets //
- // //
- // History: //
- // 24_Jan_1997 Created //
- // 15_Aug_1997 Merged the win32 and unix versions. //
- // //
- /////////////////////////////////////////////////////////////////
- #include "stdio.h"
- #if (defined(WIN32))
- #include <windows.h>
- #include <winsock.h>
- #include <assert.h>
- #else
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <thread.h>
- #include <unistd.h>
- #include <signal.h>
- #include <errno.h>
- #endif
- #include "rtp/rtp.h"
- #if (!defined(WIN32))
- #define SOCKET_ERROR -1
- #if (defined(__sun))
- #define INADDR_NONE 0xFFFFFFFF
- #endif
- typedef int SOCKET;
- #endif
- #define NUM_PATHS 2
- #define RTCP_INTERVAL 60 // rtcp activity once every 60 packets(90ms).
- char ip_address[2][256];
- unsigned short src_port[NUM_PATHS];
- unsigned short dst_port[NUM_PATHS];
- struct sockaddr_in src_addr[NUM_PATHS];
- struct sockaddr_in dst_addr[NUM_PATHS];
- SOCKET src[NUM_PATHS], dst[NUM_PATHS];
- SOCKET path[NUM_PATHS];
- SOCKET rtcp[2];
- struct sockaddr_in rtcp_addr[NUM_PATHS];
- int use_rtp = 1;
- void
- print_usage(char *appname)
- {
- printf("Usage: %s nonrtp-ip/input/output rtp-ip/input/outputn", appname);
- }
- void
- addr_init(struct sockaddr_in *addr, char *ip_address,
- unsigned short port)
- {
- unsigned long ip;
- memset(addr, 0, sizeof(*addr));
- addr->sin_family = AF_INET;
- addr->sin_port = htons(port);
- if (ip_address)
- {
- ip = inet_addr(ip_address);
- if (ip == INADDR_NONE)
- {
- struct hostent *hent_p =
- gethostbyname((char *)ip_address);
- if(hent_p == NULL)
- {
- printf("Gethostbyname failed on %sn",ip_address);
- return;
- }
- ip = *(unsigned long *)(hent_p->h_addr_list[0]);
- }
- }
- else
- {
- ip = INADDR_ANY;
- }
- addr->sin_addr.s_addr = ip;
- }
- void
- socket_error(SOCKET s, char *message)
- {
- #if (defined(WIN32))
- printf("socket = %d, errno = %d, %sn",
- s, WSAGetLastError(), message);
- #else
- printf("socket = %d, errno = %d, %sn",
- s, errno, message);
- #endif
- }
- void just_do_it()
- {
- fd_set read_fds;
- char buffer[4096];
- char *buf = buffer;
- int buf_len = sizeof(buffer);
- int length;
- int i;
- int packets_sent_count = 0;
- int packets_recd_count = 0;
- printf("Begin...n");
- path[0] = dst[1];
- path[1] = dst[0];
- FD_ZERO(&read_fds);
- for (i=0; i<NUM_PATHS; ++i)
- {
- FD_SET(src[i],&read_fds);
- }
- if (use_rtp)
- {
- boolean frame[2];
- RTPSession session(ip_address[0],RTPPacket::PT_PCMU,8000);
- RTPPacket *framer = NULL;
- RTPPacket *deframer = NULL;
- RTCPPacket rtcp_packet(&session);
- int nfds = 0;
- deframer = new RTPPacket(&session,0);
- frame[0] = TRUE;
- frame[1] = FALSE;
- for (i=0;i<NUM_PATHS;++i)
- {
- if (nfds < src[i])
- {
- nfds = src[i];
- }
- }
- nfds++;
- while (1)
- {
- select(nfds, &read_fds, NULL, NULL, NULL);
- for (i=0;i<NUM_PATHS;++i)
- {
- if (FD_ISSET(src[i],&read_fds))
- {
- if (frame[i])
- {
- if (!framer)
- {
- length = recv(src[i],buf, buf_len,0);
- framer = new RTPPacket(&session,length);
- printf("Framer created payload len = %d, packetlen = %dn",
- length, framer->GetMaxPacketLength());
- memcpy(framer->GetPayload(), buf, length);
- }
- else
- {
- length = recv(src[i],framer->GetPayload(),
- framer->GetMaxPayloadLength(),0);
- }
- framer->Frame(session.GetMediaTime(),0, length);
- send(path[i], framer->GetPacket(),
- framer->GetPacketLength(),0);
- packets_sent_count++;
- }
- else
- {
- length = recv(src[i],buf,buf_len,0);
- if (length > 0)
- {
- int ret =
- deframer->Deframe(buf,length, session.GetMediaTime());
- if (ret != 0)
- {
- printf("RTPPacket::Deframe returned %dn",ret);
- }
- send(path[i], deframer->GetPayload(),
- deframer->GetPayloadLength(),0);
- ++packets_recd_count;
- }
- }
- }
- }
- if (FD_ISSET(rtcp[0],&read_fds))
- {
- unsigned int ntp_sec;
- unsigned int ntp_frac;
- RTCPSenderInfo *sender_info;
- printf("Received rtcp packet..n");
- length = recv(rtcp[0], buf,buf_len,0);
- ntp64time(ntp_sec,ntp_frac);
- rtcp_packet.Deframe(buf,length, ntp_sec,
- ntp_frac);
- rtcp_packet.GetSenderInfo(sender_info);
- if (sender_info)
- {
- printf("Sender info..n");
- printf("Num packets = %dn", sender_info->sr_np);
- printf("Num bytes = %dn", sender_info->sr_nb);
- }
- int rr;
- RTCPReceptionReport *rr_list;
- rr = rtcp_packet.GetReceptionReports(rr_list);
- printf("Receiver report for %d reportsn",rr);
- while (rr > 0)
- {
- printf("{n"
- "trr_srcid = 0x%lxn"
- "trr_loss = %d:%dn"
- "trr_ehsr = %dn"
- "trr_dv = %dn"
- "trr_lsr = %dn"
- "trr_dlsr = %dn"
- "}n",
- rr_list[rr - 1].rr_srcid,
- RTCP_GET_INCREMENTAL_LOSS(rr_list[rr - 1].rr_loss),
- RTCP_GET_CUMULATIVE_LOSS(rr_list[rr - 1].rr_loss),
- rr_list[rr - 1].rr_ehsr,
- rr_list[rr - 1].rr_dv,
- rr_list[rr - 1].rr_lsr,
- rr_list[rr - 1].rr_dlsr);
- --rr;
- }
- RTPEndpointInfo *e;
- e = session.GetParticipant(rtcp_packet.GetSSRC());
- if (e)
- {
- RTPEndpointStats res;
- e->GetStatistics(res);
- printf("Endpoint stats for 0x%lxn"
- "{n"
- "tprobation = %dn"
- "tmisorder = %dn"
- "treceived = %dn"
- "tdelay_variance = %dn"
- "tpackets_sent = %dn"
- "toctets_sent = %dn"
- "}n",
- res.es_ssrc,
- res.es_probation,
- res.es_misorder,
- res.es_received,
- res.es_delay_variance,
- res.es_packets_sent,
- res.es_octets_sent);
- }
- }
- if ((packets_sent_count == RTCP_INTERVAL) || (packets_recd_count ==
- RTCP_INTERVAL))
- {
- unsigned int ntp_sec;
- unsigned int ntp_frac;
- //TODO
- // build and send an RTCP report
- printf("Sending rtcp packet..n");
- ntp64time(ntp_sec,ntp_frac);
- rtcp_packet.Frame(session.GetMediaTime(), ntp_sec,
- ntp_frac, 0,0, NULL,0);
- send(rtcp[1],rtcp_packet.GetPacket(),
- rtcp_packet.GetPacketLength(),0);
- if (packets_sent_count == RTCP_INTERVAL)
- {
- packets_sent_count = 0;
- }
- else
- {
- packets_recd_count = 0;
- }
- }
- FD_ZERO(&read_fds);
- for (i=0; i<NUM_PATHS; ++i)
- {
- FD_SET(src[i],&read_fds);
- }
- FD_SET(rtcp[0],&read_fds); // rtcp socket.
- }
- }
- else
- {
- while (1)
- {
- select(0, &read_fds, NULL, NULL, NULL);
- for (i=0;i<NUM_PATHS;++i)
- {
- if (FD_ISSET(src[i],&read_fds))
- {
- length = recv(src[i],buf,buf_len,0);
- if (length > 0)
- {
- send(path[i],buf,length,0);
- }
- }
- }
- FD_ZERO(&read_fds);
- for (i=0; i<NUM_PATHS; ++i)
- {
- FD_SET(src[i],&read_fds);
- }
- }
- }
- }
- int
- parse_command_line(int argc, char **argv)
- {
- int tmp1;
- int tmp2;
- if (argc < 3)
- {
- return 1;
- }
- if (sscanf(argv[1], "%[^/]%*[/]%d%*[/]%d", ip_address[0],
- &tmp1, &tmp2) != 3)
- {
- return 1;
- }
- src_port[0] = tmp1;
- dst_port[0] = tmp2;
- if (sscanf(argv[2], "%[^/]%*[/]%d%*[/]%d", ip_address[1],
- &tmp1, &tmp2) != 3)
- {
- return 1;
- }
- src_port[1] = tmp1;
- dst_port[1] = tmp2;
- printf("%s/%d/%dn",ip_address[0], src_port[0], dst_port[0]);
- printf("%s/%d/%dn",ip_address[1], src_port[1], dst_port[1]);
- if (argc >= 4)
- {
- if (!stricmp(argv[3],"nortp"))
- use_rtp = 0;
- else
- use_rtp = 1;
- }
- printf("USE_RTP = %sn",use_rtp ? "Yes" : "No");
- return 0;
- }
- int
- main (int argc, char ** argv)
- {
- #if (defined(WIN32))
- WSADATA wsadata;
- WSAStartup(MAKEWORD(1,1), &wsadata);
- #endif
- if (parse_command_line(argc,argv))
- {
- print_usage(argv[0]);
- exit(1);
- }
- int i;
- // the sockets...
- src[0] = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- src[1] = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- dst[0] = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- dst[1] = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (src[0] == SOCKET_ERROR || dst[0] == SOCKET_ERROR ||
- (src[1] == SOCKET_ERROR || dst[1] == SOCKET_ERROR))
- {
- #if (defined(WIN32))
- printf("Socket creation failed %dn", WSAGetLastError());
- #else
- printf("Socket creation failed %dn", errno);
- #endif
- exit(0);
- }
- for (i=0;i<NUM_PATHS; ++i)
- {
- addr_init(src_addr + i, NULL, src_port[i]);
- addr_init(dst_addr + i, ip_address[i], dst_port[i]);
- printf("binding on socket = %d, ip = 0x%lx, port = %dn", src[i],
- htonl(((struct sockaddr_in*)(src_addr + i))->sin_addr.s_addr),
- htons(((struct sockaddr_in*)(src_addr + i))->sin_port));
- if (bind(src[i],(struct sockaddr*)(src_addr + i),
- sizeof(struct sockaddr_in)) == SOCKET_ERROR)
- {
- socket_error(src[i],"bind");
- exit(1);
- }
- printf("connect on socket = %d, ip = 0x%x, port = %dn", dst[i],
- htonl(((struct sockaddr_in*)(dst_addr + i))->sin_addr.s_addr),
- htons(((struct sockaddr_in*)(dst_addr + i))->sin_port));
- if (connect(dst[i],(struct sockaddr*)(dst_addr + i),
- sizeof(struct sockaddr_in)) == SOCKET_ERROR)
- {
- socket_error(dst[i],"connect");
- exit(1);
- }
- }
- rtcp[0] = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);// recv
- rtcp[1] = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); // send
- addr_init(rtcp_addr + 0, NULL, src_port[1] + 1); // listen
- addr_init(rtcp_addr + 1, ip_address[1], dst_port[1] + 1); // send
- if (connect(rtcp[1],(struct sockaddr*)(rtcp_addr + 1),
- sizeof(struct sockaddr_in)) == SOCKET_ERROR)
- {
- socket_error(rtcp[1],"connect");
- exit(1);
- }
- if (bind(rtcp[0],(struct sockaddr*)(rtcp_addr + 0),
- sizeof(struct sockaddr_in)) == SOCKET_ERROR)
- {
- socket_error(rtcp[0],"bind");
- exit(1);
- }
- just_do_it();
- }