📜 ⬆️ ⬇️

Organization of live broadcast from ip camera on site

Introduction

Task
Consider the task of organizing a live video broadcast from an ip camera on the site. Our stand consists of three components:
Select ip camera
In order to be comfortable to take stream from the camera, it must support live video transmission (usually via the RTSP protocol). There is another option when you can pick up a JPEG with the current frame via HTTP from the camera at any time, but this is not so convenient. Therefore, the D'Link DCS-2121 camera with RTSP support was selected.
Media server
The most difficult part of the configuration. The functions of getting video from the camera, converting it to the required format, caching and distribution to clients fall on it. Looking ahead, I will say that it is expedient to distribute in this case either via the adobe RTMP protocol, which is natively supported by flash players, or via HTTP. For RTMP there are paid solutions: FMS , Wowza (the latter is free with the number of simultaneous connections less than ten) and free ( red5 and rtmpd ). If you distribute via HTTP, then there are even more options, since the task is divided into two subtasks:
  1. video conversion
  2. video distribution
Naturally, the number of combinations grows. For distribution, you can use, for example, apache or lighthttpd. To convert - ffmpeg. In my case it was necessary to do everything
  1. under linux
  2. is free
  3. simply
Therefore, I stopped at the simple, in my opinion, simple version, in which on the server we need only one program - the VLC video player , which, by the way, we will configure and compile for our needs. This video player does not yet have normal support for RTMP, so we will give the flv file via HTTP.

flash player in client side browser

In principle, anyone should approach, but for some reason I didn’t manage to set up JW Player . The FLV file was infinitely downloaded to the client, and the buffering did not end. The deadline was pressed and changed the player to the Flow player , as a result of which this problem disappeared.

Customization

Agreements
In order not to make reservations further, we will assume that I have the server ip address - 10.0.0.2, cameras - 10.0.0.3, mask 255.0.0.0 and gateway 10.0.0.1, in Linux I work under user user, I run all the necessary commands via sudo .
Camera setup
We will configure the camera via the web interface, set the necessary network parameters: ip address, mask, gateway, set the time. We also need to specify the port on which the camera will transmit the RTSP stream (the standard port 554 is used, and I left it). In the section "Audio and Video" we will set the characteristics of the video that we need. In any case, we can change the bitrate and size of the image later on the server when converting, but in order to keep everything neat, we’ll point out here and now. We will use MPEG4 640x480, 15 fps, 512kb bitrate. Also here it is necessary to specify the RTSP URL. This address will be used when retrieving the stream from the camera, we are required to specify only the file name. I entered camera1.sdp. As a result, the stream will be available via the link rtsp: //10.0.0.3: 554 / camera1.sdp
Server Tuning
Any Linux will do, I used Debian Lenny. Create a folder in the home in which we will work:
  1. mkdir ~ user / install
  2. cd ~ user / install
We will need to download codecs, so we connect the debian-multimedia repository:
  1. wget http: // www.debian-multimedia.org / pool / main / d / debian-multimedia-keyring / debian-multimedia-keyring_2008.10.16_all.deb
  2. dpkg -i . / debian-multimedia-keyring_2008.10.16_all.deb
Add a line to /etc/apt/sources.list
  1. deb http: // www.debian-multimedia.org stable main non-freel.deb
We will need to install a lot of packages before we go directly to VLC. I tried to list everything here, but maybe something is not enough, in this case the missing ones will have to be delivered.
  1. apt-get update
  2. apt-get install yasm make subversion xcb libxcb1-dev libxcb-shm0-dev libxcb-keysyms0-dev \
  3. libavformatcvs51 libavcodeccvs51 libavcodeccvs51-dev libavformatcvs51-dev libavutilcvs49-dev \
  4. autoconf g ++ gcc liba52-0.7.4-dev libdvbpsi3-dev libdvbpsi3 libfaad-dev libfaac-dev libfribidi-dev \
  5. libavutilcvs49 libavahi-client3 libavahi-common-dev libpostproccvs51-dev libswscalecvs0-dev \
  6. libswscalecvs0 libxvidcore4-dev libxvidcore4 libx264-dev libx264-54 automake1.9 libgcrypt11-dev \
  7. liblame-dev liblua5.1- 0 -dev libmad0-dev libmpeg2- 4 -dev libogg-dev libvorbis-dev zlib1g-dev \
  8. libvcdinfo-dev libiso9660-dev libcddb2-dev libflac-dev libx264-dev x264
All the rest will be set from source to be able to enable or disable certain options. Let's start with the latest version of ffmpeg:
  1. svn checkout svn : // svn.ffmpeg.org / ffmpeg / trunk ffmpeg
  2. cd ffmpeg
  3. . / configure
  4. make
  5. make install
  6. cd ..
If you plan to work with the h264 codec, then you can configure ffmpeg with the options ./configure --enable-libx264 --enable-gpl Next, we need the very necessary live555 streaming media library, it is through it that our VLC player will work with RTSP.
  1. wget http: // www.live555.com / liveMedia / public / live555-latest.tar.gz
  2. tar zxvf. / live555-latest.tar.gz
  3. cd live
  4. . / genMakefiles linux
  5. make
  6. cd ..
Now go to the VLC player itself. Go to http://download.videolan.org/pub/videolan/vlc/latest/ and see which is the latest version, then download and unpack the source. In my case it looked like this:
  1. wget http: // download.videolan.org / pub / videolan / vlc / latest / vlc-1.1.0.tar.bz2
  2. bzip2 -d vlc-1.1.0.tar.bz2
  3. tar xvf. / vlc-1.1.0.tar
  4. cd vlc-1.1.0
The VLC configuration is the most creative part, we will try to make it so that we have what we need and nothing extra.
  1. . / configure --enable-release --enable-faad --disable-dbus --disable-hal \
  2. --disable-remoteosd --disable-qt4 --disable-skins2 --disable-activex \
  3. --disable-v4l2 --disable-libv4l2 --disable-x11 --disable-xvideo --disable-glx \
  4. --disable-opengl --disable-visual --disable-nls --disable-mozilla \
  5. --enable-realrtsp --enable-flac --disable-lua --prefix = / usr \
  6. --with-live555-tree = / home / user / install / live --with-ffmpeg-tree = / home / user / install / ffmpeg
In the last line, we indicate the paths to the ffmpeg and live555 sources, with which we worked in the previous steps. If you plan to use the player as root, you need to add the --enable-run-as-root key if the h264 codec is --enable-x264 . Next, compile and install the player. This procedure takes a long time. In my case there was always something missing to compile, I tried to enumerate all the necessary libraries, but in any case, everything can change from version to version, so watch for errors and deliver what it asks.
  1. make
  2. make install
Software installation is complete, all we have to do is launch the player with the necessary parameters. In general, the VLC player is unique in its flexibility. The player will work for us in two simultaneous streams: the first will receive video from the camera via RTSP multiplex it in MPEG TS and send it to localhost: 8001, the second will take the result from localhost: 8001, compress, encode, package into an FLV container and give it to HTTP on port 8080. We make 2 files to run these streams, respectively:
  1. mkdir ~ user / scripts
  2. touch ~ user / scripts / stream1.sh
  3. touch ~ user / scripts / stream2.sh
Paste the lines in stream1.sh:
  1. #! / bin / sh
  2. vlc -vv rtsp: // 10.0.0.3: 554 / camera1.sdp - rtsp-caching = 100000 --no-sout-audio - sout \
  3. '#std {access = http, dst = 127.0.0.1: 8001, mux = ts}'
Here we have indicated to the player what to lose. The stream from the rtsp: //10.0.03: 554 / camera1.sdp camera was selected as the source, indicated the size of the buffer and immediately made the first stage our movie silent ( --no-sout-audio ). The result will be given to this stream at http://127.0.0.1:8001V stream2.sh insert the lines:
  1. #! / bin / sh
  2. vlc -vv http: // 127.0.0.1: 8001 --loop --http-caching = 10,000 --sout \
  3. '#transcode {vcodec = FLV1, vb = 512, fps = 15}: std {access = http {mime = video / x-flv}, dst =: 8080 / view01.flv, mux = ffmpeg {mux = flv}} '
The second stream takes http://127.0.0.1:8001, has its own cache, overwrites it (vb - bitrate, fps - FPS) and distributes it via HTTP on port 8080 as view01.flv file. If there are several interfaces on the server, then you can also specify in dst the specific ip address of the interface on which you need to distribute. In commercial operation, the streams must be run as a background and not tied to the console, in our example, we will launch them simply in two consoles. vlc should fill the buffer some time, and then pass in the regular mode. The duration of the filling depends on the size of the cache ( --rtsp-caching and --http-caching respectively). We will need to create a page with the player. For simplicity, we will do it on the same server.
  1. apt-get install apache2
Player settings
  1. cd / var / www /
Next, download Flow Player. The developer’s site has a settings wizard. To use it, you need to register. The wizard is available at http://flowplayer.org/setup/index.html . Since we have a live stream and the camera displays the current time directly into the picture, the player should be minimalistic. Turning off everything except the “Fullscreen” button, downloading the player and unpacking the contents of the archive into the / var / www folder on our server. So that everything was perfect, I also renamed the swf and js files to player.swf and player.js, respectively. I would not write about this fact if I had not forgotten how they were called before. Therefore, in the text I will also use my new names. So, create an html page
  1. touch / var / www / index.html
We save in it the following text:
  1. < html > < head >
  2. < meta http-equiv = "content-type" content = "text / html; charset = UTF-8" > < script type = "text / javascript" src = "/player.js" > < / script >
  3. < title > Camera View 1 < / title >
  4. < / head >
  5. < body >
  6. < h1 > Camera number 1 < / h1 >
  7. < a
  8. href = "http://stream.kubsu.ru:8080/view01.flv"
  9. style = "display: block; width: 520px; height: 330px; margin: 10px auto;"
  10. id = "player" >
  11. < / a >
  12. < script >
  13. flowplayer ("player", "/player.swf");
  14. < / script >
  15. < / body > < / html >
Everything is ready, go to the browser on http://10.0.0.2/ and watch the broadcast.
PS Post from the sandbox. Thanks for the invite to habr.

')

Source: https://habr.com/ru/post/98064/


All Articles