Skip to content

Repository files navigation

JavaScript client SDK for pi-webrtc

JavaScript client SDK for pi-webrtc

npm downloads Release

Web and React Native client for pi-webrtc, with TypeScript typings and support for low-latency WebRTC streaming, P2P, SFU, DataChannel control, snapshots, gamepad input, and file transfer.

Quick Start

For Web

npm install @mazupo/client
<video id="videoElement" autoplay playsinline controls></video>
<script type="module">
  import { PiCamera } from '@mazupo/client';

  const videoRef = document.getElementById('videoElement');
  const camera = new PiCamera({
    uid: 'your-custom-uid',
    mqttHost: 'your.mqtt.cloud',
    mqttPath: '/mqtt',
    mqttPort: 8884,
    mqttUsername: 'hakunamatata',
    mqttPassword: 'Wonderful',
    stunUrls: ['stun:stun1.l.google.com:19302'],
  });

  camera.onStream = (stream) => {
    videoRef.srcObject = stream ?? null;
  };

  camera.connect();
</script>

uid has to match the --uid the device was started with, and the MQTT settings are the broker both sides talk to.

For React Native

Install and configure react-native-webrtc, then install @mazupo/client:

npm install react-native-webrtc @mazupo/client

registerGlobals() has to run once at app startup, before any PiCamera is created:

import React, { useEffect, useState } from 'react';
import { RTCView, registerGlobals } from 'react-native-webrtc';
import { PiCamera, RNMediaStream } from '@mazupo/client';

registerGlobals();

export default function App() {
  const [streamUrl, setStreamUrl] = useState<string | null>(null);

  useEffect(() => {
    const camera = new PiCamera({
      uid: 'your-custom-uid',
      mqttHost: 'your.mqtt.cloud',
      mqttPath: '/mqtt',
      mqttPort: 8884,
      mqttUsername: 'hakunamatata',
      mqttPassword: 'Wonderful',
      stunUrls: ['stun:stun1.l.google.com:19302'],
    });

    camera.onStream = (stream) => {
      setStreamUrl((stream as RNMediaStream).toURL());
    };

    camera.connect();

    return () => {
      camera.terminate();
    };
  }, []);

  if (!streamUrl) {
    return null;
  }

  return <RTCView streamURL={streamUrl} style={{ flex: 1 }} />;
}

Contents

Examples

License

Licensed under the Apache License, Version 2.0. See LICENSE for the full terms, and NOTICE for the third-party components the published bundles include.

About

Javascript client package for pi-webrtc.

Topics

Resources

Stars

9 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages