Improves release of allocated audio resources on Android.

BUG=webrtc:6890
R=magjed@webrtc.org, solenberg@webrtc.org

Review-Url: https://codereview.webrtc.org/2574053003 .
Cr-Commit-Position: refs/heads/master@{#15637}
This commit is contained in:
henrika 2016-12-15 15:42:56 +01:00
parent 43c382111d
commit ac8d5164f0
2 changed files with 25 additions and 8 deletions

View File

@ -189,10 +189,12 @@ public class WebRtcAudioRecord {
AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes);
} catch (IllegalArgumentException e) {
Logging.e(TAG, e.getMessage());
releaseAudioResources();
return -1;
}
if (audioRecord == null || audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
Logging.e(TAG, "Failed to create a new AudioRecord instance");
releaseAudioResources();
return -1;
}
if (effects != null) {
@ -233,8 +235,7 @@ public class WebRtcAudioRecord {
if (effects != null) {
effects.release();
}
audioRecord.release();
audioRecord = null;
releaseAudioResources();
return true;
}
@ -274,4 +275,12 @@ public class WebRtcAudioRecord {
Logging.w(TAG, "setMicrophoneMute(" + mute + ")");
microphoneMute = mute;
}
// Releases the native AudioRecord resources.
private void releaseAudioResources() {
if (audioRecord != null) {
audioRecord.release();
audioRecord = null;
}
}
}

View File

@ -77,6 +77,7 @@ public class WebRtcAudioTrack {
assertTrue(audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING);
} catch (IllegalStateException e) {
Logging.e(TAG, "AudioTrack.play failed: " + e.getMessage());
releaseAudioResources();
return;
}
@ -202,14 +203,16 @@ public class WebRtcAudioTrack {
AudioFormat.ENCODING_PCM_16BIT, minBufferSizeInBytes, AudioTrack.MODE_STREAM);
} catch (IllegalArgumentException e) {
Logging.d(TAG, e.getMessage());
releaseAudioResources();
return false;
}
// 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) {
if (audioTrack == null || audioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
Logging.e(TAG, "Initialization of audio track failed.");
releaseAudioResources();
return false;
}
logMainParameters();
@ -222,7 +225,7 @@ public class WebRtcAudioTrack {
assertTrue(audioTrack != null);
assertTrue(audioThread == null);
if (audioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
Logging.e(TAG, "Audio track is not successfully initialized.");
Logging.e(TAG, "AudioTrack instance is not successfully initialized.");
return false;
}
audioThread = new AudioTrackThread("AudioTrackJavaThread");
@ -236,10 +239,7 @@ public class WebRtcAudioTrack {
logUnderrunCount();
audioThread.joinThread();
audioThread = null;
if (audioTrack != null) {
audioTrack.release();
audioTrack = null;
}
releaseAudioResources();
return true;
}
@ -330,4 +330,12 @@ public class WebRtcAudioTrack {
Logging.w(TAG, "setSpeakerMute(" + mute + ")");
speakerMute = mute;
}
// Releases the native AudioTrack resources.
private void releaseAudioResources() {
if (audioTrack != null) {
audioTrack.release();
audioTrack = null;
}
}
}