2005-05-02 18:31:05 +00:00
|
|
|
/**
|
|
|
|
* transport.cpp
|
|
|
|
* Yet Another RTP Stack
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
2006-05-27 15:18:34 +00:00
|
|
|
* Copyright (C) 2004-2006 Null Team
|
2005-05-02 18:31:05 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-05-27 15:18:34 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-05-02 18:31:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <yatertp.h>
|
|
|
|
|
|
|
|
#define BUF_SIZE 1500
|
|
|
|
|
|
|
|
using namespace TelEngine;
|
|
|
|
|
2006-09-26 09:00:10 +00:00
|
|
|
static unsigned long s_sleep = 5;
|
|
|
|
|
2006-05-27 15:18:34 +00:00
|
|
|
|
2006-09-26 09:00:10 +00:00
|
|
|
RTPGroup::RTPGroup(int msec, Priority prio)
|
2009-05-05 12:37:55 +00:00
|
|
|
: Mutex(true,"RTPGroup"),
|
|
|
|
Thread("RTP Group",prio), m_listChanged(false)
|
2005-05-02 18:31:05 +00:00
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugInfo,"RTPGroup::RTPGroup() [%p]",this);
|
2006-09-26 09:00:10 +00:00
|
|
|
if (msec < 1)
|
|
|
|
msec = 1;
|
|
|
|
if (msec > 50)
|
|
|
|
msec = 50;
|
|
|
|
m_sleep = msec;
|
2005-05-02 18:31:05 +00:00
|
|
|
}
|
|
|
|
|
2005-05-03 18:11:57 +00:00
|
|
|
RTPGroup::~RTPGroup()
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugInfo,"RTPGroup::~RTPGroup() [%p]",this);
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RTPGroup::cleanup()
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugInfo,"RTPGroup::cleanup() [%p]",this);
|
2005-05-03 18:11:57 +00:00
|
|
|
lock();
|
2006-01-19 21:13:49 +00:00
|
|
|
m_listChanged = true;
|
2005-05-03 18:11:57 +00:00
|
|
|
ObjList* l = &m_processors;
|
2005-07-20 04:42:29 +00:00
|
|
|
while (l) {
|
2005-05-03 18:11:57 +00:00
|
|
|
RTPProcessor* p = static_cast<RTPProcessor*>(l->get());
|
2005-07-20 04:42:29 +00:00
|
|
|
if (p) {
|
2005-05-03 18:11:57 +00:00
|
|
|
p->group(0);
|
2005-07-20 04:42:29 +00:00
|
|
|
if (p != static_cast<RTPProcessor*>(l->get()))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
l = l->next();
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
|
|
|
m_processors.clear();
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RTPGroup::run()
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugInfo,"RTPGroup::run() [%p]",this);
|
2005-05-03 18:11:57 +00:00
|
|
|
bool ok = true;
|
|
|
|
while (ok) {
|
2006-09-26 09:00:10 +00:00
|
|
|
unsigned long msec = m_sleep;
|
|
|
|
if (msec < s_sleep)
|
|
|
|
msec = s_sleep;
|
2005-05-03 18:11:57 +00:00
|
|
|
lock();
|
|
|
|
Time t;
|
|
|
|
ObjList* l = &m_processors;
|
2006-01-19 21:13:49 +00:00
|
|
|
m_listChanged = false;
|
2005-05-03 18:11:57 +00:00
|
|
|
for (ok = false;l;l = l->next()) {
|
|
|
|
RTPProcessor* p = static_cast<RTPProcessor*>(l->get());
|
|
|
|
if (p) {
|
|
|
|
ok = true;
|
|
|
|
p->timerTick(t);
|
2006-01-19 21:13:49 +00:00
|
|
|
// the list is protected from other threads but can be changed
|
|
|
|
// from this one so if it happened we just break out and try
|
|
|
|
// again later rather than using an expensive ListIterator
|
|
|
|
if (m_listChanged)
|
|
|
|
break;
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unlock();
|
2006-09-26 09:00:10 +00:00
|
|
|
Thread::msleep(msec,true);
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugInfo,"RTPGroup::run() ran out of processors [%p]",this);
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RTPGroup::join(RTPProcessor* proc)
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugAll,"RTPGroup::join(%p) [%p]",proc,this);
|
2005-05-03 18:11:57 +00:00
|
|
|
lock();
|
2006-01-19 21:13:49 +00:00
|
|
|
m_listChanged = true;
|
2005-05-03 18:11:57 +00:00
|
|
|
m_processors.append(proc)->setDelete(false);
|
|
|
|
startup();
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RTPGroup::part(RTPProcessor* proc)
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugAll,"RTPGroup::part(%p) [%p]",proc,this);
|
2005-05-03 18:11:57 +00:00
|
|
|
lock();
|
2006-01-19 21:13:49 +00:00
|
|
|
m_listChanged = true;
|
2005-05-03 18:11:57 +00:00
|
|
|
m_processors.remove(proc,false);
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
2006-09-26 09:00:10 +00:00
|
|
|
void RTPGroup::setMinSleep(int msec)
|
|
|
|
{
|
|
|
|
if (msec < 1)
|
|
|
|
msec = 1;
|
|
|
|
if (msec > 20)
|
|
|
|
msec = 20;
|
|
|
|
s_sleep = msec;
|
|
|
|
}
|
2006-05-27 15:18:34 +00:00
|
|
|
|
2007-02-01 23:33:30 +00:00
|
|
|
|
2005-07-20 04:42:29 +00:00
|
|
|
RTPProcessor::RTPProcessor()
|
2005-05-03 18:11:57 +00:00
|
|
|
: m_group(0)
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugAll,"RTPProcessor::RTPProcessor() [%p]",this);
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RTPProcessor::~RTPProcessor()
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugAll,"RTPProcessor::~RTPProcessor() [%p]",this);
|
2005-05-03 18:11:57 +00:00
|
|
|
group(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RTPProcessor::group(RTPGroup* newgrp)
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugAll,"RTPProcessor::group(%p) old=%p [%p]",newgrp,m_group,this);
|
2005-05-03 18:11:57 +00:00
|
|
|
if (newgrp == m_group)
|
|
|
|
return;
|
|
|
|
if (m_group)
|
|
|
|
m_group->part(this);
|
|
|
|
m_group = newgrp;
|
|
|
|
if (m_group)
|
|
|
|
m_group->join(this);
|
|
|
|
}
|
|
|
|
|
2006-05-27 15:18:34 +00:00
|
|
|
void RTPProcessor::rtpData(const void* data, int len)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RTPProcessor::rtcpData(const void* data, int len)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-03 18:52:40 +00:00
|
|
|
void RTPProcessor::getStats(String& stats) const
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-27 15:18:34 +00:00
|
|
|
|
2009-11-03 10:49:12 +00:00
|
|
|
RTPTransport::RTPTransport(RTPTransport::Type type)
|
2005-07-20 04:42:29 +00:00
|
|
|
: RTPProcessor(),
|
2009-11-03 10:49:12 +00:00
|
|
|
m_type(type), m_processor(0), m_monitor(0), m_autoRemote(false)
|
2005-05-03 18:11:57 +00:00
|
|
|
{
|
2009-11-03 10:49:12 +00:00
|
|
|
DDebug(DebugAll,"RTPTransport::RTPTransport(%d) [%p]",type,this);
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RTPTransport::~RTPTransport()
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
DDebug(DebugAll,"RTPTransport::~RTPTransport() [%p]",this);
|
2005-05-03 18:11:57 +00:00
|
|
|
setProcessor();
|
2005-05-10 14:54:04 +00:00
|
|
|
group(0);
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 18:31:05 +00:00
|
|
|
void RTPTransport::timerTick(const Time& when)
|
|
|
|
{
|
2005-07-20 04:42:29 +00:00
|
|
|
XDebug(DebugAll,"RTPTransport::timerTick() group=%p [%p]",group(),this);
|
2005-05-02 18:31:05 +00:00
|
|
|
if (m_rtpSock.valid()) {
|
2005-12-31 00:36:48 +00:00
|
|
|
char buf[BUF_SIZE];
|
|
|
|
SocketAddr addr;
|
2005-12-31 00:46:45 +00:00
|
|
|
int len;
|
2009-11-03 10:49:12 +00:00
|
|
|
while ((len = m_rtpSock.recvFrom(buf,sizeof(buf),addr)) > 0) {
|
|
|
|
switch (m_type) {
|
|
|
|
case RTP:
|
|
|
|
if (len < 12)
|
|
|
|
continue;
|
|
|
|
if (((unsigned char)buf[0] & 0xc0) != 0x80)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case UDPTL:
|
|
|
|
if (len < 6)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-05-26 12:25:37 +00:00
|
|
|
if (!m_remoteAddr.valid())
|
|
|
|
continue;
|
2010-09-15 14:23:20 +00:00
|
|
|
// looks like it's RTP or UDPTL, at least by length and version
|
|
|
|
bool preferred = false;
|
|
|
|
if ((m_autoRemote || (preferred = (addr == m_remotePref))) && (addr != m_remoteAddr)) {
|
|
|
|
Debug(DebugInfo,"Auto changing RTP address from %s:%d to%s %s:%d",
|
2005-12-31 00:36:48 +00:00
|
|
|
m_remoteAddr.host().c_str(),m_remoteAddr.port(),
|
2010-09-15 14:23:20 +00:00
|
|
|
(preferred ? " preferred" : ""),
|
2005-12-31 00:36:48 +00:00
|
|
|
addr.host().c_str(),addr.port());
|
2010-09-15 14:23:20 +00:00
|
|
|
// if we received from the preferred address don't auto change any more
|
|
|
|
if (preferred)
|
|
|
|
m_remotePref.clear();
|
2005-12-31 00:36:48 +00:00
|
|
|
remoteAddr(addr);
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
2005-12-31 00:36:48 +00:00
|
|
|
m_autoRemote = false;
|
|
|
|
if (addr == m_remoteAddr) {
|
2005-05-03 18:11:57 +00:00
|
|
|
if (m_processor)
|
2005-12-31 00:36:48 +00:00
|
|
|
m_processor->rtpData(buf,len);
|
2005-05-03 18:11:57 +00:00
|
|
|
if (m_monitor)
|
2005-12-31 00:36:48 +00:00
|
|
|
m_monitor->rtpData(buf,len);
|
2005-05-03 18:11:57 +00:00
|
|
|
}
|
2005-05-02 18:31:05 +00:00
|
|
|
}
|
2006-10-03 14:24:20 +00:00
|
|
|
m_rtpSock.timerTick(when);
|
2005-05-02 18:31:05 +00:00
|
|
|
}
|
2005-12-31 00:36:48 +00:00
|
|
|
if (m_rtcpSock.valid()) {
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
SocketAddr addr;
|
2005-12-31 00:46:45 +00:00
|
|
|
int len;
|
|
|
|
while (((len = m_rtcpSock.recvFrom(buf,sizeof(buf),addr)) >= 8) && (addr == m_remoteRTCP)) {
|
2005-12-31 00:36:48 +00:00
|
|
|
if (m_processor)
|
|
|
|
m_processor->rtcpData(buf,len);
|
|
|
|
if (m_monitor)
|
|
|
|
m_monitor->rtcpData(buf,len);
|
|
|
|
}
|
2006-10-03 14:24:20 +00:00
|
|
|
m_rtcpSock.timerTick(when);
|
2005-12-31 00:36:48 +00:00
|
|
|
}
|
2005-05-02 18:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RTPTransport::rtpData(const void* data, int len)
|
|
|
|
{
|
2009-11-03 10:49:12 +00:00
|
|
|
if (!data)
|
2005-05-03 18:11:57 +00:00
|
|
|
return;
|
2009-11-03 10:49:12 +00:00
|
|
|
switch (m_type) {
|
|
|
|
case RTP:
|
|
|
|
if (len < 12)
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
case UDPTL:
|
|
|
|
if (len < 6)
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-05-02 18:31:05 +00:00
|
|
|
if (m_rtpSock.valid() && m_remoteAddr.valid())
|
|
|
|
m_rtpSock.sendTo(data,len,m_remoteAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RTPTransport::rtcpData(const void* data, int len)
|
|
|
|
{
|
2005-05-03 18:11:57 +00:00
|
|
|
if ((len < 8) || !data)
|
|
|
|
return;
|
2005-05-02 18:31:05 +00:00
|
|
|
if (m_rtcpSock.valid() && m_remoteRTCP.valid())
|
|
|
|
m_rtcpSock.sendTo(data,len,m_remoteRTCP);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RTPTransport::setProcessor(RTPProcessor* processor)
|
|
|
|
{
|
2005-05-03 18:11:57 +00:00
|
|
|
if (processor) {
|
|
|
|
// both should run in the same RTP group
|
|
|
|
if (group())
|
|
|
|
processor->group(group());
|
|
|
|
else
|
|
|
|
group(processor->group());
|
|
|
|
}
|
2005-05-02 18:31:05 +00:00
|
|
|
m_processor = processor;
|
|
|
|
}
|
|
|
|
|
2005-05-03 18:11:57 +00:00
|
|
|
void RTPTransport::setMonitor(RTPProcessor* monitor)
|
|
|
|
{
|
|
|
|
m_monitor = monitor;
|
|
|
|
}
|
|
|
|
|
2006-09-13 18:50:04 +00:00
|
|
|
bool RTPTransport::localAddr(SocketAddr& addr, bool rtcp)
|
2005-05-02 18:31:05 +00:00
|
|
|
{
|
2005-05-09 22:55:47 +00:00
|
|
|
// check if sockets are already created and bound
|
|
|
|
if (m_rtpSock.valid())
|
|
|
|
return false;
|
2005-05-02 18:31:05 +00:00
|
|
|
int p = addr.port();
|
2006-09-13 18:50:04 +00:00
|
|
|
// for RTCP make sure we don't have a port or it's an even one
|
|
|
|
if (rtcp && (p & 1))
|
2005-05-09 22:55:47 +00:00
|
|
|
return false;
|
|
|
|
if (m_rtpSock.create(addr.family(),SOCK_DGRAM) && m_rtpSock.bind(addr)) {
|
2005-12-31 00:36:48 +00:00
|
|
|
m_rtpSock.setBlocking(false);
|
2006-09-13 18:50:04 +00:00
|
|
|
if (!rtcp) {
|
|
|
|
// RTCP not requested - we are done
|
|
|
|
m_rtpSock.getSockName(addr);
|
|
|
|
m_localAddr = addr;
|
|
|
|
return true;
|
|
|
|
}
|
2005-05-09 22:55:47 +00:00
|
|
|
if (!p) {
|
|
|
|
m_rtpSock.getSockName(addr);
|
|
|
|
p = addr.port();
|
|
|
|
if (p & 1) {
|
|
|
|
// allocated odd port - have to swap sockets
|
|
|
|
m_rtcpSock.attach(m_rtpSock.detach());
|
|
|
|
addr.port(p-1);
|
|
|
|
if (m_rtpSock.create(addr.family(),SOCK_DGRAM) && m_rtpSock.bind(addr)) {
|
2005-12-31 00:36:48 +00:00
|
|
|
m_rtpSock.setBlocking(false);
|
2005-05-09 22:55:47 +00:00
|
|
|
m_localAddr = addr;
|
|
|
|
return true;
|
|
|
|
}
|
2006-01-13 05:09:50 +00:00
|
|
|
DDebug(DebugMild,"RTP Socket failed with code %d",m_rtpSock.error());
|
2005-05-09 22:55:47 +00:00
|
|
|
m_rtpSock.terminate();
|
|
|
|
m_rtcpSock.terminate();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addr.port(p+1);
|
|
|
|
if (m_rtcpSock.create(addr.family(),SOCK_DGRAM) && m_rtcpSock.bind(addr)) {
|
2005-12-31 00:36:48 +00:00
|
|
|
m_rtcpSock.setBlocking(false);
|
2005-05-09 22:55:47 +00:00
|
|
|
addr.port(p);
|
|
|
|
m_localAddr = addr;
|
|
|
|
return true;
|
|
|
|
}
|
2009-06-19 11:19:20 +00:00
|
|
|
#ifdef DEBUG
|
2006-01-13 05:09:50 +00:00
|
|
|
else
|
2009-06-19 11:19:20 +00:00
|
|
|
Debug(DebugMild,"RTCP Socket failed with code %d",m_rtcpSock.error());
|
|
|
|
#endif
|
2005-05-02 18:31:05 +00:00
|
|
|
}
|
2009-06-19 11:19:20 +00:00
|
|
|
#ifdef DEBUG
|
2006-01-13 05:09:50 +00:00
|
|
|
else
|
2009-06-19 11:19:20 +00:00
|
|
|
Debug(DebugMild,"RTP Socket failed with code %d",m_rtpSock.error());
|
|
|
|
#endif
|
2005-05-09 22:55:47 +00:00
|
|
|
m_rtpSock.terminate();
|
|
|
|
m_rtcpSock.terminate();
|
2005-05-02 18:31:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-28 03:06:16 +00:00
|
|
|
bool RTPTransport::remoteAddr(SocketAddr& addr, bool sniff)
|
2005-05-02 18:31:05 +00:00
|
|
|
{
|
2008-05-26 12:25:37 +00:00
|
|
|
Lock lock(group());
|
2005-10-28 03:06:16 +00:00
|
|
|
m_autoRemote = sniff;
|
2005-05-02 18:31:05 +00:00
|
|
|
int p = addr.port();
|
2006-02-01 14:37:01 +00:00
|
|
|
// make sure we have a valid address and a port
|
|
|
|
// we do not check that it's even numbered as many NAPTs will break that
|
|
|
|
if (p && addr.valid()) {
|
2005-05-02 18:31:05 +00:00
|
|
|
m_remoteAddr = addr;
|
|
|
|
m_remoteRTCP = addr;
|
|
|
|
m_remoteRTCP.port(addr.port()+1);
|
2010-09-15 14:23:20 +00:00
|
|
|
// if sniffing packets from other sources remember preferred address
|
|
|
|
if (sniff)
|
|
|
|
m_remotePref = addr;
|
2005-05-02 18:31:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-01 23:33:30 +00:00
|
|
|
bool RTPTransport::drillHole()
|
|
|
|
{
|
|
|
|
if (m_rtpSock.valid() && m_remoteAddr.valid()) {
|
|
|
|
static const char buf[4] = { 0, 0, 0, 0 };
|
|
|
|
if (m_rtpSock.sendTo(buf,sizeof(buf),m_remoteAddr) == sizeof(buf)) {
|
|
|
|
if (m_rtcpSock.valid() && m_remoteRTCP.valid())
|
|
|
|
m_rtcpSock.sendTo(buf,sizeof(buf),m_remoteRTCP);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-02 18:31:05 +00:00
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|