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 "ardour_ui.h"
24 #include "video_image_frame.h"
25 #include "public_editor.h"
27 #include "canvas/group.h"
28 #include "utils_videotl.h"
30 #include <gtkmm2ext/utils.h>
36 using namespace ARDOUR;
37 using namespace VideoUtils;
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
46 VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Group& parent, int w, int h, std::string vsurl, std::string vfn)
51 , video_server_url(vsurl)
54 pthread_mutex_init(&request_lock, NULL);
55 pthread_mutex_init(&queue_lock, NULL);
57 video_frame_number = -1;
62 unit_position = editor.sample_to_pixel (sample_position);
63 image = new ArdourCanvas::Image (_parent, Cairo::FORMAT_ARGB32, clip_width, clip_height);
65 img = image->get_image();
69 image->put_image(img);
71 image->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
74 VideoImageFrame::~VideoImageFrame ()
76 if (thread_active) pthread_join(thread_id_tt, NULL);
78 pthread_mutex_destroy(&request_lock);
79 pthread_mutex_destroy(&queue_lock);
83 VideoImageFrame::set_position (framepos_t sample)
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;
92 VideoImageFrame::reposition ()
94 set_position (sample_position);
98 VideoImageFrame::exposeimg () {
99 ImgChanged(); /* EMIT SIGNAL */
103 VideoImageFrame::set_videoframe (framepos_t videoframenumber, int re)
105 if (video_frame_number == videoframenumber && rightend == re) return;
107 video_frame_number = videoframenumber;
110 img = image->get_image();
115 image->put_image(img);
118 /* request video-frame from decoder in background thread */
119 http_get(video_frame_number);
123 VideoImageFrame::draw_line ()
125 const int rowstride = img->stride;
126 const int clip_height = img->height;
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;
138 VideoImageFrame::fill_frame (const uint8_t r, const uint8_t g, const uint8_t b)
140 const int rowstride = img->stride;
141 const int clip_height = img->height;
142 const int clip_width = img->width;
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;
156 VideoImageFrame::draw_x ()
159 const int rowstride = img->stride;
160 const int clip_width = img->width;
161 const int clip_height = img->height;
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;
175 VideoImageFrame::cut_rightend ()
178 if (rightend < 0 ) { return; }
180 const int rowstride = img->stride;
181 const int clip_height = img->height;
182 const int clip_width = img->width;
185 if (rightend > clip_width) { return; }
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;
199 http_get_thread (void *arg) {
200 VideoImageFrame *vif = static_cast<VideoImageFrame *>(arg);
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()
210 int timeout = 1000; // * 5ms -> 5sec
213 res=a3_curl_http_get(url, &status);
214 if (status == 503) Glib::usleep(5000); // try-again
215 } while (status == 503 && --timeout > 0);
217 if (status != 200 || !res) {
218 printf("no-video frame: video-server returned http-status: %d\n", status);
221 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
222 vif->http_download_done(res);
228 VideoImageFrame::http_download_done (char *data){
229 if (queued_request) {
230 http_get_again(want_video_frame_number);
235 /* Image request failed (HTTP error or timeout) */
236 img = image->get_image();
237 fill_frame(128, 0, 0);
242 image->put_image(img);
244 img = image->get_image(false);
245 img->data = (uint8_t*) data;
246 img->destroy_callback = &freedata_cb;
249 image->put_image(img);
253 /* don't request frames too quickly, wait after user has zoomed */
256 if (queued_request) {
257 http_get_again(want_video_frame_number);
259 pthread_mutex_unlock(&request_lock);
264 VideoImageFrame::http_get(framepos_t fn) {
265 if (pthread_mutex_trylock(&request_lock)) {
266 pthread_mutex_lock(&queue_lock);
268 want_video_frame_number=fn;
269 pthread_mutex_unlock(&queue_lock);
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);
281 printf("thread creation failed. %i\n",rv);
282 http_download_done(NULL);
287 VideoImageFrame::http_get_again(framepos_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);
293 http_get_thread(this);