diff --git a/lib/NGCP/Panel/Controller/API/SoundFileRecordingsItem.pm b/lib/NGCP/Panel/Controller/API/SoundFileRecordingsItem.pm index 14aa76adb3..576c485cd4 100644 --- a/lib/NGCP/Panel/Controller/API/SoundFileRecordingsItem.pm +++ b/lib/NGCP/Panel/Controller/API/SoundFileRecordingsItem.pm @@ -62,6 +62,11 @@ sub GET :Allow { my $resource = $self->resource_from_item($c, $item); $resource->{data} = $item->data; + if($item->codec eq "PCMA") { + $resource->{codec} = 'WAV'; + $resource = $self->transcode_data($c, $item->codec, $resource); + last unless($resource); + } $c->response->header ('Content-Disposition' => 'attachment; filename="' . $resource->{filename} . '"'); $c->response->content_type('audio/x-wav'); diff --git a/lib/NGCP/Panel/Controller/Sound.pm b/lib/NGCP/Panel/Controller/Sound.pm index c26e32da7b..4901aecb2f 100644 --- a/lib/NGCP/Panel/Controller/Sound.pm +++ b/lib/NGCP/Panel/Controller/Sound.pm @@ -562,12 +562,12 @@ sub handles_edit :Chained('handles_base') :PathPart('edit') { fields => {}, back_uri => $c->req->uri, ); - + if($posted && $form->validated) { if (defined $upload) { my $soundfile = eval { $upload->slurp }; my $filename = eval { $upload->filename }; - + my $ft = File::Type->new(); unless ($ft->checktype_contents($soundfile) eq 'audio/x-wav') { NGCP::Panel::Utils::Message::error( @@ -577,7 +577,7 @@ sub handles_edit :Chained('handles_base') :PathPart('edit') { ); NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{handles_base_uri}); } - + my $target_codec = 'WAV'; # clear audio caches @@ -593,6 +593,23 @@ sub handles_edit :Chained('handles_base') :PathPart('edit') { NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{handles_base_uri}); } + if ($file_result->handle->name eq 'music_on_hold' && !$file_result->set->contract_id) { + $target_codec = 'PCMA'; + $filename =~ s/\.[^.]+$/.pcma/; + } + + try { + $soundfile = NGCP::Panel::Utils::Sounds::transcode_file( + $upload->tempname, 'WAV', $target_codec); + } catch ($e) { + NGCP::Panel::Utils::Message::error( + c => $c, + log => 'Transcoding audio file failed', + desc => $c->loc('Transcoding audio file failed'), + ); + NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{handles_base_uri}); + } + try { $file_result->update({ loopplay => $form->values->{loopplay}, @@ -669,12 +686,28 @@ sub handles_delete :Chained('handles_base') :PathPart('delete') { sub handles_download :Chained('handles_base') :PathPart('download') :Args(0) { my ($self, $c) = @_; - + my $file = $c->stash->{file_result}; my $filename = $file->filename; $filename =~ s/\.\w+$/.wav/; - my $data = $file->data; + my $data; + if($file->codec ne 'WAV') { + try { + $data = NGCP::Panel::Utils::Sounds::transcode_data( + $file->data, $file->codec, 'WAV'); + } catch($e) { + NGCP::Panel::Utils::Message::error( + c => $c, + error => $e, + desc => $c->loc('Failed to transcode audio file'), + ); + NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{handles_base_uri}); + } + } else { + $data = $file->data; + } + $c->response->header ('Content-Disposition' => 'attachment; filename="' . $filename . '"'); $c->response->content_type('audio/x-wav'); $c->response->body($data); @@ -685,7 +718,6 @@ sub handles_load_default :Chained('handles_list') :PathPart('loaddefault') :Args my ($self, $c) = @_; my $posted = ($c->request->method eq 'POST'); my $form = NGCP::Panel::Form::get("NGCP::Panel::Form::Sound::LoadDefault", $c); - my $error; $form->process( posted => $posted, params => $c->request->params, @@ -697,7 +729,7 @@ sub handles_load_default :Chained('handles_list') :PathPart('loaddefault') :Args fields => {}, back_uri => $c->req->uri, ); - + if($posted && $form->validated) { my $lang = $form->params->{language}; my $base = "/var/lib/ngcp-soundsets"; @@ -721,30 +753,29 @@ sub handles_load_default :Chained('handles_list') :PathPart('loaddefault') :Args } next unless(defined $path); - my $data_ref; - my $codec = 'WAV'; + my $handle_id = $h->get_column("handleid"); my $file_id = $h->get_column("fileid"); my $fres; my $fname = basename($path); - my $handle_id = $h->get_column("handleid"); - my $loopplay = $form->params->{loopplay} ? 1 : 0; - - read_file($path, buf_ref => \$data_ref); - - unless (${data_ref}) { - $error = "Cannot upload an empty sound file, $fname"; - die $error; - } - if(defined $file_id) { if($form->params->{override}) { $c->log->debug("override $path as $hname for existing id $file_id"); + my $data; + if(!$c->stash->{set_result}->contract_id && + grep {/^$hname$/} (qw/music_on_hold/)) { + + $fname =~ s/\.wav$/.pcma/; + $data = NGCP::Panel::Utils::Sounds::transcode_file( + $path, 'WAV', 'PCMA'); + } else { + $data = read_file($path); + } $fres = $schema->resultset('voip_sound_files')->find($file_id); $fres->update({ filename => $fname, - data => ${data_ref}, - loopplay => $loopplay, + data => $data, + loopplay => $form->params->{loopplay} ? 1 : 0, }); } else { $c->log->debug("skip $path as $hname exists via id $file_id and override is not set"); @@ -752,13 +783,26 @@ sub handles_load_default :Chained('handles_list') :PathPart('loaddefault') :Args } else { $c->log->debug("inserting $path as $hname with new id"); + my $codec = 'WAV'; + my $data; + if(!$c->stash->{set_result}->contract_id && + grep {/^$hname$/} (qw/music_on_hold/)) { + + $fname =~ s/\.wav$/.pcma/; + $codec = 'PCMA'; + $data = NGCP::Panel::Utils::Sounds::transcode_file( + $path, 'WAV', $codec); + } else { + $data = read_file($path); + } + $fres = $schema->resultset('voip_sound_files') ->create({ filename => $fname, - data => ${data_ref}, + data => $data, handle_id => $handle_id, set_id => $set_id, - loopplay => $loopplay, + loopplay => $form->params->{loopplay} ? 1 : 0, codec => $codec, }); } @@ -777,10 +821,8 @@ sub handles_load_default :Chained('handles_list') :PathPart('loaddefault') :Args } catch($e) { NGCP::Panel::Utils::Message::error( c => $c, - $error - ? (desc => $c->loc($error)) - : (error => $e, - desc => $c->loc('Failed to load default sound files.')), + error => $e, + desc => $c->loc('Failed to load default sound files.'), ); } NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{handles_base_uri}); diff --git a/lib/NGCP/Panel/Role/API/SoundFiles.pm b/lib/NGCP/Panel/Role/API/SoundFiles.pm index 2c4cad6fd0..337288bf86 100644 --- a/lib/NGCP/Panel/Role/API/SoundFiles.pm +++ b/lib/NGCP/Panel/Role/API/SoundFiles.pm @@ -191,8 +191,15 @@ sub update_item { last; } + if($resource->{handle} eq 'music_on_hold' && !$set->contract_id) { + $resource->{codec} = 'PCMA'; + $resource->{filename} =~ s/\.[^.]+$/.pcma/; + } else { + $resource->{codec} = 'WAV'; + } $resource->{data} = $recording; - + $resource = $self->transcode_data($c, 'WAV', $resource); + last unless($resource); delete $resource->{handle}; try {