2024-03-06 21:38:36 +00:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
2024-06-02 22:26:32 +00:00
|
|
|
"context"
|
2024-03-06 21:38:36 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-05-30 11:36:58 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
2024-03-06 21:38:36 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/test"
|
2024-06-10 13:43:52 +00:00
|
|
|
"github.com/pion/sdp/v3"
|
2024-06-10 07:54:08 +00:00
|
|
|
"github.com/pion/webrtc/v3"
|
2024-03-06 21:38:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-06-02 22:26:32 +00:00
|
|
|
func TestPeerConnectionCloseImmediately(t *testing.T) {
|
2024-05-19 17:41:42 +00:00
|
|
|
pc := &PeerConnection{
|
2024-05-30 11:36:58 +00:00
|
|
|
HandshakeTimeout: conf.StringDuration(10 * time.Second),
|
|
|
|
TrackGatherTimeout: conf.StringDuration(2 * time.Second),
|
|
|
|
LocalRandomUDP: true,
|
|
|
|
IPsFromInterfaces: true,
|
|
|
|
Publish: false,
|
|
|
|
Log: test.NilLogger,
|
2024-03-06 21:38:36 +00:00
|
|
|
}
|
2024-05-19 17:41:42 +00:00
|
|
|
err := pc.Start()
|
2024-03-06 21:38:36 +00:00
|
|
|
require.NoError(t, err)
|
2024-06-02 22:26:32 +00:00
|
|
|
defer pc.Close()
|
2024-03-06 21:38:36 +00:00
|
|
|
|
|
|
|
_, err = pc.CreatePartialOffer()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// wait for ICE candidates to be generated
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
pc.Close()
|
|
|
|
}
|
2024-06-02 22:26:32 +00:00
|
|
|
|
2024-09-09 10:26:35 +00:00
|
|
|
// test that an audio codec is present regardless of the fact that an audio track is.
|
2024-06-10 13:43:52 +00:00
|
|
|
func TestPeerConnectionFallbackCodecs(t *testing.T) {
|
|
|
|
pc1 := &PeerConnection{
|
|
|
|
HandshakeTimeout: conf.StringDuration(10 * time.Second),
|
|
|
|
TrackGatherTimeout: conf.StringDuration(2 * time.Second),
|
|
|
|
LocalRandomUDP: true,
|
|
|
|
IPsFromInterfaces: true,
|
|
|
|
Publish: false,
|
|
|
|
Log: test.NilLogger,
|
|
|
|
}
|
|
|
|
err := pc1.Start()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer pc1.Close()
|
|
|
|
|
|
|
|
pc2 := &PeerConnection{
|
|
|
|
HandshakeTimeout: conf.StringDuration(10 * time.Second),
|
|
|
|
TrackGatherTimeout: conf.StringDuration(2 * time.Second),
|
|
|
|
LocalRandomUDP: true,
|
|
|
|
IPsFromInterfaces: true,
|
|
|
|
Publish: true,
|
|
|
|
OutgoingTracks: []*OutgoingTrack{{
|
2024-09-09 10:59:23 +00:00
|
|
|
Caps: webrtc.RTPCodecCapability{
|
|
|
|
MimeType: webrtc.MimeTypeAV1,
|
|
|
|
ClockRate: 90000,
|
2024-06-10 13:43:52 +00:00
|
|
|
},
|
|
|
|
}},
|
|
|
|
Log: test.NilLogger,
|
|
|
|
}
|
|
|
|
err = pc2.Start()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer pc2.Close()
|
|
|
|
|
|
|
|
offer, err := pc1.CreatePartialOffer()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
answer, err := pc2.CreateFullAnswer(context.Background(), offer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var s sdp.SessionDescription
|
|
|
|
err = s.Unmarshal([]byte(answer.SDP))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, []*sdp.MediaDescription{
|
|
|
|
{
|
|
|
|
MediaName: sdp.MediaName{
|
|
|
|
Media: "video",
|
|
|
|
Port: sdp.RangedPort{Value: 9},
|
|
|
|
Protos: []string{"UDP", "TLS", "RTP", "SAVPF"},
|
|
|
|
Formats: []string{"97"},
|
|
|
|
},
|
|
|
|
ConnectionInformation: s.MediaDescriptions[0].ConnectionInformation,
|
|
|
|
Attributes: s.MediaDescriptions[0].Attributes,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MediaName: sdp.MediaName{
|
|
|
|
Media: "audio",
|
|
|
|
Port: sdp.RangedPort{Value: 9},
|
|
|
|
Protos: []string{"UDP", "TLS", "RTP", "SAVPF"},
|
|
|
|
Formats: []string{"0"},
|
|
|
|
},
|
|
|
|
ConnectionInformation: s.MediaDescriptions[1].ConnectionInformation,
|
|
|
|
Attributes: s.MediaDescriptions[1].Attributes,
|
|
|
|
},
|
|
|
|
}, s.MediaDescriptions)
|
|
|
|
}
|