2008-07-16 16:35:59 +08:00
|
|
|
//
|
2008-07-25 17:08:15 +08:00
|
|
|
// ZoneMinder RTP Data Class Implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2008-07-16 16:35:59 +08: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
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-07-16 16:35:59 +08:00
|
|
|
//
|
|
|
|
|
2008-10-17 00:11:49 +08:00
|
|
|
#include "zm.h"
|
|
|
|
|
|
|
|
#if HAVE_LIBAVFORMAT
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm_rtp_data.h"
|
|
|
|
|
|
|
|
#include "zm_rtsp.h"
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2020-05-08 01:41:09 +08:00
|
|
|
RtpDataThread::RtpDataThread(RtspThread &rtspThread, RtpSource &rtpSource) :
|
|
|
|
mRtspThread(rtspThread), mRtpSource(rtpSource), mStop(false)
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-08 01:41:09 +08:00
|
|
|
bool RtpDataThread::recvPacket(const unsigned char *packet, size_t packetLen) {
|
2016-04-04 22:11:48 +08:00
|
|
|
const RtpDataHeader *rtpHeader;
|
|
|
|
rtpHeader = (RtpDataHeader *)packet;
|
|
|
|
|
2020-05-08 01:41:09 +08:00
|
|
|
Debug(5, "Ver: %d P: %d Pt: %d Mk: %d Seq: %d T/S: %x SSRC: %x",
|
|
|
|
rtpHeader->version,
|
|
|
|
rtpHeader->p,
|
|
|
|
rtpHeader->pt,
|
|
|
|
rtpHeader->m,
|
|
|
|
ntohs(rtpHeader->seqN),
|
|
|
|
ntohl(rtpHeader->timestampN),
|
|
|
|
ntohl(rtpHeader->ssrcN));
|
2016-04-04 22:11:48 +08:00
|
|
|
|
|
|
|
//unsigned short seq = ntohs(rtpHeader->seqN);
|
|
|
|
unsigned long ssrc = ntohl(rtpHeader->ssrcN);
|
|
|
|
|
2020-05-08 01:41:09 +08:00
|
|
|
if ( mRtpSource.getSsrc() && (ssrc != mRtpSource.getSsrc()) ) {
|
|
|
|
Warning("Discarding packet for unrecognised ssrc %lx", ssrc);
|
|
|
|
return false;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
|
2020-05-08 01:41:09 +08:00
|
|
|
return mRtpSource.handlePacket(packet, packetLen);
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
2020-05-08 01:41:09 +08:00
|
|
|
int RtpDataThread::run() {
|
|
|
|
Debug(2, "Starting data thread %d on port %d",
|
|
|
|
mRtpSource.getSsrc(), mRtpSource.getLocalDataPort());
|
2016-04-04 22:11:48 +08:00
|
|
|
|
|
|
|
SockAddrInet localAddr;
|
|
|
|
UdpInetServer rtpDataSocket;
|
2016-05-06 20:32:21 +08:00
|
|
|
if ( mRtpSource.getLocalHost() != "" ) {
|
2020-05-08 01:41:09 +08:00
|
|
|
if ( !rtpDataSocket.bind(mRtpSource.getLocalHost().c_str(), mRtpSource.getLocalDataPort()) )
|
|
|
|
Fatal("Failed to bind RTP server");
|
|
|
|
} else {
|
|
|
|
if ( !rtpDataSocket.bind(
|
|
|
|
mRtspThread.getAddressFamily() == AF_INET6 ? "::" : "0.0.0.0",
|
|
|
|
mRtpSource.getLocalDataPort() ) )
|
|
|
|
Fatal("Failed to bind RTP server");
|
2016-05-06 20:32:21 +08:00
|
|
|
}
|
2020-05-08 01:41:09 +08:00
|
|
|
Debug(3, "Bound to %s:%d", mRtpSource.getLocalHost().c_str(), mRtpSource.getLocalDataPort());
|
2016-04-04 22:11:48 +08:00
|
|
|
|
2020-05-08 01:41:09 +08:00
|
|
|
Select select(3);
|
|
|
|
select.addReader(&rtpDataSocket);
|
2016-04-04 22:11:48 +08:00
|
|
|
|
|
|
|
unsigned char buffer[ZM_NETWORK_BUFSIZ];
|
2020-05-08 01:41:09 +08:00
|
|
|
while ( !zm_terminate && !mStop && (select.wait() >= 0) ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
Select::CommsList readable = select.getReadable();
|
2020-05-08 01:41:09 +08:00
|
|
|
if ( readable.size() == 0 ) {
|
|
|
|
Error("RTP timed out");
|
2016-04-04 22:11:48 +08:00
|
|
|
mStop = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-05-08 01:41:09 +08:00
|
|
|
for ( Select::CommsList::iterator iter = readable.begin(); iter != readable.end(); ++iter ) {
|
|
|
|
if ( UdpInetServer *socket = dynamic_cast<UdpInetServer *>(*iter) ) {
|
|
|
|
int nBytes = socket->recv(buffer, sizeof(buffer));
|
|
|
|
Debug(4, "Got %d bytes on sd %d", nBytes, socket->getReadDesc());
|
|
|
|
if ( nBytes ) {
|
|
|
|
recvPacket(buffer, nBytes);
|
|
|
|
} else {
|
2016-04-04 22:11:48 +08:00
|
|
|
mStop = true;
|
|
|
|
break;
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
2020-05-08 01:41:09 +08:00
|
|
|
} else {
|
|
|
|
Panic("Barfed");
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2020-05-08 01:41:09 +08:00
|
|
|
} // end foreach commsList
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
rtpDataSocket.close();
|
|
|
|
mRtspThread.stop();
|
2020-05-08 01:41:09 +08:00
|
|
|
return 0;
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
2008-10-17 00:11:49 +08:00
|
|
|
|
|
|
|
#endif // HAVE_LIBAVFORMAT
|