github.com/cnotch/ipchub@v1.1.0/service/rtsp/rtsp.go (about)

     1  // Copyright (c) 2019,CAOHONGJU All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rtsp
     6  
     7  import (
     8  	"net"
     9  	"sync"
    10  
    11  	"github.com/cnotch/ipchub/network/socket/listener"
    12  	"github.com/kelindar/tcp"
    13  	"github.com/cnotch/xlog"
    14  )
    15  
    16  // MatchRTSP 仅匹配 RTSP 请求方法
    17  // 注意:由于RTSP 和 HTTP 都有 OPTIONS 方法,因此对 RTSP 的 OPTIONS 做了进一步细化
    18  func MatchRTSP() listener.Matcher {
    19  	return listener.MatchPrefix("OPTIONS * RTSP", "OPTIONS * rtsp",
    20  		"OPTIONS rtsp://", "OPTIONS RTSP://",
    21  		MethodDescribe, MethodAnnounce, MethodSetup,
    22  		MethodPlay, MethodPause, MethodTeardown,
    23  		MethodGetParameter, MethodSetParameter,
    24  		MethodRecord, MethodRedirect)
    25  }
    26  
    27  // Server rtsp 服务器
    28  type Server struct {
    29  	logger *xlog.Logger
    30  
    31  	sessions sync.Map
    32  }
    33  
    34  // CreateAcceptHandler 创建连接接入处理器
    35  func CreateAcceptHandler() tcp.OnAccept {
    36  	svr := &Server{
    37  		logger: xlog.L(),
    38  	}
    39  	return svr.onAcceptConn
    40  }
    41  
    42  // onAcceptConn 当新连接接入时触发
    43  func (svr *Server) onAcceptConn(c net.Conn) {
    44  	s := newSession(svr, c)
    45  	go s.process()
    46  }