consolidate semaphore implementation (part one)
[ardour.git] / libs / ardour / ardour / export_handler.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_handler_h__
22 #define __ardour_export_handler_h__
23
24 #include <map>
25
26 #include <boost/operators.hpp>
27 #include <boost/shared_ptr.hpp>
28
29 #include "pbd/gstdio_compat.h"
30
31 #include "ardour/export_pointers.h"
32 #include "ardour/session.h"
33 #include "ardour/libardour_visibility.h"
34 #include "ardour/types.h"
35 #include "pbd/signals.h"
36
37 #include "i18n.h"
38
39 namespace AudioGrapher {
40         class BroadcastInfo;
41 }
42
43 namespace ARDOUR
44 {
45
46 class ExportTimespan;
47 class ExportChannelConfiguration;
48 class ExportFormatSpecification;
49 class ExportFilename;
50 class ExportGraphBuilder;
51 class Location;
52
53 class LIBARDOUR_API ExportElementFactory
54 {
55   public:
56
57         ExportElementFactory (Session & session);
58         ~ExportElementFactory ();
59
60         ExportTimespanPtr add_timespan ();
61
62         ExportChannelConfigPtr add_channel_config ();
63
64         ExportFormatSpecPtr    add_format ();
65         ExportFormatSpecPtr    add_format (XMLNode const & state);
66         ExportFormatSpecPtr    add_format_copy (ExportFormatSpecPtr other);
67
68         ExportFilenamePtr      add_filename ();
69         ExportFilenamePtr      add_filename_copy (ExportFilenamePtr other);
70
71   private:
72         Session & session;
73 };
74
75 class LIBARDOUR_API ExportHandler : public ExportElementFactory, public sigc::trackable
76 {
77   public:
78         struct FileSpec {
79                 FileSpec() {}
80                 FileSpec (ExportChannelConfigPtr channel_config, ExportFormatSpecPtr format,
81                           ExportFilenamePtr filename, BroadcastInfoPtr broadcast_info)
82                   : channel_config (channel_config)
83                   , format (format)
84                   , filename (filename)
85                   , broadcast_info (broadcast_info)
86                         {}
87
88                 ExportChannelConfigPtr channel_config;
89                 ExportFormatSpecPtr    format;
90                 ExportFilenamePtr      filename;
91                 BroadcastInfoPtr       broadcast_info;
92         };
93
94   private:
95         /* Session::get_export_handler() should be used to obtain an export handler
96          * This ensures that it doesn't go out of scope before finalize_audio_export is called
97          */
98
99         friend boost::shared_ptr<ExportHandler> Session::get_export_handler();
100         ExportHandler (Session & session);
101
102         void command_output(std::string output, size_t size);
103
104   public:
105         ~ExportHandler ();
106
107         bool add_export_config (ExportTimespanPtr timespan, ExportChannelConfigPtr channel_config,
108                                 ExportFormatSpecPtr format, ExportFilenamePtr filename,
109                                 BroadcastInfoPtr broadcast_info);
110         void do_export ();
111
112         std::string get_cd_marker_filename(std::string filename, CDMarkerFormat format);
113
114         /** signal emitted when soundcloud export reports progress updates during upload.
115          * The parameters are total and current bytes downloaded, and the current filename
116          */
117         PBD::Signal3<void, double, double, std::string> SoundcloudProgress;
118
119         /* upload credentials & preferences */
120         std::string soundcloud_username;
121         std::string soundcloud_password;
122         bool soundcloud_make_public;
123         bool soundcloud_open_page;
124         bool soundcloud_downloadable;
125
126   private:
127
128         void handle_duplicate_format_extensions();
129         int process (framecnt_t frames);
130
131         Session &          session;
132         boost::shared_ptr<ExportGraphBuilder> graph_builder;
133         ExportStatusPtr    export_status;
134
135         /* The timespan and corresponding file specifications that we are exporting;
136            there can be multiple FileSpecs for each ExportTimespan.
137         */
138         typedef std::multimap<ExportTimespanPtr, FileSpec> ConfigMap;
139         ConfigMap          config_map;
140
141         bool               normalizing;
142
143         /* Timespan management */
144
145         void start_timespan ();
146         int  process_timespan (framecnt_t frames);
147         int  process_normalize ();
148         void finish_timespan ();
149
150         typedef std::pair<ConfigMap::iterator, ConfigMap::iterator> TimespanBounds;
151         ExportTimespanPtr     current_timespan;
152         TimespanBounds        timespan_bounds;
153
154         PBD::ScopedConnection process_connection;
155         framepos_t             process_position;
156
157         /* CD Marker stuff */
158
159         struct CDMarkerStatus {
160                 CDMarkerStatus (std::string out_file, ExportTimespanPtr timespan,
161                                 ExportFormatSpecPtr format, std::string filename)
162                   : path (out_file)
163                   , timespan (timespan)
164                   , format (format)
165                   , filename (filename)
166                   , marker(0)
167                   , track_number (1)
168                   , track_position (0)
169                   , track_duration (0)
170                   , track_start_frame (0)
171                   , index_number (1)
172                   , index_position (0)
173                   {}
174
175                 ~CDMarkerStatus () {
176                         if (!g_file_set_contents (path.c_str(), out.str().c_str(), -1, NULL)) {
177                                 PBD::error << string_compose(_("Editor: cannot open \"%1\" as export file for CD marker file"), path) << endmsg;
178                         }
179
180                 }
181
182                 /* I/O */
183                 std::string         path;
184                 std::stringstream   out;
185
186                 /* General info */
187                 ExportTimespanPtr   timespan;
188                 ExportFormatSpecPtr format;
189                 std::string         filename;
190                 Location *          marker;
191
192                 /* Track info */
193                 uint32_t        track_number;
194                 framepos_t      track_position;
195                 framepos_t      track_duration;
196                 framepos_t      track_start_frame;
197
198                 /* Index info */
199                 uint32_t       index_number;
200                 framepos_t      index_position;
201         };
202
203
204         void export_cd_marker_file (ExportTimespanPtr timespan, ExportFormatSpecPtr file_format,
205                                     std::string filename, CDMarkerFormat format);
206
207         void write_cue_header (CDMarkerStatus & status);
208         void write_toc_header (CDMarkerStatus & status);
209         void write_mp4ch_header (CDMarkerStatus & status);
210
211         void write_track_info_cue (CDMarkerStatus & status);
212         void write_track_info_toc (CDMarkerStatus & status);
213         void write_track_info_mp4ch (CDMarkerStatus & status);
214
215         void write_index_info_cue (CDMarkerStatus & status);
216         void write_index_info_toc (CDMarkerStatus & status);
217         void write_index_info_mp4ch (CDMarkerStatus & status);
218
219         void frames_to_cd_frames_string (char* buf, framepos_t when);
220         void frames_to_chapter_marks_string (char* buf, framepos_t when);
221
222         std::string toc_escape_cdtext (const std::string&);
223         std::string toc_escape_filename (const std::string&);
224         std::string cue_escape_cdtext (const std::string& txt);
225
226         int cue_tracknum;
227         int cue_indexnum;
228 };
229
230 } // namespace ARDOUR
231
232 #endif /* __ardour_export_handler_h__ */