Quickly Build a Video RTSP Streaming Service with Zlm4j and Spring Boot

This guide shows how to integrate the Zlm4j library into a Spring Boot project to quickly set up RTSP, RTMP, HTTP, and RTP streaming services, configure ports, handle authentication and stream state events, and test the setup with ffmpeg and VLC.

Dunmao Tech Hub
Dunmao Tech Hub
Dunmao Tech Hub
Quickly Build a Video RTSP Streaming Service with Zlm4j and Spring Boot

Purpose

The author needed a video push‑pull streaming service. Implementing RTSP directly with Netty proved difficult, so they turned to the Zlm4j library, a JNA wrapper for ZLMediaKit, which can be easily embedded in a Spring Boot application.

1. Maven Dependencies

Add the local zlm4j JAR and the JNA library to your pom.xml:

<dependency>
  <groupId>com.aizuda</groupId>
  <artifactId>zlm4j</artifactId>
  <version>1.0.4</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/zlm4j-1.0.4.jar</systemPath>
</dependency>
<dependency>
  <groupId>net.java.dev.jna</groupId>
  <artifactId>jna</artifactId>
  <version>5.14.0</version>
</dependency>

2. Configuration Class

The configuration creates a ZLMApi instance via JNA, initializes the SDK, and starts HTTP, RTSP, RTMP, and RTP servers on specific ports. It also provides a MK_EVENTS bean for handling authentication and stream‑change callbacks.

HTTP: 7788

RTSP: 9758

RTMP: 9759

RTP: 32000

package org.example;

import com.aizuda.zlm4j.core.ZLMApi;
import com.aizuda.zlm4j.structure.MK_EVENTS;
import com.sun.jna.Native;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ZlmServerConfig {
    @Bean
    public ZLMApi zlmApi() {
        // Load native mk_api library
        ZLMApi ZLM_API = Native.load("mk_api", ZLMApi.class);
        // Initialize SDK (parameters are placeholders)
        ZLM_API.mk_env_init1(1,1,1,null,0,0,null,0,null,null,null);
        short http_server_port = ZLM_API.mk_http_server_start((short)7788, 0);
        short rtsp_server_port = ZLM_API.mk_rtsp_server_start((short)9758, 0);
        short rtmp_server_port = ZLM_API.mk_rtmp_server_start((short)9759, 0);
        short rtp_server_port = ZLM_API.mk_rtp_server_start((short)32000);
        return ZLM_API;
    }

    @Bean
    public MK_EVENTS mkEvents() {
        return new MK_EVENTS();
    }
}

3. Event Implementation

In the application’s main class a separate thread registers callbacks for publishing authentication and stream state changes. The authentication callback extracts URL parameters and approves the request, while the state‑change callback logs app, stream, and schema information.

package org.example;

import com.aizuda.zlm4j.core.ZLMApi;
import com.aizuda.zlm4j.structure.MK_EVENTS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main implements CommandLineRunner {
    @Autowired
    MK_EVENTS mkEvents;
    @Autowired
    ZLMApi zlmApi;

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // Publish authentication callback
        mkEvents.on_mk_media_publish = (url_info, invoker, sender) -> {
            String param = zlmApi.mk_media_info_get_params(url_info);
            System.err.println("Authentication reminder:");
            zlmApi.mk_publish_auth_invoker_do(invoker, "", 0, 0);
        };
        // Stream state change callback
        mkEvents.on_mk_media_changed = (regist, sender) -> {
            System.out.println("app:" + zlmApi.mk_media_source_get_app(sender));
            System.out.println("stream:" + zlmApi.mk_media_source_get_stream(sender));
            System.out.println("schema:" + zlmApi.mk_media_source_get_schema(sender));
            System.out.println("Stream change notification:" + regist);
        };
        zlmApi.mk_events_listen(mkEvents);
    }
}

4. Testing the Push‑Pull Flow

Use ffmpeg to push a local video file to the RTSP server, attaching a token query parameter to trigger the authentication callback:

ffmpeg -re -an -i /home/seaua/Desktop/input.mp4 -c:v libx264 -f rtsp rtsp://127.0.0.1:9758/steam/1?token=112233

When the stream starts, the server console prints the token and stream‑state change messages.

Event log output
Event log output

5. Playback Verification

Open VLC (or any RTMP/FLV capable player) and play the stream using the URL: rtmp://127.0.0.1:9759/steam/1 The same ports and protocols defined in the configuration are used, and additional playback‑time authentication can be added by implementing the corresponding callbacks.

VLC playback screenshot
VLC playback screenshot
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Spring Bootvideo streamingMedia ServerRTSPZlm4j
Dunmao Tech Hub
Written by

Dunmao Tech Hub

Sharing selected technical articles synced from CSDN. Follow us on CSDN: Dunmao.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.