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