     (.setSoTimeout 2000)
     (.connect (InetSocketAddress. ^InetAddress @video-source 5555))))
 
-(defn save [^OutputStream out data]
-  (doseq [[buffer size] data]
-    (.write out buffer 0 size))
-  out)
-
 (def run (atom false))
 
-(defn video-streaming []
+(defn send-init-video [^Socket socket]
+  (doto (.getOutputStream socket)
+    (.write (byte-array (map bin/ubyte [1 0 0 0])))
+    (.flush)))
+
+(defn video-streaming-task []
   (try
     (while @run
       (let [socket      (open-socket)
-            out         (agent (io/output-stream (io/file (str "sormilla-" (.format (java.text.SimpleDateFormat. "yyyyMMdd-HHmmss") (java.util.Date.)) ".h264"))))
-            reader      (make-reader (BufferedInputStream. (.getInputStream socket)))
+            reader      (make-reader (-> socket .getInputStream BufferedInputStream.))
             decoder     (make-decoder)]
         (try
-          (doto (.getOutputStream socket)
-            (.write (byte-array (map bin/ubyte [1 0 0 0])))
-            (.flush))
+          (send-init-video socket)
           (while @run
-            (let [data (reader)]
-                (send-off out save data)
-                (swap! world assoc :image (decoder (second data)))))
+            (->> (reader) (decoder) (swap! world assoc :image)))
           (catch java.io.IOException e
             (println "I/O error:" e ": reconnecting...")
             (Thread/sleep 1000))
           (finally
-            (try (.close socket) (catch Exception _))
-            (try (.close ^OutputStream @out) (catch Exception _))))))
+            (try (.close socket) (catch Exception _))))))
     (catch Throwable e
       (println "exception while processing video stream" e)
       (.printStackTrace e))))

               (start! [this config]
                 (reset! run true)
                 (reset! video-source (if (:video-sim config)
                                        (do (println "Using simulated video") (InetAddress/getByName nil))
                                        (do (println "Real video") comm/drone-ip)))
                 (task/submit :video video-streaming)
                 config)
               (stop! [this]
                 (reset! run false)

    (if (neg? (.avcodec_open context codec)) (throw (Exception. "Could not open codec")))
    (.av_init_packet packet)

    (fn ^BufferedImage [^bytes buffer]
      (let [size (alength buffer)]
        (set! (.size packet) size)
        (set! (.data_base packet) (ba->ia buffer ibuffer size))
        (set! (.data_offset packet) 0)
        (.avcodec_decode_video2 context frame got-picture? packet)
        (when (zero? (first got-picture?)) (throw (java.io.IOException. "Could not decode frame")))
        (let [picture         (.displayPicture (.priv_data context))
              width           (.imageWidth picture)
              height          (.imageHeight picture)
              picture-buffer  (int-array (* width height))
              image           (BufferedImage. width height BufferedImage/TYPE_INT_RGB)]
          (FrameUtils/YUV2RGB picture picture-buffer)
          (.setRGB image 0 0 width height picture-buffer 0 width)
          image)))))

(def video-source (atom nil))

