Skip to content
/ rtmp Public
generated from openpeeps/pistachio

RTMP Client & Server - Real-Time Messaging Protocol for Nim πŸ‘‘

Notifications You must be signed in to change notification settings

openpeeps/rtmp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

RTMP Client & Server - Real-Time Messaging Protocol for Nim πŸ‘‘

nimble install rtmp

API reference
Github Actions Github Actions

😍 Key Features

  • Based on Libevent for High performance networking
  • RTMP Client for connecting to RTMP servers
  • RTMP Server for accepting RTMP client connections
  • Super Lightweight and minimal dependencies (only Libevent)
  • Zero-Copy File Streaming support for efficient media delivery
  • Flexible API for handling RTMP messages and events
  • Live ingest support for streaming from livecam/microphone (SOON)

Examples

Here you can find some simple examples to get you started with the RTMP package.

Create a RTMP server

import pkg/rtmp

rtmp.startServer() # Start RTMP server on default port 1935

Create a RTMP streaming client

Use the following code to create a RTMP client that connects to an RTMP server and starts streaming media from disk. Check the runnable example from the examples/ folder for a complete working example.

import pkg/rtmp

let
  rtmpClient = newRtmpClient("rtmp://127.0.0.1/live/livestream")
  flvVideoPath = "./data/8721923-sd_426_226_25fps.flv"
  aacAudioPath = "./data/space_loop_78bpm.aac"

proc startStreaming(c: RtmpClient, ps: PlaylistState) =
  # Start streaming video and audio files with zero-copy.
  #
  # This is a simple example that streams one video and one
  # audio file in a loop. You can extend this to use playlists
  # and more complex logic.
  startStreamFlvZeroCopy(c, flvVideoPath, c.msgStreamId, startTs = c.ps.globalTs)
  startStreamAacAdtsZeroCopy(c, aacAudioPath, c.msgStreamId, 4'u8, startTs = c.ps.globalTs)

rtmpClient.ps = PlaylistState()
rtmpClient.onPublishOk =
  proc(c: RtmpClient) =
    # This callback is called when the client successfully connects
    # and publishes to the RTMP server. You can use this callback to
    # start streaming
    echo "[rtmp] Starting to stream video and audio..."
    startPacer(c, proc(c2: RtmpClient) = startStreaming(c2, c2.ps))
    
rtmpClient.onStreamEnd =
  proc (c: RtmpClient, st: StreamState, sent: int) =
    # This callback is called when a stream finishes sending all data.
    # You can use this to start the next video/audio in a playlist,
    # or to log stream end events.
    echo "[rtmp] Stream ended, bytes sent=", sent
    if st.msgType == 0x09'u8:
      # Video ended
      echo "[rtmp] Video stream ended. Replaying the video..."
      inc c.ps.videoIdx
      startStreamFlvZeroCopy(c, flvVideoPath, c.msgStreamId, startTs = c.ps.globalTs)
    elif st.msgType == 0x08'u8:
      # Audio ended
      echo "rtmp] Audio stream ended. Restarting audio..."
      inc c.ps.audioIdx
      startStreamAacAdtsZeroCopy(c, aacAudioPath, c.msgStreamId, 4'u8, startTs = c.ps.globalTs)

rtmpClient.onStreamError =
  proc(c: RtmpClient, st: StreamState, err: cstring) =
    echo "[rtmp] Stream error: ", err

echo "[rtmp] Starting event loop"
discard event_base_dispatch(rtmpClient.base)

Play RTMP Stream with VLC

To play an RTMP stream with URL rtmp://localhost/live/livestream on VLC player, open the player, go to Media > Open Network Stream, enter the URL and click Play.

Projects using RTMP package

Check out these projects that are using the RTMP package:

  • Groovebox β€” Lightweight CLI app for streaming to Icecast and YouTube/Twitch RTMP servers.

Todo

❀ Contributions & Support

🎩 License

MIT license. Made by Humans from OpenPeeps.
Copyright OpenPeeps & Contributors β€” All rights reserved.

Releases

No releases published

Packages

No packages published

Languages