Connect Script Manager "Edit" -> Lua Scripting Window
[ardour.git] / gtk2_ardour / video_image_frame.cc
index 9235e0d13b346a7bd979168d1aeb5e782bf9aacd..093f2351077ed32ef52222f5b28cc36020370fe0 100644 (file)
 #include <sigc++/bind.h>
 #include "ardour/tempo.h"
 
-#include "ardour_ui.h"
-#include "video_image_frame.h"
-#include "public_editor.h"
-#include "utils.h"
-#include "canvas/group.h"
-#include "utils_videotl.h"
-
 #include <gtkmm2ext/utils.h>
 #include <pthread.h>
 
-#include "i18n.h"
+#include "canvas/container.h"
+
+#include "ardour_http.h"
+#include "public_editor.h"
+#include "utils_videotl.h"
+#include "video_image_frame.h"
+
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
+using namespace VideoUtils;
 
-VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Group& parent, int w, int h, std::string vsurl, std::string vfn)
+static void freedata_cb (uint8_t *d, void* /*arg*/) {
+       /* later this can be used with libharvid
+        * the buffer/videocacheline instead of freeing it
+        */
+       free (d);
+}
+
+VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Container& parent, int w, int h, std::string vsurl, std::string vfn)
        : editor (ed)
        , _parent(&parent)
        , clip_width(w)
@@ -48,10 +56,10 @@ VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Group& parent,
        queued_request=false;
        video_frame_number = -1;
        rightend = -1;
-       frame_position = 0;
+       sample_position = 0;
        thread_active=false;
 
-       unit_position = editor.sample_to_pixel (frame_position);
+       unit_position = editor.sample_to_pixel (sample_position);
        image = new ArdourCanvas::Image (_parent, Cairo::FORMAT_ARGB32, clip_width, clip_height);
 
        img = image->get_image();
@@ -72,18 +80,18 @@ VideoImageFrame::~VideoImageFrame ()
 }
 
 void
-VideoImageFrame::set_position (framepos_t frame)
+VideoImageFrame::set_position (framepos_t sample)
 {
-       double new_unit_position = editor.sample_to_pixel (frame);
+       double new_unit_position = editor.sample_to_pixel (sample);
        image->move (ArdourCanvas::Duple (new_unit_position - unit_position, 0.0));
-       frame_position = frame;
+       sample_position = sample;
        unit_position = new_unit_position;
 }
 
 void
 VideoImageFrame::reposition ()
 {
-       set_position (frame_position);
+       set_position (sample_position);
 }
 
 void
@@ -117,7 +125,7 @@ VideoImageFrame::draw_line ()
        const int rowstride = img->stride;
        const int clip_height = img->height;
        uint8_t *pixels, *p;
-       pixels = img->data.get();
+       pixels = img->data;
 
        int y;
        for (y = 0;y < clip_height; y++) {
@@ -129,13 +137,11 @@ VideoImageFrame::draw_line ()
 void
 VideoImageFrame::fill_frame (const uint8_t r, const uint8_t g, const uint8_t b)
 {
-       memset((void*) img->data.get(), 0, img->stride * img->height); /// fill
-
        const int rowstride = img->stride;
        const int clip_height = img->height;
        const int clip_width = img->width;
        uint8_t *pixels, *p;
-       pixels = img->data.get();
+       pixels = img->data;
 
        int x,y;
        for (y = 0; y < clip_height; ++y) {
@@ -154,7 +160,7 @@ VideoImageFrame::draw_x ()
        const int clip_width = img->width;
        const int clip_height = img->height;
        uint8_t *pixels, *p;
-       pixels = img->data.get();
+       pixels = img->data;
 
        for (x = 0;x < clip_width; x++) {
                y = clip_height * x / clip_width;
@@ -175,7 +181,7 @@ VideoImageFrame::cut_rightend ()
        const int clip_height = img->height;
        const int clip_width = img->width;
        uint8_t *pixels, *p;
-       pixels = img->data.get();
+       pixels = img->data;
        if (rightend > clip_width) { return; }
 
        int x,y;
@@ -189,13 +195,13 @@ VideoImageFrame::cut_rightend ()
        }
 }
 
-void *
+static void *
 http_get_thread (void *arg) {
        VideoImageFrame *vif = static_cast<VideoImageFrame *>(arg);
        char url[2048];
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
-       snprintf(url, sizeof(url), "%s?frame=%li&w=%d&h=%di&file=%s&format=bgra",
+       snprintf(url, sizeof(url), "%s?frame=%li&w=%d&h=%d&file=%s&format=bgra",
          vif->get_video_server_url().c_str(),
          (long int) vif->get_req_frame(), vif->get_width(), vif->get_height(),
          vif->get_video_filename().c_str()
@@ -204,8 +210,8 @@ http_get_thread (void *arg) {
        int timeout = 1000; // * 5ms -> 5sec
        char *res = NULL;
        do {
-               res=curl_http_get(url, &status);
-               if (status == 503) usleep(5000); // try-again
+               res = ArdourCurl::http_get (url, &status);
+               if (status == 503) Glib::usleep(5000); // try-again
        } while (status == 503 && --timeout > 0);
 
        if (status != 200 || !res) {
@@ -221,7 +227,7 @@ http_get_thread (void *arg) {
 void
 VideoImageFrame::http_download_done (char *data){
        if (queued_request) {
-               http_maybe_get_again();
+               http_get_again(want_video_frame_number);
                return;
        }
 
@@ -235,21 +241,20 @@ VideoImageFrame::http_download_done (char *data){
                cut_rightend();
                image->put_image(img);
        } else {
-               img = image->get_image();
-               /* TODO - have curl write directly to the shared memory region */
-               memcpy((void*) img->data.get(), data, img->stride * img->height);
-               free(data);
+               img = image->get_image(false);
+               img->data = (uint8_t*) data;
+               img->destroy_callback = &freedata_cb;
                draw_line();
                cut_rightend();
                image->put_image(img);
        }
 
        exposeimg();
-       /* don't request frames rapidly, wait after user has zoomed */
-       usleep(20000);
+       /* don't request frames too quickly, wait after user has zoomed */
+       Glib::usleep(40000);
 
        if (queued_request) {
-               http_maybe_get_again();
+               http_get_again(want_video_frame_number);
        }
        pthread_mutex_unlock(&request_lock);
 }
@@ -279,7 +284,7 @@ VideoImageFrame::http_get(framepos_t fn) {
 }
 
 void
-VideoImageFrame::http_maybe_get_again() {
+VideoImageFrame::http_get_again(framepos_t /*fn*/) {
        pthread_mutex_lock(&queue_lock);
        queued_request=false;
        req_video_frame_number=want_video_frame_number;
@@ -288,69 +293,3 @@ VideoImageFrame::http_maybe_get_again() {
        http_get_thread(this);
 }
 
-
-extern "C" {
-#include <curl/curl.h>
-
-       struct MemoryStruct {
-               char *data;
-               size_t size;
-       };
-
-       static size_t
-       WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
-               size_t realsize = size * nmemb;
-               struct MemoryStruct *mem = (struct MemoryStruct *)data;
-
-               mem->data = (char *)realloc(mem->data, mem->size + realsize + 1);
-               if (mem->data) {
-                       memcpy(&(mem->data[mem->size]), ptr, realsize);
-                       mem->size += realsize;
-                       mem->data[mem->size] = 0;
-               }
-               return realsize;
-       }
-
-       char *curl_http_get (const char *u, int *status) {
-               CURL *curl;
-               CURLcode res;
-               struct MemoryStruct chunk;
-               long int httpstatus;
-               if (status) *status = 0;
-               if (strncmp("http://", u, 7)) return NULL;
-
-               chunk.data=NULL;
-               chunk.size=0;
-
-               curl = curl_easy_init();
-               if(!curl) return NULL;
-               curl_easy_setopt(curl, CURLOPT_URL, u);
-
-               curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
-               curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
-               curl_easy_setopt(curl, CURLOPT_USERAGENT, ARDOUR_USER_AGENT);
-               curl_easy_setopt(curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT);
-               curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-#ifdef CURLERRORDEBUG
-               char curlerror[CURL_ERROR_SIZE] = "";
-               curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlerror);
-#endif
-
-               res = curl_easy_perform(curl);
-               curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpstatus);
-               curl_easy_cleanup(curl);
-               if (status) *status = httpstatus;
-               if (res) {
-#ifdef CURLERRORDEBUG
-                       printf("curl_http_get() failed: %s\n", curlerror);
-#endif
-                       return NULL;
-               }
-               if (httpstatus != 200) {
-                       free (chunk.data);
-                       chunk.data = NULL;
-               }
-               return (chunk.data);
-       }
-
-} /* end extern "C" */