2 Copyright (C) 2010-2013 Paul Davis
3 Author: Robin Gareus <robin@gareus.org>
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.
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.
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.
24 #include <curl/curl.h>
26 #include "pbd/error.h"
27 #include "ardour/ardour.h"
28 #include "ardour/session_directory.h"
29 #include "video_image_frame.h"
30 #include "utils_videotl.h"
34 #include "gtk2ardour-version.h"
37 #ifndef ARDOUR_CURL_TIMEOUT
38 #define ARDOUR_CURL_TIMEOUT (60)
45 using namespace ARDOUR;
46 using namespace VideoUtils;
49 VideoUtils::confirm_video_outfn (Gtk::Window& parent, std::string outfn, std::string docroot)
51 /* replace docroot's '/' to G_DIR_SEPARATOR for the comparison */
54 const char ds = G_DIR_SEPARATOR;
55 while((found_here = docroot.find('/', look_here)) != string::npos) {
56 docroot.replace(found_here, 1, std::string(&ds, 1));
57 look_here = found_here + 1;
60 if (!docroot.empty() && docroot.compare(0, docroot.length(), outfn, 0, docroot.length())) {
61 ArdourDialog confirm (_("Destination is outside Video Server's docroot. "), true);
62 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?"));
63 confirm.get_vbox()->pack_start (m, true, true);
64 confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
65 confirm.add_button (_("Continue"), Gtk::RESPONSE_ACCEPT);
67 if (confirm.run() == RESPONSE_CANCEL) { return false; }
70 if (Glib::file_test(outfn, Glib::FILE_TEST_EXISTS)) {
71 bool overwrite = ARDOUR_UI_UTILS::overwrite_file_dialog (parent,
72 _("Confirm Overwrite"),
73 _("A file with the same name already exists. Do you want to overwrite it?"));
80 std::string dir = Glib::path_get_dirname (outfn);
81 if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
82 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
89 VideoUtils::video_dest_dir (const std::string sessiondir, const std::string docroot)
91 std::string dir = docroot;
92 if (dir.empty() || !dir.compare(0, dir.length(), sessiondir, 0, dir.length())) {
95 if ((dir.empty() || dir.at(dir.length()-1) != G_DIR_SEPARATOR)) { dir += G_DIR_SEPARATOR; }
97 if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
98 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
104 VideoUtils::video_get_docroot (ARDOUR::RCConfiguration* config)
106 if (config->get_video_advanced_setup()) {
107 return config->get_video_server_docroot();
109 #ifndef PLATFORM_WINDOWS
117 VideoUtils::video_get_server_url (ARDOUR::RCConfiguration* config)
119 if (config->get_video_advanced_setup()) {
120 return config->get_video_server_url();
122 return X_("http://127.0.0.1:1554");
127 VideoUtils::strip_file_extension (const std::string infile)
130 char *ext, *bn = strdup(infile.c_str());
131 if ((ext=strrchr(bn, '.'))) {
132 if (!strchr(ext, G_DIR_SEPARATOR)) {
136 rv = std::string(bn);
142 VideoUtils::get_file_extension (const std::string infile)
145 char *ext, *bn = strdup(infile.c_str());
146 if ((ext=strrchr(bn, '.'))) {
147 if (!strchr(ext, G_DIR_SEPARATOR)) {
148 rv=std::string(ext+1);
156 VideoUtils::video_dest_file (const std::string dir, const std::string infile)
158 return Glib::build_filename(dir, strip_file_extension(Glib::path_get_basename(infile)) + ".avi");
162 VideoUtils::video_map_path (std::string server_docroot, std::string filepath)
164 std::string rv = filepath;
167 if (server_docroot.length() > 0) {
168 if (rv.compare(0, server_docroot.length(), server_docroot) == 0 ) {
169 rv = rv.substr(server_docroot.length());
173 /* replace all G_DIR_SEPARATOR with '/' */
174 size_t look_here = 0;
176 while((found_here = rv.find(G_DIR_SEPARATOR, look_here)) != string::npos) {
177 rv.replace(found_here, 1, "/");
178 look_here = found_here + 1;
183 curl = curl_easy_init();
184 ue = curl_easy_escape(curl, rv.c_str(),rv.length());
186 rv = std::string(ue);
189 curl_easy_cleanup(curl);
195 VideoUtils::ParseCSV (const std::string &csv, std::vector<std::vector<std::string> > &lines)
201 std::vector<std::string> line;
203 std::string::const_iterator aChar = csv.begin();
204 while (aChar != csv.end()) {
213 if (inQuote == true) {
216 line.push_back(field);
223 if (inQuote == true) {
226 if (newLine == false) {
227 line.push_back(field);
228 lines.push_back(line);
238 field.push_back(*aChar);
245 line.push_back(field);
248 lines.push_back(line);
252 VideoUtils::video_query_info (
253 std::string video_server_url,
254 std::string filepath,
255 double &video_file_fps,
256 long long int &video_duration,
257 double &video_start_offset,
258 double &video_aspect_ratio
263 snprintf(url, sizeof(url), "%s%sinfo/?file=%s&format=csv"
264 , video_server_url.c_str()
265 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
267 char *res = a3_curl_http_get(url, NULL);
272 std::vector<std::vector<std::string> > lines;
273 ParseCSV(std::string(res), lines);
276 if (lines.empty() || lines.at(0).empty() || lines.at(0).size() != 6) {
279 if (atoi(lines.at(0).at(0)) != 1) return false; // version
280 video_start_offset = 0.0;
281 video_aspect_ratio = atof (lines.at(0).at(3));
282 video_file_fps = atof (lines.at(0).at(4));
283 video_duration = atoll(lines.at(0).at(5));
288 VideoUtils::video_draw_cross (Glib::RefPtr<Gdk::Pixbuf> img)
291 int rowstride = img->get_rowstride();
292 int n_channels = img->get_n_channels();
294 pixels = img->get_pixels();
297 int clip_width = img->get_width();
298 int clip_height = img->get_height();
300 for (x=0;x<clip_width;x++) {
301 y = clip_height * x / clip_width;
302 p = pixels + y * rowstride + x * n_channels;
303 p[0] = 192; p[1] = 192; p[2] = 192;
304 if (n_channels>3) p[3] = 255;
305 p = pixels + y * rowstride + (clip_width-x-1) * n_channels;
306 p[0] = 192; p[1] = 192; p[2] = 192;
307 if (n_channels>3) p[3] = 255;
313 #include <curl/curl.h>
315 struct A3MemoryStruct {
321 WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
322 size_t realsize = size * nmemb;
323 struct A3MemoryStruct *mem = (struct A3MemoryStruct *)data;
325 mem->data = (char *)realloc(mem->data, mem->size + realsize + 1);
327 memcpy(&(mem->data[mem->size]), ptr, realsize);
328 mem->size += realsize;
329 mem->data[mem->size] = 0;
334 char *a3_curl_http_get (const char *u, int *status) {
337 struct A3MemoryStruct chunk;
339 if (status) *status = 0;
340 //Glib::usleep(500000); return NULL; // TEST & DEBUG
341 if (strncmp("http://", u, 7)) return NULL;
346 curl = curl_easy_init();
347 if(!curl) return NULL;
348 curl_easy_setopt(curl, CURLOPT_URL, u);
350 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
351 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
352 curl_easy_setopt(curl, CURLOPT_USERAGENT, PROGRAM_NAME VERSIONSTRING);
353 curl_easy_setopt(curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT);
354 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
355 #ifdef CURLERRORDEBUG
356 char curlerror[CURL_ERROR_SIZE] = "";
357 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlerror);
360 res = curl_easy_perform(curl);
361 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpstatus);
362 curl_easy_cleanup(curl);
363 if (status) *status = httpstatus;
365 #ifdef CURLERRORDEBUG
366 printf("a3_curl_http_get() failed: %s\n", curlerror);
370 if (httpstatus != 200) {
377 } /* end extern "C" */