PassengerStatistics/Main/RtspServer.cpp

63 lines
1.7 KiB
C++
Raw Normal View History

2024-03-13 18:01:36 +08:00
#include "RtspServer.h"
#include <chrono>
#include <cstring>
#include <mk_common.h>
#include <mk_h264_splitter.h>
#include <mk_media.h>
#include <thread>
constexpr auto iniConfig = R"(
[rtsp]
lowLatency=1
)";
class RtspServerPrivate {
public:
mk_media media;
mk_h264_splitter splitter;
};
static void on_h264_frame(void *user_data, mk_h264_splitter splitter, const char *data, int size) {
using namespace std::chrono_literals;
std::this_thread::sleep_for(40ms);
static int dts = 0;
mk_frame frame = mk_frame_create(MKCodecH264, dts, dts, data, size, NULL, NULL);
dts += 40;
mk_media_input_frame((mk_media)user_data, frame);
mk_frame_unref(frame);
}
RtspServer::RtspServer() : m_d(new RtspServerPrivate()) {
mk_config config;
std::memset(&config, 0, sizeof(mk_config));
config.ini = iniConfig;
config.ini_is_path = 0;
config.log_mask = LOG_CONSOLE;
config.ssl_is_path = 1;
mk_env_init(&config);
mk_rtsp_server_start(554, 0);
m_d->media = mk_media_create("__defaultVhost__", "live", "video", 0, 0, 0);
codec_args v_args = {0};
mk_track v_track = mk_track_create(MKCodecH264, &v_args);
mk_media_init_track(m_d->media, v_track);
mk_media_init_complete(m_d->media);
mk_track_unref(v_track);
m_d->splitter = mk_h264_splitter_create(on_h264_frame, m_d->media, 0);
}
RtspServer::~RtspServer() {
mk_h264_splitter_release(m_d->splitter);
mk_media_release(m_d->media);
mk_stop_all_server();
if (m_d != nullptr) {
delete m_d;
}
}
void RtspServer::push(const uint8_t *data, uint32_t size) {
mk_h264_splitter_input_data(m_d->splitter, reinterpret_cast<const char *>(data), size);
}