Beispiel #1
0
    def get_data(self) -> OutputUrls:
        urls = OutputUrls()
        for url in self.data['urls']:
            urls.urls.append(OutputUrl(url['id'], url['uri'],
                                       url['http_root']))

        return urls
Beispiel #2
0
def make_timeshift_player_stream(
        settings: ServerSettings) -> TimeshiftPlayerStream:
    stream = TimeshiftPlayerStream()
    stream._settings = settings
    stream.input = InputUrls(urls=[InputUrl(id=InputUrl.generate_id())])
    stream.output = OutputUrls(urls=[OutputUrl(id=OutputUrl.generate_id())])
    return stream
Beispiel #3
0
def make_test_life_stream(settings: ServerSettings) -> TestLifeStream:
    stream = TestLifeStream()
    stream._settings = settings
    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 #4
0
class Stream(EmbeddedDocument):
    meta = {'allow_inheritance': True, 'auto_create_index': True}

    id = ObjectIdField(required=True,
                       default=ObjectId,
                       unique=True,
                       primary_key=True)
    name = StringField(default=constants.DEFAULT_STREAM_NAME,
                       max_length=constants.MAX_STREAM_NAME_LENGTH,
                       min_length=constants.MIN_STREAM_NAME_LENGTH,
                       required=True)
    icon = StringField(default=constants.DEFAULT_STREAM_ICON_URL,
                       max_length=constants.MAX_URL_LENGTH,
                       min_length=constants.MIN_URL_LENGTH,
                       required=True)
    group = StringField(default=constants.DEFAULT_STREAM_GROUP_TITLE,
                        min_length=constants.MIN_STREAM_GROUP_TITLE,
                        max_length=constants.MAX_STREAM_GROUP_TITLE,
                        required=False)
    created_date = DateTimeField(default=datetime.now)  # for inner use
    log_level = IntField(default=constants.StreamLogLevel.LOG_LEVEL_INFO,
                         required=True)

    input = EmbeddedDocumentField(InputUrls, default=InputUrls())
    output = EmbeddedDocumentField(OutputUrls, default=OutputUrls())
    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 = constants.StreamStatus.NEW
    _cpu = 0.0
    _timestamp = 0
    _rss = 0
    _loop_start_time = 0
    _restarts = 0
    _start_time = 0
    _input_streams = str()
    _output_streams = str()
    _settings = ServerSettings()

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

    def set_server_settings(self, settings: ServerSettings):
        self._settings = settings

    def reset(self):
        self._status = constants.StreamStatus.NEW
        self._cpu = 0.0
        self._timestamp = 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 = constants.StreamStatus(params[StreamFields.STATUS])
        self._cpu = params[StreamFields.CPU]
        self._timestamp = params[StreamFields.TIMESTAMP]
        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_front(self) -> dict:
        return {
            StreamFields.NAME: self.name,
            StreamFields.ID: self.get_id(),
            StreamFields.TYPE: self.get_type(),
            StreamFields.STATUS: self._status,
            StreamFields.CPU: self._cpu,
            StreamFields.TIMESTAMP: self._timestamp,
            StreamFields.RSS: self._rss,
            StreamFields.LOOP_START_TIME: self._loop_start_time,
            StreamFields.RESTARTS: self._restarts,
            StreamFields.START_TIME: self._start_time,
            StreamFields.INPUT_STREAMS: self._input_streams,
            StreamFields.OUTPUT_STREAMS: self._output_streams
        }

    def config(self) -> dict:
        conf = {
            ID_FIELD: self.get_id(),  # required
            TYPE_FIELD: self.get_type(),  # required
            FEEDBACK_DIR_FIELD: self.generate_feedback_dir(),  # required
            LOG_LEVEL_FIELD: self.get_log_level(),
            AUTO_EXIT_TIME_FIELD: self.get_auto_exit_time(),
            LOOP_FIELD: self.get_loop(),
            AVFORMAT_FIELD: self.get_avformat(),
            HAVE_VIDEO_FIELD: self.get_have_video(),  # required
            HAVE_AUDIO_FIELD: self.get_have_audio(),  # required
            RESTART_ATTEMPTS_FIELD: self.get_restart_attempts(),
            INPUT_FIELD:
            self.input.to_mongo(),  # required empty in timeshift_player
            OUTPUT_FIELD:
            self.output.to_mongo()  # required empty in timeshift_record
        }

        audio_select = self.get_audio_select()
        if audio_select != constants.INVALID_AUDIO_SELECT:
            conf[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 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_id(self) -> str:
        return str(self.id)

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

    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 fixup_output_urls(self):
        return

    # 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 _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)
Beispiel #5
0
def make_vod_encode_stream(settings: ServerSettings) -> VodEncodeStream:
    stream = VodEncodeStream()
    stream._settings = settings
    stream.input = InputUrls(urls=[InputUrl(id=InputUrl.generate_id())])
    stream.output = OutputUrls(urls=[OutputUrl(id=OutputUrl.generate_id())])
    return stream
Beispiel #6
0
def make_relay_stream(settings: ServerSettings) -> RelayStream:
    stream = RelayStream()
    stream._settings = settings
    stream.input = InputUrls(urls=[InputUrl(id=InputUrl.generate_id())])
    stream.output = OutputUrls(urls=[OutputUrl(id=OutputUrl.generate_id())])
    return stream