Android audio record/track: Remove intermediate JNI manager

After using JNI generation, there is no need to have a separate class
handling JNI interaction.

Bug: webrtc:7452
Change-Id: I25de6007190d826e2790cf6219a6ac861acfb6a8
Reviewed-on: https://webrtc-review.googlesource.com/63800
Reviewed-by: Paulina Hensman <phensman@webrtc.org>
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22541}
This commit is contained in:
Magnus Jedvert
2018-03-21 16:28:24 +01:00
committed by Commit Bot
parent b34556e598
commit 2955d82eca
4 changed files with 35 additions and 134 deletions

View File

@ -47,46 +47,13 @@ class ScopedHistogramTimer {
}; };
} // namespace } // namespace
// AudioRecordJni::JavaAudioRecord implementation.
AudioRecordJni::JavaAudioRecord::JavaAudioRecord(
const ScopedJavaLocalRef<jobject>& audio_record)
: env_(audio_record.env()), audio_record_(audio_record) {}
AudioRecordJni::JavaAudioRecord::~JavaAudioRecord() {}
int AudioRecordJni::JavaAudioRecord::InitRecording(int sample_rate,
size_t channels) {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioRecord_initRecording(env_, audio_record_,
static_cast<jint>(sample_rate),
static_cast<jint>(channels));
}
bool AudioRecordJni::JavaAudioRecord::StartRecording() {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioRecord_startRecording(env_, audio_record_);
}
bool AudioRecordJni::JavaAudioRecord::StopRecording() {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioRecord_stopRecording(env_, audio_record_);
}
bool AudioRecordJni::JavaAudioRecord::EnableBuiltInAEC(bool enable) {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioRecord_enableBuiltInAEC(env_, audio_record_,
static_cast<jboolean>(enable));
}
bool AudioRecordJni::JavaAudioRecord::EnableBuiltInNS(bool enable) {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioRecord_enableBuiltInNS(env_, audio_record_,
static_cast<jboolean>(enable));
}
// AudioRecordJni implementation. // AudioRecordJni implementation.
AudioRecordJni::AudioRecordJni(AudioManager* audio_manager) AudioRecordJni::AudioRecordJni(AudioManager* audio_manager)
: audio_manager_(audio_manager), : env_(AttachCurrentThreadIfNeeded()),
j_audio_record_(
Java_WebRtcAudioRecord_Constructor(env_,
jni::jlongFromPointer(this))),
audio_manager_(audio_manager),
audio_parameters_(audio_manager->GetRecordAudioParameters()), audio_parameters_(audio_manager->GetRecordAudioParameters()),
total_delay_in_milliseconds_(0), total_delay_in_milliseconds_(0),
direct_buffer_address_(nullptr), direct_buffer_address_(nullptr),
@ -97,8 +64,6 @@ AudioRecordJni::AudioRecordJni(AudioManager* audio_manager)
audio_device_buffer_(nullptr) { audio_device_buffer_(nullptr) {
RTC_LOG(INFO) << "ctor"; RTC_LOG(INFO) << "ctor";
RTC_DCHECK(audio_parameters_.is_valid()); RTC_DCHECK(audio_parameters_.is_valid());
j_audio_record_.reset(new JavaAudioRecord(Java_WebRtcAudioRecord_Constructor(
AttachCurrentThreadIfNeeded(), jni::jlongFromPointer(this))));
// Detach from this thread since we want to use the checker to verify calls // Detach from this thread since we want to use the checker to verify calls
// from the Java based audio thread. // from the Java based audio thread.
thread_checker_java_.DetachFromThread(); thread_checker_java_.DetachFromThread();
@ -129,8 +94,10 @@ int32_t AudioRecordJni::InitRecording() {
RTC_DCHECK(!initialized_); RTC_DCHECK(!initialized_);
RTC_DCHECK(!recording_); RTC_DCHECK(!recording_);
ScopedHistogramTimer timer("WebRTC.Audio.InitRecordingDurationMs"); ScopedHistogramTimer timer("WebRTC.Audio.InitRecordingDurationMs");
int frames_per_buffer = j_audio_record_->InitRecording(
audio_parameters_.sample_rate(), audio_parameters_.channels()); int frames_per_buffer = Java_WebRtcAudioRecord_initRecording(
env_, j_audio_record_, audio_parameters_.sample_rate(),
audio_parameters_.channels());
if (frames_per_buffer < 0) { if (frames_per_buffer < 0) {
direct_buffer_address_ = nullptr; direct_buffer_address_ = nullptr;
RTC_LOG(LS_ERROR) << "InitRecording failed"; RTC_LOG(LS_ERROR) << "InitRecording failed";
@ -156,7 +123,7 @@ int32_t AudioRecordJni::StartRecording() {
return 0; return 0;
} }
ScopedHistogramTimer timer("WebRTC.Audio.StartRecordingDurationMs"); ScopedHistogramTimer timer("WebRTC.Audio.StartRecordingDurationMs");
if (!j_audio_record_->StartRecording()) { if (!Java_WebRtcAudioRecord_startRecording(env_, j_audio_record_)) {
RTC_LOG(LS_ERROR) << "StartRecording failed"; RTC_LOG(LS_ERROR) << "StartRecording failed";
return -1; return -1;
} }
@ -170,7 +137,7 @@ int32_t AudioRecordJni::StopRecording() {
if (!initialized_ || !recording_) { if (!initialized_ || !recording_) {
return 0; return 0;
} }
if (!j_audio_record_->StopRecording()) { if (!Java_WebRtcAudioRecord_stopRecording(env_, j_audio_record_)) {
RTC_LOG(LS_ERROR) << "StopRecording failed"; RTC_LOG(LS_ERROR) << "StopRecording failed";
return -1; return -1;
} }
@ -204,7 +171,9 @@ void AudioRecordJni::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) {
int32_t AudioRecordJni::EnableBuiltInAEC(bool enable) { int32_t AudioRecordJni::EnableBuiltInAEC(bool enable) {
RTC_LOG(INFO) << "EnableBuiltInAEC(" << enable << ")"; RTC_LOG(INFO) << "EnableBuiltInAEC(" << enable << ")";
RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(thread_checker_.CalledOnValidThread());
return j_audio_record_->EnableBuiltInAEC(enable) ? 0 : -1; return Java_WebRtcAudioRecord_enableBuiltInAEC(env_, j_audio_record_, enable)
? 0
: -1;
} }
int32_t AudioRecordJni::EnableBuiltInAGC(bool enable) { int32_t AudioRecordJni::EnableBuiltInAGC(bool enable) {
@ -216,7 +185,9 @@ int32_t AudioRecordJni::EnableBuiltInAGC(bool enable) {
int32_t AudioRecordJni::EnableBuiltInNS(bool enable) { int32_t AudioRecordJni::EnableBuiltInNS(bool enable) {
RTC_LOG(INFO) << "EnableBuiltInNS(" << enable << ")"; RTC_LOG(INFO) << "EnableBuiltInNS(" << enable << ")";
RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(thread_checker_.CalledOnValidThread());
return j_audio_record_->EnableBuiltInNS(enable) ? 0 : -1; return Java_WebRtcAudioRecord_enableBuiltInNS(env_, j_audio_record_, enable)
? 0
: -1;
} }
void AudioRecordJni::CacheDirectBufferAddress( void AudioRecordJni::CacheDirectBufferAddress(

View File

@ -43,25 +43,6 @@ namespace android_adm {
// guarantees that no other (possibly non attached) thread is used. // guarantees that no other (possibly non attached) thread is used.
class AudioRecordJni { class AudioRecordJni {
public: public:
// Wraps the Java specific parts of the AudioRecordJni into one helper class.
class JavaAudioRecord {
public:
explicit JavaAudioRecord(const ScopedJavaLocalRef<jobject>& audio_record);
~JavaAudioRecord();
int InitRecording(int sample_rate, size_t channels);
bool StartRecording();
bool StopRecording();
bool EnableBuiltInAEC(bool enable);
bool EnableBuiltInNS(bool enable);
private:
JNIEnv* const env_;
rtc::ThreadChecker thread_checker_;
ScopedJavaGlobalRef<jobject> audio_record_;
};
explicit AudioRecordJni(AudioManager* audio_manager); explicit AudioRecordJni(AudioManager* audio_manager);
~AudioRecordJni(); ~AudioRecordJni();
@ -109,7 +90,8 @@ class AudioRecordJni {
rtc::ThreadChecker thread_checker_java_; rtc::ThreadChecker thread_checker_java_;
// Wraps the Java specific parts of the AudioRecordJni class. // Wraps the Java specific parts of the AudioRecordJni class.
std::unique_ptr<AudioRecordJni::JavaAudioRecord> j_audio_record_; JNIEnv* const env_;
ScopedJavaGlobalRef<jobject> j_audio_record_;
// Raw pointer to the audio manger. // Raw pointer to the audio manger.
const AudioManager* audio_manager_; const AudioManager* audio_manager_;

View File

@ -25,47 +25,12 @@ namespace webrtc {
namespace android_adm { namespace android_adm {
// AudioTrackJni::JavaAudioTrack implementation.
AudioTrackJni::JavaAudioTrack::JavaAudioTrack(
const ScopedJavaLocalRef<jobject>& audio_track)
: env_(audio_track.env()), audio_track_(audio_track) {}
AudioTrackJni::JavaAudioTrack::~JavaAudioTrack() {}
bool AudioTrackJni::JavaAudioTrack::InitPlayout(int sample_rate, int channels) {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioTrack_initPlayout(env_, audio_track_, sample_rate,
channels);
}
bool AudioTrackJni::JavaAudioTrack::StartPlayout() {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioTrack_startPlayout(env_, audio_track_);
}
bool AudioTrackJni::JavaAudioTrack::StopPlayout() {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioTrack_stopPlayout(env_, audio_track_);
}
bool AudioTrackJni::JavaAudioTrack::SetStreamVolume(int volume) {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioTrack_setStreamVolume(env_, audio_track_, volume);
}
int AudioTrackJni::JavaAudioTrack::GetStreamMaxVolume() {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioTrack_getStreamMaxVolume(env_, audio_track_);
}
int AudioTrackJni::JavaAudioTrack::GetStreamVolume() {
thread_checker_.CalledOnValidThread();
return Java_WebRtcAudioTrack_getStreamVolume(env_, audio_track_);
}
// TODO(henrika): possible extend usage of AudioManager and add it as member. // TODO(henrika): possible extend usage of AudioManager and add it as member.
AudioTrackJni::AudioTrackJni(AudioManager* audio_manager) AudioTrackJni::AudioTrackJni(AudioManager* audio_manager)
: audio_parameters_(audio_manager->GetPlayoutAudioParameters()), : env_(AttachCurrentThreadIfNeeded()),
j_audio_track_(
Java_WebRtcAudioTrack_Constructor(env_, jni::jlongFromPointer(this))),
audio_parameters_(audio_manager->GetPlayoutAudioParameters()),
direct_buffer_address_(nullptr), direct_buffer_address_(nullptr),
direct_buffer_capacity_in_bytes_(0), direct_buffer_capacity_in_bytes_(0),
frames_per_buffer_(0), frames_per_buffer_(0),
@ -74,8 +39,6 @@ AudioTrackJni::AudioTrackJni(AudioManager* audio_manager)
audio_device_buffer_(nullptr) { audio_device_buffer_(nullptr) {
RTC_LOG(INFO) << "ctor"; RTC_LOG(INFO) << "ctor";
RTC_DCHECK(audio_parameters_.is_valid()); RTC_DCHECK(audio_parameters_.is_valid());
j_audio_track_.reset(new JavaAudioTrack(Java_WebRtcAudioTrack_Constructor(
AttachCurrentThreadIfNeeded(), jni::jlongFromPointer(this))));
// Detach from this thread since we want to use the checker to verify calls // Detach from this thread since we want to use the checker to verify calls
// from the Java based audio thread. // from the Java based audio thread.
thread_checker_java_.DetachFromThread(); thread_checker_java_.DetachFromThread();
@ -105,8 +68,9 @@ int32_t AudioTrackJni::InitPlayout() {
RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(thread_checker_.CalledOnValidThread());
RTC_DCHECK(!initialized_); RTC_DCHECK(!initialized_);
RTC_DCHECK(!playing_); RTC_DCHECK(!playing_);
if (!j_audio_track_->InitPlayout(audio_parameters_.sample_rate(), if (!Java_WebRtcAudioTrack_initPlayout(env_, j_audio_track_,
audio_parameters_.channels())) { audio_parameters_.sample_rate(),
audio_parameters_.channels())) {
RTC_LOG(LS_ERROR) << "InitPlayout failed"; RTC_LOG(LS_ERROR) << "InitPlayout failed";
return -1; return -1;
} }
@ -123,7 +87,7 @@ int32_t AudioTrackJni::StartPlayout() {
<< "Playout can not start since InitPlayout must succeed first"; << "Playout can not start since InitPlayout must succeed first";
return 0; return 0;
} }
if (!j_audio_track_->StartPlayout()) { if (!Java_WebRtcAudioTrack_startPlayout(env_, j_audio_track_)) {
RTC_LOG(LS_ERROR) << "StartPlayout failed"; RTC_LOG(LS_ERROR) << "StartPlayout failed";
return -1; return -1;
} }
@ -137,7 +101,7 @@ int32_t AudioTrackJni::StopPlayout() {
if (!initialized_ || !playing_) { if (!initialized_ || !playing_) {
return 0; return 0;
} }
if (!j_audio_track_->StopPlayout()) { if (!Java_WebRtcAudioTrack_stopPlayout(env_, j_audio_track_)) {
RTC_LOG(LS_ERROR) << "StopPlayout failed"; RTC_LOG(LS_ERROR) << "StopPlayout failed";
return -1; return -1;
} }
@ -159,12 +123,14 @@ int AudioTrackJni::SpeakerVolumeIsAvailable(bool* available) {
int AudioTrackJni::SetSpeakerVolume(uint32_t volume) { int AudioTrackJni::SetSpeakerVolume(uint32_t volume) {
RTC_LOG(INFO) << "SetSpeakerVolume(" << volume << ")"; RTC_LOG(INFO) << "SetSpeakerVolume(" << volume << ")";
RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(thread_checker_.CalledOnValidThread());
return j_audio_track_->SetStreamVolume(volume) ? 0 : -1; return Java_WebRtcAudioTrack_setStreamVolume(env_, j_audio_track_, volume)
? 0
: -1;
} }
int AudioTrackJni::MaxSpeakerVolume(uint32_t* max_volume) const { int AudioTrackJni::MaxSpeakerVolume(uint32_t* max_volume) const {
RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(thread_checker_.CalledOnValidThread());
*max_volume = j_audio_track_->GetStreamMaxVolume(); *max_volume = Java_WebRtcAudioTrack_getStreamMaxVolume(env_, j_audio_track_);
return 0; return 0;
} }
@ -176,7 +142,7 @@ int AudioTrackJni::MinSpeakerVolume(uint32_t* min_volume) const {
int AudioTrackJni::SpeakerVolume(uint32_t* volume) const { int AudioTrackJni::SpeakerVolume(uint32_t* volume) const {
RTC_DCHECK(thread_checker_.CalledOnValidThread()); RTC_DCHECK(thread_checker_.CalledOnValidThread());
*volume = j_audio_track_->GetStreamVolume(); *volume = Java_WebRtcAudioTrack_getStreamVolume(env_, j_audio_track_);
RTC_LOG(INFO) << "SpeakerVolume: " << volume; RTC_LOG(INFO) << "SpeakerVolume: " << volume;
return 0; return 0;
} }

View File

@ -39,25 +39,6 @@ namespace android_adm {
// guarantees that no other (possibly non attached) thread is used. // guarantees that no other (possibly non attached) thread is used.
class AudioTrackJni { class AudioTrackJni {
public: public:
// Wraps the Java specific parts of the AudioTrackJni into one helper class.
class JavaAudioTrack {
public:
explicit JavaAudioTrack(const ScopedJavaLocalRef<jobject>& audio_track);
~JavaAudioTrack();
bool InitPlayout(int sample_rate, int channels);
bool StartPlayout();
bool StopPlayout();
bool SetStreamVolume(int volume);
int GetStreamMaxVolume();
int GetStreamVolume();
private:
JNIEnv* const env_;
rtc::ThreadChecker thread_checker_;
ScopedJavaGlobalRef<jobject> audio_track_;
};
explicit AudioTrackJni(AudioManager* audio_manager); explicit AudioTrackJni(AudioManager* audio_manager);
~AudioTrackJni(); ~AudioTrackJni();
@ -104,7 +85,8 @@ class AudioTrackJni {
rtc::ThreadChecker thread_checker_java_; rtc::ThreadChecker thread_checker_java_;
// Wraps the Java specific parts of the AudioTrackJni class. // Wraps the Java specific parts of the AudioTrackJni class.
std::unique_ptr<AudioTrackJni::JavaAudioTrack> j_audio_track_; JNIEnv* const env_;
ScopedJavaGlobalRef<jobject> j_audio_track_;
// Contains audio parameters provided to this class at construction by the // Contains audio parameters provided to this class at construction by the
// AudioManager. // AudioManager.