From d1886b5536cd03d77f4e73af10460bf8c0777e51 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 27 Nov 2017 14:57:24 -0500 Subject: [PATCH] wip --- src/zm_analysis_thread.cpp | 4 +-- src/zm_monitor.cpp | 61 +++++++++++++++++++++----------------- src/zm_packetqueue.cpp | 8 ++--- src/zm_videostore.cpp | 15 ++++++---- 4 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/zm_analysis_thread.cpp b/src/zm_analysis_thread.cpp index 8d2c14551..1efee5999 100644 --- a/src/zm_analysis_thread.cpp +++ b/src/zm_analysis_thread.cpp @@ -36,10 +36,10 @@ int AnalysisThread::run() { } if ( !monitor->Analyse() ) { -//Debug(4, "Sleeping for %d", monitor->Active()?ZM_SAMPLE_RATE:ZM_SUSPENDED_RATE); +Debug(2, "Sleeping for %d", monitor->Active()?ZM_SAMPLE_RATE:ZM_SUSPENDED_RATE); usleep(monitor->Active()?ZM_SAMPLE_RATE:ZM_SUSPENDED_RATE); } else if ( analysis_rate ) { -//Debug(4, "Sleeping for %d", analysis_rate); +Debug(2, "Sleeping for %d", analysis_rate); usleep(analysis_rate); } diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 7c7b85922..2995a2259 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -799,10 +799,10 @@ double Monitor::GetFPS() const { useconds_t Monitor::GetAnalysisRate() { capture_fps = GetFPS(); if ( !analysis_fps_limit ) { - return( 0 ); + return 0; } else if ( analysis_fps_limit > capture_fps ) { Warning( "Analysis fps (%.2f) is greater than capturing fps (%.2f)", analysis_fps_limit, capture_fps ); - return( 0 ); + return 0; } else { return( ( 1000000 / analysis_fps_limit ) - ( 1000000 / capture_fps ) ); } @@ -1241,14 +1241,23 @@ bool Monitor::Analyse() { // If do have an event, then analysis_it should point to the head of the queue, because we would have emptied it on event creation. unsigned int index = ( shared_data->last_read_index + 1 ) % image_buffer_count; + Debug(2, "Analysis index (%d), last_Write(%d)", index, shared_data->last_write_index ); + if ( analysis_it == packetqueue.pktQueue.end() ) { + Debug(2, "Analysis index (%d), last_Write(%d), at end of queue", index, shared_data->last_write_index ); + } // Move to next packet. - while ( ( index != shared_data->last_write_index ) && ( analysis_it != packetqueue.pktQueue.end() ) ) { + while ( ( index != shared_data->last_write_index ) && ( *analysis_it != packetqueue.pktQueue.back() ) ) { ++analysis_it; ZMPacket *snap = *analysis_it; struct timeval *timestamp = snap->timestamp; Image *snap_image = snap->image; Debug(2, "Analysing image (%d)", snap->image_index ); + if ( snap->image_index == -1 ) { + Debug(2, "skipping because audio"); + continue; + } + int last_section_mod = 0; @@ -1306,7 +1315,9 @@ bool Monitor::Analyse() { shared_data->active = signal; ref_image = *snap_image; - } else if ( signal ) { + }// else + + if ( signal ) { if ( Active() && (function == MODECT || function == MOCORD) ) { Debug(3, "signal and active and modect"); Event::StringSet zoneSet; @@ -1466,12 +1477,12 @@ bool Monitor::Analyse() { } // analsys_images or record stats mutex.lock(); - snap = packetqueue.popPacket(); + ZMPacket *pack = packetqueue.popPacket(); mutex.unlock(); - event->AddPacket( snap, score ); - if ( snap->image_index == -1 ) { - delete snap; - snap = NULL; + event->AddPacket( pack, score ); + if ( pack->image_index == -1 ) { + delete pack; + pack = NULL; } if ( noteSetMap.size() > 0 ) @@ -1479,28 +1490,28 @@ bool Monitor::Analyse() { } else if ( state == ALERT ) { // Alert means this frame has no motion, but we were alarmed and are still recording. mutex.lock(); - snap = packetqueue.popPacket(); + ZMPacket *pack = packetqueue.popPacket(); mutex.unlock(); - event->AddPacket( snap, score ); - if ( snap->image_index == -1 ) { - delete snap; - snap = NULL; + event->AddPacket( pack, score ); + if ( pack->image_index == -1 ) { + delete pack; + pack = NULL; } if ( noteSetMap.size() > 0 ) event->updateNotes( noteSetMap ); } else if ( state == TAPE ) { if ( !(image_count%(frame_skip+1)) ) { mutex.lock(); - snap = packetqueue.popPacket(); + ZMPacket *pack = packetqueue.popPacket(); mutex.unlock(); if ( config.bulk_frame_interval > 1 ) { - event->AddPacket( snap, (event->Frames()AddPacket( pack, (event->Frames()AddPacket( snap ); + event->AddPacket( pack ); } - if ( snap->image_index == -1 ) { - delete snap; - snap = NULL; + if ( pack->image_index == -1 ) { + delete pack; + pack = NULL; } } } @@ -1508,7 +1519,7 @@ bool Monitor::Analyse() { ref_image.Blend( *snap_image, ( state==ALARM ? alarm_ref_blend_perc : ref_blend_perc ) ); } last_signal = signal; - } // end if signal change or signal + } // end if signal } else { Debug(3,"Not ready?"); @@ -1525,11 +1536,10 @@ bool Monitor::Analyse() { shared_data->last_read_index = snap->image_index; shared_data->last_read_time = now.tv_sec; + analysis_image_count++; } // end while not at end of packetqueue //mutex.unlock(); - analysis_image_count++; - return true; } @@ -2759,7 +2769,7 @@ int Monitor::Capture() { } ZMPacket *packet = &image_buffer[index]; - Debug(2,"Reset index(%d)", index ); + Debug(2,"Reset index(%d) of (%d)", index, image_buffer_count ); packet->reset(); Image* capture_image = packet->image; int captureResult = 0; @@ -2802,11 +2812,8 @@ int Monitor::Capture() { // Only queue if we have some video packets in there. if ( packetqueue.video_packet_count || event ) { // Need to copy it into another ZMPacket. - Debug(2, "Copyingg packet"); ZMPacket *audio_packet = new ZMPacket( *packet ); - Debug(2, "Copyingg packet"); audio_packet->codec_type = camera->get_AudioStream()->codecpar->codec_type; - //packet->decode( camera->get_AudioCodecContext() ); Debug(2, "Queueing packet"); packetqueue.queuePacket( audio_packet ); } diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index 10a66c086..f1ce41ec7 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -81,13 +81,13 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream // Might not be starting with a keyframe, but should always start with a keyframe if ( frames_to_keep ) { - Debug(3, "Hit end of queue, still need (%d) video keyframes", frames_to_keep ); + Debug(4, "Hit end of queue, still need (%d) video keyframes", frames_to_keep ); } else { if ( it != pktQueue.rend() ) { - Debug(2, "Not rend"); + Debug(4, "Not rend"); ZMPacket *zm_packet = *it; - Debug(2, "packet %x %d", zm_packet, zm_packet->image_index); + Debug(4, "packet %x %d", zm_packet, zm_packet->image_index); AVPacket *av_packet = &(zm_packet->packet); while ( @@ -96,7 +96,7 @@ unsigned int zm_packetqueue::clearQueue( unsigned int frames_to_keep, int stream (( av_packet->stream_index != stream_id ) || ! ( av_packet->flags & AV_PKT_FLAG_KEY )) ) { zm_packet = *it; - Debug(2, "packet %x %d", zm_packet, zm_packet->image_index); + Debug(4, "packet %x %d", zm_packet, zm_packet->image_index); ++it; av_packet = &( (*it)->packet ); } diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp index ab7ff5f1e..3356466bb 100644 --- a/src/zm_videostore.cpp +++ b/src/zm_videostore.cpp @@ -86,10 +86,9 @@ VideoStore::VideoStore( video_in_stream_index = video_in_stream->index; #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0) video_in_ctx = avcodec_alloc_context3(NULL); - Debug(2, "copy to context"); + Debug(2, "copy to video_in_context"); avcodec_parameters_to_context(video_in_ctx, video_in_stream->codecpar); - Debug(2, "dump to context"); zm_dump_codecpar( video_in_stream->codecpar ); //video_in_ctx.codec_id = video_in_stream->codecpar.codec_id; #else @@ -138,12 +137,10 @@ Debug(2,"Copied video context from input stream"); avcodec_copy_context( video_out_ctx, video_in_ctx ); #endif video_out_ctx->time_base = (AVRational){1, 1000000}; // microseconds as base frame rate - // Same codec, just copy the packets, otherwise we have to decode/encode - video_out_codec = (AVCodec *)video_in_ctx->codec; // Only set orientation if doing passthrough, otherwise the frame image will be rotated Monitor::Orientation orientation = monitor->getOrientation(); - Debug(3, "Have orientation"); if ( orientation ) { + Debug(3, "Have orientation"); if ( orientation == Monitor::ROTATE_0 ) { } else if ( orientation == Monitor::ROTATE_90 ) { dsr = av_dict_set(&video_out_stream->metadata, "rotate", "90", 0); @@ -175,6 +172,14 @@ Debug(2,"Copied video context from input stream"); default: break; } + // Same codec, just copy the packets, otherwise we have to decode/encode + video_out_codec = avcodec_find_encoder(AV_CODEC_ID_H264); + if ( (ret = avcodec_open2(video_out_ctx, video_out_codec, NULL)) < 0 ) { + Warning("Can't open video codec (%s)! %s, trying h264", + video_out_codec->name, + av_make_error_string(ret).c_str() + ); + } } else {