Audio record improvement

Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
camera2
Leo Ma 9 years ago
parent ad73ed4397
commit f6f071a6b6

@ -31,12 +31,12 @@ public class SrsEncoder {
public static int vLandscapeHeight = 720; public static int vLandscapeHeight = 720;
public static int vOutWidth = 720; // Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK public static int vOutWidth = 720; // Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK
public static int vOutHeight = 1280; // Since Y component is quadruple size as U and V component, the stride must be set as 32x public static int vOutHeight = 1280; // Since Y component is quadruple size as U and V component, the stride must be set as 32x
public static int vBitrate = 1200 * 1000; // 1200kbps public static int vBitrate = 1200 * 1024; // 1200 kbps
public static final int VFPS = 24; public static final int VFPS = 24;
public static final int VGOP = 48; public static final int VGOP = 48;
public static final int ASAMPLERATE = 44100; public static final int ASAMPLERATE = 44100;
public static int aChannelConfig = AudioFormat.CHANNEL_IN_STEREO; public static int aChannelConfig = AudioFormat.CHANNEL_IN_STEREO;
public static final int ABITRATE = 32 * 1000; // 32kbps public static final int ABITRATE = 128 * 1024; // 128 kbps
private SrsEncodeHandler mHandler; private SrsEncodeHandler mHandler;
@ -231,12 +231,12 @@ public class SrsEncoder {
} }
public void setVideoHDMode() { public void setVideoHDMode() {
vBitrate = 1200 * 1000; // 1200 kbps vBitrate = 1200 * 1024; // 1200 kbps
x264Preset = "veryfast"; x264Preset = "veryfast";
} }
public void setVideoSmoothMode() { public void setVideoSmoothMode() {
vBitrate = 500 * 1000; // 500 kbps vBitrate = 500 * 1024; // 500 kbps
x264Preset = "superfast"; x264Preset = "superfast";
} }
@ -391,10 +391,11 @@ public class SrsEncoder {
} }
public AudioRecord chooseAudioRecord() { public AudioRecord chooseAudioRecord() {
int minBufferSize = AudioRecord.getMinBufferSize(SrsEncoder.ASAMPLERATE, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT); AudioRecord mic = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, SrsEncoder.ASAMPLERATE,
AudioRecord mic = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, SrsEncoder.ASAMPLERATE, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize); AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, getPcmBufferSize() * 4);
if (mic.getState() != AudioRecord.STATE_INITIALIZED) { if (mic.getState() != AudioRecord.STATE_INITIALIZED) {
mic = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, SrsEncoder.ASAMPLERATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize); mic = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, SrsEncoder.ASAMPLERATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, getPcmBufferSize() * 4);
if (mic.getState() != AudioRecord.STATE_INITIALIZED) { if (mic.getState() != AudioRecord.STATE_INITIALIZED) {
mic = null; mic = null;
} else { } else {
@ -407,6 +408,12 @@ public class SrsEncoder {
return mic; return mic;
} }
private int getPcmBufferSize() {
int pcmBufSize = AudioRecord.getMinBufferSize(ASAMPLERATE, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT) + 8191;
return pcmBufSize - (pcmBufSize % 8192);
}
// choose the video encoder by name. // choose the video encoder by name.
private MediaCodecInfo chooseVideoEncoder(String name) { private MediaCodecInfo chooseVideoEncoder(String name) {
int nbCodecs = MediaCodecList.getCodecCount(); int nbCodecs = MediaCodecList.getCodecCount();

@ -19,6 +19,7 @@ public class SrsPublisher {
private static AudioRecord mic; private static AudioRecord mic;
private static AcousticEchoCanceler aec; private static AcousticEchoCanceler aec;
private static AutomaticGainControl agc; private static AutomaticGainControl agc;
private byte[] mPcmBuffer = new byte[4096];
private boolean aloop = false; private boolean aloop = false;
private Thread aworker; private Thread aworker;
@ -219,14 +220,12 @@ public class SrsPublisher {
private void startAudio() { private void startAudio() {
if (mic != null) { if (mic != null) {
mic.startRecording(); mic.startRecording();
byte pcmBuffer[] = new byte[4096];
while (aloop && !Thread.interrupted()) { while (aloop && !Thread.interrupted()) {
int size = mic.read(pcmBuffer, 0, pcmBuffer.length); int size = mic.read(mPcmBuffer, 0, mPcmBuffer.length);
if (size <= 0) { if (size <= 0) {
break; break;
} }
mEncoder.onGetPcmFrame(pcmBuffer, size); mEncoder.onGetPcmFrame(mPcmBuffer, size);
} }
} }
} }

Loading…
Cancel
Save