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