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/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"
|
2024-09-09 10:59:23 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/protocols/whip"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/stream"
|
2023-10-31 13:19:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2024-09-09 10:59:23 +00:00
|
|
|
client := whip.Client{
|
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
|
|
|
}
|
|
|
|
|
2024-09-09 10:59:23 +00:00
|
|
|
_, err = client.Read(params.Context)
|
2023-10-31 13:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer client.Close() //nolint:errcheck
|
|
|
|
|
2024-09-09 10:59:23 +00:00
|
|
|
var stream *stream.Stream
|
|
|
|
|
|
|
|
medias, err := webrtc.ToStream(client.PeerConnection(), &stream)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-31 13:19:04 +00:00
|
|
|
|
|
|
|
rres := s.Parent.SetReady(defs.PathSourceStaticSetReadyReq{
|
|
|
|
Desc: &description.Session{Medias: medias},
|
|
|
|
GenerateRTPPackets: true,
|
|
|
|
})
|
|
|
|
if rres.Err != nil {
|
|
|
|
return rres.Err
|
|
|
|
}
|
|
|
|
|
2024-09-09 10:59:23 +00:00
|
|
|
stream = rres.Stream
|
2023-10-31 13:19:04 +00:00
|
|
|
|
2024-09-09 10:59:23 +00:00
|
|
|
defer s.Parent.SetNotReady(defs.PathSourceStaticSetNotReadyReq{})
|
2023-10-31 13:19:04 +00:00
|
|
|
|
2024-09-09 10:59:23 +00:00
|
|
|
client.StartReading()
|
2023-10-31 13:19:04 +00:00
|
|
|
|
|
|
|
return client.Wait(params.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
// APISourceDescribe implements StaticSource.
|
|
|
|
func (*Source) APISourceDescribe() defs.APIPathSourceOrReader {
|
|
|
|
return defs.APIPathSourceOrReader{
|
|
|
|
Type: "webrtcSource",
|
|
|
|
ID: "",
|
|
|
|
}
|
|
|
|
}
|