Skip to content

Dystopia

joshua-coppola edited this page Feb 18, 2020 · 2 revisions
  • AFS client = Yes
  • IP = ###.###.220.16
  • Kerberized logins = Yes
  • OS = Debian 7.5
  • Predecessor = Gremlin
  • Purpose = Lab surveillance

Dystopia is CIF's surveillance server. It replaces Gremlin for monitoring the CIF Lab via camera and providing a web interface for viewing the footage.

Camera Footage

All camera footage is stored in camX/ directories for each camera inside the /footage/ directory. Everything inside /footage/ can be moved or deleted and motion will recreate the camera directories. Motion is configured to only create avi files for footage.

Camera Support and Troubleshooting

Because of compatibility issues, some cameras may not be usable. To fix the issue, edit /etc/init.d/motion and add the following lines:

LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libv4l/v4l1compat.so
export LD_PRELOAD

If those do not work, try using

LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libv4l/v4l2convert.so
export LD_PRELOAD

Please note that updates to the motion package may overwrite these changes, so if cameras cease to function the first place to check should be /etc/init.d/motion to see if these lines are still present.

Camera Limitations

Bandwidth and Power Consumption

Each of our cameras is a USB 1.1 device running at full-speed, meaning it uses the maximum bandwidth specified by USB 1.1: 12Mbps. USB 2.0 specifies 480Mbps as its maximum bandwidth, but USB 1.1 signals need to be run through a transaction translator (TT) to convert the signal from USB 1.1 to 2.0. An individual TT will only have 12Mbps of bandwidth and most USB hubs are built with a single TT, so we have obtained a 7 port multi-TT USB 2.0 hub which provides each USB 1.1 device with its own 12Mbps TT, which is then sent over the 480Mbps upstream USB 2.0 port.

As for power, the maximum amount a port is allowed to draw is 500mA, although this is only if it requests more than the standard 100mA. Most of our cameras appear to use 100mA, so power is not an issue and the hub provides enough.

Cable Length

The maximum specified length for a cable between full-speed USB devices is 5 meters, or 16'5". This includes the cable between then hub and Dystopia, which is about 150cm or just under 5".

Setup

Debian Installation

For partitioning:

  • Select "Guided - use entire disk and set up LVM"
    • Select the drive to use
    • Select "All files in one partition"
    • /dev/sda2/ was used for the boot partition

For software selection:

  • Select only SSH Server and Standard System Utilities

Video Surveillance

We will use motion to record video whenever motion is detected from any number of webcams. Installing libv4l-0 will provide support for more webcams.

apt-get install libv4l-0 motion

Create a /footage/ directory to store our camera footage, and give the motion daemon ownership of it.

mkdir /footage
chown motion:motion /footage

Edit /etc/motion/motion.conf and do the following:

  • Set daemon to on to allow allow motion to be run in the background.
  • Comment out the video device line (comment it out with a ;)
  • Comment out the target_dir line (comment it out with a ;)
  • Set pre_capture to 2 to capture 2 frames before motion was detected.
  • Set post_capture to 10 to capture frames after motion was detected.
  • Set output_normal to off because we only want video footage.
  • Set ffmpeg_video_codec to msmpeg4 to export video to .avi in a format that is compatible with Windows Media Player and other common software.
  • Set movie_filename to %Y-%m-%d_%H-%M-%S_%v to make footage filenames more readable.
  • Set on_movie_end to /footage/convert_video.sh %f to make convert footage once it's finished being recorded.
  • Comment out the webcam_port line (comment it out with a ;)

Edit /etc/default/motion and set the daemon to start when using service: start_motion_daemon=yes

Multiple Cameras

To enable support for multiple cameras, a thread file must be created for each camera and referenced from /etc/motion/motion.conf. The values in motion.conf serve as default values for each thread configuration. For these instructions, is the number of the camera. Note that should start from 1, although the first camera device will be /dev/video0.

Edit /etc/motion/motion.conf and add the following at the bottom: thread /etc/motion/threadX.conf

Create/edit /etc/motion/threadX.conf with the following contents:

videodevice /dev/video

# The video input to be used (default: 8)
# Should normally be set to 1 for video/TV cards, and 8 for USB cameras
input 8

# Draw a user defined text on the images using same options as C function strftime(3)
# Default: Not defined = no text
# Text is placed in lower left corner
text_left CAMERA 

# Target base directory for pictures and films
# Recommended to use absolute patch. (Default: current working directory)
target_dir /footage/cam

# The mini-http server listens to this port for requests (default: 0 = disabled)
webcam_port 808

# Command to be executed when a picture (.ppm|.jpg) is saved (default: none)
# The filename of the picture is appended as an argument for the command.
#on_picture_save 

# Command to be executed when a movie file (.mpg|.avi) is closed. (default: none)
# Filename of movie is appended as an argument for the command.
#on_movie_end

Convert Motion Video to Web-Friendly Format

Create /footage/convert_video.sh with the following contents:

#!/bin/bash

# $1 is the file to convert

# Generate an mp4 version (used for most browsers)
avconv -i $1 -vcodec libx264 -v quiet `dirname $1`/`basename $1 .avi`.mp4

# Generate an ogv version (used for Firefox)
avconv -i $1 -vcodec libtheora -v quiet `dirname $1`/`basename $1 .avi`.ogv

# Remove the avi file
rm $1

Run the following to make the script executable.

chmod +x /footage/convert_video.sh
service motion restart

Start Motion on Boot

To start the cameras on boot, run update-rc.d motion defaults to run the start script on boot.

Be sure to go through Camera Support and Troubleshooting now because not all cameras will work properly out of the box.

Web Interface

The web interface runs on Apache and uses a C program to act as a proxy between localhost and the client, and also to support browsers which can not handle MJPEG by serving single frames. Install the required packages with

apt-get install apache2 php5 php5-ffmpeg php5-gd

While we can use pure PHP to obtain a single frame, serving the first frame took about 1000ms with PHP and 500ms with the C program. Speed is important here because we're serving multiple images per second, so install the C program. Information on the C program can be found at http://www.lavrsen.dk/foswiki/bin/view/Motion/MjpegProxyGrab.

wget http://www.lavrsen.dk/foswiki/pub/Motion/MjpegProxyGrab/mjpeg-proxygrab-1.2.tar.gz
tar xzf mjpeg-proxygrab-1.2.tar.gz # Ignore the warning about the lone zero block
cd mjpeg-proxygrab-1.2/`

Then edit Makefile and change bindir = /usr/local/apache2/cgi-bin to the cgi-bin location on Debian:

bindir = /usr/lib/cgi-bin

Edit nph-mjgrab.c and add the following under printf("HTTP/1.1 200 OK\n"); to prevent caching (this is necessary for browsers to refresh the streams nicely):

printf("Cache-Control: no-cache, must-revalidate\n");
printf("Cache-Control: no-store\n");
printf("Expires: Thu, 01 Jan 1970 00:00:00 GMT\n")

You can also edit the 3 #define lines in each source file, but their defaults work for the configuration detailed on this Wiki. Now install the files and clean up.

make
make install
cd ..
rm -r mjpeg-proxygrab-1.2*

Edit /etc/php5/apache2/php.ini and do the following:

  • Set display_errors = Off to display_errors = On

Set all files in /var/www/ to be owned by the web server group by setting the SGID bit:

sudo chmod g+s /var/www/

Link the video footage to the web server.

ln -s /footage /var/www/footage

Create a log directory to store access and download logs written by the web server:

mkdir /var/log/footage/
chown www-data:www-data /var/log/footage/

Securing the Web Interface

Set up WebAuth according to our WebAuth page.

Create a self-signed certificate for the server:

mkdir /etc/ssl/self-signed
openssl req -new -x509 -days 36500 -nodes -out /etc/ssl/self-signed/dystopia.pem -keyout /etc/ssl/self-signed/dystopia.key

Change /etc/apache2/sites-available/default to the following:

<VirtualHost *:80>
        ServerName dystopia.cif.rochester.edu
        DocumentRoot /var/www
        Redirect permanent / https://dystopia.cif.rochester.edu/
</VirtualHost>

Enable the Apache SSL module:

a2enmod ssl

Change /etc/apache2/sites-available/default-ssl to the following:

<IfModule mod_ssl.c>
<VirtualHost _default_:443>
        ServerName dystopia.cif.rochester.edu
        ServerAdmin techdirector@cif.rochester.edu

        # Prevents browsers from receiving a 400 error when sending webauth cookies
        LimitRequestFieldSize 12392

        DocumentRoot /var/www
        <Directory />
                # Restrict access to members of the 'admins' or 'board' groups
                AuthType WebAuth
                require privgroup CN=admins,OU=LinuxGroups,OU=People,DC=cif,DC=rochester,DC=edu
                require privgroup CN=board,OU=LinuxGroups,OU=People,DC=cif,DC=rochester,DC=edu

                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All AuthConfig
                Order allow,deny
                allow from all
        </Directory>
        <Directory  ~ "\.git">
                Order allow,deny
                Deny from all
        </Directory>

        <Location /logout.php>
                WebAuthDoLogout on
        </Location>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                WebAuthDontCache on
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined

        ## SSL Engine Switch:
        # Enable/Disable SSL for this virtual host.
        SSLEngine on

        # Use these self-signed certificates if a valid certificate for this subdomain is not available
        #SSLCertificateFile /etc/ssl/self-signed/dystopia.pem
        #SSLCertificateKeyFile /etc/ssl/self-signed/dystopia.key

        SSLCertificateFile /etc/ssl/certs/__cif_rochester_edu_cert.cer
        SSLCertificateKeyFile /etc/ssl/private/private_key.pem
        SSLCertificateChainFile /etc/apache2/ssl.crt/__cif_rochester_edu_interm.cer

        # OptRenegotiate:
        # This enables optimized SSL connection renegotiation handling when SSL
        # directives are used in per-directory context.
        #SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>

        <Directory "/var/www/localhost/cgi-bin">
                SSLOptions +StdEnvVars
        </Directory>

        ## ssl-accurate-shutdown:
        # This forces an accurate shutdown when the connection is closed, i.e. a
        # SSL close notify alert is send and mod_ssl waits for the close notify
        # alert of the client. This is 100% SSL/TLS standard compliant, but in
        # practice often causes hanging connections with brain-dead browsers. Use
        # this only for browsers where you know that their SSL implementation works
        # correctly.
        # Notice: Most problems of broken clients are also related to the HTTP
        # keep-alive facility, so you usually additionally want to disable
        # keep-alive for those clients, too. Use variable "nokeepalive" for this.
        # Similarly, one has to force some clients to use HTTP/1.0 to workaround
        # their broken HTTP/1.1 implementation. Use variables "downgrade-1.0" and
        # "force-response-1.0" for this.
        <IfModule setenvif_module>
                BrowserMatch ".*MSIE.*" \
                nokeepalive ssl-unclean-shutdown \
                downgrade-1.0 force-response-1.0
        </IfModule>
</VirtualHost>
</IfModule>

Enable the SSL VirtualHost with

a2ensite default-ssl

Restart Apache for the changes to take effect.

service apache2 restart

Diagnostics

2 short SMART tests were run on the hard drive on May 23, 2014 around 18:00. Both tests encountered no errors.

SATA Connectivity Issues

Dystopia originally had 2 SATA ports, but one of them fell apart and so an external disc drive must be inserted via USB if CD reading/writing is necessary. It is possible to put the cap back over the pins to repair the SATA port, or replace the entire motherboard, but both of these solutions are tedious.

Clone this wiki locally