summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-08-13 12:44:12 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-08-13 12:44:12 +0200
commit9ba0d656adb53cd1722bf98e470ca4940799c150 (patch)
treea1ccf25e20a1af23eb64dbfcd70a80589613a5fc
parentaba69b26f66ee1483ceb73316d412ce1f87d1744 (diff)
Use a single thread for recoding.
-rwxr-xr-xapp.py7
-rw-r--r--directory.py5
-rw-r--r--recode.py34
3 files changed, 39 insertions, 7 deletions
diff --git a/app.py b/app.py
index f288646..e70283c 100755
--- a/app.py
+++ b/app.py
@@ -61,11 +61,12 @@ class Application(object):
def json_recode(self, environ, start_response, path):
args = cgi.FieldStorage(environ = environ)
path = args.getvalue('path') if 'path' in args else None
- decoder = args.getvalue('decoder') if 'decoder' in args else None
- encoder = args.getvalue('encoder') if 'encoder' in args else None
f = File(path)
- f.start_recode(decoder, encoder, environ['sessionid'])
+ # see json_play()
+ if not os.path.splitext(path)[1] in ('.mp3', '.ogg'):
+ decoder, encoder = ('ffmpeg',)*2
+ f.start_recode(decoder, encoder)
start_response('200 OK', [('Content-Type', 'text/plain')])
return []
diff --git a/directory.py b/directory.py
index 48dfaa9..6b8eaaf 100644
--- a/directory.py
+++ b/directory.py
@@ -1,4 +1,4 @@
-import os, mimetypes, recode, events, threading
+import os, mimetypes, recode, events
from config import config
@@ -112,8 +112,7 @@ class File(DirectoryEntry):
events.event_pub.play(sessionid, '/cache/{0}'.format(self.path))
def start_recode(self, decoder, encoder, sessionid = None):
- t = threading.Thread(target = self.recode, args = (decoder, encoder, sessionid))
- t.start()
+ recode.RecodeThread.add((self.recode, decoder, encoder, sessionid))
def json(self):
cache_path = self.get_cache_path()
diff --git a/recode.py b/recode.py
index 5f5a623..6caa2a9 100644
--- a/recode.py
+++ b/recode.py
@@ -1,4 +1,4 @@
-import subprocess, tempfile
+import subprocess, tempfile, threading
decoders = {}
encoders = {}
@@ -63,6 +63,38 @@ class Recoder(object):
self.encoder.encode(temp.name, dest)
temp.close()
+class RecodeThread(threading.Thread):
+ lock = threading.Lock()
+ queue = []
+ running = False
+
+ def get_job(self):
+ return self.queue.pop(0) if len(self.queue) else None
+
+ def run(self):
+ self.lock.acquire()
+ job = self.get_job()
+ while job:
+ self.lock.release()
+
+ func, decoder, encoder, sessionid = job
+ func(decoder, encoder, sessionid)
+
+ self.lock.acquire()
+ job = self.get_job()
+
+ RecodeThread.running = False
+ self.lock.release()
+
+ @classmethod
+ def add(cls, job):
+ cls.lock.acquire()
+ cls.queue.append(job)
+ if not cls.running:
+ cls.running = True
+ RecodeThread().start()
+ cls.lock.release()
+
if __name__ == '__main__':
import sys
ffmpeg = FFmpeg()