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/JavaVideoSourceTestHelper_jni.h"
15 #include "sdk/android/native_api/video/video_source.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:
OnFrame(const VideoFrame & frame)24 void OnFrame(const VideoFrame& frame) { frames_.push_back(frame); }
25
GetFrames()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
TEST(JavaVideoSourceTest,CreateJavaVideoSource)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(
44 env, rtc::ThreadManager::Instance()->CurrentThread(),
45 false /* is_screencast */, true /* align_timestamps */);
46
47 ASSERT_NE(nullptr, video_track_source);
48 EXPECT_NE(nullptr,
49 video_track_source->GetJavaVideoCapturerObserver(env).obj());
50 }
51
TEST(JavaVideoSourceTest,OnFrameCapturedFrameIsDeliveredToSink)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(
61 env, rtc::ThreadManager::Instance()->CurrentThread(),
62 false /* is_screencast */, true /* align_timestamps */);
63 video_track_source->AddOrUpdateSink(&test_video_sink, rtc::VideoSinkWants());
64
65 jni::Java_JavaVideoSourceTestHelper_startCapture(
66 env, video_track_source->GetJavaVideoCapturerObserver(env),
67 true /* success */);
68 const int width = 20;
69 const int height = 32;
70 const int rotation = 180;
71 const int64_t timestamp = 987654321;
72 jni::Java_JavaVideoSourceTestHelper_deliverFrame(
73 env, width, height, rotation, timestamp,
74 video_track_source->GetJavaVideoCapturerObserver(env));
75
76 std::vector<VideoFrame> frames = test_video_sink.GetFrames();
77 ASSERT_EQ(1u, frames.size());
78 webrtc::VideoFrame frame = frames[0];
79 EXPECT_EQ(width, frame.width());
80 EXPECT_EQ(height, frame.height());
81 EXPECT_EQ(rotation, frame.rotation());
82 }
83
TEST(JavaVideoSourceTest,OnFrameCapturedFrameIsDeliveredToSinkWithPreservedTimestamp)84 TEST(JavaVideoSourceTest,
85 OnFrameCapturedFrameIsDeliveredToSinkWithPreservedTimestamp) {
86 TestVideoSink test_video_sink;
87
88 JNIEnv* env = AttachCurrentThreadIfNeeded();
89 // Wrap test thread so it can be used as the signaling thread.
90 rtc::ThreadManager::Instance()->WrapCurrentThread();
91
92 rtc::scoped_refptr<JavaVideoTrackSourceInterface> video_track_source =
93 CreateJavaVideoSource(
94 env, rtc::ThreadManager::Instance()->CurrentThread(),
95 false /* is_screencast */, false /* align_timestamps */);
96 video_track_source->AddOrUpdateSink(&test_video_sink, rtc::VideoSinkWants());
97
98 jni::Java_JavaVideoSourceTestHelper_startCapture(
99 env, video_track_source->GetJavaVideoCapturerObserver(env),
100 true /* success */);
101 const int width = 20;
102 const int height = 32;
103 const int rotation = 180;
104 const int64_t timestamp = 987654321;
105 jni::Java_JavaVideoSourceTestHelper_deliverFrame(
106 env, width, height, rotation, 987654321,
107 video_track_source->GetJavaVideoCapturerObserver(env));
108
109 std::vector<VideoFrame> frames = test_video_sink.GetFrames();
110 ASSERT_EQ(1u, frames.size());
111 webrtc::VideoFrame frame = frames[0];
112 EXPECT_EQ(width, frame.width());
113 EXPECT_EQ(height, frame.height());
114 EXPECT_EQ(rotation, frame.rotation());
115 EXPECT_EQ(timestamp / 1000, frame.timestamp_us());
116 }
117
TEST(JavaVideoSourceTest,CapturerStartedSuccessStateBecomesLive)118 TEST(JavaVideoSourceTest, CapturerStartedSuccessStateBecomesLive) {
119 JNIEnv* env = AttachCurrentThreadIfNeeded();
120 // Wrap test thread so it can be used as the signaling thread.
121 rtc::ThreadManager::Instance()->WrapCurrentThread();
122
123 rtc::scoped_refptr<JavaVideoTrackSourceInterface> video_track_source =
124 CreateJavaVideoSource(
125 env, rtc::ThreadManager::Instance()->CurrentThread(),
126 false /* is_screencast */, true /* align_timestamps */);
127
128 jni::Java_JavaVideoSourceTestHelper_startCapture(
129 env, video_track_source->GetJavaVideoCapturerObserver(env),
130 true /* success */);
131
132 EXPECT_EQ(VideoTrackSourceInterface::SourceState::kLive,
133 video_track_source->state());
134 }
135
TEST(JavaVideoSourceTest,CapturerStartedFailureStateBecomesEnded)136 TEST(JavaVideoSourceTest, CapturerStartedFailureStateBecomesEnded) {
137 JNIEnv* env = AttachCurrentThreadIfNeeded();
138 // Wrap test thread so it can be used as the signaling thread.
139 rtc::ThreadManager::Instance()->WrapCurrentThread();
140
141 rtc::scoped_refptr<JavaVideoTrackSourceInterface> video_track_source =
142 CreateJavaVideoSource(
143 env, rtc::ThreadManager::Instance()->CurrentThread(),
144 false /* is_screencast */, true /* align_timestamps */);
145
146 jni::Java_JavaVideoSourceTestHelper_startCapture(
147 env, video_track_source->GetJavaVideoCapturerObserver(env),
148 false /* success */);
149
150 EXPECT_EQ(VideoTrackSourceInterface::SourceState::kEnded,
151 video_track_source->state());
152 }
153
TEST(JavaVideoSourceTest,CapturerStoppedStateBecomesEnded)154 TEST(JavaVideoSourceTest, CapturerStoppedStateBecomesEnded) {
155 JNIEnv* env = AttachCurrentThreadIfNeeded();
156 // Wrap test thread so it can be used as the signaling thread.
157 rtc::ThreadManager::Instance()->WrapCurrentThread();
158
159 rtc::scoped_refptr<JavaVideoTrackSourceInterface> video_track_source =
160 CreateJavaVideoSource(
161 env, rtc::ThreadManager::Instance()->CurrentThread(),
162 false /* is_screencast */, true /* align_timestamps */);
163
164 jni::Java_JavaVideoSourceTestHelper_startCapture(
165 env, video_track_source->GetJavaVideoCapturerObserver(env),
166 true /* success */);
167 jni::Java_JavaVideoSourceTestHelper_stopCapture(
168 env, video_track_source->GetJavaVideoCapturerObserver(env));
169
170 EXPECT_EQ(VideoTrackSourceInterface::SourceState::kEnded,
171 video_track_source->state());
172 }
173
174 } // namespace test
175 } // namespace webrtc
176