Skip to content
This repository has been archived by the owner on Feb 20, 2018. It is now read-only.

Commit

Permalink
Save screencast only for failed sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
sh0ked committed Mar 22, 2017
1 parent 7248fa6 commit 77358a1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
2 changes: 2 additions & 0 deletions config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ class Config(object):
VMMASTER_AGENT_PORT = 9000

THREAD_POOL_MAX = 100
SAVE_SCREENCAST_ON_FAILED = False
SCREENCAST_CPU_LIMIT = 10
11 changes: 6 additions & 5 deletions core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def close(self, reason=None):
if self.vnc_helper:
self.vnc_helper.stop_recording()
self.vnc_helper.stop_proxy()
if self.take_screencast:
self.vnc_helper.convert_video()
elif getattr(config, "SAVE_SCREENCAST_ON_FAILED", False) and "failed" in self.status:
self.vnc_helper.convert_video()

current_app.sessions.remove(self)

Expand Down Expand Up @@ -169,11 +173,8 @@ def run(self, endpoint):
self.endpoint = endpoint
self.set_vm(endpoint)
self.status = "running"
self.vnc_helper = VNCVideoHelper(self.endpoint_ip,
filename_prefix=self.id)

if self.take_screencast:
self.vnc_helper.start_recording()
self.vnc_helper = VNCVideoHelper(self.endpoint_ip, filename_prefix=self.id)
self.vnc_helper.start_recording()

log.info("Session %s starting on %s (%s)." %
(self.id, self.endpoint_name, self.endpoint_ip))
Expand Down
55 changes: 29 additions & 26 deletions core/video.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
##
## flvrec.py - VNC to FLV recording tool.
##
## Copyright (c) 2009-2010 by Yusuke Shinyama
##
# coding: utf-8

import os
import sys
import signal
import socket
import logging
import os.path
import subprocess
import websockify
import multiprocessing
from twisted.internet import threads
import websockify

from core.config import config

import sys, socket, os, os.path, subprocess, signal
from vnc2flv import flv, rfb, video
from core.utils.network_utils import get_free_port

log = logging.getLogger(__name__)


class VNCVideoHelper():
class VNCVideoHelper:
recorder = None
proxy = None
__proxy_port = None
Expand All @@ -33,27 +33,29 @@ def __init__(self, host, port=5900, filename_prefix='vnc'):
self.host = host
self.port = port


@staticmethod
def _flvrec(filename, host='localhost', port=5900,
framerate=12, keyframe=120,
preferred_encoding=(0,),
blocksize=32, clipping=None,
debug=0, verbose=0):
framerate=12, keyframe=120,
preferred_encoding=(0,),
blocksize=32, clipping=None,
debug=0, verbose=0):
fp = file(filename, 'wb')
pwdcache = rfb.PWDCache('%s:%d' % (host,port))
pwdcache = rfb.PWDCache('%s:%d' % (host, port))
writer = flv.FLVWriter(fp, framerate=framerate, debug=debug)
sink = video.FLVVideoSink(writer,
blocksize=blocksize, framerate=framerate, keyframe=keyframe,
clipping=clipping, debug=debug)
client = rfb.RFBNetworkClient(host, port, sink, timeout=500/framerate,
pwdcache=pwdcache, preferred_encoding=preferred_encoding,
debug=debug)
sink = video.FLVVideoSink(
writer,
blocksize=blocksize, framerate=framerate, keyframe=keyframe,
clipping=clipping, debug=debug)
client = rfb.RFBNetworkClient(
host, port, sink, timeout=500/framerate,
pwdcache=pwdcache, preferred_encoding=preferred_encoding,
debug=debug)
if verbose:
log.debug('Start vnc recording to %s' % filename)
retval = 0
try:
def sigint_handler(sig, frame):
log.debug("%s %s" % (sig, frame))
raise KeyboardInterrupt
signal.signal(signal.SIGINT, sigint_handler)
client.open()
Expand Down Expand Up @@ -90,7 +92,7 @@ def _flv2webm(self):
"/usr/bin/cpulimit",
"-z",
"-b",
"-l", "15",
"-l", getattr(config, "SCREENCAST_CPU_LIMIT", 10),
"-p", "%s" % converter.pid
], stdin=subprocess.PIPE)

Expand All @@ -99,7 +101,7 @@ def _flv2webm(self):
self.delete_source_video()

def delete_source_video(self):
if os.path.isfile('%s.webm' % self.__filepath.split('.flv')[0]):
if self.__filepath and os.path.isfile(self.__filepath):
os.remove(self.__filepath)
log.debug('Source video %s was deleted' % self.__filepath)

Expand Down Expand Up @@ -146,9 +148,10 @@ def start_recording(self, framerate=5, size=(800, 600)):
kwargs=kwargs)
self.recorder.start()

def convert_video(self):
d = threads.deferToThread(self._flv2webm)
d.addBoth(lambda s: None)

def stop_recording(self):
if self.recorder and self.recorder.is_alive():
self.recorder.terminate()

d = threads.deferToThread(self._flv2webm)
d.addBoth(lambda s: None)

0 comments on commit 77358a1

Please sign in to comment.