* Improved export error handling, streamlined ExportFailed
[ardour.git] / libs / ardour / ardour / export_channel_configuration.h
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __ardour_export_channel_configuration_h__
22 #define __ardour_export_channel_configuration_h__
23
24 #include <set>
25 #include <list>
26
27 #include <glibmm/ustring.h>
28 #include <sigc++/signal.h>
29
30 #include <ardour/export_status.h>
31 #include <ardour/ardour.h>
32
33 #include <pbd/xml++.h>
34
35 using Glib::ustring;
36
37 namespace ARDOUR
38 {
39
40 class ExportHandler;
41 class AudioPort;
42 class ExportChannel;
43 class ExportFormatSpecification;
44 class ExportFilename;
45 class ExportProcessor;
46 class ExportTimespan;
47 class Session;
48
49 class ExportChannel : public std::set<AudioPort *>
50 {
51   public:
52         void add_port (AudioPort * port) { if (port) { insert (port); } }
53         void read_ports (float * data, nframes_t frames) const;
54 };
55
56 class ExportChannelConfiguration
57 {
58   private:
59         typedef boost::shared_ptr<ExportProcessor> ProcessorPtr;
60         typedef boost::shared_ptr<ExportTimespan> TimespanPtr;
61         typedef boost::shared_ptr<ExportFormatSpecification const> FormatPtr;
62         typedef boost::shared_ptr<ExportFilename> FilenamePtr;
63         
64         typedef std::pair<FormatPtr, FilenamePtr> FileConfig;
65         typedef std::list<FileConfig> FileConfigList;
66         
67         /// Struct for threading, acts like a pointer to a ExportChannelConfiguration
68         struct WriterThread {
69                 WriterThread (ExportChannelConfiguration & channel_config) :
70                   channel_config (channel_config), running (false) {}
71                 
72                 ExportChannelConfiguration * operator-> () { return &channel_config; }
73                 ExportChannelConfiguration & operator* () { return channel_config; }
74                 
75                 ExportChannelConfiguration & channel_config;
76                 
77                 pthread_t thread;
78                 bool      running;
79         };
80
81   private:
82         friend class ExportElementFactory;
83         ExportChannelConfiguration (Session & session);
84         
85   public:
86         XMLNode & get_state ();
87         int set_state (const XMLNode &);
88         
89         typedef boost::shared_ptr<ExportChannel const> ChannelPtr;
90         typedef std::list<ChannelPtr> ChannelList;
91         
92         ChannelList const & get_channels () { return channels; }
93         bool all_channels_have_ports ();
94         
95         ustring name () const { return _name; }
96         void set_name (ustring name) { _name = name; }
97         void set_split (bool value) { split = value; }
98         
99         bool get_split () { return split; }
100         uint32_t get_n_chans () { return channels.size(); }
101         
102         void register_channel (ChannelPtr channel) { channels.push_back (channel); }
103         void register_file_config (FormatPtr format, FilenamePtr filename) { file_configs.push_back (FileConfig (format, filename)); }
104         
105         void clear_channels () { channels.clear (); }
106         
107         /// Writes all files for this channel config @return true if a new thread was spawned
108         bool write_files (boost::shared_ptr<ExportProcessor> new_processor);
109         sigc::signal<void> FilesWritten;
110         
111         // Tells the handler the necessary information for it to handle tempfiles
112         void register_with_timespan (TimespanPtr timespan);
113         
114         void unregister_all ();
115         
116   private:
117
118         typedef boost::shared_ptr<ExportStatus> ExportStatusPtr;
119
120          Session & session;
121
122         // processor has to be prepared before doing this.
123         void write_file ();
124         
125         /// The actual write files, needed for threading
126         static void *  _write_files (void *arg);
127         WriterThread    writer_thread;
128         ProcessorPtr    processor;
129         ExportStatusPtr status;
130
131         bool            files_written;
132
133         TimespanPtr     timespan;
134         ChannelList     channels;
135         FileConfigList  file_configs;
136         
137         bool            split; // Split to mono files
138         ustring        _name;
139 };
140
141 } // namespace ARDOUR
142
143 #endif /* __ardour_export_channel_configuration_h__ */