async def get(self): stream = self.request.uri.replace("/manifest/", "") print("stream : " + stream, flush=True) user = self.request.headers.get('X-USER') if not user: self.set_status(400, "X-USER missing in headers") return # Redirect if this is an AD stream. #if stream.find("/adstream/") != -1: # self.set_header('X-Accel-Redirect','/adinsert/' + stream) # self.set_status(200,'OK') # return # Retrive the manifest from upstream. try: manifest = await self._fetch_manifest(content_provider_url + "/" + stream) except Exception as e: print(str(e)) self.set_status(500, str(e)) return # launch zk stream_base = "/".join(stream.split("/")[:-1]) zk_path = zk_prefix + "/" + stream_base # Parse manifest minfo = {"segs": {}, "streams": {}, "manifest": ""} ad_spec = { "prefix": "adstream/" + user, "path": ad_storage_root + "/" + stream_base, "interval": [4], # ad interval (#segments) "duration": [5], # ad duration } if stream.endswith(".m3u8"): zk = ZKData() minfo = parse_hls( stream_cp_url=content_provider_url + "/" + stream, m3u8=manifest, stream_info=zk.get(zk_path + "/" + stream.split("/")[-1]), ad_spec=ad_spec, ) zk.close() if stream.endswith(".mpd"): minfo = parse_dash( stream_cp_url=content_provider_url + "/" + stream, mpd=manifest, ad_spec=ad_spec, ) # set zk states self.executor.submit(self._set_states, minfo, zk_path, stream_base, user) # return the manifest self.write(minfo["manifest"]) self.set_header('content-type', minfo["content-type"])
def _get_manifest(self, stream, user): # Retrive the manifest from upstream. try: r = requests.get(content_provider_url + "/" + stream) r.raise_for_status() manifest = r.text except Exception as e: print("Exception: " + str(e), flush=True) return str(e) # launch zk stream_base = "/".join(stream.split("/")[:-1]) zk_path = zk_manifest_prefix + "/" + stream_base # Parse manifest minfo = {"segs": {}, "streams": {}, "manifest": ""} ad_spec = { "prefix": "adstream/" + user, "path": ad_storage_root + "/" + stream_base, "interval": ad_interval, # ad interval (#segments) "duration": ad_duration, # ad duration "analytic_ahead": ad_analytic_ahead, "transcode_ahead": ad_transcode_ahead, } zk = ZKData() if stream.endswith(".m3u8"): minfo = parse_hls( stream_cp_url=content_provider_url + "/" + stream, m3u8=manifest, stream_info=zk.get(zk_path + "/" + stream.split("/")[-1]), ad_spec=ad_spec, ad_segment=ad_segment) if stream.endswith(".mpd"): minfo = parse_dash(stream_cp_url=content_provider_url + "/" + stream, mpd=manifest, ad_spec=ad_spec, ad_segment=ad_segment) # set zk states if minfo["streams"]: for stream1 in minfo["streams"]: zk.set(zk_path + "/" + stream1, minfo["streams"][stream1]) if minfo["segs"]: for seg in minfo["segs"]: zk.set(zk_path + "/" + user + "/" + seg, minfo["segs"][seg]) zk.close() return minfo