vod-rtmp/frontend/app/zomstream.py

77 lines
2.5 KiB
Python
Raw Permalink Normal View History

2021-10-10 18:57:54 -04:00
import pathlib
import xml.etree.ElementTree as etree
import sys
import yaml
import urllib
class Stream:
def __init__(self, app, name, urls):
self.name = name # String
self.app = app # String
self.urls = urls # List of Dictionaries with the keys url and type
class Zomstream:
def __init__(self):
# load configuration from config.yml file
if pathlib.Path("config.yml").is_file():
stream = open('config.yml', 'r')
self.configuration = yaml.load(stream)
stream.close()
else:
print('missing configuration.')
sys.exit(1)
self.streamnames = []
def getStreamNames(self):
self.streamnames = []
# get data from the streaming server
response = urllib.request.urlopen(self.configuration['stat_url'])
content = response.read().decode('utf-8')
# parse the xml / walk the tree
tree = etree.fromstring(content)
server = tree.find('server')
applications = server.findall('application')
for application in applications:
appname = application.find('name')
if appname.text == "hls" or appname.text == "rec":
2021-10-10 18:57:54 -04:00
streams = application.find('live').findall('stream')
for stream in streams:
name = stream.find('name')
rate = stream.find('bw_video')
if rate.text != "0":
self.streamnames.append( [appname.text, name.text] )
2021-10-10 18:57:54 -04:00
return self.streamnames
def getStreams(self):
streams = []
for streamName in self.getStreamNames():
urls = []
app = streamName[0]
name = streamName[1]
2021-10-11 16:14:01 -04:00
hls_url = self.getHlsUrl (app,name)
2021-10-10 18:57:54 -04:00
rtmp_url = self.getRtmpUrl(app,name)
2021-10-11 16:14:01 -04:00
urls.append({'url': hls_url, 'type':'hls'})
2021-10-10 18:57:54 -04:00
urls.append({'url': rtmp_url,'type':'rtmp'})
stream = Stream(app=app, name=name, urls=urls)
streams.append(stream.__dict__)
return streams
2021-10-11 16:14:01 -04:00
def getHlsUrl(self,app_name,stream_name):
return '%s://%s/hls/%s/index.m3u8' % (
2021-10-10 18:57:54 -04:00
self.configuration['web_proto'],
self.configuration['base_url'],
stream_name)
2021-10-11 16:14:01 -04:00
2021-10-10 18:57:54 -04:00
def getRtmpUrl(self,app_name,stream_name):
return "rtmp://%s/%s/%s" % (
self.configuration['rtmp_base'],
app_name,
stream_name)