MT#56744 audio transcoding fixes

* detect WAV GSM 6.10 format and provide
  sox with the correct type 'gsm'/'sndfile' as otherwise
  it fails to detect it
* use Capture::Tiny instead of IPC::Simple for better capture
  (out,err,rc)
* mp3/ogg decoding fixes to reflect the GSM 6.10 possible formats

Change-Id: I79c5657543ccc4741efb3bdd56d7a0a37f251a4c
mr11.3
Kirill Solomko 2 years ago
parent 35949e9a59
commit f8831929a4

@ -2,7 +2,8 @@ package NGCP::Panel::Utils::Sounds;
use strict;
use warnings;
use IPC::System::Simple qw/capturex/;
use English;
use Capture::Tiny qw(capture);
use File::Temp qw/tempfile/;
use NGCP::Panel::Utils::Sems;
use NGCP::Panel::Utils::Preferences;
@ -12,40 +13,58 @@ use File::Basename;
sub transcode_file {
my ($tmpfile, $source_codec, $target_codec) = @_;
my $out;
my $rc = 0;
my ($cmd, $out, $err);
my $is_gsm = 0;
my @conv_args;
## quite snappy, but breaks SOAP (sigpipe's) and the catalyst devel server
## need instead to redirect like below
# check if the wav is gsm 6.10 encoded
if (lc($source_codec) eq 'wav') {
$cmd = join(' ', '/usr/bin/file', $tmpfile);
($out, $err, $rc) = capture {
system($cmd);
};
if ($out =~ /GSM 6\.10/) {
$is_gsm = 1;
@conv_args = (qw/--encoding gsm --type sndfile/);
}
}
SWITCH: for ($target_codec) {
/^PCMA$/ && do {
@conv_args = ($tmpfile, qw/--type raw --bits 16 --channels 1 -e a-law - rate 16k/);
@conv_args = (@conv_args, $tmpfile, qw/--type raw --bits 16 --channels 1 -e a-law --rate 16k -/);
last SWITCH;
};
/^WAV$/ && do {
if ($source_codec eq 'PCMA') {
# this can actually only come from inside
# certain files will be stored as PCMA (for handles with name "music_on_hold")
@conv_args = ( qw/-A --rate 16k --channels 1 --type raw/, $tmpfile, "--type", "wav", "-");
@conv_args = (qw/-A --rate 16k --channels 1 --type raw/, $tmpfile, qw/--type wav -/);
}
else {
@conv_args = ($tmpfile, qw/--type wav --bits 16 - rate 16k/);
@conv_args = (@conv_args, $tmpfile, qw/--type wav --bits 16 --rate 16k -/);
}
last SWITCH;
};
/^MP3$/ && do {
@conv_args = ($tmpfile, qw/--type mp3 --bits 16 - rate 16k/);
@conv_args = (@conv_args, $tmpfile, qw/--type mp3 --rate 16k -/);
last SWITCH;
};
/^OGG$/ && do {
@conv_args = ($tmpfile, qw/--type ogg --bits 16 - rate 16k/);
@conv_args = (@conv_args, $tmpfile, qw/--type ogg --rate 16k -/);
last SWITCH;
};
# default
} # SWITCH
$out = capturex([0], "/usr/bin/sox", @conv_args);
$cmd = join(' ', '/usr/bin/sox', '-V1', @conv_args);
($out, $err, $rc) = capture {
system($cmd);
};
if ($rc != 0 && $err) {
die "Cannot transcode sound file is_gsm=$is_gsm source_codec=$source_codec target_codec=$target_codec cmd=($cmd) error: $err";
}
return $out;
}

Loading…
Cancel
Save