Commit 64edd281 authored by Olivier Crête's avatar Olivier Crête

Make PEP8 compliant

parent 43db14d0
......@@ -2,7 +2,8 @@
# First pip3 install aiohttp & gbulb
import sys, os
import sys
import os
from aiohttp import web
import gbulb
......@@ -11,10 +12,13 @@ import gi.repository
gi.require_version('Gst', '1.0')
from gi.repository import Gst
class TranscodeOutputCreationError(Exception):
pass
class TranscodeOutput:
def __init__(self, id):
self.id = id
......@@ -31,10 +35,11 @@ class TranscodeOutput:
def get_routes():
return []
class TranscodeOutputHls(TranscodeOutput):
instances = []
def __init__(self, id, params):
def __init__(self, id, params):
TranscodeOutput.__init__(self, id)
self.__muxer = Gst.ElementFactory.make("hlssink2")
......@@ -56,7 +61,7 @@ class TranscodeOutputHls(TranscodeOutput):
self.__muxer.set_state(Gst.State.NULL)
def link_stream(self, p, pad):
caps = pad.query_caps() # Why is pad.get_current_caps() not working ?
caps = pad.query_caps() # Why is pad.get_current_caps() not working ?
if caps.get_structure(0).has_name("video/x-raw"):
enc = Gst.ElementFactory.make("x264enc")
parser = Gst.ElementFactory.make("h264parse")
......@@ -80,7 +85,8 @@ class TranscodeOutputHls(TranscodeOutput):
@staticmethod
def get_routes():
return [web.get("/{port}/{id}/{file}", TranscodeOutputHls.__get_output)]
return [web.get("/{port}/{id}/{file}",
TranscodeOutputHls.__get_output)]
@staticmethod
def __get_output(request):
......@@ -101,8 +107,10 @@ class TranscodeOutputHls(TranscodeOutput):
'type': 'hls',
'hlspath': self.hls_path}
class TranscodeOutputTsUdp(TranscodeOutput):
def __init__(self, id, params):
def __init__(self, id, params):
TranscodeOutput.__init__(self, id)
if 'port' not in params:
......@@ -114,7 +122,9 @@ class TranscodeOutputTsUdp(TranscodeOutput):
raise TranscodeOutputCreationError("TS-UDP output port invalid")
self.out_host = str(params['host'])
self.__sink = Gst.parse_bin_from_description("mpegtsmux alignment=7 ! udpsink port=%d host=\"%s\" " % (self.out_port, self.out_host), True)
self.__sink = Gst.parse_bin_from_description(
"mpegtsmux alignment=7 ! udpsink port=%d host=\"%s\" " %
(self.out_port, self.out_host), True)
self.added = False
def __add_muxer_to_pipeline(self, p):
......@@ -129,7 +139,7 @@ class TranscodeOutputTsUdp(TranscodeOutput):
self.__sink.set_state(Gst.State.NULL)
def link_stream(self, p, pad):
caps = pad.query_caps() # Why is pad.get_current_caps() not working ?
caps = pad.query_caps() # Why is pad.get_current_caps() not working ?
if caps.get_structure(0).has_name("video/x-raw"):
enc = Gst.ElementFactory.make("x264enc")
parser = Gst.ElementFactory.make("h264parse")
......@@ -157,7 +167,9 @@ class TranscodeOutputTsUdp(TranscodeOutput):
'port': self.out_port,
'host': self.out_host}
class TranscodePipeline:
def __init__(self, port):
# stored params
self.port = port
......@@ -171,7 +183,7 @@ class TranscodePipeline:
# the pipeline
self.p = Gst.Pipeline()
self.src = Gst.ElementFactory.make("uridecodebin3")
self.src.props.uri = "udp://0.0.0.0:"+str(port)
self.src.props.uri = "udp://0.0.0.0:" + str(port)
self.src.connect("pad-added", self.__src_pad_added)
self.p.add(self.src)
......@@ -180,9 +192,10 @@ class TranscodePipeline:
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("message::error", self.__bus_error)
bus.connect("sync-message::stream-collection", self.__bus_stream_collection)
bus.connect("sync-message::stream-collection",
self.__bus_stream_collection)
def __run (self):
def __run(self):
ret = self.p.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
self.p.set_state(Gst.State.NULL)
......@@ -191,11 +204,10 @@ class TranscodePipeline:
def __bus_error(self, bus, msg):
(gerr, dbg) = msg.parse_error()
print (gerr.message)
print (dbg)
print(gerr.message)
print(dbg)
self.error = dbg
def __bus_stream_collection(self, bus, msg):
c = msg.parse_stream_collection()
selected_streams = []
......@@ -206,23 +218,24 @@ class TranscodePipeline:
selected_streams += [s.get_stream_id()]
self.src.send_event(Gst.Event.new_select_streams(selected_streams))
def __src_pad_added (self, element, pad):
def __src_pad_added(self, element, pad):
for o in self.outputs:
self.outputs[o].link_stream(self.p, pad)
def add_output (self, output):
def add_output(self, output):
self.outputs[output.id] = output
output.set_port(self.port)
for pad in self.src.iterate_src_pads():
output.link_stream (self.p, pad)
output.link_stream(self.p, pad)
return self.__run()
class TranscodeServer:
def __init__(self):
self.app = web.Application()
self.app.add_routes([web.put('/{port}',self.create)])
self.app.add_routes([web.get('/{port}',self.get)])
self.app.add_routes([web.put('/{port}', self.create)])
self.app.add_routes([web.get('/{port}', self.get)])
self.app.add_routes(TranscodeOutputHls.get_routes())
self.pipelines = {}
......@@ -258,7 +271,7 @@ class TranscodeServer:
output = TranscodeOutputTsUdp(id, params)
except TranscodeOutputCreationError as e:
raise web.HTTPBadRequest(text=str(e))
if output and not p.add_output (output):
if output and not p.add_output(output):
raise web.HTTPBadRequest(text='Could not start pipeline')
self.pipelines[port] = p
......@@ -277,7 +290,7 @@ class TranscodeServer:
for o in self.pipelines[port].outputs:
out_list += [self.pipelines[port].outputs[o].get_info()]
return web.json_response({"port": port, "outputs": out_list })
return web.json_response({"port": port, "outputs": out_list})
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment