1
0
Fork 0
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:
winlin 2020-04-21 15:27:40 +08:00
commit dabad68f48
22 changed files with 2783 additions and 373 deletions

View file

@ -370,6 +370,9 @@ stream_caster {
# Whether bundle media stream port.
# default: on
invite_port_fixed on;
# interval to query equipment list from equipment or subordinate domain, unit(s)
# default: 60
query_catalog_interval 60;
}
}

View file

@ -90,16 +90,18 @@ stream_caster {
# 认为设备离线
keepalive_timeout 120;
# 注册之后是否自动给设备端发送invite
# on: 是 off 不是需要通过api控制
auto_play on;
# 设备将流发送的端口,是否固定
# on 发送流到多路复用端口 如9000
# off 自动从rtp_mix_port - rtp_max_port 之间的值中
# 选一个可以用的端口
invite_port_fixed on;
# 向设备或下级域查询设备列表的间隔,单位(秒)
# 默认60秒
query_catalog_interval 60;
}
}
vhost __defaultVhost__ {

View file

@ -24,12 +24,14 @@
<ul class="nav">
<li><a id="nav_srs_player" href="srs_player.html">SRS播放器</a></li>
<li class="active"><a id="nav_rtc_player" href="rtc_player.html">RTC播放器</a></li>
<li><a id="nav_srs_publisher" href="srs_publisher.html">SRS编码器</a></li>
<li><a id="nav_srs_chat" href="srs_chat.html">SRS会议</a></li>
<li><a id="nav_srs_bwt" href="srs_bwt.html">SRS测网速</a></li>
<!--<li><a id="nav_srs_publisher" href="srs_publisher.html">SRS编码器</a></li>-->
<!--<li><a id="nav_srs_chat" href="srs_chat.html">SRS会议</a></li>-->
<!--<li><a id="nav_srs_bwt" href="srs_bwt.html">SRS测网速</a></li>-->
<!--<li><a id="nav_jwplayer6" href="jwplayer6.html">JWPlayer6播放器</a></li>-->
<!--<li><a id="nav_osmf" href="osmf.html">AdobeOSMF播放器</a></li>-->
<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>
<!--<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>-->
<li><a id="nav_gb28181" href="srs_gb28181.html">GB28181</a></li>
<li><a href="https://github.com/ossrs/srs">源码</a></li>
</ul>
</div>
</div>

View file

@ -30,6 +30,7 @@
<!--<li><a id="nav_jwplayer6" href="jwplayer6.html">JWPlayer6播放器</a></li>-->
<!--<li><a id="nav_osmf" href="osmf.html">AdobeOSMF播放器</a></li>-->
<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>
<li><a id="nav_gb28181" href="srs_gb28181.html">SRS-GB28181</a></li>
</ul>
</div>
</div>

View file

@ -29,6 +29,7 @@
<!--<li><a id="nav_jwplayer6" href="jwplayer6.html">JWPlayer6播放器</a></li>-->
<!--<li><a id="nav_osmf" href="osmf.html">AdobeOSMF播放器</a></li>-->
<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>
<li><a id="nav_gb28181" href="srs_gb28181.html">SRS-GB28181</a></li>
</ul>
</div>
</div>

File diff suppressed because it is too large Load diff

View file

@ -34,12 +34,14 @@
<ul class="nav">
<li class="active"><a id="nav_srs_player" href="srs_player.html">SRS播放器</a></li>
<li><a id="nav_rtc_player" href="rtc_player.html">RTC播放器</a></li>
<li><a id="nav_srs_publisher" href="srs_publisher.html">SRS编码器</a></li>
<li><a id="nav_srs_chat" href="srs_chat.html">SRS会议</a></li>
<li><a id="nav_srs_bwt" href="srs_bwt.html">SRS测网速</a></li>
<!--<li><a id="nav_srs_publisher" href="srs_publisher.html">SRS编码器</a></li>-->
<!--<li><a id="nav_srs_chat" href="srs_chat.html">SRS会议</a></li>-->
<!--<li><a id="nav_srs_bwt" href="srs_bwt.html">SRS测网速</a></li>-->
<!--<li><a id="nav_jwplayer6" href="jwplayer6.html">JWPlayer6播放器</a></li>-->
<!--<li><a id="nav_osmf" href="osmf.html">AdobeOSMF播放器</a></li>-->
<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>
<!--li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>-->
<li><a id="nav_gb28181" href="srs_gb28181.html">GB28181</a></li>
<li><a href="https://github.com/ossrs/srs">源码</a></li>
</ul>
</div>
</div>

View file

@ -29,6 +29,7 @@
<!--<li><a id="nav_jwplayer6" href="jwplayer6.html">JWPlayer6播放器</a></li>-->
<!--<li><a id="nav_osmf" href="osmf.html">AdobeOSMF播放器</a></li>-->
<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>
<li><a id="nav_gb28181" href="srs_gb28181.html">SRS-GB28181</a></li>
</ul>
</div>
</div>

View file

@ -25,6 +25,7 @@
<!--<li><a id="nav_jwplayer6" href="jwplayer6.html">JWPlayer6播放器</a></li>-->
<!--<li><a id="nav_osmf" href="osmf.html">AdobeOSMF播放器</a></li>-->
<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>
<li><a id="nav_gb28181" href="srs_gb28181.html">SRS-GB28181</a></li>
</ul>
</div>
</div>

View file

@ -26,6 +26,7 @@
<!--<li><a id="nav_jwplayer6" href="jwplayer6.html">JWPlayer6播放器</a></li>-->
<!--<li><a id="nav_osmf" href="osmf.html">AdobeOSMF播放器</a></li>-->
<li class="active"><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>
<li><a id="nav_gb28181" href="srs_gb28181.html">SRS-GB28181</a></li>
</ul>
</div>
</div>

View file

@ -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");

View file

@ -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:

View file

@ -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]);
@ -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
@ -838,6 +847,21 @@ srs_error_t SrsGb28181RtmpMuxer::do_cycle()
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");
}
@ -1199,6 +1223,7 @@ srs_error_t SrsGb28181RtmpMuxer::connect()
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);
}
delete_stream_channel(id);
sip_service->remove_session(id);
return ERROR_SUCCESS;
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");
}
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);
}

View file

@ -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();

View file

@ -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) {
@ -122,12 +151,80 @@ srs_error_t SrsGb28181SipSession::do_cycle()
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){
@ -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));
//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();
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;
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)
{
@ -233,7 +415,8 @@ 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();
if (nb_buf < 10) {
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 (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,6 +473,7 @@ 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);
@ -292,17 +483,22 @@ 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
if (req->cmdtype == SrsSipCmdRequest){
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);
@ -310,53 +506,27 @@ srs_error_t SrsGb28181SipService::on_udp_sip(string peer_ip, int peer_port,
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);
//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());
if (req->status == "200") {
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());
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,27 +654,40 @@ 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);
@ -477,34 +696,54 @@ int SrsGb28181SipService::send_invite(SrsSipRequest *req, string ip, int port,
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->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;
@ -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;
}

View file

@ -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

View file

@ -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,10 +1771,13 @@ srs_error_t SrsGoApiGb28181::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa
//fixed, random
string port_mode = r->query_get("port_mode");
if (_srs_gb28181) {
if (!_srs_gb28181) {
return srs_error_new(ERROR_GB28181_SERVER_NOT_RUN, "no gb28181 engine");
}
if(action == "create_channel"){
if (id.empty()){
return srs_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id");
}
SrsGb28181StreamChannel channel;
@ -1768,9 +1786,8 @@ srs_error_t SrsGoApiGb28181::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa
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);
if ((err = _srs_gb28181->create_stream_channel(&channel)) != srs_success) {
return srs_error_wrap(err, "create stream channel");
}
data->set("query", SrsJsonAny::object()
@ -1783,29 +1800,29 @@ srs_error_t SrsGoApiGb28181::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa
->set("ssrc", SrsJsonAny::integer(channel.get_ssrc())));
return srs_api_response(w, r, obj->dumps());
}
else if(action == "delete_channel"){
} else if(action == "delete_channel"){
if (id.empty()){
return srs_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id");
}
uint32_t code = _srs_gb28181->delete_stream_channel(id);
return srs_api_response_code(w, r, code);
if ((err = _srs_gb28181->delete_stream_channel(id)) != srs_success) {
return srs_error_wrap(err, "delete stream channel");
}
else if(action == "query_channel") {
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);
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"){
if (id.empty()){
return srs_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
} 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");
@ -1815,44 +1832,83 @@ srs_error_t SrsGoApiGb28181::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa
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");
}
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);
} 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");
}
int code = _srs_gb28181->notify_sip_bye(id);
return srs_api_response_code(w, r, code);
if ((err = _srs_gb28181->notify_sip_bye(id, chid)) != srs_success) {
return srs_error_wrap(err, "notify sip bye");
}
else if(action == "sip_raw_data"){
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_api_response_code(w, r, ERROR_GB28181_VALUE_EMPTY);
return srs_error_new(ERROR_GB28181_VALUE_EMPTY, "no id");
}
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);
if ((err = _srs_gb28181->notify_sip_raw_data(id, body)) != srs_success) {
return srs_error_wrap(err, "notify sip raw data");
}
else if(action == "sip_unregister"){
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);
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");
}
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);
} 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_api_response_code(w, r, ERROR_GB28181_SERVER_NOT_RUN);
return srs_error_new(ERROR_GB28181_ACTION_INVALID, "action %s", action.c_str());
}
}
#endif

View file

@ -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

View file

@ -178,6 +178,8 @@
#define SRS_CONSTS_LOG_EXEC "EXE"
// The rtc.
#define SRS_CONSTS_LOG_RTC "RTC"
// The gb28181 stream log id.
#define SRS_CONSTS_LOG_GB28181_CASTER "GBS"
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

View file

@ -363,6 +363,13 @@
#define ERROR_GB28181_SIP_BYE_FAILED 6009
#define ERROR_GB28181_SIP_IS_INVITING 6010
#define ERROR_GB28181_CREATER_RTMPMUXER_FAILED 6011
#define ERROR_GB28181_SIP_CH_OFFLINE 6012
#define ERROR_GB28181_SIP_CH_NOTEXIST 6013
#define ERROR_GB28181_SIP_RAW_DATA_FAILED 6014
#define ERROR_GB28181_SIP_PRASE_FAILED 6015
#define ERROR_GB28181_SIP_PTZ_FAILED 6016
#define ERROR_GB28181_SIP_NOT_INVITE 6017
#define ERROR_GB28181_SIP_PTZ_CMD_INVALID 6018
///////////////////////////////////////////////////////
// HTTP API error.

View file

@ -45,10 +45,51 @@ using namespace std;
unsigned int srs_sip_random(int min,int max)
{
srand(int(time(0)));
//it is possible to duplicate data with time(0)
srand(unsigned(srs_update_system_time()));
return rand() % (max - min + 1) + min;
}
std::string srs_sip_generate_branch()
{
int rand = srs_sip_random(10000000, 99999999);
std::stringstream branch;
branch << "SrsGbB" << rand;
return branch.str();
}
std::string srs_sip_generate_to_tag()
{
uint32_t rand = srs_sip_random(10000000, 99999999);
std::stringstream branch;
branch << "SrsGbT" << rand;
return branch.str();
}
std::string srs_sip_generate_from_tag()
{
uint32_t rand = srs_sip_random(10000000, 99999999);
std::stringstream branch;
branch << "SrsGbF" << rand;
return branch.str();
}
std::string srs_sip_generate_call_id()
{
uint32_t rand = srs_sip_random(10000000, 99999999);
std::stringstream branch;
branch << "2020" << rand;
return branch.str();
}
std::string srs_sip_generate_sn()
{
uint32_t rand = srs_sip_random(10000000, 99999999);
std::stringstream sn;
sn << rand;
return sn.str();
}
std::string srs_sip_get_form_to_uri(std::string msg)
{
//<sip:34020000002000000001@3402000000>;tag=536961166
@ -109,7 +150,7 @@ std::string srs_sip_get_param(std::string msg, std::string param)
std::vector<std::string> v_pram = srs_string_split(value, "=");
if (v_pram.size() > 0) {
if (v_pram.size() > 1) {
return v_pram.at(1);
}
}
@ -156,6 +197,11 @@ SrsSipRequest::SrsSipRequest()
sip_username = "";
peer_ip = "";
peer_port = 0;
chid = "";
from_realm = "";
to_realm = "";
}
SrsSipRequest::~SrsSipRequest()
@ -240,6 +286,14 @@ void SrsSipRequest::copy(SrsSipRequest* src)
sip_username = src->sip_username;
peer_ip = src->peer_ip;
peer_port = src->peer_port;
chid = src->chid;
xml_body_map = src->xml_body_map;
device_list_map = src->device_list_map;
from_realm = src->from_realm;
to_realm = src->to_realm;
}
SrsSipStack::SrsSipStack()
@ -267,12 +321,158 @@ srs_error_t SrsSipStack::parse_request(SrsSipRequest** preq, const char* recv_ms
return err;
}
srs_error_t SrsSipStack::parse_xml(std::string xml_msg, std::map<std::string, std::string> &json_map)
{
/*
<?xml version="1.0" encoding="gb2312"?>
<Notify>
<CmdType>Keepalive</CmdType>
<SN>2034</SN>
<DeviceID>34020000001110000001</DeviceID>
<Status>OK</Status>
<Info>
<DeviceID>34020000001320000002</DeviceID>
<DeviceID>34020000001320000003</DeviceID>
<DeviceID>34020000001320000005</DeviceID>
<DeviceID>34020000001320000006</DeviceID>
<DeviceID>34020000001320000007</DeviceID>
<DeviceID>34020000001320000008</DeviceID>
</Info>
</Notify>
*/
const char* start = xml_msg.c_str();
const char* end = start + xml_msg.size();
char* p = (char*)start;
char* value_start = NULL;
std::string xml_header;
int xml_layer = 0;
//std::map<string, string> json_map;
std::map<int, string> json_key;
while (p < end) {
if (p[0] == '\n'){
p +=1;
value_start = NULL;
} else if (p[0] == '\r' && p[1] == '\n') {
p +=2;
value_start = NULL;
} else if (p[0] == '<' && p[1] == '/') { //</Notify> xml item end flag
std::string value = "";
if (value_start) {
value = std::string(value_start, p-value_start);
}
//skip </
p += 2;
//</Notify> get Notify
char *s = p;
while (p[0] != '>') {p++;}
std::string key(s, p-s);
//<DeviceList Num="2"> get DeviceList
std::vector<string> vec = srs_string_split(key, " ");
if (vec.empty()){
return srs_error_new(ERROR_GB28181_SIP_PRASE_FAILED, "prase xml");
}
key = vec.at(0);
/*xml element to map
<Notify>
<info>
<DeviceID>34020000001320000001</DeviceID>
<DeviceID>34020000001320000002</DeviceID>
</info>
</Notify>
to map is: Notify@Info@DeviceID:34020000001320000001,34020000001320000002
*/
//get map key
std::string mkey = "";
for (int i = 0; i < xml_layer ; i++){
if (mkey.empty()) {
mkey = json_key[i];
}else{
mkey = mkey + "@" + json_key[i];
}
}
//set map value
if (!mkey.empty()){
if (json_map.find(mkey) == json_map.end()){
json_map[mkey] = value;
}else{
json_map[mkey] = json_map[mkey] + ","+ value;
}
}
value_start = NULL;
xml_layer--;
} else if (p[0] == '<') { //<Notify> xml item begin flag
//skip <
p +=1;
//<Notify> get Notify
char *s = p;
while (p[0] != '>') {p++;}
std::string key(s, p-s);
if (srs_string_contains(key, "?xml")){
//xml header
xml_header = key;
json_map["XmlHeader"] = xml_header;
}else {
//<DeviceList Num="2"> get DeviceList
std::vector<string> vec = srs_string_split(key, " ");
if (vec.empty()){
return srs_error_new(ERROR_GB28181_SIP_PRASE_FAILED, "prase xml");
}
key = vec.at(0);
//key to map by xml_layer
//<Notify>
// <info>
// </info>
//</Notify>
//json_key[0] = "Notify"
//json_key[1] = "info"
json_key[xml_layer] = key;
xml_layer++;
}
p +=1;
value_start = p;
} else {
p++;
}
}
// std::map<string, string>::iterator it2;
// for (it2 = json_map.begin(); it2 != json_map.end(); ++it2) {
// srs_trace("========%s:%s", it2->first.c_str(), it2->second.c_str());
// }
return srs_success;
}
srs_error_t SrsSipStack::do_parse_request(SrsSipRequest* req, const char* recv_msg)
{
srs_error_t err = srs_success;
std::vector<std::string> header_body = srs_string_split(recv_msg, SRS_RTSP_CRLFCRLF);
if (header_body.empty()){
return srs_error_new(ERROR_GB28181_SIP_PRASE_FAILED, "parse reques message");
}
std::string header = header_body.at(0);
//Must be added SRS_RTSP_CRLFCRLF in order to handle the last line header
header += SRS_RTSP_CRLFCRLF;
std::string body = "";
if (header_body.size() > 1){
@ -296,7 +496,7 @@ srs_error_t SrsSipStack::do_parse_request(SrsSipRequest* req, const char* recv_m
newline_start = p;
if (firstline == ""){
firstline = oneline;
firstline = srs_string_replace(oneline, "\r\n", "");
srs_info("sip: first line=%s", firstline.c_str());
}else{
size_t pos = oneline.find(":");
@ -311,7 +511,7 @@ srs_error_t SrsSipStack::do_parse_request(SrsSipRequest* req, const char* recv_m
if (!strcasecmp(phead, "call-id:")) {
std::vector<std::string> vec_callid = srs_string_split(content, " ");
req->call_id = vec_callid.at(0);
req->call_id = vec_callid.empty() ? "" : vec_callid.at(0);
}
else if (!strcasecmp(phead, "contact:")) {
req->contact = content;
@ -327,8 +527,9 @@ srs_error_t SrsSipStack::do_parse_request(SrsSipRequest* req, const char* recv_m
}
else if (!strcasecmp(phead, "cseq:")) {
std::vector<std::string> vec_seq = srs_string_split(content, " ");
req->seq = strtoul(vec_seq.at(0).c_str(), NULL, 10);
req->method = vec_seq.at(1);
std::string seq = vec_seq.empty() ? "" : vec_seq.at(0);
req->seq = strtoul(seq.c_str(), NULL, 10);
req->method = vec_seq.size() > 0 ? vec_seq.at(1) : "";
}
else if (!strcasecmp(phead, "from:")) {
content = srs_string_replace(content, "sip:", "");
@ -336,6 +537,11 @@ srs_error_t SrsSipStack::do_parse_request(SrsSipRequest* req, const char* recv_m
if (srs_string_contains(content, "tag")) {
req->from_tag = srs_sip_get_param(content.c_str(), "tag");
}
std::vector<std::string> vec = srs_string_split(req->from, "@");
if (vec.size() > 1){
req->from_realm = vec.at(1);
}
}
else if (!strcasecmp(phead, "to:")) {
content = srs_string_replace(content, "sip:", "");
@ -343,9 +549,13 @@ srs_error_t SrsSipStack::do_parse_request(SrsSipRequest* req, const char* recv_m
if (srs_string_contains(content, "tag")) {
req->to_tag = srs_sip_get_param(content.c_str(), "tag");
}
std::vector<std::string> vec = srs_string_split(req->to, "@");
if (vec.size() > 1){
req->to_realm = vec.at(1);
}
}
else if (!strcasecmp(phead, "via:")) {
//std::vector<std::string> vec_via = srs_string_split(content, ";");
req->via = content;
req->branch = srs_sip_get_param(content.c_str(), "branch");
}
@ -377,29 +587,82 @@ srs_error_t SrsSipStack::do_parse_request(SrsSipRequest* req, const char* recv_m
}
std::vector<std::string> method_uri_ver = srs_string_split(firstline, " ");
if (method_uri_ver.empty()) {
return srs_error_new(ERROR_GB28181_SIP_PRASE_FAILED, "parse request firstline is empty");
}
//respone first line text:SIP/2.0 200 OK
if (!strcasecmp(method_uri_ver.at(0).c_str(), "sip/2.0")) {
req->cmdtype = SrsSipCmdRespone;
//req->method= vec_seq.at(1);
req->status = method_uri_ver.at(1);
req->status = method_uri_ver.size() > 0 ? method_uri_ver.at(1) : "";
req->version = method_uri_ver.at(0);
req->uri = req->from;
vector<string> str = srs_string_split(req->to, "@");
req->sip_auth_id = srs_string_replace(str.at(0), "sip:", "");
std::string ss = str.empty() ? "" : str.at(0);
req->sip_auth_id = srs_string_replace(ss, "sip:", "");
}else {//request first line text :MESSAGE sip:34020000002000000001@3402000000 SIP/2.0
req->cmdtype = SrsSipCmdRequest;
req->method= method_uri_ver.at(0);
req->uri = method_uri_ver.at(1);
req->version = method_uri_ver.at(2);
req->uri = method_uri_ver.size() > 0 ? method_uri_ver.at(1) : "";
req->version = method_uri_ver.size() > 1 ? method_uri_ver.at(2) : "";
vector<string> str = srs_string_split(req->from, "@");
req->sip_auth_id = srs_string_replace(str.at(0), "sip:", "");
std::string ss = str.empty() ? "" : str.at(0);
req->sip_auth_id = srs_string_replace(ss, "sip:", "");
}
req->sip_username = req->sip_auth_id;
//Content-Type: Application/MANSCDP+xml
if (!strcasecmp(req->content_type.c_str(),"application/manscdp+xml")){
std::map<std::string, std::string> body_map;
//xml to map
if ((err = parse_xml(body, body_map)) != srs_success) {
return srs_error_wrap(err, "sip parse xml");
};
//Response Cmd
if (body_map.find("Response") != body_map.end()){
std::string cmdtype = body_map["Response@CmdType"];
if (cmdtype == "Catalog"){
//Response@DeviceList@Item@DeviceID:3000001,3000002
std::vector<std::string> vec_device_id = srs_string_split(body_map["Response@DeviceList@Item@DeviceID"], ",");
//Response@DeviceList@Item@Status:ON,OFF
std::vector<std::string> vec_device_status = srs_string_split(body_map["Response@DeviceList@Item@Status"], ",");
//map key:devicd_id value:status
for(int i=0 ; i<vec_device_id.size(); i++){
std::string status = "";
if (vec_device_id.size() > i) {
status = vec_device_status.at(i);
}
req->device_list_map[vec_device_id.at(i)] = status;
}
}else{
//TODO: fixme
srs_trace("sip: Response cmdtype=%s not processed", cmdtype.c_str());
}
} //Notify Cmd
else if (body_map.find("Notify") != body_map.end()){
std::string cmdtype = body_map["Notify@CmdType"];
if (cmdtype == "Keepalive"){
//TODO: ????
std::vector<std::string> vec_device_id = srs_string_split(body_map["Notify@Info@DeviceID"], ",");
for(int i=0; i<vec_device_id.size(); i++){
//req->device_list_map[vec_device_id.at(i)] = "OFF";
}
}else{
//TODO: fixme
srs_trace("sip: Notify cmdtype=%s not processed", cmdtype.c_str());
}
}// end if(body_map)
}//end if (!strcasecmp)
srs_info("sip: method=%s uri=%s version=%s cmdtype=%s",
req->method.c_str(), req->uri.c_str(), req->version.c_str(), req->get_cmdtype_str().c_str());
srs_info("via=%s", req->via.c_str());
@ -445,9 +708,13 @@ std::string SrsSipStack::get_sip_via(SrsSipRequest const *req)
{
std::string via = srs_string_replace(req->via, SRS_SIP_VERSION"/UDP ", "");
std::vector<std::string> vec_via = srs_string_split(via, ";");
std::string ip_port = vec_via.at(0);
std::string ip_port = vec_via.empty() ? "" : vec_via.at(0);
std::vector<std::string> vec_ip_port = srs_string_split(ip_port, ":");
std::string ip = vec_ip_port.empty() ? "" : vec_ip_port.at(0);
std::string port = vec_ip_port.size() > 1 ? vec_ip_port.at(1) : "";
std::string branch, rport, received;
if (req->branch.empty()){
branch = "";
@ -455,8 +722,16 @@ std::string SrsSipStack::get_sip_via(SrsSipRequest const *req)
branch = ";branch=" + req->branch;
}
received = ";received=" + vec_ip_port.at(0);
rport = ";rport=" + vec_ip_port.at(1);
if (!req->peer_ip.empty()){
ip = req->peer_ip;
std::stringstream ss;
ss << req->peer_port;
port = ss.str();
}
received = ";received=" + ip;
rport = ";rport=" + port;
return SRS_SIP_VERSION"/UDP " + ip_port + rport + received + branch;
}
@ -640,10 +915,12 @@ void SrsSipStack::req_invite(stringstream& ss, SrsSipRequest *req, string ip, in
User-Agent: SRS/4.0.4(Leo)
Content-Length: 0
*/
char _ssrc[11];
sprintf(_ssrc, "%010d", ssrc);
std::stringstream sdp;
sdp << "v=0" << SRS_RTSP_CRLF
<< "o=" << req->sip_auth_id << " 0 0 IN IP4 " << ip << SRS_RTSP_CRLF
<< "o=" << req->serial << " 0 0 IN IP4 " << ip << SRS_RTSP_CRLF
<< "s=Play" << SRS_RTSP_CRLF
<< "c=IN IP4 " << ip << SRS_RTSP_CRLF
<< "t=0 0" << SRS_RTSP_CRLF
@ -658,37 +935,44 @@ void SrsSipStack::req_invite(stringstream& ss, SrsSipRequest *req, string ip, in
//<< "a=rtpmap:99 H265/90000" << SRS_RTSP_CRLF
//<< "a=streamMode:MAIN\r\n"
//<< "a=filesize:0\r\n"
<< "y=" << ssrc << SRS_RTSP_CRLF;
<< "y=" << _ssrc << SRS_RTSP_CRLF;
int rand = srs_sip_random(1000, 9999);
std::stringstream from, to, uri, branch, from_tag;
std::stringstream from, to, uri;
//"INVITE sip:34020000001320000001@3402000000 SIP/2.0\r\n
uri << "sip:" << req->sip_auth_id << "@" << req->realm;
uri << "sip:" << req->chid << "@" << req->realm;
//From: <sip:34020000002000000001@%s:%s>;tag=500485%d\r\n
from << req->serial << "@" << req->host << ":" << req->host_port;
to << req->sip_auth_id << "@" << req->realm;
from << req->serial << "@" << req->realm;
to << req->chid << "@" << req->realm;
req->from = from.str();
req->to = to.str();
if (!req->to_realm.empty()){
req->to = req->chid + "@" + req->to_realm;
}
if (!req->from_realm.empty()){
req->from = req->serial + "@" + req->from_realm;
}
req->uri = uri.str();
branch << "z9hG4bK3420" << rand;
from_tag << "51235" << rand;
req->branch = branch.str();
req->from_tag = from_tag.str();
req->call_id = srs_sip_generate_call_id();
req->branch = srs_sip_generate_branch();
req->from_tag = srs_sip_generate_from_tag();
ss << "INVITE " << req->uri << " " << SRS_SIP_VERSION << SRS_RTSP_CRLF
<< "Via: " << SRS_SIP_VERSION << "/UDP "<< req->host << ":" << req->host_port << ";rport;branch=" << req->branch << SRS_RTSP_CRLF
<< "From: " << get_sip_from(req) << SRS_RTSP_CRLF
<< "To: " << get_sip_to(req) << SRS_RTSP_CRLF
<< "Call-ID: 20000" << rand <<SRS_RTSP_CRLF
<< "CSeq: 20 INVITE" << SRS_RTSP_CRLF
<< "Call-ID: " << req->call_id <<SRS_RTSP_CRLF
<< "CSeq: " << req->seq << " INVITE" << SRS_RTSP_CRLF
<< "Content-Type: Application/SDP" << SRS_RTSP_CRLF
<< "Contact: <sip:" << req->to << ">" << SRS_RTSP_CRLF
<< "Max-Forwards: 70" << " \r\n"
<< "Max-Forwards: 70" << SRS_RTSP_CRLF
<< "User-Agent: " << SRS_SIP_USER_AGENT <<SRS_RTSP_CRLF
<< "Subject: "<< req->sip_auth_id << ":" << ssrc << "," << req->serial << ":0" << SRS_RTSP_CRLF
<< "Subject: "<< req->chid << ":" << _ssrc << "," << req->serial << ":0" << SRS_RTSP_CRLF
<< "Content-Length: " << sdp.str().length() << SRS_RTSP_CRLFCRLF
<< sdp.str();
}
@ -703,7 +987,7 @@ void SrsSipStack::req_401_unauthorized(std::stringstream& ss, SrsSipRequest *req
To: <sip:34020000001320000004@192.168.137.92:5061>;tag=102092689
CSeq: 1 REGISTER
Call-ID: 1650345118
User-Agent: LiveGBS v200228
User-Agent: SRS/4.0.4(Leo)
Contact: <sip:34020000002000000001@192.168.1.23:15060>
Content-Length: 0
WWW-Authenticate: Digest realm="3402000000",qop="auth",nonce="f1da98bd160f3e2efe954c6eedf5f75a"
@ -737,10 +1021,10 @@ void SrsSipStack::req_ack(std::stringstream& ss, SrsSipRequest *req){
Content-Length: 0
*/
ss << "ACK " << "sip:" << req->sip_auth_id << "@" << req->realm << " "<< SRS_SIP_VERSION << SRS_RTSP_CRLF
ss << "ACK " << "sip:" << req->chid << "@" << req->realm << " "<< SRS_SIP_VERSION << SRS_RTSP_CRLF
<< "Via: " << SRS_SIP_VERSION << "/UDP " << req->host << ":" << req->host_port << ";rport;branch=" << req->branch << SRS_RTSP_CRLF
<< "From: <sip:" << req->serial << "@" << req->host + ":" << req->host_port << ">;tag=" << req->from_tag << SRS_RTSP_CRLF
<< "To: <sip:"<< req->sip_auth_id << "@" << req->realm << ">\r\n"
<< "From: " << get_sip_from(req) << SRS_RTSP_CRLF
<< "To: "<< get_sip_to(req) << SRS_RTSP_CRLF
<< "Call-ID: " << req->call_id << SRS_RTSP_CRLF
<< "CSeq: " << req->seq << " ACK"<< SRS_RTSP_CRLF
<< "Max-Forwards: 70" << SRS_RTSP_CRLF
@ -775,15 +1059,23 @@ void SrsSipStack::req_bye(std::stringstream& ss, SrsSipRequest *req)
*/
std::stringstream from, to, uri;
uri << "sip:" << req->sip_auth_id << "@" << req->realm;
uri << "sip:" << req->chid << "@" << req->realm;
from << req->serial << "@" << req->realm;
to << req->sip_auth_id << "@" << req->realm;
to << req->chid << "@" << req->realm;
req->from = from.str();
req->to = to.str();
if (!req->to_realm.empty()){
req->to = req->chid + "@" + req->to_realm;
}
if (!req->from_realm.empty()){
req->from = req->serial + "@" + req->from_realm;
}
req->uri = uri.str();
int seq = srs_sip_random(22, 99);
ss << "BYE " << req->uri << " "<< SRS_SIP_VERSION << SRS_RTSP_CRLF
//<< "Via: "<< SRS_SIP_VERSION << "/UDP "<< req->host << ":" << req->host_port << ";rport" << branch << SRS_RTSP_CRLF
<< "Via: " << SRS_SIP_VERSION << "/UDP " << req->host << ":" << req->host_port << ";rport;branch=" << req->branch << SRS_RTSP_CRLF
@ -791,12 +1083,243 @@ void SrsSipStack::req_bye(std::stringstream& ss, SrsSipRequest *req)
<< "To: " << get_sip_to(req) << SRS_RTSP_CRLF
//bye callid is inivte callid
<< "Call-ID: " << req->call_id << SRS_RTSP_CRLF
<< "CSeq: "<< seq <<" BYE" << SRS_RTSP_CRLF
<< "CSeq: "<< req->seq <<" BYE" << SRS_RTSP_CRLF
<< "Max-Forwards: 70" << SRS_RTSP_CRLF
<< "User-Agent: " << SRS_SIP_USER_AGENT << SRS_RTSP_CRLF
<< "Content-Length: 0" << SRS_RTSP_CRLFCRLF;
}
void SrsSipStack::req_query_catalog(std::stringstream& ss, SrsSipRequest *req)
{
/*
//request: sip-agent <----MESSAGE Query Catalog--- sip-server
MESSAGE sip:34020000001110000001@192.168.1.21:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.1.17:5060;rport;branch=z9hG4bK563315752
From: <sip:34020000001110000001@3402000000>;tag=387315752
To: <sip:34020000001110000001@192.168.1.21:5060>
Call-ID: 728315752
CSeq: 32 MESSAGE
Content-Type: Application/MANSCDP+xml
Max-Forwards: 70
User-Agent: SRS/4.0.20(Leo)
Content-Length: 162
<?xml version="1.0" encoding="UTF-8"?>
<Query>
<CmdType>Catalog</CmdType>
<SN>419315752</SN>
<DeviceID>34020000001110000001</DeviceID>
</Query>
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.1.17:5060;rport=5060;branch=z9hG4bK563315752
From: <sip:34020000001110000001@3402000000>;tag=387315752
To: <sip:34020000001110000001@192.168.1.21:5060>;tag=1420696981
Call-ID: 728315752
CSeq: 32 MESSAGE
User-Agent: Embedded Net DVR/NVR/DVS
Content-Length: 0
//response: sip-agent ----MESSAGE Query Catalog---> sip-server
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.1.17:5060;rport=5060;received=192.168.1.17;branch=z9hG4bK563315752
From: <sip:34020000001110000001@3402000000>;tag=387315752
To: <sip:34020000001110000001@192.168.1.21:5060>;tag=1420696981
CSeq: 32 MESSAGE
Call-ID: 728315752
User-Agent: SRS/4.0.20(Leo)
Content-Length: 0
//request: sip-agent ----MESSAGE Response Catalog---> sip-server
MESSAGE sip:34020000001110000001@3402000000.spvmn.cn SIP/2.0
Via: SIP/2.0/UDP 192.168.1.21:5060;rport;branch=z9hG4bK1681502633
From: <sip:34020000001110000001@3402000000.spvmn.cn>;tag=1194168247
To: <sip:34020000001110000001@3402000000.spvmn.cn>
Call-ID: 685380150
CSeq: 20 MESSAGE
Content-Type: Application/MANSCDP+xml
Max-Forwards: 70
User-Agent: Embedded Net DVR/NVR/DVS
Content-Length: 909
<?xml version="1.0" encoding="gb2312"?>
<Response>
<CmdType>Catalog</CmdType>
<SN>419315752</SN>
<DeviceID>34020000001110000001</DeviceID>
<SumNum>8</SumNum>
<DeviceList Num="2">
<Item>
<DeviceID>34020000001320000001</DeviceID>
<Name>Camera 01</Name>
<Manufacturer>Manufacturer</Manufacturer>
<Model>Camera</Model>
<Owner>Owner</Owner>
<CivilCode>CivilCode</CivilCode>
<Address>192.168.254.18</Address>
<Parental>0</Parental>
<SafetyWay>0</SafetyWay>
<RegisterWay>1</RegisterWay>
<Secrecy>0</Secrecy>
<Status>ON</Status>
</Item>
<Item>
<DeviceID>34020000001320000002</DeviceID>
<Name>IPCamera 02</Name>
<Manufacturer>Manufacturer</Manufacturer>
<Model>Camera</Model>
<Owner>Owner</Owner>
<CivilCode>CivilCode</CivilCode>
<Address>192.168.254.14</Address>
<Parental>0</Parental>
<SafetyWay>0</SafetyWay>
<RegisterWay>1</RegisterWay>
<Secrecy>0</Secrecy>
<Status>OFF</Status>
</Item>
</DeviceList>
</Response>
*/
std::stringstream xml;
std::string xmlbody;
xml << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << SRS_RTSP_CRLF
<< "<Query>" << SRS_RTSP_CRLF
<< "<CmdType>Catalog</CmdType>" << SRS_RTSP_CRLF
<< "<SN>" << srs_sip_generate_sn() << "</SN>" << SRS_RTSP_CRLF
<< "<DeviceID>" << req->sip_auth_id << "</DeviceID>" << SRS_RTSP_CRLF
<< "</Query>" << SRS_RTSP_CRLF;
xmlbody = xml.str();
std::stringstream from, to, uri;
//"INVITE sip:34020000001320000001@3402000000 SIP/2.0\r\n
uri << "sip:" << req->sip_auth_id << "@" << req->realm;
//From: <sip:34020000002000000001@%s:%s>;tag=500485%d\r\n
from << req->serial << "@" << req->host << ":" << req->host_port;
to << req->sip_auth_id << "@" << req->realm;
req->from = from.str();
req->to = to.str();
req->uri = uri.str();
req->call_id = srs_sip_generate_call_id();
req->branch = srs_sip_generate_branch();
req->from_tag = srs_sip_generate_from_tag();
ss << "MESSAGE " << req->uri << " " << SRS_SIP_VERSION << SRS_RTSP_CRLF
<< "Via: " << SRS_SIP_VERSION << "/UDP "<< req->host << ":" << req->host_port << ";rport;branch=" << req->branch << SRS_RTSP_CRLF
<< "From: " << get_sip_from(req) << SRS_RTSP_CRLF
<< "To: " << get_sip_to(req) << SRS_RTSP_CRLF
<< "Call-ID: " << req->call_id << SRS_RTSP_CRLF
<< "CSeq: " << req->seq << " MESSAGE" << SRS_RTSP_CRLF
<< "Content-Type: Application/MANSCDP+xml" << SRS_RTSP_CRLF
<< "Max-Forwards: 70" << SRS_RTSP_CRLF
<< "User-Agent: " << SRS_SIP_USER_AGENT << SRS_RTSP_CRLF
<< "Content-Length: " << xmlbody.length() << SRS_RTSP_CRLFCRLF
<< xmlbody;
}
void SrsSipStack::req_ptz(std::stringstream& ss, SrsSipRequest *req, uint8_t cmd, uint8_t speed, int priority)
{
/*
<?xml version="1.0"?>
<Control>
<CmdType>DeviceControl</CmdType>
<SN>11</SN>
<DeviceID>34020000001310000053</DeviceID>
<PTZCmd>A50F01021F0000D6</PTZCmd>
</Control>
*/
uint8_t ptz_cmd[8] = {0};
ptz_cmd[0] = SRS_SIP_PTZ_START;
ptz_cmd[1] = 0x0F;
ptz_cmd[2] = 0x01;
ptz_cmd[3] = cmd;
switch(cmd){
case SrsSipPtzCmdStop: // = 0x00
ptz_cmd[4] = 0;
ptz_cmd[5] = 0;
ptz_cmd[6] = 0;
break;
case SrsSipPtzCmdRight: // = 0x01,
case SrsSipPtzCmdLeft: // = 0x02,
ptz_cmd[4] = speed;
break;
case SrsSipPtzCmdDown: // = 0x04,
case SrsSipPtzCmdUp: // = 0x08,
ptz_cmd[5] = speed;
break;
case SrsSipPtzCmdZoomOut: // = 0x10,
case SrsSipPtzCmdZoomIn: // = 0x20
ptz_cmd[6] = (speed & 0x0F) << 4;
break;
default:
return;
}
uint32_t check = 0;
for (int i = 0; i < 7; i++){
check += ptz_cmd[i];
}
ptz_cmd[7] = (uint8_t)(check % 256);
std::stringstream ss_ptzcmd;
for (int i = 0; i < 8; i++){
char hex_cmd[3] = {0};
sprintf(hex_cmd, "%02X", ptz_cmd[i]);
ss_ptzcmd << hex_cmd;
}
std::stringstream xml;
std::string xmlbody;
xml << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << SRS_RTSP_CRLF
<< "<Control>" << SRS_RTSP_CRLF
<< "<CmdType>DeviceControl</CmdType>" << SRS_RTSP_CRLF
<< "<SN>" << srs_sip_generate_sn() << "</SN>" << SRS_RTSP_CRLF
<< "<DeviceID>" << req->sip_auth_id << "</DeviceID>" << SRS_RTSP_CRLF
<< "<PTZCmd>" << ss_ptzcmd.str() << "</PTZCmd>" << SRS_RTSP_CRLF
<< "<Info>" << SRS_RTSP_CRLF
<< "<ControlPriority>" << priority << "</ControlPriority>" << SRS_RTSP_CRLF
<< "</Info>" << SRS_RTSP_CRLF
<< "</Control>" << SRS_RTSP_CRLF;
xmlbody = xml.str();
std::stringstream from, to, uri, call_id;
//"INVITE sip:34020000001320000001@3402000000 SIP/2.0\r\n
uri << "sip:" << req->sip_auth_id << "@" << req->realm;
//From: <sip:34020000002000000001@%s:%s>;tag=500485%d\r\n
from << req->serial << "@" << req->host << ":" << req->host_port;
to << req->sip_auth_id << "@" << req->realm;
req->from = from.str();
req->to = to.str();
req->uri = uri.str();
req->call_id = srs_sip_generate_call_id();
req->branch = srs_sip_generate_branch();
req->from_tag = srs_sip_generate_from_tag();
ss << "MESSAGE " << req->uri << " "<< SRS_SIP_VERSION << SRS_RTSP_CRLF
//<< "Via: "<< SRS_SIP_VERSION << "/UDP "<< req->host << ":" << req->host_port << ";rport" << branch << SRS_RTSP_CRLF
<< "Via: " << SRS_SIP_VERSION << "/UDP " << req->host << ":" << req->host_port << ";rport;branch=" << req->branch << SRS_RTSP_CRLF
<< "From: " << get_sip_from(req) << SRS_RTSP_CRLF
<< "To: " << get_sip_to(req) << SRS_RTSP_CRLF
<< "Call-ID: " << req->call_id << SRS_RTSP_CRLF
<< "CSeq: "<< req->seq <<" MESSAGE" << SRS_RTSP_CRLF
<< "Content-Type: Application/MANSCDP+xml" << SRS_RTSP_CRLF
<< "Max-Forwards: 70" << SRS_RTSP_CRLF
<< "User-Agent: " << SRS_SIP_USER_AGENT << SRS_RTSP_CRLF
<< "Content-Length: " << xmlbody.length() << SRS_RTSP_CRLFCRLF
<< xmlbody;
}
#endif

View file

@ -30,6 +30,7 @@
#include <string>
#include <sstream>
#include <map>
#include <srs_kernel_consts.hpp>
#include <srs_rtsp_stack.hpp>
@ -49,12 +50,24 @@ class SrsAudioFrame;
#define SRS_SIP_VERSION "SIP/2.0"
#define SRS_SIP_USER_AGENT RTMP_SIG_SRS_SERVER
#define SRS_SIP_PTZ_START 0xA5
enum SrsSipCmdType{
SrsSipCmdRequest=0,
SrsSipCmdRespone=1
};
enum SrsSipPtzCmdType{
SrsSipPtzCmdStop = 0x00,
SrsSipPtzCmdRight = 0x01,
SrsSipPtzCmdLeft = 0x02,
SrsSipPtzCmdDown = 0x04,
SrsSipPtzCmdUp = 0x08,
SrsSipPtzCmdZoomIn = 0x10,
SrsSipPtzCmdZoomOut = 0x20
};
std::string srs_sip_get_utc_date();
class SrsSipRequest
@ -88,6 +101,11 @@ public:
std::string www_authenticate;
std::string authorization;
std::string chid;
std::map<std::string, std::string> xml_body_map;
std::map<std::string, std::string> device_list_map;
public:
std::string serial;
std::string realm;
@ -100,6 +118,9 @@ public:
int host_port;
SrsSipCmdType cmdtype;
std::string from_realm;
std::string to_realm;
public:
SrsRtspSdp* sdp;
SrsRtspTransport* transport;
@ -131,6 +152,7 @@ public:
virtual srs_error_t parse_request(SrsSipRequest** preq, const char *recv_msg, int nb_buf);
protected:
virtual srs_error_t do_parse_request(SrsSipRequest* req, const char *recv_msg);
virtual srs_error_t parse_xml(std::string xml_msg, std::map<std::string, std::string> &json_map);
private:
//response from
@ -146,10 +168,13 @@ public:
virtual void resp_keepalive(std::stringstream& ss, SrsSipRequest *req);
//request: request sent by the sip-server, wait for sip-agent response
virtual void req_invite(std::stringstream& ss, SrsSipRequest *req, std::string ip, int port, uint32_t ssrc);
virtual void req_invite(std::stringstream& ss, SrsSipRequest *req, std::string ip,
int port, uint32_t ssrc);
virtual void req_ack(std::stringstream& ss, SrsSipRequest *req);
virtual void req_bye(std::stringstream& ss, SrsSipRequest *req);
virtual void req_401_unauthorized(std::stringstream& ss, SrsSipRequest *req);
virtual void req_query_catalog(std::stringstream& ss, SrsSipRequest *req);
virtual void req_ptz(std::stringstream& ss, SrsSipRequest *req, uint8_t cmd, uint8_t speed, int priority);
};