JSPM

ws-mpegts-player

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q22935F
  • License MIT

WebSocket MPEGTS video player for streaming from WebSocket sources

Package Exports

  • ws-mpegts-player
  • ws-mpegts-player/dist/index.esm.js
  • ws-mpegts-player/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ws-mpegts-player) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

WebSocket MPEGTS Video Player

A JavaScript library for streaming MPEGTS video from WebSocket sources. This library provides utilities to work with mpegts.js for WebSocket-based MPEGTS streams.

Installation

npm install ws-mpegts-player

Features

  • Stream MPEGTS video from WebSocket URLs
  • Utility functions for creating and managing players
  • Stream health checking
  • Simple API with customizable callbacks
  • Support for multiple stream sources

Usage

Basic Usage

import { MpegtsPlayer, StreamService } from 'ws-mpegts-player';

// Get a reference to your video element
const videoElement = document.getElementById('videoPlayer');

// Create a new player instance
const player = MpegtsPlayer.createPlayer({
  videoElement,
  url: 'wss://live-ip-5.cautio.live:443/video/live/523077696394/1',
  autoplay: true,
  onError: (error) => {
    console.error('Player error:', error);
  },
  onPlaying: () => {
    console.log('Video is playing');
  },
  onStatistics: (stats) => {
    console.log('Stream statistics:', stats);
  }
});

// Control methods using the video element
videoElement.play();
videoElement.pause();

// Change stream source
MpegtsPlayer.destroyPlayer(player);
const newPlayer = MpegtsPlayer.createPlayer({
  videoElement,
  url: 'wss://live-ip-5.cautio.live:443/video/live/523077696394/2',
  autoplay: true
});

// Clean up when done
MpegtsPlayer.destroyPlayer(newPlayer);

Using the Stream Service

import { MpegtsPlayer, StreamService } from 'ws-mpegts-player';

const videoElement = document.getElementById('videoPlayer');
const vehicleId = 'MH12SX6507';

// Create a stream service instance
const streamService = new StreamService();

// Fetch stream links and initialize player
async function initializePlayer() {
  try {
    // Get stream links
    const streamLinks = await streamService.getStreamLinks(vehicleId);
    
    // Check if streams are active
    const isStream1Active = await MpegtsPlayer.checkStreamActive(streamLinks.streamlink1);
    
    // Choose the active stream
    const streamUrl = isStream1Active ? streamLinks.streamlink1 : streamLinks.streamlink2;
    
    // Initialize player with the active stream
    const player = MpegtsPlayer.createPlayer({
      videoElement,
      url: streamUrl,
      autoplay: true
    });
    
    return player;
  } catch (error) {
    console.error('Error initializing player:', error);
    throw error;
  }
}

initializePlayer()
  .then(player => {
    console.log('Player initialized successfully');
  })
  .catch(error => {
    console.error('Failed to initialize player:', error);
  });

API Reference

MpegtsPlayer

A collection of utility functions for working with mpegts.js.

Functions

  • createPlayer(options): Create a new player instance

    • options.videoElement: The video element to attach to
    • options.url: The stream URL
    • options.autoplay: Whether to autoplay (default: false)
    • options.onError: Error callback
    • options.onPlaying: Playing callback
    • options.onStatistics: Statistics callback
    • options.playerConfig: Additional mpegts.js configuration
  • destroyPlayer(player): Destroy a player instance

  • checkStreamActive(streamUrl, timeout): Check if a WebSocket stream is active

  • getFeatureSupport(): Get browser feature support information

StreamService

A service for fetching and managing stream links.

Constructor Options

const service = new StreamService({
  // Optional
  apiBaseUrl: string, // default: 'https://apis.delcaper.com/tracking/gps'
  timeout: number // default: 10000 (ms)
});

Methods

  • getStreamLinks(vehicleId): Fetch stream links for a vehicle
  • getBestStream(vehicleId): Get the best available stream for a vehicle

Browser Support

This library requires browsers that support:

  • Media Source Extensions (MSE)
  • WebSockets
  • Promise API

Modern versions of Chrome, Firefox, Safari, and Edge are supported.

Examples

See the examples directory for working examples:

Development

# Install dependencies
npm install

# Build the library
npm run build

License

MIT