OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 29 matching lines...) Expand all Loading... |
40 int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni, | 40 int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni, |
41 jobject appliction_context) { | 41 jobject appliction_context) { |
42 if (application_context_) { | 42 if (application_context_) { |
43 jni->DeleteGlobalRef(application_context_); | 43 jni->DeleteGlobalRef(application_context_); |
44 } | 44 } |
45 application_context_ = NewGlobalRef(jni, appliction_context); | 45 application_context_ = NewGlobalRef(jni, appliction_context); |
46 | 46 |
47 return 0; | 47 return 0; |
48 } | 48 } |
49 | 49 |
50 AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni, | 50 AndroidVideoCapturerJni::AndroidVideoCapturerJni( |
51 jobject j_video_capturer) | 51 JNIEnv* jni, |
52 : j_capturer_global_(jni, j_video_capturer), | 52 jobject j_video_capturer, |
| 53 jobject j_surface_texture_helper) |
| 54 : j_video_capturer_(jni, j_video_capturer), |
| 55 j_surface_texture_helper_(jni, j_surface_texture_helper), |
53 j_video_capturer_class_( | 56 j_video_capturer_class_( |
54 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")), | 57 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")), |
55 j_observer_class_( | 58 j_observer_class_( |
56 jni, | 59 jni, |
57 FindClass(jni, | 60 FindClass(jni, |
58 "org/webrtc/VideoCapturerAndroid$NativeObserver")), | 61 "org/webrtc/VideoCapturerAndroid$NativeObserver")), |
59 capturer_(nullptr) { | 62 capturer_(nullptr) { |
60 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor"; | 63 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor"; |
61 thread_checker_.DetachFromThread(); | 64 thread_checker_.DetachFromThread(); |
62 } | 65 } |
63 | 66 |
64 AndroidVideoCapturerJni::~AndroidVideoCapturerJni() { | 67 AndroidVideoCapturerJni::~AndroidVideoCapturerJni() { |
65 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor"; | 68 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor"; |
66 jni()->CallVoidMethod( | 69 jni()->CallVoidMethod( |
67 *j_capturer_global_, | 70 *j_video_capturer_, |
68 GetMethodID(jni(), *j_video_capturer_class_, "release", "()V")); | 71 GetMethodID(jni(), *j_video_capturer_class_, "release", "()V")); |
69 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.release()"; | 72 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.release()"; |
70 } | 73 } |
71 | 74 |
72 void AndroidVideoCapturerJni::Start(int width, int height, int framerate, | 75 void AndroidVideoCapturerJni::Start(int width, int height, int framerate, |
73 webrtc::AndroidVideoCapturer* capturer) { | 76 webrtc::AndroidVideoCapturer* capturer) { |
74 LOG(LS_INFO) << "AndroidVideoCapturerJni start"; | 77 LOG(LS_INFO) << "AndroidVideoCapturerJni start"; |
75 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 78 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
76 { | 79 { |
77 rtc::CritScope cs(&capturer_lock_); | 80 rtc::CritScope cs(&capturer_lock_); |
78 RTC_CHECK(capturer_ == nullptr); | 81 RTC_CHECK(capturer_ == nullptr); |
79 RTC_CHECK(invoker_.get() == nullptr); | 82 RTC_CHECK(invoker_.get() == nullptr); |
80 capturer_ = capturer; | 83 capturer_ = capturer; |
81 invoker_.reset(new rtc::GuardedAsyncInvoker()); | 84 invoker_.reset(new rtc::GuardedAsyncInvoker()); |
82 } | 85 } |
83 jobject j_frame_observer = | 86 jobject j_frame_observer = |
84 jni()->NewObject(*j_observer_class_, | 87 jni()->NewObject(*j_observer_class_, |
85 GetMethodID(jni(), *j_observer_class_, "<init>", "(J)V"), | 88 GetMethodID(jni(), *j_observer_class_, "<init>", "(J)V"), |
86 jlongFromPointer(this)); | 89 jlongFromPointer(this)); |
87 CHECK_EXCEPTION(jni()) << "error during NewObject"; | 90 CHECK_EXCEPTION(jni()) << "error during NewObject"; |
88 | 91 |
89 jmethodID m = GetMethodID( | 92 jmethodID m = GetMethodID( |
90 jni(), *j_video_capturer_class_, "startCapture", | 93 jni(), *j_video_capturer_class_, "startCapture", |
91 "(IIILandroid/content/Context;" | 94 "(IIILandroid/content/Context;" |
92 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V"); | 95 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V"); |
93 jni()->CallVoidMethod(*j_capturer_global_, | 96 jni()->CallVoidMethod(*j_video_capturer_, |
94 m, width, height, | 97 m, width, height, |
95 framerate, | 98 framerate, |
96 application_context_, | 99 application_context_, |
97 j_frame_observer); | 100 j_frame_observer); |
98 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture"; | 101 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture"; |
99 } | 102 } |
100 | 103 |
101 void AndroidVideoCapturerJni::Stop() { | 104 void AndroidVideoCapturerJni::Stop() { |
102 LOG(LS_INFO) << "AndroidVideoCapturerJni stop"; | 105 LOG(LS_INFO) << "AndroidVideoCapturerJni stop"; |
103 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 106 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
104 { | 107 { |
105 rtc::CritScope cs(&capturer_lock_); | 108 rtc::CritScope cs(&capturer_lock_); |
106 // Destroying |invoker_| will cancel all pending calls to |capturer_|. | 109 // Destroying |invoker_| will cancel all pending calls to |capturer_|. |
107 invoker_ = nullptr; | 110 invoker_ = nullptr; |
108 capturer_ = nullptr; | 111 capturer_ = nullptr; |
109 } | 112 } |
110 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_, | 113 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_, |
111 "stopCapture", "()V"); | 114 "stopCapture", "()V"); |
112 jni()->CallVoidMethod(*j_capturer_global_, m); | 115 jni()->CallVoidMethod(*j_video_capturer_, m); |
113 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture"; | 116 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture"; |
114 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done"; | 117 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done"; |
115 } | 118 } |
116 | 119 |
117 template <typename... Args> | 120 template <typename... Args> |
118 void AndroidVideoCapturerJni::AsyncCapturerInvoke( | 121 void AndroidVideoCapturerJni::AsyncCapturerInvoke( |
119 const char* method_name, | 122 const char* method_name, |
120 void (webrtc::AndroidVideoCapturer::*method)(Args...), | 123 void (webrtc::AndroidVideoCapturer::*method)(Args...), |
121 typename Identity<Args>::type... args) { | 124 typename Identity<Args>::type... args) { |
122 rtc::CritScope cs(&capturer_lock_); | 125 rtc::CritScope cs(&capturer_lock_); |
123 if (!invoker_) { | 126 if (!invoker_) { |
124 LOG(LS_WARNING) << method_name << "() called for closed capturer."; | 127 LOG(LS_WARNING) << method_name << "() called for closed capturer."; |
125 return; | 128 return; |
126 } | 129 } |
127 invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...)); | 130 invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...)); |
128 } | 131 } |
129 | 132 |
130 void AndroidVideoCapturerJni::ReturnBuffer(int64_t time_stamp) { | 133 void AndroidVideoCapturerJni::ReturnBuffer(int64_t time_stamp) { |
131 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_, | 134 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_, |
132 "returnBuffer", "(J)V"); | 135 "returnBuffer", "(J)V"); |
133 jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp); | 136 jni()->CallVoidMethod(*j_video_capturer_, m, time_stamp); |
134 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer"; | 137 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer"; |
135 } | 138 } |
136 | 139 |
137 std::string AndroidVideoCapturerJni::GetSupportedFormats() { | 140 std::string AndroidVideoCapturerJni::GetSupportedFormats() { |
138 jmethodID m = | 141 jmethodID m = |
139 GetMethodID(jni(), *j_video_capturer_class_, | 142 GetMethodID(jni(), *j_video_capturer_class_, |
140 "getSupportedFormatsAsJson", "()Ljava/lang/String;"); | 143 "getSupportedFormatsAsJson", "()Ljava/lang/String;"); |
141 jstring j_json_caps = | 144 jstring j_json_caps = |
142 (jstring) jni()->CallObjectMethod(*j_capturer_global_, m); | 145 (jstring) jni()->CallObjectMethod(*j_video_capturer_, m); |
143 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson"; | 146 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson"; |
144 return JavaToStdString(jni(), j_json_caps); | 147 return JavaToStdString(jni(), j_json_caps); |
145 } | 148 } |
146 | 149 |
147 void AndroidVideoCapturerJni::OnCapturerStarted(bool success) { | 150 void AndroidVideoCapturerJni::OnCapturerStarted(bool success) { |
148 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success; | 151 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success; |
149 AsyncCapturerInvoke("OnCapturerStarted", | 152 AsyncCapturerInvoke("OnCapturerStarted", |
150 &webrtc::AndroidVideoCapturer::OnCapturerStarted, | 153 &webrtc::AndroidVideoCapturer::OnCapturerStarted, |
151 success); | 154 success); |
152 } | 155 } |
(...skipping 26 matching lines...) Expand all Loading... |
179 &webrtc::AndroidVideoCapturer::OnIncomingFrame, | 182 &webrtc::AndroidVideoCapturer::OnIncomingFrame, |
180 buffer, rotation, timestamp_ns); | 183 buffer, rotation, timestamp_ns); |
181 } | 184 } |
182 | 185 |
183 void AndroidVideoCapturerJni::OnTextureFrame(int width, | 186 void AndroidVideoCapturerJni::OnTextureFrame(int width, |
184 int height, | 187 int height, |
185 int64_t timestamp_ns, | 188 int64_t timestamp_ns, |
186 const NativeHandleImpl& handle) { | 189 const NativeHandleImpl& handle) { |
187 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer( | 190 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer( |
188 new rtc::RefCountedObject<AndroidTextureBuffer>( | 191 new rtc::RefCountedObject<AndroidTextureBuffer>( |
189 width, height, handle, | 192 width, height, handle, *j_surface_texture_helper_, |
190 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this, | 193 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this, |
191 timestamp_ns))); | 194 timestamp_ns))); |
192 AsyncCapturerInvoke("OnIncomingFrame", | 195 AsyncCapturerInvoke("OnIncomingFrame", |
193 &webrtc::AndroidVideoCapturer::OnIncomingFrame, | 196 &webrtc::AndroidVideoCapturer::OnIncomingFrame, |
194 buffer, 0, timestamp_ns); | 197 buffer, 0, timestamp_ns); |
195 } | 198 } |
196 | 199 |
197 void AndroidVideoCapturerJni::OnOutputFormatRequest(int width, | 200 void AndroidVideoCapturerJni::OnOutputFormatRequest(int width, |
198 int height, | 201 int height, |
199 int fps) { | 202 int fps) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 | 244 |
242 JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest) | 245 JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest) |
243 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height, | 246 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height, |
244 jint j_fps) { | 247 jint j_fps) { |
245 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest"; | 248 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest"; |
246 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest( | 249 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest( |
247 j_width, j_height, j_fps); | 250 j_width, j_height, j_fps); |
248 } | 251 } |
249 | 252 |
250 JOW(jlong, VideoCapturerAndroid_nativeCreateVideoCapturer) | 253 JOW(jlong, VideoCapturerAndroid_nativeCreateVideoCapturer) |
251 (JNIEnv* jni, jclass, jobject j_video_capturer) { | 254 (JNIEnv* jni, jclass, |
| 255 jobject j_video_capturer, jobject j_surface_texture_helper) { |
252 rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate = | 256 rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate = |
253 new rtc::RefCountedObject<AndroidVideoCapturerJni>(jni, j_video_capturer); | 257 new rtc::RefCountedObject<AndroidVideoCapturerJni>( |
| 258 jni, j_video_capturer, j_surface_texture_helper); |
254 rtc::scoped_ptr<cricket::VideoCapturer> capturer( | 259 rtc::scoped_ptr<cricket::VideoCapturer> capturer( |
255 new webrtc::AndroidVideoCapturer(delegate)); | 260 new webrtc::AndroidVideoCapturer(delegate)); |
256 // Caller takes ownership of the cricket::VideoCapturer* pointer. | 261 // Caller takes ownership of the cricket::VideoCapturer* pointer. |
257 return jlongFromPointer(capturer.release()); | 262 return jlongFromPointer(capturer.release()); |
258 } | 263 } |
259 | 264 |
260 } // namespace webrtc_jni | 265 } // namespace webrtc_jni |
OLD | NEW |