Beispiel #1
0
 def make_stream(cls, settings):
     stream = cls()
     stream._settings = settings
     stream.input = InputUrls(urls=[InputUrl(id=InputUrl.generate_id())])
     stream.output = OutputUrls(
         urls=[OutputUrl(id=OutputUrl.generate_id())])
     return stream
Beispiel #2
0
 def make_stream(cls, settings):
     stream = cls()
     stream._settings = settings
     stream.visible = False
     stream.input = InputUrls(urls=[InputUrl(id=InputUrl.generate_id())])
     stream.output = OutputUrls(urls=[OutputUrl(id=OutputUrl.generate_id(), uri=constants.DEFAULT_TEST_URL)])
     return stream
Beispiel #3
0
    def get_data(self) -> InputUrls:
        urls = InputUrls()
        for url in self.data['urls']:
            input = InputUrl(url['id'], url['uri'], url['user_agent'],
                             url['stream_link'], url['proxy'])
            urls.urls.append(input)

        return urls
Beispiel #4
0
class HardwareStream(IStream):
    log_level = IntField(default=StreamLogLevel.LOG_LEVEL_INFO, required=True)

    input = EmbeddedDocumentField(InputUrls, default=InputUrls())
    have_video = BooleanField(default=constants.DEFAULT_HAVE_VIDEO,
                              required=True)
    have_audio = BooleanField(default=constants.DEFAULT_HAVE_AUDIO,
                              required=True)
    audio_select = IntField(default=constants.INVALID_AUDIO_SELECT,
                            required=True)
    loop = BooleanField(default=constants.DEFAULT_LOOP, required=True)
    avformat = BooleanField(default=constants.DEFAULT_AVFORMAT, required=True)
    restart_attempts = IntField(default=constants.DEFAULT_RESTART_ATTEMPTS,
                                required=True)
    auto_exit_time = IntField(default=constants.DEFAULT_AUTO_EXIT_TIME,
                              required=True)

    # runtime
    _status = StreamStatus.NEW
    _cpu = 0.0
    _timestamp = 0
    _idle_time = 0
    _rss = 0
    _loop_start_time = 0
    _restarts = 0
    _start_time = 0
    _input_streams = str()
    _output_streams = str()

    def __init__(self, *args, **kwargs):
        super(HardwareStream, self).__init__(*args, **kwargs)

    def get_type(self):
        raise NotImplementedError('subclasses must override get_type()!')

    def reset(self):
        self._status = StreamStatus.NEW
        self._cpu = 0.0
        self._timestamp = 0
        self._idle_time = 0
        self._rss = 0
        self._loop_start_time = 0
        self._restarts = 0
        self._start_time = 0
        self._input_streams = str()
        self._output_streams = str()

    def update_runtime_fields(self, params: dict):
        assert self.get_id() == params[StreamFields.ID]
        assert self.get_type() == params[StreamFields.TYPE]
        self._status = StreamStatus(params[StreamFields.STATUS])
        self._cpu = params[StreamFields.CPU]
        self._timestamp = params[StreamFields.TIMESTAMP]
        self._idle_time = params[StreamFields.IDLE_TIME]
        self._rss = params[StreamFields.RSS]
        self._loop_start_time = params[StreamFields.LOOP_START_TIME]
        self._restarts = params[StreamFields.RESTARTS]
        self._start_time = params[StreamFields.START_TIME]
        self._input_streams = params[StreamFields.INPUT_STREAMS]
        self._output_streams = params[StreamFields.OUTPUT_STREAMS]

    def to_dict(self) -> dict:
        front = super(HardwareStream, self).to_dict()
        front[StreamFields.STATUS] = self._status
        front[StreamFields.CPU] = self._cpu
        front[StreamFields.TIMESTAMP] = self._timestamp
        front[StreamFields.IDLE_TIME] = self._idle_time
        front[StreamFields.RSS] = self._rss
        front[StreamFields.LOOP_START_TIME] = self._loop_start_time
        front[StreamFields.RESTARTS] = self._restarts
        front[StreamFields.START_TIME] = self._start_time
        front[StreamFields.INPUT_STREAMS] = self._input_streams
        front[StreamFields.OUTPUT_STREAMS] = self._output_streams
        # runtime
        work_time = self._timestamp - self._start_time
        quality = 100 - (100 * self._idle_time /
                         work_time) if work_time else 100
        front[StreamFields.QUALITY] = quality
        return front

    def config(self) -> dict:
        conf = super(HardwareStream, self).config()
        conf[ConfigFields.FEEDBACK_DIR_FIELD] = self.generate_feedback_dir()
        conf[ConfigFields.LOG_LEVEL_FIELD] = self.get_log_level()
        conf[ConfigFields.AUTO_EXIT_TIME_FIELD] = self.get_auto_exit_time()
        conf[ConfigFields.LOOP_FIELD] = self.get_loop()
        conf[ConfigFields.AVFORMAT_FIELD] = self.get_avformat()
        conf[ConfigFields.HAVE_VIDEO_FIELD] = self.get_have_video()  # required
        conf[ConfigFields.HAVE_AUDIO_FIELD] = self.get_have_audio()  # required
        conf[ConfigFields.RESTART_ATTEMPTS_FIELD] = self.get_restart_attempts()
        conf[ConfigFields.INPUT_FIELD] = self.input.to_mongo(
        )  # required empty in timeshift_player

        audio_select = self.get_audio_select()
        if audio_select != constants.INVALID_AUDIO_SELECT:
            conf[ConfigFields.AUDIO_SELECT_FIELD] = audio_select
        return conf

    def generate_feedback_dir(self):
        return '{0}/{1}/{2}'.format(self._settings.feedback_directory,
                                    self.get_type(), self.get_id())

    def generate_http_link(self,
                           playlist_name=constants.DEFAULT_HLS_PLAYLIST
                           ) -> OutputUrl:
        oid = OutputUrl.generate_id()
        http_root = self._generate_http_root_dir(oid)
        link = '{0}/{1}'.format(http_root, playlist_name)
        return OutputUrl(oid, self._settings.generate_http_link(link),
                         http_root)

    def generate_vod_link(self,
                          playlist_name=constants.DEFAULT_HLS_PLAYLIST
                          ) -> OutputUrl:
        oid = OutputUrl.generate_id()
        vods_root = self._generate_vods_root_dir(oid)
        link = '{0}/{1}'.format(vods_root, playlist_name)
        return OutputUrl(oid, self._settings.generate_vods_link(link),
                         vods_root)

    def generate_cod_link(self,
                          playlist_name=constants.DEFAULT_HLS_PLAYLIST
                          ) -> OutputUrl:
        oid = OutputUrl.generate_id()
        cods_root = self._generate_cods_root_dir(oid)
        link = '{0}/{1}'.format(cods_root, playlist_name)
        return OutputUrl(oid, self._settings.generate_cods_link(link),
                         cods_root)

    def get_log_level(self):
        return self.log_level

    def get_audio_select(self):
        return self.audio_select

    def get_have_video(self):
        return self.have_video

    def get_have_audio(self):
        return self.have_audio

    def get_loop(self):
        return self.loop

    def get_avformat(self):
        return self.avformat

    def get_restart_attempts(self):
        return self.restart_attempts

    def get_auto_exit_time(self):
        return self.auto_exit_time

    def generate_input_playlist(self, header=True) -> str:
        result = '#EXTM3U\n' if header else ''
        stream_type = self.get_type()
        if stream_type == constants.StreamType.RELAY or \
                stream_type == constants.StreamType.ENCODE or \
                stream_type == constants.StreamType.TIMESHIFT_PLAYER or \
                stream_type == constants.StreamType.VOD_ENCODE or \
                stream_type == constants.StreamType.VOD_RELAY:
            for out in self.input.urls:
                result += '#EXTINF:-1 tvg-id="{0}" tvg-name="{1}" tvg-logo="{2}" group-title="{3}",{4}\n{5}\n'.format(
                    self.tvg_id, self.tvg_name, self.tvg_logo, self.group,
                    self.name, out.uri)

        return result

    @classmethod
    def make_stream(cls, settings):
        stream = cls()
        stream._settings = settings
        stream.input = InputUrls(urls=[InputUrl(id=InputUrl.generate_id())])
        stream.output = OutputUrls(
            urls=[OutputUrl(id=OutputUrl.generate_id())])
        return stream

    # private
    def _generate_http_root_dir(self, oid: int):
        return '{0}/{1}/{2}/{3}'.format(self._settings.hls_directory,
                                        self.get_type(), self.get_id(), oid)

    def _generate_vods_root_dir(self, oid: int):
        return '{0}/{1}/{2}/{3}'.format(self._settings.vods_directory,
                                        self.get_type(), self.get_id(), oid)

    def _generate_cods_root_dir(self, oid: int):
        return '{0}/{1}/{2}/{3}'.format(self._settings.cods_directory,
                                        self.get_type(), self.get_id(), oid)

    def _fixup_http_output_urls(self):
        for idx, val in enumerate(self.output.urls):
            url = val.uri
            if url == constants.DEFAULT_TEST_URL:
                return

            parsed_uri = urlparse(url)
            if parsed_uri.scheme == 'http':
                filename = os.path.basename(parsed_uri.path)
                self.output.urls[idx] = self.generate_http_link(filename)

    def _fixup_vod_output_urls(self):
        for idx, val in enumerate(self.output.urls):
            url = val.uri
            if url == constants.DEFAULT_TEST_URL:
                return

            parsed_uri = urlparse(url)
            if parsed_uri.scheme == 'http':
                filename = os.path.basename(parsed_uri.path)
                self.output.urls[idx] = self.generate_vod_link(filename)

    def _fixup_cod_output_urls(self):
        for idx, val in enumerate(self.output.urls):
            url = val.uri
            if url == constants.DEFAULT_TEST_URL:
                return

            parsed_uri = urlparse(url)
            if parsed_uri.scheme == 'http':
                filename = os.path.basename(parsed_uri.path)
                self.output.urls[idx] = self.generate_cod_link(filename)
Beispiel #5
0
 def make_stream(cls, settings):
     stream = cls()
     stream.visible = False
     stream._settings = settings
     stream.input = InputUrls(urls=[InputUrl(id=InputUrl.generate_id())])
     return stream