vtl: remove cruft
[ardour.git] / gtk2_ardour / video_image_frame.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 #include <sigc++/bind.h>
21 #include "ardour/tempo.h"
22
23 #include "ardour_ui.h"
24 #include "video_image_frame.h"
25 #include "public_editor.h"
26 #include "utils.h"
27 #include "canvas/group.h"
28 #include "utils_videotl.h"
29
30 #include <gtkmm2ext/utils.h>
31 #include <pthread.h>
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37
38 VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Group& parent, int w, int h, std::string vsurl, std::string vfn)
39         : editor (ed)
40         , _parent(&parent)
41         , clip_width(w)
42         , clip_height(h)
43         , video_server_url(vsurl)
44         , video_filename(vfn)
45 {
46         pthread_mutex_init(&request_lock, NULL);
47         pthread_mutex_init(&queue_lock, NULL);
48         queued_request=false;
49         video_frame_number = -1;
50         rightend = -1;
51         frame_position = 0;
52         thread_active=false;
53
54         unit_position = editor.sample_to_pixel (frame_position);
55         image = new ArdourCanvas::Image (_parent, Cairo::FORMAT_ARGB32, clip_width, clip_height);
56
57         img = image->get_image();
58         fill_frame(0, 0, 0);
59         draw_line();
60         draw_x();
61         image->put_image(img);
62
63         image->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
64 }
65
66 VideoImageFrame::~VideoImageFrame ()
67 {
68         if (thread_active) pthread_join(thread_id_tt, NULL);
69         delete image;
70         pthread_mutex_destroy(&request_lock);
71         pthread_mutex_destroy(&queue_lock);
72 }
73
74 void
75 VideoImageFrame::set_position (framepos_t frame)
76 {
77         double new_unit_position = editor.sample_to_pixel (frame);
78         image->move (ArdourCanvas::Duple (new_unit_position - unit_position, 0.0));
79         frame_position = frame;
80         unit_position = new_unit_position;
81 }
82
83 void
84 VideoImageFrame::reposition ()
85 {
86         set_position (frame_position);
87 }
88
89 void
90 VideoImageFrame::exposeimg () {
91         ImgChanged(); /* EMIT SIGNAL */
92 }
93
94 void
95 VideoImageFrame::set_videoframe (framepos_t videoframenumber, int re)
96 {
97         if (video_frame_number == videoframenumber && rightend == re) return;
98
99         video_frame_number = videoframenumber;
100         rightend = re;
101
102         img = image->get_image();
103         fill_frame(0, 0, 0);
104         draw_x();
105         draw_line();
106         cut_rightend();
107         image->put_image(img);
108         exposeimg();
109
110         /* request video-frame from decoder in background thread */
111         http_get(video_frame_number);
112 }
113
114 void
115 VideoImageFrame::draw_line ()
116 {
117         const int rowstride = img->stride;
118         const int clip_height = img->height;
119         uint8_t *pixels, *p;
120         pixels = img->data.get();
121
122         int y;
123         for (y = 0;y < clip_height; y++) {
124                 p = pixels + y * rowstride;
125                 p[0] = 255; p[1] = 255; p[2] = 255; p[3] = 255;
126         }
127 }
128
129 void
130 VideoImageFrame::fill_frame (const uint8_t r, const uint8_t g, const uint8_t b)
131 {
132         const int rowstride = img->stride;
133         const int clip_height = img->height;
134         const int clip_width = img->width;
135         uint8_t *pixels, *p;
136         pixels = img->data.get();
137
138         int x,y;
139         for (y = 0; y < clip_height; ++y) {
140                 for (x = 0; x < clip_width; ++x) {
141                         p = pixels + y * rowstride + x * 4;
142                         p[0] = b; p[1] = g; p[2] = r; p[3] = 255;
143                 }
144         }
145 }
146
147 void
148 VideoImageFrame::draw_x ()
149 {
150         int x,y;
151         const int rowstride = img->stride;
152         const int clip_width = img->width;
153         const int clip_height = img->height;
154         uint8_t *pixels, *p;
155         pixels = img->data.get();
156
157         for (x = 0;x < clip_width; x++) {
158                 y = clip_height * x / clip_width;
159                 p = pixels + y * rowstride + x * 4;
160                 p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
161                 p = pixels + y * rowstride + (clip_width-x-1) * 4;
162                 p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
163         }
164 }
165
166 void
167 VideoImageFrame::cut_rightend ()
168 {
169
170         if (rightend < 0 ) { return; }
171
172         const int rowstride = img->stride;
173         const int clip_height = img->height;
174         const int clip_width = img->width;
175         uint8_t *pixels, *p;
176         pixels = img->data.get();
177         if (rightend > clip_width) { return; }
178
179         int x,y;
180         for (y = 0;y < clip_height; ++y) {
181                 p = pixels + y * rowstride + rightend * 4;
182                 p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
183                 for (x=rightend+1; x < clip_width; ++x) {
184                         p = pixels + y * rowstride + x * 4;
185                         p[0] = 0; p[1] = 0; p[2] = 0; p[3] = 0;
186                 }
187         }
188 }
189
190 void *
191 http_get_thread (void *arg) {
192         VideoImageFrame *vif = static_cast<VideoImageFrame *>(arg);
193         char url[2048];
194         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
195         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
196         snprintf(url, sizeof(url), "%s?frame=%li&w=%d&h=%di&file=%s&format=bgra",
197           vif->get_video_server_url().c_str(),
198           (long int) vif->get_req_frame(), vif->get_width(), vif->get_height(),
199           vif->get_video_filename().c_str()
200         );
201         int status = 0;
202         int timeout = 1000; // * 5ms -> 5sec
203         char *res = NULL;
204         do {
205                 res=curl_http_get(url, &status);
206                 if (status == 503) usleep(5000); // try-again
207         } while (status == 503 && --timeout > 0);
208
209         if (status != 200 || !res) {
210                 printf("no-video frame: video-server returned http-status: %d\n", status);
211         }
212
213         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
214         vif->http_download_done(res);
215         pthread_exit(0);
216         return 0;
217 }
218
219 void
220 VideoImageFrame::http_download_done (char *data){
221         if (queued_request) {
222                 http_maybe_get_again();
223                 return;
224         }
225
226         if (!data) {
227                 /* Image request failed (HTTP error or timeout) */
228                 img = image->get_image();
229                 fill_frame(128, 0, 0);
230                 draw_x();
231                 cut_rightend();
232                 draw_line();
233                 cut_rightend();
234                 image->put_image(img);
235         } else {
236                 img = image->get_image();
237                 /* TODO - have curl write directly to the shared memory region */
238                 memcpy((void*) img->data.get(), data, img->stride * img->height);
239                 free(data);
240                 draw_line();
241                 cut_rightend();
242                 image->put_image(img);
243         }
244
245         exposeimg();
246         /* don't request frames rapidly, wait after user has zoomed */
247         usleep(20000);
248
249         if (queued_request) {
250                 http_maybe_get_again();
251         }
252         pthread_mutex_unlock(&request_lock);
253 }
254
255
256 void
257 VideoImageFrame::http_get(framepos_t fn) {
258         if (pthread_mutex_trylock(&request_lock)) {
259                 pthread_mutex_lock(&queue_lock);
260                 queued_request=true;
261                 want_video_frame_number=fn;
262                 pthread_mutex_unlock(&queue_lock);
263                 return;
264         }
265         if (thread_active) pthread_join(thread_id_tt, NULL);
266         pthread_mutex_lock(&queue_lock);
267         queued_request=false;
268         req_video_frame_number=fn;
269         pthread_mutex_unlock(&queue_lock);
270         int rv = pthread_create(&thread_id_tt, NULL, http_get_thread, this);
271         thread_active=true;
272         if (rv) {
273                 thread_active=false;
274                 printf("thread creation failed. %i\n",rv);
275                 http_download_done(NULL);
276         }
277 }
278
279 void
280 VideoImageFrame::http_maybe_get_again() {
281         pthread_mutex_lock(&queue_lock);
282         queued_request=false;
283         req_video_frame_number=want_video_frame_number;
284         pthread_mutex_unlock(&queue_lock);
285
286         http_get_thread(this);
287 }
288
289
290 extern "C" {
291 #include <curl/curl.h>
292
293         struct MemoryStruct {
294                 char *data;
295                 size_t size;
296         };
297
298         static size_t
299         WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
300                 size_t realsize = size * nmemb;
301                 struct MemoryStruct *mem = (struct MemoryStruct *)data;
302
303                 mem->data = (char *)realloc(mem->data, mem->size + realsize + 1);
304                 if (mem->data) {
305                         memcpy(&(mem->data[mem->size]), ptr, realsize);
306                         mem->size += realsize;
307                         mem->data[mem->size] = 0;
308                 }
309                 return realsize;
310         }
311
312         char *curl_http_get (const char *u, int *status) {
313                 CURL *curl;
314                 CURLcode res;
315                 struct MemoryStruct chunk;
316                 long int httpstatus;
317                 if (status) *status = 0;
318                 if (strncmp("http://", u, 7)) return NULL;
319
320                 chunk.data=NULL;
321                 chunk.size=0;
322
323                 curl = curl_easy_init();
324                 if(!curl) return NULL;
325                 curl_easy_setopt(curl, CURLOPT_URL, u);
326
327                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
328                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
329                 curl_easy_setopt(curl, CURLOPT_USERAGENT, ARDOUR_USER_AGENT);
330                 curl_easy_setopt(curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT);
331                 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
332 #ifdef CURLERRORDEBUG
333                 char curlerror[CURL_ERROR_SIZE] = "";
334                 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlerror);
335 #endif
336
337                 res = curl_easy_perform(curl);
338                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpstatus);
339                 curl_easy_cleanup(curl);
340                 if (status) *status = httpstatus;
341                 if (res) {
342 #ifdef CURLERRORDEBUG
343                         printf("curl_http_get() failed: %s\n", curlerror);
344 #endif
345                         return NULL;
346                 }
347                 if (httpstatus != 200) {
348                         free (chunk.data);
349                         chunk.data = NULL;
350                 }
351                 return (chunk.data);
352         }
353
354 } /* end extern "C" */