<gh_stars>1-10
import io

import debugpy
import logging
import socketserver
from threading import Condition
from http import server

LOGGER = logging.getLogger("tmv.streamer")

class StreamingOutput(object):
    def __init__(self):
        self.frame = None
        self.buffer = io.BytesIO()
        self.condition = Condition()

    def write(self, buf):
        if buf.startswith(b'\xff\xd8'):
            # New frame, copy the existing buffer's content and notify all
            # clients it's available
            self.buffer.truncate()
            with self.condition:
                self.frame = self.buffer.getvalue()
                self.condition.notify_all()
            self.buffer.seek(0)
        return self.buffer.write(buf)

class StreamingHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/video':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    with StreamingHandler.output.condition:
                        StreamingHandler.output.condition.wait()
                        frame = StreamingHandler.output.frame
                    self.wfile.write(b'--FRAME\r\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\r\n')
            except Exception as e: #pylint: disable=broad-except
                logging.debug(
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()

class TMVStreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
    allow_reuse_address = True
    daemon_threads = True
    
if __name__ == '__main__':
    # Simple stream of picamera
    from picamera import PiCamera #pylint: disable=unused-import
    
    with PiCamera(resolution='640x480', framerate=10) as camera:
        output = StreamingOutput()
        camera.start_recording(output, format='mjpeg')        
        StreamingHandler.output = output
        try:
            address = ('', 5001)
            server = TMVStreamingServer(address,StreamingHandler)
            server.serve_forever()
        finally:
            camera.stop_recording()
import io

import debugpy
import logging
import socketserver
from threading import Condition
from http import server

LOGGER = logging.getLogger("tmv.streamer")

class StreamingOutput(object):
    def __init__(self):
        self.frame = None
        self.buffer = io.BytesIO()
        self.condition = Condition()

    def write(self, buf):
        if buf.startswith(b'\xff\xd8'):
            # New frame, copy the existing buffer's content and notify all
            # clients it's available
            self.buffer.truncate()
            with self.condition:
                self.frame = self.buffer.getvalue()
                self.condition.notify_all()
            self.buffer.seek(0)
        return self.buffer.write(buf)

class StreamingHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/video':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    with StreamingHandler.output.condition:
                        StreamingHandler.output.condition.wait()
                        frame = StreamingHandler.output.frame
                    self.wfile.write(b'--FRAME\r\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\r\n')
            except Exception as e: #pylint: disable=broad-except
                logging.debug(
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()

class TMVStreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
    allow_reuse_address = True
    daemon_threads = True
    
if __name__ == '__main__':
    # Simple stream of picamera
    from picamera import PiCamera #pylint: disable=unused-import
    
    with PiCamera(resolution='640x480', framerate=10) as camera:
        output = StreamingOutput()
        camera.start_recording(output, format='mjpeg')        
        StreamingHandler.output = output
        try:
            address = ('', 5001)
            server = TMVStreamingServer(address,StreamingHandler)
            server.serve_forever()
        finally:
            camera.stop_recording()
# New frame, copy the existing buffer's content and notify all # clients it's available #pylint: disable=broad-except # Simple stream of picamera #pylint: disable=unused-import