Skip to content

ThibaultBee/krtmp

Repository files navigation

krtmp: An enhanced RTMP, FLV and AMF library for Kotlin Multiplatform and Android

krtmp is a Kotlin Multiplatform implementation of RTMP, FLV and AMF protocols from legacy RTMP to enhanced RTMP v2.

Note

krtmp code have been moved to komedia/komuxer. See this discussion.

RTMP

A RTMP client and server library for Kotlin Multiplatform.

Features:

  • RTMP client
  • RTMP server
  • Statistics
  • Support for legacy RTMP
  • Support for enhanced RTMP v2 (partial)

Supported protocols:

  • RTMP
  • RTMPS
  • RTMPT (not tested)

Installation

Adds the following dependency to your project:

implementation("io.github.thibaultbee.krtmp:rtmp:0.9.0")

Usage

Client

Use RtmpConnectionBuilder to create a RTMP client:

val client = RtmpConnectionBuilder.connect(
    "rtmp://my.server.com/app/streamkey" // Your RTMP server URL (incl app name and stream key)
)

Then prepare your live by sending these messages to the server:

client.createStream() // Send createStream message
client.publish(StreamPublishType.LIVE) // Send publish message

If you already have FLV data, write your video/audio data:

try {
    // Write metadata
    val metadata = OnMetadata.Metadata(...)
    client.writeSetDataFrame(0, metadata)

    while (true) {
        // Write audio data. `audioData` are in `AudioTagHeader` format. See FLV specification for more details.
        client.writeAudio(audioTimestamp, audioData)
        // Write video data. `videoData` are in `VideoTagHeader` format. See FLV specification for more details.
        client.writeVideo(videoTimestamp, videoData)
    }
} catch (e: Exception) {
    // Handle exception
}

See FLV for more details to write audio and video frames..

Server

Use RtmpConnectionBuilder to create a RTMP server:

val server = RtmpConnectionBuilder.bind("0.0.0.0:1935") // Listening on port 1935

Then start the server:

server.listen()

FLV

A muxer/demuxer for FLV.

Features:

  • Muxer for FLV
  • Demuxer for FLV
  • AMF0 metadata
  • AMF3 metadata
  • Support for legacy RTMP
  • Support for enhanced RTMP v1: AV1, HEVC, VP8, VP9
  • Support for enhanced RTMP v2: Multitrack, Opus,...

Installation

Adds the following dependencies to your project:

implementation("io.github.thibaultbee.krtmp:flv:0.9.0")

Usage

Creates a FLV muxer and add audio/video data:

val muxer = FLVMuxer(path = "/path/to/file.flv")

// Write FLV header
flvMuxer.encodeFlvHeader(hasAudio, hasVideo)

// Register audio configurations (if any)
val audioConfig = FLVAudioConfig(
    FlvAudioConfig.SoundFormat.AAC,
    FlvAudioConfig.SoundRate.KHZ44,
    FlvAudioConfig.SoundSize.SND8BIT,
    FlvAudioConfig.SoundType.STEREO
)
// Register video configurations (if any)
val videoConfig = FLVVideoConfig(
)

// Write onMetadata
muxer.encode(0, OnMetadata(audioConfig, videoConfig))

// Write audio/video data
val audioDataFactory = AACAudioDataFactory(...)
val videoDataFactory = AVCVideoDataFactory()
// Always start with sequence headers
muxer.encode(timestamp1, audioDataFactory.sequenceStart(...))
muxer.encode(timestamp2, videoDataFactory.sequenceStart(...))
// Then write your frames
muxer.encode(timestamp3, audioDataFactory.codedFrame(...))
muxer.encode(timestamp4, videoDataFactory.codedFrame(...))

// Till you're done, then
muxer.flush()

// Close the output
muxer.close()

AMF

A serializer/deserializer for AMF0 and AMF3.

Features:

  • Serializer for AMF0
  • Serializer for AMF3
  • Deserializer for AMF0
  • Deserializer for AMF3

Installation

It requires kotlinx.serialization library. See Setup for more details. Then, adds the following dependencies to your project:

implementation("io.github.thibaultbee.krtmp:amf:0.9.0")

Usage

Creates a class and make it serializable with @Serializable annotation:

@Serializable
class MyData(val a: Int, val b: String)

Then you can serialize it to AMF0:

val data = MyData(1, "Hello")
val array = Amf.encodeToByteArray(MyData.serializer(), data)

TODO

  • More tests (missing tests samples)

Licence

Copyright 2023 Thibault B.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Enhanced RTMP, FLV and AMF for Kotlin Multiplatform

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •  

Languages