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

@ -25,6 +25,12 @@ Encode the audio at the given bit\-rate, expressed in bits/s. Unit suffixes are
Default is 196K (196000).
.TP
.BI "\-\-audio\-codec " name
Select an audio codec (opus).
Default is opus.
.TP
.BI "\-b, \-\-bit\-rate " value
Encode the video at the given bit\-rate, expressed in bits/s. Unit suffixes are supported: '\fBK\fR' (x1000) and '\fBM\fR' (x1000000).

View file

@ -60,6 +60,7 @@
#define OPT_CODEC 1040
#define OPT_NO_AUDIO 1041
#define OPT_AUDIO_BIT_RATE 1042
#define OPT_AUDIO_CODEC 1043
struct sc_option {
char shortopt;
@ -108,6 +109,13 @@ static const struct sc_option options[] = {
"Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n"
"Default is 196K (196000).",
},
{
.longopt_id = OPT_AUDIO_CODEC,
.longopt = "audio-codec",
.argdesc = "name",
.text = "Select an audio codec (opus).\n"
"Default is opus.",
},
{
.shortopt = 'b',
.longopt = "bit-rate",
@ -1418,6 +1426,16 @@ parse_codec(const char *optarg, enum sc_codec *codec) {
return false;
}
static bool
parse_audio_codec(const char *optarg, enum sc_codec *codec) {
if (!strcmp(optarg, "opus")) {
*codec = SC_CODEC_OPUS;
return true;
}
LOGE("Unsupported audio codec: %s (expected opus)", optarg);
return false;
}
static bool
parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
const char *optstring, const struct option *longopts) {
@ -1664,6 +1682,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
}
break;
case OPT_AUDIO_CODEC:
if (!parse_audio_codec(optarg, &opts->audio_codec)) {
return false;
}
break;
case OPT_OTG:
#ifdef HAVE_USB
opts->otg = true;

View file

@ -14,6 +14,7 @@ const struct scrcpy_options scrcpy_options_default = {
#endif
.log_level = SC_LOG_LEVEL_INFO,
.codec = SC_CODEC_H264,
.audio_codec = SC_CODEC_OPUS,
.record_format = SC_RECORD_FORMAT_AUTO,
.keyboard_input_mode = SC_KEYBOARD_INPUT_MODE_INJECT,
.port_range = {

View file

@ -27,6 +27,7 @@ enum sc_codec {
SC_CODEC_H264,
SC_CODEC_H265,
SC_CODEC_AV1,
SC_CODEC_OPUS,
};
enum sc_lock_video_orientation {
@ -100,6 +101,7 @@ struct scrcpy_options {
#endif
enum sc_log_level log_level;
enum sc_codec codec;
enum sc_codec audio_codec;
enum sc_record_format record_format;
enum sc_keyboard_input_mode keyboard_input_mode;
enum sc_mouse_input_mode mouse_input_mode;

View file

@ -351,6 +351,7 @@ scrcpy(struct scrcpy_options *options) {
.select_tcpip = options->select_tcpip,
.log_level = options->log_level,
.codec = options->codec,
.audio_codec = options->audio_codec,
.crop = options->crop,
.port_range = options->port_range,
.tunnel_host = options->tunnel_host,

View file

@ -165,6 +165,8 @@ sc_server_get_codec_name(enum sc_codec codec) {
return "h265";
case SC_CODEC_AV1:
return "av1";
case SC_CODEC_OPUS:
return "opus";
default:
return NULL;
}
@ -227,6 +229,10 @@ execute_server(struct sc_server *server,
if (params->codec != SC_CODEC_H264) {
ADD_PARAM("codec=%s", sc_server_get_codec_name(params->codec));
}
if (params->audio_codec != SC_CODEC_OPUS) {
ADD_PARAM("audio_codec=%s",
sc_server_get_codec_name(params->audio_codec));
}
if (params->max_size) {
ADD_PARAM("max_size=%" PRIu16, params->max_size);
}

View file

@ -26,6 +26,7 @@ struct sc_server_params {
const char *req_serial;
enum sc_log_level log_level;
enum sc_codec codec;
enum sc_codec audio_codec;
const char *crop;
const char *codec_options;
const char *encoder_name;

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);