1 /* 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 package org.webrtc; 12 13 import static org.junit.Assert.assertEquals; 14 import static org.junit.Assert.fail; 15 16 import android.support.test.InstrumentationRegistry; 17 import androidx.test.filters.SmallTest; 18 import org.junit.Before; 19 import org.junit.Test; 20 21 /** Unit tests for {@link VideoTrack}. */ 22 public class VideoTrackTest { 23 private PeerConnectionFactory factory; 24 private VideoSource videoSource; 25 private VideoTrack videoTrack; 26 27 @Before setUp()28 public void setUp() { 29 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 30 .builder(InstrumentationRegistry.getTargetContext()) 31 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 32 .createInitializationOptions()); 33 34 factory = PeerConnectionFactory.builder().createPeerConnectionFactory(); 35 videoSource = factory.createVideoSource(/* isScreencast= */ false); 36 videoTrack = factory.createVideoTrack("video", videoSource); 37 } 38 39 @Test 40 @SmallTest testAddingNullVideoSink()41 public void testAddingNullVideoSink() { 42 try { 43 videoTrack.addSink(/* sink= */ null); 44 fail("Should have thrown an IllegalArgumentException."); 45 } catch (IllegalArgumentException e) { 46 // Expected path. 47 } 48 } 49 50 @Test 51 @SmallTest testRemovingNullVideoSink()52 public void testRemovingNullVideoSink() { 53 videoTrack.removeSink(/* sink= */ null); 54 } 55 56 @Test 57 @SmallTest testRemovingNonExistantVideoSink()58 public void testRemovingNonExistantVideoSink() { 59 final VideoSink videoSink = new VideoSink() { 60 @Override 61 public void onFrame(VideoFrame frame) {} 62 }; 63 videoTrack.removeSink(videoSink); 64 } 65 66 @Test 67 @SmallTest testAddingSameVideoSinkMultipleTimes()68 public void testAddingSameVideoSinkMultipleTimes() { 69 class FrameCounter implements VideoSink { 70 private int count; 71 72 public int getCount() { 73 return count; 74 } 75 76 @Override 77 public void onFrame(VideoFrame frame) { 78 count += 1; 79 } 80 } 81 final FrameCounter frameCounter = new FrameCounter(); 82 83 final VideoFrame videoFrame = new VideoFrame( 84 JavaI420Buffer.allocate(/* width= */ 32, /* height= */ 32), /* rotation= */ 0, 85 /* timestampNs= */ 0); 86 87 videoTrack.addSink(frameCounter); 88 videoTrack.addSink(frameCounter); 89 videoSource.getCapturerObserver().onFrameCaptured(videoFrame); 90 91 // Even though we called addSink() multiple times, we should only get one frame out. 92 assertEquals(1, frameCounter.count); 93 } 94 95 @Test 96 @SmallTest testAddingAndRemovingVideoSink()97 public void testAddingAndRemovingVideoSink() { 98 final VideoFrame videoFrame = new VideoFrame( 99 JavaI420Buffer.allocate(/* width= */ 32, /* height= */ 32), /* rotation= */ 0, 100 /* timestampNs= */ 0); 101 102 final VideoSink failSink = new VideoSink() { 103 @Override 104 public void onFrame(VideoFrame frame) { 105 fail("onFrame() should not be called on removed sink"); 106 } 107 }; 108 videoTrack.addSink(failSink); 109 videoTrack.removeSink(failSink); 110 videoSource.getCapturerObserver().onFrameCaptured(videoFrame); 111 } 112 } 113