diff --git a/src/zm_monitorstream.cpp b/src/zm_monitorstream.cpp index e6929e92f..d33d84107 100644 --- a/src/zm_monitorstream.cpp +++ b/src/zm_monitorstream.cpp @@ -498,7 +498,8 @@ void MonitorStream::runStream() { if ( type == STREAM_JPEG ) fprintf( stdout, "Content-Type: multipart/x-mixed-replace;boundary=ZoneMinderFrame\r\n\r\n" ); - int last_read_index = monitor->image_buffer_count; + // point to end which is theoretically not a valid value because all indexes are % image_buffer_count + unsigned int last_read_index = monitor->image_buffer_count; time_t stream_start_time; time( &stream_start_time ); @@ -557,6 +558,8 @@ void MonitorStream::runStream() { Debug( 2, "Assigned temporary buffer" ); } } + } else { + Debug(2, "Not using playback_buffer"); } // end if connkey & playback_buffer float max_secs_since_last_sent_frame = 10.0; //should be > keep alive amount (5 secs) @@ -657,20 +660,22 @@ Debug(2, "Have checking command Queue for connkey: %d", connkey ); delayed = false; replay_rate = ZM_RATE_BASE; } - } - if ( (unsigned int)last_read_index != monitor->shared_data->last_write_index ) { - int index = monitor->shared_data->last_write_index%monitor->image_buffer_count; + } // end if ( buffered_playback && delayed ) + if ( last_read_index != monitor->shared_data->last_write_index ) { + int index = monitor->shared_data->last_write_index % monitor->image_buffer_count; // % shouldn't be neccessary last_read_index = monitor->shared_data->last_write_index; - //Debug( 1, "%d: %x - %x", index, image_buffer[index].image, image_buffer[index].image->buffer ); + //Debug( 1, "index: %d: frame_mod: %d frame count: %d", index, frame_mod, frame_count ); if ( (frame_mod == 1) || ((frame_count%frame_mod) == 0) ) { if ( !paused && !delayed ) { // Send the next frame Monitor::Snapshot *snap = &monitor->image_buffer[index]; + //Debug(2, "sending Frame."); if ( !sendFrame( snap->image, snap->timestamp ) ) { Debug(2, "sendFrame failed, quiting."); zm_terminate = true; } + // Perhaps we should use NOW instead. memcpy( &last_frame_timestamp, snap->timestamp, sizeof(last_frame_timestamp) ); //frame_sent = true; @@ -706,17 +711,22 @@ Debug(2, "Have checking command Queue for connkey: %d", connkey ); } } // end if buffered playback frame_count++; - } + } // end if ( (unsigned int)last_read_index != monitor->shared_data->last_write_index ) + unsigned long sleep_time = (unsigned long)((1000000 * ZM_RATE_BASE)/((base_fps?base_fps:1)*abs(replay_rate*2))); - Debug(2, "Sleeping for (%d)", sleep_time); - usleep( (unsigned long)((1000000 * ZM_RATE_BASE)/((base_fps?base_fps:1)*abs(replay_rate*2))) ); + Debug(4, "Sleeping for (%d)", sleep_time); + usleep( sleep_time ); if ( ttl ) { if ( (now.tv_sec - stream_start_time) > ttl ) { Debug(2, "now(%d) - start(%d) > ttl(%d) break", now.tv_sec, stream_start_time, ttl); break; } } - if ( (TV_2_FLOAT( now ) - last_frame_sent) > max_secs_since_last_sent_frame ) { + if ( ! last_frame_sent ) { + // If we didn't capture above, because frame_mod was bad? Then last_frame_sent will not have a value. + last_frame_sent = now.tv_sec; + Warning( "no last_frame_sent. Shouldn't happen. frame_mod was (%d) frame_count (%d) ", frame_mod, frame_count ); + } else if ( (TV_2_FLOAT( now ) - last_frame_sent) > max_secs_since_last_sent_frame ) { Error( "Terminating, last frame sent time %f secs more than maximum of %f", TV_2_FLOAT( now ) - last_frame_sent, max_secs_since_last_sent_frame ); break; }