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 android.support.test.filters.SmallTest; 18 import org.chromium.base.test.BaseJUnit4ClassRunner; 19 import org.junit.Before; 20 import org.junit.Test; 21 import org.junit.runner.RunWith; 22 23 /** Unit tests for {@link VideoTrack}. */ 24 @RunWith(BaseJUnit4ClassRunner.class) 25 public class VideoTrackTest { 26 private PeerConnectionFactory factory; 27 private VideoSource videoSource; 28 private VideoTrack videoTrack; 29 30 @Before setUp()31 public void setUp() { 32 PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions 33 .builder(InstrumentationRegistry.getTargetContext()) 34 .setNativeLibraryName(TestConstants.NATIVE_LIBRARY) 35 .createInitializationOptions()); 36 37 factory = PeerConnectionFactory.builder().createPeerConnectionFactory(); 38 videoSource = factory.createVideoSource(/* isScreencast= */ false); 39 videoTrack = factory.createVideoTrack("video", videoSource); 40 } 41 42 @Test 43 @SmallTest testAddingNullVideoSink()44 public void testAddingNullVideoSink() { 45 try { 46 videoTrack.addSink(/* sink= */ null); 47 fail("Should have thrown an IllegalArgumentException."); 48 } catch (IllegalArgumentException e) { 49 // Expected path. 50 } 51 } 52 53 @Test 54 @SmallTest testRemovingNullVideoSink()55 public void testRemovingNullVideoSink() { 56 videoTrack.removeSink(/* sink= */ null); 57 } 58 59 @Test 60 @SmallTest testRemovingNonExistantVideoSink()61 public void testRemovingNonExistantVideoSink() { 62 final VideoSink videoSink = new VideoSink() { 63 @Override 64 public void onFrame(VideoFrame frame) {} 65 }; 66 videoTrack.removeSink(videoSink); 67 } 68 69 @Test 70 @SmallTest testAddingSameVideoSinkMultipleTimes()71 public void testAddingSameVideoSinkMultipleTimes() { 72 class FrameCounter implements VideoSink { 73 private int count; 74 75 public int getCount() { 76 return count; 77 } 78 79 @Override 80 public void onFrame(VideoFrame frame) { 81 count += 1; 82 } 83 } 84 final FrameCounter frameCounter = new FrameCounter(); 85 86 final VideoFrame videoFrame = new VideoFrame( 87 JavaI420Buffer.allocate(/* width= */ 32, /* height= */ 32), /* rotation= */ 0, 88 /* timestampNs= */ 0); 89 90 videoTrack.addSink(frameCounter); 91 videoTrack.addSink(frameCounter); 92 videoSource.getCapturerObserver().onFrameCaptured(videoFrame); 93 94 // Even though we called addSink() multiple times, we should only get one frame out. 95 assertEquals(1, frameCounter.count); 96 } 97 98 @Test 99 @SmallTest testAddingAndRemovingVideoSink()100 public void testAddingAndRemovingVideoSink() { 101 final VideoFrame videoFrame = new VideoFrame( 102 JavaI420Buffer.allocate(/* width= */ 32, /* height= */ 32), /* rotation= */ 0, 103 /* timestampNs= */ 0); 104 105 final VideoSink failSink = new VideoSink() { 106 @Override 107 public void onFrame(VideoFrame frame) { 108 fail("onFrame() should not be called on removed sink"); 109 } 110 }; 111 videoTrack.addSink(failSink); 112 videoTrack.removeSink(failSink); 113 videoSource.getCapturerObserver().onFrameCaptured(videoFrame); 114 } 115 } 116