How to Set Up Live Video Streaming from Windows Webcam to Linux Server Using RTMP/WebRTC


1 views

To establish a reliable live streaming pipeline from a Windows client to your Linux server, we'll implement this architecture:

[Windows Laptop] --(RTMP/WebRTC)--> [Linux Server] --(HLS/DASH)--> [Website Viewers]
          |                                 |
     (Webcam Capture)                (Nginx/Node.js)

First, install and configure Nginx with RTMP module:

sudo apt update
sudo apt install nginx libnginx-mod-rtmp ffmpeg

Edit /etc/nginx/nginx.conf:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
            live on;
            record off;
            allow publish 127.0.0.1;
            allow publish your_static_ip;
            deny publish all;
            push rtmp://localhost/hls;
        }

        application hls {
            live on;
            hls on;
            hls_path /var/www/html/stream/hls;
            hls_fragment 3s;
            hls_playlist_length 60s;
        }
    }
}

Use OBS Studio with these settings:

[Settings]
Stream Type: Custom Streaming Server
URL: rtmp://your_server_ip/live
Stream Key: your_secure_key (use auth token)

For programmatic streaming via FFmpeg:

ffmpeg -f dshow -i video="Integrated Webcam" -c:v libx264 -preset veryfast \
-tune zerolatency -f flv rtmp://your_server_ip/live/streamkey

Create a simple auth endpoint in your Apache server (/var/www/html/auth.php):

<?php
$valid_keys = ['client1_key' => 'secret123', 'client2_key' => 'secret456'];
$stream_key = $_GET['key'];

if (array_key_exists($stream_key, $valid_keys)) {
    header("HTTP/1.1 200 OK");
    exit();
} else {
    header("HTTP/1.1 403 Forbidden");
    exit("Invalid stream key");
}
?>

Update Nginx RTMP config with auth:

application live {
    on_publish http://localhost/auth.php;
    # ... rest of config
}

Embed the HLS stream using video.js:

<video id="liveStream" class="video-js" controls>
    <source src="http://yourdomain.com/stream/hls/stream.m3u8" type="application/x-mpegURL">
</video>
<script src="https://vjs.zencdn.net/7.20.3/video.min.js"></script>
  • Implement TLS for RTMP (port 443)
  • Use fail2ban to block brute-force attempts
  • Rotate stream keys periodically
  • Set up bandwidth throttling in Nginx

For lower latency, consider a WebRTC solution using Janus Gateway:

sudo docker run -d --name janus \
    -p 8088:8088 -p 8188:8188 \
    -p 8089:8089 -p 8189:8189 \
    -e "DOCKER_IP=your_server_ip" \
    -v /etc/letsencrypt:/etc/letsencrypt:ro \
    meetecho/janus-gateway

Client-side JavaScript:

const peer = new RTCPeerConnection({
    iceServers: [{urls: "stun:your_server_ip:3478"}]
});
// Add video track from webcam
navigator.mediaDevices.getUserMedia({video: true})
    .then(stream => {
        stream.getTracks().forEach(track => peer.addTrack(track, stream));
    });

To stream live video from a Windows laptop to a Linux server and embed it in a website, we'll need a three-component architecture:

  1. Capture device (Windows laptop webcam)
  2. Streaming server (Your Ubuntu/Apache box)
  3. Client-side player (Website visitors)

First, install FFmpeg on your Windows machine. We'll use it to capture and stream the webcam feed:

# FFmpeg command to stream webcam to Linux server
ffmpeg -f dshow -i video="Integrated Webcam" -vcodec libx264 -preset ultrafast -tune zerolatency -f mpegts udp://your_server_ip:1234

For authentication, you can use SSH tunneling:

# Set up SSH tunnel first
ssh -L 1234:localhost:1234 user@your_server_ip
# Then run FFmpeg to localhost
ffmpeg -f dshow -i video="Integrated Webcam" -vcodec libx264 -f mpegts tcp://localhost:1234

On your Ubuntu server, install necessary packages:

sudo apt update
sudo apt install ffmpeg apache2 libapache2-mod-php

Create a PHP script to handle the stream:

 /dev/null 2>&1 &");
?>

Install and configure Nginx with RTMP module (works alongside Apache):

sudo apt install libnginx-mod-rtmp

Add to /etc/nginx/nginx.conf:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
            live on;
            record off;
        }
    }
}

Use HTML5 video with a JavaScript player like hls.js:




For production use, implement these security measures:

  • Set up HTTPS for your website
  • Use token authentication for streams
  • Configure firewall rules to limit access to streaming ports
  • Implement basic auth for the streaming endpoint

For lower latency, consider WebRTC with Janus Gateway:

# Install Janus on Ubuntu
sudo apt install janus janus-gateway

This provides sub-second latency but requires more complex setup.