From 4d7555957c632ceab4a3473c6e4fee35c19c46ac Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 14 May 2016 20:35:58 -0700 Subject: [PATCH] Fix camera --- homeassistant/components/camera/__init__.py | 25 ++++++++------------- homeassistant/components/http.py | 12 +++++----- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index e5c1bb6f77f..1806b5e66c6 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -29,9 +29,6 @@ STATE_IDLE = 'idle' ENTITY_IMAGE_URL = '/api/camera_proxy/{0}' -MULTIPART_BOUNDARY = '--jpgboundary' -MJPEG_START_HEADER = 'Content-type: {0}\r\n\r\n' - # pylint: disable=too-many-branches def setup(hass, config): @@ -87,10 +84,8 @@ class Camera(Entity): def mjpeg_stream(self, response): """Generate an HTTP MJPEG stream from camera images.""" import eventlet - response.mimetype = ('multipart/x-mixed-replace; ' - 'boundary={}'.format(MULTIPART_BOUNDARY)) - - boundary = bytes('\r\n{}\r\n'.format(MULTIPART_BOUNDARY), 'utf-8') + response.content_type = ('multipart/x-mixed-replace; ' + 'boundary=--jpegboundary') def stream(): """Stream images as mjpeg stream.""" @@ -99,16 +94,14 @@ class Camera(Entity): while True: img_bytes = self.camera_image() - if img_bytes is None: - continue - elif img_bytes == last_image: - eventlet.sleep(0.5) + if img_bytes is not None and img_bytes != last_image: + yield bytes( + '--jpegboundary\r\n' + 'Content-Type: image/jpeg\r\n' + 'Content-Length: {}\r\n\r\n'.format( + len(img_bytes)), 'utf-8') + img_bytes + b'\r\n' - yield bytes('Content-length: {}'.format(len(img_bytes)) + - '\r\nContent-type: image/jpeg\r\n\r\n', - 'utf-8') - yield img_bytes - yield boundary + last_image = img_bytes eventlet.sleep(0.5) except GeneratorExit: diff --git a/homeassistant/components/http.py b/homeassistant/components/http.py index 81280352121..eab6d4a33eb 100644 --- a/homeassistant/components/http.py +++ b/homeassistant/components/http.py @@ -2,6 +2,7 @@ import hmac import json import logging +import mimetypes import threading import re @@ -333,7 +334,7 @@ class HomeAssistantView(object): """Return a JSON message response.""" return self.json({'message': error}, status_code) - def file(self, request, fil, content_type=None): + def file(self, request, fil, mimetype=None): """Return a file.""" from werkzeug.wsgi import wrap_file from werkzeug.exceptions import NotFound @@ -344,9 +345,8 @@ class HomeAssistantView(object): except IOError: raise NotFound() - # TODO mimetypes, etc + if mimetype is None: + mimetype = mimetypes.guess_type(fil)[0] - resp = self.Response(wrap_file(request.environ, fil)) - if content_type is not None: - resp.mimetype = content_type - return resp + return self.Response(wrap_file(request.environ, fil), + mimetype=mimetype, direct_passthrough=True)