diff --git a/src/zm_eventstream.cpp b/src/zm_eventstream.cpp index 28a0e50d6..34f693f36 100644 --- a/src/zm_eventstream.cpp +++ b/src/zm_eventstream.cpp @@ -68,17 +68,17 @@ bool EventStream::loadInitialEventData(int monitor_id, SystemTimePoint event_tim curr_frame_id = 1; // curr_frame_id is 1-based if (event_time >= event_data->start_time) { Debug(2, "event time is after event start"); - for (unsigned int i = 0; i < event_data->frame_count; i++) { + for (int i = 0; i < event_data->frame_count; i++) { //Info( "eft %d > et %d", event_data->frames[i].timestamp, event_time ); if (event_data->frames[i].timestamp >= event_time) { curr_frame_id = i + 1; - Debug(3, "Set curr_stream_time: %.2f, curr_frame_id: %ld", + Debug(3, "Set curr_stream_time: %.2f, curr_frame_id: %d", FPSeconds(curr_stream_time.time_since_epoch()).count(), curr_frame_id); break; } } // end foreach frame - Debug(3, "Skipping %ld frames", event_data->frame_count); + Debug(3, "Skipping %d frames", event_data->frame_count); } else { Warning("Requested an event time less than the start of the event. event_time %" PRIi64 " < start_time %" PRIi64, static_cast(std::chrono::duration_cast(event_time.time_since_epoch()).count()), @@ -90,13 +90,13 @@ bool EventStream::loadInitialEventData(int monitor_id, SystemTimePoint event_tim bool EventStream::loadInitialEventData( uint64_t init_event_id, - unsigned int init_frame_id + int init_frame_id ) { loadEventData(init_event_id); if ( init_frame_id ) { if ( init_frame_id >= event_data->frame_count ) { - Error("Invalid frame id specified. %d > %lu", init_frame_id, event_data->frame_count); + Error("Invalid frame id specified. %d > %d", init_frame_id, event_data->frame_count); curr_stream_time = event_data->start_time; curr_frame_id = 1; } else { @@ -225,20 +225,24 @@ bool EventStream::loadEventData(uint64_t event_id) { sql = stringtf("SELECT `FrameId`, unix_timestamp(`TimeStamp`), `Delta` " "FROM `Frames` WHERE `EventId` = %" PRIu64 " ORDER BY `FrameId` ASC", event_id); - result = zmDbFetch(sql); if (!result) { exit(-1); } event_data->n_frames = mysql_num_rows(result); - event_data->frames = new FrameData[event_data->frame_count]; + if (event_data->frame_count < event_data->n_frames) { + event_data->frame_count = event_data->n_frames; + Warning("Event %" PRId64 " has more frames in the Frames table (%d) than in the Event record (%d)", + event_data->event_id, event_data->n_frames, event_data->frame_count); + } + int last_id = 0; SystemTimePoint last_timestamp = event_data->start_time; Microseconds last_delta = Seconds(0); - while ( ( dbrow = mysql_fetch_row(result) ) ) { + while ((dbrow = mysql_fetch_row(result))) { int id = atoi(dbrow[0]); //timestamp = atof(dbrow[1]); Microseconds delta = std::chrono::duration_cast(FPSeconds(atof(dbrow[2]))); @@ -280,7 +284,7 @@ bool EventStream::loadEventData(uint64_t event_id) { // Incomplete events might not have any frame data event_data->last_frame_id = last_id; - if ( mysql_errno(&dbconn) ) { + if (mysql_errno(&dbconn)) { Error("Can't fetch row: %s", mysql_error(&dbconn)); exit(mysql_errno(&dbconn)); } @@ -310,7 +314,7 @@ bool EventStream::loadEventData(uint64_t event_id) { else curr_stream_time = event_data->frames[event_data->last_frame_id-1].timestamp; } - Debug(2, "Event: %" PRIu64 ", Frames: %ld, Last Frame ID (%ld, Duration: %.2f s Frames Duration: %.2f s", + Debug(2, "Event: %" PRIu64 ", Frames: %d, Last Frame ID (%d, Duration: %.2f s Frames Duration: %.2f s", event_data->event_id, event_data->frame_count, event_data->last_frame_id, @@ -342,12 +346,12 @@ void EventStream::processCommand(const CmdMsg *msg) { if ( (mode == MODE_SINGLE || mode == MODE_NONE) && - ((unsigned int)curr_frame_id == event_data->last_frame_id) + (curr_frame_id == event_data->last_frame_id) ) { Debug(1, "Was in single or no replay mode, and at last frame, so jumping to 1st frame"); curr_frame_id = 1; } else { - Debug(1, "mode is %s, current frame is %ld, frame count is %ld, last frame id is %ld", + Debug(1, "mode is %s, current frame is %d, frame count is %d, last frame id is %d", StreamMode_Strings[(int) mode].c_str(), curr_frame_id, event_data->frame_count, @@ -404,9 +408,9 @@ void EventStream::processCommand(const CmdMsg *msg) { paused = true; replay_rate = ZM_RATE_BASE; step = 1; - if ( (unsigned int)curr_frame_id < event_data->last_frame_id ) + if (curr_frame_id < event_data->last_frame_id) curr_frame_id += 1; - Debug(1, "Got SLOWFWD command new frame id %ld", curr_frame_id); + Debug(1, "Got SLOWFWD command new frame id %d", curr_frame_id); break; case CMD_SLOWREV : paused = true; @@ -414,7 +418,7 @@ void EventStream::processCommand(const CmdMsg *msg) { step = -1; curr_frame_id -= 1; if ( curr_frame_id < 1 ) curr_frame_id = 1; - Debug(1, "Got SLOWREV command new frame id %ld", curr_frame_id); + Debug(1, "Got SLOWREV command new frame id %d", curr_frame_id); break; case CMD_FASTREV : Debug(1, "Got FAST REV command"); @@ -538,12 +542,12 @@ void EventStream::processCommand(const CmdMsg *msg) { if ( curr_frame_id < 1 ) { curr_frame_id = 1; - } else if ( (unsigned long)curr_frame_id > event_data->last_frame_id ) { + } else if (curr_frame_id > event_data->last_frame_id) { curr_frame_id = event_data->last_frame_id; } curr_stream_time = event_data->frames[curr_frame_id-1].timestamp; - Debug(1, "Got SEEK command, to %f s (new current frame id: %ld offset %f s)", + Debug(1, "Got SEEK command, to %f s (new current frame id: %d offset %f s)", FPSeconds(offset).count(), curr_frame_id, FPSeconds(event_data->frames[curr_frame_id - 1].offset).count()); @@ -615,11 +619,11 @@ bool EventStream::checkEventLoaded() { sql = stringtf( "SELECT `Id` FROM `Events` WHERE `MonitorId` = %d AND `Id` < %" PRIu64 " ORDER BY `Id` DESC LIMIT 1", event_data->monitor_id, event_data->event_id); - } else if ( (unsigned int)curr_frame_id > event_data->last_frame_id ) { + } else if (curr_frame_id > event_data->last_frame_id) { if (event_data->end_time.time_since_epoch() == Seconds(0)) { // We are viewing an in-process event, so just reload it. loadEventData(event_data->event_id); - if ( (unsigned int)curr_frame_id > event_data->last_frame_id ) + if (curr_frame_id > event_data->last_frame_id) curr_frame_id = event_data->last_frame_id; return false; } @@ -628,7 +632,7 @@ bool EventStream::checkEventLoaded() { event_data->monitor_id, event_data->event_id); } else { // No event change required - Debug(3, "No event change required, as curr frame %ld <=> event frames %lu", + Debug(3, "No event change required, as curr frame %d <=> event frames %d", curr_frame_id, event_data->frame_count); return false; } @@ -662,7 +666,7 @@ bool EventStream::checkEventLoaded() { curr_frame_id = event_data->last_frame_id; else curr_frame_id = 1; - Debug(2, "New frame id = %ld", curr_frame_id); + Debug(2, "New frame id = %d", curr_frame_id); start = std::chrono::steady_clock::now(); return true; } else { @@ -689,13 +693,13 @@ bool EventStream::checkEventLoaded() { Image * EventStream::getImage( ) { std::string path = stringtf(staticConfig.capture_file_format.c_str(), event_data->path.c_str(), curr_frame_id); - Debug(2, "EventStream::getImage path(%s) from %s frame(%ld) ", path.c_str(), event_data->path.c_str(), curr_frame_id); + Debug(2, "EventStream::getImage path(%s) from %s frame(%d) ", path.c_str(), event_data->path.c_str(), curr_frame_id); Image *image = new Image(path.c_str()); return image; } bool EventStream::sendFrame(Microseconds delta_us) { - Debug(2, "Sending frame %ld", curr_frame_id); + Debug(2, "Sending frame %d", curr_frame_id); std::string filepath; struct stat filestat = {}; @@ -881,7 +885,7 @@ void EventStream::runStream() { if ( !paused ) { // Figure out if we should send this frame - Debug(3, "not paused at cur_frame_id (%ld-1) mod frame_mod(%d)", curr_frame_id, frame_mod); + Debug(3, "not paused at cur_frame_id (%d-1) mod frame_mod(%d)", curr_frame_id, frame_mod); // If we are streaming and this frame is due to be sent // frame mod defaults to 1 and if we are going faster than max_fps will get multiplied by 2 // so if it is 2, then we send every other frame, if is it 4 then every fourth frame, etc. @@ -983,7 +987,7 @@ void EventStream::runStream() { if ( (mode == MODE_SINGLE) && ( (curr_frame_id < 1 ) || - ((unsigned int)curr_frame_id >= event_data->frame_count) + (curr_frame_id >= event_data->frame_count) ) ) { Debug(2, "Have mode==MODE_SINGLE and at end of event, looping back to start"); @@ -992,7 +996,7 @@ void EventStream::runStream() { start = now; } - if ((unsigned int)curr_frame_id <= event_data->frame_count) { + if (curr_frame_id <= event_data->frame_count) { frame_data = &event_data->frames[curr_frame_id-1]; // frame_data->delta is the time since last frame as a float in seconds @@ -1032,7 +1036,7 @@ void EventStream::runStream() { } } // end if need to sleep } else { - Debug(1, "invalid curr_frame_id %ld !< %lu", curr_frame_id, event_data->frame_count); + Debug(1, "invalid curr_frame_id %d !< %d", curr_frame_id, event_data->frame_count); } // end if not at end of event } else { // Paused @@ -1110,12 +1114,12 @@ bool EventStream::send_file(const std::string &filepath) { } if (!filestat.st_size) { fclose(fdj); /* Close the file handle */ - Info("File size is zero. Unable to send raw frame %ld: %s", curr_frame_id, strerror(errno)); + Info("File size is zero. Unable to send raw frame %d: %s", curr_frame_id, strerror(errno)); return false; } if (0 > fprintf(stdout, "Content-Length: %d\r\n\r\n", (int)filestat.st_size)) { fclose(fdj); /* Close the file handle */ - Info("Unable to send raw frame %ld: %s", curr_frame_id, strerror(errno)); + Info("Unable to send raw frame %d: %s", curr_frame_id, strerror(errno)); return false; } int rc = zm_sendfile(fileno(stdout), fileno(fdj), 0, (int)filestat.st_size); @@ -1124,7 +1128,7 @@ bool EventStream::send_file(const std::string &filepath) { fclose(fdj); /* Close the file handle */ return true; } - Warning("Unable to send raw frame %ld: %s rc %d != %d", + Warning("Unable to send raw frame %d: %s rc %d != %d", curr_frame_id, strerror(errno), rc, (int)filestat.st_size); #endif @@ -1134,7 +1138,7 @@ bool EventStream::send_file(const std::string &filepath) { int img_buffer_size = fread(img_buffer, 1, sizeof(temp_img_buffer), fdj); fclose(fdj); /* Close the file handle */ if ( !img_buffer_size ) { - Info("Unable to read raw frame %ld: %s", curr_frame_id, strerror(errno)); + Info("Unable to read raw frame %d: %s", curr_frame_id, strerror(errno)); return false; } @@ -1143,13 +1147,13 @@ bool EventStream::send_file(const std::string &filepath) { bool EventStream::send_buffer(uint8_t* buffer, int size) { if ( 0 > fprintf(stdout, "Content-Length: %d\r\n\r\n", size) ) { - Info("Unable to send raw frame %ld: %s", curr_frame_id, strerror(errno)); + Info("Unable to send raw frame %d: %s", curr_frame_id, strerror(errno)); return false; } int rc = fwrite(buffer, size, 1, stdout); if ( 1 != rc ) { - Error("Unable to send raw frame %ld: %s %d", curr_frame_id, strerror(errno), rc); + Error("Unable to send raw frame %d: %s %d", curr_frame_id, strerror(errno), rc); return false; } return true; @@ -1157,7 +1161,7 @@ bool EventStream::send_buffer(uint8_t* buffer, int size) { void EventStream::setStreamStart( uint64_t init_event_id, - unsigned int init_frame_id=0) { + int init_frame_id=0) { loadInitialEventData(init_event_id, init_frame_id); } // end void EventStream::setStreamStart(init_event_id,init_frame_id=0) diff --git a/src/zm_eventstream.h b/src/zm_eventstream.h index 969e6725d..093bcd0eb 100644 --- a/src/zm_eventstream.h +++ b/src/zm_eventstream.h @@ -49,9 +49,9 @@ class EventStream : public StreamBase { struct EventData { uint64_t event_id; unsigned int monitor_id; - unsigned long storage_id; - unsigned long frame_count; // Value of Frames column in Event - unsigned long last_frame_id; // Highest frame id known about. Can be < frame_count in incomplete events + unsigned int storage_id; + int frame_count; // Value of Frames column in Event + int last_frame_id; // Highest frame id known about. Can be < frame_count in incomplete events SystemTimePoint start_time; SystemTimePoint end_time; Microseconds duration; @@ -73,7 +73,7 @@ class EventStream : public StreamBase { StreamMode mode; bool forceEventChange; - long curr_frame_id; + int curr_frame_id; SystemTimePoint curr_stream_time; bool send_frame; TimePoint start; // clock time when started the event @@ -82,7 +82,7 @@ class EventStream : public StreamBase { protected: bool loadEventData(uint64_t event_id); - bool loadInitialEventData(uint64_t init_event_id, unsigned int init_frame_id); + bool loadInitialEventData(uint64_t init_event_id, int init_frame_id); bool loadInitialEventData(int monitor_id, SystemTimePoint event_time); bool checkEventLoaded(); @@ -118,7 +118,7 @@ class EventStream : public StreamBase { ffmpeg_input = nullptr; } } - void setStreamStart(uint64_t init_event_id, unsigned int init_frame_id); + void setStreamStart(uint64_t init_event_id, int init_frame_id); void setStreamStart(int monitor_id, time_t event_time); void setStreamMode(StreamMode p_mode) { mode = p_mode; } void runStream() override;