Merge branch 'master' into windows
[ardour.git] / gtk2_ardour / video_image_frame.cc
1 /*
2     Copyright (C) 2010 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_impl.h"
28 #include "simpleline.h"
29 #include "rgb_macros.h"
30 #include "utils_videotl.h"
31
32 #include <gtkmm2ext/utils.h>
33 #include <pthread.h>
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace VideoUtils;
40
41 VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Group& parent, int w, int h, std::string vsurl, std::string vfn)
42         : editor (ed)
43         , _parent(&parent)
44         , clip_width(w)
45         , clip_height(h)
46         , video_server_url(vsurl)
47         , video_filename(vfn)
48 {
49         pthread_mutex_init(&request_lock, NULL);
50         pthread_mutex_init(&queue_lock, NULL);
51         queued_request=false;
52         video_frame_number = -1;
53         rightend = -1;
54         frame_position = 0;
55         thread_active=false;
56
57 #if 0 /* DEBUG */
58         printf("New VideoImageFrame (%ix%i) %s - %s\n", w, h, vsurl.c_str(), vfn.c_str());
59 #endif
60
61         unit_position = editor.frame_to_unit (frame_position);
62         group = new Group (parent, unit_position, 1.0);
63         img_pixbuf = new ArdourCanvas::Pixbuf(*group);
64
65         Glib::RefPtr<Gdk::Pixbuf> img;
66
67         img = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, clip_width, clip_height);
68         img->fill(RGBA_TO_UINT(0,0,0,255));
69         img_pixbuf->property_pixbuf() = img;
70
71         draw_line();
72         video_draw_cross(img_pixbuf->property_pixbuf());
73
74         group->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
75         //img_pixbuf->signal_event().connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
76 }
77
78 VideoImageFrame::~VideoImageFrame ()
79 {
80         if (thread_active) pthread_join(thread_id_tt, NULL);
81         delete img_pixbuf;
82         delete group;
83         pthread_mutex_destroy(&request_lock);
84         pthread_mutex_destroy(&queue_lock);
85 }
86
87 void
88 VideoImageFrame::set_position (framepos_t frame)
89 {
90         double new_unit_position = editor.frame_to_unit (frame);
91         group->move (new_unit_position - unit_position, 0.0);
92         frame_position = frame;
93         unit_position = new_unit_position;
94 }
95
96 void
97 VideoImageFrame::reposition ()
98 {
99         set_position (frame_position);
100 }
101
102 void
103 VideoImageFrame::exposeimg ()
104 {
105         img_pixbuf->show();
106         /* Note: we can not use this thread to update the window
107          * it needs to be done from the Editor's thread idle_update */
108         ImgChanged(); /* EMIT SIGNAL */
109 }
110
111 void
112 VideoImageFrame::set_videoframe (framepos_t videoframenumber, int re)
113 {
114         if (video_frame_number == videoframenumber && rightend == re) return;
115
116         video_frame_number = videoframenumber;
117         rightend = re;
118 #if 0 /* dummy mode: print framenumber */
119         gchar buf[16];
120         snprintf (buf, sizeof(buf), "%li", (long int) videoframenumber);
121         img_pixbuf->property_pixbuf() = pixbuf_from_ustring(g_strdup (buf), get_font_for_style (N_("MarkerText")), 80, 60, Gdk::Color ("#C0C0C0"));
122         return;
123 #endif
124 #if 1 /* draw "empty frame" while we request the data */
125         Glib::RefPtr<Gdk::Pixbuf> img;
126         img = img_pixbuf->property_pixbuf();
127         img->fill(RGBA_TO_UINT(0,0,0,255));
128         video_draw_cross(img_pixbuf->property_pixbuf());
129         draw_line();
130         cut_rightend();
131         exposeimg();
132 #endif
133         /* request video-frame from decoder in background thread */
134         http_get(video_frame_number);
135 }
136
137 void
138 VideoImageFrame::draw_line ()
139 {
140         Glib::RefPtr<Gdk::Pixbuf> img;
141         img = img_pixbuf->property_pixbuf();
142
143         int rowstride = img->get_rowstride();
144         int clip_height = img->get_height();
145         int n_channels = img->get_n_channels();
146         guchar *pixels, *p;
147         pixels = img->get_pixels();
148
149         int y;
150         for (y=0;y<clip_height;y++) {
151                 p = pixels + y * rowstride;
152                 p[0] = 255; p[1] = 255; p[2] = 255;
153                 if (n_channels>3) p[3] = 255;
154         }
155 }
156
157 void
158 VideoImageFrame::cut_rightend ()
159 {
160         if (rightend < 0 ) { return; }
161         Glib::RefPtr<Gdk::Pixbuf> img;
162         img = img_pixbuf->property_pixbuf();
163
164         int rowstride = img->get_rowstride();
165         int clip_height = img->get_height();
166         int clip_width = img->get_width();
167         int n_channels = img->get_n_channels();
168         guchar *pixels, *p;
169         pixels = img->get_pixels();
170         if (rightend > clip_width) { return; }
171
172         int x,y;
173         for (y=0;y<clip_height;++y) {
174                 p = pixels + y * rowstride + rightend * n_channels;
175                 p[0] = 192; p[1] = 127; p[2] = 127;
176                 if (n_channels>3) p[3] = 255;
177                 for (x=rightend+1; x<clip_width; ++x) {
178                         p = pixels + y * rowstride + x * n_channels;
179                         p[0] = 0; p[1] = 0; p[2] = 0;
180                         if (n_channels>3) p[3] = 0;
181                 }
182         }
183 }
184
185 void *
186 http_get_thread (void *arg) {
187         VideoImageFrame *vif = static_cast<VideoImageFrame *>(arg);
188         char url[2048];
189         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
190         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
191         snprintf(url, sizeof(url), "%s?frame=%li&w=%d&h=%di&file=%s&format=rgb",
192           vif->get_video_server_url().c_str(),
193           (long int) vif->get_req_frame(), vif->get_width(), vif->get_height(),
194           vif->get_video_filename().c_str()
195         );
196         int status = 0;
197         int timeout = 1000; // * 5ms -> 5sec
198         char *res = NULL;
199         do {
200                 res=a3_curl_http_get(url, &status);
201                 if (status == 503) Glib::usleep(5000); // try-again
202         } while (status == 503 && --timeout > 0);
203
204         if (status != 200 || !res) {
205                 printf("no-video frame: video-server returned http-status: %d\n", status);
206         }
207
208         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
209         vif->http_download_done(res);
210         pthread_exit(0);
211         return 0;
212 }
213
214 void
215 VideoImageFrame::http_download_done (char *data){
216         if (queued_request) {
217                 http_get_again(want_video_frame_number);
218                 return;
219         }
220
221         if (!data) {
222                 /* Image request failed (HTTP error or timeout) */
223                 Glib::RefPtr<Gdk::Pixbuf> img;
224                 img = img_pixbuf->property_pixbuf();
225                 img->fill(RGBA_TO_UINT(128,0,0,255));
226                 video_draw_cross(img_pixbuf->property_pixbuf());
227                 cut_rightend();
228                 draw_line();
229                 cut_rightend();
230                 /* TODO: mark as invalid:
231                  * video_frame_number = -1;
232                  * TODO: but prevent live-loops when calling update again
233                  */
234         } else {
235                 Glib::RefPtr<Gdk::Pixbuf> tmp, img;
236 #if 0 // RGBA
237                 tmp = Gdk::Pixbuf::create_from_data ((guint8*) data, Gdk::COLORSPACE_RGB, true, 8, clip_width, clip_height, clip_width*4);
238 #else // RGB
239                 tmp = Gdk::Pixbuf::create_from_data ((guint8*) data, Gdk::COLORSPACE_RGB, false, 8, clip_width, clip_height, clip_width*3);
240 #endif
241                 img = img_pixbuf->property_pixbuf();
242                 tmp->copy_area (0, 0, clip_width, clip_height, img, 0, 0);
243                 free(data);
244                 draw_line();
245                 cut_rightend();
246         }
247
248         exposeimg();
249         /* don't request frames too quickly, wait after user has zoomed */
250         Glib::usleep(40000);
251
252         if (queued_request) {
253                 http_get_again(want_video_frame_number);
254         }
255         pthread_mutex_unlock(&request_lock);
256 }
257
258
259 void
260 VideoImageFrame::http_get(framepos_t fn) {
261         if (pthread_mutex_trylock(&request_lock)) {
262                 /* remember last request and schedule after the lock has been released. */
263                 pthread_mutex_lock(&queue_lock);
264                 queued_request=true;
265                 want_video_frame_number=fn;
266                 pthread_mutex_unlock(&queue_lock);
267 #if 0
268                 /* TODO: cancel request and start a new one
269                  *  but only if we're waiting for curl request.
270                  *  don't interrupt http_download_done()
271                  *
272                  *  This should work, but requires testing:
273                  */
274                 if (!pthread_cancel(thread_id_tt)) {
275                         pthread_mutex_unlock(&request_lock);
276                 } else return;
277 #else
278                 return;
279 #endif
280         }
281         if (thread_active) pthread_join(thread_id_tt, NULL);
282         pthread_mutex_lock(&queue_lock);
283         queued_request=false;
284         req_video_frame_number=fn;
285         pthread_mutex_unlock(&queue_lock);
286         int rv = pthread_create(&thread_id_tt, NULL, http_get_thread, this);
287         thread_active=true;
288         if (rv) {
289                 thread_active=false;
290                 printf("thread creation failed. %i\n",rv);
291                 http_download_done(NULL);
292         }
293 }
294
295 void
296 VideoImageFrame::http_get_again(framepos_t /*fn*/) {
297         pthread_mutex_lock(&queue_lock);
298         queued_request=false;
299         req_video_frame_number=want_video_frame_number;
300         pthread_mutex_unlock(&queue_lock);
301
302         http_get_thread(this);
303 }
304