2 Copyright (C) 2010, 2013 Paul Davis
3 Author: Robin Gareus <robin@gareus.org>
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.
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.
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.
20 #include <sigc++/bind.h>
21 #include "ardour/tempo.h"
23 #include "video_image_frame.h"
24 #include "public_editor.h"
25 #include "canvas/container.h"
26 #include "utils_videotl.h"
28 #include <gtkmm2ext/utils.h>
34 using namespace ARDOUR;
35 using namespace VideoUtils;
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
44 VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Container& parent, int w, int h, std::string vsurl, std::string vfn)
49 , video_server_url(vsurl)
52 pthread_mutex_init(&request_lock, NULL);
53 pthread_mutex_init(&queue_lock, NULL);
55 video_frame_number = -1;
60 unit_position = editor.sample_to_pixel (sample_position);
61 image = new ArdourCanvas::Image (_parent, Cairo::FORMAT_ARGB32, clip_width, clip_height);
63 img = image->get_image();
67 image->put_image(img);
69 image->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
72 VideoImageFrame::~VideoImageFrame ()
74 if (thread_active) pthread_join(thread_id_tt, NULL);
76 pthread_mutex_destroy(&request_lock);
77 pthread_mutex_destroy(&queue_lock);
81 VideoImageFrame::set_position (framepos_t sample)
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;
90 VideoImageFrame::reposition ()
92 set_position (sample_position);
96 VideoImageFrame::exposeimg () {
97 ImgChanged(); /* EMIT SIGNAL */
101 VideoImageFrame::set_videoframe (framepos_t videoframenumber, int re)
103 if (video_frame_number == videoframenumber && rightend == re) return;
105 video_frame_number = videoframenumber;
108 img = image->get_image();
113 image->put_image(img);
116 /* request video-frame from decoder in background thread */
117 http_get(video_frame_number);
121 VideoImageFrame::draw_line ()
123 const int rowstride = img->stride;
124 const int clip_height = img->height;
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;
136 VideoImageFrame::fill_frame (const uint8_t r, const uint8_t g, const uint8_t b)
138 const int rowstride = img->stride;
139 const int clip_height = img->height;
140 const int clip_width = img->width;
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;
154 VideoImageFrame::draw_x ()
157 const int rowstride = img->stride;
158 const int clip_width = img->width;
159 const int clip_height = img->height;
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;
173 VideoImageFrame::cut_rightend ()
176 if (rightend < 0 ) { return; }
178 const int rowstride = img->stride;
179 const int clip_height = img->height;
180 const int clip_width = img->width;
183 if (rightend > clip_width) { return; }
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;
197 http_get_thread (void *arg) {
198 VideoImageFrame *vif = static_cast<VideoImageFrame *>(arg);
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()
208 int timeout = 1000; // * 5ms -> 5sec
211 res=a3_curl_http_get(url, &status);
212 if (status == 503) Glib::usleep(5000); // try-again
213 } while (status == 503 && --timeout > 0);
215 if (status != 200 || !res) {
216 printf("no-video frame: video-server returned http-status: %d\n", status);
219 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
220 vif->http_download_done(res);
226 VideoImageFrame::http_download_done (char *data){
227 if (queued_request) {
228 http_get_again(want_video_frame_number);
233 /* Image request failed (HTTP error or timeout) */
234 img = image->get_image();
235 fill_frame(128, 0, 0);
240 image->put_image(img);
242 img = image->get_image(false);
243 img->data = (uint8_t*) data;
244 img->destroy_callback = &freedata_cb;
247 image->put_image(img);
251 /* don't request frames too quickly, wait after user has zoomed */
254 if (queued_request) {
255 http_get_again(want_video_frame_number);
257 pthread_mutex_unlock(&request_lock);
262 VideoImageFrame::http_get(framepos_t fn) {
263 if (pthread_mutex_trylock(&request_lock)) {
264 pthread_mutex_lock(&queue_lock);
266 want_video_frame_number=fn;
267 pthread_mutex_unlock(&queue_lock);
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);
279 printf("thread creation failed. %i\n",rv);
280 http_download_done(NULL);
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);
291 http_get_thread(this);