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

@mediafish/rtmp-server

v0.1.4

Published

A server that receives an RTMP live stream and populates a readable object stream of the published audio, video, and data messages

Downloads

84

Readme

Build Status Coverage Status Dependency Status Development Dependency Status Known Vulnerabilities npm Downloads XO code style

rtmp-server

A Node.js server that receives an RTMP live stream and populates a readable object stream of the published audio, video, and data messages

Only the publish command is supported. This server is only intended for live video/audio publishing and does not provide any playback functionalities.

Install

NPM

Usage

Example-1: A simple server with a single connection, single stream

const {createSimpleServer} = require('@mediafish/rtmp-server');
const {print} = require('@mediafish/flv');
// Start an RTMP server at rtmp://localhost/live
createSimpleServer('/live')
.on('data', ({timestamp, type, data}) => {
  console.log(`RTMP message: type=${type}, timestamp=${timestamp}`);
  switch (type) {
    case 'video':
      // data is FLV video tag (AVC)
      print(data);
      break;
    case 'audio':
      // data is FLV audio tag (AAC)
      print(data);
      break;
    case 'data':
      // data is an array of JS object
      for (const item of data) {
        console.log(`${JSON.stringify(item, null, 4)}`);
      }
      break;
  }
})
.on('error', err => {
  console.error(err.stack);
});

// Or you can simply pipe the stream into a writable stream
createSimpleServer('/live')
.pipe(new Transcoder('hls'))
.pipe(new FileWriter('./dist/'));

Example-2: An advanced server with multiple connections, multiple streams

const {createServer} = require('@mediafish/rtmp-server');
// Start an RTMP server at rtmp://localhost:19350/live/{main|sub}-camera
createServer({port: 19350, maxConnectionNum: 2, maxStreamNum: 2})
.once('/live/main-camera', handleConnection)
.once('/live/sub-camera', handleConnection)
.on('error', err => {
  console.error(err.stack);
});

function handleConnection(connection) {
  console.log(`Incoming connection: path="${connection.path}"`);
  return connection
  .once('stream-1', handleStream)
  .once('stream-2', handleStream)
  .on('error', err => {
    console.error(err.stack);
  });
}

function handleStream(stream) {
  console.log(`Published stream: name="${stream.name}"`);
  return stream
  .on('data', handleMessage)
  .on('error', err => {
    console.error(err.stack);
  });
}

function handleMessage({timestamp, type, data}) {
  console.log(`RTMP message: type=${type}, timestamp=${timestamp}`);
  switch (type) {
    case 'video':
      // data is FLV video tag (AVC)
      print(data);
      break;
    case 'audio':
      // data is FLV audio tag (AAC)
      print(data);
      break;
    case 'data':
      // data is an array
      for (const item of data) {
        console.log(`${JSON.stringify(item, null, 4)}`);
      }
      break;
  }
}

API

createSimpleServer(path[, options])

Creates an RTMP server with a single connection, single stream

params

| Name | Type | Required | Default | Description | | ------- | ------ | -------- | ------- | ------------- | | path | string | Yes | N/A | A specific path that a client can connect | | options | object | No | {} | An object holding option values that are used to override the internal option values |

supported options

| Name | Type | Default | Description | | ---------- | ------- | ------- | ------------- | | port | number | 1935 | The port number this server listens for|

return value

An instance of RTMPStream (See class RTMPStream)

createServer([options])

Creates an RTMP server

params

| Name | Type | Required | Default | Description | | ------- | ------ | -------- | ------- | ------------- | | options | object | No | {} | An object holding option values that are used to override the internal option values |

supported options

| Name | Type | Default | Description | | ---------- | ------- | ------- | ------------- | | port | number | 1935 | The port number this server listens for| | maxConnectionNum | number | 1 | The number of connections that can be established concurrently| | maxStreamNum | number | 1 | The number of streams the client can publish concurrently for each connection |

return value

An instance of RTMPServer (See class RTMPServer)

class RTMPServer extends EventEmitter

Represents an RTMP server

methods

All methods are inherited from EventEmitter

on(event, listener)

A method used to listen for a specific event

| Name | Type | Required | Default | Description | | ------- | ------ | -------- | ------- | ------------- | | event | string | Yes | N/A | event should be 'error' or a specific path within the RTMP server | | listener | function | Yes | N/A | If event equals to 'error', listener should be a function that takes an Error object. Otherwise, listener should be a function that takes an RTMPConnection object. |

return value

A reference to the RTMPServer, so that calls can be chained.

class RTMPConnection extends EventEmitter

Represents a connection from an RTMP client

properties

| Name | Type | Description | | ------- | ------ | ------------- | | path | string | The path string the client specified on connection as a part of URL. (e.g. rtmp://example.com/{path}) |

methods

All methods are inherited from EventEmitter

on(event, listener)

A method used to listen for a specific event

| Name | Type | Required | Default | Description | | ------- | ------ | -------- | ------- | ------------- | | event | string | Yes | N/A | event should be 'error' or a stream name with which the stream is published by the client. The stream name can be '*' which matches any names. | | listener | function | Yes | N/A | If event equals to 'error', listener should be a function that takes an Error object. Otherwise, listener should be a function that takes an RTMPStream object. |

return value

A reference to the RTMPConnection, so that calls can be chained.

class RTMPStream extends stream.Readable

Represents a stream of messages published by the RTMP client. The published audio, video, and data messages can be read from the stream. See Data format

properties

| Name | Type | Description | | ------- | ------ | ------------- | | name | string | The stream name with which the stream is published by the client. |

methods

All methods are inherited from stream.Readable

Data format

This section describes the structure of the messages that can be read from RTMPStream

Message

| Property | Type | Required | Default | Description | | ---------------- | ------------- | -------- | ------- | ------------- | | type | string | Yes | N/A | Either of {'video'/'audio'/'data'} | | timestamp | number | Yes | N/A | An integer value that represents an absolute timestamp in millisecond that wraps around every 32 bit |

Video (extends Message)

| Property | Type | Required | Default | Description | | ---------------- | ------------- | -------- | ------- | ------------- | | data | AVC | Yes | N/A | An isntance of AVC (See @mediafish/flv) |

Audio (extends Message)

| Property | Type | Required | Default | Description | | ---------------- | ------------- | -------- | ------- | ------------- | | data | AAC | Yes | N/A | An isntance of AAC (See @mediafish/flv) |

Data (extends Message)

| Property | Type | Required | Default | Description | | ---------------- | ------------- | -------- | ------- | ------------- | | data | Array | Yes | N/A | An array that contains objects, string, or number |