
This is a big refactoring of the existing C++/JNI/Java support for audio recording in native WebRTC: - Removes unused code and old WEBRTC logging macros - Now uses optimal sample rate and buffer size in Java AudioRecord (used hard-coded sample rate before) - Makes code more inline with the implementation in Chrome - Adds helper methods for JNI handling to improve readability - Changes the threading model (high-prio audio thread now lives in Java-land and C++ only works as proxy) - Adds basic thread checks - Removes all locks in C++ land - Removes all locks in Java - Improves construction/destruction - Additional cleanup Tested using AppRTCDemo and WebRTCDemo APKs on N6, N5, N7, Samsung Galaxy S4 and Samsung Galaxy S4 mini (which uses 44.1kHz as native sample rate). BUG=NONE R=magjed@webrtc.org, perkj@webrtc.org, pthatcher@webrtc.org Review URL: https://webrtc-codereview.appspot.com/33969004 Cr-Commit-Position: refs/heads/master@{#8325} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8325 4adac7df-926f-26a2-2b94-8c16560cd09d
80 lines
2.2 KiB
C++
80 lines
2.2 KiB
C++
/*
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#ifndef WEBRTC_MODULES_UTILITY_INTERFACE_HELPERS_ANDROID_H_
|
|
#define WEBRTC_MODULES_UTILITY_INTERFACE_HELPERS_ANDROID_H_
|
|
|
|
#include <jni.h>
|
|
#include <string>
|
|
|
|
// Abort the process if |jni| has a Java exception pending.
|
|
// TODO(henrika): merge with CHECK_JNI_EXCEPTION() in jni_helpers.h.
|
|
#define CHECK_EXCEPTION(jni) \
|
|
CHECK(!jni->ExceptionCheck()) \
|
|
<< (jni->ExceptionDescribe(), jni->ExceptionClear(), "")
|
|
|
|
namespace webrtc {
|
|
|
|
// Return a |JNIEnv*| usable on this thread or NULL if this thread is detached.
|
|
JNIEnv* GetEnv(JavaVM* jvm);
|
|
|
|
// JNIEnv-helper methods that wraps the API which uses the JNI interface
|
|
// pointer (JNIEnv*). It allows us to CHECK success and that no Java exception
|
|
// is thrown while calling the method.
|
|
jmethodID GetMethodID (
|
|
JNIEnv* jni, jclass c, const std::string& name, const char* signature);
|
|
|
|
jclass FindClass(JNIEnv* jni, const std::string& name);
|
|
|
|
jobject NewGlobalRef(JNIEnv* jni, jobject o);
|
|
|
|
void DeleteGlobalRef(JNIEnv* jni, jobject o);
|
|
|
|
// Return thread ID as a string.
|
|
std::string GetThreadId();
|
|
|
|
// Return thread ID as string suitable for debug logging.
|
|
std::string GetThreadInfo();
|
|
|
|
// Attach thread to JVM if necessary and detach at scope end if originally
|
|
// attached.
|
|
class AttachThreadScoped {
|
|
public:
|
|
explicit AttachThreadScoped(JavaVM* jvm);
|
|
~AttachThreadScoped();
|
|
JNIEnv* env();
|
|
|
|
private:
|
|
bool attached_;
|
|
JavaVM* jvm_;
|
|
JNIEnv* env_;
|
|
};
|
|
|
|
// Scoped holder for global Java refs.
|
|
template<class T> // T is jclass, jobject, jintArray, etc.
|
|
class ScopedGlobalRef {
|
|
public:
|
|
ScopedGlobalRef(JNIEnv* jni, T obj)
|
|
: jni_(jni), obj_(static_cast<T>(NewGlobalRef(jni, obj))) {}
|
|
~ScopedGlobalRef() {
|
|
DeleteGlobalRef(jni_, obj_);
|
|
}
|
|
T operator*() const {
|
|
return obj_;
|
|
}
|
|
private:
|
|
JNIEnv* jni_;
|
|
T obj_;
|
|
};
|
|
|
|
} // namespace webrtc
|
|
|
|
#endif // WEBRTC_MODULES_UTILITY_INTERFACE_HELPERS_ANDROID_H_
|