Improves error handling for playout initialization on Android.

We no longer crash when initialization fails.

BUG=

Review URL: https://codereview.webrtc.org/1858213002

Cr-Commit-Position: refs/heads/master@{#12241}
This commit is contained in:
henrika 2016-04-05 07:20:27 -07:00 committed by Commit bot
parent 766ad3b989
commit ef38b564ea
3 changed files with 24 additions and 11 deletions

View File

@ -33,7 +33,7 @@ AudioTrackJni::JavaAudioTrack::JavaAudioTrack(
NativeRegistration* native_reg,
std::unique_ptr<GlobalRef> audio_track)
: audio_track_(std::move(audio_track)),
init_playout_(native_reg->GetMethodId("initPlayout", "(II)V")),
init_playout_(native_reg->GetMethodId("initPlayout", "(II)Z")),
start_playout_(native_reg->GetMethodId("startPlayout", "()Z")),
stop_playout_(native_reg->GetMethodId("stopPlayout", "()Z")),
set_stream_volume_(native_reg->GetMethodId("setStreamVolume", "(I)Z")),
@ -43,8 +43,8 @@ AudioTrackJni::JavaAudioTrack::JavaAudioTrack(
AudioTrackJni::JavaAudioTrack::~JavaAudioTrack() {}
void AudioTrackJni::JavaAudioTrack::InitPlayout(int sample_rate, int channels) {
audio_track_->CallVoidMethod(init_playout_, sample_rate, channels);
bool AudioTrackJni::JavaAudioTrack::InitPlayout(int sample_rate, int channels) {
return audio_track_->CallBooleanMethod(init_playout_, sample_rate, channels);
}
bool AudioTrackJni::JavaAudioTrack::StartPlayout() {
@ -123,8 +123,11 @@ int32_t AudioTrackJni::InitPlayout() {
RTC_DCHECK(thread_checker_.CalledOnValidThread());
RTC_DCHECK(!initialized_);
RTC_DCHECK(!playing_);
j_audio_track_->InitPlayout(
audio_parameters_.sample_rate(), audio_parameters_.channels());
if (!j_audio_track_->InitPlayout(
audio_parameters_.sample_rate(), audio_parameters_.channels())) {
ALOGE("InitPlayout failed!");
return -1;
}
initialized_ = true;
return 0;
}

View File

@ -47,7 +47,7 @@ class AudioTrackJni {
std::unique_ptr<GlobalRef> audio_track);
~JavaAudioTrack();
void InitPlayout(int sample_rate, int channels);
bool InitPlayout(int sample_rate, int channels);
bool StartPlayout();
bool StopPlayout();
bool SetStreamVolume(int volume);

View File

@ -152,7 +152,7 @@ class WebRtcAudioTrack {
}
}
private void initPlayout(int sampleRate, int channels) {
private boolean initPlayout(int sampleRate, int channels) {
Logging.d(TAG, "initPlayout(sampleRate=" + sampleRate + ", channels="
+ channels + ")");
final int bytesPerFrame = channels * (BITS_PER_SAMPLE / 8);
@ -191,17 +191,27 @@ class WebRtcAudioTrack {
AudioTrack.MODE_STREAM);
} catch (IllegalArgumentException e) {
Logging.d(TAG, e.getMessage());
return;
return false;
}
assertTrue(audioTrack.getState() == AudioTrack.STATE_INITIALIZED);
assertTrue(audioTrack.getPlayState() == AudioTrack.PLAYSTATE_STOPPED);
assertTrue(audioTrack.getStreamType() == AudioManager.STREAM_VOICE_CALL);
// It can happen that an AudioTrack is created but it was not successfully
// initialized upon creation. Seems to be the case e.g. when the maximum
// number of globally available audio tracks is exceeded.
if (audioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
Logging.e(TAG, "Initialization of audio track failed.");
return false;
}
return true;
}
private boolean startPlayout() {
Logging.d(TAG, "startPlayout");
assertTrue(audioTrack != null);
assertTrue(audioThread == null);
if (audioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
Logging.e(TAG, "Audio track is not successfully initialized.");
return false;
}
audioThread = new AudioTrackThread("AudioTrackJavaThread");
audioThread.start();
return true;