Merge pull request #3494 from cesanta/rtsp

Add RTSP example
This commit is contained in:
Sergey Lyubka authored and GitHub committed 2026-04-06 09:26:59 +01:00
commit 2740eed84e
7 files changed
+2198

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
PROG ?= example # Program we are building
DELETE = rm -rf # Command to remove files
OUT ?= -o $(PROG) # Compiler argument for output file
SOURCES = main.c mongoose.c rtsp.c # Source code files
CFLAGS = -W -Wall -Wextra -g -I. -include rtsp.h # Build options
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
#CFLAGS_MONGOOSE += -DMG_ENABLE_LINES
ifeq ($(OS),Windows_NT) # Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD OUT=/Feprog.exe
PROG ?= example.exe # Use .exe suffix for the binary
CC = gcc # Use MinGW gcc compiler
CFLAGS += -lws2_32 # Link against Winsock library
DELETE = cmd /C del /Q /F /S # Command prompt command to delete files
OUT ?= -o $(PROG) # Build output
endif
all: $(PROG) # Default target. Build and run program
$(RUN) ./$(PROG) $(ARGS)
$(PROG): $(SOURCES) # Build program from sources
$(CC) $(SOURCES) $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) $(OUT)
clean: # Cleanup. Delete built program and all build artifacts
$(DELETE) $(PROG) *.o *.obj *.exe *.dSYM
+28
View File
@@ -0,0 +1,28 @@
// Copyright (c) 2026 Cesanta Software Limited
// All rights reserved
#include "mongoose.h"
int main(void) {
struct mg_mgr mgr;
struct mg_rtsp_opts opts = {
0,
0,
0,
mg_rtsp_get_mp4_frame,
(void *) "test.mp4",
mg_rtsp_get_mp4_audio,
(void *) "test.mp4",
{0x12, 0x10},
};
mg_mgr_init(&mgr);
mg_rtsp_listen(&mgr, "rtsp://0.0.0.0:8554/cam", &opts);
while (true) {
mg_mgr_poll(&mgr, 10);
}
mg_mgr_free(&mgr);
return 0;
}
+1
View File
@@ -0,0 +1 @@
../../../mongoose.c
+1
View File
@@ -0,0 +1 @@
../../../mongoose.h
File diff suppressed because it is too large. Load diff
+54
View File
@@ -0,0 +1,54 @@
#pragma once
#include "mongoose.h"
// Encoded video frame provided by user code
struct mg_rtsp_frame {
const void *data; // H.264 encoded frame (one access unit)
size_t len; // Frame size in bytes
uint64_t pts_us; // Presentation timestamp, microseconds
bool keyframe; // true for IDR frame
};
// Encoded audio frame provided by user code
struct mg_rtsp_audio {
const void *data; // AAC encoded frame, one access unit, no ADTS header
size_t len; // Frame size in bytes
uint64_t pts_us; // Presentation timestamp, microseconds
};
// Called by Mongoose when it needs the next video frame
// - frame->data must stay valid until the callback returns
// - Mongoose copies/packetizes it before returning
typedef bool (*mg_rtsp_next_frame_fn)(struct mg_rtsp_frame *frame,
void *fn_data);
// Called by Mongoose when it needs the next audio frame
// - audio->data must stay valid until the callback returns
// - Mongoose copies/packetizes it before returning
typedef bool (*mg_rtsp_next_audio_fn)(struct mg_rtsp_audio *audio,
void *fn_data);
// RTSP listener options (H.264 video, optional AAC audio)
struct mg_rtsp_opts {
unsigned width; // Optional, 0 if unknown
unsigned height; // Optional, 0 if unknown
unsigned fps; // Optional, 0 if unknown/variable
mg_rtsp_next_frame_fn fn; // User callback to provide frames
void *fn_data; // User callback data
mg_rtsp_next_audio_fn audio_fn; // Optional AAC callback, NULL if no audio
void *audio_fn_data; // Audio callback data
uint8_t aac_lc_config[2]; // AAC-LC config, e.g. {0x12, 0x10};
// sample rate and channels are inferred
// e.g. {0x12, 0x10} is 44.1 kHz, stereo.
};
// Start RTSP server listener
// E.g. mg_rtsp_listen(&mgr, "rtsp://0.0.0.0:8554/cam", &opts);
struct mg_connection *mg_rtsp_listen(struct mg_mgr *mgr, const char *url,
const struct mg_rtsp_opts *opts);
// Helper callbacks that get H.264 video audio from the MP4 file.
// fn_data must be a const char * path to the MP4 file. E.g. "test.mp4"
bool mg_rtsp_get_mp4_frame(struct mg_rtsp_frame *frame, void *fn_data);
bool mg_rtsp_get_mp4_audio(struct mg_rtsp_audio *audio, void *fn_data);
Binary file not shown.