2017-05-17 00:04:56 +08:00
|
|
|
//ZoneMinder Packet Queue Implementation Class
|
|
|
|
//Copyright 2016 Steve Gilvarry
|
|
|
|
//
|
|
|
|
//This file is part of ZoneMinder.
|
|
|
|
//
|
|
|
|
//ZoneMinder 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 3 of the License, or
|
|
|
|
//(at your option) any later version.
|
|
|
|
//
|
|
|
|
//ZoneMinder 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 ZoneMinder. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2021-01-07 22:43:53 +08:00
|
|
|
// PacketQueue must know about all iterators and manage them
|
|
|
|
|
2017-05-17 00:04:56 +08:00
|
|
|
#include "zm_packetqueue.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
|
2017-05-17 00:04:56 +08:00
|
|
|
#include "zm_ffmpeg.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_packet.h"
|
2019-02-26 22:45:40 +08:00
|
|
|
#include "zm_signal.h"
|
2017-08-24 03:05:44 +08:00
|
|
|
#include <sys/time.h>
|
2017-05-17 00:04:56 +08:00
|
|
|
|
2021-02-01 10:30:12 +08:00
|
|
|
PacketQueue::PacketQueue():
|
|
|
|
video_stream_id(-1),
|
|
|
|
max_video_packet_count(-1),
|
2021-03-27 02:26:37 +08:00
|
|
|
pre_event_video_packet_count(-1),
|
2021-02-01 10:30:12 +08:00
|
|
|
max_stream_id(-1),
|
|
|
|
packet_counts(nullptr),
|
2021-03-23 00:04:32 +08:00
|
|
|
deleting(false),
|
|
|
|
keep_keyframes(false)
|
2020-12-22 23:20:44 +08:00
|
|
|
{
|
2017-05-17 00:04:56 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 10:30:12 +08:00
|
|
|
/* Assumes queue is empty when adding streams
|
|
|
|
* Assumes first stream added will be the video stream
|
|
|
|
*/
|
2021-03-03 22:52:13 +08:00
|
|
|
int PacketQueue::addStream() {
|
2021-02-03 03:22:38 +08:00
|
|
|
deleting = false;
|
2021-03-31 04:14:38 +08:00
|
|
|
if (max_stream_id == -1) {
|
2021-03-03 22:52:13 +08:00
|
|
|
video_stream_id = 0;
|
|
|
|
max_stream_id = 0;
|
|
|
|
} else {
|
|
|
|
max_stream_id ++;
|
2020-12-10 04:01:24 +08:00
|
|
|
}
|
2021-03-03 22:52:13 +08:00
|
|
|
|
2021-03-31 04:14:38 +08:00
|
|
|
if (packet_counts) delete[] packet_counts;
|
2021-03-03 22:52:13 +08:00
|
|
|
packet_counts = new int[max_stream_id+1];
|
2021-03-31 04:14:38 +08:00
|
|
|
for (int i=0; i <= max_stream_id; ++i)
|
2021-03-03 22:52:13 +08:00
|
|
|
packet_counts[i] = 0;
|
|
|
|
return max_stream_id;
|
2021-02-01 10:30:12 +08:00
|
|
|
}
|
2020-12-10 04:01:24 +08:00
|
|
|
|
2021-02-01 10:30:12 +08:00
|
|
|
PacketQueue::~PacketQueue() {
|
|
|
|
clear();
|
2021-03-18 00:48:42 +08:00
|
|
|
if (packet_counts) {
|
2021-02-01 23:25:48 +08:00
|
|
|
delete[] packet_counts;
|
|
|
|
packet_counts = nullptr;
|
|
|
|
}
|
2021-03-18 00:48:42 +08:00
|
|
|
while (!iterators.empty()) {
|
2021-02-01 23:25:48 +08:00
|
|
|
packetqueue_iterator *it = iterators.front();
|
|
|
|
iterators.pop_front();
|
|
|
|
delete it;
|
|
|
|
}
|
2021-01-07 22:43:53 +08:00
|
|
|
Debug(4, "Done in destructor");
|
2017-05-17 00:04:56 +08:00
|
|
|
}
|
|
|
|
|
2021-01-07 22:43:53 +08:00
|
|
|
/* Enqueues the given packet. Will maintain the it pointer and image packet counts.
|
2018-08-18 04:06:03 +08:00
|
|
|
* If we have reached our max image packet count, it will pop off as many packets as are needed.
|
|
|
|
* Thus it will ensure that the same packet never gets queued twice.
|
|
|
|
*/
|
|
|
|
|
2021-05-09 09:14:20 +08:00
|
|
|
bool PacketQueue::queuePacket(std::shared_ptr<ZMPacket> add_packet) {
|
2021-02-11 03:11:00 +08:00
|
|
|
if (iterators.empty()) {
|
|
|
|
Debug(4, "No iterators so no one needs us to queue packets.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!packet_counts[video_stream_id] and !add_packet->keyframe) {
|
|
|
|
Debug(4, "No video keyframe so no one needs us to queue packets.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-27 05:01:45 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
|
|
|
|
|
|
|
if (add_packet->packet.stream_index == video_stream_id) {
|
|
|
|
if ((max_video_packet_count > 0) and (packet_counts[video_stream_id] > max_video_packet_count)) {
|
2021-04-01 23:53:54 +08:00
|
|
|
Warning("You have set the max video packets in the queue to %u."
|
2021-03-31 22:03:48 +08:00
|
|
|
" The queue is full. Either Analysis is not keeping up or"
|
|
|
|
" your camera's keyframe interval is larger than this setting."
|
|
|
|
" We are dropping packets.", max_video_packet_count);
|
2021-03-27 05:01:45 +08:00
|
|
|
if (add_packet->keyframe) {
|
|
|
|
// Have a new keyframe, so delete everything
|
|
|
|
while ((*pktQueue.begin() != add_packet) and (packet_counts[video_stream_id] > max_video_packet_count)) {
|
2021-05-09 09:14:20 +08:00
|
|
|
std::shared_ptr <ZMPacket>zm_packet = *pktQueue.begin();
|
2021-03-27 05:01:45 +08:00
|
|
|
ZMLockedPacket *lp = new ZMLockedPacket(zm_packet);
|
|
|
|
if (!lp->trylock()) {
|
|
|
|
Debug(1, "Found locked packet when trying to free up video packets. Can't continue");
|
2021-03-31 04:14:38 +08:00
|
|
|
delete lp;
|
2021-03-27 05:01:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
delete lp;
|
|
|
|
|
|
|
|
for (
|
|
|
|
std::list<packetqueue_iterator *>::iterator iterators_it = iterators.begin();
|
|
|
|
iterators_it != iterators.end();
|
|
|
|
++iterators_it
|
|
|
|
) {
|
|
|
|
packetqueue_iterator *iterator_it = *iterators_it;
|
|
|
|
// Have to check each iterator and make sure it doesn't point to the packet we are about to delete
|
|
|
|
if ( *(*iterator_it) == zm_packet ) {
|
|
|
|
Debug(1, "Bumping IT because it is at the front that we are deleting");
|
|
|
|
++(*iterators_it);
|
|
|
|
}
|
|
|
|
} // end foreach iterator
|
|
|
|
|
|
|
|
pktQueue.pop_front();
|
|
|
|
packet_counts[zm_packet->packet.stream_index] -= 1;
|
2021-04-29 06:13:16 +08:00
|
|
|
Debug(1,
|
|
|
|
"Deleting a packet with stream index:%d image_index:%d with keyframe:%d, video frames in queue:%d max: %d, queuesize:%zu",
|
|
|
|
zm_packet->packet.stream_index,
|
|
|
|
zm_packet->image_index,
|
|
|
|
zm_packet->keyframe,
|
|
|
|
packet_counts[video_stream_id],
|
|
|
|
max_video_packet_count,
|
|
|
|
pktQueue.size());
|
2021-03-27 05:01:45 +08:00
|
|
|
} // end while
|
|
|
|
}
|
|
|
|
} // end if too many video packets
|
2021-06-04 06:21:46 +08:00
|
|
|
if (max_video_packet_count > 0) {
|
|
|
|
while (packet_counts[video_stream_id] > max_video_packet_count) {
|
|
|
|
Error("Unable to free up older packets. Waiting.");
|
|
|
|
condition.wait(lck);
|
|
|
|
if (deleting or zm_terminate)
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-27 05:01:45 +08:00
|
|
|
}
|
|
|
|
} // end if this packet is a video packet
|
|
|
|
|
|
|
|
pktQueue.push_back(add_packet);
|
|
|
|
packet_counts[add_packet->packet.stream_index] += 1;
|
|
|
|
Debug(2, "packet counts for %d is %d",
|
|
|
|
add_packet->packet.stream_index,
|
|
|
|
packet_counts[add_packet->packet.stream_index]);
|
|
|
|
|
|
|
|
for (
|
|
|
|
std::list<packetqueue_iterator *>::iterator iterators_it = iterators.begin();
|
|
|
|
iterators_it != iterators.end();
|
|
|
|
++iterators_it
|
|
|
|
) {
|
|
|
|
packetqueue_iterator *iterator_it = *iterators_it;
|
2021-04-07 01:47:34 +08:00
|
|
|
if (*iterator_it == pktQueue.end()) {
|
2021-03-27 05:01:45 +08:00
|
|
|
Debug(4, "pointing it %p to back", iterator_it);
|
|
|
|
--(*iterator_it);
|
2021-04-07 01:47:34 +08:00
|
|
|
} else {
|
|
|
|
Debug(4, "it %p not at end", iterator_it);
|
2021-03-27 05:01:45 +08:00
|
|
|
}
|
|
|
|
} // end foreach iterator
|
|
|
|
} // end lock scope
|
2021-02-19 08:25:40 +08:00
|
|
|
// We signal on every packet because someday we may analyze sound
|
|
|
|
Debug(4, "packetqueue queuepacket, unlocked signalling");
|
|
|
|
condition.notify_all();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} // end bool PacketQueue::queuePacket(ZMPacket* zm_packet)
|
2021-01-07 22:43:53 +08:00
|
|
|
|
2021-05-09 09:14:20 +08:00
|
|
|
void PacketQueue::clearPackets(const std::shared_ptr<ZMPacket> &add_packet) {
|
2021-01-07 22:43:53 +08:00
|
|
|
// Only do queueCleaning if we are adding a video keyframe, so that we guarantee that there is one.
|
|
|
|
// No good. Have to satisfy two conditions:
|
|
|
|
// 1. packetqueue starts with a video keyframe
|
|
|
|
// 2. Have minimum # of video packets
|
|
|
|
// 3. No packets can be locked
|
|
|
|
// 4. No iterator can point to one of the packets
|
|
|
|
//
|
|
|
|
// So start at the beginning, counting video packets until the next keyframe.
|
|
|
|
// Then if deleting those packets doesn't break 1 and 2, then go ahead and delete them.
|
2021-06-05 03:03:08 +08:00
|
|
|
if (deleting) return;
|
2021-06-14 02:04:40 +08:00
|
|
|
if (!pktQueue.size()) return;
|
2021-06-05 03:03:08 +08:00
|
|
|
|
2021-03-23 00:04:32 +08:00
|
|
|
if (keep_keyframes and ! (
|
2021-02-19 08:25:40 +08:00
|
|
|
add_packet->packet.stream_index == video_stream_id
|
2021-03-23 00:04:32 +08:00
|
|
|
and
|
|
|
|
add_packet->keyframe
|
|
|
|
and
|
2021-03-27 02:26:37 +08:00
|
|
|
(packet_counts[video_stream_id] > pre_event_video_packet_count)
|
2021-03-23 00:04:32 +08:00
|
|
|
and
|
|
|
|
*(pktQueue.begin()) != add_packet
|
|
|
|
)
|
2021-01-07 22:43:53 +08:00
|
|
|
) {
|
2021-03-27 02:26:37 +08:00
|
|
|
Debug(3, "stream index %d ?= video_stream_id %d, keyframe %d, keep_keyframes %d, counts %d > pre_event_count %d at begin %d",
|
|
|
|
add_packet->packet.stream_index, video_stream_id, add_packet->keyframe, keep_keyframes, packet_counts[video_stream_id], pre_event_video_packet_count,
|
2021-03-04 20:46:39 +08:00
|
|
|
( *(pktQueue.begin()) != add_packet )
|
|
|
|
);
|
2021-02-19 08:25:40 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
2021-03-23 09:31:09 +08:00
|
|
|
|
2021-04-29 00:12:24 +08:00
|
|
|
// If analysis_it isn't at the end, we need to keep that many additional packets
|
2021-03-23 09:31:09 +08:00
|
|
|
int tail_count = 0;
|
2021-05-08 02:04:51 +08:00
|
|
|
if (pktQueue.back() != add_packet) {
|
2021-03-23 09:31:09 +08:00
|
|
|
packetqueue_iterator it = pktQueue.end();
|
|
|
|
--it;
|
|
|
|
while (*it != add_packet) {
|
|
|
|
if ((*it)->packet.stream_index == video_stream_id)
|
|
|
|
++tail_count;
|
|
|
|
--it;
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 02:04:51 +08:00
|
|
|
Debug(1, "Tail count is %d, queue size is %lu", tail_count, pktQueue.size());
|
2021-03-23 09:31:09 +08:00
|
|
|
|
2021-03-23 00:04:32 +08:00
|
|
|
if (!keep_keyframes) {
|
|
|
|
// If not doing passthrough, we don't care about starting with a keyframe so logic is simpler
|
2021-03-27 02:26:37 +08:00
|
|
|
while ((*pktQueue.begin() != add_packet) and (packet_counts[video_stream_id] > pre_event_video_packet_count + tail_count)) {
|
2021-05-09 09:14:20 +08:00
|
|
|
std::shared_ptr<ZMPacket> zm_packet = *pktQueue.begin();
|
2021-03-23 00:04:32 +08:00
|
|
|
ZMLockedPacket *lp = new ZMLockedPacket(zm_packet);
|
|
|
|
if (!lp->trylock()) break;
|
|
|
|
delete lp;
|
|
|
|
|
2021-04-01 23:52:25 +08:00
|
|
|
if (is_there_an_iterator_pointing_to_packet(zm_packet)) {
|
|
|
|
Warning("Found iterator at beginning of queue. Some thread isn't keeping up");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-23 00:04:32 +08:00
|
|
|
pktQueue.pop_front();
|
|
|
|
packet_counts[zm_packet->packet.stream_index] -= 1;
|
2021-04-29 06:13:16 +08:00
|
|
|
Debug(1,
|
|
|
|
"Deleting a packet with stream index:%d image_index:%d with keyframe:%d, video frames in queue:%d max: %d, queuesize:%zu",
|
|
|
|
zm_packet->packet.stream_index,
|
|
|
|
zm_packet->image_index,
|
|
|
|
zm_packet->keyframe,
|
|
|
|
packet_counts[video_stream_id],
|
|
|
|
pre_event_video_packet_count,
|
|
|
|
pktQueue.size());
|
2021-05-09 09:14:20 +08:00
|
|
|
//delete zm_packet;
|
2021-03-23 00:04:32 +08:00
|
|
|
} // end while
|
|
|
|
return;
|
|
|
|
}
|
2021-02-19 08:25:40 +08:00
|
|
|
|
|
|
|
packetqueue_iterator it = pktQueue.begin();
|
|
|
|
packetqueue_iterator next_front = pktQueue.begin();
|
|
|
|
|
|
|
|
// First packet is special because we know it is a video keyframe and only need to check for lock
|
2021-05-09 09:14:20 +08:00
|
|
|
std::shared_ptr<ZMPacket> zm_packet = *it;
|
|
|
|
if (zm_packet == add_packet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-08 02:04:51 +08:00
|
|
|
Debug(1, "trying lock on first packet");
|
2021-03-16 05:05:30 +08:00
|
|
|
ZMLockedPacket *lp = new ZMLockedPacket(zm_packet);
|
2021-03-22 00:28:33 +08:00
|
|
|
if (lp->trylock()) {
|
2021-05-10 04:39:38 +08:00
|
|
|
int video_packets_to_delete = 0; // This is a count of how many packets we will delete so we know when to stop looking
|
2021-05-08 02:04:51 +08:00
|
|
|
Debug(1, "Have lock on first packet");
|
2021-02-19 08:25:40 +08:00
|
|
|
++it;
|
2021-03-16 05:05:30 +08:00
|
|
|
delete lp;
|
2021-02-19 08:25:40 +08:00
|
|
|
|
2021-05-08 02:04:51 +08:00
|
|
|
if (it == pktQueue.end()) {
|
|
|
|
Debug(1, "Hit end already");
|
|
|
|
it = pktQueue.begin();
|
|
|
|
} else {
|
2021-02-19 08:25:40 +08:00
|
|
|
// Since we have many packets in the queue, we should NOT be pointing at end so don't need to test for that
|
2021-03-22 00:28:33 +08:00
|
|
|
while (*it != add_packet) {
|
2021-02-19 08:25:40 +08:00
|
|
|
zm_packet = *it;
|
2021-03-16 05:05:30 +08:00
|
|
|
lp = new ZMLockedPacket(zm_packet);
|
2021-04-18 00:50:20 +08:00
|
|
|
if (!lp->trylock()) {
|
|
|
|
delete lp;
|
|
|
|
break;
|
|
|
|
}
|
2021-03-16 05:05:30 +08:00
|
|
|
delete lp;
|
2021-02-01 23:25:48 +08:00
|
|
|
|
2021-04-15 00:48:53 +08:00
|
|
|
if (is_there_an_iterator_pointing_to_packet(zm_packet) and (pktQueue.begin() == next_front)) {
|
2021-02-20 04:56:42 +08:00
|
|
|
Warning("Found iterator at beginning of queue. Some thread isn't keeping up");
|
2021-02-19 08:25:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-02-03 03:22:38 +08:00
|
|
|
|
2021-03-22 00:28:33 +08:00
|
|
|
if (zm_packet->packet.stream_index == video_stream_id) {
|
|
|
|
if (zm_packet->keyframe) {
|
2021-03-05 23:02:12 +08:00
|
|
|
Debug(3, "Have a video keyframe so setting next front to it");
|
2021-02-19 08:25:40 +08:00
|
|
|
next_front = it;
|
2021-01-07 22:43:53 +08:00
|
|
|
}
|
2021-03-05 23:02:12 +08:00
|
|
|
++video_packets_to_delete;
|
2021-03-22 00:28:33 +08:00
|
|
|
Debug(4, "Counted %d video packets. Which would leave %d in packetqueue tail count is %d",
|
|
|
|
video_packets_to_delete, packet_counts[video_stream_id]-video_packets_to_delete, tail_count);
|
2021-03-27 02:26:37 +08:00
|
|
|
if (packet_counts[video_stream_id] - video_packets_to_delete <= pre_event_video_packet_count + tail_count) {
|
2021-03-05 23:02:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-01-07 22:43:53 +08:00
|
|
|
}
|
2021-05-10 04:39:38 +08:00
|
|
|
++it;
|
2021-02-19 08:25:40 +08:00
|
|
|
} // end while
|
2021-05-08 02:04:51 +08:00
|
|
|
}
|
2021-02-19 08:25:40 +08:00
|
|
|
} // end if first packet not locked
|
2021-02-24 02:12:09 +08:00
|
|
|
Debug(1, "Resulting pointing at latest packet? %d, next front points to begin? %d",
|
2021-02-19 08:25:40 +08:00
|
|
|
( *it == add_packet ),
|
|
|
|
( next_front == pktQueue.begin() )
|
|
|
|
);
|
2021-05-09 09:14:20 +08:00
|
|
|
if (next_front != pktQueue.begin()) {
|
|
|
|
while (pktQueue.begin() != next_front) {
|
2021-05-10 04:39:38 +08:00
|
|
|
zm_packet = *pktQueue.begin();
|
2021-05-09 09:14:20 +08:00
|
|
|
if (!zm_packet) {
|
2021-02-19 08:25:40 +08:00
|
|
|
Error("NULL zm_packet in queue");
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-18 04:06:03 +08:00
|
|
|
|
2021-04-29 06:13:16 +08:00
|
|
|
Debug(1,
|
|
|
|
"Deleting a packet with stream index:%d image_index:%d with keyframe:%d, video frames in queue:%d max: %d, queuesize:%zu",
|
|
|
|
zm_packet->packet.stream_index,
|
|
|
|
zm_packet->image_index,
|
|
|
|
zm_packet->keyframe,
|
|
|
|
packet_counts[video_stream_id],
|
|
|
|
pre_event_video_packet_count,
|
|
|
|
pktQueue.size());
|
2021-02-19 08:25:40 +08:00
|
|
|
pktQueue.pop_front();
|
|
|
|
packet_counts[zm_packet->packet.stream_index] -= 1;
|
2021-05-09 09:14:20 +08:00
|
|
|
//delete zm_packet;
|
2021-02-19 08:25:40 +08:00
|
|
|
}
|
|
|
|
} // end if have at least max_video_packet_count video packets remaining
|
2020-12-10 04:01:24 +08:00
|
|
|
// We signal on every packet because someday we may analyze sound
|
2017-05-17 00:04:56 +08:00
|
|
|
|
2021-02-19 08:25:40 +08:00
|
|
|
return;
|
|
|
|
} // end voidPacketQueue::clearPackets(ZMPacket* zm_packet)
|
2017-05-17 00:04:56 +08:00
|
|
|
|
2021-02-01 10:30:12 +08:00
|
|
|
void PacketQueue::clear() {
|
|
|
|
deleting = true;
|
2021-03-18 00:48:42 +08:00
|
|
|
condition.notify_all();
|
2021-06-14 02:04:40 +08:00
|
|
|
if (!packet_counts) // special case, not initialised
|
|
|
|
return;
|
2021-05-08 02:04:51 +08:00
|
|
|
Debug(1, "Clearing packetqueue");
|
2021-02-01 10:30:12 +08:00
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
|
|
|
|
2021-03-12 02:48:52 +08:00
|
|
|
while (!pktQueue.empty()) {
|
2021-05-09 09:14:20 +08:00
|
|
|
std::shared_ptr<ZMPacket> packet = pktQueue.front();
|
2021-02-01 10:30:12 +08:00
|
|
|
// Someone might have this packet, but not for very long and since we have locked the queue they won't be able to get another one
|
2021-03-17 22:07:03 +08:00
|
|
|
ZMLockedPacket *lp = new ZMLockedPacket(packet);
|
|
|
|
lp->lock();
|
2021-06-05 03:03:08 +08:00
|
|
|
Debug(1,
|
|
|
|
"Deleting a packet with stream index:%d image_index:%d with keyframe:%d, video frames in queue:%d max: %d, queuesize:%zu",
|
|
|
|
packet->packet.stream_index,
|
|
|
|
packet->image_index,
|
|
|
|
packet->keyframe,
|
|
|
|
packet_counts[video_stream_id],
|
|
|
|
pre_event_video_packet_count,
|
|
|
|
pktQueue.size());
|
2017-05-17 00:04:56 +08:00
|
|
|
pktQueue.pop_front();
|
2021-03-17 22:07:03 +08:00
|
|
|
delete lp;
|
2021-05-09 09:14:20 +08:00
|
|
|
//delete packet;
|
2021-02-01 10:30:12 +08:00
|
|
|
}
|
2021-06-05 03:03:08 +08:00
|
|
|
Debug(1, "Packetqueue is clear, deleting iterators");
|
2021-02-01 10:30:12 +08:00
|
|
|
|
2021-01-07 22:43:53 +08:00
|
|
|
for (
|
|
|
|
std::list<packetqueue_iterator *>::iterator iterators_it = iterators.begin();
|
|
|
|
iterators_it != iterators.end();
|
|
|
|
++iterators_it
|
|
|
|
) {
|
|
|
|
packetqueue_iterator *iterator_it = *iterators_it;
|
|
|
|
*iterator_it = pktQueue.begin();
|
|
|
|
} // end foreach iterator
|
2021-03-04 20:46:39 +08:00
|
|
|
|
2021-05-09 09:14:20 +08:00
|
|
|
if (packet_counts) delete[] packet_counts;
|
2021-03-04 20:46:39 +08:00
|
|
|
packet_counts = nullptr;
|
|
|
|
max_stream_id = -1;
|
|
|
|
|
2021-06-05 03:03:08 +08:00
|
|
|
Debug(1, "Packetqueue is clear, notifying");
|
2021-02-01 10:30:12 +08:00
|
|
|
condition.notify_all();
|
2017-05-17 00:04:56 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 10:30:12 +08:00
|
|
|
unsigned int PacketQueue::size() {
|
2017-05-17 00:04:56 +08:00
|
|
|
return pktQueue.size();
|
|
|
|
}
|
|
|
|
|
2021-02-01 10:30:12 +08:00
|
|
|
int PacketQueue::packet_count(int stream_id) {
|
2021-05-09 09:14:20 +08:00
|
|
|
if (stream_id < 0 or stream_id > max_stream_id) {
|
2021-02-24 02:12:09 +08:00
|
|
|
Error("Invalid stream_id %d max is %d", stream_id, max_stream_id);
|
2021-02-19 02:43:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2018-10-15 22:51:56 +08:00
|
|
|
return packet_counts[stream_id];
|
2021-02-20 01:44:20 +08:00
|
|
|
} // end int PacketQueue::packet_count(int stream_id)
|
2017-05-17 00:04:56 +08:00
|
|
|
|
2021-01-07 22:43:53 +08:00
|
|
|
// Returns a packet. Packet will be locked
|
2021-03-16 05:05:30 +08:00
|
|
|
ZMLockedPacket *PacketQueue::get_packet(packetqueue_iterator *it) {
|
2021-03-18 00:48:42 +08:00
|
|
|
if (deleting or zm_terminate)
|
2021-01-27 01:23:44 +08:00
|
|
|
return nullptr;
|
2017-12-01 03:37:36 +08:00
|
|
|
|
2021-05-09 09:14:20 +08:00
|
|
|
Debug(4, "Locking in get_packet using it %p queue end? %d",
|
|
|
|
std::addressof(*it), (*it == pktQueue.end()));
|
2020-12-10 04:01:24 +08:00
|
|
|
|
2021-04-01 23:52:25 +08:00
|
|
|
ZMLockedPacket *lp = nullptr;
|
2021-05-13 20:54:55 +08:00
|
|
|
{ // scope for lock
|
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
|
|
|
Debug(4, "Have Lock in get_packet");
|
|
|
|
while (!lp) {
|
2021-06-04 06:21:46 +08:00
|
|
|
while ((*it == pktQueue.end()) and !(deleting or zm_terminate)) {
|
2021-05-13 20:54:55 +08:00
|
|
|
Debug(2, "waiting. Queue size %zu it == end? %d", pktQueue.size(), (*it == pktQueue.end()));
|
|
|
|
condition.wait(lck);
|
2021-05-08 02:04:51 +08:00
|
|
|
}
|
2021-05-13 20:54:55 +08:00
|
|
|
if (deleting or zm_terminate) break;
|
2017-12-01 03:37:36 +08:00
|
|
|
|
2021-05-13 20:54:55 +08:00
|
|
|
std::shared_ptr<ZMPacket> p = *(*it);
|
|
|
|
if (!p) {
|
|
|
|
Error("Null p?!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Debug(3, "get_packet using it %p locking index %d",
|
2021-05-09 09:14:20 +08:00
|
|
|
std::addressof(*it), p->image_index);
|
2021-03-31 04:14:38 +08:00
|
|
|
|
2021-05-13 20:54:55 +08:00
|
|
|
lp = new ZMLockedPacket(p);
|
|
|
|
if (lp->trylock()) {
|
|
|
|
Debug(2, "Locked packet %d, unlocking packetqueue mutex", p->image_index);
|
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
delete lp;
|
|
|
|
lp = nullptr;
|
|
|
|
Debug(2, "waiting. Queue size %zu it == end? %d", pktQueue.size(), (*it == pktQueue.end()));
|
|
|
|
condition.wait(lck);
|
|
|
|
} // end while !lp
|
|
|
|
} // end scope for lock
|
2021-04-01 23:52:25 +08:00
|
|
|
|
2021-05-13 20:54:55 +08:00
|
|
|
if (!lp) {
|
|
|
|
Debug(1, "terminated, leaving");
|
|
|
|
condition.notify_all();
|
|
|
|
}
|
|
|
|
return lp;
|
2021-03-16 05:05:30 +08:00
|
|
|
} // end ZMLockedPacket *PacketQueue::get_packet(it)
|
2021-01-07 22:43:53 +08:00
|
|
|
|
2021-06-04 06:21:46 +08:00
|
|
|
// Returns a packet. Packet will be locked
|
|
|
|
ZMLockedPacket *PacketQueue::get_packet_and_increment_it(packetqueue_iterator *it) {
|
|
|
|
if (deleting or zm_terminate)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Debug(4, "Locking in get_packet using it %p queue end? %d",
|
|
|
|
std::addressof(*it), (*it == pktQueue.end()));
|
|
|
|
|
|
|
|
ZMLockedPacket *lp = nullptr;
|
|
|
|
{ // scope for lock
|
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
|
|
|
Debug(4, "Have Lock in get_packet");
|
|
|
|
while (!lp) {
|
|
|
|
while ((*it == pktQueue.end()) and !(deleting or zm_terminate)) {
|
|
|
|
Debug(2, "waiting. Queue size %zu it == end? %d", pktQueue.size(), (*it == pktQueue.end()));
|
|
|
|
condition.wait(lck);
|
|
|
|
}
|
|
|
|
if (deleting or zm_terminate) break;
|
|
|
|
|
|
|
|
std::shared_ptr<ZMPacket> p = *(*it);
|
|
|
|
if (!p) {
|
|
|
|
Error("Null p?!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Debug(3, "get_packet using it %p locking index %d",
|
|
|
|
std::addressof(*it), p->image_index);
|
|
|
|
|
|
|
|
lp = new ZMLockedPacket(p);
|
|
|
|
if (lp->trylock()) {
|
|
|
|
Debug(2, "Locked packet %d, unlocking packetqueue mutex, incrementing it", p->image_index);
|
|
|
|
++(*it);
|
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
delete lp;
|
|
|
|
lp = nullptr;
|
|
|
|
Debug(2, "waiting. Queue size %zu it == end? %d", pktQueue.size(), (*it == pktQueue.end()));
|
|
|
|
condition.wait(lck);
|
|
|
|
} // end while !lp
|
|
|
|
} // end scope for lock
|
|
|
|
|
|
|
|
if (!lp) {
|
|
|
|
Debug(1, "terminated, leaving");
|
|
|
|
condition.notify_all();
|
|
|
|
}
|
|
|
|
return lp;
|
|
|
|
} // end ZMLockedPacket *PacketQueue::get_packet_and_increment_it(it)
|
|
|
|
|
2021-04-01 23:52:25 +08:00
|
|
|
void PacketQueue::unlock(ZMLockedPacket *lp) {
|
|
|
|
delete lp;
|
|
|
|
condition.notify_all();
|
|
|
|
}
|
|
|
|
|
2021-02-01 10:30:12 +08:00
|
|
|
bool PacketQueue::increment_it(packetqueue_iterator *it) {
|
2021-04-29 06:13:16 +08:00
|
|
|
Debug(2, "Incrementing %p, queue size %zu, end? %d", it, pktQueue.size(), ((*it) == pktQueue.end()));
|
2021-06-04 06:21:46 +08:00
|
|
|
if (((*it) == pktQueue.end()) or deleting) {
|
2021-01-07 22:43:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-07 01:47:34 +08:00
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
2021-01-07 22:43:53 +08:00
|
|
|
++(*it);
|
2021-04-07 01:47:34 +08:00
|
|
|
if (*it != pktQueue.end()) {
|
2021-04-29 06:13:16 +08:00
|
|
|
Debug(2, "Incrementing %p, %p still not at end, so returning true", it, std::addressof(*it));
|
2021-01-07 22:43:53 +08:00
|
|
|
return true;
|
2020-12-10 04:01:24 +08:00
|
|
|
}
|
2021-04-07 01:47:34 +08:00
|
|
|
Debug(2, "At end");
|
2021-01-07 22:43:53 +08:00
|
|
|
return false;
|
2021-02-01 10:30:12 +08:00
|
|
|
} // end bool PacketQueue::increment_it(packetqueue_iterator *it)
|
2020-12-10 04:01:24 +08:00
|
|
|
|
2021-01-07 22:43:53 +08:00
|
|
|
// Increment it only considering packets for a given stream
|
2021-02-01 10:30:12 +08:00
|
|
|
bool PacketQueue::increment_it(packetqueue_iterator *it, int stream_id) {
|
2021-04-29 06:13:16 +08:00
|
|
|
Debug(2, "Incrementing %p, queue size %zu, end? %d", it, pktQueue.size(), (*it == pktQueue.end()));
|
2021-06-04 06:21:46 +08:00
|
|
|
if (*it == pktQueue.end()) {
|
2017-12-01 03:37:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-21 03:14:20 +08:00
|
|
|
|
2021-01-28 00:48:19 +08:00
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
2021-01-07 22:43:53 +08:00
|
|
|
do {
|
|
|
|
++(*it);
|
|
|
|
} while ( (*it != pktQueue.end()) and ( (*(*it))->packet.stream_index != stream_id) );
|
|
|
|
|
|
|
|
if ( *it != pktQueue.end() ) {
|
|
|
|
Debug(2, "Incrementing %p, still not at end, so incrementing", it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-02-01 10:30:12 +08:00
|
|
|
} // end bool PacketQueue::increment_it(packetqueue_iterator *it)
|
2020-12-22 23:20:44 +08:00
|
|
|
|
2021-02-03 03:22:38 +08:00
|
|
|
packetqueue_iterator *PacketQueue::get_event_start_packet_it(
|
|
|
|
packetqueue_iterator snapshot_it,
|
2020-12-22 23:20:44 +08:00
|
|
|
unsigned int pre_event_count
|
|
|
|
) {
|
2021-02-19 05:59:17 +08:00
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
|
|
|
|
2021-02-03 03:22:38 +08:00
|
|
|
packetqueue_iterator *it = new packetqueue_iterator;
|
|
|
|
iterators.push_back(it);
|
|
|
|
|
|
|
|
*it = snapshot_it;
|
2021-05-09 09:14:20 +08:00
|
|
|
std::shared_ptr<ZMPacket> packet = *(*it);
|
2021-04-21 11:06:17 +08:00
|
|
|
ZM_DUMP_PACKET(packet->packet, "");
|
2020-12-22 23:20:44 +08:00
|
|
|
// Step one count back pre_event_count frames as the minimum
|
|
|
|
// Do not assume that snapshot_it is video
|
|
|
|
// snapshot it might already point to the beginning
|
2021-05-07 21:10:26 +08:00
|
|
|
if (pre_event_count) {
|
|
|
|
while ((*it) != pktQueue.begin()) {
|
|
|
|
packet = *(*it);
|
|
|
|
Debug(1, "Previous packet pre_event_count %d stream_index %d keyframe %d score %d",
|
|
|
|
pre_event_count, packet->packet.stream_index, packet->keyframe, packet->score);
|
|
|
|
ZM_DUMP_PACKET(packet->packet, "");
|
|
|
|
if (packet->packet.stream_index == video_stream_id) {
|
|
|
|
pre_event_count --;
|
|
|
|
if (!pre_event_count)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(*it)--;
|
2020-12-22 23:20:44 +08:00
|
|
|
}
|
2021-05-10 04:39:38 +08:00
|
|
|
packet = *(*it);
|
2020-12-22 23:20:44 +08:00
|
|
|
}
|
2021-01-27 01:23:44 +08:00
|
|
|
// it either points to beginning or we have seen pre_event_count video packets.
|
|
|
|
|
2021-05-07 21:10:26 +08:00
|
|
|
if (pre_event_count) {
|
|
|
|
if (packet->image_index < (int)pre_event_count) {
|
|
|
|
// probably just starting up
|
|
|
|
Debug(1, "Hit end of packetqueue before satisfying pre_event_count. Needed %d more video frames", pre_event_count);
|
|
|
|
} else {
|
|
|
|
Warning("Hit end of packetqueue before satisfying pre_event_count. Needed %d more video frames", pre_event_count);
|
2021-01-07 22:43:53 +08:00
|
|
|
}
|
2021-05-07 21:10:26 +08:00
|
|
|
ZM_DUMP_PACKET(packet->packet, "");
|
2020-12-22 23:20:44 +08:00
|
|
|
return it;
|
2021-05-11 22:20:40 +08:00
|
|
|
} else if (!keep_keyframes) {
|
|
|
|
// Are encoding, so don't care about keyframes
|
|
|
|
return it;
|
2020-12-22 23:20:44 +08:00
|
|
|
}
|
2021-01-27 01:23:44 +08:00
|
|
|
|
2021-05-07 21:10:26 +08:00
|
|
|
while ((*it) != pktQueue.begin()) {
|
2021-04-21 11:06:17 +08:00
|
|
|
packet = *(*it);
|
|
|
|
ZM_DUMP_PACKET(packet->packet, "No keyframe");
|
|
|
|
if ((packet->packet.stream_index == video_stream_id) and packet->keyframe)
|
2021-01-27 01:23:44 +08:00
|
|
|
return it; // Success
|
2021-05-07 21:10:26 +08:00
|
|
|
--(*it);
|
2020-12-22 23:20:44 +08:00
|
|
|
}
|
2021-05-07 21:10:26 +08:00
|
|
|
if (!(*(*it))->keyframe) {
|
|
|
|
Warning("Hit beginning of packetqueue and packet is not a keyframe. index is %d", packet->image_index);
|
2020-12-22 23:20:44 +08:00
|
|
|
}
|
|
|
|
return it;
|
2021-02-03 03:22:38 +08:00
|
|
|
} // end packetqueue_iterator *PacketQueue::get_event_start_packet_it
|
2020-12-22 23:20:44 +08:00
|
|
|
|
2021-02-01 10:30:12 +08:00
|
|
|
void PacketQueue::dumpQueue() {
|
2021-05-09 09:14:20 +08:00
|
|
|
std::list<std::shared_ptr<ZMPacket>>::reverse_iterator it;
|
2019-06-21 03:14:20 +08:00
|
|
|
for ( it = pktQueue.rbegin(); it != pktQueue.rend(); ++ it ) {
|
2021-05-09 09:14:20 +08:00
|
|
|
std::shared_ptr<ZMPacket> zm_packet = *it;
|
2021-02-06 21:46:28 +08:00
|
|
|
ZM_DUMP_PACKET(zm_packet->packet, "");
|
2019-06-21 03:14:20 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-07 22:43:53 +08:00
|
|
|
|
|
|
|
/* Returns an iterator to the first video keyframe in the queue.
|
|
|
|
* nullptr if no keyframe video packet exists.
|
|
|
|
*/
|
2021-02-01 10:30:12 +08:00
|
|
|
packetqueue_iterator * PacketQueue::get_video_it(bool wait) {
|
2021-01-07 22:43:53 +08:00
|
|
|
packetqueue_iterator *it = new packetqueue_iterator;
|
|
|
|
iterators.push_back(it);
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
|
|
|
*it = pktQueue.begin();
|
|
|
|
|
|
|
|
if ( wait ) {
|
|
|
|
while ( ((! pktQueue.size()) or (*it == pktQueue.end())) and !zm_terminate and !deleting ) {
|
2021-04-29 06:13:16 +08:00
|
|
|
Debug(2, "waiting for packets in queue. Queue size %zu it == end? %d", pktQueue.size(), (*it == pktQueue.end()));
|
2021-01-07 22:43:53 +08:00
|
|
|
condition.wait(lck);
|
|
|
|
*it = pktQueue.begin();
|
|
|
|
}
|
|
|
|
if ( deleting or zm_terminate ) {
|
2021-02-01 23:25:48 +08:00
|
|
|
free_it(it);
|
2021-01-07 22:43:53 +08:00
|
|
|
delete it;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 09:14:20 +08:00
|
|
|
while (*it != pktQueue.end()) {
|
|
|
|
std::shared_ptr<ZMPacket> zm_packet = *(*it);
|
2021-03-16 03:08:43 +08:00
|
|
|
if (!zm_packet) {
|
2021-01-07 22:43:53 +08:00
|
|
|
Error("Null zmpacket in queue!?");
|
2021-02-01 23:25:48 +08:00
|
|
|
free_it(it);
|
2021-01-07 22:43:53 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Debug(1, "Packet keyframe %d for stream %d, so returning the it to it",
|
|
|
|
zm_packet->keyframe, zm_packet->packet.stream_index);
|
2021-03-16 03:08:43 +08:00
|
|
|
if (zm_packet->keyframe and ( zm_packet->packet.stream_index == video_stream_id )) {
|
2021-01-07 22:43:53 +08:00
|
|
|
Debug(1, "Found a keyframe for stream %d, so returning the it to it", video_stream_id);
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
++(*it);
|
|
|
|
}
|
|
|
|
Debug(1, "DIdn't Found a keyframe for stream %d, so returning the it to it", video_stream_id);
|
|
|
|
return it;
|
2021-03-16 03:08:43 +08:00
|
|
|
} // get video_it
|
2021-02-01 23:25:48 +08:00
|
|
|
|
|
|
|
void PacketQueue::free_it(packetqueue_iterator *it) {
|
|
|
|
for (
|
|
|
|
std::list<packetqueue_iterator *>::iterator iterators_it = iterators.begin();
|
|
|
|
iterators_it != iterators.end();
|
|
|
|
++iterators_it
|
|
|
|
) {
|
|
|
|
if ( *iterators_it == it ) {
|
|
|
|
iterators.erase(iterators_it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 03:22:38 +08:00
|
|
|
|
2021-05-09 09:14:20 +08:00
|
|
|
bool PacketQueue::is_there_an_iterator_pointing_to_packet(const std::shared_ptr<ZMPacket> &zm_packet) {
|
2021-02-03 03:22:38 +08:00
|
|
|
for (
|
|
|
|
std::list<packetqueue_iterator *>::iterator iterators_it = iterators.begin();
|
|
|
|
iterators_it != iterators.end();
|
|
|
|
++iterators_it
|
|
|
|
) {
|
|
|
|
packetqueue_iterator *iterator_it = *iterators_it;
|
2021-05-09 09:14:20 +08:00
|
|
|
if (*iterator_it == pktQueue.end()) {
|
2021-02-03 03:22:38 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-29 06:13:16 +08:00
|
|
|
Debug(4, "Checking iterator %p == packet ? %d", std::addressof(*iterator_it), ( *(*iterator_it) == zm_packet ));
|
2021-02-03 03:22:38 +08:00
|
|
|
// Have to check each iterator and make sure it doesn't point to the packet we are about to delete
|
2021-05-09 09:14:20 +08:00
|
|
|
if (*(*iterator_it) == zm_packet) {
|
2021-02-03 03:22:38 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // end foreach iterator
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-12 02:48:52 +08:00
|
|
|
|
|
|
|
void PacketQueue::setMaxVideoPackets(int p) {
|
|
|
|
max_video_packet_count = p;
|
|
|
|
Debug(1, "Setting max_video_packet_count to %d", p);
|
2021-05-09 09:14:20 +08:00
|
|
|
if (max_video_packet_count < 0)
|
2021-03-27 02:26:37 +08:00
|
|
|
max_video_packet_count = 0 ;
|
|
|
|
}
|
|
|
|
void PacketQueue::setPreEventVideoPackets(int p) {
|
|
|
|
pre_event_video_packet_count = p;
|
|
|
|
Debug(1, "Setting pre_event_video_packet_count to %d", p);
|
2021-05-09 09:14:20 +08:00
|
|
|
if (pre_event_video_packet_count < 1)
|
2021-03-27 02:26:37 +08:00
|
|
|
pre_event_video_packet_count = 1;
|
2021-03-12 02:48:52 +08:00
|
|
|
// We can simplify a lot of logic in queuePacket if we can assume at least 1 packet in queue
|
|
|
|
}
|
2021-09-15 04:21:32 +08:00
|
|
|
|
|
|
|
void PacketQueue::notify_all() {
|
|
|
|
condition.notify_all();
|
|
|
|
};
|
|
|
|
|
|
|
|
void PacketQueue::wait() {
|
|
|
|
std::unique_lock<std::mutex> lck(mutex);
|
|
|
|
condition.wait(lck);
|
|
|
|
}
|