Adds usage of RTC_LOG macros in JNI audio code on Android.

Based on discussion in https://webrtc-review.googlesource.com/c/src/+/37640

Bug: webrtc:8710
Change-Id: I645b6e08b0a97aac3fe31547cf42fc4ddc25bbf6
Reviewed-on: https://webrtc-review.googlesource.com/37980
Reviewed-by: Magnus Jedvert <magjed@webrtc.org>
Commit-Queue: Henrik Andreassson <henrika@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21573}
This commit is contained in:
henrika
2018-01-09 11:17:41 +01:00
committed by Commit Bot
parent 6f9c01fb58
commit c77b528a20
2 changed files with 45 additions and 62 deletions

View File

@ -10,8 +10,6 @@
#include "modules/audio_device/android/audio_record_jni.h"
#include <android/log.h>
#include <string>
#include <utility>
@ -19,17 +17,11 @@
#include "rtc_base/arraysize.h"
#include "rtc_base/checks.h"
#include "rtc_base/format_macros.h"
#include "rtc_base/logging.h"
#include "rtc_base/platform_thread.h"
#include "rtc_base/timeutils.h"
#include "system_wrappers/include/metrics.h"
#define TAG "AudioRecordJni"
#define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
#define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
namespace webrtc {
namespace {
@ -42,7 +34,7 @@ class ScopedHistogramTimer {
~ScopedHistogramTimer() {
const int64_t life_time_ms = rtc::TimeSince(start_time_ms_);
RTC_HISTOGRAM_COUNTS_1000(histogram_name_, life_time_ms);
ALOGD("%s: %" PRId64, histogram_name_.c_str(), life_time_ms);
RTC_LOG(INFO) << histogram_name_ << ": " << life_time_ms;
}
private:
@ -101,7 +93,7 @@ AudioRecordJni::AudioRecordJni(AudioManager* audio_manager)
initialized_(false),
recording_(false),
audio_device_buffer_(nullptr) {
ALOGD("ctor[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "ctor";
RTC_DCHECK(audio_parameters_.is_valid());
RTC_CHECK(j_environment_);
JNINativeMethod native_methods[] = {
@ -123,26 +115,26 @@ AudioRecordJni::AudioRecordJni(AudioManager* audio_manager)
}
AudioRecordJni::~AudioRecordJni() {
ALOGD("~dtor[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "dtor";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
Terminate();
}
int32_t AudioRecordJni::Init() {
ALOGD("Init[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "Init";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
return 0;
}
int32_t AudioRecordJni::Terminate() {
ALOGD("Terminate[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "Terminate";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
StopRecording();
return 0;
}
int32_t AudioRecordJni::InitRecording() {
ALOGD("InitRecording[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "InitRecording";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
RTC_DCHECK(!initialized_);
RTC_DCHECK(!recording_);
@ -151,11 +143,11 @@ int32_t AudioRecordJni::InitRecording() {
audio_parameters_.sample_rate(), audio_parameters_.channels());
if (frames_per_buffer < 0) {
direct_buffer_address_ = nullptr;
ALOGE("InitRecording failed!");
RTC_LOG(LS_ERROR) << "InitRecording failed";
return -1;
}
frames_per_buffer_ = static_cast<size_t>(frames_per_buffer);
ALOGD("frames_per_buffer: %" PRIuS, frames_per_buffer_);
RTC_LOG(INFO) << "frames_per_buffer: " << frames_per_buffer_;
const size_t bytes_per_frame = audio_parameters_.channels() * sizeof(int16_t);
RTC_CHECK_EQ(direct_buffer_capacity_in_bytes_,
frames_per_buffer_ * bytes_per_frame);
@ -165,13 +157,13 @@ int32_t AudioRecordJni::InitRecording() {
}
int32_t AudioRecordJni::StartRecording() {
ALOGD("StartRecording[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "StartRecording";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
RTC_DCHECK(initialized_);
RTC_DCHECK(!recording_);
ScopedHistogramTimer timer("WebRTC.Audio.StartRecordingDurationMs");
if (!j_audio_record_->StartRecording()) {
ALOGE("StartRecording failed!");
RTC_LOG(LS_ERROR) << "StartRecording failed";
return -1;
}
recording_ = true;
@ -179,13 +171,13 @@ int32_t AudioRecordJni::StartRecording() {
}
int32_t AudioRecordJni::StopRecording() {
ALOGD("StopRecording[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "StopRecording";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
if (!initialized_ || !recording_) {
return 0;
}
if (!j_audio_record_->StopRecording()) {
ALOGE("StopRecording failed!");
RTC_LOG(LS_ERROR) << "StopRecording failed";
return -1;
}
// If we don't detach here, we will hit a RTC_DCHECK in OnDataIsRecorded()
@ -199,23 +191,24 @@ int32_t AudioRecordJni::StopRecording() {
}
void AudioRecordJni::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) {
ALOGD("AttachAudioBuffer");
RTC_LOG(INFO) << "AttachAudioBuffer";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
audio_device_buffer_ = audioBuffer;
const int sample_rate_hz = audio_parameters_.sample_rate();
ALOGD("SetRecordingSampleRate(%d)", sample_rate_hz);
RTC_LOG(INFO) << "SetRecordingSampleRate(" << sample_rate_hz << ")";
audio_device_buffer_->SetRecordingSampleRate(sample_rate_hz);
const size_t channels = audio_parameters_.channels();
ALOGD("SetRecordingChannels(%" PRIuS ")", channels);
RTC_LOG(INFO) << "SetRecordingChannels(" << channels << ")";
audio_device_buffer_->SetRecordingChannels(channels);
total_delay_in_milliseconds_ =
audio_manager_->GetDelayEstimateInMilliseconds();
RTC_DCHECK_GT(total_delay_in_milliseconds_, 0);
ALOGD("total_delay_in_milliseconds: %d", total_delay_in_milliseconds_);
RTC_LOG(INFO) << "total_delay_in_milliseconds: "
<< total_delay_in_milliseconds_;
}
int32_t AudioRecordJni::EnableBuiltInAEC(bool enable) {
ALOGD("EnableBuiltInAEC[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "EnableBuiltInAEC(" << enable << ")";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
return j_audio_record_->EnableBuiltInAEC(enable) ? 0 : -1;
}
@ -227,7 +220,7 @@ int32_t AudioRecordJni::EnableBuiltInAGC(bool enable) {
}
int32_t AudioRecordJni::EnableBuiltInNS(bool enable) {
ALOGD("EnableBuiltInNS[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "EnableBuiltInNS(" << enable << ")";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
return j_audio_record_->EnableBuiltInNS(enable) ? 0 : -1;
}
@ -243,12 +236,12 @@ void JNICALL AudioRecordJni::CacheDirectBufferAddress(JNIEnv* env,
void AudioRecordJni::OnCacheDirectBufferAddress(JNIEnv* env,
jobject byte_buffer) {
ALOGD("OnCacheDirectBufferAddress");
RTC_LOG(INFO) << "OnCacheDirectBufferAddress";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
RTC_DCHECK(!direct_buffer_address_);
direct_buffer_address_ = env->GetDirectBufferAddress(byte_buffer);
jlong capacity = env->GetDirectBufferCapacity(byte_buffer);
ALOGD("direct buffer capacity: %ld", static_cast<long>(capacity));
RTC_LOG(INFO) << "direct buffer capacity: " << capacity;
direct_buffer_capacity_in_bytes_ = static_cast<size_t>(capacity);
}
@ -266,7 +259,7 @@ void JNICALL AudioRecordJni::DataIsRecorded(JNIEnv* env,
void AudioRecordJni::OnDataIsRecorded(int length) {
RTC_DCHECK(thread_checker_java_.CalledOnValidThread());
if (!audio_device_buffer_) {
ALOGE("AttachAudioBuffer has not been called!");
RTC_LOG(LS_ERROR) << "AttachAudioBuffer has not been called";
return;
}
audio_device_buffer_->SetRecordedBuffer(direct_buffer_address_,
@ -278,7 +271,7 @@ void AudioRecordJni::OnDataIsRecorded(int length) {
0, // recDelayMs
0); // clockDrift
if (audio_device_buffer_->DeliverRecordedData() == -1) {
ALOGE("AudioDeviceBuffer::DeliverRecordedData failed!");
RTC_LOG(INFO) << "AudioDeviceBuffer::DeliverRecordedData failed";
}
}

View File

@ -13,20 +13,12 @@
#include <utility>
#include <android/log.h>
#include "rtc_base/arraysize.h"
#include "rtc_base/checks.h"
#include "rtc_base/format_macros.h"
#include "rtc_base/logging.h"
#include "rtc_base/platform_thread.h"
#define TAG "AudioTrackJni"
#define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
#define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
namespace webrtc {
// AudioTrackJni::JavaAudioTrack implementation.
@ -78,7 +70,7 @@ AudioTrackJni::AudioTrackJni(AudioManager* audio_manager)
initialized_(false),
playing_(false),
audio_device_buffer_(nullptr) {
ALOGD("ctor[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "ctor";
RTC_DCHECK(audio_parameters_.is_valid());
RTC_CHECK(j_environment_);
JNINativeMethod native_methods[] = {
@ -100,32 +92,32 @@ AudioTrackJni::AudioTrackJni(AudioManager* audio_manager)
}
AudioTrackJni::~AudioTrackJni() {
ALOGD("~dtor[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "dtor";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
Terminate();
}
int32_t AudioTrackJni::Init() {
ALOGD("Init[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "Init";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
return 0;
}
int32_t AudioTrackJni::Terminate() {
ALOGD("Terminate[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "Terminate";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
StopPlayout();
return 0;
}
int32_t AudioTrackJni::InitPlayout() {
ALOGD("InitPlayout[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "InitPlayout";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
RTC_DCHECK(!initialized_);
RTC_DCHECK(!playing_);
if (!j_audio_track_->InitPlayout(audio_parameters_.sample_rate(),
audio_parameters_.channels())) {
ALOGE("InitPlayout failed!");
RTC_LOG(LS_ERROR) << "InitPlayout failed";
return -1;
}
initialized_ = true;
@ -133,12 +125,12 @@ int32_t AudioTrackJni::InitPlayout() {
}
int32_t AudioTrackJni::StartPlayout() {
ALOGD("StartPlayout[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "StartPlayout";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
RTC_DCHECK(initialized_);
RTC_DCHECK(!playing_);
if (!j_audio_track_->StartPlayout()) {
ALOGE("StartPlayout failed!");
RTC_LOG(LS_ERROR) << "StartPlayout failed";
return -1;
}
playing_ = true;
@ -146,13 +138,13 @@ int32_t AudioTrackJni::StartPlayout() {
}
int32_t AudioTrackJni::StopPlayout() {
ALOGD("StopPlayout[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "StopPlayout";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
if (!initialized_ || !playing_) {
return 0;
}
if (!j_audio_track_->StopPlayout()) {
ALOGE("StopPlayout failed!");
RTC_LOG(LS_ERROR) << "StopPlayout failed";
return -1;
}
// If we don't detach here, we will hit a RTC_DCHECK in OnDataIsRecorded()
@ -171,42 +163,40 @@ int AudioTrackJni::SpeakerVolumeIsAvailable(bool& available) {
}
int AudioTrackJni::SetSpeakerVolume(uint32_t volume) {
ALOGD("SetSpeakerVolume(%d)[tid=%d]", volume, rtc::CurrentThreadId());
RTC_LOG(INFO) << "SetSpeakerVolume(" << volume << ")";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
return j_audio_track_->SetStreamVolume(volume) ? 0 : -1;
}
int AudioTrackJni::MaxSpeakerVolume(uint32_t& max_volume) const {
ALOGD("MaxSpeakerVolume[tid=%d]", rtc::CurrentThreadId());
RTC_DCHECK(thread_checker_.CalledOnValidThread());
max_volume = j_audio_track_->GetStreamMaxVolume();
return 0;
}
int AudioTrackJni::MinSpeakerVolume(uint32_t& min_volume) const {
ALOGD("MaxSpeakerVolume[tid=%d]", rtc::CurrentThreadId());
RTC_DCHECK(thread_checker_.CalledOnValidThread());
min_volume = 0;
return 0;
}
int AudioTrackJni::SpeakerVolume(uint32_t& volume) const {
ALOGD("SpeakerVolume[tid=%d]", rtc::CurrentThreadId());
RTC_DCHECK(thread_checker_.CalledOnValidThread());
volume = j_audio_track_->GetStreamVolume();
RTC_LOG(INFO) << "SpeakerVolume: " << volume;
return 0;
}
// TODO(henrika): possibly add stereo support.
void AudioTrackJni::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) {
ALOGD("AttachAudioBuffer[tid=%d]", rtc::CurrentThreadId());
RTC_LOG(INFO) << "AttachAudioBuffer";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
audio_device_buffer_ = audioBuffer;
const int sample_rate_hz = audio_parameters_.sample_rate();
ALOGD("SetPlayoutSampleRate(%d)", sample_rate_hz);
RTC_LOG(INFO) << "SetPlayoutSampleRate(" << sample_rate_hz << ")";
audio_device_buffer_->SetPlayoutSampleRate(sample_rate_hz);
const size_t channels = audio_parameters_.channels();
ALOGD("SetPlayoutChannels(%" PRIuS ")", channels);
RTC_LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
audio_device_buffer_->SetPlayoutChannels(channels);
}
@ -221,16 +211,16 @@ void JNICALL AudioTrackJni::CacheDirectBufferAddress(JNIEnv* env,
void AudioTrackJni::OnCacheDirectBufferAddress(JNIEnv* env,
jobject byte_buffer) {
ALOGD("OnCacheDirectBufferAddress");
RTC_LOG(INFO) << "OnCacheDirectBufferAddress";
RTC_DCHECK(thread_checker_.CalledOnValidThread());
RTC_DCHECK(!direct_buffer_address_);
direct_buffer_address_ = env->GetDirectBufferAddress(byte_buffer);
jlong capacity = env->GetDirectBufferCapacity(byte_buffer);
ALOGD("direct buffer capacity: %ld", static_cast<long>(capacity));
RTC_LOG(INFO) << "direct buffer capacity: " << capacity;
direct_buffer_capacity_in_bytes_ = static_cast<size_t>(capacity);
const size_t bytes_per_frame = audio_parameters_.channels() * sizeof(int16_t);
frames_per_buffer_ = direct_buffer_capacity_in_bytes_ / bytes_per_frame;
ALOGD("frames_per_buffer: %" PRIuS, frames_per_buffer_);
RTC_LOG(INFO) << "frames_per_buffer: " << frames_per_buffer_;
}
void JNICALL AudioTrackJni::GetPlayoutData(JNIEnv* env,
@ -249,13 +239,13 @@ void AudioTrackJni::OnGetPlayoutData(size_t length) {
const size_t bytes_per_frame = audio_parameters_.channels() * sizeof(int16_t);
RTC_DCHECK_EQ(frames_per_buffer_, length / bytes_per_frame);
if (!audio_device_buffer_) {
ALOGE("AttachAudioBuffer has not been called!");
RTC_LOG(LS_ERROR) << "AttachAudioBuffer has not been called";
return;
}
// Pull decoded data (in 16-bit PCM format) from jitter buffer.
int samples = audio_device_buffer_->RequestPlayoutData(frames_per_buffer_);
if (samples <= 0) {
ALOGE("AudioDeviceBuffer::RequestPlayoutData failed!");
RTC_LOG(LS_ERROR) << "AudioDeviceBuffer::RequestPlayoutData failed";
return;
}
RTC_DCHECK_EQ(samples, frames_per_buffer_);