diff --git a/CMakeLists.txt b/CMakeLists.txt
index b41e5c349..cebab0e4e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,11 +2,12 @@
# Created by mastertheknife (Kfir Itzhak)
# For more information and installation, see the INSTALL file
#
-cmake_minimum_required (VERSION 2.8.7)
+cmake_minimum_required (VERSION 3.1.0)
project (zoneminder)
file (STRINGS "version" zoneminder_VERSION)
# make API version a minor of ZM version
set(zoneminder_API_VERSION "${zoneminder_VERSION}.1")
+set (CMAKE_CXX_STANDARD 11)
# Make sure the submodules are there
if( NOT EXISTS "${CMAKE_SOURCE_DIR}/web/api/app/Plugin/Crud/Lib/CrudControllerTrait.php" )
@@ -66,6 +67,24 @@ set(CMAKE_CXX_FLAGS_DEBUG "-Wall -D__STDC_CONSTANT_MACROS -g")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
+# GCC below 6.0 doesn't support __target__("fpu=neon") attribute, required for compiling ARM Neon code, otherwise compilation fails.
+# Must use -mfpu=neon compiler flag instead, but only do that for processors that support neon, otherwise strip the neon code alltogether,
+# because passing -fmpu=neon is unsafe to processors that don't support neon
+IF(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm" AND CMAKE_SYSTEM_NAME MATCHES "Linux")
+ IF(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
+ EXEC_PROGRAM(grep ARGS " neon " "/proc/cpuinfo" OUTPUT_VARIABLE neonoutput RETURN_VALUE neonresult)
+ IF(neonresult EQUAL 0)
+ set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mfpu=neon")
+ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mfpu=neon")
+ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -mfpu=neon")
+ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -mfpu=neon")
+ ELSE(neonresult EQUAL 0)
+ add_definitions(-DZM_STRIP_NEON=1)
+ message(STATUS "ARM Neon is not available on this processor. Neon functions will be absent")
+ ENDIF(neonresult EQUAL 0)
+ ENDIF(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
+ENDIF(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm" AND CMAKE_SYSTEM_NAME MATCHES "Linux")
+
# Modules that we need:
include (GNUInstallDirs)
include (CheckIncludeFile)
@@ -546,21 +565,21 @@ if(NOT ZM_NO_FFMPEG)
endif(SWSCALE_LIBRARIES)
# rescale (using find_library and find_path)
- find_library(SWRESAMPLE_LIBRARIES swresample)
- if(SWRESAMPLE_LIBRARIES)
- set(HAVE_LIBSWRESAMPLE 1)
- list(APPEND ZM_BIN_LIBS "${SWRESAMPLE_LIBRARIES}")
- find_path(SWRESAMPLE_INCLUDE_DIR "libswresample/swresample.h" /usr/include/ffmpeg)
- if(SWRESAMPLE_INCLUDE_DIR)
- include_directories("${SWRESAMPLE_INCLUDE_DIR}")
- set(CMAKE_REQUIRED_INCLUDES "${SWRESAMPLE_INCLUDE_DIR}")
- endif(SWRESAMPLE_INCLUDE_DIR)
- mark_as_advanced(FORCE SWRESAMPLE_LIBRARIES SWRESAMPLE_INCLUDE_DIR)
- check_include_file("libswresample/swresample.h" HAVE_LIBSWRESAMPLE_SWRESAMPLE_H)
- set(optlibsfound "${optlibsfound} SWResample")
- else(SWRESAMPLE_LIBRARIES)
- set(optlibsnotfound "${optlibsnotfound} SWResample")
- endif(SWRESAMPLE_LIBRARIES)
+ find_library(AVRESAMPLE_LIBRARIES avresample)
+ if(AVRESAMPLE_LIBRARIES)
+ set(HAVE_LIBAVRESAMPLE 1)
+ list(APPEND ZM_BIN_LIBS "${AVRESAMPLE_LIBRARIES}")
+ find_path(AVRESAMPLE_INCLUDE_DIR "libavresample/avresample.h" /usr/include/ffmpeg)
+ if(AVRESAMPLE_INCLUDE_DIR)
+ include_directories("${AVRESAMPLE_INCLUDE_DIR}")
+ set(CMAKE_REQUIRED_INCLUDES "${AVRESAMPLE_INCLUDE_DIR}")
+ endif(AVRESAMPLE_INCLUDE_DIR)
+ mark_as_advanced(FORCE AVRESAMPLE_LIBRARIES AVRESAMPLE_INCLUDE_DIR)
+ check_include_file("libavresample/avresample.h" HAVE_LIBAVRESAMPLE_AVRESAMPLE_H)
+ set(optlibsfound "${optlibsfound} AVResample")
+ else(AVRESAMPLE_LIBRARIES)
+ set(optlibsnotfound "${optlibsnotfound} AVResample")
+ endif(AVRESAMPLE_LIBRARIES)
# Find the path to the ffmpeg executable
find_program(FFMPEG_EXECUTABLE
diff --git a/distros/redhat/systemd/zoneminder.tmpfiles.in b/distros/redhat/systemd/zoneminder.tmpfiles.in
index f3acd0af7..910c360f1 100644
--- a/distros/redhat/systemd/zoneminder.tmpfiles.in
+++ b/distros/redhat/systemd/zoneminder.tmpfiles.in
@@ -1,2 +1,7 @@
D @ZM_TMPDIR@ 0755 @WEB_USER@ @WEB_GROUP@
+D @ZM_TMPDIR@/logs 0755 @WEB_USER@ @WEB_GROUP@
+D @ZM_TMPDIR@/cache 0755 @WEB_USER@ @WEB_GROUP@
+D @ZM_TMPDIR@/cache/models 0755 @WEB_USER@ @WEB_GROUP@
+D @ZM_TMPDIR@/cache/persistent 0755 @WEB_USER@ @WEB_GROUP@
+D @ZM_TMPDIR@/cache/views 0755 @WEB_USER@ @WEB_GROUP@
D @ZM_SOCKDIR@ 0755 @WEB_USER@ @WEB_GROUP@
diff --git a/distros/redhat/zoneminder.spec b/distros/redhat/zoneminder.spec
index f562b1159..0ed6323f0 100644
--- a/distros/redhat/zoneminder.spec
+++ b/distros/redhat/zoneminder.spec
@@ -29,7 +29,7 @@
Name: zoneminder
Version: 1.30.2
-Release: 1%{?dist}
+Release: 2%{?dist}
Summary: A camera monitoring and analysis tool
Group: System Environment/Daemons
# jscalendar is LGPL (any version): http://www.dynarch.com/projects/calendar/
@@ -39,7 +39,7 @@ Group: System Environment/Daemons
License: GPLv2+ and LGPLv2+ and MIT
URL: http://www.zoneminder.com/
-Source0: https://github.com/ZoneMinder/ZoneMinder/archive/v%{version}.tar.gz#/%{name}-%{version}.tar.gz
+Source0: https://github.com/ZoneMinder/ZoneMinder/archive/%{version}.tar.gz#/zoneminder-%{version}.tar.gz
Source1: https://github.com/FriendsOfCake/crud/archive/v%{crud_version}.tar.gz#/crud-%{crud_version}.tar.gz
%{?with_init_systemd:BuildRequires: systemd-devel}
@@ -130,8 +130,8 @@ designed to support as many cameras as you can attach to your computer without
too much degradation of performance.
%prep
-%autosetup
-%autosetup -a 1
+%autosetup -n ZoneMinder-%{version}
+%autosetup -a 1 -n ZoneMinder-%{version}
rmdir ./web/api/app/Plugin/Crud
mv -f crud-%{crud_version} ./web/api/app/Plugin/Crud
@@ -336,8 +336,11 @@ rm -rf %{_docdir}/%{name}-%{version}
%dir %attr(755,%{zmuid_final},%{zmgid_final}) %ghost %{_localstatedir}/run/zoneminder
%changelog
+* Thu Mar 30 2017 Andrew Bauer - 1.30.2-2
+- 1.30.2 release
+
* Wed Feb 08 2017 Andrew Bauer - 1.30.2-1
-- Bump version for 1.30.2 release
+- Bump version for 1.30.2 release candidate 1
* Wed Dec 28 2016 Andrew Bauer - 1.30.1-2
- Changes from rpmfusion #4393
diff --git a/distros/ubuntu1204/control b/distros/ubuntu1204/control
index d88ee45ed..3f360c833 100644
--- a/distros/ubuntu1204/control
+++ b/distros/ubuntu1204/control
@@ -5,6 +5,7 @@ Maintainer: Dmitry Smirnov
Uploaders: Vagrant Cascadian
Build-Depends: debhelper (>= 9), python-sphinx | python3-sphinx, apache2-dev, dh-linktree
,cmake
+ ,libx264-dev, libmp4v2-dev
,libavcodec-dev, libavformat-dev (>= 3:0.svn20090204), libswscale-dev (>= 3:0.svn20090204), libavutil-dev, libavdevice-dev
,libboost1.55-dev
,libbz2-dev
@@ -35,6 +36,7 @@ Package: zoneminder
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends}
,javascript-common
+ ,libmp4v2-2, libx264-142
,libav-tools|ffmpeg
,libdate-manip-perl
,libdbd-mysql-perl
diff --git a/distros/ubuntu1204/rules b/distros/ubuntu1204/rules
index b467c870f..2b54a2131 100755
--- a/distros/ubuntu1204/rules
+++ b/distros/ubuntu1204/rules
@@ -58,8 +58,10 @@ override_dh_auto_install:
override_dh_fixperms:
dh_fixperms
- ## 637685
- chmod -c o-r $(CURDIR)/debian/zoneminder/etc/zm/zm.conf
+ #
+ # As requested by the Debian Webapps Policy Manual §3.2.1
+ chown root:www-data $(CURDIR)/debian/zoneminder/etc/zm/zm.conf
+ chmod 640 $(CURDIR)/debian/zoneminder/etc/zm/zm.conf
override_dh_installinit:
dh_installinit --no-start
diff --git a/distros/ubuntu1604/control b/distros/ubuntu1604/control
index 85e0af77d..31074f2f4 100644
--- a/distros/ubuntu1604/control
+++ b/distros/ubuntu1604/control
@@ -7,26 +7,24 @@ Build-Depends: debhelper (>= 9), dh-systemd, python-sphinx | python3-sphinx, apa
,cmake
,libx264-dev, libmp4v2-dev
,libavcodec-dev, libavformat-dev, libswscale-dev
- , libavutil-dev, libavdevice-dev
+ ,libavutil-dev, libavdevice-dev
,libboost-dev
,libbz2-dev
,libgcrypt-dev
,libcurl4-gnutls-dev
,libgnutls-openssl-dev
- ,libjpeg-dev
+ , libjpeg8-dev | libjpeg9-dev | libjpeg62-turbo-dev
,libmysqlclient-dev
,libpcre3-dev
,libpolkit-gobject-1-dev
,libv4l-dev (>= 0.8.3) [!hurd-any]
- ,libvlc-dev
+ ,libvlc-dev,
,libdate-manip-perl
,libdbd-mysql-perl
,libphp-serialization-perl
,libsys-mmap-perl [!hurd-any]
,libwww-perl
,libdata-uuid-perl
- ,libx264-dev
- ,libmp4v2-dev
# Unbundled (dh_linktree):
,libjs-jquery
,libjs-mootools
@@ -39,6 +37,7 @@ Package: zoneminder
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends}
,javascript-common
+ ,libmp4v2-2, libx264-142|libx264-148, libswscale-ffmpeg3|libswscale4|libswscale3
,ffmpeg | libav-tools
,libdate-manip-perl, libmime-lite-perl, libmime-tools-perl
,libdbd-mysql-perl
@@ -66,6 +65,7 @@ Depends: ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends}
,policykit-1
,rsyslog | system-log-daemon
,zip
+ ,libprce3
Recommends: ${misc:Recommends}
,libapache2-mod-php5 | libapache2-mod-php | php5-fpm | php-fpm
,mysql-server | virtual-mysql-server
diff --git a/distros/ubuntu1604/rules b/distros/ubuntu1604/rules
index bf8012aa8..c7fb00198 100755
--- a/distros/ubuntu1604/rules
+++ b/distros/ubuntu1604/rules
@@ -58,8 +58,10 @@ override_dh_auto_install:
override_dh_fixperms:
dh_fixperms
- ## 637685
- chmod -c o-r $(CURDIR)/debian/zoneminder/etc/zm/zm.conf
+ #
+ # As requested by the Debian Webapps Policy Manual §3.2.1
+ chown root:www-data $(CURDIR)/debian/zoneminder/etc/zm/zm.conf
+ chmod 640 $(CURDIR)/debian/zoneminder/etc/zm/zm.conf
override_dh_installinit:
dh_installinit --no-start
diff --git a/distros/ubuntu1604/source/format b/distros/ubuntu1604/source/format
index af745b310..d3827e75a 100644
--- a/distros/ubuntu1604/source/format
+++ b/distros/ubuntu1604/source/format
@@ -1 +1 @@
-3.0 (git)
+1.0
diff --git a/distros/ubuntu1604/zoneminder.links b/distros/ubuntu1604/zoneminder.links
index 4299f392a..14a8aee91 100644
--- a/distros/ubuntu1604/zoneminder.links
+++ b/distros/ubuntu1604/zoneminder.links
@@ -1,3 +1,4 @@
/var/cache/zoneminder/events /usr/share/zoneminder/www/events
/var/cache/zoneminder/images /usr/share/zoneminder/www/images
/var/cache/zoneminder/temp /usr/share/zoneminder/www/temp
+/var/tmp /usr/share/zoneminder/www/api/app/tmp
diff --git a/distros/ubuntu1604/zoneminder.tmpfile b/distros/ubuntu1604/zoneminder.tmpfile
index d307c6640..a7333ec19 100644
--- a/distros/ubuntu1604/zoneminder.tmpfile
+++ b/distros/ubuntu1604/zoneminder.tmpfile
@@ -1,2 +1,6 @@
-d /var/run/zm 0755 www-data www-data
-d /tmp/zm 0755 www-data www-data
+d /var/run/zm 0755 www-data www-data
+d /tmp/zm 0755 www-data www-data
+d /var/tmp/zm 0755 www-data www-data
+d /var/tmp/cache 0755 www-data www-data
+d /var/tmp/cache/models 0755 www-data www-data
+d /var/tmp/cache/persistent 0755 www-data www-data
diff --git a/docs/installationguide/images/zm-multiserver.png b/docs/installationguide/images/zm-multiserver.png
new file mode 100644
index 000000000..360b612a8
Binary files /dev/null and b/docs/installationguide/images/zm-multiserver.png differ
diff --git a/docs/installationguide/images/zm-multiserver.xml b/docs/installationguide/images/zm-multiserver.xml
new file mode 100644
index 000000000..c829e1a94
--- /dev/null
+++ b/docs/installationguide/images/zm-multiserver.xml
@@ -0,0 +1 @@
+7Vvfb+I4F/1rkHYfFpEEQnlsmXZ2pXZUqaPd/fZlZIib+JsQs44pZf76uU6uEydOgELoUgkeELn+fc/JuddO6HnTxetnQZbRAw9o3HMHwWvP+9Rz3SvfhW9l2OSGsTfKDaFgQW5ySsMT+0HROEDrigU0rVSUnMeSLavGOU8SOpcVGxGCr6vVnnlcHXVJQj1iaXiak9i2/sUCGeGyXL+0/05ZGOmRHX+Sl8zI/Hso+CrB8Xqu95x98uIF0X1lC/VuwYeCc+hG/Vq8Tmms/Kh9lHvjrqW0mKSgCU5kRwMHUXgh8QpX+g9P6ANLAirA/rACF//2RMVLdvmFyjUX3+HXV77kMQ83PdePYaibGRT7ofqVd5jKjXZdtniqRnSgeB0xSZ+WZK5K18AVsEVyEWNxKgX/TqfQtwBLAnMB4zOL45op4oL94IkkuiFMUDLA6zpmYQI2yVXPz1AFyeRcFZMzHYQ+U80pkjUzocM+U76gUsA6B1jqISORyB52sC5ZMR5jlchgxFgzmSATw6LnEiD4gRi14OWOLbweSALkW6gF7cYC1qjsBzicoF/nMBBwwXb4ggWBGqYRYQ61n2O+BksE9Sg0MLFR0AguiWRcdeWCA2Fm0AVLQnVdXgHvwKCKVXM9R7ipBgPfn067gdgZDCsYu+MrC+QR3jkmxg7e88dBjJ0YEN+zBO65C7jdgKtRKsAd9xHLHfCimB4Fr2aSARsNINLgJRcy4iFPSHxbWsF7WkGVbwx8aRJcq+BWIgmWO8AXq/6fSrlBGMhKcgVWMcI9z9yt+ml1a8pXIoM6cxzGcElESLHeCAVJLWKr9wWNgQEv1XDa5MmsKayKqFa6wpKzRKZGz4/KYIhyTZUdDMwlKnmPza2dQa31EMO6Hj53A7aqgV2scS/8J+jDjvB/ZfJvZe57Pl7+L7scT/DykQoG01M3ddb4XRnjoYyeIWFGflXia4Sx6g8neOPuQ7ADeKHdYqj+n5DxcjDdX3+xOGNwwJRzkMpp9rG0H0r8qyvnxoOSUJCAAW61IKDNn5iADDoX60SBX3SmM99MsiOyVJNZvEK7ZdRP8tQw7c9WAMEhY9Q0H7Nk72ZpUFhl0zPoyqD1jbYha0GlMfyguxqy4jcHDb8qEOBMO+vTCV7XIWOEzDOo8SS5UBuWCznOkBzDyTuSowjKtnDk+WLAXnTCOI2Vd6HuL183S5Xd/dqUVOYWGNdoaVGsLT9r3oVp6z2Z0fiRpwzBnXEp+QIqxLWCIg+1EtMz5vZyfgJuOTVyTeztZpG9VLabnXALifzfJSswY7OJujbbqOt6o5NnMNrWXQazPyD23tBGyMrwUpi9rNnM+6h2RpJVN1LCaoLY6rad7jB3Vw2M1ba35X12Lj9sUeMdufzOjootoe4o50QXmwJfy8IWWNUZYnGihWac4D4CU5yMkpnusTxwbBKeSTU/9jT1TOFpCmqF8aiUp4nneWBS689Wo+Xd/3eljkuBmN7dnQcf05SHrzJd0ieZW8JePsAl7u0d99Lcp7VTHKXXO4JhQfvd0dBg3aSJdDoEHkM6HWm33YUGDPuBe1Mc/zdlr820afB9K7StZIhIkOm90uyApFERkes4Mp6O+wzUIe2rpyIZkkdhpbPi6oZ5bCfFQ/SviaQ+xznuCHUPPT3bMInuc8cYborwYztwWxjdP2LWRxpO8CzirRGzdcrdR0zd8xsDxGAwnSqpqAeITwTiIkntCGHFA2tTdcywb4hDl13ZUdHpW4opQPc7NHgKXM2U9La+skVrelqkjcdpnb3//3BaN3QgaWtKtTvXuvpIB+8OWqd8gt3B9kf0WrAuWrB/ptq1Bng+hsxCA2wCnyxvdVyk4keWgNGwnjucSgLqIx0sAa1T7l4C7IdDFwk4MwkYTqqvFbyzBOBpzUeWAP/ddjz1kQ7e8bROuXsJsB8CXiTgzCTAr78a+J4SoJ9KNOyIZ3rD+McjVJiSBRXE3nru91Cw6OtCtb2oNl8JJjff5rnTO3mBbTSqbj2KR8AGzTQTO3/crDu+vIB6yncUXa/+jqL99OVkL6DqN5ovL6CeCNyR9Xbx6V5Ahcvy7wR52lH+P8O7/Qk=
\ No newline at end of file
diff --git a/docs/installationguide/images/zm_first_screen_post_install.png b/docs/installationguide/images/zm_first_screen_post_install.png
new file mode 100644
index 000000000..568d7fcf5
Binary files /dev/null and b/docs/installationguide/images/zm_first_screen_post_install.png differ
diff --git a/docs/installationguide/index.rst b/docs/installationguide/index.rst
new file mode 100644
index 000000000..e030ed7cf
--- /dev/null
+++ b/docs/installationguide/index.rst
@@ -0,0 +1,12 @@
+Installation Guide
+======================================
+
+Contents:
+
+.. toctree::
+ :maxdepth: 2
+
+ ubuntu
+ debian
+ redhat
+ multiserver
diff --git a/docs/installationguide/multiserver.rst b/docs/installationguide/multiserver.rst
new file mode 100644
index 000000000..e9ebefac2
--- /dev/null
+++ b/docs/installationguide/multiserver.rst
@@ -0,0 +1,55 @@
+Multi-Server Install
+====================
+
+It is possible to run multiple ZoneMinder servers and manage them from a single interface. To achieve this each zoneminder server is connected to a single shared database server and shares file storage for event data.
+
+.. image:: images/zm-multiserver.png
+
+Topology Design Notes
+---------------------
+
+1. Device symbols represent separate logical functions, not necessarily separate hardware. For example, the Database Server and a ZoneMinder Server, can reside on the same physical hardware.
+
+2. Configure each ZoneMinder Server to use the same, remote Database Server (Green).
+
+3. The Storage Server (Red) represents shared storage, accessible by all ZoneMinder Servers, mounted under each server’s events folder.
+
+4. Create at least two networks for best performance. Dedicate a Storage LAN for communication with the Storage and Database Servers. Make use of multipath and jumbo frames if possible. Keep all other traffic off the Storage LAN! Dedicate the second LAN, called the Video LAN in the diagram, for all other traffic.
+
+New installs
+------------
+
+1. Follow the normal instructions for your distro for installing ZoneMinder onto all the ZoneMinder servers in the normal fashion. Only a single database will be needed either as standalone, or on one of the ZoneMinder Servers.
+
+2. On each ZoneMinder server, edit zm.conf. Find the ZM_DB_HOST variable and set it to the name or ip address of your Database Server. Find the ZM_SERVER_HOST and enter a name for this ZoneMinder server. Use a name easily recognizable by you. This name is not used by ZoneMinder for dns or any other form of network conectivity.
+
+3. Copy the file /usr/share/zoneminder/db/zm_create.sql from one of the ZoneMinder Servers to the machine targeted as the Database Server.
+
+4. Install mysql/mariadb server onto the Database Server.
+
+5. It is advised to run "mysql_secure_installation" to help secure the server.
+
+6. Using the password for the root account set during the previous step, create the ZoneMinder database and configure a database account for ZoneMinder to use:
+
+::
+
+ mysql -u root -p < zm_create.sql
+ mysql -uroot -p -e "grant all on zm.* to 'zmuser'@localhost identified by 'zmpass';"
+ mysqladmin -u root -p reload
+
+The database account credentials, zmuser/zmpass, are arbitrary. Set them to anything that suits your environment.
+Note that these commands are just an example and might not be secure enough for your environment.
+
+7. If you have chosen to change the ZoneMinder database account credentials to something other than zmuser/zmpass, you must now update zm.conf on each ZoneMinder Server. Change ZM_DB_USER and ZM_DB_PASS to the values you created in the previous step.
+
+Additionally, you must also edit /usr/share/zoneminder/www/api/app/Config/database.php in a similar manner on each ZoneMinder Server. Scroll down and change login and password to the values you created in the previous step.
+
+8. All ZoneMinders Servers must share a common events folder. This can be done in any manner supported by the underlying operating system. From the Storage Server, share/export a folder to be used for ZoneMinder events.
+
+9. From each ZoneMinder Server, mount the shared events folder on the Storage Server to the events folder on the local ZoneMinder Server.
+
+NOTE: The location of this folder varies by distro. This folder is often found under "/var/lib/zoneminder/events" for RedHat based distros and "/var/cache/zoneminder/events" for Debain based distros. This folder is NOT a Symbolic Link!
+
+10. Open your browser and point it to the web console on any of the ZoneMinder Servers (they will all be the same). Open Options, click the Servers tab,and populate this screen with all of your ZoneMinder Servers. Each server has a field for its name and its hostname. The name is what you used for ZM_SERVER_HOST in step 2. The hostname is the network name or ip address ZoneMinder should use.
+
+11. When creating a new Monitor, remember to select the server the camera will be assigned to from the Server drop down box.
diff --git a/misc/zoneminder.service.in b/misc/zoneminder.service.in
index d15c479c2..d1cfb36a0 100644
--- a/misc/zoneminder.service.in
+++ b/misc/zoneminder.service.in
@@ -12,7 +12,7 @@ Type=forking
ExecStart=@BINDIR@/zmpkg.pl start
ExecReload=@BINDIR@/zmpkg.pl restart
ExecStop=@BINDIR@/zmpkg.pl stop
-PIDFile="@ZM_RUNDIR@/zm.pid"
+PIDFile=@ZM_RUNDIR@/zm.pid
Environment=TZ=:/etc/localtime
[Install]
diff --git a/scripts/ZoneMinder/lib/ZoneMinder/ConfigData.pm.in b/scripts/ZoneMinder/lib/ZoneMinder/ConfigData.pm.in
index 4dd136657..d6ff8b2d9 100644
--- a/scripts/ZoneMinder/lib/ZoneMinder/ConfigData.pm.in
+++ b/scripts/ZoneMinder/lib/ZoneMinder/ConfigData.pm.in
@@ -354,6 +354,26 @@ our @options = (
type => $types{boolean},
category => 'system',
},
+ {
+ name => 'ZM_ENABLE_CSRF_MAGIC',
+ default => 'no',
+ description => 'Enable csrf-magic library',
+ help => q`
+ CSRF stands for Cross-Site Request Forgery which, under specific
+ circumstances, can allow an attacker to perform any task your
+ ZoneMinder user account has permission to perform. To accomplish
+ this, the attacker must write a very specific web page and get
+ you to navigate to it, while you are logged into the ZoneMinder
+ web console at the same time. Enabling ZM_ENABLE_CSRF_MAGIC will
+ help mitigate these kinds of attackes. Be warned this feature
+ is experimental and may cause problems, particularly with the API.
+ If you find a false positive and can document how to reproduce it,
+ then please report it. This feature defaults to OFF currently due to
+ its experimental nature.
+ `,
+ type => $types{boolean},
+ category => 'system',
+ },
{
name => 'ZM_OPT_USE_API',
default => 'yes',
diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Control/HikVision.pm b/scripts/ZoneMinder/lib/ZoneMinder/Control/HikVision.pm
index 2c665b4ad..f9cabd5fa 100644
--- a/scripts/ZoneMinder/lib/ZoneMinder/Control/HikVision.pm
+++ b/scripts/ZoneMinder/lib/ZoneMinder/Control/HikVision.pm
@@ -15,7 +15,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ==========================================================================
#
diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Control/SPP1802SWPTZ.pm b/scripts/ZoneMinder/lib/ZoneMinder/Control/SPP1802SWPTZ.pm
index 0f7a75012..38838237b 100644
--- a/scripts/ZoneMinder/lib/ZoneMinder/Control/SPP1802SWPTZ.pm
+++ b/scripts/ZoneMinder/lib/ZoneMinder/Control/SPP1802SWPTZ.pm
@@ -1,27 +1,34 @@
+# ==========================================================================
+#
+# ZoneMinder SunEyes SP-P1802SWPTZ IP Control Protocol Module, $Date: 2017-03-19 23:00:00 +1000 (Sat, 19 March 2017) $, $Revision: 0002 $
+# Copyright (C) 2001-2008 Philip Coombes
+# Modified for use with Foscam FI8918W IP Camera by Dave Harris
+# Modified Feb 2011 by Howard Durdle (http://durdl.es/x) to:
+# fix horizontal panning, add presets and IR on/off
+# use Control Device field to pass username and password
+# Modified May 2014 by Arun Horne (http://arunhorne.co.uk) to:
+# use HTTP basic auth as required by firmware 11.37.x.x upward
# Modified on Sep 28 2015 by Bobby Billingsley
# Changes made
# - Copied FI8918W.pm to SPP1802SWPTZ.pm
# - modified to control a SunEyes SP-P1802SWPTZ
-
-# ==========================================================================
-# ZoneMinder SunEyes SP-P1802SWPTZ IP Control Protocol Module
+# Modified on 13 March 2017 by Steve Gilvarry
+# -Address license and copyright issues
#
-# This program is free software; you can redistribute it and/or modify it under
-# the terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
#
-# This program is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
-# details.
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
-# ==========================================================================
-#
# This module contains the implementation of the SunEyes SP-P1802SWPTZ IP
# camera control protocol
#
diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm
index 4a0b4f427..9d984adf3 100644
--- a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm
+++ b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm
@@ -241,6 +241,7 @@ sub GenerateVideo {
sub delete {
my $event = $_[0];
Info( "Deleting event $event->{Id} from Monitor $event->{MonitorId} $event->{StartTime}\n" );
+ $ZoneMinder::Database::dbh->ping();
# Do it individually to avoid locking up the table for new events
my $sql = 'delete from Events where Id = ?';
my $sth = $ZoneMinder::Database::dbh->prepare_cached( $sql )
@@ -331,12 +332,15 @@ sub delete_files {
sub Storage {
return new ZoneMinder::Storage( $_[0]{StorageId} );
}
+
sub check_for_in_filesystem {
my $path = $_[0]->Path();
if ( $path ) {
my @files = glob( $path . '/*' );
+Debug("Checking for files for event $_[0]{Id} at $path using glob $path/* found " . scalar @files . " files");
return 1 if @files;
}
+Debug("Checking for files for event $_[0]{Id} at $path using glob $path/* found no files");
return 0;
}
diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm b/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm
index 49636ebe7..1f4259c6b 100644
--- a/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm
+++ b/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm
@@ -254,14 +254,14 @@ sub Sql {
} # end if terms
if ( $self->{Sql} ) {
- if ( $self->{AutoMessage} ) {
+ #if ( $self->{AutoMessage} ) {
# Include all events, including events that are still ongoing
# and have no EndTime yet
$sql .= " and ( ".$self->{Sql}." )";
- } else {
+ #} else {
# Only include closed events (events with valid EndTime)
- $sql .= " where not isnull(E.EndTime) and ( ".$self->{Sql}." )";
- }
+ #$sql .= " where not isnull(E.EndTime) and ( ".$self->{Sql}." )";
+ #}
}
my @auto_terms;
if ( $self->{AutoArchive} ) {
diff --git a/scripts/ZoneMinder/lib/ZoneMinder/General.pm b/scripts/ZoneMinder/lib/ZoneMinder/General.pm
index 8cb4428e6..743cc67e4 100644
--- a/scripts/ZoneMinder/lib/ZoneMinder/General.pm
+++ b/scripts/ZoneMinder/lib/ZoneMinder/General.pm
@@ -179,7 +179,7 @@ sub runCommand {
sub getEventPath {
my $event = shift;
- my $Storage = new ZoneMinder::Storage( $$event{Id} );
+ my $Storage = new ZoneMinder::Storage( $$event{StorageId} );
my $event_path = join( '/',
$Storage->Path(),
$event->{MonitorId},
diff --git a/scripts/ZoneMinder/lib/ZoneMinder/ONVIF.pm.in b/scripts/ZoneMinder/lib/ZoneMinder/ONVIF.pm.in
new file mode 100644
index 000000000..3b295c45f
--- /dev/null
+++ b/scripts/ZoneMinder/lib/ZoneMinder/ONVIF.pm.in
@@ -0,0 +1,358 @@
+# ==========================================================================
+#
+# ZoneMinder ONVIF Module, $Date$, $Revision$
+# Copyright (C) 2001-2008 Philip Coombes
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# ==========================================================================
+#
+# This module contains the common definitions and functions used by the rest
+# of the ZoneMinder scripts
+#
+package ZoneMinder::ONVIF;
+
+use 5.006;
+use strict;
+use warnings;
+
+require Exporter;
+require ZoneMinder::Base;
+
+our @ISA = qw(Exporter);
+
+our %EXPORT_TAGS = (
+ functions => [ qw(
+ ) ]
+ );
+push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS;
+
+our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
+
+our @EXPORT = qw();
+
+our $VERSION = $ZoneMinder::Base::VERSION;
+
+use Getopt::Std;
+use Data::UUID;
+
+require ONVIF::Client;
+
+require WSDiscovery10::Interfaces::WSDiscovery::WSDiscoveryPort;
+require WSDiscovery10::Elements::Header;
+require WSDiscovery10::Elements::Types;
+require WSDiscovery10::Elements::Scopes;
+
+require WSDiscovery::TransportUDP;
+
+sub deserialize_message {
+ my ($wsdl_client, $response) = @_;
+
+# copied and adapted from SOAP::WSDL::Client
+
+# get deserializer
+ my $deserializer = $wsdl_client->get_deserializer();
+
+ if(! $deserializer) {
+ $deserializer = SOAP::WSDL::Factory::Deserializer->get_deserializer({
+ soap_version => $wsdl_client->get_soap_version(),
+ %{ $wsdl_client->get_deserializer_args() },
+ });
+ }
+# set class resolver if serializer supports it
+ $deserializer->set_class_resolver( $wsdl_client->get_class_resolver() )
+ if ( $deserializer->can('set_class_resolver') );
+
+# Try deserializing response - there may be some,
+# even if transport did not succeed (got a 500 response)
+ if ( $response ) {
+# as our faults are false, returning a success marker is the only
+# reliable way of determining whether the deserializer succeeded.
+# Custom deserializers may return an empty list, or undef,
+# and $@ is not guaranteed to be undefined.
+ my ($success, $result_body, $result_header) = eval {
+ (1, $deserializer->deserialize( $response ));
+ };
+ if (defined $success) {
+ return wantarray
+ ? ($result_body, $result_header)
+ : $result_body;
+ }
+ elsif (blessed $@) { #}&& $@->isa('SOAP::WSDL::SOAP::Typelib::Fault11')) {
+ return $@;
+ }
+ else {
+ return $deserializer->generate_fault({
+ code => 'soap:Server',
+ role => 'urn:localhost',
+ message => "Error deserializing message: $@. \n"
+ . "Message was: \n$response"
+ });
+ }
+ };
+}
+ub interpret_messages {
+ my ($svc_discover, $services, @responses ) = @_;
+
+ my @results;
+ foreach my $response ( @responses ) {
+
+ if($verbose) {
+ print "Received message:\n" . $response . "\n";
+ }
+
+ my $result = deserialize_message($svc_discover, $response);
+ if(not $result) {
+ print "Error deserializing message. No message returned from deserializer.\n" if $verbose;
+ next;
+ }
+
+ my $xaddr;
+ foreach my $l_xaddr (split ' ', $result->get_ProbeMatch()->get_XAddrs()) {
+# find IPv4 address
+ print "l_xaddr = $l_xaddr\n" if $verbose;
+ if($l_xaddr =~ m|//[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[:/]|) {
+ $xaddr = $l_xaddr;
+ last;
+ } else {
+ print STDERR "Unable to find IPv4 address from xaddr $l_xaddr\n";
+ }
+ }
+
+# No usable address found
+ next if not $xaddr;
+
+# ignore multiple responses from one service
+ next if defined $services->{$xaddr};
+ $services->{$xaddr} = 1;
+
+ print "$xaddr, " . $svc_discover->get_soap_version() . ", ";
+
+ print "(";
+ my $scopes = $result->get_ProbeMatch()->get_Scopes();
+ my $count = 0;
+ my %scopes;
+ foreach my $scope(split ' ', $scopes) {
+ if($scope =~ m|onvif://www\.onvif\.org/(.+)/(.*)|) {
+ my ($attr, $value) = ($1,$2);
+ if( 0 < $count ++) {
+ print ", ";
+ }
+ print $attr . "=\'" . $value . "\'";
+ $scopes{$attr} = $value;
+ }
+ }
+ print ")\n";
+ push @results, { xaddr=>$xaddr,
+ soap_version => $svc_discover->get_soap_version(),
+ scopes => \%scopes,
+ };
+
+ }
+ return @results;
+} # end sub interpret_messages
+
+# functions
+
+sub discover {
+ my @results;
+
+## collect all responses
+ my @responses = ();
+
+ no warnings 'redefine';
+
+ *WSDiscovery::TransportUDP::_notify_response = sub {
+ my ($transport, $response) = @_;
+ push @responses, $response;
+ };
+
+## try both soap versions
+ my %services;
+
+ my $uuid_gen = Data::UUID->new();
+
+ if ( ( ! $soap_version ) or ( $soap_version eq '1.1' ) ) {
+
+ if($verbose) {
+ print "Probing for SOAP 1.1\n"
+ }
+ my $svc_discover = WSDiscovery10::Interfaces::WSDiscovery::WSDiscoveryPort->new({
+# no_dispatch => '1',
+ });
+ $svc_discover->set_soap_version('1.1');
+
+ my $uuid = $uuid_gen->create_str();
+
+ my $result = $svc_discover->ProbeOp(
+ { # WSDiscovery::Types::ProbeType
+ Types => 'http://www.onvif.org/ver10/network/wsdl:NetworkVideoTransmitter http://www.onvif.org/ver10/device/wsdl:Device', # QNameListType
+ Scopes => { value => '' },
+ },
+ WSDiscovery10::Elements::Header->new({
+ Action => { value => 'http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe' },
+ MessageID => { value => "urn:uuid:$uuid" },
+ To => { value => 'urn:schemas-xmlsoap-org:ws:2005:04:discovery' },
+ })
+ );
+ print $result . "\n" if $verbose;
+
+ push @results, interpret_messages($svc_discover, \%services, @responses);
+ @responses = ();
+ } # end if doing soap 1.1
+
+ if ( ( ! $soap_version ) or ( $soap_version eq '1.2' ) ) {
+ if($verbose) {
+ print "Probing for SOAP 1.2\n"
+ }
+ my $svc_discover = WSDiscovery10::Interfaces::WSDiscovery::WSDiscoveryPort->new({
+# no_dispatch => '1',
+ });
+ $svc_discover->set_soap_version('1.2');
+
+# copies of the same Probe message must have the same MessageID.
+# This is not a copy. So we generate a new uuid.
+ my $uuid = $uuid_gen->create_str();
+
+# Everyone else, like the nodejs onvif code and odm only ask for NetworkVideoTransmitter
+ my $result = $svc_discover->ProbeOp(
+ { # WSDiscovery::Types::ProbeType
+ xmlattr => { 'xmlns:dn' => 'http://www.onvif.org/ver10/network/wsdl', },
+ Types => 'dn:NetworkVideoTransmitter', # QNameListType
+ Scopes => { value => '' },
+ },
+ WSDiscovery10::Elements::Header->new({
+ Action => { value => 'http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe' },
+ MessageID => { value => "urn:uuid:$uuid" },
+ To => { value => 'urn:schemas-xmlsoap-org:ws:2005:04:discovery' },
+ })
+ );
+ print $result . "\n" if $verbose;
+ push @results, interpret_messages($svc_discover, \%services, @responses);
+ } # end if doing soap 1.2
+ return @results;
+}
+
+sub profiles {
+ my $result = $client->get_endpoint('media')->GetProfiles( { } ,, );
+ die $result if not $result;
+ if($verbose) {
+ print "Received message:\n" . $result . "\n";
+ }
+
+ my $profiles = $result->get_Profiles();
+
+ foreach my $profile ( @{ $profiles } ) {
+
+ my $token = $profile->attr()->get_token() ;
+ print $token . ", " .
+ $profile->get_Name() . ", " .
+ $profile->get_VideoEncoderConfiguration()->get_Encoding() . ", " .
+ $profile->get_VideoEncoderConfiguration()->get_Resolution()->get_Width() . ", " .
+ $profile->get_VideoEncoderConfiguration()->get_Resolution()->get_Height() . ", " .
+ $profile->get_VideoEncoderConfiguration()->get_RateControl()->get_FrameRateLimit() .
+ ", ";
+
+# Specification gives conflicting values for unicast stream types, try both.
+# http://www.onvif.org/onvif/ver10/media/wsdl/media.wsdl#op.GetStreamUri
+ foreach my $streamtype ( 'RTP_unicast', 'RTP-Unicast' ) {
+ $result = $client->get_endpoint('media')->GetStreamUri( {
+ StreamSetup => { # ONVIF::Media::Types::StreamSetup
+ Stream => $streamtype, # StreamType
+ Transport => { # ONVIF::Media::Types::Transport
+ Protocol => 'RTSP', # TransportProtocol
+ },
+ },
+ ProfileToken => $token, # ReferenceToken
+ } ,, );
+ last if $result;
+ }
+ die $result if not $result;
+# print $result . "\n";
+
+ print $result->get_MediaUri()->get_Uri() .
+ "\n";
+ } # end foreach profile
+
+#
+# use message parser without schema validation ???
+#
+
+}
+
+sub move {
+ my ($dir) = @_;
+
+ my $result = $client->get_endpoint('ptz')->GetNodes( { } ,, );
+
+ die $result if not $result;
+ print $result . "\n";
+} # end sub move
+
+sub metadata {
+ my $result = $client->get_endpoint('media')->GetMetadataConfigurations( { } ,, );
+ die $result if not $result;
+ print $result . "\n";
+
+ $result = $client->get_endpoint('media')->GetVideoAnalyticsConfigurations( { } ,, );
+ die $result if not $result;
+ print $result . "\n";
+
+# $result = $client->get_endpoint('analytics')->GetServiceCapabilities( { } ,, );
+# die $result if not $result;
+# print $result . "\n";
+
+}
+
+
+
+1;
+__END__
+
+=head1 NAME
+
+ZoneMinder::ONVIF - perl module to access onvif functions for ZoneMinder
+
+=head1 SYNOPSIS
+
+use ZoneMinder::ONVIF;
+
+=head1 DESCRIPTION
+
+This is a module to contain useful functions and import all the other modules
+required for ONVIF to work.
+
+=head2 EXPORT
+
+None by default.
+
+=head1 SEE ALSO
+
+http://www.zoneminder.com
+
+=head1 AUTHOR
+
+Philip Coombes, Ephilip.coombes@zoneminder.comE
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2001-2008 Philip Coombes
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.8.3 or,
+at your option, any later version of Perl 5 you may have available.
+
+
+=cut
diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Object.pm b/scripts/ZoneMinder/lib/ZoneMinder/Object.pm
index d79465e86..ef9ee9714 100644
--- a/scripts/ZoneMinder/lib/ZoneMinder/Object.pm
+++ b/scripts/ZoneMinder/lib/ZoneMinder/Object.pm
@@ -45,70 +45,69 @@ use ZoneMinder::Database qw(:all);
use vars qw/ $AUTOLOAD /;
sub new {
- my ( $parent, $id, $data ) = @_;
+ my ( $parent, $id, $data ) = @_;
- my $self = {};
- bless $self, $parent;
- no strict 'refs';
- my $primary_key = ${$parent.'::primary_key'};
- if ( ! $primary_key ) {
- Error( 'NO primary_key for type ' . $parent );
- return;
- } # end if
- if ( ( $$self{$primary_key} = $id ) or $data ) {
+ my $self = {};
+ bless $self, $parent;
+ no strict 'refs';
+ my $primary_key = ${$parent.'::primary_key'};
+ if ( ! $primary_key ) {
+ Error( 'NO primary_key for type ' . $parent );
+ return;
+ } # end if
+ if ( ( $$self{$primary_key} = $id ) or $data ) {
#$log->debug("loading $parent $id") if $debug or DEBUG_ALL;
- $self->load( $data );
- }
- return $self;
+ $self->load( $data );
+ }
+ return $self;
} # end sub new
sub load {
- my ( $self, $data ) = @_;
- my $type = ref $self;
- if ( ! $data ) {
- no strict 'refs';
- my $table = ${$type.'::table'};
- if ( ! $table ) {
- Error( 'NO table for type ' . $type );
- return;
- } # end if
- my $primary_key = ${$type.'::primary_key'};
- if ( ! $primary_key ) {
- Error( 'NO primary_key for type ' . $type );
- return;
- } # end if
+ my ( $self, $data ) = @_;
+ my $type = ref $self;
+ if ( ! $data ) {
+ no strict 'refs';
+ my $table = ${$type.'::table'};
+ if ( ! $table ) {
+ Error( 'NO table for type ' . $type );
+ return;
+ } # end if
+ my $primary_key = ${$type.'::primary_key'};
+ if ( ! $primary_key ) {
+ Error( 'NO primary_key for type ' . $type );
+ return;
+ } # end if
- if ( ! $$self{$primary_key} ) {
- my ( $caller, undef, $line ) = caller;
- Error( (ref $self) . "::load called without $primary_key from $caller:$line");
- } else {
- #$log->debug("Object::load Loading from db $type");
- Debug("Loading $type from $table WHERE $primary_key = $$self{$primary_key}");
- $data = $ZoneMinder::Database::dbh->selectrow_hashref( "SELECT * FROM $table WHERE $primary_key=?", {}, $$self{$primary_key} );
- if ( ! $data ) {
- if ( $ZoneMinder::Database::dbh->errstr ) {
- Error( "Failure to load Object record for $$self{$primary_key}: Reason: " . $ZoneMinder::Database::dbh->errstr );
- } else {
- Debug("No Results Loading $type from $table WHERE $primary_key = $$self{$primary_key}");
-
- } # end if
- } # end if
- } # end if
- } # end if ! $data
- if ( $data and %$data ) {
- @$self{keys %$data} = values %$data;
- } # end if
+ if ( ! $$self{$primary_key} ) {
+ my ( $caller, undef, $line ) = caller;
+ Error( (ref $self) . "::load called without $primary_key from $caller:$line");
+ } else {
+#$log->debug("Object::load Loading from db $type");
+ Debug("Loading $type from $table WHERE $primary_key = $$self{$primary_key}");
+ $data = $ZoneMinder::Database::dbh->selectrow_hashref( "SELECT * FROM $table WHERE $primary_key=?", {}, $$self{$primary_key} );
+ if ( ! $data ) {
+ if ( $ZoneMinder::Database::dbh->errstr ) {
+ Error( "Failure to load Object record for $$self{$primary_key}: Reason: " . $ZoneMinder::Database::dbh->errstr );
+ } else {
+ Debug("No Results Loading $type from $table WHERE $primary_key = $$self{$primary_key}");
+ } # end if
+ } # end if
+ } # end if
+ } # end if ! $data
+ if ( $data and %$data ) {
+ @$self{keys %$data} = values %$data;
+ } # end if
} # end sub load
sub AUTOLOAD {
- my ( $self, $newvalue ) = @_;
- my $type = ref($_[0]);
- my $name = $AUTOLOAD;
- $name =~ s/.*://;
- if ( @_ > 1 ) {
- return $_[0]{$name} = $_[1];
- }
-return $_[0]{$name};
+ my ( $self, $newvalue ) = @_;
+ my $type = ref($_[0]);
+ my $name = $AUTOLOAD;
+ $name =~ s/.*://;
+ if ( @_ > 1 ) {
+ return $_[0]{$name} = $_[1];
+ }
+ return $_[0]{$name};
}
diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in
index d711d4264..cee574975 100644
--- a/scripts/zmaudit.pl.in
+++ b/scripts/zmaudit.pl.in
@@ -41,10 +41,12 @@ yet.
=head1 OPTIONS
- -r, --report - Just report don't actually do anything
- -i, --interactive - Ask before applying any changes
-c, --continuous - Run continuously
+ -i, --interactive - Ask before applying any changes
+ -m, --monitor_id - Restrict zmaudit actions to events pertaining to the specified monitor id
+ -r, --report - Just report don't actually do anything
-v, --version - Print the installed version of ZoneMinder
+
=cut
use strict;
@@ -89,15 +91,17 @@ delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
my $report = 0;
my $interactive = 0;
my $continuous = 0;
+my $monitor_id = 0;
my $version;
logInit();
logSetSignal();
GetOptions(
- 'report' =>\$report,
'interactive' =>\$interactive,
'continuous' =>\$continuous,
+ 'monitor_id=s' =>\$monitor_id,
+ 'report' =>\$report,
'version' =>\$version
) or pod2usage(-exitstatus => -1);
@@ -153,7 +157,7 @@ MAIN: while( $loop ) {
my %Monitors;
my $db_monitors;
- my $monitorSelectSql = 'select * from Monitors order by Id';
+ my $monitorSelectSql = $monitor_id ? 'SELECT * FRO Monitors WHERE Id=?' : 'SELECT * FROM Monitors ORDER BY Id';
my $monitorSelectSth = $dbh->prepare_cached( $monitorSelectSql )
or Fatal( "Can't prepare '$monitorSelectSql': ".$dbh->errstr() );
@@ -163,7 +167,7 @@ MAIN: while( $loop ) {
or Fatal( "Can't prepare '$eventSelectSql': ".$dbh->errstr() );
$cleaned = 0;
- my $res = $monitorSelectSth->execute()
+ my $res = $monitorSelectSth->execute( $monitor_id ? $monitor_id : () )
or Fatal( "Can't execute: ".$monitorSelectSth->errstr() );
while( my $monitor = $monitorSelectSth->fetchrow_hashref() ) {
$Monitors{$$monitor{Id}} = $monitor;
@@ -267,7 +271,7 @@ MAIN: while( $loop ) {
} # if USE_DEEP_STORAGE
Debug( 'Got '.int(keys(%$fs_events))." events for monitor $monitor_dir\n" );
- delete_empty_directories( $monitor_dir );
+ #delete_empty_directories( $monitor_dir );
} # end foreach monitor
redo MAIN if ( $cleaned );
@@ -334,7 +338,10 @@ MAIN: while( $loop ) {
my $deleteStatsSth = $dbh->prepare_cached( $deleteStatsSql )
or Fatal( "Can't prepare '$deleteStatsSql': ".$dbh->errstr() );
+ # Foreach database monitor and it's list of events.
while ( my ( $db_monitor, $db_events ) = each(%$db_monitors) ) {
+
+ # If we found the monitor in the file system
if ( my $fs_events = $fs_monitors->{$db_monitor} ) {
next if ! $db_events;
@@ -351,7 +358,7 @@ Debug("Event $db_event is not in fs.");
next;
}
if ( $Event->check_for_in_filesystem() ) {
- Debug('Database events apparent exists at ' . $Event->Path() );
+ Debug('Database events apparently exists at ' . $Event->Path() );
} else {
if ( $age > $Config{ZM_AUDIT_MIN_AGE} ) {
aud_print( "Database event '$db_monitor/$db_event' does not exist at " . $Event->Path().' in filesystem' );
@@ -653,6 +660,7 @@ sub deleteSwapImage {
sub delete_empty_directories {
my $DIR;
+ Debug("delete_empty_directories $_[0]");
if ( ! opendir( $DIR, $_[0] ) ) {
Error( "delete_empty_directories: Can't open directory '".getcwd()."/$_[0]': $!" );
return;
diff --git a/scripts/zmdc.pl.in b/scripts/zmdc.pl.in
index b966ac536..39680a7ed 100644
--- a/scripts/zmdc.pl.in
+++ b/scripts/zmdc.pl.in
@@ -209,6 +209,7 @@ use ZoneMinder;
use POSIX;
use Socket;
use IO::Handle;
+use Time::HiRes qw(usleep);
#use Data::Dumper;
our %cmd_hash;
@@ -456,20 +457,25 @@ sub send_stop {
} # end sub send_stop
sub kill_until_dead {
- my ( $process ) = @_;
- # Now check it has actually gone away, if not kill -9 it
- my $count = 0;
- while( $process and $$process{pid} and kill( 0, $$process{pid} ) ) {
- if ( $count++ > 5 ) {
- dPrint( ZoneMinder::Logger::WARNING, "'$$process{command}' has not stopped at "
- .strftime( '%y/%m/%d %H:%M:%S', localtime() )
- .". Sending KILL to pid $$process{pid}\n"
- );
- kill( 'KILL', $$process{pid} );
- }
-
- sleep( 1 );
+ my ( $process ) = @_;
+# Now check it has actually gone away, if not kill -9 it
+ my $count = 0;
+ my $sigset = POSIX::SigSet->new;
+ my $blockset = POSIX::SigSet->new(SIGCHLD);
+ sigprocmask(SIG_BLOCK, $blockset, $sigset ) or die "dying at block...\n";
+ while( $process and $$process{pid} and kill( 0, $$process{pid} ) ) {
+ if ( $count++ > 50 ) {
+ dPrint( ZoneMinder::Logger::WARNING, "'$$process{command}' has not stopped at "
+ .strftime( '%y/%m/%d %H:%M:%S', localtime() )
+ .". Sending KILL to pid $$process{pid}\n"
+ );
+ kill( 'KILL', $$process{pid} );
}
+
+ sigprocmask(SIG_UNBLOCK, $blockset) or die "dying at unblock...\n";
+ usleep( 100 );
+ sigprocmask(SIG_BLOCK, $blockset, $sigset ) or die "dying at block...\n";
+ }
}
sub _stop {
diff --git a/scripts/zmfilter.pl.in b/scripts/zmfilter.pl.in
index cbff891ee..245faf23c 100644
--- a/scripts/zmfilter.pl.in
+++ b/scripts/zmfilter.pl.in
@@ -27,7 +27,7 @@ zmfilter.pl - ZoneMinder tool to filter events
=head1 SYNOPSIS
- zmfilter.pl [-f ,--filter=] [--filter_id=] | -v, --version
+zmfilter.pl [-f ,--filter=] [--filter_id=] | -v, --version
=head1 DESCRIPTION
@@ -37,10 +37,10 @@ matching events.
=head1 OPTIONS
-
- -f{filter name}, --filter={filter name} - The name of a specific filter to run
- --filter_id={filter id} - The id of a specific filter to run
- -v, --version - Print ZoneMinder version
+
+-f{filter name}, --filter={filter name} - The name of a specific filter to run
+--filter_id={filter id} - The id of a specific filter to run
+-v, --version - Print ZoneMinder version
=cut
use strict;
@@ -78,71 +78,56 @@ GetOptions(
'filter=s' =>\$filter_name,
'filter_id=s' =>\$filter_id,
'version' =>\$version
-) or pod2usage(-exitstatus => -1);
+ ) or pod2usage(-exitstatus => -1);
if ( $version ) {
- print ZoneMinder::Base::ZM_VERSION . "\n";
- exit(0);
+ print ZoneMinder::Base::ZM_VERSION . "\n";
+ exit(0);
}
require ZoneMinder::Event;
require ZoneMinder::Filter;
use constant EVENT_PATH => ($Config{ZM_DIR_EVENTS}=~m|/|)
- ? $Config{ZM_DIR_EVENTS}
- : ($Config{ZM_PATH_WEB}.'/'.$Config{ZM_DIR_EVENTS})
-;
+ ? $Config{ZM_DIR_EVENTS}
+ : ($Config{ZM_PATH_WEB}.'/'.$Config{ZM_DIR_EVENTS})
+ ;
-logInit();
-logSetSignal();
+ logInit();
+ logSetSignal();
-if ( $Config{ZM_OPT_UPLOAD} )
-{
- # Comment these out if you don't have them and don't want to upload
- # or don't want to use that format
- if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq "zip" )
- {
- require Archive::Zip;
- import Archive::Zip qw( :ERROR_CODES :CONSTANTS );
- }
- else
- {
- require Archive::Tar;
- }
- if ( $Config{ZM_UPLOAD_PROTOCOL} eq "ftp" )
- {
- require Net::FTP;
- }
- else
- {
- require Net::SFTP::Foreign;
- }
+if ( $Config{ZM_OPT_UPLOAD} ) {
+# Comment these out if you don't have them and don't want to upload
+# or don't want to use that format
+ if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq "zip" ) {
+ require Archive::Zip;
+ import Archive::Zip qw( :ERROR_CODES :CONSTANTS );
+ } else {
+ require Archive::Tar;
+ }
+ if ( $Config{ZM_UPLOAD_PROTOCOL} eq "ftp" ) {
+ require Net::FTP;
+ } else {
+ require Net::SFTP::Foreign;
+ }
}
-if ( $Config{ZM_OPT_EMAIL} )
-{
- if ( $Config{ZM_NEW_MAIL_MODULES} )
- {
- require MIME::Lite;
- require Net::SMTP;
- }
- else
- {
- require MIME::Entity;
- }
+if ( $Config{ZM_OPT_EMAIL} ) {
+ if ( $Config{ZM_NEW_MAIL_MODULES} ) {
+ require MIME::Lite;
+ require Net::SMTP;
+ } else {
+ require MIME::Entity;
+ }
}
-if ( $Config{ZM_OPT_MESSAGE} )
-{
- if ( $Config{ZM_NEW_MAIL_MODULES} )
- {
- require MIME::Lite;
- require Net::SMTP;
- }
- else
- {
- require MIME::Entity;
- }
+if ( $Config{ZM_OPT_MESSAGE} ) {
+ if ( $Config{ZM_NEW_MAIL_MODULES} ) {
+ require MIME::Lite;
+ require Net::SMTP;
+ } else {
+ require MIME::Entity;
+ }
}
$| = 1;
@@ -155,8 +140,8 @@ my $delay = $Config{ZM_FILTER_EXECUTE_INTERVAL};
my $event_id = 0;
if ( ! EVENT_PATH ) {
- Error( "No event path defined. Config was $Config{ZM_DIR_EVENTS}\n" );
- die;
+ Error( "No event path defined. Config was $Config{ZM_DIR_EVENTS}\n" );
+ die;
}
chdir( EVENT_PATH );
@@ -164,830 +149,814 @@ chdir( EVENT_PATH );
my $dbh = zmDbConnect();
if ( $filter_name ) {
- Info( "Scanning for events using filter '$filter_name'\n" );
+ Info( "Scanning for events using filter '$filter_name'\n" );
} elsif ( $filter_id ) {
- Info( "Scanning for events using filter id '$filter_id'\n" );
+ Info( "Scanning for events using filter id '$filter_id'\n" );
} else {
- Info( "Scanning for events\n" );
+ Info( "Scanning for events using all filters\n" );
}
if ( ! ( $filter_name or $filter_id ) ) {
- sleep( START_DELAY );
+ sleep( START_DELAY );
}
my $filters;
my $last_action = 0;
while( 1 ) {
- my $now = time;
- if ( ($now - $last_action) > $Config{ZM_FILTER_RELOAD_DELAY} ) {
- Debug( "Reloading filters\n" );
- $last_action = $now;
- $filters = getFilters( { Name=>$filter_name, Id=>$filter_id } );
+ my $now = time;
+ if ( ($now - $last_action) > $Config{ZM_FILTER_RELOAD_DELAY} ) {
+ Debug( "Reloading filters\n" );
+ $last_action = $now;
+ $filters = getFilters( { Name=>$filter_name, Id=>$filter_id } );
+ }
+
+ foreach my $filter ( @$filters ) {
+ if ( $$filter{Concurrent} and ! ( $filter_id or $filter_name ) ) {
+ my ( $proc ) = $0 =~ /(\S+)/;
+ my ( $id ) = $$filter{Id} =~ /(\d+)/;
+ Debug("Running concurrent filter process $proc --filter_id $$filter{Id} => $id for $$filter{Name}");
+
+ system( qq`$proc --filter "$$filter{Name}" &` );
+ } else {
+ checkFilter( $filter );
}
+ }
- foreach my $filter ( @$filters ) {
- if ( $$filter{Concurrent} and ! ( $filter_id or $filter_name ) ) {
- my ( $proc ) = $0 =~ /(\S+)/;
- my ( $id ) = $$filter{Id} =~ /(\d+)/;
- Debug("Running concurrent filter process $proc --filter_id $$filter{Id} => $id for $$filter{Name}");
+ last if ( $filter_name or $filter_id );
- system( qq`$proc --filter "$$filter{Name}" &` );
- } else {
- checkFilter( $filter );
- }
- }
-
- last if ( $filter_name or $filter_id );
-
- Debug( "Sleeping for $delay seconds\n" );
- sleep( $delay );
+ Debug( "Sleeping for $delay seconds\n" );
+ sleep( $delay );
}
-sub getFilters
-{
- my $sql_filters = @_ ? shift : {};
- my @sql_values;
+sub getFilters {
+ my $sql_filters = @_ ? shift : {};
+ my @sql_values;
- my @filters;
- my $sql = "SELECT * FROM Filters WHERE";
- if ( $$sql_filters{Name} ) {
- $sql .= " Name = ? and";
- push @sql_values, $$sql_filters{Name};
- } elsif ( $$sql_filters{Id} ) {
- $sql .= " Id = ? and";
- push @sql_values, $$sql_filters{Id};
- } else {
- $sql .= " Background = 1 and";
+ my @filters;
+ my $sql = "SELECT * FROM Filters WHERE";
+ if ( $$sql_filters{Name} ) {
+ $sql .= " Name = ? and";
+ push @sql_values, $$sql_filters{Name};
+ } elsif ( $$sql_filters{Id} ) {
+ $sql .= " Id = ? and";
+ push @sql_values, $$sql_filters{Id};
+ } else {
+ $sql .= " Background = 1 and";
+ }
+ $sql .= "( AutoArchive = 1
+ or AutoVideo = 1
+ or AutoUpload = 1
+ or AutoEmail = 1
+ or AutoMessage = 1
+ or AutoExecute = 1
+ or AutoDelete = 1
+ ) ORDER BY Name";
+ my $sth = $dbh->prepare_cached( $sql )
+ or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
+ my $res = $sth->execute( @sql_values )
+ or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+FILTER: while( my $db_filter = $sth->fetchrow_hashref() ) {
+ my $filter = new ZoneMinder::Filter( $$db_filter{Id}, $db_filter );
+ Debug( "Found filter '$db_filter->{Name}'\n" );
+ my $sql = $filter->Sql();
+
+ if ( ! $sql ) {
+ Error( "Error parsing Sql. skipping filter '$db_filter->{Name}'\n" );
+ next FILTER;
}
- $sql .= "( AutoArchive = 1
- or AutoVideo = 1
- or AutoUpload = 1
- or AutoEmail = 1
- or AutoMessage = 1
- or AutoExecute = 1
- or AutoDelete = 1
- ) ORDER BY Name";
- my $sth = $dbh->prepare_cached( $sql )
+ push( @filters, $filter );
+ }
+ $sth->finish();
+ if ( ! @filters ) {
+ Warning("No filter found for $sql with values(@sql_values)");
+ } else {
+ Debug( "Got " . @filters . " filters" );
+ }
+
+ return( \@filters );
+}
+
+sub checkFilter {
+ my $filter = shift;
+
+ my @Events = $filter->Execute();
+ Info( join( "Checking filter '$filter->{Name}'",
+ ($filter->{AutoDelete}?", delete":""),
+ ($filter->{AutoArchive}?", archive":""),
+ ($filter->{AutoVideo}?", video":""),
+ ($filter->{AutoUpload}?", upload":""),
+ ($filter->{AutoEmail}?", email":""),
+ ($filter->{AutoMessage}?", message":""),
+ ($filter->{AutoExecute}?", execute":""),
+ " returned " , scalar @Events , ' events',
+ "\n",
+ ) );
+
+ foreach my $event ( @Events ) {
+ Debug( "Checking event $event->{Id}\n" );
+ my $delete_ok = !undef;
+ $dbh->ping();
+ if ( $filter->{AutoArchive} ) {
+ Info( "Archiving event $event->{Id}\n" );
+# Do it individually to avoid locking up the table for new events
+ my $sql = "update Events set Archived = 1 where Id = ?";
+ my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
- my $res = $sth->execute( @sql_values )
- or Fatal( "Can't execute '$sql': ".$sth->errstr() );
- FILTER: while( my $db_filter = $sth->fetchrow_hashref() )
- {
- my $filter = new ZoneMinder::Filter( $$db_filter{Id}, $db_filter );
- Debug( "Found filter '$db_filter->{Name}'\n" );
- my $sql = $filter->Sql();
-
- if ( ! $sql ) {
- Error( "Error parsing Sql. skipping filter '$db_filter->{Name}'\n" );
- next FILTER;
- }
- push( @filters, $filter );
+ my $res = $sth->execute( $event->{Id} )
+ or Error( "Can't execute '$sql': ".$sth->errstr() );
}
- $sth->finish();
- if ( ! @filters ) {
- Warning("No filter found for $sql with values(@sql_values)");
- } else {
- Debug( "Got " . @filters . " filters" );
+ if ( $Config{ZM_OPT_FFMPEG} && $filter->{AutoVideo} ) {
+ if ( !$event->{Videoed} ) {
+ $delete_ok = undef if ( !generateVideo( $filter, $event ) );
+ }
}
-
- return( \@filters );
-}
-
-sub checkFilter
-{
- my $filter = shift;
-
- Debug( "Checking filter '$filter->{Name}'".
- ($filter->{AutoDelete}?", delete":"").
- ($filter->{AutoArchive}?", archive":"").
- ($filter->{AutoVideo}?", video":"").
- ($filter->{AutoUpload}?", upload":"").
- ($filter->{AutoEmail}?", email":"").
- ($filter->{AutoMessage}?", message":"").
- ($filter->{AutoExecute}?", execute":"").
- "\n"
- );
-
- foreach my $event ( $filter->Execute() ) {
- Debug( "Checking event $event->{Id}\n" );
- my $delete_ok = !undef;
-$dbh->ping();
- if ( $filter->{AutoArchive} )
- {
- Info( "Archiving event $event->{Id}\n" );
- # Do it individually to avoid locking up the table for new events
- my $sql = "update Events set Archived = 1 where Id = ?";
- my $sth = $dbh->prepare_cached( $sql )
- or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
- my $res = $sth->execute( $event->{Id} )
- or Error( "Can't execute '$sql': ".$sth->errstr() );
- }
- if ( $Config{ZM_OPT_FFMPEG} && $filter->{AutoVideo} )
- {
- if ( !$event->{Videoed} )
- {
- $delete_ok = undef if ( !generateVideo( $filter, $event ) );
- }
- }
- if ( $Config{ZM_OPT_EMAIL} && $filter->{AutoEmail} )
- {
- if ( !$event->{Emailed} )
- {
- $delete_ok = undef if ( !sendEmail( $filter, $event ) );
- }
- }
- if ( $Config{ZM_OPT_MESSAGE} && $filter->{AutoMessage} )
- {
- if ( !$event->{Messaged} )
- {
- $delete_ok = undef if ( !sendMessage( $filter, $event ) );
- }
- }
- if ( $Config{ZM_OPT_UPLOAD} && $filter->{AutoUpload} )
- {
- if ( !$event->{Uploaded} )
- {
- $delete_ok = undef if ( !uploadArchFile( $filter, $event ) );
- }
- }
- if ( $filter->{AutoExecute} )
- {
- if ( !$event->{Executed} )
- {
- $delete_ok = undef if ( !executeCommand( $filter, $event ) );
- }
- }
- if ( $filter->{AutoDelete} )
- {
- if ( $delete_ok )
- {
- my $Event = new ZoneMinder::Event( $$event{Id}, $event );
- $Event->delete();
- }
- else
- {
- Error( "Unable to delete event $event->{Id} as previous operations failed\n" );
- }
- }
+ if ( $Config{ZM_OPT_EMAIL} && $filter->{AutoEmail} ) {
+ if ( !$event->{Emailed} ) {
+ $delete_ok = undef if ( !sendEmail( $filter, $event ) );
+ }
}
+ if ( $Config{ZM_OPT_MESSAGE} && $filter->{AutoMessage} ) {
+ if ( !$event->{Messaged} ) {
+ $delete_ok = undef if ( !sendMessage( $filter, $event ) );
+ }
+ }
+ if ( $Config{ZM_OPT_UPLOAD} && $filter->{AutoUpload} ) {
+ if ( !$event->{Uploaded} ) {
+ $delete_ok = undef if ( !uploadArchFile( $filter, $event ) );
+ }
+ }
+ if ( $filter->{AutoExecute} ) {
+ if ( !$event->{Executed} ) {
+ $delete_ok = undef if ( !executeCommand( $filter, $event ) );
+ }
+ }
+ if ( $filter->{AutoDelete} ) {
+ if ( $delete_ok ) {
+ my $Event = new ZoneMinder::Event( $$event{Id}, $event );
+ $Event->delete();
+ } else {
+ Error( "Unable to delete event $event->{Id} as previous operations failed\n" );
+ }
+ } # end if AutoDelete
+ } # end foreach event
}
sub generateVideo
{
- my $filter = shift;
- my $event = shift;
- my $phone = shift;
+ my $filter = shift;
+ my $event = shift;
+ my $phone = shift;
- my $rate = $event->{DefaultRate}/100;
- my $scale = $event->{DefaultScale}/100;
- my $format;
+ my $rate = $event->{DefaultRate}/100;
+ my $scale = $event->{DefaultScale}/100;
+ my $format;
- my @ffmpeg_formats = split( /\s+/, $Config{ZM_FFMPEG_FORMATS} );
- my $default_video_format;
- my $default_phone_format;
- foreach my $ffmpeg_format( @ffmpeg_formats )
+ my @ffmpeg_formats = split( /\s+/, $Config{ZM_FFMPEG_FORMATS} );
+ my $default_video_format;
+ my $default_phone_format;
+ foreach my $ffmpeg_format( @ffmpeg_formats )
+ {
+ if ( $ffmpeg_format =~ /^(.+)\*\*$/ )
{
- if ( $ffmpeg_format =~ /^(.+)\*\*$/ )
- {
- $default_phone_format = $1;
- }
- elsif ( $ffmpeg_format =~ /^(.+)\*$/ )
- {
- $default_video_format = $1;
- }
+ $default_phone_format = $1;
}
+ elsif ( $ffmpeg_format =~ /^(.+)\*$/ )
+ {
+ $default_video_format = $1;
+ }
+ }
- if ( $phone && $default_phone_format )
- {
- $format = $default_phone_format;
- }
- elsif ( $default_video_format )
- {
- $format = $default_video_format;
- }
- else
- {
- $format = $ffmpeg_formats[0];
- }
+ if ( $phone && $default_phone_format )
+ {
+ $format = $default_phone_format;
+ }
+ elsif ( $default_video_format )
+ {
+ $format = $default_video_format;
+ }
+ else
+ {
+ $format = $ffmpeg_formats[0];
+ }
- my $command = $Config{ZM_PATH_BIN}."/zmvideo.pl -e "
- .$event->{Id}." -r ".$rate." -s ".$scale." -f ".$format;
- my $output = qx($command);
- chomp( $output );
- my $status = $? >> 8;
- if ( $status || logDebugging() )
+ my $command = $Config{ZM_PATH_BIN}."/zmvideo.pl -e "
+ .$event->{Id}." -r ".$rate." -s ".$scale." -f ".$format;
+ my $output = qx($command);
+ chomp( $output );
+ my $status = $? >> 8;
+ if ( $status || logDebugging() )
+ {
+ Debug( "Output: $output\n" );
+ }
+ if ( $status )
+ {
+ Error( "Video generation '$command' failed with status: $status\n" );
+ if ( wantarray() )
{
- Debug( "Output: $output\n" );
+ return( undef, undef );
}
- if ( $status )
+ return( 0 );
+ }
+ else
+ {
+ my $sql = "update Events set Videoed = 1 where Id = ?";
+ my $sth = $dbh->prepare_cached( $sql )
+ or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
+ my $res = $sth->execute( $event->{Id} )
+ or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+ if ( wantarray() )
{
- Error( "Video generation '$command' failed with status: $status\n" );
- if ( wantarray() )
- {
- return( undef, undef );
- }
- return( 0 );
+ return( $format, $output );
}
- else
- {
- my $sql = "update Events set Videoed = 1 where Id = ?";
- my $sth = $dbh->prepare_cached( $sql )
- or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
- my $res = $sth->execute( $event->{Id} )
- or Fatal( "Can't execute '$sql': ".$sth->errstr() );
- if ( wantarray() )
- {
- return( $format, $output );
- }
- }
- return( 1 );
+ }
+ return( 1 );
}
sub uploadArchFile
{
- my $filter = shift;
- my $event = shift;
+ my $filter = shift;
+ my $event = shift;
- if ( ! $Config{ZM_UPLOAD_HOST} )
- {
- Error( "Cannot upload archive as no upload host defined" );
- return( 0 );
- }
+ if ( ! $Config{ZM_UPLOAD_HOST} )
+ {
+ Error( "Cannot upload archive as no upload host defined" );
+ return( 0 );
+ }
- my $archFile = $event->{MonitorName}.'-'.$event->{Id};
- my $archImagePath = getEventPath( $event )
- ."/"
- .(
- ( $Config{ZM_UPLOAD_ARCH_ANALYSE} )
- ? '{*analyse,*capture}'
- : '*capture'
- )
- .".jpg"
+ my $archFile = $event->{MonitorName}.'-'.$event->{Id};
+ my $archImagePath = getEventPath( $event )
+ ."/"
+ .(
+ ( $Config{ZM_UPLOAD_ARCH_ANALYSE} )
+ ? '{*analyse,*capture}'
+ : '*capture'
+ )
+ .".jpg"
;
- my @archImageFiles = glob($archImagePath);
- my $archLocPath;
+ my @archImageFiles = glob($archImagePath);
+ my $archLocPath;
- my $archError = 0;
- if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq "zip" )
+ my $archError = 0;
+ if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq "zip" )
+ {
+ $archFile .= '.zip';
+ $archLocPath = $Config{ZM_UPLOAD_LOC_DIR}.'/'.$archFile;
+ my $zip = Archive::Zip->new();
+ Info( "Creating upload file '$archLocPath', ".int(@archImageFiles)." files\n" );
+
+ my $status = &AZ_OK;
+ foreach my $imageFile ( @archImageFiles )
{
- $archFile .= '.zip';
- $archLocPath = $Config{ZM_UPLOAD_LOC_DIR}.'/'.$archFile;
- my $zip = Archive::Zip->new();
- Info( "Creating upload file '$archLocPath', ".int(@archImageFiles)." files\n" );
-
- my $status = &AZ_OK;
- foreach my $imageFile ( @archImageFiles )
- {
- Debug( "Adding $imageFile\n" );
- my $member = $zip->addFile( $imageFile );
- if ( !$member )
- {
- Error( "Unable to add image file $imageFile to zip archive $archLocPath" );
- $archError = 1;
- last;
- }
- $member->desiredCompressionMethod( $Config{ZM_UPLOAD_ARCH_COMPRESS}
- ? &COMPRESSION_DEFLATED
- : &COMPRESSION_STORED
- );
- }
- if ( !$archError )
- {
- $status = $zip->writeToFileNamed( $archLocPath );
-
- if ( $archError = ($status != &AZ_OK) )
- {
- Error( "Zip error: $status\n " );
- }
- }
- else
- {
- Error( "Error adding images to zip archive $archLocPath, not writing" );
- }
+ Debug( "Adding $imageFile\n" );
+ my $member = $zip->addFile( $imageFile );
+ if ( !$member )
+ {
+ Error( "Unable to add image file $imageFile to zip archive $archLocPath" );
+ $archError = 1;
+ last;
+ }
+ $member->desiredCompressionMethod( $Config{ZM_UPLOAD_ARCH_COMPRESS}
+ ? &COMPRESSION_DEFLATED
+ : &COMPRESSION_STORED
+ );
}
- elsif ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq "tar" )
+ if ( !$archError )
{
- if ( $Config{ZM_UPLOAD_ARCH_COMPRESS} )
- {
- $archFile .= '.tar.gz';
- }
- else
- {
- $archFile .= '.tar';
- }
- $archLocPath = $Config{ZM_UPLOAD_LOC_DIR}.'/'.$archFile;
- Info( "Creating upload file '$archLocPath', ".int(@archImageFiles)." files\n" );
+ $status = $zip->writeToFileNamed( $archLocPath );
- if ( $archError = !Archive::Tar->create_archive(
- $archLocPath,
- $Config{ZM_UPLOAD_ARCH_COMPRESS},
- @archImageFiles
- )
- )
- {
- Error( "Tar error: ".Archive::Tar->error()."\n " );
- }
- }
-
- if ( $archError )
- {
- return( 0 );
+ if ( $archError = ($status != &AZ_OK) )
+ {
+ Error( "Zip error: $status\n " );
+ }
}
else
{
- if ( $Config{ZM_UPLOAD_PROTOCOL} eq "ftp" )
- {
- Info( "Uploading to ".$Config{ZM_UPLOAD_HOST}." using FTP\n" );
- my $ftp = Net::FTP->new(
- $Config{ZM_UPLOAD_HOST},
- Timeout=>$Config{ZM_UPLOAD_TIMEOUT},
- Passive=>$Config{ZM_UPLOAD_FTP_PASSIVE},
- Debug=>$Config{ZM_UPLOAD_DEBUG}
- );
- if ( !$ftp )
- {
- Error( "Can't create FTP connection: $@" );
- return( 0 );
- }
- $ftp->login( $Config{ZM_UPLOAD_USER}, $Config{ZM_UPLOAD_PASS} )
- or Error( "FTP - Can't login" );
- $ftp->binary()
- or Error( "FTP - Can't go binary" );
- $ftp->cwd( $Config{ZM_UPLOAD_REM_DIR} )
- or Error( "FTP - Can't cwd" )
- if ( $Config{ZM_UPLOAD_REM_DIR} );
- $ftp->put( $archLocPath )
- or Error( "FTP - Can't upload '$archLocPath'" );
- $ftp->quit()
- or Error( "FTP - Can't quit" );
- }
- else
- {
- my $host = $Config{ZM_UPLOAD_HOST};
- $host .= ":".$Config{ZM_UPLOAD_PORT}
- if $Config{ZM_UPLOAD_PORT};
- Info( "Uploading to ".$host." using SFTP\n" );
- my %sftpOptions = ( host=>$Config{ZM_UPLOAD_HOST}, user=>$Config{ZM_UPLOAD_USER} );
- $sftpOptions{password} = $Config{ZM_UPLOAD_PASS}
- if $Config{ZM_UPLOAD_PASS};
- $sftpOptions{port} = $Config{ZM_UPLOAD_PORT}
- if $Config{ZM_UPLOAD_PORT};
- $sftpOptions{timeout} = $Config{ZM_UPLOAD_TIMEOUT}
- if $Config{ZM_UPLOAD_TIMEOUT};
- my @more_ssh_args;
- push @more_ssh_args, '-o'=>'StrictHostKeyChecking=no'
- if ! $Config{ZM_UPLOAD_STRICT};
- push @more_ssh_args, '-v'
- if $Config{ZM_UPLOAD_DEBUG};
- $sftpOptions{more} = [@more_ssh_args];
- my $sftp = Net::SFTP::Foreign->new( $Config{ZM_UPLOAD_HOST}, %sftpOptions );
- if ( $sftp->error )
- {
- Error( "Can't create SFTP connection: ".$sftp->error );
- return( 0 );
- }
- $sftp->setcwd( $Config{ZM_UPLOAD_REM_DIR} )
- or Error( "SFTP - Can't setcwd: ".$sftp->error )
- if $Config{ZM_UPLOAD_REM_DIR};
- $sftp->put( $archLocPath, $archFile )
- or Error( "SFTP - Can't upload '$archLocPath': ".$sftp->error );
- }
- unlink( $archLocPath );
- my $sql = "update Events set Uploaded = 1 where Id = ?";
- my $sth = $dbh->prepare_cached( $sql )
- or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
- my $res = $sth->execute( $event->{Id} )
- or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+ Error( "Error adding images to zip archive $archLocPath, not writing" );
}
- return( 1 );
+ }
+ elsif ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq "tar" )
+ {
+ if ( $Config{ZM_UPLOAD_ARCH_COMPRESS} )
+ {
+ $archFile .= '.tar.gz';
+ }
+ else
+ {
+ $archFile .= '.tar';
+ }
+ $archLocPath = $Config{ZM_UPLOAD_LOC_DIR}.'/'.$archFile;
+ Info( "Creating upload file '$archLocPath', ".int(@archImageFiles)." files\n" );
+
+ if ( $archError = !Archive::Tar->create_archive(
+ $archLocPath,
+ $Config{ZM_UPLOAD_ARCH_COMPRESS},
+ @archImageFiles
+ )
+ )
+ {
+ Error( "Tar error: ".Archive::Tar->error()."\n " );
+ }
+ }
+
+ if ( $archError )
+ {
+ return( 0 );
+ }
+ else
+ {
+ if ( $Config{ZM_UPLOAD_PROTOCOL} eq "ftp" )
+ {
+ Info( "Uploading to ".$Config{ZM_UPLOAD_HOST}." using FTP\n" );
+ my $ftp = Net::FTP->new(
+ $Config{ZM_UPLOAD_HOST},
+ Timeout=>$Config{ZM_UPLOAD_TIMEOUT},
+ Passive=>$Config{ZM_UPLOAD_FTP_PASSIVE},
+ Debug=>$Config{ZM_UPLOAD_DEBUG}
+ );
+ if ( !$ftp )
+ {
+ Error( "Can't create FTP connection: $@" );
+ return( 0 );
+ }
+ $ftp->login( $Config{ZM_UPLOAD_USER}, $Config{ZM_UPLOAD_PASS} )
+ or Error( "FTP - Can't login" );
+ $ftp->binary()
+ or Error( "FTP - Can't go binary" );
+ $ftp->cwd( $Config{ZM_UPLOAD_REM_DIR} )
+ or Error( "FTP - Can't cwd" )
+ if ( $Config{ZM_UPLOAD_REM_DIR} );
+ $ftp->put( $archLocPath )
+ or Error( "FTP - Can't upload '$archLocPath'" );
+ $ftp->quit()
+ or Error( "FTP - Can't quit" );
+ }
+ else
+ {
+ my $host = $Config{ZM_UPLOAD_HOST};
+ $host .= ":".$Config{ZM_UPLOAD_PORT}
+ if $Config{ZM_UPLOAD_PORT};
+ Info( "Uploading to ".$host." using SFTP\n" );
+ my %sftpOptions = ( host=>$Config{ZM_UPLOAD_HOST}, user=>$Config{ZM_UPLOAD_USER} );
+ $sftpOptions{password} = $Config{ZM_UPLOAD_PASS}
+ if $Config{ZM_UPLOAD_PASS};
+ $sftpOptions{port} = $Config{ZM_UPLOAD_PORT}
+ if $Config{ZM_UPLOAD_PORT};
+ $sftpOptions{timeout} = $Config{ZM_UPLOAD_TIMEOUT}
+ if $Config{ZM_UPLOAD_TIMEOUT};
+ my @more_ssh_args;
+ push @more_ssh_args, '-o'=>'StrictHostKeyChecking=no'
+ if ! $Config{ZM_UPLOAD_STRICT};
+ push @more_ssh_args, '-v'
+ if $Config{ZM_UPLOAD_DEBUG};
+ $sftpOptions{more} = [@more_ssh_args];
+ my $sftp = Net::SFTP::Foreign->new( $Config{ZM_UPLOAD_HOST}, %sftpOptions );
+ if ( $sftp->error )
+ {
+ Error( "Can't create SFTP connection: ".$sftp->error );
+ return( 0 );
+ }
+ $sftp->setcwd( $Config{ZM_UPLOAD_REM_DIR} )
+ or Error( "SFTP - Can't setcwd: ".$sftp->error )
+ if $Config{ZM_UPLOAD_REM_DIR};
+ $sftp->put( $archLocPath, $archFile )
+ or Error( "SFTP - Can't upload '$archLocPath': ".$sftp->error );
+ }
+ unlink( $archLocPath );
+ my $sql = "update Events set Uploaded = 1 where Id = ?";
+ my $sth = $dbh->prepare_cached( $sql )
+ or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
+ my $res = $sth->execute( $event->{Id} )
+ or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+ }
+ return( 1 );
}
sub substituteTags
{
- my $text = shift;
- my $filter = shift;
- my $event = shift;
- my $attachments_ref = shift;
+ my $text = shift;
+ my $filter = shift;
+ my $event = shift;
+ my $attachments_ref = shift;
- # First we'd better check what we need to get
- # We have a filter and an event, do we need any more
- # monitor information?
- my $need_monitor = $text =~ /%(?:MET|MEH|MED|MEW|MEN|MEA)%/;
+# First we'd better check what we need to get
+# We have a filter and an event, do we need any more
+# monitor information?
+ my $need_monitor = $text =~ /%(?:MET|MEH|MED|MEW|MEN|MEA)%/;
- my $monitor = {};
- if ( $need_monitor )
- {
- my $db_now = strftime( "%Y-%m-%d %H:%M:%S", localtime() );
- my $sql = "SELECT
- M.Id,
- count(E.Id) as EventCount,
- count(if(E.Archived,1,NULL))
- as ArchEventCount,
- count(if(E.StartTime>'$db_now' - INTERVAL 1 HOUR && E.Archived = 0,1,NULL))
- as HourEventCount,
- count(if(E.StartTime>'$db_now' - INTERVAL 1 DAY && E.Archived = 0,1,NULL))
- as DayEventCount,
- count(if(E.StartTime>'$db_now' - INTERVAL 7 DAY && E.Archived = 0,1,NULL))
- as WeekEventCount,
- count(if(E.StartTime>'$db_now' - INTERVAL 1 MONTH && E.Archived = 0,1,NULL))
- as MonthEventCount
- FROM Monitors as M LEFT JOIN Events as E on E.MonitorId = M.Id
- WHERE MonitorId = ?
- GROUP BY E.MonitorId
- ORDER BY Id"
+ my $monitor = {};
+ if ( $need_monitor )
+ {
+ my $db_now = strftime( "%Y-%m-%d %H:%M:%S", localtime() );
+ my $sql = "SELECT
+ M.Id,
+ count(E.Id) as EventCount,
+ count(if(E.Archived,1,NULL))
+ as ArchEventCount,
+ count(if(E.StartTime>'$db_now' - INTERVAL 1 HOUR && E.Archived = 0,1,NULL))
+ as HourEventCount,
+ count(if(E.StartTime>'$db_now' - INTERVAL 1 DAY && E.Archived = 0,1,NULL))
+ as DayEventCount,
+ count(if(E.StartTime>'$db_now' - INTERVAL 7 DAY && E.Archived = 0,1,NULL))
+ as WeekEventCount,
+ count(if(E.StartTime>'$db_now' - INTERVAL 1 MONTH && E.Archived = 0,1,NULL))
+ as MonthEventCount
+ FROM Monitors as M LEFT JOIN Events as E on E.MonitorId = M.Id
+ WHERE MonitorId = ?
+ GROUP BY E.MonitorId
+ ORDER BY Id"
;
- my $sth = $dbh->prepare_cached( $sql )
- or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
- my $res = $sth->execute( $event->{MonitorId} )
- or Fatal( "Can't execute '$sql': ".$sth->errstr() );
- $monitor = $sth->fetchrow_hashref();
- $sth->finish();
- return() if ( !$monitor );
- }
+ my $sth = $dbh->prepare_cached( $sql )
+ or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
+ my $res = $sth->execute( $event->{MonitorId} )
+ or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+ $monitor = $sth->fetchrow_hashref();
+ $sth->finish();
+ return() if ( !$monitor );
+ }
- # Do we need the image information too?
- my $need_images = $text =~ /%(?:EPI1|EPIM|EI1|EIM)%/;
- my $first_alarm_frame;
- my $max_alarm_frame;
- my $max_alarm_score = 0;
- if ( $need_images )
+# Do we need the image information too?
+ my $need_images = $text =~ /%(?:EPI1|EPIM|EI1|EIM)%/;
+ my $first_alarm_frame;
+ my $max_alarm_frame;
+ my $max_alarm_score = 0;
+ if ( $need_images )
+ {
+ my $sql = "SELECT * FROM Frames
+ WHERE EventId = ? AND Type = 'Alarm'
+ ORDER BY FrameId"
+ ;
+ my $sth = $dbh->prepare_cached( $sql )
+ or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
+ my $res = $sth->execute( $event->{Id} )
+ or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+ while( my $frame = $sth->fetchrow_hashref() )
{
- my $sql = "SELECT * FROM Frames
- WHERE EventId = ? AND Type = 'Alarm'
- ORDER BY FrameId"
- ;
- my $sth = $dbh->prepare_cached( $sql )
- or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
- my $res = $sth->execute( $event->{Id} )
- or Fatal( "Can't execute '$sql': ".$sth->errstr() );
- while( my $frame = $sth->fetchrow_hashref() )
- {
- if ( !$first_alarm_frame )
- {
- $first_alarm_frame = $frame;
- }
- if ( $frame->{Score} > $max_alarm_score )
- {
- $max_alarm_frame = $frame;
- $max_alarm_score = $frame->{Score};
- }
- }
- $sth->finish();
+ if ( !$first_alarm_frame )
+ {
+ $first_alarm_frame = $frame;
+ }
+ if ( $frame->{Score} > $max_alarm_score )
+ {
+ $max_alarm_frame = $frame;
+ $max_alarm_score = $frame->{Score};
+ }
}
+ $sth->finish();
+ }
- my $url = $Config{ZM_URL};
- $text =~ s/%ZP%/$url/g;
- $text =~ s/%MN%/$event->{MonitorName}/g;
- $text =~ s/%MET%/$monitor->{EventCount}/g;
- $text =~ s/%MEH%/$monitor->{HourEventCount}/g;
- $text =~ s/%MED%/$monitor->{DayEventCount}/g;
- $text =~ s/%MEW%/$monitor->{WeekEventCount}/g;
- $text =~ s/%MEM%/$monitor->{MonthEventCount}/g;
- $text =~ s/%MEA%/$monitor->{ArchEventCount}/g;
- $text =~ s/%MP%/$url?view=watch&mid=$event->{MonitorId}/g;
- $text =~ s/%MPS%/$url?view=watchfeed&mid=$event->{MonitorId}&mode=stream/g;
- $text =~ s/%MPI%/$url?view=watchfeed&mid=$event->{MonitorId}&mode=still/g;
- $text =~ s/%EP%/$url?view=event&mid=$event->{MonitorId}&eid=$event->{Id}/g;
- $text =~ s/%EPS%/$url?view=event&mode=stream&mid=$event->{MonitorId}&eid=$event->{Id}/g;
- $text =~ s/%EPI%/$url?view=event&mode=still&mid=$event->{MonitorId}&eid=$event->{Id}/g;
- $text =~ s/%EI%/$event->{Id}/g;
- $text =~ s/%EN%/$event->{Name}/g;
- $text =~ s/%EC%/$event->{Cause}/g;
- $text =~ s/%ED%/$event->{Notes}/g;
- $text =~ s/%ET%/$event->{StartTime}/g;
- $text =~ s/%EL%/$event->{Length}/g;
- $text =~ s/%EF%/$event->{Frames}/g;
- $text =~ s/%EFA%/$event->{AlarmFrames}/g;
- $text =~ s/%EST%/$event->{TotScore}/g;
- $text =~ s/%ESA%/$event->{AvgScore}/g;
- $text =~ s/%ESM%/$event->{MaxScore}/g;
- if ( $first_alarm_frame )
+ my $url = $Config{ZM_URL};
+ $text =~ s/%ZP%/$url/g;
+ $text =~ s/%MN%/$event->{MonitorName}/g;
+ $text =~ s/%MET%/$monitor->{EventCount}/g;
+ $text =~ s/%MEH%/$monitor->{HourEventCount}/g;
+ $text =~ s/%MED%/$monitor->{DayEventCount}/g;
+ $text =~ s/%MEW%/$monitor->{WeekEventCount}/g;
+ $text =~ s/%MEM%/$monitor->{MonthEventCount}/g;
+ $text =~ s/%MEA%/$monitor->{ArchEventCount}/g;
+ $text =~ s/%MP%/$url?view=watch&mid=$event->{MonitorId}/g;
+ $text =~ s/%MPS%/$url?view=watchfeed&mid=$event->{MonitorId}&mode=stream/g;
+ $text =~ s/%MPI%/$url?view=watchfeed&mid=$event->{MonitorId}&mode=still/g;
+ $text =~ s/%EP%/$url?view=event&mid=$event->{MonitorId}&eid=$event->{Id}/g;
+ $text =~ s/%EPS%/$url?view=event&mode=stream&mid=$event->{MonitorId}&eid=$event->{Id}/g;
+ $text =~ s/%EPI%/$url?view=event&mode=still&mid=$event->{MonitorId}&eid=$event->{Id}/g;
+ $text =~ s/%EI%/$event->{Id}/g;
+ $text =~ s/%EN%/$event->{Name}/g;
+ $text =~ s/%EC%/$event->{Cause}/g;
+ $text =~ s/%ED%/$event->{Notes}/g;
+ $text =~ s/%ET%/$event->{StartTime}/g;
+ $text =~ s/%EL%/$event->{Length}/g;
+ $text =~ s/%EF%/$event->{Frames}/g;
+ $text =~ s/%EFA%/$event->{AlarmFrames}/g;
+ $text =~ s/%EST%/$event->{TotScore}/g;
+ $text =~ s/%ESA%/$event->{AvgScore}/g;
+ $text =~ s/%ESM%/$event->{MaxScore}/g;
+ if ( $first_alarm_frame )
+ {
+ $text =~ s/%EPI1%/$url?view=frame&mid=$event->{MonitorId}&eid=$event->{Id}&fid=$first_alarm_frame->{FrameId}/g;
+ $text =~ s/%EPIM%/$url?view=frame&mid=$event->{MonitorId}&eid=$event->{Id}&fid=$max_alarm_frame->{FrameId}/g;
+ if ( $attachments_ref && $text =~ s/%EI1%//g )
{
- $text =~ s/%EPI1%/$url?view=frame&mid=$event->{MonitorId}&eid=$event->{Id}&fid=$first_alarm_frame->{FrameId}/g;
- $text =~ s/%EPIM%/$url?view=frame&mid=$event->{MonitorId}&eid=$event->{Id}&fid=$max_alarm_frame->{FrameId}/g;
- if ( $attachments_ref && $text =~ s/%EI1%//g )
- {
- push( @$attachments_ref,
- {
- type=>"image/jpeg",
- path=>sprintf(
- "%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-capture.jpg",
- getEventPath( $event ),
- $first_alarm_frame->{FrameId}
- )
- }
+ push( @$attachments_ref,
+ {
+ type=>"image/jpeg",
+ path=>sprintf(
+ "%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-capture.jpg",
+ getEventPath( $event ),
+ $first_alarm_frame->{FrameId}
+ )
+ }
+ );
+ }
+ if ( $attachments_ref && $text =~ s/%EIM%//g )
+ {
+# Don't attach the same image twice
+ if ( !@$attachments_ref
+ || ($first_alarm_frame->{FrameId} != $max_alarm_frame->{FrameId} )
+ )
+ {
+ push( @$attachments_ref,
+ {
+ type=>"image/jpeg",
+ path=>sprintf(
+ "%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-capture.jpg",
+ getEventPath( $event ),
+ $max_alarm_frame->{FrameId}
+ )
+ }
);
- }
- if ( $attachments_ref && $text =~ s/%EIM%//g )
- {
- # Don't attach the same image twice
- if ( !@$attachments_ref
- || ($first_alarm_frame->{FrameId} != $max_alarm_frame->{FrameId} )
- )
- {
- push( @$attachments_ref,
- {
- type=>"image/jpeg",
- path=>sprintf(
- "%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-capture.jpg",
- getEventPath( $event ),
- $max_alarm_frame->{FrameId}
- )
- }
- );
- }
- }
+ }
}
- if ( $attachments_ref && $Config{ZM_OPT_FFMPEG} )
+ }
+ if ( $attachments_ref && $Config{ZM_OPT_FFMPEG} )
+ {
+ if ( $text =~ s/%EV%//g )
{
- if ( $text =~ s/%EV%//g )
- {
- my ( $format, $path ) = generateVideo( $filter, $event );
- if ( !$format )
- {
- return( undef );
- }
- push( @$attachments_ref, { type=>"video/$format", path=>$path } );
- }
- if ( $text =~ s/%EVM%//g )
- {
- my ( $format, $path ) = generateVideo( $filter, $event, 1 );
- if ( !$format )
- {
- return( undef );
- }
- push( @$attachments_ref, { type=>"video/$format", path=>$path } );
- }
+ my ( $format, $path ) = generateVideo( $filter, $event );
+ if ( !$format )
+ {
+ return( undef );
+ }
+ push( @$attachments_ref, { type=>"video/$format", path=>$path } );
}
- $text =~ s/%FN%/$filter->{Name}/g;
- ( my $filter_name = $filter->{Name} ) =~ s/ /+/g;
- $text =~ s/%FP%/$url?view=filter&mid=$event->{MonitorId}&filter_name=$filter_name/g;
+ if ( $text =~ s/%EVM%//g )
+ {
+ my ( $format, $path ) = generateVideo( $filter, $event, 1 );
+ if ( !$format )
+ {
+ return( undef );
+ }
+ push( @$attachments_ref, { type=>"video/$format", path=>$path } );
+ }
+ }
+ $text =~ s/%FN%/$filter->{Name}/g;
+ ( my $filter_name = $filter->{Name} ) =~ s/ /+/g;
+ $text =~ s/%FP%/$url?view=filter&mid=$event->{MonitorId}&filter_name=$filter_name/g;
- return( $text );
+ return( $text );
}
sub sendEmail
{
- my $filter = shift;
- my $event = shift;
+ my $filter = shift;
+ my $event = shift;
- if ( ! $Config{ZM_FROM_EMAIL} )
+ if ( ! $Config{ZM_FROM_EMAIL} )
+ {
+ Error( "No 'from' email address defined, not sending email" );
+ return( 0 );
+ }
+ if ( ! $Config{ZM_EMAIL_ADDRESS} )
+ {
+ Error( "No email address defined, not sending email" );
+ return( 0 );
+ }
+
+ Info( "Creating notification email\n" );
+
+ my $subject = substituteTags( $Config{ZM_EMAIL_SUBJECT}, $filter, $event );
+ return( 0 ) if ( !$subject );
+ my @attachments;
+ my $body = substituteTags( $Config{ZM_EMAIL_BODY}, $filter, $event, \@attachments );
+ return( 0 ) if ( !$body );
+
+ Info( "Sending notification email '$subject'\n" );
+
+ eval
+ {
+ if ( $Config{ZM_NEW_MAIL_MODULES} )
{
- Error( "No 'from' email address defined, not sending email" );
- return( 0 );
- }
- if ( ! $Config{ZM_EMAIL_ADDRESS} )
- {
- Error( "No email address defined, not sending email" );
- return( 0 );
- }
-
- Info( "Creating notification email\n" );
-
- my $subject = substituteTags( $Config{ZM_EMAIL_SUBJECT}, $filter, $event );
- return( 0 ) if ( !$subject );
- my @attachments;
- my $body = substituteTags( $Config{ZM_EMAIL_BODY}, $filter, $event, \@attachments );
- return( 0 ) if ( !$body );
-
- Info( "Sending notification email '$subject'\n" );
-
- eval
- {
- if ( $Config{ZM_NEW_MAIL_MODULES} )
- {
- ### Create the multipart container
- my $mail = MIME::Lite->new (
- From => $Config{ZM_FROM_EMAIL},
- To => $Config{ZM_EMAIL_ADDRESS},
- Subject => $subject,
- Type => "multipart/mixed"
+### Create the multipart container
+ my $mail = MIME::Lite->new (
+ From => $Config{ZM_FROM_EMAIL},
+ To => $Config{ZM_EMAIL_ADDRESS},
+ Subject => $subject,
+ Type => "multipart/mixed"
+ );
+### Add the text message part
+ $mail->attach (
+ Type => "TEXT",
+ Data => $body
+ );
+### Add the attachments
+ foreach my $attachment ( @attachments )
+ {
+ Info( "Attaching '$attachment->{path}\n" );
+ $mail->attach(
+ Path => $attachment->{path},
+ Type => $attachment->{type},
+ Disposition => "attachment"
);
- ### Add the text message part
- $mail->attach (
- Type => "TEXT",
- Data => $body
- );
- ### Add the attachments
- foreach my $attachment ( @attachments )
- {
- Info( "Attaching '$attachment->{path}\n" );
- $mail->attach(
- Path => $attachment->{path},
- Type => $attachment->{type},
- Disposition => "attachment"
- );
- }
- ### Send the Message
- if ( $Config{ZM_SSMTP_MAIL} ) {
- my $ssmtp_location = $Config{ZM_SSMTP_PATH};
- if ( !$ssmtp_location ) {
- if ( logDebugging() ) {
- Debug( "which ssmtp: $ssmtp_location - set ssmtp path in options to suppress this message\n" );
- }
- $ssmtp_location = qx('which ssmtp');
- }
- if ( !$ssmtp_location ) {
- Debug( "Can't find ssmtp, trying MIME::Lite->send" );
- MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
- $mail->send();
- } else {
- ### Send using SSMTP
- $mail->send( 'sendmail', $ssmtp_location, $Config{ZM_EMAIL_ADDRESS} );
- }
- } else {
- MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
- $mail->send();
- }
+ }
+### Send the Message
+ if ( $Config{ZM_SSMTP_MAIL} ) {
+ my $ssmtp_location = $Config{ZM_SSMTP_PATH};
+ if ( !$ssmtp_location ) {
+ if ( logDebugging() ) {
+ Debug( "which ssmtp: $ssmtp_location - set ssmtp path in options to suppress this message\n" );
+ }
+ $ssmtp_location = qx('which ssmtp');
}
- else
- {
- my $mail = MIME::Entity->build(
- From => $Config{ZM_FROM_EMAIL},
- To => $Config{ZM_EMAIL_ADDRESS},
- Subject => $subject,
- Type => (($body=~//)?'text/html':'text/plain'),
- Data => $body
- );
-
- foreach my $attachment ( @attachments )
- {
- Info( "Attaching '$attachment->{path}\n" );
- $mail->attach(
- Path => $attachment->{path},
- Type => $attachment->{type},
- Encoding => "base64"
- );
- }
- $mail->smtpsend( Host => $Config{ZM_EMAIL_HOST}, MailFrom => $Config{ZM_FROM_EMAIL} );
+ if ( !$ssmtp_location ) {
+ Debug( "Can't find ssmtp, trying MIME::Lite->send" );
+ MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
+ $mail->send();
+ } else {
+### Send using SSMTP
+ $mail->send( 'sendmail', $ssmtp_location, $Config{ZM_EMAIL_ADDRESS} );
}
- };
- if ( $@ )
- {
- Error( "Can't send email: $@" );
- return( 0 );
+ } else {
+ MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
+ $mail->send();
+ }
}
else
{
- Info( "Notification email sent\n" );
- }
- my $sql = "update Events set Emailed = 1 where Id = ?";
- my $sth = $dbh->prepare_cached( $sql )
- or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
- my $res = $sth->execute( $event->{Id} )
- or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+ my $mail = MIME::Entity->build(
+ From => $Config{ZM_FROM_EMAIL},
+ To => $Config{ZM_EMAIL_ADDRESS},
+ Subject => $subject,
+ Type => (($body=~//)?'text/html':'text/plain'),
+ Data => $body
+ );
- return( 1 );
+ foreach my $attachment ( @attachments )
+ {
+ Info( "Attaching '$attachment->{path}\n" );
+ $mail->attach(
+ Path => $attachment->{path},
+ Type => $attachment->{type},
+ Encoding => "base64"
+ );
+ }
+ $mail->smtpsend( Host => $Config{ZM_EMAIL_HOST}, MailFrom => $Config{ZM_FROM_EMAIL} );
+ }
+ };
+ if ( $@ )
+ {
+ Error( "Can't send email: $@" );
+ return( 0 );
+ }
+ else
+ {
+ Info( "Notification email sent\n" );
+ }
+ my $sql = "update Events set Emailed = 1 where Id = ?";
+ my $sth = $dbh->prepare_cached( $sql )
+ or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
+ my $res = $sth->execute( $event->{Id} )
+ or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+
+ return( 1 );
}
sub sendMessage
{
- my $filter = shift;
- my $event = shift;
+ my $filter = shift;
+ my $event = shift;
- if ( ! $Config{ZM_FROM_EMAIL} )
+ if ( ! $Config{ZM_FROM_EMAIL} )
+ {
+ Error( "No 'from' email address defined, not sending message" );
+ return( 0 );
+ }
+ if ( ! $Config{ZM_MESSAGE_ADDRESS} )
+ {
+ Error( "No message address defined, not sending message" );
+ return( 0 );
+ }
+
+ Info( "Creating notification message\n" );
+
+ my $subject = substituteTags( $Config{ZM_MESSAGE_SUBJECT}, $filter, $event );
+ return( 0 ) if ( !$subject );
+ my @attachments;
+ my $body = substituteTags( $Config{ZM_MESSAGE_BODY}, $filter, $event, \@attachments );
+ return( 0 ) if ( !$body );
+
+ Info( "Sending notification message '$subject'\n" );
+
+ eval
+ {
+ if ( $Config{ZM_NEW_MAIL_MODULES} )
{
- Error( "No 'from' email address defined, not sending message" );
- return( 0 );
- }
- if ( ! $Config{ZM_MESSAGE_ADDRESS} )
- {
- Error( "No message address defined, not sending message" );
- return( 0 );
- }
-
- Info( "Creating notification message\n" );
-
- my $subject = substituteTags( $Config{ZM_MESSAGE_SUBJECT}, $filter, $event );
- return( 0 ) if ( !$subject );
- my @attachments;
- my $body = substituteTags( $Config{ZM_MESSAGE_BODY}, $filter, $event, \@attachments );
- return( 0 ) if ( !$body );
-
- Info( "Sending notification message '$subject'\n" );
-
- eval
- {
- if ( $Config{ZM_NEW_MAIL_MODULES} )
- {
- ### Create the multipart container
- my $mail = MIME::Lite->new (
- From => $Config{ZM_FROM_EMAIL},
- To => $Config{ZM_MESSAGE_ADDRESS},
- Subject => $subject,
- Type => "multipart/mixed"
+### Create the multipart container
+ my $mail = MIME::Lite->new (
+ From => $Config{ZM_FROM_EMAIL},
+ To => $Config{ZM_MESSAGE_ADDRESS},
+ Subject => $subject,
+ Type => "multipart/mixed"
+ );
+### Add the text message part
+ $mail->attach (
+ Type => "TEXT",
+ Data => $body
+ );
+### Add the attachments
+ foreach my $attachment ( @attachments )
+ {
+ Info( "Attaching '$attachment->{path}\n" );
+ $mail->attach(
+ Path => $attachment->{path},
+ Type => $attachment->{type},
+ Disposition => "attachment"
);
- ### Add the text message part
- $mail->attach (
- Type => "TEXT",
- Data => $body
- );
- ### Add the attachments
- foreach my $attachment ( @attachments )
- {
- Info( "Attaching '$attachment->{path}\n" );
- $mail->attach(
- Path => $attachment->{path},
- Type => $attachment->{type},
- Disposition => "attachment"
- );
- }
- ### Send the Message
- if ( $Config{ZM_SSMTP_MAIL} ) {
- my $ssmtp_location = $Config{ZM_SSMTP_PATH};
- if ( !$ssmtp_location ) {
- if ( logDebugging() ) {
- Debug( "which ssmtp: $ssmtp_location - set ssmtp path in options to suppress this message\n" );
- }
- $ssmtp_location = qx('which ssmtp');
- }
- if ( !$ssmtp_location ) {
- Debug( "Can't find ssmtp, trying MIME::Lite->send" );
- MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
- $mail->send();
- } else {
- ### Send using SSMTP
- $mail->send( 'sendmail', $ssmtp_location, $Config{ZM_MESSAGE_ADDRESS} );
- }
- } else {
- MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
- $mail->send();
- }
+ }
+### Send the Message
+ if ( $Config{ZM_SSMTP_MAIL} ) {
+ my $ssmtp_location = $Config{ZM_SSMTP_PATH};
+ if ( !$ssmtp_location ) {
+ if ( logDebugging() ) {
+ Debug( "which ssmtp: $ssmtp_location - set ssmtp path in options to suppress this message\n" );
+ }
+ $ssmtp_location = qx('which ssmtp');
}
- else
- {
- my $mail = MIME::Entity->build(
- From => $Config{ZM_FROM_EMAIL},
- To => $Config{ZM_MESSAGE_ADDRESS},
- Subject => $subject,
- Type => (($body=~//)?'text/html':'text/plain'),
- Data => $body
- );
-
- foreach my $attachment ( @attachments )
- {
- Info( "Attaching '$attachment->{path}\n" );
- $mail->attach(
- Path => $attachment->{path},
- Type => $attachment->{type},
- Encoding => "base64"
- );
- }
- $mail->smtpsend( Host => $Config{ZM_EMAIL_HOST},
- MailFrom => $Config{ZM_FROM_EMAIL}
- );
+ if ( !$ssmtp_location ) {
+ Debug( "Can't find ssmtp, trying MIME::Lite->send" );
+ MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
+ $mail->send();
+ } else {
+### Send using SSMTP
+ $mail->send( 'sendmail', $ssmtp_location, $Config{ZM_MESSAGE_ADDRESS} );
}
- };
- if ( $@ )
- {
- Error( "Can't send email: $@" );
- return( 0 );
+ } else {
+ MIME::Lite->send( "smtp", $Config{ZM_EMAIL_HOST}, Timeout=>60 );
+ $mail->send();
+ }
}
else
{
- Info( "Notification message sent\n" );
- }
- my $sql = "update Events set Messaged = 1 where Id = ?";
- my $sth = $dbh->prepare_cached( $sql )
- or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
- my $res = $sth->execute( $event->{Id} )
- or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+ my $mail = MIME::Entity->build(
+ From => $Config{ZM_FROM_EMAIL},
+ To => $Config{ZM_MESSAGE_ADDRESS},
+ Subject => $subject,
+ Type => (($body=~//)?'text/html':'text/plain'),
+ Data => $body
+ );
- return( 1 );
+ foreach my $attachment ( @attachments )
+ {
+ Info( "Attaching '$attachment->{path}\n" );
+ $mail->attach(
+ Path => $attachment->{path},
+ Type => $attachment->{type},
+ Encoding => "base64"
+ );
+ }
+ $mail->smtpsend( Host => $Config{ZM_EMAIL_HOST},
+ MailFrom => $Config{ZM_FROM_EMAIL}
+ );
+ }
+ };
+ if ( $@ )
+ {
+ Error( "Can't send email: $@" );
+ return( 0 );
+ }
+ else
+ {
+ Info( "Notification message sent\n" );
+ }
+ my $sql = "update Events set Messaged = 1 where Id = ?";
+ my $sth = $dbh->prepare_cached( $sql )
+ or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
+ my $res = $sth->execute( $event->{Id} )
+ or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+
+ return( 1 );
}
sub executeCommand
{
- my $filter = shift;
- my $event = shift;
+ my $filter = shift;
+ my $event = shift;
- my $event_path = getEventPath( $event );
+ my $event_path = getEventPath( $event );
- my $command = $filter->{AutoExecuteCmd};
- $command .= " $event_path";
+ my $command = $filter->{AutoExecuteCmd};
+ $command .= " $event_path";
- Info( "Executing '$command'\n" );
- my $output = qx($command);
- my $status = $? >> 8;
- if ( $status || logDebugging() )
- {
- chomp( $output );
- Debug( "Output: $output\n" );
- }
- if ( $status )
- {
- Error( "Command '$command' exited with status: $status\n" );
- return( 0 );
- }
- else
- {
- my $sql = "update Events set Executed = 1 where Id = ?";
- my $sth = $dbh->prepare_cached( $sql )
- or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
- my $res = $sth->execute( $event->{Id} )
- or Fatal( "Can't execute '$sql': ".$sth->errstr() );
- }
- return( 1 );
+ Info( "Executing '$command'\n" );
+ my $output = qx($command);
+ my $status = $? >> 8;
+ if ( $status || logDebugging() )
+ {
+ chomp( $output );
+ Debug( "Output: $output\n" );
+ }
+ if ( $status )
+ {
+ Error( "Command '$command' exited with status: $status\n" );
+ return( 0 );
+ }
+ else
+ {
+ my $sql = "update Events set Executed = 1 where Id = ?";
+ my $sth = $dbh->prepare_cached( $sql )
+ or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
+ my $res = $sth->execute( $event->{Id} )
+ or Fatal( "Can't execute '$sql': ".$sth->errstr() );
+ }
+ return( 1 );
}
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a2e9de9cd..0d2c27a77 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -4,7 +4,7 @@
configure_file(zm_config.h.in "${CMAKE_CURRENT_BINARY_DIR}/zm_config.h" @ONLY)
# Group together all the source files that are used by all the binaries (zmc, zma, zmu, zms etc)
-set(ZM_BIN_SRC_FILES zm_box.cpp zm_buffer.cpp zm_camera.cpp zm_comms.cpp zm_config.cpp zm_coord.cpp zm_curl_camera.cpp zm.cpp zm_db.cpp zm_logger.cpp zm_event.cpp zm_exception.cpp zm_file_camera.cpp zm_ffmpeg_camera.cpp zm_image.cpp zm_jpeg.cpp zm_libvlc_camera.cpp zm_local_camera.cpp zm_monitor.cpp zm_ffmpeg.cpp zm_mpeg.cpp zm_packet.cpp zm_packetqueue.cpp zm_poly.cpp zm_regexp.cpp zm_remote_camera.cpp zm_remote_camera_http.cpp zm_remote_camera_rtsp.cpp zm_rtp.cpp zm_rtp_ctrl.cpp zm_rtp_data.cpp zm_rtp_source.cpp zm_rtsp.cpp zm_rtsp_auth.cpp zm_sdp.cpp zm_signal.cpp zm_stream.cpp zm_thread.cpp zm_time.cpp zm_timer.cpp zm_user.cpp zm_utils.cpp zm_video.cpp zm_videostore.cpp zm_zone.cpp zm_storage.cpp)
+set(ZM_BIN_SRC_FILES zm_box.cpp zm_buffer.cpp zm_camera.cpp zm_comms.cpp zm_config.cpp zm_coord.cpp zm_curl_camera.cpp zm.cpp zm_db.cpp zm_logger.cpp zm_event.cpp zm_exception.cpp zm_file_camera.cpp zm_ffmpeg_camera.cpp zm_image.cpp zm_jpeg.cpp zm_libvlc_camera.cpp zm_local_camera.cpp zm_monitor.cpp zm_monitorstream.cpp zm_ffmpeg.cpp zm_mpeg.cpp zm_packet.cpp zm_packetqueue.cpp zm_poly.cpp zm_regexp.cpp zm_remote_camera.cpp zm_remote_camera_http.cpp zm_remote_camera_rtsp.cpp zm_rtp.cpp zm_rtp_ctrl.cpp zm_rtp_data.cpp zm_rtp_source.cpp zm_rtsp.cpp zm_rtsp_auth.cpp zm_sdp.cpp zm_signal.cpp zm_stream.cpp zm_thread.cpp zm_time.cpp zm_timer.cpp zm_user.cpp zm_utils.cpp zm_video.cpp zm_videostore.cpp zm_zone.cpp zm_storage.cpp)
# A fix for cmake recompiling the source files for every target.
add_library(zm STATIC ${ZM_BIN_SRC_FILES})
diff --git a/src/zm_event.cpp b/src/zm_event.cpp
index 61fa6c926..0e59c8a7e 100644
--- a/src/zm_event.cpp
+++ b/src/zm_event.cpp
@@ -55,8 +55,10 @@ char Event::capture_file_format[PATH_MAX];
char Event::analyse_file_format[PATH_MAX];
char Event::general_file_format[PATH_MAX];
char Event::video_file_format[PATH_MAX];
+constexpr const char * Event::frame_type_names[3];
int Event::pre_alarm_count = 0;
+
Event::PreAlarmData Event::pre_alarm_data[MAX_PRE_ALARM_FRAMES] = { { 0 } };
Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string &p_cause, const StringSetMap &p_noteSetMap, bool p_videoEvent ) :
@@ -192,7 +194,6 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string
#if ZM_HAVE_VIDEOWRITER_X264MP4
videowriter = new X264MP4Writer(video_file, monitor->Width(), monitor->Height(), monitor->Colours(), monitor->SubpixelOrder(), monitor->GetOptEncoderParams());
#else
- videowriter = NULL;
Error("ZoneMinder was not compiled with the X264 MP4 video writer, check dependencies (x264 and mp4v2)");
#endif
}
@@ -629,16 +630,16 @@ void Event::AddFrame( Image *image, struct timeval timestamp, int score, Image *
struct DeltaTimeval delta_time;
DELTA_TIMEVAL( delta_time, timestamp, start_time, DT_PREC_2 );
- const char *frame_type = score>0?"Alarm":(score<0?"Bulk":"Normal");
+ FrameType frame_type = score>0?ALARM:(score<0?BULK:NORMAL);
if ( score < 0 )
score = 0;
- bool db_frame = (strcmp(frame_type,"Bulk") != 0) || ((frames%config.bulk_frame_interval)==0) || !frames;
+ bool db_frame = ( frame_type != BULK ) || ((frames%config.bulk_frame_interval)==0) || !frames;
if ( db_frame ) {
- Debug( 1, "Adding frame %d of type \"%s\" to DB", frames, frame_type );
+ Debug( 1, "Adding frame %d of type \"%s\" to DB", frames, Event::frame_type_names[frame_type] );
static char sql[ZM_SQL_MED_BUFSIZ];
- snprintf( sql, sizeof(sql), "insert into Frames ( EventId, FrameId, Type, TimeStamp, Delta, Score ) values ( %d, %d, '%s', from_unixtime( %ld ), %s%ld.%02ld, %d )", id, frames, frame_type, timestamp.tv_sec, delta_time.positive?"":"-", delta_time.sec, delta_time.fsec, score );
+ snprintf( sql, sizeof(sql), "insert into Frames ( EventId, FrameId, Type, TimeStamp, Delta, Score ) values ( %d, %d, '%s', from_unixtime( %ld ), %s%ld.%02ld, %d )", id, frames, frame_type_names[frame_type], timestamp.tv_sec, delta_time.positive?"":"-", delta_time.sec, delta_time.fsec, score );
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't insert frame: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
@@ -646,7 +647,7 @@ void Event::AddFrame( Image *image, struct timeval timestamp, int score, Image *
last_db_frame = frames;
// We are writing a Bulk frame
- if ( !strcmp( frame_type,"Bulk") ) {
+ if ( frame_type == BULK ) {
snprintf( sql, sizeof(sql), "update Events set Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d where Id = %d", delta_time.positive?"":"-", delta_time.sec, delta_time.fsec, frames, alarm_frames, tot_score, (int)(alarm_frames?(tot_score/alarm_frames):0), max_score, id );
if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't update event: %s", mysql_error( &dbconn ) );
@@ -658,7 +659,7 @@ void Event::AddFrame( Image *image, struct timeval timestamp, int score, Image *
end_time = timestamp;
// We are writing an Alarm frame
- if ( !strcmp( frame_type,"Alarm") ) {
+ if ( frame_type == ALARM ) {
alarm_frames++;
tot_score += score;
diff --git a/src/zm_event.h b/src/zm_event.h
index 6036ea83f..4ded4ed36 100644
--- a/src/zm_event.h
+++ b/src/zm_event.h
@@ -65,7 +65,8 @@ class Event {
typedef std::map StringSetMap;
protected:
- typedef enum { NORMAL, BULK, ALARM } FrameType;
+ typedef enum { NORMAL=0, BULK, ALARM } FrameType;
+ static constexpr const char * frame_type_names[3] = { "Normal", "Bulk", "Alarm" };
struct PreAlarmData {
Image *image;
diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp
index 07efff42f..1eba7ec06 100644
--- a/src/zm_ffmpeg_camera.cpp
+++ b/src/zm_ffmpeg_camera.cpp
@@ -42,8 +42,7 @@ FfmpegCamera::FfmpegCamera( int p_id, const std::string &p_path, const std::stri
mMethod( p_method ),
mOptions( p_options )
{
- if ( capture )
- {
+ if ( capture ) {
Initialise();
}
@@ -84,20 +83,19 @@ FfmpegCamera::FfmpegCamera( int p_id, const std::string &p_path, const std::stri
}
-FfmpegCamera::~FfmpegCamera()
-{
-
+FfmpegCamera::~FfmpegCamera() {
+ if ( videoStore ) {
+ delete videoStore;
+ }
CloseFfmpeg();
- if ( capture )
- {
+ if ( capture ) {
Terminate();
}
}
-void FfmpegCamera::Initialise()
-{
+void FfmpegCamera::Initialise() {
if ( logDebugging() )
av_log_set_level( AV_LOG_DEBUG );
else
@@ -107,12 +105,10 @@ void FfmpegCamera::Initialise()
avformat_network_init();
}
-void FfmpegCamera::Terminate()
-{
+void FfmpegCamera::Terminate() {
}
-int FfmpegCamera::PrimeCapture()
-{
+int FfmpegCamera::PrimeCapture() {
mVideoStreamId = -1;
mAudioStreamId = -1;
Info( "Priming capture from %s", mPath.c_str() );
@@ -435,6 +431,10 @@ int FfmpegCamera::OpenFfmpeg() {
Fatal( "You must compile ffmpeg with the --enable-swscale option to use ffmpeg cameras" );
#endif // HAVE_LIBSWSCALE
+ if ( mVideoCodecContext->width != width || mVideoCodecContext->height != height ) {
+ Warning( "Monitor dimensions are %dx%d but camera is sending %dx%d", width, height, mVideoCodecContext->width, mVideoCodecContext->height );
+ }
+
mCanCapture = true;
return 0;
@@ -676,16 +676,22 @@ Debug(5, "After av_read_frame (%d)", ret );
videoStore = NULL;
}
- //Buffer video packets, since we are not recording. All audio packets are keyframes, so only if it's a video keyframe
+ // Buffer video packets, since we are not recording.
+ // All audio packets are keyframes, so only if it's a video keyframe
if ( packet.stream_index == mVideoStreamId) {
if ( key_frame ) {
Debug(3, "Clearing queue");
packetqueue.clearQueue();
- }
- if ( packet.pts && video_last_pts > packet.pts ) {
- Warning( "Clearing queue due to out of order pts packet.pts=%d video_last_pts=%d", packet.pts, video_last_pts );
+ }
+#if 0
+// Not sure this is valid. While a camera will PROBABLY always have an increasing pts... it doesn't have to.
+// Also, I think there are integer wrap-around issues.
+
+else if ( packet.pts && video_last_pts > packet.pts ) {
+ Warning( "Clearing queue due to out of order pts packet.pts(%d) < video_last_pts(%d)");
packetqueue.clearQueue();
}
+#endif
}
if (
@@ -708,7 +714,7 @@ Debug(5, "After av_read_frame (%d)", ret );
}
Debug(4, "about to decode video" );
-#if LIBAVCODEC_VERSION_CHECK(57, 0, 0, 0, 0)
+#if LIBAVCODEC_VERSION_CHECK(58, 0, 0, 0, 0)
ret = avcodec_send_packet( mVideoCodecContext, &packet );
if ( ret < 0 ) {
av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE );
@@ -779,7 +785,7 @@ Debug(5, "After av_read_frame (%d)", ret );
}
}
} else {
-#if LIBAVUTIL_VERSION_CHECK(54, 23, 0, 23, 0)
+#if LIBAVUTIL_VERSION_CHECK(56, 23, 0, 23, 0)
Debug( 3, "Some other stream index %d, %s", packet.stream_index, av_get_media_type_string( mFormatContext->streams[packet.stream_index]->codecpar->codec_type) );
#else
Debug( 3, "Some other stream index %d", packet.stream_index );
diff --git a/src/zm_image.cpp b/src/zm_image.cpp
index 9c8c12fda..250ed1b45 100644
--- a/src/zm_image.cpp
+++ b/src/zm_image.cpp
@@ -45,7 +45,6 @@ static short *r_v_table;
static short *g_v_table;
static short *g_u_table;
static short *b_u_table;
-__attribute__((aligned(16))) static const uint8_t movemask[16] = {0,4,8,12,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
jpeg_compress_struct *Image::writejpg_ccinfo[101] = { 0 };
jpeg_compress_struct *Image::encodejpg_ccinfo[101] = { 0 };
@@ -196,6 +195,9 @@ void Image::Initialise()
if(config.cpu_extensions && sseversion >= 20) {
fptr_blend = &sse2_fastblend; /* SSE2 fast blend */
Debug(4,"Blend: Using SSE2 fast blend function");
+ } else if(config.cpu_extensions && neonversion >= 1) {
+ fptr_blend = &neon32_armv7_fastblend; /* ARM Neon fast blend */
+ Debug(4,"Blend: Using ARM Neon fast blend function");
} else {
fptr_blend = &std_fastblend; /* standard fast blend */
Debug(4,"Blend: Using fast blend function");
@@ -249,6 +251,14 @@ void Image::Initialise()
// fptr_delta8_abgr = &std_delta8_abgr;
fptr_delta8_gray8 = &sse2_delta8_gray8;
Debug(4,"Delta: Using SSE2 delta functions");
+ } else if(neonversion >= 1) {
+ /* ARM Neon available */
+ fptr_delta8_rgba = &neon32_armv7_delta8_rgba;
+ fptr_delta8_bgra = &neon32_armv7_delta8_bgra;
+ fptr_delta8_argb = &neon32_armv7_delta8_argb;
+ fptr_delta8_abgr = &neon32_armv7_delta8_abgr;
+ fptr_delta8_gray8 = &neon32_armv7_delta8_gray8;
+ Debug(4,"Delta: Using ARM Neon delta functions");
} else {
/* No suitable SSE version available */
fptr_delta8_rgba = &std_delta8_rgba;
@@ -267,24 +277,20 @@ void Image::Initialise()
fptr_delta8_gray8 = &std_delta8_gray8;
Debug(4,"Delta: CPU extensions disabled, using standard delta functions");
}
+
+ /*
+ SSSE3 deinterlacing functions were removed because they were usually equal
+ or slower than the standard code (compiled with -O2 or better)
+ The function is too complicated to be vectorized efficiently
+ */
+ fptr_deinterlace_4field_rgba = &std_deinterlace_4field_rgba;
+ fptr_deinterlace_4field_bgra = &std_deinterlace_4field_bgra;
+ fptr_deinterlace_4field_argb = &std_deinterlace_4field_argb;
+ fptr_deinterlace_4field_abgr = &std_deinterlace_4field_abgr;
+ fptr_deinterlace_4field_gray8 = &std_deinterlace_4field_gray8;
+ Debug(4,"Deinterlace: Using standard functions");
- /* Use SSSE3 deinterlace functions? */
- if(config.cpu_extensions && sseversion >= 35) {
- fptr_deinterlace_4field_rgba = &ssse3_deinterlace_4field_rgba;
- fptr_deinterlace_4field_bgra = &ssse3_deinterlace_4field_bgra;
- fptr_deinterlace_4field_argb = &ssse3_deinterlace_4field_argb;
- fptr_deinterlace_4field_abgr = &ssse3_deinterlace_4field_abgr;
- fptr_deinterlace_4field_gray8 = &ssse3_deinterlace_4field_gray8;
- Debug(4,"Deinterlace: Using SSSE3 delta functions");
- } else {
- fptr_deinterlace_4field_rgba = &std_deinterlace_4field_rgba;
- fptr_deinterlace_4field_bgra = &std_deinterlace_4field_bgra;
- fptr_deinterlace_4field_argb = &std_deinterlace_4field_argb;
- fptr_deinterlace_4field_abgr = &std_deinterlace_4field_abgr;
- fptr_deinterlace_4field_gray8 = &std_deinterlace_4field_gray8;
- Debug(4,"Deinterlace: Using standard delta functions");
- }
-
+#if defined(__i386__) && !defined(__x86_64__)
/* Use SSE2 aligned memory copy? */
if(config.cpu_extensions && sseversion >= 20) {
fptr_imgbufcpy = &sse2_aligned_memcpy;
@@ -293,6 +299,10 @@ void Image::Initialise()
fptr_imgbufcpy = &memcpy;
Debug(4,"Image buffer copy: Using standard memcpy");
}
+#else
+ fptr_imgbufcpy = &memcpy;
+ Debug(4,"Image buffer copy: Using standard memcpy");
+#endif
/* Code below relocated from zm_local_camera */
Debug( 3, "Setting up static colour tables" );
@@ -1768,7 +1778,7 @@ const Coord Image::centreCoord( const char *text ) const
line = text+index;
line_no++;
}
- int x = (width - (max_line_len * CHAR_WIDTH) ) / 2;
+ int x = (width - (max_line_len * ZM_CHAR_WIDTH) ) / 2;
int y = (height - (line_no * LINE_HEIGHT) ) / 2;
return( Coord( x, y ) );
}
@@ -1858,7 +1868,7 @@ void Image::Annotate( const char *p_text, const Coord &coord, const unsigned int
while ( (index < text_len) && (line_len = strcspn( line, "\n" )) )
{
- unsigned int line_width = line_len * CHAR_WIDTH * size;
+ unsigned int line_width = line_len * ZM_CHAR_WIDTH * size;
unsigned int lo_line_x = coord.X();
unsigned int lo_line_y = coord.Y() + (line_no * LINE_HEIGHT * size);
@@ -1889,17 +1899,17 @@ void Image::Annotate( const char *p_text, const Coord &coord, const unsigned int
if ( colours == ZM_COLOUR_GRAY8 )
{
unsigned char *ptr = &buffer[(lo_line_y*width)+lo_line_x];
- for ( unsigned int y = lo_line_y, r = 0; y < hi_line_y && r < (CHAR_HEIGHT * size); y++, r++, ptr += width )
+ for ( unsigned int y = lo_line_y, r = 0; y < hi_line_y && r < (ZM_CHAR_HEIGHT * size); y++, r++, ptr += width )
{
unsigned char *temp_ptr = ptr;
for ( unsigned int x = lo_line_x, c = 0; x < hi_line_x && c < line_len; c++ )
{
int f;
if (size == 2)
- f = bigfontdata[(line[c] * CHAR_HEIGHT * size) + r];
+ f = bigfontdata[(line[c] * ZM_CHAR_HEIGHT * size) + r];
else
- f = fontdata[(line[c] * CHAR_HEIGHT) + r];
- for ( unsigned int i = 0; i < (CHAR_WIDTH * size) && x < hi_line_x; i++, x++, temp_ptr++ )
+ f = fontdata[(line[c] * ZM_CHAR_HEIGHT) + r];
+ for ( unsigned int i = 0; i < (ZM_CHAR_WIDTH * size) && x < hi_line_x; i++, x++, temp_ptr++ )
{
if ( f & (zm_text_bitmask >> i) )
{
@@ -1919,17 +1929,17 @@ void Image::Annotate( const char *p_text, const Coord &coord, const unsigned int
unsigned int wc = width * colours;
unsigned char *ptr = &buffer[((lo_line_y*width)+lo_line_x)*colours];
- for ( unsigned int y = lo_line_y, r = 0; y < hi_line_y && r < (CHAR_HEIGHT * size); y++, r++, ptr += wc )
+ for ( unsigned int y = lo_line_y, r = 0; y < hi_line_y && r < (ZM_CHAR_HEIGHT * size); y++, r++, ptr += wc )
{
unsigned char *temp_ptr = ptr;
for ( unsigned int x = lo_line_x, c = 0; x < hi_line_x && c < line_len; c++ )
{
int f;
if (size == 2)
- f = bigfontdata[(line[c] * CHAR_HEIGHT * size) + r];
+ f = bigfontdata[(line[c] * ZM_CHAR_HEIGHT * size) + r];
else
- f = fontdata[(line[c] * CHAR_HEIGHT) + r];
- for ( unsigned int i = 0; i < (CHAR_WIDTH * size) && x < hi_line_x; i++, x++, temp_ptr += colours )
+ f = fontdata[(line[c] * ZM_CHAR_HEIGHT) + r];
+ for ( unsigned int i = 0; i < (ZM_CHAR_WIDTH * size) && x < hi_line_x; i++, x++, temp_ptr += colours )
{
if ( f & (zm_text_bitmask >> i) )
{
@@ -1955,17 +1965,17 @@ void Image::Annotate( const char *p_text, const Coord &coord, const unsigned int
unsigned int wc = width * colours;
uint8_t *ptr = &buffer[((lo_line_y*width)+lo_line_x)<<2];
- for ( unsigned int y = lo_line_y, r = 0; y < hi_line_y && r < (CHAR_HEIGHT * size); y++, r++, ptr += wc )
+ for ( unsigned int y = lo_line_y, r = 0; y < hi_line_y && r < (ZM_CHAR_HEIGHT * size); y++, r++, ptr += wc )
{
Rgb* temp_ptr = (Rgb*)ptr;
for ( unsigned int x = lo_line_x, c = 0; x < hi_line_x && c < line_len; c++ )
{
int f;
if (size == 2)
- f = bigfontdata[(line[c] * CHAR_HEIGHT * size) + r];
+ f = bigfontdata[(line[c] * ZM_CHAR_HEIGHT * size) + r];
else
- f = fontdata[(line[c] * CHAR_HEIGHT) + r];
- for ( unsigned int i = 0; i < (CHAR_WIDTH * size) && x < hi_line_x; i++, x++, temp_ptr++ )
+ f = fontdata[(line[c] * ZM_CHAR_HEIGHT) + r];
+ for ( unsigned int i = 0; i < (ZM_CHAR_WIDTH * size) && x < hi_line_x; i++, x++, temp_ptr++ )
{
if ( f & (zm_text_bitmask >> i) )
{
@@ -2081,35 +2091,54 @@ void Image::DeColourise()
subpixelorder = ZM_SUBPIX_ORDER_NONE;
size = width * height;
- if ( colours == ZM_COLOUR_RGB32 )
- {
+ if(colours == ZM_COLOUR_RGB32 && config.cpu_extensions && sseversion >= 35) {
+ /* Use SSSE3 functions */
switch(subpixelorder) {
case ZM_SUBPIX_ORDER_BGRA:
- std_convert_bgra_gray8(buffer,buffer,pixels);
+ ssse3_convert_bgra_gray8(buffer,buffer,pixels);
break;
case ZM_SUBPIX_ORDER_ARGB:
- std_convert_argb_gray8(buffer,buffer,pixels);
+ ssse3_convert_argb_gray8(buffer,buffer,pixels);
break;
case ZM_SUBPIX_ORDER_ABGR:
- std_convert_abgr_gray8(buffer,buffer,pixels);
+ ssse3_convert_abgr_gray8(buffer,buffer,pixels);
break;
case ZM_SUBPIX_ORDER_RGBA:
default:
- std_convert_rgba_gray8(buffer,buffer,pixels);
+ ssse3_convert_rgba_gray8(buffer,buffer,pixels);
break;
}
} else {
- /* Assume RGB24 */
- switch(subpixelorder) {
- case ZM_SUBPIX_ORDER_BGR:
- std_convert_bgr_gray8(buffer,buffer,pixels);
- break;
- case ZM_SUBPIX_ORDER_RGB:
- default:
- std_convert_rgb_gray8(buffer,buffer,pixels);
- break;
- }
-
+ /* Use standard functions */
+ if ( colours == ZM_COLOUR_RGB32 )
+ {
+ switch(subpixelorder) {
+ case ZM_SUBPIX_ORDER_BGRA:
+ std_convert_bgra_gray8(buffer,buffer,pixels);
+ break;
+ case ZM_SUBPIX_ORDER_ARGB:
+ std_convert_argb_gray8(buffer,buffer,pixels);
+ break;
+ case ZM_SUBPIX_ORDER_ABGR:
+ std_convert_abgr_gray8(buffer,buffer,pixels);
+ break;
+ case ZM_SUBPIX_ORDER_RGBA:
+ default:
+ std_convert_rgba_gray8(buffer,buffer,pixels);
+ break;
+ }
+ } else {
+ /* Assume RGB24 */
+ switch(subpixelorder) {
+ case ZM_SUBPIX_ORDER_BGR:
+ std_convert_bgr_gray8(buffer,buffer,pixels);
+ break;
+ case ZM_SUBPIX_ORDER_RGB:
+ default:
+ std_convert_rgb_gray8(buffer,buffer,pixels);
+ break;
+ }
+ }
}
}
@@ -3279,6 +3308,68 @@ __attribute__((noinline)) void std_fastblend(const uint8_t* col1, const uint8_t*
}
}
+/* FastBlend Neon for AArch32 */
+#if (defined(__arm__) && !defined(ZM_STRIP_NEON))
+__attribute__((noinline,__target__("fpu=neon")))
+#endif
+void neon32_armv7_fastblend(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, double blendpercent) {
+#if (defined(__arm__) && !defined(ZM_STRIP_NEON))
+ static int8_t divider = 0;
+ static double current_blendpercent = 0.0;
+
+ if(current_blendpercent != blendpercent) {
+ /* Attempt to match the blending percent to one of the possible values */
+ if(blendpercent < 2.34375) {
+ // 1.5625% blending
+ divider = 6;
+ } else if(blendpercent >= 2.34375 && blendpercent < 4.6875) {
+ // 3.125% blending
+ divider = 5;
+ } else if(blendpercent >= 4.6875 && blendpercent < 9.375) {
+ // 6.25% blending
+ divider = 4;
+ } else if(blendpercent >= 9.375 && blendpercent < 18.75) {
+ // 12.5% blending
+ divider = 3;
+ } else if(blendpercent >= 18.75 && blendpercent < 37.5) {
+ // 25% blending
+ divider = 2;
+ } else if(blendpercent >= 37.5) {
+ // 50% blending
+ divider = 1;
+ }
+ // We only have instruction to shift left by a variable, going negative shifts right :)
+ divider *= -1;
+ current_blendpercent = blendpercent;
+ }
+
+ /* Q0(D0,D1) = col1 */
+ /* Q1(D2,D3) = col2 */
+ /* Q2(D4,D5) = col1 backup */
+ /* Q3(D6,D7) = divider */
+
+ __asm__ __volatile__ (
+ "mov r12, %4\n\t"
+ "vdup.8 q3, r12\n\t"
+ "neon32_armv7_fastblend_iter:\n\t"
+ "vldm %0!, {q0}\n\t"
+ "vldm %1!, {q1}\n\t"
+ "vrshl.u8 q2, q0, q3\n\t"
+ "vrshl.u8 q1, q1, q3\n\t"
+ "vsub.i8 q1, q1, q2\n\t"
+ "vadd.i8 q1, q1, q0\n\t"
+ "vstm %2!, {q1}\n\t"
+ "subs %3, %3, #16\n\t"
+ "bne neon32_armv7_fastblend_iter\n\t"
+ :
+ : "r" (col1), "r" (col2), "r" (result), "r" (count), "g" (divider)
+ : "%r12", "%q0", "%q1", "%q2", "%q3", "cc", "memory"
+ );
+#else
+ Panic("Neon function called on a non-ARM platform or Neon code is absent");
+#endif
+}
+
__attribute__((noinline)) void std_blend(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, double blendpercent) {
double divide = blendpercent / 100.0;
double opacity = 1.0 - divide;
@@ -3501,6 +3592,88 @@ __attribute__((noinline)) void std_delta8_abgr(const uint8_t* col1, const uint8_
}
}
+/* Grayscale Neon for AArch32 */
+#if (defined(__arm__) && !defined(ZM_STRIP_NEON))
+__attribute__((noinline,__target__("fpu=neon")))
+#endif
+void neon32_armv7_delta8_gray8(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
+#if (defined(__arm__) && !defined(ZM_STRIP_NEON))
+
+ /* Q0(D0,D1) = col1 */
+ /* Q1(D2,D3) = col2 */
+
+ __asm__ __volatile__ (
+ "neon32_armv7_delta8_gray8_iter:\n\t"
+ "vldm %0!, {q0}\n\t"
+ "vldm %1!, {q1}\n\t"
+ "vabd.u8 q0, q0, q1\n\t"
+ "vstm %2!, {q0}\n\t"
+ "subs %3, %3, #16\n\t"
+ "bne neon32_armv7_delta8_gray8_iter\n\t"
+ :
+ : "r" (col1), "r" (col2), "r" (result), "r" (count)
+ : "%q0", "%q1", "cc", "memory"
+ );
+#else
+ Panic("Neon function called on a non-ARM platform or Neon code is absent");
+#endif
+}
+
+/* RGB32 Neon for AArch32 */
+#if (defined(__arm__) && !defined(ZM_STRIP_NEON))
+__attribute__((noinline,__target__("fpu=neon")))
+#endif
+void neon32_armv7_delta8_rgb32(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, uint32_t multiplier) {
+#if (defined(__arm__) && !defined(ZM_STRIP_NEON))
+
+ /* Q0(D0,D1) = col1 */
+ /* Q1(D2,D3) = col2 */
+ /* Q2(D4,D5) = multiplier */
+
+ __asm__ __volatile__ (
+ "mov r12, %4\n\t"
+ "vdup.32 q2, r12\n\t"
+ "neon32_armv7_delta8_rgb32_iter:\n\t"
+ "vldm %0!, {q0}\n\t"
+ "vldm %1!, {q1}\n\t"
+ "vabd.u8 q0, q0, q1\n\t"
+ "vrshr.u8 q0, q0, #3\n\t"
+ "vmul.i8 q0, q0, q2\n\t"
+ "vpadd.i8 d0, d0, d1\n\t"
+ "vpadd.i8 d2, d2, d3\n\t"
+ "vpadd.i8 d0, d0, d2\n\t"
+ "vst1.32 {d0[0]}, [%2]!\n\t"
+ "subs %3, %3, #4\n\t"
+ "bne neon32_armv7_delta8_rgb32_iter\n\t"
+ :
+ : "r" (col1), "r" (col2), "r" (result), "r" (count), "r" (multiplier)
+ : "%r12", "%q0", "%q1", "%q2", "cc", "memory"
+ );
+#else
+ Panic("Neon function called on a non-ARM platform or Neon code is absent");
+#endif
+}
+
+/* RGB32: RGBA Neon for AArch32 */
+void neon32_armv7_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
+ neon32_armv7_delta8_rgb32(col1, col2, result, count, 0x00010502);
+}
+
+/* RGB32: BGRA Neon for AArch32 */
+void neon32_armv7_delta8_bgra(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
+ neon32_armv7_delta8_rgb32(col1, col2, result, count, 0x00020501);
+}
+
+/* RGB32: ARGB Neon for AArch32 */
+void neon32_armv7_delta8_argb(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
+ neon32_armv7_delta8_rgb32(col1, col2, result, count, 0x01050200);
+}
+
+/* RGB32: ABGR Neon for AArch32 */
+void neon32_armv7_delta8_abgr(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
+ neon32_armv7_delta8_rgb32(col1, col2, result, count, 0x02050100);
+}
+
/* Grayscale SSE2 */
#if defined(__i386__) || defined(__x86_64__)
__attribute__((noinline,__target__("sse2")))
@@ -3766,25 +3939,31 @@ void sse2_delta8_abgr(const uint8_t* col1, const uint8_t* col2, uint8_t* result,
#endif
}
-/* RGB32: RGBA SSSE3 */
+/* RGB32 SSSE3 */
#if defined(__i386__) || defined(__x86_64__)
__attribute__((noinline,__target__("ssse3")))
#endif
-void ssse3_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
+void ssse3_delta8_rgb32(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, uint32_t multiplier) {
#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
-
+
+ /* XMM0 - zero */
+ /* XMM1 - col1 */
+ /* XMM2 - col2 */
+ /* XMM3 - multiplier */
+ /* XMM4 - divide mask */
+
__asm__ __volatile__ (
"mov $0x1F1F1F1F, %%eax\n\t"
"movd %%eax, %%xmm4\n\t"
"pshufd $0x0, %%xmm4, %%xmm4\n\t"
- "mov $0xff, %%eax\n\t"
- "movd %%eax, %%xmm0\n\t"
- "pshufd $0x0, %%xmm0, %%xmm0\n\t"
- "movdqa %4, %%xmm5\n\t"
+ "mov %4, %%eax\n\t"
+ "movd %%eax, %%xmm3\n\t"
+ "pshufd $0x0, %%xmm3, %%xmm3\n\t"
+ "pxor %%xmm0, %%xmm0\n\t"
"sub $0x10, %0\n\t"
"sub $0x10, %1\n\t"
"sub $0x4, %2\n\t"
- "ssse3_delta8_rgba_iter:\n\t"
+ "ssse3_delta8_rgb32_iter:\n\t"
"movdqa (%0,%3,4), %%xmm1\n\t"
"movdqa (%1,%3,4), %%xmm2\n\t"
"psrlq $0x3, %%xmm1\n\t"
@@ -3792,200 +3971,41 @@ void ssse3_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result
"pand %%xmm4, %%xmm1\n\t"
"pand %%xmm4, %%xmm2\n\t"
"psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm3\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x8, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "pslld $0x2, %%xmm2\n\t"
- "paddd %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm3, %%xmm1\n\t"
- "pand %%xmm0, %%xmm1\n\t"
- "paddd %%xmm1, %%xmm1\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x10, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "pshufb %%xmm5, %%xmm1\n\t"
+ "pabsb %%xmm1, %%xmm1\n\t"
+ "pmaddubsw %%xmm3, %%xmm1\n\t"
+ "phaddw %%xmm0, %%xmm1\n\t"
+ "packuswb %%xmm1, %%xmm1\n\t"
"movd %%xmm1, %%eax\n\t"
"movnti %%eax, (%2,%3)\n\t"
"sub $0x4, %3\n\t"
- "jnz ssse3_delta8_rgba_iter\n\t"
+ "jnz ssse3_delta8_rgb32_iter\n\t"
:
- : "r" (col1), "r" (col2), "r" (result), "r" (count), "m" (*movemask)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "cc", "memory"
+ : "r" (col1), "r" (col2), "r" (result), "r" (count), "g" (multiplier)
+ : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "cc", "memory"
);
#else
Panic("SSE function called on a non x86\\x86-64 platform");
#endif
}
+/* RGB32: RGBA SSSE3 */
+void ssse3_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
+ ssse3_delta8_rgb32(col1, col2, result, count, 0x00010502);
+}
+
/* RGB32: BGRA SSSE3 */
-#if defined(__i386__) || defined(__x86_64__)
-__attribute__((noinline,__target__("ssse3")))
-#endif
void ssse3_delta8_bgra(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
-#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
-
- __asm__ __volatile__ (
- "mov $0x1F1F1F1F, %%eax\n\t"
- "movd %%eax, %%xmm4\n\t"
- "pshufd $0x0, %%xmm4, %%xmm4\n\t"
- "mov $0xff, %%eax\n\t"
- "movd %%eax, %%xmm0\n\t"
- "pshufd $0x0, %%xmm0, %%xmm0\n\t"
- "movdqa %4, %%xmm5\n\t"
- "sub $0x10, %0\n\t"
- "sub $0x10, %1\n\t"
- "sub $0x4, %2\n\t"
- "ssse3_delta8_bgra_iter:\n\t"
- "movdqa (%0,%3,4), %%xmm1\n\t"
- "movdqa (%1,%3,4), %%xmm2\n\t"
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm3\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x8, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "pslld $0x2, %%xmm2\n\t"
- "paddd %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm3, %%xmm1\n\t"
- "pand %%xmm0, %%xmm1\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x10, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "paddd %%xmm2, %%xmm2\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "pshufb %%xmm5, %%xmm1\n\t"
- "movd %%xmm1, %%eax\n\t"
- "movnti %%eax, (%2,%3)\n\t"
- "sub $0x4, %3\n\t"
- "jnz ssse3_delta8_bgra_iter\n\t"
- :
- : "r" (col1), "r" (col2), "r" (result), "r" (count), "m" (*movemask)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "cc", "memory"
- );
-#else
- Panic("SSE function called on a non x86\\x86-64 platform");
-#endif
+ ssse3_delta8_rgb32(col1, col2, result, count, 0x00020501);
}
/* RGB32: ARGB SSSE3 */
-#if defined(__i386__) || defined(__x86_64__)
-__attribute__((noinline,__target__("ssse3")))
-#endif
void ssse3_delta8_argb(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
-#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
-
- __asm__ __volatile__ (
- "mov $0x1F1F1F1F, %%eax\n\t"
- "movd %%eax, %%xmm4\n\t"
- "pshufd $0x0, %%xmm4, %%xmm4\n\t"
- "mov $0xff, %%eax\n\t"
- "movd %%eax, %%xmm0\n\t"
- "pshufd $0x0, %%xmm0, %%xmm0\n\t"
- "movdqa %4, %%xmm5\n\t"
- "sub $0x10, %0\n\t"
- "sub $0x10, %1\n\t"
- "sub $0x4, %2\n\t"
- "ssse3_delta8_argb_iter:\n\t"
- "movdqa (%0,%3,4), %%xmm1\n\t"
- "movdqa (%1,%3,4), %%xmm2\n\t"
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm3\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x10, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "pslld $0x2, %%xmm2\n\t"
- "paddd %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm3, %%xmm1\n\t"
- "psrld $0x8, %%xmm1\n\t"
- "pand %%xmm0, %%xmm1\n\t"
- "paddd %%xmm1, %%xmm1\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x18, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "pshufb %%xmm5, %%xmm1\n\t"
- "movd %%xmm1, %%eax\n\t"
- "movnti %%eax, (%2,%3)\n\t"
- "sub $0x4, %3\n\t"
- "jnz ssse3_delta8_argb_iter\n\t"
- :
- : "r" (col1), "r" (col2), "r" (result), "r" (count), "m" (*movemask)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "cc", "memory"
- );
-#else
- Panic("SSE function called on a non x86\\x86-64 platform");
-#endif
+ ssse3_delta8_rgb32(col1, col2, result, count, 0x01050200);
}
/* RGB32: ABGR SSSE3 */
-#if defined(__i386__) || defined(__x86_64__)
-__attribute__((noinline,__target__("ssse3")))
-#endif
void ssse3_delta8_abgr(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count) {
-#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
-
- __asm__ __volatile__ (
- "mov $0x1F1F1F1F, %%eax\n\t"
- "movd %%eax, %%xmm4\n\t"
- "pshufd $0x0, %%xmm4, %%xmm4\n\t"
- "mov $0xff, %%eax\n\t"
- "movd %%eax, %%xmm0\n\t"
- "pshufd $0x0, %%xmm0, %%xmm0\n\t"
- "movdqa %4, %%xmm5\n\t"
- "sub $0x10, %0\n\t"
- "sub $0x10, %1\n\t"
- "sub $0x4, %2\n\t"
- "ssse3_delta8_abgr_iter:\n\t"
- "movdqa (%0,%3,4), %%xmm1\n\t"
- "movdqa (%1,%3,4), %%xmm2\n\t"
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm3\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x10, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "pslld $0x2, %%xmm2\n\t"
- "paddd %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm3, %%xmm1\n\t"
- "psrld $0x8, %%xmm1\n\t"
- "pand %%xmm0, %%xmm1\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x18, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "paddd %%xmm2, %%xmm2\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "pshufb %%xmm5, %%xmm1\n\t"
- "movd %%xmm1, %%eax\n\t"
- "movnti %%eax, (%2,%3)\n\t"
- "sub $0x4, %3\n\t"
- "jnz ssse3_delta8_abgr_iter\n\t"
- :
- : "r" (col1), "r" (col2), "r" (result), "r" (count), "m" (*movemask)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "cc", "memory"
- );
-#else
- Panic("SSE function called on a non x86\\x86-64 platform");
-#endif
+ ssse3_delta8_rgb32(col1, col2, result, count, 0x02050100);
}
@@ -4187,55 +4207,68 @@ __attribute__((noinline)) void std_convert_yuyv_gray8(const uint8_t* col1, uint8
}
}
-/* RGBA to grayscale SSSE3 */
+/* RGB32 to grayscale SSSE3 */
#if defined(__i386__) || defined(__x86_64__)
__attribute__((noinline,__target__("ssse3")))
#endif
-void ssse3_convert_rgba_gray8(const uint8_t* col1, uint8_t* result, unsigned long count) {
+void ssse3_convert_rgb32_gray8(const uint8_t* col1, uint8_t* result, unsigned long count, uint32_t multiplier) {
#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
+ /* XMM0 - zero */
+ /* XMM1 - col1 */
+ /* XMM3 - multiplier */
+ /* XMM4 - divide mask */
+
__asm__ __volatile__ (
"mov $0x1F1F1F1F, %%eax\n\t"
"movd %%eax, %%xmm4\n\t"
"pshufd $0x0, %%xmm4, %%xmm4\n\t"
- "mov $0xff, %%eax\n\t"
- "movd %%eax, %%xmm0\n\t"
- "pshufd $0x0, %%xmm0, %%xmm0\n\t"
- "movdqa %3, %%xmm5\n\t"
+ "mov %3, %%eax\n\t"
+ "movd %%eax, %%xmm3\n\t"
+ "pshufd $0x0, %%xmm3, %%xmm3\n\t"
+ "pxor %%xmm0, %%xmm0\n\t"
"sub $0x10, %0\n\t"
"sub $0x4, %1\n\t"
- "ssse3_convert_rgba_gray8_iter:\n\t"
- "movdqa (%0,%2,4), %%xmm3\n\t"
- "psrlq $0x3, %%xmm3\n\t"
- "pand %%xmm4, %%xmm3\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x8, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "pslld $0x2, %%xmm2\n\t"
- "paddd %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm3, %%xmm1\n\t"
- "pand %%xmm0, %%xmm1\n\t"
- "paddd %%xmm1, %%xmm1\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm3, %%xmm2\n\t"
- "psrld $0x10, %%xmm2\n\t"
- "pand %%xmm0, %%xmm2\n\t"
- "paddd %%xmm2, %%xmm1\n\t"
- "pshufb %%xmm5, %%xmm1\n\t"
+ "ssse3_convert_rgb32_gray8_iter:\n\t"
+ "movdqa (%0,%2,4), %%xmm1\n\t"
+ "psrlq $0x3, %%xmm1\n\t"
+ "pand %%xmm4, %%xmm1\n\t"
+ "pmaddubsw %%xmm3, %%xmm1\n\t"
+ "phaddw %%xmm0, %%xmm1\n\t"
+ "packuswb %%xmm1, %%xmm1\n\t"
"movd %%xmm1, %%eax\n\t"
"movnti %%eax, (%1,%2)\n\t"
"sub $0x4, %2\n\t"
- "jnz ssse3_convert_rgba_gray8_iter\n\t"
+ "jnz ssse3_convert_rgb32_gray8_iter\n\t"
:
- : "r" (col1), "r" (result), "r" (count), "m" (*movemask)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "cc", "memory"
+ : "r" (col1), "r" (result), "r" (count), "g" (multiplier)
+ : "%eax", "%xmm0", "%xmm1", "%xmm3", "%xmm4", "cc", "memory"
);
#else
Panic("SSE function called on a non x86\\x86-64 platform");
#endif
}
+/* RGBA to grayscale SSSE3 */
+void ssse3_convert_rgba_gray8(const uint8_t* col1, uint8_t* result, unsigned long count) {
+ ssse3_convert_rgb32_gray8(col1, result, count, 0x00010502);
+}
+
+/* BGRA to grayscale SSSE3 */
+void ssse3_convert_bgra_gray8(const uint8_t* col1, uint8_t* result, unsigned long count) {
+ ssse3_convert_rgb32_gray8(col1, result, count, 0x00020501);
+}
+
+/* ARGB to grayscale SSSE3 */
+void ssse3_convert_argb_gray8(const uint8_t* col1, uint8_t* result, unsigned long count) {
+ ssse3_convert_rgb32_gray8(col1, result, count, 0x01050200);
+}
+
+/* ABGR to grayscale SSSE3 */
+void ssse3_convert_abgr_gray8(const uint8_t* col1, uint8_t* result, unsigned long count) {
+ ssse3_convert_rgb32_gray8(col1, result, count, 0x02050100);
+}
+
/* Converts a YUYV image into grayscale by extracting the Y channel */
#if defined(__i386__) || defined(__x86_64__)
__attribute__((noinline,__target__("ssse3")))
@@ -4855,871 +4888,3 @@ __attribute__((noinline)) void std_deinterlace_4field_abgr(uint8_t* col1, uint8_
pncurrent += 4;
}
}
-
-/* Grayscale SSSE3 */
-#if defined(__i386__) || defined(__x86_64__)
-__attribute__((noinline,__target__("ssse3")))
-#endif
-void ssse3_deinterlace_4field_gray8(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height) {
-
-#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
- union {
- uint32_t int32;
- uint8_t int8a[4];
- } threshold_mask;
- threshold_mask.int8a[0] = threshold;
- threshold_mask.int8a[1] = 0;
- threshold_mask.int8a[2] = threshold;
- threshold_mask.int8a[3] = 0;
-
- unsigned long row_width = width;
- uint8_t* max_ptr = col1 + (row_width * (height-2));
- uint8_t* max_ptr2 = col1 + row_width;
-
- __asm__ __volatile__ (
- /* Load the threshold */
- "mov %5, %%eax\n\t"
- "movd %%eax, %%xmm4\n\t"
- "pshufd $0x0, %%xmm4, %%xmm4\n\t"
- /* Zero the temporary register */
- "pxor %%xmm0, %%xmm0\n\t"
-
- "algo_ssse3_deinterlace_4field_gray8:\n\t"
-
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "pmaxub %%xmm2, %%xmm1\n\t"
- "pminub %%xmm5, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "pmaxub %%xmm2, %%xmm1\n\t"
- "pminub %%xmm6, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
- "movdqa %%xmm1, %%xmm2\n\t"
-
- /* Do the comparison on words instead of bytes because we don't have unsigned comparison */
- "punpcklbw %%xmm0, %%xmm1\n\t" // Expand pixels 0-7 into words into xmm1
- "punpckhbw %%xmm0, %%xmm2\n\t" // Expand pixels 8-15 into words into xmm2
- "pcmpgtw %%xmm4, %%xmm1\n\t" // Compare average delta with threshold for pixels 0-7
- "pcmpgtw %%xmm4, %%xmm2\n\t" // Compare average delta with threshold for pixels 8-15
- "packsswb %%xmm2, %%xmm1\n\t" // Pack the comparison results into xmm1
-
- "movdqa (%0,%4), %%xmm2\n\t" // Load pbelow
- "pavgb %%xmm5, %%xmm2\n\t" // Average pabove and pbelow
- "pand %%xmm1, %%xmm2\n\t" // Filter out pixels in avg that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm2, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
-
- "sub %4, %0\n\t" // Restore pcurrent to pabove
- "sub %4, %1\n\t" // Restore pncurrent to pnabove
-
- /* Next pixels */
- "add $0x10, %0\n\t" // Add 16 to pcurrent
- "add $0x10, %1\n\t" // Add 16 to pncurrent
-
- /* Check if we reached the row end */
- "cmp %2, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_gray8\n\t" // Go for another iteration
-
- /* Next row */
- "add %4, %0\n\t" // Add width to pcurrent
- "add %4, %1\n\t" // Add width to pncurrent
- "mov %0, %2\n\t"
- "add %4, %2\n\t" // Add width to max_ptr2
-
- /* Check if we reached the end */
- "cmp %3, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_gray8\n\t" // Go for another iteration
-
- /* Special case for the last line */
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "pmaxub %%xmm2, %%xmm1\n\t"
- "pminub %%xmm5, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "pmaxub %%xmm2, %%xmm1\n\t"
- "pminub %%xmm6, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
- "movdqa %%xmm1, %%xmm2\n\t"
-
- /* Do the comparison on words instead of bytes because we don't have unsigned comparison */
- "punpcklbw %%xmm0, %%xmm1\n\t" // Expand pixels 0-7 into words into xmm1
- "punpckhbw %%xmm0, %%xmm2\n\t" // Expand pixels 8-15 into words into xmm2
- "pcmpgtw %%xmm4, %%xmm1\n\t" // Compare average delta with threshold for pixels 0-7
- "pcmpgtw %%xmm4, %%xmm2\n\t" // Compare average delta with threshold for pixels 8-15
- "packsswb %%xmm2, %%xmm1\n\t" // Pack the comparison results into xmm1
-
- "pand %%xmm1, %%xmm5\n\t" // Filter out pixels in pabove that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm5, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
- :
- : "r" (col1), "r" (col2), "r" (max_ptr2), "r" (max_ptr), "r" (row_width), "m" (threshold_mask.int32)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "cc", "memory"
- );
-#else
- Panic("SSE function called on a non x86\\x86-64 platform");
-#endif
-}
-
-/* RGBA SSSE3 */
-#if defined(__i386__) || defined(__x86_64__)
-__attribute__((noinline,__target__("ssse3")))
-#endif
-void ssse3_deinterlace_4field_rgba(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height) {
-#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
- __attribute__((aligned(16))) static const uint8_t movemask2[16] = {1,1,1,1,1,0,0,2,9,9,9,9,9,8,8,10};
-
- const uint32_t threshold_val = threshold;
-
- unsigned long row_width = width*4;
- uint8_t* max_ptr = col1 + (row_width * (height-2));
- uint8_t* max_ptr2 = col1 + row_width;
-
- __asm__ __volatile__ (
- "mov $0x1F1F1F1F, %%eax\n\t"
- "movd %%eax, %%xmm4\n\t"
- "pshufd $0x0, %%xmm4, %%xmm4\n\t"
- "movdqa %6, %%xmm3\n\t"
- "mov %5, %%eax\n\t"
-#if defined(__x86_64__)
- "movd %%eax, %%xmm8\n\t"
- "pshufd $0x0, %%xmm8, %%xmm8\n\t"
-#endif
- /* Zero the temporary register */
- "pxor %%xmm0, %%xmm0\n\t"
-
- "algo_ssse3_deinterlace_4field_rgba:\n\t"
-
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
-
-#if defined(__x86_64__)
- "pcmpgtd %%xmm8, %%xmm1\n\t" // Compare average delta with the threshold
-#else
- "movd %%eax, %%xmm7\n\t" // Setup the threshold
- "pshufd $0x0, %%xmm7, %%xmm7\n\t"
-
- "pcmpgtd %%xmm7, %%xmm1\n\t" // Compare average delta with the threshold
-#endif
- "movdqa (%0,%4), %%xmm2\n\t" // Load pbelow
- "pavgb %%xmm5, %%xmm2\n\t" // Average pabove and pbelow
- "pand %%xmm1, %%xmm2\n\t" // Filter out pixels in avg that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm2, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
-
- "sub %4, %0\n\t" // Restore pcurrent to pabove
- "sub %4, %1\n\t" // Restore pncurrent to pnabove
-
- /* Next pixels */
- "add $0x10, %0\n\t" // Add 16 to pcurrent
- "add $0x10, %1\n\t" // Add 16 to pncurrent
-
- /* Check if we reached the row end */
- "cmp %2, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_rgba\n\t" // Go for another iteration
-
- /* Next row */
- "add %4, %0\n\t" // Add width to pcurrent
- "add %4, %1\n\t" // Add width to pncurrent
- "mov %0, %2\n\t"
- "add %4, %2\n\t" // Add width to max_ptr2
-
- /* Check if we reached the end */
- "cmp %3, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_rgba\n\t" // Go for another iteration
-
- /* Special case for the last line */
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
-
-#if defined(__x86_64__)
- "pcmpgtd %%xmm8, %%xmm1\n\t" // Compare average delta with the threshold
-#else
- "movd %%eax, %%xmm7\n\t" // Setup the threshold
- "pshufd $0x0, %%xmm7, %%xmm7\n\t"
-
- "pcmpgtd %%xmm7, %%xmm1\n\t" // Compare average delta with the threshold
-#endif
- "pand %%xmm1, %%xmm5\n\t" // Filter out pixels in pabove that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm5, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
- :
- : "r" (col1), "r" (col2), "r" (max_ptr2), "r" (max_ptr), "r" (row_width), "m" (threshold_val), "m" (*movemask2)
-#if defined(__x86_64__)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "%xmm8", "cc", "memory"
-#else
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "cc", "memory"
-#endif
- );
-#else
- Panic("SSE function called on a non x86\\x86-64 platform");
-#endif
-}
-
-/* BGRA SSSE3 */
-#if defined(__i386__) || defined(__x86_64__)
-__attribute__((noinline,__target__("ssse3")))
-#endif
-void ssse3_deinterlace_4field_bgra(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height) {
-#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
- __attribute__((aligned(16))) static const uint8_t movemask2[16] = {1,1,1,1,1,2,2,0,9,9,9,9,9,10,10,8};
-
- const uint32_t threshold_val = threshold;
-
- unsigned long row_width = width*4;
- uint8_t* max_ptr = col1 + (row_width * (height-2));
- uint8_t* max_ptr2 = col1 + row_width;
-
- __asm__ __volatile__ (
- "mov $0x1F1F1F1F, %%eax\n\t"
- "movd %%eax, %%xmm4\n\t"
- "pshufd $0x0, %%xmm4, %%xmm4\n\t"
- "movdqa %6, %%xmm3\n\t"
- "mov %5, %%eax\n\t"
-#if defined(__x86_64__)
- "movd %%eax, %%xmm8\n\t"
- "pshufd $0x0, %%xmm8, %%xmm8\n\t"
-#endif
- /* Zero the temporary register */
- "pxor %%xmm0, %%xmm0\n\t"
-
- "algo_ssse3_deinterlace_4field_bgra:\n\t"
-
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
-
-#if defined(__x86_64__)
- "pcmpgtd %%xmm8, %%xmm1\n\t" // Compare average delta with the threshold
-#else
- "movd %%eax, %%xmm7\n\t" // Setup the threshold
- "pshufd $0x0, %%xmm7, %%xmm7\n\t"
-
- "pcmpgtd %%xmm7, %%xmm1\n\t" // Compare average delta with the threshold
-#endif
- "movdqa (%0,%4), %%xmm2\n\t" // Load pbelow
- "pavgb %%xmm5, %%xmm2\n\t" // Average pabove and pbelow
- "pand %%xmm1, %%xmm2\n\t" // Filter out pixels in avg that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm2, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
-
- "sub %4, %0\n\t" // Restore pcurrent to pabove
- "sub %4, %1\n\t" // Restore pncurrent to pnabove
-
- /* Next pixels */
- "add $0x10, %0\n\t" // Add 16 to pcurrent
- "add $0x10, %1\n\t" // Add 16 to pncurrent
-
- /* Check if we reached the row end */
- "cmp %2, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_bgra\n\t" // Go for another iteration
-
- /* Next row */
- "add %4, %0\n\t" // Add width to pcurrent
- "add %4, %1\n\t" // Add width to pncurrent
- "mov %0, %2\n\t"
- "add %4, %2\n\t" // Add width to max_ptr2
-
- /* Check if we reached the end */
- "cmp %3, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_bgra\n\t" // Go for another iteration
-
- /* Special case for the last line */
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
-
-#if defined(__x86_64__)
- "pcmpgtd %%xmm8, %%xmm1\n\t" // Compare average delta with the threshold
-#else
- "movd %%eax, %%xmm7\n\t" // Setup the threshold
- "pshufd $0x0, %%xmm7, %%xmm7\n\t"
-
- "pcmpgtd %%xmm7, %%xmm1\n\t" // Compare average delta with the threshold
-#endif
- "pand %%xmm1, %%xmm5\n\t" // Filter out pixels in pabove that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm5, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
- :
- : "r" (col1), "r" (col2), "r" (max_ptr2), "r" (max_ptr), "r" (row_width), "m" (threshold_val), "m" (*movemask2)
-#if defined(__x86_64__)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "%xmm8", "cc", "memory"
-#else
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "cc", "memory"
-#endif
- );
-#else
- Panic("SSE function called on a non x86\\x86-64 platform");
-#endif
-}
-
-/* ARGB SSSE3 */
-#if defined(__i386__) || defined(__x86_64__)
-__attribute__((noinline,__target__("ssse3")))
-#endif
-void ssse3_deinterlace_4field_argb(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height) {
-#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
- __attribute__((aligned(16))) static const uint8_t movemask2[16] = {2,2,2,2,2,1,1,3,10,10,10,10,10,9,9,11};
-
- const uint32_t threshold_val = threshold;
-
- unsigned long row_width = width*4;
- uint8_t* max_ptr = col1 + (row_width * (height-2));
- uint8_t* max_ptr2 = col1 + row_width;
-
- __asm__ __volatile__ (
- "mov $0x1F1F1F1F, %%eax\n\t"
- "movd %%eax, %%xmm4\n\t"
- "pshufd $0x0, %%xmm4, %%xmm4\n\t"
- "movdqa %6, %%xmm3\n\t"
- "mov %5, %%eax\n\t"
-#if defined(__x86_64__)
- "movd %%eax, %%xmm8\n\t"
- "pshufd $0x0, %%xmm8, %%xmm8\n\t"
-#endif
- /* Zero the temporary register */
- "pxor %%xmm0, %%xmm0\n\t"
-
- "algo_ssse3_deinterlace_4field_argb:\n\t"
-
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
-
-#if defined(__x86_64__)
- "pcmpgtd %%xmm8, %%xmm1\n\t" // Compare average delta with the threshold
-#else
- "movd %%eax, %%xmm7\n\t" // Setup the threshold
- "pshufd $0x0, %%xmm7, %%xmm7\n\t"
-
- "pcmpgtd %%xmm7, %%xmm1\n\t" // Compare average delta with the threshold
-#endif
- "movdqa (%0,%4), %%xmm2\n\t" // Load pbelow
- "pavgb %%xmm5, %%xmm2\n\t" // Average pabove and pbelow
- "pand %%xmm1, %%xmm2\n\t" // Filter out pixels in avg that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm2, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
-
- "sub %4, %0\n\t" // Restore pcurrent to pabove
- "sub %4, %1\n\t" // Restore pncurrent to pnabove
-
- /* Next pixels */
- "add $0x10, %0\n\t" // Add 16 to pcurrent
- "add $0x10, %1\n\t" // Add 16 to pncurrent
-
- /* Check if we reached the row end */
- "cmp %2, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_argb\n\t" // Go for another iteration
-
- /* Next row */
- "add %4, %0\n\t" // Add width to pcurrent
- "add %4, %1\n\t" // Add width to pncurrent
- "mov %0, %2\n\t"
- "add %4, %2\n\t" // Add width to max_ptr2
-
- /* Check if we reached the end */
- "cmp %3, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_argb\n\t" // Go for another iteration
-
- /* Special case for the last line */
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
-
-#if defined(__x86_64__)
- "pcmpgtd %%xmm8, %%xmm1\n\t" // Compare average delta with the threshold
-#else
- "movd %%eax, %%xmm7\n\t" // Setup the threshold
- "pshufd $0x0, %%xmm7, %%xmm7\n\t"
-
- "pcmpgtd %%xmm7, %%xmm1\n\t" // Compare average delta with the threshold
-#endif
- "pand %%xmm1, %%xmm5\n\t" // Filter out pixels in pabove that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm5, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
- :
- : "r" (col1), "r" (col2), "r" (max_ptr2), "r" (max_ptr), "r" (row_width), "m" (threshold_val), "m" (*movemask2)
-#if defined(__x86_64__)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "%xmm8", "cc", "memory"
-#else
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "cc", "memory"
-#endif
- );
-#else
- Panic("SSE function called on a non x86\\x86-64 platform");
-#endif
-}
-
-/* ABGR SSSE3 */
-#if defined(__i386__) || defined(__x86_64__)
-__attribute__((noinline,__target__("ssse3")))
-#endif
-void ssse3_deinterlace_4field_abgr(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height) {
-#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
- __attribute__((aligned(16))) static const uint8_t movemask2[16] = {2,2,2,2,2,3,3,1,10,10,10,10,10,11,11,9};
-
- const uint32_t threshold_val = threshold;
-
- unsigned long row_width = width*4;
- uint8_t* max_ptr = col1 + (row_width * (height-2));
- uint8_t* max_ptr2 = col1 + row_width;
-
- __asm__ __volatile__ (
- "mov $0x1F1F1F1F, %%eax\n\t"
- "movd %%eax, %%xmm4\n\t"
- "pshufd $0x0, %%xmm4, %%xmm4\n\t"
- "movdqa %6, %%xmm3\n\t"
- "mov %5, %%eax\n\t"
-#if defined(__x86_64__)
- "movd %%eax, %%xmm8\n\t"
- "pshufd $0x0, %%xmm8, %%xmm8\n\t"
-#endif
- /* Zero the temporary register */
- "pxor %%xmm0, %%xmm0\n\t"
-
- "algo_ssse3_deinterlace_4field_abgr:\n\t"
-
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
-
-#if defined(__x86_64__)
- "pcmpgtd %%xmm8, %%xmm1\n\t" // Compare average delta with the threshold
-#else
- "movd %%eax, %%xmm7\n\t" // Setup the threshold
- "pshufd $0x0, %%xmm7, %%xmm7\n\t"
-
- "pcmpgtd %%xmm7, %%xmm1\n\t" // Compare average delta with the threshold
-#endif
- "movdqa (%0,%4), %%xmm2\n\t" // Load pbelow
- "pavgb %%xmm5, %%xmm2\n\t" // Average pabove and pbelow
- "pand %%xmm1, %%xmm2\n\t" // Filter out pixels in avg that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm2, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
-
- "sub %4, %0\n\t" // Restore pcurrent to pabove
- "sub %4, %1\n\t" // Restore pncurrent to pnabove
-
- /* Next pixels */
- "add $0x10, %0\n\t" // Add 16 to pcurrent
- "add $0x10, %1\n\t" // Add 16 to pncurrent
-
- /* Check if we reached the row end */
- "cmp %2, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_abgr\n\t" // Go for another iteration
-
- /* Next row */
- "add %4, %0\n\t" // Add width to pcurrent
- "add %4, %1\n\t" // Add width to pncurrent
- "mov %0, %2\n\t"
- "add %4, %2\n\t" // Add width to max_ptr2
-
- /* Check if we reached the end */
- "cmp %3, %0\n\t"
- "jb algo_ssse3_deinterlace_4field_abgr\n\t" // Go for another iteration
-
- /* Special case for the last line */
- /* Load pabove into xmm1 and pnabove into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm5\n\t" /* Keep backup of pabove in xmm5 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
- "movdqa %%xmm1, %%xmm7\n\t" /* Backup of delta2 in xmm7 for now */
-
- /* Next row */
- "add %4, %0\n\t"
- "add %4, %1\n\t"
-
- /* Load pcurrent into xmm1 and pncurrent into xmm2 */
- "movdqa (%0), %%xmm1\n\t"
- "movdqa (%1), %%xmm2\n\t"
- "movdqa %%xmm1, %%xmm6\n\t" /* Keep backup of pcurrent in xmm6 */
- "psrlq $0x3, %%xmm1\n\t"
- "psrlq $0x3, %%xmm2\n\t"
- "pand %%xmm4, %%xmm1\n\t"
- "pand %%xmm4, %%xmm2\n\t"
- "psubb %%xmm2, %%xmm1\n\t"
- "pabsb %%xmm1, %%xmm2\n\t"
- "movdqa %%xmm2, %%xmm1\n\t"
- "punpckldq %%xmm1, %%xmm1\n\t"
- "pshufb %%xmm3, %%xmm1\n\t"
- "psadbw %%xmm0, %%xmm1\n\t"
- "punpckhdq %%xmm2, %%xmm2\n\t"
- "pshufb %%xmm3, %%xmm2\n\t"
- "psadbw %%xmm0, %%xmm2\n\t"
- "packuswb %%xmm2, %%xmm1\n\t"
-
- "pavgb %%xmm7, %%xmm1\n\t" // Average the two deltas together
-
-#if defined(__x86_64__)
- "pcmpgtd %%xmm8, %%xmm1\n\t" // Compare average delta with the threshold
-#else
- "movd %%eax, %%xmm7\n\t" // Setup the threshold
- "pshufd $0x0, %%xmm7, %%xmm7\n\t"
-
- "pcmpgtd %%xmm7, %%xmm1\n\t" // Compare average delta with the threshold
-#endif
- "pand %%xmm1, %%xmm5\n\t" // Filter out pixels in pabove that shouldn't be copied
- "pandn %%xmm6, %%xmm1\n\t" // Filter out pixels in pcurrent that should be replaced
-
- "por %%xmm5, %%xmm1\n\t" // Put the new values in pcurrent
- "movntdq %%xmm1, (%0)\n\t" // Write pcurrent
- :
- : "r" (col1), "r" (col2), "r" (max_ptr2), "r" (max_ptr), "r" (row_width), "m" (threshold_val), "m" (*movemask2)
-#if defined(__x86_64__)
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "%xmm8", "cc", "memory"
-#else
- : "%eax", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "cc", "memory"
-#endif
- );
-#else
- Panic("SSE function called on a non x86\\x86-64 platform");
-#endif
-}
diff --git a/src/zm_image.h b/src/zm_image.h
index 0a01f1f18..0a90c5c23 100644
--- a/src/zm_image.h
+++ b/src/zm_image.h
@@ -122,8 +122,8 @@ protected:
}
public:
- enum { CHAR_HEIGHT=11, CHAR_WIDTH=6 };
- enum { LINE_HEIGHT=CHAR_HEIGHT+0 };
+ enum { ZM_CHAR_HEIGHT=11, ZM_CHAR_WIDTH=6 };
+ enum { LINE_HEIGHT=ZM_CHAR_HEIGHT+0 };
protected:
static bool initialised;
@@ -264,6 +264,7 @@ public:
/* Blend functions */
void sse2_fastblend(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, double blendpercent);
void std_fastblend(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, double blendpercent);
+void neon32_armv7_fastblend(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, double blendpercent);
void std_blend(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count, double blendpercent);
/* Delta functions */
@@ -274,6 +275,11 @@ void std_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result,
void std_delta8_bgra(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
void std_delta8_argb(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
void std_delta8_abgr(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
+void neon32_armv7_delta8_gray8(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
+void neon32_armv7_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
+void neon32_armv7_delta8_bgra(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
+void neon32_armv7_delta8_argb(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
+void neon32_armv7_delta8_abgr(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
void sse2_delta8_gray8(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
void sse2_delta8_rgba(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
void sse2_delta8_bgra(const uint8_t* col1, const uint8_t* col2, uint8_t* result, unsigned long count);
@@ -293,6 +299,9 @@ void std_convert_argb_gray8(const uint8_t* col1, uint8_t* result, unsigned long
void std_convert_abgr_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
void std_convert_yuyv_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
void ssse3_convert_rgba_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
+void ssse3_convert_bgra_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
+void ssse3_convert_argb_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
+void ssse3_convert_abgr_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
void ssse3_convert_yuyv_gray8(const uint8_t* col1, uint8_t* result, unsigned long count);
void zm_convert_yuyv_rgb(const uint8_t* col1, uint8_t* result, unsigned long count);
void zm_convert_yuyv_rgba(const uint8_t* col1, uint8_t* result, unsigned long count);
@@ -309,8 +318,3 @@ void std_deinterlace_4field_rgba(uint8_t* col1, uint8_t* col2, unsigned int thre
void std_deinterlace_4field_bgra(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height);
void std_deinterlace_4field_argb(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height);
void std_deinterlace_4field_abgr(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height);
-void ssse3_deinterlace_4field_gray8(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height);
-void ssse3_deinterlace_4field_rgba(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height);
-void ssse3_deinterlace_4field_bgra(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height);
-void ssse3_deinterlace_4field_argb(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height);
-void ssse3_deinterlace_4field_abgr(uint8_t* col1, uint8_t* col2, unsigned int threshold, unsigned int width, unsigned int height);
diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp
index 39efb712d..516d11a0c 100644
--- a/src/zm_monitor.cpp
+++ b/src/zm_monitor.cpp
@@ -638,6 +638,7 @@ Monitor::~Monitor() {
close( map_fd );
if ( purpose == CAPTURE ) {
+ // How about we store this in the object on instantiation so that we don't have to do this again.
char mmap_path[PATH_MAX] = "";
snprintf( mmap_path, sizeof(mmap_path), "%s/zm.mmap.%d", config.path_map, id );
@@ -1380,7 +1381,8 @@ bool Monitor::Analyse() {
}
}
} // end if section_length
- if ( !event ) {
+
+ if ( ! event ) {
// Create event
event = new Event( this, *timestamp, "Continuous", noteSetMap, videoRecording );
@@ -1443,7 +1445,7 @@ bool Monitor::Analyse() {
}
event->AddFrames( pre_event_images, images, timestamps );
}
- }
+ } // end if false or config.overlap_timed_events
} // end if ! event
}
if ( score ) {
@@ -1619,7 +1621,8 @@ bool Monitor::Analyse() {
}
shared_data->state = state = IDLE;
last_section_mod = 0;
- }
+ } // end if ( trigger_data->trigger_state != TRIGGER_OFF )
+
if ( (!signal_change && signal) && (function == MODECT || function == MOCORD) ) {
if ( state == ALARM ) {
ref_image.Blend( *snap_image, alarm_ref_blend_perc );
@@ -1628,9 +1631,9 @@ bool Monitor::Analyse() {
}
}
last_signal = signal;
- }
+ } // end if Enabled()
- shared_data->last_read_index = index%image_buffer_count;
+ shared_data->last_read_index = index % image_buffer_count;
//shared_data->last_read_time = image_buffer[index].timestamp->tv_sec;
shared_data->last_read_time = now.tv_sec;
@@ -3235,782 +3238,6 @@ bool Monitor::DumpSettings( char *output, bool verbose ) {
return( true );
} // bool Monitor::DumpSettings( char *output, bool verbose )
-bool MonitorStream::checkSwapPath( const char *path, bool create_path ) {
- uid_t uid = getuid();
- gid_t gid = getgid();
-
- struct stat stat_buf;
- if ( stat( path, &stat_buf ) < 0 ) {
- if ( create_path && errno == ENOENT ) {
- Debug( 3, "Swap path '%s' missing, creating", path );
- if ( mkdir( path, 0755 ) ) {
- Error( "Can't mkdir %s: %s", path, strerror(errno));
- return( false );
- }
- if ( stat( path, &stat_buf ) < 0 ) {
- Error( "Can't stat '%s': %s", path, strerror(errno) );
- return( false );
- }
- } else {
- Error( "Can't stat '%s': %s", path, strerror(errno) );
- return( false );
- }
- }
- if ( !S_ISDIR(stat_buf.st_mode) ) {
- Error( "Swap image path '%s' is not a directory", path );
- return( false );
- }
-
- mode_t mask = 0;
- if ( uid == stat_buf.st_uid ) {
- // If we are the owner
- mask = 00700;
- } else if ( gid == stat_buf.st_gid ) {
- // If we are in the owner group
- mask = 00070;
- } else {
- // We are neither the owner nor in the group
- mask = 00007;
- }
-
- if ( (stat_buf.st_mode & mask) != mask ) {
- Error( "Insufficient permissions on swap image path '%s'", path );
- return( false );
- }
- return( true );
-}
-
-void MonitorStream::processCommand( const CmdMsg *msg ) {
- Debug( 2, "Got message, type %d, msg %d", msg->msg_type, msg->msg_data[0] );
- // Check for incoming command
- switch( (MsgCommand)msg->msg_data[0] ) {
- case CMD_PAUSE :
- {
- Debug( 1, "Got PAUSE command" );
-
- // Set paused flag
- paused = true;
- // Set delayed flag
- delayed = true;
- last_frame_sent = TV_2_FLOAT( now );
- break;
- }
- case CMD_PLAY :
- {
- Debug( 1, "Got PLAY command" );
- if ( paused ) {
- // Clear paused flag
- paused = false;
- // Set delayed_play flag
- delayed = true;
- }
- replay_rate = ZM_RATE_BASE;
- break;
- }
- case CMD_VARPLAY :
- {
- Debug( 1, "Got VARPLAY command" );
- if ( paused ) {
- // Clear paused flag
- paused = false;
- // Set delayed_play flag
- delayed = true;
- }
- replay_rate = ntohs(((unsigned char)msg->msg_data[2]<<8)|(unsigned char)msg->msg_data[1])-32768;
- break;
- }
- case CMD_STOP :
- {
- Debug( 1, "Got STOP command" );
-
- // Clear paused flag
- paused = false;
- // Clear delayed_play flag
- delayed = false;
- break;
- }
- case CMD_FASTFWD :
- {
- Debug( 1, "Got FAST FWD command" );
- if ( paused ) {
- // Clear paused flag
- paused = false;
- // Set delayed_play flag
- delayed = true;
- }
- // Set play rate
- switch ( replay_rate )
- {
- case 2 * ZM_RATE_BASE :
- replay_rate = 5 * ZM_RATE_BASE;
- break;
- case 5 * ZM_RATE_BASE :
- replay_rate = 10 * ZM_RATE_BASE;
- break;
- case 10 * ZM_RATE_BASE :
- replay_rate = 25 * ZM_RATE_BASE;
- break;
- case 25 * ZM_RATE_BASE :
- case 50 * ZM_RATE_BASE :
- replay_rate = 50 * ZM_RATE_BASE;
- break;
- default :
- replay_rate = 2 * ZM_RATE_BASE;
- break;
- }
- break;
- }
- case CMD_SLOWFWD :
- {
- Debug( 1, "Got SLOW FWD command" );
- // Set paused flag
- paused = true;
- // Set delayed flag
- delayed = true;
- // Set play rate
- replay_rate = ZM_RATE_BASE;
- // Set step
- step = 1;
- break;
- }
- case CMD_SLOWREV :
- {
- Debug( 1, "Got SLOW REV command" );
- // Set paused flag
- paused = true;
- // Set delayed flag
- delayed = true;
- // Set play rate
- replay_rate = ZM_RATE_BASE;
- // Set step
- step = -1;
- break;
- }
- case CMD_FASTREV :
- {
- Debug( 1, "Got FAST REV command" );
- if ( paused ) {
- // Clear paused flag
- paused = false;
- // Set delayed_play flag
- delayed = true;
- }
- // Set play rate
- switch ( replay_rate ) {
- case -2 * ZM_RATE_BASE :
- replay_rate = -5 * ZM_RATE_BASE;
- break;
- case -5 * ZM_RATE_BASE :
- replay_rate = -10 * ZM_RATE_BASE;
- break;
- case -10 * ZM_RATE_BASE :
- replay_rate = -25 * ZM_RATE_BASE;
- break;
- case -25 * ZM_RATE_BASE :
- case -50 * ZM_RATE_BASE :
- replay_rate = -50 * ZM_RATE_BASE;
- break;
- default :
- replay_rate = -2 * ZM_RATE_BASE;
- break;
- }
- break;
- }
- case CMD_ZOOMIN :
- {
- x = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
- y = ((unsigned char)msg->msg_data[3]<<8)|(unsigned char)msg->msg_data[4];
- Debug( 1, "Got ZOOM IN command, to %d,%d", x, y );
- switch ( zoom ) {
- case 100:
- zoom = 150;
- break;
- case 150:
- zoom = 200;
- break;
- case 200:
- zoom = 300;
- break;
- case 300:
- zoom = 400;
- break;
- case 400:
- default :
- zoom = 500;
- break;
- }
- break;
- }
- case CMD_ZOOMOUT :
- {
- Debug( 1, "Got ZOOM OUT command" );
- switch ( zoom ) {
- case 500:
- zoom = 400;
- break;
- case 400:
- zoom = 300;
- break;
- case 300:
- zoom = 200;
- break;
- case 200:
- zoom = 150;
- break;
- case 150:
- default :
- zoom = 100;
- break;
- }
- break;
- }
- case CMD_PAN :
- {
- x = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
- y = ((unsigned char)msg->msg_data[3]<<8)|(unsigned char)msg->msg_data[4];
- Debug( 1, "Got PAN command, to %d,%d", x, y );
- break;
- }
- case CMD_SCALE :
- {
- scale = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
- Debug( 1, "Got SCALE command, to %d", scale );
- break;
- }
- case CMD_QUIT :
- {
- Info ("User initiated exit - CMD_QUIT");
- break;
- }
- case CMD_QUERY :
- {
- Debug( 1, "Got QUERY command, sending STATUS" );
- break;
- }
- default :
- {
- Error( "Got unexpected command %d", msg->msg_data[0] );
- break;
- }
- }
-
- struct {
- int id;
- int state;
- double fps;
- int buffer_level;
- int rate;
- double delay;
- int zoom;
- bool delayed;
- bool paused;
- bool enabled;
- bool forced;
- } status_data;
-
- status_data.id = monitor->Id();
- status_data.fps = monitor->GetFPS();
- status_data.state = monitor->shared_data->state;
- if ( playback_buffer > 0 )
- status_data.buffer_level = (MOD_ADD( (temp_write_index-temp_read_index), 0, temp_image_buffer_count )*100)/temp_image_buffer_count;
- else
- status_data.buffer_level = 0;
- status_data.delayed = delayed;
- status_data.paused = paused;
- status_data.rate = replay_rate;
- status_data.delay = TV_2_FLOAT( now ) - TV_2_FLOAT( last_frame_timestamp );
- status_data.zoom = zoom;
- //status_data.enabled = monitor->shared_data->active;
- status_data.enabled = monitor->trigger_data->trigger_state!=Monitor::TRIGGER_OFF;
- status_data.forced = monitor->trigger_data->trigger_state==Monitor::TRIGGER_ON;
- Debug( 2, "L:%d, D:%d, P:%d, R:%d, d:%.3f, Z:%d, E:%d F:%d",
- status_data.buffer_level,
- status_data.delayed,
- status_data.paused,
- status_data.rate,
- status_data.delay,
- status_data.zoom,
- status_data.enabled,
- status_data.forced
- );
-
- DataMsg status_msg;
- status_msg.msg_type = MSG_DATA_WATCH;
- memcpy( &status_msg.msg_data, &status_data, sizeof(status_data) );
- int nbytes = 0;
- if ( (nbytes = sendto( sd, &status_msg, sizeof(status_msg), MSG_DONTWAIT, (sockaddr *)&rem_addr, sizeof(rem_addr) )) < 0 ) {
- //if ( errno != EAGAIN )
- {
- Error( "Can't sendto on sd %d: %s", sd, strerror(errno) );
- //exit( -1 );
- }
- }
-
- // quit after sending a status, if this was a quit request
- if ((MsgCommand)msg->msg_data[0]==CMD_QUIT)
- exit(0);
-
- updateFrameRate( monitor->GetFPS() );
-}
-
-bool MonitorStream::sendFrame( const char *filepath, struct timeval *timestamp ) {
- bool send_raw = ((scale>=ZM_SCALE_BASE)&&(zoom==ZM_SCALE_BASE));
-
- if ( type != STREAM_JPEG )
- send_raw = false;
- if ( !config.timestamp_on_capture && timestamp )
- send_raw = false;
-
- if ( !send_raw ) {
- Image temp_image( filepath );
-
- return( sendFrame( &temp_image, timestamp ) );
- } else {
- int img_buffer_size = 0;
- static unsigned char img_buffer[ZM_MAX_IMAGE_SIZE];
-
- FILE *fdj = NULL;
- if ( (fdj = fopen( filepath, "r" )) ) {
- img_buffer_size = fread( img_buffer, 1, sizeof(img_buffer), fdj );
- fclose( fdj );
- } else {
- Error( "Can't open %s: %s", filepath, strerror(errno) );
- return( false );
- }
-
- // Calculate how long it takes to actually send the frame
- struct timeval frameStartTime;
- gettimeofday( &frameStartTime, NULL );
-
- fprintf( stdout, "--ZoneMinderFrame\r\n" );
- fprintf( stdout, "Content-Length: %d\r\n", img_buffer_size );
- fprintf( stdout, "Content-Type: image/jpeg\r\n\r\n" );
- if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) != 1 ) {
- if ( !zm_terminate )
- Error( "Unable to send stream frame: %s", strerror(errno) );
- return( false );
- }
- fprintf( stdout, "\r\n\r\n" );
- fflush( stdout );
-
- struct timeval frameEndTime;
- gettimeofday( &frameEndTime, NULL );
-
- int frameSendTime = tvDiffMsec( frameStartTime, frameEndTime );
- if ( frameSendTime > 1000/maxfps ) {
- maxfps /= 2;
- Error( "Frame send time %d msec too slow, throttling maxfps to %.2f", frameSendTime, maxfps );
- }
-
- last_frame_sent = TV_2_FLOAT( now );
-
- return( true );
- }
- return( false );
-}
-
-bool MonitorStream::sendFrame( Image *image, struct timeval *timestamp ) {
- Image *send_image = prepareImage( image );
- if ( !config.timestamp_on_capture && timestamp )
- monitor->TimestampImage( send_image, timestamp );
-
-#if HAVE_LIBAVCODEC
- if ( type == STREAM_MPEG ) {
- if ( !vid_stream ) {
- vid_stream = new VideoStream( "pipe:", format, bitrate, effective_fps, send_image->Colours(), send_image->SubpixelOrder(), send_image->Width(), send_image->Height() );
- fprintf( stdout, "Content-type: %s\r\n\r\n", vid_stream->MimeType() );
- vid_stream->OpenStream();
- }
- static struct timeval base_time;
- struct DeltaTimeval delta_time;
- if ( !frame_count )
- base_time = *timestamp;
- DELTA_TIMEVAL( delta_time, *timestamp, base_time, DT_PREC_3 );
- /* double pts = */ vid_stream->EncodeFrame( send_image->Buffer(), send_image->Size(), config.mpeg_timed_frames, delta_time.delta );
- } else
-#endif // HAVE_LIBAVCODEC
- {
- static unsigned char temp_img_buffer[ZM_MAX_IMAGE_SIZE];
-
- int img_buffer_size = 0;
- unsigned char *img_buffer = temp_img_buffer;
-
- // Calculate how long it takes to actually send the frame
- struct timeval frameStartTime;
- gettimeofday( &frameStartTime, NULL );
-
- fprintf( stdout, "--ZoneMinderFrame\r\n" );
- switch( type ) {
- case STREAM_JPEG :
- send_image->EncodeJpeg( img_buffer, &img_buffer_size );
- fprintf( stdout, "Content-Type: image/jpeg\r\n" );
- break;
- case STREAM_RAW :
- fprintf( stdout, "Content-Type: image/x-rgb\r\n" );
- img_buffer = (uint8_t*)send_image->Buffer();
- img_buffer_size = send_image->Size();
- break;
- case STREAM_ZIP :
- fprintf( stdout, "Content-Type: image/x-rgbz\r\n" );
- unsigned long zip_buffer_size;
- send_image->Zip( img_buffer, &zip_buffer_size );
- img_buffer_size = zip_buffer_size;
- break;
- default :
- Fatal( "Unexpected frame type %d", type );
- break;
- }
- fprintf( stdout, "Content-Length: %d\r\n\r\n", img_buffer_size );
- if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) != 1 ) {
- if ( !zm_terminate )
- Error( "Unable to send stream frame: %s", strerror(errno) );
- return( false );
- }
- fprintf( stdout, "\r\n\r\n" );
- fflush( stdout );
-
- struct timeval frameEndTime;
- gettimeofday( &frameEndTime, NULL );
-
- int frameSendTime = tvDiffMsec( frameStartTime, frameEndTime );
- if ( frameSendTime > 1000/maxfps ) {
- maxfps /= 1.5;
- Error( "Frame send time %d msec too slow, throttling maxfps to %.2f", frameSendTime, maxfps );
- }
- }
- last_frame_sent = TV_2_FLOAT( now );
- return( true );
-}
-
-void MonitorStream::runStream() {
- if ( type == STREAM_SINGLE ) {
- // Not yet migrated over to stream class
- monitor->SingleImage( scale );
- return;
- }
-
- openComms();
-
- checkInitialised();
-
- updateFrameRate( monitor->GetFPS() );
-
- 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;
-
- time_t stream_start_time;
- time( &stream_start_time );
-
- frame_count = 0;
-
- temp_image_buffer = 0;
- temp_image_buffer_count = playback_buffer;
- temp_read_index = temp_image_buffer_count;
- temp_write_index = temp_image_buffer_count;
-
- char *swap_path = 0;
- bool buffered_playback = false;
-
- // 15 is the max length for the swap path suffix, /zmswap-whatever, assuming max 6 digits for monitor id
- const int max_swap_len_suffix = 15;
-
- int swap_path_length = strlen(config.path_swap) + 1; // +1 for NULL terminator
- int subfolder1_length = snprintf(NULL, 0, "/zmswap-m%d", monitor->Id() ) + 1;
- int subfolder2_length = snprintf(NULL, 0, "/zmswap-q%06d", connkey ) + 1;
- int total_swap_path_length = swap_path_length + subfolder1_length + subfolder2_length;
-
- if ( connkey && playback_buffer > 0 ) {
-
- if ( total_swap_path_length + max_swap_len_suffix > PATH_MAX ) {
- Error( "Swap Path is too long. %d > %d ", total_swap_path_length+max_swap_len_suffix, PATH_MAX );
- } else {
- swap_path = (char *)malloc( total_swap_path_length+max_swap_len_suffix );
- strncpy( swap_path, config.path_swap, swap_path_length );
-
- Debug( 3, "Checking swap path folder: %s", swap_path );
- if ( checkSwapPath( swap_path, false ) ) {
- // Append the subfolder name /zmswap-m{monitor-id} to the end of swap_path
- int ndx = swap_path_length - 1; // Array index of the NULL terminator
- snprintf( &(swap_path[ndx]), subfolder1_length, "/zmswap-m%d", monitor->Id() );
-
- Debug( 4, "Checking swap path subfolder: %s", swap_path );
- if ( checkSwapPath( swap_path, true ) ) {
- // Append the subfolder name /zmswap-q{connection key} to the end of swap_path
- ndx = swap_path_length+subfolder1_length - 2; // Array index of the NULL terminator
- snprintf( &(swap_path[ndx]), subfolder2_length, "/zmswap-q%06d", connkey );
-
- Debug( 4, "Checking swap path subfolder: %s", swap_path );
- if ( checkSwapPath( swap_path, true ) ) {
- buffered_playback = true;
- }
- }
- }
-
- if ( !buffered_playback ) {
- Error( "Unable to validate swap image path, disabling buffered playback" );
- } else {
- Debug( 2, "Assigning temporary buffer" );
- temp_image_buffer = new SwapImage[temp_image_buffer_count];
- memset( temp_image_buffer, 0, sizeof(*temp_image_buffer)*temp_image_buffer_count );
- Debug( 2, "Assigned temporary buffer" );
- }
- }
- }
-
- float max_secs_since_last_sent_frame = 10.0; //should be > keep alive amount (5 secs)
- while ( !zm_terminate ) {
- bool got_command = false;
- if ( feof( stdout ) || ferror( stdout ) || !monitor->ShmValid() ) {
- break;
- }
-
- gettimeofday( &now, NULL );
-
- if ( connkey ) {
- while(checkCommandQueue()) {
- got_command = true;
- }
- }
-
- //bool frame_sent = false;
- if ( buffered_playback && delayed ) {
- if ( temp_read_index == temp_write_index ) {
- // Go back to live viewing
- Debug( 1, "Exceeded temporary streaming buffer" );
- // Clear paused flag
- paused = false;
- // Clear delayed_play flag
- delayed = false;
- replay_rate = ZM_RATE_BASE;
- } else {
- if ( !paused ) {
- int temp_index = MOD_ADD( temp_read_index, 0, temp_image_buffer_count );
- //Debug( 3, "tri: %d, ti: %d", temp_read_index, temp_index );
- SwapImage *swap_image = &temp_image_buffer[temp_index];
-
- if ( !swap_image->valid ) {
- paused = true;
- delayed = true;
- temp_read_index = MOD_ADD( temp_read_index, (replay_rate>=0?-1:1), temp_image_buffer_count );
- } else {
- //Debug( 3, "siT: %f, lfT: %f", TV_2_FLOAT( swap_image->timestamp ), TV_2_FLOAT( last_frame_timestamp ) );
- double expected_delta_time = ((TV_2_FLOAT( swap_image->timestamp ) - TV_2_FLOAT( last_frame_timestamp )) * ZM_RATE_BASE)/replay_rate;
- double actual_delta_time = TV_2_FLOAT( now ) - last_frame_sent;
-
- //Debug( 3, "eDT: %.3lf, aDT: %.3f, lFS:%.3f, NOW:%.3f", expected_delta_time, actual_delta_time, last_frame_sent, TV_2_FLOAT( now ) );
- // If the next frame is due
- if ( actual_delta_time > expected_delta_time ) {
- //Debug( 2, "eDT: %.3lf, aDT: %.3f", expected_delta_time, actual_delta_time );
- if ( temp_index%frame_mod == 0 ) {
- Debug( 2, "Sending delayed frame %d", temp_index );
- // Send the next frame
- if ( !sendFrame( temp_image_buffer[temp_index].file_name, &temp_image_buffer[temp_index].timestamp ) )
- zm_terminate = true;
- memcpy( &last_frame_timestamp, &(swap_image->timestamp), sizeof(last_frame_timestamp) );
- //frame_sent = true;
- }
- temp_read_index = MOD_ADD( temp_read_index, (replay_rate>0?1:-1), temp_image_buffer_count );
- }
- }
- } else if ( step != 0 ) {
- temp_read_index = MOD_ADD( temp_read_index, (step>0?1:-1), temp_image_buffer_count );
-
- SwapImage *swap_image = &temp_image_buffer[temp_read_index];
-
- // Send the next frame
- if ( !sendFrame( temp_image_buffer[temp_read_index].file_name, &temp_image_buffer[temp_read_index].timestamp ) )
- zm_terminate = true;
- memcpy( &last_frame_timestamp, &(swap_image->timestamp), sizeof(last_frame_timestamp) );
- //frame_sent = true;
- step = 0;
- } else {
- int temp_index = MOD_ADD( temp_read_index, 0, temp_image_buffer_count );
-
- double actual_delta_time = TV_2_FLOAT( now ) - last_frame_sent;
- if ( got_command || actual_delta_time > 5 ) {
- // Send keepalive
- Debug( 2, "Sending keepalive frame %d", temp_index );
- // Send the next frame
- if ( !sendFrame( temp_image_buffer[temp_index].file_name, &temp_image_buffer[temp_index].timestamp ) )
- zm_terminate = true;
- //frame_sent = true;
- }
- }
- }
- if ( temp_read_index == temp_write_index ) {
- // Go back to live viewing
- Warning( "Rewound over write index, resuming live play" );
- // Clear paused flag
- paused = false;
- // Clear delayed_play flag
- 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;
- last_read_index = monitor->shared_data->last_write_index;
- //Debug( 1, "%d: %x - %x", index, image_buffer[index].image, image_buffer[index].image->buffer );
- if ( (frame_mod == 1) || ((frame_count%frame_mod) == 0) ) {
- if ( !paused && !delayed ) {
- // Send the next frame
- Monitor::Snapshot *snap = &monitor->image_buffer[index];
-
- if ( !sendFrame( snap->image, snap->timestamp ) )
- zm_terminate = true;
- memcpy( &last_frame_timestamp, snap->timestamp, sizeof(last_frame_timestamp) );
- //frame_sent = true;
-
- temp_read_index = temp_write_index;
- }
- }
- if ( buffered_playback ) {
- if ( monitor->shared_data->valid ) {
- if ( monitor->image_buffer[index].timestamp->tv_sec ) {
- int temp_index = temp_write_index%temp_image_buffer_count;
- Debug( 2, "Storing frame %d", temp_index );
- if ( !temp_image_buffer[temp_index].valid ) {
- snprintf( temp_image_buffer[temp_index].file_name, sizeof(temp_image_buffer[0].file_name), "%s/zmswap-i%05d.jpg", swap_path, temp_index );
- temp_image_buffer[temp_index].valid = true;
- }
- memcpy( &(temp_image_buffer[temp_index].timestamp), monitor->image_buffer[index].timestamp, sizeof(temp_image_buffer[0].timestamp) );
- monitor->image_buffer[index].image->WriteJpeg( temp_image_buffer[temp_index].file_name, config.jpeg_file_quality );
- temp_write_index = MOD_ADD( temp_write_index, 1, temp_image_buffer_count );
- if ( temp_write_index == temp_read_index ) {
- // Go back to live viewing
- Warning( "Exceeded temporary buffer, resuming live play" );
- // Clear paused flag
- paused = false;
- // Clear delayed_play flag
- delayed = false;
- replay_rate = ZM_RATE_BASE;
- }
- } else {
- Warning( "Unable to store frame as timestamp invalid" );
- }
- } else {
- Warning( "Unable to store frame as shared memory invalid" );
- }
- }
- frame_count++;
- }
- usleep( (unsigned long)((1000000 * ZM_RATE_BASE)/((base_fps?base_fps:1)*abs(replay_rate*2))) );
- if ( ttl ) {
- if ( (now.tv_sec - stream_start_time) > ttl ) {
- break;
- }
- }
- 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;
- }
- }
- if ( buffered_playback ) {
- Debug( 1, "Cleaning swap files from %s", swap_path );
- struct stat stat_buf;
- if ( stat( swap_path, &stat_buf ) < 0 ) {
- if ( errno != ENOENT ) {
- Error( "Can't stat '%s': %s", swap_path, strerror(errno) );
- }
- } else if ( !S_ISDIR(stat_buf.st_mode) ) {
- Error( "Swap image path '%s' is not a directory", swap_path );
- } else {
- char glob_pattern[PATH_MAX] = "";
-
- snprintf( glob_pattern, sizeof(glob_pattern), "%s/*.*", swap_path );
- glob_t pglob;
- int glob_status = glob( glob_pattern, 0, 0, &pglob );
- if ( glob_status != 0 ) {
- if ( glob_status < 0 ) {
- Error( "Can't glob '%s': %s", glob_pattern, strerror(errno) );
- } else {
- Debug( 1, "Can't glob '%s': %d", glob_pattern, glob_status );
- }
- } else {
- for ( unsigned int i = 0; i < pglob.gl_pathc; i++ ) {
- if ( unlink( pglob.gl_pathv[i] ) < 0 ) {
- Error( "Can't unlink '%s': %s", pglob.gl_pathv[i], strerror(errno) );
- }
- }
- }
- globfree( &pglob );
- if ( rmdir( swap_path ) < 0 ) {
- Error( "Can't rmdir '%s': %s", swap_path, strerror(errno) );
- }
- }
- }
- if ( swap_path ) free( swap_path );
- closeComms();
-}
-
-void Monitor::SingleImage( int scale) {
- int img_buffer_size = 0;
- static JOCTET img_buffer[ZM_MAX_IMAGE_SIZE];
- Image scaled_image;
- int index = shared_data->last_write_index%image_buffer_count;
- Snapshot *snap = &image_buffer[index];
- Image *snap_image = snap->image;
-
- if ( scale != ZM_SCALE_BASE ) {
- scaled_image.Assign( *snap_image );
- scaled_image.Scale( scale );
- snap_image = &scaled_image;
- }
- if ( !config.timestamp_on_capture ) {
- TimestampImage( snap_image, snap->timestamp );
- }
- snap_image->EncodeJpeg( img_buffer, &img_buffer_size );
-
- fprintf( stdout, "Content-Length: %d\r\n", img_buffer_size );
- fprintf( stdout, "Content-Type: image/jpeg\r\n\r\n" );
- fwrite( img_buffer, img_buffer_size, 1, stdout );
-}
-
-void Monitor::SingleImageRaw( int scale) {
- Image scaled_image;
- int index = shared_data->last_write_index%image_buffer_count;
- Snapshot *snap = &image_buffer[index];
- Image *snap_image = snap->image;
-
- if ( scale != ZM_SCALE_BASE ) {
- scaled_image.Assign( *snap_image );
- scaled_image.Scale( scale );
- snap_image = &scaled_image;
- }
- if ( !config.timestamp_on_capture ) {
- TimestampImage( snap_image, snap->timestamp );
- }
-
- fprintf( stdout, "Content-Length: %d\r\n", snap_image->Size() );
- fprintf( stdout, "Content-Type: image/x-rgb\r\n\r\n" );
- fwrite( snap_image->Buffer(), snap_image->Size(), 1, stdout );
-}
-
-void Monitor::SingleImageZip( int scale) {
- unsigned long img_buffer_size = 0;
- static Bytef img_buffer[ZM_MAX_IMAGE_SIZE];
- Image scaled_image;
- int index = shared_data->last_write_index%image_buffer_count;
- Snapshot *snap = &image_buffer[index];
- Image *snap_image = snap->image;
-
- if ( scale != ZM_SCALE_BASE ) {
- scaled_image.Assign( *snap_image );
- scaled_image.Scale( scale );
- snap_image = &scaled_image;
- }
- if ( !config.timestamp_on_capture ) {
- TimestampImage( snap_image, snap->timestamp );
- }
- snap_image->Zip( img_buffer, &img_buffer_size );
-
- fprintf( stdout, "Content-Length: %ld\r\n", img_buffer_size );
- fprintf( stdout, "Content-Type: image/x-rgbz\r\n\r\n" );
- fwrite( img_buffer, img_buffer_size, 1, stdout );
-}
-
unsigned int Monitor::Colours() const { return( camera->Colours() ); }
unsigned int Monitor::SubpixelOrder() const { return( camera->SubpixelOrder() ); }
int Monitor::PrimeCapture() {
@@ -4023,3 +3250,7 @@ int Monitor::PostCapture() {
return( camera->PostCapture() );
}
Monitor::Orientation Monitor::getOrientation() const { return orientation; }
+
+Monitor::Snapshot *Monitor::getSnapshot() {
+ return &image_buffer[ shared_data->last_write_index%image_buffer_count ];
+}
diff --git a/src/zm_monitor.h b/src/zm_monitor.h
index d17da0477..91b464f88 100644
--- a/src/zm_monitor.h
+++ b/src/zm_monitor.h
@@ -443,6 +443,7 @@ public:
State GetState() const;
int GetImage( int index=-1, int scale=100 );
+Snapshot *getSnapshot();
struct timeval GetTimestamp( int index=-1 ) const;
void UpdateAdaptiveSkip();
useconds_t GetAnalysisRate();
@@ -509,9 +510,6 @@ public:
//void StreamImages( int scale=100, int maxfps=10, time_t ttl=0, int msq_id=0 );
//void StreamImagesRaw( int scale=100, int maxfps=10, time_t ttl=0 );
//void StreamImagesZip( int scale=100, int maxfps=10, time_t ttl=0 );
- void SingleImage( int scale=100 );
- void SingleImageRaw( int scale=100 );
- void SingleImageZip( int scale=100 );
#if HAVE_LIBAVCODEC
//void StreamMpeg( const char *format, int scale=100, int maxfps=10, int bitrate=100000 );
#endif // HAVE_LIBAVCODEC
@@ -519,49 +517,4 @@ public:
#define MOD_ADD( var, delta, limit ) (((var)+(limit)+(delta))%(limit))
-class MonitorStream : public StreamBase {
- protected:
- typedef struct SwapImage {
- bool valid;
- struct timeval timestamp;
- char file_name[PATH_MAX];
- } SwapImage;
-
- private:
- SwapImage *temp_image_buffer;
- int temp_image_buffer_count;
- int temp_read_index;
- int temp_write_index;
-
- protected:
- time_t ttl;
-
- protected:
- int playback_buffer;
- bool delayed;
-
- int frame_count;
-
- protected:
- bool checkSwapPath( const char *path, bool create_path );
-
- bool sendFrame( const char *filepath, struct timeval *timestamp );
- bool sendFrame( Image *image, struct timeval *timestamp );
- void processCommand( const CmdMsg *msg );
-
- public:
- MonitorStream() : playback_buffer( 0 ), delayed( false ), frame_count( 0 ) {
- }
- void setStreamBuffer( int p_playback_buffer ) {
- playback_buffer = p_playback_buffer;
- }
- void setStreamTTL( time_t p_ttl ) {
- ttl = p_ttl;
- }
- bool setStreamStart( int monitor_id ) {
- return loadMonitor( monitor_id );
- }
- void runStream();
-};
-
#endif // ZM_MONITOR_H
diff --git a/src/zm_monitorstream.cpp b/src/zm_monitorstream.cpp
new file mode 100644
index 000000000..07da05811
--- /dev/null
+++ b/src/zm_monitorstream.cpp
@@ -0,0 +1,803 @@
+//
+// ZoneMinder Monitor Class Implementation, $Date$, $Revision$
+// Copyright (C) 2001-2008 Philip Coombes
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+
+#include "zm.h"
+#include "zm_db.h"
+#include "zm_time.h"
+#include "zm_mpeg.h"
+#include "zm_signal.h"
+#include "zm_monitor.h"
+#include "zm_monitorstream.h"
+#include
+#include
+
+bool MonitorStream::checkSwapPath( const char *path, bool create_path ) {
+
+ struct stat stat_buf;
+ if ( stat( path, &stat_buf ) < 0 ) {
+ if ( create_path && errno == ENOENT ) {
+ Debug( 3, "Swap path '%s' missing, creating", path );
+ if ( mkdir( path, 0755 ) ) {
+ Error( "Can't mkdir %s: %s", path, strerror(errno));
+ return( false );
+ }
+ if ( stat( path, &stat_buf ) < 0 ) {
+ Error( "Can't stat '%s': %s", path, strerror(errno) );
+ return( false );
+ }
+ } else {
+ Error( "Can't stat '%s': %s", path, strerror(errno) );
+ return( false );
+ }
+ }
+ if ( !S_ISDIR(stat_buf.st_mode) ) {
+ Error( "Swap image path '%s' is not a directory", path );
+ return( false );
+ }
+
+ uid_t uid = getuid();
+ gid_t gid = getgid();
+
+ mode_t mask = 0;
+ if ( uid == stat_buf.st_uid ) {
+ // If we are the owner
+ mask = 00700;
+ } else if ( gid == stat_buf.st_gid ) {
+ // If we are in the owner group
+ mask = 00070;
+ } else {
+ // We are neither the owner nor in the group
+ mask = 00007;
+ }
+
+ if ( (stat_buf.st_mode & mask) != mask ) {
+ Error( "Insufficient permissions on swap image path '%s'", path );
+ return( false );
+ }
+ return( true );
+} // end bool MonitorStream::checkSwapPath( const char *path, bool create_path )
+
+void MonitorStream::processCommand( const CmdMsg *msg ) {
+ Debug( 2, "Got message, type %d, msg %d", msg->msg_type, msg->msg_data[0] );
+ // Check for incoming command
+ switch( (MsgCommand)msg->msg_data[0] ) {
+ case CMD_PAUSE :
+ {
+ Debug( 1, "Got PAUSE command" );
+
+ // Set paused flag
+ paused = true;
+ // Set delayed flag
+ delayed = true;
+ last_frame_sent = TV_2_FLOAT( now );
+ break;
+ }
+ case CMD_PLAY :
+ {
+ Debug( 1, "Got PLAY command" );
+ if ( paused ) {
+ // Clear paused flag
+ paused = false;
+ // Set delayed_play flag
+ delayed = true;
+ }
+ replay_rate = ZM_RATE_BASE;
+ break;
+ }
+ case CMD_VARPLAY :
+ {
+ Debug( 1, "Got VARPLAY command" );
+ if ( paused ) {
+ // Clear paused flag
+ paused = false;
+ // Set delayed_play flag
+ delayed = true;
+ }
+ replay_rate = ntohs(((unsigned char)msg->msg_data[2]<<8)|(unsigned char)msg->msg_data[1])-32768;
+ break;
+ }
+ case CMD_STOP :
+ {
+ Debug( 1, "Got STOP command" );
+
+ // Clear paused flag
+ paused = false;
+ // Clear delayed_play flag
+ delayed = false;
+ break;
+ }
+ case CMD_FASTFWD :
+ {
+ Debug( 1, "Got FAST FWD command" );
+ if ( paused ) {
+ // Clear paused flag
+ paused = false;
+ // Set delayed_play flag
+ delayed = true;
+ }
+ // Set play rate
+ switch ( replay_rate )
+ {
+ case 2 * ZM_RATE_BASE :
+ replay_rate = 5 * ZM_RATE_BASE;
+ break;
+ case 5 * ZM_RATE_BASE :
+ replay_rate = 10 * ZM_RATE_BASE;
+ break;
+ case 10 * ZM_RATE_BASE :
+ replay_rate = 25 * ZM_RATE_BASE;
+ break;
+ case 25 * ZM_RATE_BASE :
+ case 50 * ZM_RATE_BASE :
+ replay_rate = 50 * ZM_RATE_BASE;
+ break;
+ default :
+ replay_rate = 2 * ZM_RATE_BASE;
+ break;
+ }
+ break;
+ }
+ case CMD_SLOWFWD :
+ {
+ Debug( 1, "Got SLOW FWD command" );
+ // Set paused flag
+ paused = true;
+ // Set delayed flag
+ delayed = true;
+ // Set play rate
+ replay_rate = ZM_RATE_BASE;
+ // Set step
+ step = 1;
+ break;
+ }
+ case CMD_SLOWREV :
+ {
+ Debug( 1, "Got SLOW REV command" );
+ // Set paused flag
+ paused = true;
+ // Set delayed flag
+ delayed = true;
+ // Set play rate
+ replay_rate = ZM_RATE_BASE;
+ // Set step
+ step = -1;
+ break;
+ }
+ case CMD_FASTREV :
+ {
+ Debug( 1, "Got FAST REV command" );
+ if ( paused ) {
+ // Clear paused flag
+ paused = false;
+ // Set delayed_play flag
+ delayed = true;
+ }
+ // Set play rate
+ switch ( replay_rate ) {
+ case -2 * ZM_RATE_BASE :
+ replay_rate = -5 * ZM_RATE_BASE;
+ break;
+ case -5 * ZM_RATE_BASE :
+ replay_rate = -10 * ZM_RATE_BASE;
+ break;
+ case -10 * ZM_RATE_BASE :
+ replay_rate = -25 * ZM_RATE_BASE;
+ break;
+ case -25 * ZM_RATE_BASE :
+ case -50 * ZM_RATE_BASE :
+ replay_rate = -50 * ZM_RATE_BASE;
+ break;
+ default :
+ replay_rate = -2 * ZM_RATE_BASE;
+ break;
+ }
+ break;
+ }
+ case CMD_ZOOMIN :
+ {
+ x = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
+ y = ((unsigned char)msg->msg_data[3]<<8)|(unsigned char)msg->msg_data[4];
+ Debug( 1, "Got ZOOM IN command, to %d,%d", x, y );
+ switch ( zoom ) {
+ case 100:
+ zoom = 150;
+ break;
+ case 150:
+ zoom = 200;
+ break;
+ case 200:
+ zoom = 300;
+ break;
+ case 300:
+ zoom = 400;
+ break;
+ case 400:
+ default :
+ zoom = 500;
+ break;
+ }
+ break;
+ }
+ case CMD_ZOOMOUT :
+ {
+ Debug( 1, "Got ZOOM OUT command" );
+ switch ( zoom ) {
+ case 500:
+ zoom = 400;
+ break;
+ case 400:
+ zoom = 300;
+ break;
+ case 300:
+ zoom = 200;
+ break;
+ case 200:
+ zoom = 150;
+ break;
+ case 150:
+ default :
+ zoom = 100;
+ break;
+ }
+ break;
+ }
+ case CMD_PAN :
+ {
+ x = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
+ y = ((unsigned char)msg->msg_data[3]<<8)|(unsigned char)msg->msg_data[4];
+ Debug( 1, "Got PAN command, to %d,%d", x, y );
+ break;
+ }
+ case CMD_SCALE :
+ {
+ scale = ((unsigned char)msg->msg_data[1]<<8)|(unsigned char)msg->msg_data[2];
+ Debug( 1, "Got SCALE command, to %d", scale );
+ break;
+ }
+ case CMD_QUIT :
+ {
+ Info ("User initiated exit - CMD_QUIT");
+ break;
+ }
+ case CMD_QUERY :
+ {
+ Debug( 1, "Got QUERY command, sending STATUS" );
+ break;
+ }
+ default :
+ {
+ Error( "Got unexpected command %d", msg->msg_data[0] );
+ break;
+ }
+ }
+
+ struct {
+ int id;
+ int state;
+ double fps;
+ int buffer_level;
+ int rate;
+ double delay;
+ int zoom;
+ bool delayed;
+ bool paused;
+ bool enabled;
+ bool forced;
+ } status_data;
+
+ status_data.id = monitor->Id();
+ status_data.fps = monitor->GetFPS();
+ status_data.state = monitor->shared_data->state;
+ if ( playback_buffer > 0 )
+ status_data.buffer_level = (MOD_ADD( (temp_write_index-temp_read_index), 0, temp_image_buffer_count )*100)/temp_image_buffer_count;
+ else
+ status_data.buffer_level = 0;
+ status_data.delayed = delayed;
+ status_data.paused = paused;
+ status_data.rate = replay_rate;
+ status_data.delay = TV_2_FLOAT( now ) - TV_2_FLOAT( last_frame_timestamp );
+ status_data.zoom = zoom;
+ //status_data.enabled = monitor->shared_data->active;
+ status_data.enabled = monitor->trigger_data->trigger_state!=Monitor::TRIGGER_OFF;
+ status_data.forced = monitor->trigger_data->trigger_state==Monitor::TRIGGER_ON;
+ Debug( 2, "L:%d, D:%d, P:%d, R:%d, d:%.3f, Z:%d, E:%d F:%d",
+ status_data.buffer_level,
+ status_data.delayed,
+ status_data.paused,
+ status_data.rate,
+ status_data.delay,
+ status_data.zoom,
+ status_data.enabled,
+ status_data.forced
+ );
+
+ DataMsg status_msg;
+ status_msg.msg_type = MSG_DATA_WATCH;
+ memcpy( &status_msg.msg_data, &status_data, sizeof(status_data) );
+ int nbytes = 0;
+ if ( (nbytes = sendto( sd, &status_msg, sizeof(status_msg), MSG_DONTWAIT, (sockaddr *)&rem_addr, sizeof(rem_addr) )) < 0 ) {
+ //if ( errno != EAGAIN )
+ {
+ Error( "Can't sendto on sd %d: %s", sd, strerror(errno) );
+ //exit( -1 );
+ }
+ }
+
+ // quit after sending a status, if this was a quit request
+ if ((MsgCommand)msg->msg_data[0]==CMD_QUIT)
+ exit(0);
+
+ updateFrameRate( monitor->GetFPS() );
+} // end void MonitorStream::processCommand( const CmdMsg *msg )
+
+bool MonitorStream::sendFrame( const char *filepath, struct timeval *timestamp ) {
+ bool send_raw = ((scale>=ZM_SCALE_BASE)&&(zoom==ZM_SCALE_BASE));
+
+ if ( type != STREAM_JPEG )
+ send_raw = false;
+ if ( !config.timestamp_on_capture && timestamp )
+ send_raw = false;
+
+ if ( !send_raw ) {
+ Image temp_image( filepath );
+
+ return( sendFrame( &temp_image, timestamp ) );
+ } else {
+ int img_buffer_size = 0;
+ static unsigned char img_buffer[ZM_MAX_IMAGE_SIZE];
+
+ FILE *fdj = NULL;
+ if ( (fdj = fopen( filepath, "r" )) ) {
+ img_buffer_size = fread( img_buffer, 1, sizeof(img_buffer), fdj );
+ fclose( fdj );
+ } else {
+ Error( "Can't open %s: %s", filepath, strerror(errno) );
+ return( false );
+ }
+
+ // Calculate how long it takes to actually send the frame
+ struct timeval frameStartTime;
+ gettimeofday( &frameStartTime, NULL );
+
+ fprintf( stdout, "--ZoneMinderFrame\r\n" );
+ fprintf( stdout, "Content-Length: %d\r\n", img_buffer_size );
+ fprintf( stdout, "Content-Type: image/jpeg\r\n\r\n" );
+ if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) != 1 ) {
+ if ( ! zm_terminate )
+ Error( "Unable to send stream frame: %s", strerror(errno) );
+ return( false );
+ }
+ fprintf( stdout, "\r\n\r\n" );
+ fflush( stdout );
+
+ struct timeval frameEndTime;
+ gettimeofday( &frameEndTime, NULL );
+
+ int frameSendTime = tvDiffMsec( frameStartTime, frameEndTime );
+ if ( frameSendTime > 1000/maxfps ) {
+ maxfps /= 2;
+ Error( "Frame send time %d msec too slow, throttling maxfps to %.2f", frameSendTime, maxfps );
+ }
+
+ last_frame_sent = TV_2_FLOAT( now );
+
+ return( true );
+ }
+ return( false );
+}
+
+bool MonitorStream::sendFrame( Image *image, struct timeval *timestamp ) {
+ Image *send_image = prepareImage( image );
+ if ( !config.timestamp_on_capture && timestamp )
+ monitor->TimestampImage( send_image, timestamp );
+
+#if HAVE_LIBAVCODEC
+ if ( type == STREAM_MPEG ) {
+ if ( !vid_stream ) {
+ vid_stream = new VideoStream( "pipe:", format, bitrate, effective_fps, send_image->Colours(), send_image->SubpixelOrder(), send_image->Width(), send_image->Height() );
+ fprintf( stdout, "Content-type: %s\r\n\r\n", vid_stream->MimeType() );
+ vid_stream->OpenStream();
+ }
+ static struct timeval base_time;
+ struct DeltaTimeval delta_time;
+ if ( !frame_count )
+ base_time = *timestamp;
+ DELTA_TIMEVAL( delta_time, *timestamp, base_time, DT_PREC_3 );
+ /* double pts = */ vid_stream->EncodeFrame( send_image->Buffer(), send_image->Size(), config.mpeg_timed_frames, delta_time.delta );
+ } else
+#endif // HAVE_LIBAVCODEC
+ {
+ static unsigned char temp_img_buffer[ZM_MAX_IMAGE_SIZE];
+
+ int img_buffer_size = 0;
+ unsigned char *img_buffer = temp_img_buffer;
+
+ // Calculate how long it takes to actually send the frame
+ struct timeval frameStartTime;
+ gettimeofday( &frameStartTime, NULL );
+
+ fprintf( stdout, "--ZoneMinderFrame\r\n" );
+ switch( type ) {
+ case STREAM_JPEG :
+ send_image->EncodeJpeg( img_buffer, &img_buffer_size );
+ fprintf( stdout, "Content-Type: image/jpeg\r\n" );
+ break;
+ case STREAM_RAW :
+ fprintf( stdout, "Content-Type: image/x-rgb\r\n" );
+ img_buffer = (uint8_t*)send_image->Buffer();
+ img_buffer_size = send_image->Size();
+ break;
+ case STREAM_ZIP :
+ fprintf( stdout, "Content-Type: image/x-rgbz\r\n" );
+ unsigned long zip_buffer_size;
+ send_image->Zip( img_buffer, &zip_buffer_size );
+ img_buffer_size = zip_buffer_size;
+ break;
+ default :
+ Fatal( "Unexpected frame type %d", type );
+ break;
+ }
+ fprintf( stdout, "Content-Length: %d\r\n\r\n", img_buffer_size );
+ if ( fwrite( img_buffer, img_buffer_size, 1, stdout ) != 1 ) {
+ if ( !zm_terminate )
+ Error( "Unable to send stream frame: %s", strerror(errno) );
+ return( false );
+ }
+ fprintf( stdout, "\r\n\r\n" );
+ fflush( stdout );
+
+ struct timeval frameEndTime;
+ gettimeofday( &frameEndTime, NULL );
+
+ int frameSendTime = tvDiffMsec( frameStartTime, frameEndTime );
+ if ( frameSendTime > 1000/maxfps ) {
+ maxfps /= 1.5;
+ Error( "Frame send time %d msec too slow, throttling maxfps to %.2f", frameSendTime, maxfps );
+ }
+ }
+ last_frame_sent = TV_2_FLOAT( now );
+ return( true );
+} // end bool MonitorStream::sendFrame( Image *image, struct timeval *timestamp )
+
+void MonitorStream::runStream() {
+ if ( type == STREAM_SINGLE ) {
+ // Not yet migrated over to stream class
+ SingleImage( scale );
+ return;
+ }
+
+ openComms();
+
+ checkInitialised();
+
+ updateFrameRate( monitor->GetFPS() );
+
+ 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;
+
+ time_t stream_start_time;
+ time( &stream_start_time );
+
+ frame_count = 0;
+
+ temp_image_buffer = 0;
+ temp_image_buffer_count = playback_buffer;
+ temp_read_index = temp_image_buffer_count;
+ temp_write_index = temp_image_buffer_count;
+
+ char *swap_path = 0;
+ bool buffered_playback = false;
+
+ // 15 is the max length for the swap path suffix, /zmswap-whatever, assuming max 6 digits for monitor id
+ const int max_swap_len_suffix = 15;
+
+ int swap_path_length = strlen(config.path_swap) + 1; // +1 for NULL terminator
+ int subfolder1_length = snprintf(NULL, 0, "/zmswap-m%d", monitor->Id() ) + 1;
+ int subfolder2_length = snprintf(NULL, 0, "/zmswap-q%06d", connkey ) + 1;
+ int total_swap_path_length = swap_path_length + subfolder1_length + subfolder2_length;
+
+ if ( connkey && playback_buffer > 0 ) {
+
+ if ( total_swap_path_length + max_swap_len_suffix > PATH_MAX ) {
+ Error( "Swap Path is too long. %d > %d ", total_swap_path_length+max_swap_len_suffix, PATH_MAX );
+ } else {
+ swap_path = (char *)malloc( total_swap_path_length+max_swap_len_suffix );
+ strncpy( swap_path, config.path_swap, swap_path_length );
+
+ Debug( 3, "Checking swap path folder: %s", swap_path );
+ if ( checkSwapPath( swap_path, false ) ) {
+ // Append the subfolder name /zmswap-m{monitor-id} to the end of swap_path
+ int ndx = swap_path_length - 1; // Array index of the NULL terminator
+ snprintf( &(swap_path[ndx]), subfolder1_length, "/zmswap-m%d", monitor->Id() );
+
+ Debug( 4, "Checking swap path subfolder: %s", swap_path );
+ if ( checkSwapPath( swap_path, true ) ) {
+ // Append the subfolder name /zmswap-q{connection key} to the end of swap_path
+ ndx = swap_path_length+subfolder1_length - 2; // Array index of the NULL terminator
+ snprintf( &(swap_path[ndx]), subfolder2_length, "/zmswap-q%06d", connkey );
+
+ Debug( 4, "Checking swap path subfolder: %s", swap_path );
+ if ( checkSwapPath( swap_path, true ) ) {
+ buffered_playback = true;
+ }
+ }
+ }
+
+ if ( !buffered_playback ) {
+ Error( "Unable to validate swap image path, disabling buffered playback" );
+ } else {
+ Debug( 2, "Assigning temporary buffer" );
+ temp_image_buffer = new SwapImage[temp_image_buffer_count];
+ memset( temp_image_buffer, 0, sizeof(*temp_image_buffer)*temp_image_buffer_count );
+ Debug( 2, "Assigned temporary buffer" );
+ }
+ }
+ }
+
+ float max_secs_since_last_sent_frame = 10.0; //should be > keep alive amount (5 secs)
+ while ( !zm_terminate ) {
+ bool got_command = false;
+ if ( feof( stdout ) || ferror( stdout ) || !monitor->ShmValid() ) {
+ break;
+ }
+
+ gettimeofday( &now, NULL );
+
+ if ( connkey ) {
+ while(checkCommandQueue()) {
+ got_command = true;
+ }
+ }
+
+ //bool frame_sent = false;
+ if ( buffered_playback && delayed ) {
+ if ( temp_read_index == temp_write_index ) {
+ // Go back to live viewing
+ Debug( 1, "Exceeded temporary streaming buffer" );
+ // Clear paused flag
+ paused = false;
+ // Clear delayed_play flag
+ delayed = false;
+ replay_rate = ZM_RATE_BASE;
+ } else {
+ if ( !paused ) {
+ int temp_index = MOD_ADD( temp_read_index, 0, temp_image_buffer_count );
+ //Debug( 3, "tri: %d, ti: %d", temp_read_index, temp_index );
+ SwapImage *swap_image = &temp_image_buffer[temp_index];
+
+ if ( !swap_image->valid ) {
+ paused = true;
+ delayed = true;
+ temp_read_index = MOD_ADD( temp_read_index, (replay_rate>=0?-1:1), temp_image_buffer_count );
+ } else {
+ //Debug( 3, "siT: %f, lfT: %f", TV_2_FLOAT( swap_image->timestamp ), TV_2_FLOAT( last_frame_timestamp ) );
+ double expected_delta_time = ((TV_2_FLOAT( swap_image->timestamp ) - TV_2_FLOAT( last_frame_timestamp )) * ZM_RATE_BASE)/replay_rate;
+ double actual_delta_time = TV_2_FLOAT( now ) - last_frame_sent;
+
+ //Debug( 3, "eDT: %.3lf, aDT: %.3f, lFS:%.3f, NOW:%.3f", expected_delta_time, actual_delta_time, last_frame_sent, TV_2_FLOAT( now ) );
+ // If the next frame is due
+ if ( actual_delta_time > expected_delta_time ) {
+ //Debug( 2, "eDT: %.3lf, aDT: %.3f", expected_delta_time, actual_delta_time );
+ if ( temp_index%frame_mod == 0 ) {
+ Debug( 2, "Sending delayed frame %d", temp_index );
+ // Send the next frame
+ if ( ! sendFrame( temp_image_buffer[temp_index].file_name, &temp_image_buffer[temp_index].timestamp ) )
+ zm_terminate = true;
+ memcpy( &last_frame_timestamp, &(swap_image->timestamp), sizeof(last_frame_timestamp) );
+ //frame_sent = true;
+ }
+ temp_read_index = MOD_ADD( temp_read_index, (replay_rate>0?1:-1), temp_image_buffer_count );
+ }
+ }
+ } else if ( step != 0 ) {
+ temp_read_index = MOD_ADD( temp_read_index, (step>0?1:-1), temp_image_buffer_count );
+
+ SwapImage *swap_image = &temp_image_buffer[temp_read_index];
+
+ // Send the next frame
+ if ( !sendFrame( temp_image_buffer[temp_read_index].file_name, &temp_image_buffer[temp_read_index].timestamp ) )
+ zm_terminate = true;
+ memcpy( &last_frame_timestamp, &(swap_image->timestamp), sizeof(last_frame_timestamp) );
+ //frame_sent = true;
+ step = 0;
+ } else {
+ int temp_index = MOD_ADD( temp_read_index, 0, temp_image_buffer_count );
+
+ double actual_delta_time = TV_2_FLOAT( now ) - last_frame_sent;
+ if ( got_command || actual_delta_time > 5 ) {
+ // Send keepalive
+ Debug( 2, "Sending keepalive frame %d", temp_index );
+ // Send the next frame
+ if ( !sendFrame( temp_image_buffer[temp_index].file_name, &temp_image_buffer[temp_index].timestamp ) )
+ zm_terminate = true;
+ //frame_sent = true;
+ }
+ }
+ }
+ if ( temp_read_index == temp_write_index ) {
+ // Go back to live viewing
+ Warning( "Rewound over write index, resuming live play" );
+ // Clear paused flag
+ paused = false;
+ // Clear delayed_play flag
+ 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;
+ last_read_index = monitor->shared_data->last_write_index;
+ //Debug( 1, "%d: %x - %x", index, image_buffer[index].image, image_buffer[index].image->buffer );
+ if ( (frame_mod == 1) || ((frame_count%frame_mod) == 0) ) {
+ if ( !paused && !delayed ) {
+ // Send the next frame
+ Monitor::Snapshot *snap = &monitor->image_buffer[index];
+
+ if ( !sendFrame( snap->image, snap->timestamp ) )
+ zm_terminate = true;
+ memcpy( &last_frame_timestamp, snap->timestamp, sizeof(last_frame_timestamp) );
+ //frame_sent = true;
+
+ temp_read_index = temp_write_index;
+ }
+ }
+ if ( buffered_playback ) {
+ if ( monitor->shared_data->valid ) {
+ if ( monitor->image_buffer[index].timestamp->tv_sec ) {
+ int temp_index = temp_write_index%temp_image_buffer_count;
+ Debug( 2, "Storing frame %d", temp_index );
+ if ( !temp_image_buffer[temp_index].valid ) {
+ snprintf( temp_image_buffer[temp_index].file_name, sizeof(temp_image_buffer[0].file_name), "%s/zmswap-i%05d.jpg", swap_path, temp_index );
+ temp_image_buffer[temp_index].valid = true;
+ }
+ memcpy( &(temp_image_buffer[temp_index].timestamp), monitor->image_buffer[index].timestamp, sizeof(temp_image_buffer[0].timestamp) );
+ monitor->image_buffer[index].image->WriteJpeg( temp_image_buffer[temp_index].file_name, config.jpeg_file_quality );
+ temp_write_index = MOD_ADD( temp_write_index, 1, temp_image_buffer_count );
+ if ( temp_write_index == temp_read_index ) {
+ // Go back to live viewing
+ Warning( "Exceeded temporary buffer, resuming live play" );
+ // Clear paused flag
+ paused = false;
+ // Clear delayed_play flag
+ delayed = false;
+ replay_rate = ZM_RATE_BASE;
+ }
+ } else {
+ Warning( "Unable to store frame as timestamp invalid" );
+ }
+ } else {
+ Warning( "Unable to store frame as shared memory invalid" );
+ }
+ }
+ frame_count++;
+ }
+ usleep( (unsigned long)((1000000 * ZM_RATE_BASE)/((base_fps?base_fps:1)*abs(replay_rate*2))) );
+ if ( ttl ) {
+ if ( (now.tv_sec - stream_start_time) > ttl ) {
+ break;
+ }
+ }
+ 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;
+ }
+ }
+ if ( buffered_playback ) {
+ Debug( 1, "Cleaning swap files from %s", swap_path );
+ struct stat stat_buf;
+ if ( stat( swap_path, &stat_buf ) < 0 ) {
+ if ( errno != ENOENT ) {
+ Error( "Can't stat '%s': %s", swap_path, strerror(errno) );
+ }
+ } else if ( !S_ISDIR(stat_buf.st_mode) ) {
+ Error( "Swap image path '%s' is not a directory", swap_path );
+ } else {
+ char glob_pattern[PATH_MAX] = "";
+
+ snprintf( glob_pattern, sizeof(glob_pattern), "%s/*.*", swap_path );
+ glob_t pglob;
+ int glob_status = glob( glob_pattern, 0, 0, &pglob );
+ if ( glob_status != 0 ) {
+ if ( glob_status < 0 ) {
+ Error( "Can't glob '%s': %s", glob_pattern, strerror(errno) );
+ } else {
+ Debug( 1, "Can't glob '%s': %d", glob_pattern, glob_status );
+ }
+ } else {
+ for ( unsigned int i = 0; i < pglob.gl_pathc; i++ ) {
+ if ( unlink( pglob.gl_pathv[i] ) < 0 ) {
+ Error( "Can't unlink '%s': %s", pglob.gl_pathv[i], strerror(errno) );
+ }
+ }
+ }
+ globfree( &pglob );
+ if ( rmdir( swap_path ) < 0 ) {
+ Error( "Can't rmdir '%s': %s", swap_path, strerror(errno) );
+ }
+ }
+ }
+ if ( swap_path ) free( swap_path );
+ closeComms();
+}
+
+void MonitorStream::SingleImage( int scale ) {
+ int img_buffer_size = 0;
+ static JOCTET img_buffer[ZM_MAX_IMAGE_SIZE];
+ Image scaled_image;
+ Monitor::Snapshot *snap = monitor->getSnapshot();
+ Image *snap_image = snap->image;
+
+ if ( scale != ZM_SCALE_BASE ) {
+ scaled_image.Assign( *snap_image );
+ scaled_image.Scale( scale );
+ snap_image = &scaled_image;
+ }
+ if ( !config.timestamp_on_capture ) {
+ monitor->TimestampImage( snap_image, snap->timestamp );
+ }
+ snap_image->EncodeJpeg( img_buffer, &img_buffer_size );
+
+ fprintf( stdout, "Content-Length: %d\r\n", img_buffer_size );
+ fprintf( stdout, "Content-Type: image/jpeg\r\n\r\n" );
+ fwrite( img_buffer, img_buffer_size, 1, stdout );
+}
+
+void MonitorStream::SingleImageRaw( int scale ) {
+ Image scaled_image;
+ Monitor::Snapshot *snap = monitor->getSnapshot();
+ Image *snap_image = snap->image;
+
+ if ( scale != ZM_SCALE_BASE ) {
+ scaled_image.Assign( *snap_image );
+ scaled_image.Scale( scale );
+ snap_image = &scaled_image;
+ }
+ if ( !config.timestamp_on_capture ) {
+ monitor->TimestampImage( snap_image, snap->timestamp );
+ }
+
+ fprintf( stdout, "Content-Length: %d\r\n", snap_image->Size() );
+ fprintf( stdout, "Content-Type: image/x-rgb\r\n\r\n" );
+ fwrite( snap_image->Buffer(), snap_image->Size(), 1, stdout );
+}
+
+void MonitorStream::SingleImageZip( int scale ) {
+ unsigned long img_buffer_size = 0;
+ static Bytef img_buffer[ZM_MAX_IMAGE_SIZE];
+ Image scaled_image;
+
+ Monitor::Snapshot *snap = monitor->getSnapshot();
+ Image *snap_image = snap->image;
+
+ if ( scale != ZM_SCALE_BASE ) {
+ scaled_image.Assign( *snap_image );
+ scaled_image.Scale( scale );
+ snap_image = &scaled_image;
+ }
+ if ( !config.timestamp_on_capture ) {
+ monitor->TimestampImage( snap_image, snap->timestamp );
+ }
+ snap_image->Zip( img_buffer, &img_buffer_size );
+
+ fprintf( stdout, "Content-Length: %ld\r\n", img_buffer_size );
+ fprintf( stdout, "Content-Type: image/x-rgbz\r\n\r\n" );
+ fwrite( img_buffer, img_buffer_size, 1, stdout );
+}
diff --git a/src/zm_monitorstream.h b/src/zm_monitorstream.h
new file mode 100644
index 000000000..fb8e22a13
--- /dev/null
+++ b/src/zm_monitorstream.h
@@ -0,0 +1,77 @@
+//
+// ZoneMinder MonitorStream Class Interfaces, $Date$, $Revision$
+// Copyright (C) 2001-2008 Philip Coombes
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+
+#ifndef ZM_MONITORSTREAM_H
+#define ZM_MONITORSTREAM_H
+
+#include "zm.h"
+#include "zm_coord.h"
+#include "zm_image.h"
+#include "zm_utils.h"
+#include "zm_monitor.h"
+
+class MonitorStream : public StreamBase {
+ protected:
+ typedef struct SwapImage {
+ bool valid;
+ struct timeval timestamp;
+ char file_name[PATH_MAX];
+ } SwapImage;
+
+ private:
+ SwapImage *temp_image_buffer;
+ int temp_image_buffer_count;
+ int temp_read_index;
+ int temp_write_index;
+
+ protected:
+ time_t ttl;
+
+ protected:
+ int playback_buffer;
+ bool delayed;
+
+ int frame_count;
+
+ protected:
+ bool checkSwapPath( const char *path, bool create_path );
+
+ bool sendFrame( const char *filepath, struct timeval *timestamp );
+ bool sendFrame( Image *image, struct timeval *timestamp );
+ void processCommand( const CmdMsg *msg );
+ void SingleImage( int scale=100 );
+ void SingleImageRaw( int scale=100 );
+ void SingleImageZip( int scale=100 );
+
+ public:
+ MonitorStream() : playback_buffer( 0 ), delayed( false ), frame_count( 0 ) {
+ }
+ void setStreamBuffer( int p_playback_buffer ) {
+ playback_buffer = p_playback_buffer;
+ }
+ void setStreamTTL( time_t p_ttl ) {
+ ttl = p_ttl;
+ }
+ bool setStreamStart( int monitor_id ) {
+ return loadMonitor( monitor_id );
+ }
+ void runStream();
+};
+
+#endif // ZM_MONITORSTREAM_H
diff --git a/src/zm_remote_camera_http.cpp b/src/zm_remote_camera_http.cpp
index 1b4cb52d3..95c8ae507 100644
--- a/src/zm_remote_camera_http.cpp
+++ b/src/zm_remote_camera_http.cpp
@@ -227,7 +227,7 @@ int RemoteCameraHttp::ReadData( Buffer &buffer, unsigned int bytes_expected ) {
}
if ( total_bytes_to_read == 0 ) {
- if( mode == SINGLE_IMAGE ) {
+ if ( mode == SINGLE_IMAGE ) {
int error = 0;
socklen_t len = sizeof (error);
int retval = getsockopt( sd, SOL_SOCKET, SO_ERROR, &error, &len );
@@ -267,7 +267,7 @@ int RemoteCameraHttp::ReadData( Buffer &buffer, unsigned int bytes_expected ) {
Debug( 2, "Socket closed" );
//Disconnect(); // Disconnect is done outside of ReadData now.
return( -1 );
- } else if ( bytes_read < total_bytes_to_read ) {
+ } else if ( (unsigned int)bytes_read < total_bytes_to_read ) {
Error( "Incomplete read, expected %d, got %d", total_bytes_to_read, bytes_read );
return( -1 );
}
diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp
index e777a9266..988a5c9ea 100644
--- a/src/zm_utils.cpp
+++ b/src/zm_utils.cpp
@@ -24,8 +24,16 @@
#include
#include
#include
+#if defined(__arm__)
+#include
+#endif
+
+#ifdef HAVE_CURL_CURL_H
+#include
+#endif
unsigned int sseversion = 0;
+unsigned int neonversion = 0;
std::string trimSet(std::string str, std::string trimset) {
// Trim Both leading and trailing sets
@@ -234,30 +242,59 @@ int pairsplit(const char* string, const char delim, std::string& name, std::stri
return 0;
}
-/* Sets sse_version */
-void ssedetect() {
+/* Detect special hardware features, such as SIMD instruction sets */
+void hwcaps_detect() {
+ neonversion = 0;
+ sseversion = 0;
#if (defined(__i386__) || defined(__x86_64__))
/* x86 or x86-64 processor */
- uint32_t r_edx, r_ecx;
-
+ uint32_t r_edx, r_ecx, r_ebx;
+
+#ifdef __x86_64__
__asm__ __volatile__(
-#if defined(__i386__)
- "pushl %%ebx;\n\t"
-#endif
+ "push %%rbx\n\t"
+ "mov $0x0,%%ecx\n\t"
+ "mov $0x7,%%eax\n\t"
+ "cpuid\n\t"
+ "push %%rbx\n\t"
"mov $0x1,%%eax\n\t"
"cpuid\n\t"
-#if defined(__i386__)
- "popl %%ebx;\n\t"
-#endif
- : "=d" (r_edx), "=c" (r_ecx)
+ "pop %%rax\n\t"
+ "pop %%rbx\n\t"
+ : "=d" (r_edx), "=c" (r_ecx), "=a" (r_ebx)
+ :
:
- : "%eax"
-#if !defined(__i386__)
- , "%ebx"
-#endif
);
-
- if (r_ecx & 0x00000200) {
+#else
+ __asm__ __volatile__(
+ "push %%ebx\n\t"
+ "mov $0x0,%%ecx\n\t"
+ "mov $0x7,%%eax\n\t"
+ "cpuid\n\t"
+ "push %%ebx\n\t"
+ "mov $0x1,%%eax\n\t"
+ "cpuid\n\t"
+ "pop %%eax\n\t"
+ "pop %%ebx\n\t"
+ : "=d" (r_edx), "=c" (r_ecx), "=a" (r_ebx)
+ :
+ :
+ );
+#endif
+
+ if (r_ebx & 0x00000020) {
+ sseversion = 52; /* AVX2 */
+ Debug(1,"Detected a x86\\x86-64 processor with AVX2");
+ } else if (r_ecx & 0x10000000) {
+ sseversion = 51; /* AVX */
+ Debug(1,"Detected a x86\\x86-64 processor with AVX");
+ } else if (r_ecx & 0x00100000) {
+ sseversion = 42; /* SSE4.2 */
+ Debug(1,"Detected a x86\\x86-64 processor with SSE4.2");
+ } else if (r_ecx & 0x00080000) {
+ sseversion = 41; /* SSE4.1 */
+ Debug(1,"Detected a x86\\x86-64 processor with SSE4.1");
+ } else if (r_ecx & 0x00000200) {
sseversion = 35; /* SSSE3 */
Debug(1,"Detected a x86\\x86-64 processor with SSSE3");
} else if (r_ecx & 0x00000001) {
@@ -272,12 +309,20 @@ void ssedetect() {
} else {
sseversion = 0;
Debug(1,"Detected a x86\\x86-64 processor");
+ }
+#elif defined(__arm__)
+ // ARM processor
+ // To see if it supports NEON, we need to get that information from the kernel
+ unsigned long auxval = getauxval(AT_HWCAP);
+ if (auxval & HWCAP_ARM_NEON) {
+ Debug(1,"Detected ARM processor with Neon");
+ neonversion = 1;
+ } else {
+ Debug(1,"Detected ARM processor");
}
-
#else
- /* Non x86 or x86-64 processor, SSE2 is not available */
- Debug(1,"Detected a non x86\\x86-64 processor");
- sseversion = 0;
+ // Unknown processor
+ Debug(1,"Detected unknown processor architecture");
#endif
}
@@ -345,3 +390,18 @@ void timespec_diff(struct timespec *start, struct timespec *end, struct timespec
}
}
+std::string UriDecode( const std::string &encoded ) {
+#ifdef HAVE_LIBCURL
+ CURL *curl = curl_easy_init();
+ int outlength;
+ char *cres = curl_easy_unescape(curl, encoded.c_str(), encoded.length(), &outlength);
+ std::string res(cres, cres + outlength);
+ curl_free(cres);
+ curl_easy_cleanup(curl);
+ return res;
+#else
+Warning("ZM Compiled without LIBCURL. UriDecoding not implemented.");
+ return encoded;
+#endif
+}
+
diff --git a/src/zm_utils.h b/src/zm_utils.h
index 6dbf76a4d..7235bb15f 100644
--- a/src/zm_utils.h
+++ b/src/zm_utils.h
@@ -54,10 +54,13 @@ inline int min( int a, int b )
return( a<=b?a:b );
}
-void ssedetect();
void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes);
void timespec_diff(struct timespec *start, struct timespec *end, struct timespec *diff);
+void hwcaps_detect();
extern unsigned int sseversion;
+extern unsigned int neonversion;
+
+std::string UriDecode( const std::string &encoded );
#endif // ZM_UTILS_H
diff --git a/src/zm_videostore.cpp b/src/zm_videostore.cpp
index e05d451b0..7112b78f5 100644
--- a/src/zm_videostore.cpp
+++ b/src/zm_videostore.cpp
@@ -1,4 +1,3 @@
-//
// ZoneMinder Video Storage Implementation
// Written by Chris Wiggins
// http://chriswiggins.co.nz
@@ -129,50 +128,6 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
video_output_context->time_base.den
);
-#if 0
- if ( video_input_context->sample_aspect_ratio.den && ( video_output_stream->sample_aspect_ratio.den != video_input_context->sample_aspect_ratio.den ) ) {
- Warning("Fixing sample_aspect_ratio.den from (%d) to (%d)", video_output_stream->sample_aspect_ratio.den, video_input_context->sample_aspect_ratio.den );
- video_output_stream->sample_aspect_ratio.den = video_input_context->sample_aspect_ratio.den;
- } else {
- Debug(3, "aspect ratio denominator is (%d)", video_output_stream->sample_aspect_ratio.den );
- }
- if ( video_input_context->sample_aspect_ratio.num && ( video_output_stream->sample_aspect_ratio.num != video_input_context->sample_aspect_ratio.num ) ) {
- Warning("Fixing sample_aspect_ratio.num from video_output_stream(%d) to video_input_stream(%d)", video_output_stream->sample_aspect_ratio.num, video_input_context->sample_aspect_ratio.num );
- video_output_stream->sample_aspect_ratio.num = video_input_context->sample_aspect_ratio.num;
- } else {
- Debug(3, "aspect ratio numerator is (%d)", video_output_stream->sample_aspect_ratio.num );
- }
- if ( video_output_context->codec_id != video_input_context->codec_id ) {
- Warning("Fixing video_output_context->codec_id");
- video_output_context->codec_id = video_input_context->codec_id;
- }
- if ( ! video_output_context->time_base.num ) {
- Warning("video_output_context->time_base.num is not set%d/%d. Fixing by setting it to 1", video_output_context->time_base.num, video_output_context->time_base.den);
- Warning("video_output_context->time_base.num is not set%d/%d. Fixing by setting it to 1", video_output_stream->time_base.num, video_output_stream->time_base.den);
- video_output_context->time_base.num = video_output_stream->time_base.num;
- video_output_context->time_base.den = video_output_stream->time_base.den;
- }
-
- if ( video_output_stream->sample_aspect_ratio.den != video_output_context->sample_aspect_ratio.den ) {
- Warning("Fixingample_aspect_ratio.den");
- video_output_stream->sample_aspect_ratio.den = video_output_context->sample_aspect_ratio.den;
- }
- if ( video_output_stream->sample_aspect_ratio.num != video_input_context->sample_aspect_ratio.num ) {
- Warning("Fixingample_aspect_ratio.num");
- video_output_stream->sample_aspect_ratio.num = video_input_context->sample_aspect_ratio.num;
- }
- if ( video_output_context->codec_id != video_input_context->codec_id ) {
- Warning("Fixing video_output_context->codec_id");
- video_output_context->codec_id = video_input_context->codec_id;
- }
- if ( ! video_output_context->time_base.num ) {
- Warning("video_output_context->time_base.num is not set%d/%d. Fixing by setting it to 1", video_output_context->time_base.num, video_output_context->time_base.den);
- Warning("video_output_context->time_base.num is not set%d/%d. Fixing by setting it to 1", video_output_stream->time_base.num, video_output_stream->time_base.den);
- video_output_context->time_base.num = video_output_stream->time_base.num;
- video_output_context->time_base.den = video_output_stream->time_base.den;
- }
-#endif
-
// WHY?
//video_output_context->codec_tag = 0;
if (!video_output_context->codec_tag) {
@@ -209,180 +164,19 @@ VideoStore::VideoStore(const char *filename_in, const char *format_in,
audio_output_codec = NULL;
audio_input_context = NULL;
+ audio_output_stream = NULL;
+ resample_context = NULL;
if (audio_input_stream) {
audio_input_context = audio_input_stream->codec;
if ( audio_input_context->codec_id != AV_CODEC_ID_AAC ) {
-#ifdef HAVE_LIBSWRESAMPLE
- resample_context = NULL;
- char error_buffer[256];
+ static char error_buffer[256];
avcodec_string(error_buffer, sizeof(error_buffer), audio_input_context, 0 );
Debug(3, "Got something other than AAC (%s)", error_buffer );
- audio_output_stream = NULL;
-
- audio_output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
- if ( audio_output_codec ) {
-Debug(2, "Have audio output codec");
- audio_output_stream = avformat_new_stream( oc, audio_output_codec );
-
- audio_output_context = audio_output_stream->codec;
-
- if ( audio_output_context ) {
-
-Debug(2, "Have audio_output_context");
- AVDictionary *opts = NULL;
- av_dict_set(&opts, "strict", "experimental", 0);
-
- /* put sample parameters */
- audio_output_context->bit_rate = audio_input_context->bit_rate;
- audio_output_context->sample_rate = audio_input_context->sample_rate;
- audio_output_context->channels = audio_input_context->channels;
- audio_output_context->channel_layout = audio_input_context->channel_layout;
- audio_output_context->sample_fmt = audio_input_context->sample_fmt;
- //audio_output_context->refcounted_frames = 1;
-
- if (audio_output_codec->supported_samplerates) {
- int found = 0;
- for ( unsigned int i = 0; audio_output_codec->supported_samplerates[i]; i++) {
- if ( audio_output_context->sample_rate == audio_output_codec->supported_samplerates[i] ) {
- found = 1;
- break;
- }
- }
- if ( found ) {
- Debug(3, "Sample rate is good");
- } else {
- audio_output_context->sample_rate = audio_output_codec->supported_samplerates[0];
- Debug(1, "Sampel rate is no good, setting to (%d)", audio_output_codec->supported_samplerates[0] );
- }
- }
-
- /* check that the encoder supports s16 pcm input */
- if (!check_sample_fmt( audio_output_codec, audio_output_context->sample_fmt)) {
- Debug( 3, "Encoder does not support sample format %s, setting to FLTP",
- av_get_sample_fmt_name( audio_output_context->sample_fmt));
- audio_output_context->sample_fmt = AV_SAMPLE_FMT_FLTP;
- }
-
- //audio_output_stream->time_base = audio_input_stream->time_base;
- audio_output_context->time_base = (AVRational){ 1, audio_output_context->sample_rate };
-
- Debug(3, "Audio Time bases input stream (%d/%d) input codec: (%d/%d) output_stream (%d/%d) output codec (%d/%d)",
- audio_input_stream->time_base.num,
- audio_input_stream->time_base.den,
- audio_input_context->time_base.num,
- audio_input_context->time_base.den,
- audio_output_stream->time_base.num,
- audio_output_stream->time_base.den,
- audio_output_context->time_base.num,
- audio_output_context->time_base.den
- );
-
- ret = avcodec_open2(audio_output_context, audio_output_codec, &opts );
- if ( ret < 0 ) {
- av_strerror(ret, error_buffer, sizeof(error_buffer));
- Fatal( "could not open codec (%d) (%s)\n", ret, error_buffer );
- } else {
-
- Debug(1, "Audio output bit_rate (%d) sample_rate(%d) channels(%d) fmt(%d) layout(%d) frame_size(%d), refcounted_frames(%d)",
- audio_output_context->bit_rate,
- audio_output_context->sample_rate,
- audio_output_context->channels,
- audio_output_context->sample_fmt,
- audio_output_context->channel_layout,
- audio_output_context->frame_size,
- audio_output_context->refcounted_frames
- );
-#if 1
- /** Create the FIFO buffer based on the specified output sample format. */
- if (!(fifo = av_audio_fifo_alloc(audio_output_context->sample_fmt,
- audio_output_context->channels, 1))) {
- Error("Could not allocate FIFO\n");
- return;
- }
-#endif
- output_frame_size = audio_output_context->frame_size;
- /** Create a new frame to store the audio samples. */
- if (!(input_frame = zm_av_frame_alloc())) {
- Error("Could not allocate input frame");
- return;
- }
-
- /** Create a new frame to store the audio samples. */
- if (!(output_frame = zm_av_frame_alloc())) {
- Error("Could not allocate output frame");
- av_frame_free(&input_frame);
- return;
- }
- /**
- * Create a resampler context for the conversion.
- * Set the conversion parameters.
- * Default channel layouts based on the number of channels
- * are assumed for simplicity (they are sometimes not detected
- * properly by the demuxer and/or decoder).
- */
- resample_context = swr_alloc_set_opts(NULL,
- av_get_default_channel_layout(audio_output_context->channels),
- audio_output_context->sample_fmt,
- audio_output_context->sample_rate,
- av_get_default_channel_layout( audio_input_context->channels),
- audio_input_context->sample_fmt,
- audio_input_context->sample_rate,
- 0, NULL);
- if (!resample_context) {
- Error( "Could not allocate resample context\n");
- return;
- }
- /**
- * Perform a sanity check so that the number of converted samples is
- * not greater than the number of samples to be converted.
- * If the sample rates differ, this case has to be handled differently
- */
- av_assert0(audio_output_context->sample_rate == audio_input_context->sample_rate);
- /** Open the resampler with the specified parameters. */
- if ((ret = swr_init(resample_context)) < 0) {
- Error( "Could not open resample context\n");
- swr_free(&resample_context);
- return;
- }
- /**
- * Allocate as many pointers as there are audio channels.
- * Each pointer will later point to the audio samples of the corresponding
- * channels (although it may be NULL for interleaved formats).
- */
- if (!( converted_input_samples = (uint8_t *)calloc( audio_output_context->channels, sizeof(*converted_input_samples))) ) {
- Error( "Could not allocate converted input sample pointers\n");
- return;
- }
- /**
- * Allocate memory for the samples of all channels in one consecutive
- * block for convenience.
- */
- if ((ret = av_samples_alloc( &converted_input_samples, NULL,
- audio_output_context->channels,
- audio_output_context->frame_size,
- audio_output_context->sample_fmt, 0)) < 0) {
- Error( "Could not allocate converted input samples (error '%s')\n",
- av_make_error_string(ret).c_str() );
-
- av_freep(converted_input_samples);
- free(converted_input_samples);
- return;
- }
- Debug(2, "Success opening AAC codec");
- }
- av_dict_free(&opts);
- } else {
- Error( "could not allocate codec context for AAC\n");
- }
- } else {
- Error( "could not find codec for AAC\n");
+ if ( ! setup_resampler() ) {
+ return;
}
-#else
- Error("Not built with libswresample library. Cannot do audio conversion to AAC");
- audio_output_stream = NULL;
-#endif
} else {
Debug(3, "Got AAC" );
@@ -390,32 +184,32 @@ Debug(2, "Have audio_output_context");
if ( ! audio_output_stream ) {
Error("Unable to create audio out stream\n");
audio_output_stream = NULL;
- }
- audio_output_context = audio_output_stream->codec;
-
- ret = avcodec_copy_context(audio_output_context, audio_input_context);
- if (ret < 0) {
- Fatal("Unable to copy audio context %s\n", av_make_error_string(ret).c_str());
- }
- audio_output_context->codec_tag = 0;
- if ( audio_output_context->channels > 1 ) {
- Warning("Audio isn't mono, changing it.");
- audio_output_context->channels = 1;
} else {
- Debug(3, "Audio is mono");
- }
+ audio_output_context = audio_output_stream->codec;
+
+ ret = avcodec_copy_context(audio_output_context, audio_input_context);
+ if (ret < 0) {
+ Error("Unable to copy audio context %s\n", av_make_error_string(ret).c_str());
+ audio_output_stream = NULL;
+ } else {
+ audio_output_context->codec_tag = 0;
+ if ( audio_output_context->channels > 1 ) {
+ Warning("Audio isn't mono, changing it.");
+ audio_output_context->channels = 1;
+ } else {
+ Debug(3, "Audio is mono");
+ }
+ }
+ } // end if audio_output_stream
} // end if is AAC
-if ( audio_output_stream ) {
- if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
- audio_output_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
- }
+ if ( audio_output_stream ) {
+ if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
+ audio_output_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
+ }
}
- } else {
- Debug(3, "No Audio output stream");
- audio_output_stream = NULL;
- }
+ } // end if audio_input_stream
/* open the output file, if needed */
if (!(output_format->flags & AVFMT_NOFILE)) {
@@ -444,27 +238,18 @@ if ( audio_output_stream ) {
av_make_error_string(ret).c_str());
}
- prevDts = 0;
- video_start_pts = 0;
- video_start_dts = 0;
- audio_start_pts = 0;
- audio_start_dts = 0;
+ video_last_pts = 0;
+ video_last_dts = 0;
+ audio_last_pts = 0;
+ audio_last_dts = 0;
+ previous_pts = 0;
+ previous_dts = 0;
- filter_in_rescale_delta_last = AV_NOPTS_VALUE;
-
- // now - when streaming started
- //startTime=av_gettime()-nStartTime;//oc->start_time;
- //Info("VideoStore startTime=%d\n",startTime);
-
- if ( init_filters() < 0 ) {
- Fatal("Could not init fileters");
- }
} // VideoStore::VideoStore
VideoStore::~VideoStore(){
if ( audio_output_codec ) {
-Debug(1, "Have audio encoder, need to flush it's output" );
// Do we need to flush the outputs? I have no idea.
AVPacket pkt;
int got_packet;
@@ -474,7 +259,7 @@ Debug(1, "Have audio encoder, need to flush it's output" );
int64_t size;
while(1) {
-#if LIBAVCODEC_VERSION_CHECK(57, 0, 0, 0, 0)
+#if LIBAVCODEC_VERSION_CHECK(58, 0, 0, 0, 0)
ret = avcodec_receive_packet( audio_output_context, &pkt );
#else
ret = avcodec_encode_audio2( audio_output_context, &pkt, NULL, &got_packet );
@@ -520,6 +305,10 @@ Debug(2, "writing flushed packet pts(%d) dts(%d) duration(%d)", pkt.pts, pkt.dts
}
if (audio_output_stream) {
avcodec_close(audio_output_context);
+ if ( resample_context ) {
+ avresample_close( resample_context );
+ avresample_free( &resample_context );
+ }
}
// WHen will be not using a file ?
@@ -534,10 +323,198 @@ Debug(2, "writing flushed packet pts(%d) dts(%d) duration(%d)", pkt.pts, pkt.dts
/* free the stream */
avformat_free_context(oc);
+}
-#ifdef HAVE_LIBSWRESAMPLE
- if ( resample_context )
- swr_free( &resample_context );
+bool VideoStore::setup_resampler() {
+#ifdef HAVE_LIBAVRESAMPLE
+ static char error_buffer[256];
+
+ audio_output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
+ if ( ! audio_output_codec ) {
+ Error("Could not find codec for AAC");
+ return false;
+ }
+ Debug(2, "Have audio output codec");
+
+ audio_output_stream = avformat_new_stream( oc, audio_output_codec );
+ audio_output_context = audio_output_stream->codec;
+
+ if ( ! audio_output_context ) {
+ Error( "could not allocate codec context for AAC\n");
+ audio_output_stream = NULL;
+ return false;
+ }
+
+ Debug(2, "Have audio_output_context");
+
+ AVDictionary *opts = NULL;
+ av_dict_set(&opts, "strict", "experimental", 0);
+
+ /* put sample parameters */
+ audio_output_context->bit_rate = audio_input_context->bit_rate;
+ audio_output_context->sample_rate = audio_input_context->sample_rate;
+ audio_output_context->channels = audio_input_context->channels;
+ audio_output_context->channel_layout = audio_input_context->channel_layout;
+ audio_output_context->sample_fmt = audio_input_context->sample_fmt;
+ //audio_output_context->refcounted_frames = 1;
+
+ if (audio_output_codec->supported_samplerates) {
+ int found = 0;
+ for ( unsigned int i = 0; audio_output_codec->supported_samplerates[i]; i++) {
+ if ( audio_output_context->sample_rate == audio_output_codec->supported_samplerates[i] ) {
+ found = 1;
+ break;
+ }
+ }
+ if ( found ) {
+ Debug(3, "Sample rate is good");
+ } else {
+ audio_output_context->sample_rate = audio_output_codec->supported_samplerates[0];
+ Debug(1, "Sampel rate is no good, setting to (%d)", audio_output_codec->supported_samplerates[0] );
+ }
+ }
+
+ /* check that the encoder supports s16 pcm input */
+ if (!check_sample_fmt( audio_output_codec, audio_output_context->sample_fmt)) {
+ Debug( 3, "Encoder does not support sample format %s, setting to FLTP",
+ av_get_sample_fmt_name( audio_output_context->sample_fmt));
+ audio_output_context->sample_fmt = AV_SAMPLE_FMT_FLTP;
+ }
+
+ //audio_output_stream->time_base = audio_input_stream->time_base;
+ audio_output_context->time_base = (AVRational){ 1, audio_output_context->sample_rate };
+
+ Debug(3, "Audio Time bases input stream (%d/%d) input codec: (%d/%d) output_stream (%d/%d) output codec (%d/%d)",
+ audio_input_stream->time_base.num,
+ audio_input_stream->time_base.den,
+ audio_input_context->time_base.num,
+ audio_input_context->time_base.den,
+ audio_output_stream->time_base.num,
+ audio_output_stream->time_base.den,
+ audio_output_context->time_base.num,
+ audio_output_context->time_base.den
+ );
+
+ ret = avcodec_open2(audio_output_context, audio_output_codec, &opts );
+ av_dict_free(&opts);
+ if ( ret < 0 ) {
+ av_strerror(ret, error_buffer, sizeof(error_buffer));
+ Fatal( "could not open codec (%d) (%s)\n", ret, error_buffer );
+ audio_output_codec = NULL;
+ audio_output_context = NULL;
+ audio_output_stream = NULL;
+ return false;
+ }
+
+ Debug(1, "Audio output bit_rate (%d) sample_rate(%d) channels(%d) fmt(%d) layout(%d) frame_size(%d), refcounted_frames(%d)",
+ audio_output_context->bit_rate,
+ audio_output_context->sample_rate,
+ audio_output_context->channels,
+ audio_output_context->sample_fmt,
+ audio_output_context->channel_layout,
+ audio_output_context->frame_size,
+ audio_output_context->refcounted_frames
+ );
+
+ output_frame_size = audio_output_context->frame_size;
+ /** Create a new frame to store the audio samples. */
+ if (!(input_frame = zm_av_frame_alloc())) {
+ Error("Could not allocate input frame");
+ return false;
+ }
+
+ /** Create a new frame to store the audio samples. */
+ if (!(output_frame = zm_av_frame_alloc())) {
+ Error("Could not allocate output frame");
+ av_frame_free(&input_frame);
+ return false;
+ }
+
+ // Setup the audio resampler
+ resample_context = avresample_alloc_context();
+ if ( ! resample_context ) {
+ Error( "Could not allocate resample context\n");
+ return false;
+ }
+
+ // Some formats (i.e. WAV) do not produce the proper channel layout
+ if ( audio_input_context->channel_layout == 0 ) {
+ Error( "Bad channel layout. Need to set it to mono.\n");
+ av_opt_set_int( resample_context, "in_channel_layout", av_get_channel_layout( "mono" ), 0 );
+ } else {
+ av_opt_set_int( resample_context, "in_channel_layout", audio_input_context->channel_layout, 0 );
+ }
+
+ av_opt_set_int( resample_context, "in_sample_fmt", audio_input_context->sample_fmt, 0);
+ av_opt_set_int( resample_context, "in_sample_rate", audio_input_context->sample_rate, 0);
+ av_opt_set_int( resample_context, "in_channels", audio_input_context->channels,0);
+ //av_opt_set_int( resample_context, "out_channel_layout", audio_output_context->channel_layout, 0);
+ av_opt_set_int( resample_context, "out_channel_layout", av_get_channel_layout( "mono" ), 0 );
+ av_opt_set_int( resample_context, "out_sample_fmt", audio_output_context->sample_fmt, 0);
+ av_opt_set_int( resample_context, "out_sample_rate", audio_output_context->sample_rate, 0);
+ av_opt_set_int( resample_context, "out_channels", audio_output_context->channels, 0);
+
+ ret = avresample_open( resample_context );
+ if ( ret < 0 ) {
+ Error( "Could not open resample context\n");
+ return false;
+ }
+
+#if 0
+ /**
+ * Allocate as many pointers as there are audio channels.
+ * Each pointer will later point to the audio samples of the corresponding
+ * channels (although it may be NULL for interleaved formats).
+ */
+ if (!( converted_input_samples = (uint8_t *)calloc( audio_output_context->channels, sizeof(*converted_input_samples))) ) {
+ Error( "Could not allocate converted input sample pointers\n");
+ return;
+ }
+ /**
+ * Allocate memory for the samples of all channels in one consecutive
+ * block for convenience.
+ */
+ if ((ret = av_samples_alloc( &converted_input_samples, NULL,
+ audio_output_context->channels,
+ audio_output_context->frame_size,
+ audio_output_context->sample_fmt, 0)) < 0) {
+ Error( "Could not allocate converted input samples (error '%s')\n",
+ av_make_error_string(ret).c_str() );
+
+ av_freep(converted_input_samples);
+ free(converted_input_samples);
+ return;
+ }
+#endif
+
+ output_frame->nb_samples = audio_output_context->frame_size;
+ output_frame->format = audio_output_context->sample_fmt;
+ output_frame->channel_layout = audio_output_context->channel_layout;
+
+ // The codec gives us the frame size, in samples, we calculate the size of the samples buffer in bytes
+ unsigned int audioSampleBuffer_size = av_samples_get_buffer_size( NULL, audio_output_context->channels, audio_output_context->frame_size, audio_output_context->sample_fmt, 0 );
+ converted_input_samples = (uint8_t*) av_malloc( audioSampleBuffer_size );
+
+ if ( !converted_input_samples ) {
+ Error( "Could not allocate converted input sample pointers\n");
+ return false;
+ }
+
+ // Setup the data pointers in the AVFrame
+ if ( avcodec_fill_audio_frame(
+ output_frame,
+ audio_output_context->channels,
+ audio_output_context->sample_fmt,
+ (const uint8_t*) converted_input_samples,
+ audioSampleBuffer_size, 0 ) < 0 ) {
+ Error( "Could not allocate converted input sample pointers\n");
+ return false;
+ }
+
+ return true;
+#else
+ Error("Not built with libavresample library. Cannot do audio conversion to AAC");
+ return false;
#endif
}
@@ -559,45 +536,63 @@ void VideoStore::dumpPacket( AVPacket *pkt ){
}
int VideoStore::writeVideoFramePacket( AVPacket *ipkt ) {
-
-
- Debug(4, "writeVideoFrame init_packet");
av_init_packet(&opkt);
-if ( 1 ) {
//Scale the PTS of the outgoing packet to be the correct time base
if (ipkt->pts != AV_NOPTS_VALUE) {
- if ( (!video_start_pts) || (video_start_pts > ipkt->pts) ) {
- Debug(1, "Resetting video_start_pts from (%d) to (%d)", video_start_pts, ipkt->pts );
- //never gets set, so the first packet can set it.
- video_start_pts = ipkt->pts;
+
+ if ( ! video_last_pts ) {
+ // This is the first packet.
+ opkt.pts = 0;
+ Debug(2, "Starting video video_last_pts will become (%d)", ipkt->pts );
+ } else {
+ if ( ipkt->pts < video_last_pts ) {
+ Debug(1, "Resetting video_last_pts from (%d) to (%d)", video_last_pts, ipkt->pts );
+ // wrap around, need to figure out the distance FIXME having this wrong should cause a jump, but then play ok?
+ opkt.pts = previous_pts + av_rescale_q( ipkt->pts, video_input_stream->time_base, video_output_stream->time_base);
+ } else {
+ opkt.pts = previous_pts + av_rescale_q( ipkt->pts - video_last_pts, video_input_stream->time_base, video_output_stream->time_base);
+ }
}
- opkt.pts = av_rescale_q(ipkt->pts - video_start_pts, video_input_stream->time_base, video_output_stream->time_base);
- //- ost_tb_start_time;
- Debug(3, "opkt.pts = %d from ipkt->pts(%d) - startPts(%d)", opkt.pts, ipkt->pts, video_start_pts );
+ Debug(3, "opkt.pts = %d from ipkt->pts(%d) - last_pts(%d)", opkt.pts, ipkt->pts, video_last_pts );
+ video_last_pts = ipkt->pts;
} else {
Debug(3, "opkt.pts = undef");
opkt.pts = AV_NOPTS_VALUE;
}
//Scale the DTS of the outgoing packet to be the correct time base
- if(ipkt->dts == AV_NOPTS_VALUE) {
- // why are we using cur_dts instead of packet.dts?
- if ( (!video_start_dts) || (video_start_dts > video_input_stream->cur_dts) ) {
- Debug(1, "Resetting video_start_dts from (%d) to (%d) p.dts was (%d)", video_start_dts, video_input_stream->cur_dts, ipkt->dts );
- video_start_dts = video_input_stream->cur_dts;
- }
- opkt.dts = av_rescale_q(video_input_stream->cur_dts - video_start_dts, AV_TIME_BASE_Q, video_output_stream->time_base);
- Debug(3, "opkt.dts = %d from video_input_stream->cur_dts(%d) - startDts(%d)",
- opkt.dts, video_input_stream->cur_dts, video_start_dts
- );
+
+ // Just because the input stream wraps, doesn't mean the output needs to. Really, if we are limiting ourselves to 10min segments I can't imagine every wrapping in the output. So need to handle input wrap, without causing output wrap.
+ if ( ! video_last_dts ) {
+ // This is the first packet.
+ opkt.dts = 0;
+ Debug(1, "Starting video video_last_pts will become (%d)", ipkt->dts );
} else {
- if ( (!video_start_dts) || (video_start_dts > ipkt->dts) ) {
- Debug(1, "Resetting video_start_dts from (%d) to (%d)", video_start_dts, ipkt->dts );
- video_start_dts = ipkt->dts;
+ if ( ipkt->dts == AV_NOPTS_VALUE ) {
+ // why are we using cur_dts instead of packet.dts? I think cur_dts is in AV_TIME_BASE_Q, but ipkt.dts is in video_input_stream->time_base
+ if ( video_input_stream->cur_dts < video_last_dts ) {
+ Debug(1, "Resetting video_last_dts from (%d) to (%d) p.dts was (%d)", video_last_dts, video_input_stream->cur_dts, ipkt->dts );
+ opkt.dts = previous_dts + av_rescale_q(video_input_stream->cur_dts, AV_TIME_BASE_Q, video_output_stream->time_base);
+ } else {
+ opkt.dts = previous_dts + av_rescale_q(video_input_stream->cur_dts - video_last_dts, AV_TIME_BASE_Q, video_output_stream->time_base);
+ }
+ Debug(3, "opkt.dts = %d from video_input_stream->cur_dts(%d) - previus_dts(%d)",
+ opkt.dts, video_input_stream->cur_dts, video_last_dts
+ );
+ video_last_dts = video_input_stream->cur_dts;
+ } else {
+ if ( ipkt->dts < video_last_dts ) {
+ Debug(1, "Resetting video_last_dts from (%d) to (%d)", video_last_dts, ipkt->dts );
+ opkt.dts = previous_dts + av_rescale_q( ipkt->dts, video_input_stream->time_base, video_output_stream->time_base);
+ } else {
+ opkt.dts = previous_dts + av_rescale_q( ipkt->dts - video_last_dts, video_input_stream->time_base, video_output_stream->time_base);
+ }
+ Debug(3, "opkt.dts = %d from ipkt.dts(%d) - previus_dts(%d)",
+ opkt.dts, ipkt->dts, video_last_dts
+ );
+ video_last_dts = ipkt->dts;
}
- opkt.dts = av_rescale_q(ipkt->dts - video_start_dts, video_input_stream->time_base, video_output_stream->time_base);
- Debug(3, "opkt.dts = %d from ipkt->dts(%d) - startDts(%d)", opkt.dts, ipkt->dts, video_start_dts );
}
if ( opkt.dts > opkt.pts ) {
Debug( 1, "opkt.dts(%d) must be <= opkt.pts(%d). Decompression must happen before presentation.", opkt.dts, opkt.pts );
@@ -605,20 +600,6 @@ if ( 1 ) {
}
opkt.duration = av_rescale_q(ipkt->duration, video_input_stream->time_base, video_output_stream->time_base);
-} else {
- // Using this results in super fast video output, might be because it should be using the codec time base instead of stream tb
- //av_packet_rescale_ts( &opkt, video_input_stream->time_base, video_output_stream->time_base );
-}
-
-if ( opkt.dts != AV_NOPTS_VALUE ) {
- int64_t max = video_output_stream->cur_dts + !(oc->oformat->flags & AVFMT_TS_NONSTRICT);
- if ( video_output_stream->cur_dts && ( video_output_stream->cur_dts != AV_NOPTS_VALUE ) && ( max > opkt.dts ) ) {
- Warning("st:%d PTS: %" PRId64 " DTS: %" PRId64 " < %" PRId64 " invalid, clipping", opkt.stream_index, opkt.pts, opkt.dts, max);
- if( opkt.pts >= opkt.dts)
- opkt.pts = FFMAX(opkt.pts, max);
- opkt.dts = max;
- }
-}
opkt.flags = ipkt->flags;
opkt.pos=-1;
@@ -637,46 +618,47 @@ if ( opkt.dts != AV_NOPTS_VALUE ) {
#if 0
if (video_output_context->codec_type == AVMEDIA_TYPE_VIDEO && (output_format->flags & AVFMT_RAWPICTURE)) {
- AVPicture pict;
-Debug(3, "video and RAWPICTURE");
+ AVPicture pict;
+ Debug(3, "video and RAWPICTURE");
/* store AVPicture in AVPacket, as expected by the output format */
avpicture_fill(&pict, opkt.data, video_output_context->pix_fmt, video_output_context->width, video_output_context->height, 0);
- av_image_fill_arrays(
- opkt.data = (uint8_t *)&pict;
- opkt.size = sizeof(AVPicture);
- opkt.flags |= AV_PKT_FLAG_KEY;
- } else {
-Debug(4, "Not video and RAWPICTURE");
- }
+ av_image_fill_arrays(
+ opkt.data = (uint8_t *)&pict;
+ opkt.size = sizeof(AVPicture);
+ opkt.flags |= AV_PKT_FLAG_KEY;
+ } else {
+ Debug(4, "Not video and RAWPICTURE");
+ }
#endif
- AVPacket safepkt;
- memcpy(&safepkt, &opkt, sizeof(AVPacket));
+ AVPacket safepkt;
+ memcpy(&safepkt, &opkt, sizeof(AVPacket));
- if ((opkt.data == NULL)||(opkt.size < 1)) {
- Warning("%s:%d: Mangled AVPacket: discarding frame", __FILE__, __LINE__ );
- dumpPacket( ipkt);
- dumpPacket(&opkt);
+ if ((opkt.data == NULL)||(opkt.size < 1)) {
+ Warning("%s:%d: Mangled AVPacket: discarding frame", __FILE__, __LINE__ );
+ dumpPacket( ipkt);
+ dumpPacket(&opkt);
- } else if ((prevDts > 0) && (prevDts > opkt.dts)) {
- Warning("%s:%d: DTS out of order: %lld \u226E %lld; discarding frame", __FILE__, __LINE__, prevDts, opkt.dts);
- prevDts = opkt.dts;
- dumpPacket(&opkt);
+ } else if ((previous_dts > 0) && (previous_dts > opkt.dts)) {
+ Warning("%s:%d: DTS out of order: %lld \u226E %lld; discarding frame", __FILE__, __LINE__, previous_dts, opkt.dts);
+ previous_dts = opkt.dts;
+ dumpPacket(&opkt);
- } else {
+ } else {
- prevDts = opkt.dts; // Unsure if av_interleaved_write_frame() clobbers opkt.dts when out of order, so storing in advance
- ret = av_interleaved_write_frame(oc, &opkt);
- if(ret<0){
- // There's nothing we can really do if the frame is rejected, just drop it and get on with the next
- Warning("%s:%d: Writing frame [av_interleaved_write_frame()] failed: %s(%d) ", __FILE__, __LINE__, av_make_error_string(ret).c_str(), (ret));
- dumpPacket(&safepkt);
- }
- }
+ previous_dts = opkt.dts; // Unsure if av_interleaved_write_frame() clobbers opkt.dts when out of order, so storing in advance
+ previous_pts = opkt.pts;
+ ret = av_interleaved_write_frame(oc, &opkt);
+ if(ret<0){
+ // There's nothing we can really do if the frame is rejected, just drop it and get on with the next
+ Warning("%s:%d: Writing frame [av_interleaved_write_frame()] failed: %s(%d) ", __FILE__, __LINE__, av_make_error_string(ret).c_str(), (ret));
+ dumpPacket(&safepkt);
+ }
+ }
- zm_av_packet_unref(&opkt);
+ zm_av_packet_unref(&opkt);
- return 0;
+ return 0;
}
@@ -687,102 +669,46 @@ int VideoStore::writeAudioFramePacket( AVPacket *ipkt ) {
Debug(1, "Called writeAudioFramePacket when no audio_output_stream");
return 0;//FIXME -ve return codes do not free packet in ffmpeg_camera at the moment
}
- /*if(!keyframeMessage)
- return -1;*/
- //zm_dump_stream_format( oc, ipkt->stream_index, 0, 1 );
- av_init_packet(&opkt);
- Debug(5, "after init packet" );
-
-#if 1
- //Scale the PTS of the outgoing packet to be the correct time base
- if ( ipkt->pts != AV_NOPTS_VALUE ) {
- if ( (!audio_start_pts) || ( audio_start_pts > ipkt->pts ) ) {
- Debug(1, "Resetting audeo_start_pts from (%d) to (%d)", audio_start_pts, ipkt->pts );
- //never gets set, so the first packet can set it.
- audio_start_pts = ipkt->pts;
- }
- opkt.pts = av_rescale_q(ipkt->pts - audio_start_pts, audio_input_stream->time_base, audio_output_stream->time_base);
- Debug(2, "opkt.pts = %d from ipkt->pts(%d) - startPts(%d)", opkt.pts, ipkt->pts, audio_start_pts );
- } else {
- Debug(2, "opkt.pts = undef");
- }
-
- //Scale the DTS of the outgoing packet to be the correct time base
- if(ipkt->dts == AV_NOPTS_VALUE) {
- if ( (!audio_start_dts) || (audio_start_dts > audio_input_stream->cur_dts ) ) {
- Debug(1, "Resetting audio_start_pts from (%d) to cur_dts (%d)", audio_start_dts, audio_input_stream->cur_dts );
- audio_start_dts = audio_input_stream->cur_dts;
- }
- opkt.dts = av_rescale_q(audio_input_stream->cur_dts - audio_start_dts, AV_TIME_BASE_Q, audio_output_stream->time_base);
- Debug(2, "opkt.dts = %d from video_input_stream->cur_dts(%d) - startDts(%d)",
- opkt.dts, audio_input_stream->cur_dts, audio_start_dts
- );
- } else {
- if ( ( ! audio_start_dts ) || ( audio_start_dts > ipkt->dts ) ) {
- Debug(1, "Resetting audeo_start_dts from (%d) to (%d)", audio_start_dts, ipkt->dts );
- audio_start_dts = ipkt->dts;
- }
- opkt.dts = av_rescale_q(ipkt->dts - audio_start_dts, audio_input_stream->time_base, audio_output_stream->time_base);
- Debug(2, "opkt.dts = %d from ipkt->dts(%d) - startDts(%d)", opkt.dts, ipkt->dts, audio_start_dts );
- }
- if ( opkt.dts > opkt.pts ) {
- Debug(1,"opkt.dts(%d) must be <= opkt.pts(%d). Decompression must happen before presentation.", opkt.dts, opkt.pts );
- opkt.dts = opkt.pts;
- }
- //opkt.pts = AV_NOPTS_VALUE;
- //opkt.dts = AV_NOPTS_VALUE;
-
- opkt.duration = av_rescale_q(ipkt->duration, audio_input_stream->time_base, audio_output_stream->time_base);
-#else
-#endif
-
- // pkt.pos: byte position in stream, -1 if unknown
- opkt.pos = -1;
- opkt.flags = ipkt->flags;
- opkt.stream_index = ipkt->stream_index;
-Debug(2, "Stream index is %d", opkt.stream_index );
if ( audio_output_codec ) {
-#ifdef HAVE_LIBSWRESAMPLE
- // Need to re-encode
-#if 1
- ret = avcodec_send_packet( audio_input_context, ipkt );
- if ( ret < 0 ) {
- Error("avcodec_send_packet fail %s", av_make_error_string(ret).c_str());
- return 0;
- }
+#if 0
+ ret = avcodec_send_packet( audio_input_context, ipkt );
+ if ( ret < 0 ) {
+ Error("avcodec_send_packet fail %s", av_make_error_string(ret).c_str());
+ return 0;
+ }
- ret = avcodec_receive_frame( audio_input_context, input_frame );
- if ( ret < 0 ) {
- Error("avcodec_receive_frame fail %s", av_make_error_string(ret).c_str());
- return 0;
- }
-Debug(2, "Frame: samples(%d), format(%d), sample_rate(%d), channel layout(%d) refd(%d)",
-input_frame->nb_samples,
-input_frame->format,
-input_frame->sample_rate,
-input_frame->channel_layout,
-audio_output_context->refcounted_frames
-);
+ ret = avcodec_receive_frame( audio_input_context, input_frame );
+ if ( ret < 0 ) {
+ Error("avcodec_receive_frame fail %s", av_make_error_string(ret).c_str());
+ return 0;
+ }
+ Debug(2, "Frame: samples(%d), format(%d), sample_rate(%d), channel layout(%d) refd(%d)",
+ input_frame->nb_samples,
+ input_frame->format,
+ input_frame->sample_rate,
+ input_frame->channel_layout,
+ audio_output_context->refcounted_frames
+ );
- ret = avcodec_send_frame( audio_output_context, input_frame );
- if ( ret < 0 ) {
+ ret = avcodec_send_frame( audio_output_context, input_frame );
+ if ( ret < 0 ) {
+ av_frame_unref( input_frame );
+ Error("avcodec_send_frame fail(%d), %s codec is open(%d) is_encoder(%d)", ret, av_make_error_string(ret).c_str(),
+ avcodec_is_open( audio_output_context ),
+ av_codec_is_encoder( audio_output_context->codec)
+ );
+ return 0;
+ }
+ ret = avcodec_receive_packet( audio_output_context, &opkt );
+ if ( ret < 0 ) {
+ av_frame_unref( input_frame );
+ Error("avcodec_receive_packet fail %s", av_make_error_string(ret).c_str());
+ return 0;
+ }
av_frame_unref( input_frame );
- Error("avcodec_send_frame fail(%d), %s codec is open(%d) is_encoder(%d)", ret, av_make_error_string(ret).c_str(),
-avcodec_is_open( audio_output_context ),
-av_codec_is_encoder( audio_output_context->codec)
-);
- return 0;
- }
- ret = avcodec_receive_packet( audio_output_context, &opkt );
- if ( ret < 0 ) {
- av_frame_unref( input_frame );
- Error("avcodec_receive_packet fail %s", av_make_error_string(ret).c_str());
- return 0;
- }
- av_frame_unref( input_frame );
#else
@@ -793,13 +719,13 @@ av_codec_is_encoder( audio_output_context->codec)
* to flush it.
*/
if ((ret = avcodec_decode_audio4(audio_input_context, input_frame,
- &data_present, ipkt)) < 0) {
- Error( "Could not decode frame (error '%s')\n",
- av_make_error_string(ret).c_str());
- dumpPacket( ipkt );
- av_frame_free(&input_frame);
- zm_av_packet_unref(&opkt);
- return 0;
+ &data_present, ipkt)) < 0) {
+ Error( "Could not decode frame (error '%s')\n",
+ av_make_error_string(ret).c_str());
+ dumpPacket( ipkt );
+ av_frame_free(&input_frame);
+ zm_av_packet_unref(&opkt);
+ return 0;
}
if ( ! data_present ) {
Debug(2, "Not ready to transcode a frame yet.");
@@ -810,71 +736,40 @@ av_codec_is_encoder( audio_output_context->codec)
int frame_size = input_frame->nb_samples;
Debug(4, "Frame size: %d", frame_size );
-
- Debug(4, "About to convert");
-
- /** Convert the samples using the resampler. */
- if ((ret = swr_convert(resample_context,
- &converted_input_samples, frame_size,
- (const uint8_t **)input_frame->extended_data , frame_size)) < 0) {
- Error( "Could not convert input samples (error '%s')\n",
- av_make_error_string(ret).c_str()
- );
+ // Resample the input into the audioSampleBuffer until we proceed the whole decoded data
+ if ( (ret = avresample_convert( resample_context,
+ NULL,
+ 0,
+ 0,
+ input_frame->data,
+ 0,
+ input_frame->nb_samples )) < 0 ) {
+ Error( "Could not resample frame (error '%s')\n",
+ av_make_error_string(ret).c_str());
return 0;
}
- Debug(4, "About to realloc");
- if ((ret = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
- Error( "Could not reallocate FIFO to %d\n", av_audio_fifo_size(fifo) + frame_size );
- return 0;
- }
- /** Store the new samples in the FIFO buffer. */
- Debug(4, "About to write");
- if (av_audio_fifo_write(fifo, (void **)&converted_input_samples, frame_size) < frame_size) {
- Error( "Could not write data to FIFO\n");
+ if ( avresample_available( resample_context ) < output_frame->nb_samples ) {
+ Debug(1, "No enough samples yet");
return 0;
}
- /**
- * Set the frame's parameters, especially its size and format.
- * av_frame_get_buffer needs this to allocate memory for the
- * audio samples of the frame.
- * Default channel layouts based on the number of channels
- * are assumed for simplicity.
- */
- output_frame->nb_samples = audio_output_context->frame_size;
- output_frame->channel_layout = audio_output_context->channel_layout;
- output_frame->channels = audio_output_context->channels;
- output_frame->format = audio_output_context->sample_fmt;
- output_frame->sample_rate = audio_output_context->sample_rate;
- /**
- * Allocate the samples of the created frame. This call will make
- * sure that the audio frame can hold as many samples as specified.
- */
- Debug(4, "getting buffer");
- if (( ret = av_frame_get_buffer( output_frame, 0)) < 0) {
- Error( "Couldnt allocate output frame buffer samples (error '%s')",
- av_make_error_string(ret).c_str() );
- Error("Frame: samples(%d) layout (%d) format(%d) rate(%d)", output_frame->nb_samples,
- output_frame->channel_layout, output_frame->format , output_frame->sample_rate
- );
- zm_av_packet_unref(&opkt);
+ // Read a frame audio data from the resample fifo
+ if ( avresample_read( resample_context, output_frame->data, output_frame->nb_samples ) != output_frame->nb_samples ) {
+ Warning( "Error reading resampled audio: " );
return 0;
}
- Debug(4, "About to read");
- if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
- Error( "Could not read data from FIFO\n");
- return 0;
- }
+ av_init_packet(&opkt);
+ Debug(5, "after init packet" );
/** Set a timestamp based on the sample rate for the container. */
- output_frame->pts = av_rescale_q( opkt.pts, audio_output_context->time_base, audio_output_stream->time_base );
+ //output_frame->pts = av_rescale_q( opkt.pts, audio_output_context->time_base, audio_output_stream->time_base );
- // convert the packet to the codec timebase from the stream timebase
-Debug(3, "output_frame->pts(%d) best effort(%d)", output_frame->pts,
-av_frame_get_best_effort_timestamp(output_frame)
- );
+ // convert the packet to the codec timebase from the stream timebase
+ //Debug(3, "output_frame->pts(%d) best effort(%d)", output_frame->pts,
+ //av_frame_get_best_effort_timestamp(output_frame)
+ //);
/**
* Encode the audio frame and store it in the temporary packet.
* The output audio stream encoder is used to do this.
@@ -894,32 +789,73 @@ av_frame_get_best_effort_timestamp(output_frame)
zm_av_packet_unref(&opkt);
return 0;
}
-
-Debug(2, "opkt dts (%d) pts(%d) duration:(%d)", opkt.dts, opkt.pts, opkt.duration );
-
- // Convert tb from code back to stream
- //av_packet_rescale_ts(&opkt, audio_output_context->time_base, audio_output_stream->time_base);
-if (opkt.pts != AV_NOPTS_VALUE) {
- opkt.pts = av_rescale_q( opkt.pts, audio_output_context->time_base, audio_output_stream->time_base);
-}
- if ( opkt.dts != AV_NOPTS_VALUE)
- opkt.dts = av_rescale_q( opkt.dts, audio_output_context->time_base, audio_output_stream->time_base);
- if ( opkt.duration > 0)
- opkt.duration = av_rescale_q( opkt.duration, audio_output_context->time_base, audio_output_stream->time_base);
-Debug(2, "opkt dts (%d) pts(%d) duration:(%d) pos(%d) ", opkt.dts, opkt.pts, opkt.duration, opkt.pos );
-
-
-//opkt.dts = AV_NOPTS_VALUE;
-
-
-#endif
#endif
} else {
+ av_init_packet(&opkt);
+ Debug(5, "after init packet" );
opkt.data = ipkt->data;
opkt.size = ipkt->size;
}
+ // PTS is difficult, because of the buffering of the audio packets in the resampler. So we have to do it once we actually have a packet...
+
+ //Scale the PTS of the outgoing packet to be the correct time base
+ if ( ipkt->pts != AV_NOPTS_VALUE ) {
+ if ( !audio_last_pts ) {
+ opkt.pts = 0;
+ } else {
+ if ( audio_last_pts > ipkt->pts ) {
+ Debug(1, "Resetting audeo_start_pts from (%d) to (%d)", audio_last_pts, ipkt->pts );
+ }
+ opkt.pts = previous_pts + av_rescale_q(ipkt->pts - audio_last_pts, audio_input_stream->time_base, audio_output_stream->time_base);
+ Debug(2, "opkt.pts = %d from ipkt->pts(%d) - last_pts(%d)", opkt.pts, ipkt->pts, audio_last_pts );
+ }
+ audio_last_pts = ipkt->pts;
+ } else {
+ Debug(2, "opkt.pts = undef");
+ opkt.pts = AV_NOPTS_VALUE;
+ }
+
+ //Scale the DTS of the outgoing packet to be the correct time base
+ if ( ! audio_last_dts ) {
+ opkt.dts = 0;
+ } else {
+ if( ipkt->dts == AV_NOPTS_VALUE ) {
+ // So if the input has no dts assigned... still need an output dts... so we use cur_dts?
+
+ if ( audio_last_dts > audio_input_stream->cur_dts ) {
+ Debug(1, "Resetting audio_last_pts from (%d) to cur_dts (%d)", audio_last_dts, audio_input_stream->cur_dts );
+ opkt.dts = previous_dts + av_rescale_q( audio_input_stream->cur_dts, AV_TIME_BASE_Q, audio_output_stream->time_base);
+ } else {
+ opkt.dts = previous_dts + av_rescale_q( audio_input_stream->cur_dts - audio_last_dts, AV_TIME_BASE_Q, audio_output_stream->time_base);
+ }
+ audio_last_dts = audio_input_stream->cur_dts;
+ Debug(2, "opkt.dts = %d from video_input_stream->cur_dts(%d) - last_dts(%d)", opkt.dts, audio_input_stream->cur_dts, audio_last_dts );
+ } else {
+ if ( audio_last_dts > ipkt->dts ) {
+ Debug(1, "Resetting audio_last_dts from (%d) to (%d)", audio_last_dts, ipkt->dts );
+ opkt.dts = previous_dts + av_rescale_q(ipkt->dts, audio_input_stream->time_base, audio_output_stream->time_base);
+ } else {
+ opkt.dts = previous_dts + av_rescale_q(ipkt->dts - audio_last_dts, audio_input_stream->time_base, audio_output_stream->time_base);
+ }
+ Debug(2, "opkt.dts = %d from ipkt->dts(%d) - last_dts(%d)", opkt.dts, ipkt->dts, audio_last_dts );
+ }
+ }
+ if ( opkt.dts > opkt.pts ) {
+ Debug(1,"opkt.dts(%d) must be <= opkt.pts(%d). Decompression must happen before presentation.", opkt.dts, opkt.pts );
+ opkt.dts = opkt.pts;
+ }
+
+ // I wonder if we could just use duration instead of all the hoop jumping above?
+ opkt.duration = av_rescale_q(ipkt->duration, audio_input_stream->time_base, audio_output_stream->time_base);
+
+ // pkt.pos: byte position in stream, -1 if unknown
+ opkt.pos = -1;
+ opkt.flags = ipkt->flags;
+ opkt.stream_index = ipkt->stream_index;
+ Debug(2, "Stream index is %d", opkt.stream_index );
+
AVPacket safepkt;
memcpy(&safepkt, &opkt, sizeof(AVPacket));
ret = av_interleaved_write_frame(oc, &opkt);
@@ -931,160 +867,5 @@ Debug(2, "opkt dts (%d) pts(%d) duration:(%d) pos(%d) ", opkt.dts, opkt.pts, opk
}
zm_av_packet_unref(&opkt);
return 0;
-}
+} // end int VideoStore::writeAudioFramePacket( AVPacket *ipkt )
-static int init_filter(FilteringContext* fctx, AVCodecContext *dec_ctx,
- AVCodecContext *enc_ctx, const char *filter_spec)
-{
- char args[512];
- int ret = 0;
- AVFilter *buffersrc = NULL;
- AVFilter *buffersink = NULL;
- AVFilterContext *buffersrc_ctx = NULL;
- AVFilterContext *buffersink_ctx = NULL;
- AVFilterInOut *outputs = avfilter_inout_alloc();
- AVFilterInOut *inputs = avfilter_inout_alloc();
- AVFilterGraph *filter_graph = avfilter_graph_alloc();
- if (!outputs || !inputs || !filter_graph) {
- ret = AVERROR(ENOMEM);
- goto end;
- }
- if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
- buffersrc = avfilter_get_by_name("buffer");
- buffersink = avfilter_get_by_name("buffersink");
- if (!buffersrc || !buffersink) {
- av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
- ret = AVERROR_UNKNOWN;
- goto end;
- }
- snprintf(args, sizeof(args),
- "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
- dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
- dec_ctx->time_base.num, dec_ctx->time_base.den,
- dec_ctx->sample_aspect_ratio.num,
- dec_ctx->sample_aspect_ratio.den);
- ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
- args, NULL, filter_graph);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
- goto end;
- }
- ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
- NULL, NULL, filter_graph);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
- goto end;
- }
- ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
- (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
- AV_OPT_SEARCH_CHILDREN);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
- goto end;
- }
- } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
- buffersrc = avfilter_get_by_name("abuffer");
- buffersink = avfilter_get_by_name("abuffersink");
- if (!buffersrc || !buffersink) {
- av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
- ret = AVERROR_UNKNOWN;
- goto end;
- }
- if (!dec_ctx->channel_layout)
- dec_ctx->channel_layout =
- av_get_default_channel_layout(dec_ctx->channels);
- snprintf(args, sizeof(args),
- "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
- dec_ctx->time_base.num, dec_ctx->time_base.den, dec_ctx->sample_rate,
- av_get_sample_fmt_name(dec_ctx->sample_fmt),
- dec_ctx->channel_layout);
- ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
- args, NULL, filter_graph);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
- goto end;
- }
- ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
- NULL, NULL, filter_graph);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
- goto end;
- }
- ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
- (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
- AV_OPT_SEARCH_CHILDREN);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
- goto end;
- }
- ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
- (uint8_t*)&enc_ctx->channel_layout,
- sizeof(enc_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
- goto end;
- }
- ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
- (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
- AV_OPT_SEARCH_CHILDREN);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
- goto end;
- }
- } else {
- ret = AVERROR_UNKNOWN;
- goto end;
- }
- /* Endpoints for the filter graph. */
- outputs->name = av_strdup("in");
- outputs->filter_ctx = buffersrc_ctx;
- outputs->pad_idx = 0;
- outputs->next = NULL;
- inputs->name = av_strdup("out");
- inputs->filter_ctx = buffersink_ctx;
- inputs->pad_idx = 0;
- inputs->next = NULL;
- if (!outputs->name || !inputs->name) {
- ret = AVERROR(ENOMEM);
- goto end;
- }
- if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
- &inputs, &outputs, NULL)) < 0)
- goto end;
- if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
- goto end;
- /* Fill FilteringContext */
- fctx->buffersrc_ctx = buffersrc_ctx;
- fctx->buffersink_ctx = buffersink_ctx;
- fctx->filter_graph = filter_graph;
-end:
- avfilter_inout_free(&inputs);
- avfilter_inout_free(&outputs);
- return ret;
-}
-static int init_filters(void)
-{
- const char *filter_spec;
- unsigned int i;
- int ret;
- filter_ctx = av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
- if (!filter_ctx)
- return AVERROR(ENOMEM);
- for (i = 0; i < ifmt_ctx->nb_streams; i++) {
- filter_ctx[i].buffersrc_ctx = NULL;
- filter_ctx[i].buffersink_ctx = NULL;
- filter_ctx[i].filter_graph = NULL;
- if (!(ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO
- || ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO))
- continue;
- if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
- filter_spec = "null"; /* passthrough (dummy) filter for video */
- else
- filter_spec = "anull"; /* passthrough (dummy) filter for audio */
- ret = init_filter(&filter_ctx[i], ifmt_ctx->streams[i]->codec,
- ofmt_ctx->streams[i]->codec, filter_spec);
- if (ret)
- return ret;
- }
- return 0;
-}
diff --git a/src/zm_videostore.h b/src/zm_videostore.h
index 2d67e8145..43f508a6b 100644
--- a/src/zm_videostore.h
+++ b/src/zm_videostore.h
@@ -8,6 +8,9 @@ extern "C" {
#ifdef HAVE_LIBSWRESAMPLE
#include "libswresample/swresample.h"
#endif
+#ifdef HAVE_LIBAVRESAMPLE
+#include "libavresample/avresample.h"
+#endif
}
#if HAVE_LIBAVCODEC
@@ -44,7 +47,10 @@ private:
AVAudioFifo *fifo;
int output_frame_size;
#ifdef HAVE_LIBSWRESAMPLE
- SwrContext *resample_context = NULL;
+ //SwrContext *resample_context = NULL;
+#endif
+#ifdef HAVE_LIBAVRESAMPLE
+AVAudioResampleContext* resample_context;
#endif
uint8_t *converted_input_samples = NULL;
@@ -54,17 +60,20 @@ private:
bool keyframeMessage;
int keyframeSkipNumber;
- int64_t video_start_pts;
- int64_t video_start_dts;
- int64_t audio_start_pts;
- int64_t audio_start_dts;
+ // These are for input
+ int64_t video_last_pts;
+ int64_t video_last_dts;
+ int64_t audio_last_pts;
+ int64_t audio_last_dts;
- int64_t start_pts;
- int64_t start_dts;
+ // These are for output, should start at zero. We assume they do not wrap because we just aren't going to save files that big.
+ int64_t previous_pts;
+ int64_t previous_dts;
- int64_t prevDts;
int64_t filter_in_rescale_delta_last;
+ bool setup_resampler();
+
public:
VideoStore(const char *filename_in, const char *format_in, AVStream *video_input_stream, AVStream *audio_input_stream, int64_t nStartTime, Monitor * p_monitor );
~VideoStore();
diff --git a/src/zm_zone.cpp b/src/zm_zone.cpp
index 5d5ae41cd..243e4a50f 100644
--- a/src/zm_zone.cpp
+++ b/src/zm_zone.cpp
@@ -945,15 +945,13 @@ int Zone::Load( Monitor *monitor, Zone **&zones )
{
static char sql[ZM_SQL_MED_BUFSIZ];
snprintf( sql, sizeof(sql), "select Id,Name,Type+0,Units,Coords,AlarmRGB,CheckMethod+0,MinPixelThreshold,MaxPixelThreshold,MinAlarmPixels,MaxAlarmPixels,FilterX,FilterY,MinFilterPixels,MaxFilterPixels,MinBlobPixels,MaxBlobPixels,MinBlobs,MaxBlobs,OverloadFrames,ExtendAlarmFrames from Zones where MonitorId = %d order by Type, Id", monitor->Id() );
- if ( mysql_query( &dbconn, sql ) )
- {
+ if ( mysql_query( &dbconn, sql ) ) {
Error( "Can't run query: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
MYSQL_RES *result = mysql_store_result( &dbconn );
- if ( !result )
- {
+ if ( !result ) {
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
exit( mysql_errno( &dbconn ) );
}
@@ -961,8 +959,7 @@ int Zone::Load( Monitor *monitor, Zone **&zones )
Debug( 1, "Got %d zones for monitor %s", n_zones, monitor->Name() );
delete[] zones;
zones = new Zone *[n_zones];
- for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )
- {
+ for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ ) {
zones[i] = NULL;
int col = 0;
@@ -995,17 +992,18 @@ int Zone::Load( Monitor *monitor, Zone **&zones )
Polygon polygon;
if ( !ParsePolygonString( Coords, polygon ) ) {
Error( "Unable to parse polygon string '%s' for zone %d/%s for monitor %s, ignoring", Coords, Id, Name, monitor->Name() );
+ n_zones -= 1;
continue;
}
if ( polygon.LoX() < 0 || polygon.HiX() >= (int)monitor->Width()
|| polygon.LoY() < 0 || polygon.HiY() >= (int)monitor->Height() ) {
Error( "Zone %d/%s for monitor %s extends outside of image dimensions, (%d,%d), (%d,%d), ignoring", Id, Name, monitor->Name(), polygon.LoX(), polygon.LoY(), polygon.HiX(), polygon.HiY() );
+ n_zones -= 1;
continue;
}
- if ( false && !strcmp( Units, "Percent" ) )
- {
+ if ( false && !strcmp( Units, "Percent" ) ) {
MinAlarmPixels = (MinAlarmPixels*polygon.Area())/100;
MaxAlarmPixels = (MaxAlarmPixels*polygon.Area())/100;
MinFilterPixels = (MinFilterPixels*polygon.Area())/100;
diff --git a/src/zma.cpp b/src/zma.cpp
index f9557ba59..8d446668b 100644
--- a/src/zma.cpp
+++ b/src/zma.cpp
@@ -133,7 +133,7 @@ int main( int argc, char *argv[] )
logInit( log_id_string );
- ssedetect();
+ hwcaps_detect();
Monitor *monitor = Monitor::Load( id, true, Monitor::ANALYSIS );
diff --git a/src/zmc.cpp b/src/zmc.cpp
index 088b5667d..58b60e7a9 100644
--- a/src/zmc.cpp
+++ b/src/zmc.cpp
@@ -73,8 +73,7 @@ possible, this should run at more or less constant speed.
#include "zm_signal.h"
#include "zm_monitor.h"
-void Usage()
-{
+void Usage() {
fprintf( stderr, "zmc -d or -r -H -P -p or -f or -m \n" );
fprintf( stderr, "Options:\n" );
@@ -91,8 +90,7 @@ void Usage()
exit( 0 );
}
-int main( int argc, char *argv[] )
-{
+int main( int argc, char *argv[] ) {
self = argv[0];
srand( getpid() * time( 0 ) );
@@ -110,21 +108,19 @@ int main( int argc, char *argv[] )
{"protocol", 1, 0, 'r'},
{"host", 1, 0, 'H'},
{"port", 1, 0, 'P'},
- {"path", 1, 0, 'p'},
- {"file", 1, 0, 'f'},
+ {"path", 1, 0, 'p'},
+ {"file", 1, 0, 'f'},
{"monitor", 1, 0, 'm'},
{"help", 0, 0, 'h'},
{"version", 0, 0, 'v'},
{0, 0, 0, 0}
};
- while (1)
- {
+ while (1) {
int option_index = 0;
int c = getopt_long (argc, argv, "d:H:P:p:f:m:h:v", long_options, &option_index);
- if (c == -1)
- {
+ if (c == -1) {
break;
}
@@ -161,8 +157,7 @@ int main( int argc, char *argv[] )
}
}
- if (optind < argc)
- {
+ if (optind < argc) {
fprintf( stderr, "Extraneous options, " );
while (optind < argc)
printf ("%s ", argv[optind++]);
@@ -171,37 +166,28 @@ int main( int argc, char *argv[] )
}
int modes = ( device[0]?1:0 + host[0]?1:0 + file[0]?1:0 + (monitor_id>0?1:0) );
- if ( modes > 1 )
- {
+ if ( modes > 1 ) {
fprintf( stderr, "Only one of device, host/port/path, file or monitor id allowed\n" );
Usage();
exit( 0 );
}
- if ( modes < 1 )
- {
+ if ( modes < 1 ) {
fprintf( stderr, "One of device, host/port/path, file or monitor id must be specified\n" );
Usage();
exit( 0 );
}
char log_id_string[32] = "";
- if ( device[0] )
- {
+ if ( device[0] ) {
const char *slash_ptr = strrchr( device, '/' );
snprintf( log_id_string, sizeof(log_id_string), "zmc_d%s", slash_ptr?slash_ptr+1:device );
- }
- else if ( host[0] )
- {
+ } else if ( host[0] ) {
snprintf( log_id_string, sizeof(log_id_string), "zmc_h%s", host );
- }
- else if ( file[0] )
- {
+ } else if ( file[0] ) {
const char *slash_ptr = strrchr( file, '/' );
snprintf( log_id_string, sizeof(log_id_string), "zmc_f%s", slash_ptr?slash_ptr+1:file );
- }
- else
- {
+ } else {
snprintf( log_id_string, sizeof(log_id_string), "zmc_m%d", monitor_id );
}
@@ -209,40 +195,31 @@ int main( int argc, char *argv[] )
logInit( log_id_string );
- ssedetect();
+ hwcaps_detect();
Monitor **monitors = 0;
int n_monitors = 0;
#if ZM_HAS_V4L
- if ( device[0] )
- {
+ if ( device[0] ) {
n_monitors = Monitor::LoadLocalMonitors( device, monitors, Monitor::CAPTURE );
- }
- else
+ } else
#endif // ZM_HAS_V4L
- if ( host[0] )
- {
+ if ( host[0] ) {
if ( !port )
port = "80";
n_monitors = Monitor::LoadRemoteMonitors( protocol, host, port, path, monitors, Monitor::CAPTURE );
- }
- else if ( file[0] )
- {
+ } else if ( file[0] ) {
n_monitors = Monitor::LoadFileMonitors( file, monitors, Monitor::CAPTURE );
- }
- else
- {
+ } else {
Monitor *monitor = Monitor::Load( monitor_id, true, Monitor::CAPTURE );
- if ( monitor )
- {
+ if ( monitor ) {
monitors = new Monitor *[1];
monitors[0] = monitor;
n_monitors = 1;
}
}
- if ( !n_monitors )
- {
+ if ( !n_monitors ) {
Error( "No monitors found" );
exit ( -1 );
}
@@ -259,8 +236,7 @@ int main( int argc, char *argv[] )
sigaddset( &block_set, SIGUSR2 );
monitors[0]->setStartupTime( (time_t)time(NULL) );
- if ( monitors[0]->PrimeCapture() < 0 )
- {
+ if ( monitors[0]->PrimeCapture() < 0 ) {
Error( "Failed to prime capture of initial monitor" );
exit( -1 );
}
@@ -269,8 +245,7 @@ int main( int argc, char *argv[] )
long *alarm_capture_delays = new long[n_monitors];
long *next_delays = new long[n_monitors];
struct timeval * last_capture_times = new struct timeval[n_monitors];
- for ( int i = 0; i < n_monitors; i++ )
- {
+ for ( int i = 0; i < n_monitors; 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();
@@ -279,18 +254,14 @@ int main( int argc, char *argv[] )
int result = 0;
struct timeval now;
struct DeltaTimeval delta_time;
- while( !zm_terminate )
- {
+ while( !zm_terminate ) {
sigprocmask( SIG_BLOCK, &block_set, 0 );
- for ( int i = 0; i < n_monitors; i++ )
- {
+ for ( int i = 0; i < n_monitors; i++ ) {
long min_delay = MAXINT;
gettimeofday( &now, NULL );
- for ( int j = 0; j < n_monitors; j++ )
- {
- if ( last_capture_times[j].tv_sec )
- {
+ for ( int j = 0; j < n_monitors; j++ ) {
+ if ( last_capture_times[j].tv_sec ) {
DELTA_TIMEVAL( delta_time, now, last_capture_times[j], DT_PREC_3 );
if ( monitors[i]->GetState() == Monitor::ALARM )
next_delays[j] = alarm_capture_delays[j]-delta_time.delta;
@@ -298,48 +269,39 @@ int main( int argc, char *argv[] )
next_delays[j] = capture_delays[j]-delta_time.delta;
if ( next_delays[j] < 0 )
next_delays[j] = 0;
- }
- else
- {
+ } else {
next_delays[j] = 0;
}
- if ( next_delays[j] <= min_delay )
- {
+ if ( next_delays[j] <= min_delay ) {
min_delay = next_delays[j];
}
}
- if ( next_delays[i] <= min_delay || next_delays[i] <= 0 )
- {
- if ( monitors[i]->PreCapture() < 0 )
- {
+ if ( next_delays[i] <= min_delay || next_delays[i] <= 0 ) {
+ 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 );
zm_terminate = true;
result = -1;
break;
}
- if ( monitors[i]->Capture() < 0 )
- {
+ 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 );
zm_terminate = true;
result = -1;
break;
}
- if ( monitors[i]->PostCapture() < 0 )
- {
+ 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 );
zm_terminate = true;
result = -1;
break;
}
- if ( next_delays[i] > 0 )
- {
+ if ( next_delays[i] > 0 ) {
gettimeofday( &now, NULL );
DELTA_TIMEVAL( delta_time, now, last_capture_times[i], DT_PREC_3 );
long sleep_time = next_delays[i]-delta_time.delta;
- if ( sleep_time > 0 )
- {
+ if ( sleep_time > 0 ) {
usleep( sleep_time*(DT_MAXGRAN/DT_PREC_3) );
}
}
@@ -349,8 +311,7 @@ int main( int argc, char *argv[] )
} // end foreach n_monitors
sigprocmask( SIG_UNBLOCK, &block_set, 0 );
} // end while ! zm_terminate
- for ( int i = 0; i < n_monitors; i++ )
- {
+ for ( int i = 0; i < n_monitors; i++ ) {
delete monitors[i];
}
delete [] monitors;
diff --git a/src/zmf.cpp b/src/zmf.cpp
index f547081ab..fac1b76c8 100644
--- a/src/zmf.cpp
+++ b/src/zmf.cpp
@@ -138,157 +138,158 @@ void Usage()
int main( int argc, char *argv[] )
{
- self = argv[0];
+ self = argv[0];
- srand( getpid() * time( 0 ) );
+ srand( getpid() * time( 0 ) );
- int id = -1;
+ int id = -1;
- static struct option long_options[] = {
- {"monitor", 1, 0, 'm'},
- {"help", 0, 0, 'h'},
- {"version", 0, 0, 'v'},
- {0, 0, 0, 0}
- };
+ static struct option long_options[] = {
+ {"monitor", 1, 0, 'm'},
+ {"help", 0, 0, 'h'},
+ {"version", 0, 0, 'v'},
+ {0, 0, 0, 0}
+ };
- while (1)
- {
- int option_index = 0;
+ while (1)
+ {
+ int option_index = 0;
- int c = getopt_long (argc, argv, "m:h:v", long_options, &option_index);
- if (c == -1)
- {
- break;
- }
+ int c = getopt_long (argc, argv, "m:h:v", long_options, &option_index);
+ if (c == -1)
+ {
+ break;
+ }
- switch (c)
- {
- case 'm':
- id = atoi(optarg);
- break;
- case 'h':
- case '?':
- Usage();
- break;
- case 'v':
- std::cout << ZM_VERSION << "\n";
- exit(0);
- default:
- //fprintf( stderr, "?? getopt returned character code 0%o ??\n", c );
- break;
- }
- }
+ switch (c)
+ {
+ case 'm':
+ id = atoi(optarg);
+ break;
+ case 'h':
+ case '?':
+ Usage();
+ break;
+ case 'v':
+ std::cout << ZM_VERSION << "\n";
+ exit(0);
+ default:
+ //fprintf( stderr, "?? getopt returned character code 0%o ??\n", c );
+ break;
+ }
+ }
- if (optind < argc)
- {
- fprintf( stderr, "Extraneous options, " );
- while (optind < argc)
- printf ("%s ", argv[optind++]);
- printf ("\n");
- Usage();
- }
+ if (optind < argc)
+ {
+ fprintf( stderr, "Extraneous options, " );
+ while (optind < argc)
+ printf ("%s ", argv[optind++]);
+ printf ("\n");
+ Usage();
+ }
- if ( id < 0 )
- {
- fprintf( stderr, "Bogus monitor %d\n", id );
- Usage();
- exit( 0 );
- }
+ if ( id < 0 )
+ {
+ fprintf( stderr, "Bogus monitor %d\n", id );
+ Usage();
+ exit( 0 );
+ }
- char log_id_string[16];
- snprintf( log_id_string, sizeof(log_id_string), "m%d", id );
+ char log_id_string[16];
+ snprintf( log_id_string, sizeof(log_id_string), "m%d", id );
- zmLoadConfig();
+ zmLoadConfig();
- logInit( "zmf" );
-
- ssedetect();
+ logInit( "zmf" );
+
+ hwcaps_detect();
- Monitor *monitor = Monitor::Load( id, false, Monitor::QUERY );
+ Monitor *monitor = Monitor::Load( id, false, Monitor::QUERY );
- if ( !monitor )
- {
- fprintf( stderr, "Can't find monitor with id of %d\n", id );
- exit( -1 );
- }
- Storage *Storage = monitor->getStorage();
+ if ( !monitor )
+ {
+ fprintf( stderr, "Can't find monitor with id of %d\n", id );
+ exit( -1 );
+ }
- char capt_path[PATH_MAX];
- char anal_path[PATH_MAX];
- snprintf( capt_path, sizeof(capt_path), "%s/%d/%%s/%%0%dd-capture.jpg", Storage->Path(), monitor->Id(), config.event_image_digits );
- snprintf( anal_path, sizeof(anal_path), "%s/%d/%%s/%%0%dd-analyse.jpg", Storage->Path(), monitor->Id(), config.event_image_digits );
- zmSetDefaultTermHandler();
- zmSetDefaultDieHandler();
+ Storage *Storage = monitor->getStorage();
- sigset_t block_set;
- sigemptyset( &block_set );
+ char capt_path[PATH_MAX];
+ char anal_path[PATH_MAX];
+ snprintf( capt_path, sizeof(capt_path), "%s/%d/%%s/%%0%dd-capture.jpg", Storage->Path(), monitor->Id(), config.event_image_digits );
+ snprintf( anal_path, sizeof(anal_path), "%s/%d/%%s/%%0%dd-analyse.jpg", Storage->Path(), monitor->Id(), config.event_image_digits );
+ zmSetDefaultTermHandler();
+ zmSetDefaultDieHandler();
- int sd = OpenSocket( monitor->Id() );
+ sigset_t block_set;
+ sigemptyset( &block_set );
- FrameHeader frame_header = { 0, 0, false, 0 };
- //unsigned char *image_data = 0;
+ int sd = OpenSocket( monitor->Id() );
- fd_set rfds;
+ FrameHeader frame_header = { 0, 0, false, 0 };
+ //unsigned char *image_data = 0;
- struct timeval timeout;
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
- while( 1 )
- {
- struct timeval temp_timeout = timeout;
+ fd_set rfds;
- FD_ZERO(&rfds);
- FD_SET(sd, &rfds);
- int n_found = select( sd+1, &rfds, NULL, NULL, &temp_timeout );
- if( n_found == 0 )
- {
- Debug( 1, "Select timed out" );
- continue;
- }
- else if ( n_found < 0)
- {
- Error( "Select error: %s", strerror(errno) );
- ReopenSocket( sd, monitor->Id() );
- continue;
- }
+ struct timeval timeout;
+ timeout.tv_sec = 1;
+ timeout.tv_usec = 0;
+ while( 1 )
+ {
+ struct timeval temp_timeout = timeout;
- sigprocmask( SIG_BLOCK, &block_set, 0 );
+ FD_ZERO(&rfds);
+ FD_SET(sd, &rfds);
+ int n_found = select( sd+1, &rfds, NULL, NULL, &temp_timeout );
+ if( n_found == 0 )
+ {
+ Debug( 1, "Select timed out" );
+ continue;
+ }
+ else if ( n_found < 0)
+ {
+ Error( "Select error: %s", strerror(errno) );
+ ReopenSocket( sd, monitor->Id() );
+ continue;
+ }
- int n_bytes = read( sd, &frame_header, sizeof(frame_header) );
- if ( n_bytes != sizeof(frame_header) )
- {
- if ( n_bytes < 0 )
- {
- Error( "Can't read frame header: %s", strerror(errno) );
- }
- else if ( n_bytes > 0 )
- {
- Error( "Incomplete read of frame header, %d bytes only", n_bytes );
- }
- else
- {
- Warning( "Socket closed at remote end" );
- }
- ReopenSocket( sd, monitor->Id() );
- continue;
- }
- Debug( 1, "Read frame header, expecting %ld bytes of image", frame_header.image_length );
- static unsigned char image_data[ZM_MAX_IMAGE_SIZE];
+ sigprocmask( SIG_BLOCK, &block_set, 0 );
- // Read for pipe and loop until bytes expected have been read or an error occurs
- int bytes_read = 0;
- do
- {
- n_bytes = read( sd, image_data+bytes_read, frame_header.image_length-bytes_read );
- if (n_bytes < 0) break; // break on error
- if (n_bytes < (int)frame_header.image_length)
- {
- // print some informational messages
- if (bytes_read == 0)
- {
- Debug(4,"Image read : Short read %d bytes of %d expected bytes",n_bytes,frame_header.image_length);
+ int n_bytes = read( sd, &frame_header, sizeof(frame_header) );
+ if ( n_bytes != sizeof(frame_header) )
+ {
+ if ( n_bytes < 0 )
+ {
+ Error( "Can't read frame header: %s", strerror(errno) );
+ }
+ else if ( n_bytes > 0 )
+ {
+ Error( "Incomplete read of frame header, %d bytes only", n_bytes );
+ }
+ else
+ {
+ Warning( "Socket closed at remote end" );
+ }
+ ReopenSocket( sd, monitor->Id() );
+ continue;
+ }
+ Debug( 1, "Read frame header, expecting %ld bytes of image", frame_header.image_length );
+ static unsigned char image_data[ZM_MAX_IMAGE_SIZE];
+
+ // Read for pipe and loop until bytes expected have been read or an error occurs
+ int bytes_read = 0;
+ do
+ {
+ n_bytes = read( sd, image_data+bytes_read, frame_header.image_length-bytes_read );
+ if (n_bytes < 0) break; // break on error
+ if (n_bytes < (int)frame_header.image_length)
+ {
+ // print some informational messages
+ if (bytes_read == 0)
+ {
+ Debug(4,"Image read : Short read %d bytes of %d expected bytes",n_bytes,frame_header.image_length);
}
- else if (bytes_read+n_bytes == (int)frame_header.image_length)
+ else if (bytes_read+n_bytes == (int)frame_header.image_length)
{
Debug(5,"Image read : Read rest of short read: %d bytes read total of %d bytes",n_bytes,frame_header.image_length);
}
@@ -296,13 +297,14 @@ int main( int argc, char *argv[] )
{
Debug(6,"Image read : continuing, read %d bytes (%d so far)", n_bytes, bytes_read+n_bytes);
}
- }
+ }
bytes_read+= n_bytes;
} while (n_bytes>0 && (bytes_read < (ssize_t)frame_header.image_length) );
// Print errors if there was a problem
if ( n_bytes < 1 )
{
+
Error( "Only read %d bytes of %d\n", bytes_read, frame_header.image_length);
if ( n_bytes < 0 )
{
diff --git a/src/zms.cpp b/src/zms.cpp
index ad7a4ca92..f37b63ca1 100644
--- a/src/zms.cpp
+++ b/src/zms.cpp
@@ -25,6 +25,7 @@
#include "zm_user.h"
#include "zm_signal.h"
#include "zm_monitor.h"
+#include "zm_monitorstream.h"
bool ValidateAccess( User *user, int mon_id ) {
bool allowed = true;
@@ -66,9 +67,9 @@ int main( int argc, const char *argv[] )
unsigned int bitrate = 100000;
unsigned int ttl = 0;
EventStream::StreamMode replay = EventStream::MODE_SINGLE;
- char username[64] = "";
- char password[64] = "";
- char auth[64] = "";
+ std::string username;
+ std::string password;
+ char auth[64] = "";
unsigned int connkey = 0;
unsigned int playback_buffer = 0;
@@ -86,8 +87,8 @@ int main( int argc, const char *argv[] )
zmLoadConfig();
logInit( "zms" );
-
- ssedetect();
+
+ hwcaps_detect();
zmSetDefaultTermHandler();
zmSetDefaultDieHandler();
@@ -148,7 +149,7 @@ int main( int argc, const char *argv[] )
else if ( config.opt_use_auth ) {
if ( strcmp( config.auth_relay, "none" ) == 0 ) {
if ( !strcmp( name, "user" ) ) {
- strncpy( username, value, sizeof(username) );
+ username = UriDecode( value );
}
} else {
//if ( strcmp( config.auth_relay, "hashed" ) == 0 )
@@ -160,10 +161,12 @@ int main( int argc, const char *argv[] )
//else if ( strcmp( config.auth_relay, "plain" ) == 0 )
{
if ( !strcmp( name, "user" ) ) {
- strncpy( username, value, sizeof(username) );
+ username = UriDecode( value );
+ Debug( 1, "Have %s for username", username.c_str() );
}
if ( !strcmp( name, "pass" ) ) {
- strncpy( password, value, sizeof(password) );
+ password = UriDecode( value );
+ Debug( 1, "Have %s for password", password.c_str() );
}
}
}
@@ -175,20 +178,24 @@ int main( int argc, const char *argv[] )
User *user = 0;
if ( strcmp( config.auth_relay, "none" ) == 0 ) {
- if ( *username ) {
- user = zmLoadUser( username );
+ if ( username.length() ) {
+ user = zmLoadUser( username.c_str() );
}
} else {
//if ( strcmp( config.auth_relay, "hashed" ) == 0 )
{
if ( *auth ) {
user = zmLoadAuthUser( auth, config.auth_hash_ips );
+ } else {
+ Debug( 1, "Need both username and password" );
}
}
//else if ( strcmp( config.auth_relay, "plain" ) == 0 )
{
- if ( *username && *password ) {
- user = zmLoadUser( username, password );
+ if ( username.length() && password.length() ) {
+ user = zmLoadUser( username.c_str(), password.c_str() );
+ } else {
+ Debug( 1, "Need both username and password" );
}
}
} // auth is none or something else
diff --git a/src/zmstreamer.cpp b/src/zmstreamer.cpp
index 74cc01226..3a4e74d9f 100644
--- a/src/zmstreamer.cpp
+++ b/src/zmstreamer.cpp
@@ -200,7 +200,7 @@ int main(int argc, char** argv) {
logInit("zmstreamer");
- ssedetect();
+ hwcaps_detect();
// Setting stream parameters
MonitorStream stream;
diff --git a/utils/do_debian_package.sh b/utils/do_debian_package.sh
index 2b649a3a1..84bd7acf0 100755
--- a/utils/do_debian_package.sh
+++ b/utils/do_debian_package.sh
@@ -87,12 +87,12 @@ else
echo "Defaulting to master branch";
BRANCH="master";
fi;
+ if [ "$SNAPSHOT" == "NOW" ]; then
+ SNAPSHOT=`date +%Y%m%d%H%M%S`;
+ fi;
fi;
fi
-if [ "$URGENCY" = "" ]; then
- URGENCY="medium"
-fi;
# Instead of cloning from github each time, if we have a fork lying around, update it and pull from there instead.
if [ ! -d "${GITHUB_FORK}_zoneminder_release" ]; then
@@ -139,10 +139,10 @@ cd "$DIRECTORY.orig";
git submodule init
git submodule update --init --recursive
-if [ $DISTRO == "trusty" ] || [ $DISTRO == "precise" ]; then
+if [ "$DISTRO" == "trusty" ] || [ "$DISTRO" == "precise" ]; then
ln -sf distros/ubuntu1204 debian
else
- if [ $DISTRO == "wheezy" ]; then
+ if [ "$DISTRO" == "wheezy" ]; then
ln -sf distros/debian debian
else
ln -sf distros/ubuntu1604 debian
@@ -159,6 +159,10 @@ else
fi
fi
+if [ "$URGENCY" = "" ]; then
+ URGENCY="medium"
+fi;
+
if [ "$SNAPSHOT" == "stable" ]; then
cat < debian/changelog
zoneminder ($VERSION-$DISTRO${PACKAGE_VERSION}) $DISTRO; urgency=$URGENCY
@@ -182,6 +186,7 @@ fi;
# Auto-install all ZoneMinder's depedencies using the Debian control file
sudo apt-get install devscripts equivs
sudo mk-build-deps -ir ./debian/control
+echo "Status: $?"
#rm -rf .git
#rm .gitignore
@@ -201,6 +206,7 @@ if [ "$DEBSIGN_KEYID" != "" ]; then
DEBUILD="$DEBUILD -k$DEBSIGN_KEYID"
fi
$DEBUILD
+echo "Status: $?"
cd ../
if [ "$INTERACTIVE" != "no" ]; then
@@ -222,7 +228,7 @@ if [ $TYPE == "binary" ]; then
echo "Do you want to upload this binary to zmrepo? (y/N)"
read install
if [ "$install" == "Y" ]; then
- scp "zoneminder_*-${VERSION}-${DISTRO}*" "zmrepo.connortechnology.com:zmrepo/debian-${BRANCH}/"
+ scp "zoneminder_*-${VERSION}-${DISTRO}*" "zmrepo@zmrepo.connortechnology.com:debian/${BRANCH}/mini-dinstall/incoming/"
fi;
fi;
fi;
diff --git a/utils/packpack/deb.mk.patch b/utils/packpack/deb.mk.patch
new file mode 100644
index 000000000..0cf0100f2
--- /dev/null
+++ b/utils/packpack/deb.mk.patch
@@ -0,0 +1,11 @@
+--- a/packpack/pack/deb.mk 2017-01-15 16:41:32.938418279 -0600
++++ b/packpack/pack/deb.mk 2017-02-16 15:44:43.267900717 -0600
+@@ -14,7 +14,7 @@
+ DPKG_BUILD:=$(PRODUCT)_$(DEB_VERSION)-$(RELEASE)_$(DPKG_ARCH).build
+ DPKG_DSC:=$(PRODUCT)_$(DEB_VERSION)-$(RELEASE).dsc
+ DPKG_ORIG_TARBALL:=$(PRODUCT)_$(DEB_VERSION).orig.tar.$(TARBALL_COMPRESSOR)
+-DPKG_DEBIAN_TARBALL:=$(PRODUCT)_$(DEB_VERSION)-$(RELEASE).debian.tar.$(TARBALL_COMPRESSOR)
++DPKG_DEBIAN_TARBALL:=$(PRODUCT)_$(DEB_VERSION)-$(RELEASE).tar.$(TARBALL_COMPRESSOR)
+
+ # gh-7: Ubuntu/Debian should export DEBIAN_FRONTEND=noninteractive
+ export DEBIAN_FRONTEND=noninteractive
diff --git a/utils/packpack/startpackpack.sh b/utils/packpack/startpackpack.sh
index e3933a9b6..117f1c1a9 100755
--- a/utils/packpack/startpackpack.sh
+++ b/utils/packpack/startpackpack.sh
@@ -52,6 +52,12 @@ if [ "${OS}" == "el" ] || [ "${OS}" == "fedora" ]; then
#patch -p1 < utils/packpack/autosetup.patch
ln -sf distros/redhat rpm
+ # The rpm specfile requires the Crud submodule folder to be empty
+ if [ -e "web/api/app/Plugin/Crud/LICENSE.txt" ]; then
+ rm -rf web/api/app/Plugin/Crud
+ mkdir web/api/app/Plugin/Crud
+ fi
+
if [ "${OS}" == "el" ]; then
zmrepodistro=${OS}
else
@@ -80,6 +86,12 @@ if [ "${OS}" == "el" ] || [ "${OS}" == "fedora" ]; then
elif [ "${OS}" == "debian" ] || [ "${OS}" == "ubuntu" ]; then
echo "Begin Debian build..."
+ # patch packpack to remove "debian" from the source tarball filename
+ patch --dry-run --silent -f -p1 < utils/packpack/deb.mk.patch 2>/dev/null
+ if [ $? -eq 0 ]; then
+ patch -p1 < utils/packpack/deb.mk.patch
+ fi
+
# Uncompress the Crud tarball and move it into place
if [ -e "web/api/app/Plugin/Crud/LICENSE.txt" ]; then
echo "Crud plugin already installed..."
diff --git a/web/ajax/stream.php b/web/ajax/stream.php
index 4e3a263c9..463558640 100644
--- a/web/ajax/stream.php
+++ b/web/ajax/stream.php
@@ -113,11 +113,13 @@ switch ( $data['type'] )
$data['delay'] = round( $data['delay'], 2 );
$data['zoom'] = round( $data['zoom']/SCALE_BASE, 1 );
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == "hashed" ) {
+ session_start();
$time = time();
// Regenerate auth hash after half the lifetime of the hash
if ( $_SESSION['AuthHashGeneratedAt'] < $time - (ZM_AUTH_HASH_TTL * 1800) ) {
$data['auth'] = generateAuthHash( ZM_AUTH_HASH_IPS );
}
+ session_write_close();
}
ajaxResponse( array( 'status'=>$data ) );
break;
@@ -129,11 +131,13 @@ switch ( $data['type'] )
$data['rate'] /= RATE_BASE;
$data['zoom'] = round( $data['zoom']/SCALE_BASE, 1 );
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == "hashed" ) {
+ session_start();
$time = time();
// Regenerate auth hash after half the lifetime of the hash
if ( $_SESSION['AuthHashGeneratedAt'] < $time - (ZM_AUTH_HASH_TTL * 1800) ) {
$data['auth'] = generateAuthHash( ZM_AUTH_HASH_IPS );
}
+ session_write_close();
}
ajaxResponse( array( 'status'=>$data ) );
break;
diff --git a/web/api/app/Config/core.php.default b/web/api/app/Config/core.php.default
index 43736a61f..a210fbd79 100644
--- a/web/api/app/Config/core.php.default
+++ b/web/api/app/Config/core.php.default
@@ -31,7 +31,7 @@
* In production mode, flash messages redirect after a time interval.
* In development mode, you need to click the flash message to continue.
*/
- Configure::write('debug', 2);
+ Configure::write('debug', 0);
/**
* Configure the Error handler used to handle errors for your application. By default
diff --git a/web/api/app/Controller/AppController.php b/web/api/app/Controller/AppController.php
index 6775fa846..bfb20aa49 100644
--- a/web/api/app/Controller/AppController.php
+++ b/web/api/app/Controller/AppController.php
@@ -91,7 +91,7 @@ class AppController extends Controller {
if( ! $this->Session->Read('user.Username') ) {
throw new UnauthorizedException(__('Not Authenticated'));
return;
- } else if ( ! $this->Session->Read('user.Username') ) {
+ } else if ( ! $this->Session->Read('user.Enabled') ) {
throw new UnauthorizedException(__('User is not enabled'));
return;
}
diff --git a/web/includes/Event.php b/web/includes/Event.php
index e9ecd4bae..11a8f4faa 100644
--- a/web/includes/Event.php
+++ b/web/includes/Event.php
@@ -196,7 +196,7 @@ class Event {
} // end function createListThumbnail
function getImageSrc( $frame, $scale=SCALE_BASE, $captureOnly=false, $overwrite=false ) {
- $Storage = new Storage( $this->{'StorageId'} );
+ $Storage = new Storage( isset($this->{'StorageId'}) ? $this->{'StorageId'} : NULL );
$Event = $this;
$eventPath = $Event->Path();
diff --git a/web/includes/Monitor.php b/web/includes/Monitor.php
index 4dd2e6a2a..9e4c6a95a 100644
--- a/web/includes/Monitor.php
+++ b/web/includes/Monitor.php
@@ -143,7 +143,7 @@ private $control_fields = array(
}
} else {
- Error("No row for Monitor " . $IdOrRow );
+ Error('No row for Monitor ' . $IdOrRow );
}
} # end if isset($IdOrRow)
} // end function __construct
@@ -170,6 +170,7 @@ private $control_fields = array(
}
}
}
+
public function getStreamSrc( $args, $querySep='&' ) {
if ( isset($this->{'ServerId'}) and $this->{'ServerId'} ) {
$Server = new Server( $this->{'ServerId'} );
@@ -178,27 +179,27 @@ private $control_fields = array(
$streamSrc = ZM_BASE_URL.ZM_PATH_ZMS;
}
- $args[] = "monitor=".$this->{'Id'};
+ $args['monitor'] = $this->{'Id'};
if ( ZM_OPT_USE_AUTH ) {
- if ( ZM_AUTH_RELAY == "hashed" ) {
- $args[] = "auth=".generateAuthHash( ZM_AUTH_HASH_IPS );
- } elseif ( ZM_AUTH_RELAY == "plain" ) {
- $args[] = "user=".$_SESSION['username'];
- $args[] = "pass=".$_SESSION['password'];
- } elseif ( ZM_AUTH_RELAY == "none" ) {
- $args[] = "user=".$_SESSION['username'];
+ if ( ZM_AUTH_RELAY == 'hashed' ) {
+ $args['auth'] = generateAuthHash( ZM_AUTH_HASH_IPS );
+ } elseif ( ZM_AUTH_RELAY == 'plain' ) {
+ $args['user'] = $_SESSION['username'];
+ $args['pass'] = $_SESSION['password'];
+ } elseif ( ZM_AUTH_RELAY == 'none' ) {
+ $args['user'] = $_SESSION['username'];
}
}
- if ( !in_array( "mode=single", $args ) && !empty($GLOBALS['connkey']) ) {
- $args[] = "connkey=".$GLOBALS['connkey'];
+ if ( ( (!isset($args['mode'])) or ( $args['mode'] != 'single' ) ) && !empty($GLOBALS['connkey']) ) {
+ $args['connkey'] = $GLOBALS['connkey'];
}
if ( ZM_RAND_STREAM ) {
- $args[] = "rand=".time();
+ $args['rand'] = time();
}
if ( count($args) ) {
- $streamSrc .= "?".join( $querySep, $args );
+ $streamSrc .= '?'.http_build_query( $args,'', $querySep );
}
return( $streamSrc );
diff --git a/web/includes/actions.php b/web/includes/actions.php
index 1f0e0ec47..d306c3bbc 100644
--- a/web/includes/actions.php
+++ b/web/includes/actions.php
@@ -103,6 +103,7 @@ if ( !empty($action) ) {
userLogin( $username, $password );
$refreshParent = true;
$view = 'console';
+ $redirect = true;
} else if ( $action == 'logout' ) {
userLogout();
$refreshParent = true;
@@ -133,37 +134,50 @@ if ( !empty($action) ) {
$filterName = $_REQUEST['newFilterName'];
}
if ( $filterName ) {
- $sql = "REPLACE INTO Filters SET Name = ".dbEscape($filterName).",";
+ # Replace will teplace any filter with the same Id
+ # Since we aren't specifying the Id , this is effectively an insert
+ $sql = 'REPLACE INTO Filters SET Name = '.dbEscape($filterName).',';
} else {
$sql = 'UPDATE Filters SET';
- $endSql = "where Id = ".$_REQUEST['Id'];
+ $endSql = 'WHERE Id = '.$_REQUEST['Id'];
}
+
+ # endSql is only set if ! filterName... so... woulnd't this always be true
if ( !empty($filterName) || $endSql ) {
$_REQUEST['filter']['sort_field'] = validStr($_REQUEST['sort_field']);
$_REQUEST['filter']['sort_asc'] = validStr($_REQUEST['sort_asc']);
$_REQUEST['filter']['limit'] = validInt($_REQUEST['limit']);
- $sql .= " Query = ".dbEscape(jsonEncode($_REQUEST['filter']));
+ $sql .= ' Query = '.dbEscape(jsonEncode($_REQUEST['filter']));
if ( !empty($_REQUEST['AutoArchive']) )
- $sql .= ", AutoArchive = ".dbEscape($_REQUEST['AutoArchive']);
+ $sql .= ', AutoArchive = '.dbEscape($_REQUEST['AutoArchive']);
if ( !empty($_REQUEST['AutoVideo']) )
- $sql .= ", AutoVideo = ".dbEscape($_REQUEST['AutoVideo']);
+ $sql .= ', AutoVideo = '.dbEscape($_REQUEST['AutoVideo']);
if ( !empty($_REQUEST['AutoUpload']) )
- $sql .= ", AutoUpload = ".dbEscape($_REQUEST['AutoUpload']);
+ $sql .= ', AutoUpload = '.dbEscape($_REQUEST['AutoUpload']);
if ( !empty($_REQUEST['AutoEmail']) )
- $sql .= ", AutoEmail = ".dbEscape($_REQUEST['AutoEmail']);
+ $sql .= ', AutoEmail = '.dbEscape($_REQUEST['AutoEmail']);
if ( !empty($_REQUEST['AutoMessage']) )
- $sql .= ", AutoMessage = ".dbEscape($_REQUEST['AutoMessage']);
+ $sql .= ', AutoMessage = '.dbEscape($_REQUEST['AutoMessage']);
if ( !empty($_REQUEST['AutoExecute']) && !empty($_REQUEST['AutoExecuteCmd']) )
- $sql .= ", AutoExecute = ".dbEscape($_REQUEST['AutoExecute']).", AutoExecuteCmd = ".dbEscape($_REQUEST['AutoExecuteCmd']);
+ $sql .= ', AutoExecute = '.dbEscape($_REQUEST['AutoExecute']).", AutoExecuteCmd = ".dbEscape($_REQUEST['AutoExecuteCmd']);
if ( !empty($_REQUEST['AutoDelete']) )
- $sql .= ", AutoDelete = ".dbEscape($_REQUEST['AutoDelete']);
+ $sql .= ', AutoDelete = '.dbEscape($_REQUEST['AutoDelete']);
if ( !empty($_REQUEST['background']) )
- $sql .= ", Background = ".dbEscape($_REQUEST['background']);
+ $sql .= ', Background = '.dbEscape($_REQUEST['background']);
if ( !empty($_REQUEST['concurrent']) )
- $sql .= ", Concurrent = ".dbEscape($_REQUEST['concurrent']);
+ $sql .= ', Concurrent = '.dbEscape($_REQUEST['concurrent']);
$sql .= $endSql;
dbQuery( $sql );
- $refreshParent = true;
+ if ( $filterName ) {
+ $filter = dbFetchOne( 'SELECT * FROM Filters WHERE Name=?', NULL, array($filterName) );
+ if ( $filter ) {
+ # This won't work yet because refreshparent refreshes the old filter. Need to do a redirect instead of a refresh.
+ $_REQUEST['Id'] = $filter['Id'];
+ } else {
+ Error("No new Id despite new name");
+ }
+ }
+ $refreshParent = '/index.php?view=filter&Id='.$_REQUEST['Id'];
}
} // end if canedit events
} // end if action == filter
@@ -512,7 +526,7 @@ if ( !empty($action) ) {
dbQuery( "update TriggersX10 set ".implode( ", ", $x10Changes )." where MonitorId=?", array($mid) );
} elseif ( !$user['MonitorIds'] ) {
if ( !$x10Monitor ) {
- dbQuery( "insert into TriggersX10 set MonitorId = ?".implode( ", ", $x10Changes ), array( $mid ) );
+ dbQuery( "insert into TriggersX10 set MonitorId = ?, ".implode( ", ", $x10Changes ), array( $mid ) );
} else {
dbQuery( "delete from TriggersX10 where MonitorId = ?", array($mid) );
}
@@ -822,12 +836,13 @@ if ( !empty($action) ) {
if ( count( $changes ) ) {
if ( !empty($_REQUEST['uid']) ) {
dbQuery( "update Users set ".implode( ", ", $changes )." where Id = ?", array($_REQUEST['uid']) );
+ # If we are updating the logged in user, then update our session user data.
+ if ( $user and ( $dbUser['Username'] == $user['Username'] ) )
+ userLogin( $dbUser['Username'], $dbUser['Password'] );
} else {
dbQuery( "insert into Users set ".implode( ", ", $changes ) );
}
$refreshParent = true;
- if ( $dbUser['Username'] == $user['Username'] )
- userLogin( $dbUser['Username'], $dbUser['Password'] );
}
$view = 'none';
} elseif ( $action == 'state' ) {
diff --git a/web/includes/csrf/LICENSE.txt b/web/includes/csrf/LICENSE.txt
new file mode 100644
index 000000000..37b07717d
--- /dev/null
+++ b/web/includes/csrf/LICENSE.txt
@@ -0,0 +1,9 @@
+Copyright (c) 2008-2013, Edward Z. Yang
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/web/includes/csrf/NEWS.txt b/web/includes/csrf/NEWS.txt
new file mode 100644
index 000000000..66d52f6da
--- /dev/null
+++ b/web/includes/csrf/NEWS.txt
@@ -0,0 +1,69 @@
+
+ [[ news ]]
+
+1.0.4 released 2013-07-17
+
+ [SECURITY FIXES]
+
+ - When secret key was not explicitly set, it was not being used
+ by the csrf_hash() function. Thanks sparticvs for reporting.
+
+ [FEATURES]
+
+ - The default 'CSRF check failed' page now offers a handy 'Try
+ again' button, which resubmits the form.
+
+ [BUG FIXES]
+
+ - The fix for 1.0.3 inadvertantly turned off XMLHttpRequest
+ overloading for all browsers; it has now been fixed to only
+ apply to IE.
+
+1.0.3 released 2012-01-31
+
+ [BUG FIXES]
+
+ - Internet Explorer 8 adds support for XMLHttpRequest.prototype,
+ but this support is broken for method overloading. We
+ explicitly disable JavaScript overloading for Internet Explorer.
+ Thanks Kelly Lu for reporting.
+
+ - A global declaration was omitted, resulting in a variable
+ not being properly introduced in PHP 5.3. Thanks Whitney Beck for
+ reporting.
+
+1.0.2 released 2009-03-08
+
+ [SECURITY FIXES]
+
+ - Due to a typo, csrf-magic accidentally treated the secret key
+ as always present. This means that there was a possible CSRF
+ attack against users without any cookies. No attacks in the
+ wild were known at the time of this release. Thanks Jakub
+ Vrána for reporting.
+
+1.0.1 released 2008-11-02
+
+ [NEW FEATURES]
+
+ - Support for composite tokens; this also fixes a bug with using
+ IP-based tokens for users with cookies disabled.
+
+ - Native support cookie tokens; use csrf_conf('cookie', $name) to
+ specify the name of a cookie that the CSRF token should be
+ placed in. This is useful if you have a Squid cache, and need
+ to configure it to ignore this token.
+
+ - Tips/tricks section in README.txt.
+
+ - There is now a two hour expiration time on all tokens. This
+ can be modified using csrf_conf('expires', $seconds).
+
+ - ClickJacking protection using an iframe breaker. Disable with
+ csrf_conf('frame-breaker', false).
+
+ [BUG FIXES]
+
+ - CsrfMagic.send() incorrectly submitted GET requests twice,
+ once without the magic token and once with the token. Reported
+ by Kelly Lu .
diff --git a/web/includes/csrf/README.txt b/web/includes/csrf/README.txt
new file mode 100644
index 000000000..98d225dba
--- /dev/null
+++ b/web/includes/csrf/README.txt
@@ -0,0 +1,160 @@
+
+ [[ csrf-magic ]]
+
+Add the following line to the top of all web-accessible PHP pages. If you have
+a common file included by everything, put it there.
+
+ include_once '/path/to/csrf-magic.php';
+
+Do it, test it, then forget about it. csrf-magic is protecting you if nothing
+bad happens. Read on if you run into problems.
+
+
+ TABLE OF CONTENTS
+ + ------------------- +
+ 1. TIPS AND TRICKS
+ 2. AJAX
+ 3. CONFIGURE
+ 4. THANKS
+ 5. FOOTNOTES
+ + ------------------- +
+
+
+1. TIPS AND TRICKS
+
+ * If your JavaScript and AJAX is persistently getting errors, check the
+ AJAX section below on how to fix.
+
+ * The CSS overlay protection makes it impossible to display your website
+ in frame/iframe elements. You can disable it with
+ csrf_conf('frame-breaker', false) in your csrf_startup() function.
+
+ * csrf-magic will start a session. To disable, use csrf_conf('auto-session',
+ false) in your csrf_startup() function.
+
+ * The default error message is a little user unfriendly. Write your own
+ function which outputs an error message and set csrf_conf('callback',
+ 'myCallbackFunction') in your csrf_startup() function.
+
+ * Make sure csrf_conf('secret', 'ABCDEFG') has something random in it. If
+ the directory csrf-magic.php is in is writable, csrf-magic will generate
+ a secret key for you in the csrf-secret.php file.
+
+ * Remember you can use auto_prepend to include csrf-magic.php on all your
+ pages. You may want to create a stub file which you can include that
+ includes csrf-magic.php as well as performs configuration.
+
+ * The default expiration time for tokens is two hours. If you expect your
+ users to need longer to fill out forms, be sure to enable double
+ submission when the token is invalid.
+
+
+2. AJAX
+
+csrf-magic has the ability to dynamically rewrite AJAX requests which use
+XMLHttpRequest. However, due to the invasiveness of this procedure, it is
+not enabled by default. You can enable it by adding this code before you
+include csrf-magic.php.
+
+ function csrf_startup() {
+ csrf_conf('rewrite-js', '/web/path/to/csrf-magic.js');
+ }
+ // include_once '/path/to/csrf-magic.php';
+
+(Be sure to place csrf-magic.js somewhere web accessible).
+
+The default method CSRF Magic uses to rewrite AJAX requests will
+only work for browsers with support for XmlHttpRequest.prototype (this excludes
+all versions of Internet Explorer). See this page for more information:
+http://stackoverflow.com/questions/664315/internet-explorer-8-prototypes-and-xmlhttprequest
+
+However, csrf-magic.js will
+automatically detect and play nice with the following JavaScript frameworks:
+
+ * jQuery
+ * Prototype
+ * MooTools
+ * Ext
+ * Dojo
+
+(Note 2013-07-16: It has been a long time since this manual support has
+been updated, and some JavaScript libraries have placed their copies of XHR
+in local variables in closures, which makes it difficult for us to monkey-patch
+it in automatically.)
+
+To rewrite your own JavaScript library to use csrf-magic.js, you should modify
+your function that generates XMLHttpRequest to have this at the end:
+
+ return new CsrfMagic(xhrObject);
+
+With whatever xhrObject may be. If you have literal instances of XMLHttpRequest
+in your code, find and replace ''new XMLHttpRequest'' with ''new CsrfMagic''
+(CsrfMagic will automatically instantiate an XMLHttpRequest object in a
+cross-platform manner as necessary).
+
+If you don't want csrf-magic monkeying around with your XMLHttpRequest object,
+you can manually rewrite your AJAX code to include the variable. The important
+information is stored in the global variables csrfMagicName and csrfMagicToken.
+CsrfMagic.process may also be of interest, as it takes one parameter, a
+querystring, and prepends the CSRF token to the value.
+
+
+3. CONFIGURE
+
+csrf-magic has some configuration options that you can set inside the
+csrf_startup() function. They are described in csrf-magic.php, and you can
+set them using the convenience function csrf_conf($name, $value).
+
+For example, this is a recommended configuration:
+
+ /**
+ * This is a function that gets called if a csrf check fails. csrf-magic will
+ * then exit afterwards.
+ */
+ function my_csrf_callback() {
+ echo "You're doing bad things young man!";
+ }
+
+ function csrf_startup() {
+
+ // While csrf-magic has a handy little heuristic for determining whether
+ // or not the content in the buffer is HTML or not, you should really
+ // give it a nudge and turn rewriting *off* when the content is
+ // not HTML. Implementation details will vary.
+ if (isset($_POST['ajax'])) csrf_conf('rewrite', false);
+
+ // This is a secret value that must be set in order to enable username
+ // and IP based checks. Don't show this to anyone. A secret id will
+ // automatically be generated for you if the directory csrf-magic.php
+ // is placed in is writable.
+ csrf_conf('secret', 'ABCDEFG123456');
+
+ // This enables JavaScript rewriting and will ensure your AJAX calls
+ // don't stop working.
+ csrf_conf('rewrite-js', '/csrf-magic.js');
+
+ // This makes csrf-magic call my_csrf_callback() before exiting when
+ // there is a bad csrf token. This lets me customize the error page.
+ csrf_conf('callback', 'my_csrf_callback');
+
+ // While this is enabled by default to boost backwards compatibility,
+ // for security purposes it should ideally be off. Some users can be
+ // NATted or have dialup addresses which rotate frequently. Cookies
+ // are much more reliable.
+ csrf_conf('allow-ip', false);
+
+ }
+
+ // Finally, include the library
+ include_once '/path/to/csrf-magic.php';
+
+Configuration gets stored in the $GLOBALS['csrf'] array.
+
+
+4. THANKS
+
+My thanks to Chris Shiflett, for unintentionally inspiring the idea, as well
+as telling me the original variant of the Bob and Mallory story,
+and the Django CSRF Middleware authors, who thought up of this before me.
+Gareth Heyes suggested using the frame-breaker option to protect against
+CSS overlay attacks.
diff --git a/web/includes/csrf/csrf-magic.js b/web/includes/csrf/csrf-magic.js
new file mode 100644
index 000000000..0989c1065
--- /dev/null
+++ b/web/includes/csrf/csrf-magic.js
@@ -0,0 +1,191 @@
+/**
+ * @file
+ *
+ * Rewrites XMLHttpRequest to automatically send CSRF token with it. In theory
+ * plays nice with other JavaScript libraries, needs testing though.
+ */
+
+// Here are the basic overloaded method definitions
+// The wrapper must be set BEFORE onreadystatechange is written to, since
+// a bug in ActiveXObject prevents us from properly testing for it.
+CsrfMagic = function(real) {
+ // try to make it ourselves, if you didn't pass it
+ if (!real) try { real = new XMLHttpRequest; } catch (e) {;}
+ if (!real) try { real = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {;}
+ if (!real) try { real = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {;}
+ if (!real) try { real = new ActiveXObject('Msxml2.XMLHTTP.4.0'); } catch (e) {;}
+ this.csrf = real;
+ // properties
+ var csrfMagic = this;
+ real.onreadystatechange = function() {
+ csrfMagic._updateProps();
+ return csrfMagic.onreadystatechange ? csrfMagic.onreadystatechange() : null;
+ };
+ csrfMagic._updateProps();
+}
+
+CsrfMagic.prototype = {
+
+ open: function(method, url, async, username, password) {
+ if (method == 'POST') this.csrf_isPost = true;
+ // deal with Opera bug, thanks jQuery
+ if (username) return this.csrf_open(method, url, async, username, password);
+ else return this.csrf_open(method, url, async);
+ },
+ csrf_open: function(method, url, async, username, password) {
+ if (username) return this.csrf.open(method, url, async, username, password);
+ else return this.csrf.open(method, url, async);
+ },
+
+ send: function(data) {
+ if (!this.csrf_isPost) return this.csrf_send(data);
+ prepend = csrfMagicName + '=' + csrfMagicToken + '&';
+ // XXX: Removed to eliminate 'Refused to set unsafe header "Content-length" ' errors in modern browsers
+ // if (this.csrf_purportedLength === undefined) {
+ // this.csrf_setRequestHeader("Content-length", this.csrf_purportedLength + prepend.length);
+ // delete this.csrf_purportedLength;
+ // }
+ delete this.csrf_isPost;
+ return this.csrf_send(prepend + data);
+ },
+ csrf_send: function(data) {
+ return this.csrf.send(data);
+ },
+
+ setRequestHeader: function(header, value) {
+ // We have to auto-set this at the end, since we don't know how long the
+ // nonce is when added to the data.
+ if (this.csrf_isPost && header == "Content-length") {
+ this.csrf_purportedLength = value;
+ return;
+ }
+ return this.csrf_setRequestHeader(header, value);
+ },
+ csrf_setRequestHeader: function(header, value) {
+ return this.csrf.setRequestHeader(header, value);
+ },
+
+ abort: function() {
+ return this.csrf.abort();
+ },
+ getAllResponseHeaders: function() {
+ return this.csrf.getAllResponseHeaders();
+ },
+ getResponseHeader: function(header) {
+ return this.csrf.getResponseHeader(header);
+ } // ,
+}
+
+// proprietary
+CsrfMagic.prototype._updateProps = function() {
+ this.readyState = this.csrf.readyState;
+ if (this.readyState == 4) {
+ this.responseText = this.csrf.responseText;
+ this.responseXML = this.csrf.responseXML;
+ this.status = this.csrf.status;
+ this.statusText = this.csrf.statusText;
+ }
+}
+CsrfMagic.process = function(base) {
+ if(typeof base == 'object') {
+ base[csrfMagicName] = csrfMagicToken;
+ return base;
+ }
+ var prepend = csrfMagicName + '=' + csrfMagicToken;
+ if (base) return prepend + '&' + base;
+ return prepend;
+}
+// callback function for when everything on the page has loaded
+CsrfMagic.end = function() {
+ // This rewrites forms AGAIN, so in case buffering didn't work this
+ // certainly will.
+ forms = document.getElementsByTagName('form');
+ for (var i = 0; i < forms.length; i++) {
+ form = forms[i];
+ if (form.method.toUpperCase() !== 'POST') continue;
+ if (form.elements[csrfMagicName]) continue;
+ var input = document.createElement('input');
+ input.setAttribute('name', csrfMagicName);
+ input.setAttribute('value', csrfMagicToken);
+ input.setAttribute('type', 'hidden');
+ form.appendChild(input);
+ }
+}
+
+// Sets things up for Mozilla/Opera/nice browsers
+// We very specifically match against Internet Explorer, since they haven't
+// implemented prototypes correctly yet.
+if (window.XMLHttpRequest && window.XMLHttpRequest.prototype && '\v' != 'v') {
+ var x = XMLHttpRequest.prototype;
+ var c = CsrfMagic.prototype;
+
+ // Save the original functions
+ x.csrf_open = x.open;
+ x.csrf_send = x.send;
+ x.csrf_setRequestHeader = x.setRequestHeader;
+
+ // Notice that CsrfMagic is itself an instantiatable object, but only
+ // open, send and setRequestHeader are necessary as decorators.
+ x.open = c.open;
+ x.send = c.send;
+ x.setRequestHeader = c.setRequestHeader;
+} else {
+ // The only way we can do this is by modifying a library you have been
+ // using. We support YUI, script.aculo.us, prototype, MooTools,
+ // jQuery, Ext and Dojo.
+ if (window.jQuery) {
+ // jQuery didn't implement a new XMLHttpRequest function, so we have
+ // to do this the hard way.
+ jQuery.csrf_ajax = jQuery.ajax;
+ jQuery.ajax = function( s ) {
+ if (s.type && s.type.toUpperCase() == 'POST') {
+ s = jQuery.extend(true, s, jQuery.extend(true, {}, jQuery.ajaxSettings, s));
+ if ( s.data && s.processData && typeof s.data != "string" ) {
+ s.data = jQuery.param(s.data);
+ }
+ s.data = CsrfMagic.process(s.data);
+ }
+ return jQuery.csrf_ajax( s );
+ }
+ }
+ if (window.Prototype) {
+ // This works for script.aculo.us too
+ Ajax.csrf_getTransport = Ajax.getTransport;
+ Ajax.getTransport = function() {
+ return new CsrfMagic(Ajax.csrf_getTransport());
+ }
+ }
+ if (window.MooTools) {
+ Browser.csrf_Request = Browser.Request;
+ Browser.Request = function () {
+ return new CsrfMagic(Browser.csrf_Request());
+ }
+ }
+ if (window.YAHOO) {
+ // old YUI API
+ YAHOO.util.Connect.csrf_createXhrObject = YAHOO.util.Connect.createXhrObject;
+ YAHOO.util.Connect.createXhrObject = function (transaction) {
+ obj = YAHOO.util.Connect.csrf_createXhrObject(transaction);
+ obj.conn = new CsrfMagic(obj.conn);
+ return obj;
+ }
+ }
+ if (window.Ext) {
+ // Ext can use other js libraries as loaders, so it has to come last
+ // Ext's implementation is pretty identical to Yahoo's, but we duplicate
+ // it for comprehensiveness's sake.
+ Ext.lib.Ajax.csrf_createXhrObject = Ext.lib.Ajax.createXhrObject;
+ Ext.lib.Ajax.createXhrObject = function (transaction) {
+ obj = Ext.lib.Ajax.csrf_createXhrObject(transaction);
+ obj.conn = new CsrfMagic(obj.conn);
+ return obj;
+ }
+ }
+ if (window.dojo) {
+ // NOTE: this doesn't work with latest dojo
+ dojo.csrf__xhrObj = dojo._xhrObj;
+ dojo._xhrObj = function () {
+ return new CsrfMagic(dojo.csrf__xhrObj());
+ }
+ }
+}
diff --git a/web/includes/csrf/csrf-magic.php b/web/includes/csrf/csrf-magic.php
new file mode 100644
index 000000000..153417f4e
--- /dev/null
+++ b/web/includes/csrf/csrf-magic.php
@@ -0,0 +1,405 @@
+
+ */
+$GLOBALS['csrf']['input-name'] = '__csrf_magic';
+
+/**
+ * Set this to false if your site must work inside of frame/iframe elements,
+ * but do so at your own risk: this configuration protects you against CSS
+ * overlay attacks that defeat tokens.
+ */
+$GLOBALS['csrf']['frame-breaker'] = true;
+
+/**
+ * Whether or not CSRF Magic should be allowed to start a new session in order
+ * to determine the key.
+ */
+$GLOBALS['csrf']['auto-session'] = true;
+
+/**
+ * Whether or not csrf-magic should produce XHTML style tags.
+ */
+$GLOBALS['csrf']['xhtml'] = true;
+
+// FUNCTIONS:
+
+// Don't edit this!
+$GLOBALS['csrf']['version'] = '1.0.4';
+
+/**
+ * Rewrites
+ CSRF check failed. Your form session may have expired, or you may not have
+ cookies enabled.
+
+ Debug: $tokens
', $buffer, $count);
+ if (!$count) {
+ $buffer .= $script;
+ }
+ }
+ return $buffer;
+}
+
+/**
+ * Checks if this is a post request, and if it is, checks if the nonce is valid.
+ * @param bool $fatal Whether or not to fatally error out if there is a problem.
+ * @return True if check passes or is not necessary, false if failure.
+ */
+function csrf_check($fatal = true) {
+ if ($_SERVER['REQUEST_METHOD'] !== 'POST') return true;
+ csrf_start();
+ $name = $GLOBALS['csrf']['input-name'];
+ $ok = false;
+ $tokens = '';
+ do {
+ if (!isset($_POST[$name])) break;
+ // we don't regenerate a token and check it because some token creation
+ // schemes are volatile.
+ $tokens = $_POST[$name];
+ if (!csrf_check_tokens($tokens)) break;
+ $ok = true;
+ } while (false);
+ if ($fatal && !$ok) {
+ $callback = $GLOBALS['csrf']['callback'];
+ if (trim($tokens, 'A..Za..z0..9:;,') !== '') $tokens = 'hidden';
+ $callback($tokens);
+ exit;
+ }
+ return $ok;
+}
+
+/**
+ * Retrieves a valid token(s) for a particular context. Tokens are separated
+ * by semicolons.
+ */
+function csrf_get_tokens() {
+ $has_cookies = !empty($_COOKIE);
+
+ // $ip implements a composite key, which is sent if the user hasn't sent
+ // any cookies. It may or may not be used, depending on whether or not
+ // the cookies "stick"
+ $secret = csrf_get_secret();
+ if (!$has_cookies && $secret) {
+ // :TODO: Harden this against proxy-spoofing attacks
+ $IP_ADDRESS = (isset($_SERVER['IP_ADDRESS']) ? $_SERVER['IP_ADDRESS'] : $_SERVER['REMOTE_ADDR']);
+ $ip = ';ip:' . csrf_hash($IP_ADDRESS);
+ } else {
+ $ip = '';
+ }
+ csrf_start();
+
+ // These are "strong" algorithms that don't require per se a secret
+ if (session_id()) return 'sid:' . csrf_hash(session_id()) . $ip;
+ if ($GLOBALS['csrf']['cookie']) {
+ $val = csrf_generate_secret();
+ setcookie($GLOBALS['csrf']['cookie'], $val);
+ return 'cookie:' . csrf_hash($val) . $ip;
+ }
+ if ($GLOBALS['csrf']['key']) return 'key:' . csrf_hash($GLOBALS['csrf']['key']) . $ip;
+ // These further algorithms require a server-side secret
+ if (!$secret) return 'invalid';
+ if ($GLOBALS['csrf']['user'] !== false) {
+ return 'user:' . csrf_hash($GLOBALS['csrf']['user']);
+ }
+ if ($GLOBALS['csrf']['allow-ip']) {
+ return ltrim($ip, ';');
+ }
+ return 'invalid';
+}
+
+function csrf_flattenpost($data) {
+ $ret = array();
+ foreach($data as $n => $v) {
+ $ret = array_merge($ret, csrf_flattenpost2(1, $n, $v));
+ }
+ return $ret;
+}
+function csrf_flattenpost2($level, $key, $data) {
+ if(!is_array($data)) return array($key => $data);
+ $ret = array();
+ foreach($data as $n => $v) {
+ $nk = $level >= 1 ? $key."[$n]" : "[$n]";
+ $ret = array_merge($ret, csrf_flattenpost2($level+1, $nk, $v));
+ }
+ return $ret;
+}
+
+/**
+ * @param $tokens is safe for HTML consumption
+ */
+function csrf_callback($tokens) {
+ // (yes, $tokens is safe to echo without escaping)
+ header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
+ $data = '';
+ foreach (csrf_flattenpost($_POST) as $key => $value) {
+ if ($key == $GLOBALS['csrf']['input-name']) continue;
+ $data .= '';
+ }
+ echo "