github.com/pion/webrtc/v4@v4.0.1/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/v3/test" 15 "github.com/pion/webrtc/v4/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, signalORTCPair(stackA, stackB)) 30 31 track, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion") 32 assert.NoError(t, err) 33 34 rtpSender, err := stackA.api.NewRTPSender(track, stackA.dtls) 35 assert.NoError(t, err) 36 assert.NoError(t, rtpSender.Send(rtpSender.GetParameters())) 37 38 rtpReceiver, err := stackB.api.NewRTPReceiver(RTPCodecTypeVideo, stackB.dtls) 39 assert.NoError(t, err) 40 assert.NoError(t, rtpReceiver.Receive(RTPReceiveParameters{Encodings: []RTPDecodingParameters{ 41 {RTPCodingParameters: rtpSender.GetParameters().Encodings[0].RTPCodingParameters}, 42 }})) 43 44 seenPacket, seenPacketCancel := context.WithCancel(context.Background()) 45 go func() { 46 track := rtpReceiver.Track() 47 _, _, err := track.ReadRTP() 48 assert.NoError(t, err) 49 50 seenPacketCancel() 51 }() 52 53 func() { 54 for range time.Tick(time.Millisecond * 20) { 55 select { 56 case <-seenPacket.Done(): 57 return 58 default: 59 assert.NoError(t, track.WriteSample(media.Sample{Data: []byte{0xAA}, Duration: time.Second})) 60 } 61 } 62 }() 63 64 assert.NoError(t, rtpSender.Stop()) 65 assert.NoError(t, rtpReceiver.Stop()) 66 67 assert.NoError(t, stackA.close()) 68 assert.NoError(t, stackB.close()) 69 }