Friday, 8 March 2013

The worlds ugliest webm streaming webserver?

I was trying to find an easy way to get low latency video from a webcam to a remote browser today. Requirements:

  1. Quick deployment
  2. Runs on Raspberry Pi
  3. Runs with out-of-the-box debs (see quick deployment)
My hacky solution was a Django python app that uses the StreamingHttpResponse class, gstreamer and a pipe. Disguisting, but works well. Sadly, latency is about 10 seconds to localhost so its not exactly live... 

import pygst
pygst.require("0.10")
import gst
def grab(request):
  """ Return a webm live stream from the first attached webcam. """
  class VideoStreamer():
    def __init__(self):
      self.pipein, self.pipeout = os.pipe()
      self.player = gst.parse_launch ("v4l2src ! video/x-raw-yuv,width=640,height=480,framerate=10/1 ! ffmpegcolorspace ! vp8enc max-latency=1 lag-in-frames=1 ! webmmux name='m' streamable=true ! fdsink fd=%d" % self.pipeout)
      self.player.set_state(gst.STATE_PLAYING)
    def start(self):
      fd = os.fdopen(self.pipein)
      try:
        while True:
          yield fd.read(4096)
      except Exception, e:
        print "Exception was ", e
    def __del__(self):
      self.player.set_state(gst.STATE_NULL)
      os.close(self.pipeout)
  return StreamingHttpResponse(VideoStreamer().start(), content_type="video/webm")

Requires pygst and django 1.5 (use pip).

No comments:

Post a Comment