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"
37 using namespace ARDOUR;
40 confirm_video_outfn (std::string outfn, std::string docroot)
42 /* replace docroot's '/' to G_DIR_SEPARATOR for the comparison */
45 const char ds = G_DIR_SEPARATOR;
46 while((found_here = docroot.find('/', look_here)) != string::npos) {
47 docroot.replace(found_here, 1, std::string(&ds, 1));
48 look_here = found_here + 1;
51 if (!docroot.empty() && docroot.compare(0, docroot.length(), outfn, 0, docroot.length())) {
52 ArdourDialog confirm (_("Destination is outside Video Server's docroot. "), true);
53 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?"));
54 confirm.get_vbox()->pack_start (m, true, true);
55 confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
56 confirm.add_button (_("Continue"), Gtk::RESPONSE_ACCEPT);
58 if (confirm.run() == RESPONSE_CANCEL) { return false; }
61 if (Glib::file_test(outfn, Glib::FILE_TEST_EXISTS)) {
62 ArdourDialog confirm (_("Confirm Overwrite"), true);
63 Label m (_("A file with the same name already exists. Do you want to overwrite it?"));
64 confirm.get_vbox()->pack_start (m, true, true);
65 confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
66 confirm.add_button (_("Overwrite"), Gtk::RESPONSE_ACCEPT);
68 if (confirm.run() == RESPONSE_CANCEL) { return false; }
71 std::string dir = Glib::path_get_dirname (outfn);
72 if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
73 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
80 video_dest_dir (const std::string sessiondir, const std::string docroot)
82 std::string dir = docroot;
83 if (dir.empty() || !dir.compare(0, dir.length(), sessiondir, 0, dir.length())) {
86 if ((dir.empty() || dir.at(dir.length()-1) != G_DIR_SEPARATOR)) { dir += G_DIR_SEPARATOR; }
88 if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
89 error << string_compose(_("Cannot create video folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
95 video_get_docroot (ARDOUR::RCConfiguration* config)
97 if (config->get_video_advanced_setup()) {
98 return config->get_video_server_docroot();
104 video_get_server_url (ARDOUR::RCConfiguration* config)
106 if (config->get_video_advanced_setup()) {
107 return config->get_video_server_url();
109 return X_("http://localhost:1554");
114 strip_file_extension (const std::string infile)
117 char *ext, *bn = strdup(infile.c_str());
118 if ((ext=strrchr(bn, '.'))) {
119 if (!strchr(ext, G_DIR_SEPARATOR)) {
123 rv = std::string(bn);
129 get_file_extension (const std::string infile)
132 char *ext, *bn = strdup(infile.c_str());
133 if ((ext=strrchr(bn, '.'))) {
134 if (!strchr(ext, G_DIR_SEPARATOR)) {
135 rv=std::string(ext+1);
143 video_dest_file (const std::string dir, const std::string infile)
145 return dir + "a3_" + strip_file_extension(Glib::path_get_basename(infile)) + ".avi";
149 video_map_path (std::string server_docroot, std::string filepath)
151 std::string rv = filepath;
153 /* replace all G_DIR_SEPARATOR with '/' */
154 size_t look_here = 0;
156 while((found_here = rv.find(G_DIR_SEPARATOR, look_here)) != string::npos) {
157 rv.replace(found_here, 1, "/");
158 look_here = found_here + 1;
162 if (server_docroot.length() > 0) {
163 if (rv.compare(0, server_docroot.length(), server_docroot) == 0 ) {
164 rv = rv.substr(server_docroot.length());
170 curl = curl_easy_init();
171 ue = curl_easy_escape(curl, rv.c_str(),rv.length());
173 rv = std::string(ue);
176 curl_easy_cleanup(curl);
182 ParseCSV (const std::string &csv, std::vector<std::vector<std::string> > &lines)
188 std::vector<std::string> line;
190 std::string::const_iterator aChar = csv.begin();
191 while (aChar != csv.end()) {
200 if (inQuote == true) {
203 line.push_back(field);
210 if (inQuote == true) {
213 if (newLine == false) {
214 line.push_back(field);
215 lines.push_back(line);
225 field.push_back(*aChar);
232 line.push_back(field);
235 lines.push_back(line);
240 std::string video_server_url,
241 std::string filepath,
242 double &video_file_fps,
243 long long int &video_duration,
244 double &video_start_offset,
245 double &video_aspect_ratio
250 snprintf(url, sizeof(url), "%s%sinfo/?file=%s&format=plain"
251 , video_server_url.c_str()
252 , (video_server_url.length()>0 && video_server_url.at(video_server_url.length()-1) == '/')?"":"/"
254 char *res = curl_http_get(url, NULL);
256 #ifndef COMPILER_MINGW
260 pch = strtok_r(res, "\n", &pst);
263 printf("VideoFileInfo [%i] -> '%s'\n", pid, pch);
268 if (version != 1) break;
270 video_file_fps = atof(pch);
273 video_duration = atoll(pch);
276 video_start_offset = atof(pch);
279 video_aspect_ratio = atof(pch);
284 pch = strtok_r(NULL,"\n", &pst);
297 video_draw_cross (Glib::RefPtr<Gdk::Pixbuf> img)
300 int rowstride = img->get_rowstride();
301 int n_channels = img->get_n_channels();
303 pixels = img->get_pixels();
306 int clip_width = img->get_width();
307 int clip_height = img->get_height();
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;
322 #include <curl/curl.h>
324 struct MemoryStruct {
330 WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
331 size_t realsize = size * nmemb;
332 struct MemoryStruct *mem = (struct MemoryStruct *)data;
334 mem->data = (char *)realloc(mem->data, mem->size + realsize + 1);
336 memcpy(&(mem->data[mem->size]), ptr, realsize);
337 mem->size += realsize;
338 mem->data[mem->size] = 0;
343 char *curl_http_get (const char *u, int *status) {
346 struct MemoryStruct chunk;
348 if (status) *status = 0;
349 //usleep(500000); return NULL; // TEST & DEBUG
350 if (strncmp("http://", u, 7)) return NULL;
355 curl = curl_easy_init();
356 if(!curl) return NULL;
357 curl_easy_setopt(curl, CURLOPT_URL, u);
359 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
360 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
361 curl_easy_setopt(curl, CURLOPT_USERAGENT, ARDOUR_USER_AGENT);
362 curl_easy_setopt(curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT);
363 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
364 #ifdef CURLERRORDEBUG
365 char curlerror[CURL_ERROR_SIZE] = "";
366 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlerror);
369 res = curl_easy_perform(curl);
370 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpstatus);
371 curl_easy_cleanup(curl);
372 if (status) *status = httpstatus;
374 #ifdef CURLERRORDEBUG
375 printf("curl_http_get() failed: %s\n", curlerror);
379 if (httpstatus != 200) {
386 } /* end extern "C" */