Code Monkey home page Code Monkey logo

pi-h264-to-browser's Introduction

Pi H264 To Browser

Pi H264 To Browser is a simple Python application designed to stream hardware encoded h.264 from a Raspberry Pi equiped with a V1, V2, or HQ camera module, directly to a browser.

Capabillities

  • Stream to multiple clients simultaneously (usually only limited by your network connection)
  • Support any resolution and framerate the camera module can capture and the gpu can encode
  • Able to do both of the preceding from any Raspberry Pi

Features

  1. A screen that displays an unaltered video stream that allows you to switch to full screen mode.
  2. A screen that provides a focus peaking overlay to help focus the camera. focus peaking demo
  3. A screen that provides a center reticle overlay to aide in centering a subject in the frame.
  4. A screen that provides a standard 9 grid overlay to aide in more creative framing.

Viewing

When server.py is running the feed can be vied from any broswer via the following urls. rpi_address is the ip address or hostname of your Raspberry Pi, and serverPort is the port you set in the configuration section.

  1. The primary viewing screen
    http://<rpi_address>:<serverPort>/
    
  2. The focus peaking screen
    http://<rpi_address>:<serverPort>/focus/
    
  3. The center reticle screen
    http://<rpi_address>:<serverPort>/center/
    
  4. The 9 grid screen
    http://<rpi_address>:<serverPort>/grid/
    

Installation

  1. Ensure the camera module is properly connected to the Raspberry Pi
  2. Ensure the operating system is up to date, and the camera interface is enabled
  3. Install the Picamera Python module
    sudo apt-get install python3-picamera
    
  4. Install pip to handle loading Python pckages not avaiable in the Raspberry Pi OS archives
    sudo apt install python3-pip
    
  5. Install the Tornado framework
    sudo pip3 install tornado
    
  6. Donwload Pi H264 To Browser, and copy the src directoy to your Raspberry Pi

configuration

open server.py and edit the following section of code as needed.

  • The webserver will run on the port you set serverPort to.
  • Refer to the Picamera documentation for details on how to configure it. A lage number of options exist (far more than listed below), that allow for 100% customization of camera.
    1. sensor modes, resolutions and framerates
    2. general camera settings
      • video_denoise
      • iso
      • shutter_speed
      • exposure_mode
      • awb_mode
      • awb_gains
      • exposure_compensation
      • brightness
      • sharpness
      • contrast
      • saturation
      • hflip
      • vflip
      • meter_mode
    3. recordingOptions
      • inline_headers and sps_timing should always be set to true.
    4. Focus peaking screen
      • focusPeakingColor - What color a pixel that is in focus should be. This is a webgl color format string, and must be 4 comma seperated floating point numbers between 0.0 and 1.0! From left to right the numbers represent the red, green, blue, and alpha channels.
      • focusPeakingthreshold - Determines at what point a pixel is considred to be in focus. A pixel with a value below the threshold is considered out of focus, or an aberration. A pixel above the threshold is considered in focus. This is a floating point number that has a theoretical maximum range of 0.0 to 1.0, however values between 0.02 and 0.11 generally yield the best results.
    5. Center screen
      • centerColor - What color the centering reticle should be. This is a css color format string, and must be 4 comma seperated values. The first three are integers ranging from 0 to 255, and the forth is a float ranging from 0.0 to 1.0. From left to right the numbers represent the red, green, blue, and alpha channels.
      • centerThickness - and integer value that sets the thickness(in pixels) of the lines that make up the reticle.
    6. 9 grid screen
      • gridColor - What color the grid should be. This is a css color format string, and must be 4 comma seperated values. The first three are integers ranging from 0 to 255, and the forth is a float ranging from 0.0 to 1.0. From left to right the numbers represent the red, green, blue, and alpha channels.
      • gridThickness - and integer value that sets the thickness(in pixels) of the lines that make up the grid.
# start configuration
serverPort = 8000

camera = PiCamera(sensor_mode=2, resolution='1920x1080', framerate=30)
camera.video_denoise = False

recordingOptions = {
    'format' : 'h264', 
    'quality' : 20, 
    'profile' : 'high', 
    'level' : '4.2', 
    'intra_period' : 15, 
    'intra_refresh' : 'both', 
    'inline_headers' : True, 
    'sps_timing' : True
}

focusPeakingColor = '1.0, 0.0, 0.0, 1.0'
focusPeakingthreshold = 0.055

centerColor = '255, 0, 0, 1.0'
centerThickness = 2

gridColor = '255, 0, 0, 1.0'
gridThickness = 2
# end configuration

Running

  • from the terminal
    python3 server.py
    
  • at startup
    • An rc.local example!
      sudo python3 /home/pi/code/streaming/server.py > /home/pi/code/streaming/log.txt 2>&1 &
      

How It Works

  • Picamera handles all the video related tasks.
  • Tornado handles serving out the html and js assets via http, and the h264 stream via websockets.
  • jMuxer handles muxing the h264 stream (in browser) and playing it via Media Source extensions.

Licencing

pi-h264-to-browser's People

Contributors

ch3ri0ur avatar dans98 avatar lostapathy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pi-h264-to-browser's Issues

Doesn't work if IP address changes or if host has multiple names

Hi, first of all, I really like your project. It's the simplest way to stream h264 to a browser, and it performs really well too.

The setup I use consists of a Raspberry PI 1B (basically the oldest series), a USB WIFI stick, and Raspberry Pi OS lite 2021.05. Server.py works like charm if I start it from a SSH shell, but won't work if I start it from rc.local.

After some debugging it turns out that WIFI connection is not established by the time the script starts and the IP is 169.254.x.x at first, and later changes to the router provided 192.168.x.x. Since the script performs html templating at startup the websocket URL's will be wrong.

This mechanism also fails if I try to access my PI from the internet. It's behind a NAT router, and a local IP doesn't work for websocket connections from the public internet.

My suggestion is to move templating into the request handlers, and use the 'Host' header of the request in the template. This way it's guaranteed that the browser will find the ws endpoint even if different DNS names or port mapping is in use.

I can send a PR, if you're interested.

Center focus does not seem to do anything

I get a picture at 1280x720 and 20 fps, but the "center" just overlays a red cross and doesn't seem to do anything. Is it supposed to do something else? Also, "focus" doesn't seem to do anything at all except twinkle a part of the screen. Is there a way of controlling these functions?

Create a version that works with Picamera2

I attempted to run this on my Raspberry pi 4 with Picamera2 and coultn't get it to work.
the picamera2 docs only have a mjpeg server example and a seperate H264 quantiser example

Is there another branch/version of this repo where this works with picamera2 ?

I suspect the changes are something like the following however I don't speak python fluently.

To be added:

from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import CircularOutput  # (or FileOutput?)

and then the camera references should be removed or replaced by picam2
where picam2 iis intialised and starts streaming.

Also in the try:

    picam2 = Picamera2()
    fps = 30
    dur = 5
    micro = int((1 / fps) * 1000000)
    vconfig = picam2.create_video_configuration()
    vconfig['controls']['FrameDurationLimits'] = (micro, micro)
    picam2.configure(vconfig)
    encoder = H264Encoder()
    output = CircularOutput(buffersize=int(fps * (dur + 0.2)), outputtofile=False)
    output.fileoutput = "file.h264"
    picam2.start_recording(encoder, output)
    application = tornado.web.Application(requestHandlers)
    application.listen(serverPort)
    streamBuffer.setLoop(loop)
    loop.start()

If anyone has any hints on getting this to work, I'm willing to give it some attempts but it's not familiar to me, I would need some guidance.

Issues with Safari

Hi,
Thank you very much for creating this, it seems to do exactly what I was looking for!

Unfortunately though, I can only get it to work properly in Chrome (on macOS). If I open the page in Safari (on either macOS or iOS), the stream plays for a very short time, when it stops and can only be made to resume by manually clicking the play button in the video controls. Still, it seems as soon as playback has caught up with the frames that were received while playback was paused, it stops again.

Do you have any ideas how this problem could be solved? I'd love to use this in a project I'm currently investigating.

BTW, how did you reach a latency of close to 100ms? I'm closer to 300ms in Chrome (streaming from a Pi Zero W through wired ethernet (USB adapter)).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.