github.com/pion/webrtc/v4@v4.0.1/rtpreceiver_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/interceptor"
    15  	"github.com/pion/transport/v3/test"
    16  	"github.com/pion/webrtc/v4/pkg/media"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  // Assert that SetReadDeadline works as expected
    21  // This test uses VNet since we must have zero loss
    22  func Test_RTPReceiver_SetReadDeadline(t *testing.T) {
    23  	lim := test.TimeOut(time.Second * 30)
    24  	defer lim.Stop()
    25  
    26  	report := test.CheckRoutines(t)
    27  	defer report()
    28  
    29  	sender, receiver, wan := createVNetPair(t, &interceptor.Registry{})
    30  
    31  	track, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion")
    32  	assert.NoError(t, err)
    33  
    34  	_, err = sender.AddTrack(track)
    35  	assert.NoError(t, err)
    36  
    37  	seenPacket, seenPacketCancel := context.WithCancel(context.Background())
    38  	receiver.OnTrack(func(trackRemote *TrackRemote, r *RTPReceiver) {
    39  		// Set Deadline for both RTP and RTCP Stream
    40  		assert.NoError(t, r.SetReadDeadline(time.Now().Add(time.Second)))
    41  		assert.NoError(t, trackRemote.SetReadDeadline(time.Now().Add(time.Second)))
    42  
    43  		// First call will not error because we cache for probing
    44  		_, _, readErr := trackRemote.ReadRTP()
    45  		assert.NoError(t, readErr)
    46  
    47  		_, _, readErr = trackRemote.ReadRTP()
    48  		assert.Error(t, readErr)
    49  
    50  		_, _, readErr = r.ReadRTCP()
    51  		assert.Error(t, readErr)
    52  
    53  		seenPacketCancel()
    54  	})
    55  
    56  	peerConnectionsConnected := untilConnectionState(PeerConnectionStateConnected, sender, receiver)
    57  
    58  	assert.NoError(t, signalPair(sender, receiver))
    59  
    60  	peerConnectionsConnected.Wait()
    61  	assert.NoError(t, track.WriteSample(media.Sample{Data: []byte{0xAA}, Duration: time.Second}))
    62  
    63  	<-seenPacket.Done()
    64  	assert.NoError(t, wan.Stop())
    65  	closePairNow(t, sender, receiver)
    66  }