0.1.1 • Published 1 year ago

@megh-computing/mp4-stream-player v0.1.1

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
1 year ago

MP4 Stream Player - Megh Computing

npm npm dependencies node npm type definitions

Plays a (live) fragmented MP4 stream in an HTML video element using the Media Source Extensions (MSE) API. This package cannot be used to play pre-recorded video unless the video data is appended to the player at the playback rate (real-time), which is not typically feasible without decoding/parsing the MP4 file and inspecting the sample timings.

Features

  • Hybrid package: CommonJS and ESM supported
  • Modern (ES2020+) browsers supported: Chrome, Edge, and Firefox all work with acceptable performance
  • First-class TypeScript support

Environment

This package only works in the browser.

Firefox seems to have performance issues decoding live video using MSE.

Installation

Install the package as a dependency using npm:

$ npm install @megh-computing/mp4-stream-player

or, install using yarn:

$ yarn add @megh-computing/mp4-stream-player

Example Usage

import {MP4StreamPlayer} from "@megh-computing/mp4-stream-player";

...

// Create player and configure events
const player: MP4StreamPlayer = new MP4StreamPlayer();
player.on("error", (type, message) => {
    console.error("Decode error:", type, message);
    // Dispose player with reason
    player.dispose("decode");
    // Close MP4 socket
    socket.close(4000, "decode error");
    // Re-connect to MP4 socket?
});

// Optional configuration (defaults provided)
player.setFastForwardThreshold(0.25); // How far behind live can the player get before playback is set to 2x
player.setSeekThreshold(1.0); // How far behind live can the player get before seeking to live
player.setPruneThreshold(120.0); // How old media data is before it's pruned
player.setPruneTimeSpan(30.0); // How much media data is pruned at a time

...

// Start the player
player.play();

...

// Append MP4 data to the player
player.append(data);

...

// If the stream restarts (indicated by the socket closing),
// then dispose the player and create a new one when the socket reconnects
player.dispose();

License

Copyright (c) 2023 Megh Computing, Inc.

All rights reserved. No warranty, explicit or implied, provided. Unauthorized use, modification, or distribution is strictly prohibited. Homepage: https://megh.com/

Please contact us if you use Megh VAS and would like to be issued a license to use this package.