Skip to content

Commit 4b5bc09

Browse files
Merge new logic from master. We now delete a non-keyframe from head instead of waiting in capture.
1 parent 22f398d commit 4b5bc09

File tree

1 file changed

+68
-67
lines changed

1 file changed

+68
-67
lines changed

src/zm_packetqueue.cpp

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "zm_ffmpeg.h"
2525
#include "zm_packet.h"
2626
#include "zm_signal.h"
27-
#include <sys/time.h>
2827

2928
PacketQueue::PacketQueue():
3029
video_stream_id(-1),
@@ -87,87 +86,86 @@ bool PacketQueue::queuePacket(std::shared_ptr<ZMPacket> add_packet) {
8786
{
8887
std::unique_lock<std::mutex> lck(mutex);
8988

90-
if (add_packet->packet.stream_index == video_stream_id) {
91-
if ((max_video_packet_count > 0) and (packet_counts[video_stream_id] > max_video_packet_count)) {
92-
Warning("You have set the max video packets in the queue to %u."
93-
" The queue is full. Either Analysis is not keeping up or"
94-
" your camera's keyframe interval is larger than this setting."
95-
" We are dropping packets.", max_video_packet_count);
96-
if (add_packet->keyframe) {
97-
// Have a new keyframe, so delete everything
98-
while ((*pktQueue.begin() != add_packet) and (packet_counts[video_stream_id] > max_video_packet_count)) {
99-
std::shared_ptr <ZMPacket>zm_packet = *pktQueue.begin();
100-
ZMLockedPacket *lp = new ZMLockedPacket(zm_packet);
101-
if (!lp->trylock()) {
102-
Debug(1, "Found locked packet when trying to free up video packets. Can't continue");
103-
delete lp;
104-
break;
105-
}
106-
delete lp;
107-
108-
for (
109-
std::list<packetqueue_iterator *>::iterator iterators_it = iterators.begin();
110-
iterators_it != iterators.end();
111-
++iterators_it
112-
) {
113-
packetqueue_iterator *iterator_it = *iterators_it;
114-
// Have to check each iterator and make sure it doesn't point to the packet we are about to delete
115-
if ( *(*iterator_it) == zm_packet ) {
116-
Debug(1, "Bumping IT because it is at the front that we are deleting");
117-
++(*iterators_it);
118-
}
119-
} // end foreach iterator
120-
121-
pktQueue.pop_front();
122-
packet_counts[zm_packet->packet.stream_index] -= 1;
123-
Debug(1,
124-
"Deleting a packet with stream index:%d image_index:%d with keyframe:%d, video frames in queue:%d max: %d, queuesize:%zu",
125-
zm_packet->packet.stream_index,
126-
zm_packet->image_index,
127-
zm_packet->keyframe,
128-
packet_counts[video_stream_id],
129-
max_video_packet_count,
130-
pktQueue.size());
131-
} // end while
132-
}
133-
} // end if too many video packets
134-
if (max_video_packet_count > 0) {
135-
while (packet_counts[video_stream_id] > max_video_packet_count) {
136-
Error("Unable to free up older packets. Waiting.");
137-
condition.notify_all();
138-
condition.wait(lck);
139-
if (deleting or zm_terminate)
140-
return false;
141-
}
142-
}
143-
} // end if this packet is a video packet
144-
14589
pktQueue.push_back(add_packet);
14690
packet_counts[add_packet->packet.stream_index] += 1;
14791
Debug(2, "packet counts for %d is %d",
14892
add_packet->packet.stream_index,
14993
packet_counts[add_packet->packet.stream_index]);
15094

15195
for (
152-
std::list<packetqueue_iterator *>::iterator iterators_it = iterators.begin();
96+
auto iterators_it = iterators.begin();
15397
iterators_it != iterators.end();
15498
++iterators_it
15599
) {
156100
packetqueue_iterator *iterator_it = *iterators_it;
157101
if (*iterator_it == pktQueue.end()) {
158-
Debug(4, "pointing it %p to back", iterator_it);
159102
--(*iterator_it);
160-
} else {
161-
Debug(4, "it %p not at end", iterator_it);
162103
}
163104
} // end foreach iterator
105+
106+
if (
107+
(add_packet->packet.stream_index == video_stream_id)
108+
and
109+
(max_video_packet_count > 0)
110+
and
111+
(packet_counts[video_stream_id] > max_video_packet_count)
112+
) {
113+
Warning("You have set the max video packets in the queue to %u."
114+
" The queue is full. Either Analysis is not keeping up or"
115+
" your camera's keyframe interval is larger than this setting."
116+
, max_video_packet_count);
117+
118+
for (
119+
auto it = ++pktQueue.begin();
120+
it != pktQueue.end() and *it != add_packet;
121+
) {
122+
std::shared_ptr <ZMPacket>zm_packet = *it;
123+
124+
ZMLockedPacket *lp = new ZMLockedPacket(zm_packet);
125+
if (!lp->trylock()) {
126+
Debug(1, "Found locked packet when trying to free up video packets. Skipping to next one");
127+
delete lp;
128+
++it;
129+
continue;
130+
}
131+
132+
for (
133+
auto iterators_it = iterators.begin();
134+
iterators_it != iterators.end();
135+
++iterators_it
136+
) {
137+
auto iterator_it = *iterators_it;
138+
// Have to check each iterator and make sure it doesn't point to the packet we are about to delete
139+
if ((*iterator_it!=pktQueue.end()) and (*(*iterator_it) == zm_packet)) {
140+
Debug(1, "Bumping IT because it is at the front that we are deleting");
141+
++(*iterator_it);
142+
}
143+
} // end foreach iterator
144+
145+
it = pktQueue.erase(it);
146+
packet_counts[zm_packet->packet.stream_index] -= 1;
147+
Debug(1,
148+
"Deleting a packet with stream index:%d image_index:%d with keyframe:%d, video frames in queue:%d max: %d, queuesize:%zu",
149+
zm_packet->packet.stream_index,
150+
zm_packet->image_index,
151+
zm_packet->keyframe,
152+
packet_counts[video_stream_id],
153+
max_video_packet_count,
154+
pktQueue.size());
155+
156+
delete lp;
157+
158+
if (zm_packet->packet.stream_index == video_stream_id)
159+
break;
160+
} // end while
161+
} // end if not able catch up
164162
} // end lock scope
165163
// We signal on every packet because someday we may analyze sound
166164
Debug(4, "packetqueue queuepacket, unlocked signalling");
167165
condition.notify_all();
168166

169167
return true;
170-
} // end bool PacketQueue::queuePacket(ZMPacket* zm_packet)
168+
} // end bool PacketQueue::queuePacket(ZMPacket* zm_packet)
171169

172170
void PacketQueue::clearPackets(const std::shared_ptr<ZMPacket> &add_packet) {
173171
// Only do queueCleaning if we are adding a video keyframe, so that we guarantee that there is one.
@@ -241,20 +239,19 @@ void PacketQueue::clearPackets(const std::shared_ptr<ZMPacket> &add_packet) {
241239
return;
242240
}
243241

244-
packetqueue_iterator it = pktQueue.begin();
245-
packetqueue_iterator next_front = pktQueue.begin();
242+
auto it = pktQueue.begin();
243+
auto next_front = pktQueue.begin();
246244

247245
// First packet is special because we know it is a video keyframe and only need to check for lock
248246
std::shared_ptr<ZMPacket> zm_packet = *it;
249247
if (zm_packet == add_packet) {
250248
return;
251249
}
252250

253-
Debug(1, "trying lock on first packet");
254251
ZMLockedPacket *lp = new ZMLockedPacket(zm_packet);
255252
if (lp->trylock()) {
256253
int video_packets_to_delete = 0; // This is a count of how many packets we will delete so we know when to stop looking
257-
Debug(1, "Have lock on first packet");
254+
Debug(4, "Have lock on first packet");
258255
++it;
259256
delete lp;
260257

@@ -269,10 +266,14 @@ void PacketQueue::clearPackets(const std::shared_ptr<ZMPacket> &add_packet) {
269266
}
270267
delete lp;
271268

272-
if (is_there_an_iterator_pointing_to_packet(zm_packet) and (pktQueue.begin() == next_front)) {
273-
Warning("Found iterator at beginning of queue. Some thread isn't keeping up");
269+
#if 0
270+
// There are no threads that follow analysis thread. So there cannot be an it pointing here
271+
if (is_there_an_iterator_pointing_to_packet(zm_packet)) {
272+
if (pktQueue.begin() == next_front)
273+
Warning("Found iterator at beginning of queue. Some thread isn't keeping up");
274274
break;
275275
}
276+
#endif
276277

277278
if (zm_packet->packet.stream_index == video_stream_id) {
278279
if (zm_packet->keyframe) {

0 commit comments

Comments
 (0)