diff --git a/debian/control b/debian/control index 863cb5b4a3..24f3049fc3 100644 --- a/debian/control +++ b/debian/control @@ -96,6 +96,7 @@ Depends: libsipwise-base-perl, libsoap-lite-perl, libsoap-wsdl-perl, + libsox-fmt-mp3, libstring-mkpasswd-perl, libtemplate-perl, libtemplate-plugin-json-escape-perl, diff --git a/lib/NGCP/Panel/Controller/API/VoicemailRecordings.pm b/lib/NGCP/Panel/Controller/API/VoicemailRecordings.pm index 9ae05b7ed2..57bed05753 100644 --- a/lib/NGCP/Panel/Controller/API/VoicemailRecordings.pm +++ b/lib/NGCP/Panel/Controller/API/VoicemailRecordings.pm @@ -47,6 +47,15 @@ __PACKAGE__->config( }, ); +sub query_params { + return [ + { + param => 'format', + description => 'Output format of the voicemail recording, supported: mp3, ogg, wav', + }, + ]; +} + sub gather_default_action_roles { my ($self, %args) = @_; my @roles = (); push @roles, 'NGCP::Panel::Role::HTTPMethods' if $args{attributes}->{Method}; diff --git a/lib/NGCP/Panel/Controller/API/VoicemailRecordingsItem.pm b/lib/NGCP/Panel/Controller/API/VoicemailRecordingsItem.pm index ce06b49f3b..ebbbddb8b7 100644 --- a/lib/NGCP/Panel/Controller/API/VoicemailRecordingsItem.pm +++ b/lib/NGCP/Panel/Controller/API/VoicemailRecordingsItem.pm @@ -8,6 +8,7 @@ use HTTP::Status qw(:constants); use NGCP::Panel::Utils::ValidateJSON qw(); use NGCP::Panel::Utils::Subscriber; +use NGCP::Panel::Utils::Sounds; require Catalyst::ActionRole::ACL; require NGCP::Panel::Role::HTTPMethods; require Catalyst::ActionRole::RequireSSL; @@ -61,10 +62,17 @@ sub GET :Allow { last unless $self->valid_id($c, $id); my $item = $self->item_by_id($c, $id); last unless $self->resource_exists($c, voicemailrecording => $item); - my $filename = NGCP::Panel::Utils::Subscriber::get_voicemail_filename($c,$item); + my $format = $c->request->params->{format} // 'wav'; + unless ($format && $format =~ /^(wav|mp3|ogg)$/) { + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Unknown format '$format', supported: wav,mp3,ogg."); + last; + } + my $sr = \%NGCP::Panel::Utils::Subscriber::; + my $ss = \%NGCP::Panel::Utils::Sounds::; + my $filename = $sr->{get_voicemail_filename}($c,$item,$format); $c->response->header ('Content-Disposition' => 'attachment; filename="'.$filename.'"'); - $c->response->content_type('audio/x-wav'); - $c->response->body($item->recording); + $c->response->content_type($sr->{get_voicemail_content_type}($c,$format)); + $c->response->body($ss->{transcode_data}($item->recording,'WAV',uc($format))); return; } return; diff --git a/lib/NGCP/Panel/Utils/Sounds.pm b/lib/NGCP/Panel/Utils/Sounds.pm index 1d0527cad3..22417c16b1 100644 --- a/lib/NGCP/Panel/Utils/Sounds.pm +++ b/lib/NGCP/Panel/Utils/Sounds.pm @@ -30,6 +30,14 @@ sub transcode_file { } last SWITCH; }; + /^MP3$/ && do { + @conv_args = ($tmpfile, qw/--type mp3 --bits 16 - rate 8k/); + last SWITCH; + }; + /^OGG$/ && do { + @conv_args = ($tmpfile, qw/--type ogg --bits 16 - rate 8k/); + last SWITCH; + }; # default } # SWITCH diff --git a/lib/NGCP/Panel/Utils/Subscriber.pm b/lib/NGCP/Panel/Utils/Subscriber.pm index 63ce349ab2..4b200039ae 100644 --- a/lib/NGCP/Panel/Utils/Subscriber.pm +++ b/lib/NGCP/Panel/Utils/Subscriber.pm @@ -1827,9 +1827,21 @@ sub get_subscriber_pbx_status{ return 0; } -sub get_voicemail_filename{ - my($c, $voicemail_item) = @_; - return 'voicemail-'.$voicemail_item->msgnum.'.wav'; +sub get_voicemail_filename { + my ($c, $voicemail_item, $format) = @_; + return 'voicemail-'.$voicemail_item->msgnum.'.'.$format; +} + +sub get_voicemail_content_type { + my($c, $format) = @_; + + SWITCH: for ($format) { + /^wav$/ && return "audio/x-wav"; + /^mp3$/ && return "audio/mpeg"; + /^ogg$/ && return "audio/ogg"; + return; + } + return; } 1;