Skip to content

Commit

Permalink
0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jneilliii committed Aug 11, 2018
1 parent 34ae219 commit 7073767
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 31 deletions.
80 changes: 52 additions & 28 deletions octoprint_youtubelive/__init__.py
Expand Up @@ -9,13 +9,15 @@ class youtubelive(octoprint.plugin.StartupPlugin,
octoprint.plugin.TemplatePlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.SettingsPlugin,
octoprint.plugin.SimpleApiPlugin):
octoprint.plugin.SimpleApiPlugin,
octoprint.plugin.EventHandlerPlugin):

def __init__(self):
self.client = docker.from_env()
self.container = None

##~~ StartupPlugin

def on_after_startup(self):
self._logger.info("OctoPrint-YouTubeLive loaded! Checking stream status.")
try:
Expand All @@ -27,21 +29,24 @@ def on_after_startup(self):
self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False))

##~~ TemplatePlugin

def get_template_configs(self):
return [dict(type="settings",custom_bindings=False)]

##~~ AssetPlugin

def get_assets(self):
return dict(
js=["js/youtubelive.js"],
css=["css/youtubelive.css"]
)

##~~ SettingsPlugin

def get_settings_defaults(self):
return dict(channel_id="",stream_id="",streaming=False)
return dict(channel_id="",stream_id="",streaming=False,auto_start=False)

##~~ SimpleApiPlugin mixin
##~~ SimpleApiPlugin

def get_api_commands(self):
return dict(startStream=[],stopStream=[],checkStream=[])
Expand All @@ -53,39 +58,58 @@ def on_api_command(self, command, data):

if command == 'startStream':
self._logger.info("Start stream command received.")
if not self.container:
filters = []
if self._settings.global_get(["webcam","flipH"]):
filters.append("hflip")
if self._settings.global_get(["webcam","flipV"]):
filters.append("vflip")
if self._settings.global_get(["webcam","rotate90"]):
filters.append("transpose=cclock")
if len(filters) == 0:
filters.append("null")
try:
self.container = self.client.containers.run("octoprint/youtubelive:latest",command=[self._settings.global_get(["webcam","stream"]),self._settings.get(["stream_id"]),",".join(filters)],detach=True,privileged=True,name="YouTubeLive",auto_remove=True)
self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=True))
except Exception, e:
self._plugin_manager.send_plugin_message(self._identifier, dict(error=str(e),status=True,streaming=False))
return
self.startStream()

if command == 'stopStream':
self._logger.info("Stop stream command received.")
if self.container:
try:
self.container.stop()
self.container = None
self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False))
except Exception, e:
self._plugin_manager.send_plugin_message(self._identifier, dict(error=str(e),status=True,streaming=False))
else:
self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False))
self.stopStream()

if command == 'checkStream':
self._logger.info("Checking stream status.")
if self.container:
self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=True))
else:
self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False))

##-- EventHandlerPlugin

def on_event(self, event, payload):
if event == "PrintStarted" and self._settings.get(["auto_start"]):
self.startStream()

if event in ["PrintDone","PrintCancelled"] and self._settings.get(["auto_start"]):
self.stopStream()

##-- Utility Functions

def startStream(self):
if not self.container:
filters = []
if self._settings.global_get(["webcam","flipH"]):
filters.append("hflip")
if self._settings.global_get(["webcam","flipV"]):
filters.append("vflip")
if self._settings.global_get(["webcam","rotate90"]):
filters.append("transpose=cclock")
if len(filters) == 0:
filters.append("null")
try:
self.container = self.client.containers.run("octoprint/youtubelive:latest",command=[self._settings.global_get(["webcam","stream"]),self._settings.get(["stream_id"]),",".join(filters)],detach=True,privileged=True,name="YouTubeLive",auto_remove=True)
self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=True))
except Exception, e:
self._plugin_manager.send_plugin_message(self._identifier, dict(error=str(e),status=True,streaming=False))
return

def stopStream(self):
if self.container:
try:
self.container.stop()
self.container = None
self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False))
except Exception, e:
self._plugin_manager.send_plugin_message(self._identifier, dict(error=str(e),status=True,streaming=False))
else:
self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False))

##~~ Softwareupdate hook
def get_update_information(self):
Expand Down
14 changes: 13 additions & 1 deletion octoprint_youtubelive/static/js/youtubelive.js
Expand Up @@ -7,6 +7,7 @@ $(function () {
self.stream_id = ko.observable();
self.streaming = ko.observable();
self.processing = ko.observable(false);
self.view_url = ko.observable();
self.icon = ko.pureComputed(function() {
var icons = [];
if (self.streaming() && !self.processing()) {
Expand All @@ -25,7 +26,7 @@ $(function () {
});
self.btnclass = ko.pureComputed(function() {
return self.streaming() ? 'btn-primary' : 'btn-danger';
});
});


// This will get called before the youtubeliveViewModel gets bound to the DOM, but after its depedencies have
Expand Down Expand Up @@ -53,6 +54,17 @@ $(function () {
})
}

self.onTabChange = function(next, current) {
if(next == '#tab_plugin_youtubelive'){
if(self.settingsViewModel.settings.webcam.streamRatio() == '4:3'){
$('#youtubelive_wrapper').css('padding-bottom','75%');
}
self.view_url('https://www.youtube.com/embed/live_stream?channel=' + self.settingsViewModel.settings.plugins.youtubelive.channel_id());
} else {
self.view_url('');
}
}

self.onDataUpdaterPluginMessage = function(plugin, data) {
if (plugin != "youtubelive") {
return;
Expand Down
6 changes: 6 additions & 0 deletions octoprint_youtubelive/templates/youtubelive_settings.jinja2
Expand Up @@ -12,4 +12,10 @@
<input type="text" class="input-block-level" data-bind="value: settings.plugins.youtubelive.stream_id" placeholder="Enter your personal live stream id here">
</div>
</div>
<div class="control-group" data-bind="visible: settings.plugins.youtubelive.stream_id().length > 0">
<label class="control-label">{{ _('Stream While Printing') }}</label>
<div class="controls">
<input class="input-checkbox" type="checkbox" data-bind="checked: settings.plugins.youtubelive.auto_start"></input>
</div>
</div>
</form>
2 changes: 1 addition & 1 deletion octoprint_youtubelive/templates/youtubelive_tab.jinja2
@@ -1,5 +1,5 @@
<div id="youtubelive_wrapper">
<iframe data-bind="attr: {src: 'https://www.youtube.com/embed/live_stream?channel=' + settingsViewModel.settings.plugins.youtubelive.channel_id()}" frameborder="0" allowfullscreen></iframe>
<iframe data-bind="attr: {src: view_url()}" frameborder="0" allowfullscreen></iframe>
</div>
<div class="row-fluid btn-group" style="text-align: center;padding-top: 20px;" data-bind="visible: settingsViewModel.settings.plugins.youtubelive.stream_id().length > 0">
<button class="btn" data-bind="click: toggleStream, css: btnclass, disable:processing"><i data-bind="css: icon"></i><span data-bind="text: streaming() ? ' Stop' : ' Go Live'"></span></button>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -14,7 +14,7 @@
plugin_name = "YouTube Live"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.4.0"
plugin_version = "0.5.0"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 7073767

Please sign in to comment.