网站通话使用webrtc,新增ffmpeg

This commit is contained in:
2025-12-16 15:12:03 +08:00
parent 811764fd8a
commit c8ab9b74ab
3 changed files with 103 additions and 30 deletions

View File

@@ -181,9 +181,9 @@ type ConnectionHandler struct {
remoteAddr string
// 连接状态
streamID string
isPublish bool
appName string
streamID string
isPublish bool
appName string
msgStreamID uint32
// 统计信息
@@ -773,6 +773,18 @@ func (r *RTMPServer) startHTTPFLVServer() {
// handleFLVRequest 处理 FLV 请求
func (r *RTMPServer) handleFLVRequest(w http.ResponseWriter, req *http.Request) {
// 添加 CORS 头,允许跨域访问
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Range")
w.Header().Set("Access-Control-Expose-Headers", "Content-Length, Content-Range")
// 处理 OPTIONS 预检请求
if req.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
path := req.URL.Path
if len(path) < 7 {
http.Error(w, "Invalid path", http.StatusBadRequest)
@@ -1007,12 +1019,14 @@ func (r *RTMPServer) GenerateStreamURLs(roomID, userID string) (*RTMPStream, err
token := generateStreamToken(streamID)
stream := &RTMPStream{
ID: streamID,
RoomID: roomID,
UserID: userID,
PushURL: fmt.Sprintf("rtmp://%s:%d/live/%s?token=%s", publicIP, r.config.RTMPPort, streamID, token),
PullURL: fmt.Sprintf("rtmp://%s:%d/live/%s", publicIP, r.config.RTMPPort, streamID),
FLVURL: fmt.Sprintf("http://%s:%d/live/%s.flv", publicIP, r.config.HTTPFLVPort, streamID),
ID: streamID,
RoomID: roomID,
UserID: userID,
PushURL: fmt.Sprintf("rtmp://%s:%d/live/%s?token=%s", publicIP, r.config.RTMPPort, streamID, token),
PullURL: fmt.Sprintf("rtmp://%s:%d/live/%s", publicIP, r.config.RTMPPort, streamID),
// 使用 HTTPS 域名,通过 Nginx 反向代理到 HTTP-FLV 服务
// 避免浏览器混合内容安全策略阻止 HTTP 请求
FLVURL: fmt.Sprintf("https://g-ws.nailaoyun.cn/live/%s.flv", streamID),
CreatedAt: time.Now(),
IsActive: true,
stopChan: make(chan struct{}),