This story presents a simple use case with a webcam as a producer and a video stream player as a consumer. The webcam is attached to a computer, which connects to ZebraStream and waits for a data request from the consumer. The video play runs on a mobile device, such as a phone or tablet, on a completely different network. When video player is started, it loads the stream URL with corresponding settings and triggers a data request. The producer then switches on the webcam and delivers the video stream, which is instantly played on the mobile device. From the receiver point of view, this functionality may seem standard. However, the lack of an intermediate server and the overall simplicity of integration on both sides is exemplary for the design of ZebraStream. This code examples here use a Linux shell with our streaming CLI zebrastream and ffmpeg for video transcoding. Replace sample access tokens and stream addresses with your own.

Producer

Using the streaming CLI, we just create a YAML configuration file called webcam.yaml:

stream_path: /userspace/webcam
access_token: kie0EesheiV3Aewoh4utiYah
content_type: video/mp4
mode: write
command: ffmpeg -hide_banner -loglevel error -f v4l2 -i /dev/video0 -c:v libx264 -preset ultrafast -tune zerolatency -g 30 -b:v 500k -an -flags +cgop -sc_threshold 0 -flush_packets 1 -f mp4 -movflags frag_keyframe+empty_moov+default_base_moof pipe:1

These ffmpeg parameter should be adjusted to your needs. Then just run

zebrastream --config-file webcam.yaml write

In short, we

  • set the stream address
  • specify a write access token for the stream address
  • specify a content type (only needed for browsers to play the video directly)
  • instruct the producer to run ffmpeg with corresponding parameters when data is requested
  • start the producer

Without going into details (you can just read the ffmpeg documentation), the arguments tell ffmpeg to read from the first Linux video device, to convert the video to H264 in a browser-compatible profile, to feed the video stream into it's standard output, and to stop after 30 seconds (-t 30). Feel free to adjust parameters like profile, bitrate etc.

Consumer

We install VLC on Android (available on Google Play store of F-droid), or just use a browser like Firefox on the desktop, and open the following stream URL: https://connect.zebrastream.io/v0/userspace/webcam?accesstoken=kie0EesheiV3Aewoh4utiYah&mode=await-writer&timeout=10&redirect=true.

For details, refer to the 'Push and Pull' tutorial in our knowledge base. The video player then connects to ZebraStream, waiting for 10 seconds for the producer to open the channel, and starts transmitting video data to display.

Use Cases and Variations

There are many simple uses cases for an on-demand camera stream like this one: event stream, baby camera etc. You can bookmark the consumer link on a TV set-top box and turn on a remote camera using the remote control. You can also have a live display waiting for data by removing the timeout parameter in the consumer and wrapping it in a loop that connects after each session. In this case, the display will start playing the video when someone starts the producer script, possibly by pressing a button. Modify the video encoding and add a microphone and audio track to it. Or you can add some kind of live video processing, such as object recognition, before feeding the video stream into ZebraStream.