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.
|
|
|
|
**
|
|
|
|
** v4l2DeviceSource.cpp
|
|
|
|
**
|
|
|
|
** ZoneMinder Live555 source
|
|
|
|
**
|
|
|
|
** -------------------------------------------------------------------------*/
|
|
|
|
|
2021-01-21 23:23:30 +08:00
|
|
|
#include <utility>
|
2021-01-07 00:26:08 +08:00
|
|
|
|
|
|
|
#include "zm_rtsp_server_device_source.h"
|
|
|
|
#include "zm_rtsp_server_frame.h"
|
|
|
|
#include "zm_logger.h"
|
|
|
|
|
|
|
|
ZoneMinderDeviceSource::ZoneMinderDeviceSource(
|
|
|
|
UsageEnvironment& env,
|
|
|
|
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-01-07 00:26:08 +08:00
|
|
|
m_monitor(monitor),
|
|
|
|
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);
|
|
|
|
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() {
|
|
|
|
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-21 23:23:30 +08:00
|
|
|
Debug(2, "deliverFrame timestamp: %d.%d size: %d queuesize: %d",
|
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;
|
|
|
|
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
|
|
|
// Convert pts to timeval
|
|
|
|
int64_t pts = av_rescale_q(pkt->pts, m_stream->time_base, AV_TIME_BASE_Q);
|
|
|
|
timeval tv = { pts/1000000, pts % 1000000 };
|
|
|
|
dumpPacket(m_stream, pkt, "rtspServer");
|
|
|
|
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-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);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|