github.com/pion/webrtc/v3@v3.2.24/ortc_media_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 //go:build !js 5 // +build !js 6 7 package webrtc 8 9 import ( 10 "context" 11 "testing" 12 "time" 13 14 "github.com/pion/transport/v2/test" 15 "github.com/pion/webrtc/v3/pkg/media" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func Test_ORTC_Media(t *testing.T) { 20 lim := test.TimeOut(time.Second * 20) 21 defer lim.Stop() 22 23 report := test.CheckRoutines(t) 24 defer report() 25 26 stackA, stackB, err := newORTCPair() 27 assert.NoError(t, err) 28 29 assert.NoError(t, stackA.api.mediaEngine.RegisterDefaultCodecs()) 30 assert.NoError(t, stackB.api.mediaEngine.RegisterDefaultCodecs()) 31 32 assert.NoError(t, signalORTCPair(stackA, stackB)) 33 34 track, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion") 35 assert.NoError(t, err) 36 37 rtpSender, err := stackA.api.NewRTPSender(track, stackA.dtls) 38 assert.NoError(t, err) 39 assert.NoError(t, rtpSender.Send(rtpSender.GetParameters())) 40 41 rtpReceiver, err := stackB.api.NewRTPReceiver(RTPCodecTypeVideo, stackB.dtls) 42 assert.NoError(t, err) 43 assert.NoError(t, rtpReceiver.Receive(RTPReceiveParameters{Encodings: []RTPDecodingParameters{ 44 {RTPCodingParameters: rtpSender.GetParameters().Encodings[0].RTPCodingParameters}, 45 }})) 46 47 seenPacket, seenPacketCancel := context.WithCancel(context.Background()) 48 go func() { 49 track := rtpReceiver.Track() 50 _, _, err := track.ReadRTP() 51 assert.NoError(t, err) 52 53 seenPacketCancel() 54 }() 55 56 func() { 57 for range time.Tick(time.Millisecond * 20) { 58 select { 59 case <-seenPacket.Done(): 60 return 61 default: 62 assert.NoError(t, track.WriteSample(media.Sample{Data: []byte{0xAA}, Duration: time.Second})) 63 } 64 } 65 }() 66 67 assert.NoError(t, rtpSender.Stop()) 68 assert.NoError(t, rtpReceiver.Stop()) 69 70 assert.NoError(t, stackA.close()) 71 assert.NoError(t, stackB.close()) 72 }