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-playerFeatures
- 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 instanceoptions.videoElement: The video element to attach tooptions.url: The stream URLoptions.autoplay: Whether to autoplay (default: false)options.onError: Error callbackoptions.onPlaying: Playing callbackoptions.onStatistics: Statistics callbackoptions.playerConfig: Additional mpegts.js configuration
destroyPlayer(player): Destroy a player instancecheckStreamActive(streamUrl, timeout): Check if a WebSocket stream is activegetFeatureSupport(): 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 vehiclegetBestStream(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:
- Simple Player: Basic player implementation
Development
# Install dependencies
npm install
# Build the library
npm run buildLicense
MIT