1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Merge branch 'dev-28181' of https://github.com/xialixin/srs into feature/gb28281-2

This commit is contained in:
winlin 2020-03-20 21:39:04 +08:00
commit de9a004ff7
14 changed files with 2776 additions and 5 deletions

View file

@ -275,6 +275,11 @@ bool srs_stream_caster_is_flv(string caster)
return caster == "flv";
}
bool srs_stream_caster_is_gb28181(string caster)
{
return caster == "gb28181";
}
bool srs_config_apply_filter(SrsConfDirective* dvr_apply, SrsRequest* req)
{
static bool DEFAULT = true;
@ -2137,7 +2142,26 @@ srs_error_t SrsConfig::global_to_json(SrsJsonObject* obj)
sobj->set(sdir->name, sdir->dumps_arg0_to_integer());
} else if (sdir->name == "rtp_port_max") {
sobj->set(sdir->name, sdir->dumps_arg0_to_integer());
} else if (sdir->name == "rtp_idle_timeout") {
sobj->set(sdir->name, sdir->dumps_arg0_to_integer());
} else if (sdir->name == "ack_timeout") {
sobj->set(sdir->name, sdir->dumps_arg0_to_integer());
} else if (sdir->name == "keepalive_timeout") {
sobj->set(sdir->name, sdir->dumps_arg0_to_integer());
} else if (sdir->name == "audio_enable") {
sobj->set(sdir->name, sdir->dumps_arg0_to_boolean());
} else if (sdir->name == "host") {
sobj->set(sdir->name, sdir->dumps_arg0_to_str());
} else if (sdir->name == "serial") {
sobj->set(sdir->name, sdir->dumps_arg0_to_str());
} else if (sdir->name == "realm") {
sobj->set(sdir->name, sdir->dumps_arg0_to_str());
} else if (sdir->name == "wait_keyframe") {
sobj->set(sdir->name, sdir->dumps_arg0_to_str());
} else if (sdir->name == "print_sip_message") {
sobj->set(sdir->name, sdir->dumps_arg0_to_str());
}
}
obj->set(dir->name, sobj);
} else {
@ -3650,7 +3674,11 @@ srs_error_t SrsConfig::check_normal_config()
SrsConfDirective* conf = stream_caster->at(i);
string n = conf->name;
if (n != "enabled" && n != "caster" && n != "output"
&& n != "listen" && n != "rtp_port_min" && n != "rtp_port_max") {
&& n != "listen" && n != "rtp_port_min" && n != "rtp_port_max"
&& n != "rtp_idle_timeout" && n != "ack_timeout" && n != "keepalive_timeout"
&& n != "host" && n != "serial" && n != "realm"
&& n != "audio_enable" && n != "wait_keyframe"
&& n != "print_sip_message") {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal stream_caster.%s", n.c_str());
}
}
@ -4266,6 +4294,150 @@ int SrsConfig::get_stream_caster_rtp_port_max(SrsConfDirective* conf)
return ::atoi(conf->arg0().c_str());
}
int SrsConfig::get_stream_caster_gb28181_rtp_ide_timeout(SrsConfDirective* conf)
{
static int DEFAULT = 30;
if (!conf) {
return DEFAULT;
}
conf = conf->get("rtp_ide_timeout");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return ::atoi(conf->arg0().c_str());
}
int SrsConfig::get_stream_caster_gb28181_ack_timeout(SrsConfDirective* conf)
{
static int DEFAULT = 30;
if (!conf) {
return DEFAULT;
}
conf = conf->get("ack_timeout");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return ::atoi(conf->arg0().c_str());
}
int SrsConfig::get_stream_caster_gb28181_keepalive_timeout(SrsConfDirective* conf)
{
static int DEFAULT = 30;
if (!conf) {
return DEFAULT;
}
conf = conf->get("keepalive_timeout");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return ::atoi(conf->arg0().c_str());
}
string SrsConfig::get_stream_caster_gb28181_host(SrsConfDirective* conf)
{
static string DEFAULT = "127.0.0.1";
if (!conf) {
return DEFAULT;
}
conf = conf->get("host");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return conf->arg0();
}
string SrsConfig::get_stream_caster_gb28181_serial(SrsConfDirective* conf)
{
static string DEFAULT = "";
if (!conf) {
return DEFAULT;
}
conf = conf->get("serial");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return conf->arg0();
}
string SrsConfig::get_stream_caster_gb28181_realm(SrsConfDirective* conf)
{
static string DEFAULT = "";
if (!conf) {
return DEFAULT;
}
conf = conf->get("realm");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return conf->arg0();
}
bool SrsConfig::get_stream_caster_gb28181_audio_enable(SrsConfDirective* conf)
{
static bool DEFAULT = true;
if (!conf) {
return DEFAULT;
}
conf = conf->get("audio_enable");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return SRS_CONF_PERFER_FALSE(conf->arg0());
}
bool SrsConfig::get_stream_caster_gb28181_print_sip_message(SrsConfDirective* conf)
{
static bool DEFAULT = false;
if (!conf) {
return DEFAULT;
}
conf = conf->get("print_sip_message");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return SRS_CONF_PERFER_FALSE(conf->arg0());
}
bool SrsConfig::get_stream_caster_gb28181_wait_keyframe(SrsConfDirective* conf)
{
static bool DEFAULT = false;
if (!conf) {
return DEFAULT;
}
conf = conf->get("wait_keyframe");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return SRS_CONF_PERFER_FALSE(conf->arg0());
}
SrsConfDirective* SrsConfig::get_vhost(string vhost, bool try_default_vhost)
{
srs_assert(root);

View file

@ -119,6 +119,7 @@ extern bool srs_config_dvr_is_plan_session(std::string plan);
extern bool srs_stream_caster_is_udp(std::string caster);
extern bool srs_stream_caster_is_rtsp(std::string caster);
extern bool srs_stream_caster_is_flv(std::string caster);
extern bool srs_stream_caster_is_gb28181(std::string caster);
// Whether the dvr_apply active the stream specified by req.
extern bool srs_config_apply_filter(SrsConfDirective* dvr_apply, SrsRequest* req);
@ -498,6 +499,17 @@ public:
virtual int get_stream_caster_rtp_port_min(SrsConfDirective* conf);
// Get the max udp port for rtp of stream caster rtsp.
virtual int get_stream_caster_rtp_port_max(SrsConfDirective* conf);
virtual int get_stream_caster_gb28181_rtp_ide_timeout(SrsConfDirective* conf);
virtual int get_stream_caster_gb28181_ack_timeout(SrsConfDirective* conf);
virtual int get_stream_caster_gb28181_keepalive_timeout(SrsConfDirective* conf);
virtual bool get_stream_caster_gb28181_audio_enable(SrsConfDirective* conf);
virtual std::string get_stream_caster_gb28181_host(SrsConfDirective* conf);
virtual std::string get_stream_caster_gb28181_serial(SrsConfDirective* conf);
virtual std::string get_stream_caster_gb28181_realm(SrsConfDirective* conf);
virtual bool get_stream_caster_gb28181_print_sip_message(SrsConfDirective* conf);
virtual bool get_stream_caster_gb28181_wait_keyframe(SrsConfDirective* conf);
// vhost specified section
public:
// Get the vhost directive by vhost name.

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,330 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2020 Winlin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SRS_APP_GB28181_HPP
#define SRS_APP_GB28181_HPP
#include <srs_core.hpp>
#include <arpa/inet.h>
#include <string>
#include <vector>
#include <map>
#include <srs_app_st.hpp>
#include <srs_app_thread.hpp>
#include <srs_app_listener.hpp>
#include <srs_rtsp_stack.hpp>
#include <srs_kernel_stream.hpp>
#include <srs_app_log.hpp>
#include <srs_kernel_file.hpp>
class SrsStSocket;
class SrsRtpConn;
class SrsRtspConn;
class SrsRtspStack;
class SrsRtspCaster;
class SrsConfDirective;
class SrsRtpPacket;
class SrsRequest;
class SrsStSocket;
class SrsRtmpClient;
class SrsRawH264Stream;
class SrsRawAacStream;
struct SrsRawAacStreamCodec;
class SrsSharedPtrMessage;
class SrsAudioFrame;
class SrsSimpleStream;
class SrsPithyPrint;
class SrsSimpleRtmpClient;
class SrsSipStack;
class SrsGb28181Caster;
class SrsRtspJitter;
class SrsRtspAudioCache;
class SrsSipRequest;
class SrsGb28181Conn;
class SrsGb28281ClientInfo;
/* gb28181 program stream struct define
*/
struct SrsPsPacketStartCode
{
uint8_t start_code[3];
uint8_t stream_id[1];
};
struct SrsPsPacketHeader
{
SrsPsPacketStartCode start;// 4
uint8_t info[9];
uint8_t stuffing_length;
};
struct SrsPsPacketBBHeader
{
SrsPsPacketStartCode start;
uint16_t length;
};
struct SrsPsePacket
{
SrsPsPacketStartCode start;
uint16_t length;
uint8_t info[2];
uint8_t stuffing_length;
};
struct SrsPsMapPacket
{
SrsPsPacketStartCode start;
uint16_t length;
};
class SrsPsRtpPacket: public SrsRtpPacket
{
public:
SrsPsRtpPacket();
virtual ~SrsPsRtpPacket();
public:
virtual srs_error_t decode(SrsBuffer* stream);
};
// A rtp connection which transport a stream.
class SrsPsRtpConn: public ISrsUdpHandler
{
private:
SrsPithyPrint* pprint;
SrsUdpListener* listener;
SrsGb28181Conn* gb28181;
SrsPsRtpPacket* cache;
std::map<uint32_t, SrsSimpleStream*> cache_payload;
std::string session_id;
int _port;
uint32_t pre_timestamp;
SrsFileWriter ps_fw;
SrsFileWriter video_fw;
SrsFileWriter audio_fw;
bool first_keyframe_flag;
bool wait_first_keyframe;
bool audio_enable;
public:
SrsPsRtpConn(SrsGb28181Conn* r, int p, std::string sid, bool a, bool k);
virtual ~SrsPsRtpConn();
private:
int64_t parse_ps_timestamp(const uint8_t* p);
private:
bool can_send_ps_av_packet();
void dispose();
public:
virtual int port();
virtual srs_error_t listen();
// Interface ISrsUdpHandler
public:
virtual srs_error_t on_udp_packet(const sockaddr* from, const int fromlen, char* buf, int nb_buf);
virtual srs_error_t on_ps_stream(char* ps_data, int ps_size, uint32_t timestamp);
};
class SrsGb28281ClientInfo {
public:
SrsGb28281ClientInfo();
virtual ~SrsGb28281ClientInfo();
public:
sockaddr* sock_from;
int sock_fromlen;
srs_netfd_t stfd;
SrsSipRequest *req;
};
enum Srs28181CtrlStatusType{
Srs28181Unkonw = 0,
Srs28181RegisterOk = 1,
Srs28181AliveOk = 2,
Srs28181InviteOk = 3,
Srs28181Trying = 4,
Srs28181Bye = 5,
};
class SrsGb28181Conn : public ISrsCoroutineHandler, public ISrsConnection
{
private:
std::string output_template;
SrsPithyPrint* pprint;
public:
Srs28181CtrlStatusType register_status;
Srs28181CtrlStatusType alive_status;
Srs28181CtrlStatusType invite_status;
srs_utime_t register_time;
srs_utime_t alive_time;
srs_utime_t invite_time;
srs_utime_t recv_rtp_time;
std::string rtmp_url;
int reg_expires;
private:
std::string session_id;
// video stream.
int video_id;
std::string video_codec;
SrsPsRtpConn* video_rtp;
// audio stream.
int audio_id;
std::string audio_codec;
int audio_sample_rate;
int audio_channel;
SrsPsRtpConn* audio_rtp;
public:
SrsGb28281ClientInfo* info;
private:
SrsStSocket* skt;
SrsSipStack* sip;
SrsGb28181Caster* caster;
SrsCoroutine* trd;
private:
SrsSipRequest* req;
SrsSimpleRtmpClient* sdk;
SrsRtspJitter* vjitter;
SrsRtspJitter* ajitter;
private:
SrsRawH264Stream* avc;
std::string h264_sps;
std::string h264_pps;
bool h264_sps_changed;
bool h264_pps_changed;
bool h264_sps_pps_sent;
private:
SrsRawAacStream* aac;
std::string aac_specific_config;
public:
SrsGb28181Conn(SrsGb28181Caster* c, std::string id);
virtual ~SrsGb28181Conn();
public:
virtual srs_error_t serve();
virtual std::string remote_ip();
virtual void set_request_info(SrsSipRequest *req);
virtual std::string get_session_id();
private:
virtual srs_error_t do_cycle();
// internal methods
public:
virtual srs_error_t start_rtp_listen(int port);
virtual srs_error_t stop_rtp_listen();
// Interface ISrsOneCycleThreadHandler
public:
virtual srs_error_t cycle();
public:
virtual srs_error_t on_rtp_video(SrsSimpleStream* stream, int64_t dts, int keyframe);
virtual srs_error_t on_rtp_audio(SrsSimpleStream* stream, int64_t dts);
private:
virtual srs_error_t write_h264_sps_pps(uint32_t dts, uint32_t pts);
virtual srs_error_t write_h264_ipb_frame(char* frame, int frame_size, uint32_t dts, uint32_t pts);
virtual srs_error_t write_audio_raw_frame(char* frame, int frame_size, SrsRawAacStreamCodec* codec, uint32_t dts);
virtual srs_error_t rtmp_write_packet(char type, uint32_t timestamp, char* data, int size);
private:
// Connect to RTMP server.
virtual srs_error_t connect();
// Close the connection to RTMP server.
virtual void close();
};
class SrsGb28181Config
{
public:
std::string sip_host;
std::string sip_port;
std::string sip_serial;
std::string sip_realm;
int sip_ack_timeout;
int sip_keepalive_timeout;
int rtp_idle_timeout;
bool audio_enable;
std::string output;
int rtp_port_min;
int rtp_port_max;
int listen_port;
bool print_sip_message;
bool wait_keyframe;
public:
SrsGb28181Config(SrsConfDirective* c);
virtual ~SrsGb28181Config();
};
//gb28181 conn manager
class SrsGb28181Caster : public ISrsUdpHandler
{
private:
SrsGb28181Config *config;
// The key: port, value: whether used.
std::map<int, bool> used_ports;
SrsSipStack *sip;
srs_netfd_t lfd;
private:
std::map<std::string, SrsGb28181Conn*> clients;
SrsCoroutineManager* manager;
public:
SrsGb28181Caster(SrsConfDirective* c);
virtual ~SrsGb28181Caster();
private:
srs_error_t fetch_or_create(SrsSipRequest* r, SrsGb28181Conn** gb28181);
virtual SrsGb28181Conn* fetch(const SrsSipRequest* r);
virtual void destroy();
public:
// Alloc a rtp port from local ports pool.
// @param pport output the rtp port.
virtual srs_error_t alloc_port(int* pport);
// Free the alloced rtp port.
virtual void free_port(int lpmin, int lpmax);
virtual srs_error_t initialize();
virtual void set_stfd(srs_netfd_t fd);
virtual SrsGb28181Config GetGb28181Config();
// Interface ISrsUdpHandler
public:
virtual srs_error_t on_udp_packet(const sockaddr* from, const int fromlen, char* buf, int nb_buf);
private:
virtual srs_error_t on_udp_bytes(std::string host, int port, char* buf, int nb_buf, sockaddr* from, int fromlen);
// internal methods.
public:
virtual srs_error_t send_message(sockaddr* f, int l, std::stringstream& ss);
virtual srs_error_t send_bye(SrsSipRequest *req, sockaddr *f, int l);
virtual srs_error_t send_ack(SrsSipRequest *req, sockaddr *f, int l);
virtual srs_error_t send_invite(SrsSipRequest *req, sockaddr *f, int l, int port);
virtual srs_error_t send_status(SrsSipRequest *req, sockaddr *f, int l);
virtual void remove(SrsGb28181Conn* conn);
};
#endif

View file

@ -59,6 +59,13 @@ srs_error_t ISrsUdpHandler::on_stfd_change(srs_netfd_t /*fd*/)
return srs_success;
}
void ISrsUdpHandler::set_stfd(srs_netfd_t /*fd*/)
{
}
ISrsTcpHandler::ISrsTcpHandler()
{
}
@ -104,6 +111,8 @@ srs_error_t SrsUdpListener::listen()
if ((err = srs_udp_listen(ip, port, &lfd)) != srs_success) {
return srs_error_wrap(err, "listen %s:%d", ip.c_str(), port);
}
handler->set_stfd(lfd);
srs_freep(trd);
trd = new SrsSTCoroutine("udp", this);
@ -206,4 +215,3 @@ srs_error_t SrsTcpListener::cycle()
return err;
}

View file

@ -43,6 +43,8 @@ public:
// When fd changed, for instance, reload the listen port,
// notify the handler and user can do something.
virtual srs_error_t on_stfd_change(srs_netfd_t fd);
virtual void set_stfd(srs_netfd_t fd);
public:
// When udp listener got a udp packet, notice server to process it.
// @param type, the client type, used to create concrete connection,

View file

@ -52,6 +52,7 @@ using namespace std;
#include <srs_kernel_consts.hpp>
#include <srs_app_thread.hpp>
#include <srs_app_coworkers.hpp>
#include <srs_app_gb28181.hpp>
// system interval in srs_utime_t,
// all resolution times should be times togother,
@ -109,6 +110,8 @@ std::string srs_listener_type2string(SrsListenerType type)
return "RTSP";
case SrsListenerFlv:
return "HTTP-FLV";
case SrsListenerGb28181:
return "GB28181-SIP over UDP";
default:
return "UNKONWN";
}
@ -301,7 +304,7 @@ srs_error_t SrsUdpStreamListener::listen(string i, int p)
// the caller already ensure the type is ok,
// we just assert here for unknown stream caster.
srs_assert(type == SrsListenerMpegTsOverUdp);
srs_assert(type == SrsListenerMpegTsOverUdp || type == SrsListenerGb28181);
ip = i;
port = p;
@ -339,6 +342,22 @@ SrsUdpCasterListener::~SrsUdpCasterListener()
srs_freep(caster);
}
SrsGb28181Listener::SrsGb28181Listener(SrsServer* svr, SrsListenerType t, SrsConfDirective* c) : SrsUdpStreamListener(svr, t, NULL)
{
// the caller already ensure the type is ok,
// we just assert here for unknown stream caster.
srs_assert(type == SrsListenerGb28181);
if (type == SrsListenerGb28181) {
caster = new SrsGb28181Caster(c);
}
}
SrsGb28181Listener::~SrsGb28181Listener()
{
srs_freep(caster);
}
SrsSignalManager* SrsSignalManager::instance = NULL;
SrsSignalManager::SrsSignalManager(SrsServer* s)
@ -1325,6 +1344,8 @@ srs_error_t SrsServer::listen_stream_caster()
listener = new SrsRtspListener(this, SrsListenerRtsp, stream_caster);
} else if (srs_stream_caster_is_flv(caster)) {
listener = new SrsHttpFlvListener(this, SrsListenerFlv, stream_caster);
} else if (srs_stream_caster_is_gb28181(caster)) {
listener = new SrsGb28181Listener(this, SrsListenerGb28181, stream_caster);
} else {
return srs_error_new(ERROR_STREAM_CASTER_ENGINE, "invalid caster %s", caster.c_str());
}

View file

@ -69,6 +69,8 @@ enum SrsListenerType
SrsListenerRtsp = 4,
// TCP stream, FLV stream over HTTP.
SrsListenerFlv = 5,
// UDP stream, gb28181 stream
SrsListenerGb28181 = 6,
};
// A common tcp listener, for RTMP/HTTP server.
@ -156,6 +158,14 @@ public:
virtual ~SrsUdpCasterListener();
};
// A UDP sip listener, for sip server.
class SrsGb28181Listener : public SrsUdpStreamListener
{
public:
SrsGb28181Listener(SrsServer* svr, SrsListenerType t, SrsConfDirective* c);
virtual ~SrsGb28181Listener();
};
// Convert signal to io,
// @see: st-1.9/docs/notes.html
class SrsSignalManager : public ISrsCoroutineHandler