2013-03-12 17:00:09 -04:00
|
|
|
/*
|
2019-08-02 17:26:43 -04:00
|
|
|
* Copyright (C) 2013-2015 Tim Mayberry <mojofunk@gmail.com>
|
|
|
|
* Copyright (C) 2013-2018 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2013-2018 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2013-03-12 17:00:09 -04:00
|
|
|
#include <sigc++/bind.h>
|
|
|
|
#include "ardour/tempo.h"
|
|
|
|
|
|
|
|
#include <gtkmm2ext/utils.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2016-07-18 10:10:35 -04:00
|
|
|
#include "canvas/container.h"
|
|
|
|
|
|
|
|
#include "ardour_http.h"
|
|
|
|
#include "public_editor.h"
|
|
|
|
#include "utils_videotl.h"
|
|
|
|
#include "video_image_frame.h"
|
|
|
|
|
2016-07-14 14:44:52 -04:00
|
|
|
#include "pbd/i18n.h"
|
2013-03-12 17:00:09 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ARDOUR;
|
2013-08-03 10:54:57 -04:00
|
|
|
using namespace VideoUtils;
|
2013-03-12 17:00:09 -04:00
|
|
|
|
2013-06-21 17:14:53 -04:00
|
|
|
static void freedata_cb (uint8_t *d, void* /*arg*/) {
|
2013-04-17 14:26:09 -04:00
|
|
|
/* later this can be used with libharvid
|
|
|
|
* the buffer/videocacheline instead of freeing it
|
|
|
|
*/
|
|
|
|
free (d);
|
|
|
|
}
|
|
|
|
|
2014-06-22 11:41:05 -04:00
|
|
|
VideoImageFrame::VideoImageFrame (PublicEditor& ed, ArdourCanvas::Container& parent, int w, int h, std::string vsurl, std::string vfn)
|
2013-03-12 17:00:09 -04:00
|
|
|
: editor (ed)
|
|
|
|
, _parent(&parent)
|
|
|
|
, clip_width(w)
|
|
|
|
, clip_height(h)
|
|
|
|
, video_server_url(vsurl)
|
|
|
|
, video_filename(vfn)
|
|
|
|
{
|
|
|
|
pthread_mutex_init(&request_lock, NULL);
|
|
|
|
pthread_mutex_init(&queue_lock, NULL);
|
|
|
|
queued_request=false;
|
|
|
|
video_frame_number = -1;
|
|
|
|
rightend = -1;
|
2013-04-16 21:34:54 -04:00
|
|
|
sample_position = 0;
|
2013-03-12 17:00:09 -04:00
|
|
|
thread_active=false;
|
|
|
|
|
2013-04-16 21:34:54 -04:00
|
|
|
unit_position = editor.sample_to_pixel (sample_position);
|
2013-04-16 17:28:53 -04:00
|
|
|
image = new ArdourCanvas::Image (_parent, Cairo::FORMAT_ARGB32, clip_width, clip_height);
|
2013-03-12 17:00:09 -04:00
|
|
|
|
2013-04-16 16:57:19 -04:00
|
|
|
img = image->get_image();
|
2017-12-19 13:14:33 -05:00
|
|
|
fill_frame (0, 0, 0);
|
2013-03-12 17:00:09 -04:00
|
|
|
draw_line();
|
2013-04-16 16:57:19 -04:00
|
|
|
draw_x();
|
|
|
|
image->put_image(img);
|
2013-03-12 17:00:09 -04:00
|
|
|
|
2013-04-16 17:28:53 -04:00
|
|
|
image->Event.connect (sigc::bind (sigc::mem_fun (editor, &PublicEditor::canvas_videotl_bar_event), _parent));
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoImageFrame::~VideoImageFrame ()
|
|
|
|
{
|
|
|
|
if (thread_active) pthread_join(thread_id_tt, NULL);
|
2013-04-16 16:57:19 -04:00
|
|
|
delete image;
|
2013-03-12 17:00:09 -04:00
|
|
|
pthread_mutex_destroy(&request_lock);
|
|
|
|
pthread_mutex_destroy(&queue_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-18 12:39:17 -04:00
|
|
|
VideoImageFrame::set_position (samplepos_t sample)
|
2013-03-12 17:00:09 -04:00
|
|
|
{
|
2013-04-16 21:39:24 -04:00
|
|
|
double new_unit_position = editor.sample_to_pixel (sample);
|
2013-04-16 17:28:53 -04:00
|
|
|
image->move (ArdourCanvas::Duple (new_unit_position - unit_position, 0.0));
|
2013-04-16 21:34:54 -04:00
|
|
|
sample_position = sample;
|
2013-03-12 17:00:09 -04:00
|
|
|
unit_position = new_unit_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoImageFrame::reposition ()
|
|
|
|
{
|
2013-04-16 21:34:54 -04:00
|
|
|
set_position (sample_position);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-16 16:57:19 -04:00
|
|
|
VideoImageFrame::exposeimg () {
|
2013-03-12 17:00:09 -04:00
|
|
|
ImgChanged(); /* EMIT SIGNAL */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-18 12:39:17 -04:00
|
|
|
VideoImageFrame::set_videoframe (samplepos_t videoframenumber, int re)
|
2013-03-12 17:00:09 -04:00
|
|
|
{
|
|
|
|
if (video_frame_number == videoframenumber && rightend == re) return;
|
|
|
|
|
|
|
|
video_frame_number = videoframenumber;
|
|
|
|
rightend = re;
|
2013-04-16 16:57:19 -04:00
|
|
|
|
|
|
|
img = image->get_image();
|
2017-12-19 13:14:33 -05:00
|
|
|
fill_frame (0, 0, 0);
|
2013-04-16 16:57:19 -04:00
|
|
|
draw_x();
|
2013-03-12 17:00:09 -04:00
|
|
|
draw_line();
|
|
|
|
cut_rightend();
|
2013-04-16 16:57:19 -04:00
|
|
|
image->put_image(img);
|
2013-03-12 17:00:09 -04:00
|
|
|
exposeimg();
|
2013-04-16 16:57:19 -04:00
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
/* request video-frame from decoder in background thread */
|
2017-12-19 13:14:33 -05:00
|
|
|
http_get (video_frame_number);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoImageFrame::draw_line ()
|
|
|
|
{
|
2013-04-16 16:57:19 -04:00
|
|
|
const int rowstride = img->stride;
|
|
|
|
const int clip_height = img->height;
|
|
|
|
uint8_t *pixels, *p;
|
2013-04-17 14:26:09 -04:00
|
|
|
pixels = img->data;
|
2013-03-12 17:00:09 -04:00
|
|
|
|
|
|
|
int y;
|
2013-04-16 16:57:19 -04:00
|
|
|
for (y = 0;y < clip_height; y++) {
|
2013-03-12 17:00:09 -04:00
|
|
|
p = pixels + y * rowstride;
|
2013-04-16 16:57:19 -04:00
|
|
|
p[0] = 255; p[1] = 255; p[2] = 255; p[3] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-19 13:14:33 -05:00
|
|
|
VideoImageFrame::fill_frame (const uint8_t r, const uint8_t g, const uint8_t b)
|
2013-04-16 16:57:19 -04:00
|
|
|
{
|
|
|
|
const int rowstride = img->stride;
|
|
|
|
const int clip_height = img->height;
|
|
|
|
const int clip_width = img->width;
|
|
|
|
uint8_t *pixels, *p;
|
2013-04-17 14:26:09 -04:00
|
|
|
pixels = img->data;
|
2013-04-16 16:57:19 -04:00
|
|
|
|
|
|
|
int x,y;
|
|
|
|
for (y = 0; y < clip_height; ++y) {
|
|
|
|
for (x = 0; x < clip_width; ++x) {
|
|
|
|
p = pixels + y * rowstride + x * 4;
|
|
|
|
p[0] = b; p[1] = g; p[2] = r; p[3] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoImageFrame::draw_x ()
|
|
|
|
{
|
|
|
|
int x,y;
|
|
|
|
const int rowstride = img->stride;
|
|
|
|
const int clip_width = img->width;
|
|
|
|
const int clip_height = img->height;
|
|
|
|
uint8_t *pixels, *p;
|
2013-04-17 14:26:09 -04:00
|
|
|
pixels = img->data;
|
2013-04-16 16:57:19 -04:00
|
|
|
|
|
|
|
for (x = 0;x < clip_width; x++) {
|
|
|
|
y = clip_height * x / clip_width;
|
|
|
|
p = pixels + y * rowstride + x * 4;
|
|
|
|
p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
|
|
|
|
p = pixels + y * rowstride + (clip_width-x-1) * 4;
|
|
|
|
p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoImageFrame::cut_rightend ()
|
|
|
|
{
|
2013-04-16 16:57:19 -04:00
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
if (rightend < 0 ) { return; }
|
2013-04-16 16:57:19 -04:00
|
|
|
|
|
|
|
const int rowstride = img->stride;
|
|
|
|
const int clip_height = img->height;
|
|
|
|
const int clip_width = img->width;
|
|
|
|
uint8_t *pixels, *p;
|
2013-04-17 14:26:09 -04:00
|
|
|
pixels = img->data;
|
2013-03-12 17:00:09 -04:00
|
|
|
if (rightend > clip_width) { return; }
|
|
|
|
|
|
|
|
int x,y;
|
2013-04-16 16:57:19 -04:00
|
|
|
for (y = 0;y < clip_height; ++y) {
|
|
|
|
p = pixels + y * rowstride + rightend * 4;
|
|
|
|
p[0] = 192; p[1] = 192; p[2] = 192; p[3] = 255;
|
|
|
|
for (x=rightend+1; x < clip_width; ++x) {
|
|
|
|
p = pixels + y * rowstride + x * 4;
|
|
|
|
p[0] = 0; p[1] = 0; p[2] = 0; p[3] = 0;
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 20:02:46 -04:00
|
|
|
static void *
|
2013-03-12 17:00:09 -04:00
|
|
|
http_get_thread (void *arg) {
|
|
|
|
VideoImageFrame *vif = static_cast<VideoImageFrame *>(arg);
|
|
|
|
char url[2048];
|
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
|
|
|
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
|
2017-12-19 13:14:53 -05:00
|
|
|
snprintf(url, sizeof(url), "%s?frame=%li&w=%d&h=%d&file=%s&format=bgra",
|
2013-03-12 17:00:09 -04:00
|
|
|
vif->get_video_server_url().c_str(),
|
2017-12-19 13:14:33 -05:00
|
|
|
(long int) vif->get_req_frame(), vif->get_width(), vif->get_height(),
|
2013-03-12 17:00:09 -04:00
|
|
|
vif->get_video_filename().c_str()
|
|
|
|
);
|
2013-03-18 19:48:37 -04:00
|
|
|
int status = 0;
|
2013-03-26 09:11:52 -04:00
|
|
|
int timeout = 1000; // * 5ms -> 5sec
|
2013-03-18 19:48:37 -04:00
|
|
|
char *res = NULL;
|
|
|
|
do {
|
2018-06-02 13:24:17 -04:00
|
|
|
res = ArdourCurl::http_get (url, &status, false);
|
2013-08-29 19:43:44 -04:00
|
|
|
if (status == 503) Glib::usleep(5000); // try-again
|
2013-03-18 19:48:37 -04:00
|
|
|
} while (status == 503 && --timeout > 0);
|
|
|
|
|
2013-03-26 09:11:52 -04:00
|
|
|
if (status != 200 || !res) {
|
2013-03-26 09:48:15 -04:00
|
|
|
printf("no-video frame: video-server returned http-status: %d\n", status);
|
2013-03-26 09:11:52 -04:00
|
|
|
}
|
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
|
|
|
vif->http_download_done(res);
|
|
|
|
pthread_exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VideoImageFrame::http_download_done (char *data){
|
|
|
|
if (queued_request) {
|
|
|
|
http_get_again(want_video_frame_number);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
/* Image request failed (HTTP error or timeout) */
|
2013-04-16 16:57:19 -04:00
|
|
|
img = image->get_image();
|
2017-12-19 13:14:33 -05:00
|
|
|
fill_frame (128, 0, 0);
|
2013-04-16 16:57:19 -04:00
|
|
|
draw_x();
|
2013-03-12 17:00:09 -04:00
|
|
|
cut_rightend();
|
|
|
|
draw_line();
|
|
|
|
cut_rightend();
|
2013-04-16 16:57:19 -04:00
|
|
|
image->put_image(img);
|
2013-03-12 17:00:09 -04:00
|
|
|
} else {
|
2013-04-17 14:26:09 -04:00
|
|
|
img = image->get_image(false);
|
|
|
|
img->data = (uint8_t*) data;
|
|
|
|
img->destroy_callback = &freedata_cb;
|
2013-03-12 17:00:09 -04:00
|
|
|
draw_line();
|
|
|
|
cut_rightend();
|
2013-04-16 16:57:19 -04:00
|
|
|
image->put_image(img);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
exposeimg();
|
2018-01-21 05:27:26 -05:00
|
|
|
/* don't request frames too quickly, wait after user has zoomed */
|
2013-07-19 04:46:52 -04:00
|
|
|
Glib::usleep(40000);
|
2013-03-12 17:00:09 -04:00
|
|
|
|
|
|
|
if (queued_request) {
|
|
|
|
http_get_again(want_video_frame_number);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&request_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-12-19 13:14:33 -05:00
|
|
|
VideoImageFrame::http_get (samplepos_t fn) {
|
2013-03-12 17:00:09 -04:00
|
|
|
if (pthread_mutex_trylock(&request_lock)) {
|
|
|
|
pthread_mutex_lock(&queue_lock);
|
|
|
|
queued_request=true;
|
|
|
|
want_video_frame_number=fn;
|
|
|
|
pthread_mutex_unlock(&queue_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (thread_active) pthread_join(thread_id_tt, NULL);
|
|
|
|
pthread_mutex_lock(&queue_lock);
|
|
|
|
queued_request=false;
|
|
|
|
req_video_frame_number=fn;
|
|
|
|
pthread_mutex_unlock(&queue_lock);
|
2024-09-27 17:03:26 -04:00
|
|
|
/* do not use `pbd pthread_create_and_store' here.
|
|
|
|
* The request thread uses aync cancellation */
|
2013-03-12 17:00:09 -04:00
|
|
|
int rv = pthread_create(&thread_id_tt, NULL, http_get_thread, this);
|
|
|
|
thread_active=true;
|
|
|
|
if (rv) {
|
|
|
|
thread_active=false;
|
|
|
|
printf("thread creation failed. %i\n",rv);
|
|
|
|
http_download_done(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-18 12:39:17 -04:00
|
|
|
VideoImageFrame::http_get_again(samplepos_t /*fn*/) {
|
2013-03-12 17:00:09 -04:00
|
|
|
pthread_mutex_lock(&queue_lock);
|
|
|
|
queued_request=false;
|
|
|
|
req_video_frame_number=want_video_frame_number;
|
|
|
|
pthread_mutex_unlock(&queue_lock);
|
|
|
|
|
|
|
|
http_get_thread(this);
|
|
|
|
}
|
|
|
|
|