Skip to content

Commit 926c10b

Browse files
author
archurtan
committed
音频采集--adts头部自适应channel config
Change-Id: I2175b3208a949e67557642387bafd24d044aa977
1 parent 5d5cb3b commit 926c10b

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

sdk/video-link-android/src/main/java/com/tencent/iot/video/link/util/audio/AudioRecordUtil.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99

1010
public class AudioRecordUtil implements EncoderListener {
11-
private int channelConfig = AudioFormat.CHANNEL_IN_STEREO; //设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
12-
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT; //音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
11+
private static final int DEFAULT_CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_STEREO; //设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
12+
private static final int DEFAULT_AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT; //音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
1313
private volatile boolean recorderState = true; //录制状态
1414
private byte[] buffer;
1515
private AudioRecord audioRecord;
@@ -21,11 +21,12 @@ public class AudioRecordUtil implements EncoderListener {
2121
private int sampleRate; //音频采样率
2222
private int channel;
2323
private int bitDepth;
24+
private int channelCount; //声道数
2425

2526
public AudioRecordUtil(Context ctx, String id, int sampleRate) {
2627
context = ctx;
2728
deviceId = id;
28-
init(sampleRate, channelConfig, audioFormat);
29+
init(sampleRate, DEFAULT_CHANNEL_CONFIG, DEFAULT_AUDIO_FORMAT);
2930
}
3031
public AudioRecordUtil(Context ctx, String id, int sampleRate, int channel, int bitDepth) {
3132
context = ctx;
@@ -38,6 +39,11 @@ private void init(int sampleRate, int channel, int bitDepth) {
3839
this.sampleRate = sampleRate;
3940
this.channel = channel;
4041
this.bitDepth = bitDepth;
42+
if (channel == AudioFormat.CHANNEL_IN_MONO) {
43+
this.channelCount = 1;
44+
} else if (channel == AudioFormat.CHANNEL_IN_STEREO) {
45+
this.channelCount = 2;
46+
}
4147
}
4248

4349
/**
@@ -53,7 +59,7 @@ public void start() {
5359
private void reset() {
5460
buffer = new byte[recordMinBufferSize];
5561
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channel, bitDepth, recordMinBufferSize);
56-
pcmEncoder = new PCMEncoder(sampleRate, this, PCMEncoder.AAC_FORMAT);
62+
pcmEncoder = new PCMEncoder(sampleRate, channelCount, this, PCMEncoder.AAC_FORMAT);
5763
flvPacker = new FLVPacker();
5864
}
5965

sdk/video-link-android/src/main/java/com/tencent/iot/video/link/util/audio/PCMEncoder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public class PCMEncoder {
1515
private static final int KEY_BIT_RATE = 96000;
1616
//读取数据的最大字节数
1717
private static final int KEY_MAX_INPUT_SIZE = 1024 * 1024;
18-
//声道数
19-
private static final int CHANNEL_COUNT = 2;
2018
public static final int AAC_FORMAT = 0;
2119
public static final int G711_FORMAT = 1;
2220
private MediaCodec mediaCodec;
@@ -26,6 +24,8 @@ public class PCMEncoder {
2624
private EncoderListener encoderListener;
2725
private int encodeType = 0;
2826
private int sampleRate = 0;
27+
//声道数
28+
private int channelCount = 0;
2929

3030
// 采样频率对照表
3131
private static Map<Integer, Integer> samplingFrequencyIndexMap = new HashMap<>();
@@ -45,10 +45,11 @@ public class PCMEncoder {
4545
samplingFrequencyIndexMap.put(8000, 11);
4646
}
4747

48-
public PCMEncoder(int sampleRate, EncoderListener encoderListener, int encodeFormat) {
48+
public PCMEncoder(int sampleRate, int channelCount, EncoderListener encoderListener, int encodeFormat) {
4949
this.encoderListener = encoderListener;
5050
this.encodeType = encodeFormat;
5151
this.sampleRate = sampleRate;
52+
this.channelCount = channelCount;
5253
init();
5354
}
5455

@@ -59,7 +60,7 @@ private void init() {
5960
try {
6061
//参数对应-> mime type、采样率、声道数
6162
MediaFormat encodeFormat = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_AAC,
62-
sampleRate, CHANNEL_COUNT);
63+
sampleRate, channelCount);
6364
//比特率
6465
encodeFormat.setInteger(MediaFormat.KEY_BIT_RATE, KEY_BIT_RATE);
6566
encodeFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
@@ -130,7 +131,7 @@ public void encodeData(byte[] data) {
130131
*/
131132
private void addADTStoPacket(byte[] packet, int packetLen) {
132133
int profile = 2; // AAC LC
133-
int chanCfg = 2; // CPE
134+
int chanCfg = channelCount;
134135
int freqIdx = samplingFrequencyIndexMap.get(sampleRate);
135136
// filled in ADTS data
136137
packet[0] = (byte) 0xFF;

0 commit comments

Comments
 (0)