2021-01-07 00:26:08 +08:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
** This software is in the public domain, furnished "as is", without technical
|
|
|
|
** support, and with no warranty, express or implied, as to its usefulness for
|
|
|
|
** any purpose.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** ZoneMinder Live555 source
|
|
|
|
**
|
|
|
|
** -------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "zm_rtsp_server_device_source.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
|
|
|
|
#include "zm_config.h"
|
2021-01-07 00:26:08 +08:00
|
|
|
#include "zm_logger.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_rtsp_server_frame.h"
|
|
|
|
#include "zm_signal.h"
|
|
|
|
|
2021-01-27 23:08:42 +08:00
|
|
|
#if HAVE_RTSP_SERVER
|
2021-01-07 00:26:08 +08:00
|
|
|
ZoneMinderDeviceSource::ZoneMinderDeviceSource(
|
|
|
|
UsageEnvironment& env,
|
2021-02-08 02:12:39 +08:00
|
|
|
std::shared_ptr<Monitor> monitor,
|
2021-01-21 23:23:30 +08:00
|
|
|
AVStream *stream,
|
|
|
|
unsigned int queueSize
|
|
|
|
) :
|
2021-01-07 00:26:08 +08:00
|
|
|
FramedSource(env),
|
2021-01-21 23:23:30 +08:00
|
|
|
m_stream(stream),
|
2021-02-08 02:12:39 +08:00
|
|
|
m_monitor(std::move(monitor)),
|
2021-01-07 00:26:08 +08:00
|
|
|
m_packetqueue(nullptr),
|
|
|
|
m_packetqueue_it(nullptr),
|
|
|
|
m_queueSize(queueSize)
|
|
|
|
{
|
|
|
|
m_eventTriggerId = envir().taskScheduler().createEventTrigger(ZoneMinderDeviceSource::deliverFrameStub);
|
|
|
|
memset(&m_thid, 0, sizeof(m_thid));
|
|
|
|
memset(&m_mutex, 0, sizeof(m_mutex));
|
|
|
|
if ( m_monitor ) {
|
|
|
|
m_packetqueue = m_monitor->GetPacketQueue();
|
|
|
|
if ( !m_packetqueue ) {
|
|
|
|
Fatal("No packetqueue");
|
|
|
|
}
|
2021-01-21 23:23:30 +08:00
|
|
|
pthread_mutex_init(&m_mutex, nullptr);
|
|
|
|
pthread_create(&m_thid, nullptr, threadStub, this);
|
2021-01-07 00:26:08 +08:00
|
|
|
} else {
|
|
|
|
Error("No monitor in ZoneMinderDeviceSource");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ZoneMinderDeviceSource::~ZoneMinderDeviceSource() {
|
2021-01-21 23:23:30 +08:00
|
|
|
stop = 1;
|
2021-01-07 00:26:08 +08:00
|
|
|
envir().taskScheduler().deleteEventTrigger(m_eventTriggerId);
|
|
|
|
pthread_join(m_thid, nullptr);
|
2021-02-01 00:43:03 +08:00
|
|
|
while ( m_captureQueue.size() ) {
|
|
|
|
NAL_Frame * f = m_captureQueue.front();
|
|
|
|
m_captureQueue.pop_front();
|
|
|
|
delete f;
|
|
|
|
}
|
|
|
|
|
2021-01-07 00:26:08 +08:00
|
|
|
pthread_mutex_destroy(&m_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// thread mainloop
|
|
|
|
void* ZoneMinderDeviceSource::thread() {
|
2021-01-21 23:23:30 +08:00
|
|
|
stop = 0;
|
2021-01-07 00:26:08 +08:00
|
|
|
|
|
|
|
while ( !stop ) {
|
|
|
|
getNextFrame();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// getting FrameSource callback
|
|
|
|
void ZoneMinderDeviceSource::doGetNextFrame() {
|
|
|
|
deliverFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
// stopping FrameSource callback
|
|
|
|
void ZoneMinderDeviceSource::doStopGettingFrames() {
|
2021-02-01 10:31:00 +08:00
|
|
|
stop = 1;
|
2021-01-07 00:26:08 +08:00
|
|
|
Debug(1, "ZoneMinderDeviceSource::doStopGettingFrames");
|
|
|
|
FramedSource::doStopGettingFrames();
|
|
|
|
}
|
|
|
|
|
|
|
|
// deliver frame to the sink
|
|
|
|
void ZoneMinderDeviceSource::deliverFrame() {
|
2021-01-20 02:43:25 +08:00
|
|
|
if ( !isCurrentlyAwaitingData() ) {
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(4, "not awaiting data");
|
2021-01-20 02:43:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&m_mutex);
|
|
|
|
if ( m_captureQueue.empty() ) {
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(4, "Queue is empty");
|
2021-01-20 02:43:25 +08:00
|
|
|
pthread_mutex_unlock(&m_mutex);
|
|
|
|
return;
|
2021-01-21 23:23:30 +08:00
|
|
|
}
|
2021-01-20 02:43:25 +08:00
|
|
|
|
|
|
|
NAL_Frame *frame = m_captureQueue.front();
|
|
|
|
m_captureQueue.pop_front();
|
2021-01-21 23:23:30 +08:00
|
|
|
pthread_mutex_unlock(&m_mutex);
|
|
|
|
|
|
|
|
fDurationInMicroseconds = 0;
|
|
|
|
fFrameSize = 0;
|
2021-01-20 02:43:25 +08:00
|
|
|
|
|
|
|
unsigned int nal_size = frame->size();
|
|
|
|
|
|
|
|
if ( nal_size > fMaxSize ) {
|
|
|
|
fFrameSize = fMaxSize;
|
|
|
|
fNumTruncatedBytes = nal_size - fMaxSize;
|
|
|
|
} else {
|
|
|
|
fFrameSize = nal_size;
|
|
|
|
}
|
2021-01-27 03:14:05 +08:00
|
|
|
Debug(2, "deliverFrame stream: %d timestamp: %ld.%06ld size: %d queuesize: %d",
|
2021-01-26 02:23:57 +08:00
|
|
|
m_stream->index,
|
2021-01-20 02:43:25 +08:00
|
|
|
frame->m_timestamp.tv_sec, frame->m_timestamp.tv_usec,
|
|
|
|
fFrameSize,
|
|
|
|
m_captureQueue.size()
|
|
|
|
);
|
|
|
|
|
|
|
|
fPresentationTime = frame->m_timestamp;
|
|
|
|
memcpy(fTo, frame->buffer(), fFrameSize);
|
|
|
|
|
|
|
|
if ( fFrameSize > 0 ) {
|
|
|
|
// send Frame to the consumer
|
|
|
|
FramedSource::afterGetting(this);
|
|
|
|
}
|
2021-01-21 23:23:30 +08:00
|
|
|
delete frame;
|
2021-01-20 02:43:25 +08:00
|
|
|
} // end void ZoneMinderDeviceSource::deliverFrame()
|
2021-01-07 00:26:08 +08:00
|
|
|
|
|
|
|
// FrameSource callback on read event
|
|
|
|
void ZoneMinderDeviceSource::incomingPacketHandler() {
|
|
|
|
if ( this->getNextFrame() <= 0 ) {
|
|
|
|
handleClosure(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read from monitor
|
|
|
|
int ZoneMinderDeviceSource::getNextFrame() {
|
2021-01-07 22:46:06 +08:00
|
|
|
if ( zm_terminate )
|
|
|
|
return -1;
|
|
|
|
|
2021-01-07 00:26:08 +08:00
|
|
|
if ( !m_packetqueue_it ) {
|
|
|
|
m_packetqueue_it = m_packetqueue->get_video_it(true);
|
|
|
|
}
|
|
|
|
ZMPacket *zm_packet = m_packetqueue->get_packet(m_packetqueue_it);
|
2021-01-21 23:23:30 +08:00
|
|
|
while ( zm_packet and (zm_packet->packet.stream_index != m_stream->index) ) {
|
2021-01-07 22:46:06 +08:00
|
|
|
zm_packet->unlock();
|
|
|
|
// We want our stream to start at the same it as the video
|
|
|
|
// but if this is an audio stream we need to increment past that first packet
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(4, "Have audio packet, skipping");
|
|
|
|
m_packetqueue->increment_it(m_packetqueue_it, m_stream->index);
|
2021-01-07 22:46:06 +08:00
|
|
|
zm_packet = m_packetqueue->get_packet(m_packetqueue_it);
|
|
|
|
}
|
2021-01-07 00:26:08 +08:00
|
|
|
if ( !zm_packet ) {
|
|
|
|
Debug(1, "null zm_packet %p", zm_packet);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// packet is locked
|
2021-01-21 23:23:30 +08:00
|
|
|
AVPacket *pkt = &zm_packet->packet;
|
2021-01-07 00:26:08 +08:00
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
// Convert pts to timeval
|
2021-01-27 03:14:05 +08:00
|
|
|
int64_t pts = av_rescale_q(pkt->dts, m_stream->time_base, AV_TIME_BASE_Q);
|
2021-01-26 02:23:57 +08:00
|
|
|
timeval tv = { pts/1000000, pts%1000000 };
|
2021-02-06 21:46:28 +08:00
|
|
|
ZM_DUMP_STREAM_PACKET(m_stream, (*pkt), "rtspServer");
|
2021-01-21 23:23:30 +08:00
|
|
|
Debug(2, "pts %" PRId64 " pkt.pts %" PRId64 " tv %d.%d", pts, pkt->pts, tv.tv_sec, tv.tv_usec);
|
2021-01-07 00:26:08 +08:00
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
std::list< std::pair<unsigned char*, size_t> > framesList = this->splitFrames(pkt->data, pkt->size);
|
2021-01-07 00:26:08 +08:00
|
|
|
zm_packet->unlock();
|
2021-01-07 22:46:06 +08:00
|
|
|
zm_packet = nullptr;// we no longer have the lock so shouldn't be accessing it
|
2021-02-03 03:23:08 +08:00
|
|
|
m_packetqueue->increment_it(m_packetqueue_it, m_stream->index);
|
2021-01-07 00:26:08 +08:00
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
while ( framesList.size() ) {
|
|
|
|
std::pair<unsigned char*, size_t> nal = framesList.front();
|
|
|
|
framesList.pop_front();
|
2021-01-07 00:26:08 +08:00
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
NAL_Frame *frame = new NAL_Frame(nal.first, nal.second, tv);
|
2021-01-07 00:26:08 +08:00
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
pthread_mutex_lock(&m_mutex);
|
2021-01-27 03:14:05 +08:00
|
|
|
if ( m_captureQueue.size() ) {
|
|
|
|
NAL_Frame * f = m_captureQueue.front();
|
2021-01-27 07:29:02 +08:00
|
|
|
while ( m_captureQueue.size() and ((f->m_timestamp.tv_sec - tv.tv_sec) > 10) ) {
|
2021-01-27 03:14:05 +08:00
|
|
|
m_captureQueue.pop_front();
|
|
|
|
delete f;
|
|
|
|
f = m_captureQueue.front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if 0
|
2021-01-21 23:23:30 +08:00
|
|
|
while ( m_captureQueue.size() >= m_queueSize ) {
|
|
|
|
Debug(2, "Queue full dropping frame %d", m_captureQueue.size());
|
|
|
|
NAL_Frame * f = m_captureQueue.front();
|
|
|
|
m_captureQueue.pop_front();
|
|
|
|
delete f;
|
|
|
|
}
|
2021-01-27 03:14:05 +08:00
|
|
|
#endif
|
2021-01-21 23:23:30 +08:00
|
|
|
m_captureQueue.push_back(frame);
|
|
|
|
pthread_mutex_unlock(&m_mutex);
|
2021-01-07 00:26:08 +08:00
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
// post an event to ask to deliver the frame
|
|
|
|
envir().taskScheduler().triggerEvent(m_eventTriggerId, this);
|
|
|
|
} // end while we get frame from data
|
2021-01-07 00:26:08 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// split packet in frames
|
|
|
|
std::list< std::pair<unsigned char*,size_t> > ZoneMinderDeviceSource::splitFrames(unsigned char* frame, unsigned frameSize) {
|
|
|
|
std::list< std::pair<unsigned char*,size_t> > frameList;
|
|
|
|
if ( frame != nullptr ) {
|
|
|
|
frameList.push_back(std::pair<unsigned char*,size_t>(frame, frameSize));
|
|
|
|
}
|
|
|
|
return frameList;
|
|
|
|
}
|
2021-01-23 08:25:38 +08:00
|
|
|
|
|
|
|
// extract a frame
|
|
|
|
unsigned char* ZoneMinderDeviceSource::extractFrame(unsigned char* frame, size_t& size, size_t& outsize) {
|
|
|
|
outsize = size;
|
|
|
|
size = 0;
|
|
|
|
return frame;
|
|
|
|
}
|
2021-02-04 11:59:03 +08:00
|
|
|
#endif // HAVE_RTSP_SERVER
|