Friday, June 14, 2019

Raspberry PI & Kodi: Point-to-Point Video Live Streaming via FFMPEG

Many of us recall the joy of 1st week of grade school -- new fat pencils & erasers, rulers, different papers & the odd smell of 16 new Crayola crayons!  Unspoken in grade school? You were learning to color / draw / create / capture ideas & abstractions to paper.

In the world of the Internet -- and video capable smartphones, Video is the new "paper" -- Learning to color / draw / create / capture ideas & abstractions on this new "moving paper" is a necessary & essential skill.

Background: Recently this author has been learning to setup live streaming for tutoring, home schooling, "live scene" broadcast, big screen TV sharing, remote teaching, video conferencing and related.  Rather than 10,000 words, perhaps a diagram of "Studio" of video sources and destinations for minimal setup:


Figure 1: Basic Studio Setup
(click for larger)

Challenge?  Under $400 USD budget.  Equipment: (a) Utilize 8+ year old $150 laptops and low cost, $35 WebCam, $45  Raspberry Pi platforms.  (b) Transmit video / audio to Kodi media center for "big screen" TV sharing with classrooms and assembled groups. (c) Avoid expensive or proprietary solutions with monthly fees, (d) Finally, recast & stream the video to "live" cloud services like Twitch or YouTube for tutoring & teaching anywhere on Internet.

Part #1: Test video sources for end-to-end network function checks.  Why NOT just use live video sources? Just stream a webcam?  Many small studios use "just a webcam" to test end-to-end -- AND the webcam ADDS EXTRA COMPLEXITY that can fail. A fail resistant video source is required.  If a test video signal can succeed in traversing the network connections, the odds increase that other video streams can and will work.  Slide shows, rolling video, live webcams, etc.

History: This author worked at a PBS TV station in early 1980s. Every TV station & studio has stable, ALWAYS AVAILABLE "video signal generators" (colorbars) and "Station ID" slides (test cards) -- for routine video feed testing / switching -- and often for "fail over" -- when a "live scene" went sour -- or live camera broke -- or when scheduled satellite video feed dropped out -- or the pre-recorded program on Video Tape failed or became "too noisy" for tolerable viewing.  

"Home school" TV studios, online classrooms & web conferencing need similar stable sources -- often to provide stable startup video feed (sign on) -- as new viewers join at random times -- and to uniquely Identify the live feed as "the right one." ("Hey, if you see Sunflower icon with countdown, you have found kickstart of our live video feed!").
  
Very simple & robust video source & kickstart:  Use FFMPEG to send color bars & 440 Hz ticking tone to laptop or BigScreen TV.  This will  test "in house" network connections (i.e. 'Is our LAN is "passing" the live video?').   Referencing Figure-1 above -- and from the command line on the Raspberry Pi B+ (LAN host IP 192.168.1.66) -- running a recent version of Debian -- issue this command: 

/usr/bin/ffmpeg -re \
  -f lavfi -i testsrc=size=288x160:rate=10 \
  -f lavfi -i "sine=frequency=440:sample_rate=22050:beep_factor=4" \
  -f mpegts udp://192.168.1.71:1234

To receive the transmitted test video, on the old laptop (LAN host IP 192.168.1.71) -- running some recent version of Linux -- from the command line issue this command:

/usr/bin/ffplay udp://127.0.0.1:1234
# -- or ---
/usr/bin/mpv udp://127.0.0.1:1234

In 5-30 seconds, the color bars and 440 Hz tone should pop up on the laptop screen as detailed in this live test video.  There will be error & caution messages from FFPLAY or MPV -- this is normal -- as the video stream format (mpegts) sends full index frames only intermittently: 


Successful local network 'broadcast' test 
of color bars and 440 tone.

Part #2: Sending test signal to media player Kodi & BigScreen TV -- host IP 192.168.1.80 in Figure-1.  Using a plain text editor (Windoze Notepad, VIM, Nano etc) -- create a "STRM" text file named "play-me.strm" with this single line: 

udp://127.0.0.1:1234
 Save this file on your Kodi media player is some easy to locate folder -- for example "/storage/videos/stream_play" -- and -- from the Raspberry Pi (host 192.168.1.66 in Figure-1) -- again issue the FFMPEG command -- but with the receiving host changed from old laptop (host 192.168.1.71) to new target host Kodi (IP 192.168.1.80) -- only the "71" changes to "80" to redirect the video test stream to a new target:

/usr/bin/ffmpeg -re \
  -f lavfi -i testsrc=size=288x160:rate=10 \
  -f lavfi -i "sine=frequency=440:sample_rate=22050:beep_factor=4" \
  -f mpegts udp://192.168.1.71:1234

Receiving video signal on Kodi / BigScreen TV: Using the graphical interface on Kodi -- navigate to the folder "/storage/video/stream_play" -- and select file "play-me.strm" -- Kodi will open the STRM file -- and a rotating circle will manifest -- indicating Kodi is attempting to "read" and "sync" on the video stream.  This may require 10-50 seconds to succeed.

Key Caveat: IP host numbers will vary in your studio setup.  The KEY IDEA? The test video signal can be "switched" and re-targeted in your studio -- from sending to old laptop -- to Big Screen TV -- or out to live stream servers in the Internet cloud.  And if the test video signal can be re-targeted -- any slide sequence video or webcam feed can be re-targeted.

Up Next: How to live stream video slides & audio via FFMPEG.