2021-01-27 23:08:42 +08:00
|
|
|
|
2021-01-27 23:50:07 +08:00
|
|
|
#include "zm.h"
|
|
|
|
|
|
|
|
#if HAVE_RTSP_SERVER
|
2020-12-10 01:19:35 +08:00
|
|
|
#include "zm_rtsp_server_thread.h"
|
2021-01-07 00:24:52 +08:00
|
|
|
#include "zm_rtsp_server_device_source.h"
|
|
|
|
#include "zm_rtsp_server_h264_device_source.h"
|
2021-01-23 09:23:30 +08:00
|
|
|
#include "zm_rtsp_server_adts_source.h"
|
2021-01-07 00:24:52 +08:00
|
|
|
#include "zm_rtsp_server_unicast_server_media_subsession.h"
|
|
|
|
#include <StreamReplicator.hh>
|
|
|
|
|
2020-12-10 01:19:35 +08:00
|
|
|
RTSPServerThread::RTSPServerThread(Monitor *p_monitor) :
|
|
|
|
monitor(p_monitor),
|
2021-01-07 00:24:52 +08:00
|
|
|
terminate(0)
|
|
|
|
{
|
|
|
|
//unsigned short rtsp_over_http_port = 0;
|
|
|
|
//const char *realm = "ZoneMinder";
|
|
|
|
//unsigned int timeout = 65;
|
2021-01-21 23:26:46 +08:00
|
|
|
OutPacketBuffer::maxSize = 2000000;
|
2020-12-10 01:19:35 +08:00
|
|
|
|
2021-01-07 00:24:52 +08:00
|
|
|
scheduler = BasicTaskScheduler::createNew();
|
|
|
|
env = BasicUsageEnvironment::createNew(*scheduler);
|
|
|
|
authDB = nullptr;
|
|
|
|
//authDB = new UserAuthenticationDatabase("ZoneMinder");
|
|
|
|
//authDB->addUserRecord("username1", "password1"); // replace these with real strings
|
2020-12-10 01:19:35 +08:00
|
|
|
|
2021-01-27 03:14:29 +08:00
|
|
|
portNumBits rtspServerPortNum = config.min_rtsp_port + monitor->Id();
|
2021-01-07 00:24:52 +08:00
|
|
|
rtspServer = RTSPServer::createNew(*env, rtspServerPortNum, authDB);
|
|
|
|
|
|
|
|
if ( rtspServer == nullptr ) {
|
2021-01-21 23:26:46 +08:00
|
|
|
Fatal("Failed to create rtspServer at port %d", rtspServerPortNum);
|
2021-01-07 00:24:52 +08:00
|
|
|
return;
|
2020-12-10 01:19:35 +08:00
|
|
|
}
|
2021-01-20 02:43:25 +08:00
|
|
|
const char *prefix = rtspServer->rtspURLPrefix();
|
|
|
|
Debug(1, "RTSP prefix is %s", prefix);
|
|
|
|
delete[] prefix;
|
2021-01-07 00:24:52 +08:00
|
|
|
} // end RTSPServerThread::RTSPServerThread
|
2020-12-10 01:19:35 +08:00
|
|
|
|
|
|
|
RTSPServerThread::~RTSPServerThread() {
|
2021-01-07 00:24:52 +08:00
|
|
|
if ( rtspServer ) {
|
|
|
|
Medium::close(rtspServer);
|
|
|
|
} // end if rtsp_server
|
2021-01-21 23:26:46 +08:00
|
|
|
while ( sources.size() ) {
|
|
|
|
FramedSource *source = sources.front();
|
|
|
|
sources.pop_front();
|
|
|
|
Medium::close(source);
|
|
|
|
}
|
2021-01-07 00:24:52 +08:00
|
|
|
env->reclaim();
|
|
|
|
delete scheduler;
|
2020-12-10 01:19:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int RTSPServerThread::run() {
|
2021-01-07 00:24:52 +08:00
|
|
|
Debug(1, "RTSPServerThread::run()");
|
2020-12-10 01:19:35 +08:00
|
|
|
if ( rtspServer )
|
2021-01-07 00:24:52 +08:00
|
|
|
env->taskScheduler().doEventLoop(&terminate); // does not return
|
|
|
|
Debug(1, "RTSPServerThread::done()");
|
2020-12-10 01:19:35 +08:00
|
|
|
return 0;
|
2021-01-07 00:24:52 +08:00
|
|
|
} // end in RTSPServerThread::run()
|
|
|
|
|
|
|
|
void RTSPServerThread::stop() {
|
|
|
|
Debug(1, "RTSPServerThread::stop()");
|
|
|
|
terminate = 1;
|
2021-01-23 09:23:30 +08:00
|
|
|
for ( std::list<FramedSource *>::iterator it = sources.begin(); it != sources.end(); ++it ) {
|
|
|
|
(*it)->stopGettingFrames();
|
|
|
|
}
|
2021-01-07 00:24:52 +08:00
|
|
|
} // end RTSPServerThread::stop()
|
|
|
|
|
|
|
|
bool RTSPServerThread::stopped() const {
|
|
|
|
return terminate ? true : false;
|
|
|
|
} // end RTSPServerThread::stopped()
|
|
|
|
|
2021-01-28 01:50:40 +08:00
|
|
|
void RTSPServerThread::addStream(AVStream *video_stream, AVStream *audio_stream) {
|
2021-01-07 00:24:52 +08:00
|
|
|
if ( !rtspServer )
|
|
|
|
return;
|
|
|
|
|
2021-01-21 23:26:46 +08:00
|
|
|
int queueSize = 30;
|
2021-01-20 02:43:25 +08:00
|
|
|
bool repeatConfig = true;
|
|
|
|
bool muxTS = false;
|
2021-01-21 23:26:46 +08:00
|
|
|
ServerMediaSession *sms = nullptr;
|
|
|
|
|
2021-01-28 01:50:40 +08:00
|
|
|
if ( video_stream ) {
|
2021-01-21 23:26:46 +08:00
|
|
|
StreamReplicator* videoReplicator = nullptr;
|
|
|
|
FramedSource *source = nullptr;
|
2021-01-28 01:50:40 +08:00
|
|
|
std::string rtpFormat = getRtpFormat(video_stream->codecpar->codec_id, false);
|
|
|
|
if ( rtpFormat.empty() ) {
|
|
|
|
Error("No streaming format");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Debug(1, "RTSP: format %s", rtpFormat.c_str());
|
2021-01-21 23:26:46 +08:00
|
|
|
if ( rtpFormat == "video/H264" ) {
|
2021-01-28 01:50:40 +08:00
|
|
|
source = H264_ZoneMinderDeviceSource::createNew(*env, monitor, video_stream, queueSize, repeatConfig, muxTS);
|
2021-01-21 23:26:46 +08:00
|
|
|
} else if ( rtpFormat == "video/H265" ) {
|
2021-01-28 01:50:40 +08:00
|
|
|
source = H265_ZoneMinderDeviceSource::createNew(*env, monitor, video_stream, queueSize, repeatConfig, muxTS);
|
2021-01-20 02:43:25 +08:00
|
|
|
}
|
2021-01-21 23:26:46 +08:00
|
|
|
if ( source == nullptr ) {
|
|
|
|
Error("Unable to create source");
|
|
|
|
} else {
|
|
|
|
videoReplicator = StreamReplicator::createNew(*env, source, false);
|
|
|
|
}
|
|
|
|
sources.push_back(source);
|
|
|
|
|
|
|
|
// Create Unicast Session
|
|
|
|
if ( videoReplicator ) {
|
|
|
|
if ( !sms )
|
|
|
|
sms = ServerMediaSession::createNew(*env, "streamname");
|
|
|
|
sms->addSubsession(UnicastServerMediaSubsession::createNew(*env, videoReplicator, rtpFormat));
|
2021-01-07 00:24:52 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-21 23:26:46 +08:00
|
|
|
if ( audio_stream ) {
|
|
|
|
StreamReplicator* replicator = nullptr;
|
|
|
|
FramedSource *source = nullptr;
|
2021-01-28 01:50:40 +08:00
|
|
|
std::string rtpFormat = getRtpFormat(audio_stream->codecpar->codec_id, false);
|
2021-01-21 23:26:46 +08:00
|
|
|
if ( rtpFormat == "audio/AAC" ) {
|
2021-01-23 09:23:30 +08:00
|
|
|
source = ADTS_ZoneMinderDeviceSource::createNew(*env, monitor, audio_stream, queueSize);
|
|
|
|
Debug(1, "ADTS source %p", source);
|
2021-01-21 23:26:46 +08:00
|
|
|
}
|
|
|
|
if ( source ) {
|
2021-01-23 09:23:30 +08:00
|
|
|
replicator = StreamReplicator::createNew(*env, source, false /* deleteWhenLastReplicaDies */);
|
2021-01-21 23:26:46 +08:00
|
|
|
sources.push_back(source);
|
|
|
|
}
|
|
|
|
if ( replicator ) {
|
|
|
|
if ( !sms )
|
|
|
|
sms = ServerMediaSession::createNew(*env, "streamname");
|
|
|
|
sms->addSubsession(UnicastServerMediaSubsession::createNew(*env, replicator, rtpFormat));
|
|
|
|
}
|
2021-01-23 09:23:30 +08:00
|
|
|
} else {
|
2021-01-28 01:50:40 +08:00
|
|
|
Debug(1, "Not Adding audio stream");
|
|
|
|
}
|
|
|
|
if ( sms ) {
|
|
|
|
rtspServer->addServerMediaSession(sms);
|
|
|
|
char *url = rtspServer->rtspURL(sms);
|
|
|
|
Debug(1, "url is %s", url);
|
|
|
|
delete[] url;
|
2021-01-07 00:24:52 +08:00
|
|
|
}
|
|
|
|
} // end void addStream
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
// convert V4L2 pix format to RTP mime
|
|
|
|
// -----------------------------------------
|
2021-01-20 02:43:25 +08:00
|
|
|
const std::string RTSPServerThread::getRtpFormat(AVCodecID codec_id, bool muxTS) {
|
|
|
|
if ( muxTS ) {
|
|
|
|
return "video/MP2T";
|
2021-01-07 00:24:52 +08:00
|
|
|
} else {
|
2021-01-07 22:46:06 +08:00
|
|
|
switch ( codec_id ) {
|
2021-01-20 02:43:25 +08:00
|
|
|
case AV_CODEC_ID_H265 : return "video/H265";
|
|
|
|
case AV_CODEC_ID_H264 : return "video/H264";
|
2021-01-07 00:24:52 +08:00
|
|
|
//case PIX_FMT_MJPEG: rtpFormat = "video/JPEG"; break;
|
|
|
|
//case PIX_FMT_JPEG : rtpFormat = "video/JPEG"; break;
|
2021-01-07 22:46:06 +08:00
|
|
|
//case AV_PIX_FMT_VP8 : rtpFormat = "video/VP8" ; break;
|
|
|
|
//case AV_PIX_FMT_VP9 : rtpFormat = "video/VP9" ; break;
|
2021-01-21 23:26:46 +08:00
|
|
|
case AV_CODEC_ID_AAC : return "audio/AAC";
|
2021-01-07 22:46:06 +08:00
|
|
|
default: break;
|
2021-01-07 00:24:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 02:43:25 +08:00
|
|
|
return "";
|
2021-01-07 00:24:52 +08:00
|
|
|
}
|
2020-12-10 01:19:35 +08:00
|
|
|
#endif
|