Introduction
In a previous post, I described how you could set up the Beaglebone Black to capture video and process video using OpenCV with the Logitech C920 camera. In this post I am going to look at how you can begin streaming data using the camera and code that I had adapted previously. The previous post is here and the final instructional page is here. I also just posted on how to stream video data using RTP.
This post looks at how you can stream video using UDP unicasting and multicasting. UDP unicasting is very similar to the RTP stream from before except there is no need to distribute a configuration file to the client via RTP or RTSP. The downside of unicasting is that the video transmitter needs to know the destination address of the receiver – kind of the opposite of what you might expect!
UDP Multicasting allows you to have many clients attach to the same video stream, which is really useful if you want to have the viewer instigate the connection and you wish to have many viewers.
Getting Started
Download the source code for my boneCV repository. This has the source code for everything that is discussed from here on down. Use:
1 |
git clone git://github.com/derekmolloy/boneCV |
To clone the repository to your local file system. All of the code is in this repository and the scripts necessary to stream the video are listed there too.
Next, I will look at a few different ways of streaming the video using avconv (ffmpeg) and the code that I had previously distributed through this repository.
UDP Stream – Unicast
The first way is by using UDP and unicasting to your player. I have found that VLC works very well for this. In my case I am streaming from the Beaglebone Black using the code from the repository and I am receiving the video on my Windows 7 PC which is running VLC media player version 2.0.7 and 2.0.6.
Step 1. Start the UDP Unicast
On the Beaglebone modify the streamVideoUDP script to replace the IP address listed with the IP address of your Windows 7 machine. The code for this is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#!/bin/bash echo "Video Streaming for the Beaglebone - derekmolloy.ie" echo "Piping the output of capture to avconv" # Next line not necessary if you are using my -F option on capture v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=1 # Pipe the output of capture into avconv/ffmpeg # capture "-F" My H264 passthrough mode # "-o" Output the video (to be passed to avconv via pipe) # "-c0" Capture 0 frames, which means infinite frames in my program # avconv "-re" read input at the native frame rate # "-i -" Take the input from the pipe # "-vcodec copy" Do not transcode the video # "-f rtp rtp://192.168.1.2:1234/" Force rtp to output to address of my PC on port 1234 ./capture -F -o -c0|avconv -re -i - -vcodec copy -f mpegts udp://192.168.1.4:1234 |
Where the address after udp:// is the one that you need to adapt. You can see that this works by starting up the capture program from my previous post and piping the raw video output to avconv where it specifies the location of the viewing instance. In my case the IP address is 192.168.1.4 – just to be clear, this is the IP address of my PC on which VLC is running. In this case you do not need a distributed configuration file, like in the case of RTP (you will see later). You can see this running in Figure 1.
Figure 1. The UDP Stream being captured and unicasted from the Beaglebone.
Step 2. Open the Stream using VLC
Open VLC and go to Media -> Open Network Stream and under the “Network” tab enter the URL as: “udp://@:1234”, as illustrated in Figure 2.
Figure 2. The use of UDP unicast for viewing the video stream.
Press Play/hit enter and then after a few seconds the video stream should appear. The latency isn’t great – maybe 2 seconds but it is working very well as displayed in Figure 3. In particular, the frame rate is really excellent.
Figure 3. The Video Stream being played on the PC in Full HD. Click for a 1:1 Version of the image.
UDP Stream – Multicast
I had no experience of multicasting so I was surprised at how easy this step was once I had worked it out. I think you have to be lucky in this step with your network configuration and your router. Please see this links for information about multicast addresses:
Class D | 1110 | 224.0.0.0 – 239.255.255.255 | Multicast IP Addresses |
- http://www.cisco.com/en/US/tech/tk828/technologies_tech_note09186a0080094821.shtml
- http://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml
On my network 226.0.0.1 is working perfectly! So, here is the script that I am using to multicast to my network:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#!/bin/bash echo "Video Streaming for the Beaglebone - derekmolloy.ie" echo "Piping the output of capture to avconv" # Next line not necessary if you are using my -F option on capture v4l2-ctl --set-fmt-video=width=1920,height=1080,pixelformat=1 # Pipe the output of capture into avconv/ffmpeg # capture "-F" My H264 passthrough mode # "-o" Output the video (to be passed to avconv via pipe) # "-c0" Capture 0 frames, which means infinite frames in my program # avconv "-i -" Take the input from the pipe # "-vcodec copy" Do not transcode the video # "-f rtp rtp://192.168.1.2:1234/" Force rtp to output to address of my PC on port 1234 ./capture -F -o -c0|avconv -re -i - -vcodec copy -f mpegts udp://226.0.0.1:1234 |
Step 1. You can execute this by just typing ./streamVideoMulti as in Figure 4.
Figure 4. The Shell running on my Beaglebone – You can see the udp multicast address, the bitrate etc.
Step 2. Start up VLC and open the Stream. You specify the address of the multicast stream like in Figure 5. In my case it is udp://@226.0.0.1:1234/ and all appears to work correctly
Figure 5. Setting up the VLC to play my multicast stream from within Windows.
I have opened this stream on 3 different machines on the same network and it works perfectly. There is approximately a 2 second lag, but once it begins all players are perfectly synchronized.
Conclusions
I hope to make a video on this shortly as there are other elements that I would like to discuss. One important feature of this type of streaming is that the C920 is taking care of all video coding, a computationally very expensive operation. This means that the load on the Beaglebone is very light, as displayed in Figure 6. For the UDP streams the total cost is about 12% of CPU and 4.1% of Memory for each stream. Since the stream is running at about 3Mbits/sec there is plenty of bandwidth for multiple USB streams via a USB hub and even over WiFi.
Figure 6. Top running on the machine. You can see that it is using 9.3% CPU 0.8% Memory for avconv and 2.6% of CPU and 3.3% of Memory for capture.
Hi Derek, good tutorial, everything went well on my beaglebone. A little warning though, the multicast part crashing my router. Seem my 7 years old router is not built to handle HD streams. Other than that I got everything working in the end
Hi Derek, very good tutorial. Still learning Linux, so much of your steps are over my head. A friend of mine has been asked if he can multicast a local conference, across the internet to several remote towns. He’s baffled and has asked me for help. How hard would this be to run over the internet? I can provide the Linux systems for each end, but the programming is beyond me (as mentioned above). I would require more than “sample” scripts to enter into each system. Displays would most likely be 1:1 Any recommendations? Thank you in advance.
hi derek, amazing,, can you tell me how beaglebone work to cmucam4. thank you
can v send frames captured by opencv through udp??other than the thos captured by libv4l2??
Sir , how to remove this error….. kindly guide me plz
[mp3 @ 0x16c36e0] Format detected only with low score of 1, misdetection possible!
[mp3 @ 0x16c4000] Header missing
Last message repeated 111 timessess
[mp3 @ 0x16c36e0] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from ‘pipe:’:
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0.0: Audio: mp1, 0 channels, s16p
Output #0, mpegts, to ‘udp://192.168.0.107:1234’:
Output file #0 does not contain any stream
hey hi derek
Excellent tutorial. I m building a project in which i am using BeagleBone Black . I wanted to know whether the HD camera cape is necessary for higher resolution or we can achieve it by webcams? Do reply me asap.
Hi Derek, this may be the solution to my problem.
I have an old CNC Machine, Which have Plasma Display. Machine is OK but Display is out of order.
Video input to the display have following Specifications.
1-Mono chrome Video Data (TTL) 2- Pixel Dot Clock= 50 MHz, 3- H-Sync=25 Micro Sec. 4- V-Sync=25 Mili Sec. (Measured with Oscilloscope) 4- Estimated Resolution is 1000X1000 Pixels.
I want this Video Stream to be captured by Beagle Bone Black and Display it on Standadard HDMI Monitor.
Please advise if it is possible.
Thanks.
Nasir
hi
i am getting error atleast output file must be specified
hi , i am using logitech c310 camera which doesn’t support H264 format .
what should i change in this line “./capture -F -o -c0|avconv -re -i – -vcodec copy -f mpegts udp://226.0.0.1:1234” .
Hi, I have only tested this code with the C920. There is no guarantee that any other camera will work. Only H264 cameras can use the -F option.
Hi Derek,
Is there a way to grab the frames on a OpenCV based code, then processing each frames, and stream the result over the network?
What I’m trying to build is a machine vision IP camera by using my C920 attached to a BB Black.
Could you give me a clue on how to achieve that kind of live view processing?
Cheers,
Sandro
Hi Sandro, Unfortunately, I don’t think the BBB is powerful enough on its own. You might have luck with a separate FPGA board. I have supported the LOGi FPGA Development Board for Beaglebone at Kickstarter by Valent F(x) and I reckon you could achieve your scenario with the help of that board. I’m looking forward to getting mine! Derek.
Thank you, Derek. The LOGi solution sounds really awesome.
I’ll wait for the mass production of it.
For now I’m planning to buy a “micro HDMI to AV” converter and I’m going to try to get the live video from the BBB output. All that I need is a wireless live video with the processed frames.
Without the need for H.264 compression anymore, I could even use my hacked C615 attached to a robot: https://plus.google.com/102102017868635558116/posts/FcduKtKAzjM
I’m looking forward to read your tutorials about the LOGi FPGA shield for BB Black.
Programming an FPGA board sounds to be a really hard job. But you’re one of the bests teachers around. 😉
Hi Derek,
I’m trying to do a wifi controlled robot sending a video stream. I tried to use your source for the udp stream. Everything works fine as long as the BBB is connected with a cable to my notebook. But if I try to use wifi, the stream is very slow. VLC doesn’t even display it. The BBB is sending a frame in about one or two seconds with wifi.
The wifi itself works fine with a good bandwith. I even tried to change the resolution of the sended video but theres no change – still as slow as before and no picture in vlc. (That sounds very strange to me because there should be less data and so it should be faster with more fps…)
Is it possible that the BBB is to weak for this?
Do you have a hint would could go wrong here?
Thank you very much.
Hi Stefan, I will have to try that. In theory it should be fine, but I also had problems streaming stereo video simultaneously over wired Ethernet unless I set the resolution to be very low. The funny thing about the C920 is that it trys to maintain the data rate at a high level. Here is code: http://www.psykokwak.com/blog/images/arobotv4/capture.tgz to change the bitrate to a defined level. It works slowly, but works! Derek.
Hy Derek ! Hy every one ! I am making videowall as a my final eng project.i try a Lot for stream video using udp over network.from master side i successfully stream video and i can see that data is continuously stream but Receiver side when i use code there is always error like failed to connect server,stream is not seekable,time out,so i want to know what can i do for proper stream using udp.is there any necessary for config ffserver config file.reply me as soon as possible
Hey Derek,
i´ve got streams by RTP and UDP running. But as i try to display the stream in an own Android App, there is only RTSP working.
So I tried to Stream with the C920 by RTSP with your file in your Folder on Github.
I Start the Stream by following line (IP is the IP of the Android destination):
root@beaglebone:~/boneCV# ./capture -F -o -c0|avconv -re -i – -vcodec copy -f rtsp rtsp://192.168.178.28:1234
Force Format 2
avconv version v0.8.4, Copyright (c) 2000-2012 the Libav developers
built on Jul 3 2013 19:28:08 with gcc 4.7.3 20130205 (prerelease)
…………………………………………………………………………………………………………………[h264 @ 0x2da80] max_analyze_duration reached
[h264 @ 0x2da80] Estimating duration from bitrate, this may be inaccurate
Input #0, h264, from ‘pipe:’:
Duration: N/A, bitrate: .N/A
Stream #0.0: Video: h264 (Constrained Baseline), yuvj420p, 1920×1080 [PAR 1:1 DAR 16:9], 25 fps, 25 tbr, 1200k tbn, 60 tbc
….Output #0, rtsp, to ‘rtsp://192.168.178.28:1234’:
Metadata:
encoder : Lavf53.21.0
Stream #0.0: Video: libx264, yuvj420p, 1920×1080 [PAR 1:1 DAR 16:9], q=2-31, 90k tbn, 1200k tbc
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Could not write header for output file #0 (incorrect codec parameters ?)
Could you please give me a hint what is wrong in my configuration?!
Thanks a lot in advance.
Hi Thomas, It has been a while since I wrote that tutorial, but I don’t think I ever managed to get RTSP working – I had RTP, and UDP working. I left my attempt code in the repository as a starting point for others. Apologies if you understood otherwise. Derek.
Hello Derek,
When I run ./streamVideoRTP I get two errors… First is that it is not finding my v4l2-ctl which I know I have…
Also it is saying Force Format 2…. VIDIOC_S_FMT error 22….
pipe:: Invalid data found when processing input
Any help is greatly appreciated 🙂
Hi everyone
I have Live multicast stream coming from a LAN switch ..I want to take this stream to my website or blog.can anyone tell me that how i can integrate this stream with my website or blog.
i am currently watching the stream on VLC player on multicast IP via UDP port like udp://@239.194.0.1:6001
plz anyone can guide me
As I posted in the thread on RTP streaming, I am having trouble getting the example scripts in the boneCV package to run. Following shows how I’ve modified the UDP script to work on my setup, and the resulting output when trying to execute:
cat streamVideoUDP
#!/bin/bash
echo “Video Streaming for the Beaglebone – derekmolloy.ie”
echo “Piping the output of capture to avconv”
# Next line not necessary if you are using my -F option on capture
v4l2-ctl –set-fmt-video=width=640,height=480,pixelformat=1
# Pipe the output of capture into avconv/ffmpeg
# capture “-F” My H264 passthrough mode
# “-o” Output the video (to be passed to avconv via pipe)
# “-c0” Capture 0 frames, which means infinite frames in my program
# avconv “-re” read input at the native frame rate
# “-i -” Take the input from the pipe
# “-vcodec copy” Do not transcode the video
# “-f rtp rtp://192.168.1.2:1234/” Force rtp to output to address of my PC on port 1234
./capture -o -c0|avconv -re -i – -vcodec copy -f mpegts udp://192.168.7.2:1234
root@beaglebone:~/boneCV/boneCV-master# ./streamVideoUDP
Video Streaming for the Beaglebone – derekmolloy.ie
Piping the output of capture to avconv
Force Format 0
avconv version 0.8.12-6:0.8.12-1, Copyright (c) 2000-2014 the Libav developers
built on Jun 2 2014 01:31:12 with gcc 4.6.3
………………….[mp3 @ 0x40ae0] Format detected only with low score of 1, misdetection possible!
[mp3 @ 0x429a0] Header missing
Last message repeated 106 timess
[mp3 @ 0x40ae0] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from ‘pipe:’:
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0.0: Audio: mp1, 0 channels, s16
Output #0, mpegts, to ‘udp://192.168.7.2:1234’:
Output file #0 does not contain any stream
I am changing the capture size and so eliminated the -F option from the capture command. I am using these example to learn about streaming and just starting out, so please be gentle, but if somebody could explain these errors to me and point me in the right direction your advice will be very welcome.
Cheers,
Mitch
It looks like the problem here was my camera – got it working with a newer Logitech model.
Thanks again for these great written and video tutorials, and please keep it up!
Could somone plz tell me how to record a UDP multicast stream is there any free software which records automatically if there is any stream
thanks
Hi Derek! I really enjoy your tutorials! Have you tried using camera and LCD in the same project?
Hi Derek! Thanks again for another great, detailed tutorial. Is it possible to capture raw bayer data from the Logitech C920 camera sensor directly?
Hello,
Really great guide! I recently bought a C920 and i was able to live stream to another computer through UDP via BeagleBone Black and Raspberry pi with very nice quality and low cpu utilisation.
I have one minor/annoying issue in both cases, with the Rpi and BBB though. The streamed video output always seems to have a 3 second delay…
I use VLC player on a mac for the playback (which is connected to the embedded devices via wired ethernet).
I tried ffmpeg and avconv with the same passthrough settings as well as lowering the resolution and fps but still that annoying delay of 3 seconds persists.
Is it a software limitation of the v4l2 capturing or hardware limitation? Or maybe a buffer default/ setting of v4l2?
(I will try to stream from a normal pc in a week, just to check the hardware limitation)
Thanks for the great reference and tutorial!
I finally manage to stream from a BBB to a Raspberry Pi B+, over ethernet (1Gbps router).
I happen to have the same annoyance Kostas reports, of some lagging seconds, from 25 to 0.2… Really!
I’m using “omxplayer -o hdmi udp://226.0.0.1:1234” on the RPi to decode the streaming, and my only question is if there is a way to do something on either side (receiver or transmitter) to assure the minimum delay.
25 seconds some times? Really? Where is the data being stored? Is it “in transit” ? 25 x 3Mbps = 75 Mb…
Best regards,
Hi Derek, I am working with a Logitech C270 webcam on 1280×720 MJPEG format. Changed your code on capture.c on raw2mpg4 and so on so that it could support my camera. Being able to capture video and convert the raw to mp4 tried to go on UDPcasting.
Well that didn’t work so well. I seem to transmit from BBB but not receive from VLC.
Think it’s a matter of VLC? Not supporting MJPEG format. No errors pop-up apart from “Format mjpeg detected only with low score of 25, misdetection possible!”. Any advice? Thank you in advance..
Nick.
help please,
I’m trying to get video streaming on VLC. I followed all the steps, modified the scripts and everything, but the streaming is NOT displayed on VLC. I’m using the same camara C920 and SSH terminal. I was able to capture output.raw, move it to my desktop machine using PSFTP and display the video on VLC ( however, raw2mpeg doesn’t work to convert the raw file to mp4 (ffmpeg doesn’t work, so I had to use avcon -f h264 -i output.raw -vcodec copy output.mp4). then I foolowd all the steps for RTP streaming. streamVideoRPT works…there”s no error, I can see the traffic, the sdp file shows on desktop and VLC, I hit play and nothing happen. same story with streamVideoUDP. What can I possibly be doing wrong. ?
Please help !!
Hi Rudy, I’m afraid that I don’t know. It may be a network configuration problem, but it is not clear what is going wrong. Derek.
hi. i need to stream unicast via vlc. if anyone can help me out???
Everithing is OK, but delay of 3 seconds is a problem. Similar to MPG4 in other câmeras. Maybe interframes compression is impossible without delay?
hello,
i tried the same setup for streaming audio: However, after 3h20 the streaming stops..Iguess the recording stops…
Have you any idea why or how to create a 24h streaming. We want to use a streaming multicast for in our cinema’s screen for the replacement of analoque cd player music…
command:
sudo arecord -d 0 -D plughw:1,0 -f S16_LE -c 2 -r 44100 -t wav | avconv -i pipe:0 -acodec mp2 -ac 2 -loglevel debug -f mpegts udp://239.0.0.1:1234;
screen print during the start of avconv:
Input #0, wav, from ‘pipe:0’:
Duration: 03:22:53.94, bitrate: N/A
Stream #0.0, 218, 1/44100: Audio: pcm_s16le, 44100 Hz, 2 channels, s16, 1411 kb/s
I just got this up and running. I am using a wifi adapter TP-LINK TL-WN722N to connect my BBB to the home. The video freezes and then resumes after a delay with some distortion. This happens repeatedly. What causes this? Congestion in the network?
These modifications to VLC seem to resolve the problem: https://www.vlchelp.com/stop-hd-video-from-freezing-vlc/
I can’t believe it’s now finally released. This kind of video streaming is such a wonderful to your own Desktops.
Dear Derek,
when i run streamVideoUDP i get the following:
debian@beaglebone:/dev/boneCV$ sudo ./streamVideoUDP
Video Streaming for the Beaglebone – derekmolloy.ie
Piping the output of capture to avconv
./streamVideoUDP: line 17: ./capture: No such file or directory
./capture: No such file or directoryffmpeg version 3.2.14-1~deb9u1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
configuration: –prefix=/usr –extra-version=’1~deb9u1′ –toolchain=hardened –libdir=/usr/lib/arm-linux-gnueabihf –incdir=/usr/include/arm-linux-gnueabihf –enable-gpl –disable-stripping –enable-avresample –enable-avisynth –enable-gnutls –enable-ladspa –enable-libass –enable-libbluray –enable-libbs2b –enable-libcaca –enable-libcdio –enable-libebur128 –enable-libflite –enable-libfontconfig –enable-libfreetype –enable-libfribidi –enable-libgme –enable-libgsm –enable-libmp3lame –enable-libopenjpeg –enable-libopenmpt –enable-libopus –enable-libpulse –enable-librubberband –enable-libshine –enable-libsnappy –enable-libsoxr –enable-libspeex –enable-libssh –enable-libtheora –enable-libtwolame –enable-libvorbis –enable-libvpx –enable-libwavpack –enable-libwebp –enable-libx265 –enable-libxvid –enable-libzmq –enable-libzvbi –enable-omx –enable-openal –enable-opengl –enable-sdl2 –enable-libdc1394 –enable-libiec61883 –enable-chromaprint –enable-frei0r –enable-libopencv –enable-libx264 –enable-shared
libavutil 55. 34.101 / 55. 34.101
libavcodec 57. 64.101 / 57. 64.101
libavformat 57. 56.101 / 57. 56.101
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libavresample 3. 1. 0 / 3. 1. 0
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
pipe:: Invalid data found when processing input
any ideas what i should do?
Thanks,
Giorgos