Merge branch 'nsm' of https://github.com/royvegard/ardour
[ardour.git] / gtk2_ardour / utils_videotl.cc
1 /*
2     Copyright (C) 2010-2013 Paul Davis
3     Author: Robin Gareus <robin@gareus.org>
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 #ifdef WITH_VIDEOTIMELINE
21
22 #include <cstdio>
23 #include <string>
24 #include <cerrno>
25 #include <gtkmm.h>
26 #include <curl/curl.h>
27
28 #include "pbd/error.h"
29 #include "ardour/ardour.h"
30 #include "ardour/session_directory.h"
31 #include "video_image_frame.h"
32 #include "utils_videotl.h"
33
34 #include "i18n.h"
35
36 using namespace Gtk;
37 using namespace std;
38 using namespace PBD;
39 using namespace ARDOUR;
40
41 bool
42 confirm_video_outfn (std::string outfn, std::string docroot)
43 {
44         /* replace docroot's '/' to G_DIR_SEPARATOR for the comparison */
45         size_t look_here = 0;
46         size_t found_here;
47         const char ds = G_DIR_SEPARATOR;
48         while((found_here = docroot.find('/', look_here)) != string::npos) {
49                 docroot.replace(found_here, 1, std::string(&ds, 1));
50                 look_here = found_here + 1;
51         }
52
53         if (!docroot.empty() && docroot.compare(0, docroot.length(), outfn, 0, docroot.length())) {
54                 ArdourDialog confirm (_("Destination is outside Video Server's docroot. "), true);
55                 Label m (_("The destination file path is outside of the Video Server's docroot. The file will not be readable by the Video Server. Do you still want to continue?"));
56                 confirm.get_vbox()->pack_start (m, true, true);
57                 confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
58                 confirm.add_button (_("Continue"), Gtk::RESPONSE_ACCEPT);
59                 confirm.show_all ();
60                 if (confirm.run() == RESPONSE_CANCEL) { return false; }
61         }
62
63         if (Glib::file_test(outfn, Glib::FILE_TEST_EXISTS)) {
64                 ArdourDialog confirm (_("Confirm Overwrite"), true);
65                 Label m (_("A file with the same name already exists.  Do you want to overwrite it?"));
66                 confirm.get_vbox()->pack_start (m, true, true);
67                 confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
68                 confirm.add_button (_("Overwrite"), Gtk::RESPONSE_ACCEPT);
69                 confirm.show_all ();
70                 if (confirm.run() == RESPONSE_CANCEL) { return false; }
71         }
72
73         std::string dir = Glib::path_get_dirname (outfn);
74         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
75                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
76                 return false;
77         }
78         return true;
79 }
80
81 std::string
82 video_dest_dir (const std::string sessiondir, const std::string docroot)
83 {
84         std::string dir = docroot;
85         if (dir.empty() || !dir.compare(0, dir.length(), sessiondir, 0, dir.length())) {
86                 dir=sessiondir;
87         }
88         if ((dir.empty() || dir.at(dir.length()-1) != G_DIR_SEPARATOR)) { dir += G_DIR_SEPARATOR; }
89
90         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
91                 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
92         }
93         return dir;
94 }
95
96 std::string
97 video_get_docroot (ARDOUR::RCConfiguration* config)
98 {
99         if (config->get_video_advanced_setup()) {
100                 return config->get_video_server_docroot();
101         }
102         return X_("/");
103 }
104
105 std::string
106 video_get_server_url (ARDOUR::RCConfiguration* config)
107 {
108         if (config->get_video_advanced_setup()) {
109                 return config->get_video_server_url();
110         }
111         return X_("http://localhost:1554");
112 }
113
114
115 std::string
116 strip_file_extension (const std::string infile)
117 {
118         std::string rv;
119         char *ext, *bn = strdup(infile.c_str());
120         if ((ext=strrchr(bn, '.'))) {
121                 if (!strchr(ext, G_DIR_SEPARATOR)) {
122                         *ext = 0;
123                 }
124         }
125         rv = std::string(bn);
126         free(bn);
127         return rv;
128 }
129
130 std::string
131 get_file_extension (const std::string infile)
132 {
133         std::string rv = "";
134         char *ext, *bn = strdup(infile.c_str());
135         if ((ext=strrchr(bn, '.'))) {
136                 if (!strchr(ext, G_DIR_SEPARATOR)) {
137                         rv=std::string(ext+1);
138                 }
139         }
140         free(bn);
141         return rv;
142 }
143
144 std::string
145 video_dest_file (const std::string dir, const std::string infile)
146 {
147         return dir + "a3_" + strip_file_extension(Glib::path_get_basename(infile)) + ".avi";
148 }
149
150 std::string
151 video_map_path (std::string server_docroot, std::string filepath)
152 {
153         std::string rv = filepath;
154
155         /* replace all G_DIR_SEPARATOR with '/' */
156         size_t look_here = 0;
157         size_t found_here;
158         while((found_here = rv.find(G_DIR_SEPARATOR, look_here)) != string::npos) {
159                 rv.replace(found_here, 1, "/");
160                 look_here = found_here + 1;
161         }
162
163         /* strip docroot */
164         if (server_docroot.length() > 0) {
165                 if (rv.compare(0, server_docroot.length(), server_docroot) == 0 ) {
166                         rv = rv.substr(server_docroot.length());
167                 }
168         }
169
170         CURL *curl;
171         char *ue;
172         curl = curl_easy_init();
173         ue = curl_easy_escape(curl, rv.c_str(),rv.length());
174         if (ue) {
175                 rv = std::string(ue);
176                 curl_free(ue);
177         }
178         curl_easy_cleanup(curl);
179
180         return rv;
181 }
182
183 void
184 ParseCSV (const std::string &csv, std::vector<std::vector<std::string> > &lines)
185 {
186         bool inQuote(false);
187         bool newLine(false);
188         std::string field;
189         lines.clear();
190         std::vector<std::string> line;
191
192         std::string::const_iterator aChar = csv.begin();
193         while (aChar != csv.end()) {
194                 switch (*aChar) {
195                 case '"':
196                  newLine = false;
197                  inQuote = !inQuote;
198                  break;
199
200                 case ',':
201                  newLine = false;
202                  if (inQuote == true) {
203                                 field += *aChar;
204                  } else {
205                                 line.push_back(field);
206                                 field.clear();
207                  }
208                  break;
209
210                 case '\n':
211                 case '\r':
212                  if (inQuote == true) {
213                                 field += *aChar;
214                  } else {
215                                 if (newLine == false) {
216                                          line.push_back(field);
217                                          lines.push_back(line);
218                                          field.clear();
219                                          line.clear();
220                                          newLine = true;
221                                 }
222                  }
223                  break;
224
225                 default:
226                          newLine = false;
227                          field.push_back(*aChar);
228                          break;
229                 }
230                 aChar++;
231         }
232
233          if (field.size())
234                 line.push_back(field);
235
236          if (line.size())
237                 lines.push_back(line);
238 }
239
240 bool
241 video_query_info (
242                 std::string video_server_url,
243                 std::string filepath,
244                 double &video_file_fps,
245                 long long int &video_duration,
246                 double &video_start_offset,
247                 double &video_aspect_ratio
248                 )
249 {
250         char url[2048];
251
252         snprintf(url, sizeof(url), "%s%sinfo/?file=%s&format=plain"
253                         , video_server_url.c_str()
254                         , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
255                         , filepath.c_str());
256         char *res = curl_http_get(url, NULL);
257         int pid=0;
258         if (res) {
259                 char *pch, *pst;
260                 int version;
261                 pch = strtok_r(res, "\n", &pst);
262                 while (pch) {
263 #if 0 /* DEBUG */
264                         printf("VideoFileInfo [%i] -> '%s'\n", pid, pch);
265 #endif
266                         switch (pid) {
267                                 case 0:
268                                   version = atoi(pch);
269                                         if (version != 1) break;
270                                 case 1:
271                                   video_file_fps = atof(pch);
272                                         break;
273                                 case 2:
274                                         video_duration = atoll(pch);
275                                         break;
276                                 case 3:
277                                         video_start_offset = atof(pch);
278                                         break;
279                                 case 4:
280                                         video_aspect_ratio = atof(pch);
281                                         break;
282                                 default:
283                                         break;
284                         }
285                         pch = strtok_r(NULL,"\n", &pst);
286                         ++pid;
287                 }
288           free(res);
289         }
290         if (pid!=5) {
291                 return false;
292         }
293         return true;
294 }
295
296 void
297 video_draw_cross (Glib::RefPtr<Gdk::Pixbuf> img)
298 {
299
300         int rowstride = img->get_rowstride();
301         int n_channels = img->get_n_channels();
302         guchar *pixels, *p;
303         pixels = img->get_pixels();
304
305         int x,y;
306         int clip_width = img->get_width();
307         int clip_height = img->get_height();
308
309         for (x=0;x<clip_width;x++) {
310                 y = clip_height * x / clip_width;
311                 p = pixels + y * rowstride + x * n_channels;
312                 p[0] = 192; p[1] = 192; p[2] = 192;
313                 if (n_channels>3) p[3] = 255;
314                 p = pixels + y * rowstride + (clip_width-x-1) * n_channels;
315                 p[0] = 192; p[1] = 192; p[2] = 192;
316                 if (n_channels>3) p[3] = 255;
317         }
318 }
319
320 #endif /* WITH_VIDEOTIMELINE */