aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/livestreamer/stream/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/livestreamer/stream/__init__.py')
-rw-r--r--src/livestreamer/stream/__init__.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/livestreamer/stream/__init__.py b/src/livestreamer/stream/__init__.py
new file mode 100644
index 0000000..235d2f7
--- /dev/null
+++ b/src/livestreamer/stream/__init__.py
@@ -0,0 +1,65 @@
+from ..compat import str
+
+import os
+import pbs
+import time
+import tempfile
+
+class StreamError(Exception):
+ pass
+
+class Stream(object):
+ """
+ This is a base class that should be inherited when implementing
+ different stream types. Should only be used directly from plugins.
+ """
+
+ def __init__(self, session):
+ self.session = session
+
+ def open(self):
+ """
+ Opens a connection to the stream.
+ Returns a file-like object than can be used to read data.
+ Raises :exc:`StreamError` on failure.
+ """
+ raise NotImplementedError
+
+class StreamProcess(Stream):
+ def __init__(self, session, params={}):
+ Stream.__init__(self, session)
+
+ self.params = params
+ self.params["_bg"] = True
+ self.params["_err"] = open(os.devnull, "w")
+ self.errorlog = self.session.options.get("errorlog")
+
+ def cmdline(self):
+ return str(self.cmd.bake(**self.params))
+
+ def open(self):
+ if self.errorlog:
+ tmpfile = tempfile.NamedTemporaryFile(prefix="livestreamer",
+ suffix=".err", delete=False)
+ self.params["_err"] = tmpfile
+
+ stream = self.cmd(**self.params)
+
+ # Wait 0.5 seconds to see if program exited prematurely
+ time.sleep(0.5)
+ stream.process.poll()
+
+ if stream.process.returncode is not None:
+ if self.errorlog:
+ raise StreamError(("Error while executing subprocess, error output logged to: {0}").format(tmpfile.name))
+ else:
+ raise StreamError("Error while executing subprocess")
+
+ return stream.process.stdout
+
+
+from .http import HTTPStream
+from .rtmpdump import RTMPStream
+
+__all__ = ["StreamError", "Stream", "StreamProcess",
+ "RTMPStream", "HTTPStream"]