From 20f6985dff4e4ae34b33eb54e4f30c329a9f5851 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Tue, 24 Apr 2018 14:11:27 -0400 Subject: [PATCH] implement capturing bandwidth --- db/zm_create.sql.in | 1 + db/zm_update-1.31.44.sql | 12 ++++++++++++ src/zm_camera.h | 3 +++ src/zm_ffmpeg_camera.cpp | 2 ++ src/zm_monitor.cpp | 11 +++++++++-- src/zm_monitor.h | 2 ++ web/includes/Monitor.php | 1 + web/skins/classic/views/_monitor_filters.php | 2 +- web/skins/classic/views/console.php | 1 + 9 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 db/zm_update-1.31.44.sql diff --git a/db/zm_create.sql.in b/db/zm_create.sql.in index 9c237b8a0..48d1a2fcd 100644 --- a/db/zm_create.sql.in +++ b/db/zm_create.sql.in @@ -528,6 +528,7 @@ CREATE TABLE `Monitor_Status` ( `Status` enum('Unknown','NotRunning','Running','Connected','Signal') NOT NULL default 'Unknown', `CaptureFPS` DECIMAL(10,2) NOT NULL default 0, `AnalysisFPS` DECIMAL(5,2) NOT NULL default 0, + `CaptureBandwidth` INT NOT NULL default 0, PRIMARY KEY (`MonitorId`) ) ENGINE=MEMORY; -- diff --git a/db/zm_update-1.31.44.sql b/db/zm_update-1.31.44.sql new file mode 100644 index 000000000..3c8a1525a --- /dev/null +++ b/db/zm_update-1.31.44.sql @@ -0,0 +1,12 @@ + +SET @s = (SELECT IF( + (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = DATABASE() + AND table_name = 'Monitor_Status' + AND column_name = 'CaptureBandwidth' + ) > 0, +"SELECT 'Column CaptureBandwidth already exists in Monitor_Status'", +"ALTER TABLE `Monitor_Status` ADD `CaptureBandwidth` INT NOT NULL default 0 AFTER `AnalysisFPS`" +)); + +PREPARE stmt FROM @s; +EXECUTE stmt; diff --git a/src/zm_camera.h b/src/zm_camera.h index 9833e098f..a524fc380 100644 --- a/src/zm_camera.h +++ b/src/zm_camera.h @@ -53,6 +53,8 @@ protected: int contrast; bool capture; bool record_audio; + unsigned int bytes; + public: Camera( unsigned int p_monitor_id, SourceType p_type, unsigned int p_width, unsigned int p_height, int p_colours, int p_subpixelorder, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio ); @@ -74,6 +76,7 @@ public: unsigned int SubpixelOrder() const { return( subpixelorder ); } unsigned int Pixels() const { return( pixels ); } unsigned int ImageSize() const { return( imagesize ); } + unsigned int Bytes() const { return bytes; }; virtual int Brightness( int/*p_brightness*/=-1 ) { return( -1 ); } virtual int Hue( int/*p_hue*/=-1 ) { return( -1 ); } diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index 29db3600d..2216d723b 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -292,6 +292,7 @@ int FfmpegCamera::Capture( Image &image ) { } else { Debug( 4, "Different stream_index %d", packet.stream_index ); } // end if packet.stream_index == mVideoStreamId + bytes += packet.size; zm_av_packet_unref( &packet ); } // end while ! frameComplete return 1; @@ -708,6 +709,7 @@ int FfmpegCamera::CaptureAndRecord( Image &image, timeval recording, char* event } int keyframe = packet.flags & AV_PKT_FLAG_KEY; + bytes += packet.size; dumpPacket(&packet); //Video recording diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 01a67581b..b1ea7be22 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -380,6 +380,7 @@ Monitor::Monitor( ParseEncoderParameters(encoderparams.c_str(), &encoderparamsvec); fps = 0.0; + last_camera_bytes = 0; event_count = 0; image_count = 0; ready_count = warmup_count; @@ -2457,15 +2458,21 @@ int Monitor::Capture() { if ( now != last_fps_time ) { // # of images per interval / the amount of time it took double new_fps = double(fps_report_interval)/(now-last_fps_time); + unsigned int new_camera_bytes = camera->Bytes(); + unsigned int new_capture_bandwidth = (new_camera_bytes - last_camera_bytes)/(now-last_fps_time); + last_camera_bytes = new_camera_bytes; //Info( "%d -> %d -> %d", fps_report_interval, now, last_fps_time ); //Info( "%d -> %d -> %lf -> %lf", now-last_fps_time, fps_report_interval/(now-last_fps_time), double(fps_report_interval)/(now-last_fps_time), fps ); - Info("%s: images:%d - Capturing at %.2lf fps", name, image_count, new_fps); + Info("%s: images:%d - Capturing at %.2lf fps, capturing bandwidth %ubytes/sec", name, image_count, new_fps, new_capture_bandwidth); last_fps_time = now; if ( new_fps != fps ) { fps = new_fps; + db_mutex.lock(); static char sql[ZM_SQL_SML_BUFSIZ]; - snprintf(sql, sizeof(sql), "INSERT INTO Monitor_Status (MonitorId,CaptureFPS) VALUES (%d, %.2lf) ON DUPLICATE KEY UPDATE CaptureFPS = %.2lf", id, fps, fps); + snprintf(sql, sizeof(sql), + "INSERT INTO Monitor_Status (MonitorId,CaptureFPS,CaptureBandwidth) VALUES (%d, %.2lf,%u) ON DUPLICATE KEY UPDATE CaptureFPS = %.2lf, CaptureBandwidth=%u", + id, fps, new_capture_bandwidth, fps, new_capture_bandwidth); if ( mysql_query(&dbconn, sql) ) { Error("Can't run query: %s", mysql_error(&dbconn)); } diff --git a/src/zm_monitor.h b/src/zm_monitor.h index 800dab9db..fd4988ca4 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -288,6 +288,8 @@ protected: bool embed_exif; // Whether to embed Exif data into each image frame or not double fps; + unsigned int last_camera_bytes; + Image delta_image; Image ref_image; Image alarm_image; // Used in creating analysis images, will be initialized in Analysis diff --git a/web/includes/Monitor.php b/web/includes/Monitor.php index 74cb3f528..07e151a07 100644 --- a/web/includes/Monitor.php +++ b/web/includes/Monitor.php @@ -24,6 +24,7 @@ private $defaults = array( private $status_fields = array( 'AnalysisFPS' => null, 'CaptureFPS' => null, + 'CaptureBandwidth' => null, ); private $control_fields = array( 'Name' => '', diff --git a/web/skins/classic/views/_monitor_filters.php b/web/skins/classic/views/_monitor_filters.php index 2ee0e437d..70e1655ea 100644 --- a/web/skins/classic/views/_monitor_filters.php +++ b/web/skins/classic/views/_monitor_filters.php @@ -152,7 +152,7 @@ $html .= htmlSelect( 'Status[]', $status_options, ) ); $html .= ''; - $sql = 'SELECT *,S.Status AS Status, S.CaptureFPS AS CaptureFPS, S.AnalysisFPS AS AnalysisFPS + $sql = 'SELECT *,S.Status AS Status, S.CaptureFPS AS CaptureFPS, S.AnalysisFPS AS AnalysisFPS, S.CaptureBandwidth AS CaptureBandwidth FROM Monitors AS M LEFT JOIN Monitor_Status AS S ON MonitorId=Id ' . ( count($conditions) ? ' WHERE ' . implode(' AND ', $conditions) : '' ).' ORDER BY Sequence ASC'; $monitors = dbFetchAll($sql, null, $values); diff --git a/web/skins/classic/views/console.php b/web/skins/classic/views/console.php index 7565335bb..d4749ab83 100644 --- a/web/skins/classic/views/console.php +++ b/web/skins/classic/views/console.php @@ -275,6 +275,7 @@ if ( $fclass != 'infoText' ) $dot_class=$fclass; $fps_string .= '/' . $monitor['AnalysisFPS']; } if ($fps_string) $fps_string .= ' fps'; + $fps_string .= ' ' . human_filesize($monitor['CaptureBandwidth']).'/s'; echo $fps_string; ?>