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 curl for HTTP requests and ffmpeg for video transcoding. Replace sample access tokens and stream addresses with your own. 

Producer

Using the generic bash producer script from our example Git repository, we open a shell and run

export ZEBRASTREAM_PATH='/9b09a7ce-024d-4836-ab95-90b776dfd439/mystream'
export ZEBRASTREAM_ACCESSTOKEN='kie0EesheiV3Aewoh4utiYah'
export ZEBRASTREAM_CONTENT_TYPE='video/mp4'
./zebrastream-producer.bash ffmpeg -hide_banner -loglevel error -f v4l2 -i /dev/video0 -c:v libx264 -preset ultrafast -tune zerolatency -g 30 -b:v 500k -an -f mp4 -movflags frag_keyframe+empty_moov+default_base_moof -t 30 -

In short

  • 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)
  • start the generic bash producer script
  • instruct the producer script to run ffmpeg with corresponding parameters when data is requested

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) and open the following stream: https://connect.zebrastream.io/v0/9b09a7ce-024d-4836-ab95-90b776dfd439/mystream?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.