Advice for IP Camera Streaming to YouTube

Share here your ideas for new projects
Post Reply
AlexanderB
Posts: 6
Joined: Fri Mar 15, 2019 10:48 am
languages_spoken: english
Has thanked: 0
Been thanked: 0
Contact:

Advice for IP Camera Streaming to YouTube

Unread post by AlexanderB » Sat Mar 16, 2019 6:08 am

Hi all,

I am in search of some advice, wrt using an Odroid for streaming an IP camera to YouTube.

Currently we have an IP camera (Dahua) which is connected to an old Mac Mini (2009). FFmpeg is used to convert the RTSP stream from the camera to a RTMP stream which is sent to YouTube.
To be able to control what is shown, we use a scale/crop combination to get a specific part of the screen (a storks nest, for who wants to know :)). FFmpeg is set up to have two streams, one to YouTube, to other to a recording. This makes it possible for us to create compositions of interesting events, eg the hatching of the eggs. Those recording are stored to an external harddisk, which once a week is swapped with a second HD.
Also, the incoming 1280x720 size is downscaled to 640x360. Initially this downscaling was mostly done to be able to push the data over a slow connection to YouTube.

Recently we were able to upgrade to a fiber connection, so connection speed isn't a problem anymore. But now the old Mac Mini is turning out to be the problem. As soon as I increase the outgoing resolution, the system uses 100% CPU, and output speed is slow (far below 1x).

So now I am looking for some advice regarding the capabilities of a small "computer" for what we want.
Ideally I'd like to be able to process full-hd, for the recording as well as for streaming to YouTube. Besides that, there are some requirements that I can't change:
* Dahua camera (IP camera)
* Scaling/Cropping to be able to zoom
* Sound support
* Highest possible resolution
* Stream to YouTube
* Save stream for later processing (this can/should be the unscaled/uncropped stream)

Is there an Odroid which is a suitable solution for want we want to do? Is encoding/decoding using hw acceleration possible? And if so, with what software? FFmpeg or eg GStreamer, or something else alltogether?
Also, without a full desktop, what is a good way to store the data so that it can easily be retrieved later on? I'm thinking of setting up a simple FTP server on the Pi unit, and hope this doesn't have a negative impact on the CPU.

While a rather extensive question, I do hope someone can give me some advice!

Thanks in advance!

User avatar
mad_ady
Posts: 6552
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1, H2, N2
Location: Bucharest, Romania
Has thanked: 169 times
Been thanked: 128 times
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by mad_ady » Sat Mar 16, 2019 3:33 pm

While I can't guarantee it will work, the xu4 seems like a good candidate for your needs. It supports hw encoding with both ffmpeg and gstreamer, with gstreamer having fewer bugs.
Here's a link with some examples that can give you an idea on performance: viewtopic.php?t=24366

Saving a h264 stream with -c:v copy will not consume resources, so it depends on your pipeline - worst case you connect to your webcam twice - once to save the stream without decoding and once to decode, crop, stream.

As far as I know gstreamer supports hardware scaling (not sure if it can be used for cropping or not), but the whole encoder needs to work on images with sides multiple of 64 pixels.
Note that the encoder produces a bit larger bitstream than the software encoder (has poorer encoding because it's designed for real-time).

So - it should work (for up to 1080p) without too many headaches and still leave plenty of unused resources to use the xu4 as a nas too.

AlexanderB
Posts: 6
Joined: Fri Mar 15, 2019 10:48 am
languages_spoken: english
Has thanked: 0
Been thanked: 0
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by AlexanderB » Sat Mar 16, 2019 10:14 pm

Hi,

Thanks for the reply! I see the Xu4 is currently out of stock, and the N2 being mentioned on the blog. The specs and benchmarks of the N2 seem to be much better.

With both being available again in April, is the extra money worth the N2?

User avatar
mad_ady
Posts: 6552
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1, H2, N2
Location: Bucharest, Romania
Has thanked: 169 times
Been thanked: 128 times
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by mad_ady » Sun Mar 17, 2019 1:53 am

The n2 is based on an amlogic processor, while xu4 is based on samsung. Samsung is better supported in software, while also being more mature.
For C2 (also amlogic cpu) @crashoverride reversed their libraries and created a hardware encoding programs that can be interfaced with ffmpeg, but I had problems getting sound to be in sync.
I may try software encoding on the n2 to see if it can keep up. Can you share your ffmpeg command line so I can give it a try?

AlexanderB
Posts: 6
Joined: Fri Mar 15, 2019 10:48 am
languages_spoken: english
Has thanked: 0
Been thanked: 0
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by AlexanderB » Tue Mar 19, 2019 5:35 am

Hi,

Sorry for the late reply, I didn't have access to the script until just now. Below the FFmpeg command that I use at the moment. It uses a lower incoming and outgoing resolution, and currently also scales the recording. Ideally I'd like this to be a higher resolution, and optionally the recordings can be without scale/crop. (if I understand things correctly, that is even easier, since there is no processing needed on that specific stream?).

Also, important to note, I am in no way an FFmpeg expect (not even a novice ;)). So perhaps there are even better ways to do what we do. I'm open for any/all suggestions!

Code: Select all

cmd="ffmpeg \
-f lavfi -i anullsrc \
-stimeout 10000000 \
-rtsp_transport tcp -i \"$rtsp_source\" \
-tune zerolatency -vcodec libx264 -pix_fmt + -c:v libx264 -c:a aac -map 1:v:0 -map 1:a:0 \
-strict experimental \
-vsync passthrough \
-vf scale=1.6*640:1.6*360,crop=iw/1.6:ih/1.6:260:150 \
-use_localtime 1 \
-hls_time 900 \
-hls_list_size 672 \
-hls_segment_filename $recordings/recording_%Y-%m-%d_%H-%M-%S.ts \
-hls_flags delete_segments \
-f hls $recordings/index.m3u8 \
-tune zerolatency -preset ultrafast -crf 30 -maxrate 2M -bufsize 1M -vcodec libx264 -pix_fmt + -c:v libx264 -c:a aac -map 1:v:0 -map 1:a:0 \
-strict experimental \
-vsync passthrough \
-vf scale=1.6*640:1.6*360,crop=iw/1.6:ih/1.6:260:150 \
-f flv $rtmp_destination"

User avatar
mad_ady
Posts: 6552
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1, H2, N2
Location: Bucharest, Romania
Has thanked: 169 times
Been thanked: 128 times
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by mad_ady » Tue Mar 19, 2019 4:03 pm

I'll try to run your pipeline with a 1280x1024 with both xu4 and n2 to check the load/performance and I'll let you know.

AlexanderB
Posts: 6
Joined: Fri Mar 15, 2019 10:48 am
languages_spoken: english
Has thanked: 0
Been thanked: 0
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by AlexanderB » Sun Mar 24, 2019 4:50 pm

Hello,

I'm wondering if you had a chance to test the pipeline we use. Would be great to have some more information!

User avatar
mad_ady
Posts: 6552
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1, H2, N2
Location: Bucharest, Romania
Has thanked: 169 times
Been thanked: 128 times
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by mad_ady » Sun Mar 24, 2019 7:55 pm

Not yet, sorry. It's on my todo list for the next few days.

User avatar
mad_ady
Posts: 6552
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1, H2, N2
Location: Bucharest, Romania
Has thanked: 169 times
Been thanked: 128 times
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by mad_ady » Mon Mar 25, 2019 5:59 pm

Ok, tired it with my webcam, but I have a low resolution:

Code: Select all

Input #1, rtsp, from 'rtsp://192.168.1.52:554/camera-h264-stream.sdp':
  Metadata:
    title           : IPCam
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #1:0: Video: h264 (Baseline), yuv420p(progressive), 640x480, 15.83 tbr, 90k tbn, 180k tbc
    Stream #1:1: Audio: pcm_alaw, 8000 Hz, mono, s16, 64 kb/s

On XU4 with software encoding/decoding it could sustain real-time transcoding (effective framerate was about 11fps, but this is I think because of the camera):

Code: Select all

[hls @ 0x52fcf0] Opening 'recording_2019-03-25_08-35-50.ts' for writing
Output #0, hls, to 'index.m3u8':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Video: h264 (libx264), yuv420p, 640x360, q=-1--1, 15.83 fps, 90k tbn, 15.83 tbc
    Metadata:
      encoder         : Lavc57.107.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC), 8000 Hz, mono, fltp, 48 kb/s
    Metadata:
      encoder         : Lavc57.107.100 aac
[libx264 @ 0x63f8e0] using cpu capabilities: ARMv6 NEON
[libx264 @ 0x63f8e0] profile Constrained Baseline, level 2.2
[libx264 @ 0x63f8e0] 264 - core 152 r2854 e9a5903 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=5 lookahead_threads=5 sliced_threads=1 slices=5 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=15 scenecut=0 intra_refresh=0 rc_lookahead=0 rc=crf mbtree=0 crf=30.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=2000 vbv_bufsize=1000 crf_max=0.0 nal_hrd=none filler=0 ip_ratio=1.40 aq=0
Output #1, flv, to 'stream.flv':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #1:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 640x360, q=-1--1, 15.83 fps, 1k tbn, 15.83 tbc
    Metadata:
      encoder         : Lavc57.107.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 2000000/0/0 buffer size: 1000000 vbv_delay: -1
    Stream #1:1: Audio: aac (LC) ([10][0][0][0] / 0x000A), 8000 Hz, mono, fltp, 48 kb/s
    Metadata:
      encoder         : Lavc57.107.100 aac
[hls @ 0x52fcf0] Opening 'index.m3u8.tmp' for writing:34.66 bitrate=N/A speed=1.01x    
frame= 2994 fps= 11 q=14.0 Lq=20.0 size=N/A time=00:04:35.05 bitrate=N/A speed=1.01x    
video:28675kB audio:2733kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

CPU usage was about 30% on the big cores, 50% on two little cores. Load average: 2. The fan as also spinning most of the time.

Code: Select all

  1  [|||||||||||||||          46.0%]   5  [|||||||||||||||          49.4%]
  2  [||||||||||||             36.9%]   6  [|||||||||                27.3%]
  3  [|||                       7.2%]   7  [||||||                   17.3%]
  4  [|                         0.6%]   8  [|||||||                  18.5%]
  Mem[||||||||||||||||||| 342M/1.95G]   Tasks: 61, 157 thr; 3 running
  Swp[                         0K/0K]   Load average: 2.22 0.93 0.62 
                                        Uptime: 00:50:45

On the N2: I got the same with about 50% on the little cores and 10% on the big cores (load). Load average: 2.73.

Code: Select all

[hls @ 0x5583e8ec10] Opening 'index.m3u8.tmp' for writing53 bitrate=N/A speed=1.02x    
frame= 1991 fps= 11 q=22.0 Lq=28.0 size=N/A time=00:03:02.17 bitrate=N/A speed=1.02x    

Code: Select all

  1  [|||||||||||||||          46.1%]   4  [||||                     11.0%]
  2  [|||||||||||              33.1%]   5  [|||                       9.1%]
  3  [||||                     13.3%]   6  [|                         2.6%]
  Mem[||||||              196M/3.63G]   Tasks: 27, 70 thr; 5 running
  Swp[                         0K/0K]   Load average: 1.92 1.52 0.87 
                                        Uptime: 00:09:33
So, the N2 will be able to stream more streams, or higher resolution streams than the XU4 *with software encoding/decoding*.

User avatar
memeka
Posts: 4327
Joined: Mon May 20, 2013 10:22 am
languages_spoken: english
ODROIDs: XU rev2 + eMMC + UART
U3 + eMMC + IO Shield + UART
Has thanked: 1 time
Been thanked: 16 times
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by memeka » Mon Mar 25, 2019 6:49 pm

On XU4
Gstreamer + hardware transcoding = up to 1080p 120fps total (e.g 4 streams 1080p@30fps)

AlexanderB
Posts: 6
Joined: Fri Mar 15, 2019 10:48 am
languages_spoken: english
Has thanked: 0
Been thanked: 0
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by AlexanderB » Wed Mar 27, 2019 4:07 am

So if I understand this correctly, the XU4 does better on hardware due to chip/driver support?

And in our case, where we have one incoming stream, and 2 outgoing (1 to youtube, 1 to file). The XU4 should do fine with Full HD?

Do you perhaps have a GStreamer example of what I'm trying to do? I had a look at it, but am still at a loss to the usage of gstreamer ;).

Thanks for testing and the help!

User avatar
memeka
Posts: 4327
Joined: Mon May 20, 2013 10:22 am
languages_spoken: english
ODROIDs: XU rev2 + eMMC + UART
U3 + eMMC + IO Shield + UART
Has thanked: 1 time
Been thanked: 16 times
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by memeka » Wed Mar 27, 2019 3:19 pm

@AlexanderB

XU4 has better drivers for hardware decoding/encoding.
It's possible to do it on C2 too (and probably on N2), but via 3rd party programs that access the encoder/decoder.

Gstreamer is pretty easy to use - you just pipe one thing to another. In your case, you need to pipe RTSP source (rtspsrc) to a demuxer, then to a decoder (v4l2h264dec for example, for hardware decoding of H264), then to an encoder (v4l2h264enc for hw encoder, with options for bitstream etc..), then you probably want to use "tee" to split from now on - one path can do to a muxer, then to a file (e.g. matroskamux then filesink), and another path to another muxer then to hlssink.

User avatar
mad_ady
Posts: 6552
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, N1, H2, N2
Location: Bucharest, Romania
Has thanked: 169 times
Been thanked: 128 times
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by mad_ady » Wed Mar 27, 2019 3:39 pm

Yes, for hardware encoding XU4 is the platform to go to. It can do hardware encoding and scaling (not sure if ffmpeg uses the hardware scaler, but gstreamer does). Not sure what happens when you want to overlay stuff over your stream - some things may be downgraded to software encoding.
There are also disadvantages:
- some cameras produce a video stream that is incompatible with the hardware decoder (you can use software decoding)
- resolution must be divisible by 64
- the hw encoder is not terribly efficient and may produce a higher bitrate stream for the same quality when compared to the software encoder. It's the same as your phone's camera - it's designed to look good at high bitrates, but I didn't like the output at low bitrates (for example I would get a good-looking I frame followed by really degraded P frames - the encoder does not produce B frames as far as I know)

@memeka is the gstreamer pipeline guru around these parts...

AlexanderB
Posts: 6
Joined: Fri Mar 15, 2019 10:48 am
languages_spoken: english
Has thanked: 0
Been thanked: 0
Contact:

Re: Advice for IP Camera Streaming to YouTube

Unread post by AlexanderB » Thu Apr 04, 2019 9:30 pm

All thanks for the feedback! I'll have to wait for the XU4 to be available again. If I get it, I'll set it up and try to get it going with gstreamer.

Post Reply

Return to “The Ideas”

Who is online

Users browsing this forum: No registered users and 1 guest