2015-02-13 12:46:51 +00:00
|
|
|
/*
|
|
|
|
|
* libjingle
|
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "talk/app/webrtc/java/jni/androidvideocapturer_jni.h"
|
|
|
|
|
#include "talk/app/webrtc/java/jni/classreferenceholder.h"
|
2015-10-08 15:32:38 +02:00
|
|
|
#include "talk/app/webrtc/java/jni/native_handle_impl.h"
|
2015-02-19 08:43:38 +00:00
|
|
|
#include "webrtc/base/bind.h"
|
2015-08-25 23:22:08 +02:00
|
|
|
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
|
2015-02-13 12:46:51 +00:00
|
|
|
|
|
|
|
|
namespace webrtc_jni {
|
|
|
|
|
|
|
|
|
|
jobject AndroidVideoCapturerJni::application_context_ = nullptr;
|
|
|
|
|
|
|
|
|
|
// static
|
|
|
|
|
int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni,
|
|
|
|
|
jobject appliction_context) {
|
|
|
|
|
if (application_context_) {
|
|
|
|
|
jni->DeleteGlobalRef(application_context_);
|
|
|
|
|
}
|
|
|
|
|
application_context_ = NewGlobalRef(jni, appliction_context);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni,
|
|
|
|
|
jobject j_video_capturer)
|
|
|
|
|
: j_capturer_global_(jni, j_video_capturer),
|
|
|
|
|
j_video_capturer_class_(
|
|
|
|
|
jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
|
2015-02-19 08:43:38 +00:00
|
|
|
j_observer_class_(
|
2015-02-13 12:46:51 +00:00
|
|
|
jni,
|
|
|
|
|
FindClass(jni,
|
2015-02-19 08:43:38 +00:00
|
|
|
"org/webrtc/VideoCapturerAndroid$NativeObserver")),
|
2015-08-25 23:22:08 +02:00
|
|
|
capturer_(nullptr) {
|
2015-04-20 13:00:49 -07:00
|
|
|
LOG(LS_INFO) << "AndroidVideoCapturerJni ctor";
|
2015-02-19 08:43:38 +00:00
|
|
|
thread_checker_.DetachFromThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
|
2015-08-25 23:22:08 +02:00
|
|
|
LOG(LS_INFO) << "AndroidVideoCapturerJni dtor";
|
2015-09-23 12:01:28 +02:00
|
|
|
jni()->CallVoidMethod(
|
|
|
|
|
*j_capturer_global_,
|
|
|
|
|
GetMethodID(jni(), *j_video_capturer_class_, "release", "()V"));
|
|
|
|
|
CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.release()";
|
2015-02-19 08:43:38 +00:00
|
|
|
}
|
2015-02-13 12:46:51 +00:00
|
|
|
|
|
|
|
|
void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
|
|
|
|
|
webrtc::AndroidVideoCapturer* capturer) {
|
2015-04-20 13:00:49 -07:00
|
|
|
LOG(LS_INFO) << "AndroidVideoCapturerJni start";
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
2015-08-25 23:22:08 +02:00
|
|
|
{
|
|
|
|
|
rtc::CritScope cs(&capturer_lock_);
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(capturer_ == nullptr);
|
|
|
|
|
RTC_CHECK(invoker_.get() == nullptr);
|
2015-08-25 23:22:08 +02:00
|
|
|
capturer_ = capturer;
|
|
|
|
|
invoker_.reset(new rtc::GuardedAsyncInvoker());
|
|
|
|
|
}
|
|
|
|
|
jobject j_frame_observer =
|
2015-02-19 08:43:38 +00:00
|
|
|
jni()->NewObject(*j_observer_class_,
|
2015-08-25 23:22:08 +02:00
|
|
|
GetMethodID(jni(), *j_observer_class_, "<init>", "(J)V"),
|
|
|
|
|
jlongFromPointer(this));
|
2015-02-13 12:46:51 +00:00
|
|
|
CHECK_EXCEPTION(jni()) << "error during NewObject";
|
|
|
|
|
|
|
|
|
|
jmethodID m = GetMethodID(
|
|
|
|
|
jni(), *j_video_capturer_class_, "startCapture",
|
|
|
|
|
"(IIILandroid/content/Context;"
|
|
|
|
|
"Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
|
|
|
|
|
jni()->CallVoidMethod(*j_capturer_global_,
|
|
|
|
|
m, width, height,
|
|
|
|
|
framerate,
|
|
|
|
|
application_context_,
|
2015-08-25 23:22:08 +02:00
|
|
|
j_frame_observer);
|
2015-02-13 12:46:51 +00:00
|
|
|
CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 08:43:38 +00:00
|
|
|
void AndroidVideoCapturerJni::Stop() {
|
2015-04-20 13:00:49 -07:00
|
|
|
LOG(LS_INFO) << "AndroidVideoCapturerJni stop";
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
2015-08-25 23:22:08 +02:00
|
|
|
{
|
|
|
|
|
rtc::CritScope cs(&capturer_lock_);
|
|
|
|
|
// Destroying |invoker_| will cancel all pending calls to |capturer_|.
|
|
|
|
|
invoker_ = nullptr;
|
|
|
|
|
capturer_ = nullptr;
|
|
|
|
|
}
|
2015-02-13 12:46:51 +00:00
|
|
|
jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
|
2015-02-19 08:43:38 +00:00
|
|
|
"stopCapture", "()V");
|
|
|
|
|
jni()->CallVoidMethod(*j_capturer_global_, m);
|
2015-02-13 12:46:51 +00:00
|
|
|
CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
|
2015-04-20 13:00:49 -07:00
|
|
|
LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
|
2015-02-25 09:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-25 23:22:08 +02:00
|
|
|
template <typename... Args>
|
|
|
|
|
void AndroidVideoCapturerJni::AsyncCapturerInvoke(
|
|
|
|
|
const char* method_name,
|
|
|
|
|
void (webrtc::AndroidVideoCapturer::*method)(Args...),
|
|
|
|
|
Args... args) {
|
|
|
|
|
rtc::CritScope cs(&capturer_lock_);
|
|
|
|
|
if (!invoker_) {
|
|
|
|
|
LOG(LS_WARNING) << method_name << "() called for closed capturer.";
|
2015-04-20 16:54:42 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2015-08-25 23:22:08 +02:00
|
|
|
invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...));
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
void AndroidVideoCapturerJni::ReturnBuffer(int64_t time_stamp) {
|
2015-02-25 09:20:07 +00:00
|
|
|
jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
|
|
|
|
|
"returnBuffer", "(J)V");
|
|
|
|
|
jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp);
|
2015-04-02 12:30:51 +02:00
|
|
|
CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer";
|
2015-02-13 12:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string AndroidVideoCapturerJni::GetSupportedFormats() {
|
|
|
|
|
jmethodID m =
|
|
|
|
|
GetMethodID(jni(), *j_video_capturer_class_,
|
|
|
|
|
"getSupportedFormatsAsJson", "()Ljava/lang/String;");
|
|
|
|
|
jstring j_json_caps =
|
|
|
|
|
(jstring) jni()->CallObjectMethod(*j_capturer_global_, m);
|
|
|
|
|
CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
|
|
|
|
|
return JavaToStdString(jni(), j_json_caps);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-25 09:20:07 +00:00
|
|
|
void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
|
2015-08-25 23:22:08 +02:00
|
|
|
LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
|
|
|
|
|
AsyncCapturerInvoke("OnCapturerStarted",
|
|
|
|
|
&webrtc::AndroidVideoCapturer::OnCapturerStarted,
|
|
|
|
|
success);
|
2015-02-25 09:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-08 15:32:38 +02:00
|
|
|
void AndroidVideoCapturerJni::OnMemoryBufferFrame(void* video_frame,
|
|
|
|
|
int length,
|
|
|
|
|
int width,
|
|
|
|
|
int height,
|
|
|
|
|
int rotation,
|
|
|
|
|
int64_t timestamp_ns) {
|
2015-09-29 01:13:43 -07:00
|
|
|
const uint8_t* y_plane = static_cast<uint8_t*>(video_frame);
|
2015-08-25 23:22:08 +02:00
|
|
|
// Android guarantees that the stride is a multiple of 16.
|
|
|
|
|
// http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29
|
|
|
|
|
int y_stride;
|
|
|
|
|
int uv_stride;
|
|
|
|
|
webrtc::Calc16ByteAlignedStride(width, &y_stride, &uv_stride);
|
|
|
|
|
const uint8_t* v_plane = y_plane + y_stride * height;
|
|
|
|
|
const uint8_t* u_plane =
|
|
|
|
|
v_plane + uv_stride * webrtc::AlignInt(height, 2) / 2;
|
|
|
|
|
|
|
|
|
|
// Wrap the Java buffer, and call ReturnBuffer() in the wrapped
|
|
|
|
|
// VideoFrameBuffer destructor.
|
|
|
|
|
rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
|
|
|
|
|
new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
|
|
|
|
|
width, height, y_plane, y_stride, u_plane, uv_stride, v_plane,
|
|
|
|
|
uv_stride,
|
2015-10-08 15:32:38 +02:00
|
|
|
rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this,
|
|
|
|
|
timestamp_ns)));
|
2015-08-25 23:22:08 +02:00
|
|
|
AsyncCapturerInvoke("OnIncomingFrame",
|
|
|
|
|
&webrtc::AndroidVideoCapturer::OnIncomingFrame,
|
2015-10-08 15:32:38 +02:00
|
|
|
buffer, rotation, timestamp_ns);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-15 05:45:07 -07:00
|
|
|
void AndroidVideoCapturerJni::OnTextureFrame(
|
|
|
|
|
int width,
|
|
|
|
|
int height,
|
|
|
|
|
int64_t timestamp_ns,
|
|
|
|
|
const NativeTextureHandleImpl& handle) {
|
|
|
|
|
// TODO(magjed): Fix this. See bug webrtc:4993.
|
|
|
|
|
RTC_NOTREACHED()
|
|
|
|
|
<< "The rest of the stack for Android expects the native "
|
|
|
|
|
"handle to be a NativeHandleImpl with a SurfaceTexture, not a "
|
|
|
|
|
"NativeTextureHandleImpl";
|
2015-10-08 15:32:38 +02:00
|
|
|
rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
|
2015-10-12 06:53:20 -07:00
|
|
|
new rtc::RefCountedObject<AndroidTextureBuffer>(
|
2015-10-08 15:32:38 +02:00
|
|
|
width, height, handle,
|
|
|
|
|
rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this,
|
|
|
|
|
timestamp_ns)));
|
|
|
|
|
AsyncCapturerInvoke("OnIncomingFrame",
|
|
|
|
|
&webrtc::AndroidVideoCapturer::OnIncomingFrame,
|
|
|
|
|
buffer, 0, timestamp_ns);
|
2015-02-25 09:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-15 09:53:05 +02:00
|
|
|
void AndroidVideoCapturerJni::OnOutputFormatRequest(int width,
|
|
|
|
|
int height,
|
|
|
|
|
int fps) {
|
2015-08-25 23:22:08 +02:00
|
|
|
AsyncCapturerInvoke("OnOutputFormatRequest",
|
|
|
|
|
&webrtc::AndroidVideoCapturer::OnOutputFormatRequest,
|
|
|
|
|
width, height, fps);
|
2015-06-15 09:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-13 12:46:51 +00:00
|
|
|
JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
|
|
|
|
|
|
2015-10-08 12:53:33 +02:00
|
|
|
JOW(void,
|
|
|
|
|
VideoCapturerAndroid_00024NativeObserver_nativeOnByteBufferFrameCaptured)
|
2015-09-29 01:13:43 -07:00
|
|
|
(JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
|
2015-10-08 15:32:38 +02:00
|
|
|
jint width, jint height, jint rotation, jlong timestamp) {
|
2015-09-29 01:13:43 -07:00
|
|
|
jboolean is_copy = true;
|
|
|
|
|
jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
|
|
|
|
|
// If this is a copy of the original frame, it means that the memory
|
|
|
|
|
// is not direct memory and thus VideoCapturerAndroid does not guarantee
|
|
|
|
|
// that the memory is valid when we have released |j_frame|.
|
|
|
|
|
// TODO(magjed): Move ReleaseByteArrayElements() into ReturnBuffer() and
|
|
|
|
|
// remove this check.
|
|
|
|
|
RTC_CHECK(!is_copy)
|
|
|
|
|
<< "NativeObserver_nativeOnFrameCaptured: frame is a copy";
|
2015-08-25 23:22:08 +02:00
|
|
|
reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
|
2015-10-08 15:32:38 +02:00
|
|
|
->OnMemoryBufferFrame(bytes, length, width, height, rotation, timestamp);
|
2015-09-29 01:13:43 -07:00
|
|
|
jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
|
2015-02-13 12:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-08 15:32:38 +02:00
|
|
|
JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnTextureFrameCaptured)
|
|
|
|
|
(JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
|
|
|
|
|
jint j_oes_texture_id, jfloatArray j_transform_matrix,
|
|
|
|
|
jlong j_timestamp) {
|
|
|
|
|
reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
|
|
|
|
|
->OnTextureFrame(j_width, j_height, j_timestamp,
|
2015-10-15 05:45:07 -07:00
|
|
|
NativeTextureHandleImpl(jni, j_oes_texture_id,
|
|
|
|
|
j_transform_matrix));
|
2015-10-08 15:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-19 08:43:38 +00:00
|
|
|
JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
|
2015-02-25 09:20:07 +00:00
|
|
|
(JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
|
2015-04-20 13:00:49 -07:00
|
|
|
LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
|
2015-02-25 09:20:07 +00:00
|
|
|
reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
|
|
|
|
|
j_success);
|
2015-02-13 12:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-15 09:53:05 +02:00
|
|
|
JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest)
|
|
|
|
|
(JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
|
|
|
|
|
jint j_fps) {
|
|
|
|
|
LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest";
|
|
|
|
|
reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest(
|
|
|
|
|
j_width, j_height, j_fps);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 16:21:54 +02:00
|
|
|
JOW(jlong, VideoCapturerAndroid_nativeCreateVideoCapturer)
|
|
|
|
|
(JNIEnv* jni, jclass, jobject j_video_capturer) {
|
|
|
|
|
rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate =
|
|
|
|
|
new rtc::RefCountedObject<AndroidVideoCapturerJni>(jni, j_video_capturer);
|
|
|
|
|
rtc::scoped_ptr<cricket::VideoCapturer> capturer(
|
|
|
|
|
new webrtc::AndroidVideoCapturer(delegate));
|
|
|
|
|
// Caller takes ownership of the cricket::VideoCapturer* pointer.
|
|
|
|
|
return jlongFromPointer(capturer.release());
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-13 12:46:51 +00:00
|
|
|
} // namespace webrtc_jni
|