# rpi_camera_livestream

> Using NodeJS and Raspberry Pi camera module to provide a livestream for your website.

Latest version **1.0.3** (published 2022-11-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install rpi_camera_livestream
pnpm add rpi_camera_livestream
yarn add rpi_camera_livestream
bun add rpi_camera_livestream
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2022-11-21 |
| First published | 2022-08-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 20.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | RootDev4 |
| Maintainers | rootdev4 |
| Keywords | camera, html, javascript, livestream, nodejs, pi, raspberry, web, webcam |

## Links

- npm: https://www.npmjs.com/package/rpi_camera_livestream
- Repository: https://github.com/RootDev4/rpi_camera_livestream
- Homepage: https://github.com/RootDev4/rpi_camera_livestream#readme
- Issues: https://github.com/RootDev4/rpi_camera_livestream/issues
- npm.io page: https://npm.io/package/rpi_camera_livestream

## Dependencies (2)

- [express](https://npm.io/package/express.md) ^4.18.1
- [@zebrajaeger/raspberry-pi-camera-native](https://npm.io/package/@zebrajaeger/raspberry-pi-camera-native.md) ^0.0.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2022-11-21
- 1.0.2 — 2022-09-23
- 1.0.0 — 2022-08-19

## README

# Raspberry Pi Camera Livestream
Using NodeJS and Raspberry Pi camera module to provide a livestream for your website.
Forked and improved version of caseymcj's [raspberrypi_node_camera_web_streamer](https://github.com/caseymcj/raspberrypi_node_camera_web_streamer).
This project is up-to-date and works with the latest Raspberry Pi OS 11 (Bullseye).

## Installation
First make sure the legacy camera support is enabled using `sudo raspi-config` and then select `3 Interface Options`.

### Install via npm
Create a new project with
```
npm init -y
```
and after that, install this package with
```
npm install rpi_camera_livestream
```

### Install via git
Clone this repository with
```
git clone https://github.com/RootDev4/rpi_camera_livestream.git
```
and install the dependencies with
```
npm install
```

## Quick start
The following snippet automatically starts an express webserver and continuously streams the captured video frames from the Raspberry Pi camera module via the default route /live.stream
### Server
```javascript
const livestream = require('rpi_camera_livestream')

livestream.start()
```

### Client
```html
<img src="http://<your_server_ipaddr>/live.stream">
```
or just open `http://<your_server_ipaddr>/live.stream` in your browser.

## Basic usage
```javascript
const livestream = require('rpi_camera_livestream')

livestream.setVerboseMode(true)     // enable verbose mode
livestream.setPort(3333)            // set webserver port
livestream.setPathname('/webcam')   // set route/pathname
livestream.start().then(url => console.log(`Livestream started on ${url}`))
// > Livestream started on http://<your_server_ipaddr>:3333/webcam
```
### Controls
```javascript
const livestream = require('rpi_camera_livestream')

livestream.start()  // Starts livestream and returns livestream URL
livestream.pause()  // Pauses a started livestream
livestream.resume() // Resumes a paused livestream
livestream.stop()   // Stops a started livestream
```
All controlling methods are promise-based and hence await'able and then'able
```javascript
const livestream = require('rpi_camera_livestream')

livestream.start().then(url => {
    console.log(`Livestream started on ${url}`)

    livestream.pause().then(() => {
        livestream.resume()
    })
})

await livestream.stop()
```
### Get last frame
Returns the last captured frame as buffered value.  If no images have been captured (which may be the case if no users have connected yet), this value is `null`.
```javascript
livestream.getLastFrame()
```
### Take snapshot
Takes a snapshot by using the last captured frame if it is not `null` and returns it as a base64 encoded image string based on the encoding type you set.
```javascript
livestream.getSnapshot()
```
### Get supported encoding types
Returns a comma-separated string list containing the supported encoding types.
```javascript
livestream.getSupportedEncodingTypes()
```
## Socket.io support
### Server
First install Socket.io with `npm i socket.io`
```javascript
const livestream = require('rpi_camera_livestream')
livestream.start()

// Web socket
const http = require('http').Server(livestream.server.app)
const io = require('socket.io')(http)

io.on('connection', socket => {
    console.log('User connected to socket')
    livestream.camera.on('frame', data => socket.emit('stream', data))
})

// Start HTTP server
// IMPORTANT: use http.listen() and NOT app.listen()
const port = livestream.server.port
http.listen(port, () => console.log(`Server is up and listen on port ${port}`))
```
### Client
```html
<img id="webstream" width="800" height="600">

<script src="https://cdn.jsdelivr.net/npm/socket.io@4.5.3/client-dist/socket.io.min.js"></script>
<script>
    const bufferToBase64 = buffer => {
        const bytes = new Uint8Array(buffer)
        let binary = ''
        for (let i = 0; i < bytes.byteLength; i++) binary += String.fromCharCode(bytes[i])
        return `data:image/png;base64,${window.btoa(binary)}`
    }

    const socket = io()
    const image = document.getElementById('webstream')

    socket.on('stream', data => image.src = bufferToBase64(data))
</script>
```
## Documentation
### Verbose mode
Enable/Disable verbose mode. Default: disabled
```javascript
livestream.setVerboseMode(true)
```

### Register a running express webserver
Register an express webserver, if you want to use your already running webserver
```javascript
const livestream = require('rpi_camera_livestream')
const express = require('express')
const app = express()
const port = 8080

livestream.register(app, port)
livestream.start()

app.listen(port, () => console.log(`Webserver listening on port ${port}`))
```
### Set webserver port
Default: 8000
```javascript
livestream.setPort(3333)
```
### Set route/pathname
Default: /live.stream
```javascript
livestream.setPathname('/webcam')
```
### Set video width
Default: 1280px
```javascript
livestream.setWidth(800)
```
Minimum width is 32px. Maximum width depends on your camera version (v1: 2592px, v2: 3280px). Specify your camera version if necessary (default: v2)
```javascript
livestream.setWidth(800, 1) // v1 camera
```
### Set video height
Default: 720px
```javascript
livestream.setHeight(600)
```
Minimum height is 16px. Maximum height depends on your camera version (v1: 1944px, v2: 2464px). Specify your camera version if necessary (default: v2)
```javascript
livestream.setHeight(600, 1) // v1 camera
```
### Set FPS
Default: 16
```javascript
livestream.setWidth(25) // min: 1, max: 90
```
### Set encoding type
Default: JPEG (hardware accelerated)
```javascript
livestream.setEncoding('PNG')
```
Valid encoding types are: JPEG, GIF, PNG, PPM, TGA, BMP
### Set quality
Default: 25
```javascript
livestream.setQuality(15) // min: 1, max: 100
```
Lower values lead to a faster stream.

## To-Do
- Add SSL support

## License
[MIT](../blob/master/LICENSE)

---
_Source: https://npm.io/package/rpi_camera_livestream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
