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"
26 #include "canvas/container.h"
27 #include "utils_videotl.h"
29 #include <gtkmm2ext/utils.h>
35 using namespace ARDOUR;
36 using namespace VideoUtils;
38 static void freedata_cb (uint8_t *d, void* /*arg*/) {
39 /* later this can be used with libharvid
40 * the buffer/videocacheline instead of freeing it
45 VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Container& parent, int w, int h, std::string vsurl, std::string vfn)
50 , video_server_url(vsurl)
53 pthread_mutex_init(&request_lock, NULL);
54 pthread_mutex_init(&queue_lock, NULL);
56 video_frame_number = -1;
61 unit_position = editor.sample_to_pixel (sample_position);
62 image = new ArdourCanvas::Image (_parent, Cairo::FORMAT_ARGB32, clip_width, clip_height);
64 img = image->get_image();
68 image->put_image(img);
70 image->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
73 VideoImageFrame::~VideoImageFrame ()
75 if (thread_active) pthread_join(thread_id_tt, NULL);
77 pthread_mutex_destroy(&request_lock);
78 pthread_mutex_destroy(&queue_lock);
82 VideoImageFrame::set_position (framepos_t sample)
84 double new_unit_position = editor.sample_to_pixel (sample);
85 image->move (ArdourCanvas::Duple (new_unit_position - unit_position, 0.0));
86 sample_position = sample;
87 unit_position = new_unit_position;
91 VideoImageFrame::reposition ()
93 set_position (sample_position);
97 VideoImageFrame::exposeimg () {
98 ImgChanged(); /* EMIT SIGNAL */
102 VideoImageFrame::set_videoframe (framepos_t videoframenumber, int re)
104 if (video_frame_number == videoframenumber && rightend == re) return;
106 video_frame_number = videoframenumber;
109 img = image->get_image();
114 image->put_image(img);
117 /* request video-frame from decoder in background thread */
118 http_get(video_frame_number);
122 VideoImageFrame::draw_line ()
124 const int rowstride = img->stride;
125 const int clip_height = img->height;
130 for (y = 0;y < clip_height; y++) {
131 p = pixels + y * rowstride;
132 p[0] = 255; p[1] = 255; p[2] = 255; p[3] = 255;
137 VideoImageFrame::fill_frame (const uint8_t r, const uint8_t g, const uint8_t b)
139 const int rowstride = img->stride;
140 const int clip_height = img->height;
141 const int clip_width = img->width;
146 for (y = 0; y < clip_height; ++y) {
147 for (x = 0; x < clip_width; ++x) {
148 p = pixels + y * rowstride + x * 4;
149 p[0] = b; p[1] = g; p[2] = r; p[3] = 255;
155 VideoImageFrame::draw_x ()
158 const int rowstride = img->stride;
159 const int clip_width = img->width;
160 const int clip_height = img->height;
164 for (x = 0;x < clip_width; x++) {
165 y = clip_height * x / clip_width;
166 p = pixels + y * rowstride + x * 4;
167 p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
168 p = pixels + y * rowstride + (clip_width-x-1) * 4;
169 p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
174 VideoImageFrame::cut_rightend ()
177 if (rightend < 0 ) { return; }
179 const int rowstride = img->stride;
180 const int clip_height = img->height;
181 const int clip_width = img->width;
184 if (rightend > clip_width) { return; }
187 for (y = 0;y < clip_height; ++y) {
188 p = pixels + y * rowstride + rightend * 4;
189 p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
190 for (x=rightend+1; x < clip_width; ++x) {
191 p = pixels + y * rowstride + x * 4;
192 p[0] = 0; p[1] = 0; p[2] = 0; p[3] = 0;
198 http_get_thread (void *arg) {
199 VideoImageFrame *vif = static_cast<VideoImageFrame *>(arg);
201 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
202 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
203 snprintf(url, sizeof(url), "%s?frame=%li&w=%d&h=%d&file=%s&format=bgra",
204 vif->get_video_server_url().c_str(),
205 (long int) vif->get_req_frame(), vif->get_width(), vif->get_height(),
206 vif->get_video_filename().c_str()
209 int timeout = 1000; // * 5ms -> 5sec
212 res=a3_curl_http_get(url, &status);
213 if (status == 503) Glib::usleep(5000); // try-again
214 } while (status == 503 && --timeout > 0);
216 if (status != 200 || !res) {
217 printf("no-video frame: video-server returned http-status: %d\n", status);
220 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
221 vif->http_download_done(res);
227 VideoImageFrame::http_download_done (char *data){
228 if (queued_request) {
229 http_get_again(want_video_frame_number);
234 /* Image request failed (HTTP error or timeout) */
235 img = image->get_image();
236 fill_frame(128, 0, 0);
241 image->put_image(img);
243 img = image->get_image(false);
244 img->data = (uint8_t*) data;
245 img->destroy_callback = &freedata_cb;
248 image->put_image(img);
252 /* don't request frames too quickly, wait after user has zoomed */
255 if (queued_request) {
256 http_get_again(want_video_frame_number);
258 pthread_mutex_unlock(&request_lock);
263 VideoImageFrame::http_get(framepos_t fn) {
264 if (pthread_mutex_trylock(&request_lock)) {
265 pthread_mutex_lock(&queue_lock);
267 want_video_frame_number=fn;
268 pthread_mutex_unlock(&queue_lock);
271 if (thread_active) pthread_join(thread_id_tt, NULL);
272 pthread_mutex_lock(&queue_lock);
273 queued_request=false;
274 req_video_frame_number=fn;
275 pthread_mutex_unlock(&queue_lock);
276 int rv = pthread_create(&thread_id_tt, NULL, http_get_thread, this);
280 printf("thread creation failed. %i\n",rv);
281 http_download_done(NULL);
286 VideoImageFrame::http_get_again(framepos_t /*fn*/) {
287 pthread_mutex_lock(&queue_lock);
288 queued_request=false;
289 req_video_frame_number=want_video_frame_number;
290 pthread_mutex_unlock(&queue_lock);
292 http_get_thread(this);