2023-05-16 13:59:37 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2023-05-16 16:01:05 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2023-05-16 13:59:37 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/media"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/ringbuffer"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/pion/ice/v2"
|
|
|
|
"github.com/pion/webrtc/v3"
|
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2023-05-16 13:59:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type trackRecvPair struct {
|
|
|
|
track *webrtc.TrackRemote
|
|
|
|
receiver *webrtc.RTPReceiver
|
|
|
|
}
|
|
|
|
|
|
|
|
func mediasOfOutgoingTracks(tracks []*webRTCOutgoingTrack) media.Medias {
|
|
|
|
ret := make(media.Medias, len(tracks))
|
|
|
|
for i, track := range tracks {
|
|
|
|
ret[i] = track.media
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func mediasOfIncomingTracks(tracks []*webRTCIncomingTrack) media.Medias {
|
|
|
|
ret := make(media.Medias, len(tracks))
|
|
|
|
for i, track := range tracks {
|
|
|
|
ret[i] = track.media
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func gatherOutgoingTracks(medias media.Medias) ([]*webRTCOutgoingTrack, error) {
|
|
|
|
var tracks []*webRTCOutgoingTrack
|
|
|
|
|
|
|
|
videoTrack, err := newWebRTCOutgoingTrackVideo(medias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if videoTrack != nil {
|
|
|
|
tracks = append(tracks, videoTrack)
|
|
|
|
}
|
|
|
|
|
|
|
|
audioTrack, err := newWebRTCOutgoingTrackAudio(medias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if audioTrack != nil {
|
|
|
|
tracks = append(tracks, audioTrack)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tracks == nil {
|
|
|
|
return nil, fmt.Errorf(
|
2023-07-18 22:14:50 +00:00
|
|
|
"the stream doesn't contain any supported codec, which are currently AV1, VP9, VP8, H264, Opus, G722, G711")
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tracks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gatherIncomingTracks(
|
|
|
|
ctx context.Context,
|
|
|
|
pc *peerConnection,
|
|
|
|
trackRecv chan trackRecvPair,
|
|
|
|
) ([]*webRTCIncomingTrack, error) {
|
|
|
|
var tracks []*webRTCIncomingTrack
|
|
|
|
|
|
|
|
t := time.NewTimer(webrtcTrackGatherTimeout)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
2023-06-27 11:58:06 +00:00
|
|
|
if len(tracks) == 0 {
|
|
|
|
return nil, fmt.Errorf("no tracks found")
|
|
|
|
}
|
2023-05-16 13:59:37 +00:00
|
|
|
return tracks, nil
|
|
|
|
|
|
|
|
case pair := <-trackRecv:
|
|
|
|
track, err := newWebRTCIncomingTrack(pair.track, pair.receiver, pc.WriteRTCP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tracks = append(tracks, track)
|
|
|
|
|
|
|
|
if len(tracks) == 2 {
|
|
|
|
return tracks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-pc.disconnected:
|
|
|
|
return nil, fmt.Errorf("peer connection closed")
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, fmt.Errorf("terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type webRTCSessionPathManager interface {
|
|
|
|
publisherAdd(req pathPublisherAddReq) pathPublisherAnnounceRes
|
|
|
|
readerAdd(req pathReaderAddReq) pathReaderSetupPlayRes
|
|
|
|
}
|
|
|
|
|
|
|
|
type webRTCSession struct {
|
|
|
|
readBufferCount int
|
|
|
|
wg *sync.WaitGroup
|
|
|
|
iceHostNAT1To1IPs []string
|
|
|
|
iceUDPMux ice.UDPMux
|
|
|
|
iceTCPMux ice.TCPMux
|
|
|
|
pathManager webRTCSessionPathManager
|
|
|
|
parent *webRTCManager
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
ctxCancel func()
|
|
|
|
created time.Time
|
|
|
|
uuid uuid.UUID
|
|
|
|
secret uuid.UUID
|
2023-07-18 21:41:52 +00:00
|
|
|
req webRTCSessionNewReq
|
2023-05-16 16:01:05 +00:00
|
|
|
answerSent bool
|
2023-07-05 19:20:26 +00:00
|
|
|
mutex sync.RWMutex
|
|
|
|
pc *peerConnection
|
2023-05-16 13:59:37 +00:00
|
|
|
|
2023-07-18 21:41:52 +00:00
|
|
|
chNew chan webRTCSessionNewReq
|
|
|
|
chAddCandidates chan webRTCSessionAddCandidatesReq
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWebRTCSession(
|
|
|
|
parentCtx context.Context,
|
|
|
|
readBufferCount int,
|
2023-07-18 21:41:52 +00:00
|
|
|
remoteAddr string,
|
2023-05-16 13:59:37 +00:00
|
|
|
wg *sync.WaitGroup,
|
|
|
|
iceHostNAT1To1IPs []string,
|
|
|
|
iceUDPMux ice.UDPMux,
|
|
|
|
iceTCPMux ice.TCPMux,
|
|
|
|
pathManager webRTCSessionPathManager,
|
|
|
|
parent *webRTCManager,
|
|
|
|
) *webRTCSession {
|
|
|
|
ctx, ctxCancel := context.WithCancel(parentCtx)
|
|
|
|
|
|
|
|
s := &webRTCSession{
|
2023-07-18 21:41:52 +00:00
|
|
|
readBufferCount: readBufferCount,
|
|
|
|
wg: wg,
|
|
|
|
iceHostNAT1To1IPs: iceHostNAT1To1IPs,
|
|
|
|
iceUDPMux: iceUDPMux,
|
|
|
|
iceTCPMux: iceTCPMux,
|
|
|
|
parent: parent,
|
|
|
|
pathManager: pathManager,
|
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
|
|
|
created: time.Now(),
|
|
|
|
uuid: uuid.New(),
|
|
|
|
secret: uuid.New(),
|
|
|
|
chNew: make(chan webRTCSessionNewReq),
|
|
|
|
chAddCandidates: make(chan webRTCSessionAddCandidatesReq),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Log(logger.Info, "created by %s", remoteAddr)
|
2023-05-16 13:59:37 +00:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go s.run()
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *webRTCSession) Log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
id := hex.EncodeToString(s.uuid[:4])
|
|
|
|
s.parent.Log(level, "[session %v] "+format, append([]interface{}{id}, args...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *webRTCSession) close() {
|
|
|
|
s.ctxCancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *webRTCSession) run() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
2023-07-18 21:41:52 +00:00
|
|
|
err := s.runInner()
|
|
|
|
|
|
|
|
s.ctxCancel()
|
|
|
|
|
|
|
|
s.parent.sessionClose(s)
|
|
|
|
|
|
|
|
s.Log(logger.Info, "closed (%v)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *webRTCSession) runInner() error {
|
|
|
|
select {
|
|
|
|
case req := <-s.chNew:
|
|
|
|
s.req = req
|
|
|
|
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return fmt.Errorf("terminated")
|
|
|
|
}
|
|
|
|
|
|
|
|
errStatusCode, err := s.runInner2()
|
2023-05-16 13:59:37 +00:00
|
|
|
|
|
|
|
if !s.answerSent {
|
2023-07-19 10:31:50 +00:00
|
|
|
s.req.res <- webRTCSessionNewRes{
|
2023-05-16 16:01:05 +00:00
|
|
|
err: err,
|
|
|
|
errStatusCode: errStatusCode,
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 21:41:52 +00:00
|
|
|
return err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 21:41:52 +00:00
|
|
|
func (s *webRTCSession) runInner2() (int, error) {
|
2023-05-16 13:59:37 +00:00
|
|
|
if s.req.publish {
|
|
|
|
return s.runPublish()
|
|
|
|
}
|
|
|
|
return s.runRead()
|
|
|
|
}
|
|
|
|
|
2023-05-16 16:01:05 +00:00
|
|
|
func (s *webRTCSession) runPublish() (int, error) {
|
2023-05-16 13:59:37 +00:00
|
|
|
res := s.pathManager.publisherAdd(pathPublisherAddReq{
|
|
|
|
author: s,
|
|
|
|
pathName: s.req.pathName,
|
|
|
|
skipAuth: true,
|
|
|
|
})
|
|
|
|
if res.err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusInternalServerError, res.err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer res.path.publisherRemove(pathPublisherRemoveReq{author: s})
|
|
|
|
|
|
|
|
pc, err := newPeerConnection(
|
2023-06-30 14:47:10 +00:00
|
|
|
s.parent.generateICEServers(),
|
2023-05-16 13:59:37 +00:00
|
|
|
s.iceHostNAT1To1IPs,
|
|
|
|
s.iceUDPMux,
|
|
|
|
s.iceTCPMux,
|
|
|
|
s)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
defer pc.close()
|
|
|
|
|
|
|
|
_, err = pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo, webrtc.RtpTransceiverInit{
|
|
|
|
Direction: webrtc.RTPTransceiverDirectionRecvonly,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio, webrtc.RtpTransceiverInit{
|
|
|
|
Direction: webrtc.RTPTransceiverDirectionRecvonly,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trackRecv := make(chan trackRecvPair)
|
|
|
|
|
|
|
|
pc.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
|
|
|
|
select {
|
|
|
|
case trackRecv <- trackRecvPair{track, receiver}:
|
|
|
|
case <-pc.closed:
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-24 15:06:06 +00:00
|
|
|
offer := s.buildOffer()
|
2023-05-16 13:59:37 +00:00
|
|
|
err = pc.SetRemoteDescription(*offer)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
answer, err := pc.CreateAnswer(nil)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = pc.SetLocalDescription(answer)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 16:35:19 +00:00
|
|
|
err = s.waitGatheringDone(pc)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp := pc.LocalDescription()
|
|
|
|
answer = *tmp
|
|
|
|
|
2023-07-19 10:31:50 +00:00
|
|
|
s.writeAnswer(&answer)
|
2023-05-16 13:59:37 +00:00
|
|
|
|
|
|
|
go s.readRemoteCandidates(pc)
|
|
|
|
|
|
|
|
err = s.waitUntilConnected(pc)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return 0, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tracks, err := gatherIncomingTracks(s.ctx, pc, trackRecv)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return 0, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
medias := mediasOfIncomingTracks(tracks)
|
|
|
|
|
|
|
|
rres := res.path.publisherStart(pathPublisherStartReq{
|
|
|
|
author: s,
|
|
|
|
medias: medias,
|
|
|
|
generateRTPPackets: false,
|
|
|
|
})
|
|
|
|
if rres.err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return 0, rres.err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Log(logger.Info, "is publishing to path '%s', %s",
|
|
|
|
res.path.name,
|
|
|
|
sourceMediaInfo(medias))
|
|
|
|
|
|
|
|
for _, track := range tracks {
|
|
|
|
track.start(rres.stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-pc.disconnected:
|
2023-05-16 16:01:05 +00:00
|
|
|
return 0, fmt.Errorf("peer connection closed")
|
2023-05-16 13:59:37 +00:00
|
|
|
|
|
|
|
case <-s.ctx.Done():
|
2023-05-16 16:01:05 +00:00
|
|
|
return 0, fmt.Errorf("terminated")
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 16:01:05 +00:00
|
|
|
func (s *webRTCSession) runRead() (int, error) {
|
2023-05-16 13:59:37 +00:00
|
|
|
res := s.pathManager.readerAdd(pathReaderAddReq{
|
|
|
|
author: s,
|
|
|
|
pathName: s.req.pathName,
|
|
|
|
skipAuth: true,
|
|
|
|
})
|
|
|
|
if res.err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
if strings.HasPrefix(res.err.Error(), "no one is publishing") {
|
|
|
|
return http.StatusNotFound, res.err
|
|
|
|
}
|
|
|
|
return http.StatusInternalServerError, res.err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer res.path.readerRemove(pathReaderRemoveReq{author: s})
|
|
|
|
|
|
|
|
tracks, err := gatherOutgoingTracks(res.stream.medias())
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pc, err := newPeerConnection(
|
2023-06-30 14:47:10 +00:00
|
|
|
s.parent.generateICEServers(),
|
2023-05-16 13:59:37 +00:00
|
|
|
s.iceHostNAT1To1IPs,
|
|
|
|
s.iceUDPMux,
|
|
|
|
s.iceTCPMux,
|
|
|
|
s)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
defer pc.close()
|
|
|
|
|
|
|
|
for _, track := range tracks {
|
|
|
|
var err error
|
|
|
|
track.sender, err = pc.AddTrack(track.track)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:06:06 +00:00
|
|
|
offer := s.buildOffer()
|
2023-05-16 13:59:37 +00:00
|
|
|
err = pc.SetRemoteDescription(*offer)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
answer, err := pc.CreateAnswer(nil)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = pc.SetLocalDescription(answer)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.waitGatheringDone(pc)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return http.StatusBadRequest, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 16:35:19 +00:00
|
|
|
tmp := pc.LocalDescription()
|
|
|
|
answer = *tmp
|
|
|
|
|
2023-07-19 10:31:50 +00:00
|
|
|
s.writeAnswer(&answer)
|
2023-05-16 13:59:37 +00:00
|
|
|
|
|
|
|
go s.readRemoteCandidates(pc)
|
|
|
|
|
|
|
|
err = s.waitUntilConnected(pc)
|
|
|
|
if err != nil {
|
2023-05-16 16:01:05 +00:00
|
|
|
return 0, err
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ringBuffer, _ := ringbuffer.New(uint64(s.readBufferCount))
|
|
|
|
defer ringBuffer.Close()
|
|
|
|
|
|
|
|
writeError := make(chan error)
|
|
|
|
|
|
|
|
for _, track := range tracks {
|
|
|
|
track.start(s.ctx, s, res.stream, ringBuffer, writeError)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.stream.readerRemove(s)
|
|
|
|
|
|
|
|
s.Log(logger.Info, "is reading from path '%s', %s",
|
|
|
|
res.path.name, sourceMediaInfo(mediasOfOutgoingTracks(tracks)))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
item, ok := ringBuffer.Pull()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
item.(func())()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-pc.disconnected:
|
2023-05-16 16:01:05 +00:00
|
|
|
return 0, fmt.Errorf("peer connection closed")
|
2023-05-16 13:59:37 +00:00
|
|
|
|
|
|
|
case err := <-writeError:
|
2023-05-16 16:01:05 +00:00
|
|
|
return 0, err
|
2023-05-16 13:59:37 +00:00
|
|
|
|
|
|
|
case <-s.ctx.Done():
|
2023-05-16 16:01:05 +00:00
|
|
|
return 0, fmt.Errorf("terminated")
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:06:06 +00:00
|
|
|
func (s *webRTCSession) buildOffer() *webrtc.SessionDescription {
|
|
|
|
return &webrtc.SessionDescription{
|
|
|
|
Type: webrtc.SDPTypeOffer,
|
|
|
|
SDP: string(s.req.offer),
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *webRTCSession) waitGatheringDone(pc *peerConnection) error {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pc.localCandidateRecv:
|
|
|
|
case <-pc.gatheringDone:
|
|
|
|
return nil
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return fmt.Errorf("terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 10:31:50 +00:00
|
|
|
func (s *webRTCSession) writeAnswer(answer *webrtc.SessionDescription) {
|
|
|
|
s.req.res <- webRTCSessionNewRes{
|
2023-05-16 13:59:37 +00:00
|
|
|
sx: s,
|
2023-05-24 15:06:06 +00:00
|
|
|
answer: []byte(answer.SDP),
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
2023-07-19 10:31:50 +00:00
|
|
|
s.answerSent = true
|
2023-05-16 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *webRTCSession) waitUntilConnected(pc *peerConnection) error {
|
|
|
|
t := time.NewTimer(webrtcHandshakeTimeout)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
return fmt.Errorf("deadline exceeded")
|
|
|
|
|
|
|
|
case <-pc.connected:
|
|
|
|
break outer
|
|
|
|
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return fmt.Errorf("terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 19:20:26 +00:00
|
|
|
s.mutex.Lock()
|
2023-05-16 13:59:37 +00:00
|
|
|
s.pc = pc
|
2023-07-05 19:20:26 +00:00
|
|
|
s.mutex.Unlock()
|
2023-05-16 13:59:37 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *webRTCSession) readRemoteCandidates(pc *peerConnection) {
|
|
|
|
for {
|
|
|
|
select {
|
2023-07-18 21:41:52 +00:00
|
|
|
case req := <-s.chAddCandidates:
|
2023-05-16 13:59:37 +00:00
|
|
|
for _, candidate := range req.candidates {
|
|
|
|
err := pc.AddICECandidate(*candidate)
|
|
|
|
if err != nil {
|
|
|
|
req.res <- webRTCSessionAddCandidatesRes{err: err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req.res <- webRTCSessionAddCandidatesRes{}
|
|
|
|
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 21:41:52 +00:00
|
|
|
// new is called by webRTCHTTPServer through webRTCManager.
|
|
|
|
func (s *webRTCSession) new(req webRTCSessionNewReq) webRTCSessionNewRes {
|
|
|
|
select {
|
|
|
|
case s.chNew <- req:
|
|
|
|
return <-req.res
|
|
|
|
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return webRTCSessionNewRes{err: fmt.Errorf("terminated"), errStatusCode: http.StatusInternalServerError}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addCandidates is called by webRTCHTTPServer through webRTCManager.
|
|
|
|
func (s *webRTCSession) addCandidates(
|
2023-05-16 13:59:37 +00:00
|
|
|
req webRTCSessionAddCandidatesReq,
|
|
|
|
) webRTCSessionAddCandidatesRes {
|
|
|
|
select {
|
2023-07-18 21:41:52 +00:00
|
|
|
case s.chAddCandidates <- req:
|
2023-05-16 13:59:37 +00:00
|
|
|
return <-req.res
|
|
|
|
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
return webRTCSessionAddCandidatesRes{err: fmt.Errorf("terminated")}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiSourceDescribe implements sourceStaticImpl.
|
|
|
|
func (s *webRTCSession) apiSourceDescribe() pathAPISourceOrReader {
|
|
|
|
return pathAPISourceOrReader{
|
|
|
|
Type: "webRTCSession",
|
|
|
|
ID: s.uuid.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiReaderDescribe implements reader.
|
|
|
|
func (s *webRTCSession) apiReaderDescribe() pathAPISourceOrReader {
|
|
|
|
return s.apiSourceDescribe()
|
|
|
|
}
|
2023-05-18 13:07:47 +00:00
|
|
|
|
|
|
|
func (s *webRTCSession) apiItem() *apiWebRTCSession {
|
2023-07-05 19:20:26 +00:00
|
|
|
s.mutex.RLock()
|
|
|
|
defer s.mutex.RUnlock()
|
|
|
|
|
2023-05-18 13:07:47 +00:00
|
|
|
peerConnectionEstablished := false
|
|
|
|
localCandidate := ""
|
|
|
|
remoteCandidate := ""
|
|
|
|
bytesReceived := uint64(0)
|
|
|
|
bytesSent := uint64(0)
|
|
|
|
|
2023-07-05 19:20:26 +00:00
|
|
|
if s.pc != nil {
|
2023-05-18 13:07:47 +00:00
|
|
|
peerConnectionEstablished = true
|
2023-07-05 19:20:26 +00:00
|
|
|
localCandidate = s.pc.localCandidate()
|
|
|
|
remoteCandidate = s.pc.remoteCandidate()
|
|
|
|
bytesReceived = s.pc.bytesReceived()
|
|
|
|
bytesSent = s.pc.bytesSent()
|
2023-05-18 13:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &apiWebRTCSession{
|
|
|
|
ID: s.uuid,
|
|
|
|
Created: s.created,
|
|
|
|
RemoteAddr: s.req.remoteAddr,
|
|
|
|
PeerConnectionEstablished: peerConnectionEstablished,
|
|
|
|
LocalCandidate: localCandidate,
|
|
|
|
RemoteCandidate: remoteCandidate,
|
|
|
|
State: func() string {
|
|
|
|
if s.req.publish {
|
|
|
|
return "publish"
|
|
|
|
}
|
|
|
|
return "read"
|
|
|
|
}(),
|
2023-07-05 19:20:26 +00:00
|
|
|
Path: s.req.pathName,
|
2023-05-18 13:07:47 +00:00
|
|
|
BytesReceived: bytesReceived,
|
|
|
|
BytesSent: bytesSent,
|
|
|
|
}
|
|
|
|
}
|