2 Copyright (C) 2010 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.
20 #ifdef WITH_VIDEOTIMELINE
22 #include <sigc++/bind.h>
23 #include "ardour/tempo.h"
25 #include "ardour_ui.h"
26 #include "video_image_frame.h"
27 #include "public_editor.h"
29 #include "canvas_impl.h"
30 #include "simpleline.h"
31 #include "rgb_macros.h"
32 #include "utils_videotl.h"
34 #include <gtkmm2ext/utils.h>
40 using namespace ARDOUR;
42 VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Group& parent, int w, int h, std::string vsurl, std::string vfn)
47 , video_server_url(vsurl)
50 pthread_mutex_init(&request_lock, NULL);
51 pthread_mutex_init(&queue_lock, NULL);
53 video_frame_number = -1;
59 printf("New VideoImageFrame (%ix%i) %s - %s\n", w, h, vsurl.c_str(), vfn.c_str());
62 unit_position = editor.frame_to_unit (frame_position);
63 group = new Group (parent, unit_position, 1.0);
64 img_pixbuf = new ArdourCanvas::Pixbuf(*group);
66 Glib::RefPtr<Gdk::Pixbuf> img;
68 img = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, clip_width, clip_height);
69 img->fill(RGBA_TO_UINT(0,0,0,255));
70 img_pixbuf->property_pixbuf() = img;
73 video_draw_cross(img_pixbuf->property_pixbuf());
75 group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
76 //img_pixbuf->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
79 VideoImageFrame::~VideoImageFrame ()
81 if (thread_active) pthread_join(thread_id_tt, NULL);
84 pthread_mutex_destroy(&request_lock);
85 pthread_mutex_destroy(&queue_lock);
89 VideoImageFrame::set_position (framepos_t frame)
91 double new_unit_position = editor.frame_to_unit (frame);
92 group->move (new_unit_position - unit_position, 0.0);
93 frame_position = frame;
94 unit_position = new_unit_position;
98 VideoImageFrame::reposition ()
100 set_position (frame_position);
104 VideoImageFrame::exposeimg ()
107 /* Note: we can not use this thread to update the window
108 * it needs to be done from the Editor's thread idle_update */
109 ImgChanged(); /* EMIT SIGNAL */
113 VideoImageFrame::set_videoframe (framepos_t videoframenumber, int re)
115 if (video_frame_number == videoframenumber && rightend == re) return;
117 video_frame_number = videoframenumber;
119 #if 0 /* dummy mode: print framenumber */
121 snprintf (buf, sizeof(buf), "%li", (long int) videoframenumber);
122 img_pixbuf->property_pixbuf() = pixbuf_from_ustring(g_strdup (buf), get_font_for_style (N_("MarkerText")), 80, 60, Gdk::Color ("#C0C0C0"));
125 #if 1 /* draw "empty frame" while we request the data */
126 Glib::RefPtr<Gdk::Pixbuf> img;
127 img = img_pixbuf->property_pixbuf();
128 img->fill(RGBA_TO_UINT(0,0,0,255));
129 video_draw_cross(img_pixbuf->property_pixbuf());
134 /* request video-frame from decoder in background thread */
135 http_get(video_frame_number);
139 VideoImageFrame::draw_line ()
141 Glib::RefPtr<Gdk::Pixbuf> img;
142 img = img_pixbuf->property_pixbuf();
144 int rowstride = img->get_rowstride();
145 int clip_height = img->get_height();
146 int n_channels = img->get_n_channels();
148 pixels = img->get_pixels();
151 for (y=0;y<clip_height;y++) {
152 p = pixels + y * rowstride;
153 p[0] = 255; p[1] = 255; p[2] = 255;
154 if (n_channels>3) p[3] = 255;
159 VideoImageFrame::cut_rightend ()
161 if (rightend < 0 ) { return; }
162 Glib::RefPtr<Gdk::Pixbuf> img;
163 img = img_pixbuf->property_pixbuf();
165 int rowstride = img->get_rowstride();
166 int clip_height = img->get_height();
167 int clip_width = img->get_width();
168 int n_channels = img->get_n_channels();
170 pixels = img->get_pixels();
171 if (rightend > clip_width) { return; }
174 for (y=0;y<clip_height;++y) {
175 p = pixels + y * rowstride + rightend * n_channels;
176 p[0] = 192; p[1] = 127; p[2] = 127;
177 if (n_channels>3) p[3] = 255;
178 for (x=rightend+1; x<clip_width; ++x) {
179 p = pixels + y * rowstride + x * n_channels;
180 p[0] = 0; p[1] = 0; p[2] = 0;
181 if (n_channels>3) p[3] = 0;
187 http_get_thread (void *arg) {
188 VideoImageFrame *vif = static_cast<VideoImageFrame *>(arg);
190 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
191 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
192 snprintf(url, sizeof(url), "%s?frame=%li&w=%d&h=%di&file=%s&format=rgb",
193 vif->get_video_server_url().c_str(),
194 (long int) vif->get_req_frame(), vif->get_width(), vif->get_height(),
195 vif->get_video_filename().c_str()
198 int timeout = 1000; // * 5ms -> 5sec
201 res=curl_http_get(url, &status);
202 if (status == 503) usleep(5000); // try-again
203 } while (status == 503 && --timeout > 0);
205 if (status != 200 || !res) {
206 printf("no-video frame: video-server returned http-status: %d\n", status);
209 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
210 vif->http_download_done(res);
216 VideoImageFrame::http_download_done (char *data){
217 if (queued_request) {
218 http_get_again(want_video_frame_number);
223 /* Image request failed (HTTP error or timeout) */
224 Glib::RefPtr<Gdk::Pixbuf> img;
225 img = img_pixbuf->property_pixbuf();
226 img->fill(RGBA_TO_UINT(128,0,0,255));
227 video_draw_cross(img_pixbuf->property_pixbuf());
231 /* TODO: mark as invalid:
232 * video_frame_number = -1;
233 * TODO: but prevent live-loops when calling update again
236 Glib::RefPtr<Gdk::Pixbuf> tmp, img;
238 tmp = Gdk::Pixbuf::create_from_data ((guint8*) data, Gdk::COLORSPACE_RGB, true, 8, clip_width, clip_height, clip_width*4);
240 tmp = Gdk::Pixbuf::create_from_data ((guint8*) data, Gdk::COLORSPACE_RGB, false, 8, clip_width, clip_height, clip_width*3);
242 img = img_pixbuf->property_pixbuf();
243 tmp->copy_area (0, 0, clip_width, clip_height, img, 0, 0);
250 /* don't request frames too quickly, wait after user has zoomed */
253 if (queued_request) {
254 http_get_again(want_video_frame_number);
256 pthread_mutex_unlock(&request_lock);
261 VideoImageFrame::http_get(framepos_t fn) {
262 if (pthread_mutex_trylock(&request_lock)) {
263 /* remember last request and schedule after the lock has been released. */
264 pthread_mutex_lock(&queue_lock);
266 want_video_frame_number=fn;
267 pthread_mutex_unlock(&queue_lock);
269 /* TODO: cancel request and start a new one
270 * but only if we're waiting for curl request.
271 * don't interrupt http_download_done()
273 * This should work, but requires testing:
275 if (!pthread_cancel(thread_id_tt)) {
276 pthread_mutex_unlock(&request_lock);
282 if (thread_active) pthread_join(thread_id_tt, NULL);
283 pthread_mutex_lock(&queue_lock);
284 queued_request=false;
285 req_video_frame_number=fn;
286 pthread_mutex_unlock(&queue_lock);
287 int rv = pthread_create(&thread_id_tt, NULL, http_get_thread, this);
291 printf("thread creation failed. %i\n",rv);
292 http_download_done(NULL);
297 VideoImageFrame::http_get_again(framepos_t fn) {
298 pthread_mutex_lock(&queue_lock);
299 queued_request=false;
300 req_video_frame_number=want_video_frame_number;
301 pthread_mutex_unlock(&queue_lock);
303 http_get_thread(this);
308 #include <curl/curl.h>
310 struct MemoryStruct {
316 WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
317 size_t realsize = size * nmemb;
318 struct MemoryStruct *mem = (struct MemoryStruct *)data;
320 mem->data = (char *)realloc(mem->data, mem->size + realsize + 1);
322 memcpy(&(mem->data[mem->size]), ptr, realsize);
323 mem->size += realsize;
324 mem->data[mem->size] = 0;
329 char *curl_http_get (const char *u, int *status) {
332 struct MemoryStruct chunk;
334 if (status) *status = 0;
335 //usleep(500000); return NULL; // TEST & DEBUG
336 if (strncmp("http://", u, 7)) return NULL;
341 curl = curl_easy_init();
342 if(!curl) return NULL;
343 curl_easy_setopt(curl, CURLOPT_URL, u);
345 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
346 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
347 curl_easy_setopt(curl, CURLOPT_USERAGENT, ARDOUR_USER_AGENT);
348 curl_easy_setopt(curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT);
349 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
350 #define CURLERRORDEBUG /* XXX */
351 #ifdef CURLERRORDEBUG
352 char curlerror[CURL_ERROR_SIZE] = "";
353 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlerror);
356 res = curl_easy_perform(curl);
357 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpstatus);
358 curl_easy_cleanup(curl);
359 if (status) *status = httpstatus;
361 #ifdef CURLERRORDEBUG
362 printf("curl_http_get() failed: %s\n", curlerror);
366 if (httpstatus != 200) {
373 } /* end extern "C" */
375 #endif /* WITH_VIDEOTIMELINE */