2023-10-31 13:19:04 +00:00
|
|
|
// Package webrtc contains the WebRTC static source.
|
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/description"
|
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/rtptime"
|
|
|
|
|
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/defs"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2023-11-02 11:38:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/protocols/tls"
|
2023-10-31 13:19:04 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/protocols/webrtc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Source is a WebRTC static source.
|
|
|
|
type Source struct {
|
2024-06-18 20:10:26 +00:00
|
|
|
ReadTimeout conf.StringDuration
|
|
|
|
Parent defs.StaticSourceParent
|
2023-10-31 13:19:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 18:17:17 +00:00
|
|
|
// Log implements logger.Writer.
|
2023-10-31 13:19:04 +00:00
|
|
|
func (s *Source) Log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
s.Parent.Log(level, "[WebRTC source] "+format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements StaticSource.
|
|
|
|
func (s *Source) Run(params defs.StaticSourceRunParams) error {
|
|
|
|
s.Log(logger.Debug, "connecting")
|
|
|
|
|
2024-06-18 20:10:26 +00:00
|
|
|
u, err := url.Parse(params.ResolvedSource)
|
2023-10-31 13:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Scheme = strings.ReplaceAll(u.Scheme, "whep", "http")
|
|
|
|
|
2024-03-10 10:51:15 +00:00
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: tls.ConfigForFingerprint(params.Conf.SourceFingerprint),
|
2024-03-10 10:33:00 +00:00
|
|
|
}
|
2024-03-10 10:51:15 +00:00
|
|
|
defer tr.CloseIdleConnections()
|
2024-03-10 10:33:00 +00:00
|
|
|
|
|
|
|
client := webrtc.WHIPClient{
|
2024-03-10 10:51:15 +00:00
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: time.Duration(s.ReadTimeout),
|
|
|
|
Transport: tr,
|
|
|
|
},
|
|
|
|
URL: u,
|
|
|
|
Log: s,
|
2023-10-31 13:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tracks, err := client.Read(params.Context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer client.Close() //nolint:errcheck
|
|
|
|
|
|
|
|
medias := webrtc.TracksToMedias(tracks)
|
|
|
|
|
|
|
|
rres := s.Parent.SetReady(defs.PathSourceStaticSetReadyReq{
|
|
|
|
Desc: &description.Session{Medias: medias},
|
|
|
|
GenerateRTPPackets: true,
|
|
|
|
})
|
|
|
|
if rres.Err != nil {
|
|
|
|
return rres.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer s.Parent.SetNotReady(defs.PathSourceStaticSetNotReadyReq{})
|
|
|
|
|
|
|
|
timeDecoder := rtptime.NewGlobalDecoder()
|
|
|
|
|
|
|
|
for i, media := range medias {
|
|
|
|
ci := i
|
|
|
|
cmedia := media
|
|
|
|
trackWrapper := &webrtc.TrackWrapper{ClockRat: cmedia.Formats[0].ClockRate()}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
pkt, err := tracks[ci].ReadRTP()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pts, ok := timeDecoder.Decode(trackWrapper, pkt)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rres.Stream.WriteRTPPacket(cmedia, cmedia.Formats[0], pkt, time.Now(), pts)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Wait(params.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
// APISourceDescribe implements StaticSource.
|
|
|
|
func (*Source) APISourceDescribe() defs.APIPathSourceOrReader {
|
|
|
|
return defs.APIPathSourceOrReader{
|
|
|
|
Type: "webrtcSource",
|
|
|
|
ID: "",
|
|
|
|
}
|
|
|
|
}
|