Skip to content

Commit 014c35d

Browse files
FIxup analysis index in shm. monitorstream now keeps up with analysis. SLeep 10 millis instead of 100. Seems to take around 30 for AI.
1 parent aeaf55a commit 014c35d

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

src/zm_monitor.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,21 +2223,24 @@ int Monitor::Analyse() {
22232223
}
22242224

22252225
int count = 10;
2226+
delayed_packet_lock = ai_queue.size() ? &ai_queue.front() : &packet_lock;
2227+
delayed_packet = delayed_packet_lock->packet_;
22262228
do {
22272229
// packet got to the card
22282230
Debug(1, "Doing receive_detection queue size: %zu", ai_queue.size());
2229-
delayed_packet_lock = ai_queue.size() ? &ai_queue.front() : &packet_lock;
2230-
delayed_packet = delayed_packet_lock->packet_;
22312231

22322232
ret = quadra_yolo->receive_detection(delayed_packet);
22332233
if (0 < ret) {
2234-
if (delayed_packet != packet) {
2234+
Debug(1, "Success %d", delayed_packet->image_index);
2235+
if (delayed_packet.get() != packet.get()) {
22352236
Debug(1, "Pushing packet, popping delayed");
22362237
ai_queue.push_back(std::move(packet_lock));
22372238
packet = delayed_packet;
22382239
packet_lock = std::move(ai_queue.front());
22392240
ai_queue.pop_front();
22402241
packetqueue.increment_it(analysis_it);
2242+
} else {
2243+
Debug(1, "packet %p != delayed_packet %p", packet.get(), delayed_packet.get());
22412244
}
22422245
if (packet->ai_frame)
22432246
zm_dump_video_frame(packet->ai_frame.get(), "after detect");
@@ -2255,14 +2258,14 @@ int Monitor::Analyse() {
22552258

22562259
} else {
22572260
// EAGAIN
2258-
Debug(1, "ret %d EAGAIN", ret);
2261+
Debug(1, "ret %d EAGAIN, sleeping 10 millis", ret);
22592262
//if (packet == delayed_packet) { // Can this be otherwise?
22602263
//ai_queue.push_back(std::move(packet_lock));
22612264
//Debug(1, "Pushing packet %d on queue, size now %zu", packet->image_index, ai_queue.size());
22622265
//packetqueue.increment_it(analysis_it);
22632266
//}
22642267
//return 0;
2265-
std::this_thread::sleep_for(Milliseconds(100));
2268+
std::this_thread::sleep_for(Milliseconds(10));
22662269
count -= 1;
22672270
}
22682271
} while (ret == 0 and count > 0);
@@ -2643,38 +2646,31 @@ int Monitor::Analyse() {
26432646
// Only do these if it's a video packet.
26442647
unsigned int index = shared_data->last_analysis_index;
26452648
index++;
2646-
index = index % image_buffer_count;
2649+
index = (index+1) % image_buffer_count;
26472650
if (packet->ai_frame) {
26482651
analysis_image_buffer[index]->AVPixFormat(static_cast<AVPixelFormat>(packet->ai_frame->format));
26492652
Debug(1, "ai_frame pixformat %d, for index %d, packet %d", packet->ai_frame->format, index, packet->image_index);
26502653
analysis_image_buffer[index]->Assign(packet->ai_frame.get());
26512654
analysis_image_pixelformats[index] = static_cast<AVPixelFormat>(packet->ai_frame->format);
2652-
shared_data->last_analysis_index = index;
2653-
2654-
shared_data->last_read_index = packet->image_index;
2655-
shared_data->analysis_image_count++;
26562655
} else if (packet->analysis_image) {
26572656
analysis_image_buffer[index]->Assign(*packet->analysis_image);
26582657
analysis_image_pixelformats[index] = packet->analysis_image->AVPixFormat();
26592658
Debug(1, "analysis %d, for index %d, packet %d", analysis_image_pixelformats[index], index, packet->image_index);
2660-
shared_data->last_analysis_index = index;
2661-
shared_data->analysis_image_count++;
26622659
} else if (packet->image) {
26632660
analysis_image_buffer[index]->Assign(*packet->image);
26642661
analysis_image_pixelformats[index] = packet->image->AVPixFormat();
2665-
shared_data->last_analysis_index = index;
26662662
Debug(1, "image %d, for index %d", analysis_image_pixelformats[index], index);
2667-
shared_data->analysis_image_count++;
26682663
} else if (packet->in_frame) {
26692664
analysis_image_buffer[index]->AVPixFormat(static_cast<AVPixelFormat>(packet->in_frame->format));
26702665
Debug(1, "in_frame pixformat %d, for index %d, packet %d", packet->in_frame->format, index, packet->image_index);
26712666
analysis_image_buffer[index]->Assign(packet->in_frame.get());
26722667
analysis_image_pixelformats[index] = static_cast<AVPixelFormat>(packet->in_frame->format);
2673-
shared_data->last_analysis_index = index;
2674-
shared_data->analysis_image_count++;
26752668
} else {
26762669
Debug(1, "Unable to find an image to assign for index %d", index);
26772670
}
2671+
shared_data->last_analysis_index = index;
2672+
shared_data->last_read_index = index;
2673+
shared_data->analysis_image_count++;
26782674
shared_analysis_timestamps[index] = zm::chrono::duration_cast<timeval>(packet->timestamp.time_since_epoch());
26792675
} else {
26802676
Debug(3, "Not video, not clearing packets");
@@ -3065,6 +3061,7 @@ int Monitor::OpenDecoder() {
30653061

30663062
mVideoCodecContext = avcodec_alloc_context3(mVideoCodec);
30673063
avcodec_parameters_to_context(mVideoCodecContext, mVideoStream->codecpar);
3064+
mVideoCodecContext->framerate = mVideoStream->r_frame_rate;
30683065

30693066
#ifdef CODEC_FLAG2_FAST
30703067
mVideoCodecContext->flags2 |= CODEC_FLAG2_FAST | CODEC_FLAG_LOW_DELAY;
@@ -3200,6 +3197,7 @@ int Monitor::Decode() {
32003197
if (packet->codec_type != AVMEDIA_TYPE_VIDEO) {
32013198
packet->decoded = true;
32023199
Debug(4, "Not video,probably audio");
3200+
packetqueue.increment_it(decoder_it);
32033201
return 1; // Don't need decode
32043202
}
32053203
}
@@ -3215,9 +3213,10 @@ int Monitor::Decode() {
32153213
) {
32163214
Debug(1, "send_packet %d", packet->image_index);
32173215
int ret;
3218-
do {
3216+
//do {
32193217
ret = packet->send_packet(mVideoCodecContext);
3220-
} while(!ret and !zm_terminate);
3218+
if ( 0>=ret) return -1; //make it sleep?
3219+
//} while(!ret and !zm_terminate);
32213220

32223221
if (ret < 0) {
32233222
// No need to push because it didn't get into the decoder.
@@ -3882,7 +3881,6 @@ int Monitor::Pause() {
38823881
analysis_thread->Stop();
38833882
Debug(1, "Analysis stopped");
38843883
}
3885-
while (ai_queue.size()) ai_queue.pop_front();
38863884

38873885
Debug(1, "Stopping packetqueue");
38883886
// Wake everyone up
@@ -3902,6 +3900,8 @@ int Monitor::Pause() {
39023900
Debug(1, "Joining analysis");
39033901
analysis_thread->Join();
39043902
}
3903+
while (decoder_queue.size()) decoder_queue.pop_front();
3904+
while (ai_queue.size()) ai_queue.pop_front();
39053905

39063906
// Must close event before closing camera because it uses in_streams
39073907
if (close_event_thread.joinable()) {

src/zm_monitorlink_token.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ class Token {
158158
return type_ == rhs.type_ && value_ == rhs.value_;
159159
}
160160

161-
~Token() noexcept = default;
161+
~Token() {
162+
if (monitor_link_)
163+
delete monitor_link_;
164+
165+
}
162166
constexpr void type( TokenType const type ) noexcept {
163167
if ( type != type_ ) {
164168
type_ = type;

src/zm_monitorstream.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -728,36 +728,37 @@ void MonitorStream::runStream() {
728728
}
729729
} // end if (buffered_playback && delayed)
730730

731-
int last_index;
732-
last_index = (last_read_index+1) % monitor->image_buffer_count;
731+
int index = (last_read_index+1) % monitor->image_buffer_count;
733732
int last_count;
733+
int last_index;
734734
std::vector<Image *> *image_buffer;
735735
AVPixelFormat *pixelformats;
736736
if (monitor->ObjectDetection() != Monitor::OBJECT_DETECTION_NONE) {
737737
Debug(4, "Using OBJDETECT");
738738
//if (last_read_index != last_image_count < last_count) {
739739
//if (monitor->shared_data->last_analysis_index != last_read_index+1) {
740740
//}
741-
//last_index = monitor->shared_data->last_analysis_index;
741+
last_index = monitor->shared_data->last_analysis_index;
742742
last_count = monitor->shared_data->analysis_image_count;
743743
image_buffer = &monitor->analysis_image_buffer;
744744
pixelformats = monitor->analysis_image_pixelformats;
745745
} else if ( monitor->Analysing() != Monitor::ANALYSING_NONE) {
746746
Debug(1, "Using ANALYSIS");
747-
//last_index = monitor->shared_data->last_analysis_index;
747+
last_index = monitor->shared_data->last_analysis_index;
748748
last_count = monitor->shared_data->analysis_image_count;
749749
image_buffer = &monitor->analysis_image_buffer;
750750
pixelformats = monitor->analysis_image_pixelformats;
751751
} else {
752752
Debug(1, "Using LIVE");
753-
//last_index = monitor->shared_data->last_write_index;
753+
last_index = monitor->shared_data->last_analysis_index;
754754
last_count = monitor->shared_data->analysis_image_count;
755755
image_buffer = &monitor->analysis_image_buffer;
756756
pixelformats = monitor->analysis_image_pixelformats;
757757
}
758758

759+
Debug(1, "index %d, last_read_index %d, last_index %d, last_image_count %d last_count %d", index, last_read_index, last_index, last_image_count, last_count);
759760
if (
760-
//last_read_index != last_index ||
761+
index != last_index ||
761762
last_image_count < last_count) {
762763
//if (last_read_index != monitor->shared_data->last_write_index || last_image_count < monitor->shared_data->image_count) {
763764
// have a new image to send
@@ -891,7 +892,6 @@ void MonitorStream::runStream() {
891892
}
892893
std::this_thread::sleep_for( MonitorStream::MAX_SLEEP / 10);
893894
continue;
894-
895895
} // end if ( (unsigned int)last_read_index != monitor->shared_data->last_write_index )
896896

897897
FPSeconds sleep_time;

0 commit comments

Comments
 (0)