from threading import Thread

def is_alive(subprocess):
    return True if (subprocess and subprocess.poll() is None) else False

class Streamer:
    def __init__(self):
        self.run = False
        self.thread = None

    def is_streaming(self):
        return True if (self.thread and self.thread.is_alive()) else False

    def start_stream(self):
        if self.is_streaming():
            raise RuntimeError('Stream thread is already running')

        self.run = True
        self.thread = Thread(target=self._stream_thread, daemon=True, args=())
        self.thread.start()

    def end_stream(self):
        if self.thread is None:
            raise RuntimeError('No stream thread to terminate')

        self.run = False
        self.thread.join()
        self.thread = None
