From 91dfada2d093c43bc7faa57a54120b013c2f9bcf Mon Sep 17 00:00:00 2001 From: GZharun Date: Thu, 15 Jan 2015 13:47:06 +0200 Subject: [PATCH] [Summary] Added export cleanup [Reviewed by] Andriy Mishyn --- libs/ardour/ardour/export_graph_builder.h | 13 ++- libs/ardour/export_graph_builder.cc | 115 +++++++++++++++++++++- libs/ardour/export_handler.cc | 2 +- 3 files changed, 125 insertions(+), 5 deletions(-) diff --git a/libs/ardour/ardour/export_graph_builder.h b/libs/ardour/ardour/export_graph_builder.h index 40960b2b38..c266198679 100644 --- a/libs/ardour/ardour/export_graph_builder.h +++ b/libs/ardour/ardour/export_graph_builder.h @@ -68,17 +68,20 @@ class LIBARDOUR_API ExportGraphBuilder unsigned get_normalize_cycle_count() const; void reset (); + void cleanup (bool remove_out_files = false); void set_current_timespan (boost::shared_ptr span); void add_config (FileSpec const & config); private: void add_split_config (FileSpec const & config); - + class Encoder { public: template boost::shared_ptr > init (FileSpec const & new_config); void add_child (FileSpec const & new_config); + void remove_children (); + void destroy_writer (bool delete_out_file); bool operator== (FileSpec const & other_config) const; static int get_real_format (FileSpec const & config); @@ -95,6 +98,8 @@ class LIBARDOUR_API ExportGraphBuilder std::list filenames; PBD::ScopedConnection copy_files_connection; + std::string writer_filename; + // Only one of these should be available at a time FloatWriterPtr float_writer; IntWriterPtr int_writer; @@ -108,6 +113,7 @@ class LIBARDOUR_API ExportGraphBuilder SFC (ExportGraphBuilder &, FileSpec const & new_config, framecnt_t max_frames); FloatSinkPtr sink (); void add_child (FileSpec const & new_config); + void remove_children (bool remove_out_files); bool operator== (FileSpec const & other_config) const; private: @@ -130,6 +136,7 @@ class LIBARDOUR_API ExportGraphBuilder Normalizer (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames); FloatSinkPtr sink (); void add_child (FileSpec const & new_config); + void remove_children (bool remove_out_files); bool operator== (FileSpec const & other_config) const; unsigned get_normalize_cycle_count() const; @@ -167,6 +174,8 @@ class LIBARDOUR_API ExportGraphBuilder SRC (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames); FloatSinkPtr sink (); void add_child (FileSpec const & new_config); + void remove_children (bool remove_out_files); + bool operator== (FileSpec const & other_config) const; private: @@ -189,6 +198,7 @@ class LIBARDOUR_API ExportGraphBuilder SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames); FloatSinkPtr sink (); void add_child (FileSpec const & new_config); + void remove_children (bool remove_out_files); bool operator== (FileSpec const & other_config) const; private: @@ -206,6 +216,7 @@ class LIBARDOUR_API ExportGraphBuilder public: ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map); void add_child (FileSpec const & new_config); + void remove_children (bool remove_out_files); bool operator== (FileSpec const & other_config) const; private: diff --git a/libs/ardour/export_graph_builder.cc b/libs/ardour/export_graph_builder.cc index 2c0c44033d..3eb623b352 100644 --- a/libs/ardour/export_graph_builder.cc +++ b/libs/ardour/export_graph_builder.cc @@ -112,6 +112,17 @@ ExportGraphBuilder::reset () normalizers.clear (); } +void +ExportGraphBuilder::cleanup (bool remove_out_files/*=false*/) +{ + ChannelConfigList::iterator iter = channel_configs.begin(); + + while (iter != channel_configs.end() ) { + iter->remove_children(remove_out_files); + iter = channel_configs.erase(iter); + } +} + void ExportGraphBuilder::set_current_timespan (boost::shared_ptr span) { @@ -175,7 +186,7 @@ ExportGraphBuilder::add_split_config (FileSpec const & config) // No duplicate channel config found, create new one channel_configs.push_back (new ChannelConfig (*this, config, channels)); } - + /* Encoder */ template <> @@ -210,6 +221,33 @@ ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config) { filenames.push_back (new_config.filename); } + +void +ExportGraphBuilder::Encoder::destroy_writer (bool delete_out_file) +{ + if (delete_out_file ) { + + if (float_writer) { + float_writer->close (); + } + + if (int_writer) { + int_writer->close (); + } + + if (short_writer) { + short_writer->close (); + } + + if (std::remove(writer_filename.c_str() ) != 0) { + std::cout << "Encoder::destroy_writer () : Error removing file: " << strerror(errno) << std::endl; + } + } + + float_writer.reset (); + int_writer.reset (); + short_writer.reset (); +} bool ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const @@ -231,9 +269,9 @@ ExportGraphBuilder::Encoder::init_writer (boost::shared_ptrget_n_chans(); int format = get_real_format (config); config.filename->set_channel_config(config.channel_config); - string filename = config.filename->get_path (config.format); + writer_filename = config.filename->get_path (config.format); - writer.reset (new AudioGrapher::SndfileWriter (filename, format, channels, config.format->sample_rate(), config.broadcast_info)); + writer.reset (new AudioGrapher::SndfileWriter (writer_filename, format, channels, config.format->sample_rate(), config.broadcast_info)); writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1)); } @@ -306,6 +344,20 @@ ExportGraphBuilder::SFC::add_child (FileSpec const & new_config) } } +void +ExportGraphBuilder::SFC::remove_children (bool remove_out_files) +{ + boost::ptr_list::iterator iter = children.begin (); + + while (iter != children.end() ) { + + if (remove_out_files) { + iter->destroy_writer(remove_out_files); + } + iter = children.erase (iter); + } +} + bool ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const { @@ -365,6 +417,17 @@ ExportGraphBuilder::Normalizer::add_child (FileSpec const & new_config) threader->add_output (children.back().sink()); } +void +ExportGraphBuilder::Normalizer::remove_children (bool remove_out_files) +{ + boost::ptr_list::iterator iter = children.begin (); + + while (iter != children.end() ) { + iter->remove_children (remove_out_files); + iter = children.erase (iter); + } +} + bool ExportGraphBuilder::Normalizer::operator== (FileSpec const & other_config) const { @@ -424,6 +487,27 @@ ExportGraphBuilder::SRC::add_child (FileSpec const & new_config) add_child_to_list (new_config, children); } } + +void +ExportGraphBuilder::SRC::remove_children (bool remove_out_files) +{ + boost::ptr_list::iterator sfc_iter = children.begin(); + + while (sfc_iter != children.end() ) { + converter->remove_output (sfc_iter->sink() ); + sfc_iter->remove_children (remove_out_files); + sfc_iter = children.erase (sfc_iter); + } + + boost::ptr_list::iterator norm_iter = normalized_children.begin(); + + while (norm_iter != normalized_children.end() ) { + converter->remove_output (norm_iter->sink() ); + norm_iter->remove_children (remove_out_files); + norm_iter = normalized_children.erase (norm_iter); + } + +} template void @@ -486,6 +570,18 @@ ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config) children.push_back (new SRC (parent, new_config, max_frames_in)); silence_trimmer->add_output (children.back().sink()); } + +void +ExportGraphBuilder::SilenceHandler::remove_children (bool remove_out_files) +{ + boost::ptr_list::iterator iter = children.begin(); + + while (iter != children.end() ) { + silence_trimmer->remove_output (iter->sink() ); + iter->remove_children (remove_out_files); + iter = children.erase (iter); + } +} bool ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const @@ -549,6 +645,19 @@ ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config) children.push_back (new SilenceHandler (parent, new_config, max_frames_out)); chunker->add_output (children.back().sink ()); } + +void +ExportGraphBuilder::ChannelConfig::remove_children (bool remove_out_files) +{ + boost::ptr_list::iterator iter = children.begin(); + + while(iter != children.end() ) { + + chunker->remove_output (iter->sink ()); + iter->remove_children (remove_out_files); + iter = children.erase(iter); + } +} bool ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const diff --git a/libs/ardour/export_handler.cc b/libs/ardour/export_handler.cc index 3c008fd16d..6b877efb1f 100644 --- a/libs/ardour/export_handler.cc +++ b/libs/ardour/export_handler.cc @@ -118,7 +118,7 @@ ExportHandler::ExportHandler (Session & session) ExportHandler::~ExportHandler () { - // TODO remove files that were written but not finished + graph_builder->cleanup (export_status->aborted () ); } /** Add an export to the `to-do' list */