rtpstuff.cpp
资源名称:h323.zip [点击查看]
上传用户:hnnddl
上传日期:2007-01-06
资源大小:3580k
文件大小:22k
源码类别:
IP电话/视频会议
开发平台:
WINDOWS
- /*
- * $Revision: 1.15 $
- * $Date: 1998/11/03 19:29:00 $
- */
- ////////////////////////////////////////////////////////////////
- // Copyright (c) 1996-98 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//
- ////////////////////////////////////////////////////////////////
- //
- ////////////////////////////////////////////////////////////////
- // Example programs are provided soley to demonstrate one //
- // possible use of the stack libraries and are included for //
- // instructional purposes only. You are free to use, modify //
- // and/or redistribute any portion of code in the example //
- // programs. However, such examples are not intended to //
- // represent production quality code. //
- // //
- // THE COPYRIGHT HOLDERS PROVIDE THESE EXAMPLE PROGRAMS //
- // "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED //
- // OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED //
- // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A //
- // PARTICULAR PURPOSE. //
- ////////////////////////////////////////////////////////////////
- #include <stdio.h>
- #include <fcntl.h>
- #if (defined(WIN32))
- #include <windows.h>
- #include <winsock.h>
- #include <assert.h>
- #elif defined(VXWORKS)
- #include "vxWorks.h"
- #include "taskLib.h"
- #include "semLib.h"
- #include "selectLib.h"
- #include "fcntl.h"
- #include "sockLib.h"
- #include "inetLib.h"
- #include "hostLib.h"
- #else
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #if (defined(__hpux))
- #include <pthread.h>
- #else
- #include <thread.h>
- #endif
- #include <unistd.h>
- #include <signal.h>
- #include <errno.h>
- #if (defined(USE_POLL))
- #include <stropts.h>
- #include <sys/poll.h>
- #endif
- #endif
- #include "exchange.h"
- #include "util/platform.h"
- #include "rtpstuff.h"
- #include "api/apierr.h"
- #if (!defined(WIN32))
- #define INVALID_SOCKET -1
- #define INVALID_HANDLE_VALIE -1
- #define SOCKET_ERROR -1
- #endif
- #define RTCP_INTERVAL 60
- #define FRAMES_PER_RTP_PACKET 3
- #define AUDIO_FRAME_SIZE 24
- #define AUDIO_FRAME_SIZE_IN_MS 30
- #if (defined(VXWORKS))
- #define AUDIO_FILE "/tmp/sample.723"
- #else
- #define AUDIO_FILE "sample.723"
- #endif
- #define PORT_LOWER_LIMIT 8000
- #define PORT_UPPER_LIMIT 10000
- static unsigned short next_port = PORT_LOWER_LIMIT;
- // ip and port in netbyte order...
- int
- H245RTPSession::my_bind(SOCKET s, unsigned int ip, unsigned short *port)
- {
- sockaddr_in addr;
- int ret;
- addr.sin_family = AF_INET;
- addr.sin_port = (*port) ? (*port) : htons(INADDR_ANY);
- #if (defined(VXWORKS)) // vxworks does not allow us to bind to a named address
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- #else
- addr.sin_addr.s_addr = (ip) ? (ip) : htonl(INADDR_ANY);
- #endif
- {
- ret = bind(s, (sockaddr*)&addr, sizeof(addr));
- if (!ret)
- {
- #if(defined(__sun) || defined(WIN32))
- int address_length = sizeof(addr);
- #else
- size_t address_length = sizeof(addr);
- #endif
- #if defined(__hpux)
- ::getsockname(s, (sockaddr *)&addr, (int *)&address_length);
- #else
- ::getsockname(s, (sockaddr *)&addr, &address_length);
- #endif
- *port = addr.sin_port;
- }
- }
- return ret;
- }
- // ip and port in netbyte order..
- int
- H245RTPSession::my_connect(SOCKET s, unsigned int ip, unsigned short port)
- {
- sockaddr_in addr;
- addr.sin_family = AF_INET;
- addr.sin_port = port;
- addr.sin_addr.s_addr = ip;
- return connect(s, (sockaddr*)&addr, sizeof(addr));
- }
- void
- H245RTPSession::socket_error(SOCKET s, char *message)
- {
- LOG("H245RTPSession::socket_errorn");
- #if (defined(WIN32))
- LOG("H245RTPSession::socket = %d, errno = %d, %sn",
- s, WSAGetLastError(), message);
- #else
- LOG("H245RTPSession::socket = %d, errno = %d, %sn",
- s, errno, message);
- #endif
- }
- // return 0 on success, -1 on failure.
- H245RTPSession::H245RTPSession(char * session_name, int pt,int sr, int &status):
- RTPSession(session_name, pt, sr)
- {
- LOG("H245RTPSession::H245RTPSessionn");
- audio_fh = NULL;
- framer = deframer = NULL;
- rtcp_packet = NULL;
- status = 0;
- lrtp_port = 0;
- lrtcp_port = 0;
- rrtp_port = 0;
- rrtcp_port = 0;
- current_tc = 0;
- remote_address = 0;
- local_address = HTONS(INADDR_ANY);
- #if defined(WIN32)
- stop_rtpsession_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- #elif defined(VXWORKS)
- stop_rtpsession_socket = -1;
- #else
- if (pipe(stop_rtpsession_pipefds) < 0)
- {
- LOG("pipe creation failedn");
- }
- stop_rtpsession_socket = stop_rtpsession_pipefds[0];
- #endif
- lrtp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- lrtcp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- rrtp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- rrtcp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if ( lrtp_socket == INVALID_SOCKET ||
- lrtcp_socket== INVALID_SOCKET ||
- rrtp_socket== INVALID_SOCKET ||
- rrtcp_socket== INVALID_SOCKET
- #if !defined(VXWORKS) && !defined(__hpux)
- || stop_rtpsession_socket == INVALID_SOCKET
- #endif
- )
- {
- status = -1;
- socket_error((unsigned int)-1,"Socket creation failedn");
- }
- #if (defined(VXWORKS))
- thread_id = ERROR;
- #elif (defined(__hpux))
- thread_id = cma_c_null;
- #else
- thread_id = 0;
- #if (defined(WIN32))
- thread_handle = INVALID_HANDLE_VALUE;
- #endif
- #endif
- }
- H245RTPSession::~H245RTPSession()
- {
- // Stop the RTP session...
- StopSession();
- #if (!defined(WIN32))
- #define closesocket close
- #endif
- if (lrtp_socket != INVALID_SOCKET)
- {
- closesocket(lrtp_socket);
- }
- if (lrtcp_socket != INVALID_SOCKET)
- {
- closesocket(lrtcp_socket);
- }
- if (rrtp_socket != INVALID_SOCKET)
- {
- closesocket(rrtp_socket);
- }
- if (rrtcp_socket != INVALID_SOCKET)
- {
- closesocket(rrtcp_socket);
- }
- #if !defined(WIN32)
- #undef closesocket
- #endif
- // Free up other resources..
- // asked to terminate ourselves;
- if (audio_fh != NULL)
- {
- fclose(audio_fh);
- audio_fh = NULL;
- }
- if (framer)
- {
- delete framer;
- }
- if (deframer)
- {
- delete deframer;
- }
- if (rtcp_packet)
- {
- delete rtcp_packet;
- }
- #if (!defined(WIN32) && !defined(VXWORKS))
- if (stop_rtpsession_pipefds[0] != INVALID_SOCKET)
- {
- close(stop_rtpsession_pipefds[0]);
- close(stop_rtpsession_pipefds[1]);
- }
- #endif
- }
- int
- H245RTPSession::SetLocalPorts(unsigned int ip, unsigned short &rtp_port,
- unsigned short &rtcp_port)
- {
- unsigned short tmp1, tmp2;
- local_address = ip;
- if (rtp_port == 0 || rtcp_port == 0)
- {
- tmp1 = htons(next_port);
- tmp2 = htons(next_port + 1);
- next_port += 2;
- if (next_port == PORT_UPPER_LIMIT)
- {
- next_port = PORT_LOWER_LIMIT;
- }
- }
- else
- {
- tmp1 = rtp_port;
- tmp2 = rtcp_port;
- }
- // Warning! if bind fails on the following, you should try binding
- // for the next consecutive pair of ports and not simply return error
- if (!lrtp_port)
- {
- lrtp_port = tmp1;
- if (my_bind(lrtp_socket, ip, &lrtp_port) == SOCKET_ERROR)
- {
- socket_error(lrtp_socket, "bind, lrtp_socket");
- return(-1);
- }
- rtp_port = lrtp_port;
- }
- else
- {
- rtp_port = lrtp_port;
- }
- if (!lrtcp_port)
- {
- lrtcp_port = tmp2;
- if (my_bind(lrtcp_socket, ip, &lrtcp_port) == SOCKET_ERROR)
- {
- socket_error(lrtp_socket, "bind, lrtp_socket");
- return(-1);
- }
- rtcp_port = lrtcp_port;
- }
- else
- {
- rtcp_port = lrtcp_port;
- }
- return(0);
- }
- void
- H245RTPSession::GetLocalPorts(unsigned int& ip, unsigned short &rtp_port,
- unsigned short &rtcp_port)
- {
- ip = local_address;
- rtp_port = lrtp_port;
- rtcp_port = lrtcp_port;
- }
- // assumes unicast ip address for now....
- int
- H245RTPSession::SetRemotePorts(unsigned int ip, unsigned short rtp_port,
- unsigned short rtcp_port)
- {
- remote_address = ip;
- if (!rrtp_port && rtp_port)
- {
- rrtp_port = rtp_port;
- if (my_connect(rrtp_socket, ip, rrtp_port) == SOCKET_ERROR)
- {
- socket_error(rrtp_socket, "connect, rrtp_socket");
- return(-1);
- }
- }
- if (!rrtcp_port && rtcp_port)
- {
- rrtcp_port = rtcp_port;
- if (my_connect(rrtcp_socket, ip, rrtcp_port) == SOCKET_ERROR)
- {
- socket_error(rrtp_socket, "connect, rrtp_socket");
- return(-1);
- }
- }
- return(0);
- }
- int
- H245RTPSession::StartSession()
- {
- static int first_time = 1;
- LOG("H245RTPSession::StartSessionn");
- if (init_rtp_session() != 0)
- {
- LOG("H245RTPSession:: cannot init h245 rtp sessionn");
- return(-1);
- }
- #if (defined(WIN32))
- thread_handle = (HANDLE) _beginthreadex(NULL,
- 0 ,
- _begin_rtp_session,
- this,
- 0,
- (unsigned *)&thread_id);
- if (thread_handle == INVALID_HANDLE_VALUE)
- {
- return(-1);
- }
- else
- {
- LOG("H245RTPSession::Successfully spawned rtp sessionn");
- }
- #elif (defined(VXWORKS))
- thread_id = taskSpawn("do_rtp",
- 90,
- 0,
- 8096,
- (FUNCPTR)_begin_rtp_session,
- (int)this,
- 0,0,0,0,0,0,0,0,0);
- if (thread_id == ERROR)
- {
- LOG("StartSession: taskSpawn failed, errno = 0x%xn", errno);
- return -1;
- }
- #elif (defined(__hpux))
- int ret = pthread_create( &thread_id,
- pthread_attr_default,
- _begin_rtp_session,
- this);
- LOG("StartSession: pthread_create returned for &thread_id: %xn",&thread_id)
- ;
- if (ret != 0)
- {
- LOG("StartSession: thread creation failed, errno = %dn", errno);
- return(-1);
- }
- else
- {
- LOG("H245RTPSession::Successfully spawned rtp sessionn");
- }
- #else
- int ret = thr_create(NULL,
- 0 ,
- _begin_rtp_session,
- this,
- 0,
- &thread_id);
- if (ret != 0)
- {
- LOG("StartSession: thread creation failed, errno = %dn", errno);
- return(-1);
- }
- else
- {
- LOG("H245RTPSession::Successfully spawned rtp sessionn");
- }
- #endif
- if (first_time)
- {
- first_time = 0;
- }
- return(0);
- }
- void
- H245RTPSession::StopSession()
- {
- LOG("stopping session thread_id = %dn", thread_id);
- #if (defined(WIN32))
- if (thread_handle != INVALID_HANDLE_VALUE)
- {
- if (stop_rtpsession_socket != INVALID_SOCKET)
- {
- closesocket(stop_rtpsession_socket);
- stop_rtpsession_socket = INVALID_SOCKET;
- }
- // Wait for the thread to exit.
- if (WaitForSingleObject(thread_handle, 1000*5) == WAIT_TIMEOUT)
- {
- LOG("Wait for RTP thread timedoutn");
- TerminateThread(thread_handle,0);
- }
- CloseHandle(thread_handle);
- thread_handle = INVALID_HANDLE_VALUE;
- }
- #elif (defined(VXWORKS))
- if (thread_id != ERROR)
- {
- LOG("Calling taskIdVerify(%d)n",thread_id);
- if (taskIdVerify(thread_id) == OK)
- {
- LOG("Calling taskDelete(%d)n",thread_id);
- if (taskDelete(thread_id) == ERROR)
- {
- LOG("StopSession: taskDelete failed..n");
- }
- }
- }
- #elif (defined(__hpux))
- if (!pthread_equal(thread_id,cma_c_null))
- {
- pthread_t who_exited;
- pthread_addr_t status;
- close (stop_rtpsession_pipefds[0]);
- close (stop_rtpsession_pipefds[1]);
- stop_rtpsession_pipefds[0]=stop_rtpsession_pipefds[1] = INVALID_SOCKET;
- // Wait for the thread to exit.
- if (pthread_join(thread_id,&status) != 0)
- {
- LOG("Wait for RTP thread timedoutn");
- pthread_cancel(thread_id);
- }
- pthread_detach(&thread_id);
- thread_id = cma_c_null;
- }
- #else
- if (thread_id != 0)
- {
- thread_t who_exited;
- void *status;
- close (stop_rtpsession_pipefds[0]);
- close (stop_rtpsession_pipefds[1]);
- stop_rtpsession_pipefds[0]=stop_rtpsession_pipefds[1] = INVALID_SOCKET;
- // Wait for the thread to exit.
- if (thr_join(thread_id,&who_exited,&status) != 0)
- {
- LOG("Wait for RTP thread timedoutn");
- thr_kill(thread_id,SIGHUP);
- }
- thread_id = 0;
- }
- #endif
- }
- int
- H245RTPSession::init_rtp_session()
- {
- LOG("H245RTPSession::init_rtp_sessionn");
- // assign port numbers and bind
- if (lrtp_port == 0 || lrtcp_port == 0)
- {
- if (SetLocalPorts(local_address, lrtp_port, lrtcp_port)
- == -1)
- {
- LOG("H245RTPSession::SetLocalPorts failedn");
- return(-1);
- }
- }
- return(0);
- }
- void
- H245RTPSession::recv_rtcp_packet(SOCKET rtcp_sock,
- RTCPPacket *packet)
- {
- unsigned int ntp_sec;
- unsigned int ntp_frac;
- char buffer[4096];
- char *buf = buffer;
- int buf_len = sizeof(buffer);
- int length;
- length = recv(rtcp_sock, buf,buf_len,0);
- ntp64time(ntp_sec,ntp_frac);
- packet->Deframe(buf,length, ntp_sec,
- ntp_frac);
- #if (defined(__ENABLE_LOGGING__))
- RTCPSenderInfo *sender_info;
- packet->GetSenderInfo(sender_info);
- if (sender_info)
- {
- LOG("tSender infont{n");
- LOG("ttNum packets = %dn", sender_info->sr_np);
- LOG("ttNum bytes = %dnt}n", sender_info->sr_nb);
- }
- int rr;
- RTCPReceptionReport *rr_list;
- rr = packet->GetReceptionReports(rr_list);
- if (rr > 0)
- LOG("tReceiver report for %d receiversn",rr);
- while (rr > 0)
- {
- LOG("t{n"
- "ttrr_srcid = 0x%lxn"
- "ttrr_loss = %d:%dn"
- "ttrr_ehsr = %dn"
- "ttrr_dv = %dn"
- "ttrr_lsr = %dn"
- "ttrr_dlsr = %dn"
- "t}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 = GetParticipant(packet->GetSSRC());
- if (e)
- {
- RTPEndpointStats res;
- e->GetStatistics(res);
- if (res.es_received > 0)
- {
- LOG("tEndpoint stats for 0x%lxn"
- "t{n"
- "ttprobation = 0x%lxn"
- "ttmisorder = %dn"
- "ttreceived = %dn"
- "ttdelay_variance = %dn"
- "ttpackets_sent = %dn"
- "ttoctets_sent = %dn"
- "t}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);
- }
- }
- #endif
- }
- void
- H245RTPSession::send_rtcp_packet(SOCKET rtcp_sock,
- RTCPPacket *packet)
- {
- unsigned int ntp_sec;
- unsigned int ntp_frac;
- ntp64time(ntp_sec,ntp_frac);
- packet->Frame(current_tc, ntp_sec,
- ntp_frac, 0,0, NULL,0);
- send(rtcp_sock,packet->GetPacket(),
- packet->GetPacketLength(),0);
- }
- #if (defined(WIN32))
- unsigned __stdcall
- #elif (defined(VXWORKS))
- int
- #else
- void *
- #endif
- H245RTPSession::_begin_rtp_session(void *c)
- {
- H245RTPSession *s = (H245RTPSession *) c;
- s->do_rtp();
- return 0;
- }
- #if defined(USE_POLL)
- #define RTP_SOCKET_INDEX 0
- #define RTCP_SOCKET_INDEX 1
- #define INTERCONNECT_INDEX 2
- void
- H245RTPSession::do_rtp()
- {
- int nfds;
- int length;
- char buffer[4096];
- char *buf = buffer;
- struct pollfd pfds[8];
- int packets_sent_count = 0;
- int packets_recd_count = 0;
- int buf_len = sizeof(buffer);
- int interconnect;
- struct timeval timeout;
- int tout = AUDIO_FRAME_SIZE_IN_MS * FRAMES_PER_RTP_PACKET; /*ms*/
- audio_fh = fopen(AUDIO_FILE, "r+b");
- if (audio_fh == NULL)
- {
- LOG("Error: do_rtp, failed to open %sn", AUDIO_FILE);
- }
- rtcp_packet = new RTCPPacket(this);
- deframer = new RTPPacket(this,0);
- timeout.tv_sec = 0;
- timeout.tv_usec = tout;
- // Set the session payload type..
- SetPayloadType(local_pt);
- LOG("Begin _do_rtpn");
- interconnect = stop_rtpsession_socket;
- // First identify ourselves in the session..
- send_rtcp_packet(rrtcp_socket, rtcp_packet);
- memset(pfds,0, sizeof(pfds));
- nfds = 2;
- pfds[RTP_SOCKET_INDEX].fd = lrtp_socket;
- pfds[RTP_SOCKET_INDEX].events = POLLIN;
- pfds[RTCP_SOCKET_INDEX].fd = lrtcp_socket;
- pfds[RTCP_SOCKET_INDEX].events = POLLIN;
- if (interconnect != INVALID_SOCKET)
- {
- nfds++;
- pfds[INTERCONNECT_INDEX].fd = interconnect;
- }
- while (1)
- {
- int ret;
- pfds[RTCP_SOCKET_INDEX].revents = 0;
- pfds[RTCP_SOCKET_INDEX].revents = 0;
- if ((poll(pfds,nfds,tout) == 0) && audio_fh != NULL)
- {
- // The two types of Frame functions have been demonstrated
- // below (within the #if 1 else #endif).
- #if 1
- if (!framer)
- {
- length = fread(buf, 1,
- (AUDIO_FRAME_SIZE * FRAMES_PER_RTP_PACKET), audio_fh);
- if (length <= 0)
- {
- fclose(audio_fh);
- audio_fh = NULL;
- }
- framer = new RTPPacket(this,length);
- LOG("Framer created payload len = %d, packetlen = %dn",
- length, framer->GetMaxPacketLength());
- memcpy(framer->GetPayload(), buf, length);
- }
- else
- {
- length = fread(framer->GetPayload(), 1,
- (AUDIO_FRAME_SIZE * FRAMES_PER_RTP_PACKET),
- audio_fh);
- if (length <= 0)
- {
- rewind(audio_fh);
- continue;
- }
- }
- framer->Frame(current_tc, 0, length);
- send(rrtp_socket, framer->GetPacket(),
- framer->GetPacketLength(),0);
- #else
- // BEGIN: New RTPPacket::Frame demo
- // Following code demonstrates use of RTPPacket::Frame
- // with one less buffer copy.
- if (!framer)
- {
- // Don't let RTPPacket allocate any space for
- // the payload (max_payload = 0)
- framer = new RTPPacket(this,0);
- }
- // Leave just enough space at the beginning of the
- // buffer for RTPHeader..
- length = fread(buffer + framer->GetHeaderLength(),1,
- (AUDIO_FRAME_SIZE * FRAMES_PER_RTP_PACKET), audio_fh);
- if (length <= 0)
- {
- rewind(audio_fh);
- continue;
- }
- framer->Frame(buf,sizeof(buffer),current_tc,0,length);
- send(rrtp_socket, buffer,framer->GetPacketLength(),0);
- // END:: New RTPPacket::Frame demo
- #endif
- current_tc += (AUDIO_FRAME_SIZE_IN_MS * FRAMES_PER_RTP_PACKET)*8;
- packets_sent_count++;
- timeout.tv_sec = 0;
- timeout.tv_usec = tout;
- }
- if ((interconnect != INVALID_SOCKET) &&
- pfds[INTERCONNECT_INDEX].revents)
- {
- LOG("H245RTPSession::do_rtp "
- "pfds[INTERCONNECT_INDEX].revents = %xn",
- pfds[INTERCONNECT_INDEX].revents);
- LOG("RTPThread:: exitingn");
- #if defined(__hpux)
- pthread_exit(NULL);
- #else
- thr_exit(NULL);
- #endif
- }
- if (pfds[RTP_SOCKET_INDEX].revents & POLLIN)
- {
- length = recv(lrtp_socket,buf,buf_len,0);
- if (length > 0)
- {
- int ret = deframer->Deframe(buf,length,current_tc);
- if (!PROT_IS_SUCCESS(ret))
- {
- LOG("RTPPacket::Deframe returned %dn",ret);
- }
- ++packets_recd_count;
- }
- }
- if (pfds[RTCP_SOCKET_INDEX].revents & POLLIN)
- {
- recv_rtcp_packet(lrtcp_socket, rtcp_packet);
- }
- if ((packets_recd_count == RTCP_INTERVAL) ||
- (packets_sent_count == RTCP_INTERVAL))
- {
- send_rtcp_packet(rrtcp_socket, rtcp_packet);
- packets_recd_count = 0;
- packets_sent_count = 0;
- }
- }
- }
- #else // USE_POLL
- void
- H245RTPSession::do_rtp()
- {
- int nfds;
- int length;
- char buffer[4096];
- char *buf = buffer;
- fd_set read_fds;
- fd_set except_fds;
- int packets_sent_count = 0;
- int packets_recd_count = 0;
- int buf_len = sizeof(buffer);
- int interconnect;
- struct timeval timeout;
- int tout = AUDIO_FRAME_SIZE_IN_MS * FRAMES_PER_RTP_PACKET * 1000; /*us*/
- audio_fh = fopen(AUDIO_FILE, "r+b");
- if (audio_fh == NULL)
- {
- LOG("Error: do_rtp, failed to open %sn", AUDIO_FILE);
- }
- rtcp_packet = new RTCPPacket(this);
- deframer = new RTPPacket(this,0);
- timeout.tv_sec = 0;
- timeout.tv_usec = tout;
- // Set the session payload type..
- SetPayloadType(local_pt);
- LOG("Begin _do_rtpn");
- interconnect = stop_rtpsession_socket;
- // First identify ourselves in the session..
- send_rtcp_packet(rrtcp_socket, rtcp_packet);
- if (lrtp_socket > lrtcp_socket)
- {
- nfds = lrtp_socket;
- }
- else
- {
- nfds = lrtcp_socket;
- }
- if ((interconnect != INVALID_SOCKET) && (nfds < interconnect))
- {
- nfds = interconnect;
- }
- ++nfds;
- while (1)
- {
- int select_ret;
- FD_ZERO(&read_fds);
- FD_ZERO(&except_fds);
- FD_SET(lrtp_socket, &read_fds);
- FD_SET(lrtcp_socket, &read_fds);
- if (interconnect != INVALID_SOCKET)
- {
- FD_SET(interconnect, &read_fds);
- FD_SET(interconnect, &except_fds);
- }
- select_ret = select(
- nfds,
- &read_fds,
- NULL,
- &except_fds,
- &timeout);
- #if (defined(WIN32))
- assert(SOCKET_ERROR == -1);
- #endif
- if (select_ret == -1)
- {
- LOG("RTPThread:: exitingn");
- #if (defined(WIN32))
- //closesocket(interconnect);
- //_endthreadex(2);
- return;
- #elif (defined(VXWORKS))
- return;
- #else
- thr_exit(NULL);
- #endif
- }
- else if (select_ret == 0 && audio_fh != NULL)
- {
- // The two types of Frame functions have been demonstrated
- // below (within the #if 1 else #endif).
- #if 1
- if (!framer)
- {
- length = fread(buf, 1,
- (AUDIO_FRAME_SIZE * FRAMES_PER_RTP_PACKET), audio_fh);
- if (length <= 0)
- {
- fclose(audio_fh);
- audio_fh = NULL;
- }
- framer = new RTPPacket(this,length);
- LOG("Framer created payload len = %d, packetlen = %dn",
- length, framer->GetMaxPacketLength());
- memcpy(framer->GetPayload(), buf, length);
- }
- else
- {
- length = fread(framer->GetPayload(), 1,
- (AUDIO_FRAME_SIZE * FRAMES_PER_RTP_PACKET),
- audio_fh);
- if (length <= 0)
- {
- rewind(audio_fh);
- continue;
- }
- }
- framer->Frame(current_tc, 0, length);
- send(rrtp_socket, framer->GetPacket(),
- framer->GetPacketLength(),0);
- #else
- // BEGIN: New RTPPacket::Frame demo
- // Following code demonstrates use of RTPPacket::Frame
- // with one less buffer copy.
- if (!framer)
- {
- // Don't let RTPPacket allocate any space for
- // the payload (max_payload = 0)
- framer = new RTPPacket(this,0);
- }
- // Leave just enough space at the beginning of the
- // buffer for RTPHeader..
- length = fread(buffer + framer->GetHeaderLength(),1,
- (AUDIO_FRAME_SIZE * FRAMES_PER_RTP_PACKET), audio_fh);
- if (length <= 0)
- {
- rewind(audio_fh);
- continue;
- }
- framer->Frame(buf,sizeof(buffer),current_tc,0,length);
- send(rrtp_socket, buffer,framer->GetPacketLength(),0);
- // END:: New RTPPacket::Frame demo
- #endif
- current_tc += (AUDIO_FRAME_SIZE_IN_MS * FRAMES_PER_RTP_PACKET)*8;
- packets_sent_count++;
- timeout.tv_sec = 0;
- timeout.tv_usec = tout;
- }
- if (interconnect != INVALID_SOCKET &&
- FD_ISSET(interconnect, &except_fds))
- {
- // printf("do_rtp: exceptionn");
- LOG("RTPThread:: exitingn");
- #if (defined(WIN32))
- //closesocket(interconnect);
- //_endthreadex(2);
- return;
- #elif (defined(VXWORKS))
- return;
- #else
- thr_exit(NULL);
- #endif
- }
- if (FD_ISSET(lrtp_socket,&read_fds))
- {
- length = recv(lrtp_socket,buf,buf_len,0);
- if (length > 0)
- {
- int ret = deframer->Deframe(buf,length,current_tc);
- if (!PROT_IS_SUCCESS(ret))
- {
- LOG("RTPPacket::Deframe returned %dn",ret);
- }
- ++packets_recd_count;
- }
- }
- if (FD_ISSET(lrtcp_socket,&read_fds))
- {
- recv_rtcp_packet(lrtcp_socket, rtcp_packet);
- }
- if ((packets_recd_count == RTCP_INTERVAL) ||
- (packets_sent_count == RTCP_INTERVAL))
- {
- send_rtcp_packet(rrtcp_socket, rtcp_packet);
- packets_recd_count = 0;
- packets_sent_count = 0;
- }
- }
- }
- #endif