import io
import socket
import struct
import time
import picamera
import sys


class SplitFrames(object):
    def __init__(self, connection):
        self.connection = connection
        self.stream = io.BytesIO()
        self.count = 0

    def write(self, buf):
        if buf.startswith(b'\xff\xd8'):
            size = self.stream.tell()
            if size > 0:
                self.connection.write(struct.pack('<L', size))
                self.connection.flush()
                self.connection.seek(0)
                self.connection.write(self.stream.read(size))
                self.count += 1
                self.stream.seek(0)
        self.stream.write(buf)


my_server = '172.30.1.34'
res = (320, 240)
client_socket = socket.socket()
client_socket.connect((my_server, 8000))
connection = client_socket.makefile("wb")

try:
    output = SplitFrames(connection)
    with picamera.PiCamera(resolution=res, framerate=30) as camera:
        time.sleep(2)
        start = time.time()
        camera.start_recording(output, format='mjpeg')
        camera.wait_recording(sys.maxint)
        camera.stop_recording()

        connection.write(struct.pack('<L', 0))
finally:
    finish = time.time()
    print('Sent %d images in %d seconds at %.2ffps' % (
        output.count, finish-start, output.count / (finish-start)))
    connection.close()
    client_socket.close()
