封装hls

This commit is contained in:
yangjiechina
2024-03-02 18:41:00 +08:00
parent 4947217197
commit f7552240d0
16 changed files with 505 additions and 192 deletions

View File

@@ -16,8 +16,6 @@ type Session interface {
func NewSession(conn net.Conn) Session {
impl := &sessionImpl{}
impl.Protocol = stream.ProtocolRtmpStr
impl.RemoteAddr = conn.RemoteAddr().String()
stack := librtmp.NewStack(impl)
impl.stack = stack
@@ -26,27 +24,26 @@ func NewSession(conn net.Conn) Session {
}
type sessionImpl struct {
stream.SessionImpl
//解析rtmp协议栈
stack *librtmp.Stack
//publisher/sink
handle interface{}
isPublish bool
conn net.Conn
isPublisher bool
conn net.Conn
}
func (s *sessionImpl) OnPublish(app, stream_ string, response chan utils.HookState) {
s.SessionImpl.Stream = app + "/" + stream_
publisher := NewPublisher(s.SessionImpl.Stream, s.stack)
s.stack.SetOnPublishHandler(publisher)
s.stack.SetOnTransDeMuxerHandler(publisher)
sourceId := app + "_" + stream_
source := NewPublisher(sourceId, s.stack, s.conn)
s.stack.SetOnPublishHandler(source)
s.stack.SetOnTransDeMuxerHandler(source)
//stream.SessionImpl统一处理, Source是否已经存在, Hook回调....
s.SessionImpl.OnPublish(publisher, nil, func() {
s.handle = publisher
s.isPublish = true
publisher.Init()
//推流事件Source统一处理, 是否已经存在, Hook回调....
source.(*publisher).Publish(source.(*publisher), func() {
s.handle = source
s.isPublisher = true
source.Init()
response <- utils.HookStateOK
}, func(state utils.HookState) {
@@ -55,10 +52,11 @@ func (s *sessionImpl) OnPublish(app, stream_ string, response chan utils.HookSta
}
func (s *sessionImpl) OnPlay(app, stream_ string, response chan utils.HookState) {
s.SessionImpl.Stream = app + "/" + stream_
sourceId := app + "_" + stream_
sink := NewSink(stream.GenerateSinkId(s.conn), s.SessionImpl.Stream, s.conn)
s.SessionImpl.OnPlay(sink, nil, func() {
//拉流事件Sink统一处理
sink := NewSink(stream.GenerateSinkId(s.conn), sourceId, s.conn)
sink.(*stream.SinkImpl).Play(sink, func() {
s.handle = sink
response <- utils.HookStateOK
}, func(state utils.HookState) {
@@ -68,8 +66,8 @@ func (s *sessionImpl) OnPlay(app, stream_ string, response chan utils.HookState)
func (s *sessionImpl) Input(conn net.Conn, data []byte) error {
//如果是推流并且握手成功后续收到的包都将发送给LoopEvent处理
if s.isPublish {
s.handle.(*Publisher).AddEvent(stream.SourceEventInput, data)
if s.isPublisher {
s.handle.(*publisher).AddEvent(stream.SourceEventInput, data)
return nil
} else {
return s.stack.Input(conn, data)
@@ -83,8 +81,8 @@ func (s *sessionImpl) Close() {
_, ok := s.handle.(*Publisher)
if ok {
if s.isPublish {
s.handle.(*Publisher).AddEvent(stream.SourceEventClose, nil)
if s.isPublisher {
s.handle.(*publisher).AddEvent(stream.SourceEventClose, nil)
}
} else {
sink := s.handle.(stream.ISink)