Add option to select audio codec

Introduce the selection mechanism. Alternative codecs will be added
later.
This commit is contained in:
Romain Vimont
2023-02-18 19:05:43 +01:00
parent 4dbb7aa685
commit 105b482082
10 changed files with 64 additions and 6 deletions

View File

@@ -37,7 +37,6 @@ public final class AudioEncoder {
}
}
private static final String MIMETYPE = MediaFormat.MIMETYPE_AUDIO_OPUS;
private static final int SAMPLE_RATE = 48000;
private static final int CHANNELS = 2;
@@ -90,9 +89,9 @@ public final class AudioEncoder {
return builder.build();
}
private static MediaFormat createFormat(int bitRate) {
private static MediaFormat createFormat(String mimeType, int bitRate) {
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, MIMETYPE);
format.setString(MediaFormat.KEY_MIME, mimeType);
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, CHANNELS);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, SAMPLE_RATE);
@@ -203,13 +202,15 @@ public final class AudioEncoder {
public void encode() throws IOException {
try {
try {
mediaCodec = MediaCodec.createEncoderByType(MIMETYPE); // may throw IOException
String mimeType = streamer.getCodec().getMimeType();
mediaCodec = MediaCodec.createEncoderByType(mimeType); // may throw IOException
recorder = createAudioRecord();
mediaCodecThread = new HandlerThread("AudioEncoder");
mediaCodecThread.start();
MediaFormat format = createFormat(bitRate);
MediaFormat format = createFormat(mimeType, bitRate);
mediaCodec.setCallback(new EncoderCallback(), new Handler(mediaCodecThread.getLooper()));
mediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

View File

@@ -11,6 +11,7 @@ public class Options {
private boolean audio = true;
private int maxSize;
private VideoCodec codec = VideoCodec.H264;
private AudioCodec audioCodec = AudioCodec.OPUS;
private int bitRate = 8000000;
private int audioBitRate = 196000;
private int maxFps;
@@ -75,6 +76,14 @@ public class Options {
this.codec = codec;
}
public AudioCodec getAudioCodec() {
return audioCodec;
}
public void setAudioCodec(AudioCodec audioCodec) {
this.audioCodec = audioCodec;
}
public int getBitRate() {
return bitRate;
}

View File

@@ -111,7 +111,8 @@ public final class Server {
AudioEncoder audioEncoder = null;
if (audio) {
Streamer audioStreamer = new Streamer(connection.getAudioFd(), AudioCodec.OPUS, options.getSendCodecId(), options.getSendFrameMeta());
Streamer audioStreamer = new Streamer(connection.getAudioFd(), options.getAudioCodec(), options.getSendCodecId(),
options.getSendFrameMeta());
audioEncoder = new AudioEncoder(audioStreamer, options.getAudioBitRate());
audioEncoder.start();
}
@@ -202,6 +203,13 @@ public final class Server {
}
options.setCodec(codec);
break;
case "audio_codec":
AudioCodec audioCodec = AudioCodec.findByName(value);
if (audioCodec == null) {
throw new IllegalArgumentException("Audio codec " + value + " not supported");
}
options.setAudioCodec(audioCodec);
break;
case "max_size":
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
options.setMaxSize(maxSize);