fix wrong path
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
#include "../include/rtmp_capi.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void on_connect_cb(const char* ip, void* data)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef RTMP_CAPI_H
|
||||
#define RTMP_CAPI_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -10,16 +10,14 @@ extern "C" {
|
||||
|
||||
typedef void *RtmpServerHandle;
|
||||
|
||||
enum RtmpLogLevel
|
||||
{
|
||||
enum RtmpLogLevel {
|
||||
RTMP_LOG_ERROR = 0,
|
||||
RTMP_LOG_WARN = 1,
|
||||
RTMP_LOG_INFO = 2,
|
||||
RTMP_LOG_DEBUG = 3
|
||||
};
|
||||
|
||||
struct RtmpStreamStats
|
||||
{
|
||||
struct RtmpStreamStats {
|
||||
uint64_t bytes_sent;
|
||||
uint64_t bytes_received;
|
||||
uint32_t video_frames;
|
||||
@@ -35,11 +33,15 @@ typedef void (*RtmpOnPublishCallback)(const char* client_ip, const char* app,
|
||||
typedef void (*RtmpOnPlayCallback)(const char *client_ip, const char *app,
|
||||
const char *stream_key, void *user_data);
|
||||
typedef void (*RtmpOnAudioDataCallback)(const char *app, const char *stream_key,
|
||||
const uint8_t* data, uint32_t length, uint32_t timestamp, void* user_data);
|
||||
const uint8_t *data, uint32_t length,
|
||||
uint32_t timestamp, void *user_data);
|
||||
typedef void (*RtmpOnVideoDataCallback)(const char *app, const char *stream_key,
|
||||
const uint8_t* data, uint32_t length, uint32_t timestamp, void* user_data);
|
||||
const uint8_t *data, uint32_t length,
|
||||
uint32_t timestamp, void *user_data);
|
||||
typedef void (*RtmpOnDisconnectCallback)(const char *client_ip, const char *app,
|
||||
const char* stream_key, bool was_publishing, bool was_playing, void* user_data);
|
||||
const char *stream_key,
|
||||
bool was_publishing, bool was_playing,
|
||||
void *user_data);
|
||||
typedef bool (*RtmpAuthCallback)(const char *app, const char *stream_key,
|
||||
const char *client_ip, void *user_data);
|
||||
|
||||
@@ -62,7 +64,8 @@ void rtmp_server_set_on_audio_data(RtmpServerHandle handle,
|
||||
void rtmp_server_set_on_video_data(RtmpServerHandle handle,
|
||||
RtmpOnVideoDataCallback cb, void *user_data);
|
||||
void rtmp_server_set_on_disconnect(RtmpServerHandle handle,
|
||||
RtmpOnDisconnectCallback cb, void* user_data);
|
||||
RtmpOnDisconnectCallback cb,
|
||||
void *user_data);
|
||||
void rtmp_server_set_auth_callback(RtmpServerHandle handle, RtmpAuthCallback cb,
|
||||
void *user_data);
|
||||
|
||||
@@ -81,7 +84,8 @@ int rtmp_server_get_active_publishers(RtmpServerHandle handle);
|
||||
int rtmp_server_get_active_players(RtmpServerHandle handle);
|
||||
int rtmp_server_get_total_connections(RtmpServerHandle handle);
|
||||
struct RtmpStreamStats rtmp_server_get_stream_stats(RtmpServerHandle handle,
|
||||
const char* app, const char* stream_key);
|
||||
const char *app,
|
||||
const char *stream_key);
|
||||
|
||||
// Recording
|
||||
bool rtmp_server_start_recording(RtmpServerHandle handle, const char *app,
|
||||
@@ -93,14 +97,15 @@ bool rtmp_server_is_recording(RtmpServerHandle handle, const char* app,
|
||||
|
||||
// Broadcasting
|
||||
bool rtmp_server_broadcast_audio(RtmpServerHandle handle, const char *app,
|
||||
const char* stream_key, const uint8_t* data, uint32_t length,
|
||||
uint32_t timestamp);
|
||||
const char *stream_key, const uint8_t *data,
|
||||
uint32_t length, uint32_t timestamp);
|
||||
bool rtmp_server_broadcast_video(RtmpServerHandle handle, const char *app,
|
||||
const char* stream_key, const uint8_t* data, uint32_t length,
|
||||
uint32_t timestamp);
|
||||
const char *stream_key, const uint8_t *data,
|
||||
uint32_t length, uint32_t timestamp);
|
||||
// FIXED: Added missing declaration
|
||||
bool rtmp_server_broadcast_metadata(RtmpServerHandle handle, const char *app,
|
||||
const char* stream_key, const uint8_t* data, uint32_t length);
|
||||
const char *stream_key, const uint8_t *data,
|
||||
uint32_t length);
|
||||
|
||||
// Logger
|
||||
void rtmp_logger_set_level(enum RtmpLogLevel level);
|
||||
|
||||
@@ -1,31 +1,29 @@
|
||||
#ifndef RTMP_SERVER_H
|
||||
#define RTMP_SERVER_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <arpa/inet.h>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <cstdint>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <netinet/in.h>
|
||||
#include <queue>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
namespace rtmp
|
||||
{
|
||||
namespace rtmp {
|
||||
|
||||
// RTMP Message Types
|
||||
enum class MessageType : uint8_t
|
||||
{
|
||||
enum class MessageType : uint8_t {
|
||||
SET_CHUNK_SIZE = 1,
|
||||
ABORT_MESSAGE = 2,
|
||||
ACKNOWLEDGEMENT = 3,
|
||||
@@ -44,8 +42,7 @@ namespace rtmp
|
||||
};
|
||||
|
||||
// User Control Message Types
|
||||
enum class UserControlType : uint16_t
|
||||
{
|
||||
enum class UserControlType : uint16_t {
|
||||
STREAM_BEGIN = 0,
|
||||
STREAM_EOF = 1,
|
||||
STREAM_DRY = 2,
|
||||
@@ -56,8 +53,7 @@ namespace rtmp
|
||||
};
|
||||
|
||||
// AMF0 Data Types
|
||||
enum class AMF0Type : uint8_t
|
||||
{
|
||||
enum class AMF0Type : uint8_t {
|
||||
NUMBER = 0x00,
|
||||
BOOLEAN = 0x01,
|
||||
STRING = 0x02,
|
||||
@@ -69,17 +65,10 @@ namespace rtmp
|
||||
};
|
||||
|
||||
// Log Levels
|
||||
enum class LogLevel
|
||||
{
|
||||
ERROR = 0,
|
||||
WARN = 1,
|
||||
INFO = 2,
|
||||
DEBUG = 3
|
||||
};
|
||||
enum class LogLevel { ERROR = 0, WARN = 1, INFO = 2, DEBUG = 3 };
|
||||
|
||||
// AMF0 Value
|
||||
class AMF0Value
|
||||
{
|
||||
class AMF0Value {
|
||||
public:
|
||||
AMF0Type type;
|
||||
double number;
|
||||
@@ -91,8 +80,7 @@ namespace rtmp
|
||||
};
|
||||
|
||||
// RTMP Chunk Header
|
||||
struct ChunkHeader
|
||||
{
|
||||
struct ChunkHeader {
|
||||
uint8_t fmt;
|
||||
uint32_t csid;
|
||||
uint32_t timestamp;
|
||||
@@ -103,15 +91,13 @@ namespace rtmp
|
||||
};
|
||||
|
||||
// RTMP Message
|
||||
struct RTMPMessage
|
||||
{
|
||||
struct RTMPMessage {
|
||||
ChunkHeader header;
|
||||
std::vector<uint8_t> payload;
|
||||
};
|
||||
|
||||
// Stream Information
|
||||
struct StreamInfo
|
||||
{
|
||||
struct StreamInfo {
|
||||
std::string app;
|
||||
std::string stream_key;
|
||||
bool is_publishing;
|
||||
@@ -122,8 +108,7 @@ namespace rtmp
|
||||
};
|
||||
|
||||
// Stream Statistics
|
||||
struct StreamStatistics
|
||||
{
|
||||
struct StreamStatistics {
|
||||
uint64_t bytes_sent = 0;
|
||||
uint64_t bytes_received = 0;
|
||||
uint32_t video_frames = 0;
|
||||
@@ -133,40 +118,35 @@ namespace rtmp
|
||||
|
||||
StreamStatistics() : start_time(std::chrono::steady_clock::now()) {}
|
||||
|
||||
double getBitrate() const
|
||||
{
|
||||
double getBitrate() const {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::seconds>(
|
||||
now - start_time).count();
|
||||
if (duration == 0) return 0;
|
||||
auto duration =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(now - start_time)
|
||||
.count();
|
||||
if (duration == 0)
|
||||
return 0;
|
||||
return (bytes_sent * 8.0) / duration / 1000.0; // kbps
|
||||
}
|
||||
|
||||
double getUptime() const
|
||||
{
|
||||
double getUptime() const {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
return std::chrono::duration_cast<std::chrono::seconds>(
|
||||
now - start_time).count();
|
||||
return std::chrono::duration_cast<std::chrono::seconds>(now - start_time)
|
||||
.count();
|
||||
}
|
||||
};
|
||||
|
||||
// GOP Cache for instant playback
|
||||
class GOPCache
|
||||
{
|
||||
class GOPCache {
|
||||
public:
|
||||
void addVideoFrame(const std::vector<uint8_t> &data, uint32_t timestamp);
|
||||
void addAudioFrame(const std::vector<uint8_t> &data, uint32_t timestamp);
|
||||
void addMetadata(const std::vector<uint8_t> &data);
|
||||
void sendToPlayer(class RTMPSession *session);
|
||||
void clear();
|
||||
bool hasKeyframe() const
|
||||
{
|
||||
return has_keyframe;
|
||||
}
|
||||
bool hasKeyframe() const { return has_keyframe; }
|
||||
|
||||
private:
|
||||
struct CachedFrame
|
||||
{
|
||||
struct CachedFrame {
|
||||
MessageType type;
|
||||
std::vector<uint8_t> data;
|
||||
uint32_t timestamp;
|
||||
@@ -181,23 +161,19 @@ namespace rtmp
|
||||
};
|
||||
|
||||
// FLV File Recorder
|
||||
class FLVRecorder
|
||||
{
|
||||
class FLVRecorder {
|
||||
public:
|
||||
FLVRecorder(const std::string &filename);
|
||||
~FLVRecorder();
|
||||
|
||||
bool start();
|
||||
void stop();
|
||||
bool isRecording() const
|
||||
{
|
||||
return recording;
|
||||
}
|
||||
bool isRecording() const { return recording; }
|
||||
|
||||
void writeAudioFrame(const std::vector<uint8_t> &data, uint32_t timestamp);
|
||||
void writeVideoFrame(const std::vector<uint8_t> &data, uint32_t timestamp);
|
||||
void writeMetadata(const std::map<std::string, std::shared_ptr<AMF0Value>>
|
||||
&metadata);
|
||||
void writeMetadata(
|
||||
const std::map<std::string, std::shared_ptr<AMF0Value>> &metadata);
|
||||
|
||||
private:
|
||||
std::string filename;
|
||||
@@ -209,13 +185,12 @@ namespace rtmp
|
||||
void writeFLVHeader();
|
||||
void writeFLVTag(uint8_t tag_type, const std::vector<uint8_t> &data,
|
||||
uint32_t timestamp);
|
||||
std::vector<uint8_t> encodeMetadata(const
|
||||
std::map<std::string, std::shared_ptr<AMF0Value>> &metadata);
|
||||
std::vector<uint8_t> encodeMetadata(
|
||||
const std::map<std::string, std::shared_ptr<AMF0Value>> &metadata);
|
||||
};
|
||||
|
||||
// RTMP Client Session
|
||||
class RTMPSession
|
||||
{
|
||||
class RTMPSession {
|
||||
public:
|
||||
RTMPSession(int fd, const std::string &client_ip);
|
||||
~RTMPSession();
|
||||
@@ -226,27 +201,12 @@ namespace rtmp
|
||||
bool sendChunk(uint32_t csid, uint32_t timestamp, uint8_t msg_type,
|
||||
uint32_t stream_id, const std::vector<uint8_t> &data);
|
||||
|
||||
int getFd() const
|
||||
{
|
||||
return client_fd;
|
||||
}
|
||||
const StreamInfo &getStreamInfo() const
|
||||
{
|
||||
return stream_info;
|
||||
}
|
||||
StreamInfo &getStreamInfo()
|
||||
{
|
||||
return stream_info;
|
||||
}
|
||||
int getFd() const { return client_fd; }
|
||||
const StreamInfo &getStreamInfo() const { return stream_info; }
|
||||
StreamInfo &getStreamInfo() { return stream_info; }
|
||||
|
||||
void setChunkSize(uint32_t size)
|
||||
{
|
||||
chunk_size = size;
|
||||
}
|
||||
uint32_t getChunkSize() const
|
||||
{
|
||||
return chunk_size;
|
||||
}
|
||||
void setChunkSize(uint32_t size) { chunk_size = size; }
|
||||
uint32_t getChunkSize() const { return chunk_size; }
|
||||
|
||||
// Acknowledgement handling
|
||||
void onBytesReceived(size_t bytes);
|
||||
@@ -258,34 +218,18 @@ namespace rtmp
|
||||
void sendPong(uint32_t timestamp);
|
||||
|
||||
// Statistics
|
||||
StreamStatistics &getStats()
|
||||
{
|
||||
return stats;
|
||||
}
|
||||
const StreamStatistics &getStats() const
|
||||
{
|
||||
return stats;
|
||||
}
|
||||
StreamStatistics &getStats() { return stats; }
|
||||
const StreamStatistics &getStats() const { return stats; }
|
||||
|
||||
// Message queue access for server
|
||||
std::queue<RTMPMessage> &getMessageQueue()
|
||||
{
|
||||
return message_queue;
|
||||
}
|
||||
std::mutex &getQueueMutex()
|
||||
{
|
||||
return queue_mutex;
|
||||
}
|
||||
std::queue<RTMPMessage> &getMessageQueue() { return message_queue; }
|
||||
std::mutex &getQueueMutex() { return queue_mutex; }
|
||||
|
||||
// Last activity tracking
|
||||
std::chrono::steady_clock::time_point getLastActivity() const
|
||||
{
|
||||
std::chrono::steady_clock::time_point getLastActivity() const {
|
||||
return last_activity;
|
||||
}
|
||||
void updateActivity()
|
||||
{
|
||||
last_activity = std::chrono::steady_clock::now();
|
||||
}
|
||||
void updateActivity() { last_activity = std::chrono::steady_clock::now(); }
|
||||
|
||||
// AMF0 public access for server
|
||||
std::shared_ptr<AMF0Value> decodeAMF0(const uint8_t *data, size_t len,
|
||||
@@ -325,8 +269,8 @@ namespace rtmp
|
||||
// AMF0 Encoding/Decoding
|
||||
std::vector<uint8_t> encodeAMF0String(const std::string &str);
|
||||
std::vector<uint8_t> encodeAMF0Number(double num);
|
||||
std::vector<uint8_t> encodeAMF0Object(const
|
||||
std::map<std::string, std::shared_ptr<AMF0Value>> &obj);
|
||||
std::vector<uint8_t> encodeAMF0Object(
|
||||
const std::map<std::string, std::shared_ptr<AMF0Value>> &obj);
|
||||
|
||||
// Command handlers
|
||||
bool handleConnect(const std::vector<std::shared_ptr<AMF0Value>> &args);
|
||||
@@ -347,37 +291,35 @@ namespace rtmp
|
||||
// Callback types
|
||||
using OnConnectCallback = std::function<void(std::shared_ptr<RTMPSession>)>;
|
||||
using OnPublishCallback =
|
||||
std::function<void(std::shared_ptr<RTMPSession>, const std::string& app, const std::string& stream_key)>;
|
||||
std::function<void(std::shared_ptr<RTMPSession>, const std::string &app,
|
||||
const std::string &stream_key)>;
|
||||
using OnPlayCallback =
|
||||
std::function<void(std::shared_ptr<RTMPSession>, const std::string& app, const std::string& stream_key)>;
|
||||
std::function<void(std::shared_ptr<RTMPSession>, const std::string &app,
|
||||
const std::string &stream_key)>;
|
||||
using OnAudioDataCallback =
|
||||
std::function<void(std::shared_ptr<RTMPSession>, const std::vector<uint8_t>& data, uint32_t timestamp)>;
|
||||
std::function<void(std::shared_ptr<RTMPSession>,
|
||||
const std::vector<uint8_t> &data, uint32_t timestamp)>;
|
||||
using OnVideoDataCallback =
|
||||
std::function<void(std::shared_ptr<RTMPSession>, const std::vector<uint8_t>& data, uint32_t timestamp)>;
|
||||
using OnMetaDataCallback =
|
||||
std::function<void(std::shared_ptr<RTMPSession>, const std::map<std::string, std::shared_ptr<AMF0Value>>& metadata)>;
|
||||
std::function<void(std::shared_ptr<RTMPSession>,
|
||||
const std::vector<uint8_t> &data, uint32_t timestamp)>;
|
||||
using OnMetaDataCallback = std::function<void(
|
||||
std::shared_ptr<RTMPSession>,
|
||||
const std::map<std::string, std::shared_ptr<AMF0Value>> &metadata)>;
|
||||
using OnDisconnectCallback = std::function<void(std::shared_ptr<RTMPSession>)>;
|
||||
using AuthCallback =
|
||||
std::function<bool(const std::string& app, const std::string& stream_key, const std::string& client_ip)>;
|
||||
std::function<bool(const std::string &app, const std::string &stream_key,
|
||||
const std::string &client_ip)>;
|
||||
|
||||
// Logger
|
||||
class Logger
|
||||
{
|
||||
class Logger {
|
||||
public:
|
||||
static Logger &getInstance()
|
||||
{
|
||||
static Logger &getInstance() {
|
||||
static Logger instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void setLevel(LogLevel level)
|
||||
{
|
||||
current_level = level;
|
||||
}
|
||||
LogLevel getLevel() const
|
||||
{
|
||||
return current_level;
|
||||
}
|
||||
void setLevel(LogLevel level) { current_level = level; }
|
||||
LogLevel getLevel() const { return current_level; }
|
||||
|
||||
void error(const std::string &msg);
|
||||
void warn(const std::string &msg);
|
||||
@@ -393,62 +335,28 @@ namespace rtmp
|
||||
};
|
||||
|
||||
// RTMP Server
|
||||
class RTMPServer
|
||||
{
|
||||
class RTMPServer {
|
||||
public:
|
||||
RTMPServer(int port = 1935);
|
||||
~RTMPServer();
|
||||
|
||||
bool start();
|
||||
void stop();
|
||||
bool isRunning() const
|
||||
{
|
||||
return running;
|
||||
}
|
||||
bool isRunning() const { return running; }
|
||||
|
||||
// Callbacks
|
||||
void setOnConnect(OnConnectCallback cb)
|
||||
{
|
||||
on_connect = cb;
|
||||
}
|
||||
void setOnPublish(OnPublishCallback cb)
|
||||
{
|
||||
on_publish = cb;
|
||||
}
|
||||
void setOnPlay(OnPlayCallback cb)
|
||||
{
|
||||
on_play = cb;
|
||||
}
|
||||
void setOnAudioData(OnAudioDataCallback cb)
|
||||
{
|
||||
on_audio_data = cb;
|
||||
}
|
||||
void setOnVideoData(OnVideoDataCallback cb)
|
||||
{
|
||||
on_video_data = cb;
|
||||
}
|
||||
void setOnMetaData(OnMetaDataCallback cb)
|
||||
{
|
||||
on_metadata = cb;
|
||||
}
|
||||
void setOnDisconnect(OnDisconnectCallback cb)
|
||||
{
|
||||
on_disconnect = cb;
|
||||
}
|
||||
void setAuthCallback(AuthCallback cb)
|
||||
{
|
||||
auth_callback = cb;
|
||||
}
|
||||
void setOnConnect(OnConnectCallback cb) { on_connect = cb; }
|
||||
void setOnPublish(OnPublishCallback cb) { on_publish = cb; }
|
||||
void setOnPlay(OnPlayCallback cb) { on_play = cb; }
|
||||
void setOnAudioData(OnAudioDataCallback cb) { on_audio_data = cb; }
|
||||
void setOnVideoData(OnVideoDataCallback cb) { on_video_data = cb; }
|
||||
void setOnMetaData(OnMetaDataCallback cb) { on_metadata = cb; }
|
||||
void setOnDisconnect(OnDisconnectCallback cb) { on_disconnect = cb; }
|
||||
void setAuthCallback(AuthCallback cb) { auth_callback = cb; }
|
||||
|
||||
// GOP Cache
|
||||
void enableGOPCache(bool enable)
|
||||
{
|
||||
use_gop_cache = enable;
|
||||
}
|
||||
bool isGOPCacheEnabled() const
|
||||
{
|
||||
return use_gop_cache;
|
||||
}
|
||||
void enableGOPCache(bool enable) { use_gop_cache = enable; }
|
||||
bool isGOPCacheEnabled() const { return use_gop_cache; }
|
||||
|
||||
// Recording
|
||||
bool startRecording(const std::string &app, const std::string &stream_key,
|
||||
@@ -459,53 +367,27 @@ namespace rtmp
|
||||
// Statistics
|
||||
StreamStatistics getStreamStats(const std::string &app,
|
||||
const std::string &stream_key) const;
|
||||
std::vector<std::pair<std::string, StreamStatistics>> getAllStreamStats() const;
|
||||
std::vector<std::pair<std::string, StreamStatistics>>
|
||||
getAllStreamStats() const;
|
||||
int getActivePublishers() const;
|
||||
int getActivePlayers() const;
|
||||
int getTotalConnections() const;
|
||||
|
||||
// Connection limits
|
||||
void setMaxPublishersPerStream(int max)
|
||||
{
|
||||
max_publishers_per_stream = max;
|
||||
}
|
||||
void setMaxPlayersPerStream(int max)
|
||||
{
|
||||
max_players_per_stream = max;
|
||||
}
|
||||
void setMaxTotalConnections(int max)
|
||||
{
|
||||
max_total_connections = max;
|
||||
}
|
||||
int getMaxPublishersPerStream() const
|
||||
{
|
||||
return max_publishers_per_stream;
|
||||
}
|
||||
int getMaxPlayersPerStream() const
|
||||
{
|
||||
return max_players_per_stream;
|
||||
}
|
||||
int getMaxTotalConnections() const
|
||||
{
|
||||
return max_total_connections;
|
||||
}
|
||||
void setMaxPublishersPerStream(int max) { max_publishers_per_stream = max; }
|
||||
void setMaxPlayersPerStream(int max) { max_players_per_stream = max; }
|
||||
void setMaxTotalConnections(int max) { max_total_connections = max; }
|
||||
int getMaxPublishersPerStream() const { return max_publishers_per_stream; }
|
||||
int getMaxPlayersPerStream() const { return max_players_per_stream; }
|
||||
int getMaxTotalConnections() const { return max_total_connections; }
|
||||
|
||||
// Ping/Pong
|
||||
void enablePingPong(bool enable, int interval_seconds = 30);
|
||||
bool isPingPongEnabled() const
|
||||
{
|
||||
return ping_enabled;
|
||||
}
|
||||
bool isPingPongEnabled() const { return ping_enabled; }
|
||||
|
||||
// Timeout handling
|
||||
void setConnectionTimeout(int seconds)
|
||||
{
|
||||
connection_timeout = seconds;
|
||||
}
|
||||
int getConnectionTimeout() const
|
||||
{
|
||||
return connection_timeout;
|
||||
}
|
||||
void setConnectionTimeout(int seconds) { connection_timeout = seconds; }
|
||||
int getConnectionTimeout() const { return connection_timeout; }
|
||||
|
||||
// Broadcasting
|
||||
bool sendAudioToPlayers(const std::string &app, const std::string &stream_key,
|
||||
@@ -570,8 +452,7 @@ namespace rtmp
|
||||
void timeoutCheckRoutine();
|
||||
|
||||
std::string makeStreamKey(const std::string &app,
|
||||
const std::string& stream) const
|
||||
{
|
||||
const std::string &stream) const {
|
||||
return app + "/" + stream;
|
||||
}
|
||||
|
||||
@@ -579,7 +460,8 @@ namespace rtmp
|
||||
const std::string &stream_key) const;
|
||||
int countPlayers(const std::string &app, const std::string &stream_key) const;
|
||||
bool checkConnectionLimits(const std::string &app,
|
||||
const std::string& stream_key, bool is_publisher) const;
|
||||
const std::string &stream_key,
|
||||
bool is_publisher) const;
|
||||
};
|
||||
|
||||
// Utility macros
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
#include "rtmp_capi.h"
|
||||
#include "rtmp_server.h"
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include "../include/rtmp_capi.h"
|
||||
#include "../include/rtmp_server.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rtmp_capi_internal
|
||||
{
|
||||
struct RtmpServerImpl
|
||||
{
|
||||
namespace rtmp_capi_internal {
|
||||
struct RtmpServerImpl {
|
||||
rtmp::RTMPServer *server;
|
||||
RtmpOnConnectCallback on_connect_cb;
|
||||
void *on_connect_userdata;
|
||||
@@ -23,213 +20,225 @@ namespace rtmp_capi_internal
|
||||
void *on_disconnect_userdata;
|
||||
RtmpAuthCallback auth_cb;
|
||||
void *auth_userdata;
|
||||
RtmpServerImpl() : server(nullptr), on_connect_cb(nullptr),
|
||||
on_connect_userdata(nullptr),
|
||||
on_publish_cb(nullptr), on_publish_userdata(nullptr), on_play_cb(nullptr),
|
||||
on_play_userdata(nullptr),
|
||||
on_audio_cb(nullptr), on_audio_userdata(nullptr), on_video_cb(nullptr),
|
||||
on_video_userdata(nullptr),
|
||||
on_disconnect_cb(nullptr), on_disconnect_userdata(nullptr), auth_cb(nullptr),
|
||||
RtmpServerImpl()
|
||||
: server(nullptr), on_connect_cb(nullptr), on_connect_userdata(nullptr),
|
||||
on_publish_cb(nullptr), on_publish_userdata(nullptr),
|
||||
on_play_cb(nullptr), on_play_userdata(nullptr), on_audio_cb(nullptr),
|
||||
on_audio_userdata(nullptr), on_video_cb(nullptr),
|
||||
on_video_userdata(nullptr), on_disconnect_cb(nullptr),
|
||||
on_disconnect_userdata(nullptr), auth_cb(nullptr),
|
||||
auth_userdata(nullptr) {}
|
||||
};
|
||||
}
|
||||
} // namespace rtmp_capi_internal
|
||||
|
||||
using Impl = rtmp_capi_internal::RtmpServerImpl;
|
||||
|
||||
extern "C" {
|
||||
RtmpServerHandle rtmp_server_create(int port)
|
||||
{
|
||||
RtmpServerHandle rtmp_server_create(int port) {
|
||||
Impl *impl = new Impl();
|
||||
impl->server = new rtmp::RTMPServer(port);
|
||||
impl->server->setOnConnect([impl](std::shared_ptr<rtmp::RTMPSession> session)
|
||||
{
|
||||
if (!impl || !impl->on_connect_cb) return;
|
||||
impl->server->setOnConnect(
|
||||
[impl](std::shared_ptr<rtmp::RTMPSession> session) {
|
||||
if (!impl || !impl->on_connect_cb)
|
||||
return;
|
||||
const auto &info = session->getStreamInfo();
|
||||
impl->on_connect_cb(info.client_ip.c_str(), impl->on_connect_userdata);
|
||||
});
|
||||
impl->server->setOnPublish([impl](std::shared_ptr<rtmp::RTMPSession> session,
|
||||
const std::string & app, const std::string & stream_key)
|
||||
{
|
||||
if (!impl || !impl->on_publish_cb) return;
|
||||
const std::string &app,
|
||||
const std::string &stream_key) {
|
||||
if (!impl || !impl->on_publish_cb)
|
||||
return;
|
||||
const auto &info = session->getStreamInfo();
|
||||
impl->on_publish_cb(info.client_ip.c_str(), app.c_str(), stream_key.c_str(),
|
||||
impl->on_publish_userdata);
|
||||
});
|
||||
impl->server->setOnPlay([impl](std::shared_ptr<rtmp::RTMPSession> session,
|
||||
const std::string & app, const std::string & stream_key)
|
||||
{
|
||||
if (!impl || !impl->on_play_cb) return;
|
||||
const std::string &app,
|
||||
const std::string &stream_key) {
|
||||
if (!impl || !impl->on_play_cb)
|
||||
return;
|
||||
const auto &info = session->getStreamInfo();
|
||||
impl->on_play_cb(info.client_ip.c_str(), app.c_str(), stream_key.c_str(),
|
||||
impl->on_play_userdata);
|
||||
});
|
||||
impl->server->setOnAudioData([impl](std::shared_ptr<rtmp::RTMPSession> session,
|
||||
const std::vector<uint8_t> &data, uint32_t timestamp)
|
||||
{
|
||||
if (!impl || !impl->on_audio_cb) return;
|
||||
impl->server->setOnAudioData(
|
||||
[impl](std::shared_ptr<rtmp::RTMPSession> session,
|
||||
const std::vector<uint8_t> &data, uint32_t timestamp) {
|
||||
if (!impl || !impl->on_audio_cb)
|
||||
return;
|
||||
const auto &info = session->getStreamInfo();
|
||||
impl->on_audio_cb(info.app.c_str(), info.stream_key.c_str(), data.data(),
|
||||
static_cast<uint32_t>(data.size()), timestamp, impl->on_audio_userdata);
|
||||
impl->on_audio_cb(info.app.c_str(), info.stream_key.c_str(),
|
||||
data.data(), static_cast<uint32_t>(data.size()),
|
||||
timestamp, impl->on_audio_userdata);
|
||||
});
|
||||
impl->server->setOnVideoData([impl](std::shared_ptr<rtmp::RTMPSession> session,
|
||||
const std::vector<uint8_t> &data, uint32_t timestamp)
|
||||
{
|
||||
if (!impl || !impl->on_video_cb) return;
|
||||
impl->server->setOnVideoData(
|
||||
[impl](std::shared_ptr<rtmp::RTMPSession> session,
|
||||
const std::vector<uint8_t> &data, uint32_t timestamp) {
|
||||
if (!impl || !impl->on_video_cb)
|
||||
return;
|
||||
const auto &info = session->getStreamInfo();
|
||||
impl->on_video_cb(info.app.c_str(), info.stream_key.c_str(), data.data(),
|
||||
static_cast<uint32_t>(data.size()), timestamp, impl->on_video_userdata);
|
||||
impl->on_video_cb(info.app.c_str(), info.stream_key.c_str(),
|
||||
data.data(), static_cast<uint32_t>(data.size()),
|
||||
timestamp, impl->on_video_userdata);
|
||||
});
|
||||
impl->server->setOnDisconnect([impl](std::shared_ptr<rtmp::RTMPSession>
|
||||
session)
|
||||
{
|
||||
if (!impl || !impl->on_disconnect_cb) return;
|
||||
impl->server->setOnDisconnect(
|
||||
[impl](std::shared_ptr<rtmp::RTMPSession> session) {
|
||||
if (!impl || !impl->on_disconnect_cb)
|
||||
return;
|
||||
const auto &info = session->getStreamInfo();
|
||||
impl->on_disconnect_cb(info.client_ip.c_str(), info.app.c_str(),
|
||||
info.stream_key.c_str(), info.is_publishing, info.is_playing,
|
||||
impl->on_disconnect_userdata);
|
||||
info.stream_key.c_str(), info.is_publishing,
|
||||
info.is_playing, impl->on_disconnect_userdata);
|
||||
});
|
||||
impl->server->setAuthCallback([impl](const std::string &app,
|
||||
const std::string & stream_key, const std::string & client_ip) -> bool
|
||||
{
|
||||
if (!impl || !impl->auth_cb) return true;
|
||||
return impl->auth_cb(app.c_str(), stream_key.c_str(), client_ip.c_str(), impl->auth_userdata);
|
||||
const std::string &stream_key,
|
||||
const std::string &client_ip) -> bool {
|
||||
if (!impl || !impl->auth_cb)
|
||||
return true;
|
||||
return impl->auth_cb(app.c_str(), stream_key.c_str(), client_ip.c_str(),
|
||||
impl->auth_userdata);
|
||||
});
|
||||
return impl;
|
||||
}
|
||||
// add all other functions as above
|
||||
void rtmp_server_destroy(RtmpServerHandle handle)
|
||||
{
|
||||
if (!handle) return;
|
||||
void rtmp_server_destroy(RtmpServerHandle handle) {
|
||||
if (!handle)
|
||||
return;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
delete impl->server;
|
||||
delete impl;
|
||||
}
|
||||
bool rtmp_server_start(RtmpServerHandle handle)
|
||||
{
|
||||
if (!handle) return false;
|
||||
bool rtmp_server_start(RtmpServerHandle handle) {
|
||||
if (!handle)
|
||||
return false;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
return impl->server->start();
|
||||
}
|
||||
void rtmp_server_stop(RtmpServerHandle handle)
|
||||
{
|
||||
if (!handle) return;
|
||||
void rtmp_server_stop(RtmpServerHandle handle) {
|
||||
if (!handle)
|
||||
return;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
impl->server->stop();
|
||||
}
|
||||
bool rtmp_server_is_running(RtmpServerHandle handle)
|
||||
{
|
||||
if (!handle) return false;
|
||||
bool rtmp_server_is_running(RtmpServerHandle handle) {
|
||||
if (!handle)
|
||||
return false;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
return impl->server->isRunning();
|
||||
}
|
||||
void rtmp_server_set_on_connect(RtmpServerHandle handle,
|
||||
RtmpOnConnectCallback cb, void* user_data)
|
||||
{
|
||||
if (!handle) return;
|
||||
RtmpOnConnectCallback cb, void *user_data) {
|
||||
if (!handle)
|
||||
return;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
impl->on_connect_cb = cb;
|
||||
impl->on_connect_userdata = user_data;
|
||||
}
|
||||
void rtmp_server_set_on_publish(RtmpServerHandle handle,
|
||||
RtmpOnPublishCallback cb, void* user_data)
|
||||
{
|
||||
if (!handle) return;
|
||||
RtmpOnPublishCallback cb, void *user_data) {
|
||||
if (!handle)
|
||||
return;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
impl->on_publish_cb = cb;
|
||||
impl->on_publish_userdata = user_data;
|
||||
}
|
||||
void rtmp_server_set_on_play(RtmpServerHandle handle, RtmpOnPlayCallback cb,
|
||||
void* user_data)
|
||||
{
|
||||
if (!handle) return;
|
||||
void *user_data) {
|
||||
if (!handle)
|
||||
return;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
impl->on_play_cb = cb;
|
||||
impl->on_play_userdata = user_data;
|
||||
}
|
||||
void rtmp_server_set_on_audio_data(RtmpServerHandle handle,
|
||||
RtmpOnAudioDataCallback cb, void* user_data)
|
||||
{
|
||||
if (!handle) return;
|
||||
RtmpOnAudioDataCallback cb,
|
||||
void *user_data) {
|
||||
if (!handle)
|
||||
return;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
impl->on_audio_cb = cb;
|
||||
impl->on_audio_userdata = user_data;
|
||||
}
|
||||
void rtmp_server_set_on_video_data(RtmpServerHandle handle,
|
||||
RtmpOnVideoDataCallback cb, void* user_data)
|
||||
{
|
||||
if (!handle) return;
|
||||
RtmpOnVideoDataCallback cb,
|
||||
void *user_data) {
|
||||
if (!handle)
|
||||
return;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
impl->on_video_cb = cb;
|
||||
impl->on_video_userdata = user_data;
|
||||
}
|
||||
void rtmp_server_set_on_disconnect(RtmpServerHandle handle,
|
||||
RtmpOnDisconnectCallback cb, void* user_data)
|
||||
{
|
||||
if (!handle) return;
|
||||
RtmpOnDisconnectCallback cb,
|
||||
void *user_data) {
|
||||
if (!handle)
|
||||
return;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
impl->on_disconnect_cb = cb;
|
||||
impl->on_disconnect_userdata = user_data;
|
||||
}
|
||||
void rtmp_server_set_auth_callback(RtmpServerHandle handle, RtmpAuthCallback cb,
|
||||
void* user_data)
|
||||
{
|
||||
if (!handle) return;
|
||||
void *user_data) {
|
||||
if (!handle)
|
||||
return;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
impl->auth_cb = cb;
|
||||
impl->auth_userdata = user_data;
|
||||
}
|
||||
void rtmp_server_enable_gop_cache(RtmpServerHandle handle, bool enable)
|
||||
{
|
||||
if (!handle) return;
|
||||
void rtmp_server_enable_gop_cache(RtmpServerHandle handle, bool enable) {
|
||||
if (!handle)
|
||||
return;
|
||||
static_cast<Impl *>(handle)->server->enableGOPCache(enable);
|
||||
}
|
||||
void rtmp_server_set_max_publishers_per_stream(RtmpServerHandle handle,
|
||||
int max)
|
||||
{
|
||||
if (!handle) return;
|
||||
int max) {
|
||||
if (!handle)
|
||||
return;
|
||||
static_cast<Impl *>(handle)->server->setMaxPublishersPerStream(max);
|
||||
}
|
||||
void rtmp_server_set_max_players_per_stream(RtmpServerHandle handle, int max)
|
||||
{
|
||||
if (!handle) return;
|
||||
void rtmp_server_set_max_players_per_stream(RtmpServerHandle handle, int max) {
|
||||
if (!handle)
|
||||
return;
|
||||
static_cast<Impl *>(handle)->server->setMaxPlayersPerStream(max);
|
||||
}
|
||||
void rtmp_server_set_max_total_connections(RtmpServerHandle handle, int max)
|
||||
{
|
||||
if (!handle) return;
|
||||
void rtmp_server_set_max_total_connections(RtmpServerHandle handle, int max) {
|
||||
if (!handle)
|
||||
return;
|
||||
static_cast<Impl *>(handle)->server->setMaxTotalConnections(max);
|
||||
}
|
||||
void rtmp_server_set_connection_timeout(RtmpServerHandle handle, int seconds)
|
||||
{
|
||||
if (!handle) return;
|
||||
void rtmp_server_set_connection_timeout(RtmpServerHandle handle, int seconds) {
|
||||
if (!handle)
|
||||
return;
|
||||
static_cast<Impl *>(handle)->server->setConnectionTimeout(seconds);
|
||||
}
|
||||
void rtmp_server_enable_ping_pong(RtmpServerHandle handle, bool enable,
|
||||
int interval_seconds)
|
||||
{
|
||||
if (!handle) return;
|
||||
int interval_seconds) {
|
||||
if (!handle)
|
||||
return;
|
||||
static_cast<Impl *>(handle)->server->enablePingPong(enable, interval_seconds);
|
||||
}
|
||||
int rtmp_server_get_active_publishers(RtmpServerHandle handle)
|
||||
{
|
||||
if (!handle) return 0;
|
||||
int rtmp_server_get_active_publishers(RtmpServerHandle handle) {
|
||||
if (!handle)
|
||||
return 0;
|
||||
return static_cast<Impl *>(handle)->server->getActivePublishers();
|
||||
}
|
||||
int rtmp_server_get_active_players(RtmpServerHandle handle)
|
||||
{
|
||||
if (!handle) return 0;
|
||||
int rtmp_server_get_active_players(RtmpServerHandle handle) {
|
||||
if (!handle)
|
||||
return 0;
|
||||
return static_cast<Impl *>(handle)->server->getActivePlayers();
|
||||
}
|
||||
int rtmp_server_get_total_connections(RtmpServerHandle handle)
|
||||
{
|
||||
if (!handle) return 0;
|
||||
int rtmp_server_get_total_connections(RtmpServerHandle handle) {
|
||||
if (!handle)
|
||||
return 0;
|
||||
return static_cast<Impl *>(handle)->server->getTotalConnections();
|
||||
}
|
||||
struct RtmpStreamStats rtmp_server_get_stream_stats(RtmpServerHandle handle,
|
||||
const char* app, const char* stream_key)
|
||||
{
|
||||
const char *app,
|
||||
const char *stream_key) {
|
||||
struct RtmpStreamStats stats = {0};
|
||||
if (!handle || !app || !stream_key) return stats;
|
||||
if (!handle || !app || !stream_key)
|
||||
return stats;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
auto cstats = impl->server->getStreamStats(app, stream_key);
|
||||
stats.bytes_sent = cstats.bytes_sent;
|
||||
@@ -242,53 +251,53 @@ extern "C" {
|
||||
return stats;
|
||||
}
|
||||
bool rtmp_server_start_recording(RtmpServerHandle handle, const char *app,
|
||||
const char* stream_key, const char* filename)
|
||||
{
|
||||
if (!handle || !app || !stream_key || !filename) return false;
|
||||
const char *stream_key, const char *filename) {
|
||||
if (!handle || !app || !stream_key || !filename)
|
||||
return false;
|
||||
return static_cast<Impl *>(handle)->server->startRecording(app, stream_key,
|
||||
filename);
|
||||
}
|
||||
void rtmp_server_stop_recording(RtmpServerHandle handle, const char *app,
|
||||
const char* stream_key)
|
||||
{
|
||||
if (!handle || !app || !stream_key) return;
|
||||
const char *stream_key) {
|
||||
if (!handle || !app || !stream_key)
|
||||
return;
|
||||
static_cast<Impl *>(handle)->server->stopRecording(app, stream_key);
|
||||
}
|
||||
bool rtmp_server_is_recording(RtmpServerHandle handle, const char *app,
|
||||
const char* stream_key)
|
||||
{
|
||||
if (!handle || !app || !stream_key) return false;
|
||||
const char *stream_key) {
|
||||
if (!handle || !app || !stream_key)
|
||||
return false;
|
||||
return static_cast<Impl *>(handle)->server->isRecording(app, stream_key);
|
||||
}
|
||||
bool rtmp_server_broadcast_audio(RtmpServerHandle handle, const char *app,
|
||||
const char* stream_key, const uint8_t* data, uint32_t length,
|
||||
uint32_t timestamp)
|
||||
{
|
||||
if (!handle || !app || !stream_key || !data || length == 0) return false;
|
||||
const char *stream_key, const uint8_t *data,
|
||||
uint32_t length, uint32_t timestamp) {
|
||||
if (!handle || !app || !stream_key || !data || length == 0)
|
||||
return false;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
std::vector<uint8_t> vec(data, data + length);
|
||||
return impl->server->sendAudioToPlayers(app, stream_key, vec, timestamp);
|
||||
}
|
||||
bool rtmp_server_broadcast_video(RtmpServerHandle handle, const char *app,
|
||||
const char* stream_key, const uint8_t* data, uint32_t length,
|
||||
uint32_t timestamp)
|
||||
{
|
||||
if (!handle || !app || !stream_key || !data || length == 0) return false;
|
||||
const char *stream_key, const uint8_t *data,
|
||||
uint32_t length, uint32_t timestamp) {
|
||||
if (!handle || !app || !stream_key || !data || length == 0)
|
||||
return false;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
std::vector<uint8_t> vec(data, data + length);
|
||||
return impl->server->sendVideoToPlayers(app, stream_key, vec, timestamp);
|
||||
}
|
||||
bool rtmp_server_broadcast_metadata(RtmpServerHandle handle, const char *app,
|
||||
const char* stream_key, const uint8_t* data, uint32_t length)
|
||||
{
|
||||
if (!handle || !app || !stream_key || !data || length == 0) return false;
|
||||
const char *stream_key, const uint8_t *data,
|
||||
uint32_t length) {
|
||||
if (!handle || !app || !stream_key || !data || length == 0)
|
||||
return false;
|
||||
Impl *impl = static_cast<Impl *>(handle);
|
||||
std::vector<uint8_t> vec(data, data + length);
|
||||
impl->server->sendMetadataToPlayers(app, stream_key, vec);
|
||||
return true;
|
||||
}
|
||||
void rtmp_logger_set_level(RtmpLogLevel level)
|
||||
{
|
||||
void rtmp_logger_set_level(RtmpLogLevel level) {
|
||||
rtmp::Logger::getInstance().setLevel((rtmp::LogLevel)level);
|
||||
}
|
||||
}
|
||||
1004
src/rtmp_server.cpp
1004
src/rtmp_server.cpp
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user