0.1.4 • Published 1 year ago

@mediafish/rtmp-server v0.1.4

Weekly downloads
13
License
MIT
Repository
github
Last release
1 year ago

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

NameTypeRequiredDefaultDescription
pathstringYesN/AA specific path that a client can connect
optionsobjectNo{}An object holding option values that are used to override the internal option values
supported options
NameTypeDefaultDescription
portnumber1935The port number this server listens for

return value

An instance of RTMPStream (See class RTMPStream)

createServer([options])

Creates an RTMP server

params

NameTypeRequiredDefaultDescription
optionsobjectNo{}An object holding option values that are used to override the internal option values
supported options
NameTypeDefaultDescription
portnumber1935The port number this server listens for
maxConnectionNumnumber1The number of connections that can be established concurrently
maxStreamNumnumber1The 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

NameTypeRequiredDefaultDescription
eventstringYesN/Aevent should be 'error' or a specific path within the RTMP server
listenerfunctionYesN/AIf 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

NameTypeDescription
pathstringThe 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

NameTypeRequiredDefaultDescription
eventstringYesN/Aevent 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.
listenerfunctionYesN/AIf 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

NameTypeDescription
namestringThe 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

PropertyTypeRequiredDefaultDescription
typestringYesN/AEither of {'video'/'audio'/'data'}
timestampnumberYesN/AAn integer value that represents an absolute timestamp in millisecond that wraps around every 32 bit

Video (extends Message)

PropertyTypeRequiredDefaultDescription
dataAVCYesN/AAn isntance of AVC (See @mediafish/flv)

Audio (extends Message)

PropertyTypeRequiredDefaultDescription
dataAACYesN/AAn isntance of AAC (See @mediafish/flv)

Data (extends Message)

PropertyTypeRequiredDefaultDescription
dataArrayYesN/AAn array that contains objects, string, or number
0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago