python http streaming via multisocketsink with gstreamer

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

python http streaming via multisocketsink with gstreamer

Melli Mails

I would like to stream a single source to multiple clients via http.
Clients should be able to connect and disconnect while streaming is
already ongoing.

I am aware of http launch but I would prefer to do this in python
because I would like to add additional functionality later that easier
for me to write in python.

I use the gstreamer packages that come with raspbian Jessie (GStreamer
1.4.4) on a Raspberry Pi 2

My questions:
- I cannot stream to more than one client with the code below and I
would like to know what I need to change to make it work
- If I stop my single client and try to reconnect afterwards, the stream
does not start again. What needs to be done to fix this?
- the code does not even work for a single client if I use raspbian
stretch (which comes with gstreamer 1.10.4). Any idea what needs to be

Thank you for your help!

Best regards,

here is the code:

#!/usr/bin/env python
import os,signal,sys,time
import gi,BaseHTTPServer,SocketServer

gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gio, Gst

# Initialize the gstreamer toolchain

# reopen stdout in non-buffered mode
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

class gst_broadcaster:

     def __init__(self, gpipeline):
         self.pipeline = Gst.parse_launch(gpipeline)
         self.source = self.pipeline.get_by_name('source')
         self.sink = self.pipeline.get_by_name('sink')
         self.bus = self.pipeline.get_bus()
         self.clients = []

     def kill(self):

     def add_client(self,sink,gsock):

     def remove_client(self,sink,gsock):
         if gsock in self.clients:

     def handle(self, wfile):
         gsock = Gio.Socket().new_from_fd(wfile.fileno())
         self.sink.emit("add", gsock)
         while gsock in self.clients:

     def message_handler(self,bus,message):
         msgType = message.type
         if msgType == Gst.MessageType.ERROR:
             print "\n Unable to play Video. Error: ", \
         elif msgType == Gst.MessageType.EOS:

def get_pipeline():
     r= "videotestsrc name=source is-live=true ! x264enc "
     r+= ' ! h264parse ! matroskamux name=mux streamable=true !
multisocketsink name=sink'
     return r

def get_player():
     pipeline = get_pipeline()
     player =  gst_broadcaster(pipeline)
     return player

class MyHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):

     def do_HEAD(self):
         self.send_header("Content-type", "video/x-matroska")
     def do_GET(self):

class ThreadedHTTPServer(SocketServer.ThreadingMixIn,
     """Handle requests in a separate thread."""

port = 8001
httpd = ThreadedHTTPServer(("", port), MyHTTPHandler)

def httpd_start():
     print time.asctime(), "server start (port %i)" % port

def httpd_shutdown():
     print time.asctime(), "server stop"

def signal_handler(signal, frame):

if __name__ == '__main__':
     signal.signal(signal.SIGINT, signal_handler)
     signal.signal(signal.SIGTERM, signal_handler)
     player = get_player()
     except KeyboardInterrupt:
gstreamer-devel mailing list
[hidden email]