mirror of
https://github.com/lkmio/lkm.git
synced 2025-10-05 15:16:49 +08:00
封装hls
This commit is contained in:
@@ -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)
|
||||
|
Reference in New Issue
Block a user