mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Merge branch 'feature/gb28181' into develop
This commit is contained in:
commit
dabad68f48
22 changed files with 2783 additions and 373 deletions
|
@ -2195,8 +2195,34 @@ srs_error_t SrsConfig::global_to_json(SrsJsonObject* obj)
|
|||
sobj->set(sdir->name, sdir->dumps_arg0_to_str());
|
||||
} else if (sdir->name == "auto_create_channel") {
|
||||
sobj->set(sdir->name, sdir->dumps_arg0_to_str());
|
||||
}
|
||||
}
|
||||
} else if (sdir->name == "sip"){
|
||||
SrsJsonObject* ssobj = SrsJsonAny::object();
|
||||
sobj->set(sdir->name, ssobj);
|
||||
|
||||
for (int j = 0; j < (int)sdir->directives.size(); j++) {
|
||||
SrsConfDirective* ssdir = sdir->directives.at(j);
|
||||
if (ssdir->name == "enabled") {
|
||||
ssobj->set(ssdir->name, ssdir->dumps_arg0_to_boolean());
|
||||
} else if (ssdir->name == "listen") {
|
||||
ssobj->set(ssdir->name, ssdir->dumps_arg0_to_integer());
|
||||
} else if (ssdir->name == "serial") {
|
||||
ssobj->set(ssdir->name, ssdir->dumps_arg0_to_str());
|
||||
} else if (ssdir->name == "realm") {
|
||||
ssobj->set(ssdir->name, ssdir->dumps_arg0_to_str());
|
||||
} else if (ssdir->name == "ack_timeout") {
|
||||
ssobj->set(ssdir->name, ssdir->dumps_arg0_to_integer());
|
||||
} else if (ssdir->name == "keepalive_timeout") {
|
||||
ssobj->set(ssdir->name, ssdir->dumps_arg0_to_integer());
|
||||
} else if (ssdir->name == "auto_play") {
|
||||
ssobj->set(ssdir->name, ssdir->dumps_arg0_to_boolean());
|
||||
} else if (ssdir->name == "invite_port_fixed") {
|
||||
ssobj->set(ssdir->name, ssdir->dumps_arg0_to_boolean());
|
||||
} else if (ssdir->name == "query_catalog_interval") {
|
||||
ssobj->set(ssdir->name, ssdir->dumps_arg0_to_integer());
|
||||
}
|
||||
}
|
||||
}//end if
|
||||
}//end for
|
||||
obj->set(dir->name, sobj);
|
||||
} else {
|
||||
continue;
|
||||
|
@ -3730,7 +3756,8 @@ srs_error_t SrsConfig::check_normal_config()
|
|||
for (int j = 0; j < (int)conf->directives.size(); j++) {
|
||||
string m = conf->at(j)->name;
|
||||
if (m != "enabled" && m != "listen" && m != "ack_timeout" && m != "keepalive_timeout"
|
||||
&& m != "host" && m != "serial" && m != "realm" && m != "auto_play" && m != "invite_port_fixed") {
|
||||
&& m != "host" && m != "serial" && m != "realm" && m != "auto_play" && m != "invite_port_fixed"
|
||||
&& m != "query_catalog_interval") {
|
||||
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal stream_caster.%s", m.c_str());
|
||||
}
|
||||
}
|
||||
|
@ -4630,6 +4657,28 @@ bool SrsConfig::get_stream_caster_gb28181_auto_create_channel(SrsConfDirective*
|
|||
return SRS_CONF_PERFER_FALSE(conf->arg0());
|
||||
}
|
||||
|
||||
srs_utime_t SrsConfig::get_stream_caster_gb28181_sip_query_catalog_interval(SrsConfDirective* conf)
|
||||
{
|
||||
static srs_utime_t DEFAULT = 60 * SRS_UTIME_SECONDS;
|
||||
|
||||
|
||||
if (!conf) {
|
||||
return DEFAULT;
|
||||
}
|
||||
|
||||
conf = conf->get("sip");
|
||||
if (!conf) {
|
||||
return DEFAULT;
|
||||
}
|
||||
|
||||
conf = conf->get("query_catalog_interval");
|
||||
if (!conf || conf->arg0().empty()) {
|
||||
return DEFAULT;
|
||||
}
|
||||
|
||||
return (srs_utime_t)(::atoi(conf->arg0().c_str()) * SRS_UTIME_SECONDS);
|
||||
}
|
||||
|
||||
int SrsConfig::get_rtc_server_enabled()
|
||||
{
|
||||
SrsConfDirective* conf = root->get("rtc_server");
|
||||
|
|
|
@ -517,6 +517,7 @@ public:
|
|||
virtual int get_stream_caster_gb28181_sip_listen(SrsConfDirective* conf);
|
||||
virtual bool get_stream_caster_gb28181_sip_invite_port_fixed(SrsConfDirective* conf);
|
||||
virtual bool get_stream_caster_gb28181_auto_create_channel(SrsConfDirective* conf);
|
||||
virtual srs_utime_t get_stream_caster_gb28181_sip_query_catalog_interval(SrsConfDirective* conf);
|
||||
|
||||
// rtc section
|
||||
public:
|
||||
|
|
|
@ -268,7 +268,7 @@ srs_error_t SrsGb28181PsRtpProcessor::on_udp_packet(const sockaddr* from, const
|
|||
}
|
||||
|
||||
if (pprint->can_print()) {
|
||||
srs_trace("<- " SRS_CONSTS_LOG_STREAM_CASTER " gb28181: client_id %s, peer(%s, %d) ps rtp packet %dB, age=%d, vt=%d/%u, sts=%u/%u/%#x, paylod=%dB",
|
||||
srs_trace("<- " SRS_CONSTS_LOG_GB28181_CASTER " gb28181: client_id %s, peer(%s, %d) ps rtp packet %dB, age=%d, vt=%d/%u, sts=%u/%u/%#x, paylod=%dB",
|
||||
channel_id.c_str(), address_string, peer_port, nb_buf, pprint->age(), pkt.version,
|
||||
pkt.payload_type, pkt.sequence_number, pkt.timestamp, pkt.ssrc,
|
||||
pkt.payload->length()
|
||||
|
@ -312,7 +312,12 @@ srs_error_t SrsGb28181PsRtpProcessor::on_udp_packet(const sockaddr* from, const
|
|||
channel.set_channel_id(tmp_id);
|
||||
channel.set_port_mode(RTP_PORT_MODE_FIXED);
|
||||
channel.set_ssrc(pkt.ssrc);
|
||||
_srs_gb28181->create_stream_channel(&channel);
|
||||
|
||||
srs_error_t err2 = srs_success;
|
||||
if ((err2 = _srs_gb28181->create_stream_channel(&channel)) != srs_success){
|
||||
srs_warn("gb28181: RtpProcessor create stream channel error %s", srs_error_desc(err2).c_str());
|
||||
srs_error_reset(err2);
|
||||
};
|
||||
|
||||
muxer = _srs_gb28181->fetch_rtmpmuxer(tmp_id);
|
||||
}
|
||||
|
@ -324,7 +329,7 @@ srs_error_t SrsGb28181PsRtpProcessor::on_udp_packet(const sockaddr* from, const
|
|||
muxer->set_channel_peer_ip(address_string);
|
||||
//not the first peer port's non processing
|
||||
if (muxer->channel_peer_port() != peer_port){
|
||||
srs_warn("<- " SRS_CONSTS_LOG_STREAM_CASTER " gb28181: client_id %s, ssrc=%#x, first peer_port=%d cur peer_port=%d",
|
||||
srs_warn("<- " SRS_CONSTS_LOG_GB28181_CASTER " gb28181: client_id %s, ssrc=%#x, first peer_port=%d cur peer_port=%d",
|
||||
muxer->get_channel_id().c_str(), pkt.ssrc, muxer->channel_peer_port(), peer_port);
|
||||
srs_freep(key->second);
|
||||
}else {
|
||||
|
@ -357,6 +362,7 @@ SrsPsStreamDemixer::SrsPsStreamDemixer(ISrsPsStreamHander *h, std::string id, bo
|
|||
audio_enable = a;
|
||||
wait_first_keyframe = k;
|
||||
channel_id = id;
|
||||
first_keyframe_flag = false;
|
||||
}
|
||||
|
||||
SrsPsStreamDemixer::~SrsPsStreamDemixer()
|
||||
|
@ -404,7 +410,6 @@ int64_t SrsPsStreamDemixer::parse_ps_timestamp(const uint8_t* p)
|
|||
srs_error_t SrsPsStreamDemixer::on_ps_stream(char* ps_data, int ps_size, uint32_t timestamp, uint32_t ssrc)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
int complete_len = 0;
|
||||
int incomplete_len = ps_size;
|
||||
char *next_ps_pack = ps_data;
|
||||
|
@ -589,8 +594,10 @@ srs_error_t SrsPsStreamDemixer::on_ps_stream(char* ps_data, int ps_size, uint32_
|
|||
//ts=1000 seq=4 mark=true payload= audio
|
||||
incomplete_len = ps_size - complete_len;
|
||||
complete_len = complete_len + incomplete_len;
|
||||
|
||||
}
|
||||
|
||||
first_keyframe_flag = false;
|
||||
srs_trace("gb28181: client_id %s, unkonw ps data (%#x/%u) %02x %02x %02x %02x\n",
|
||||
channel_id.c_str(), ssrc, timestamp,
|
||||
next_ps_pack[0], next_ps_pack[1], next_ps_pack[2], next_ps_pack[3]);
|
||||
|
@ -599,7 +606,7 @@ srs_error_t SrsPsStreamDemixer::on_ps_stream(char* ps_data, int ps_size, uint32_
|
|||
}
|
||||
|
||||
if (complete_len != ps_size){
|
||||
srs_trace("gb28181: client_id %s decode ps packet error (%#x/%u)! ps_size=%d complete=%d \n",
|
||||
srs_trace("gb28181: client_id %s decode ps packet error (%#x/%u)! ps_size=%d complete=%d \n",
|
||||
channel_id.c_str(), ssrc, timestamp, ps_size, complete_len);
|
||||
}else if (hander && video_stream.length() && can_send_ps_av_packet()) {
|
||||
if ((err = hander->on_rtp_video(&video_stream, video_pts)) != srs_success) {
|
||||
|
@ -646,6 +653,7 @@ SrsGb28181Config::SrsGb28181Config(SrsConfDirective* c)
|
|||
sip_ack_timeout = _srs_config->get_stream_caster_gb28181_ack_timeout(c);
|
||||
sip_keepalive_timeout = _srs_config->get_stream_caster_gb28181_keepalive_timeout(c);
|
||||
sip_invite_port_fixed = _srs_config->get_stream_caster_gb28181_sip_invite_port_fixed(c);
|
||||
sip_query_catalog_interval = _srs_config->get_stream_caster_gb28181_sip_query_catalog_interval(c);
|
||||
}
|
||||
|
||||
SrsGb28181Config::~SrsGb28181Config()
|
||||
|
@ -653,7 +661,6 @@ SrsGb28181Config::~SrsGb28181Config()
|
|||
|
||||
}
|
||||
|
||||
|
||||
//SrsGb28181RtmpMuxer gb28181 rtmp muxer, process ps stream to rtmp
|
||||
SrsGb28181RtmpMuxer::SrsGb28181RtmpMuxer(SrsGb28181Manger* c, std::string id, bool a, bool k)
|
||||
{
|
||||
|
@ -675,7 +682,8 @@ SrsGb28181RtmpMuxer::SrsGb28181RtmpMuxer(SrsGb28181Manger* c, std::string id, bo
|
|||
wait_ps_queue = srs_cond_new();
|
||||
|
||||
stream_idle_timeout = -1;
|
||||
recv_stream_time = 0;
|
||||
recv_rtp_stream_time = 0;
|
||||
send_rtmp_stream_time = 0;
|
||||
|
||||
_rtmp_url = "";
|
||||
|
||||
|
@ -768,7 +776,7 @@ std::string SrsGb28181RtmpMuxer::rtmp_url()
|
|||
|
||||
srs_utime_t SrsGb28181RtmpMuxer::get_recv_stream_time()
|
||||
{
|
||||
return recv_stream_time;
|
||||
return recv_rtp_stream_time;
|
||||
}
|
||||
|
||||
|
||||
|
@ -785,7 +793,8 @@ void SrsGb28181RtmpMuxer::destroy()
|
|||
srs_error_t SrsGb28181RtmpMuxer::do_cycle()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
recv_stream_time = srs_get_system_time();
|
||||
recv_rtp_stream_time = srs_get_system_time();
|
||||
send_rtmp_stream_time = srs_get_system_time();
|
||||
|
||||
//consume ps stream, and check status
|
||||
while (true) {
|
||||
|
@ -819,7 +828,7 @@ srs_error_t SrsGb28181RtmpMuxer::do_cycle()
|
|||
}
|
||||
|
||||
srs_utime_t now = srs_get_system_time();
|
||||
srs_utime_t duration = now - recv_stream_time;
|
||||
srs_utime_t duration = now - recv_rtp_stream_time;
|
||||
|
||||
//if no RTP data is received within 2 seconds,
|
||||
//the peer-port and peer-ip will be cleared and
|
||||
|
@ -831,13 +840,28 @@ srs_error_t SrsGb28181RtmpMuxer::do_cycle()
|
|||
channel->set_rtp_peer_port(0);
|
||||
channel->set_rtp_peer_ip("");
|
||||
}
|
||||
|
||||
|
||||
SrsGb28181Config config = gb28181_manger->get_gb28181_config();
|
||||
if (duration > config.rtp_idle_timeout){
|
||||
srs_trace("gb28181: client id=%s, stream idle timeout, stop!!!", channel_id.c_str());
|
||||
break;
|
||||
}
|
||||
|
||||
//RTMP connection is about to timeout without receiving any data.,
|
||||
//waiting for the next time there is data automatically connected
|
||||
//it is related to the following two parameter settings of the rtmp server
|
||||
//the publish 1st packet timeout in srs_utime_t
|
||||
//publish_1stpkt_timeout default 20000ms
|
||||
//the publish normal packet timeout in srs_utime_t
|
||||
//publish_normal_timeout default 5000ms
|
||||
duration = now - send_rtmp_stream_time;
|
||||
bool will_timeout = duration > (5 * SRS_UTIME_SECONDS);
|
||||
if (will_timeout && sdk){
|
||||
srs_warn("gb28181: client id=%s RTMP connection is about to time out without receiving any data",
|
||||
channel_id.c_str());
|
||||
rtmp_close();
|
||||
}
|
||||
|
||||
if (ps_queue.empty()){
|
||||
srs_cond_timedwait(wait_ps_queue, 200 * SRS_UTIME_MILLISECONDS);
|
||||
}else {
|
||||
|
@ -862,7 +886,7 @@ void SrsGb28181RtmpMuxer::ps_packet_enqueue(SrsPsRtpPacket *pkt)
|
|||
{
|
||||
srs_assert(pkt);
|
||||
|
||||
recv_stream_time = srs_get_system_time();
|
||||
recv_rtp_stream_time = srs_get_system_time();
|
||||
|
||||
//prevent consumers from being unable to process data
|
||||
//and accumulating in the queue
|
||||
|
@ -923,7 +947,7 @@ srs_error_t SrsGb28181RtmpMuxer::on_rtp_video(SrsSimpleStream *stream, int64_t f
|
|||
uint32_t pts = (uint32_t)(fpts / 90);
|
||||
srs_info("gb28181rtmpmuxer: on_rtp_video dts=%u", dts);
|
||||
|
||||
recv_stream_time = srs_get_system_time();
|
||||
|
||||
|
||||
SrsBuffer *avs = new SrsBuffer(stream->bytes(), stream->length());
|
||||
SrsAutoFree(SrsBuffer, avs);
|
||||
|
@ -1010,8 +1034,6 @@ srs_error_t SrsGb28181RtmpMuxer::on_rtp_audio(SrsSimpleStream* stream, int64_t f
|
|||
return srs_error_wrap(err, "jitter");
|
||||
}
|
||||
|
||||
recv_stream_time = srs_get_system_time();
|
||||
|
||||
uint32_t dts = (uint32_t)(fdts / 90);
|
||||
|
||||
// send each frame.
|
||||
|
@ -1167,6 +1189,8 @@ srs_error_t SrsGb28181RtmpMuxer::rtmp_write_packet(char type, uint32_t timestamp
|
|||
|
||||
SrsSharedPtrMessage* msg = NULL;
|
||||
|
||||
send_rtmp_stream_time = srs_get_system_time();
|
||||
|
||||
if ((err = srs_rtmp_create_msg(type, timestamp, data, size, sdk->sid(), &msg)) != srs_success) {
|
||||
return srs_error_wrap(err, "create message");
|
||||
}
|
||||
|
@ -1197,8 +1221,9 @@ srs_error_t SrsGb28181RtmpMuxer::connect()
|
|||
srs_utime_t cto = SRS_CONSTS_RTMP_TIMEOUT;
|
||||
srs_utime_t sto = SRS_CONSTS_RTMP_PULSE;
|
||||
sdk = new SrsSimpleRtmpClient(url, cto, sto);
|
||||
|
||||
|
||||
srs_trace("gb28181: rtmp connect url=%s", url.c_str());
|
||||
|
||||
if ((err = sdk->connect()) != srs_success) {
|
||||
close();
|
||||
return srs_error_wrap(err, "connect %s failed, cto=%dms, sto=%dms.", url.c_str(), srsu2msi(cto), srsu2msi(sto));
|
||||
|
@ -1368,8 +1393,8 @@ uint32_t SrsGb28181Manger::generate_ssrc(std::string id)
|
|||
{
|
||||
srand(uint(time(0)));
|
||||
// TODO: SSRC rules can be customized,
|
||||
//gb28281 live ssrc max value 0999999999(3B9AC9FF)
|
||||
//gb28281 vod ssrc max value 1999999999(773593FF)
|
||||
//gb28181 live ssrc max value 0999999999(3B9AC9FF)
|
||||
//gb28181 vod ssrc max value 1999999999(773593FF)
|
||||
uint8_t index = uint8_t(rand() % (0x0F - 0x01 + 1) + 0x01);
|
||||
uint32_t ssrc = 0x2FFFF00 & (hash_code(id) << 8) | index;
|
||||
//uint32_t ssrc = 0x00FFFFFF & (hash_code(id));
|
||||
|
@ -1527,8 +1552,9 @@ void SrsGb28181Manger::stop_rtp_listen(std::string id)
|
|||
}
|
||||
|
||||
//api
|
||||
uint32_t SrsGb28181Manger::create_stream_channel(SrsGb28181StreamChannel *channel)
|
||||
srs_error_t SrsGb28181Manger::create_stream_channel(SrsGb28181StreamChannel *channel)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
srs_assert(channel);
|
||||
|
||||
std::string id = channel->get_channel_id();
|
||||
|
@ -1539,15 +1565,14 @@ uint32_t SrsGb28181Manger::create_stream_channel(SrsGb28181StreamChannel *channe
|
|||
SrsGb28181StreamChannel s = muxer->get_channel();
|
||||
channel->copy(&s);
|
||||
//return ERROR_GB28181_SESSION_IS_EXIST;
|
||||
return ERROR_SUCCESS;
|
||||
return err;
|
||||
}
|
||||
|
||||
//create on rtmp muxer, gb28181 stream to rtmp
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if ((err = fetch_or_create_rtmpmuxer(id, &muxer)) != srs_success){
|
||||
srs_warn("gb28181: create rtmp muxer error, %s", srs_error_desc(err).c_str());
|
||||
srs_freep(err);
|
||||
return ERROR_GB28181_CREATER_RTMPMUXER_FAILED;
|
||||
return err;
|
||||
}
|
||||
|
||||
//Start RTP listening port, receive gb28181 stream,
|
||||
|
@ -1564,21 +1589,19 @@ uint32_t SrsGb28181Manger::create_stream_channel(SrsGb28181StreamChannel *channe
|
|||
if (port_mode == RTP_PORT_MODE_RANDOM){
|
||||
alloc_port(&rtp_port);
|
||||
if (rtp_port <= 0){
|
||||
return ERROR_GB28181_RTP_PORT_FULL;
|
||||
return srs_error_new(ERROR_GB28181_RTP_PORT_FULL, "gb28181: rtp port full");
|
||||
}
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if ((err = start_ps_rtp_listen(id, rtp_port)) != srs_success){
|
||||
srs_warn("gb28181: start ps rtp listen error, %s", srs_error_desc(err).c_str());
|
||||
srs_freep(err);
|
||||
free_port(rtp_port, rtp_port + 1);
|
||||
return ERROR_GB28181_CREATER_RTMPMUXER_FAILED;
|
||||
return err;
|
||||
}
|
||||
}
|
||||
else if(port_mode == RTP_PORT_MODE_FIXED) {
|
||||
rtp_port = config->rtp_mux_port;
|
||||
}
|
||||
else{
|
||||
return ERROR_GB28181_PORT_MODE_INVALID;
|
||||
return srs_error_new(ERROR_GB28181_PORT_MODE_INVALID, "gb28181: port mode invalid");
|
||||
}
|
||||
|
||||
uint32_t ssrc = channel->get_ssrc();
|
||||
|
@ -1653,32 +1676,36 @@ uint32_t SrsGb28181Manger::create_stream_channel(SrsGb28181StreamChannel *channe
|
|||
|
||||
muxer->copy_channel(channel);
|
||||
|
||||
return ERROR_SUCCESS;
|
||||
return err;
|
||||
}
|
||||
|
||||
uint32_t SrsGb28181Manger::delete_stream_channel(std::string id)
|
||||
srs_error_t SrsGb28181Manger::delete_stream_channel(std::string id)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
//notify the device to stop streaming
|
||||
//if an internal sip service controlled channel
|
||||
notify_sip_bye(id);
|
||||
notify_sip_bye(id, id);
|
||||
|
||||
SrsGb28181RtmpMuxer *muxer = fetch_rtmpmuxer(id);
|
||||
if (muxer){
|
||||
stop_rtp_listen(id);
|
||||
muxer->stop();
|
||||
return ERROR_SUCCESS;
|
||||
return err;
|
||||
}else {
|
||||
return ERROR_GB28181_SESSION_IS_NOTEXIST;
|
||||
return srs_error_new(ERROR_GB28181_SESSION_IS_NOTEXIST, "stream channel is not exists");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint32_t SrsGb28181Manger::queue_stream_channel(std::string id, SrsJsonArray* arr)
|
||||
srs_error_t SrsGb28181Manger::query_stream_channel(std::string id, SrsJsonArray* arr)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if (!id.empty()){
|
||||
SrsGb28181RtmpMuxer *muxer = fetch_rtmpmuxer(id);
|
||||
if (!muxer){
|
||||
return ERROR_GB28181_SESSION_IS_NOTEXIST;
|
||||
return srs_error_new(ERROR_GB28181_SESSION_IS_NOTEXIST, "stream channel not exists");
|
||||
}
|
||||
SrsJsonObject* obj = SrsJsonAny::object();
|
||||
arr->append(obj);
|
||||
|
@ -1693,27 +1720,30 @@ uint32_t SrsGb28181Manger::queue_stream_channel(std::string id, SrsJsonArray* ar
|
|||
}
|
||||
}
|
||||
|
||||
return ERROR_SUCCESS;
|
||||
return err;
|
||||
}
|
||||
|
||||
uint32_t SrsGb28181Manger::notify_sip_invite(std::string id, std::string ip, int port, uint32_t ssrc)
|
||||
srs_error_t SrsGb28181Manger::notify_sip_invite(std::string id, std::string ip, int port, uint32_t ssrc, std::string chid)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if (!sip_service){
|
||||
return ERROR_GB28181_SIP_NOT_RUN;
|
||||
return srs_error_new(ERROR_GB28181_SIP_NOT_RUN, "sip not run");
|
||||
}
|
||||
|
||||
//if RTMP Muxer does not exist, you need to create
|
||||
SrsGb28181RtmpMuxer *muxer = fetch_rtmpmuxer(id);
|
||||
std::string key = id+"@"+chid;
|
||||
SrsGb28181RtmpMuxer *muxer = fetch_rtmpmuxer(key);
|
||||
|
||||
if (!muxer){
|
||||
//if there is an invalid parameter, the channel will be created automatically
|
||||
if (ip.empty() || port == 0 || ssrc == 0){
|
||||
//channel not exist
|
||||
SrsGb28181StreamChannel channel;
|
||||
channel.set_channel_id(id);
|
||||
int code = create_stream_channel(&channel);
|
||||
if (code != ERROR_SUCCESS){
|
||||
return code;
|
||||
channel.set_channel_id(key);
|
||||
err = create_stream_channel(&channel);
|
||||
if (err != srs_success){
|
||||
return err;
|
||||
}
|
||||
|
||||
ip = channel.get_ip();
|
||||
|
@ -1730,30 +1760,36 @@ uint32_t SrsGb28181Manger::notify_sip_invite(std::string id, std::string ip, int
|
|||
|
||||
SrsSipRequest req;
|
||||
req.sip_auth_id = id;
|
||||
return sip_service->send_invite(&req, ip, port, ssrc);
|
||||
|
||||
return sip_service->send_invite(&req, ip, port, ssrc, chid);
|
||||
}
|
||||
|
||||
uint32_t SrsGb28181Manger::notify_sip_bye(std::string id)
|
||||
srs_error_t SrsGb28181Manger::notify_sip_bye(std::string id, std::string chid)
|
||||
{
|
||||
if (!sip_service){
|
||||
return ERROR_GB28181_SIP_NOT_RUN;
|
||||
}
|
||||
|
||||
SrsGb28181RtmpMuxer *muxer = fetch_rtmpmuxer(id);
|
||||
if (muxer){
|
||||
muxer->rtmp_close();
|
||||
return srs_error_new(ERROR_GB28181_SIP_NOT_RUN, "sip not run");
|
||||
}
|
||||
|
||||
SrsSipRequest req;
|
||||
req.sip_auth_id = id;
|
||||
return sip_service->send_bye(&req);
|
||||
return sip_service->send_bye(&req, chid);
|
||||
}
|
||||
|
||||
uint32_t SrsGb28181Manger::notify_sip_raw_data(std::string id, std::string data)
|
||||
srs_error_t SrsGb28181Manger::notify_sip_ptz(std::string id, std::string chid, std::string cmd,
|
||||
uint8_t speed, int priority)
|
||||
{
|
||||
if (!sip_service){
|
||||
return ERROR_GB28181_SIP_NOT_RUN;
|
||||
return srs_error_new(ERROR_GB28181_SIP_NOT_RUN, "sip not run");
|
||||
}
|
||||
|
||||
SrsSipRequest req;
|
||||
req.sip_auth_id = id;
|
||||
return sip_service->send_ptz(&req, chid, cmd, speed, priority);
|
||||
}
|
||||
|
||||
srs_error_t SrsGb28181Manger::notify_sip_raw_data(std::string id, std::string data)
|
||||
{
|
||||
if (!sip_service){
|
||||
return srs_error_new(ERROR_GB28181_SIP_NOT_RUN, "sip not run");
|
||||
}
|
||||
|
||||
SrsSipRequest req;
|
||||
|
@ -1762,13 +1798,31 @@ uint32_t SrsGb28181Manger::notify_sip_raw_data(std::string id, std::string data)
|
|||
|
||||
}
|
||||
|
||||
uint32_t SrsGb28181Manger::notify_sip_unregister(std::string id)
|
||||
srs_error_t SrsGb28181Manger::notify_sip_unregister(std::string id)
|
||||
{
|
||||
if (!sip_service){
|
||||
return ERROR_GB28181_SIP_NOT_RUN;
|
||||
return srs_error_new(ERROR_GB28181_SIP_NOT_RUN, "sip not run");
|
||||
}
|
||||
sip_service->remove_session(id);
|
||||
return delete_stream_channel(id);
|
||||
}
|
||||
|
||||
srs_error_t SrsGb28181Manger::notify_sip_query_catalog(std::string id)
|
||||
{
|
||||
if (!sip_service){
|
||||
return srs_error_new(ERROR_GB28181_SIP_NOT_RUN, "sip not run");
|
||||
}
|
||||
|
||||
delete_stream_channel(id);
|
||||
sip_service->remove_session(id);
|
||||
return ERROR_SUCCESS;
|
||||
SrsSipRequest req;
|
||||
req.sip_auth_id = id;
|
||||
return sip_service->send_query_catalog(&req);
|
||||
}
|
||||
|
||||
srs_error_t SrsGb28181Manger::query_sip_session(std::string id, SrsJsonArray* arr)
|
||||
{
|
||||
if (!sip_service){
|
||||
return srs_error_new(ERROR_GB28181_SIP_NOT_RUN, "sip not run");
|
||||
}
|
||||
|
||||
return sip_service->query_sip_session(id, arr);
|
||||
}
|
|
@ -218,7 +218,8 @@ private:
|
|||
SrsPithyPrint* pprint;
|
||||
SrsGb28181StreamChannel *channel;
|
||||
int stream_idle_timeout;
|
||||
srs_utime_t recv_stream_time;
|
||||
srs_utime_t recv_rtp_stream_time;
|
||||
srs_utime_t send_rtmp_stream_time;
|
||||
private:
|
||||
std::string channel_id;
|
||||
std::string _rtmp_url;
|
||||
|
@ -313,6 +314,7 @@ public:
|
|||
srs_utime_t sip_keepalive_timeout;
|
||||
bool sip_auto_play;
|
||||
bool sip_invite_port_fixed;
|
||||
srs_utime_t sip_query_catalog_interval;
|
||||
|
||||
public:
|
||||
SrsGb28181Config(SrsConfDirective* c);
|
||||
|
@ -392,9 +394,7 @@ private:
|
|||
std::map<uint32_t, SrsGb28181RtmpMuxer*> rtmpmuxers_ssrc;
|
||||
std::map<std::string, SrsGb28181RtmpMuxer*> rtmpmuxers;
|
||||
SrsCoroutineManager* manager;
|
||||
|
||||
SrsGb28181SipService* sip_service;
|
||||
|
||||
public:
|
||||
SrsGb28181Manger(SrsConfDirective* c);
|
||||
virtual ~SrsGb28181Manger();
|
||||
|
@ -413,14 +413,17 @@ public:
|
|||
|
||||
public:
|
||||
//stream channel api
|
||||
uint32_t create_stream_channel(SrsGb28181StreamChannel *channel);
|
||||
uint32_t delete_stream_channel(std::string id);
|
||||
uint32_t queue_stream_channel(std::string id, SrsJsonArray* arr);
|
||||
srs_error_t create_stream_channel(SrsGb28181StreamChannel *channel);
|
||||
srs_error_t delete_stream_channel(std::string id);
|
||||
srs_error_t query_stream_channel(std::string id, SrsJsonArray* arr);
|
||||
//sip api
|
||||
uint32_t notify_sip_invite(std::string id, std::string ip, int port, uint32_t ssrc);
|
||||
uint32_t notify_sip_bye(std::string id);
|
||||
uint32_t notify_sip_raw_data(std::string id, std::string data);
|
||||
uint32_t notify_sip_unregister(std::string id);
|
||||
srs_error_t notify_sip_invite(std::string id, std::string ip, int port, uint32_t ssrc, std::string chid);
|
||||
srs_error_t notify_sip_bye(std::string id, std::string chid);
|
||||
srs_error_t notify_sip_raw_data(std::string id, std::string data);
|
||||
srs_error_t notify_sip_unregister(std::string id);
|
||||
srs_error_t notify_sip_query_catalog(std::string id);
|
||||
srs_error_t notify_sip_ptz(std::string id, std::string chid, std::string cmd, uint8_t speed, int priority);
|
||||
srs_error_t query_sip_session(std::string id, SrsJsonArray* arr);
|
||||
|
||||
private:
|
||||
void destroy();
|
||||
|
|
|
@ -65,6 +65,18 @@ std::string srs_get_sip_session_status_str(SrsGb28181SipSessionStatusType status
|
|||
}
|
||||
}
|
||||
|
||||
SrsGb28181Device::SrsGb28181Device()
|
||||
{
|
||||
device_id = "";
|
||||
invite_status = SrsGb28181SipSessionUnkonw;
|
||||
invite_time = 0;
|
||||
device_status = "";
|
||||
|
||||
}
|
||||
|
||||
SrsGb28181Device::~SrsGb28181Device()
|
||||
{}
|
||||
|
||||
SrsGb28181SipSession::SrsGb28181SipSession(SrsGb28181SipService *c, SrsSipRequest* r)
|
||||
{
|
||||
servcie = c;
|
||||
|
@ -82,15 +94,19 @@ SrsGb28181SipSession::SrsGb28181SipSession(SrsGb28181SipService *c, SrsSipReques
|
|||
_register_time = 0;
|
||||
_alive_time = 0;
|
||||
_invite_time = 0;
|
||||
_query_catalog_time = 0;
|
||||
|
||||
_peer_ip = "";
|
||||
_peer_port = 0;
|
||||
|
||||
_fromlen = 0;
|
||||
_sip_cseq = 100;
|
||||
}
|
||||
|
||||
SrsGb28181SipSession::~SrsGb28181SipSession()
|
||||
{
|
||||
destroy();
|
||||
|
||||
srs_freep(req);
|
||||
srs_freep(trd);
|
||||
srs_freep(pprint);
|
||||
|
@ -107,12 +123,25 @@ srs_error_t SrsGb28181SipSession::serve()
|
|||
return err;
|
||||
}
|
||||
|
||||
void SrsGb28181SipSession::destroy()
|
||||
{
|
||||
//destory all device
|
||||
std::map<std::string, SrsGb28181Device*>::iterator it;
|
||||
for (it = _device_list.begin(); it != _device_list.end(); ++it) {
|
||||
srs_freep(it->second);
|
||||
}
|
||||
|
||||
_device_list.clear();
|
||||
}
|
||||
|
||||
srs_error_t SrsGb28181SipSession::do_cycle()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
_register_time = srs_get_system_time();
|
||||
_alive_time = srs_get_system_time();
|
||||
_invite_time = srs_get_system_time();
|
||||
//call it immediately after alive ok;
|
||||
_query_catalog_time = 0;
|
||||
|
||||
while (true) {
|
||||
|
||||
|
@ -121,14 +150,82 @@ srs_error_t SrsGb28181SipSession::do_cycle()
|
|||
if ((err = trd->pull()) != srs_success) {
|
||||
return srs_error_wrap(err, "gb28181 sip session cycle");
|
||||
}
|
||||
|
||||
|
||||
SrsGb28181Config *config = servcie->get_config();
|
||||
srs_utime_t now = srs_get_system_time();
|
||||
srs_utime_t reg_duration = now - _register_time;
|
||||
srs_utime_t alive_duration = now - _alive_time;
|
||||
srs_utime_t invite_duration = now - _invite_time;
|
||||
SrsGb28181Config *config = servcie->get_config();
|
||||
srs_utime_t query_duration = now - _query_catalog_time;
|
||||
|
||||
//send invite, play client av
|
||||
//start ps rtp listen, recv ps stream
|
||||
if (_register_status == SrsGb28181SipSessionRegisterOk &&
|
||||
_alive_status == SrsGb28181SipSessionAliveOk)
|
||||
{
|
||||
std::map<std::string, SrsGb28181Device*>::iterator it;
|
||||
for (it = _device_list.begin(); it != _device_list.end(); it++) {
|
||||
SrsGb28181Device *device = it->second;
|
||||
std::string chid = it->first;
|
||||
|
||||
//update device invite time
|
||||
srs_utime_t invite_duration = 0;
|
||||
if (device->invite_time != 0){
|
||||
invite_duration = srs_get_system_time() - device->invite_time;
|
||||
}
|
||||
|
||||
//It is possible that the camera head keeps pushing and opening,
|
||||
//and the duration will be very large. It will take 1 day to update
|
||||
if (invite_duration > 24 * SRS_UTIME_HOURS){
|
||||
device->invite_time = srs_get_system_time();
|
||||
}
|
||||
|
||||
if (device->invite_status == SrsGb28181SipSessionTrying &&
|
||||
invite_duration > config->sip_ack_timeout){
|
||||
device->invite_status = SrsGb28181SipSessionUnkonw;
|
||||
}
|
||||
|
||||
if (!config->sip_auto_play) continue;
|
||||
|
||||
//offline or already invite device does not need to send invite
|
||||
if (device->device_status != "ON" ||
|
||||
device->invite_status != SrsGb28181SipSessionUnkonw) continue;
|
||||
|
||||
SrsGb28181StreamChannel ch;
|
||||
|
||||
ch.set_channel_id(_session_id + "@" + chid);
|
||||
ch.set_ip(config->host);
|
||||
|
||||
if (config->sip_invite_port_fixed){
|
||||
ch.set_port_mode(RTP_PORT_MODE_FIXED);
|
||||
}else {
|
||||
ch.set_port_mode(RTP_PORT_MODE_RANDOM);
|
||||
}
|
||||
|
||||
//create stream channel, ready for recv device av stream
|
||||
srs_error_t err = _srs_gb28181->create_stream_channel(&ch);
|
||||
|
||||
if ((err = _srs_gb28181->create_stream_channel(&ch)) == srs_success){
|
||||
SrsSipRequest req;
|
||||
req.sip_auth_id = _session_id;
|
||||
|
||||
//send invite to device, req push av stream
|
||||
err = servcie->send_invite(&req, ch.get_ip(),
|
||||
ch.get_rtp_port(), ch.get_ssrc(), chid);
|
||||
}
|
||||
|
||||
int code = srs_error_code(err);
|
||||
if (err != srs_success){
|
||||
srs_error_reset(err);
|
||||
}
|
||||
|
||||
//the same device can't be sent too fast. the device can't handle it
|
||||
srs_usleep(1*SRS_UTIME_SECONDS);
|
||||
|
||||
srs_trace("gb28181: %s clients device=%s send invite code=%d",
|
||||
_session_id.c_str(), chid.c_str(), code);
|
||||
}//end for (it)
|
||||
}//end if (config)
|
||||
|
||||
|
||||
if (_register_status == SrsGb28181SipSessionRegisterOk &&
|
||||
reg_duration > _reg_expires){
|
||||
srs_trace("gb28181: sip session=%s register expire", _session_id.c_str());
|
||||
|
@ -142,29 +239,48 @@ srs_error_t SrsGb28181SipSession::do_cycle()
|
|||
break;
|
||||
}
|
||||
|
||||
if (_invite_status == SrsGb28181SipSessionTrying &&
|
||||
invite_duration > config->sip_ack_timeout){
|
||||
_invite_status == SrsGb28181SipSessionUnkonw;
|
||||
//query device channel
|
||||
if (_alive_status == SrsGb28181SipSessionAliveOk &&
|
||||
query_duration >= config->sip_query_catalog_interval) {
|
||||
SrsSipRequest req;
|
||||
req.sip_auth_id = _session_id;
|
||||
_query_catalog_time = srs_get_system_time();
|
||||
|
||||
srs_error_t err = servcie->send_query_catalog(&req);
|
||||
if (err != srs_success){
|
||||
srs_trace("gb28181: sip query catalog error %s",srs_error_desc(err).c_str());
|
||||
srs_error_reset(err);
|
||||
}
|
||||
|
||||
if (pprint->can_print()){
|
||||
srs_trace("gb28181: sip session=%s peer(%s, %d) status(%s,%s,%s) duration(%u,%u,%u)",
|
||||
//print device status
|
||||
srs_trace("gb28181: sip session=%s peer(%s, %d) status(%s,%s) duration(%u,%u)",
|
||||
_session_id.c_str(), _peer_ip.c_str(), _peer_port,
|
||||
srs_get_sip_session_status_str(_register_status).c_str(),
|
||||
srs_get_sip_session_status_str(_alive_status).c_str(),
|
||||
srs_get_sip_session_status_str(_invite_status).c_str(),
|
||||
(reg_duration / SRS_UTIME_SECONDS),
|
||||
(alive_duration / SRS_UTIME_SECONDS),
|
||||
(invite_duration / SRS_UTIME_SECONDS));
|
||||
(alive_duration / SRS_UTIME_SECONDS));
|
||||
|
||||
std::map<std::string, SrsGb28181Device*>::iterator it;
|
||||
for (it = _device_list.begin(); it != _device_list.end(); it++) {
|
||||
SrsGb28181Device *device = it->second;
|
||||
std::string chid = it->first;
|
||||
|
||||
//It is possible that the camera head keeps pushing and opening,
|
||||
//and the duration will be very large. It will take 1 day to update
|
||||
if (invite_duration > 24 * SRS_UTIME_HOURS){
|
||||
_invite_time = srs_get_system_time();
|
||||
srs_utime_t invite_duration = srs_get_system_time() - device->invite_time;
|
||||
|
||||
if (device->invite_status != SrsGb28181SipSessionTrying &&
|
||||
device->invite_status != SrsGb28181SipSessionInviteOk){
|
||||
invite_duration = 0;
|
||||
}
|
||||
|
||||
srs_trace("gb28181: sip session=%s device=%s status(%s, %s), duration(%u)",
|
||||
_session_id.c_str(), chid.c_str(), device->device_status.c_str(),
|
||||
srs_get_sip_session_status_str(device->invite_status).c_str(),
|
||||
(invite_duration / SRS_UTIME_SECONDS));
|
||||
}
|
||||
}
|
||||
srs_usleep(5* SRS_UTIME_SECONDS);
|
||||
}
|
||||
|
||||
srs_usleep(1 * SRS_UTIME_SECONDS);
|
||||
}//end while
|
||||
|
||||
return err;
|
||||
}
|
||||
|
@ -191,6 +307,72 @@ srs_error_t SrsGb28181SipSession::cycle()
|
|||
return err;
|
||||
}
|
||||
|
||||
void SrsGb28181SipSession::update_device_list(std::map<std::string, std::string> lst)
|
||||
{
|
||||
std::map<std::string, std::string>::iterator it;
|
||||
for (it = lst.begin(); it != lst.end(); ++it) {
|
||||
std::string id = it->first;
|
||||
std::string status = it->second;
|
||||
|
||||
if (_device_list.find(id) == _device_list.end()){
|
||||
SrsGb28181Device *device = new SrsGb28181Device();
|
||||
device->device_id = id;
|
||||
device->device_status = status;
|
||||
device->invite_status = SrsGb28181SipSessionUnkonw;
|
||||
device->invite_time = 0;
|
||||
_device_list[id] = device;
|
||||
|
||||
}else {
|
||||
SrsGb28181Device *device = _device_list[id];
|
||||
device->device_status = status;
|
||||
}
|
||||
|
||||
// srs_trace("gb28181: sip session %s, deviceid=%s status=(%s,%s)",
|
||||
// _session_id.c_str(), id.c_str(), status.c_str(),
|
||||
// srs_get_sip_session_status_str(device.invite_status).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
SrsGb28181Device* SrsGb28181SipSession::get_device_info(std::string chid)
|
||||
{
|
||||
if (_device_list.find(chid) != _device_list.end()){
|
||||
return _device_list[chid];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void SrsGb28181SipSession::dumps(SrsJsonObject* obj)
|
||||
{
|
||||
obj->set("id", SrsJsonAny::str(_session_id.c_str()));
|
||||
obj->set("device_sumnum", SrsJsonAny::integer(_device_list.size()));
|
||||
|
||||
SrsJsonArray* arr = SrsJsonAny::array();
|
||||
obj->set("devices", arr);
|
||||
std::map<std::string, SrsGb28181Device*>::iterator it;
|
||||
for (it = _device_list.begin(); it != _device_list.end(); ++it) {
|
||||
SrsGb28181Device *device = it->second;
|
||||
SrsJsonObject* obj = SrsJsonAny::object();
|
||||
arr->append(obj);
|
||||
obj->set("device_id", SrsJsonAny::str(device->device_id.c_str()));
|
||||
obj->set("device_status", SrsJsonAny::str(device->device_status.c_str()));
|
||||
obj->set("invite_status", SrsJsonAny::str(srs_get_sip_session_status_str(device->invite_status).c_str()));
|
||||
obj->set("invite_time", SrsJsonAny::integer(device->invite_time/SRS_UTIME_SECONDS));
|
||||
}
|
||||
|
||||
//obj->set("rtmp_port", SrsJsonAny::integer(rtmp_port));
|
||||
// obj->set("app", SrsJsonAny::str(app.c_str()));
|
||||
// obj->set("stream", SrsJsonAny::str(stream.c_str()));
|
||||
// obj->set("rtmp_url", SrsJsonAny::str(rtmp_url.c_str()));
|
||||
|
||||
// obj->set("ssrc", SrsJsonAny::integer(ssrc));
|
||||
// obj->set("rtp_port", SrsJsonAny::integer(rtp_port));
|
||||
// obj->set("port_mode", SrsJsonAny::str(port_mode.c_str()));
|
||||
// obj->set("rtp_peer_port", SrsJsonAny::integer(rtp_peer_port));
|
||||
// obj->set("rtp_peer_ip", SrsJsonAny::str(rtp_peer_ip.c_str()));
|
||||
// obj->set("recv_time", SrsJsonAny::integer(recv_time/SRS_UTIME_SECONDS));
|
||||
// obj->set("recv_time_str", SrsJsonAny::str(recv_time_str.c_str()));
|
||||
}
|
||||
|
||||
//gb28181 sip Service
|
||||
SrsGb28181SipService::SrsGb28181SipService(SrsConfDirective* c)
|
||||
{
|
||||
|
@ -232,8 +414,9 @@ srs_error_t SrsGb28181SipService::on_udp_packet(const sockaddr* from, const int
|
|||
}
|
||||
std::string peer_ip = std::string(address_string);
|
||||
int peer_port = atoi(port_string);
|
||||
|
||||
srs_error_t err = on_udp_sip(peer_ip, peer_port, buf, nb_buf, (sockaddr*)from, fromlen);
|
||||
|
||||
std::string recv_msg(buf, nb_buf);
|
||||
srs_error_t err = on_udp_sip(peer_ip, peer_port, recv_msg, (sockaddr*)from, fromlen);
|
||||
if (err != srs_success) {
|
||||
return srs_error_wrap(err, "process udp");
|
||||
}
|
||||
|
@ -241,20 +424,23 @@ srs_error_t SrsGb28181SipService::on_udp_packet(const sockaddr* from, const int
|
|||
}
|
||||
|
||||
srs_error_t SrsGb28181SipService::on_udp_sip(string peer_ip, int peer_port,
|
||||
char* buf, int nb_buf, sockaddr* from, const int fromlen)
|
||||
std::string recv_msg, sockaddr* from, const int fromlen)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
srs_info("gb28181: request peer(%s, %d) nbbuf=%d", peer_ip.c_str(), peer_port, nb_buf);
|
||||
srs_info("gb28181: request recv message=%s", buf);
|
||||
int recv_len = recv_msg.size();
|
||||
char* recv_data = (char*)recv_msg.c_str();
|
||||
|
||||
srs_info("gb28181: request peer(%s, %d) nbbuf=%d", peer_ip.c_str(), peer_port, recv_len);
|
||||
srs_info("gb28181: request recv message=%s", recv_data);
|
||||
|
||||
if (nb_buf < 10) {
|
||||
if (recv_len < 10) {
|
||||
return err;
|
||||
}
|
||||
|
||||
SrsSipRequest* req = NULL;
|
||||
|
||||
if ((err = sip->parse_request(&req, buf, nb_buf)) != srs_success) {
|
||||
if ((err = sip->parse_request(&req, recv_data, recv_len)) != srs_success) {
|
||||
return srs_error_wrap(err, "parse sip request");
|
||||
}
|
||||
|
||||
|
@ -266,6 +452,10 @@ srs_error_t SrsGb28181SipService::on_udp_sip(string peer_ip, int peer_port,
|
|||
|
||||
if (req->is_register()) {
|
||||
std::vector<std::string> serial = srs_string_split(srs_string_replace(req->uri,"sip:", ""), "@");
|
||||
if (serial.empty()){
|
||||
return srs_error_new(ERROR_GB28181_SIP_PRASE_FAILED, "register string split");
|
||||
}
|
||||
|
||||
if (serial.at(0) != config->sip_serial){
|
||||
srs_warn("gb28181: client:%s request serial and server serial inconformity(%s:%s)",
|
||||
req->sip_auth_id.c_str(), serial.at(0).c_str(), config->sip_serial.c_str());
|
||||
|
@ -273,7 +463,7 @@ srs_error_t SrsGb28181SipService::on_udp_sip(string peer_ip, int peer_port,
|
|||
}
|
||||
|
||||
srs_trace("gb28181: request client id=%s peer(%s, %d)", req->sip_auth_id.c_str(), peer_ip.c_str(), peer_port);
|
||||
srs_trace("gb28181: request %s method=%s, uri=%s, version=%s expires=%d",
|
||||
srs_trace("gb28181: %s method=%s, uri=%s, version=%s expires=%d",
|
||||
req->get_cmdtype_str().c_str(), req->method.c_str(),
|
||||
req->uri.c_str(), req->version.c_str(), req->expires);
|
||||
|
||||
|
@ -283,7 +473,8 @@ srs_error_t SrsGb28181SipService::on_udp_sip(string peer_ip, int peer_port,
|
|||
return err;
|
||||
}
|
||||
srs_assert(sip_session);
|
||||
|
||||
sip_session->set_request(req);
|
||||
|
||||
send_status(req, from, fromlen);
|
||||
sip_session->set_register_status(SrsGb28181SipSessionRegisterOk);
|
||||
sip_session->set_register_time(srs_get_system_time());
|
||||
|
@ -292,71 +483,50 @@ srs_error_t SrsGb28181SipService::on_udp_sip(string peer_ip, int peer_port,
|
|||
sip_session->set_sockaddr_len(fromlen);
|
||||
sip_session->set_peer_ip(peer_ip);
|
||||
sip_session->set_peer_port(peer_port);
|
||||
|
||||
}else if (req->is_message()) {
|
||||
SrsGb28181SipSession* sip_session = fetch(session_id);
|
||||
|
||||
if (!sip_session){
|
||||
sip_session = fetch_session_by_callid(req->call_id);
|
||||
}
|
||||
|
||||
if (!sip_session || sip_session->register_status() == SrsGb28181SipSessionUnkonw){
|
||||
srs_trace("gb28181: %s client not registered", req->sip_auth_id.c_str());
|
||||
return err;
|
||||
}
|
||||
|
||||
//reponse status
|
||||
send_status(req, from, fromlen);
|
||||
//sip_session->set_register_status(SrsGb28181SipSessionRegisterOk);
|
||||
//sip_session->set_register_time(srs_get_system_time());
|
||||
sip_session->set_alive_status(SrsGb28181SipSessionAliveOk);
|
||||
sip_session->set_alive_time(srs_get_system_time());
|
||||
sip_session->set_sockaddr((sockaddr)*from);
|
||||
sip_session->set_sockaddr_len(fromlen);
|
||||
sip_session->set_peer_port(peer_port);
|
||||
sip_session->set_peer_ip(peer_ip);
|
||||
|
||||
//send invite, play client av
|
||||
//start ps rtp listen, recv ps stream
|
||||
if (config->sip_auto_play && sip_session->register_status() == SrsGb28181SipSessionRegisterOk &&
|
||||
sip_session->alive_status() == SrsGb28181SipSessionAliveOk &&
|
||||
sip_session->invite_status() == SrsGb28181SipSessionUnkonw)
|
||||
{
|
||||
srs_trace("gb28181: request client id=%s, peer(%s, %d)", req->sip_auth_id.c_str(),
|
||||
peer_ip.c_str(), peer_port);
|
||||
srs_trace("gb28181: request %s method=%s, uri=%s, version=%s ", req->get_cmdtype_str().c_str(),
|
||||
req->method.c_str(), req->uri.c_str(), req->version.c_str());
|
||||
|
||||
SrsGb28181StreamChannel ch;
|
||||
ch.set_channel_id(session_id);
|
||||
ch.set_ip(config->host);
|
||||
if (config->sip_invite_port_fixed){
|
||||
ch.set_port_mode(RTP_PORT_MODE_FIXED);
|
||||
}else {
|
||||
ch.set_port_mode(RTP_PORT_MODE_RANDOM);
|
||||
if (req->cmdtype == SrsSipCmdRequest){
|
||||
send_status(req, from, fromlen);
|
||||
sip_session->set_alive_status(SrsGb28181SipSessionAliveOk);
|
||||
sip_session->set_alive_time(srs_get_system_time());
|
||||
sip_session->set_sockaddr((sockaddr)*from);
|
||||
sip_session->set_sockaddr_len(fromlen);
|
||||
sip_session->set_peer_port(peer_port);
|
||||
sip_session->set_peer_ip(peer_ip);
|
||||
|
||||
//update device list
|
||||
if (req->device_list_map.size() > 0){
|
||||
sip_session->update_device_list(req->device_list_map);
|
||||
}
|
||||
|
||||
int code = _srs_gb28181->create_stream_channel(&ch);
|
||||
if (code == ERROR_SUCCESS){
|
||||
code = send_invite(req, ch.get_ip(),
|
||||
ch.get_rtp_port(), ch.get_ssrc());
|
||||
}
|
||||
|
||||
if (code == ERROR_SUCCESS){
|
||||
sip_session->set_invite_status(SrsGb28181SipSessionTrying);
|
||||
sip_session->set_invite_time(srs_get_system_time());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}else if (req->is_invite()) {
|
||||
SrsGb28181SipSession* sip_session = fetch(session_id);
|
||||
SrsGb28181SipSession* sip_session = fetch_session_by_callid(req->call_id);
|
||||
|
||||
srs_trace("gb28181: request client id=%s, peer(%s, %d)", req->sip_auth_id.c_str(),
|
||||
peer_ip.c_str(), peer_port);
|
||||
srs_trace("gb28181: request %s method=%s, uri=%s, version=%s ",
|
||||
srs_trace("gb28181: %s method=%s, uri=%s, version=%s ",
|
||||
req->get_cmdtype_str().c_str(), req->method.c_str(), req->uri.c_str(), req->version.c_str());
|
||||
|
||||
if (!sip_session){
|
||||
srs_trace("gb28181: %s client not registered", req->sip_auth_id.c_str());
|
||||
srs_trace("gb28181: call_id %s not map %s client ", req->call_id.c_str(), req->sip_auth_id.c_str());
|
||||
return err;
|
||||
}
|
||||
|
||||
sip_session->set_sockaddr((sockaddr)*from);
|
||||
sip_session->set_sockaddr_len(fromlen);
|
||||
// sip_session->set_sockaddr((sockaddr)*from);
|
||||
// sip_session->set_sockaddr_len(fromlen);
|
||||
|
||||
if (sip_session->register_status() == SrsGb28181SipSessionUnkonw ||
|
||||
sip_session->alive_status() == SrsGb28181SipSessionUnkonw) {
|
||||
|
@ -364,37 +534,72 @@ srs_error_t SrsGb28181SipService::on_udp_sip(string peer_ip, int peer_port,
|
|||
return err;
|
||||
}
|
||||
|
||||
if (req->cmdtype == SrsSipCmdRespone && req->status == "200") {
|
||||
if (req->cmdtype == SrsSipCmdRespone){
|
||||
srs_trace("gb28181: INVITE response %s client status=%s", req->sip_auth_id.c_str(), req->status.c_str());
|
||||
send_ack(req, from, fromlen);
|
||||
sip_session->set_invite_status(SrsGb28181SipSessionInviteOk);
|
||||
sip_session->set_invite_time(srs_get_system_time());
|
||||
//Record tag and branch, which are required by the 'bye' command,
|
||||
sip_session->set_request(req);
|
||||
}else{
|
||||
sip_session->set_invite_status(SrsGb28181SipSessionUnkonw);
|
||||
sip_session->set_invite_time(srs_get_system_time());
|
||||
|
||||
if (req->status == "200") {
|
||||
send_ack(req, from, fromlen);
|
||||
SrsGb28181Device *device = sip_session->get_device_info(req->sip_auth_id);
|
||||
if (device){
|
||||
device->invite_status = SrsGb28181SipSessionInviteOk;
|
||||
device->req_inivate.copy(req);
|
||||
device->invite_time = srs_get_system_time();
|
||||
}
|
||||
}else if (req->status == "100") {
|
||||
//send_ack(req, from, fromlen);
|
||||
SrsGb28181Device *device = sip_session->get_device_info(req->sip_auth_id);
|
||||
if (device){
|
||||
device->req_inivate.copy(req);
|
||||
device->invite_status = SrsGb28181SipSessionTrying;
|
||||
device->invite_time = srs_get_system_time();
|
||||
}
|
||||
}else{
|
||||
SrsGb28181Device *device = sip_session->get_device_info(req->sip_auth_id);
|
||||
if (device){
|
||||
device->req_inivate.copy(req);
|
||||
device->invite_status = SrsGb28181SipSessionUnkonw;
|
||||
device->invite_time = srs_get_system_time();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}else if (req->is_bye()) {
|
||||
srs_trace("gb28181: request client id=%s, peer(%s, %d)", req->sip_auth_id.c_str(),
|
||||
peer_ip.c_str(), peer_port);
|
||||
srs_trace("gb28181: request %s method=%s, uri=%s, version=%s ",
|
||||
srs_trace("gb28181: %s method=%s, uri=%s, version=%s ",
|
||||
req->get_cmdtype_str().c_str(), req->method.c_str(), req->uri.c_str(), req->version.c_str());
|
||||
|
||||
SrsGb28181SipSession* sip_session = fetch(session_id);
|
||||
send_status(req, from, fromlen);
|
||||
|
||||
SrsGb28181SipSession* sip_session = fetch_session_by_callid(req->call_id);
|
||||
srs_trace("gb28181: request client id=%s, peer(%s, %d)", req->sip_auth_id.c_str(),
|
||||
peer_ip.c_str(), peer_port);
|
||||
srs_trace("gb28181: %s method=%s, uri=%s, version=%s ",
|
||||
req->get_cmdtype_str().c_str(), req->method.c_str(), req->uri.c_str(), req->version.c_str());
|
||||
|
||||
if (!sip_session){
|
||||
srs_trace("gb28181: %s client not registered", req->sip_auth_id.c_str());
|
||||
srs_trace("gb28181: call_id %s not map %s client ", req->call_id.c_str(), req->sip_auth_id.c_str());
|
||||
return err;
|
||||
}
|
||||
|
||||
sip_session->set_sockaddr((sockaddr)*from);
|
||||
sip_session->set_sockaddr_len(fromlen);
|
||||
|
||||
sip_session->set_invite_status(SrsGb28181SipSessionBye);
|
||||
sip_session->set_invite_time(srs_get_system_time());
|
||||
|
||||
if (req->cmdtype == SrsSipCmdRespone){
|
||||
srs_trace("gb28181: BYE %s client status=%s", req->sip_auth_id.c_str(), req->status.c_str());
|
||||
|
||||
if (req->status == "200") {
|
||||
SrsGb28181Device *device = sip_session->get_device_info(req->sip_auth_id);
|
||||
if (device){
|
||||
device->invite_status = SrsGb28181SipSessionBye;
|
||||
device->invite_time = srs_get_system_time();
|
||||
}
|
||||
}else {
|
||||
//TODO:fixme
|
||||
SrsGb28181Device *device = sip_session->get_device_info(req->sip_auth_id);
|
||||
if (device){
|
||||
device->invite_status = SrsGb28181SipSessionBye;
|
||||
device->invite_time = srs_get_system_time();
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
srs_trace("gb28181: ingor request method=%s", req->method.c_str());
|
||||
}
|
||||
|
@ -427,6 +632,7 @@ int SrsGb28181SipService::send_ack(SrsSipRequest *req, sockaddr *f, int l)
|
|||
req->host_port = config->sip_port;
|
||||
req->realm = config->sip_realm;
|
||||
req->serial = config->sip_serial;
|
||||
req->chid = req->sip_auth_id;
|
||||
|
||||
sip->req_ack(ss, req);
|
||||
return send_message(f, l, ss);
|
||||
|
@ -448,64 +654,97 @@ int SrsGb28181SipService::send_status(SrsSipRequest *req, sockaddr *f, int l)
|
|||
}
|
||||
|
||||
|
||||
int SrsGb28181SipService::send_invite(SrsSipRequest *req, string ip, int port, uint32_t ssrc)
|
||||
srs_error_t SrsGb28181SipService::send_invite(SrsSipRequest *req, string ip, int port, uint32_t ssrc, std::string chid)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
srs_assert(req);
|
||||
|
||||
SrsGb28181SipSession *sip_session = fetch(req->sip_auth_id);
|
||||
|
||||
if (!sip_session){
|
||||
return ERROR_GB28181_SESSION_IS_NOTEXIST;
|
||||
return srs_error_new(ERROR_GB28181_SESSION_IS_NOTEXIST, "sip session not exist");
|
||||
}
|
||||
|
||||
//if you are inviting or succeed in invite,
|
||||
//you cannot invite again. you need to 'bye' and try again
|
||||
if (sip_session->invite_status() == SrsGb28181SipSessionTrying ||
|
||||
sip_session->invite_status() == SrsGb28181SipSessionInviteOk){
|
||||
return ERROR_GB28181_SIP_IS_INVITING;
|
||||
SrsGb28181Device *device = sip_session->get_device_info(chid);
|
||||
if (!device || device->device_status != "ON"){
|
||||
return srs_error_new(ERROR_GB28181_SIP_CH_OFFLINE, "sip device channel offline");
|
||||
}
|
||||
|
||||
|
||||
if (device->invite_status == SrsGb28181SipSessionTrying ||
|
||||
device->invite_status == SrsGb28181SipSessionInviteOk){
|
||||
return srs_error_new(ERROR_GB28181_SIP_IS_INVITING, "sip device channel inviting");
|
||||
}
|
||||
|
||||
req->host = config->host;
|
||||
req->host_port = config->sip_port;
|
||||
req->realm = config->sip_realm;
|
||||
req->serial = config->sip_serial;
|
||||
req->chid = chid;
|
||||
req->seq = sip_session->sip_cseq();
|
||||
|
||||
SrsSipRequest register_req = sip_session->request();
|
||||
req->to_realm = register_req.to_realm;
|
||||
req->from_realm = config->sip_realm;
|
||||
|
||||
std::stringstream ss;
|
||||
sip->req_invite(ss, req, ip, port, ssrc);
|
||||
|
||||
|
||||
sockaddr addr = sip_session->sockaddr_from();
|
||||
|
||||
if (send_message(&addr, sip_session->sockaddr_fromlen(), ss) <= 0)
|
||||
{
|
||||
return ERROR_GB28181_SIP_INVITE_FAILED;
|
||||
return srs_error_new(ERROR_GB28181_SIP_INVITE_FAILED, "sip device invite failed");
|
||||
}
|
||||
|
||||
sip_session->set_invite_status(SrsGb28181SipSessionTrying);
|
||||
//prame branch, from_tag, to_tag, call_id,
|
||||
//The parameter of 'bye' must be the same as 'invite'
|
||||
device->req_inivate.copy(req);
|
||||
device->invite_time = srs_get_system_time();
|
||||
device->invite_status = SrsGb28181SipSessionTrying;
|
||||
|
||||
return ERROR_SUCCESS;
|
||||
//call_id map sip_session
|
||||
sip_session_map_by_callid(sip_session, req->call_id);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int SrsGb28181SipService::send_bye(SrsSipRequest *req)
|
||||
srs_error_t SrsGb28181SipService::send_bye(SrsSipRequest *req, std::string chid)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
srs_assert(req);
|
||||
|
||||
SrsGb28181SipSession *sip_session = fetch(req->sip_auth_id);
|
||||
|
||||
if (!sip_session){
|
||||
return ERROR_GB28181_SESSION_IS_NOTEXIST;
|
||||
return srs_error_new(ERROR_GB28181_SESSION_IS_NOTEXIST, "sip session not exist");
|
||||
}
|
||||
|
||||
SrsGb28181Device *device = sip_session->get_device_info(chid);
|
||||
if (!device){
|
||||
return srs_error_new(ERROR_GB28181_SIP_CH_NOTEXIST, "sip device channel not exist");
|
||||
}
|
||||
|
||||
//prame branch, from_tag, to_tag, call_id,
|
||||
//The parameter of 'bye' must be the same as 'invite'
|
||||
SrsSipRequest r = sip_session->request();
|
||||
req->copy(&r);
|
||||
//SrsSipRequest r = sip_session->request();
|
||||
|
||||
req->host = config->host;
|
||||
req->copy(&device->req_inivate);
|
||||
|
||||
req->host = config->host;
|
||||
req->host_port = config->sip_port;
|
||||
req->realm = config->sip_realm;
|
||||
req->serial = config->sip_serial;
|
||||
|
||||
req->chid = chid;
|
||||
req->seq = sip_session->sip_cseq();
|
||||
|
||||
SrsSipRequest register_req = sip_session->request();
|
||||
req->to_realm = register_req.to_realm;
|
||||
req->from_realm = config->sip_realm;
|
||||
|
||||
//get protocol stack
|
||||
std::stringstream ss;
|
||||
sip->req_bye(ss, req);
|
||||
|
@ -513,20 +752,22 @@ int SrsGb28181SipService::send_bye(SrsSipRequest *req)
|
|||
sockaddr addr = sip_session->sockaddr_from();
|
||||
if (send_message(&addr, sip_session->sockaddr_fromlen(), ss) <= 0)
|
||||
{
|
||||
return ERROR_GB28181_SIP_BYE_FAILED;
|
||||
return srs_error_new(ERROR_GB28181_SIP_BYE_FAILED, "sip bye failed");
|
||||
}
|
||||
|
||||
return ERROR_SUCCESS;
|
||||
return err;
|
||||
}
|
||||
|
||||
int SrsGb28181SipService::send_sip_raw_data(SrsSipRequest *req, std::string data)
|
||||
srs_error_t SrsGb28181SipService::send_sip_raw_data(SrsSipRequest *req, std::string data)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
srs_assert(req);
|
||||
|
||||
SrsGb28181SipSession *sip_session = fetch(req->sip_auth_id);
|
||||
|
||||
if (!sip_session){
|
||||
return ERROR_GB28181_SESSION_IS_NOTEXIST;
|
||||
return srs_error_new(ERROR_GB28181_SESSION_IS_NOTEXIST, "sip session no exist");
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
|
@ -535,10 +776,140 @@ int SrsGb28181SipService::send_sip_raw_data(SrsSipRequest *req, std::string dat
|
|||
sockaddr addr = sip_session->sockaddr_from();
|
||||
if (send_message(&addr, sip_session->sockaddr_fromlen(), ss) <= 0)
|
||||
{
|
||||
return ERROR_GB28181_SIP_BYE_FAILED;
|
||||
return srs_error_new(ERROR_GB28181_SIP_RAW_DATA_FAILED, "sip raw data failed");
|
||||
}
|
||||
|
||||
return ERROR_SUCCESS;
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsGb28181SipService::send_query_catalog(SrsSipRequest *req)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
srs_assert(req);
|
||||
|
||||
SrsGb28181SipSession *sip_session = fetch(req->sip_auth_id);
|
||||
|
||||
if (!sip_session){
|
||||
return srs_error_new(ERROR_GB28181_SESSION_IS_NOTEXIST, "sip session not exist");
|
||||
}
|
||||
|
||||
req->host = config->host;
|
||||
req->host_port = config->sip_port;
|
||||
req->realm = config->sip_realm;
|
||||
req->serial = config->sip_serial;
|
||||
req->chid = req->sip_auth_id;
|
||||
req->seq = sip_session->sip_cseq();
|
||||
|
||||
//get protocol stack
|
||||
std::stringstream ss;
|
||||
sip->req_query_catalog(ss, req);
|
||||
|
||||
return send_sip_raw_data(req, ss.str());
|
||||
}
|
||||
|
||||
srs_error_t SrsGb28181SipService::send_ptz(SrsSipRequest *req, std::string chid, std::string cmd,
|
||||
uint8_t speed, int priority)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
srs_assert(req);
|
||||
|
||||
SrsGb28181SipSession *sip_session = fetch(req->sip_auth_id);
|
||||
|
||||
if (!sip_session){
|
||||
return srs_error_new(ERROR_GB28181_SESSION_IS_NOTEXIST, "sip session not exist");
|
||||
}
|
||||
|
||||
SrsGb28181Device *device = sip_session->get_device_info(chid);
|
||||
if (!device){
|
||||
return srs_error_new(ERROR_GB28181_SIP_CH_NOTEXIST, "sip device channel not exist");
|
||||
}
|
||||
|
||||
if (device->invite_status != SrsGb28181SipSessionInviteOk){
|
||||
return srs_error_new(ERROR_GB28181_SIP_NOT_INVITE, "sip device channel not inviting");
|
||||
}
|
||||
|
||||
//prame branch, from_tag, to_tag, call_id,
|
||||
//The parameter of 'bye' must be the same as 'invite'
|
||||
//SrsSipRequest r = sip_session->request();
|
||||
req->copy(&device->req_inivate);
|
||||
|
||||
req->host = config->host;
|
||||
req->host_port = config->sip_port;
|
||||
req->realm = config->sip_realm;
|
||||
req->serial = config->sip_serial;
|
||||
req->chid = chid;
|
||||
req->seq = sip_session->sip_cseq();
|
||||
|
||||
SrsSipPtzCmdType ptzcmd = SrsSipPtzCmdRight;
|
||||
const char *ss_cmd = cmd.c_str();
|
||||
if (!strcasecmp(ss_cmd, "stop")){
|
||||
ptzcmd = SrsSipPtzCmdStop;
|
||||
}else if (!strcasecmp(ss_cmd, "right")){
|
||||
ptzcmd = SrsSipPtzCmdRight;
|
||||
}else if (!strcasecmp(ss_cmd, "left")){
|
||||
ptzcmd = SrsSipPtzCmdLeft;
|
||||
}else if (!strcasecmp(ss_cmd, "down")){
|
||||
ptzcmd = SrsSipPtzCmdDown;
|
||||
}else if (!strcasecmp(ss_cmd, "up")){
|
||||
ptzcmd = SrsSipPtzCmdUp;
|
||||
}else if (!strcasecmp(ss_cmd, "zoomout")){
|
||||
ptzcmd = SrsSipPtzCmdZoomOut;
|
||||
}else if (!strcasecmp(ss_cmd, "zoomin")){
|
||||
ptzcmd = SrsSipPtzCmdZoomIn;
|
||||
}else{
|
||||
return srs_error_new(ERROR_GB28181_SIP_PTZ_CMD_INVALID, "sip ptz cmd no support");
|
||||
}
|
||||
|
||||
if (speed < 0 || speed > 0xFF){
|
||||
return srs_error_new(ERROR_GB28181_SIP_PTZ_CMD_INVALID, "sip ptz cmd speed out of range");
|
||||
}
|
||||
|
||||
if (priority <= 0 ){
|
||||
priority = 5;
|
||||
}
|
||||
|
||||
//get protocol stack
|
||||
std::stringstream ss;
|
||||
sip->req_ptz(ss, req, ptzcmd, speed, priority);
|
||||
|
||||
sockaddr addr = sip_session->sockaddr_from();
|
||||
if (send_message(&addr, sip_session->sockaddr_fromlen(), ss) <= 0)
|
||||
{
|
||||
return srs_error_new(ERROR_GB28181_SIP_PTZ_FAILED, "sip ptz failed");
|
||||
}
|
||||
|
||||
//call_id map sip_session
|
||||
sip_session_map_by_callid(sip_session, req->call_id);
|
||||
|
||||
return err;
|
||||
|
||||
}
|
||||
|
||||
srs_error_t SrsGb28181SipService::query_sip_session(std::string sid, SrsJsonArray* arr)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if (!sid.empty()){
|
||||
SrsGb28181SipSession* sess = fetch(sid);
|
||||
if (!sess){
|
||||
return srs_error_new(ERROR_GB28181_SESSION_IS_NOTEXIST, "sip session not exist");
|
||||
}
|
||||
SrsJsonObject* obj = SrsJsonAny::object();
|
||||
arr->append(obj);
|
||||
sess->dumps(obj);
|
||||
}else {
|
||||
std::map<std::string, SrsGb28181SipSession*>::iterator it;
|
||||
for (it = sessions.begin(); it != sessions.end(); ++it) {
|
||||
SrsGb28181SipSession* sess = it->second;
|
||||
SrsJsonObject* obj = SrsJsonAny::object();
|
||||
arr->append(obj);
|
||||
sess->dumps(obj);
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsGb28181SipService::fetch_or_create_sip_session(SrsSipRequest *req, SrsGb28181SipSession** sip_session)
|
||||
|
@ -596,4 +967,30 @@ void SrsGb28181SipService::destroy()
|
|||
sessions.clear();
|
||||
}
|
||||
|
||||
void SrsGb28181SipService::sip_session_map_by_callid(SrsGb28181SipSession *sess, std::string call_id)
|
||||
{
|
||||
if (sessions_by_callid.find(call_id) == sessions_by_callid.end()) {
|
||||
sessions_by_callid[call_id] = sess;
|
||||
}
|
||||
}
|
||||
|
||||
void SrsGb28181SipService::sip_session_unmap_by_callid(std::string call_id)
|
||||
{
|
||||
std::map<std::string, SrsGb28181SipSession*>::iterator it = sessions_by_callid.find(call_id);
|
||||
if (it != sessions_by_callid.end()) {
|
||||
sessions_by_callid.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
SrsGb28181SipSession* SrsGb28181SipService::fetch_session_by_callid(std::string call_id)
|
||||
{
|
||||
SrsGb28181SipSession* session = NULL;
|
||||
if (sessions_by_callid.find(call_id) == sessions_by_callid.end()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
session = sessions_by_callid[call_id];
|
||||
return session;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -41,6 +41,7 @@ class SrsSipRequest;
|
|||
class SrsGb28181Config;
|
||||
class SrsSipStack;
|
||||
class SrsGb28181SipService;
|
||||
class SrsGb28181Device;
|
||||
|
||||
enum SrsGb28181SipSessionStatusType{
|
||||
SrsGb28181SipSessionUnkonw = 0,
|
||||
|
@ -51,6 +52,19 @@ enum SrsGb28181SipSessionStatusType{
|
|||
SrsGb28181SipSessionBye = 5,
|
||||
};
|
||||
|
||||
class SrsGb28181Device
|
||||
{
|
||||
public:
|
||||
SrsGb28181Device();
|
||||
virtual ~SrsGb28181Device();
|
||||
public:
|
||||
std::string device_id;
|
||||
std::string device_status;
|
||||
SrsGb28181SipSessionStatusType invite_status;
|
||||
srs_utime_t invite_time;
|
||||
SrsSipRequest req_inivate;
|
||||
};
|
||||
|
||||
class SrsGb28181SipSession: public ISrsCoroutineHandler, public ISrsConnection
|
||||
{
|
||||
private:
|
||||
|
@ -67,6 +81,7 @@ private:
|
|||
srs_utime_t _alive_time;
|
||||
srs_utime_t _invite_time;
|
||||
srs_utime_t _reg_expires;
|
||||
srs_utime_t _query_catalog_time;
|
||||
|
||||
std::string _peer_ip;
|
||||
int _peer_port;
|
||||
|
@ -75,10 +90,17 @@ private:
|
|||
int _fromlen;
|
||||
SrsSipRequest *req;
|
||||
|
||||
std::map<std::string, SrsGb28181Device*> _device_list;
|
||||
//std::map<std::string, int> _device_status;
|
||||
int _sip_cseq;
|
||||
|
||||
public:
|
||||
SrsGb28181SipSession(SrsGb28181SipService *c, SrsSipRequest* r);
|
||||
virtual ~SrsGb28181SipSession();
|
||||
|
||||
private:
|
||||
void destroy();
|
||||
|
||||
public:
|
||||
void set_register_status(SrsGb28181SipSessionStatusType s) { _register_status = s;}
|
||||
void set_alive_status(SrsGb28181SipSessionStatusType s) { _alive_status = s;}
|
||||
|
@ -94,7 +116,6 @@ public:
|
|||
void set_sockaddr_len(int l) { _fromlen = l;}
|
||||
void set_request(SrsSipRequest *r) { req->copy(r);}
|
||||
|
||||
|
||||
SrsGb28181SipSessionStatusType register_status() { return _register_status;}
|
||||
SrsGb28181SipSessionStatusType alive_status() { return _alive_status;}
|
||||
SrsGb28181SipSessionStatusType invite_status() { return _invite_status;}
|
||||
|
@ -108,8 +129,13 @@ public:
|
|||
sockaddr sockaddr_from() { return _from;}
|
||||
int sockaddr_fromlen() { return _fromlen;}
|
||||
SrsSipRequest request() { return *req;}
|
||||
int sip_cseq(){ return _sip_cseq++;}
|
||||
|
||||
std::string session_id() { return _session_id;}
|
||||
public:
|
||||
void update_device_list(std::map<std::string, std::string> devlist);
|
||||
SrsGb28181Device *get_device_info(std::string chid);
|
||||
void dumps(SrsJsonObject* obj);
|
||||
|
||||
public:
|
||||
virtual srs_error_t serve();
|
||||
|
@ -130,6 +156,7 @@ private:
|
|||
srs_netfd_t lfd;
|
||||
|
||||
std::map<std::string, SrsGb28181SipSession*> sessions;
|
||||
std::map<std::string, SrsGb28181SipSession*> sessions_by_callid;
|
||||
public:
|
||||
SrsGb28181SipService(SrsConfDirective* c);
|
||||
virtual ~SrsGb28181SipService();
|
||||
|
@ -140,15 +167,17 @@ public:
|
|||
virtual void set_stfd(srs_netfd_t fd);
|
||||
private:
|
||||
void destroy();
|
||||
srs_error_t on_udp_sip(std::string host, int port, char* buf, int nb_buf, sockaddr* from, int fromlen);
|
||||
srs_error_t on_udp_sip(std::string host, int port, std::string recv_msg, sockaddr* from, int fromlen);
|
||||
public:
|
||||
int send_message(sockaddr* f, int l, std::stringstream& ss);
|
||||
|
||||
int send_ack(SrsSipRequest *req, sockaddr *f, int l);
|
||||
int send_status(SrsSipRequest *req, sockaddr *f, int l);
|
||||
|
||||
int send_invite(SrsSipRequest *req, std::string ip, int port, uint32_t ssrc);
|
||||
int send_bye(SrsSipRequest *req);
|
||||
srs_error_t send_invite(SrsSipRequest *req, std::string ip, int port, uint32_t ssrc, std::string chid);
|
||||
srs_error_t send_bye(SrsSipRequest *req, std::string chid);
|
||||
srs_error_t send_query_catalog(SrsSipRequest *req);
|
||||
srs_error_t send_ptz(SrsSipRequest *req, std::string chid, std::string cmd, uint8_t speed, int priority);
|
||||
|
||||
// The SIP command is transmitted through HTTP API,
|
||||
// and the body content is transmitted to the device,
|
||||
|
@ -164,7 +193,8 @@ public:
|
|||
// Content-Length: 0
|
||||
//
|
||||
//
|
||||
int send_sip_raw_data(SrsSipRequest *req, std::string data);
|
||||
srs_error_t send_sip_raw_data(SrsSipRequest *req, std::string data);
|
||||
srs_error_t query_sip_session(std::string sid, SrsJsonArray* arr);
|
||||
|
||||
public:
|
||||
srs_error_t fetch_or_create_sip_session(SrsSipRequest *req, SrsGb28181SipSession** sess);
|
||||
|
@ -172,6 +202,9 @@ public:
|
|||
void remove_session(std::string id);
|
||||
SrsGb28181Config* get_config();
|
||||
|
||||
void sip_session_map_by_callid(SrsGb28181SipSession *sess, std::string call_id);
|
||||
void sip_session_unmap_by_callid(std::string call_id);
|
||||
SrsGb28181SipSession* fetch_session_by_callid(std::string call_id);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1741,6 +1741,21 @@ SrsGoApiGb28181::~SrsGoApiGb28181()
|
|||
|
||||
srs_error_t SrsGoApiGb28181::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if ((err = do_serve_http(w, r)) != srs_success) {
|
||||
srs_warn("Server GB28181 err %s", srs_error_desc(err).c_str());
|
||||
int code = srs_error_code(err); srs_error_reset(err);
|
||||
return srs_api_response_code(w, r, code);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsGoApiGb28181::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
SrsJsonObject* obj = SrsJsonAny::object();
|
||||
SrsAutoFree(SrsJsonObject, obj);
|
||||
|
||||
|
@ -1756,103 +1771,144 @@ srs_error_t SrsGoApiGb28181::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa
|
|||
//fixed, random
|
||||
string port_mode = r->query_get("port_mode");
|
||||
|
||||
if (_srs_gb28181) {
|
||||
if(action == "create_channel"){
|
||||
if (id.empty()){
|
||||
return srs_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
|
||||
}
|
||||
if (!_srs_gb28181) {
|
||||
return srs_error_new(ERROR_GB28181_SERVER_NOT_RUN, "no gb28181 engine");
|
||||
}
|
||||
|
||||
SrsGb28181StreamChannel channel;
|
||||
channel.set_channel_id(id);
|
||||
channel.set_app(app);
|
||||
channel.set_stream(stream);
|
||||
channel.set_port_mode(port_mode);
|
||||
|
||||
uint32_t code = _srs_gb28181->create_stream_channel(&channel);
|
||||
if (code != ERROR_SUCCESS) {
|
||||
return srs_api_response_code(w, r, code);
|
||||
}
|
||||
|
||||
data->set("query", SrsJsonAny::object()
|
||||
->set("id", SrsJsonAny::str(channel.get_channel_id().c_str()))
|
||||
->set("ip", SrsJsonAny::str(channel.get_ip().c_str()))
|
||||
->set("rtmp_port", SrsJsonAny::integer(channel.get_rtmp_port()))
|
||||
->set("app", SrsJsonAny::str(channel.get_app().c_str()))
|
||||
->set("stream", SrsJsonAny::str(channel.get_stream().c_str()))
|
||||
->set("rtp_port", SrsJsonAny::integer(channel.get_rtp_port()))
|
||||
->set("ssrc", SrsJsonAny::integer(channel.get_ssrc())));
|
||||
return srs_api_response(w, r, obj->dumps());
|
||||
|
||||
}
|
||||
else if(action == "delete_channel"){
|
||||
if (id.empty()){
|
||||
return srs_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
|
||||
}
|
||||
|
||||
uint32_t code = _srs_gb28181->delete_stream_channel(id);
|
||||
return srs_api_response_code(w, r, code);
|
||||
}
|
||||
else if(action == "query_channel") {
|
||||
SrsJsonArray* arr = SrsJsonAny::array();
|
||||
data->set("channels", arr);
|
||||
|
||||
uint32_t code = _srs_gb28181->queue_stream_channel(id, arr);
|
||||
if (code != ERROR_SUCCESS) {
|
||||
return srs_api_response_code(w, r, code);
|
||||
}
|
||||
|
||||
return srs_api_response(w, r, obj->dumps());
|
||||
}
|
||||
else if(action == "sip_invite"){
|
||||
if (id.empty()){
|
||||
return srs_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
|
||||
}
|
||||
|
||||
string ssrc = r->query_get("ssrc");
|
||||
string rtp_port = r->query_get("rtp_port");
|
||||
string ip = r->query_get("ip");
|
||||
|
||||
int _port = strtoul(rtp_port.c_str(), NULL, 10);
|
||||
uint32_t _ssrc = (uint32_t)(strtoul(ssrc.c_str(), NULL, 10));
|
||||
|
||||
|
||||
|
||||
int code = _srs_gb28181->notify_sip_invite(id, ip, _port, _ssrc);
|
||||
return srs_api_response_code(w, r, code);
|
||||
}
|
||||
else if(action == "sip_bye"){
|
||||
if (id.empty()){
|
||||
return srs_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
|
||||
}
|
||||
|
||||
int code = _srs_gb28181->notify_sip_bye(id);
|
||||
return srs_api_response_code(w, r, code);
|
||||
}
|
||||
else if(action == "sip_raw_data"){
|
||||
if (id.empty()){
|
||||
return srs_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
|
||||
}
|
||||
|
||||
std::string body;
|
||||
r->body_read_all(body);
|
||||
int code = _srs_gb28181->notify_sip_raw_data(id, body);
|
||||
return srs_api_response_code(w, r, code);
|
||||
}
|
||||
else if(action == "sip_unregister"){
|
||||
if (id.empty()){
|
||||
return srs_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
|
||||
}
|
||||
|
||||
int code = _srs_gb28181->notify_sip_unregister(id);
|
||||
return srs_api_response_code(w, r, code);
|
||||
}
|
||||
else
|
||||
{
|
||||
return srs_api_response_code(w, r, ERROR_GB28181_ACTION_INVALID);
|
||||
if(action == "create_channel"){
|
||||
if (id.empty()){
|
||||
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id");
|
||||
}
|
||||
|
||||
}else {
|
||||
return srs_api_response_code(w, r, ERROR_GB28181_SERVER_NOT_RUN);
|
||||
SrsGb28181StreamChannel channel;
|
||||
channel.set_channel_id(id);
|
||||
channel.set_app(app);
|
||||
channel.set_stream(stream);
|
||||
channel.set_port_mode(port_mode);
|
||||
|
||||
if ((err = _srs_gb28181->create_stream_channel(&channel)) != srs_success) {
|
||||
return srs_error_wrap(err, "create stream channel");
|
||||
}
|
||||
|
||||
data->set("query", SrsJsonAny::object()
|
||||
->set("id", SrsJsonAny::str(channel.get_channel_id().c_str()))
|
||||
->set("ip", SrsJsonAny::str(channel.get_ip().c_str()))
|
||||
->set("rtmp_port", SrsJsonAny::integer(channel.get_rtmp_port()))
|
||||
->set("app", SrsJsonAny::str(channel.get_app().c_str()))
|
||||
->set("stream", SrsJsonAny::str(channel.get_stream().c_str()))
|
||||
->set("rtp_port", SrsJsonAny::integer(channel.get_rtp_port()))
|
||||
->set("ssrc", SrsJsonAny::integer(channel.get_ssrc())));
|
||||
return srs_api_response(w, r, obj->dumps());
|
||||
|
||||
} else if(action == "delete_channel"){
|
||||
if (id.empty()){
|
||||
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id");
|
||||
}
|
||||
|
||||
if ((err = _srs_gb28181->delete_stream_channel(id)) != srs_success) {
|
||||
return srs_error_wrap(err, "delete stream channel");
|
||||
}
|
||||
|
||||
return srs_api_response_code(w, r, code);
|
||||
} else if(action == "query_channel") {
|
||||
SrsJsonArray* arr = SrsJsonAny::array();
|
||||
data->set("channels", arr);
|
||||
|
||||
if ((err = _srs_gb28181->query_stream_channel(id, arr)) != srs_success) {
|
||||
return srs_error_wrap(err, "query stream channel");
|
||||
}
|
||||
|
||||
return srs_api_response(w, r, obj->dumps());
|
||||
} else if(action == "sip_invite"){
|
||||
string chid = r->query_get("chid");
|
||||
if (id.empty() || chid.empty()){
|
||||
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id or chid");
|
||||
}
|
||||
|
||||
string ssrc = r->query_get("ssrc");
|
||||
string rtp_port = r->query_get("rtp_port");
|
||||
string ip = r->query_get("ip");
|
||||
|
||||
int _port = strtoul(rtp_port.c_str(), NULL, 10);
|
||||
uint32_t _ssrc = (uint32_t)(strtoul(ssrc.c_str(), NULL, 10));
|
||||
|
||||
if ((err = _srs_gb28181->notify_sip_invite(id, ip, _port, _ssrc, chid)) != srs_success) {
|
||||
return srs_error_wrap(err, "notify sip invite");
|
||||
}
|
||||
|
||||
return srs_api_response_code(w, r, code);
|
||||
} else if(action == "sip_bye"){
|
||||
string chid = r->query_get("chid");
|
||||
if (id.empty() || chid.empty()){
|
||||
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id or chid");
|
||||
}
|
||||
|
||||
if ((err = _srs_gb28181->notify_sip_bye(id, chid)) != srs_success) {
|
||||
return srs_error_wrap(err, "notify sip bye");
|
||||
}
|
||||
|
||||
return srs_api_response_code(w, r, code);
|
||||
} else if(action == "sip_ptz"){
|
||||
string chid = r->query_get("chid");
|
||||
string ptzcmd = r->query_get("ptzcmd");
|
||||
string speed = r->query_get("speed");
|
||||
string priority = r->query_get("priority");
|
||||
if (id.empty() || chid.empty() || ptzcmd.empty() || speed.empty()){
|
||||
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id or chid or ptzcmd or speed");
|
||||
}
|
||||
|
||||
uint8_t _speed = (uint8_t)(strtoul(speed.c_str(), NULL, 10));
|
||||
int _priority = (int)(strtoul(priority.c_str(), NULL, 10));
|
||||
|
||||
if ((err = _srs_gb28181->notify_sip_ptz(id, chid, ptzcmd, _speed, _priority)) != srs_success) {
|
||||
return srs_error_wrap(err, "notify sip ptz");
|
||||
}
|
||||
|
||||
return srs_api_response_code(w, r, code);
|
||||
} else if(action == "sip_raw_data"){
|
||||
if (id.empty()){
|
||||
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id");
|
||||
}
|
||||
|
||||
std::string body;
|
||||
r->body_read_all(body);
|
||||
|
||||
if ((err = _srs_gb28181->notify_sip_raw_data(id, body)) != srs_success) {
|
||||
return srs_error_wrap(err, "notify sip raw data");
|
||||
}
|
||||
|
||||
return srs_api_response_code(w, r, code);
|
||||
} else if(action == "sip_unregister"){
|
||||
if (id.empty()){
|
||||
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id");
|
||||
}
|
||||
|
||||
if ((err = _srs_gb28181->notify_sip_unregister(id)) != srs_success) {
|
||||
return srs_error_wrap(err, "notify sip unregister");
|
||||
}
|
||||
|
||||
return srs_api_response_code(w, r, code);
|
||||
} else if(action == "sip_query_catalog"){
|
||||
if (id.empty()){
|
||||
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id");
|
||||
}
|
||||
|
||||
if ((err = _srs_gb28181->notify_sip_query_catalog(id)) != srs_success) {
|
||||
return srs_error_wrap(err, "notify sip query catelog");
|
||||
}
|
||||
|
||||
return srs_api_response_code(w, r, code);
|
||||
} else if(action == "sip_query_session"){
|
||||
SrsJsonArray* arr = SrsJsonAny::array();
|
||||
data->set("sessions", arr);
|
||||
|
||||
if ((err = _srs_gb28181->query_sip_session(id, arr)) != srs_success) {
|
||||
return srs_error_wrap(err, "notify sip session");
|
||||
}
|
||||
|
||||
return srs_api_response(w, r, obj->dumps());
|
||||
} else {
|
||||
return srs_error_new(ERROR_GB28181_ACTION_INVALID, "action %s", action.c_str());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -249,6 +249,8 @@ public:
|
|||
virtual ~SrsGoApiGb28181();
|
||||
public:
|
||||
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
|
||||
private:
|
||||
virtual srs_error_t do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
|
||||
};
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue