npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

media-switcher

v1.0.5

Published

A JavaScript class to allow MediaRecorder switch between media streams

Downloads

112

Readme

MediaSwitcher class

A Javascript class that allows the switching of video and audio tracks in a MediaStream.

What does it do?

JavaScript Media and Streaming API's MediaRecorder has a problem. It can not switch streams or stream tracks while recording. Recording stops if this is attempted.

MediaSwitcher acts as an intermediary between your stream sources and the MediaRecorder. It connects to the MediaRecorder as a single, uninterrupted stream source, and transmits your various other stream sources (video, audio, etc.) to the recorder. As a result, you can switch between contents.

How does it work?

The class is the implementation of a long known workaround for the above problem. It creates an RTCPeerConnection with itself and sends your stream through it. The output is actually the receiving peer's stream.

Live demo

Demo: https://dhk6x2.csb.app/

Sandbox: https://codesandbox.io/s/dhk6x2

Installation

You would've never guessed:

npm i media-switcher

and then:

const MediaSwitcher = require('media-switcher');
const mediaSwitcher = new MediaSwitcher();

Alternatively you can include MediaSwitcher.class.js the traditional way:

<script src="MediaSwitcher.class.js" />

Methods

initialize(inputStream: MediaStream): Promise(outputStream: MediaStream)

Initializes a MediaSwitcher. The stream can be an empty MediaStream and you can add tracks later. Returns a Promise, which resolves to the MediaStream coming from the MediaSwitcher. Use this stream for your MediaRecorder.

changeTrack(track: MediaStreamTrack | CanvasCaptureMediaStreamTrack)

Changes the current audio or video track to the supplied one. The kind of the track will be autodetected.

changeStream(stream: MediaStream)

Changes both the audio and the video tracks to the one in the supplied stream.

Examples

Let's say you have two <video> elements in your HTML DOM:

<video id="inputVideo" loop controls src="nyancat.mp4" />
<video id="outputVideo" muted />

The latter is muted so we won't hear two audio streams.

Let's connect the stream from inputVideo to outputVideo:

//  Get videos
const inputVideo = document.getElementById('inputVideo');
const outputVideo = document.getElementById('outputVideo');

//  Create a stream source
var myStream = inputVideo.captureStream();

//  Create instance
var mediaSwitcher = new MediaSwitcher();

//  Initialize MediaSwitcher
mediaSwitcher.initialize(myStream)
    .then(switcherStream => {
        outputVideo.srcObject = switcherStream;
        outputVideo.play();
    })
    .catch(err => console.error(err.message));

Now, if you play inputVideo, its content will appear in outputVideo, but it passes through the MediaSwitcher.

Switching streams

Let's change the audio track to a different one, coming from an MP3 file.

<video id="inputVideo" loop controls src="nyancat.mp4" />
<video id="outputVideo" muted />
<audio id="inputAudio" loop controls src="erika.mp3" />

To achieve this, add this to the code:

const inputAudio = document.getElementById('inputAudio');

inputAudio.onplay = () => {
    inputVideo.muted = true;
    const track = inputAudio.srcObject.getAudioStreams()[0];
    mediaSwitcher.changeTrack(track);
}

Now when you click the Play button on the audio element, the MP3 will replace the video's original soundtrack, while the video keeps playing.