PiCamera.js

PiCamera.js

PiCamera.js

This package provides the JavaScript client-side implementation for pi_webrtc, a project designed to enable WebRTC-based real-time video and audio streaming on a Raspberry Pi.

Installation

npm install picamera.js

Example

Live video

Display live streaming on the HTML <video> element.

import { PiCamera } from 'picamera.js';

let videoRef = document.getElementById('videoElement');

let conn = new PiCamera({
  deviceUid: 'your-custom-uid',
  mqttHost: 'your.mqtt.cloud',
  mqttPath: '/mqtt',
  mqttPort: '8884', // Websocket Port
  mqttUsername: 'hakunamatata',
  mqttPassword: 'Wonderful',
  stunUrls: ["stun:stun1.l.google.com:19302", "stun:stun2.l.google.com:19302"],
});
conn.attach(videoRef);
conn.connect();

Capture a snapshot

Use webrtc datachannel to get the snapshot image only.

let conn = new PiCamera({
  deviceUid: 'your-custom-uid',
  mqttHost: 'your.mqtt.cloud',
  mqttPath: '/mqtt',
  mqttPort: '8884', // Websocket Port
  mqttUsername: 'hakunamatata',
  mqttPassword: 'Wonderful',
  stunUrls: ["stun:stun1.l.google.com:19302"],
  datachannelOnly: true,
});

conn.onDatachannel = (dc) => {
  conn.snapshot();
}

conn.onSnapshot = (image) => {
  // get a base64 image here, then terminate the connection.
  conn.terminate();
}

conn.connect();

API

Options

Available flags for initialization.

Option Type Default Description
deviceUid string   The custom --uid provided in the running pi_webrtc.
mqttHost string   The MQTT server host.
mqttPath string /mqtt The MQTT server path.
mqttPort number 8884 The WebSocket port for the MQTT server.
mqttProtocol string wss The portocol for the MQTT server.
mqttUsername string   The username for the MQTT server.
mqttPassword string   The password for the MQTT server.
stunUrls string[]   An array of STUN server URLs for WebRTC.
turnUrl string   The TURN server URL for WebRTC.
turnUsername string   The username for the TURN server.
turnPassword string   The password for the TURN server.
timeout number 10000 The connection timeout in milliseconds (ms).
datachannelOnly boolean false Specifies that the connection is only for data transfer, without media streams.
isMicOn boolean true Enables the local microphone stream by default if the connection is established.
isSpeakerOn boolean true Enables the remote audio stream by default if the connection is established.

Events

Methods

License

This project is licensed under the AGPL-3.0-only license. Under this license, anyone is free to use, modify, and distribute the code, but any use in network services or any modifications require the source code to be made public. Commercial usage without source code disclosure is not permitted.

Commercial License

If your use case does not permit source code disclosure or you have other licensing requirements, a commercial license is available. This option exempts you from the AGPL’s source code sharing requirements and includes dedicated support. The commercial license also enables optional features, such as removing watermarks or unlocking additional functionalities.

For more information, please contact: 📧 Email: tzu.huan.tai@gmail.com