mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-09-27 20:52:08 +08:00
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package rtmp
|
|
|
|
import (
|
|
"github.com/AlexxIT/go2rtc/internal/api"
|
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
|
"github.com/AlexxIT/go2rtc/pkg/rtmp"
|
|
"github.com/rs/zerolog/log"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func Init() {
|
|
streams.HandleFunc("rtmp", streamsHandle)
|
|
|
|
api.HandleFunc("api/stream.flv", apiHandle)
|
|
}
|
|
|
|
func streamsHandle(url string) (core.Producer, error) {
|
|
conn := rtmp.NewClient(url)
|
|
if err := conn.Dial(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := conn.Describe(); err != nil {
|
|
return nil, err
|
|
}
|
|
return conn, nil
|
|
}
|
|
|
|
func apiHandle(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "POST" {
|
|
http.Error(w, "", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
dst := r.URL.Query().Get("dst")
|
|
stream := streams.Get(dst)
|
|
if stream == nil {
|
|
http.Error(w, api.StreamNotFound, http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
res := &http.Response{Body: r.Body, Request: r}
|
|
client, err := rtmp.Accept(res)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if err = client.Describe(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
stream.AddProducer(client)
|
|
|
|
if err = client.Start(); err != nil && err != io.EOF {
|
|
log.Warn().Err(err).Caller().Send()
|
|
}
|
|
|
|
stream.RemoveProducer(client)
|
|
}
|