Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Stream Live Audio With Low Latency

I have programmed a server that creates an audio stream from my MacBook's audio input, using express, osx-audio and lame:

const http = require("http");
const express = require("express");
const audio = require("osx-audio");
const lame = require("lame");
const audioInput = new audio.Input();

const encoder = new lame.Encoder({
  channels: 2,
  bitDepth: 16,
  sampleRate: 44100,
  bitRate: 128,
  outSampleRate: 22050,
  mode: lame.STEREO
});

audioInput.pipe(encoder);

const app = express();
const server = http.Server(app);

app.get("/stream.mp3", (req, res) => {
  res.set({
    "Content-Type": "audio/mpeg",
    "Transfer-Encoding": "chunked"
  });
  encoder.pipe(res);
});

server.listen(3000);

On the client side, the sound from this audio stream is included as an <audio> element like so:

<audio controls autoplay preload="none">
  <source src="./stream.mp3" type="audio/mpeg" />
  <p>Oops – your browser doesn't support HTML5 audio!</p>
</audio>

This works – I can hear the sound from the input source I've selected on my laptop from any browser that is connected to the server when I click the “play” button on the audio element.

However the audio played by the browser lags several seconds behind the original signal. It seems although I'm using preload="none", the browser buffers quite a lot of the audio stream before it starts playing it.

Is there something obvious missing here? Is there a better way to achieve real-time audio with only a few milliseconds latency instead of several seconds?

If you're interested, my project's complete source code is here on GitHub.

like image 749
Patrick Hund Avatar asked May 12 '19 06:05

Patrick Hund


People also ask

How do I stream low latency?

To disable or enable low latency on your channel, head to your dashboard. Click the Hamburger icon, then open the Preferences drop down menu and select Stream. Latency mode settings can be found under the Stream Key & Preferences section at the top of the page. Here, you can pick Low latency or Normal latency.

Which latency is best for live streaming?

For professional broadcasts, you want to aim for less than 15 seconds of latency. Dacast, for example, offers HLS delivery with RTMP ingests for low latency streaming. This reduces latency to 15 seconds or less. This is the type of latency that is acceptable for a professional broadcast.

Does low latency affect streaming?

With a lower latency, viewers are more likely to feel the issues between the encoder and the player. Network congestion and other factors may also cause live streaming issues, which can delay your stream. Delays can happen even when you have a great network that can sustain your average streaming bitrate.

How can I improve my streaming latency?

To fix it, try: Improve your Internet connection (use ethernet, upgrade bandwidth, stop unnecessary network usage, or update your network device). Lower down the frame rate, bitrate, and resolution in your stream settings. Use dynamical changing bitrate in network settings.


1 Answers

Rather than reinventing the wheel, you can use FFMPEG, advertised as “A complete, cross-platform solution to record, convert and stream audio and video.”

Example:

ffmpeg -re -i input -f rtsp -muxdelay 0.1 rtsp://server/live.sdp

You can choose which library (h.264, mpeg, etc) your browser is OK with.

like image 134
Dr Yuan Shenghai Avatar answered Oct 02 '22 17:10

Dr Yuan Shenghai