@@ -199,8 +199,9 @@ func TestMediaPort(t *testing.T) {
199199 log := logger .GetLogger ()
200200
201201 m1 , err := NewMediaPortWith (log .WithName ("one" ), nil , c1 , & MediaOptions {
202- IP : newIP ("1.1.1.1" ),
203- Ports : rtcconfig.PortRange {Start : 10000 },
202+ IP : newIP ("1.1.1.1" ),
203+ Ports : rtcconfig.PortRange {Start : 10000 },
204+ NoInputResample : true ,
204205 }, tconf .Rate )
205206 require .NoError (t , err )
206207 defer m1 .Close ()
@@ -235,11 +236,13 @@ func TestMediaPort(t *testing.T) {
235236 err = m2 .SetConfig (conf )
236237 require .NoError (t , err )
237238
238- require .Equal (t , info .SDPName , m1 .Config ().Audio .Codec .Info ().SDPName )
239- require .Equal (t , info .SDPName , m2 .Config ().Audio .Codec .Info ().SDPName )
239+ codec1 := m1 .Config ().Audio .Codec
240+ codec2 := m2 .Config ().Audio .Codec
241+ require .Equal (t , info .SDPName , codec1 .Info ().SDPName )
242+ require .Equal (t , info .SDPName , codec2 .Info ().SDPName )
240243
241244 var buf1 msdk.PCM16Sample
242- bw1 := msdk .NewPCM16BufferWriter (& buf1 , tconf . Rate )
245+ bw1 := msdk .NewPCM16BufferWriter (& buf1 , codec1 . Info (). SampleRate )
243246 m1 .WriteAudioTo (bw1 )
244247
245248 var buf2 msdk.PCM16Sample
@@ -257,7 +260,8 @@ func TestMediaPort(t *testing.T) {
257260 sample2 [i ] = - 5116
258261 }
259262
260- writes := 1
263+ writes1 := 1
264+ writes2 := 1
261265 if tconf .Rate == nativeRate {
262266 expChain := fmt .Sprintf ("Switch(%d) -> %s(encode) -> RTP(%d)" , nativeRate , name , nativeRate )
263267 require .Equal (t , expChain , w1 .String ())
@@ -271,20 +275,31 @@ func TestMediaPort(t *testing.T) {
271275 require .Equal (t , expChain , w1 .String ())
272276 require .Equal (t , expChain , w2 .String ())
273277
274- expChain = fmt .Sprintf ("RTP(%d) -> %s(decode) -> Resample(%d->48000) -> Switch(48000) -> Buffer(48000)" , nativeRate , name , nativeRate )
275- require .Equal (t , expChain , PrintAudioInWriter (m1 ))
276- require .Equal (t , expChain , PrintAudioInWriter (m2 ))
277-
278- writes += 2 // resampler will buffer a few frames
278+ // This side does not resample the received audio, it uses sample rate of the RTP source.
279+ expChain1 := fmt .Sprintf ("RTP(%d) -> %s(decode) -> Switch(%d) -> Buffer(%d)" , nativeRate , name , nativeRate , nativeRate )
280+ // This side resamples the received audio to the expected sample rate.
281+ expChain2 := fmt .Sprintf ("RTP(%d) -> %s(decode) -> Resample(%d->48000) -> Switch(48000) -> Buffer(48000)" , nativeRate , name , nativeRate )
282+ require .Equal (t , expChain1 , PrintAudioInWriter (m1 ))
283+ require .Equal (t , expChain2 , PrintAudioInWriter (m2 ))
284+
285+ // resampler will buffer a few frames
286+ writes1 += 2
287+ writes2 += 2
288+ // a few more because of higher resample quality required
279289 if nativeRate == 8000 {
280- writes += 3 // a few more because of higher resample quality required
290+ writes1 += 3
291+ writes2 += 5
292+ }
293+ if strings .HasPrefix (info .SDPName , "G722/" ) {
294+ writes2 += 1
281295 }
282296 }
283297
284- for range writes {
298+ for range writes1 {
285299 err = w1 .WriteSample (sample1 )
286300 require .NoError (t , err )
287-
301+ }
302+ for range writes2 {
288303 err = w2 .WriteSample (sample2 )
289304 require .NoError (t , err )
290305 }
@@ -309,6 +324,7 @@ func TestMediaPort(t *testing.T) {
309324}
310325
311326func checkPCM (t testing.TB , exp , got msdk.PCM16Sample ) {
327+ t .Helper ()
312328 require .Equal (t , len (exp ), len (got ))
313329 expSamples := slices .Clone (exp )
314330 slices .Sort (expSamples )
@@ -338,6 +354,7 @@ func newMediaPair(t testing.TB, opt1, opt2 *MediaOptions) (m1, m2 *MediaPort) {
338354
339355 opt1 .IP = newIP ("1.1.1.1" )
340356 opt1 .Ports = rtcconfig.PortRange {Start : 10000 }
357+ opt1 .NoInputResample = true
341358
342359 opt2 .IP = newIP ("2.2.2.2" )
343360 opt2 .Ports = rtcconfig.PortRange {Start : 20000 }
0 commit comments