Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | #include <vector> |
| 12 | |
| 13 | #include "api/video/video_sink_interface.h" |
| 14 | #include "sdk/android/generated_native_unittests_jni/jni/JavaVideoSourceTestHelper_jni.h" |
| 15 | #include "sdk/android/native_api/video/videosource.h" |
| 16 | #include "test/gtest.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | |
| 21 | namespace { |
| 22 | class TestVideoSink : public rtc::VideoSinkInterface<VideoFrame> { |
| 23 | public: |
| 24 | void OnFrame(const VideoFrame& frame) { frames_.push_back(frame); } |
| 25 | |
| 26 | std::vector<VideoFrame> GetFrames() { |
| 27 | std::vector<VideoFrame> temp = frames_; |
| 28 | frames_.clear(); |
| 29 | return temp; |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | std::vector<VideoFrame> frames_; |
| 34 | }; |
| 35 | } // namespace |
| 36 | |
| 37 | TEST(JavaVideoSourceTest, CreateJavaVideoSource) { |
| 38 | JNIEnv* env = AttachCurrentThreadIfNeeded(); |
| 39 | // Wrap test thread so it can be used as the signaling thread. |
| 40 | rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 41 | |
| 42 | rtc::scoped_refptr<JavaVideoTrackSourceInterface> video_track_source = |
| 43 | CreateJavaVideoSource(env, |
| 44 | rtc::ThreadManager::Instance()->CurrentThread(), |
| 45 | false /* is_screencast */); |
| 46 | |
| 47 | ASSERT_NE(nullptr, video_track_source); |
| 48 | EXPECT_NE(nullptr, |
| 49 | video_track_source->GetJavaVideoCapturerObserver(env).obj()); |
| 50 | } |
| 51 | |
| 52 | TEST(JavaVideoSourceTest, OnFrameCapturedFrameIsDeliveredToSink) { |
| 53 | TestVideoSink test_video_sink; |
| 54 | |
| 55 | JNIEnv* env = AttachCurrentThreadIfNeeded(); |
| 56 | // Wrap test thread so it can be used as the signaling thread. |
| 57 | rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 58 | |
| 59 | rtc::scoped_refptr<JavaVideoTrackSourceInterface> video_track_source = |
| 60 | CreateJavaVideoSource(env, |
| 61 | rtc::ThreadManager::Instance()->CurrentThread(), |
| 62 | false /* is_screencast */); |
| 63 | video_track_source->AddOrUpdateSink(&test_video_sink, rtc::VideoSinkWants()); |
| 64 | |
Magnus Jedvert | c7da266 | 2018-05-18 12:13:56 +0200 | [diff] [blame] | 65 | jni::Java_JavaVideoSourceTestHelper_startCapture( |
Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 66 | env, video_track_source->GetJavaVideoCapturerObserver(env), |
| 67 | true /* success */); |
Magnus Jedvert | 6507054 | 2018-06-14 12:23:01 +0200 | [diff] [blame] | 68 | const int width = 20; |
| 69 | const int height = 32; |
| 70 | const int rotation = 180; |
Magnus Jedvert | c7da266 | 2018-05-18 12:13:56 +0200 | [diff] [blame] | 71 | jni::Java_JavaVideoSourceTestHelper_deliverFrame( |
Magnus Jedvert | 6507054 | 2018-06-14 12:23:01 +0200 | [diff] [blame] | 72 | env, width, height, rotation, |
| 73 | video_track_source->GetJavaVideoCapturerObserver(env)); |
Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 74 | |
| 75 | std::vector<VideoFrame> frames = test_video_sink.GetFrames(); |
| 76 | ASSERT_EQ(1u, frames.size()); |
| 77 | webrtc::VideoFrame frame = frames[0]; |
Magnus Jedvert | 6507054 | 2018-06-14 12:23:01 +0200 | [diff] [blame] | 78 | EXPECT_EQ(width, frame.width()); |
| 79 | EXPECT_EQ(height, frame.height()); |
| 80 | EXPECT_EQ(rotation, frame.rotation()); |
Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | TEST(JavaVideoSourceTest, CapturerStartedSuccessStateBecomesLive) { |
| 84 | JNIEnv* env = AttachCurrentThreadIfNeeded(); |
| 85 | // Wrap test thread so it can be used as the signaling thread. |
| 86 | rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 87 | |
| 88 | rtc::scoped_refptr<JavaVideoTrackSourceInterface> video_track_source = |
| 89 | CreateJavaVideoSource(env, |
| 90 | rtc::ThreadManager::Instance()->CurrentThread(), |
| 91 | false /* is_screencast */); |
| 92 | |
Magnus Jedvert | c7da266 | 2018-05-18 12:13:56 +0200 | [diff] [blame] | 93 | jni::Java_JavaVideoSourceTestHelper_startCapture( |
Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 94 | env, video_track_source->GetJavaVideoCapturerObserver(env), |
| 95 | true /* success */); |
| 96 | |
| 97 | EXPECT_EQ(VideoTrackSourceInterface::SourceState::kLive, |
| 98 | video_track_source->state()); |
| 99 | } |
| 100 | |
| 101 | TEST(JavaVideoSourceTest, CapturerStartedFailureStateBecomesEnded) { |
| 102 | JNIEnv* env = AttachCurrentThreadIfNeeded(); |
| 103 | // Wrap test thread so it can be used as the signaling thread. |
| 104 | rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 105 | |
| 106 | rtc::scoped_refptr<JavaVideoTrackSourceInterface> video_track_source = |
| 107 | CreateJavaVideoSource(env, |
| 108 | rtc::ThreadManager::Instance()->CurrentThread(), |
| 109 | false /* is_screencast */); |
| 110 | |
Magnus Jedvert | c7da266 | 2018-05-18 12:13:56 +0200 | [diff] [blame] | 111 | jni::Java_JavaVideoSourceTestHelper_startCapture( |
Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 112 | env, video_track_source->GetJavaVideoCapturerObserver(env), |
| 113 | false /* success */); |
| 114 | |
| 115 | EXPECT_EQ(VideoTrackSourceInterface::SourceState::kEnded, |
| 116 | video_track_source->state()); |
| 117 | } |
| 118 | |
| 119 | TEST(JavaVideoSourceTest, CapturerStoppedStateBecomesEnded) { |
| 120 | JNIEnv* env = AttachCurrentThreadIfNeeded(); |
| 121 | // Wrap test thread so it can be used as the signaling thread. |
| 122 | rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 123 | |
| 124 | rtc::scoped_refptr<JavaVideoTrackSourceInterface> video_track_source = |
| 125 | CreateJavaVideoSource(env, |
| 126 | rtc::ThreadManager::Instance()->CurrentThread(), |
| 127 | false /* is_screencast */); |
| 128 | |
Magnus Jedvert | c7da266 | 2018-05-18 12:13:56 +0200 | [diff] [blame] | 129 | jni::Java_JavaVideoSourceTestHelper_startCapture( |
Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 130 | env, video_track_source->GetJavaVideoCapturerObserver(env), |
| 131 | true /* success */); |
Magnus Jedvert | c7da266 | 2018-05-18 12:13:56 +0200 | [diff] [blame] | 132 | jni::Java_JavaVideoSourceTestHelper_stopCapture( |
Sami Kalliomäki | ff1de0a | 2018-05-16 12:49:47 +0200 | [diff] [blame] | 133 | env, video_track_source->GetJavaVideoCapturerObserver(env)); |
| 134 | |
| 135 | EXPECT_EQ(VideoTrackSourceInterface::SourceState::kEnded, |
| 136 | video_track_source->state()); |
| 137 | } |
| 138 | |
| 139 | } // namespace test |
| 140 | } // namespace webrtc |