# OBS-Studio python scripts
# Copyright (C) 2020 Regio Software GmbH ralf.klammer@regio-software.de

# Project   OBS Restart Streaming
# Version   1.0
# @author   Ralf Klammer

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# restart_streaming.py

import obspython as obs

# ------------------------------------------------------------


def script_description():
    return "<b>OBS Restart Streaming</b>" + \
        "<hr>" + \
        "Script to constantly check if stream is still running. " + \
        "<br/>" + \
        "<br/>" + \
        "The script can be en-/disabled and interval can be set." + \
        "<br/><br/>" + \
        "Made by Ralf Klammer, © 2020" + \
        "<hr>"


def script_properties():
    props = obs.obs_properties_create()
    obs.obs_properties_add_bool(props, "enabled", "Enabled")
    obs.obs_properties_add_int_slider(
        props, "interval", "Interval (seconds)", 5, 120, 1)
    return props


def check_stream(initial=False):
    if not obs.obs_frontend_streaming_active():
        obs.obs_frontend_streaming_start()
        print('Restarted Stream!')


def script_update(settings):
    if obs.obs_data_get_bool(settings, "enabled"):
        print('Add timer!')
        obs.timer_add(
            check_stream,
            1 * obs.obs_data_get_int(settings, "interval") * 1000)
    else:
        print('Remove timer!')
        obs.timer_remove(check_stream)
