blob: b58ed1a45a54ed07b9d87fc243082bd9de070c7b [file] [log] [blame]
Sami Kalliomäkiff1de0a2018-05-16 12:49:47 +02001/*
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
18namespace webrtc {
19namespace test {
20
21namespace {
22class 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
37TEST(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
52TEST(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 Jedvertc7da2662018-05-18 12:13:56 +020065 jni::Java_JavaVideoSourceTestHelper_startCapture(
Sami Kalliomäkiff1de0a2018-05-16 12:49:47 +020066 env, video_track_source->GetJavaVideoCapturerObserver(env),
67 true /* success */);
Magnus Jedvert65070542018-06-14 12:23:01 +020068 const int width = 20;
69 const int height = 32;
70 const int rotation = 180;
Magnus Jedvertc7da2662018-05-18 12:13:56 +020071 jni::Java_JavaVideoSourceTestHelper_deliverFrame(
Magnus Jedvert65070542018-06-14 12:23:01 +020072 env, width, height, rotation,
73 video_track_source->GetJavaVideoCapturerObserver(env));
Sami Kalliomäkiff1de0a2018-05-16 12:49:47 +020074
75 std::vector<VideoFrame> frames = test_video_sink.GetFrames();
76 ASSERT_EQ(1u, frames.size());
77 webrtc::VideoFrame frame = frames[0];
Magnus Jedvert65070542018-06-14 12:23:01 +020078 EXPECT_EQ(width, frame.width());
79 EXPECT_EQ(height, frame.height());
80 EXPECT_EQ(rotation, frame.rotation());
Sami Kalliomäkiff1de0a2018-05-16 12:49:47 +020081}
82
83TEST(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 Jedvertc7da2662018-05-18 12:13:56 +020093 jni::Java_JavaVideoSourceTestHelper_startCapture(
Sami Kalliomäkiff1de0a2018-05-16 12:49:47 +020094 env, video_track_source->GetJavaVideoCapturerObserver(env),
95 true /* success */);
96
97 EXPECT_EQ(VideoTrackSourceInterface::SourceState::kLive,
98 video_track_source->state());
99}
100
101TEST(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 Jedvertc7da2662018-05-18 12:13:56 +0200111 jni::Java_JavaVideoSourceTestHelper_startCapture(
Sami Kalliomäkiff1de0a2018-05-16 12:49:47 +0200112 env, video_track_source->GetJavaVideoCapturerObserver(env),
113 false /* success */);
114
115 EXPECT_EQ(VideoTrackSourceInterface::SourceState::kEnded,
116 video_track_source->state());
117}
118
119TEST(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 Jedvertc7da2662018-05-18 12:13:56 +0200129 jni::Java_JavaVideoSourceTestHelper_startCapture(
Sami Kalliomäkiff1de0a2018-05-16 12:49:47 +0200130 env, video_track_source->GetJavaVideoCapturerObserver(env),
131 true /* success */);
Magnus Jedvertc7da2662018-05-18 12:13:56 +0200132 jni::Java_JavaVideoSourceTestHelper_stopCapture(
Sami Kalliomäkiff1de0a2018-05-16 12:49:47 +0200133 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