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