towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Sat Jun 15, 2013 3:09 am

Quick question, when I played with nginx rtmp the first time, the source ffmpeg sending rtmp and nginx were together pretty much pegging the CPU on the RPi. FFmpeg HLS alone is in the low single digits.

Is this what others are seeing too? Is there another way of getting video into nginx besides sending rtmp?

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Sat Jun 15, 2013 3:30 pm

Mine is hardly averaging 7% CPU with ffmpeg into rtmp. That is the whole point.

What is your command line on ffmpeg and did u compile ffmpeg/ use the binary on here ?

Nginx uses about 2% and ffmpeg 5% at any quality. 1080, 720 or 480.

I also use medium
Overclock but that should not make a big difference.

Def not pegging on mine. Did u try the network patch for free member at least 16mb. Also on model b I only use 64mb gpu the rest is system.

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Sun Jun 16, 2013 1:50 pm

But you are running both ffmpeg and nginx on the same Raspberry pi?

I get this now and I’ve recompiled nginx with fewer extras and reformulated my setup to resemble yours

Code: Select all

%Cpu(s): 11.2 us, 59.1 sy,  0.0 ni, 16.5 id,  0.0 wa,  0.0 hi, 13.2 si,  0.0 st
KiB Mem:    445044 total,   200108 used,   244936 free,    38408 buffers
KiB Swap:   262140 total,        0 used,   262140 free,   108016 cached

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 4789 root      20   0   27828   9104   3652 R 39.8  2.0   0:29.48 ffmpeg
 4781 http      20   0    9084   1752    772 S 38.9  0.4   0:27.23 nginx
 4788 root      20   0   60876   1152    784 S  1.3  0.3   0:01.16 raspivid

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Sun Jun 16, 2013 2:11 pm

Yea both ffmpeg and nginx on the pi. I'll get a sceen

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Sun Jun 16, 2013 2:29 pm

Wait, I’ve gone down to 640x480 and I get 10% CPU now.

Were you testing this with low res all the time? Your script has no -w and -h so I assumed you were using 1080p?

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Sun Jun 16, 2013 4:42 pm

Can you show ffmpeg -protocols?

Code: Select all

# ffmpeg -protocols
ffmpeg version 1.2.1 Copyright (c) 2000-2013 the FFmpeg developers
  built on Jun 15 2013 10:38:07 with gcc 4.7.2 (GCC)
  configuration: --prefix=/usr --disable-debug --disable-static --enable-gpl --enable-ffmpeg --enable-ffplay --enable-ffserver --enable-libfdk-aac --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libv4l2 --enable-nonfree --enable-libvorbis --enable-runtime-cpudetect --enable-shared --enable-version3 --disable-x11grab --disable-doc libavutil      52. 18.100 / 52. 18.100
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.104 / 54. 63.104
  libavdevice    54.  3.103 / 54.  3.103
  libavfilter     3. 42.103 /  3. 42.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
Supported file protocols:
Input:
applehttp
cache
concat
data
file
hls
http
httpproxy
mmsh
mmst
pipe
rtp
srtp
tcp
udp
rtmp
rtmpe
rtmps
rtmpt
rtmpte
Output:
file
http
httpproxy
md5
pipe
rtp
srtp
tcp
udp
rtmp
rtmpe
rtmps
rtmpt
rtmpte

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Sun Jun 16, 2013 5:53 pm

Yea - using 1080p but i am not 100% sure the CPU usage with 1080. With 720p I was surprised and happy that it was like 10%. So it means I can use mysql and other software to do my weather station just fine.

I turned it off for now because I have some problem with other stuff at home and have to fix it. Will try to get it back later.

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Sun Jun 16, 2013 5:57 pm

I’m recompiling ffmpeg now without librtmp to check. I have a hunch that you might be using internal rtmp protocol and I’m using librtmp support. What does your ffmpeg compile info say, please?

BTW, I had a very low delay of 100ms or so. What did you get again?

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Sun Jun 16, 2013 7:49 pm

Great success! My hunch was correct.

Code: Select all

%Cpu(s): 12.3 us, 10.7 sy,  0.0 ni, 73.7 id,  0.0 wa,  0.0 hi,  3.2 si,  0.0 st
KiB Mem:    445044 total,   281824 used,   163220 free,    33840 buffers
KiB Swap:   262140 total,        0 used,   262140 free,   199560 cached

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
29093 root      20   0   31096  13620   2996 R 14.3  3.1   0:04.58 ffmpeg
16537 http      20   0    9452   2232   1048 S  5.8  0.5   0:22.42 nginx
So ffmpeg-internal RTMP has many more options than librtmp and when you compiled using librtmp there’s no switching.

Check the options here: http://ffmpeg.org/ffmpeg-all.html#rtmp

Suppose you can tune the buffering to balance CPU load and latency.

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Sun Jun 16, 2013 10:06 pm

Did you say you got 100ms delay? are you sure?? The least I can get with tuing client buffering to 0ms is 300~400ms.

Sorry I did reply earlier. was busy fixing another system at home.. that turned out to be a waste of my day. so annoyed. any way.
So what did I do that i got the low CPU usage? I just compiled ffmpeg on my own from source and compiled rtmp-module. I did not pay special attention to anything else- so it was pure luck.

But I am interested in that fine tuining stuff you said.

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Sun Jun 16, 2013 10:13 pm

I’m still looking into the tuning possibilities.

When you compile ffmpeg with --enable-librtmp then it uses that library, if you don’t have that library or you disable it then ffmpeg compiles its own internal rtmp code, which is *much* lower CPU.

Can you paste your output of "ffmpeg -protocols" here?

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 7:17 am

Code: Select all

root@weather:~# ffmpeg -protocols
ffmpeg version N-53430-g2c2e69b Copyright (c) 2000-2013 the FFmpeg developers
  built on May 25 2013 12:30:58 with gcc 4.6 (Debian 4.6.3-14+rpi1)
  configuration:
  libavutil      52. 34.100 / 52. 34.100
  libavcodec     55. 12.100 / 55. 12.100
  libavformat    55.  7.100 / 55.  7.100
  libavdevice    55.  1.101 / 55.  1.101
  libavfilter     3. 70.100 /  3. 70.100
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
Supported file protocols:
Input:
cache
concat
crypto
data
ffrtmphttp
file
ftp
gopher
hls
http
httpproxy
mmsh
mmst
pipe
rtmp
rtmpt
rtp
srtp
tcp
udp
Output:
ffrtmphttp
file
ftp
gopher
http
httpproxy
md5
pipe
rtmp
rtmpt
rtp
srtp
tcp
udp
1 user watching the pushed version to an external server. When nobody is on it is all idle? Weird but cool I suppose.
Image

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 10:38 am

Thanks, yeah you’re using the internal RTMP as well.

What I found pretty cool is the record feature in nginx-rtmp-module. So not only can it stream RTMP, it can handle HLS and dumping to another .flv file also. And you can opt to only record keyframes, which means you get a convenient timelapse as well without re-encoding the H264 stream. It just greps the keyframes out and writes them to an flv.

It’s the »eierlegende Wollmilchsau« of streaming on the RPi.

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 10:46 am

eierlegende Wollmilchsau - Egg laying Milk giving Woollen Pig.
Google single word transalte - [Egg laying] - [jack of all trades]
Yes, the rtmp-module is really versatile. The author is very talented and pro active. But he never saw the egg laying pig thing coming his way :lol: :lol: :lol: :lol:

Can you give example of how to configure the rtmp-module to do those time lapse frames and possible the record feature.

Glad you got it sorted! :)

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 11:05 am

Here’s my setup:

nginx.conf

Code: Select all

worker_processes 1;

user http http;
error_log /tmp/nginx.error.log;

events {
    worker_connections  1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    log_format short '$remote_addr [$time_local]  '
	    '"$request" $status $body_bytes_sent $bytes_sent$pipe $sent_http_content_range';
    access_log /tmp/nginx.access.log short;
    sendfile on;

    gzip on;
    gzip_types text/plain application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js application/x-mpegURL;

    server {
        listen 80 default;
        server_name _;

        keepalive_timeout 65;

        location /live {
            alias /data/live;
            autoindex on;
        }

        location /hls {
            alias /tmp/hls;
            autoindex on;
            set $sent_http_accept_ranges bytes;
            types {
                video/MP2T ts;
                application/vnd.apple.mpegurl m3u8;
            }
        }

        location /control {
            rtmp_control all;
        }

        location /stat {  
            rtmp_stat all;  
            rtmp_stat_stylesheet stat.xsl;  
        }

        location /stat.xsl {  
            root /data/live;  
        }
    }
}


rtmp {
    server {
        listen 1935;
        chunk_size 8192;

        application rtmp {
            live on;

            record keyframes;
            record_path /data/live/record;
            record_suffix -keyframes.flv;
            record_unique on;

            recorder ondemand {
               record video manual;
               record_suffix -dump.flv;
               record_path /data/live/record;
               record_unique on;
           }

            #    hls on;
            #    hls_path /tmp/hls;
            #    hls_fragment 4s;
            #    hls_playlist_length 10m;

            # Execute raspivid streaming script
            # change the path to point to the following script
            exec_static /usr/local/bin/rtmp-nginx.sh; 
            exec_kill_signal term;
        }
    }
}
And the ffmpeg script is launched by that exec statement above

/root/rtmp-nginx.sh

Code: Select all

#!/bin/bash

mkdir -p /tmp/hls

on_die () {
    # kill all children
    pkill -KILL -P $$
}

trap 'on_die' TERM

export FFREPORT="file=/tmp/%p-report-%t.log"

/opt/vc/bin/raspivid -n -mm matrix -w 1280 -h 720 -fps 25 -g 100 -t 0 -b 15000000 -o - \
  | ffmpeg -loglevel quiet -nostats -y \
      -f h264 \
      -i - \
      -c:v copy \
      -map 0:0 \
      -f flv \
      -rtmp_buffer 100 \
      -rtmp_live live \
      rtmp://localhost/rtmp/live &
wait
This records keyframes all the time and when you visit the link

Code: Select all

http://alarmpi/control/record/start?app=rtmp&name=live&rec=ondemand
it will start recording live video on disk, i.e., if you see something worth saving just click the link on your live streaming page (use XmlHttpRequest).
Last edited by towolf on Mon Jun 17, 2013 1:10 pm, edited 4 times in total.

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 11:18 am

Awesome! May I use that info on my blog?
I like the exec script from nginx. that is cool.

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 11:22 am

Sure.

Read this also: https://github.com/arut/nginx-rtmp-modu ... er-in-bash

It ensures that restarting the webserver cleanly kills ffmpeg, so you won’t get multiple accesses to the camera (tends to crash things).

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 12:32 pm

Does not seem to work? :(
I tried the bash file on its own -commented out the traps and stuff. And it works; I can view the stream. So remove comments..

and I use the exec directive- But there are no streams, no errors either. Not sure how to debug the problem?

-EDIT

nginx is running as www-data.. Maybe that is the problem? How can i fix that issue?

-EDIT2
Yes changed nginx to root and it works. As long as nginx wont be public then root is ok. But what's the best way to fix this issue?

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 12:44 pm

ppumkin wrote:Does not seem to work? :(
and I use the exec directive- But there are no streams, no errors either. Not sure how to debug the problem?

Yes changed nginx to root and it works. As long as nginx wont be public then root is ok. But what's the best way to fix this issue?
They all run as http user for me (i.e., www-data)

Code: Select all

31476 http      20   0   29044  11500   3072 S 19.4  2.6  11:12.10 ffmpeg
31472 http      20   0    9824   2844   1056 S  8.4  0.6   6:23.90 nginx
31475 http      20   0   60876   1144    780 S  2.9  0.3   1:30.57 raspivid
Can you append this snippet to the end of the exec-static line and look what it is saying in there?

Code: Select all

2>>/tmp/ffmpeg-rtmp-script.log
Alternatively check the nginx error log (perhaps under /var/log for you?)

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 12:57 pm

NO it does not even run ?

I put a line in there to create a timestamp file. That works ok if nginx is Root. But not it just does not even run the script?

I put that line of yours in and nothing. It wont do cause it wont run. Not sure. nginx log is empty. no error.

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 1:05 pm

Did you change the path to point to the correct script? Dunno how much hand holding you need, but that one should be obvious.

I.e., don’t put the script into /root, put it somewhere else, like /usr/local/bin, or something.

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 1:15 pm

Yea- Sorry I am a bit of newbie in Linux - Yes I had them in /root obviosly http doesnt have perm to run it from there.

I did move them out and set the user as www-data but it still does not work. Do I need to set some kind of permission on the files also like? 777 or something like that? Because when i changed it back to root it worked again using the /usr/local/bin

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 1:23 pm

ppumkin wrote:Yea- Sorry I am a bit of newbie in Linux - Yes I had them in /root obviosly http doesnt have perm to run it from there.

I did move them out and set the user as www-data but it still does not work. Do I need to set some kind of permission on the files also like? 777 or something like that? Because when i changed it back to root it worked again using the /usr/local/bin

Code: Select all

sudo chmod +rx /usr/local/bin/rtmp-nginx.sh
and then try it out as www-data

Code: Select all

sudo -u www-data /usr/local/bin/rtmp-nginx.sh

ppumkin
Posts: 82
Joined: Tue May 29, 2012 10:22 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 1:31 pm

Right I did not do +rx just +x but that did not seem to change anything. I ran the script as www-data

Image

Possibly it does not have access to both ffmpeg and raspivid also.

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Live streaming from PiCam

Mon Jun 17, 2013 1:39 pm

mount? What are you mounting?

Please add "set -x" at the top of the script and paste the output of running it as www-data. And ls -la "/usr/local/bin/"

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 11 guests