From f43507dce0f505060da55f544f9cb4130b947bf1 Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 7 Feb 2021 19:12:39 +0100 Subject: [PATCH] Use the power of smart pointers to manage Monitor instances --- src/zm_analysis_thread.cpp | 6 +- src/zm_analysis_thread.h | 5 +- src/zm_camera.cpp | 18 +---- src/zm_camera.h | 11 ++- src/zm_curl_camera.cpp | 4 +- src/zm_curl_camera.h | 2 +- src/zm_eventstream.cpp | 1 - src/zm_eventstream.h | 4 -- src/zm_ffmpeg_camera.cpp | 4 +- src/zm_ffmpeg_camera.h | 2 +- src/zm_fifo.cpp | 2 +- src/zm_file_camera.cpp | 4 +- src/zm_file_camera.h | 2 +- src/zm_libvlc_camera.cpp | 4 +- src/zm_libvlc_camera.h | 2 +- src/zm_libvnc_camera.cpp | 4 +- src/zm_libvnc_camera.h | 2 +- src/zm_local_camera.cpp | 4 +- src/zm_local_camera.h | 2 +- src/zm_monitor.cpp | 79 ++++++++++----------- src/zm_monitor.h | 13 ++-- src/zm_remote_camera.cpp | 4 +- src/zm_remote_camera.h | 2 +- src/zm_remote_camera_http.cpp | 10 +-- src/zm_remote_camera_http.h | 2 +- src/zm_remote_camera_nvsocket.cpp | 6 +- src/zm_remote_camera_nvsocket.h | 2 +- src/zm_remote_camera_rtsp.cpp | 8 +-- src/zm_remote_camera_rtsp.h | 2 +- src/zm_rtsp_server_adts_source.cpp | 4 +- src/zm_rtsp_server_adts_source.h | 6 +- src/zm_rtsp_server_device_source.cpp | 4 +- src/zm_rtsp_server_device_source.h | 6 +- src/zm_rtsp_server_h264_device_source.cpp | 8 +-- src/zm_rtsp_server_h264_device_source.h | 16 ++--- src/zm_rtsp_server_thread.cpp | 4 +- src/zm_rtsp_server_thread.h | 5 +- src/zm_stream.cpp | 6 -- src/zm_stream.h | 4 +- src/zmc.cpp | 83 +++++++++++------------ src/zmu.cpp | 9 +-- 41 files changed, 166 insertions(+), 200 deletions(-) diff --git a/src/zm_analysis_thread.cpp b/src/zm_analysis_thread.cpp index 8ad67b6ce..a7002fc23 100644 --- a/src/zm_analysis_thread.cpp +++ b/src/zm_analysis_thread.cpp @@ -2,10 +2,8 @@ #include "zm_signal.h" -AnalysisThread::AnalysisThread(Monitor *p_monitor) { - monitor = p_monitor; - terminate = false; -} +AnalysisThread::AnalysisThread(std::shared_ptr monitor) : + monitor(std::move(monitor)), terminate(false) {} AnalysisThread::~AnalysisThread() { Debug(2, "THREAD: deleteing analysis thread"); diff --git a/src/zm_analysis_thread.h b/src/zm_analysis_thread.h index 1adeac3a8..70d0cd182 100644 --- a/src/zm_analysis_thread.h +++ b/src/zm_analysis_thread.h @@ -3,14 +3,15 @@ #include "zm_monitor.h" #include "zm_thread.h" +#include class AnalysisThread : public Thread { private: + std::shared_ptr monitor; bool terminate; - Monitor *monitor; public: - explicit AnalysisThread(Monitor *); + explicit AnalysisThread(std::shared_ptr monitor); ~AnalysisThread(); int run(); diff --git a/src/zm_camera.cpp b/src/zm_camera.cpp index 6234b096d..a4ebf485e 100644 --- a/src/zm_camera.cpp +++ b/src/zm_camera.cpp @@ -22,7 +22,7 @@ #include "zm_monitor.h" Camera::Camera( - unsigned int p_monitor_id, + const Monitor *monitor, SourceType p_type, unsigned int p_width, unsigned int p_height, @@ -35,8 +35,7 @@ Camera::Camera( bool p_capture, bool p_record_audio ) : - monitor_id(p_monitor_id), - monitor(nullptr), + monitor(monitor), type(p_type), width(p_width), height(p_height), @@ -62,7 +61,7 @@ Camera::Camera( imagesize = height * linesize; Debug(2, "New camera id: %d width: %d line size: %d height: %d colours: %d subpixelorder: %d capture: %d", - monitor_id, width, linesize, height, colours, subpixelorder, capture); + monitor->Id(), width, linesize, height, colours, subpixelorder, capture); } Camera::~Camera() { @@ -74,17 +73,6 @@ Camera::~Camera() { } } -Monitor *Camera::getMonitor() { - if ( ! monitor ) - monitor = Monitor::Load(monitor_id, false, Monitor::QUERY); - return monitor; -} - -void Camera::setMonitor(Monitor *p_monitor) { - monitor = p_monitor; - monitor_id = monitor->Id(); -} - AVStream *Camera::get_VideoStream() { if ( !mVideoStream ) { if ( !mFormatContext ) diff --git a/src/zm_camera.h b/src/zm_camera.h index 718ce3a48..c092bcc0e 100644 --- a/src/zm_camera.h +++ b/src/zm_camera.h @@ -21,10 +21,10 @@ #define ZM_CAMERA_H #include "zm_image.h" +#include "zm_monitor.h" #include #include -class Monitor; class ZMPacket; // @@ -35,8 +35,7 @@ class Camera { protected: typedef enum { LOCAL_SRC, REMOTE_SRC, FILE_SRC, FFMPEG_SRC, LIBVLC_SRC, CURL_SRC, VNC_SRC } SourceType; - unsigned int monitor_id; - Monitor * monitor; // Null on instantiation, set as soon as possible. + const Monitor *monitor; SourceType type; unsigned int width; unsigned int linesize; @@ -62,7 +61,7 @@ protected: public: Camera( - unsigned int p_monitor_id, + const Monitor* monitor, SourceType p_type, unsigned int p_width, unsigned int p_height, @@ -77,9 +76,7 @@ public: ); virtual ~Camera(); - unsigned int getId() const { return monitor_id; } - Monitor *getMonitor(); - void setMonitor( Monitor *p_monitor ); + unsigned int getId() const { return monitor->Id(); } SourceType Type() const { return type; } bool IsLocal() const { return type == LOCAL_SRC; } bool IsRemote() const { return type == REMOTE_SRC; } diff --git a/src/zm_curl_camera.cpp b/src/zm_curl_camera.cpp index 48df87da0..9baf21572 100644 --- a/src/zm_curl_camera.cpp +++ b/src/zm_curl_camera.cpp @@ -69,8 +69,8 @@ void bind_libcurl_symbols() { *(void**) (&curl_easy_cleanup_f) = dlsym(curl_lib, "curl_easy_cleanup"); } -cURLCamera::cURLCamera( int p_id, const std::string &p_path, const std::string &p_user, const std::string &p_pass, unsigned int p_width, unsigned int p_height, int p_colours, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio ) : - Camera( p_id, CURL_SRC, p_width, p_height, p_colours, ZM_SUBPIX_ORDER_DEFAULT_FOR_COLOUR(p_colours), p_brightness, p_contrast, p_hue, p_colour, p_capture, p_record_audio ), +cURLCamera::cURLCamera( const Monitor* monitor, const std::string &p_path, const std::string &p_user, const std::string &p_pass, unsigned int p_width, unsigned int p_height, int p_colours, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio ) : + Camera( monitor, CURL_SRC, p_width, p_height, p_colours, ZM_SUBPIX_ORDER_DEFAULT_FOR_COLOUR(p_colours), p_brightness, p_contrast, p_hue, p_colour, p_capture, p_record_audio ), mPath( p_path ), mUser( p_user ), mPass ( p_pass ), bTerminate( false ), bReset( false ), mode ( MODE_UNSET ) { diff --git a/src/zm_curl_camera.h b/src/zm_curl_camera.h index 04fe71e32..02174fe33 100644 --- a/src/zm_curl_camera.h +++ b/src/zm_curl_camera.h @@ -61,7 +61,7 @@ protected: pthread_cond_t request_complete_cond; public: - cURLCamera( int p_id, const std::string &path, const std::string &username, const std::string &password, unsigned int p_width, unsigned int p_height, int p_colours, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio ); + cURLCamera( const Monitor* monitor, const std::string &path, const std::string &username, const std::string &password, unsigned int p_width, unsigned int p_height, int p_colours, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio ); ~cURLCamera(); const std::string &Path() const { return( mPath ); } diff --git a/src/zm_eventstream.cpp b/src/zm_eventstream.cpp index 66b8e3b2b..9da70f955 100644 --- a/src/zm_eventstream.cpp +++ b/src/zm_eventstream.cpp @@ -166,7 +166,6 @@ bool EventStream::loadEventData(uint64_t event_id) { if ( !monitor ) { monitor = Monitor::Load(event_data->monitor_id, false, Monitor::QUERY); } else if ( monitor->Id() != event_data->monitor_id ) { - delete monitor; monitor = Monitor::Load(event_data->monitor_id, false, Monitor::QUERY); } if ( !monitor ) { diff --git a/src/zm_eventstream.h b/src/zm_eventstream.h index ab30207e5..0a2714186 100644 --- a/src/zm_eventstream.h +++ b/src/zm_eventstream.h @@ -115,10 +115,6 @@ class EventStream : public StreamBase { delete event_data; event_data = nullptr; } - if ( monitor ) { - delete monitor; - monitor = nullptr; - } if ( storage ) { delete storage; storage = nullptr; diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index b7844b1cb..bd67d5c8c 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -91,7 +91,7 @@ static enum AVPixelFormat find_fmt_by_hw_type(const enum AVHWDeviceType type) { #endif FfmpegCamera::FfmpegCamera( - int p_id, + const Monitor *monitor, const std::string &p_path, const std::string &p_method, const std::string &p_options, @@ -107,7 +107,7 @@ FfmpegCamera::FfmpegCamera( const std::string &p_hwaccel_name, const std::string &p_hwaccel_device) : Camera( - p_id, + monitor, FFMPEG_SRC, p_width, p_height, diff --git a/src/zm_ffmpeg_camera.h b/src/zm_ffmpeg_camera.h index de5fa4735..bac1c42d3 100644 --- a/src/zm_ffmpeg_camera.h +++ b/src/zm_ffmpeg_camera.h @@ -67,7 +67,7 @@ class FfmpegCamera : public Camera { public: FfmpegCamera( - int p_id, + const Monitor *monitor, const std::string &path, const std::string &p_method, const std::string &p_options, diff --git a/src/zm_fifo.cpp b/src/zm_fifo.cpp index c89739096..3e09f8886 100644 --- a/src/zm_fifo.cpp +++ b/src/zm_fifo.cpp @@ -204,7 +204,7 @@ void FifoStream::setStreamStart(const char * path) { void FifoStream::setStreamStart(int monitor_id, const char * format) { char diag_path[PATH_MAX]; - Monitor * monitor = Monitor::Load(monitor_id, false, Monitor::QUERY); + std::shared_ptr monitor = Monitor::Load(monitor_id, false, Monitor::QUERY); if ( !strcmp(format, "reference") ) { snprintf(diag_path, sizeof(diag_path), "%s/diagpipe-r-%d.jpg", diff --git a/src/zm_file_camera.cpp b/src/zm_file_camera.cpp index 5ddaba998..24b9e74ca 100644 --- a/src/zm_file_camera.cpp +++ b/src/zm_file_camera.cpp @@ -23,7 +23,7 @@ #include FileCamera::FileCamera( - int p_id, + const Monitor *monitor, const char *p_path, int p_width, int p_height, @@ -35,7 +35,7 @@ FileCamera::FileCamera( bool p_capture, bool p_record_audio) : Camera( - p_id, + monitor, FILE_SRC, p_width, p_height, diff --git a/src/zm_file_camera.h b/src/zm_file_camera.h index 0d815d842..61d38cfe3 100644 --- a/src/zm_file_camera.h +++ b/src/zm_file_camera.h @@ -32,7 +32,7 @@ protected: public: FileCamera( - int p_id, + const Monitor *monitor, const char *p_path, int p_width, int p_height, diff --git a/src/zm_libvlc_camera.cpp b/src/zm_libvlc_camera.cpp index 22369cb6e..2cab3ee80 100644 --- a/src/zm_libvlc_camera.cpp +++ b/src/zm_libvlc_camera.cpp @@ -99,7 +99,7 @@ void LibvlcUnlockBuffer(void* opaque, void* picture, void *const *planes) { } LibvlcCamera::LibvlcCamera( - int p_id, + const Monitor *monitor, const std::string &p_path, const std::string &p_method, const std::string &p_options, @@ -114,7 +114,7 @@ LibvlcCamera::LibvlcCamera( bool p_record_audio ) : Camera( - p_id, + monitor, LIBVLC_SRC, p_width, p_height, diff --git a/src/zm_libvlc_camera.h b/src/zm_libvlc_camera.h index 6ffc023aa..074989d17 100644 --- a/src/zm_libvlc_camera.h +++ b/src/zm_libvlc_camera.h @@ -56,7 +56,7 @@ protected: libvlc_media_player_t *mLibvlcMediaPlayer; public: - LibvlcCamera( int p_id, const std::string &path, const std::string &p_method, const std::string &p_options, int p_width, int p_height, int p_colours, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio ); + LibvlcCamera( const Monitor *monitor, const std::string &path, const std::string &p_method, const std::string &p_options, int p_width, int p_height, int p_colours, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture, bool p_record_audio ); ~LibvlcCamera(); const std::string &Path() const { return mPath; } diff --git a/src/zm_libvnc_camera.cpp b/src/zm_libvnc_camera.cpp index d64f1d2f2..05a88aa6d 100644 --- a/src/zm_libvnc_camera.cpp +++ b/src/zm_libvnc_camera.cpp @@ -63,7 +63,7 @@ static rfbCredential* GetCredentialsCallback(rfbClient* cl, int credentialType){ } VncCamera::VncCamera( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &host, const std::string &port, const std::string &user, @@ -78,7 +78,7 @@ VncCamera::VncCamera( bool p_capture, bool p_record_audio ) : Camera( - p_monitor_id, + monitor, VNC_SRC, p_width, p_height, diff --git a/src/zm_libvnc_camera.h b/src/zm_libvnc_camera.h index 019e4ef99..4c74e8657 100644 --- a/src/zm_libvnc_camera.h +++ b/src/zm_libvnc_camera.h @@ -28,7 +28,7 @@ protected: std::string mPass; public: VncCamera( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &host, const std::string &port, const std::string &user, diff --git a/src/zm_local_camera.cpp b/src/zm_local_camera.cpp index cada977d0..ba626f106 100644 --- a/src/zm_local_camera.cpp +++ b/src/zm_local_camera.cpp @@ -302,7 +302,7 @@ AVFrame **LocalCamera::capturePictures = nullptr; LocalCamera *LocalCamera::last_camera = nullptr; LocalCamera::LocalCamera( - int p_id, + const Monitor *monitor, const std::string &p_device, int p_channel, int p_standard, @@ -320,7 +320,7 @@ LocalCamera::LocalCamera( bool p_capture, bool p_record_audio, unsigned int p_extras) : - Camera( p_id, LOCAL_SRC, p_width, p_height, p_colours, ZM_SUBPIX_ORDER_DEFAULT_FOR_COLOUR(p_colours), p_brightness, p_contrast, p_hue, p_colour, p_capture, p_record_audio ), + Camera( monitor, LOCAL_SRC, p_width, p_height, p_colours, ZM_SUBPIX_ORDER_DEFAULT_FOR_COLOUR(p_colours), p_brightness, p_contrast, p_hue, p_colour, p_capture, p_record_audio ), device( p_device ), channel( p_channel ), standard( p_standard ), diff --git a/src/zm_local_camera.h b/src/zm_local_camera.h index 9256dac5a..6d3ff2672 100644 --- a/src/zm_local_camera.h +++ b/src/zm_local_camera.h @@ -114,7 +114,7 @@ protected: public: LocalCamera( - int p_id, + const Monitor *monitor, const std::string &device, int p_channel, int p_format, diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 69d3cf6bf..62fe36027 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -650,7 +650,7 @@ Camera * Monitor::getCamera() { int extras = (deinterlacing>>24)&0xff; camera = new LocalCamera( - id, + this, device, channel, format, @@ -672,7 +672,7 @@ Camera * Monitor::getCamera() { } else if ( type == REMOTE ) { if ( protocol == "http" ) { camera = new RemoteCameraHttp( - id, + this, method, host, port, @@ -691,7 +691,7 @@ Camera * Monitor::getCamera() { #if HAVE_LIBAVFORMAT else if ( protocol == "rtsp" ) { camera = new RemoteCameraRtsp( - id, + this, method, host, // Host port, // Port @@ -714,7 +714,7 @@ Camera * Monitor::getCamera() { } } else if ( type == FILE ) { camera = new FileCamera( - id, + this, path.c_str(), camera_width, camera_height, @@ -729,7 +729,7 @@ Camera * Monitor::getCamera() { } else if ( type == FFMPEG ) { #if HAVE_LIBAVFORMAT camera = new FfmpegCamera( - id, + this, path, method, options, @@ -748,7 +748,7 @@ Camera * Monitor::getCamera() { #endif // HAVE_LIBAVFORMAT } else if ( type == NVSOCKET ) { camera = new RemoteCameraNVSocket( - id, + this, host.c_str(), port.c_str(), path.c_str(), @@ -765,7 +765,7 @@ Camera * Monitor::getCamera() { } else if ( type == LIBVLC ) { #if HAVE_LIBVLC camera = new LibvlcCamera( - id, + this, path.c_str(), method, options, @@ -785,7 +785,7 @@ Camera * Monitor::getCamera() { } else if ( type == CURL ) { #if HAVE_LIBCURL camera = new cURLCamera( - id, + this, path.c_str(), user.c_str(), pass.c_str(), @@ -805,7 +805,7 @@ Camera * Monitor::getCamera() { } else if ( type == VNC ) { #if HAVE_LIBVNC camera = new VncCamera( - id, + this, host.c_str(), port.c_str(), user.c_str(), @@ -825,11 +825,10 @@ Camera * Monitor::getCamera() { #endif // HAVE_LIBVNC } // end if type - camera->setMonitor(this); return camera; } // end Monitor::getCamera -Monitor *Monitor::Load(unsigned int p_id, bool load_zones, Purpose purpose) { +std::shared_ptr Monitor::Load(unsigned int p_id, bool load_zones, Purpose purpose) { std::string sql = load_monitor_sql + stringtf(" WHERE Id=%d", p_id); zmDbRow dbrow; @@ -837,11 +836,12 @@ Monitor *Monitor::Load(unsigned int p_id, bool load_zones, Purpose purpose) { Error("Can't use query result: %s", mysql_error(&dbconn)); return nullptr; } - Monitor *monitor = new Monitor(); + + std::shared_ptr monitor = std::make_shared(); monitor->Load(dbrow.mysql_row(), load_zones, purpose); return monitor; -} // end Monitor *Monitor::Load(unsigned int p_id, bool load_zones, Purpose purpose) +} bool Monitor::connect() { Debug(3, "Connecting to monitor. Purpose is %d", purpose); @@ -2376,36 +2376,37 @@ void Monitor::ReloadLinkedMonitors(const char *p_linked_monitors) { } // end if p_linked_monitors } // end void Monitor::ReloadLinkedMonitors(const char *p_linked_monitors) -int Monitor::LoadMonitors(std::string sql, Monitor **&monitors, Purpose purpose) { - +std::vector> Monitor::LoadMonitors(std::string sql, Purpose purpose) { Debug(1, "Loading Monitors with %s", sql.c_str()); MYSQL_RES *result = zmDbFetch(sql.c_str()); - if ( !result ) { + if (!result) { Error("Can't load local monitors: %s", mysql_error(&dbconn)); - return 0; + return {}; } int n_monitors = mysql_num_rows(result); Debug(1, "Got %d monitors", n_monitors); - delete[] monitors; - monitors = new Monitor *[n_monitors]; - for( int i=0; MYSQL_ROW dbrow = mysql_fetch_row(result); i++ ) { - monitors[i] = new Monitor(); - monitors[i]->Load(dbrow, true, purpose); - // need to load zones and set Purpose, 1, purpose); + + std::vector> monitors; + monitors.reserve(n_monitors); + + for (int i = 0; MYSQL_ROW dbrow = mysql_fetch_row(result); i++) { + monitors.emplace_back(); + monitors.back()->Load(dbrow, true, purpose); } - if ( mysql_errno(&dbconn) ) { + + if (mysql_errno(&dbconn)) { Error("Can't fetch row: %s", mysql_error(&dbconn)); mysql_free_result(result); - return 0; + return {}; } mysql_free_result(result); - return n_monitors; -} // end int Monitor::LoadMonitors(std::string sql, Monitor **&monitors, Purpose purpose) + return monitors; +} #if ZM_HAS_V4L -int Monitor::LoadLocalMonitors(const char *device, Monitor **&monitors, Purpose purpose) { +std::vector> Monitor::LoadLocalMonitors(const char *device, Purpose purpose) { std::string sql = load_monitor_sql + " WHERE `Function` != 'None' AND `Type` = 'Local'"; @@ -2413,32 +2414,32 @@ int Monitor::LoadLocalMonitors(const char *device, Monitor **&monitors, Purpose sql += " AND `Device`='" + std::string(device) + "'"; if ( staticConfig.SERVER_ID ) sql += stringtf(" AND `ServerId`=%d", staticConfig.SERVER_ID); - return LoadMonitors(sql, monitors, purpose); -} // end int Monitor::LoadLocalMonitors(const char *device, Monitor **&monitors, Purpose purpose) + return LoadMonitors(sql, purpose); +} #endif // ZM_HAS_V4L -int Monitor::LoadRemoteMonitors(const char *protocol, const char *host, const char *port, const char *path, Monitor **&monitors, Purpose purpose) { +std::vector> Monitor::LoadRemoteMonitors(const char *protocol, const char *host, const char *port, const char *path, Purpose purpose) { std::string sql = load_monitor_sql + " WHERE `Function` != 'None' AND `Type` = 'Remote'"; if ( staticConfig.SERVER_ID ) sql += stringtf(" AND `ServerId`=%d", staticConfig.SERVER_ID); if ( protocol ) sql += stringtf(" AND `Protocol` = '%s' AND `Host` = '%s' AND `Port` = '%s' AND `Path` = '%s'", protocol, host, port, path); - return LoadMonitors(sql, monitors, purpose); -} // end int Monitor::LoadRemoteMonitors + return LoadMonitors(sql, purpose); +} -int Monitor::LoadFileMonitors(const char *file, Monitor **&monitors, Purpose purpose) { +std::vector> Monitor::LoadFileMonitors(const char *file, Purpose purpose) { std::string sql = load_monitor_sql + " WHERE `Function` != 'None' AND `Type` = 'File'"; if ( file[0] ) sql += " AND `Path`='" + std::string(file) + "'"; if ( staticConfig.SERVER_ID ) { sql += stringtf(" AND `ServerId`=%d", staticConfig.SERVER_ID); } - return LoadMonitors(sql, monitors, purpose); -} // end int Monitor::LoadFileMonitors + return LoadMonitors(sql, purpose); +} #if HAVE_LIBAVFORMAT -int Monitor::LoadFfmpegMonitors(const char *file, Monitor **&monitors, Purpose purpose) { +std::vector> Monitor::LoadFfmpegMonitors(const char *file, Purpose purpose) { std::string sql = load_monitor_sql + " WHERE `Function` != 'None' AND `Type` = 'Ffmpeg'"; if ( file[0] ) sql += " AND `Path` = '" + std::string(file) + "'"; @@ -2446,8 +2447,8 @@ int Monitor::LoadFfmpegMonitors(const char *file, Monitor **&monitors, Purpose p if ( staticConfig.SERVER_ID ) { sql += stringtf(" AND `ServerId`=%d", staticConfig.SERVER_ID); } - return LoadMonitors(sql, monitors, purpose); -} // end int Monitor::LoadFfmpegMonitors + return LoadMonitors(sql, purpose); +} #endif // HAVE_LIBAVFORMAT /* Returns 0 on success, even if no new images are available (transient error) diff --git a/src/zm_monitor.h b/src/zm_monitor.h index 6f916c42c..43bb14b77 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -26,6 +26,7 @@ #include "zm_packet.h" #include "zm_packetqueue.h" #include "zm_video.h" +#include #include #include @@ -532,16 +533,16 @@ public: std::vector Groups(); StringVector GroupNames(); - static int LoadMonitors(std::string sql, Monitor **&monitors, Purpose purpose); // Returns # of Monitors loaded, 0 on failure. + static std::vector> LoadMonitors(std::string sql, Purpose purpose); // Returns # of Monitors loaded, 0 on failure. #if ZM_HAS_V4L - static int LoadLocalMonitors(const char *device, Monitor **&monitors, Purpose purpose); + static std::vector> LoadLocalMonitors(const char *device, Purpose purpose); #endif // ZM_HAS_V4L - static int LoadRemoteMonitors(const char *protocol, const char *host, const char*port, const char*path, Monitor **&monitors, Purpose purpose); - static int LoadFileMonitors(const char *file, Monitor **&monitors, Purpose purpose); + static std::vector> LoadRemoteMonitors(const char *protocol, const char *host, const char*port, const char*path, Purpose purpose); + static std::vector> LoadFileMonitors(const char *file, Purpose purpose); #if HAVE_LIBAVFORMAT - static int LoadFfmpegMonitors(const char *file, Monitor **&monitors, Purpose purpose); + static std::vector> LoadFfmpegMonitors(const char *file, Purpose purpose); #endif // HAVE_LIBAVFORMAT - static Monitor *Load(unsigned int id, bool load_zones, Purpose purpose); + static std::shared_ptr Load(unsigned int id, bool load_zones, Purpose purpose); void Load(MYSQL_ROW dbrow, bool load_zones, Purpose purpose); //void writeStreamImage( Image *image, struct timeval *timestamp, int scale, int mag, int x, int y ); //void StreamImages( int scale=100, int maxfps=10, time_t ttl=0, int msq_id=0 ); diff --git a/src/zm_remote_camera.cpp b/src/zm_remote_camera.cpp index 55e275751..ff26b580c 100644 --- a/src/zm_remote_camera.cpp +++ b/src/zm_remote_camera.cpp @@ -24,7 +24,7 @@ #include RemoteCamera::RemoteCamera( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &p_protocol, const std::string &p_host, const std::string &p_port, @@ -39,7 +39,7 @@ RemoteCamera::RemoteCamera( bool p_capture, bool p_record_audio ) : - Camera( p_monitor_id, REMOTE_SRC, p_width, p_height, p_colours, ZM_SUBPIX_ORDER_DEFAULT_FOR_COLOUR(p_colours), p_brightness, p_contrast, p_hue, p_colour, p_capture, p_record_audio ), + Camera( monitor, REMOTE_SRC, p_width, p_height, p_colours, ZM_SUBPIX_ORDER_DEFAULT_FOR_COLOUR(p_colours), p_brightness, p_contrast, p_hue, p_colour, p_capture, p_record_audio ), protocol( p_protocol ), host( p_host ), port( p_port ), diff --git a/src/zm_remote_camera.h b/src/zm_remote_camera.h index 03c041468..3c7f9f698 100644 --- a/src/zm_remote_camera.h +++ b/src/zm_remote_camera.h @@ -52,7 +52,7 @@ protected: public: RemoteCamera( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &p_proto, const std::string &p_host, const std::string &p_port, diff --git a/src/zm_remote_camera_http.cpp b/src/zm_remote_camera_http.cpp index 43c3a3820..b0d1337bd 100644 --- a/src/zm_remote_camera_http.cpp +++ b/src/zm_remote_camera_http.cpp @@ -42,7 +42,7 @@ static RegExpr *content_type_expr = nullptr; #endif RemoteCameraHttp::RemoteCameraHttp( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &p_method, const std::string &p_host, const std::string &p_port, @@ -56,7 +56,7 @@ RemoteCameraHttp::RemoteCameraHttp( bool p_capture, bool p_record_audio ) : RemoteCamera( - p_monitor_id, + monitor, "http", p_host, p_port, @@ -81,11 +81,11 @@ RemoteCameraHttp::RemoteCameraHttp( else if ( p_method == "regexp" ) { method = REGEXP; } else - Fatal( "Unrecognised method '%s' when creating HTTP camera %d", p_method.c_str(), monitor_id ); + Fatal("Unrecognised method '%s' when creating HTTP camera %d", p_method.c_str(), monitor->Id()); if ( capture ) { Initialise(); } - mVideoStream = NULL; + mVideoStream = nullptr; } RemoteCameraHttp::~RemoteCameraHttp() { @@ -156,7 +156,7 @@ int RemoteCameraHttp::Connect() { addr = (struct sockaddr_in *)p->ai_addr; inet_ntop( AF_INET, &(addr->sin_addr), buf, INET6_ADDRSTRLEN ); - Warning("Can't connect to remote camera mid: %d at %s: %s", monitor_id, buf, strerror(errno) ); + Warning("Can't connect to remote camera mid: %d at %s: %s", monitor->Id(), buf, strerror(errno)); continue; } diff --git a/src/zm_remote_camera_http.h b/src/zm_remote_camera_http.h index 0ea90039a..baf1dc0f4 100644 --- a/src/zm_remote_camera_http.h +++ b/src/zm_remote_camera_http.h @@ -42,7 +42,7 @@ protected: public: RemoteCameraHttp( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &method, const std::string &host, const std::string &port, diff --git a/src/zm_remote_camera_nvsocket.cpp b/src/zm_remote_camera_nvsocket.cpp index 580b9ee28..7eaa4c053 100644 --- a/src/zm_remote_camera_nvsocket.cpp +++ b/src/zm_remote_camera_nvsocket.cpp @@ -32,7 +32,7 @@ #endif RemoteCameraNVSocket::RemoteCameraNVSocket( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &p_host, const std::string &p_port, const std::string &p_path, @@ -46,7 +46,7 @@ RemoteCameraNVSocket::RemoteCameraNVSocket( bool p_capture, bool p_record_audio ) : RemoteCamera( - p_monitor_id, + monitor, "http", p_host, p_port, @@ -115,7 +115,7 @@ int RemoteCameraNVSocket::Connect() { close(sd); sd = -1; - Warning("Can't connect to socket mid: %d : %s", monitor_id, strerror(errno) ); + Warning("Can't connect to socket mid: %d : %s", monitor->Id(), strerror(errno)); return -1; } diff --git a/src/zm_remote_camera_nvsocket.h b/src/zm_remote_camera_nvsocket.h index 7a6374a1b..5d021a8ff 100644 --- a/src/zm_remote_camera_nvsocket.h +++ b/src/zm_remote_camera_nvsocket.h @@ -32,7 +32,7 @@ protected: public: RemoteCameraNVSocket( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &host, const std::string &port, const std::string &path, diff --git a/src/zm_remote_camera_rtsp.cpp b/src/zm_remote_camera_rtsp.cpp index f612cc838..86d41a4e9 100644 --- a/src/zm_remote_camera_rtsp.cpp +++ b/src/zm_remote_camera_rtsp.cpp @@ -25,7 +25,7 @@ #if HAVE_LIBAVFORMAT RemoteCameraRtsp::RemoteCameraRtsp( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &p_method, const std::string &p_host, const std::string &p_port, @@ -41,7 +41,7 @@ RemoteCameraRtsp::RemoteCameraRtsp( bool p_capture, bool p_record_audio ) : RemoteCamera( - p_monitor_id, "rtsp", + monitor, "rtsp", p_host, p_port, p_path, p_width, p_height, p_colours, p_brightness, p_contrast, p_hue, p_colour, @@ -59,7 +59,7 @@ RemoteCameraRtsp::RemoteCameraRtsp( else if ( p_method == "rtpRtspHttp" ) method = RtspThread::RTP_RTSP_HTTP; else - Fatal("Unrecognised method '%s' when creating RTSP camera %d", p_method.c_str(), monitor_id); + Fatal("Unrecognised method '%s' when creating RTSP camera %d", p_method.c_str(), monitor->Id()); if ( capture ) { Initialise(); @@ -113,7 +113,7 @@ void RemoteCameraRtsp::Terminate() { } int RemoteCameraRtsp::Connect() { - rtspThread = new RtspThread(monitor_id, method, protocol, host, port, path, auth, rtsp_describe); + rtspThread = new RtspThread(monitor->Id(), method, protocol, host, port, path, auth, rtsp_describe); rtspThread->start(); diff --git a/src/zm_remote_camera_rtsp.h b/src/zm_remote_camera_rtsp.h index a715d9517..bd8b30a7c 100644 --- a/src/zm_remote_camera_rtsp.h +++ b/src/zm_remote_camera_rtsp.h @@ -55,7 +55,7 @@ protected: public: RemoteCameraRtsp( - unsigned int p_monitor_id, + const Monitor *monitor, const std::string &method, const std::string &host, const std::string &port, diff --git a/src/zm_rtsp_server_adts_source.cpp b/src/zm_rtsp_server_adts_source.cpp index 723d17001..1c0c1a5a4 100644 --- a/src/zm_rtsp_server_adts_source.cpp +++ b/src/zm_rtsp_server_adts_source.cpp @@ -27,12 +27,12 @@ static unsigned const samplingFrequencyTable[16] = { // ADTS_ZoneMinderDeviceSource::ADTS_ZoneMinderDeviceSource( UsageEnvironment& env, - Monitor *monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize ) : - ZoneMinderDeviceSource(env, monitor, stream, queueSize), + ZoneMinderDeviceSource(env, std::move(monitor), stream, queueSize), samplingFrequencyIndex(0), channels(stream->codecpar->channels) { diff --git a/src/zm_rtsp_server_adts_source.h b/src/zm_rtsp_server_adts_source.h index e4292d9a0..d8516bc30 100644 --- a/src/zm_rtsp_server_adts_source.h +++ b/src/zm_rtsp_server_adts_source.h @@ -24,18 +24,18 @@ class ADTS_ZoneMinderDeviceSource : public ZoneMinderDeviceSource { public: static ADTS_ZoneMinderDeviceSource* createNew( UsageEnvironment& env, - Monitor* monitor, + std::shared_ptr monitor, AVStream * stream, unsigned int queueSize ) { Debug(1, "m_stream %p codecpar %p channels %d", stream, stream->codecpar, stream->codecpar->channels); - return new ADTS_ZoneMinderDeviceSource(env, monitor, stream, queueSize); + return new ADTS_ZoneMinderDeviceSource(env, std::move(monitor), stream, queueSize); }; protected: ADTS_ZoneMinderDeviceSource( UsageEnvironment& env, - Monitor *monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize ); diff --git a/src/zm_rtsp_server_device_source.cpp b/src/zm_rtsp_server_device_source.cpp index 00fccc683..abd1f8381 100644 --- a/src/zm_rtsp_server_device_source.cpp +++ b/src/zm_rtsp_server_device_source.cpp @@ -18,13 +18,13 @@ #if HAVE_RTSP_SERVER ZoneMinderDeviceSource::ZoneMinderDeviceSource( UsageEnvironment& env, - Monitor* monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize ) : FramedSource(env), m_stream(stream), - m_monitor(monitor), + m_monitor(std::move(monitor)), m_packetqueue(nullptr), m_packetqueue_it(nullptr), m_queueSize(queueSize) diff --git a/src/zm_rtsp_server_device_source.h b/src/zm_rtsp_server_device_source.h index b21ff27d8..b74a8a606 100644 --- a/src/zm_rtsp_server_device_source.h +++ b/src/zm_rtsp_server_device_source.h @@ -26,7 +26,7 @@ class ZoneMinderDeviceSource: public FramedSource { public: static ZoneMinderDeviceSource* createNew( UsageEnvironment& env, - Monitor* monitor, + std::shared_ptr monitor, AVStream * stream, unsigned int queueSize ) { @@ -37,7 +37,7 @@ class ZoneMinderDeviceSource: public FramedSource { int getHeight() { return m_monitor->Height(); }; protected: - ZoneMinderDeviceSource(UsageEnvironment& env, Monitor* monitor, AVStream * stream, unsigned int queueSize); + ZoneMinderDeviceSource(UsageEnvironment& env, std::shared_ptr monitor, AVStream * stream, unsigned int queueSize); virtual ~ZoneMinderDeviceSource(); protected: @@ -63,7 +63,7 @@ class ZoneMinderDeviceSource: public FramedSource { std::list m_captureQueue; EventTriggerId m_eventTriggerId; AVStream *m_stream; - Monitor* m_monitor; + std::shared_ptr m_monitor; PacketQueue *m_packetqueue; std::list::iterator *m_packetqueue_it; diff --git a/src/zm_rtsp_server_h264_device_source.cpp b/src/zm_rtsp_server_h264_device_source.cpp index 0a312efe3..c10852196 100644 --- a/src/zm_rtsp_server_h264_device_source.cpp +++ b/src/zm_rtsp_server_h264_device_source.cpp @@ -24,12 +24,12 @@ // H264_ZoneMinderDeviceSource::H264_ZoneMinderDeviceSource( UsageEnvironment& env, - Monitor *monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize, bool repeatConfig, bool keepMarker) - : H26X_ZoneMinderDeviceSource(env, monitor, stream, queueSize, repeatConfig, keepMarker) + : H26X_ZoneMinderDeviceSource(env, std::move(monitor), stream, queueSize, repeatConfig, keepMarker) { // extradata appears to simply be the SPS and PPS NAL's this->splitFrames(m_stream->codecpar->extradata, m_stream->codecpar->extradata_size); @@ -89,12 +89,12 @@ std::list< std::pair > H264_ZoneMinderDeviceSource::spli H265_ZoneMinderDeviceSource::H265_ZoneMinderDeviceSource( UsageEnvironment& env, - Monitor *monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize, bool repeatConfig, bool keepMarker) - : H26X_ZoneMinderDeviceSource(env, monitor, stream, queueSize, repeatConfig, keepMarker) + : H26X_ZoneMinderDeviceSource(env, std::move(monitor), stream, queueSize, repeatConfig, keepMarker) { // extradata appears to simply be the SPS and PPS NAL's this->splitFrames(m_stream->codecpar->extradata, m_stream->codecpar->extradata_size); diff --git a/src/zm_rtsp_server_h264_device_source.h b/src/zm_rtsp_server_h264_device_source.h index a2ef81b52..5c01717c5 100644 --- a/src/zm_rtsp_server_h264_device_source.h +++ b/src/zm_rtsp_server_h264_device_source.h @@ -23,13 +23,13 @@ class H26X_ZoneMinderDeviceSource : public ZoneMinderDeviceSource { protected: H26X_ZoneMinderDeviceSource( UsageEnvironment& env, - Monitor *monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize, bool repeatConfig, bool keepMarker) : - ZoneMinderDeviceSource(env, monitor, stream, queueSize), + ZoneMinderDeviceSource(env, std::move(monitor), stream, queueSize), m_repeatConfig(repeatConfig), m_keepMarker(keepMarker), m_frameType(0) { } @@ -51,18 +51,18 @@ class H264_ZoneMinderDeviceSource : public H26X_ZoneMinderDeviceSource { public: static H264_ZoneMinderDeviceSource* createNew( UsageEnvironment& env, - Monitor *monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize, bool repeatConfig, bool keepMarker) { - return new H264_ZoneMinderDeviceSource(env, monitor, stream, queueSize, repeatConfig, keepMarker); + return new H264_ZoneMinderDeviceSource(env, std::move(monitor), stream, queueSize, repeatConfig, keepMarker); } protected: H264_ZoneMinderDeviceSource( UsageEnvironment& env, - Monitor *monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize, bool repeatConfig, @@ -76,18 +76,18 @@ class H265_ZoneMinderDeviceSource : public H26X_ZoneMinderDeviceSource { public: static H265_ZoneMinderDeviceSource* createNew( UsageEnvironment& env, - Monitor *monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize, bool repeatConfig, bool keepMarker) { - return new H265_ZoneMinderDeviceSource(env, monitor, stream, queueSize, repeatConfig, keepMarker); + return new H265_ZoneMinderDeviceSource(env, std::move(monitor), stream, queueSize, repeatConfig, keepMarker); } protected: H265_ZoneMinderDeviceSource( UsageEnvironment& env, - Monitor *monitor, + std::shared_ptr monitor, AVStream *stream, unsigned int queueSize, bool repeatConfig, diff --git a/src/zm_rtsp_server_thread.cpp b/src/zm_rtsp_server_thread.cpp index 891363510..6aadcf549 100644 --- a/src/zm_rtsp_server_thread.cpp +++ b/src/zm_rtsp_server_thread.cpp @@ -8,8 +8,8 @@ #if HAVE_RTSP_SERVER #include -RTSPServerThread::RTSPServerThread(Monitor *p_monitor) : - monitor(p_monitor), +RTSPServerThread::RTSPServerThread(std::shared_ptr monitor) : + monitor(std::move(monitor)), terminate(0) { //unsigned short rtsp_over_http_port = 0; diff --git a/src/zm_rtsp_server_thread.h b/src/zm_rtsp_server_thread.h index e3f19554e..d09eb1300 100644 --- a/src/zm_rtsp_server_thread.h +++ b/src/zm_rtsp_server_thread.h @@ -5,6 +5,7 @@ #include "zm_ffmpeg.h" #include "zm_thread.h" #include +#include #if HAVE_RTSP_SERVER #include @@ -14,7 +15,7 @@ class Monitor; class RTSPServerThread : public Thread { private: - Monitor *monitor; + std::shared_ptr monitor; char terminate; TaskScheduler* scheduler; @@ -25,7 +26,7 @@ class RTSPServerThread : public Thread { std::list sources; public: - explicit RTSPServerThread(Monitor *); + explicit RTSPServerThread(std::shared_ptr monitor); ~RTSPServerThread(); void addStream(AVStream *, AVStream *); int run(); diff --git a/src/zm_stream.cpp b/src/zm_stream.cpp index aa9b676c3..ecbed9434 100644 --- a/src/zm_stream.cpp +++ b/src/zm_stream.cpp @@ -34,16 +34,10 @@ StreamBase::~StreamBase() { } #endif closeComms(); - if ( monitor ) { - delete monitor; - monitor = nullptr; - } } bool StreamBase::loadMonitor(int p_monitor_id) { monitor_id = p_monitor_id; - if ( monitor ) - delete monitor; if ( !(monitor = Monitor::Load(monitor_id, false, Monitor::QUERY)) ) { Error("Unable to load monitor id %d for streaming", monitor_id); diff --git a/src/zm_stream.h b/src/zm_stream.h index 607d4d07a..01452c4b0 100644 --- a/src/zm_stream.h +++ b/src/zm_stream.h @@ -22,7 +22,7 @@ #include "zm_logger.h" #include "zm_mpeg.h" - +#include #include class Image; @@ -93,7 +93,7 @@ protected: protected: int monitor_id; - Monitor *monitor; + std::shared_ptr monitor; StreamType type; const char *format; diff --git a/src/zmc.cpp b/src/zmc.cpp index 90925aa48..efc4944c2 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -57,6 +57,7 @@ possible, this should run at more or less constant speed. #include "zm_analysis_thread.h" #include "zm_camera.h" #include "zm_db.h" +#include "zm_define.h" #include "zm_monitor.h" #include "zm_rtsp_server_thread.h" #include "zm_signal.h" @@ -186,29 +187,26 @@ int main(int argc, char *argv[]) { hwcaps_detect(); - Monitor **monitors = nullptr; - int n_monitors = 0; + std::vector> monitors; #if ZM_HAS_V4L if ( device[0] ) { - n_monitors = Monitor::LoadLocalMonitors(device, monitors, Monitor::CAPTURE); + monitors = Monitor::LoadLocalMonitors(device, Monitor::CAPTURE); } else #endif // ZM_HAS_V4L if ( host[0] ) { if ( !port ) port = "80"; - n_monitors = Monitor::LoadRemoteMonitors(protocol, host, port, path, monitors, Monitor::CAPTURE); + monitors = Monitor::LoadRemoteMonitors(protocol, host, port, path, Monitor::CAPTURE); } else if ( file[0] ) { - n_monitors = Monitor::LoadFileMonitors(file, monitors, Monitor::CAPTURE); + monitors = Monitor::LoadFileMonitors(file, Monitor::CAPTURE); } else { - Monitor *monitor = Monitor::Load(monitor_id, true, Monitor::CAPTURE); + std::shared_ptr monitor = Monitor::Load(monitor_id, true, Monitor::CAPTURE); if ( monitor ) { - monitors = new Monitor *[1]; - monitors[0] = monitor; - n_monitors = 1; + monitors.push_back(monitor); } } - if ( !n_monitors ) { + if (monitors.empty()) { Error("No monitors found"); exit(-1); } @@ -232,17 +230,17 @@ int main(int argc, char *argv[]) { while ( !zm_terminate ) { result = 0; static char sql[ZM_SQL_SML_BUFSIZ]; - for ( int i = 0; i < n_monitors; i++ ) { - if ( ! monitors[i]->getCamera() ) { + for (const std::shared_ptr &monitor : monitors) { + if (!monitor->getCamera()) { } - if ( ! monitors[i]->connect() ) { + if (!monitor->connect()) { } time_t now = (time_t)time(nullptr); - monitors[i]->setStartupTime(now); + monitor->setStartupTime(now); snprintf(sql, sizeof(sql), - "INSERT INTO Monitor_Status (MonitorId,Status,CaptureFPS,AnalysisFPS) VALUES (%d, 'Running',0,0) ON DUPLICATE KEY UPDATE Status='Running',CaptureFPS=0,AnalysisFPS=0", - monitors[i]->Id()); + "INSERT INTO Monitor_Status (MonitorId,Status,CaptureFPS,AnalysisFPS) VALUES (%d, 'Running',0,0) ON DUPLICATE KEY UPDATE Status='Running',CaptureFPS=0,AnalysisFPS=0", + monitor->Id()); if ( mysql_query(&dbconn, sql) ) { Error("Can't run query: %s", mysql_error(&dbconn)); } @@ -263,10 +261,10 @@ int main(int argc, char *argv[]) { } continue; } - for ( int i = 0; i < n_monitors; i++ ) { + for (std::shared_ptr &monitor : monitors) { snprintf(sql, sizeof(sql), - "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%d, 'Connected') ON DUPLICATE KEY UPDATE Status='Connected'", - monitors[i]->Id()); + "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%d, 'Connected') ON DUPLICATE KEY UPDATE Status='Connected'", + monitor->Id()); if ( mysql_query(&dbconn, sql) ) { Error("Can't run query: %s", mysql_error(&dbconn)); } @@ -275,17 +273,17 @@ int main(int argc, char *argv[]) { #if HAVE_RTSP_SERVER RTSPServerThread ** rtsp_server_threads = nullptr; if ( config.min_rtsp_port and monitors[0]->RTSPServer() ) { - rtsp_server_threads = new RTSPServerThread *[n_monitors]; + rtsp_server_threads = new RTSPServerThread *[monitors.size()]; Debug(1, "Starting RTSP server because min_rtsp_port is set"); } else { Debug(1, "Not starting RTSP server because min_rtsp_port not set"); } #endif - AnalysisThread **analysis_threads = new AnalysisThread *[n_monitors]; - int *capture_delays = new int[n_monitors]; - int *alarm_capture_delays = new int[n_monitors]; - struct timeval * last_capture_times = new struct timeval[n_monitors]; - for ( int i = 0; i < n_monitors; i++ ) { + AnalysisThread **analysis_threads = new AnalysisThread *[monitors.size()]; + int *capture_delays = new int[monitors.size()]; + int *alarm_capture_delays = new int[monitors.size()]; + struct timeval * last_capture_times = new struct timeval[monitors.size()]; + for (size_t i = 0; i < monitors.size(); i++) { last_capture_times[i].tv_sec = last_capture_times[i].tv_usec = 0; capture_delays[i] = monitors[i]->GetCaptureDelay(); alarm_capture_delays[i] = monitors[i]->GetAlarmCaptureDelay(); @@ -298,11 +296,11 @@ int main(int argc, char *argv[]) { analysis_threads[i] = new AnalysisThread(monitors[i]); analysis_threads[i]->start(); } else { - analysis_threads[i] = NULL; + analysis_threads[i] = nullptr; } #if HAVE_RTSP_SERVER if ( rtsp_server_threads ) { - for ( int i = 0; i < n_monitors; i++ ) { + for (size_t i = 0; i < monitors.size(); i++) { rtsp_server_threads[i] = new RTSPServerThread(monitors[i]); Camera *camera = monitors[i]->getCamera(); rtsp_server_threads[i]->addStream(camera->get_VideoStream(), camera->get_AudioStream()); @@ -317,25 +315,24 @@ int main(int argc, char *argv[]) { while ( !zm_terminate ) { //sigprocmask(SIG_BLOCK, &block_set, 0); - for ( int i = 0; i < n_monitors; i++ ) { - + for (size_t i = 0; i < monitors.size(); i++) { monitors[i]->CheckAction(); if ( monitors[i]->PreCapture() < 0 ) { - Error("Failed to pre-capture monitor %d %d (%d/%d)", - monitors[i]->Id(), monitors[i]->Name(), i+1, n_monitors); + Error("Failed to pre-capture monitor %d %d (%d/" SZFMTD ")", + monitors[i]->Id(), monitors[i]->Name(), i+1, monitors.size()); result = -1; break; } if ( monitors[i]->Capture() < 0 ) { - Error("Failed to capture image from monitor %d %s (%d/%d)", - monitors[i]->Id(), monitors[i]->Name(), i+1, n_monitors); + Error("Failed to capture image from monitor %d %s (%d/" SZFMTD ")", + monitors[i]->Id(), monitors[i]->Name(), i+1, monitors.size()); result = -1; break; } if ( monitors[i]->PostCapture() < 0 ) { - Error("Failed to post-capture monitor %d %s (%d/%d)", - monitors[i]->Id(), monitors[i]->Name(), i+1, n_monitors); + Error("Failed to post-capture monitor %d %s (%d/" SZFMTD ")", + monitors[i]->Id(), monitors[i]->Name(), i+1, monitors.size()); result = -1; break; } @@ -373,8 +370,8 @@ int main(int argc, char *argv[]) { } if ( zm_reload ) { - for ( int i = 0; i < n_monitors; i++ ) { - monitors[i]->Reload(); + for (std::shared_ptr &monitor : monitors) { + monitor->Reload(); } logTerm(); logInit(log_id_string); @@ -383,7 +380,7 @@ int main(int argc, char *argv[]) { } // end while ! zm_terminate and connected // Killoff the analysis threads. Don't need them spinning while we try to reconnect - for ( int i = 0; i < n_monitors; i++ ) { + for (size_t i = 0; i < monitors.size(); i++) { if ( analysis_threads[i] ) { analysis_threads[i]->stop(); } @@ -394,7 +391,7 @@ int main(int argc, char *argv[]) { #endif } - for ( int i = 0; i < n_monitors; i++ ) { + for (size_t i = 0; i < monitors.size(); i++) { monitors[i]->Close(); // Killoff the analysis threads. Don't need them spinning while we try to reconnect @@ -435,18 +432,16 @@ int main(int argc, char *argv[]) { Debug(1,"Updating Monitor status"); - for ( int i = 0; i < n_monitors; i++ ) { + for (std::shared_ptr &monitor : monitors) { static char sql[ZM_SQL_SML_BUFSIZ]; snprintf(sql, sizeof(sql), "INSERT INTO Monitor_Status (MonitorId,Status) VALUES (%d, 'Connected') ON DUPLICATE KEY UPDATE Status='NotRunning'", - monitors[i]->Id()); + monitor->Id()); if ( mysql_query(&dbconn, sql) ) { Error("Can't run query: %s", mysql_error(&dbconn)); } - monitors[i]->disconnect(); - delete monitors[i]; + monitor->disconnect(); } - delete [] monitors; Image::Deinitialise(); Debug(1,"terminating"); diff --git a/src/zmu.cpp b/src/zmu.cpp index 32b9ad001..633c26f7d 100644 --- a/src/zmu.cpp +++ b/src/zmu.cpp @@ -473,7 +473,7 @@ int main(int argc, char *argv[]) { } // end if auth if ( mon_id > 0 ) { - Monitor *monitor = Monitor::Load(mon_id, function&(ZMU_QUERY|ZMU_ZONES), Monitor::QUERY); + std::shared_ptr monitor = Monitor::Load(mon_id, function&(ZMU_QUERY|ZMU_ZONES), Monitor::QUERY); if ( !monitor ) { Error("Unable to load monitor %d", mon_id); exit_zmu(-1); @@ -484,8 +484,6 @@ int main(int argc, char *argv[]) { } if ( !monitor->connect() ) { Error("Can't connect to capture daemon: %d %s", monitor->Id(), monitor->Name()); - delete monitor; - monitor = nullptr; exit_zmu(-1); } @@ -705,8 +703,6 @@ int main(int argc, char *argv[]) { if ( !function ) { Usage(); } - delete monitor; - monitor = nullptr; } else { // non monitor functions if ( function & ZMU_QUERY ) { #if ZM_HAS_V4L @@ -745,7 +741,7 @@ int main(int argc, char *argv[]) { int monitor_function = atoi(dbrow[1]); if ( !user || user->canAccess(monitor_id) ) { if ( monitor_function > 1 ) { - Monitor *monitor = Monitor::Load(monitor_id, false, Monitor::QUERY); + std::shared_ptr monitor = Monitor::Load(monitor_id, false, Monitor::QUERY); if ( monitor && monitor->connect() ) { struct timeval tv = monitor->GetTimestamp(); printf( "%4d%5d%6d%9d%11ld.%02ld%6d%6d%8" PRIu64 "%8.2f\n", @@ -759,7 +755,6 @@ int main(int argc, char *argv[]) { monitor->GetLastEventId(), monitor->GetFPS() ); - delete monitor; } } else { struct timeval tv = { 0, 0 };