Skip to content

Commit

Permalink
增加HTTP API接口,获取服务信息
Browse files Browse the repository at this point in the history
  • Loading branch information
q191201771 committed Oct 11, 2020
1 parent d7df4d9 commit f2fc9a7
Show file tree
Hide file tree
Showing 22 changed files with 509 additions and 122 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<a title="codeline" target="_blank" href="https://github.com/q191201771/lal"><img src="https://sloc.xyz/github/q191201771/lal/?category=code"></a>
<a title="license" target="_blank" href="https://github.com/q191201771/lal/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square"></a>
<a title="lastcommit" target="_blank" href="https://github.com/q191201771/lal/commits/master"><img src="https://img.shields.io/github/commit-activity/m/q191201771/lal.svg?style=flat-square"></a>
<a title="commitactivity" target="_blank" href="https://github.com/q191201771/lal/graphs/commit-activity"><img src="https://img.shields.io/github/last-commit/q191201771/lal.svg?style=flat-square"></a>
<br>
<a title="pr" target="_blank" href="https://github.com/q191201771/lal/pulls"><img src="https://img.shields.io/github/issues-pr-closed/q191201771/lal.svg?style=flat-square&color=FF9966"></a>
<a title="hits" target="_blank" href="https://github.com/q191201771/lal"><img src="https://hits.b3log.org/q191201771/lal.svg?style=flat-square"></a>
Expand Down
8 changes: 6 additions & 2 deletions conf/edge.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"httpflv": {
"enable": true,
"sub_listen_addr": ":8090",
"enable_https": true,
"enable_https": false,
"https_addr": ":4443",
"https_cert_file": "./conf/cert.pem",
"https_key_file": "./conf/key.pem",
Expand Down Expand Up @@ -38,9 +38,13 @@
"enable": true,
"addr": "127.0.0.1:19350"
},
"http_api": {
"enable": true,
"addr": ":8093"
},
"pprof": {
"enable": false,
"addr": ":10002"
"addr": ":10011"
},
"log": {
"level": 1,
Expand Down
4 changes: 4 additions & 0 deletions conf/lalserver.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"enable": false,
"addr": ""
},
"http_api": {
"enable": true,
"addr": ":8083"
},
"pprof": {
"enable": true,
"addr": ":10001"
Expand Down
4 changes: 4 additions & 0 deletions conf/lalserver.conf.json.brief
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"enable": false, // 是否开启回源拉流功能,开启后,当自身接收到拉流请求,而流不存在时,会从其他服务器拉取这个流到本地
"addr": "" // 回源拉流的地址。格式举例 "127.0.0.1:19351"
},
"http_api": {
"enable": true, // 是否开启HTTP API接口
"addr": ":8083" // 监听地址
},
"pprof": {
"enable": true, // 是否开启Go pprof web服务的监听
"addr": ":10001" // Go pprof web地址
Expand Down
4 changes: 4 additions & 0 deletions conf/lalserver.conf.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"enable": false,
"addr": ""
},
"http_api": {
"enable": true,
"addr": ":8083"
},
"pprof": {
"enable": true,
"addr": ":10001"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/q191201771/lal

go 1.12

require github.com/q191201771/naza v0.15.0
require github.com/q191201771/naza v0.15.1
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/q191201771/naza v0.15.0 h1:HFyRrluqZhpnBu6YQ1soIk6cR9P8G/9sDMFLBhTTBRc=
github.com/q191201771/naza v0.15.0/go.mod h1:SE14GBGO9mAn6JZl3NlfWGtNOT7xQjxOG7f3YOdBThM=
github.com/q191201771/naza v0.15.1 h1:y9D7jbzHeD883PqBZTln+O47E40dFoRlQUrWYOA5GoM=
github.com/q191201771/naza v0.15.1/go.mod h1:SE14GBGO9mAn6JZl3NlfWGtNOT7xQjxOG7f3YOdBThM=
1 change: 1 addition & 0 deletions pkg/avc/avc.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ func CaptureAVCC2AnnexB(w io.Writer, payload []byte) error {
return nil
}

// TODO chef: hevc中,ctx作为参数传入,这里考虑统一一下
// 尝试解析SPS所有字段,实验中,请勿直接使用该函数
func ParseSPS(payload []byte) (Context, error) {
var sps SPS
Expand Down
71 changes: 71 additions & 0 deletions pkg/base/stat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2020, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)

package base

const (
// StatGroup.AudioCodec
AudioCodecAAC = "AAC"

// StatGroup.VideoCodec
VideoCodecAVC = "H264"
VideoCodecHEVC = "H265"

// StatSession.Protocol
ProtocolRTMP = "RTMP"
ProtocolRTSP = "RTSP"
ProtocolHTTPFLV = "HTTP-FLV"
ProtocolHTTPTS = "HTTP-TS"
)

type StatGroup struct {
StreamName string `json:"stream_name"`
AudioCodec string `json:"audio_codec"`
VideoCodec string `json:"video_codec"`
VideoWidth int `json:"video_width"`
VideoHeight int `json:"video_height"`
StatPub StatPub `json:"pub"`
StatSubs []StatSub `json:"subs"`
}

type StatPub struct {
StatSession
}

type StatSub struct {
StatSession
}

type StatSession struct {
Protocol string `json:"protocol"`
StartTime string `json:"start_time"`
RemoteAddr string `json:"remote_addr"`
ReadBytesSum uint64 `json:"read_bytes_sum"`
WroteBytesSum uint64 `json:"wrote_bytes_sum"`
Bitrate int `json:"bitrate"`
}

func StatSession2Pub(ss StatSession) (ret StatPub) {
ret.Protocol = ss.Protocol
ret.StartTime = ss.StartTime
ret.RemoteAddr = ss.RemoteAddr
ret.ReadBytesSum = ss.ReadBytesSum
ret.WroteBytesSum = ss.WroteBytesSum
ret.Bitrate = ss.Bitrate
return
}

func StatSession2Sub(ss StatSession) (ret StatSub) {
ret.Protocol = ss.Protocol
ret.StartTime = ss.StartTime
ret.RemoteAddr = ss.RemoteAddr
ret.ReadBytesSum = ss.ReadBytesSum
ret.WroteBytesSum = ss.WroteBytesSum
ret.Bitrate = ss.Bitrate
return
}
18 changes: 9 additions & 9 deletions pkg/httpflv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ type ServerConfig struct {
}

type Server struct {
obs ServerObserver
config ServerConfig
ln net.Listener
httpsLn net.Listener
observer ServerObserver
config ServerConfig
ln net.Listener
httpsLn net.Listener
}

// TODO chef: 监听太难看了,考虑直接传入Listener对象,或直接路由进来,使得不同server可以共用端口

func NewServer(obs ServerObserver, config ServerConfig) *Server {
func NewServer(observer ServerObserver, config ServerConfig) *Server {
return &Server{
obs: obs,
config: config,
observer: observer,
config: config,
}
}

Expand Down Expand Up @@ -133,11 +133,11 @@ func (server *Server) handleConnect(conn net.Conn) {
}
nazalog.Debugf("[%s] < read http request. uri=%s", session.UniqueKey, session.URI)

if !server.obs.OnNewHTTPFLVSubSession(session) {
if !server.observer.OnNewHTTPFLVSubSession(session) {
session.Dispose()
}

err := session.RunLoop()
nazalog.Debugf("[%s] httpflv sub session loop done. err=%v", session.UniqueKey, err)
server.obs.OnDelHTTPFLVSubSession(session)
server.observer.OnDelHTTPFLVSubSession(session)
}
26 changes: 25 additions & 1 deletion pkg/httpflv/server_sub_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ type SubSession struct {

IsFresh bool

conn connection.Connection
conn connection.Connection
prevConnStat connection.Stat
stat base.StatSub
}

func NewSubSession(conn net.Conn) *SubSession {
Expand All @@ -50,6 +52,13 @@ func NewSubSession(conn net.Conn) *SubSession {
option.WriteChanSize = wChanSize
option.WriteTimeoutMS = subSessionWriteTimeoutMS
}),
stat: base.StatSub{
StatSession: base.StatSession{
Protocol: base.ProtocolHTTPFLV,
StartTime: time.Now().Format("2006-01-02 15:04:05.999"),
RemoteAddr: conn.RemoteAddr().String(),
},
},
}
nazalog.Infof("[%s] lifecycle new httpflv SubSession. session=%p, remote addr=%s", uk, s, conn.RemoteAddr().String())
return s
Expand Down Expand Up @@ -134,6 +143,21 @@ func (session *SubSession) Dispose() {
_ = session.conn.Close()
}

func (session *SubSession) GetStat() base.StatSub {
currStat := session.conn.GetStat()
session.stat.ReadBytesSum = currStat.ReadBytesSum
session.stat.WroteBytesSum = currStat.WroteBytesSum
return session.stat
}

func (session *SubSession) UpdateStat(tickCount uint32) {
currStat := session.conn.GetStat()
var diffStat connection.Stat
diffStat.WroteBytesSum = currStat.WroteBytesSum - session.prevConnStat.WroteBytesSum
session.stat.Bitrate = int(diffStat.WroteBytesSum * 8 / 1024 / 5)
session.prevConnStat = currStat
}

func init() {
flvHTTPResponseHeaderStr := "HTTP/1.1 200 OK\r\n" +
"Server: " + base.LALHTTPFLVSubSessionServer + "\r\n" +
Expand Down
16 changes: 8 additions & 8 deletions pkg/httpts/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ type ServerObserver interface {
}

type Server struct {
obs ServerObserver
addr string
ln net.Listener
observer ServerObserver
addr string
ln net.Listener
}

func NewServer(obs ServerObserver, addr string) *Server {
func NewServer(observer ServerObserver, addr string) *Server {
return &Server{
obs: obs,
addr: addr,
observer: observer,
addr: addr,
}
}

Expand Down Expand Up @@ -71,11 +71,11 @@ func (server *Server) handleConnect(conn net.Conn) {
}
log.Debugf("[%s] < read http request. uri=%s", session.UniqueKey, session.URI)

if !server.obs.OnNewHTTPTSSubSession(session) {
if !server.observer.OnNewHTTPTSSubSession(session) {
session.Dispose()
}

err := session.RunLoop()
log.Debugf("[%s] httpts sub session loop done. err=%v", session.UniqueKey, err)
server.obs.OnDelHTTPTSSubSession(session)
server.observer.OnDelHTTPTSSubSession(session)
}
26 changes: 25 additions & 1 deletion pkg/httpts/server_sub_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type SubSession struct {

IsFresh bool

conn connection.Connection
conn connection.Connection
prevConnStat connection.Stat
stat base.StatSub
}

func NewSubSession(conn net.Conn) *SubSession {
Expand All @@ -49,6 +51,13 @@ func NewSubSession(conn net.Conn) *SubSession {
option.WriteChanSize = wChanSize
option.WriteTimeoutMS = subSessionWriteTimeoutMS
}),
stat: base.StatSub{
StatSession: base.StatSession{
Protocol: base.ProtocolHTTPTS,
StartTime: time.Now().Format("2006-01-02 15:04:05.999"),
RemoteAddr: conn.RemoteAddr().String(),
},
},
}
nazalog.Infof("[%s] lifecycle new httpts SubSession. session=%p, remote addr=%s", uk, s, conn.RemoteAddr().String())
return s
Expand Down Expand Up @@ -129,6 +138,21 @@ func (session *SubSession) Dispose() {
_ = session.conn.Close()
}

func (session *SubSession) GetStat() base.StatSub {
currStat := session.conn.GetStat()
session.stat.ReadBytesSum = currStat.ReadBytesSum
session.stat.WroteBytesSum = currStat.WroteBytesSum
return session.stat
}

func (session *SubSession) UpdateStat(tickCount uint32) {
currStat := session.conn.GetStat()
var diffStat connection.Stat
diffStat.WroteBytesSum = currStat.WroteBytesSum - session.prevConnStat.WroteBytesSum
session.stat.Bitrate = int(diffStat.WroteBytesSum * 8 / 1024 / 5)
session.prevConnStat = currStat
}

func init() {
tsHTTPResponseHeaderStr := "HTTP/1.1 200 OK\r\n" +
"Server: " + base.LALHTTPTSSubSessionServer + "\r\n" +
Expand Down
11 changes: 9 additions & 2 deletions pkg/logic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ type Config struct {
RelayPushConfig RelayPushConfig `json:"relay_push"`
RelayPullConfig RelayPullConfig `json:"relay_pull"`

PProfConfig PProfConfig `json:"pprof"`
LogConfig nazalog.Option `json:"log"`
HTTPAPIConfig HTTPAPIConfig `json:"http_api"`
PProfConfig PProfConfig `json:"pprof"`
LogConfig nazalog.Option `json:"log"`
}

type RTMPConfig struct {
Expand Down Expand Up @@ -69,6 +70,11 @@ type RelayPullConfig struct {
Addr string `json:"addr"`
}

type HTTPAPIConfig struct {
Enable bool `json:"enable"`
Addr string `json:"addr"`
}

type PProfConfig struct {
Enable bool `json:"enable"`
Addr string `json:"addr"`
Expand Down Expand Up @@ -97,6 +103,7 @@ func LoadConf(confFile string) (*Config, error) {
!j.Exist("rtsp") ||
!j.Exist("relay_push") ||
!j.Exist("relay_pull") ||
!j.Exist("http_api") ||
!j.Exist("pprof") ||
!j.Exist("log") {
return &config, errors.New("missing key field in config file")
Expand Down
Loading

0 comments on commit f2fc9a7

Please sign in to comment.