Skip to content

Commit

Permalink
[cli] roc-streaminggh-608: Add --io-encoding to all CLI tools
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Aug 21, 2024
1 parent a304d63 commit 1300dd0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 95 deletions.
7 changes: 5 additions & 2 deletions src/tools/roc_copy/cmdline.ggo
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ section "Options"
option "frame-len" - "Duration of the internal frames, TIME units"
typestr="TIME" string optional

option "rate" r "Output sample rate, Hz"
int optional
option "output-encoding" - "Output file encoding"
typestr="IO_ENCODING" string optional

option "resampler-backend" - "Resampler backend"
values="default","builtin","speex","speexdec" default="default" enum optional
Expand All @@ -38,6 +38,9 @@ FILE_URI defines an absolute or relative file path, e.g.:
FILE_FORMAT is the output file format name, e.g.:
wav; ogg; mp3

IO_ENCODING is input or output format/rate/channels, e.g.:
s16/44100/mono; f32/48000/stereo

TIME is an integer or floating-point number with a suffix, e.g.:
123ns; 1.23us; 1.23ms; 1.23s; 1.23m; 1.23h;

Expand Down
136 changes: 57 additions & 79 deletions src/tools/roc_copy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ int main(int argc, char** argv) {
core::HeapArena::set_guards(core::HeapArena_DefaultGuards
| core::HeapArena_LeakGuard);

core::HeapArena arena;

core::CrashHandler crash_handler;

gengetopt_args_info args;
Expand Down Expand Up @@ -59,9 +61,6 @@ int main(int argc, char** argv) {
pipeline::TranscoderConfig transcoder_config;

sndio::IoConfig source_config;
source_config.sample_spec.set_channel_set(
transcoder_config.input_sample_spec.channel_set());
source_config.sample_spec.set_sample_rate(0);

if (args.frame_len_given) {
if (!core::parse_duration(args.frame_len_arg, source_config.frame_length)) {
Expand All @@ -74,7 +73,38 @@ int main(int argc, char** argv) {
}
}

core::HeapArena arena;
switch (args.resampler_backend_arg) {
case resampler_backend_arg_default:
transcoder_config.resampler.backend = audio::ResamplerBackend_Auto;
break;
case resampler_backend_arg_builtin:
transcoder_config.resampler.backend = audio::ResamplerBackend_Builtin;
break;
case resampler_backend_arg_speex:
transcoder_config.resampler.backend = audio::ResamplerBackend_Speex;
break;
case resampler_backend_arg_speexdec:
transcoder_config.resampler.backend = audio::ResamplerBackend_SpeexDec;
break;
default:
break;
}

switch (args.resampler_profile_arg) {
case resampler_profile_arg_low:
transcoder_config.resampler.profile = audio::ResamplerProfile_Low;
break;
case resampler_profile_arg_medium:
transcoder_config.resampler.profile = audio::ResamplerProfile_Medium;
break;
case resampler_profile_arg_high:
transcoder_config.resampler.profile = audio::ResamplerProfile_High;
break;
default:
break;
}

transcoder_config.enable_profiling = args.profile_flag;

core::SlabPool<audio::Frame> frame_pool("frame_pool", arena);
core::SlabPool<core::Buffer> frame_buffer_pool(
Expand All @@ -95,20 +125,17 @@ int main(int argc, char** argv) {
}

address::IoUri input_uri(arena);
if (args.input_given) {
if (!address::parse_io_uri(args.input_arg, input_uri) || !input_uri.is_file()) {
roc_log(LogError, "invalid --input file URI");
return 1;
}
if (!address::parse_io_uri(args.input_arg, input_uri) || !input_uri.is_file()) {
roc_log(LogError, "invalid --input file URI");
return 1;
}

if (!args.input_format_given && input_uri.is_special_file()) {
roc_log(LogError, "--input-format should be specified if --input is \"-\"");
return 1;
}

core::ScopedPtr<sndio::ISource> input_source;
if (input_uri.is_valid()) {
{
const status::StatusCode code = backend_dispatcher.open_source(
input_uri, args.input_format_arg, source_config, input_source);

Expand All @@ -117,64 +144,25 @@ int main(int argc, char** argv) {
status::code_to_str(code));
return 1;
}
} else {
const status::StatusCode code =
backend_dispatcher.open_default_source(source_config, input_source);

if (code != status::StatusOK) {
roc_log(LogError, "can't open default --input file or device: status=%s",
status::code_to_str(code));
if (input_source->has_clock()) {
roc_log(LogError, "unsupported --input type");
return 1;
}
}
if (input_source->has_clock()) {
roc_log(LogError, "unsupported --input type");
return 1;
}

transcoder_config.input_sample_spec.set_sample_rate(
input_source->sample_spec().sample_rate());
transcoder_config.input_sample_spec = input_source->sample_spec();

if (args.rate_given) {
transcoder_config.output_sample_spec.set_sample_rate((size_t)args.rate_arg);
if (args.output_encoding_given) {
if (!audio::parse_sample_spec(args.output_encoding_arg,
transcoder_config.output_sample_spec)) {
roc_log(LogError, "invalid --output-encoding");
return 1;
}
} else {
transcoder_config.output_sample_spec.set_sample_rate(
transcoder_config.input_sample_spec.sample_rate());
}

switch (args.resampler_backend_arg) {
case resampler_backend_arg_default:
transcoder_config.resampler.backend = audio::ResamplerBackend_Auto;
break;
case resampler_backend_arg_builtin:
transcoder_config.resampler.backend = audio::ResamplerBackend_Builtin;
break;
case resampler_backend_arg_speex:
transcoder_config.resampler.backend = audio::ResamplerBackend_Speex;
break;
case resampler_backend_arg_speexdec:
transcoder_config.resampler.backend = audio::ResamplerBackend_SpeexDec;
break;
default:
break;
transcoder_config.output_sample_spec = transcoder_config.input_sample_spec;
}

switch (args.resampler_profile_arg) {
case resampler_profile_arg_low:
transcoder_config.resampler.profile = audio::ResamplerProfile_Low;
break;
case resampler_profile_arg_medium:
transcoder_config.resampler.profile = audio::ResamplerProfile_Medium;
break;
case resampler_profile_arg_high:
transcoder_config.resampler.profile = audio::ResamplerProfile_High;
break;
default:
break;
}

transcoder_config.enable_profiling = args.profile_flag;

audio::IFrameWriter* output_writer = NULL;

sndio::IoConfig sink_config;
Expand All @@ -189,37 +177,27 @@ int main(int argc, char** argv) {
return 1;
}
}

if (!args.output_format_given && output_uri.is_special_file()) {
roc_log(LogError, "--output-format should be specified if --output is \"-\"");
return 1;
}

core::ScopedPtr<sndio::ISink> output_sink;
if (args.output_given) {
if (output_uri.is_valid()) {
const status::StatusCode code = backend_dispatcher.open_sink(
output_uri, args.output_format_arg, sink_config, output_sink);

if (code != status::StatusOK) {
roc_log(LogError, "can't open --output file or device: status=%s",
status::code_to_str(code));
return 1;
}
} else {
const status::StatusCode code =
backend_dispatcher.open_default_sink(sink_config, output_sink);

if (code != status::StatusOK) {
roc_log(LogError, "can't open default --output device: status=%s",
status::code_to_str(code));
return 1;
}
const status::StatusCode code = backend_dispatcher.open_sink(
output_uri, args.output_format_arg, sink_config, output_sink);

if (code != status::StatusOK) {
roc_log(LogError, "can't open --output file or device: status=%s",
status::code_to_str(code));
return 1;
}

if (output_sink->has_clock()) {
roc_log(LogError, "unsupported --output type");
return 1;
}

output_writer = output_sink.get();
}

Expand Down
7 changes: 5 additions & 2 deletions src/tools/roc_recv/cmdline.ggo
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ section "Options"
option "plc" - "Which PLC algorithm to use"
values="none","beep" default="none" enum optional

option "rate" - "Override output device sample rate, Hz"
int optional
option "io-encoding" - "Force output file or device encoding"
typestr="IO_ENCODING" string optional

option "frame-len" - "Duration of the I/O frames, TIME units"
typestr="TIME" string optional
Expand All @@ -88,6 +88,9 @@ IO_URI is a device or file URI, e.g.:
pulse://default; pulse://alsa_input.pci-0000_00_1f.3.analog-stereo
file:///home/user/test.wav; file:./test.wav; file:-

IO_ENCODING is output file or device format/rate/channels, e.g.:
s16/44100/mono; f32/48000/stereo

FILE_FORMAT is the output file format name, e.g.:
wav; ogg; mp3

Expand Down
8 changes: 3 additions & 5 deletions src/tools/roc_recv/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,11 @@ int main(int argc, char** argv) {
}
}

// TODO(gh-608): replace --rate with --io-encoding
if (args.rate_given) {
if (args.rate_arg <= 0) {
roc_log(LogError, "invalid --rate: should be > 0");
if (args.io_encoding_given) {
if (!audio::parse_sample_spec(args.io_encoding_arg, io_config.sample_spec)) {
roc_log(LogError, "invalid --io-encoding");
return 1;
}
io_config.sample_spec.set_sample_rate((size_t)args.rate_arg);
}

// TODO(gh-568): remove set_frame_size() after removing sox
Expand Down
7 changes: 5 additions & 2 deletions src/tools/roc_send/cmdline.ggo
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ section "Options"
option "resampler-profile" - "Resampler profile"
values="low","medium","high" default="medium" enum optional

option "rate" - "Override input device sample rate, Hz"
int optional
option "io-encoding" - "Force input device encoding"
typestr="IO_ENCODING" string optional

option "nbsrc" - "Number of source packets in FEC block"
int optional
Expand Down Expand Up @@ -81,6 +81,9 @@ IO_URI is a device or file URI, e.g.:
pulse://default; pulse://alsa_input.pci-0000_00_1f.3.analog-stereo
file:///home/user/test.wav; file:./test.wav; file:-

IO_ENCODING is input device format/rate/channels, e.g.:
s16/44100/mono; f32/48000/stereo

FILE_FORMAT is the output file format name, e.g.:
wav; ogg; mp3

Expand Down
8 changes: 3 additions & 5 deletions src/tools/roc_send/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ int main(int argc, char** argv) {
}
}

// TODO(gh-608): replace --rate with --io-encoding
if (args.rate_given) {
if (args.rate_arg <= 0) {
roc_log(LogError, "invalid --rate: should be > 0");
if (args.io_encoding_given) {
if (!audio::parse_sample_spec(args.io_encoding_arg, io_config.sample_spec)) {
roc_log(LogError, "invalid --io-encoding");
return 1;
}
io_config.sample_spec.set_sample_rate((size_t)args.rate_arg);
}

// TODO(gh-568): remove set_frame_size() after removing sox
Expand Down

0 comments on commit 1300dd0

Please sign in to comment.