enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 "video_image_frame.h"
24 #include "public_editor.h"
25 #include "canvas/container.h"
26 #include "utils_videotl.h"
27
28 #include <gtkmm2ext/utils.h>
29 #include <pthread.h>
30
31 #include "pbd/i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35 using namespace VideoUtils;
36
37 static void freedata_cb (uint8_t *d, void* /*arg*/) {
38         /* later this can be used with libharvid
39          * the buffer/videocacheline instead of freeing it
40          */
41         free (d);
42 }
43
44 VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Container& parent, int w, int h, std::string vsurl, std::string vfn)
45         : editor (ed)
46         , _parent(&parent)
47         , clip_width(w)
48         , clip_height(h)
49         , video_server_url(vsurl)
50         , video_filename(vfn)
51 {
52         pthread_mutex_init(&request_lock, NULL);
53         pthread_mutex_init(&queue_lock, NULL);
54         queued_request=false;
55         video_frame_number = -1;
56         rightend = -1;
57         sample_position = 0;
58         thread_active=false;
59
60         unit_position = editor.sample_to_pixel (sample_position);
61         image = new ArdourCanvas::Image (_parent, Cairo::FORMAT_ARGB32, clip_width, clip_height);
62
63         img = image->get_image();
64         fill_frame(0, 0, 0);
65         draw_line();
66         draw_x();
67         image->put_image(img);
68
69         image->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
70 }
71
72 VideoImageFrame::~VideoImageFrame ()
73 {
74         if (thread_active) pthread_join(thread_id_tt, NULL);
75         delete image;
76         pthread_mutex_destroy(&request_lock);
77         pthread_mutex_destroy(&queue_lock);
78 }
79
80 void
81 VideoImageFrame::set_position (framepos_t sample)
82 {
83         double new_unit_position = editor.sample_to_pixel (sample);
84         image->move (ArdourCanvas::Duple (new_unit_position - unit_position, 0.0));
85         sample_position = sample;
86         unit_position = new_unit_position;
87 }
88
89 void
90 VideoImageFrame::reposition ()
91 {
92         set_position (sample_position);
93 }
94
95 void
96 VideoImageFrame::exposeimg () {
97         ImgChanged(); /* EMIT SIGNAL */
98 }
99
100 void
101 VideoImageFrame::set_videoframe (framepos_t videoframenumber, int re)
102 {
103         if (video_frame_number == videoframenumber && rightend == re) return;
104
105         video_frame_number = videoframenumber;
106         rightend = re;
107
108         img = image->get_image();
109         fill_frame(0, 0, 0);
110         draw_x();
111         draw_line();
112         cut_rightend();
113         image->put_image(img);
114         exposeimg();
115
116         /* request video-frame from decoder in background thread */
117         http_get(video_frame_number);
118 }
119
120 void
121 VideoImageFrame::draw_line ()
122 {
123         const int rowstride = img->stride;
124         const int clip_height = img->height;
125         uint8_t *pixels, *p;
126         pixels = img->data;
127
128         int y;
129         for (y = 0;y < clip_height; y++) {
130                 p = pixels + y * rowstride;
131                 p[0] = 255; p[1] = 255; p[2] = 255; p[3] = 255;
132         }
133 }
134
135 void
136 VideoImageFrame::fill_frame (const uint8_t r, const uint8_t g, const uint8_t b)
137 {
138         const int rowstride = img->stride;
139         const int clip_height = img->height;
140         const int clip_width = img->width;
141         uint8_t *pixels, *p;
142         pixels = img->data;
143
144         int x,y;
145         for (y = 0; y < clip_height; ++y) {
146                 for (x = 0; x < clip_width; ++x) {
147                         p = pixels + y * rowstride + x * 4;
148                         p[0] = b; p[1] = g; p[2] = r; p[3] = 255;
149                 }
150         }
151 }
152
153 void
154 VideoImageFrame::draw_x ()
155 {
156         int x,y;
157         const int rowstride = img->stride;
158         const int clip_width = img->width;
159         const int clip_height = img->height;
160         uint8_t *pixels, *p;
161         pixels = img->data;
162
163         for (x = 0;x < clip_width; x++) {
164                 y = clip_height * x / clip_width;
165                 p = pixels + y * rowstride + x * 4;
166                 p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
167                 p = pixels + y * rowstride + (clip_width-x-1) * 4;
168                 p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
169         }
170 }
171
172 void
173 VideoImageFrame::cut_rightend ()
174 {
175
176         if (rightend < 0 ) { return; }
177
178         const int rowstride = img->stride;
179         const int clip_height = img->height;
180         const int clip_width = img->width;
181         uint8_t *pixels, *p;
182         pixels = img->data;
183         if (rightend > clip_width) { return; }
184
185         int x,y;
186         for (y = 0;y < clip_height; ++y) {
187                 p = pixels + y * rowstride + rightend * 4;
188                 p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
189                 for (x=rightend+1; x < clip_width; ++x) {
190                         p = pixels + y * rowstride + x * 4;
191                         p[0] = 0; p[1] = 0; p[2] = 0; p[3] = 0;
192                 }
193         }
194 }
195
196 static void *
197 http_get_thread (void *arg) {
198         VideoImageFrame *vif = static_cast<VideoImageFrame *>(arg);
199         char url[2048];
200         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
201         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
202         snprintf(url, sizeof(url), "%s?frame=%li&w=%d&h=%d&file=%s&format=bgra",
203           vif->get_video_server_url().c_str(),
204           (long int) vif->get_req_frame(), vif->get_width(), vif->get_height(),
205           vif->get_video_filename().c_str()
206         );
207         int status = 0;
208         int timeout = 1000; // * 5ms -> 5sec
209         char *res = NULL;
210         do {
211                 res=a3_curl_http_get(url, &status);
212                 if (status == 503) Glib::usleep(5000); // try-again
213         } while (status == 503 && --timeout > 0);
214
215         if (status != 200 || !res) {
216                 printf("no-video frame: video-server returned http-status: %d\n", status);
217         }
218
219         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
220         vif->http_download_done(res);
221         pthread_exit(0);
222         return 0;
223 }
224
225 void
226 VideoImageFrame::http_download_done (char *data){
227         if (queued_request) {
228                 http_get_again(want_video_frame_number);
229                 return;
230         }
231
232         if (!data) {
233                 /* Image request failed (HTTP error or timeout) */
234                 img = image->get_image();
235                 fill_frame(128, 0, 0);
236                 draw_x();
237                 cut_rightend();
238                 draw_line();
239                 cut_rightend();
240                 image->put_image(img);
241         } else {
242                 img = image->get_image(false);
243                 img->data = (uint8_t*) data;
244                 img->destroy_callback = &freedata_cb;
245                 draw_line();
246                 cut_rightend();
247                 image->put_image(img);
248         }
249
250         exposeimg();
251         /* don't request frames too quickly, wait after user has zoomed */
252         Glib::usleep(40000);
253
254         if (queued_request) {
255                 http_get_again(want_video_frame_number);
256         }
257         pthread_mutex_unlock(&request_lock);
258 }
259
260
261 void
262 VideoImageFrame::http_get(framepos_t fn) {
263         if (pthread_mutex_trylock(&request_lock)) {
264                 pthread_mutex_lock(&queue_lock);
265                 queued_request=true;
266                 want_video_frame_number=fn;
267                 pthread_mutex_unlock(&queue_lock);
268                 return;
269         }
270         if (thread_active) pthread_join(thread_id_tt, NULL);
271         pthread_mutex_lock(&queue_lock);
272         queued_request=false;
273         req_video_frame_number=fn;
274         pthread_mutex_unlock(&queue_lock);
275         int rv = pthread_create(&thread_id_tt, NULL, http_get_thread, this);
276         thread_active=true;
277         if (rv) {
278                 thread_active=false;
279                 printf("thread creation failed. %i\n",rv);
280                 http_download_done(NULL);
281         }
282 }
283
284 void
285 VideoImageFrame::http_get_again(framepos_t /*fn*/) {
286         pthread_mutex_lock(&queue_lock);
287         queued_request=false;
288         req_video_frame_number=want_video_frame_number;
289         pthread_mutex_unlock(&queue_lock);
290
291         http_get_thread(this);
292 }
293