修复 rtsp 推流一个低级错误

This commit is contained in:
langhuihui
2024-07-29 20:41:22 +08:00
parent bc17976582
commit 17241550bb
12 changed files with 72 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
global:
# loglevel: trace
loglevel: trace
# enableauth: true
# tcp:
# listenaddr: :50051
@@ -22,7 +22,7 @@ rtmp:
# subaudio: false
pull:
pullonsub:
live/pull: rtmp://localhost/live/test
# live/pull: rtmp://localhost/live/test
# flv:
# pull:
# pullonstart:

View File

@@ -3,7 +3,6 @@ package main
import (
"context"
"flag"
"m7s.live/m7s/v5"
_ "m7s.live/m7s/v5/plugin/console"
_ "m7s.live/m7s/v5/plugin/debug"

View File

@@ -100,6 +100,19 @@ func (c *Collection[K, T]) Get(key K) (item T, ok bool) {
return
}
func (c *Collection[K, T]) Find(f func(T) bool) (item T, ok bool) {
if c.L != nil {
c.L.RLock()
defer c.L.RUnlock()
}
for _, item = range c.Items {
if f(item) {
return item, true
}
}
return
}
func (c *Collection[K, T]) GetKey() K {
return c.Items[0].GetKey()
}

View File

@@ -2,6 +2,7 @@ package m7s
import (
"context"
myip "github.com/husanpao/ip"
"gorm.io/gorm"
"log/slog"
"m7s.live/m7s/v5/pkg/db"
@@ -209,6 +210,20 @@ func (p *Plugin) GetCommonConf() *config.Common {
return &p.config
}
func (p *Plugin) GetPublicIP(netcardIP string) string {
if p.config.PublicIP != "" {
return p.config.PublicIP
}
if publicIP, ok := Routes[netcardIP]; ok { //根据网卡ip获取对应的公网ip
return publicIP
}
localIp := myip.InternalIPv4()
if publicIP, ok := Routes[localIp]; ok {
return publicIP
}
return localIp
}
func (p *Plugin) settingPath() string {
return filepath.Join(p.Server.SettingDir, strings.ToLower(p.Meta.Name)+".yaml")
}

View File

@@ -51,6 +51,7 @@ func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28
if d.Status == DeviceRecoverStatus {
d.Status = DeviceOnlineStatus
}
d.Debug("OnMessage", "cmdType", msg.CmdType, "body", string(req.Body()))
switch msg.CmdType {
case "Keepalive":
d.LastKeepaliveAt = time.Now()
@@ -95,13 +96,13 @@ func (d *Device) eventLoop(gb *GB28181Plugin) {
if err != nil {
d.Error("catalog", "err", err)
} else {
d.Debug("catalog", "response", response)
d.Debug("catalog", "response", response.Short())
}
response, err = d.queryDeviceInfo(send)
if err != nil {
d.Error("deviceInfo", "err", err)
} else {
d.Debug("deviceInfo", "response", response)
d.Debug("deviceInfo", "response", response.Short())
}
subTick := time.NewTicker(time.Second * 3600)
defer subTick.Stop()
@@ -114,13 +115,13 @@ func (d *Device) eventLoop(gb *GB28181Plugin) {
if err != nil {
d.Error("subCatalog", "err", err)
} else {
d.Debug("subCatalog", "response", response)
d.Debug("subCatalog", "response", response.Short())
}
response, err = d.subscribePosition(int(gb.Position.Interval/time.Second), send)
if err != nil {
d.Error("subPosition", "err", err)
} else {
d.Debug("subPosition", "response", response)
d.Debug("subPosition", "response", response.Short())
}
case <-catalogTick.C:
if time.Since(d.LastKeepaliveAt) > time.Second*3600 {
@@ -131,7 +132,7 @@ func (d *Device) eventLoop(gb *GB28181Plugin) {
if err != nil {
d.Error("catalog", "err", err)
} else {
d.Debug("catalog", "response", response)
d.Debug("catalog", "response", response.Short())
}
case event, ok := <-d.eventChan:
if !ok {

View File

@@ -1,6 +1,7 @@
package plugin_gb28181
import (
"errors"
"fmt"
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
@@ -19,6 +20,10 @@ type Dialog struct {
session *sipgo.DialogClientSession
}
func (d *Dialog) GetCallID() string {
return d.session.InviteRequest.CallID().Value()
}
func (d *Dialog) Connect(p *m7s.Client) (err error) {
sss := strings.Split(p.RemoteURL, "/")
deviceId, channelId := sss[0], sss[1]
@@ -80,9 +85,9 @@ func (d *Dialog) Pull(p *m7s.Puller) (err error) {
if err != nil {
return
}
inviteResponseBody := d.session.InviteResponse.Body()
d.gb.Info("InviteResponse: %s", inviteResponseBody)
ds := strings.Split(string(inviteResponseBody), "\r\n")
inviteResponseBody := string(d.session.InviteResponse.Body())
d.Info("inviteResponse", "body", inviteResponseBody)
ds := strings.Split(inviteResponseBody, "\r\n")
for _, l := range ds {
if ls := strings.Split(l, "="); len(ls) > 1 {
if ls[0] == "y" && len(ls[1]) > 0 {
@@ -112,3 +117,10 @@ func (d *Dialog) Pull(p *m7s.Puller) (err error) {
func (d *Dialog) GetKey() uint32 {
return d.SSRC
}
func (d *Dialog) Bye() {
if d.Receiver != nil && d.Receiver.Publisher != nil {
d.Receiver.Publisher.Stop(errors.New("bye"))
}
d.session.Close()
}

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
myip "github.com/husanpao/ip"
"github.com/icholy/digest"
"github.com/pion/rtp"
"github.com/rs/zerolog"
@@ -55,7 +54,7 @@ type GB28181Plugin struct {
tcpPorts chan uint16
}
var _ = m7s.InstallPlugin[GB28181Plugin](pb.RegisterApiHandler, pb.Api_ServiceDesc)
var _ = m7s.InstallPlugin[GB28181Plugin](pb.RegisterApiHandler, &pb.Api_ServiceDesc)
func init() {
sip.SIPDebug = true
@@ -67,6 +66,7 @@ func (gb *GB28181Plugin) OnInit() (err error) {
gb.server, _ = sipgo.NewServer(gb.ua, sipgo.WithServerLogger(logger)) // Creating server handle for ua
gb.server.OnRegister(gb.OnRegister)
gb.server.OnMessage(gb.OnMessage)
gb.server.OnBye(gb.OnBye)
gb.devices.L = new(sync.RWMutex)
if gb.MediaPort.Valid() {
@@ -177,8 +177,8 @@ func (gb *GB28181Plugin) OnRegister(req *sip.Request, tx sip.ServerTransaction)
err = tx.Respond(sip.NewResponseFromRequest(req, sip.StatusUnauthorized, "Unathorized", nil))
return
}
err = tx.Respond(sip.NewResponseFromRequest(req, sip.StatusOK, "OK", nil))
}
err = tx.Respond(sip.NewResponseFromRequest(req, sip.StatusOK, "OK", nil))
if isUnregister {
if d, ok := gb.devices.Get(id); ok {
close(d.eventChan)
@@ -233,16 +233,7 @@ func (gb *GB28181Plugin) StoreDevice(id string, req *sip.Request) (d *Device) {
from := req.From()
source := req.Source()
servIp := req.Recipient.Host
mediaIP := myip.InternalIPv4()
var ok bool
//如果用户配置过则使用配置的
if publicIP := gb.GetCommonConf().PublicIP; publicIP != "" {
mediaIP = publicIP
} else if publicIP, ok = m7s.Routes[servIp]; ok { //根据网卡ip获取对应的公网ip
mediaIP = publicIP
} else if publicIP, ok = m7s.Routes[mediaIP]; ok {
mediaIP = publicIP
}
mediaIP := gb.GetPublicIP(servIp)
//如果相等,则服务器是内网通道.海康摄像头不支持...自动获取
//if strings.LastIndex(deviceIp, ".") != -1 && strings.LastIndex(servIp, ".") != -1 {
// if servIp[0:strings.LastIndex(servIp, ".")] == deviceIp[0:strings.LastIndex(deviceIp, ".")] || mediaIP == "" {
@@ -309,3 +300,12 @@ func (gb *GB28181Plugin) OnTCPConnect(conn *net.TCPConn) {
close(theDialog.FeedChan)
}
}
func (gb *GB28181Plugin) OnBye(req *sip.Request, tx sip.ServerTransaction) {
if dialog, ok := gb.dialogs.Find(func(d *Dialog) bool {
return d.GetCallID() == req.CallID().Value()
}); ok {
gb.Warn("OnBye", "dialog", dialog.GetCallID())
dialog.Bye()
}
}

View File

@@ -22,13 +22,13 @@ message Channel {
string Model =5;
string Owner =6;
string CivilCode = 7;
string Address = 8;
string Address = 8;
int32 Port = 9;
int32 Parental = 10;
int32 SafetyWay = 11;
int32 RegisterWay = 12;
int32 Secrecy =13;
string Status =14;
string Status =14;
google.protobuf.Timestamp gpsTime=15;
string Longitude = 16;
string Latitude = 17;

View File

@@ -1 +0,0 @@
package gb28181

View File

@@ -117,7 +117,7 @@
<div class="input">
<span>协议切换:</span>
<select id="protocol">
<option value="hdl">hdl(http-flv)</option>
<option value="flv">http-flv</option>
<option value="ws-flv">ws-flv</option>
<option value="ws-raw">jessica(ws-raw)</option>
<option value="ws-h265">ws-h265</option>
@@ -351,17 +351,17 @@
const query = new URLSearchParams(location.search);
const isHttps = location.protocol.startsWith("https");
const type = query.get('type') || 'hdl';
const type = query.get('type') || 'flv';
$protocol.value = type;
let pluginName = type;
let ext = "." + type;
switch (type) {
case 'hdl':
case 'flv':
ext = '.flv';
break;
case 'ws-flv':
ext = '.flv';
pluginName = 'jessica';
pluginName = 'flv';
break;
case 'ws-raw':
ext = "";

View File

@@ -77,7 +77,7 @@ func (r *Video) Parse(t *AVTrack) (err error) {
if sps, err = base64.StdEncoding.DecodeString(sprops[0]); err != nil {
return
}
if pps, err = base64.StdEncoding.DecodeString(sprops[1]); err == nil {
if pps, err = base64.StdEncoding.DecodeString(sprops[1]); err != nil {
return
}
}

View File

@@ -91,11 +91,11 @@ type rawconfig = map[string]map[string]any
func init() {
for k, v := range myip.LocalAndInternalIPs() {
Routes[k] = v
fmt.Println(k, v)
if lastdot := strings.LastIndex(k, "."); lastdot >= 0 {
Routes[k[0:lastdot]] = k
}
}
slog.Info("init", "routes", Routes)
}
func (s *Server) Run(ctx context.Context, conf any) (err error) {