[Summary] Added export cleanup
authorGZharun <grygoriiz@wavesglobal.com>
Thu, 15 Jan 2015 11:47:06 +0000 (13:47 +0200)
committerPaul Davis <paul@linuxaudiosystems.com>
Mon, 29 Jun 2015 18:18:11 +0000 (14:18 -0400)
[Reviewed by] Andriy Mishyn

libs/ardour/ardour/export_graph_builder.h
libs/ardour/export_graph_builder.cc
libs/ardour/export_handler.cc

index 40960b2b389300f0a199a75dbee055202856337e..c266198679c1e9d56be70b52585b7ef73bfbfecf 100644 (file)
@@ -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<ExportTimespan> span);
        void add_config (FileSpec const & config);
 
   private:
 
        void add_split_config (FileSpec const & config);
-
+    
        class Encoder {
          public:
                template <typename T> boost::shared_ptr<AudioGrapher::Sink<T> > 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<ExportFilenamePtr> 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:
index 5e8c052b637dbaf804dae5e876de058c4d263a9d..a217db6a2c717e4d23e763c97652f8075fb91bb1 100644 (file)
@@ -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<ExportTimespan> 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_ptr<AudioGrapher::Sndfil
        unsigned channels = config.channel_config->get_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<T> (filename, format, channels, config.format->sample_rate(), config.broadcast_info));
+       writer.reset (new AudioGrapher::SndfileWriter<T> (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<Encoder>::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<SFC>::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<SFC>::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<Normalizer>::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<typename T>
 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<SRC>::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
@@ -551,6 +647,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<SilenceHandler>::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
index e755d9b70b2e772b2e6f26838fad5738eaae1363..37e5f11455b42076faee5835c6c56ad6fe53b518 100644 (file)
@@ -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 */