from __future__ import division
 
 from timeside.decoder.core import *
-
+from timeside.tools.gstutils import MainloopThread
+import threading
 
 class FileDecoder(Decoder):
     """ gstreamer-based decoder """
                                         given the media duration""")
 
         # a lock to wait wait for gstreamer thread to be ready
-        import threading
         self.discovered_cond = threading.Condition(threading.Lock())
         self.discovered = False
 
 
         self.queue = Queue.Queue(QUEUE_SIZE)
 
-        import threading
-
-        class MainloopThread(threading.Thread):
-            def __init__(self, mainloop):
-                threading.Thread.__init__(self)
-                self.mainloop = mainloop
-
-            def run(self):
-                self.mainloop.run()
         self.mainloop = gobject.MainLoop()
         self.mainloopthread = MainloopThread(self.mainloop)
         self.mainloopthread.start()
 
 from __future__ import division
 
 from timeside.decoder.core import *
+from timeside.tools.gstutils import MainloopThread
 
 
 class LiveDecoder(Decoder):
 
         self.queue = Queue.Queue(QUEUE_SIZE)
 
-        import threading
-
-        class MainloopThread(threading.Thread):
-            def __init__(self, mainloop):
-                threading.Thread.__init__(self)
-                self.mainloop = mainloop
-
-            def run(self):
-                self.mainloop.run()
         self.mainloop = gobject.MainLoop()
         self.mainloopthread = MainloopThread(self.mainloop)
         self.mainloopthread.start()
 
 from timeside.core import Processor, implements, interfacedoc
 from timeside.component import abstract
 from timeside.api import IEncoder
-from timeside.tools import numpy_array_to_gst_buffer
+from timeside.tools import numpy_array_to_gst_buffer, MainloopThread
 
 #from gst import _gst as gst
 import pygst
 import gobject
 gobject.threads_init()
 
+import threading
+
 # Streaming queue configuration
 QUEUE_SIZE = 10
 GST_APPSINK_MAX_BUFFERS = 10
         if not self.filename and not self.streaming:
             raise Exception('Must give an output')
 
-        import threading
         self.end_cond = threading.Condition(threading.Lock())
 
         self.eod = False
         self.bus.add_signal_watch()
         self.bus.connect("message", self._on_message_cb)
 
-        import threading
-
-        class MainloopThread(threading.Thread):
-            def __init__(self, mainloop):
-                threading.Thread.__init__(self)
-                self.mainloop = mainloop
-
-            def run(self):
-                self.mainloop.run()
         self.mainloop = gobject.MainLoop()
         self.mainloopthread = MainloopThread(self.mainloop)
         self.mainloopthread.start()
 
-from numpy import array, getbuffer, frombuffer
+from numpy import getbuffer, frombuffer
 
 import pygst
 pygst.require('0.10')
 import gobject
 gobject.threads_init()
 
+import threading
+
 
 def numpy_array_to_gst_buffer(frames, CHUNK_SIZE, num_samples, SAMPLE_RATE):
     from gst import Buffer
     samples = frombuffer(buf.data, dtype='float32')
     samples.resize([len(samples)/chan, chan])
     return samples
+
+
+class MainloopThread(threading.Thread):
+    def __init__(self, mainloop):
+        threading.Thread.__init__(self)
+        self.mainloop = mainloop
+
+    def run(self):
+        self.mainloop.run()